grok-4.3 · popcorn · pop_l123_all_gpt
correct 11_pipeline_three_stage_mlp.py
model
grok-4.3
turns / max
5 / 15
tool calls
5
tokens (total)
22744
wall clock
1263.8s
truncated
no
backend
cuda fp32
variant
popcorn
runtime
0.19 μs
ref runtime
0.02 μs
speedup
0.12x
started
2026-05-11T19:50:28Z
timingkernelbench.timing
kernel runtime
0.19 μs
ref runtime
0.02 μs
speedup
0.12x
kernel mean / median / std
0.19 / — / 5.88e-04 μs
ref mean / median / std
0.02 / — / 0.01 μs
numerical precisioneval_kernel_against_ref
max abs error
7.45e-08
mean abs error
1.56e-08
max rel error
1.76e-05
mean rel error
6.13e-07
gpu memoryextended_metrics.measure_memory
kernel peak
33.06 MB
ref peak
33.06 MB
kernel / ref
1.00x
kernel launches / fusiontorch.profiler
kernel launches
1
ref launches
22
fusion ratio (ref / kernel)
22.00x
total cuda time
26 μs
SOL (speed-of-light)nsight_hardware_counters
SOL score (0–1)
1.000e-04
dram util %
0.0%
compute util %
0.0%
bottleneck
unknown
dominant pipe
None
arithmetic intensity
—
ridge point
20.00
roofline / occupancynsight_hardware_counters
dram bandwidth
0.22 GB/s
dram util %
0.0%
fp32 tflops
—
fp32 util %
—
fp16 tflops
—
occupancy %
9.4%
bottleneck
unknown
dominant pipe %
—
arithmetic intensity
—
ridge point
20.00
L1 hit %
—
L2 hit %
96.8%
ld sectors / req
1.46
st sectors / req
—
regs / thread
32
smem / block
1024 B
block size
256
peak bw
3350.00 GB/s
peak fp32 tflops
67.00
peak fp16 tflops
1979.00
warp stalls (top 5)nsight
long_scoreboard
1141.9%
short_scoreboard
26.1%
reference PyTorch source (the prompt the agent sees)
"""
Three-stage MLP on packed activations: h1 = relu(h0 @ W1), h2 = relu(h1 @ W2), out = h2 @ W3.
Dimensions are fixed; weights are trainable parameters with default init.
"""

import torch
import torch.nn as nn

from kernelbench.distributed_collectives import default_device

B = 16
H0 = 48
H1 = 64
H2 = 40
H3 = 24


class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.W1 = nn.Linear(H0, H1, bias=False)
        self.W2 = nn.Linear(H1, H2, bias=False)
        self.W3 = nn.Linear(H2, H3, bias=False)

    def forward(self, h0: torch.Tensor) -> torch.Tensor:
        h1 = torch.relu(self.W1(h0))
        h2 = torch.relu(self.W2(h1))
        return self.W3(h2)


def get_inputs():
    dev = default_device()
    p = popcorn_pri
    return [torch.randn(p.jitter_int(B), H0, device=dev)]


def get_init_inputs():
    return []
