grok-4.3 · popcorn · pop_l123_default_gpt
correct 30_SelectiveScan_Mamba.py
model
grok-4.3
turns / max
4 / 12
tool calls
4
tokens (total)
17547
wall clock
2162.3s
truncated
no
backend
cuda fp32
variant
popcorn
runtime
7.12 μs
ref runtime
170.00 μs
speedup
23.88x
started
2026-05-11T11:45:28Z
timingkernelbench.timing
kernel runtime
7.12 μs
ref runtime
170.00 μs
speedup
23.88x
kernel mean / median / std
7.12 / — / 0.01 μs
ref mean / median / std
170.00 / — / 533.00 μs
numerical precisioneval_kernel_against_ref
max abs error
8.58e-06
mean abs error
1.06e-07
max rel error
0.33
mean rel error
7.25e-07
gpu memoryextended_metrics.measure_memory
kernel peak
1733.61 MB
ref peak
1765.86 MB
kernel / ref
0.98x
kernel launches / fusiontorch.profiler
kernel launches
51
ref launches
26681
fusion ratio (ref / kernel)
523.16x
total cuda time
2713 μs
energyNVML (pynvml)
kernel mJ / run
968.52
ref mJ / run
11866.50
ratio (ref / kernel)
12.25x
avg power
135.75 W
ref avg power
166.17 W
SOL (speed-of-light)heuristic_from_speedup
SOL score (0–1)
1.000
dram util %
—
compute util %
—
bottleneck
unknown
dominant pipe
—
arithmetic intensity
—
ridge point
—
roofline / occupancyheuristic
dram bandwidth
—
dram util %
—
fp32 tflops
—
fp32 util %
—
fp16 tflops
—
occupancy %
—
bottleneck
—
dominant pipe %
—
arithmetic intensity
—
ridge point
—
L1 hit %
—
L2 hit %
—
ld sectors / req
—
st sectors / req
—
regs / thread
—
smem / block
—
block size
—
peak bw
—
peak fp32 tflops
—
peak fp16 tflops
—
flagssubmit_kernel guard
excessive speedup
flagged
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn

class Model(nn.Module):
    """
    Selective scan (Mamba's S6 layer): projects input to derive input-dependent SSM parameters
    (delta, B, C), discretizes a learned state matrix A, runs the linear recurrence, and
    applies a skip connection via D. The fusion opportunity is keeping all intermediates
    (discretized parameters, hidden states) in SRAM rather than materializing them to global memory.

    Args:
        d_model (int): Input feature dimension.
        d_state (int): Hidden state dimension per channel (N in Mamba, typically 16).
    """
    def __init__(self, d_model: int, d_state: int):
        super(Model, self).__init__()
        self.d_model = d_model
        self.d_state = d_state
        self.A_log = nn.Parameter(torch.randn(d_model, d_state))
        self.proj_B = nn.Linear(d_model, d_state, bias=False)
        self.proj_C = nn.Linear(d_model, d_state, bias=False)
        self.proj_delta = nn.Linear(d_model, d_model, bias=True)
        self.D = nn.Parameter(torch.ones(d_model))

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        B, L, D = x.shape
        N = self.d_state

        delta = torch.nn.functional.softplus(self.proj_delta(x))
        B_t = self.proj_B(x)
        C_t = self.proj_C(x)

        A = -torch.exp(self.A_log)
        dA = torch.exp(delta.unsqueeze(-1) * A.unsqueeze(0).unsqueeze(0))
        dB = delta.unsqueeze(-1) * B_t.unsqueeze(2)
        x_db = x.unsqueeze(-1) * dB

        h = torch.zeros(B, D, N, device=x.device, dtype=x.dtype)
        outputs = []
        for t in range(L):
            h = dA[:, t] * h + x_db[:, t]
            y_t = (h * C_t[:, t].unsqueeze(1)).sum(dim=-1)
            outputs.append(y_t)

        y = torch.stack(outputs, dim=1)
        y = y + x * self.D.unsqueeze(0).unsqueeze(0)
        return y

batch_size = 16
d_model = 256
d_state = 16
seq_len = 2048

