incorrect
93_masked_cumsum.py
started
2026-05-05T02:22:54Z
correctness issue:
timingkernelbench.timing
kernel mean / median / std
—
ref mean / median / std
8.92 / — / 0.04 μs
roofline / occupancynsight_hardware_counters
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn
class Model(nn.Module):
"""
A model that performs a masked cumulative sum, only summing elements that satisfy a condition.
Parameters:
dim (int): The dimension along which to perform the masked cumulative sum.
"""
def __init__(self, dim):
super(Model, self).__init__()
self.dim = dim
def forward(self, x, mask):
"""
Args:
x (torch.Tensor): Input tensor of shape (batch_size, *input_shape).
mask (torch.Tensor): Boolean mask of the same shape as x.
Returns:
torch.Tensor: Cumulative sum of elements where mask is True.
"""
return torch.cumsum(x * mask, dim=self.dim)
batch_size = 32768
input_shape = (32768,)
dim = 1
def get_inputs():
x = torch.rand(batch_size, *input_shape)
mask = torch.randint(0, 2, x.shape).bool() # Random boolean mask
return [x, mask]
def get_init_inputs():
return [dim]
turn 0 · FINAL · 1 tool call(s) · 83.7s turn · 83.7s LLM · 6,866 chars (click to expand)
turn 083.7s turn · 83.7s LLM · 0.0s tools · FINAL
reasoning
**Evaluating block scan efficiency**
I'm thinking about using block scans for efficiency. I wonder if implementing the Blelloch algorithm could be beneficial here. The idea would be to share 1024 floats and use a mask load. For each tile, I could load 4 values per thread in a coalesced manner, potentially storing the shared index as t + i * blockDim. It feels like there’s a lot of potential in optimizing this approach, so I’ll have to explore it further!
**Preparing for submission**
I’m confirming a few calculations, like ensuring that 4 multiplied by 4096 is accurate. I think using a base row of 32768 multiplied by 4 seems good too. Also, I’m considering if I need to include <cuda_runtime.h> for uchar4/float4 types. After checking everything, I feel ready to submit. I just need to ensure that all the necessary components are in place before I send it off!
call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n_cpp_src = r\"\"\"\n#include <torch/extension.h>\n\ntorch::Tensor masked_cumsum_forward(torch::Tensor x, torch::Tensor mask, int64_t dim);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda_runtime.h>\n#include <stdint.h>\n\n#define BLOCK_SIZE 1024\n#define ITEMS_PER_THREAD 4\n#define NUM_WARPS 32\n\n__device__ __forceinline__ float warp_scan_inclusive(float v) {\n unsigned mask = 0xffffffffu;\n float n;\n n = __shfl_up_sync(mask, v, 1); if ((threadIdx.x & 31) >= 1) v += n;\n n = __shfl_up_sync(mask, v, 2); if ((threadIdx.x & 31) >= 2) v += n;\n n = __shfl_up_sync(mask, v, 4); if ((threadIdx.x & 31) >= 4) v += n;\n n = __shfl_up_sync(mask, v, 8); if ((threadIdx.x & 31) >= 8) v += n;\n n = __shfl_up_sync(mask, v, 16); if ((threadIdx.x & 31) >= 16) v += n;\n return v;\n}\n\n__device__ __forceinline__ float block_exclusive_scan_1024(float v, float* __restrict__ warp_sums, float &block_total) {\n const int lane = threadIdx.x & 31;\n const int wid = threadIdx.x >> 5;\n\n float incl = warp_scan_inclusive(v);\n if (lane == 31) warp_sums[wid] = incl;\n __syncthreads();\n\n if (wid == 0) {\n float w = warp_sums[lane];\n float wincl = warp_scan_inclusive(w);\n warp_sums[lane] = wincl;\n }\n __syncthreads();\n\n float warp_prefix = (wid == 0) ? 0.0f : warp_sums[wid - 1];\n block_total = warp_sums[NUM_WARPS - 1];\n return warp_prefix + (incl - v);\n}\n\n__global__ __launch_bounds__(BLOCK_SIZE, 2)\nvoid masked_cumsum_dim1_kernel(const float* __restrict__ x,\n const bool* __restrict__ mask,\n float* __restrict__ out,\n int64_t rows, int64_t cols) {\n int row = blockIdx.x;\n if ((int64_t)row >= rows) return;\n\n __shared__ float warp_sums[NUM_WARPS];\n\n const int tid = threadIdx.x;\n const int64_t row_off = (int64_t)row * cols;\n float row_prefix = 0.0f;\n\n #pragma unroll 1\n for (int64_t tile = 0; tile < cols; tile += (int64_t)BLOCK_SIZE * ITEMS_PER_THREAD) {\n const int64_t col = tile + (int64_t)tid * ITEMS_PER_THREAD;\n const int64_t idx = row_off + col;\n\n float v0 = 0.0f, v1 = 0.0f, v2 = 0.0f, v3 = 0.0f;\n bool full = (col + 3 < cols);\n if (full) {\n float4 xv = *reinterpret_cast<const float4*>(x + idx);\n uchar4 mv = *reinterpret_cast<const uchar4*>(mask + idx);\n v0 = mv.x ? xv.x : 0.0f;\n v1 = mv.y ? xv.y : 0.0f;\n v2 = mv.z ? xv.z : 0.0f;\n v3 = mv.w ? xv.w : 0.0f;\n } else {\n if (col + 0 < cols) v0 = mask[idx + 0] ? x[idx + 0] : 0.0f;\n if (col + 1 < cols) v1 = mask[idx + 1] ? x[idx + 1] : 0.0f;\n if (col + 2 < cols) v2 = mask[idx + 2] ? x[idx + 2] : 0.0f;\n if (col + 3 < cols) v3 = mask[idx + 3] ? x[idx + 3] : 0.0f;\n }\n\n float p0 = v0;\n float p1 = p0 + v1;\n float p2 = p1 + v2;\n float p3 = p2 + v3;\n\n float tile_total;\n float thread_excl = block_exclusive_scan_1024(p3, warp_sums, tile_total);\n float base = row_prefix + thread_excl;\n\n if (full) {\n float4 ov;\n ov.x = base + p0;\n ov.y = base + p1;\n ov.z = base + p2;\n ov.w = base + p3;\n *reinterpret_cast<float4*>(out + idx) = ov;\n } else {\n if (col + 0 < cols) out[idx + 0] = base + p0;\n if (col + 1 < cols) out[idx + 1] = base + p1;\n if (col + 2 < cols) out[idx + 2] = base + p2;\n if (col + 3 < cols) out[idx + 3] = base + p3;\n }\n row_prefix += tile_total;\n }\n}\n\ntorch::Tensor masked_cumsum_forward(torch::Tensor x, torch::Tensor mask, int64_t dim) {\n auto out = torch::empty_like(x);\n int64_t cols;\n int64_t rows;\n if (dim < 0) dim += x.dim();\n // Optimized path for the benchmark layout: cumulative sum along the last/second dimension.\n cols = x.size(dim);\n rows = x.numel() / cols;\n\n const dim3 block(BLOCK_SIZE);\n const dim3 grid((unsigned int)rows);\n masked_cumsum_dim1_kernel<<<grid, block, 0, at::cuda::getCurrentCUDAStream()>>>(\n x.data_ptr<float>(), mask.data_ptr<bool>(), out.data_ptr<float>(), rows, cols);\n return out;\n}\n\"\"\"\n\n_ext = load_inline(\n name=\"masked_cumsum_ext_v1\",\n cpp_sources=[_cpp_src],\n cuda_sources=[_cuda_src],\n functions=[\"masked_cumsum_forward\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\"],\n verbose=False,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, dim):\n super(ModelNew, self).__init__()\n self.dim = dim\n\n def forward(self, x, mask):\n return _ext.masked_cumsum_forward(x, mask, self.dim)\n"
}
result ← submit_kernel (fail)
failsubmit_kernel
submit_kernel FAILED: correctness check did not pass (? trials).
runtime_error: expected scalar type Bool but found Float
final submitted kernel (147 lines, 4,833 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline
_cpp_src = r"""
#include <torch/extension.h>
torch::Tensor masked_cumsum_forward(torch::Tensor x, torch::Tensor mask, int64_t dim);
"""
_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <stdint.h>
#define BLOCK_SIZE 1024
#define ITEMS_PER_THREAD 4
#define NUM_WARPS 32
__device__ __forceinline__ float warp_scan_inclusive(float v) {
unsigned mask = 0xffffffffu;
float n;
n = __shfl_up_sync(mask, v, 1); if ((threadIdx.x & 31) >= 1) v += n;
n = __shfl_up_sync(mask, v, 2); if ((threadIdx.x & 31) >= 2) v += n;
n = __shfl_up_sync(mask, v, 4); if ((threadIdx.x & 31) >= 4) v += n;
n = __shfl_up_sync(mask, v, 8); if ((threadIdx.x & 31) >= 8) v += n;
n = __shfl_up_sync(mask, v, 16); if ((threadIdx.x & 31) >= 16) v += n;
return v;
}
__device__ __forceinline__ float block_exclusive_scan_1024(float v, float* __restrict__ warp_sums, float &block_total) {
const int lane = threadIdx.x & 31;
const int wid = threadIdx.x >> 5;
float incl = warp_scan_inclusive(v);
if (lane == 31) warp_sums[wid] = incl;
__syncthreads();
if (wid == 0) {
float w = warp_sums[lane];
float wincl = warp_scan_inclusive(w);
warp_sums[lane] = wincl;
}
__syncthreads();
float warp_prefix = (wid == 0) ? 0.0f : warp_sums[wid - 1];
block_total = warp_sums[NUM_WARPS - 1];
return warp_prefix + (incl - v);
}
__global__ __launch_bounds__(BLOCK_SIZE, 2)
void masked_cumsum_dim1_kernel(const float* __restrict__ x,
const bool* __restrict__ mask,
float* __restrict__ out,
int64_t rows, int64_t cols) {
int row = blockIdx.x;
if ((int64_t)row >= rows) return;
__shared__ float warp_sums[NUM_WARPS];
const int tid = threadIdx.x;
const int64_t row_off = (int64_t)row * cols;
float row_prefix = 0.0f;
#pragma unroll 1
for (int64_t tile = 0; tile < cols; tile += (int64_t)BLOCK_SIZE * ITEMS_PER_THREAD) {
const int64_t col = tile + (int64_t)tid * ITEMS_PER_THREAD;
const int64_t idx = row_off + col;
float v0 = 0.0f, v1 = 0.0f, v2 = 0.0f, v3 = 0.0f;
bool full = (col + 3 < cols);
if (full) {
float4 xv = *reinterpret_cast<const float4*>(x + idx);
uchar4 mv = *reinterpret_cast<const uchar4*>(mask + idx);
v0 = mv.x ? xv.x : 0.0f;
v1 = mv.y ? xv.y : 0.0f;
v2 = mv.z ? xv.z : 0.0f;
v3 = mv.w ? xv.w : 0.0f;
} else {
if (col + 0 < cols) v0 = mask[idx + 0] ? x[idx + 0] : 0.0f;
if (col + 1 < cols) v1 = mask[idx + 1] ? x[idx + 1] : 0.0f;
if (col + 2 < cols) v2 = mask[idx + 2] ? x[idx + 2] : 0.0f;
if (col + 3 < cols) v3 = mask[idx + 3] ? x[idx + 3] : 0.0f;
}
float p0 = v0;
float p1 = p0 + v1;
float p2 = p1 + v2;
float p3 = p2 + v3;
float tile_total;
float thread_excl = block_exclusive_scan_1024(p3, warp_sums, tile_total);
float base = row_prefix + thread_excl;
if (full) {
float4 ov;
ov.x = base + p0;
ov.y = base + p1;
ov.z = base + p2;
ov.w = base + p3;
*reinterpret_cast<float4*>(out + idx) = ov;
} else {
if (col + 0 < cols) out[idx + 0] = base + p0;
if (col + 1 < cols) out[idx + 1] = base + p1;
if (col + 2 < cols) out[idx + 2] = base + p2;
if (col + 3 < cols) out[idx + 3] = base + p3;
}
row_prefix += tile_total;
}
}
torch::Tensor masked_cumsum_forward(torch::Tensor x, torch::Tensor mask, int64_t dim) {
auto out = torch::empty_like(x);
int64_t cols;
int64_t rows;
if (dim < 0) dim += x.dim();
// Optimized path for the benchmark layout: cumulative sum along the last/second dimension.
cols = x.size(dim);
rows = x.numel() / cols;
const dim3 block(BLOCK_SIZE);
const dim3 grid((unsigned int)rows);
masked_cumsum_dim1_kernel<<<grid, block, 0, at::cuda::getCurrentCUDAStream()>>>(
x.data_ptr<float>(), mask.data_ptr<bool>(), out.data_ptr<float>(), rows, cols);
return out;
}
"""
_ext = load_inline(
name="masked_cumsum_ext_v1",
cpp_sources=[_cpp_src],
cuda_sources=[_cuda_src],
functions=["masked_cumsum_forward"],
extra_cflags=["-O3"],
extra_cuda_cflags=["-O3"],
verbose=False,
)
class ModelNew(nn.Module):
def __init__(self, dim):
super(ModelNew, self).__init__()
self.dim = dim
def forward(self, x, mask):
return _ext.masked_cumsum_forward(x, mask, self.dim)