gpt-5.5-priority · original · l123_single_gpt
correct 36_RMSNorm_.py
model
gpt-5.5-priority
turns / max
1 / 1
tool calls
1
tokens (total)
0
wall clock
—
truncated
no
backend
cuda fp32
variant
original
runtime
5.79 μs
ref runtime
14.70 μs
speedup
2.54x
started
2026-05-05T02:16:33Z
timingkernelbench.timing
kernel runtime
5.79 μs
ref runtime
14.70 μs
speedup
2.54x
kernel mean / median / std
5.79 / — / 0.24 μs
ref mean / median / std
14.70 / — / 0.89 μs
numerical precisioneval_kernel_against_ref
max abs error
8.34e-07
mean abs error
5.32e-08
max rel error
5.40e-07
mean rel error
6.15e-08
gpu memoryextended_metrics.measure_memory
kernel peak
14369.00 MB
ref peak
14481.00 MB
kernel / ref
0.99x
kernel launches / fusiontorch.profiler
kernel launches
2
ref launches
7
fusion ratio (ref / kernel)
3.50x
total cuda time
62 μs
energyNVML (pynvml)
kernel mJ / run
1932.48
ref mJ / run
1691.74
ratio (ref / kernel)
0.88x
avg power
335.46 W
ref avg power
118.89 W
SOL (speed-of-light)nsight_hardware_counters
SOL score (0–1)
0.850
dram util %
85.0%
compute util %
0.0%
bottleneck
unknown
dominant pipe
None
arithmetic intensity
—
ridge point
20.00
roofline / occupancynsight_hardware_counters
dram bandwidth
2848.77 GB/s
dram util %
85.0%
fp32 tflops
—
fp32 util %
—
fp16 tflops
—
occupancy %
10.1%
bottleneck
unknown
dominant pipe %
—
arithmetic intensity
—
ridge point
20.00
L1 hit %
—
L2 hit %
55.4%
ld sectors / req
16.00
st sectors / req
—
regs / thread
255
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
3882.1%
short_scoreboard
11.2%
mio_throttle
0.0%
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn

