grok-4.3 · popcorn · pop_l123_default_gpt
correct 40_SmithWatermanDPScore.py
model
grok-4.3
turns / max
3 / 12
tool calls
3
tokens (total)
12992
wall clock
2106.4s
truncated
no
backend
cuda fp32
variant
popcorn
runtime
0.19 μs
ref runtime
137.00 μs
speedup
713.54x
started
2026-05-11T14:43:02Z
timingkernelbench.timing
kernel runtime
0.19 μs
ref runtime
137.00 μs
speedup
713.54x
kernel mean / median / std
0.19 / — / 8.99e-04 μs
ref mean / median / std
137.00 / — / 8.62 μs
numerical precisioneval_kernel_against_ref
max abs error
3.81e-06
mean abs error
2.86e-07
max rel error
1.32e-07
mean rel error
9.14e-09
gpu memoryextended_metrics.measure_memory
kernel peak
33.06 MB
ref peak
33.13 MB
kernel / ref
1.00x
kernel launches / fusiontorch.profiler
kernel launches
4
ref launches
61456
fusion ratio (ref / kernel)
15364.00x
total cuda time
41 μs
energyNVML (pynvml)
kernel mJ / run
240.86
ref mJ / run
16137.38
ratio (ref / kernel)
67.00x
avg power
649.79 W
ref avg power
120.39 W
SOL (speed-of-light)nsight_hardware_counters
SOL score (0–1)
0.001
dram util %
0.1%
compute util %
0.0%
bottleneck
unknown
dominant pipe
None
arithmetic intensity
—
ridge point
20.00
roofline / occupancynsight_hardware_counters
dram bandwidth
3.83 GB/s
dram util %
0.1%
fp32 tflops
—
fp32 util %
—
fp16 tflops
—
occupancy %
7.6%
bottleneck
unknown
dominant pipe %
—
arithmetic intensity
—
ridge point
20.00
L1 hit %
—
L2 hit %
220.4%
ld sectors / req
16.00
st sectors / req
—
regs / thread
45
smem / block
6272 B
block size
144
peak bw
3350.00 GB/s
peak fp32 tflops
67.00
peak fp16 tflops
1979.00
warp stalls (top 5)nsight
short_scoreboard
185.7%
long_scoreboard
127.1%
flagssubmit_kernel guard
excessive speedup
flagged
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    """
    Differentiable Smith-Waterman-style local sequence alignment scoring.
    Uses a soft-max relaxation of the DP recurrence so gradients can flow
    through the alignment matrix.  
    """
    def __init__(self, gap_open=-1.0, temperature=1.0):
        super().__init__()
        self.gap_open = gap_open
        self.temperature = temperature

    def forward(self, score_matrix: torch.Tensor) -> torch.Tensor:
        """
        Args:
            score_matrix: (B, M, N) – per-position similarity scores
        Returns:
            alignment_score: (B,) – soft local-alignment score (log-sum-exp over DP cells)
        """
        B, M, N = score_matrix.shape
        neg_inf = torch.tensor(float("-inf"), device=score_matrix.device, dtype=score_matrix.dtype)

        # H[i][j] = max(0, H[i-1][j-1]+S[i][j], H[i-1][j]+gap, H[i][j-1]+gap)
        H = score_matrix.new_full((B, M + 1, N + 1), 0.0)

        for i in range(1, M + 1):
            for j in range(1, N + 1):
                match = H[:, i - 1, j - 1] + score_matrix[:, i - 1, j - 1]
                delete = H[:, i - 1, j] + self.gap_open
                insert = H[:, i, j - 1] + self.gap_open
                zero = torch.zeros(B, device=score_matrix.device)
                stack = torch.stack([zero, match, delete, insert], dim=-1)
                H[:, i, j] = self.temperature * torch.logsumexp(stack / self.temperature, dim=-1)

        return H.view(B, -1).max(dim=-1).values


seq_m = 32
seq_n = 32
batch_size = 16


def get_inputs():
    return [torch.randn(popcorn_pri.jitter_int(batch_size), seq_m, seq_n)]


def get_init_inputs():
    return []
