grok-4.3 · popcorn · pop_l123_st_gpt
incorrect 14_GaussianProcessRBFKernel.py
model
grok-4.3
turns / max
1 / 1
tool calls
1
tokens (total)
2303
wall clock
649.6s
truncated
no
backend
cuda fp32
variant
popcorn
runtime
—
ref runtime
0.07 μs
speedup
—
started
2026-05-11T09:08:01Z
correctness issue: Output mismatch
timingkernelbench.timing
kernel runtime
—
ref runtime
0.07 μs
speedup
—
kernel mean / median / std
—
ref mean / median / std
0.07 / — / 0.00 μs
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
—
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn


class Model(nn.Module):
    """
    RBF (Squared Exponential) covariance matrix computation for Gaussian
    Processes.  Given two sets of input points, computes the full
    kernel matrix K[i,j] = sigma^2 * exp(-||x_i - x_j||^2 / (2 * l^2)).
    The dominant cost in GP inference and a natural target for fusion.
    """

    def __init__(self, input_dim, lengthscale=1.0, variance=1.0):
        super().__init__()
        self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())
        self.log_variance = nn.Parameter(torch.tensor(variance).log())

    def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:
        """
        Args:
            X1: (B, N, D) – first set of inputs
            X2: (B, M, D) – second set of inputs
        Returns:
            K: (B, N, M) – covariance matrix
        """
        l2 = self.log_lengthscale.exp() ** 2
        var = self.log_variance.exp()

        # Squared distances via expansion
        X1_sq = (X1 ** 2).sum(-1, keepdim=True)  # (B, N, 1)
        X2_sq = (X2 ** 2).sum(-1, keepdim=True)  # (B, M, 1)
        dist_sq = X1_sq - 2.0 * X1 @ X2.transpose(-1, -2) + X2_sq.transpose(-1, -2)
        dist_sq = dist_sq.clamp(min=0.0)

        return var * torch.exp(-0.5 * dist_sq / l2)


input_dim = 8
num_train = 512
num_test = 128
batch_size = 16


def get_inputs():
    X1 = torch.randn(popcorn_pri.jitter_int(batch_size), popcorn_pri.jitter_int(num_train), input_dim)
    X2 = torch.randn(popcorn_pri.jitter_int(batch_size), popcorn_pri.jitter_int(num_test), input_dim)
    return [X1, X2]


def get_init_inputs():
    return [input_dim]
turn 0649.6s turn · 12.6s LLM · 637.0s tools · tokens in=1,328 · out=736 · total=2,303 · FINAL
reasoning
The task is to replace the forward computation of a PyTorch model with a custom CUDA kernel for RBF covariance matrix computation.
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 = r'''\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <math.h>\n\n__global__ void rbf_kernel(\n const float* __restrict__ X1,\n const float* __restrict__ X2,\n float* __restrict__ K,\n const float var,\n const float inv_2l2,\n const int B, const int N, const int M, const int D\n) {\n int b = blockIdx.z;\n int i = blockIdx.y * blockDim.y + threadIdx.y;\n int j = blockIdx.x * blockDim.x + threadIdx.x;\n\n if (i >= N || j >= M || b >= B) return;\n\n float dist_sq = 0.0f;\n const float* x1_ptr = X1 + (b * N + i) * D;\n const float* x2_ptr = X2 + (b * M + j) * D;\n\n for (int d = 0; d < D; ++d) {\n float diff = x1_ptr[d] - x2_ptr[d];\n dist_sq += diff * diff;\n }\n\n float val = var * expf(-0.5f * dist_sq * inv_2l2);\n K[(b * N + i) * M + j] = val;\n}\n\ntorch::Tensor rbf_cuda(torch::Tensor X1, torch::Tensor X2, float var, float inv_2l2) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n\n auto K = torch::empty({B, N, M}, X1.options());\n\n dim3 threads(16, 16);\n dim3 blocks((M + 15) / 16, (N + 15) / 16, B);\n\n rbf_kernel<<<blocks, threads>>>(\n X1.data_ptr<float>(),\n X2.data_ptr<float>(),\n K.data_ptr<float>(),\n var,\n inv_2l2,\n B, N, M, D\n );\n\n return K;\n}\n'''\n\ncpp_source = r'''\ntorch::Tensor rbf_cuda(torch::Tensor X1, torch::Tensor X2, float var, float inv_2l2);\n'''\n\nrbf_module = load_inline(\n name=\"rbf_cuda\",\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=[\"rbf_cuda\"],\n verbose=False,\n extra_cuda_cflags=[\"-O3\"]\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n inv_2l2 = 1.0 / (2.0 * l2)\n return rbf_module.rbf_cuda(X1, X2, var.item(), inv_2l2.item())\n" }
result ← submit_kernel (fail)
failsubmit_kernel submit_kernel FAILED: correctness check did not pass ((0 / 5) trials). correctness_issue: Output mismatch max_difference: ['0.250000', '0.250000', '0.250000', '0.250000', '0.250000'] avg_difference: ['0.049613', '0.051119', '0.049839', '0.049715', '0.050459']
final submitted kernel (86 lines, 2,302 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline

cuda_source = r'''
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <math.h>

__global__ void rbf_kernel(
    const float* __restrict__ X1,
    const float* __restrict__ X2,
    float* __restrict__ K,
    const float var,
    const float inv_2l2,
    const int B, const int N, const int M, const int D
) {
    int b = blockIdx.z;
    int i = blockIdx.y * blockDim.y + threadIdx.y;
    int j = blockIdx.x * blockDim.x + threadIdx.x;

    if (i >= N || j >= M || b >= B) return;

    float dist_sq = 0.0f;
    const float* x1_ptr = X1 + (b * N + i) * D;
    const float* x2_ptr = X2 + (b * M + j) * D;

    for (int d = 0; d < D; ++d) {
        float diff = x1_ptr[d] - x2_ptr[d];
        dist_sq += diff * diff;
    }

    float val = var * expf(-0.5f * dist_sq * inv_2l2);
    K[(b * N + i) * M + j] = val;
}

torch::Tensor rbf_cuda(torch::Tensor X1, torch::Tensor X2, float var, float inv_2l2) {
    const int B = X1.size(0);
    const int N = X1.size(1);
    const int D = X1.size(2);
    const int M = X2.size(1);

    auto K = torch::empty({B, N, M}, X1.options());

    dim3 threads(16, 16);
    dim3 blocks((M + 15) / 16, (N + 15) / 16, B);

    rbf_kernel<<<blocks, threads>>>(
        X1.data_ptr<float>(),
        X2.data_ptr<float>(),
        K.data_ptr<float>(),
        var,
        inv_2l2,
        B, N, M, D
    );

    return K;
}
'''

cpp_source = r'''
torch::Tensor rbf_cuda(torch::Tensor X1, torch::Tensor X2, float var, float inv_2l2);
'''

rbf_module = load_inline(
    name="rbf_cuda",
    cpp_sources=[cpp_source],
    cuda_sources=[cuda_source],
    functions=["rbf_cuda"],
    verbose=False,
    extra_cuda_cflags=["-O3"]
)

class ModelNew(nn.Module):
    def __init__(self, input_dim, lengthscale=1.0, variance=1.0):
        super().__init__()
        self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())
        self.log_variance = nn.Parameter(torch.tensor(variance).log())

    def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:
        l2 = self.log_lengthscale.exp() ** 2
        var = self.log_variance.exp()
        inv_2l2 = 1.0 / (2.0 * l2)
        return rbf_module.rbf_cuda(X1, X2, var.item(), inv_2l2.item())