class Model(nn.Module):
    """
    Simple model that performs RMS Normalization.
    """
    def __init__(self, num_features: int, eps: float = 1e-5):
        """
        Initializes the RMSNorm layer.

        Args:
            num_features (int): Number of features in the input tensor.
            eps (float, optional): A small value added to the denominator to avoid division by zero. Defaults to 1e-5.
        """
        super(Model, self).__init__()
        self.num_features = num_features
        self.eps = eps

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Applies RMS Normalization to the input tensor.

        Args:
            x (torch.Tensor): Input tensor of shape (batch_size, num_features, *).

        Returns:
            torch.Tensor: Output tensor with RMS Normalization applied, same shape as input.
        """
        # Calculate the RMS along the feature dimension
        rms = torch.sqrt(torch.mean(x ** 2, dim=1, keepdim=True) + self.eps)

        # Normalize the input by dividing by the RMS
        return x / rms

batch_size = 112
features = 64
dim1 = 512
dim2 = 512

def get_inputs():
    x = torch.rand(batch_size, features, dim1, dim2)
    return [x]

def get_init_inputs():
    return [features]
turn 0 · FINAL · 1 tool call(s) · 49.6s turn · 49.6s LLM · 8,635 chars (click to expand)
turn 049.6s turn · 49.6s LLM · 0.0s tools · FINAL
call → submit_kernel
callsubmit_kernel { "kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n\n_cpp_src = r'''\n#include <torch/extension.h>\n\ntorch::Tensor rmsnorm_forward(torch::Tensor x, double eps);\n'''\n\n_cuda_src = r'''\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cstdint>\n\n__global__ void rmsnorm64_vec4_kernel(const float* __restrict__ x,\n float* __restrict__ y,\n int64_t vec_cols,\n int S4,\n int S,\n float eps) {\n int64_t tid = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n\n for (int64_t idx = tid; idx < vec_cols; idx += stride) {\n int n = (int)(idx / S4);\n int sv = (int)(idx - (int64_t)n * S4);\n int s = sv << 2;\n int64_t base = ((int64_t)n * 64LL * (int64_t)S) + (int64_t)s;\n\n float sum0 = 0.0f, sum1 = 0.0f, sum2 = 0.0f, sum3 = 0.0f;\n\n #pragma unroll\n for (int c = 0; c < 64; ++c) {\n const float4 v = *reinterpret_cast<const float4*>(x + base + (int64_t)c * S);\n sum0 = fmaf(v.x, v.x, sum0);\n sum1 = fmaf(v.y, v.y, sum1);\n sum2 = fmaf(v.z, v.z, sum2);\n sum3 = fmaf(v.w, v.w, sum3);\n }\n\n const float inv0 = rsqrtf(sum0 * 0.015625f + eps);\n const float inv1 = rsqrtf(sum1 * 0.015625f + eps);\n const float inv2 = rsqrtf(sum2 * 0.015625f + eps);\n const float inv3 = rsqrtf(sum3 * 0.015625f + eps);\n\n #pragma unroll\n for (int c = 0; c < 64; ++c) {\n const int64_t off = base + (int64_t)c * S;\n const float4 v = *reinterpret_cast<const float4*>(x + off);\n float4 o;\n o.x = v.x * inv0;\n o.y = v.y * inv1;\n o.z = v.z * inv2;\n o.w = v.w * inv3;\n *reinterpret_cast<float4*>(y + off) = o;\n }\n }\n}\n\n__global__ void rmsnorm_generic_vec4_kernel(const float* __restrict__ x,\n float* __restrict__ y,\n int64_t vec_cols,\n int C,\n int S4,\n int S,\n float invC,\n float eps) {\n int64_t tid = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n\n for (int64_t idx = tid; idx < vec_cols; idx += stride) {\n int n = (int)(idx / S4);\n int sv = (int)(idx - (int64_t)n * S4);\n int s = sv << 2;\n int64_t base = ((int64_t)n * (int64_t)C * (int64_t)S) + (int64_t)s;\n\n float sum0 = 0.0f, sum1 = 0.0f, sum2 = 0.0f, sum3 = 0.0f;\n for (int c = 0; c < C; ++c) {\n const float4 v = *reinterpret_cast<const float4*>(x + base + (int64_t)c * S);\n sum0 = fmaf(v.x, v.x, sum0);\n sum1 = fmaf(v.y, v.y, sum1);\n sum2 = fmaf(v.z, v.z, sum2);\n sum3 = fmaf(v.w, v.w, sum3);\n }\n\n const float inv0 = rsqrtf(sum0 * invC + eps);\n const float inv1 = rsqrtf(sum1 * invC + eps);\n const float inv2 = rsqrtf(sum2 * invC + eps);\n const float inv3 = rsqrtf(sum3 * invC + eps);\n\n for (int c = 0; c < C; ++c) {\n const int64_t off = base + (int64_t)c * S;\n const float4 v = *reinterpret_cast<const float4*>(x + off);\n float4 o;\n o.x = v.x * inv0;\n o.y = v.y * inv1;\n o.z = v.z * inv2;\n o.w = v.w * inv3;\n *reinterpret_cast<float4*>(y + off) = o;\n }\n }\n}\n\n__global__ void rmsnorm_generic_scalar_kernel(const float* __restrict__ x,\n float* __restrict__ y,\n int64_t cols,\n int C,\n int S,\n float invC,\n float eps) {\n int64_t tid = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n\n for (int64_t idx = tid; idx < cols; idx += stride) {\n int n = (int)(idx / S);\n int s = (int)(idx - (int64_t)n * S);\n int64_t base = ((int64_t)n * (int64_t)C * (int64_t)S) + (int64_t)s;\n float sum = 0.0f;\n for (int c = 0; c < C; ++c) {\n float v = x[base + (int64_t)c * S];\n sum = fmaf(v, v, sum);\n }\n float inv = rsqrtf(sum * invC + eps);\n for (int c = 0; c < C; ++c) {\n int64_t off = base + (int64_t)c * S;\n y[off] = x[off] * inv;\n }\n }\n}\n\ntorch::Tensor rmsnorm_forward(torch::Tensor x, double eps) {\n TORCH_CHECK(x.is_cuda(), \"x must be a CUDA tensor\");\n TORCH_CHECK(x.scalar_type() == at::kFloat, \"only float32 is supported\");\n TORCH_CHECK(x.is_contiguous(), \"x must be contiguous\");\n TORCH_CHECK(x.dim() >= 2, \"x must have at least 2 dimensions\");\n\n auto y = torch::empty_like(x);\n const int64_t B64 = x.size(0);\n const int64_t C64 = x.size(1);\n const int64_t numel = x.numel();\n if (numel == 0) return y;\n const int64_t S64 = numel / (B64 * C64);\n\n TORCH_CHECK(C64 <= 2147483647LL && S64 <= 2147483647LL, \"dimensions too large\");\n const int C = (int)C64;\n const int S = (int)S64;\n\n const float* xp = x.data_ptr<float>();\n float* yp = y.data_ptr<float>();\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n const int threads = 256;\n\n if ((S & 3) == 0) {\n const int S4 = S >> 2;\n const int64_t vec_cols = B64 * (int64_t)S4;\n int blocks = (int)((vec_cols + threads - 1) / threads);\n if (blocks > 65535) blocks = 65535;\n if (C == 64) {\n rmsnorm64_vec4_kernel<<<blocks, threads, 0, stream>>>(xp, yp, vec_cols, S4, S, (float)eps);\n } else {\n rmsnorm_generic_vec4_kernel<<<blocks, threads, 0, stream>>>(xp, yp, vec_cols, C, S4, S, 1.0f / (float)C, (float)eps);\n }\n } else {\n const int64_t cols = B64 * S64;\n int blocks = (int)((cols + threads - 1) / threads);\n if (blocks > 65535) blocks = 65535;\n rmsnorm_generic_scalar_kernel<<<blocks, threads, 0, stream>>>(xp, yp, cols, C, S, 1.0f / (float)C, (float)eps);\n }\n return y;\n}\n'''\n\n_ext = load_inline(\n name='rmsnorm_forward_ext_v1',\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=['rmsnorm_forward'],\n extra_cflags=['-O3'],\n extra_cuda_cflags=['-O3', '--use_fast_math'],\n verbose=False,\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, num_features: int, eps: float = 1e-5):\n super(ModelNew, self).__init__()\n self.num_features = num_features\n self.eps = eps\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n return _ext.rmsnorm_forward(x, self.eps)\n" }
result ← submit_kernel (ok)
oksubmit_kernel submit_kernel PASSED: (5 / 5) correctness trials all passed. Kernel runtime: 5.79 μs Runtime stats: mean=5.79μs median=?μs std=0.24μs Numerical precision: max_abs_err=8.34e-07 mean_abs_err=5.32e-08 max_rel_err=5.40e-07 Memory: 14369.0 MB peak (ref: 14481.0 MB, ratio: 0.9923x) Kernel launches: 2 (ref: 7, fusion ratio: 3.5) Energy: 1932.48 mJ/run (ref: 1691.74 mJ, ratio: 0.8754x) SOL score: 0.8504
final submitted kernel (196 lines, 7,161 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline


_cpp_src = r'''
#include <torch/extension.h>

torch::Tensor rmsnorm_forward(torch::Tensor x, double eps);
'''

_cuda_src = r'''
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cstdint>

__global__ void rmsnorm64_vec4_kernel(const float* __restrict__ x,
                                      float* __restrict__ y,
                                      int64_t vec_cols,
                                      int S4,
                                      int S,
                                      float eps) {
    int64_t tid = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
    int64_t stride = (int64_t)blockDim.x * gridDim.x;

    for (int64_t idx = tid; idx < vec_cols; idx += stride) {
        int n = (int)(idx / S4);
        int sv = (int)(idx - (int64_t)n * S4);
        int s = sv << 2;
        int64_t base = ((int64_t)n * 64LL * (int64_t)S) + (int64_t)s;

        float sum0 = 0.0f, sum1 = 0.0f, sum2 = 0.0f, sum3 = 0.0f;

        #pragma unroll
        for (int c = 0; c < 64; ++c) {
            const float4 v = *reinterpret_cast<const float4*>(x + base + (int64_t)c * S);
            sum0 = fmaf(v.x, v.x, sum0);
            sum1 = fmaf(v.y, v.y, sum1);
            sum2 = fmaf(v.z, v.z, sum2);
            sum3 = fmaf(v.w, v.w, sum3);
        }

        const float inv0 = rsqrtf(sum0 * 0.015625f + eps);
        const float inv1 = rsqrtf(sum1 * 0.015625f + eps);
        const float inv2 = rsqrtf(sum2 * 0.015625f + eps);
        const float inv3 = rsqrtf(sum3 * 0.015625f + eps);

        #pragma unroll
        for (int c = 0; c < 64; ++c) {
            const int64_t off = base + (int64_t)c * S;
            const float4 v = *reinterpret_cast<const float4*>(x + off);
            float4 o;
            o.x = v.x * inv0;
            o.y = v.y * inv1;
            o.z = v.z * inv2;
            o.w = v.w * inv3;
            *reinterpret_cast<float4*>(y + off) = o;
        }
    }
}

__global__ void rmsnorm_generic_vec4_kernel(const float* __restrict__ x,
                                            float* __restrict__ y,
                                            int64_t vec_cols,
                                            int C,
                                            int S4,
                                            int S,
                                            float invC,
                                            float eps) {
    int64_t tid = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
    int64_t stride = (int64_t)blockDim.x * gridDim.x;

    for (int64_t idx = tid; idx < vec_cols; idx += stride) {
        int n = (int)(idx / S4);
        int sv = (int)(idx - (int64_t)n * S4);
        int s = sv << 2;
        int64_t base = ((int64_t)n * (int64_t)C * (int64_t)S) + (int64_t)s;

        float sum0 = 0.0f, sum1 = 0.0f, sum2 = 0.0f, sum3 = 0.0f;
        for (int c = 0; c < C; ++c) {
            const float4 v = *reinterpret_cast<const float4*>(x + base + (int64_t)c * S);
            sum0 = fmaf(v.x, v.x, sum0);
            sum1 = fmaf(v.y, v.y, sum1);
            sum2 = fmaf(v.z, v.z, sum2);
            sum3 = fmaf(v.w, v.w, sum3);
        }

        const float inv0 = rsqrtf(sum0 * invC + eps);
        const float inv1 = rsqrtf(sum1 * invC + eps);
        const float inv2 = rsqrtf(sum2 * invC + eps);
        const float inv3 = rsqrtf(sum3 * invC + eps);

        for (int c = 0; c < C; ++c) {
            const int64_t off = base + (int64_t)c * S;
            const float4 v = *reinterpret_cast<const float4*>(x + off);
            float4 o;
            o.x = v.x * inv0;
            o.y = v.y * inv1;
            o.z = v.z * inv2;
            o.w = v.w * inv3;
            *reinterpret_cast<float4*>(y + off) = o;
        }
    }
}

__global__ void rmsnorm_generic_scalar_kernel(const float* __restrict__ x,
                                              float* __restrict__ y,
                                              int64_t cols,
                                              int C,
                                              int S,
                                              float invC,
                                              float eps) {
    int64_t tid = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
    int64_t stride = (int64_t)blockDim.x * gridDim.x;

    for (int64_t idx = tid; idx < cols; idx += stride) {
        int n = (int)(idx / S);
        int s = (int)(idx - (int64_t)n * S);
        int64_t base = ((int64_t)n * (int64_t)C * (int64_t)S) + (int64_t)s;
        float sum = 0.0f;
        for (int c = 0; c < C; ++c) {
            float v = x[base + (int64_t)c * S];
            sum = fmaf(v, v, sum);
        }
        float inv = rsqrtf(sum * invC + eps);
        for (int c = 0; c < C; ++c) {
            int64_t off = base + (int64_t)c * S;
            y[off] = x[off] * inv;
        }
    }
}

torch::Tensor rmsnorm_forward(torch::Tensor x, double eps) {
    TORCH_CHECK(x.is_cuda(), "x must be a CUDA tensor");
    TORCH_CHECK(x.scalar_type() == at::kFloat, "only float32 is supported");
    TORCH_CHECK(x.is_contiguous(), "x must be contiguous");
    TORCH_CHECK(x.dim() >= 2, "x must have at least 2 dimensions");

    auto y = torch::empty_like(x);
    const int64_t B64 = x.size(0);
    const int64_t C64 = x.size(1);
    const int64_t numel = x.numel();
    if (numel == 0) return y;
    const int64_t S64 = numel / (B64 * C64);

    TORCH_CHECK(C64 <= 2147483647LL && S64 <= 2147483647LL, "dimensions too large");
    const int C = (int)C64;
    const int S = (int)S64;

    const float* xp = x.data_ptr<float>();
    float* yp = y.data_ptr<float>();
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    const int threads = 256;

    if ((S & 3) == 0) {
        const int S4 = S >> 2;
        const int64_t vec_cols = B64 * (int64_t)S4;
        int blocks = (int)((vec_cols + threads - 1) / threads);
        if (blocks > 65535) blocks = 65535;
        if (C == 64) {
            rmsnorm64_vec4_kernel<<<blocks, threads, 0, stream>>>(xp, yp, vec_cols, S4, S, (float)eps);
        } else {
            rmsnorm_generic_vec4_kernel<<<blocks, threads, 0, stream>>>(xp, yp, vec_cols, C, S4, S, 1.0f / (float)C, (float)eps);
        }
    } else {
        const int64_t cols = B64 * S64;
        int blocks = (int)((cols + threads - 1) / threads);
        if (blocks > 65535) blocks = 65535;
        rmsnorm_generic_scalar_kernel<<<blocks, threads, 0, stream>>>(xp, yp, cols, C, S, 1.0f / (float)C, (float)eps);
    }
    return y;
}
'''

_ext = load_inline(
    name='rmsnorm_forward_ext_v1',
    cpp_sources=_cpp_src,
    cuda_sources=_cuda_src,
    functions=['rmsnorm_forward'],
    extra_cflags=['-O3'],
    extra_cuda_cflags=['-O3', '--use_fast_math'],
    verbose=False,
)


class ModelNew(nn.Module):
    def __init__(self, num_features: int, eps: float = 1e-5):
        super(ModelNew, self).__init__()
        self.num_features = num_features
        self.eps = eps

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return _ext.rmsnorm_forward(x, self.eps)