Skip to content
KernelIndex
Search⌘K

submission 66267

SeanP · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

No package. Vendor the mirrored source: 71 lines, June 9 Researcher Reciprocity License v1.0.

prefixsum_v2.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-prefixsum-v2-66267?include=source"
interfacepython
Compatibility
measured onNVIDIA A100
declared hardwareNVIDIA A100
architecturessm_80
dtypesfp32

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
Inclusive prefix sumsuite of 11 cases
NVIDIA A100
7.20ms
#21 of 25
2025-10-25

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:bb46c24f1e2105055fdba8bc6f51a048642471ab415ff03c6ca5c8e60498e449
license declaredunknown
license concludedunknown
authorsSeanP
imported2026-08-15

Kernel source

prefixsum_v2.py71 lines
#!POPCORN leaderboard prefixsum_v2

# This is a submission template for popcorn leaderboard 'prefixsum_v2'.
# Your task is as follows:
# > Implement an inclusive prefix sum (scan) kernel that matches the reference implementation.
# > The kernel should compute the cumulative sum of all elements up to each position.
# > Because of numerical instability, the tolerance is scaled by the square root of the input size.
# > 
# > Input:
# > - `data`: A 1D tensor of size `n`
# > Output:
# > - `output`: A 1D tensor of size `n`
# The deadline for this leaderboard is 2025-12-30 00:00:00+00:00

# You can automatically route this file to specific GPUs by adding a line
# `#!POPCORN gpus <GPUs>` to the header of this file.
# Happy hacking!

from task import input_t, output_t


#def custom_kernel(data: input_t) -> output_t:
#    pass


from utils import match_reference, DeterministicContext
import torch
from task import input_t, output_t


def custom_kernel(data: input_t) -> output_t:
    """
    Reference implementation of inclusive prefix sum using PyTorch.
    Args:
        data: Input tensor to compute prefix sum on
    Returns:
        Tensor containing the inclusive prefix sum
    """
    with DeterministicContext():
        data, output = data
        output = torch.cumsum(data.to(torch.float64), dim=0).to(torch.float64)
        return output


def generate_input(size: int, seed: int) -> input_t:
    """
    Generates random input tensor.
    Returns:
        Tensor to compute prefix sum on
    """
    gen = torch.Generator(device="cuda")
    gen.manual_seed(seed)
    x = torch.randn(
        size, device="cuda", dtype=torch.float32, generator=gen
    ).contiguous()
    y = torch.empty(size, device="cuda", dtype=torch.float32).contiguous()
    return x, y


# This algorithm is very sensitive to the tolerance and the error is magnified by the input size
# The tolerance is scaled by the square root of the input size
def check_implementation(data: input_t, output: output_t) -> str:
    # Then get the size for scaling the tolerance
    n = data[0].numel()

    scale_factor = n ** 0.5  # Square root of input size
    rtol = 1e-5 * scale_factor
    atol = 1e-5 * scale_factor

    return match_reference(data, output, reference=ref_kernel, rtol=rtol, atol=atol)
scrolls · 71 lines total

Source code from GPU Mode and the KernelBot dataset · June 9 Researcher Reciprocity License v1.0

Best evidence level for this revision: reported

JSON