turn 0928.4s turn · 19.2s LLM · 909.2s tools · tokens in=1,707 · out=883 · total=3,401
reasoning
The task is to replace the forward computation of this PyTorch model with a custom CUDA kernel. The model computes a soft Smith-Waterman alignment score using a dynamic programming approach with logsu...I need to replace the forward computation of a PyTorch model with a custom CUDA kernel. The model computes a soft Smith-Waterman alignment score using dynamic programming with logsumexp for softness. It fills a (B, M+1, N+1) matrix H, where each cell depends on the left, ขึ้นจาก diagonal, and diagonal previous cells, plus the score. Since M and N are 32, batch size 16, it's feasible to write a CUDA kernel where each thread processes one batch element, looping over i and j to fill the H matrix.
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#include <cfloat>\n\n__global__ void soft_smith_waterman_kernel(\n const float* __restrict__ score_matrix,\n float* __restrict__ output,\n int B, int M, int N,\n float gap_open, float temperature\n) {\n int b = blockIdx.x * blockDim.x + threadIdx.x;\n if (b >= B) return;\n\n const float* scores = score_matrix + b * M * N;\n float max_score = 0.0f;\n const float neg_inf = -FLT_MAX;\n const float inv_temp = 1.0f / temperature;\n\n // Use two rows: prev and curr, size N+1\n extern __shared__ float shared[];\n float* prev_row = shared + threadIdx.x * (N + 1);\n float* curr_row = shared + (blockDim.x * (N + 1)) + threadIdx.x * (N + 1);\n\n // Initialize first row and col to 0\n for (int j = 0; j <= N; j++) {\n prev_row[j] = 0.0f;\n }\n curr_row[0] = 0.0f;\n\n for (int i = 1; i <= M; i++) {\n curr_row[0] = 0.0f;\n for (int j = 1; j <= N; j++) {\n float match = prev_row[j-1] + scores[(i-1)*N + (j-1)];\n float delete_ = prev_row[j] + gap_open;\n float insert = curr_row[j-1] + gap_open;\n float zero = 0.0f;\n\n // logsumexp with temperature\n float m = fmaxf(fmaxf(zero, match), fmaxf(delete_, insert));\n float sum = expf((zero - m) * inv_temp) +\n expf((match - m) * inv_temp) +\n expf((delete_ - m) * inv_temp) +\n expf((insert - m) * inv_temp);\n float val = temperature * logf(sum) + m;\n curr_row[j] = val;\n if (val > max_score) max_score = val;\n }\n // swap rows\n float* temp = prev_row;\n prev_row = curr_row;\n curr_row = temp;\n }\n\n output[b] = max_score;\n}\n\ntorch::Tensor soft_smith_waterman_cuda(torch::Tensor score_matrix, float gap_open, float temperature) {\n int B = score_matrix.size(0);\n int M = score_matrix.size(1);\n int N = score_matrix.size(2);\n\n auto output = torch::zeros({B}, score_matrix.options());\n\n int threads = 16; // since B=16 typically\n int blocks = (B + threads - 1) / threads;\n int shared_size = 2 * threads * (N + 1) * sizeof(float);\n\n soft_smith_waterman_kernel<<<blocks, threads, shared_size>>>(\n score_matrix.data_ptr<float>(),\n output.data_ptr<float>(),\n B, M, N, gap_open, temperature\n );\n\n return output;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor soft_smith_waterman_cuda(torch::Tensor score_matrix, float gap_open, float temperature);\n'''\n\nmodule = load_inline(\n name='soft_sw',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['soft_smith_waterman_cuda'],\n verbose=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, gap_open=-1.0, temperature=1.0):\n super().__init__()\n self.gap_open = gap_open\n self.temperature = temperature\n self.module = module\n\n def forward(self, score_matrix: torch.Tensor) -> torch.Tensor:\n return self.module.soft_smith_waterman_cuda(score_matrix, self.gap_open, self.temperature)\n" }
result ← compile_kernel (ok)
okcompile_kernel compile_kernel PASSED: kernel compiled without errors.
turn 1593.7s turn · 9.2s LLM · 584.5s tools · tokens in=3,418 · out=884 · total=4,309
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#include <cfloat>\n\n__global__ void soft_smith_waterman_kernel(\n const float* __restrict__ score_matrix,\n float* __restrict__ output,\n int B, int M, int N,\n float gap_open, float temperature\n) {\n int b = blockIdx.x * blockDim.x + threadIdx.x;\n if (b >= B) return;\n\n const float* scores = score_matrix + b * M * N;\n float max_score = 0.0f;\n const float neg_inf = -FLT_MAX;\n const float inv_temp = 1.0f / temperature;\n\n // Use two rows: prev and curr, size N+1\n extern __shared__ float shared[];\n float* prev_row = shared + threadIdx.x * (N + 1);\n float* curr_row = shared + (blockDim.x * (N + 1)) + threadIdx.x * (N + 1);\n\n // Initialize first row and col to 0\n for (int j = 0; j <= N; j++) {\n prev_row[j] = 0.0f;\n }\n curr_row[0] = 0.0f;\n\n for (int i = 1; i <= M; i++) {\n curr_row[0] = 0.0f;\n for (int j = 1; j <= N; j++) {\n float match = prev_row[j-1] + scores[(i-1)*N + (j-1)];\n float delete_ = prev_row[j] + gap_open;\n float insert = curr_row[j-1] + gap_open;\n float zero = 0.0f;\n\n // logsumexp with temperature\n float m = fmaxf(fmaxf(zero, match), fmaxf(delete_, insert));\n float sum = expf((zero - m) * inv_temp) +\n expf((match - m) * inv_temp) +\n expf((delete_ - m) * inv_temp) +\n expf((insert - m) * inv_temp);\n float val = temperature * logf(sum) + m;\n curr_row[j] = val;\n if (val > max_score) max_score = val;\n }\n // swap rows\n float* temp = prev_row;\n prev_row = curr_row;\n curr_row = temp;\n }\n\n output[b] = max_score;\n}\n\ntorch::Tensor soft_smith_waterman_cuda(torch::Tensor score_matrix, float gap_open, float temperature) {\n int B = score_matrix.size(0);\n int M = score_matrix.size(1);\n int N = score_matrix.size(2);\n\n auto output = torch::zeros({B}, score_matrix.options());\n\n int threads = 16; // since B=16 typically\n int blocks = (B + threads - 1) / threads;\n int shared_size = 2 * threads * (N + 1) * sizeof(float);\n\n soft_smith_waterman_kernel<<<blocks, threads, shared_size>>>(\n score_matrix.data_ptr<float>(),\n output.data_ptr<float>(),\n B, M, N, gap_open, temperature\n );\n\n return output;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor soft_smith_waterman_cuda(torch::Tensor score_matrix, float gap_open, float temperature);\n'''\n\nmodule = load_inline(\n name='soft_sw',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['soft_smith_waterman_cuda'],\n verbose=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, gap_open=-1.0, temperature=1.0):\n super().__init__()\n self.gap_open = gap_open\n self.temperature = temperature\n self.module = module\n\n def forward(self, score_matrix: torch.Tensor) -> torch.Tensor:\n return self.module.soft_smith_waterman_cuda(score_matrix, self.gap_open, self.temperature)\n" }
result ← run_correctness (ok)
okrun_correctness run_correctness PASSED: (5 / 5) trials all matched the reference. Numerical precision: max_abs_err=3.81e-06 mean_abs_err=2.86e-07 max_rel_err=1.32e-07
turn 2584.2s turn · 7.5s LLM · 576.7s tools · tokens in=4,369 · out=866 · total=5,282 · FINAL
reasoning
Correct! Now, since it's the final, I should 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_runtime.h>\n#include <cfloat>\n\n__global__ void soft_smith_waterman_kernel(\n const float* __restrict__ score_matrix,\n float* __restrict__ output,\n int B, int M, int N,\n float gap_open, float temperature\n) {\n int b = blockIdx.x * blockDim.x + threadIdx.x;\n if (b >= B) return;\n\n const float* scores = score_matrix + b * M * N;\n float max_score = 0.0f;\n const float inv_temp = 1.0f / temperature;\n\n // Use two rows: prev and curr, size N+1\n extern __shared__ float shared[];\n float* prev_row = shared + threadIdx.x * (N + 1);\n float* curr_row = shared + (blockDim.x * (N + 1)) + threadIdx.x * (N + 1);\n\n // Initialize first row and col to 0\n for (int j = 0; j <= N; j++) {\n prev_row[j] = 0.0f;\n }\n curr_row[0] = 0.0f;\n\n for (int i = 1; i <= M; i++) {\n curr_row[0] = 0.0f;\n for (int j = 1; j <= N; j++) {\n float match = prev_row[j-1] + scores[(i-1)*N + (j-1)];\n float delete_ = prev_row[j] + gap_open;\n float insert = curr_row[j-1] + gap_open;\n float zero = 0.0f;\n\n // logsumexp with temperature\n float m = fmaxf(fmaxf(zero, match), fmaxf(delete_, insert));\n float sum = expf((zero - m) * inv_temp) +\n expf((match - m) * inv_temp) +\n expf((delete_ - m) * inv_temp) +\n expf((insert - m) * inv_temp);\n float val = temperature * logf(sum) + m;\n curr_row[j] = val;\n if (val > max_score) max_score = val;\n }\n // swap rows\n float* temp = prev_row;\n prev_row = curr_row;\n curr_row = temp;\n }\n\n output[b] = max_score;\n}\n\ntorch::Tensor soft_smith_waterman_cuda(torch::Tensor score_matrix, float gap_open, float temperature) {\n int B = score_matrix.size(0);\n int M = score_matrix.size(1);\n int N = score_matrix.size(2);\n\n auto output = torch::zeros({B}, score_matrix.options());\n\n int threads = 16;\n int blocks = (B + threads - 1) / threads;\n int shared_size = 2 * threads * (N + 1) * sizeof(float);\n\n soft_smith_waterman_kernel<<<blocks, threads, shared_size>>>(\n score_matrix.data_ptr<float>(),\n output.data_ptr<float>(),\n B, M, N, gap_open, temperature\n );\n\n return output;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor soft_smith_waterman_cuda(torch::Tensor score_matrix, float gap_open, float temperature);\n'''\n\nmodule = load_inline(\n name='soft_sw',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['soft_smith_waterman_cuda'],\n verbose=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, gap_open=-1.0, temperature=1.0):\n super().__init__()\n self.gap_open = gap_open;\n self.temperature = temperature;\n self.module = module;\n\n def forward(self, score_matrix: torch.Tensor) -> torch.Tensor:\n return self.module.soft_smith_waterman_cuda(score_matrix, self.gap_open, self.temperature)\n" }
result ← submit_kernel (ok)
oksubmit_kernel submit_kernel PASSED: (5 / 5) 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=3.81e-06 mean_abs_err=2.86e-07 max_rel_err=1.32e-07 Memory: 33.06 MB peak (ref: 33.13 MB, ratio: 0.9978x) Kernel launches: 4 (ref: 61456, fusion ratio: 15364.0) Energy: 240.86 mJ/run (ref: 16137.38 mJ, ratio: 66.999x) SOL score: 0.0011 Flagged for excessive speedup — this submission may have been rejected by automated review.
final submitted kernel (103 lines, 3,137 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline

cuda_source = '''
#include <torch/extension.h>
#include <cuda_runtime.h>
#include <cfloat>

__global__ void soft_smith_waterman_kernel(
    const float* __restrict__ score_matrix,
    float* __restrict__ output,
    int B, int M, int N,
    float gap_open, float temperature
) {
    int b = blockIdx.x * blockDim.x + threadIdx.x;
    if (b >= B) return;

    const float* scores = score_matrix + b * M * N;
    float max_score = 0.0f;
    const float inv_temp = 1.0f / temperature;

    // Use two rows: prev and curr, size N+1
    extern __shared__ float shared[];
    float* prev_row = shared + threadIdx.x * (N + 1);
    float* curr_row = shared + (blockDim.x * (N + 1)) + threadIdx.x * (N + 1);

    // Initialize first row and col to 0
    for (int j = 0; j <= N; j++) {
        prev_row[j] = 0.0f;
    }
    curr_row[0] = 0.0f;

    for (int i = 1; i <= M; i++) {
        curr_row[0] = 0.0f;
        for (int j = 1; j <= N; j++) {
            float match = prev_row[j-1] + scores[(i-1)*N + (j-1)];
            float delete_ = prev_row[j] + gap_open;
            float insert = curr_row[j-1] + gap_open;
            float zero = 0.0f;

            // logsumexp with temperature
            float m = fmaxf(fmaxf(zero, match), fmaxf(delete_, insert));
            float sum = expf((zero - m) * inv_temp) +
                        expf((match - m) * inv_temp) +
                        expf((delete_ - m) * inv_temp) +
                        expf((insert - m) * inv_temp);
            float val = temperature * logf(sum) + m;
            curr_row[j] = val;
            if (val > max_score) max_score = val;
        }
        // swap rows
        float* temp = prev_row;
        prev_row = curr_row;
        curr_row = temp;
    }

    output[b] = max_score;
}

torch::Tensor soft_smith_waterman_cuda(torch::Tensor score_matrix, float gap_open, float temperature) {
    int B = score_matrix.size(0);
    int M = score_matrix.size(1);
    int N = score_matrix.size(2);

    auto output = torch::zeros({B}, score_matrix.options());

    int threads = 16;
    int blocks = (B + threads - 1) / threads;
    int shared_size = 2 * threads * (N + 1) * sizeof(float);

    soft_smith_waterman_kernel<<<blocks, threads, shared_size>>>(
        score_matrix.data_ptr<float>(),
        output.data_ptr<float>(),
        B, M, N, gap_open, temperature
    );

    return output;
}
'''

cpp_source = '''
torch::Tensor soft_smith_waterman_cuda(torch::Tensor score_matrix, float gap_open, float temperature);
'''

module = load_inline(
    name='soft_sw',
    cpp_sources=[cpp_source],
    cuda_sources=[cuda_source],
    functions=['soft_smith_waterman_cuda'],
    verbose=True
)

class ModelNew(nn.Module):
    def __init__(self, gap_open=-1.0, temperature=1.0):
        super().__init__()
        self.gap_open = gap_open;
        self.temperature = temperature;
        self.module = module;

    def forward(self, score_matrix: torch.Tensor) -> torch.Tensor:
        return self.module.soft_smith_waterman_cuda(score_matrix, self.gap_open, self.temperature)