grok-4.3 · popcorn · pop_l123_all_gpt
correct 28_FusedGroupNormSiLU.py
model
grok-4.3
turns / max
4 / 15
tool calls
4
tokens (total)
17444
wall clock
1481.4s
truncated
no
backend
cuda fp32
variant
popcorn
runtime
0.02 μs
ref runtime
0.03 μs
speedup
1.30x
started
2026-05-11T20:36:34Z
timingkernelbench.timing
kernel runtime
0.02 μs
ref runtime
0.03 μs
speedup
1.30x
kernel mean / median / std
0.02 / — / 2.41e-04 μs
ref mean / median / std
0.03 / — / 4.46e-04 μs
numerical precisioneval_kernel_against_ref
max abs error
1.19e-06
mean abs error
2.12e-08
max rel error
0.01
mean rel error
6.86e-08
gpu memoryextended_metrics.measure_memory
kernel peak
42.00 MB
ref peak
46.50 MB
kernel / ref
0.90x
kernel launches / fusiontorch.profiler
kernel launches
2
ref launches
12
fusion ratio (ref / kernel)
6.00x
total cuda time
50 μs
SOL (speed-of-light)nsight_hardware_counters
SOL score (0–1)
0.149
dram util %
8.6%
compute util %
14.9%
bottleneck
compute-bound
dominant pipe
FP32
arithmetic intensity
—
ridge point
20.00
roofline / occupancynsight_hardware_counters
dram bandwidth
289.19 GB/s
dram util %
8.6%
fp32 tflops
—
fp32 util %
—
fp16 tflops
—
occupancy %
23.9%
bottleneck
compute-bound
dominant pipe %
14.9%
arithmetic intensity
—
ridge point
20.00
L1 hit %
—
L2 hit %
56.4%
ld sectors / req
2.50
st sectors / req
—
regs / thread
29
smem / block
3072 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
729.1%
short_scoreboard
87.9%
mio_throttle
8.7%
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn
import torch.nn.functional as F

# Default ModelNew is PyTorch-only. run_and_check with check_kernel=True and backend=cuda
# requires real CUDA (__global__ + load_inline/cpp_extension) in the kernel file—use
# check_kernel=False until you add a custom kernel implementation.

