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"
}