Skip to content
KernelIndex
Search⌘K

submission 122955

gup · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-vectorsum-v2-122955?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
Vector sum reductionsuite of 6 cases
NVIDIA A100
152.1µs
#59 of 96
2025-12-04

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:a18db875bf71604cdfbb5956dce8e04101bbd2e6e65dc316fda4e2359fd5f29b
license declaredunknown
license concludedunknown
authorsgup
imported2026-08-15

Techniques

Extracted from the mirrored source by pattern, never inferred. Each row cites its line.

num-warps = 8num_warps = 8 if BLOCK >= 2048 else 4
stages = 2_block_sum_kernel[grid](x, partial, n, BLOCK=BLOCK, num_warps=num_warps, num_stages=2)

Kernel source

submission.py74 lines
import math
from typing import Optional

import torch

try:
    import triton
    import triton.language as tl

    _HAS_TRITON = True
except Exception:
    _HAS_TRITON = False

from task import input_t, output_t


if _HAS_TRITON:

    @triton.jit
    def _block_sum_kernel(x_ptr, partial_ptr, n_elements, BLOCK: tl.constexpr):
        pid = tl.program_id(0)
        block_start = pid * BLOCK
        offsets = block_start + tl.arange(0, BLOCK)
        mask = offsets < n_elements
        x = tl.load(x_ptr + offsets, mask=mask, other=0.0)
        x = x.to(tl.float64)
        acc = tl.sum(x, axis=0)
        tl.store(partial_ptr + pid, acc)


def _triton_reduce(x: torch.Tensor) -> Optional[torch.Tensor]:
    """Fast two-pass reduction using Triton; returns None if Triton unavailable."""
    if not _HAS_TRITON:
        return None
    if not x.is_cuda:
        return None

    # Ensure contiguous for coalesced loads
    x = x.contiguous()
    n = x.numel()
    if n == 0:
        return torch.zeros((), device=x.device, dtype=torch.float32)

    BLOCK = 2048 if n >= (1 << 20) else 1024
    num_warps = 8 if BLOCK >= 2048 else 4
    grid = (triton.cdiv(n, BLOCK),)

    partial = torch.empty(grid[0], device=x.device, dtype=torch.float64)

    _block_sum_kernel[grid](x, partial, n, BLOCK=BLOCK, num_warps=num_warps, num_stages=2)

    # Final reduction with PyTorch on a small buffer; accumulate in float64.
    return torch.sum(partial, dtype=torch.float64).to(torch.float32)


def custom_kernel(data: input_t) -> output_t:
    """
    Sum all elements of the input vector. Accumulates in float64 to match the
    reference, then returns a 0-d float32 tensor. Uses a fast Triton two-pass
    reduction on CUDA; falls back to torch.sum otherwise.
    """
    x, out = data

    total = _triton_reduce(x)
    if total is None:
        total = torch.sum(x, dtype=torch.float64).to(torch.float32)

    try:
        out.view(-1)[0] = total
    except Exception:
        pass

    return total
scrolls · 74 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