turn 1130.0s turn · 30.0s LLM · 0.0s tools
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n_cpp_src = r\"\"\"\n#include <torch/extension.h>\ntorch::Tensor mse_forward(torch::Tensor predictions, torch::Tensor targets);\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 <stdint.h>\n\nstatic __device__ __forceinline__ float warp_reduce_sum(float v) {\n v += __shfl_down_sync(0xffffffff, v, 16);\n v += __shfl_down_sync(0xffffffff, v, 8);\n v += __shfl_down_sync(0xffffffff, v, 4);\n v += __shfl_down_sync(0xffffffff, v, 2);\n v += __shfl_down_sync(0xffffffff, v, 1);\n return v;\n}\n\nstatic __device__ __forceinline__ float block_reduce_sum(float v) {\n __shared__ float shared[32];\n int lane = threadIdx.x & 31;\n int wid = threadIdx.x >> 5;\n v = warp_reduce_sum(v);\n if (lane == 0) shared[wid] = v;\n __syncthreads();\n v = (threadIdx.x < ((blockDim.x + 31) >> 5)) ? shared[lane] : 0.0f;\n if (wid == 0) v = warp_reduce_sum(v);\n return v;\n}\n\n__global__ void mse_atomic_vec4_kernel(const float* __restrict__ a,\n const float* __restrict__ b,\n float* __restrict__ out,\n int64_t n4,\n float inv_n) {\n int tid = threadIdx.x;\n int64_t idx = (int64_t)blockIdx.x * blockDim.x + tid;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n const float4* __restrict__ a4 = reinterpret_cast<const float4*>(a);\n const float4* __restrict__ b4 = reinterpret_cast<const float4*>(b);\n float sum = 0.0f;\n for (int64_t i = idx; i < n4; i += stride) {\n float4 av = a4[i];\n float4 bv = b4[i];\n float d0 = av.x - bv.x;\n float d1 = av.y - bv.y;\n float d2 = av.z - bv.z;\n float d3 = av.w - bv.w;\n sum = fmaf(d0, d0, sum);\n sum = fmaf(d1, d1, sum);\n sum = fmaf(d2, d2, sum);\n sum = fmaf(d3, d3, sum);\n }\n sum = block_reduce_sum(sum);\n if (tid == 0) atomicAdd(out, sum * inv_n);\n}\n\n__global__ void mse_atomic_scalar_kernel(const float* __restrict__ a,\n const float* __restrict__ b,\n float* __restrict__ out,\n int64_t n,\n float inv_n) {\n int tid = threadIdx.x;\n int64_t idx = (int64_t)blockIdx.x * blockDim.x + tid;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n float sum = 0.0f;\n for (int64_t i = idx; i < n; i += stride) {\n float d = a[i] - b[i];\n sum = fmaf(d, d, sum);\n }\n sum = block_reduce_sum(sum);\n if (tid == 0) atomicAdd(out, sum * inv_n);\n}\n\ntorch::Tensor mse_forward(torch::Tensor predictions, torch::Tensor targets) {\n const int64_t n = predictions.numel();\n auto out = torch::empty({}, predictions.options());\n if (n == 0) return out;\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n cudaMemsetAsync(out.data_ptr<float>(), 0, sizeof(float), stream);\n\n const int threads = 1024;\n bool vec4 = ((n & 3LL) == 0) &&\n ((((uintptr_t)predictions.data_ptr<float>()) & 15) == 0) &&\n ((((uintptr_t)targets.data_ptr<float>()) & 15) == 0);\n int64_t work = vec4 ? (n >> 2) : n;\n int blocks = (int)((work + threads - 1) / threads);\n if (blocks < 1) blocks = 1;\n if (blocks > 65535) blocks = 65535;\n float inv_n = 1.0f / (float)n;\n\n if (vec4) {\n mse_atomic_vec4_kernel<<<blocks, threads, 0, stream>>>(\n predictions.data_ptr<float>(), targets.data_ptr<float>(), out.data_ptr<float>(), work, inv_n);\n } else {\n mse_atomic_scalar_kernel<<<blocks, threads, 0, stream>>>(\n predictions.data_ptr<float>(), targets.data_ptr<float>(), out.data_ptr<float>(), n, inv_n);\n }\n return out;\n}\n\"\"\"\n\n_ext = load_inline(\n name=\"mse_reduce_ext_final\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"mse_forward\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"],\n verbose=False,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super(ModelNew, self).__init__()\n def forward(self, predictions, targets):\n return _ext.mse_forward(predictions, targets)\n"
}
call → static_check
callstatic_check
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n_cpp_src = r\"\"\"\n#include <torch/extension.h>\ntorch::Tensor mse_forward(torch::Tensor predictions, torch::Tensor targets);\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 <stdint.h>\n\nstatic __device__ __forceinline__ float warp_reduce_sum(float v) {\n v += __shfl_down_sync(0xffffffff, v, 16);\n v += __shfl_down_sync(0xffffffff, v, 8);\n v += __shfl_down_sync(0xffffffff, v, 4);\n v += __shfl_down_sync(0xffffffff, v, 2);\n v += __shfl_down_sync(0xffffffff, v, 1);\n return v;\n}\n\nstatic __device__ __forceinline__ float block_reduce_sum(float v) {\n __shared__ float shared[32];\n int lane = threadIdx.x & 31;\n int wid = threadIdx.x >> 5;\n v = warp_reduce_sum(v);\n if (lane == 0) shared[wid] = v;\n __syncthreads();\n v = (threadIdx.x < ((blockDim.x + 31) >> 5)) ? shared[lane] : 0.0f;\n if (wid == 0) v = warp_reduce_sum(v);\n return v;\n}\n\n__global__ void mse_atomic_vec4_kernel(const float* __restrict__ a,\n const float* __restrict__ b,\n float* __restrict__ out,\n int64_t n4,\n float inv_n) {\n int tid = threadIdx.x;\n int64_t idx = (int64_t)blockIdx.x * blockDim.x + tid;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n const float4* __restrict__ a4 = reinterpret_cast<const float4*>(a);\n const float4* __restrict__ b4 = reinterpret_cast<const float4*>(b);\n float sum = 0.0f;\n for (int64_t i = idx; i < n4; i += stride) {\n float4 av = a4[i];\n float4 bv = b4[i];\n float d0 = av.x - bv.x;\n float d1 = av.y - bv.y;\n float d2 = av.z - bv.z;\n float d3 = av.w - bv.w;\n sum = fmaf(d0, d0, sum);\n sum = fmaf(d1, d1, sum);\n sum = fmaf(d2, d2, sum);\n sum = fmaf(d3, d3, sum);\n }\n sum = block_reduce_sum(sum);\n if (tid == 0) atomicAdd(out, sum * inv_n);\n}\n\n__global__ void mse_atomic_scalar_kernel(const float* __restrict__ a,\n const float* __restrict__ b,\n float* __restrict__ out,\n int64_t n,\n float inv_n) {\n int tid = threadIdx.x;\n int64_t idx = (int64_t)blockIdx.x * blockDim.x + tid;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n float sum = 0.0f;\n for (int64_t i = idx; i < n; i += stride) {\n float d = a[i] - b[i];\n sum = fmaf(d, d, sum);\n }\n sum = block_reduce_sum(sum);\n if (tid == 0) atomicAdd(out, sum * inv_n);\n}\n\ntorch::Tensor mse_forward(torch::Tensor predictions, torch::Tensor targets) {\n const int64_t n = predictions.numel();\n auto out = torch::empty({}, predictions.options());\n if (n == 0) return out;\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n cudaMemsetAsync(out.data_ptr<float>(), 0, sizeof(float), stream);\n\n const int threads = 1024;\n bool vec4 = ((n & 3LL) == 0) &&\n ((((uintptr_t)predictions.data_ptr<float>()) & 15) == 0) &&\n ((((uintptr_t)targets.data_ptr<float>()) & 15) == 0);\n int64_t work = vec4 ? (n >> 2) : n;\n int blocks = (int)((work + threads - 1) / threads);\n if (blocks < 1) blocks = 1;\n if (blocks > 65535) blocks = 65535;\n float inv_n = 1.0f / (float)n;\n\n if (vec4) {\n mse_atomic_vec4_kernel<<<blocks, threads, 0, stream>>>(\n predictions.data_ptr<float>(), targets.data_ptr<float>(), out.data_ptr<float>(), work, inv_n);\n } else {\n mse_atomic_scalar_kernel<<<blocks, threads, 0, stream>>>(\n predictions.data_ptr<float>(), targets.data_ptr<float>(), out.data_ptr<float>(), n, inv_n);\n }\n return out;\n}\n\"\"\"\n\n_ext = load_inline(\n name=\"mse_reduce_ext_final\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"mse_forward\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"],\n verbose=False,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super(ModelNew, self).__init__()\n def forward(self, predictions, targets):\n return _ext.mse_forward(predictions, targets)\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=9.54e-07 mean_abs_err=6.23e-07 max_rel_err=5.40e-06
result ← static_check (ok)
okstatic_check
static_check PASSED: no violations or warnings detected.