Skip to content
KernelIndex
Search⌘K

submission 780525

John · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission_v5.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-vectorsum-v2-780525?include=source"
interfacepython
Compatibility
measured onNVIDIA H100
declared hardwareNVIDIA H100
architecturessm_90
dtypesfp32

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
Vector sum reductionsuite of 6 cases
NVIDIA H100
81.3µs
#5 of 37
2026-05-02

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:1b2dbb8bdd0156107f0e31b77172076338740d973d59a3508c62a8e6a756211b
license declaredunknown
license concludedunknown
authorsJohn
imported2026-08-15

Techniques

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

shared-memory__shared__ float warp_sums[kWarpsPerBlock];
vector-width = float4const float4* x4 = reinterpret_cast<const float4*>(x);

Kernel source

submission_v5.py188 lines
#!POPCORN leaderboard vectorsum_v2

import hashlib
import os
import subprocess
import sys
from pathlib import Path

from task import input_t, output_t

from torch.utils.cpp_extension import CUDA_HOME, load_inline

subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "ninja"])

_extension_module = None

CPP_SRC = r"""
#include <torch/extension.h>

void run_vectorsum_cuda(torch::Tensor x, torch::Tensor out);

void run_vectorsum(torch::Tensor x, torch::Tensor out) {
  run_vectorsum_cuda(x, out);
}

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def("run_vectorsum", &run_vectorsum, "Vector sum CUDA launcher");
}
"""

CUDA_SRC = r"""
#include <cuda_runtime.h>
#include <torch/extension.h>

namespace {

constexpr int kThreadsPerBlock = 256;
constexpr int kWarpsPerBlock = kThreadsPerBlock / 32;
constexpr int kItemsPerThread = 32;
constexpr int kBlockItems = kThreadsPerBlock * kItemsPerThread;

__inline__ __device__ float warp_reduce_sum(float val) {
  for (int offset = 16; offset > 0; offset /= 2) {
    val += __shfl_down_sync(0xffffffff, val, offset);
  }
  return val;
}

__global__ __launch_bounds__(kThreadsPerBlock, 8) void reduce_tiles_aligned_kernel(
    const float* __restrict__ x,
    float* __restrict__ out) {
  const int tid = threadIdx.x;
  const int lane = tid & 31;
  const int warp = tid >> 5;
  const int tile_idx = blockIdx.x;

  __shared__ float warp_sums[kWarpsPerBlock];
  float acc = 0.0f;
  const float4* x4 = reinterpret_cast<const float4*>(x);

  const int vec_index = tile_idx * (kBlockItems / 4) + tid;
  float4 v0 = __ldcs(&x4[vec_index]);
  float4 v1 = __ldcs(&x4[vec_index + kThreadsPerBlock]);
  acc += v0.x + v0.y + v0.z + v0.w;
  acc += v1.x + v1.y + v1.z + v1.w;
  v0 = __ldcs(&x4[vec_index + 2 * kThreadsPerBlock]);
  v1 = __ldcs(&x4[vec_index + 3 * kThreadsPerBlock]);
  acc += v0.x + v0.y + v0.z + v0.w;
  acc += v1.x + v1.y + v1.z + v1.w;
  v0 = __ldcs(&x4[vec_index + 4 * kThreadsPerBlock]);
  v1 = __ldcs(&x4[vec_index + 5 * kThreadsPerBlock]);
  acc += v0.x + v0.y + v0.z + v0.w;
  acc += v1.x + v1.y + v1.z + v1.w;
  v0 = __ldcs(&x4[vec_index + 6 * kThreadsPerBlock]);
  v1 = __ldcs(&x4[vec_index + 7 * kThreadsPerBlock]);
  acc += v0.x + v0.y + v0.z + v0.w;
  acc += v1.x + v1.y + v1.z + v1.w;

  acc = warp_reduce_sum(acc);
  if (lane == 0) {
    warp_sums[warp] = acc;
  }
  __syncthreads();

  if (warp == 0) {
    float block_sum = (lane < kWarpsPerBlock) ? warp_sums[lane] : 0.0f;
    block_sum = warp_reduce_sum(block_sum);
    if (lane == 0) {
      atomicAdd(out, block_sum);
    }
  }
}

__global__ void reduce_tail_kernel(
    const float* __restrict__ x,
    float* __restrict__ out,
    int64_t tail_start,
    int64_t n_elements) {
  const int tid = threadIdx.x;
  const int lane = tid & 31;
  const int warp = tid >> 5;

  __shared__ float warp_sums[kWarpsPerBlock];
  float acc = 0.0f;

  for (int64_t index = tail_start + tid; index < n_elements; index += kThreadsPerBlock) {
    acc += x[index];
  }

  acc = warp_reduce_sum(acc);
  if (lane == 0) {
    warp_sums[warp] = acc;
  }
  __syncthreads();

  if (warp == 0) {
    float block_sum = (lane < kWarpsPerBlock) ? warp_sums[lane] : 0.0f;
    block_sum = warp_reduce_sum(block_sum);
    if (lane == 0) {
      atomicAdd(out, block_sum);
    }
  }
}

}  // namespace

void run_vectorsum_cuda(torch::Tensor x, torch::Tensor out) {
  (void)cudaMemsetAsync(out.data_ptr<float>(), 0, sizeof(float));

  const int64_t n_elements = x.numel();
  const int64_t n_full_tiles = n_elements / kBlockItems;
  const int64_t full_tile_elements = n_full_tiles * kBlockItems;
  const bool has_tail = full_tile_elements < n_elements;

  if (n_full_tiles > 0) {
    const int full_tile_blocks = static_cast<int>(n_full_tiles);
    reduce_tiles_aligned_kernel<<<full_tile_blocks, kThreadsPerBlock>>>(
        x.data_ptr<float>(),
        out.data_ptr<float>());
  }
  if (has_tail) {
    reduce_tail_kernel<<<1, kThreadsPerBlock>>>(
        x.data_ptr<float>(),
        out.data_ptr<float>(),
        full_tile_elements,
        n_elements);
  }
}
"""


