Skip to content
KernelIndex
Search⌘K

submission 511990

iharryli · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

FourCore_0227_combo_c_448_noalloc_sm89.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-vectorsum-v2-511990?include=source"
interfacepython
Compatibility
measured onNVIDIA L4
declared hardwareNVIDIA L4
architecturessm_89
dtypesfp32

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
Vector sum reductionsuite of 6 cases
NVIDIA L4
912.0µs
#6 of 26
2026-02-28

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:71ffbffdae6ea7e773e9f5f4d3839799675b4d8c699b3d14ff27984e55adf4fd
license declaredunknown
license concludedunknown
authorsiharryli
imported2026-08-15

Techniques

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

shared-memory__shared__ float warpSums[warps];

Kernel source

FourCore_0227_combo_c_448_noalloc_sm89.py88 lines
#@SUBMISSION@

import os
import torch
from torch.utils.cpp_extension import load_inline
from task import input_t, output_t

os.environ["TORCH_CUDA_ARCH_LIST"] = "8.9"

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

template <typename T>
__device__ __forceinline__ T warpReduction(T v, unsigned mask = 0xffffffffu) {
    #pragma unroll
    for (int offset = 16; offset > 0; offset >>= 1) v += __shfl_down_sync(mask, v, offset);
    return v;
}

template <unsigned blockSize>
__global__ __launch_bounds__(blockSize, 2)
void reduction_doubleout(const float* __restrict__ data, double* out, size_t size) {
    size_t idx = static_cast<size_t>(blockIdx.x) * blockDim.x + threadIdx.x;
    const size_t stride = static_cast<size_t>(gridDim.x) * blockDim.x;
    constexpr unsigned warps = (blockSize + 31) / 32;
    constexpr unsigned mask = (1u << warps) - 1u;
    __shared__ float warpSums[warps];

    float sum = 0.0f;
    while (idx < size) {
        sum += data[idx];
        idx += stride;
    }

    const float w = warpReduction(sum);
    if (!(threadIdx.x & 31)) warpSums[threadIdx.x >> 5] = w;
    __syncthreads();

    if (threadIdx.x < warps) {
        const float b = warpReduction(warpSums[threadIdx.x], mask);
        if (!threadIdx.x) atomicAdd(out, static_cast<double>(b));
    }
}

__global__ void cast_one(const double* __restrict__ in, float* out) {
    if (!threadIdx.x && !blockIdx.x) out[0] = static_cast<float>(in[0]);
}

torch::Tensor kernelReduce(torch::Tensor x, torch::Tensor out) {
    constexpr int threads = 448;
    const int blocks = (x.numel() + threads - 1) / threads;
    reduction_doubleout<448><<<blocks, threads>>>(
        reinterpret_cast<const float*>(x.data_ptr<float>()),
        reinterpret_cast<double*>(out.data_ptr<double>()),
        x.numel());
    return out;
}

