correct
6_EdgeSoftmaxMultiHeadCSR.py
started
2026-05-11T10:47:06Z
timingkernelbench.timing
kernel mean / median / std
0.01 / — / 6.28e-04 μs
ref mean / median / std
15.50 / — / 0.42 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
720.25x
energyNVML (pynvml)
ratio (ref / kernel)
8.08x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
warp stalls (top 5)nsight
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn
class Model(nn.Module):
"""
Multi-head row-wise softmax over CSR edge scores.
This extends scalar graph edge softmax to multiple attention heads while
preserving sparse CSR layout. Each row is normalized independently for each
head.
"""
def __init__(self):
super().__init__()
def forward(
self,
row_ptr: torch.Tensor,
edge_scores: torch.Tensor,
) -> torch.Tensor:
num_nodes = row_ptr.numel() - 1
out = torch.empty_like(edge_scores)
for node in range(num_nodes):
start = int(row_ptr[node].item())
end = int(row_ptr[node + 1].item())
if end > start:
out[start:end] = torch.softmax(edge_scores[start:end], dim=0)
return out
num_nodes = 384
avg_degree = 18
num_heads = 8
def get_inputs():
return list(popcorn_pri.make_csr_multihead_edge_scores(num_nodes, avg_degree, num_heads))
def get_init_inputs():
return []
turn 016.9s turn · 16.9s LLM · 0.0s tools · tokens in=1,584 · out=6 · total=1,936
reasoning
The task is to replace the forward computation of a PyTorch model with a custom CUDA kernel. The model does a multi-head row-wise softmax over CSR edge scores.
The task involves replacing the forward computation of a PyTorch model with a custom CUDA kernel. The model performs a multi-head row-wise softmax on CSR edge scores.
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 1196.4s turn · 30.3s LLM · 166.2s tools · tokens in=2,169 · out=660 · total=2,985
reasoning
GPU is H100, plenty of resources, data is tiny so any reasonable kernel will be fast.
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n\n__global__ void csr_softmax_kernel(\n const int* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads,\n int num_edges\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n // Compute max\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n if (val > max_val) max_val = val;\n }\n // Compute exp sum\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n // Compute softmax\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n int num_nodes = row_ptr.numel() - 1;\n auto num_edges = edge_scores.size(0);\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8); // num_heads=8\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads,\n num_edges\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n return self.module.csr_softmax_cuda(row_ptr, edge_scores)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 277.2s turn · 10.0s LLM · 67.3s tools · tokens in=3,002 · out=661 · total=3,670
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n\n__global__ void csr_softmax_kernel(\n const int* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads,\n int num_edges\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n // Compute max\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n if (val > max_val) max_val = val;\n }\n // Compute exp sum\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n // Compute softmax\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n int num_nodes = row_ptr.numel() - 1;\n auto num_edges = edge_scores.size(0);\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8); // num_heads=8\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads,\n num_edges\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n return self.module.csr_softmax_cuda(row_ptr, edge_scores)\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 3198.9s turn · 10.3s LLM · 188.6s tools · tokens in=3,699 · out=669 · total=4,537
reasoning
Error: row_ptr is int32 but in kernel treated as int, but data_ptr<int>() assumes int32? Wait, error is expected Int but Float? Perhaps in call.
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n#include <cstdint>\n\n__global__ void csr_softmax_kernel(\n const int32_t* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads,\n int num_edges\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n // Compute max\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n if (val > max_val) max_val = val;\n }\n // Compute exp sum\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n // Compute softmax\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n int num_nodes = row_ptr.numel() - 1;\n auto num_edges = edge_scores.size(0);\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8); // num_heads=8\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int32_t>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads,\n num_edges\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n return self.module.csr_softmax_cuda(row_ptr, edge_scores)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 4197.9s turn · 7.0s LLM · 190.9s tools · tokens in=4,554 · out=670 · total=5,229
reasoning
Now run correctness again.
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n#include <cstdint>\n\n__global__ void csr_softmax_kernel(\n const int32_t* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads,\n int num_edges\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n // Compute max\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n if (val > max_val) max_val = val;\n }\n // Compute exp sum\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n // Compute softmax\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n int num_nodes = row_ptr.numel() - 1;\n auto num_edges = edge_scores.size(0);\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8); // num_heads=8\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int32_t>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads,\n num_edges\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n return self.module.csr_softmax_cuda(row_ptr, edge_scores)\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 5383.6s turn · 11.0s LLM · 372.6s tools · tokens in=5,258 · out=696 · total=6,426
reasoning
Still error. The error "expected scalar type Int but found Float" – perhaps row_ptr is being treated as float somewhere.
I was troubleshooting an error in the CUDA kernel implementation for the PyTorch model. The error said "expected scalar type Int but type Float," which suggested a type mismatch with row_ptr.
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n#include <cstdint>\n\n__global__ void csr_softmax_kernel(\n const int32_t* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, val);\n }\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n TORCH_CHECK(row_ptr.is_cuda(), \"row_ptr must be CUDA tensor\");\n TORCH_CHECK(edge_scores.is_cuda(), \"edge_scores must be CUDA tensor\");\n int num_nodes = row_ptr.numel() - 1;\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8);\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int32_t>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n if not row_ptr.is_cuda():\n row_ptr = row_ptr.cuda()\n if not edge_scores.is_cuda():\n edge_scores = edge_scores.cuda()\n return self.module.csr_softmax_cuda(row_ptr.contiguous(), edge_scores.contiguous())\n"
}
result ← run_correctness (fail)
failrun_correctness
run_correctness FAILED: ? trials did not all match.
runtime_error: 'bool' object is not callable
turn 6265.1s turn · 7.6s LLM · 257.5s tools · tokens in=6,455 · out=625 · total=7,128
reasoning
New error. Probably because TORCH_CHECK uses , not ().
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n#include <cstdint>\n\n__global__ void csr_softmax_kernel(\n const int32_t* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, val);\n }\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n int num_nodes = row_ptr.numel() - 1;\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8);\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int32_t>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n return self.module.csr_softmax_cuda(row_ptr, edge_scores)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 7394.1s turn · 7.0s LLM · 387.1s tools · tokens in=7,145 · out=626 · total=7,784
reasoning
Now test correctness, assuming inputs are moved to CUDA by harness.
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n#include <cstdint>\n\n__global__ void csr_softmax_kernel(\n const int32_t* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, val);\n }\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n int num_nodes = row_ptr.numel() - 1;\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8);\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int32_t>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n return self.module.csr_softmax_cuda(row_ptr, edge_scores)\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 8305.2s turn · 6.8s LLM · 298.3s tools · tokens in=7,813 · out=652 · total=8,613
reasoning
Still failing. The issue must be that row_ptr is int32 but in CUDA kernel launch, perhaps the harness passes CPU tensors, and data_ptr on CPU tensor for CUDA? But no.
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n#include <cstdint>\n\n__global__ void csr_softmax_kernel(\n const int32_t* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, val);\n }\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n int num_nodes = row_ptr.numel() - 1;\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8);\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int32_t>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n row_ptr = row_ptr.cuda().to(torch.int32).contiguous()\n edge_scores = edge_scores.cuda().contiguous()\n return self.module.csr_softmax_cuda(row_ptr, edge_scores)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 9162.4s turn · 8.4s LLM · 154.0s tools · tokens in=8,664 · out=653 · total=9,323
reasoning
Now try correctness with this.
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n#include <cstdint>\n\n__global__ void csr_softmax_kernel(\n const int32_t* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, val);\n }\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n int num_nodes = row_ptr.numel() - 1;\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8);\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int32_t>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n row_ptr = row_ptr.cuda().to(torch.int32).contiguous()\n edge_scores = edge_scores.cuda().contiguous()\n return self.module.csr_softmax_cuda(row_ptr, edge_scores)\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=2.98e-08 mean_abs_err=9.08e-10 max_rel_err=1.19e-07
turn 10412.2s turn · 6.7s LLM · 405.5s tools · tokens in=9,417 · out=652 · total=10,076 · FINAL
reasoning
Correct now! So submit it.
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.h>\n#include <cuda_runtime.h>\n#include <cfloat>\n#include <cstdint>\n\n__global__ void csr_softmax_kernel(\n const int32_t* __restrict__ row_ptr,\n const float* __restrict__ edge_scores,\n float* __restrict__ out,\n int num_nodes,\n int num_heads\n) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n\n int start = row_ptr[node];\n int end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {\n float max_val = -FLT_MAX;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, val);\n }\n float sum_exp = 0.0f;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n sum_exp += expf(val - max_val);\n }\n float inv_sum = 1.0f / sum_exp;\n for (int i = 0; i < degree; i++) {\n float val = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;\n }\n }\n}\n\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {\n int num_nodes = row_ptr.numel() - 1;\n auto num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n\n dim3 grid(num_nodes);\n dim3 block(8);\n\n csr_softmax_kernel<<<grid, block>>>(\n row_ptr.data_ptr<int32_t>(),\n edge_scores.data_ptr<float>(),\n out.data_ptr<float>(),\n num_nodes,\n num_heads\n );\n\n return out;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n\"\"\"\n\nmodule = load_inline(\n name=\"csr_softmax\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"csr_softmax_cuda\"],\n verbose=False,\n with_cuda=True,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n\n def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:\n row_ptr = row_ptr.cuda().to(torch.int32).contiguous()\n edge_scores = edge_scores.cuda().contiguous()\n return self.module.csr_softmax_cuda(row_ptr, edge_scores)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 0.01 μs
Runtime stats: mean=0.01μs median=?μs std=0.00μs
Numerical precision: max_abs_err=2.98e-08 mean_abs_err=9.08e-10 max_rel_err=1.19e-07
Memory: 33.43 MB peak (ref: 33.42 MB, ratio: 1.0x)
Kernel launches: 8 (ref: 5762, fusion ratio: 720.25)
Energy: 243.08 mJ/run (ref: 1963.62 mJ, ratio: 8.0781x)
SOL score: 0.0555
Flagged for excessive speedup — this submission may have been rejected by automated review.
final submitted kernel (88 lines, 2,432 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.h>
#include <cuda_runtime.h>
#include <cfloat>
#include <cstdint>
__global__ void csr_softmax_kernel(
const int32_t* __restrict__ row_ptr,
const float* __restrict__ edge_scores,
float* __restrict__ out,
int num_nodes,
int num_heads
) {
int node = blockIdx.x;
if (node >= num_nodes) return;
int start = row_ptr[node];
int end = row_ptr[node + 1];
int degree = end - start;
if (degree <= 0) return;
for (int h = threadIdx.x; h < num_heads; h += blockDim.x) {
float max_val = -FLT_MAX;
for (int i = 0; i < degree; i++) {
float val = edge_scores[(start + i) * num_heads + h];
max_val = fmaxf(max_val, val);
}
float sum_exp = 0.0f;
for (int i = 0; i < degree; i++) {
float val = edge_scores[(start + i) * num_heads + h];
sum_exp += expf(val - max_val);
}
float inv_sum = 1.0f / sum_exp;
for (int i = 0; i < degree; i++) {
float val = edge_scores[(start + i) * num_heads + h];
out[(start + i) * num_heads + h] = expf(val - max_val) * inv_sum;
}
}
}
torch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {
int num_nodes = row_ptr.numel() - 1;
auto num_heads = edge_scores.size(1);
auto out = torch::empty_like(edge_scores);
dim3 grid(num_nodes);
dim3 block(8);
csr_softmax_kernel<<<grid, block>>>(
row_ptr.data_ptr<int32_t>(),
edge_scores.data_ptr<float>(),
out.data_ptr<float>(),
num_nodes,
num_heads
);
return out;
}
"""
cpp_source = """
torch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);
"""
module = load_inline(
name="csr_softmax",
cpp_sources=cpp_source,
cuda_sources=cuda_source,
functions=["csr_softmax_cuda"],
verbose=False,
with_cuda=True,
)
class ModelNew(nn.Module):
def __init__(self):
super().__init__()
self.module = module
def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:
row_ptr = row_ptr.cuda().to(torch.int32).contiguous()
edge_scores = edge_scores.cuda().contiguous()
return self.module.csr_softmax_cuda(row_ptr, edge_scores)