correct
15_COOScatterAddNodeFeatures.py
started
2026-05-11T22:20:43Z
timingkernelbench.timing
kernel mean / median / std
0.03 / — / 6.87e-04 μs
ref mean / median / std
0.02 / — / 3.30e-04 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
1.12x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
202.94 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):
"""
Scatter-add edge features into destination-node accumulators.
This is a canonical graph primitive for COO-formatted workloads where edge
messages are already materialized and must be reduced into node states.
"""
def __init__(self, num_nodes):
super().__init__()
self.num_nodes = num_nodes
def forward(
self,
dst_idx: torch.Tensor,
edge_feat: torch.Tensor,
) -> torch.Tensor:
feat_dim = edge_feat.shape[1]
out = torch.zeros(self.num_nodes, feat_dim, dtype=edge_feat.dtype, device=edge_feat.device)
out.index_add_(0, dst_idx.long(), edge_feat)
return out
num_nodes = 768
num_edges = 16384
feat_dim = 64
def get_inputs():
p = popcorn_pri
n_edges = p.jitter_int(num_edges)
fd = p.jitter_int(feat_dim, align=8)
dst_idx = torch.randint(0, num_nodes, (n_edges,), dtype=torch.int32)
edge_feat = torch.randn(n_edges, fd, dtype=torch.float32)
return [dst_idx, edge_feat]
def get_init_inputs():
return [num_nodes]
turn 0134.7s turn · 11.6s LLM · 123.1s tools · tokens in=2,004 · out=452 · total=3,064
reasoning
The task is to replace the forward computation of a PyTorch model with a custom CUDA kernel. The model does a scatter-add: it takes destination indices and edge features, and accumulates the edge feat...I need to replace the forward computation of a Pyron PyTorch model with a custom CUDA kernel. The model accumulates edge features into node accumulators using scatter-add, specifically summing edge_feat[i] into out[dst_idx[i]] for each i, initializing out as zeros(num_nodes, feat_dim).
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n\n__global__ void scatter_add_kernel(const int* __restrict__ dst_idx,\n const float* __restrict__ edge_feat,\n float* __restrict__ out,\n int num_edges, int feat_dim) {\n int edge = blockIdx.x * blockDim.x + threadIdx.x;\n if (edge < num_edges) {\n int dst = dst_idx[edge];\n const float* src = edge_feat + edge * feat_dim;\n float* dst_ptr = out + dst * feat_dim;\n for (int f = 0; f < feat_dim; ++f) {\n atomicAdd(dst_ptr + f, src[f]);\n }\n }\n}\n\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes) {\n int num_edges = dst_idx.size(0);\n int feat_dim = edge_feat.size(1);\n auto out = torch::zeros({num_nodes, feat_dim}, edge_feat.options());\n const int threads = 256;\n const int blocks = (num_edges + threads - 1) / threads;\n scatter_add_kernel<<<blocks, threads>>>(\n dst_idx.data_ptr<int>(),\n edge_feat.data_ptr<float>(),\n out.data_ptr<float>(),\n num_edges, feat_dim);\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes);\n\"\"\"\n\nscatter_add = load_inline(\n name=\"scatter_add\",\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=[\"scatter_add_cuda\"],\n verbose=True,\n with_cuda=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, num_nodes):\n super().__init__()\n self.num_nodes = num_nodes\n\n def forward(self, dst_idx: torch.Tensor, edge_feat: torch.Tensor) -> torch.Tensor:\n return scatter_add.scatter_add_cuda(dst_idx, edge_feat, self.num_nodes)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 14.0s turn · 4.0s LLM · 0.0s tools · tokens in=3,081 · out=453 · total=3,541
reasoning
Compiled successfully. Now check correctness.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n\n__global__ void scatter_add_kernel(const int* __restrict__ dst_idx,\n const float* __restrict__ edge_feat,\n float* __restrict__ out,\n int num_edges, int feat_dim) {\n int edge = blockIdx.x * blockDim.x + threadIdx.x;\n if (edge < num_edges) {\n int dst = dst_idx[edge];\n const float* src = edge_feat + edge * feat_dim;\n float* dst_ptr = out + dst * feat_dim;\n for (int f = 0; f < feat_dim; ++f) {\n atomicAdd(dst_ptr + f, src[f]);\n }\n }\n}\n\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes) {\n int num_edges = dst_idx.size(0);\n int feat_dim = edge_feat.size(1);\n auto out = torch::zeros({num_nodes, feat_dim}, edge_feat.options());\n const int threads = 256;\n const int blocks = (num_edges + threads - 1) / threads;\n scatter_add_kernel<<<blocks, threads>>>(\n dst_idx.data_ptr<int>(),\n edge_feat.data_ptr<float>(),\n out.data_ptr<float>(),\n num_edges, feat_dim);\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes);\n\"\"\"\n\nscatter_add = load_inline(\n name=\"scatter_add\",\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=[\"scatter_add_cuda\"],\n verbose=True,\n with_cuda=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, num_nodes):\n super().__init__()\n self.num_nodes = num_nodes\n\n def forward(self, dst_idx: torch.Tensor, edge_feat: torch.Tensor) -> torch.Tensor:\n return scatter_add.scatter_add_cuda(dst_idx, edge_feat, self.num_nodes)\n"
}
result ← run_correctness (fail)
failrun_correctness
run_correctness FAILED: ? trials did not all match.
runtime_error: expected scalar type Int but found Float
turn 280.4s turn · 4.9s LLM · 75.4s tools · tokens in=3,570 · out=469 · total=4,202
reasoning
Error: dst_idx is int32 but kernel expects int, which might be 32-bit, but perhaps casting.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cstdint>\n\n__global__ void scatter_add_kernel(const int32_t* __restrict__ dst_idx,\n const float* __restrict__ edge_feat,\n float* __restrict__ out,\n int num_edges, int feat_dim) {\n int edge = blockIdx.x * blockDim.x + threadIdx.x;\n if (edge < num_edges) {\n int32_t dst = dst_idx[edge];\n const float* src = edge_feat + edge * feat_dim;\n float* dst_ptr = out + (int64_t)dst * feat_dim;\n for (int f = 0; f < feat_dim; ++f) {\n atomicAdd(dst_ptr + f, src[f]);\n }\n }\n}\n\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes) {\n int num_edges = dst_idx.size(0);\n int feat_dim = edge_feat.size(1);\n auto out = torch::zeros({num_nodes, feat_dim}, edge_feat.options());\n const int threads = 256;\n const int blocks = (num_edges + threads - 1) / threads;\n scatter_add_kernel<<<blocks, threads>>>(\n dst_idx.data_ptr<int32_t>(),\n edge_feat.data_ptr<float>(),\n out.data_ptr<float>(),\n num_edges, feat_dim);\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes);\n\"\"\"\n\nscatter_add = load_inline(\n name=\"scatter_add\",\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=[\"scatter_add_cuda\"],\n verbose=True,\n with_cuda=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, num_nodes):\n super().__init__()\n self.num_nodes = num_nodes\n\n def forward(self, dst_idx: torch.Tensor, edge_feat: torch.Tensor) -> torch.Tensor:\n return scatter_add.scatter_add_cuda(dst_idx, edge_feat, self.num_nodes)\n"
}
result ← run_correctness (fail)
failrun_correctness
run_correctness FAILED: ? trials did not all match.
runtime_error: expected scalar type Int but found Float
turn 382.0s turn · 4.9s LLM · 77.0s tools · tokens in=4,231 · out=481 · total=4,873
reasoning
Still error. The error is strange. Perhaps inside load or something. The error mentions scalar type Int but Float – maybe dst_idx needs to be long?
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cstdint>\n\n__global__ void scatter_add_kernel(const int64_t* __restrict__ dst_idx,\n const float* __restrict__ edge_feat,\n float* __restrict__ out,\n int num_edges, int feat_dim) {\n int edge = blockIdx.x * blockDim.x + threadIdx.x;\n if (edge < num_edges) {\n int64_t dst = dst_idx[edge];\n const float* src = edge_feat + edge * feat_dim;\n float* dst_ptr = out + dst * feat_dim;\n for (int f = 0; f < feat_dim; ++f) {\n atomicAdd(dst_ptr + f, src[f]);\n }\n }\n}\n\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes) {\n auto dst_idx_long = dst_idx.to(torch::kInt64);\n int num_edges = dst_idx_long.size(0);\n int feat_dim = edge_feat.size(1);\n auto out = torch::zeros({num_nodes, feat_dim}, edge_feat.options());\n const int threads = 256;\n const int blocks = (num_edges + threads - 1) / threads;\n scatter_add_kernel<<<blocks, threads>>>(\n dst_idx_long.data_ptr<int64_t>(),\n edge_feat.data_ptr<float>(),\n out.data_ptr<float>(),\n num_edges, feat_dim);\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes);\n\"\"\"\n\nscatter_add = load_inline(\n name=\"scatter_add\",\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=[\"scatter_add_cuda\"],\n verbose=True,\n with_cuda=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, num_nodes):\n super().__init__()\n self.num_nodes = num_nodes\n\n def forward(self, dst_idx: torch.Tensor, edge_feat: torch.Tensor) -> torch.Tensor:\n return scatter_add.scatter_add_cuda(dst_idx, edge_feat, self.num_nodes)\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (3 / 3) trials all matched the reference.
Numerical precision: max_abs_err=6.68e-06 mean_abs_err=3.78e-07 max_rel_err=1.43e-02
turn 495.6s turn · 7.2s LLM · 88.4s tools · tokens in=4,933 · out=480 · total=5,576 · FINAL
reasoning
Correct now. Good, passed.
call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cstdint>\n\n__global__ void scatter_add_kernel(const int64_t* __restrict__ dst_idx,\n const float* __restrict__ edge_feat,\n float* __restrict__ out,\n int num_edges, int feat_dim) {\n int edge = blockIdx.x * blockDim.x + threadIdx.x;\n if (edge < num_edges) {\n int64_t dst = dst_idx[edge];\n const float* src = edge_feat + edge * feat_dim;\n float* dst_ptr = out + dst * feat_dim;\n for (int f = 0; f < feat_dim; ++f) {\n atomicAdd(dst_ptr + f, src[f]);\n }\n }\n}\n\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes) {\n auto dst_idx_long = dst_idx.to(torch::kInt64);\n int num_edges = dst_idx_long.size(0);\n int feat_dim = edge_feat.size(1);\n auto out = torch::zeros({num_nodes, feat_dim}, edge_feat.options());\n const int threads = 256;\n const int blocks = (num_edges + threads - 1) / threads;\n scatter_add_kernel<<<blocks, threads>>>(\n dst_idx_long.data_ptr<int64_t>(),\n edge_feat.data_ptr<float>(),\n out.data_ptr<float>(),\n num_edges, feat_dim);\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes);\n\"\"\"\n\nscatter_add = load_inline(\n name=\"scatter_add\",\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=[\"scatter_add_cuda\"],\n verbose=True,\n with_cuda=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, num_nodes):\n super().__init__()\n self.num_nodes = num_nodes\n\n def forward(self, dst_idx: torch.Tensor, edge_feat: torch.Tensor) -> torch.Tensor:\n return scatter_add.scatter_add_cuda(dst_idx, edge_feat, self.num_nodes)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (3 / 3) correctness trials all passed.
Kernel runtime: 0.03 μs
Runtime stats: mean=0.03μs median=?μs std=0.00μs
Numerical precision: max_abs_err=7.63e-06 mean_abs_err=3.78e-07 max_rel_err=2.13e-02
Memory: 37.38 MB peak (ref: 37.38 MB, ratio: 1.0x)
Kernel launches: 8 (ref: 9, fusion ratio: 1.125)
SOL score: 0.0606
final submitted kernel (61 lines, 1,977 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline
cuda_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>
#include <cstdint>
__global__ void scatter_add_kernel(const int64_t* __restrict__ dst_idx,
const float* __restrict__ edge_feat,
float* __restrict__ out,
int num_edges, int feat_dim) {
int edge = blockIdx.x * blockDim.x + threadIdx.x;
if (edge < num_edges) {
int64_t dst = dst_idx[edge];
const float* src = edge_feat + edge * feat_dim;
float* dst_ptr = out + dst * feat_dim;
for (int f = 0; f < feat_dim; ++f) {
atomicAdd(dst_ptr + f, src[f]);
}
}
}
torch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes) {
auto dst_idx_long = dst_idx.to(torch::kInt64);
int num_edges = dst_idx_long.size(0);
int feat_dim = edge_feat.size(1);
auto out = torch::zeros({num_nodes, feat_dim}, edge_feat.options());
const int threads = 256;
const int blocks = (num_edges + threads - 1) / threads;
scatter_add_kernel<<<blocks, threads>>>(
dst_idx_long.data_ptr<int64_t>(),
edge_feat.data_ptr<float>(),
out.data_ptr<float>(),
num_edges, feat_dim);
return out;
}
"""
cpp_source = """
torch::Tensor scatter_add_cuda(torch::Tensor dst_idx, torch::Tensor edge_feat, int num_nodes);
"""
scatter_add = load_inline(
name="scatter_add",
cpp_sources=[cpp_source],
cuda_sources=[cuda_source],
functions=["scatter_add_cuda"],
verbose=True,
with_cuda=True
)
class ModelNew(nn.Module):
def __init__(self, num_nodes):
super().__init__()
self.num_nodes = num_nodes
def forward(self, dst_idx: torch.Tensor, edge_feat: torch.Tensor) -> torch.Tensor:
return scatter_add.scatter_add_cuda(dst_idx, edge_feat, self.num_nodes)