torch::Tensor kernelCast(torch::Tensor in, torch::Tensor out) {
    cast_one<<<1, 1>>>(reinterpret_cast<const double*>(in.data_ptr<double>()), reinterpret_cast<float*>(out.data_ptr<float>()));
    return out;
}
"""

cpp_source = """
torch::Tensor kernelReduce(torch::Tensor x, torch::Tensor out);
torch::Tensor kernelCast(torch::Tensor in, torch::Tensor out);
"""

module = load_inline(
    name="vectorsum_fourcore_0227_combo_c_448_noalloc_sm89",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=["kernelReduce", "kernelCast"],
    verbose=False,
    extra_cuda_cflags=["-O3", "--use_fast_math", "-maxrregcount=80"],
)

result = torch.tensor(0.0, dtype=torch.float64, device="cuda:0")

def custom_kernel(data: input_t) -> output_t:
    x, out = data
    result.fill_(0.0)
    module.kernelReduce(x, result)
    module.kernelCast(result, out)
    return out[0]
scrolls · 88 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 511988.

#@SUBMISSION@
import os
-
import torch
from torch.utils.cpp_extension import load_inline
-
from task import input_t, output_t
- os.environ.setdefault("TORCH_CUDA_ARCH_LIST", "8.0+PTX")
+ os.environ["TORCH_CUDA_ARCH_LIST"] = "8.9"
cuda_source = r"""
#include <pybind11/pybind11.h>
⋯ 2 unchanged lines
template <typename T>
__device__ __forceinline__ T warpReduction(T v, unsigned mask = 0xffffffffu) {
#pragma unroll
- for (int offset = 16; offset > 0; offset >>= 1) {
- v += __shfl_down_sync(mask, v, offset);
- }
+ for (int offset = 16; offset > 0; offset >>= 1) v += __shfl_down_sync(mask, v, offset);
return v;
}
template <unsigned blockSize>
- __global__ void reduction_doubleout(const float* __restrict__ data, double* out, size_t size) {
- size_t index = static_cast<size_t>(blockIdx.x) * blockDim.x + threadIdx.x;
+ __global__ __launch_bounds__(blockSize, 2)
+ void reduction_doubleout(const float* __restrict__ data, double* out, size_t size) {
+ size_t idx = static_cast<size_t>(blockIdx.x) * blockDim.x + threadIdx.x;
const size_t stride = static_cast<size_t>(gridDim.x) * blockDim.x;
+ constexpr unsigned warps = (blockSize + 31) / 32;
+ constexpr unsigned mask = (1u << warps) - 1u;
+ __shared__ float warpSums[warps];
- constexpr unsigned blockWarpCount = (blockSize + 31) / 32;
- constexpr unsigned blockReductionThreadMask = (1u << blockWarpCount) - 1u;
- __shared__ float warpSums[blockWarpCount];
-
- float threadSum = 0.0f;
- while (index < size) {
- threadSum += data[index];
- index += stride;
+ float sum = 0.0f;
+ while (idx < size) {
+ sum += data[idx];
+ idx += stride;
}
- const float warpSum = warpReduction(threadSum);
- const unsigned warpNumber = threadIdx.x >> 5;
- if (!(threadIdx.x & 31)) warpSums[warpNumber] = warpSum;
+ const float w = warpReduction(sum);
+ if (!(threadIdx.x & 31)) warpSums[threadIdx.x >> 5] = w;
__syncthreads();
- if (threadIdx.x < blockWarpCount) {
- const float blockSum = warpReduction(warpSums[threadIdx.x], blockReductionThreadMask);
- if (!threadIdx.x) atomicAdd(out, static_cast<double>(blockSum));
+ if (threadIdx.x < warps) {
+ const float b = warpReduction(warpSums[threadIdx.x], mask);
+ if (!threadIdx.x) atomicAdd(out, static_cast<double>(b));
}
}
⋯ 2 unchanged lines
}
torch::Tensor kernelReduce(torch::Tensor x, torch::Tensor out) {
- constexpr int threads = 256;
+ constexpr int threads = 448;
const int blocks = (x.numel() + threads - 1) / threads;
- reduction_doubleout<256><<<blocks, threads>>>(
+ reduction_doubleout<448><<<blocks, threads>>>(
reinterpret_cast<const float*>(x.data_ptr<float>()),
reinterpret_cast<double*>(out.data_ptr<double>()),
x.numel());
⋯ 1 unchanged lines
}
torch::Tensor kernelCast(torch::Tensor in, torch::Tensor out) {
- cast_one<<<1, 1>>>(
- reinterpret_cast<const double*>(in.data_ptr<double>()),
- reinterpret_cast<float*>(out.data_ptr<float>()));
+ cast_one<<<1, 1>>>(reinterpret_cast<const double*>(in.data_ptr<double>()), reinterpret_cast<float*>(out.data_ptr<float>()));
return out;
}
"""
⋯ 4 unchanged lines
"""
module = load_inline(
- name="vectorsum_fourcore_0227_top1_noalloc_cast",
+ name="vectorsum_fourcore_0227_combo_c_448_noalloc_sm89",
cpp_sources=cpp_source,
cuda_sources=cuda_source,
functions=["kernelReduce", "kernelCast"],
verbose=False,
- extra_cuda_cflags=["-O3", "--use_fast_math"],
+ extra_cuda_cflags=["-O3", "--use_fast_math", "-maxrregcount=80"],
)
result = torch.tensor(0.0, dtype=torch.float64, device="cuda:0")
-
def custom_kernel(data: input_t) -> output_t:
- x, out_buf = data
+ x, out = data
result.fill_(0.0)
module.kernelReduce(x, result)
- module.kernelCast(result, out_buf)
- return out_buf[0]
+ module.kernelCast(result, out)
+ return out[0]
scrolls · 115 diff lines total

Best evidence level for this revision: reported

JSON