correct
6_EdgeSoftmaxMultiHeadCSR.py
started
2026-05-11T17:20:52Z
timingkernelbench.timing
kernel mean / median / std
0.04 / — / 0.00 μs
ref mean / median / std
16.20 / — / 0.15 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
640.22x
energyNVML (pynvml)
ratio (ref / kernel)
24.06x
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 087.0s turn · 12.0s LLM · 75.0s tools · tokens in=2,115 · out=592 · total=3,525
reasoning
The task is to replace the forward computation of a PyTorch model with a custom CUDA kernel. The model performs multi-head row-wise softmax over CSR edge scores.
The task involves replacing the forward computation of a PyTorch model with a CUDA kernel. The model applies multi-head row-wise softmax to CSR edge scores. Edge_scores has shape [num_edges, num_heads], and for each node, we softmax along dim=0 separately for each head on its outgoing edges.
To implement this 1. I need to create a ModelNew class that takes the same inputs as Model, implements forward with the same signature, and outputs the same thing, now using a CUDA kernel.
2. Since direct CUDA programming is complex, I'll use torch.utils.cpp_extension.load_inline() to compile an inline CUDA kernel.
3.
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = r'''\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n__global__ void csr_softmax_kernel(const int* __restrict__ row_ptr, const float* __restrict__ edge_scores, float* __restrict__ out, int num_nodes, int num_heads) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\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 = 0; h < num_heads; h++) {\n float max_val = -INFINITY;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, v);\n }\n float sum = 0.0f;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n sum += expf(v - max_val);\n }\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(v - max_val) / 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.size(0) - 1;\n int num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n dim3 grid(num_nodes);\n csr_softmax_kernel<<<grid, 1>>>(row_ptr.data_ptr<int>(), edge_scores.data_ptr<float>(), out.data_ptr<float>(), num_nodes, num_heads);\n return out;\n}\n'''\n\ncpp_source = r'''\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n'''\n\nmodule = load_inline(\n name='csr_softmax_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['csr_softmax_cuda'],\n verbose=True\n)\n\nclass ModelNew(torch.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 179.3s turn · 4.9s LLM · 74.4s tools · tokens in=3,542 · out=593 · total=4,142
reasoning
Kernel compiled. Now check correctness.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = r'''\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n__global__ void csr_softmax_kernel(const int* __restrict__ row_ptr, const float* __restrict__ edge_scores, float* __restrict__ out, int num_nodes, int num_heads) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\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 = 0; h < num_heads; h++) {\n float max_val = -INFINITY;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, v);\n }\n float sum = 0.0f;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n sum += expf(v - max_val);\n }\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(v - max_val) / 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.size(0) - 1;\n int num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n dim3 grid(num_nodes);\n csr_softmax_kernel<<<grid, 1>>>(row_ptr.data_ptr<int>(), edge_scores.data_ptr<float>(), out.data_ptr<float>(), num_nodes, num_heads);\n return out;\n}\n'''\n\ncpp_source = r'''\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n'''\n\nmodule = load_inline(\n name='csr_softmax_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['csr_softmax_cuda'],\n verbose=False\n)\n\nclass ModelNew(torch.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 293.8s turn · 7.1s LLM · 86.7s tools · tokens in=4,171 · out=615 · total=4,954
reasoning
Error: row_ptr is int32 but probably need to handle device. Inputs are on CPU?
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = r'''\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n__global__ void csr_softmax_kernel(const int* __restrict__ row_ptr, const float* __restrict__ edge_scores, float* __restrict__ out, int num_nodes, int num_heads) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\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 = 0; h < num_heads; h++) {\n float max_val = -INFINITY;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, v);\n }\n float sum = 0.0f;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n sum += expf(v - max_val);\n }\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(v - max_val) / 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.size(0) - 1;\n int num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n dim3 grid(num_nodes);\n csr_softmax_kernel<<<grid, 1>>>(row_ptr.data_ptr<int>(), edge_scores.data_ptr<float>(), out.data_ptr<float>(), num_nodes, num_heads);\n return out;\n}\n'''\n\ncpp_source = r'''\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n'''\n\nmodule = load_inline(\n name='csr_softmax_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['csr_softmax_cuda'],\n verbose=False\n)\n\nclass ModelNew(torch.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()\n edge_scores = edge_scores.cuda()\n out = self.module.csr_softmax_cuda(row_ptr, edge_scores)\n return out.cpu()\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.5s turn · 7.4s LLM · 75.1s tools · tokens in=4,983 · out=651 · total=5,724
reasoning
Still error. Perhaps row_ptr dtype int32, data_ptr<int>() assumes int32? torch int is int32 yes.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = r'''\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n__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) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n int32_t start = row_ptr[node];\n int32_t end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for(int h = 0; h < num_heads; h++) {\n float max_val = -INFINITY;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, v);\n }\n float sum = 0.0f;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n sum += expf(v - max_val);\n }\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(v - max_val) / 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 TORCH_CHECK(row_ptr.dtype() == torch::kInt32, \"row_ptr must be int32\");\n int num_nodes = row_ptr.size(0) - 1;\n int num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n dim3 grid(num_nodes);\n csr_softmax_kernel<<<grid, 1>>>(row_ptr.data_ptr<int32_t>(), edge_scores.data_ptr<float>(), out.data_ptr<float>(), num_nodes, num_heads);\n return out;\n}\n'''\n\ncpp_source = r'''\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n'''\n\nmodule = load_inline(\n name='csr_softmax_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['csr_softmax_cuda'],\n verbose=False\n)\n\nclass ModelNew(torch.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)\n edge_scores = edge_scores.cuda()\n out = self.module.csr_softmax_cuda(row_ptr, edge_scores)\n return out.cpu()\n"
}
result ← run_correctness (fail)
failrun_correctness
run_correctness FAILED: ? trials did not all match.
runtime_error: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
turn 496.2s turn · 8.8s LLM · 87.4s tools · tokens in=5,769 · out=687 · total=6,600
reasoning
Oh, the reference model is CPU, and my out.cpu() but perhaps comparison.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = r'''\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n__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) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n int32_t start = row_ptr[node];\n int32_t end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for(int h = 0; h < num_heads; h++) {\n float max_val = -INFINITY;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, v);\n }\n float sum = 0.0f;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n sum += expf(v - max_val);\n }\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(v - max_val) / 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 TORCH_CHECK(row_ptr.dtype() == torch::kInt32, \"row_ptr must be int32\");\n int num_nodes = row_ptr.size(0) - 1;\n int num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n dim3 grid(num_nodes);\n csr_softmax_kernel<<<grid, 1>>>(row_ptr.data_ptr<int32_t>(), edge_scores.data_ptr<float>(), out.data_ptr<float>(), num_nodes, num_heads);\n cudaDeviceSynchronize();\n return out;\n}\n'''\n\ncpp_source = r'''\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n'''\n\nmodule = load_inline(\n name='csr_softmax_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['csr_softmax_cuda'],\n verbose=False\n)\n\nclass ModelNew(torch.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().to(torch.int32)\n edge_scores = edge_scores.cuda()\n out = self.module.csr_softmax_cuda(row_ptr, edge_scores)\n return out.cpu()\n else:\n return self.module.csr_softmax_cuda(row_ptr.to(torch.int32), edge_scores)\n"
}
result ← run_correctness (fail)
failrun_correctness
run_correctness FAILED: ? trials did not all match.
runtime_error: 'bool' object is not callable
turn 522.6s turn · 6.2s LLM · 16.4s tools · tokens in=6,629 · out=687 · total=7,353
reasoning
Error in my code? if not row_ptr.is_cuda() — is_cuda is method, yes .is_cuda()
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = r'''\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n__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) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n int32_t start = row_ptr[node];\n int32_t end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for(int h = 0; h < num_heads; h++) {\n float max_val = -INFINITY;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, v);\n }\n float sum = 0.0f;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n sum += expf(v - max_val);\n }\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(v - max_val) / 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 TORCH_CHECK(row_ptr.dtype() == torch::kInt32, \"row_ptr must be int32\");\n int num_nodes = row_ptr.size(0) - 1;\n int num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n dim3 grid(num_nodes);\n csr_softmax_kernel<<<grid, 1>>>(row_ptr.data_ptr<int32_t>(), edge_scores.data_ptr<float>(), out.data_ptr<float>(), num_nodes, num_heads);\n cudaDeviceSynchronize();\n return out;\n}\n'''\n\ncpp_source = r'''\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n'''\n\nmodule = load_inline(\n name='csr_softmax_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['csr_softmax_cuda'],\n verbose=False\n)\n\nclass ModelNew(torch.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().to(torch.int32)\n edge_scores = edge_scores.cuda()\n out = self.module.csr_softmax_cuda(row_ptr, edge_scores)\n return out.cpu()\n else:\n return self.module.csr_softmax_cuda(row_ptr.to(torch.int32), edge_scores)\n"
}
result ← run_correctness (fail)
failrun_correctness
run_correctness FAILED: ? trials did not all match.
runtime_error: 'bool' object is not callable
turn 681.6s turn · 5.8s LLM · 75.8s tools · tokens in=7,382 · out=667 · total=8,164
reasoning
Still? if not row_ptr.is_cuda(): — .is_cuda is attribute, it's bool, no ().
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = r'''\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n__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) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n int32_t start = row_ptr[node];\n int32_t end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for(int h = 0; h < num_heads; h++) {\n float max_val = -INFINITY;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, v);\n }\n float sum = 0.0f;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n sum += expf(v - max_val);\n }\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(v - max_val) / 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 TORCH_CHECK(row_ptr.dtype() == torch::kInt32, \"row_ptr must be int32\");\n int num_nodes = row_ptr.size(0) - 1;\n int num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n dim3 grid(num_nodes);\n csr_softmax_kernel<<<grid, 1>>>(row_ptr.data_ptr<int32_t>(), edge_scores.data_ptr<float>(), out.data_ptr<float>(), num_nodes, num_heads);\n cudaDeviceSynchronize();\n return out;\n}\n'''\n\ncpp_source = r'''\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n'''\n\nmodule = load_inline(\n name='csr_softmax_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['csr_softmax_cuda'],\n verbose=False\n)\n\nclass ModelNew(torch.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 orig_device = row_ptr.device\n row_ptr = row_ptr.cuda().to(torch.int32)\n edge_scores = edge_scores.cuda()\n out = self.module.csr_softmax_cuda(row_ptr, edge_scores)\n return out.to(orig_device)\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (3 / 3) trials all matched the reference.
Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00
turn 7158.8s turn · 5.9s LLM · 153.0s tools · tokens in=8,224 · out=666 · total=8,898 · FINAL
reasoning
Great, correct now. Now submit.
call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = r'''\n#include <torch/extension.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n__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) {\n int node = blockIdx.x;\n if (node >= num_nodes) return;\n int32_t start = row_ptr[node];\n int32_t end = row_ptr[node + 1];\n int degree = end - start;\n if (degree <= 0) return;\n\n for(int h = 0; h < num_heads; h++) {\n float max_val = -INFINITY;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n max_val = fmaxf(max_val, v);\n }\n float sum = 0.0f;\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n sum += expf(v - max_val);\n }\n for(int i = 0; i < degree; i++) {\n float v = edge_scores[(start + i) * num_heads + h];\n out[(start + i) * num_heads + h] = expf(v - max_val) / 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 TORCH_CHECK(row_ptr.dtype() == torch::kInt32, \"row_ptr must be int32\");\n int num_nodes = row_ptr.size(0) - 1;\n int num_heads = edge_scores.size(1);\n auto out = torch::empty_like(edge_scores);\n dim3 grid(num_nodes);\n csr_softmax_kernel<<<grid, 1>>>(row_ptr.data_ptr<int32_t>(), edge_scores.data_ptr<float>(), out.data_ptr<float>(), num_nodes, num_heads);\n cudaDeviceSynchronize();\n return out;\n}\n'''\n\ncpp_source = r'''\ntorch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);\n'''\n\nmodule = load_inline(\n name='csr_softmax_module',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['csr_softmax_cuda'],\n verbose=False\n)\n\nclass ModelNew(torch.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 orig_device = row_ptr.device\n row_ptr = row_ptr.cuda().to(torch.int32)\n edge_scores = edge_scores.cuda()\n out = self.module.csr_softmax_cuda(row_ptr, edge_scores)\n return out.to(orig_device)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (3 / 3) correctness trials all passed.
Kernel runtime: 0.04 μs
Runtime stats: mean=0.04μs median=?μs std=0.00μs
Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00
Memory: 0.43 MB peak (ref: 0.42 MB, ratio: 1.0023x)
Kernel launches: 9 (ref: 5762, fusion ratio: 640.2222)
Energy: 242.74 mJ/run (ref: 5841.5 mJ, ratio: 24.0648x)
SOL score: 0.0867
Flagged for excessive speedup — this submission may have been rejected by automated review.
final submitted kernel (72 lines, 2,485 chars) — full source, untruncated
import torch
from torch.utils.cpp_extension import load_inline
cuda_source = r'''
#include <torch/extension.h>
#include <cuda_runtime.h>
#include <cmath>
__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;
int32_t start = row_ptr[node];
int32_t end = row_ptr[node + 1];
int degree = end - start;
if (degree <= 0) return;
for(int h = 0; h < num_heads; h++) {
float max_val = -INFINITY;
for(int i = 0; i < degree; i++) {
float v = edge_scores[(start + i) * num_heads + h];
max_val = fmaxf(max_val, v);
}
float sum = 0.0f;
for(int i = 0; i < degree; i++) {
float v = edge_scores[(start + i) * num_heads + h];
sum += expf(v - max_val);
}
for(int i = 0; i < degree; i++) {
float v = edge_scores[(start + i) * num_heads + h];
out[(start + i) * num_heads + h] = expf(v - max_val) / sum;
}
}
}
torch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores) {
TORCH_CHECK(row_ptr.is_cuda(), "row_ptr must be CUDA tensor");
TORCH_CHECK(edge_scores.is_cuda(), "edge_scores must be CUDA tensor");
TORCH_CHECK(row_ptr.dtype() == torch::kInt32, "row_ptr must be int32");
int num_nodes = row_ptr.size(0) - 1;
int num_heads = edge_scores.size(1);
auto out = torch::empty_like(edge_scores);
dim3 grid(num_nodes);
csr_softmax_kernel<<<grid, 1>>>(row_ptr.data_ptr<int32_t>(), edge_scores.data_ptr<float>(), out.data_ptr<float>(), num_nodes, num_heads);
cudaDeviceSynchronize();
return out;
}
'''
cpp_source = r'''
torch::Tensor csr_softmax_cuda(torch::Tensor row_ptr, torch::Tensor edge_scores);
'''
module = load_inline(
name='csr_softmax_module',
cpp_sources=[cpp_source],
cuda_sources=[cuda_source],
functions=['csr_softmax_cuda'],
verbose=False
)
class ModelNew(torch.nn.Module):
def __init__(self):
super().__init__()
self.module = module
def forward(self, row_ptr: torch.Tensor, edge_scores: torch.Tensor) -> torch.Tensor:
orig_device = row_ptr.device
row_ptr = row_ptr.cuda().to(torch.int32)
edge_scores = edge_scores.cuda()
out = self.module.csr_softmax_cuda(row_ptr, edge_scores)
return out.to(orig_device)