def get_inputs():
    return [torch.randn(popcorn_pri.jitter_int(batch_size), popcorn_pri.jitter_int(seq_len), d_model)]

def get_init_inputs():
    return [d_model, d_state]
turn 0284.9s turn · 7.2s LLM · 277.7s tools · tokens in=1,769 · out=6 · total=2,407
reasoning
The task is to replace the forward computation of this selective scan (Mamba's S6 layer) with a custom CUDA kernel that is faster than the PyTorch reference. The task involves replacing the forward computation of a PyTorch model, specifically the selective scan in Mamba's S6 layer, with a custom CUDA kernel to make it faster than the PyTorch reference. The reference uses a loop over the sequence length, which is inefficient for long sequences like 2048.
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 1670.0s turn · 11.7s LLM · 658.3s tools · tokens in=2,640 · out=991 · total=3,990
reasoning
GPU is H100, high bandwidth, lots of compute. I need to replace the forward computation of the PyTorch model with a custom CUDA kernel. The new ModelNew class should take the same inputs as the original Model, have the same forward signature, and produce the same output numerically.
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\n__global__ void selective_scan_kernel(\n const float* __restrict__ x,\n const float* __restrict__ delta,\n const float* __restrict__ B_t,\n const float* __restrict__ C_t,\n const float* __restrict__ dA,\n const float* __restrict__ x_db,\n float* __restrict__ y,\n int B, int L, int D, int N\n) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * D;\n if (idx >= total) return;\n int b = idx / D;\n int d = idx % D;\n\n float h[16]; // assuming N <=16\n for (int n=0; n<N; n++) h[n] = 0.0f;\n\n for (int t=0; t<L; t++) {\n int base_dn = (b * L + t) * D * N + d * N;\n for (int n=0; n<N; n++) {\n float da = dA[base_dn + n];\n float xdb = x_db[base_dn + n];\n h[n] = da * h[n] + xdb;\n }\n float yt = 0.0f;\n int base_cn = (b * L + t) * N;\n for (int n=0; n<N; n++) {\n yt += h[n] * C_t[base_cn + n];\n }\n y[(b * L + t) * D + d] = yt;\n }\n}\n\ntorch::Tensor selective_scan_cuda(\n torch::Tensor x,\n torch::Tensor delta,\n torch::Tensor B_t,\n torch::Tensor C_t,\n torch::Tensor dA,\n torch::Tensor x_db\n) {\n int B = x.size(0);\n int L = x.size(1);\n int D = x.size(2);\n int N = B_t.size(2);\n auto y = torch::empty({B, L, D}, x.options());\n int total = B * D;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n selective_scan_kernel<<<blocks, threads>>>(\n x.data_ptr<float>(), delta.data_ptr<float>(), B_t.data_ptr<float>(),\n C_t.data_ptr<float>(), dA.data_ptr<float>(), x_db.data_ptr<float>(),\n y.data_ptr<float>(), B, L, D, N\n );\n return y;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor selective_scan_cuda(\n torch::Tensor x,\n torch::Tensor delta,\n torch::Tensor B_t,\n torch::Tensor C_t,\n torch::Tensor dA,\n torch::Tensor x_db\n);\n'''\n\nmodule = load_inline(\n name='selective_scan',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['selective_scan_cuda'],\n verbose=True,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, d_model: int, d_state: int):\n super(ModelNew, self).__init__()\n self.d_model = d_model\n self.d_state = d_state\n self.A_log = nn.Parameter(torch.randn(d_model, d_state))\n self.proj_B = nn.Linear(d_model, d_state, bias=False)\n self.proj_C = nn.Linear(d_model, d_state, bias=False)\n self.proj_delta = nn.Linear(d_model, d_model, bias=True)\n self.D = nn.Parameter(torch.ones(d_model))\n self.scan_module = module\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n B, L, D = x.shape\n N = self.d_state\n delta = torch.nn.functional.softplus(self.proj_delta(x))\n B_t = self.proj_B(x)\n C_t = self.proj_C(x)\n A = -torch.exp(self.A_log)\n dA = torch.exp(delta.unsqueeze(-1) * A.unsqueeze(0).unsqueeze(0))\n dB = delta.unsqueeze(-1) * B_t.unsqueeze(2)\n x_db = x.unsqueeze(-1) * dB\n y = self.scan_module.selective_scan_cuda(x, delta, B_t, C_t, dA, x_db)\n y = y + x * self.D.unsqueeze(0).unsqueeze(0)\n return y\n" }
result ← compile_kernel (ok)
okcompile_kernel compile_kernel PASSED: kernel compiled without errors.
turn 2427.5s turn · 11.4s LLM · 416.1s tools · tokens in=4,007 · out=992 · total=5,006
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\n__global__ void selective_scan_kernel(\n const float* __restrict__ x,\n const float* __restrict__ delta,\n const float* __restrict__ B_t,\n const float* __restrict__ C_t,\n const float* __restrict__ dA,\n const float* __restrict__ x_db,\n float* __restrict__ y,\n int B, int L, int D, int N\n) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * D;\n if (idx >= total) return;\n int b = idx / D;\n int d = idx % D;\n\n float h[16]; // assuming N <=16\n for (int n=0; n<N; n++) h[n] = 0.0f;\n\n for (int t=0; t<L; t++) {\n int base_dn = (b * L + t) * D * N + d * N;\n for (int n=0; n<N; n++) {\n float da = dA[base_dn + n];\n float xdb = x_db[base_dn + n];\n h[n] = da * h[n] + xdb;\n }\n float yt = 0.0f;\n int base_cn = (b * L + t) * N;\n for (int n=0; n<N; n++) {\n yt += h[n] * C_t[base_cn + n];\n }\n y[(b * L + t) * D + d] = yt;\n }\n}\n\ntorch::Tensor selective_scan_cuda(\n torch::Tensor x,\n torch::Tensor delta,\n torch::Tensor B_t,\n torch::Tensor C_t,\n torch::Tensor dA,\n torch::Tensor x_db\n) {\n int B = x.size(0);\n int L = x.size(1);\n int D = x.size(2);\n int N = B_t.size(2);\n auto y = torch::empty({B, L, D}, x.options());\n int total = B * D;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n selective_scan_kernel<<<blocks, threads>>>(\n x.data_ptr<float>(), delta.data_ptr<float>(), B_t.data_ptr<float>(),\n C_t.data_ptr<float>(), dA.data_ptr<float>(), x_db.data_ptr<float>(),\n y.data_ptr<float>(), B, L, D, N\n );\n return y;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor selective_scan_cuda(\n torch::Tensor x,\n torch::Tensor delta,\n torch::Tensor B_t,\n torch::Tensor C_t,\n torch::Tensor dA,\n torch::Tensor x_db\n);\n'''\n\nmodule = load_inline(\n name='selective_scan',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['selective_scan_cuda'],\n verbose=False,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, d_model: int, d_state: int):\n super(ModelNew, self).__init__()\n self.d_model = d_model\n self.d_state = d_state\n self.A_log = nn.Parameter(torch.randn(d_model, d_state))\n self.proj_B = nn.Linear(d_model, d_state, bias=False)\n self.proj_C = nn.Linear(d_model, d_state, bias=False)\n self.proj_delta = nn.Linear(d_model, d_model, bias=True)\n self.D = nn.Parameter(torch.ones(d_model))\n self.scan_module = module\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n B, L, D = x.shape\n N = self.d_state\n delta = torch.nn.functional.softplus(self.proj_delta(x))\n B_t = self.proj_B(x)\n C_t = self.proj_C(x)\n A = -torch.exp(self.A_log)\n dA = torch.exp(delta.unsqueeze(-1) * A.unsqueeze(0).unsqueeze(0))\n dB = delta.unsqueeze(-1) * B_t.unsqueeze(2)\n x_db = x.unsqueeze(-1) * dB\n y = self.scan_module.selective_scan_cuda(x, delta, B_t, C_t, dA, x_db)\n y = y + x * self.D.unsqueeze(0).unsqueeze(0)\n return y\n" }
result ← run_correctness (ok)
okrun_correctness run_correctness PASSED: (5 / 5) trials all matched the reference. Numerical precision: max_abs_err=8.58e-06 mean_abs_err=1.06e-07 max_rel_err=3.33e-01
turn 3780.0s turn · 10.6s LLM · 769.4s tools · tokens in=5,066 · out=991 · total=6,144 · FINAL
reasoning
Correct! Now, to submit, but is it faster? The kernel is still materializing dA and x_db which are huge: B*L*D*N =16*2048*256*16 ~ 134M elements, 500MB+, and the scan is sequential per thread.
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\n__global__ void selective_scan_kernel(\n const float* __restrict__ x,\n const float* __restrict__ delta,\n const float* __restrict__ B_t,\n const float* __restrict__ C_t,\n const float* __restrict__ dA,\n const float* __restrict__ x_db,\n float* __restrict__ y,\n int B, int L, int D, int N\n) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * D;\n if (idx >= total) return;\n int b = idx / D;\n int d = idx % D;\n\n float h[16]; // assuming N <=16\n for (int n=0; n<N; n++) h[n] = 0.0f;\n\n for (int t=0; t<L; t++) {\n int base_dn = (b * L + t) * D * N + d * N;\n for (int n=0; n<N; n++) {\n float da = dA[base_dn + n];\n float xdb = x_db[base_dn + n];\n h[n] = da * h[n] + xdb;\n }\n float yt = 0.0f;\n int base_cn = (b * L + t) * N;\n for (int n=0; n<N; n++) {\n yt += h[n] * C_t[base_cn + n];\n }\n y[(b * L + t) * D + d] = yt;\n }\n}\n\ntorch::Tensor selective_scan_cuda(\n torch::Tensor x,\n torch::Tensor delta,\n torch::Tensor B_t,\n torch::Tensor C_t,\n torch::Tensor dA,\n torch::Tensor x_db\n) {\n int B = x.size(0);\n int L = x.size(1);\n int D = x.size(2);\n int N = B_t.size(2);\n auto y = torch::empty({B, L, D}, x.options());\n int total = B * D;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n selective_scan_kernel<<<blocks, threads>>>(\n x.data_ptr<float>(), delta.data_ptr<float>(), B_t.data_ptr<float>(),\n C_t.data_ptr<float>(), dA.data_ptr<float>(), x_db.data_ptr<float>(),\n y.data_ptr<float>(), B, L, D, N\n );\n return y;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor selective_scan_cuda(\n torch::Tensor x,\n torch::Tensor delta,\n torch::Tensor B_t,\n torch::Tensor C_t,\n torch::Tensor dA,\n torch::Tensor x_db\n);\n'''\n\nmodule = load_inline(\n name='selective_scan',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['selective_scan_cuda'],\n verbose=False,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, d_model: int, d_state: int):\n super(ModelNew, self).__init__()\n self.d_model = d_model\n self.d_state = d_state\n self.A_log = nn.Parameter(torch.randn(d_model, d_state))\n self.proj_B = nn.Linear(d_model, d_state, bias=False)\n self.proj_C = nn.Linear(d_model, d_state, bias=False)\n self.proj_delta = nn.Linear(d_model, d_model, bias=True)\n self.D = nn.Parameter(torch.ones(d_model))\n self.scan_module = module\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n B, L, D = x.shape\n N = self.d_state\n delta = torch.nn.functional.softplus(self.proj_delta(x))\n B_t = self.proj_B(x)\n C_t = self.proj_C(x)\n A = -torch.exp(self.A_log)\n dA = torch.exp(delta.unsqueeze(-1) * A.unsqueeze(0).unsqueeze(0))\n dB = delta.unsqueeze(-1) * B_t.unsqueeze(2)\n x_db = x.unsqueeze(-1) * dB\n y = self.scan_module.selective_scan_cuda(x, delta, B_t, C_t, dA, x_db)\n y = y + x * self.D.unsqueeze(0).unsqueeze(0)\n return y\n" }
result ← submit_kernel (ok)
oksubmit_kernel submit_kernel PASSED: (5 / 5) correctness trials all passed. Kernel runtime: 7.12 μs Runtime stats: mean=7.12μs median=?μs std=0.01μs Numerical precision: max_abs_err=8.58e-06 mean_abs_err=1.06e-07 max_rel_err=3.33e-01 Memory: 1733.61 MB peak (ref: 1765.86 MB, ratio: 0.9817x) Kernel launches: 51 (ref: 26681, fusion ratio: 523.1569) Energy: 968.52 mJ/run (ref: 11866.5 mJ, ratio: 12.2522x) SOL score: 1.0 Flagged for excessive speedup — this submission may have been rejected by automated review.
final submitted kernel (115 lines, 3,342 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>

__global__ void selective_scan_kernel(
    const float* __restrict__ x,
    const float* __restrict__ delta,
    const float* __restrict__ B_t,
    const float* __restrict__ C_t,
    const float* __restrict__ dA,
    const float* __restrict__ x_db,
    float* __restrict__ y,
    int B, int L, int D, int N
) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    int total = B * D;
    if (idx >= total) return;
    int b = idx / D;
    int d = idx % D;

    float h[16];  // assuming N <=16
    for (int n=0; n<N; n++) h[n] = 0.0f;

    for (int t=0; t<L; t++) {
        int base_dn = (b * L + t) * D * N + d * N;
        for (int n=0; n<N; n++) {
            float da = dA[base_dn + n];
            float xdb = x_db[base_dn + n];
            h[n] = da * h[n] + xdb;
        }
        float yt = 0.0f;
        int base_cn = (b * L + t) * N;
        for (int n=0; n<N; n++) {
            yt += h[n] * C_t[base_cn + n];
        }
        y[(b * L + t) * D + d] = yt;
    }
}

torch::Tensor selective_scan_cuda(
    torch::Tensor x,
    torch::Tensor delta,
    torch::Tensor B_t,
    torch::Tensor C_t,
    torch::Tensor dA,
    torch::Tensor x_db
) {
    int B = x.size(0);
    int L = x.size(1);
    int D = x.size(2);
    int N = B_t.size(2);
    auto y = torch::empty({B, L, D}, x.options());
    int total = B * D;
    int threads = 256;
    int blocks = (total + threads - 1) / threads;
    selective_scan_kernel<<<blocks, threads>>>(
        x.data_ptr<float>(), delta.data_ptr<float>(), B_t.data_ptr<float>(),
        C_t.data_ptr<float>(), dA.data_ptr<float>(), x_db.data_ptr<float>(),
        y.data_ptr<float>(), B, L, D, N
    );
    return y;
}
'''

cpp_source = '''
torch::Tensor selective_scan_cuda(
    torch::Tensor x,
    torch::Tensor delta,
    torch::Tensor B_t,
    torch::Tensor C_t,
    torch::Tensor dA,
    torch::Tensor x_db
);
'''

module = load_inline(
    name='selective_scan',
    cpp_sources=[cpp_source],
    cuda_sources=[cuda_source],
    functions=['selective_scan_cuda'],
    verbose=False,
    extra_cuda_cflags=['-O3']
)

class ModelNew(nn.Module):
    def __init__(self, d_model: int, d_state: int):
        super(ModelNew, self).__init__()
        self.d_model = d_model
        self.d_state = d_state
        self.A_log = nn.Parameter(torch.randn(d_model, d_state))
        self.proj_B = nn.Linear(d_model, d_state, bias=False)
        self.proj_C = nn.Linear(d_model, d_state, bias=False)
        self.proj_delta = nn.Linear(d_model, d_model, bias=True)
        self.D = nn.Parameter(torch.ones(d_model))
        self.scan_module = module

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        B, L, D = x.shape
        N = self.d_state
        delta = torch.nn.functional.softplus(self.proj_delta(x))
        B_t = self.proj_B(x)
        C_t = self.proj_C(x)
        A = -torch.exp(self.A_log)
        dA = torch.exp(delta.unsqueeze(-1) * A.unsqueeze(0).unsqueeze(0))
        dB = delta.unsqueeze(-1) * B_t.unsqueeze(2)
        x_db = x.unsqueeze(-1) * dB
        y = self.scan_module.selective_scan_cuda(x, delta, B_t, C_t, dA, x_db)
        y = y + x * self.D.unsqueeze(0).unsqueeze(0)
        return y