class Model(nn.Module):
    """
    Fused GroupNorm + SiLU (Swish), a common post-conv / residual-branch pattern in
    ResNet-family backbones (SiLU instead of ReLU) and in spatial blocks of diffusion
    transformers (DiT) where features are (N, C, H, W).

    Reference pipeline for a single fused kernel: normalize channels with
    `GroupNorm`, then apply `SiLU` elementwise (`x * sigmoid(x)`).
    """

    def __init__(
        self,
        num_channels: int,
        num_groups: int,
        eps: float = 1e-5,
        affine: bool = True,
    ):
        super().__init__()
        assert num_channels % num_groups == 0
        self.norm = nn.GroupNorm(
            num_groups=num_groups,
            num_channels=num_channels,
            eps=eps,
            affine=affine,
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Args:
            x: (N, C, H, W) feature map
        Returns:
            Same shape as x
        """
        return F.silu(self.norm(x))


class ModelNew(nn.Module):
    """
    KernelBench candidate entry point. Replace with a fused CUDA/Triton kernel;
    default delegates to `Model` for harness smoke tests (same file as ref + kernel).
    """

    def __init__(
        self,
        num_channels: int,
        num_groups: int,
        eps: float = 1e-5,
        affine: bool = True,
    ):
        super().__init__()
        self._impl = Model(num_channels, num_groups, eps=eps, affine=affine)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self._impl(x)


batch_size = 8
num_channels = 256
num_groups = 32
height = 24
width = 24


def get_inputs():
    p = popcorn_pri
    x = torch.randn(p.jitter_int(batch_size), num_channels, p.jitter_int(height), p.jitter_int(width))
    return [x]


def get_init_inputs():
    return [num_channels, num_groups]
turn 0410.8s turn · 2.5s LLM · 408.3s tools · tokens in=2,281 · out=6 · total=2,461
reasoning
The task is to replace the forward computation of a PyTorch model with a custom CUDA kernel. The model does GroupNorm followed by SiLU (Swish activation).
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 1412.7s turn · 12.3s LLM · 400.4s tools · tokens in=2,694 · out=1,011 · total=3,899
reasoning
GPU is H100, high bandwidth, lots of compute.
call → compile_kernel
callcompile_kernel { "kernel_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\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 fused_groupnorm_silu_kernel(\n const float* __restrict__ input,\n const float* __restrict__ gamma,\n const float* __restrict__ beta,\n float* __restrict__ output,\n int N, int C, int H, int W,\n int num_groups, float eps) {\n \n int nc = blockIdx.x; // n * num_groups + g\n int n = nc / num_groups;\n int g = nc % num_groups;\n \n int group_size = C / num_groups * H * W;\n int c_start = g * (C / num_groups);\n \n // Compute mean and var for this group\n float sum = 0.0f;\n float sum_sq = 0.0f;\n \n for (int i = threadIdx.x; i < group_size; i += blockDim.x) {\n int c = c_start + (i / (H*W));\n int hw = i % (H*W);\n int idx = ((n * C + c) * H * W) + hw;\n float val = input[idx];\n sum += val;\n sum_sq += val * val;\n }\n \n // Block reduce\n __shared__ float shared_sum[256];\n __shared__ float shared_sq[256];\n \n shared_sum[threadIdx.x] = sum;\n shared_sq[threadIdx.x] = sum_sq;\n __syncthreads();\n \n for (int s = blockDim.x / 2; s > 0; s >>= 1) {\n if (threadIdx.x < s) {\n shared_sum[threadIdx.x] += shared_sum[threadIdx.x + s];\n shared_sq[threadIdx.x] += shared_sq[threadIdx.x + s];\n }\n __syncthreads();\n }\n \n float mean = shared_sum[0] / group_size;\n float var = shared_sq[0] / group_size - mean * mean;\n float inv_std = rsqrtf(var + eps);\n \n // Normalize, apply gamma/beta, SiLU\n for (int i = threadIdx.x; i < group_size; i += blockDim.x) {\n int c = c_start + (i / (H*W));\n int hw = i % (H*W);\n int idx = ((n * C + c) * H * W) + hw;\n float val = input[idx];\n float normed = (val - mean) * inv_std;\n float out_val = normed * gamma[c] + beta[c];\n // SiLU\n out_val = out_val / (1.0f + expf(-out_val));\n output[idx] = out_val;\n }\n}\n\ntorch::Tensor fused_groupnorm_silu(\n torch::Tensor input,\n torch::Tensor gamma,\n torch::Tensor beta,\n int num_groups,\n float eps) {\n \n auto N = input.size(0);\n auto C = input.size(1);\n auto H = input.size(2);\n auto W = input.size(3);\n \n auto output = torch::empty_like(input);\n \n int num_blocks = N * num_groups;\n fused_groupnorm_silu_kernel<<<num_blocks, 256>>>(\n input.data_ptr<float>(),\n gamma.data_ptr<float>(),\n beta.data_ptr<float>(),\n output.data_ptr<float>(),\n N, C, H, W,\n num_groups, eps);\n \n return output;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor fused_groupnorm_silu(\n torch::Tensor input,\n torch::Tensor gamma,\n torch::Tensor beta,\n int num_groups,\n float eps);\n'''\n\nmodule = load_inline(\n name='fused_gn_silu',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['fused_groupnorm_silu'],\n verbose=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, num_channels, num_groups, eps=1e-5, affine=True):\n super().__init__()\n self.norm = nn.GroupNorm(num_groups=num_groups, num_channels=num_channels, eps=eps, affine=affine)\n self.num_groups = num_groups\n self.eps = eps\n self.fused = module\n \n def forward(self, x):\n gamma = self.norm.weight\n beta = self.norm.bias\n return self.fused.fused_groupnorm_silu(x, gamma, beta, self.num_groups, self.eps)\n" }
result ← compile_kernel (ok)
okcompile_kernel compile_kernel PASSED: kernel compiled without errors.
turn 2166.6s turn · 8.1s LLM · 158.5s tools · tokens in=3,916 · out=1,012 · total=4,935
reasoning
Compiled successfully. Now check correctness.
call → run_correctness
callrun_correctness { "kernel_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\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 fused_groupnorm_silu_kernel(\n const float* __restrict__ input,\n const float* __restrict__ gamma,\n const float* __restrict__ beta,\n float* __restrict__ output,\n int N, int C, int H, int W,\n int num_groups, float eps) {\n \n int nc = blockIdx.x; // n * num_groups + g\n int n = nc / num_groups;\n int g = nc % num_groups;\n \n int group_size = C / num_groups * H * W;\n int c_start = g * (C / num_groups);\n \n // Compute mean and var for this group\n float sum = 0.0f;\n float sum_sq = 0.0f;\n \n for (int i = threadIdx.x; i < group_size; i += blockDim.x) {\n int c = c_start + (i / (H*W));\n int hw = i % (H*W);\n int idx = ((n * C + c) * H * W) + hw;\n float val = input[idx];\n sum += val;\n sum_sq += val * val;\n }\n \n // Block reduce\n __shared__ float shared_sum[256];\n __shared__ float shared_sq[256];\n \n shared_sum[threadIdx.x] = sum;\n shared_sq[threadIdx.x] = sum_sq;\n __syncthreads();\n \n for (int s = blockDim.x / 2; s > 0; s >>= 1) {\n if (threadIdx.x < s) {\n shared_sum[threadIdx.x] += shared_sum[threadIdx.x + s];\n shared_sq[threadIdx.x] += shared_sq[threadIdx.x + s];\n }\n __syncthreads();\n }\n \n float mean = shared_sum[0] / group_size;\n float var = shared_sq[0] / group_size - mean * mean;\n float inv_std = rsqrtf(var + eps);\n \n // Normalize, apply gamma/beta, SiLU\n for (int i = threadIdx.x; i < group_size; i += blockDim.x) {\n int c = c_start + (i / (H*W));\n int hw = i % (H*W);\n int idx = ((n * C + c) * H * W) + hw;\n float val = input[idx];\n float normed = (val - mean) * inv_std;\n float out_val = normed * gamma[c] + beta[c];\n // SiLU\n out_val = out_val / (1.0f + expf(-out_val));\n output[idx] = out_val;\n }\n}\n\ntorch::Tensor fused_groupnorm_silu(\n torch::Tensor input,\n torch::Tensor gamma,\n torch::Tensor beta,\n int num_groups,\n float eps) {\n \n auto N = input.size(0);\n auto C = input.size(1);\n auto H = input.size(2);\n auto W = input.size(3);\n \n auto output = torch::empty_like(input);\n \n int num_blocks = N * num_groups;\n fused_groupnorm_silu_kernel<<<num_blocks, 256>>>(\n input.data_ptr<float>(),\n gamma.data_ptr<float>(),\n beta.data_ptr<float>(),\n output.data_ptr<float>(),\n N, C, H, W,\n num_groups, eps);\n \n return output;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor fused_groupnorm_silu(\n torch::Tensor input,\n torch::Tensor gamma,\n torch::Tensor beta,\n int num_groups,\n float eps);\n'''\n\nmodule = load_inline(\n name='fused_gn_silu',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['fused_groupnorm_silu'],\n verbose=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, num_channels, num_groups, eps=1e-5, affine=True):\n super().__init__()\n self.norm = nn.GroupNorm(num_groups=num_groups, num_channels=num_channels, eps=eps, affine=affine)\n self.num_groups = num_groups\n self.eps = eps\n self.fused = module\n \n def forward(self, x):\n gamma = self.norm.weight\n beta = self.norm.bias\n return self.fused.fused_groupnorm_silu(x, gamma, beta, self.num_groups, self.eps)\n" }
result ← run_correctness (ok)
okrun_correctness run_correctness PASSED: (3 / 3) trials all matched the reference. Numerical precision: max_abs_err=1.19e-06 mean_abs_err=2.12e-08 max_rel_err=1.25e-02
turn 3491.3s turn · 19.7s LLM · 471.6s tools · tokens in=4,995 · out=1,011 · total=6,149 · FINAL
reasoning
Correct! Now, to check if faster, but since we need to submit, and it should be faster, but let's profile or just submit.
call → submit_kernel
callsubmit_kernel { "kernel_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\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 fused_groupnorm_silu_kernel(\n const float* __restrict__ input,\n const float* __restrict__ gamma,\n const float* __restrict__ beta,\n float* __restrict__ output,\n int N, int C, int H, int W,\n int num_groups, float eps) {\n \n int nc = blockIdx.x; // n * num_groups + g\n int n = nc / num_groups;\n int g = nc % num_groups;\n \n int group_size = C / num_groups * H * W;\n int c_start = g * (C / num_groups);\n \n // Compute mean and var for this group\n float sum = 0.0f;\n float sum_sq = 0.0f;\n \n for (int i = threadIdx.x; i < group_size; i += blockDim.x) {\n int c = c_start + (i / (H*W));\n int hw = i % (H*W);\n int idx = ((n * C + c) * H * W) + hw;\n float val = input[idx];\n sum += val;\n sum_sq += val * val;\n }\n \n // Block reduce\n __shared__ float shared_sum[256];\n __shared__ float shared_sq[256];\n \n shared_sum[threadIdx.x] = sum;\n shared_sq[threadIdx.x] = sum_sq;\n __syncthreads();\n \n for (int s = blockDim.x / 2; s > 0; s >>= 1) {\n if (threadIdx.x < s) {\n shared_sum[threadIdx.x] += shared_sum[threadIdx.x + s];\n shared_sq[threadIdx.x] += shared_sq[threadIdx.x + s];\n }\n __syncthreads();\n }\n \n float mean = shared_sum[0] / group_size;\n float var = shared_sq[0] / group_size - mean * mean;\n float inv_std = rsqrtf(var + eps);\n \n // Normalize, apply gamma/beta, SiLU\n for (int i = threadIdx.x; i < group_size; i += blockDim.x) {\n int c = c_start + (i / (H*W));\n int hw = i % (H*W);\n int idx = ((n * C + c) * H * W) + hw;\n float val = input[idx];\n float normed = (val - mean) * inv_std;\n float out_val = normed * gamma[c] + beta[c];\n // SiLU\n out_val = out_val / (1.0f + expf(-out_val));\n output[idx] = out_val;\n }\n}\n\ntorch::Tensor fused_groupnorm_silu(\n torch::Tensor input,\n torch::Tensor gamma,\n torch::Tensor beta,\n int num_groups,\n float eps) {\n \n auto N = input.size(0);\n auto C = input.size(1);\n auto H = input.size(2);\n auto W = input.size(3);\n \n auto output = torch::empty_like(input);\n \n int num_blocks = N * num_groups;\n fused_groupnorm_silu_kernel<<<num_blocks, 256>>>(\n input.data_ptr<float>(),\n gamma.data_ptr<float>(),\n beta.data_ptr<float>(),\n output.data_ptr<float>(),\n N, C, H, W,\n num_groups, eps);\n \n return output;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor fused_groupnorm_silu(\n torch::Tensor input,\n torch::Tensor gamma,\n torch::Tensor beta,\n int num_groups,\n float eps);\n'''\n\nmodule = load_inline(\n name='fused_gn_silu',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['fused_groupnorm_silu'],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, num_channels, num_groups, eps=1e-5, affine=True):\n super().__init__()\n self.norm = nn.GroupNorm(num_groups=num_groups, num_channels=num_channels, eps=eps, affine=affine)\n self.num_groups = num_groups\n self.eps = eps\n self.fused = module\n \n def forward(self, x):\n gamma = self.norm.weight\n beta = self.norm.bias\n return self.fused.fused_groupnorm_silu(x, gamma, beta, self.num_groups, self.eps)\n" }
result ← submit_kernel (ok)
oksubmit_kernel submit_kernel PASSED: (3 / 3) correctness trials all passed. Kernel runtime: 0.02 μs Runtime stats: mean=0.02μs median=?μs std=0.00μs Numerical precision: max_abs_err=1.19e-06 mean_abs_err=2.12e-08 max_rel_err=1.25e-02 Memory: 42.0 MB peak (ref: 46.5 MB, ratio: 0.9032x) Kernel launches: 2 (ref: 12, fusion ratio: 6.0) SOL score: 0.1494
final submitted kernel (129 lines, 3,561 chars) — full source, untruncated
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.cpp_extension import load_inline

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

__global__ void fused_groupnorm_silu_kernel(
    const float* __restrict__ input,
    const float* __restrict__ gamma,
    const float* __restrict__ beta,
    float* __restrict__ output,
    int N, int C, int H, int W,
    int num_groups, float eps) {
    
    int nc = blockIdx.x; // n * num_groups + g
    int n = nc / num_groups;
    int g = nc % num_groups;
    
    int group_size = C / num_groups * H * W;
    int c_start = g * (C / num_groups);
    
    // Compute mean and var for this group
    float sum = 0.0f;
    float sum_sq = 0.0f;
    
    for (int i = threadIdx.x; i < group_size; i += blockDim.x) {
        int c = c_start + (i / (H*W));
        int hw = i % (H*W);
        int idx = ((n * C + c) * H * W) + hw;
        float val = input[idx];
        sum += val;
        sum_sq += val * val;
    }
    
    // Block reduce
    __shared__ float shared_sum[256];
    __shared__ float shared_sq[256];
    
    shared_sum[threadIdx.x] = sum;
    shared_sq[threadIdx.x] = sum_sq;
    __syncthreads();
    
    for (int s = blockDim.x / 2; s > 0; s >>= 1) {
        if (threadIdx.x < s) {
            shared_sum[threadIdx.x] += shared_sum[threadIdx.x + s];
            shared_sq[threadIdx.x] += shared_sq[threadIdx.x + s];
        }
        __syncthreads();
    }
    
    float mean = shared_sum[0] / group_size;
    float var = shared_sq[0] / group_size - mean * mean;
    float inv_std = rsqrtf(var + eps);
    
    // Normalize, apply gamma/beta, SiLU
    for (int i = threadIdx.x; i < group_size; i += blockDim.x) {
        int c = c_start + (i / (H*W));
        int hw = i % (H*W);
        int idx = ((n * C + c) * H * W) + hw;
        float val = input[idx];
        float normed = (val - mean) * inv_std;
        float out_val = normed * gamma[c] + beta[c];
        // SiLU
        out_val = out_val / (1.0f + expf(-out_val));
        output[idx] = out_val;
    }
}

torch::Tensor fused_groupnorm_silu(
    torch::Tensor input,
    torch::Tensor gamma,
    torch::Tensor beta,
    int num_groups,
    float eps) {
    
    auto N = input.size(0);
    auto C = input.size(1);
    auto H = input.size(2);
    auto W = input.size(3);
    
    auto output = torch::empty_like(input);
    
    int num_blocks = N * num_groups;
    fused_groupnorm_silu_kernel<<<num_blocks, 256>>>(
        input.data_ptr<float>(),
        gamma.data_ptr<float>(),
        beta.data_ptr<float>(),
        output.data_ptr<float>(),
        N, C, H, W,
        num_groups, eps);
    
    return output;
}
'''

cpp_source = '''
torch::Tensor fused_groupnorm_silu(
    torch::Tensor input,
    torch::Tensor gamma,
    torch::Tensor beta,
    int num_groups,
    float eps);
'''

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

class ModelNew(nn.Module):
    def __init__(self, num_channels, num_groups, eps=1e-5, affine=True):
        super().__init__()
        self.norm = nn.GroupNorm(num_groups=num_groups, num_channels=num_channels, eps=eps, affine=affine)
        self.num_groups = num_groups
        self.eps = eps
        self.fused = module
    
    def forward(self, x):
        gamma = self.norm.weight
        beta = self.norm.bias
        return self.fused.fused_groupnorm_silu(x, gamma, beta, self.num_groups, self.eps)