correct
24_VoxelGridPooling.py
started
2026-05-04T05:06:23Z
timingkernelbench.timing
kernel mean / median / std
0.02 / — / 4.21e-04 μs
ref mean / median / std
11.20 / — / 0.84 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
2242.00x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
214.73 GB/s
warp stalls (top 5)nsight
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn
class Model(nn.Module):
"""
Voxel-grid pooling for 3-D molecular point clouds. Discretizes
continuous 3-D coordinates into a regular grid and pools per-atom
features into each voxel. Used in 3-D-CNN approaches for molecular
property prediction and ligand docking (e.g., PointVoxel-CNN,
3D-Infomax).
"""
def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):
super().__init__()
self.feat_dim = feat_dim
self.grid_size = grid_size
self.voxel_size = voxel_size
self.linear = nn.Linear(feat_dim, feat_dim)
def forward(
self, coords: torch.Tensor, features: torch.Tensor
) -> torch.Tensor:
"""
Args:
coords: (B, N, 3) – atom coordinates
features: (B, N, feat_dim) – per-atom features
Returns:
voxel_grid: (B, feat_dim, G, G, G) – pooled voxel features
"""
B, N, _ = coords.shape
G = self.grid_size
features = self.linear(features)
# Center coordinates and discretize
center = coords.mean(dim=1, keepdim=True)
shifted = (coords - center) / self.voxel_size + G / 2.0
indices = shifted.long().clamp(0, G - 1) # (B, N, 3)
voxel_grid = coords.new_zeros(B, self.feat_dim, G, G, G)
ix = indices[:, :, 0]
iy = indices[:, :, 1]
iz = indices[:, :, 2]
batch_idx = torch.arange(B, device=coords.device).unsqueeze(1).expand(-1, N)
for d in range(self.feat_dim):
feat_d = features[:, :, d]
voxel_grid[:, d].index_put_(
(batch_idx.reshape(-1), ix.reshape(-1), iy.reshape(-1), iz.reshape(-1)),
feat_d.reshape(-1),
accumulate=True,
)
return voxel_grid
feat_dim = 32
grid_size = 32
voxel_size = 1.0
num_atoms = 256
batch_size = 4
def get_inputs():
p = popcorn_pri
bs = p.jitter_int(batch_size)
na = p.jitter_int(num_atoms)
coords = torch.randn(bs, na, 3) * 5.0
features = torch.randn(bs, na, feat_dim)
return [coords, features]
def get_init_inputs():
return [feat_dim, grid_size, voxel_size]
turn 09.7s turn · 9.7s LLM · 0.0s tools
call → get_gpu_specs
callget_gpu_specs
{}
result ← get_gpu_specs (ok)
okget_gpu_specs
GPU specs for NVIDIA H100 80GB HBM3:
total memory (runtime): 79.1 GB
spec entry: H100
GPU Architecture: Hopper
GPU Memory: 80GB
Memory Bandwidth: 3.35 TB/s
FP64 TFLOPS: 34
FP64 Tensor Core TFLOPS: 67
FP32 TFLOPS: 67
TF32 Tensor Core TFLOPS: 989 with sparsity
BFLOAT16 Tensore Core TFLOPS: 1979 with sparsity
FP16 Tensor Core TFLOPS: 1979 with sparsity
FP8 Tensor Core TFLOPS: 3958 with sparsity
INT8 Tensor Core TOPS: 3958 with sparsity
Register File Size: 64K 32-bit registers per SM
Maximum number of registers per thread: 255
Maximum number of thread blocks per SM: 32
Shared memory capacity per SM: 228 KB
Maximum shared memory per thread block: 227 KB
turn 1 · 1 tool call(s) · 12.0s turn · 12.0s LLM · 6,179 chars (click to expand)
turn 112.0s turn · 12.0s LLM · 0.0s tools
reasoning
**Considering model implementation**
I need to make sure to copy the weights when I'm implementing ModelNew. It seems that I should also compare it to a reference model that has the same initialized parameters, which might involve using state_dict for loading. Oh, and our class must include a linear layer, of course. Once I have those components in place, I'll proceed to compile and test everything to ensure it works correctly.
call → compile_kernel
callcompile_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 voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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\n__global__ void center_kernel(const float* __restrict__ coords, float* __restrict__ centers, int B, int N) {\n int b = blockIdx.x;\n int tid = threadIdx.x;\n float sx = 0.f, sy = 0.f, sz = 0.f;\n for (int n = tid; n < N; n += blockDim.x) {\n const float* p = coords + ((b * N + n) * 3);\n sx += p[0]; sy += p[1]; sz += p[2];\n }\n __shared__ float shx[256], shy[256], shz[256];\n shx[tid] = sx; shy[tid] = sy; shz[tid] = sz;\n __syncthreads();\n for (int s = blockDim.x >> 1; s > 0; s >>= 1) {\n if (tid < s) { shx[tid] += shx[tid+s]; shy[tid] += shy[tid+s]; shz[tid] += shz[tid+s]; }\n __syncthreads();\n }\n if (tid == 0) {\n float invN = 1.0f / (float)N;\n centers[b*3+0] = shx[0] * invN;\n centers[b*3+1] = shy[0] * invN;\n centers[b*3+2] = shz[0] * invN;\n }\n}\n\n__global__ void scatter_linear_kernel(const float* __restrict__ coords,\n const float* __restrict__ features,\n const float* __restrict__ weight,\n const float* __restrict__ bias,\n const float* __restrict__ centers,\n float* __restrict__ out,\n int B, int N, int F, int G, float inv_voxel) {\n int atom = blockIdx.x;\n int b = atom / N;\n int n = atom - b * N;\n int d = threadIdx.x;\n\n const float* c = coords + ((b * N + n) * 3);\n float halfG = 0.5f * (float)G;\n int ix = (int)((c[0] - centers[b*3+0]) * inv_voxel + halfG);\n int iy = (int)((c[1] - centers[b*3+1]) * inv_voxel + halfG);\n int iz = (int)((c[2] - centers[b*3+2]) * inv_voxel + halfG);\n ix = ix < 0 ? 0 : (ix >= G ? G - 1 : ix);\n iy = iy < 0 ? 0 : (iy >= G ? G - 1 : iy);\n iz = iz < 0 ? 0 : (iz >= G ? G - 1 : iz);\n\n const float* x = features + ((b * N + n) * F);\n int vox = (ix * G + iy) * G + iz;\n int G3 = G * G * G;\n for (int fd = d; fd < F; fd += blockDim.x) {\n float acc = bias[fd];\n const float* w = weight + fd * F;\n // F is normally 32; keep generic\n for (int j = 0; j < F; ++j) acc += x[j] * w[j];\n atomicAdd(out + ((b * F + fd) * G3 + vox), acc);\n }\n}\n\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size) {\n TORCH_CHECK(coords.is_cuda() && features.is_cuda(), \"inputs must be CUDA\");\n TORCH_CHECK(coords.scalar_type() == torch::kFloat32 && features.scalar_type() == torch::kFloat32, \"only float32 supported\");\n coords = coords.contiguous();\n features = features.contiguous();\n weight = weight.contiguous();\n bias = bias.contiguous();\n int B = (int)coords.size(0);\n int N = (int)coords.size(1);\n int F = (int)features.size(2);\n int G = (int)grid_size;\n auto out = torch::zeros({B, F, G, G, G}, coords.options());\n auto centers = torch::empty({B, 3}, coords.options());\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n int cthreads = 256;\n center_kernel<<<B, cthreads, 0, stream>>>(coords.data_ptr<float>(), centers.data_ptr<float>(), B, N);\n int threads = 32;\n if (F > 32) threads = 128;\n scatter_linear_kernel<<<B * N, threads, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), B, N, F, G, (float)(1.0 / voxel_size));\n return out;\n}\n\"\"\"\n\n_ext = load_inline(name=\"voxel_pool_ext_v1\", cpp_sources=_cpp_src, cuda_sources=_cuda_src, functions=[\"voxel_pool_forward\"], extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"], extra_cflags=[\"-O3\"], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__()\n self.feat_dim = feat_dim\n self.grid_size = grid_size\n self.voxel_size = voxel_size\n self.linear = nn.Linear(feat_dim, feat_dim)\n\n def forward(self, coords: torch.Tensor, features: torch.Tensor) -> torch.Tensor:\n return _ext.voxel_pool_forward(coords, features, self.linear.weight, self.linear.bias, self.grid_size, float(self.voxel_size))\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 29.9s turn · 9.9s LLM · 0.0s tools
call → run_correctness
callrun_correctness
{
"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 voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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\n__global__ void center_kernel(const float* __restrict__ coords, float* __restrict__ centers, int B, int N) {\n int b = blockIdx.x;\n int tid = threadIdx.x;\n float sx = 0.f, sy = 0.f, sz = 0.f;\n for (int n = tid; n < N; n += blockDim.x) {\n const float* p = coords + ((b * N + n) * 3);\n sx += p[0]; sy += p[1]; sz += p[2];\n }\n __shared__ float shx[256], shy[256], shz[256];\n shx[tid] = sx; shy[tid] = sy; shz[tid] = sz;\n __syncthreads();\n for (int s = blockDim.x >> 1; s > 0; s >>= 1) {\n if (tid < s) { shx[tid] += shx[tid+s]; shy[tid] += shy[tid+s]; shz[tid] += shz[tid+s]; }\n __syncthreads();\n }\n if (tid == 0) {\n float invN = 1.0f / (float)N;\n centers[b*3+0] = shx[0] * invN;\n centers[b*3+1] = shy[0] * invN;\n centers[b*3+2] = shz[0] * invN;\n }\n}\n\n__global__ void scatter_linear_kernel(const float* __restrict__ coords,\n const float* __restrict__ features,\n const float* __restrict__ weight,\n const float* __restrict__ bias,\n const float* __restrict__ centers,\n float* __restrict__ out,\n int B, int N, int F, int G, float inv_voxel) {\n int atom = blockIdx.x;\n int b = atom / N;\n int n = atom - b * N;\n int d = threadIdx.x;\n\n const float* c = coords + ((b * N + n) * 3);\n float halfG = 0.5f * (float)G;\n int ix = (int)((c[0] - centers[b*3+0]) * inv_voxel + halfG);\n int iy = (int)((c[1] - centers[b*3+1]) * inv_voxel + halfG);\n int iz = (int)((c[2] - centers[b*3+2]) * inv_voxel + halfG);\n ix = ix < 0 ? 0 : (ix >= G ? G - 1 : ix);\n iy = iy < 0 ? 0 : (iy >= G ? G - 1 : iy);\n iz = iz < 0 ? 0 : (iz >= G ? G - 1 : iz);\n\n const float* x = features + ((b * N + n) * F);\n int vox = (ix * G + iy) * G + iz;\n int G3 = G * G * G;\n for (int fd = d; fd < F; fd += blockDim.x) {\n float acc = bias[fd];\n const float* w = weight + fd * F;\n for (int j = 0; j < F; ++j) acc += x[j] * w[j];\n atomicAdd(out + ((b * F + fd) * G3 + vox), acc);\n }\n}\n\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size) {\n TORCH_CHECK(coords.is_cuda() && features.is_cuda(), \"inputs must be CUDA\");\n TORCH_CHECK(coords.scalar_type() == torch::kFloat32 && features.scalar_type() == torch::kFloat32, \"only float32 supported\");\n coords = coords.contiguous();\n features = features.contiguous();\n weight = weight.contiguous();\n bias = bias.contiguous();\n int B = (int)coords.size(0);\n int N = (int)coords.size(1);\n int F = (int)features.size(2);\n int G = (int)grid_size;\n auto out = torch::zeros({B, F, G, G, G}, coords.options());\n auto centers = torch::empty({B, 3}, coords.options());\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n center_kernel<<<B, 256, 0, stream>>>(coords.data_ptr<float>(), centers.data_ptr<float>(), B, N);\n int threads = F > 32 ? 128 : 32;\n scatter_linear_kernel<<<B * N, threads, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), B, N, F, G, (float)(1.0 / voxel_size));\n return out;\n}\n\"\"\"\n\n_ext = load_inline(name=\"voxel_pool_ext_v1\", cpp_sources=_cpp_src, cuda_sources=_cuda_src, functions=[\"voxel_pool_forward\"], extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"], extra_cflags=[\"-O3\"], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__()\n self.feat_dim = feat_dim\n self.grid_size = grid_size\n self.voxel_size = voxel_size\n self.linear = nn.Linear(feat_dim, feat_dim)\n\n def forward(self, coords: torch.Tensor, features: torch.Tensor) -> torch.Tensor:\n return _ext.voxel_pool_forward(coords, features, self.linear.weight, self.linear.bias, self.grid_size, float(self.voxel_size))\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=7.15e-07 mean_abs_err=3.81e-10 max_rel_err=6.52e-03
turn 37.8s turn · 7.8s LLM · 0.0s tools
call → profile_kernel
callprofile_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>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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__global__ void center_kernel(const float* __restrict__ coords, float* __restrict__ centers, int B, int N) {int b=blockIdx.x;int tid=threadIdx.x;float sx=0.f,sy=0.f,sz=0.f;for(int n=tid;n<N;n+=blockDim.x){const float*p=coords+((b*N+n)*3);sx+=p[0];sy+=p[1];sz+=p[2];}__shared__ float shx[256],shy[256],shz[256];shx[tid]=sx;shy[tid]=sy;shz[tid]=sz;__syncthreads();for(int s=blockDim.x>>1;s>0;s>>=1){if(tid<s){shx[tid]+=shx[tid+s];shy[tid]+=shy[tid+s];shz[tid]+=shz[tid+s];}__syncthreads();}if(tid==0){float invN=1.0f/(float)N;centers[b*3]=shx[0]*invN;centers[b*3+1]=shy[0]*invN;centers[b*3+2]=shz[0]*invN;}}\n__global__ void scatter_linear_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){int atom=blockIdx.x;int b=atom/N;int n=atom-b*N;int d=threadIdx.x;const float*c=coords+((b*N+n)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=ix<0?0:(ix>=G?G-1:ix);iy=iy<0?0:(iy>=G?G-1:iy);iz=iz<0?0:(iz>=G?G-1:iz);const float*x=features+((b*N+n)*F);int vox=(ix*G+iy)*G+iz;int G3=G*G*G;for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd];const float*w=weight+fd*F;for(int j=0;j<F;++j)acc+=x[j]*w[j];atomicAdd(out+((b*F+fd)*G3+vox),acc);}}\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size){coords=coords.contiguous();features=features.contiguous();weight=weight.contiguous();bias=bias.contiguous();int B=(int)coords.size(0);int N=(int)coords.size(1);int F=(int)features.size(2);int G=(int)grid_size;auto out=torch::zeros({B,F,G,G,G},coords.options());auto centers=torch::empty({B,3},coords.options());cudaStream_t stream=at::cuda::getCurrentCUDAStream();center_kernel<<<B,256,0,stream>>>(coords.data_ptr<float>(),centers.data_ptr<float>(),B,N);int threads=F>32?128:32;scatter_linear_kernel<<<B*N,threads,0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),B,N,F,G,(float)(1.0/voxel_size));return out;}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_prof1\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__(); self.feat_dim=feat_dim; self.grid_size=grid_size; self.voxel_size=voxel_size; self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self, coords, features):\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← profile_kernel (ok)
okprofile_kernel
profile_kernel PASSED: profiling complete.
=== Kernel Profile Summary ===
--- Kernel Breakdown (by GPU time) ---
#1: aten::zeros 10141.0μs (36%) calls=1
#2: aten::zero_ 7939.0μs (28%) calls=1
#3: aten::fill_ 7903.0μs (28%) calls=1
#4: aten::empty 2146.0μs (8%) calls=1
#5: aten::empty 22.0μs (0%) calls=1
--- Timing ---
GPU time: 19.10 μs
--- Memory Bandwidth ---
DRAM BW: 195.0/3350 GB/s (5.8%)
read=180.7 write=14.3 GB/s
Hit rates: L2=271.3%
--- Compute Throughput ---
Dominant pipe: FP32 util=5.5%
Pipe util: FMA=5.5%, Tensor=0.1%
--- Roofline Analysis ---
Ridge point (FP32): 20.00 FLOPs/byte
Classification: MEMORY-BOUND
--- Occupancy ---
Achieved: 93.4%
Eligible warps/cycle: 0.46
WARNING: <1 eligible warp/cycle = latency-bound, needs more parallelism
Launch: regs/thread=64, smem/blk=6144B, block=416, grid=5124
--- Memory Access Quality ---
Global ld coalescing: 27.01 sectors/req (27.0x wasted BW)
--- Warp Stall Reasons (% of issue cycles) ---
long_scoreboard 4123.2% ####################
mio_throttle 2746.3% ####################
short_scoreboard 2076.1% ####################
--- Optimization Hints ---
-> 4123% stalls: global memory latency (waiting for DRAM/L2). + sectors/req=27.0 -> uncoalesced global loads, restructure access pattern or use __ldg Try shared-memory tiling, prefetching, or reducing working set.
-> Global load coalescing=27.0x (ideal=1.0) -> 27x wasted DRAM BW. Make per-warp accesses contiguous.
-> Low eligible warps + low BW% + low compute% = LATENCY-BOUND. Need more parallelism (larger grid, more ILP, prefetching).
turn 4 · 1 tool call(s) · 28.3s turn · 28.3s LLM · 7,553 chars (click to expand)
turn 428.3s turn · 28.3s LLM · 0.0s tools
reasoning
**Evaluating output requirements**
I need to generate 32 outputs, with each one requiring a specific configuration of atoms and warps. I’m working with 1024 warps where each warp has 1024 FMAs, bringing me to 1 million FMAs. I’m considering the memory utilization, particularly whether using uncoalesced weights might work in L1 cache and if a constant weight for all atoms would fit in 4KB. Also, precomputing features in a linear way with Torch might not be the solution. I should definitely compare variants and think about using a custom zero kernel.
**Considering CUDA operations**
I'm thinking about whether one can zero output in the center with `cudaMemset` asynchronously without a kernel launch. It seems like this is an asynchronous operation, which is interesting. Maybe I should try compiling, correcting, or profiling this to see how it works. There appears to be some intricacies worth exploring, especially in terms of optimizing performance and understanding the behavior of memory operations in CUDA.
call → compile_kernel
callcompile_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>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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\n__global__ void center_kernel(const float* __restrict__ coords, float* __restrict__ centers, int N) {\n int b = blockIdx.x;\n int tid = threadIdx.x;\n float sx = 0.f, sy = 0.f, sz = 0.f;\n for (int n = tid; n < N; n += blockDim.x) {\n const float* p = coords + ((b * N + n) * 3);\n sx += p[0]; sy += p[1]; sz += p[2];\n }\n __shared__ float shx[256], shy[256], shz[256];\n shx[tid] = sx; shy[tid] = sy; shz[tid] = sz;\n __syncthreads();\n for (int s = 128; s > 0; s >>= 1) {\n if (tid < s) { shx[tid] += shx[tid+s]; shy[tid] += shy[tid+s]; shz[tid] += shz[tid+s]; }\n __syncthreads();\n }\n if (tid == 0) {\n float invN = 1.0f / (float)N;\n centers[b*3+0] = shx[0] * invN;\n centers[b*3+1] = shy[0] * invN;\n centers[b*3+2] = shz[0] * invN;\n }\n}\n\n__global__ void scatter_linear32_kernel(const float* __restrict__ coords,\n const float* __restrict__ features,\n const float* __restrict__ weight,\n const float* __restrict__ bias,\n const float* __restrict__ centers,\n float* __restrict__ out,\n int N, int G, float inv_voxel) {\n int atom = blockIdx.x;\n int b = blockIdx.y;\n int n = atom;\n int fd = threadIdx.x; // 0..31\n const float* c = coords + ((b * N + n) * 3);\n float halfG = 0.5f * (float)G;\n int ix = (int)((c[0] - centers[b*3+0]) * inv_voxel + halfG);\n int iy = (int)((c[1] - centers[b*3+1]) * inv_voxel + halfG);\n int iz = (int)((c[2] - centers[b*3+2]) * inv_voxel + halfG);\n ix = max(0, min(ix, G-1));\n iy = max(0, min(iy, G-1));\n iz = max(0, min(iz, G-1));\n const float* x = features + ((b * N + n) * 32);\n const float* w = weight + fd * 32;\n float acc = bias[fd];\n#pragma unroll\n for (int j = 0; j < 32; ++j) acc = fmaf(x[j], w[j], acc);\n int vox = (ix * G + iy) * G + iz;\n int G3 = G * G * G;\n atomicAdd(out + ((b * 32 + fd) * G3 + vox), acc);\n}\n\n__global__ void scatter_linear_generic_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){\n int atom=blockIdx.x; int b=atom/N; int n=atom-b*N; int d=threadIdx.x;\n const float* c=coords+((b*N+n)*3); float halfG=0.5f*(float)G;\n int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG); int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG); int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);\n ix=max(0,min(ix,G-1)); iy=max(0,min(iy,G-1)); iz=max(0,min(iz,G-1));\n const float* x=features+((b*N+n)*F); int vox=(ix*G+iy)*G+iz; int G3=G*G*G;\n for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd]; const float*w=weight+fd*F; for(int j=0;j<F;++j) acc+=x[j]*w[j]; atomicAdd(out+((b*F+fd)*G3+vox),acc);} }\n\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size) {\n coords = coords.contiguous(); features = features.contiguous(); weight = weight.contiguous(); bias = bias.contiguous();\n int B = (int)coords.size(0); int N = (int)coords.size(1); int F = (int)features.size(2); int G = (int)grid_size;\n auto out = torch::empty({B, F, G, G, G}, coords.options());\n auto centers = torch::empty({B, 3}, coords.options());\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n cudaMemsetAsync(out.data_ptr<float>(), 0, (size_t)B*F*G*G*G*sizeof(float), stream);\n center_kernel<<<B, 256, 0, stream>>>(coords.data_ptr<float>(), centers.data_ptr<float>(), N);\n if (F == 32) {\n dim3 grid(N, B);\n scatter_linear32_kernel<<<grid, 32, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), N, G, (float)(1.0/voxel_size));\n } else {\n int threads = F > 32 ? 128 : 32;\n scatter_linear_generic_kernel<<<B*N, threads, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), B,N,F,G,(float)(1.0/voxel_size));\n }\n return out;\n}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_v2\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__(); self.feat_dim=feat_dim; self.grid_size=grid_size; self.voxel_size=voxel_size; self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self, coords: torch.Tensor, features: torch.Tensor) -> torch.Tensor:\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 5 · 1 tool call(s) · 11.4s turn · 11.4s LLM · 6,533 chars (click to expand)
turn 511.4s turn · 11.4s LLM · 0.0s tools
call → run_correctness
callrun_correctness
{
"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>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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\n__global__ void center_kernel(const float* __restrict__ coords, float* __restrict__ centers, int N) {\n int b = blockIdx.x;\n int tid = threadIdx.x;\n float sx = 0.f, sy = 0.f, sz = 0.f;\n for (int n = tid; n < N; n += blockDim.x) {\n const float* p = coords + ((b * N + n) * 3);\n sx += p[0]; sy += p[1]; sz += p[2];\n }\n __shared__ float shx[256], shy[256], shz[256];\n shx[tid] = sx; shy[tid] = sy; shz[tid] = sz;\n __syncthreads();\n for (int s = 128; s > 0; s >>= 1) {\n if (tid < s) { shx[tid] += shx[tid+s]; shy[tid] += shy[tid+s]; shz[tid] += shz[tid+s]; }\n __syncthreads();\n }\n if (tid == 0) {\n float invN = 1.0f / (float)N;\n centers[b*3+0] = shx[0] * invN;\n centers[b*3+1] = shy[0] * invN;\n centers[b*3+2] = shz[0] * invN;\n }\n}\n\n__global__ void scatter_linear32_kernel(const float* __restrict__ coords,\n const float* __restrict__ features,\n const float* __restrict__ weight,\n const float* __restrict__ bias,\n const float* __restrict__ centers,\n float* __restrict__ out,\n int N, int G, float inv_voxel) {\n int atom = blockIdx.x;\n int b = blockIdx.y;\n int n = atom;\n int fd = threadIdx.x;\n const float* c = coords + ((b * N + n) * 3);\n float halfG = 0.5f * (float)G;\n int ix = (int)((c[0] - centers[b*3+0]) * inv_voxel + halfG);\n int iy = (int)((c[1] - centers[b*3+1]) * inv_voxel + halfG);\n int iz = (int)((c[2] - centers[b*3+2]) * inv_voxel + halfG);\n ix = max(0, min(ix, G-1));\n iy = max(0, min(iy, G-1));\n iz = max(0, min(iz, G-1));\n const float* x = features + ((b * N + n) * 32);\n const float* w = weight + fd * 32;\n float acc = bias[fd];\n#pragma unroll\n for (int j = 0; j < 32; ++j) acc = fmaf(x[j], w[j], acc);\n int vox = (ix * G + iy) * G + iz;\n int G3 = G * G * G;\n atomicAdd(out + ((b * 32 + fd) * G3 + vox), acc);\n}\n\n__global__ void scatter_linear_generic_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){\n int atom=blockIdx.x; int b=atom/N; int n=atom-b*N; int d=threadIdx.x;\n const float* c=coords+((b*N+n)*3); float halfG=0.5f*(float)G;\n int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG); int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG); int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);\n ix=max(0,min(ix,G-1)); iy=max(0,min(iy,G-1)); iz=max(0,min(iz,G-1));\n const float* x=features+((b*N+n)*F); int vox=(ix*G+iy)*G+iz; int G3=G*G*G;\n for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd]; const float*w=weight+fd*F; for(int j=0;j<F;++j) acc+=x[j]*w[j]; atomicAdd(out+((b*F+fd)*G3+vox),acc);} }\n\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size) {\n coords = coords.contiguous(); features = features.contiguous(); weight = weight.contiguous(); bias = bias.contiguous();\n int B = (int)coords.size(0); int N = (int)coords.size(1); int F = (int)features.size(2); int G = (int)grid_size;\n auto out = torch::empty({B, F, G, G, G}, coords.options());\n auto centers = torch::empty({B, 3}, coords.options());\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n cudaMemsetAsync(out.data_ptr<float>(), 0, (size_t)B*F*G*G*G*sizeof(float), stream);\n center_kernel<<<B, 256, 0, stream>>>(coords.data_ptr<float>(), centers.data_ptr<float>(), N);\n if (F == 32) {\n dim3 grid(N, B);\n scatter_linear32_kernel<<<grid, 32, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), N, G, (float)(1.0/voxel_size));\n } else {\n int threads = F > 32 ? 128 : 32;\n scatter_linear_generic_kernel<<<B*N, threads, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), B,N,F,G,(float)(1.0/voxel_size));\n }\n return out;\n}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_v2\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__(); self.feat_dim=feat_dim; self.grid_size=grid_size; self.voxel_size=voxel_size; self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self, coords: torch.Tensor, features: torch.Tensor) -> torch.Tensor:\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=7.15e-07 mean_abs_err=3.81e-10 max_rel_err=6.52e-03
turn 69.6s turn · 9.6s LLM · 0.0s tools
call → profile_kernel
callprofile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n_cpp_src=r\"\"\"\n#include <torch/extension.h>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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__global__ void center_kernel(const float* __restrict__ coords,float* __restrict__ centers,int N){int b=blockIdx.x;int tid=threadIdx.x;float sx=0.f,sy=0.f,sz=0.f;for(int n=tid;n<N;n+=blockDim.x){const float*p=coords+((b*N+n)*3);sx+=p[0];sy+=p[1];sz+=p[2];}__shared__ float shx[256],shy[256],shz[256];shx[tid]=sx;shy[tid]=sy;shz[tid]=sz;__syncthreads();for(int s=128;s>0;s>>=1){if(tid<s){shx[tid]+=shx[tid+s];shy[tid]+=shy[tid+s];shz[tid]+=shz[tid+s];}__syncthreads();}if(tid==0){float invN=1.0f/(float)N;centers[b*3]=shx[0]*invN;centers[b*3+1]=shy[0]*invN;centers[b*3+2]=shz[0]*invN;}}\n__global__ void scatter_linear32_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int N,int G,float inv_voxel){int atom=blockIdx.x;int b=blockIdx.y;int fd=threadIdx.x;const float*c=coords+((b*N+atom)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+atom)*32);const float*w=weight+fd*32;float acc=bias[fd];#pragma unroll\nfor(int j=0;j<32;++j)acc=fmaf(x[j],w[j],acc);int vox=(ix*G+iy)*G+iz;int G3=G*G*G;atomicAdd(out+((b*32+fd)*G3+vox),acc);}\n__global__ void scatter_linear_generic_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){int atom=blockIdx.x;int b=atom/N;int n=atom-b*N;int d=threadIdx.x;const float*c=coords+((b*N+n)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+n)*F);int vox=(ix*G+iy)*G+iz;int G3=G*G*G;for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd];const float*w=weight+fd*F;for(int j=0;j<F;++j)acc+=x[j]*w[j];atomicAdd(out+((b*F+fd)*G3+vox),acc);}}\ntorch::Tensor voxel_pool_forward(torch::Tensor coords,torch::Tensor features,torch::Tensor weight,torch::Tensor bias,int64_t grid_size,double voxel_size){coords=coords.contiguous();features=features.contiguous();weight=weight.contiguous();bias=bias.contiguous();int B=(int)coords.size(0);int N=(int)coords.size(1);int F=(int)features.size(2);int G=(int)grid_size;auto out=torch::empty({B,F,G,G,G},coords.options());auto centers=torch::empty({B,3},coords.options());cudaStream_t stream=at::cuda::getCurrentCUDAStream();cudaMemsetAsync(out.data_ptr<float>(),0,(size_t)B*F*G*G*G*sizeof(float),stream);center_kernel<<<B,256,0,stream>>>(coords.data_ptr<float>(),centers.data_ptr<float>(),N);if(F==32){dim3 grid(N,B);scatter_linear32_kernel<<<grid,32,0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),N,G,(float)(1.0/voxel_size));}else{int threads=F>32?128:32;scatter_linear_generic_kernel<<<B*N,threads,0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),B,N,F,G,(float)(1.0/voxel_size));}return out;}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_prof2\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self,feat_dim,grid_size=32,voxel_size=1.0):\n super().__init__();self.feat_dim=feat_dim;self.grid_size=grid_size;self.voxel_size=voxel_size;self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self,coords,features):\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← profile_kernel (fail)
failprofile_kernel
profile_kernel FAILED: worker exited with code 1.
^
2 errors detected in the compilation of "/scratch/abaner/KernelBench/runs/l12_all_gpt/popcorn/gpt-5.5-priority/level_1_problem_24_cache/k_f62d31610b5b/voxel_pool_ext_prof2/cuda.cu".
ninja: build stopped: subcommand failed.
turn 7 · 1 tool call(s) · 17.9s turn · 17.9s LLM · 8,167 chars (click to expand)
turn 717.9s turn · 17.9s LLM · 0.0s tools
call → profile_kernel
callprofile_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>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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\n__global__ void center_kernel(const float* __restrict__ coords, float* __restrict__ centers, int N) {\n int b = blockIdx.x;\n int tid = threadIdx.x;\n float sx = 0.f, sy = 0.f, sz = 0.f;\n for (int n = tid; n < N; n += blockDim.x) {\n const float* p = coords + ((b * N + n) * 3);\n sx += p[0]; sy += p[1]; sz += p[2];\n }\n __shared__ float shx[256], shy[256], shz[256];\n shx[tid] = sx; shy[tid] = sy; shz[tid] = sz;\n __syncthreads();\n for (int s = 128; s > 0; s >>= 1) {\n if (tid < s) { shx[tid] += shx[tid+s]; shy[tid] += shy[tid+s]; shz[tid] += shz[tid+s]; }\n __syncthreads();\n }\n if (tid == 0) {\n float invN = 1.0f / (float)N;\n centers[b*3+0] = shx[0] * invN;\n centers[b*3+1] = shy[0] * invN;\n centers[b*3+2] = shz[0] * invN;\n }\n}\n\n__global__ void scatter_linear32_kernel(const float* __restrict__ coords,\n const float* __restrict__ features,\n const float* __restrict__ weight,\n const float* __restrict__ bias,\n const float* __restrict__ centers,\n float* __restrict__ out,\n int N, int G, float inv_voxel) {\n int atom = blockIdx.x;\n int b = blockIdx.y;\n int fd = threadIdx.x;\n const float* c = coords + ((b * N + atom) * 3);\n float halfG = 0.5f * (float)G;\n int ix = (int)((c[0] - centers[b*3+0]) * inv_voxel + halfG);\n int iy = (int)((c[1] - centers[b*3+1]) * inv_voxel + halfG);\n int iz = (int)((c[2] - centers[b*3+2]) * inv_voxel + halfG);\n ix = max(0, min(ix, G-1));\n iy = max(0, min(iy, G-1));\n iz = max(0, min(iz, G-1));\n const float* x = features + ((b * N + atom) * 32);\n const float* w = weight + fd * 32;\n float acc = bias[fd];\n#pragma unroll\n for (int j = 0; j < 32; ++j) acc = fmaf(x[j], w[j], acc);\n int vox = (ix * G + iy) * G + iz;\n int G3 = G * G * G;\n atomicAdd(out + ((b * 32 + fd) * G3 + vox), acc);\n}\n\n__global__ void scatter_linear_generic_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){\n int atom=blockIdx.x; int b=atom/N; int n=atom-b*N; int d=threadIdx.x;\n const float* c=coords+((b*N+n)*3); float halfG=0.5f*(float)G;\n int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG); int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG); int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);\n ix=max(0,min(ix,G-1)); iy=max(0,min(iy,G-1)); iz=max(0,min(iz,G-1));\n const float* x=features+((b*N+n)*F); int vox=(ix*G+iy)*G+iz; int G3=G*G*G;\n for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd]; const float*w=weight+fd*F; for(int j=0;j<F;++j) acc+=x[j]*w[j]; atomicAdd(out+((b*F+fd)*G3+vox),acc);} }\n\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size) {\n coords = coords.contiguous(); features = features.contiguous(); weight = weight.contiguous(); bias = bias.contiguous();\n int B = (int)coords.size(0); int N = (int)coords.size(1); int F = (int)features.size(2); int G = (int)grid_size;\n auto out = torch::empty({B, F, G, G, G}, coords.options());\n auto centers = torch::empty({B, 3}, coords.options());\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n cudaMemsetAsync(out.data_ptr<float>(), 0, (size_t)B*F*G*G*G*sizeof(float), stream);\n center_kernel<<<B, 256, 0, stream>>>(coords.data_ptr<float>(), centers.data_ptr<float>(), N);\n if (F == 32) {\n dim3 grid(N, B);\n scatter_linear32_kernel<<<grid, 32, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), N, G, (float)(1.0/voxel_size));\n } else {\n int threads = F > 32 ? 128 : 32;\n scatter_linear_generic_kernel<<<B*N, threads, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), B,N,F,G,(float)(1.0/voxel_size));\n }\n return out;\n}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_prof2b\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__(); self.feat_dim=feat_dim; self.grid_size=grid_size; self.voxel_size=voxel_size; self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self, coords, features):\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← profile_kernel (ok)
okprofile_kernel
profile_kernel PASSED: profiling complete.
=== Kernel Profile Summary ===
--- Kernel Breakdown (by GPU time) ---
#1: aten::empty 1915.0μs (100%) calls=1
#2: aten::empty 9.0μs (0%) calls=1
--- Timing ---
GPU time: 11.46 μs [↓7.6μs ok]
--- Memory Bandwidth ---
DRAM BW: 195.3/3350 GB/s (5.8%) [↑0.0% ok]
read=195.3 write=0.0 GB/s
Hit rates: L2=168.8%
--- Compute Throughput ---
Pipe util: FMA=1.7%, Tensor=0.2%
--- Roofline Analysis ---
Ridge point (FP32): 20.00 FLOPs/byte
Classification: UNKNOWN
--- Occupancy ---
Achieved: 24.0% [↓69.4% REGRESSED]
Eligible warps/cycle: 0.11
WARNING: <1 eligible warp/cycle = latency-bound, needs more parallelism
Launch: regs/thread=49, smem/blk=5120B, block=288, grid=1028
--- Memory Access Quality ---
Global ld coalescing: 27.01 sectors/req (27.0x wasted BW)
--- Warp Stall Reasons (% of issue cycles) ---
long_scoreboard 5274.1% ####################
short_scoreboard 241.5% ####################
--- Optimization Hints ---
-> 5274% stalls: global memory latency (waiting for DRAM/L2). + sectors/req=27.0 -> uncoalesced global loads, restructure access pattern or use __ldg Try shared-memory tiling, prefetching, or reducing working set.
-> Global load coalescing=27.0x (ideal=1.0) -> 27x wasted DRAM BW. Make per-warp accesses contiguous.
-> Low occupancy (24%). Consider __launch_bounds__ or reducing register pressure.
-> Low eligible warps + low BW% + low compute% = LATENCY-BOUND. Need more parallelism (larger grid, more ILP, prefetching).
--- Delta vs Previous Iteration ---
GPU time: 19.10→11.46μs (↓7.65) ok
DRAM BW%: 5.82→5.83% (↑0.01) ok
Occupancy: 93.39→24.00% (↓69.39) REGRESSED
Eligible warps/cyc: 0.46→0.11 (↓0.35) REGRESSED
L2 hit%: 271.28→168.76% (↓102.52) REGRESSED
turn 8 · 1 tool call(s) · 23.3s turn · 23.3s LLM · 8,903 chars (click to expand)
turn 823.3s turn · 23.3s LLM · 0.0s tools
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n_cpp_src=r\"\"\"\n#include <torch/extension.h>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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__global__ void center_kernel(const float* __restrict__ coords,float* __restrict__ centers,int N){int b=blockIdx.x,tid=threadIdx.x;float sx=0,sy=0,sz=0;for(int n=tid;n<N;n+=256){const float*p=coords+((b*N+n)*3);sx+=p[0];sy+=p[1];sz+=p[2];}__shared__ float shx[256],shy[256],shz[256];shx[tid]=sx;shy[tid]=sy;shz[tid]=sz;__syncthreads();for(int s=128;s>0;s>>=1){if(tid<s){shx[tid]+=shx[tid+s];shy[tid]+=shy[tid+s];shz[tid]+=shz[tid+s];}__syncthreads();}if(tid==0){float inv=1.0f/(float)N;centers[b*3]=shx[0]*inv;centers[b*3+1]=shy[0]*inv;centers[b*3+2]=shz[0]*inv;}}\n__inline__ __device__ float warp_sum(float v){#pragma unroll\n for(int off=16;off>0;off>>=1) v+=__shfl_down_sync(0xffffffff,v,off); return v;}\n__global__ void scatter_warp32_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int N,int G,float inv_voxel){int group=blockIdx.x;int atom=blockIdx.y;int b=blockIdx.z;int lane=threadIdx.x&31;int warp=threadIdx.x>>5;int fd=group*8+warp;const float*c=coords+((b*N+atom)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+atom)*32);float prod=x[lane]*weight[fd*32+lane];float sum=warp_sum(prod);if(lane==0){sum+=bias[fd];int vox=(ix*G+iy)*G+iz;int G3=G*G*G;atomicAdd(out+((b*32+fd)*G3+vox),sum);}}\n__global__ void scatter_generic(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){int atom=blockIdx.x;int b=atom/N;int n=atom-b*N;int d=threadIdx.x;const float*c=coords+((b*N+n)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+n)*F);int vox=(ix*G+iy)*G+iz;int G3=G*G*G;for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd];const float*w=weight+fd*F;for(int j=0;j<F;++j)acc+=x[j]*w[j];atomicAdd(out+((b*F+fd)*G3+vox),acc);}}\ntorch::Tensor voxel_pool_forward(torch::Tensor coords,torch::Tensor features,torch::Tensor weight,torch::Tensor bias,int64_t grid_size,double voxel_size){coords=coords.contiguous();features=features.contiguous();weight=weight.contiguous();bias=bias.contiguous();int B=coords.size(0),N=coords.size(1),F=features.size(2),G=(int)grid_size;auto out=torch::empty({B,F,G,G,G},coords.options());auto centers=torch::empty({B,3},coords.options());cudaStream_t stream=at::cuda::getCurrentCUDAStream();cudaMemsetAsync(out.data_ptr<float>(),0,(size_t)B*F*G*G*G*sizeof(float),stream);center_kernel<<<B,256,0,stream>>>(coords.data_ptr<float>(),centers.data_ptr<float>(),N);if(F==32){dim3 grid(4,N,B);scatter_warp32_kernel<<<grid,256,0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),N,G,(float)(1.0/voxel_size));}else{scatter_generic<<<B*N,(F>32?128:32),0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),B,N,F,G,(float)(1.0/voxel_size));}return out;}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_warp\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__(); self.feat_dim=feat_dim; self.grid_size=grid_size; self.voxel_size=voxel_size; self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self, coords, features):\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← compile_kernel (fail)
failcompile_kernel
compile_kernel FAILED: RuntimeError.
RuntimeError: Error building extension 'voxel_pool_ext_warp': [1/3] /opt/conda/bin/x86_64-conda-linux-gnu-c++ -MMD -MF main.o.d -DTORCH_EXTENSION_NAME=voxel_pool_ext_warp -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -fPIC -std=c++17 -O3 -c /scratch/abaner/KernelBench/runs/l12_all_gpt/popcorn/gpt-5.5-priority/level_1_problem_24_cache/k_1c868fbdad16/voxel_pool_ext_warp/main.cpp -o main.o
[2/3] /usr/local/cuda/bin/nvcc -MD -MF cuda.cuda.o.d -ccbin /opt/conda/bin/x86_64-conda-linux-gnu-cc -DTORCH_EXTENSION_NAME=voxel_pool_ext_warp -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -gencode=arch=compute_90,code=compute_90 -gencode=arch=compute_90,code=sm_90 --compiler-options '-fPIC' -O3 --use_fast_math -std=c++17 -c /scratch/abaner/KernelBench/runs/l12_all_gpt/popcorn/gpt-5.5-priority/level_1_problem_24_cache/k_1c868fbdad16/voxel_pool_ext_warp/cuda.cu -o cuda.cuda.o
FAILED: [code=2] cuda.cuda.o
/usr/local/cuda/bin/nvcc -MD -MF cuda.cuda.o.d -ccbin /opt/conda/bin/x86_64-conda-linux-gnu-cc -DTORCH_EXTENSION_NAME=voxel_pool_ext_warp -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -gencode=arch=compute_90,code=compute_90 -gencode=arch=compute_90,code=sm_90 --compiler-options '-fPIC' -O3 --use_fast_math -std=c++17 -c /scratch/abaner/KernelBench/runs/l12_all_gpt/popcorn/gpt-5.5-priority/level_1_problem_24_cache/k_1c868fbdad16/voxel_pool_ext_warp/cuda.cu -o cuda.cuda.o
nvcc warning : incompatible redefinition for option 'compiler-bindir', the last value of this option was used
/scratch/abaner/KernelBench/runs/l12_all_gpt/popcorn/gpt-5.5-priority/level_1_problem_24_cache/k_1c868fbdad16/voxel_pool_ext_warp/cuda.cu(10): error: "#" not expected here
float warp_sum(float v){#pragma unroll
^
/scratch/abaner/KernelBench/runs/l12_all_gpt/popcorn/gpt-5.5-priority/level_1_problem_24_cache/k_1c868fbdad16/voxel_pool_ext_warp/cuda.cu(10): error: expected an expression
float warp_sum(float v){#pragma unroll
^
2 errors detected in the compilation of "/scratch/abaner/KernelBench/runs/l12_all_gpt/popcorn/gpt-5.5-priority/level_1_problem_24_cache/k_1c868fbdad16/voxel_pool_ext_warp/cuda.cu".
ninja: build stopped: subcommand failed.
turn 910.6s turn · 10.6s LLM · 0.0s tools
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n_cpp_src=r\"\"\"\n#include <torch/extension.h>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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__global__ void center_kernel(const float* __restrict__ coords,float* __restrict__ centers,int N){int b=blockIdx.x,tid=threadIdx.x;float sx=0,sy=0,sz=0;for(int n=tid;n<N;n+=256){const float*p=coords+((b*N+n)*3);sx+=p[0];sy+=p[1];sz+=p[2];}__shared__ float shx[256],shy[256],shz[256];shx[tid]=sx;shy[tid]=sy;shz[tid]=sz;__syncthreads();for(int s=128;s>0;s>>=1){if(tid<s){shx[tid]+=shx[tid+s];shy[tid]+=shy[tid+s];shz[tid]+=shz[tid+s];}__syncthreads();}if(tid==0){float inv=1.0f/(float)N;centers[b*3]=shx[0]*inv;centers[b*3+1]=shy[0]*inv;centers[b*3+2]=shz[0]*inv;}}\n__inline__ __device__ float warp_sum(float v){\n#pragma unroll\n for(int off=16;off>0;off>>=1) v+=__shfl_down_sync(0xffffffff,v,off); return v;}\n__global__ void scatter_warp32_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int N,int G,float inv_voxel){int group=blockIdx.x;int atom=blockIdx.y;int b=blockIdx.z;int lane=threadIdx.x&31;int warp=threadIdx.x>>5;int fd=group*8+warp;const float*c=coords+((b*N+atom)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+atom)*32);float prod=x[lane]*weight[fd*32+lane];float sum=warp_sum(prod);if(lane==0){sum+=bias[fd];int vox=(ix*G+iy)*G+iz;int G3=G*G*G;atomicAdd(out+((b*32+fd)*G3+vox),sum);}}\n__global__ void scatter_generic(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){int atom=blockIdx.x;int b=atom/N;int n=atom-b*N;int d=threadIdx.x;const float*c=coords+((b*N+n)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+n)*F);int vox=(ix*G+iy)*G+iz;int G3=G*G*G;for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd];const float*w=weight+fd*F;for(int j=0;j<F;++j)acc+=x[j]*w[j];atomicAdd(out+((b*F+fd)*G3+vox),acc);}}\ntorch::Tensor voxel_pool_forward(torch::Tensor coords,torch::Tensor features,torch::Tensor weight,torch::Tensor bias,int64_t grid_size,double voxel_size){coords=coords.contiguous();features=features.contiguous();weight=weight.contiguous();bias=bias.contiguous();int B=coords.size(0),N=coords.size(1),F=features.size(2),G=(int)grid_size;auto out=torch::empty({B,F,G,G,G},coords.options());auto centers=torch::empty({B,3},coords.options());cudaStream_t stream=at::cuda::getCurrentCUDAStream();cudaMemsetAsync(out.data_ptr<float>(),0,(size_t)B*F*G*G*G*sizeof(float),stream);center_kernel<<<B,256,0,stream>>>(coords.data_ptr<float>(),centers.data_ptr<float>(),N);if(F==32){dim3 grid(4,N,B);scatter_warp32_kernel<<<grid,256,0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),N,G,(float)(1.0/voxel_size));}else{scatter_generic<<<B*N,(F>32?128:32),0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),B,N,F,G,(float)(1.0/voxel_size));}return out;}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_warp\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__(); self.feat_dim=feat_dim; self.grid_size=grid_size; self.voxel_size=voxel_size; self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self, coords, features):\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 1012.0s turn · 12.0s LLM · 0.0s tools
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n_cpp_src=r\"\"\"\n#include <torch/extension.h>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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__global__ void center_kernel(const float* __restrict__ coords,float* __restrict__ centers,int N){int b=blockIdx.x,tid=threadIdx.x;float sx=0,sy=0,sz=0;for(int n=tid;n<N;n+=256){const float*p=coords+((b*N+n)*3);sx+=p[0];sy+=p[1];sz+=p[2];}__shared__ float shx[256],shy[256],shz[256];shx[tid]=sx;shy[tid]=sy;shz[tid]=sz;__syncthreads();for(int s=128;s>0;s>>=1){if(tid<s){shx[tid]+=shx[tid+s];shy[tid]+=shy[tid+s];shz[tid]+=shz[tid+s];}__syncthreads();}if(tid==0){float inv=1.0f/(float)N;centers[b*3]=shx[0]*inv;centers[b*3+1]=shy[0]*inv;centers[b*3+2]=shz[0]*inv;}}\n__inline__ __device__ float warp_sum(float v){\n#pragma unroll\n for(int off=16;off>0;off>>=1) v+=__shfl_down_sync(0xffffffff,v,off); return v;}\n__global__ void scatter_warp32_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int N,int G,float inv_voxel){int group=blockIdx.x;int atom=blockIdx.y;int b=blockIdx.z;int lane=threadIdx.x&31;int warp=threadIdx.x>>5;int fd=group*8+warp;const float*c=coords+((b*N+atom)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+atom)*32);float prod=x[lane]*weight[fd*32+lane];float sum=warp_sum(prod);if(lane==0){sum+=bias[fd];int vox=(ix*G+iy)*G+iz;int G3=G*G*G;atomicAdd(out+((b*32+fd)*G3+vox),sum);}}\n__global__ void scatter_generic(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){int atom=blockIdx.x;int b=atom/N;int n=atom-b*N;int d=threadIdx.x;const float*c=coords+((b*N+n)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+n)*F);int vox=(ix*G+iy)*G+iz;int G3=G*G*G;for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd];const float*w=weight+fd*F;for(int j=0;j<F;++j)acc+=x[j]*w[j];atomicAdd(out+((b*F+fd)*G3+vox),acc);}}\ntorch::Tensor voxel_pool_forward(torch::Tensor coords,torch::Tensor features,torch::Tensor weight,torch::Tensor bias,int64_t grid_size,double voxel_size){coords=coords.contiguous();features=features.contiguous();weight=weight.contiguous();bias=bias.contiguous();int B=coords.size(0),N=coords.size(1),F=features.size(2),G=(int)grid_size;auto out=torch::empty({B,F,G,G,G},coords.options());auto centers=torch::empty({B,3},coords.options());cudaStream_t stream=at::cuda::getCurrentCUDAStream();cudaMemsetAsync(out.data_ptr<float>(),0,(size_t)B*F*G*G*G*sizeof(float),stream);center_kernel<<<B,256,0,stream>>>(coords.data_ptr<float>(),centers.data_ptr<float>(),N);if(F==32){dim3 grid(4,N,B);scatter_warp32_kernel<<<grid,256,0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),N,G,(float)(1.0/voxel_size));}else{scatter_generic<<<B*N,(F>32?128:32),0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),B,N,F,G,(float)(1.0/voxel_size));}return out;}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_warp\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__(); self.feat_dim=feat_dim; self.grid_size=grid_size; self.voxel_size=voxel_size; self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self, coords, features):\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=4.77e-07 mean_abs_err=2.95e-10 max_rel_err=1.63e-02
turn 11 · 1 tool call(s) · 9.8s turn · 9.8s LLM · 7,233 chars (click to expand)
turn 119.8s turn · 9.8s LLM · 0.0s tools
call → profile_kernel
callprofile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n_cpp_src=r\"\"\"\n#include <torch/extension.h>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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__global__ void center_kernel(const float* __restrict__ coords,float* __restrict__ centers,int N){int b=blockIdx.x,tid=threadIdx.x;float sx=0,sy=0,sz=0;for(int n=tid;n<N;n+=256){const float*p=coords+((b*N+n)*3);sx+=p[0];sy+=p[1];sz+=p[2];}__shared__ float shx[256],shy[256],shz[256];shx[tid]=sx;shy[tid]=sy;shz[tid]=sz;__syncthreads();for(int s=128;s>0;s>>=1){if(tid<s){shx[tid]+=shx[tid+s];shy[tid]+=shy[tid+s];shz[tid]+=shz[tid+s];}__syncthreads();}if(tid==0){float inv=1.0f/(float)N;centers[b*3]=shx[0]*inv;centers[b*3+1]=shy[0]*inv;centers[b*3+2]=shz[0]*inv;}}\n__inline__ __device__ float warp_sum(float v){\n#pragma unroll\n for(int off=16;off>0;off>>=1) v+=__shfl_down_sync(0xffffffff,v,off); return v;}\n__global__ void scatter_warp32_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int N,int G,float inv_voxel){int group=blockIdx.x;int atom=blockIdx.y;int b=blockIdx.z;int lane=threadIdx.x&31;int warp=threadIdx.x>>5;int fd=group*8+warp;const float*c=coords+((b*N+atom)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+atom)*32);float prod=x[lane]*weight[fd*32+lane];float sum=warp_sum(prod);if(lane==0){sum+=bias[fd];int vox=(ix*G+iy)*G+iz;int G3=G*G*G;atomicAdd(out+((b*32+fd)*G3+vox),sum);}}\n__global__ void scatter_generic(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){int atom=blockIdx.x;int b=atom/N;int n=atom-b*N;int d=threadIdx.x;const float*c=coords+((b*N+n)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+n)*F);int vox=(ix*G+iy)*G+iz;int G3=G*G*G;for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd];const float*w=weight+fd*F;for(int j=0;j<F;++j)acc+=x[j]*w[j];atomicAdd(out+((b*F+fd)*G3+vox),acc);}}\ntorch::Tensor voxel_pool_forward(torch::Tensor coords,torch::Tensor features,torch::Tensor weight,torch::Tensor bias,int64_t grid_size,double voxel_size){coords=coords.contiguous();features=features.contiguous();weight=weight.contiguous();bias=bias.contiguous();int B=coords.size(0),N=coords.size(1),F=features.size(2),G=(int)grid_size;auto out=torch::empty({B,F,G,G,G},coords.options());auto centers=torch::empty({B,3},coords.options());cudaStream_t stream=at::cuda::getCurrentCUDAStream();cudaMemsetAsync(out.data_ptr<float>(),0,(size_t)B*F*G*G*G*sizeof(float),stream);center_kernel<<<B,256,0,stream>>>(coords.data_ptr<float>(),centers.data_ptr<float>(),N);if(F==32){dim3 grid(4,N,B);scatter_warp32_kernel<<<grid,256,0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),N,G,(float)(1.0/voxel_size));}else{scatter_generic<<<B*N,(F>32?128:32),0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),B,N,F,G,(float)(1.0/voxel_size));}return out;}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_warpprof\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self,feat_dim,grid_size=32,voxel_size=1.0):\n super().__init__();self.feat_dim=feat_dim;self.grid_size=grid_size;self.voxel_size=voxel_size;self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self,coords,features):\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← profile_kernel (ok)
okprofile_kernel
profile_kernel PASSED: profiling complete.
=== Kernel Profile Summary ===
--- Kernel Breakdown (by GPU time) ---
#1: aten::empty 2005.0μs (100%) calls=1
#2: aten::empty 10.0μs (0%) calls=1
--- Timing ---
GPU time: 10.46 μs [↓1.0μs ok]
--- Memory Bandwidth ---
DRAM BW: 223.5/3350 GB/s (6.7%) [↑0.8% ok]
read=223.5 write=0.0 GB/s
Hit rates: L2=169.0%
--- Compute Throughput ---
Dominant pipe: FP32 util=22.0%
Pipe util: FMA=22.0%, Tensor=0.2%
--- Roofline Analysis ---
Ridge point (FP32): 20.00 FLOPs/byte
Classification: COMPUTE-BOUND
--- Occupancy ---
Achieved: 91.2% [↑67.2% ok]
Eligible warps/cycle: 1.87
Launch: regs/thread=54, smem/blk=5120B, block=512, grid=4100
--- Memory Access Quality ---
Global ld coalescing: 13.67 sectors/req (13.7x wasted BW)
--- Warp Stall Reasons (% of issue cycles) ---
long_scoreboard 1290.5% ####################
short_scoreboard 434.0% ####################
mio_throttle 261.8% ####################
--- Optimization Hints ---
-> 1291% stalls: global memory latency (waiting for DRAM/L2). + sectors/req=13.7 -> uncoalesced global loads, restructure access pattern or use __ldg Try shared-memory tiling, prefetching, or reducing working set.
-> Global load coalescing=13.7x (ideal=1.0) -> 14x wasted DRAM BW. Make per-warp accesses contiguous.
-> Compute-bound but NOT using tensor cores. If applicable, use FP16/BF16 with torch.matmul or WMMA intrinsics.
--- Delta vs Previous Iteration ---
GPU time: 11.46→10.46μs (↓0.99) ok
DRAM BW%: 5.83→6.67% (↑0.84) ok
Occupancy: 24.00→91.22% (↑67.22) ok
Eligible warps/cyc: 0.11→1.87 (↑1.76) ok
Ld sectors/req: 27.01→13.67 (↓13.35) ok
L2 hit%: 168.76→169.00% (↑0.24) ok
turn 1216.7s turn · 16.7s LLM · 0.0s tools
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n_cpp_src=r\"\"\"\n#include <torch/extension.h>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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__global__ void center_kernel(const float* __restrict__ coords,float* __restrict__ centers,int N){int b=blockIdx.x,tid=threadIdx.x;float sx=0,sy=0,sz=0;for(int n=tid;n<N;n+=256){const float*p=coords+((b*N+n)*3);sx+=p[0];sy+=p[1];sz+=p[2];}__shared__ float shx[256],shy[256],shz[256];shx[tid]=sx;shy[tid]=sy;shz[tid]=sz;__syncthreads();for(int s=128;s>0;s>>=1){if(tid<s){shx[tid]+=shx[tid+s];shy[tid]+=shy[tid+s];shz[tid]+=shz[tid+s];}__syncthreads();}if(tid==0){float inv=1.0f/(float)N;centers[b*3]=shx[0]*inv;centers[b*3+1]=shy[0]*inv;centers[b*3+2]=shz[0]*inv;}}\n__inline__ __device__ float warp_sum(float v){\n#pragma unroll\n for(int off=16;off>0;off>>=1) v+=__shfl_down_sync(0xffffffff,v,off); return v;}\n__global__ void scatter_warp32_sh_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int N,int G,float inv_voxel){int group=blockIdx.x;int atom=blockIdx.y;int b=blockIdx.z;int tid=threadIdx.x;int lane=tid&31;int warp=tid>>5;int fd=group*8+warp;__shared__ float xs[32];if(tid<32) xs[tid]=features[(b*N+atom)*32+tid];__syncthreads();const float*c=coords+((b*N+atom)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));float sum=warp_sum(xs[lane]*weight[fd*32+lane]);if(lane==0){sum+=bias[fd];int vox=(ix*G+iy)*G+iz;int G3=G*G*G;atomicAdd(out+((b*32+fd)*G3+vox),sum);}}\n__global__ void scatter_generic(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){int atom=blockIdx.x;int b=atom/N;int n=atom-b*N;int d=threadIdx.x;const float*c=coords+((b*N+n)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+n)*F);int vox=(ix*G+iy)*G+iz;int G3=G*G*G;for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd];const float*w=weight+fd*F;for(int j=0;j<F;++j)acc+=x[j]*w[j];atomicAdd(out+((b*F+fd)*G3+vox),acc);}}\ntorch::Tensor voxel_pool_forward(torch::Tensor coords,torch::Tensor features,torch::Tensor weight,torch::Tensor bias,int64_t grid_size,double voxel_size){coords=coords.contiguous();features=features.contiguous();weight=weight.contiguous();bias=bias.contiguous();int B=coords.size(0),N=coords.size(1),F=features.size(2),G=(int)grid_size;auto out=torch::empty({B,F,G,G,G},coords.options());auto centers=torch::empty({B,3},coords.options());cudaStream_t stream=at::cuda::getCurrentCUDAStream();cudaMemsetAsync(out.data_ptr<float>(),0,(size_t)B*F*G*G*G*sizeof(float),stream);center_kernel<<<B,256,0,stream>>>(coords.data_ptr<float>(),centers.data_ptr<float>(),N);if(F==32){dim3 grid(4,N,B);scatter_warp32_sh_kernel<<<grid,256,0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),N,G,(float)(1.0/voxel_size));}else{scatter_generic<<<B*N,(F>32?128:32),0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),B,N,F,G,(float)(1.0/voxel_size));}return out;}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_sh\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__(); self.feat_dim=feat_dim; self.grid_size=grid_size; self.voxel_size=voxel_size; self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self, coords, features):\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 1311.0s turn · 11.0s LLM · 0.0s tools
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n_cpp_src=r\"\"\"\n#include <torch/extension.h>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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__global__ void center_kernel(const float* __restrict__ coords,float* __restrict__ centers,int N){int b=blockIdx.x,tid=threadIdx.x;float sx=0,sy=0,sz=0;for(int n=tid;n<N;n+=256){const float*p=coords+((b*N+n)*3);sx+=p[0];sy+=p[1];sz+=p[2];}__shared__ float shx[256],shy[256],shz[256];shx[tid]=sx;shy[tid]=sy;shz[tid]=sz;__syncthreads();for(int s=128;s>0;s>>=1){if(tid<s){shx[tid]+=shx[tid+s];shy[tid]+=shy[tid+s];shz[tid]+=shz[tid+s];}__syncthreads();}if(tid==0){float inv=1.0f/(float)N;centers[b*3]=shx[0]*inv;centers[b*3+1]=shy[0]*inv;centers[b*3+2]=shz[0]*inv;}}\n__inline__ __device__ float warp_sum(float v){\n#pragma unroll\n for(int off=16;off>0;off>>=1) v+=__shfl_down_sync(0xffffffff,v,off); return v;}\n__global__ void scatter_warp32_sh_kernel(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int N,int G,float inv_voxel){int group=blockIdx.x;int atom=blockIdx.y;int b=blockIdx.z;int tid=threadIdx.x;int lane=tid&31;int warp=tid>>5;int fd=group*8+warp;__shared__ float xs[32];if(tid<32) xs[tid]=features[(b*N+atom)*32+tid];__syncthreads();const float*c=coords+((b*N+atom)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));float sum=warp_sum(xs[lane]*weight[fd*32+lane]);if(lane==0){sum+=bias[fd];int vox=(ix*G+iy)*G+iz;int G3=G*G*G;atomicAdd(out+((b*32+fd)*G3+vox),sum);}}\n__global__ void scatter_generic(const float* __restrict__ coords,const float* __restrict__ features,const float* __restrict__ weight,const float* __restrict__ bias,const float* __restrict__ centers,float* __restrict__ out,int B,int N,int F,int G,float inv_voxel){int atom=blockIdx.x;int b=atom/N;int n=atom-b*N;int d=threadIdx.x;const float*c=coords+((b*N+n)*3);float halfG=0.5f*(float)G;int ix=(int)((c[0]-centers[b*3])*inv_voxel+halfG);int iy=(int)((c[1]-centers[b*3+1])*inv_voxel+halfG);int iz=(int)((c[2]-centers[b*3+2])*inv_voxel+halfG);ix=max(0,min(ix,G-1));iy=max(0,min(iy,G-1));iz=max(0,min(iz,G-1));const float*x=features+((b*N+n)*F);int vox=(ix*G+iy)*G+iz;int G3=G*G*G;for(int fd=d;fd<F;fd+=blockDim.x){float acc=bias[fd];const float*w=weight+fd*F;for(int j=0;j<F;++j)acc+=x[j]*w[j];atomicAdd(out+((b*F+fd)*G3+vox),acc);}}\ntorch::Tensor voxel_pool_forward(torch::Tensor coords,torch::Tensor features,torch::Tensor weight,torch::Tensor bias,int64_t grid_size,double voxel_size){coords=coords.contiguous();features=features.contiguous();weight=weight.contiguous();bias=bias.contiguous();int B=coords.size(0),N=coords.size(1),F=features.size(2),G=(int)grid_size;auto out=torch::empty({B,F,G,G,G},coords.options());auto centers=torch::empty({B,3},coords.options());cudaStream_t stream=at::cuda::getCurrentCUDAStream();cudaMemsetAsync(out.data_ptr<float>(),0,(size_t)B*F*G*G*G*sizeof(float),stream);center_kernel<<<B,256,0,stream>>>(coords.data_ptr<float>(),centers.data_ptr<float>(),N);if(F==32){dim3 grid(4,N,B);scatter_warp32_sh_kernel<<<grid,256,0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),N,G,(float)(1.0/voxel_size));}else{scatter_generic<<<B*N,(F>32?128:32),0,stream>>>(coords.data_ptr<float>(),features.data_ptr<float>(),weight.data_ptr<float>(),bias.data_ptr<float>(),centers.data_ptr<float>(),out.data_ptr<float>(),B,N,F,G,(float)(1.0/voxel_size));}return out;}\n\"\"\"\n_ext=load_inline(name=\"voxel_pool_ext_sh\",cpp_sources=_cpp_src,cuda_sources=_cuda_src,functions=[\"voxel_pool_forward\"],extra_cuda_cflags=[\"-O3\",\"--use_fast_math\"],extra_cflags=[\"-O3\"],verbose=False)\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__(); self.feat_dim=feat_dim; self.grid_size=grid_size; self.voxel_size=voxel_size; self.linear=nn.Linear(feat_dim,feat_dim)\n def forward(self, coords, features):\n return _ext.voxel_pool_forward(coords,features,self.linear.weight,self.linear.bias,self.grid_size,float(self.voxel_size))\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=4.77e-07 mean_abs_err=2.95e-10 max_rel_err=1.63e-02
turn 14 · FINAL · 1 tool call(s) · 20.3s turn · 20.3s LLM · 7,840 chars (click to expand)
turn 1420.3s turn · 20.3s 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>\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);\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\n__global__ void center_kernel(const float* __restrict__ coords, float* __restrict__ centers, int N) {\n int b = blockIdx.x, tid = threadIdx.x;\n float sx = 0.f, sy = 0.f, sz = 0.f;\n for (int n = tid; n < N; n += 256) {\n const float* p = coords + ((b * N + n) * 3);\n sx += p[0]; sy += p[1]; sz += p[2];\n }\n __shared__ float shx[256], shy[256], shz[256];\n shx[tid] = sx; shy[tid] = sy; shz[tid] = sz;\n __syncthreads();\n for (int s = 128; s > 0; s >>= 1) {\n if (tid < s) {\n shx[tid] += shx[tid+s];\n shy[tid] += shy[tid+s];\n shz[tid] += shz[tid+s];\n }\n __syncthreads();\n }\n if (tid == 0) {\n float inv = 1.0f / (float)N;\n centers[b*3+0] = shx[0] * inv;\n centers[b*3+1] = shy[0] * inv;\n centers[b*3+2] = shz[0] * inv;\n }\n}\n\n__inline__ __device__ float warp_sum(float v) {\n#pragma unroll\n for (int off = 16; off > 0; off >>= 1) v += __shfl_down_sync(0xffffffff, v, off);\n return v;\n}\n\n__global__ void scatter_warp32_sh_kernel(const float* __restrict__ coords,\n const float* __restrict__ features,\n const float* __restrict__ weight,\n const float* __restrict__ bias,\n const float* __restrict__ centers,\n float* __restrict__ out,\n int N, int G, float inv_voxel) {\n int group = blockIdx.x; // 0..3, eight output channels per block\n int atom = blockIdx.y;\n int b = blockIdx.z;\n int tid = threadIdx.x;\n int lane = tid & 31;\n int warp = tid >> 5;\n int fd = group * 8 + warp;\n\n __shared__ float xs[32];\n if (tid < 32) xs[tid] = features[(b * N + atom) * 32 + tid];\n __syncthreads();\n\n const float* c = coords + ((b * N + atom) * 3);\n float halfG = 0.5f * (float)G;\n int ix = (int)((c[0] - centers[b*3+0]) * inv_voxel + halfG);\n int iy = (int)((c[1] - centers[b*3+1]) * inv_voxel + halfG);\n int iz = (int)((c[2] - centers[b*3+2]) * inv_voxel + halfG);\n ix = max(0, min(ix, G - 1));\n iy = max(0, min(iy, G - 1));\n iz = max(0, min(iz, G - 1));\n\n float sum = warp_sum(xs[lane] * weight[fd * 32 + lane]);\n if (lane == 0) {\n sum += bias[fd];\n int vox = (ix * G + iy) * G + iz;\n int G3 = G * G * G;\n atomicAdd(out + ((b * 32 + fd) * G3 + vox), sum);\n }\n}\n\n__global__ void scatter_generic_kernel(const float* __restrict__ coords,\n const float* __restrict__ features,\n const float* __restrict__ weight,\n const float* __restrict__ bias,\n const float* __restrict__ centers,\n float* __restrict__ out,\n int B, int N, int F, int G, float inv_voxel) {\n int atom = blockIdx.x;\n int b = atom / N;\n int n = atom - b * N;\n int d = threadIdx.x;\n const float* c = coords + ((b * N + n) * 3);\n float halfG = 0.5f * (float)G;\n int ix = (int)((c[0] - centers[b*3+0]) * inv_voxel + halfG);\n int iy = (int)((c[1] - centers[b*3+1]) * inv_voxel + halfG);\n int iz = (int)((c[2] - centers[b*3+2]) * inv_voxel + halfG);\n ix = max(0, min(ix, G - 1));\n iy = max(0, min(iy, G - 1));\n iz = max(0, min(iz, G - 1));\n const float* x = features + ((b * N + n) * F);\n int vox = (ix * G + iy) * G + iz;\n int G3 = G * G * G;\n for (int fd = d; fd < F; fd += blockDim.x) {\n float acc = bias[fd];\n const float* w = weight + fd * F;\n for (int j = 0; j < F; ++j) acc += x[j] * w[j];\n atomicAdd(out + ((b * F + fd) * G3 + vox), acc);\n }\n}\n\ntorch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size) {\n coords = coords.contiguous();\n features = features.contiguous();\n weight = weight.contiguous();\n bias = bias.contiguous();\n int B = (int)coords.size(0);\n int N = (int)coords.size(1);\n int F = (int)features.size(2);\n int G = (int)grid_size;\n auto out = torch::empty({B, F, G, G, G}, coords.options());\n auto centers = torch::empty({B, 3}, coords.options());\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n cudaMemsetAsync(out.data_ptr<float>(), 0, (size_t)B * F * G * G * G * sizeof(float), stream);\n center_kernel<<<B, 256, 0, stream>>>(coords.data_ptr<float>(), centers.data_ptr<float>(), N);\n if (F == 32) {\n dim3 grid(4, N, B);\n scatter_warp32_sh_kernel<<<grid, 256, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), N, G, (float)(1.0 / voxel_size));\n } else {\n scatter_generic_kernel<<<B * N, (F > 32 ? 128 : 32), 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), B, N, F, G, (float)(1.0 / voxel_size));\n }\n return out;\n}\n\"\"\"\n\n_ext = load_inline(name=\"voxel_pool_ext_final\", cpp_sources=_cpp_src, cuda_sources=_cuda_src, functions=[\"voxel_pool_forward\"], extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"], extra_cflags=[\"-O3\"], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):\n super().__init__()\n self.feat_dim = feat_dim\n self.grid_size = grid_size\n self.voxel_size = voxel_size\n self.linear = nn.Linear(feat_dim, feat_dim)\n\n def forward(self, coords: torch.Tensor, features: torch.Tensor) -> torch.Tensor:\n return _ext.voxel_pool_forward(coords, features, self.linear.weight, self.linear.bias, self.grid_size, float(self.voxel_size))\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 0.02 μs
Runtime stats: mean=0.02μs median=?μs std=0.00μs
Numerical precision: max_abs_err=4.77e-07 mean_abs_err=2.95e-10 max_rel_err=1.63e-02
Memory: 49.15 MB peak (ref: 49.85 MB, ratio: 0.9859x)
Kernel launches: 2 (ref: 4484, fusion ratio: 2242.0)
SOL score: 0.2095
Flagged for excessive speedup — this submission may have been rejected by automated review.
final submitted kernel (151 lines, 6,401 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 voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size);
"""
_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda.h>
#include <cuda_runtime.h>
__global__ void center_kernel(const float* __restrict__ coords, float* __restrict__ centers, int N) {
int b = blockIdx.x, tid = threadIdx.x;
float sx = 0.f, sy = 0.f, sz = 0.f;
for (int n = tid; n < N; n += 256) {
const float* p = coords + ((b * N + n) * 3);
sx += p[0]; sy += p[1]; sz += p[2];
}
__shared__ float shx[256], shy[256], shz[256];
shx[tid] = sx; shy[tid] = sy; shz[tid] = sz;
__syncthreads();
for (int s = 128; s > 0; s >>= 1) {
if (tid < s) {
shx[tid] += shx[tid+s];
shy[tid] += shy[tid+s];
shz[tid] += shz[tid+s];
}
__syncthreads();
}
if (tid == 0) {
float inv = 1.0f / (float)N;
centers[b*3+0] = shx[0] * inv;
centers[b*3+1] = shy[0] * inv;
centers[b*3+2] = shz[0] * inv;
}
}
__inline__ __device__ float warp_sum(float v) {
#pragma unroll
for (int off = 16; off > 0; off >>= 1) v += __shfl_down_sync(0xffffffff, v, off);
return v;
}
__global__ void scatter_warp32_sh_kernel(const float* __restrict__ coords,
const float* __restrict__ features,
const float* __restrict__ weight,
const float* __restrict__ bias,
const float* __restrict__ centers,
float* __restrict__ out,
int N, int G, float inv_voxel) {
int group = blockIdx.x; // 0..3, eight output channels per block
int atom = blockIdx.y;
int b = blockIdx.z;
int tid = threadIdx.x;
int lane = tid & 31;
int warp = tid >> 5;
int fd = group * 8 + warp;
__shared__ float xs[32];
if (tid < 32) xs[tid] = features[(b * N + atom) * 32 + tid];
__syncthreads();
const float* c = coords + ((b * N + atom) * 3);
float halfG = 0.5f * (float)G;
int ix = (int)((c[0] - centers[b*3+0]) * inv_voxel + halfG);
int iy = (int)((c[1] - centers[b*3+1]) * inv_voxel + halfG);
int iz = (int)((c[2] - centers[b*3+2]) * inv_voxel + halfG);
ix = max(0, min(ix, G - 1));
iy = max(0, min(iy, G - 1));
iz = max(0, min(iz, G - 1));
float sum = warp_sum(xs[lane] * weight[fd * 32 + lane]);
if (lane == 0) {
sum += bias[fd];
int vox = (ix * G + iy) * G + iz;
int G3 = G * G * G;
atomicAdd(out + ((b * 32 + fd) * G3 + vox), sum);
}
}
__global__ void scatter_generic_kernel(const float* __restrict__ coords,
const float* __restrict__ features,
const float* __restrict__ weight,
const float* __restrict__ bias,
const float* __restrict__ centers,
float* __restrict__ out,
int B, int N, int F, int G, float inv_voxel) {
int atom = blockIdx.x;
int b = atom / N;
int n = atom - b * N;
int d = threadIdx.x;
const float* c = coords + ((b * N + n) * 3);
float halfG = 0.5f * (float)G;
int ix = (int)((c[0] - centers[b*3+0]) * inv_voxel + halfG);
int iy = (int)((c[1] - centers[b*3+1]) * inv_voxel + halfG);
int iz = (int)((c[2] - centers[b*3+2]) * inv_voxel + halfG);
ix = max(0, min(ix, G - 1));
iy = max(0, min(iy, G - 1));
iz = max(0, min(iz, G - 1));
const float* x = features + ((b * N + n) * F);
int vox = (ix * G + iy) * G + iz;
int G3 = G * G * G;
for (int fd = d; fd < F; fd += blockDim.x) {
float acc = bias[fd];
const float* w = weight + fd * F;
for (int j = 0; j < F; ++j) acc += x[j] * w[j];
atomicAdd(out + ((b * F + fd) * G3 + vox), acc);
}
}
torch::Tensor voxel_pool_forward(torch::Tensor coords, torch::Tensor features, torch::Tensor weight, torch::Tensor bias, int64_t grid_size, double voxel_size) {
coords = coords.contiguous();
features = features.contiguous();
weight = weight.contiguous();
bias = bias.contiguous();
int B = (int)coords.size(0);
int N = (int)coords.size(1);
int F = (int)features.size(2);
int G = (int)grid_size;
auto out = torch::empty({B, F, G, G, G}, coords.options());
auto centers = torch::empty({B, 3}, coords.options());
cudaStream_t stream = at::cuda::getCurrentCUDAStream();
cudaMemsetAsync(out.data_ptr<float>(), 0, (size_t)B * F * G * G * G * sizeof(float), stream);
center_kernel<<<B, 256, 0, stream>>>(coords.data_ptr<float>(), centers.data_ptr<float>(), N);
if (F == 32) {
dim3 grid(4, N, B);
scatter_warp32_sh_kernel<<<grid, 256, 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), N, G, (float)(1.0 / voxel_size));
} else {
scatter_generic_kernel<<<B * N, (F > 32 ? 128 : 32), 0, stream>>>(coords.data_ptr<float>(), features.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(), centers.data_ptr<float>(), out.data_ptr<float>(), B, N, F, G, (float)(1.0 / voxel_size));
}
return out;
}
"""
_ext = load_inline(name="voxel_pool_ext_final", cpp_sources=_cpp_src, cuda_sources=_cuda_src, functions=["voxel_pool_forward"], extra_cuda_cflags=["-O3", "--use_fast_math"], extra_cflags=["-O3"], verbose=False)
class ModelNew(nn.Module):
def __init__(self, feat_dim, grid_size=32, voxel_size=1.0):
super().__init__()
self.feat_dim = feat_dim
self.grid_size = grid_size
self.voxel_size = voxel_size
self.linear = nn.Linear(feat_dim, feat_dim)
def forward(self, coords: torch.Tensor, features: torch.Tensor) -> torch.Tensor:
return _ext.voxel_pool_forward(coords, features, self.linear.weight, self.linear.bias, self.grid_size, float(self.voxel_size))