turn 0200.3s turn · 2.4s LLM · 197.9s tools · tokens in=2,029 · out=6 · total=2,160
reasoning
The task is to replace the forward computation of a PyTorch model with a custom CUDA kernel. The model is a three-stage MLP: h1 = relu(h0 @ W1), h2 = relu(h1 @ W2), out = h2 @ W3. No biases, dimension...
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 1222.8s turn · 18.6s LLM · 204.2s tools · tokens in=2,393 · out=872 · total=3,595
reasoning
It's an H100, high bandwidth, tensor cores, etc. But since dimensions are tiny (16x48, etc.), it's probably memory bound or just launch overhead dominated, but we need to beat PyTorch's matmuls.
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 mlp_kernel(const float* h0, const float* W1, const float* W2, const float* W3,\n float* out, int B, int H0, int H1, int H2, int H3) {\n // Simple implementation: each thread computes one element of final output\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * H3;\n if (idx >= total) return;\n int b = idx / H3;\n int h3 = idx % H3;\n\n // Compute h1 = relu(h0[b] @ W1.T) W1 is (H1, H0)\n float h1[64];\n for (int h1 = 0; h1 < H1; h1++) {\n float sum = 0.0f;\n for (int h0 = 0; h0 < H0; h0++) {\n sum += h0[b * H0 + h0] * W1[h1 * H0 + h0];\n }\n h1[h1] = fmaxf(sum, 0.0f);\n }\n\n // h2 = relu(h1 @ W2.T) W2 (H2, H1)\n float h2[40];\n for (int h2 = 0; h2 < H2; h2++) {\n float sum = 0.0f;\n for (int h1 = 0; h1 < H1; h1++) {\n sum += h1[h1] * W2[h2 * H1 + h1];\n }\n h2[h2] = fmaxf(sum, 0.0f);\n }\n\n // out = h2 @ W3.T W3 (H3, H2)\n float sum = 0.0f;\n for (int h2 = 0; h2 < H2; h2++) {\n sum += h2[h2] * W3[h3 * H2 + h2];\n }\n out[b * H3 + h3] = sum;\n}\n\ntorch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3) {\n int B = h0.size(0);\n int H0 = h0.size(1);\n int H1 = W1.size(0);\n int H2 = W2.size(0);\n int H3 = W3.size(0);\n auto out = torch::empty({B, H3}, h0.options());\n int threads = 256;\n int blocks = (B * H3 + threads - 1) / threads;\n mlp_kernel<<<blocks, threads>>>(h0.data_ptr<float>(), W1.data_ptr<float>(),\n W2.data_ptr<float>(), W3.data_ptr<float>(),\n out.data_ptr<float>(), B, H0, H1, H2, H3);\n return out;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3);\n'''\n\nmodule = load_inline(\n name='mlp_cuda',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['mlp_forward'],\n verbose=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.W1 = nn.Linear(48, 64, bias=False)\n self.W2 = nn.Linear(64, 40, bias=False)\n self.W3 = nn.Linear(40, 24, bias=False)\n\n def forward(self, h0: torch.Tensor) -> torch.Tensor:\n return module.mlp_forward(h0, self.W1.weight, self.W2.weight, self.W3.weight)\n" }
result ← compile_kernel (fail)
failcompile_kernel compile_kernel FAILED: RuntimeError. RuntimeError: Error building extension 'mlp_cuda'
turn 2301.8s turn · 12.2s LLM · 289.6s tools · tokens in=3,620 · out=839 · total=4,675
reasoning
It failed to compile. I need to see the error, but the tool didn't return the output. The description says it returns compiler output on failure.
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 mlp_kernel(const float* h0, const float* W1, const float* W2, const float* W3,\n float* out, int B, int H0, int H1, int H2, int H3) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * H3;\n if (idx >= total) return;\n int b = idx / H3;\n int h3 = idx % H3;\n\n // Compute h1 = relu(h0[b] @ W1.T) W1 is (H1, H0)\n float h1_val[64];\n for (int i = 0; i < H1; i++) {\n float sum = 0.0f;\n for (int j = 0; j < H0; j++) {\n sum += h0[b * H0 + j] * W1[i * H0 + j];\n }\n h1_val[i] = fmaxf(sum, 0.0f);\n }\n\n // h2 = relu(h1 @ W2.T) W2 (H2, H1)\n float h2_val[40];\n for (int i = 0; i < H2; i++) {\n float sum = 0.0f;\n for (int j = 0; j < H1; j++) {\n sum += h1_val[j] * W2[i * H1 + j];\n }\n h2_val[i] = fmaxf(sum, 0.0f);\n }\n\n // out = h2 @ W3.T W3 (H3, H2)\n float sum = 0.0f;\n for (int j = 0; j < H2; j++) {\n sum += h2_val[j] * W3[h3 * H2 + j];\n }\n out[b * H3 + h3] = sum;\n}\n\ntorch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3) {\n int B = h0.size(0);\n int H0 = h0.size(1);\n int H1 = W1.size(0);\n int H2 = W2.size(0);\n int H3 = W3.size(0);\n auto out = torch::empty({B, H3}, h0.options());\n int threads = 256;\n int blocks = (B * H3 + threads - 1) / threads;\n mlp_kernel<<<blocks, threads>>>(h0.data_ptr<float>(), W1.data_ptr<float>(),\n W2.data_ptr<float>(), W3.data_ptr<float>(),\n out.data_ptr<float>(), B, H0, H1, H2, H3);\n return out;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3);\n'''\n\nmodule = load_inline(\n name='mlp_cuda',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['mlp_forward'],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.W1 = nn.Linear(48, 64, bias=False)\n self.W2 = nn.Linear(64, 40, bias=False)\n self.W3 = nn.Linear(40, 24, bias=False)\n\n def forward(self, h0: torch.Tensor) -> torch.Tensor:\n return module.mlp_forward(h0, self.W1.weight, self.W2.weight, self.W3.weight)\n" }
result ← compile_kernel (ok)
okcompile_kernel compile_kernel PASSED: kernel compiled without errors.
turn 3186.4s turn · 8.9s LLM · 177.4s tools · tokens in=4,692 · out=840 · total=5,539
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 mlp_kernel(const float* h0, const float* W1, const float* W2, const float* W3,\n float* out, int B, int H0, int H1, int H2, int H3) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * H3;\n if (idx >= total) return;\n int b = idx / H3;\n int h3 = idx % H3;\n\n // Compute h1 = relu(h0[b] @ W1.T) W1 is (H1, H0)\n float h1_val[64];\n for (int i = 0; i < H1; i++) {\n float sum = 0.0f;\n for (int j = 0; j < H0; j++) {\n sum += h0[b * H0 + j] * W1[i * H0 + j];\n }\n h1_val[i] = fmaxf(sum, 0.0f);\n }\n\n // h2 = relu(h1 @ W2.T) W2 (H2, H1)\n float h2_val[40];\n for (int i = 0; i < H2; i++) {\n float sum = 0.0f;\n for (int j = 0; j < H1; j++) {\n sum += h1_val[j] * W2[i * H1 + j];\n }\n h2_val[i] = fmaxf(sum, 0.0f);\n }\n\n // out = h2 @ W3.T W3 (H3, H2)\n float sum = 0.0f;\n for (int j = 0; j < H2; j++) {\n sum += h2_val[j] * W3[h3 * H2 + j];\n }\n out[b * H3 + h3] = sum;\n}\n\ntorch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3) {\n int B = h0.size(0);\n int H0 = h0.size(1);\n int H1 = W1.size(0);\n int H2 = W2.size(0);\n int H3 = W3.size(0);\n auto out = torch::empty({B, H3}, h0.options());\n int threads = 256;\n int blocks = (B * H3 + threads - 1) / threads;\n mlp_kernel<<<blocks, threads>>>(h0.data_ptr<float>(), W1.data_ptr<float>(),\n W2.data_ptr<float>(), W3.data_ptr<float>(),\n out.data_ptr<float>(), B, H0, H1, H2, H3);\n return out;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3);\n'''\n\nmodule = load_inline(\n name='mlp_cuda',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['mlp_forward'],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.W1 = nn.Linear(48, 64, bias=False)\n self.W2 = nn.Linear(64, 40, bias=False)\n self.W3 = nn.Linear(40, 24, bias=False)\n\n def forward(self, h0: torch.Tensor) -> torch.Tensor:\n return module.mlp_forward(h0, self.W1.weight, self.W2.weight, self.W3.weight)\n" }
result ← run_correctness (ok)
okrun_correctness run_correctness PASSED: (3 / 3) trials all matched the reference. Numerical precision: max_abs_err=7.45e-08 mean_abs_err=1.56e-08 max_rel_err=1.76e-05
turn 4352.6s turn · 16.3s LLM · 336.3s tools · tokens in=5,599 · out=839 · total=6,775 · FINAL
reasoning
Correct! Now, to check if faster, I should profile or submit, but since it's the final, I need to see if it's faster than reference. I realize the reference model uses cuBLAS, which is highly optimized, but for these tiny sizes, my naive CUDA kernel might be slower or faster depending on fusion effects. To ensure it's faster, I need to profile or submit for timing, but I'll only submit when confident it's correct and optimized.
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\n__global__ void mlp_kernel(const float* h0, const float* W1, const float* W2, const float* W3,\n float* out, int B, int H0, int H1, int H2, int H3) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * H3;\n if (idx >= total) return;\n int b = idx / H3;\n int h3 = idx % H3;\n\n // Compute h1 = relu(h0[b] @ W1.T) W1 is (H1, H0)\n float h1_val[64];\n for (int i = 0; i < H1; i++) {\n float sum = 0.0f;\n for (int j = 0; j < H0; j++) {\n sum += h0[b * H0 + j] * W1[i * H0 + j];\n }\n h1_val[i] = fmaxf(sum, 0.0f);\n }\n\n // h2 = relu(h1 @ W2.T) W2 (H2, H1)\n float h2_val[40];\n for (int i = 0; i < H2; i++) {\n float sum = 0.0f;\n for (int j = 0; j < H1; j++) {\n sum += h1_val[j] * W2[i * H1 + j];\n }\n h2_val[i] = fmaxf(sum, 0.0f);\n }\n\n // out = h2 @ W3.T W3 (H3, H2)\n float sum = 0.0f;\n for (int j = 0; j < H2; j++) {\n sum += h2_val[j] * W3[h3 * H2 + j];\n }\n out[b * H3 + h3] = sum;\n}\n\ntorch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3) {\n int B = h0.size(0);\n int H0 = h0.size(1);\n int H1 = W1.size(0);\n int H2 = W2.size(0);\n int H3 = W3.size(0);\n auto out = torch::empty({B, H3}, h0.options());\n int threads = 256;\n int blocks = (B * H3 + threads - 1) / threads;\n mlp_kernel<<<blocks, threads>>>(h0.data_ptr<float>(), W1.data_ptr<float>(),\n W2.data_ptr<float>(), W3.data_ptr<float>(),\n out.data_ptr<float>(), B, H0, H1, H2, H3);\n return out;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3);\n'''\n\nmodule = load_inline(\n name='mlp_cuda',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['mlp_forward'],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.W1 = nn.Linear(48, 64, bias=False)\n self.W2 = nn.Linear(64, 40, bias=False)\n self.W3 = nn.Linear(40, 24, bias=False)\n\n def forward(self, h0: torch.Tensor) -> torch.Tensor:\n return module.mlp_forward(h0, self.W1.weight, self.W2.weight, self.W3.weight)\n" }
result ← submit_kernel (ok)
oksubmit_kernel submit_kernel PASSED: (3 / 3) correctness trials all passed. Kernel runtime: 0.19 μs Runtime stats: mean=0.19μs median=?μs std=0.00μs Numerical precision: max_abs_err=7.45e-08 mean_abs_err=1.56e-08 max_rel_err=1.76e-05 Memory: 33.06 MB peak (ref: 33.06 MB, ratio: 0.9998x) Kernel launches: 1 (ref: 22, fusion ratio: 22.0) SOL score: 0.0001
final submitted kernel (82 lines, 2,464 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>

__global__ void mlp_kernel(const float* h0, const float* W1, const float* W2, const float* W3,
                           float* out, int B, int H0, int H1, int H2, int H3) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    int total = B * H3;
    if (idx >= total) return;
    int b = idx / H3;
    int h3 = idx % H3;

    // Compute h1 = relu(h0[b] @ W1.T)  W1 is (H1, H0)
    float h1_val[64];
    for (int i = 0; i < H1; i++) {
        float sum = 0.0f;
        for (int j = 0; j < H0; j++) {
            sum += h0[b * H0 + j] * W1[i * H0 + j];
        }
        h1_val[i] = fmaxf(sum, 0.0f);
    }

    // h2 = relu(h1 @ W2.T) W2 (H2, H1)
    float h2_val[40];
    for (int i = 0; i < H2; i++) {
        float sum = 0.0f;
        for (int j = 0; j < H1; j++) {
            sum += h1_val[j] * W2[i * H1 + j];
        }
        h2_val[i] = fmaxf(sum, 0.0f);
    }

    // out = h2 @ W3.T  W3 (H3, H2)
    float sum = 0.0f;
    for (int j = 0; j < H2; j++) {
        sum += h2_val[j] * W3[h3 * H2 + j];
    }
    out[b * H3 + h3] = sum;
}

torch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3) {
    int B = h0.size(0);
    int H0 = h0.size(1);
    int H1 = W1.size(0);
    int H2 = W2.size(0);
    int H3 = W3.size(0);
    auto out = torch::empty({B, H3}, h0.options());
    int threads = 256;
    int blocks = (B * H3 + threads - 1) / threads;
    mlp_kernel<<<blocks, threads>>>(h0.data_ptr<float>(), W1.data_ptr<float>(),
                                     W2.data_ptr<float>(), W3.data_ptr<float>(),
                                     out.data_ptr<float>(), B, H0, H1, H2, H3);
    return out;
}
'''

cpp_source = '''
torch::Tensor mlp_forward(torch::Tensor h0, torch::Tensor W1, torch::Tensor W2, torch::Tensor W3);
'''

module = load_inline(
    name='mlp_cuda',
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=['mlp_forward'],
    verbose=False
)

class ModelNew(nn.Module):
    def __init__(self):
        super().__init__()
        self.W1 = nn.Linear(48, 64, bias=False)
        self.W2 = nn.Linear(64, 40, bias=False)
        self.W3 = nn.Linear(40, 24, bias=False)

    def forward(self, h0: torch.Tensor) -> torch.Tensor:
        return module.mlp_forward(h0, self.W1.weight, self.W2.weight, self.W3.weight)