def _module_name() -> str:
    source_hash = hashlib.sha256((CPP_SRC + CUDA_SRC).encode("utf-8")).hexdigest()[:16]
    return f"vectorsum_v5_ext_{source_hash}"


def _load_extension():
    global _extension_module

    if _extension_module is not None:
        return _extension_module

    if CUDA_HOME:
        nvcc_dir = str(Path(CUDA_HOME) / "bin")
        path_parts = os.environ.get("PATH", "").split(os.pathsep)
        if nvcc_dir not in path_parts:
            os.environ["PATH"] = os.pathsep.join([nvcc_dir, *path_parts])

    _extension_module = load_inline(
        name=_module_name(),
        cpp_sources=CPP_SRC,
        cuda_sources=CUDA_SRC,
        functions=None,
        extra_cflags=["-O3", "-std=c++17"],
        extra_cuda_cflags=["-O3", "-std=c++17", "--use_fast_math", "-lineinfo"],
        with_cuda=True,
        verbose=False,
    )
    return _extension_module


ext = _load_extension()

def custom_kernel(data: input_t) -> output_t:
    vector, output = data
    ext.run_vectorsum(vector, output)
    return output[0]
scrolls · 188 lines total

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

Changes from previous submission

Against this author's previous submission submission 780282.

⋯ 7 unchanged lines
from task import input_t, output_t
- import torch
from torch.utils.cpp_extension import CUDA_HOME, load_inline
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "ninja"])
⋯ 6 unchanged lines
void run_vectorsum_cuda(torch::Tensor x, torch::Tensor out);
void run_vectorsum(torch::Tensor x, torch::Tensor out) {
- TORCH_CHECK(x.is_cuda(), "x must be a CUDA tensor");
- TORCH_CHECK(out.is_cuda(), "out must be a CUDA tensor");
- TORCH_CHECK(x.scalar_type() == torch::kFloat32, "x must be float32");
- TORCH_CHECK(out.scalar_type() == torch::kFloat32, "out must be float32");
- TORCH_CHECK(x.is_contiguous(), "x must be contiguous");
- TORCH_CHECK(out.is_contiguous(), "out must be contiguous");
- TORCH_CHECK(x.dim() == 1, "x must be 1D");
- TORCH_CHECK(out.numel() == 1, "out must have one element");
- TORCH_CHECK(x.device() == out.device(), "x and out must be on same device");
run_vectorsum_cuda(x, out);
}
⋯ 3 unchanged lines
"""
CUDA_SRC = r"""
- #include <c10/cuda/CUDAGuard.h>
- #include <c10/cuda/CUDAException.h>
#include <cuda_runtime.h>
#include <torch/extension.h>
⋯ 24 unchanged lines
const float4* x4 = reinterpret_cast<const float4*>(x);
const int vec_index = tile_idx * (kBlockItems / 4) + tid;
- float4 v = x4[vec_index];
- acc += v.x + v.y + v.z + v.w;
- v = x4[vec_index + kThreadsPerBlock];
- acc += v.x + v.y + v.z + v.w;
- v = x4[vec_index + 2 * kThreadsPerBlock];
- acc += v.x + v.y + v.z + v.w;
- v = x4[vec_index + 3 * kThreadsPerBlock];
- acc += v.x + v.y + v.z + v.w;
- v = x4[vec_index + 4 * kThreadsPerBlock];
- acc += v.x + v.y + v.z + v.w;
- v = x4[vec_index + 5 * kThreadsPerBlock];
- acc += v.x + v.y + v.z + v.w;
- v = x4[vec_index + 6 * kThreadsPerBlock];
- acc += v.x + v.y + v.z + v.w;
- v = x4[vec_index + 7 * kThreadsPerBlock];
- acc += v.x + v.y + v.z + v.w;
+ float4 v0 = __ldcs(&x4[vec_index]);
+ float4 v1 = __ldcs(&x4[vec_index + kThreadsPerBlock]);
+ acc += v0.x + v0.y + v0.z + v0.w;
+ acc += v1.x + v1.y + v1.z + v1.w;
+ v0 = __ldcs(&x4[vec_index + 2 * kThreadsPerBlock]);
+ v1 = __ldcs(&x4[vec_index + 3 * kThreadsPerBlock]);
+ acc += v0.x + v0.y + v0.z + v0.w;
+ acc += v1.x + v1.y + v1.z + v1.w;
+ v0 = __ldcs(&x4[vec_index + 4 * kThreadsPerBlock]);
+ v1 = __ldcs(&x4[vec_index + 5 * kThreadsPerBlock]);
+ acc += v0.x + v0.y + v0.z + v0.w;
+ acc += v1.x + v1.y + v1.z + v1.w;
+ v0 = __ldcs(&x4[vec_index + 6 * kThreadsPerBlock]);
+ v1 = __ldcs(&x4[vec_index + 7 * kThreadsPerBlock]);
+ acc += v0.x + v0.y + v0.z + v0.w;
+ acc += v1.x + v1.y + v1.z + v1.w;
acc = warp_reduce_sum(acc);
if (lane == 0) {
⋯ 44 unchanged lines
} // namespace
void run_vectorsum_cuda(torch::Tensor x, torch::Tensor out) {
- const c10::cuda::CUDAGuard device_guard(x.device());
- C10_CUDA_CHECK(cudaMemsetAsync(out.data_ptr<float>(), 0, sizeof(float)));
+ (void)cudaMemsetAsync(out.data_ptr<float>(), 0, sizeof(float));
const int64_t n_elements = x.numel();
const int64_t n_full_tiles = n_elements / kBlockItems;
⋯ 19 unchanged lines
def _module_name() -> str:
source_hash = hashlib.sha256((CPP_SRC + CUDA_SRC).encode("utf-8")).hexdigest()[:16]
- return f"vectorsum_v4_ext_{source_hash}"
+ return f"vectorsum_v5_ext_{source_hash}"
def _load_extension():
⋯ 21 unchanged lines
return _extension_module
+ ext = _load_extension()
+
def custom_kernel(data: input_t) -> output_t:
vector, output = data
- n_elements = vector.numel()
-
- if n_elements == 0:
- output[0] = 0.0
- return output[0]
-
- ext = _load_extension()
ext.run_vectorsum(vector, output)
return output[0]
scrolls · 108 diff lines total

Best evidence level for this revision: reported

JSON