incorrect
31_bitmask_or_popcount.py
started
2026-05-05T00:01:56Z
correctness issue:
timingkernelbench.timing
kernel mean / median / std
—
ref mean / median / std
0.65 / — / 0.23 μs
roofline / occupancynsight_hardware_counters
reference PyTorch source (the prompt the agent sees)
"""
R int32 bitmasks per batch element: reduce with bitwise OR across virtual ranks, then popcount (32-bit).
Output shape (B,) float32: number of set bits in the unsigned lower 32 bits of the OR result.
"""
import torch
import torch.nn as nn
from kernelbench.distributed_collectives import default_device
R = 4
B = 24
def _popcount_u32(v: torch.Tensor) -> torch.Tensor:
"""v: int32 or int64 tensor (B,), return float32 popcount in 0..32."""
b = v.shape[0]
u = v.to(torch.int64) & 0xFFFFFFFF
c = torch.zeros(b, dtype=torch.int64, device=v.device)
for k in range(32):
c = c + ((u >> k) & 1)
return c.to(torch.float32)
class Model(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x: torch.Tensor) -> torch.Tensor:
# x: (R, B) int32
acc = x[0].to(torch.int64)
for r in range(1, R):
acc = acc | x[r].to(torch.int64)
acc = acc & 0xFFFFFFFF
return _popcount_u32(acc.to(torch.int32))
def get_inputs():
dev = default_device()
p = popcorn_pri
ints = torch.randint(
-(2**31), 2**31 - 1, (p.jitter_int(R), p.jitter_int(B)), device=dev
)
return [ints.to(torch.int32)]
def get_init_inputs():
return []
turn 013.0s turn · 13.0s LLM · 0.0s tools · FINAL
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 bitmask_popcount_forward(torch::Tensor x);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <stdint.h>\n\n__global__ void bitmask_popcount_kernel(const int32_t* __restrict__ x,\n float* __restrict__ out,\n int B) {\n int b = threadIdx.x;\n if (b < B) {\n uint32_t v = (uint32_t)x[b]\n | (uint32_t)x[B + b]\n | (uint32_t)x[(B << 1) + b]\n | (uint32_t)x[3 * B + b];\n out[b] = (float)__popc(v);\n }\n}\n\ntorch::Tensor bitmask_popcount_forward(torch::Tensor x) {\n const int B = (int)x.size(1);\n auto out = torch::empty({B}, x.options().dtype(torch::kFloat32));\n bitmask_popcount_kernel<<<1, 32, 0, at::cuda::getCurrentCUDAStream()>>>(\n reinterpret_cast<const int32_t*>(x.data_ptr<int>()),\n out.data_ptr<float>(),\n B);\n return out;\n}\n\"\"\"\n\n_ext = load_inline(\n name=\"bitmask_popcount_ext_v1\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"bitmask_popcount_forward\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\"],\n verbose=False,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n return _ext.bitmask_popcount_forward(x)\n"
}
result ← submit_kernel (fail)
failsubmit_kernel
submit_kernel FAILED: correctness check did not pass (? trials).
runtime_error: expected scalar type Int but found Float
final submitted kernel (58 lines, 1,561 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 bitmask_popcount_forward(torch::Tensor x);
"""
_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdint.h>
__global__ void bitmask_popcount_kernel(const int32_t* __restrict__ x,
float* __restrict__ out,
int B) {
int b = threadIdx.x;
if (b < B) {
uint32_t v = (uint32_t)x[b]
| (uint32_t)x[B + b]
| (uint32_t)x[(B << 1) + b]
| (uint32_t)x[3 * B + b];
out[b] = (float)__popc(v);
}
}
torch::Tensor bitmask_popcount_forward(torch::Tensor x) {
const int B = (int)x.size(1);
auto out = torch::empty({B}, x.options().dtype(torch::kFloat32));
bitmask_popcount_kernel<<<1, 32, 0, at::cuda::getCurrentCUDAStream()>>>(
reinterpret_cast<const int32_t*>(x.data_ptr<int>()),
out.data_ptr<float>(),
B);
return out;
}
"""
_ext = load_inline(
name="bitmask_popcount_ext_v1",
cpp_sources=_cpp_src,
cuda_sources=_cuda_src,
functions=["bitmask_popcount_forward"],
extra_cflags=["-O3"],
extra_cuda_cflags=["-O3"],
verbose=False,
)
class ModelNew(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x: torch.Tensor) -> torch.Tensor:
return _ext.bitmask_popcount_forward(x)