gpt-5.5-priority · original · l12_default_gpt
correct 54_conv_standard_3D__square_input__square_kernel.py
model
gpt-5.5-priority
turns / max
7 / 10
tool calls
7
tokens (total)
0
wall clock
—
truncated
no
backend
cuda fp32
variant
original
runtime
6.69 μs
ref runtime
1.85 μs
speedup
0.28x
started
2026-05-02T02:14:18Z
timingkernelbench.timing
kernel runtime
6.69 μs
ref runtime
1.85 μs
speedup
0.28x
kernel mean / median / std
6.69 / — / 0.15 μs
ref mean / median / std
1.85 / — / 0.04 μs
numerical precisioneval_kernel_against_ref
max abs error
0.00
mean abs error
0.00
max rel error
0.00
mean rel error
0.00
gpu memoryextended_metrics.measure_memory
kernel peak
1012.01 MB
ref peak
1012.01 MB
kernel / ref
1.00x
kernel launches / fusiontorch.profiler
kernel launches
2
ref launches
4
fusion ratio (ref / kernel)
2.00x
total cuda time
55 μs
energyNVML (pynvml)
kernel mJ / run
2358.72
ref mJ / run
0.00
ratio (ref / kernel)
—
avg power
352.67 W
ref avg power
0.00 W
SOL (speed-of-light)nsight_hardware_counters
SOL score (0–1)
0.182
dram util %
4.5%
compute util %
18.2%
bottleneck
compute-bound
dominant pipe
FP32
arithmetic intensity
—
ridge point
20.00
roofline / occupancynsight_hardware_counters
dram bandwidth
151.51 GB/s
dram util %
4.5%
fp32 tflops
—
fp32 util %
—
fp16 tflops
—
occupancy %
96.2%
bottleneck
compute-bound
dominant pipe %
18.2%
arithmetic intensity
—
ridge point
20.00
L1 hit %
—
L2 hit %
99.2%
ld sectors / req
10.40
st sectors / req
—
regs / thread
32
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
862.8%
mio_throttle
93.0%
short_scoreboard
76.1%
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn

class Model(nn.Module):
    """
    Performs a standard 3D convolution operation with square input and square kernel.

    Args:
        in_channels (int): Number of channels in the input tensor.
        out_channels (int): Number of channels produced by the convolution.
        kernel_size (int): Size of the square convolution kernel.
        stride (int, optional): Stride of the convolution. Defaults to 1.
        padding (int, optional): Padding applied to the input. Defaults to 0.
        dilation (int, optional): Spacing between kernel elements. Defaults to 1.
        groups (int, optional): Number of blocked connections from input channels to output channels. Defaults to 1.
        bias (bool, optional): If `True`, adds a learnable bias to the output. Defaults to `False`.
    """
    def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = False):
        super(Model, self).__init__()
        self.conv3d = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
        
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Performs the 3D convolution.

        Args:
            x (torch.Tensor): Input tensor of shape (batch_size, in_channels, depth, width, height).

        Returns:
            torch.Tensor: Output tensor of shape (batch_size, out_channels, depth_out, width_out, height_out).
        """
        return self.conv3d(x)

# Test code
batch_size = 16
in_channels = 3
out_channels = 64
kernel_size = 3
depth = 64
width = 64
height = 64

def get_inputs():
    x = torch.rand(batch_size, in_channels, depth, width, height)
    return [x]

def get_init_inputs():
    return [in_channels, out_channels, kernel_size]  # Provide in_channels, out_channels, kernel_size for initialization
turn 08.3s turn · 8.3s LLM · 0.0s tools
reasoning
**Investigating GPU Specs** I need to look up the specifications for the GPU. I want to gather all necessary details, like memory size, core count, clock speed, and power consumption. Understanding these specs is vital for assessing performance and compatibility with certain tasks or applications. I'll make sure to double-check the source of information I find to ensure accuracy. Once I've collected the right data, I can present it to the user clearly and effectively!
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 1 · 1 tool call(s) · 55.0s turn · 55.0s LLM · 10,632 chars (click to expand)
turn 155.0s turn · 55.0s LLM · 0.0s tools
reasoning
**Evaluating bias handling** I’m considering how to deal with bias in the code. If bias is false, should I just pass `self.conv3d.bias`? Maybe I should define a simpler wrapper with `torch::Tensor` for bias and a boolean for `has_bias`. If there’s no bias, I’d want to use `torch.empty(0, device=x.device)`. I also need to keep the same signature and focus on correctly outputting shape. Implementing a specialized kernel might also require optimization techniques like unrolling.
call → compile_kernel
callcompile_kernel { "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>\n#include <vector>\n\ntorch::Tensor conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride, int64_t padding, int64_t dilation, int64_t groups);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n__global__ void conv3d_c3k3s1p0_vec4_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH;\n int od = t % OD; t /= OD;\n int oc = t % OC; t /= OC;\n int n = (int)t;\n int ow = gw << 2;\n\n float a0 = has_bias ? b[oc] : 0.0f;\n float a1 = a0, a2 = a0, a3 = a0;\n\n const int HW = H * W;\n const int DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n const float* wp = w + (long long)oc * 81;\n\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0);\n float x1 = __ldg(xp + 1);\n float x2 = __ldg(xp + 2);\n float x3 = __ldg(xp + 3);\n float x4 = __ldg(xp + 4);\n float x5 = __ldg(xp + 5);\n float w0 = __ldg(wp + c*27 + kz*9 + ky*3 + 0);\n float w1 = __ldg(wp + c*27 + kz*9 + ky*3 + 1);\n float w2 = __ldg(wp + c*27 + kz*9 + ky*3 + 2);\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1);\n a2 = fmaf(w0, x2, a2); a2 = fmaf(w1, x3, a2); a2 = fmaf(w2, x4, a2);\n a3 = fmaf(w0, x3, a3); a3 = fmaf(w1, x4, a3); a3 = fmaf(w2, x5, a3);\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; op[1] = a1; op[2] = a2; op[3] = a3;\n } else {\n // Tail at the end of each row (for OW not divisible by 4).\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0);\n float x1 = __ldg(xp + 1);\n float x2 = __ldg(xp + 2);\n float x3 = (ow + 1 < OW) ? __ldg(xp + 3) : 0.0f;\n float w0 = __ldg(wp + c*27 + kz*9 + ky*3 + 0);\n float w1 = __ldg(wp + c*27 + kz*9 + ky*3 + 1);\n float w2 = __ldg(wp + c*27 + kz*9 + ky*3 + 2);\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n if (ow + 1 < OW) { a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1); }\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0;\n if (ow + 1 < OW) op[1] = a1;\n }\n}\n\n__global__ void conv3d_generic_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int C, int D, int H, int W,\n int OC, int KD, int KH, int KW,\n int OD, int OH, int OW,\n int stride, int padding, int dilation,\n int has_bias) {\n long long total = (long long)N * OC * OD * OH * OW;\n long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (idx >= total) return;\n int ow = idx % OW; long long t = idx / OW;\n int oh = t % OH; t /= OH;\n int od = t % OD; t /= OD;\n int oc = t % OC; t /= OC;\n int n = (int)t;\n float sum = has_bias ? b[oc] : 0.0f;\n for (int c = 0; c < C; ++c) {\n for (int kz = 0; kz < KD; ++kz) {\n int iz = od * stride - padding + kz * dilation;\n if ((unsigned)iz >= (unsigned)D) continue;\n for (int ky = 0; ky < KH; ++ky) {\n int iy = oh * stride - padding + ky * dilation;\n if ((unsigned)iy >= (unsigned)H) continue;\n for (int kx = 0; kx < KW; ++kx) {\n int ix = ow * stride - padding + kx * dilation;\n if ((unsigned)ix >= (unsigned)W) continue;\n float xv = x[(((long long)n*C + c)*D + iz)*H*W + iy*W + ix];\n float wv = w[(((long long)oc*C + c)*KD + kz)*KH*KW + ky*KW + kx];\n sum = fmaf(wv, xv, sum);\n }\n }\n }\n }\n out[idx] = sum;\n}\n\ntorch::Tensor conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride64, int64_t padding64, int64_t dilation64, int64_t groups64) {\n TORCH_CHECK(x.is_cuda(), \"x must be CUDA\");\n TORCH_CHECK(weight.is_cuda(), \"weight must be CUDA\");\n x = x.contiguous();\n weight = weight.contiguous();\n int N = (int)x.size(0), C = (int)x.size(1), D = (int)x.size(2), H = (int)x.size(3), W = (int)x.size(4);\n int OC = (int)weight.size(0), KD = (int)weight.size(2), KH = (int)weight.size(3), KW = (int)weight.size(4);\n int stride = (int)stride64, padding = (int)padding64, dilation = (int)dilation64;\n int OD = (D + 2*padding - dilation*(KD-1) - 1) / stride + 1;\n int OH = (H + 2*padding - dilation*(KH-1) - 1) / stride + 1;\n int OW = (W + 2*padding - dilation*(KW-1) - 1) / stride + 1;\n auto out = torch::empty({N, OC, OD, OH, OW}, x.options());\n int has_bias = (bias.defined() && bias.numel() > 0) ? 1 : 0;\n const float* bp = has_bias ? bias.contiguous().data_ptr<float>() : nullptr;\n const int threads = 256;\n if (C == 3 && KD == 3 && KH == 3 && KW == 3 && stride == 1 && padding == 0 && dilation == 1 && groups64 == 1 && x.scalar_type() == torch::kFloat32) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n int blocks = (int)((total + threads - 1) / threads);\n conv3d_c3k3s1p0_vec4_kernel<<<blocks, threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp, out.data_ptr<float>(), N, D, H, W, OC, OD, OH, OW, has_bias);\n } else {\n long long total = (long long)N * OC * OD * OH * OW;\n int blocks = (int)((total + threads - 1) / threads);\n conv3d_generic_kernel<<<blocks, threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp, out.data_ptr<float>(), N, C, D, H, W, OC, KD, KH, KW, OD, OH, OW, stride, padding, dilation, has_bias);\n }\n return out;\n}\n\"\"\"\n\n_ext = load_inline(name=\"conv3d_c3k3_ext_v1\", cpp_sources=_cpp_src, cuda_sources=_cuda_src,\n functions=[\"conv3d_forward\"], extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = False):\n super(ModelNew, self).__init__()\n self.conv3d = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)\n self.stride = stride\n self.padding = padding\n self.dilation = dilation\n self.groups = groups\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n b = self.conv3d.bias if self.conv3d.bias is not None else torch.empty(0, device=x.device, dtype=x.dtype)\n return _ext.conv3d_forward(x, self.conv3d.weight, b, int(self.stride), int(self.padding), int(self.dilation), int(self.groups))\n" }
result ← compile_kernel (ok)
okcompile_kernel compile_kernel PASSED: kernel compiled without errors.
turn 2 · 1 tool call(s) · 16.9s turn · 16.9s LLM · 10,165 chars (click to expand)
turn 216.9s turn · 16.9s 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>\n#include <vector>\n\ntorch::Tensor conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride, int64_t padding, int64_t dilation, int64_t groups);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n__global__ void conv3d_c3k3s1p0_vec4_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH;\n int od = t % OD; t /= OD;\n int oc = t % OC; t /= OC;\n int n = (int)t;\n int ow = gw << 2;\n\n float a0 = has_bias ? b[oc] : 0.0f;\n float a1 = a0, a2 = a0, a3 = a0;\n\n const int HW = H * W;\n const int DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n const float* wp = w + (long long)oc * 81;\n\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0);\n float x1 = __ldg(xp + 1);\n float x2 = __ldg(xp + 2);\n float x3 = __ldg(xp + 3);\n float x4 = __ldg(xp + 4);\n float x5 = __ldg(xp + 5);\n float w0 = __ldg(wp + c*27 + kz*9 + ky*3 + 0);\n float w1 = __ldg(wp + c*27 + kz*9 + ky*3 + 1);\n float w2 = __ldg(wp + c*27 + kz*9 + ky*3 + 2);\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1);\n a2 = fmaf(w0, x2, a2); a2 = fmaf(w1, x3, a2); a2 = fmaf(w2, x4, a2);\n a3 = fmaf(w0, x3, a3); a3 = fmaf(w1, x4, a3); a3 = fmaf(w2, x5, a3);\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; op[1] = a1; op[2] = a2; op[3] = a3;\n } else {\n // Tail at the end of each row (for OW not divisible by 4).\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0);\n float x1 = __ldg(xp + 1);\n float x2 = __ldg(xp + 2);\n float x3 = (ow + 1 < OW) ? __ldg(xp + 3) : 0.0f;\n float w0 = __ldg(wp + c*27 + kz*9 + ky*3 + 0);\n float w1 = __ldg(wp + c*27 + kz*9 + ky*3 + 1);\n float w2 = __ldg(wp + c*27 + kz*9 + ky*3 + 2);\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n if (ow + 1 < OW) { a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1); }\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0;\n if (ow + 1 < OW) op[1] = a1;\n }\n}\n\n__global__ void conv3d_generic_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int C, int D, int H, int W,\n int OC, int KD, int KH, int KW,\n int OD, int OH, int OW,\n int stride, int padding, int dilation,\n int has_bias) {\n long long total = (long long)N * OC * OD * OH * OW;\n long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (idx >= total) return;\n int ow = idx % OW; long long t = idx / OW;\n int oh = t % OH; t /= OH;\n int od = t % OD; t /= OD;\n int oc = t % OC; t /= OC;\n int n = (int)t;\n float sum = has_bias ? b[oc] : 0.0f;\n for (int c = 0; c < C; ++c) {\n for (int kz = 0; kz < KD; ++kz) {\n int iz = od * stride - padding + kz * dilation;\n if ((unsigned)iz >= (unsigned)D) continue;\n for (int ky = 0; ky < KH; ++ky) {\n int iy = oh * stride - padding + ky * dilation;\n if ((unsigned)iy >= (unsigned)H) continue;\n for (int kx = 0; kx < KW; ++kx) {\n int ix = ow * stride - padding + kx * dilation;\n if ((unsigned)ix >= (unsigned)W) continue;\n float xv = x[(((long long)n*C + c)*D + iz)*H*W + iy*W + ix];\n float wv = w[(((long long)oc*C + c)*KD + kz)*KH*KW + ky*KW + kx];\n sum = fmaf(wv, xv, sum);\n }\n }\n }\n }\n out[idx] = sum;\n}\n\ntorch::Tensor conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride64, int64_t padding64, int64_t dilation64, int64_t groups64) {\n TORCH_CHECK(x.is_cuda(), \"x must be CUDA\");\n TORCH_CHECK(weight.is_cuda(), \"weight must be CUDA\");\n x = x.contiguous();\n weight = weight.contiguous();\n int N = (int)x.size(0), C = (int)x.size(1), D = (int)x.size(2), H = (int)x.size(3), W = (int)x.size(4);\n int OC = (int)weight.size(0), KD = (int)weight.size(2), KH = (int)weight.size(3), KW = (int)weight.size(4);\n int stride = (int)stride64, padding = (int)padding64, dilation = (int)dilation64;\n int OD = (D + 2*padding - dilation*(KD-1) - 1) / stride + 1;\n int OH = (H + 2*padding - dilation*(KH-1) - 1) / stride + 1;\n int OW = (W + 2*padding - dilation*(KW-1) - 1) / stride + 1;\n auto out = torch::empty({N, OC, OD, OH, OW}, x.options());\n int has_bias = (bias.defined() && bias.numel() > 0) ? 1 : 0;\n const float* bp = has_bias ? bias.contiguous().data_ptr<float>() : nullptr;\n const int threads = 256;\n if (C == 3 && KD == 3 && KH == 3 && KW == 3 && stride == 1 && padding == 0 && dilation == 1 && groups64 == 1 && x.scalar_type() == torch::kFloat32) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n int blocks = (int)((total + threads - 1) / threads);\n conv3d_c3k3s1p0_vec4_kernel<<<blocks, threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp, out.data_ptr<float>(), N, D, H, W, OC, OD, OH, OW, has_bias);\n } else {\n long long total = (long long)N * OC * OD * OH * OW;\n int blocks = (int)((total + threads - 1) / threads);\n conv3d_generic_kernel<<<blocks, threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp, out.data_ptr<float>(), N, C, D, H, W, OC, KD, KH, KW, OD, OH, OW, stride, padding, dilation, has_bias);\n }\n return out;\n}\n\"\"\"\n\n_ext = load_inline(name=\"conv3d_c3k3_ext_v1\", cpp_sources=_cpp_src, cuda_sources=_cuda_src,\n functions=[\"conv3d_forward\"], extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = False):\n super(ModelNew, self).__init__()\n self.conv3d = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)\n self.stride = stride\n self.padding = padding\n self.dilation = dilation\n self.groups = groups\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n b = self.conv3d.bias if self.conv3d.bias is not None else torch.empty(0, device=x.device, dtype=x.dtype)\n return _ext.conv3d_forward(x, self.conv3d.weight, b, int(self.stride), int(self.padding), int(self.dilation), int(self.groups))\n" }
result ← run_correctness (ok)
okrun_correctness run_correctness PASSED: (5 / 5) trials all matched the reference. Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00
turn 3 · 1 tool call(s) · 30.8s turn · 30.8s LLM · 11,735 chars (click to expand)
turn 330.8s turn · 30.8s LLM · 0.0s tools
call → compile_kernel
callcompile_kernel { "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 conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride, int64_t padding, int64_t dilation, int64_t groups);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n__constant__ float c_weight[8192];\n\n__global__ void conv3d_c3k3s1p0_vec4_const_kernel(const float* __restrict__ x,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH;\n int od = t % OD; t /= OD;\n int oc = t % OC; t /= OC;\n int n = (int)t;\n int ow = gw << 2;\n float a0 = has_bias ? b[oc] : 0.0f;\n float a1 = a0, a2 = a0, a3 = a0;\n const int HW = H * W;\n const int DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n int wbase = oc * 81;\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);\n float x3 = __ldg(xp + 3), x4 = __ldg(xp + 4), x5 = __ldg(xp + 5);\n int wo = wbase + c*27 + kz*9 + ky*3;\n float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1);\n a2 = fmaf(w0, x2, a2); a2 = fmaf(w1, x3, a2); a2 = fmaf(w2, x4, a2);\n a3 = fmaf(w0, x3, a3); a3 = fmaf(w1, x4, a3); a3 = fmaf(w2, x5, a3);\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; op[1] = a1; op[2] = a2; op[3] = a3;\n } else {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);\n float x3 = (ow + 1 < OW) ? __ldg(xp + 3) : 0.0f;\n int wo = wbase + c*27 + kz*9 + ky*3;\n float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n if (ow + 1 < OW) { a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1); }\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; if (ow + 1 < OW) op[1] = a1;\n }\n}\n\n__global__ void conv3d_c3k3s1p0_vec4_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH; int od = t % OD; t /= OD; int oc = t % OC; t /= OC; int n = (int)t;\n int ow = gw << 2;\n float a0 = has_bias ? b[oc] : 0.0f; float a1 = a0, a2 = a0, a3 = a0;\n const int HW = H * W, DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n const float* wp = w + (long long)oc * 81;\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;\n #pragma unroll\n for(int kz=0;kz<3;++kz){\n #pragma unroll\n for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;\n float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=__ldg(xp+3),x4=__ldg(xp+4),x5=__ldg(xp+5);\n float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);\n a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0);\n a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);\n a2=fmaf(w0,x2,a2);a2=fmaf(w1,x3,a2);a2=fmaf(w2,x4,a2);\n a3=fmaf(w0,x3,a3);a3=fmaf(w1,x4,a3);a3=fmaf(w2,x5,a3);\n }}}\n float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0;op[1]=a1;op[2]=a2;op[3]=a3;\n } else {\n #pragma unroll\n for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;\n #pragma unroll\n for(int kz=0;kz<3;++kz){\n #pragma unroll\n for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;\n float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=(ow+1<OW)?__ldg(xp+3):0.0f;\n float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);\n a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0); if(ow+1<OW){a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);} \n }}}\n float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0; if(ow+1<OW) op[1]=a1;\n }\n}\n\n__global__ void conv3d_generic_kernel(const float* __restrict__ x,const float* __restrict__ w,const float* __restrict__ b,float* __restrict__ out,int N,int C,int D,int H,int W,int OC,int KD,int KH,int KW,int OD,int OH,int OW,int stride,int padding,int dilation,int has_bias){\n long long total=(long long)N*OC*OD*OH*OW; long long idx=(long long)blockIdx.x*blockDim.x+threadIdx.x; if(idx>=total)return;\n int ow=idx%OW; long long t=idx/OW; int oh=t%OH; t/=OH; int od=t%OD; t/=OD; int oc=t%OC; t/=OC; int n=(int)t; float sum=has_bias?b[oc]:0.0f;\n for(int c=0;c<C;++c) for(int kz=0;kz<KD;++kz){int iz=od*stride-padding+kz*dilation; if((unsigned)iz>=(unsigned)D)continue; for(int ky=0;ky<KH;++ky){int iy=oh*stride-padding+ky*dilation; if((unsigned)iy>=(unsigned)H)continue; for(int kx=0;kx<KW;++kx){int ix=ow*stride-padding+kx*dilation; if((unsigned)ix>=(unsigned)W)continue; float xv=x[(((long long)n*C+c)*D+iz)*H*W+iy*W+ix]; float wv=w[(((long long)oc*C+c)*KD+kz)*KH*KW+ky*KW+kx]; sum=fmaf(wv,xv,sum);}}}\n out[idx]=sum;\n}\n\ntorch::Tensor conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride64, int64_t padding64, int64_t dilation64, int64_t groups64) {\n TORCH_CHECK(x.is_cuda(), \"x must be CUDA\"); TORCH_CHECK(weight.is_cuda(), \"weight must be CUDA\");\n x=x.contiguous(); weight=weight.contiguous();\n int N=(int)x.size(0), C=(int)x.size(1), D=(int)x.size(2), H=(int)x.size(3), W=(int)x.size(4);\n int OC=(int)weight.size(0), KD=(int)weight.size(2), KH=(int)weight.size(3), KW=(int)weight.size(4);\n int stride=(int)stride64, padding=(int)padding64, dilation=(int)dilation64;\n int OD=(D+2*padding-dilation*(KD-1)-1)/stride+1, OH=(H+2*padding-dilation*(KH-1)-1)/stride+1, OW=(W+2*padding-dilation*(KW-1)-1)/stride+1;\n auto out=torch::empty({N,OC,OD,OH,OW}, x.options());\n int has_bias=(bias.defined() && bias.numel()>0)?1:0; const float* bp=has_bias?bias.contiguous().data_ptr<float>():nullptr;\n const int threads=256;\n if(C==3 && KD==3 && KH==3 && KW==3 && stride==1 && padding==0 && dilation==1 && groups64==1 && x.scalar_type()==torch::kFloat32){\n int groupsW=(OW+3)>>2; long long total=(long long)N*OC*OD*OH*groupsW; int blocks=(int)((total+threads-1)/threads);\n if (OC*81 <= 8192) {\n cudaMemcpyToSymbol(c_weight, weight.data_ptr<float>(), (size_t)OC*81*sizeof(float), 0, cudaMemcpyDeviceToDevice);\n conv3d_c3k3s1p0_vec4_const_kernel<<<blocks,threads>>>(x.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);\n } else {\n conv3d_c3k3s1p0_vec4_kernel<<<blocks,threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);\n }\n } else {\n long long total=(long long)N*OC*OD*OH*OW; int blocks=(int)((total+threads-1)/threads);\n conv3d_generic_kernel<<<blocks,threads>>>(x.data_ptr<float>(),weight.data_ptr<float>(),bp,out.data_ptr<float>(),N,C,D,H,W,OC,KD,KH,KW,OD,OH,OW,stride,padding,dilation,has_bias);\n }\n return out;\n}\n\"\"\"\n\n_ext = load_inline(name=\"conv3d_c3k3_ext_v2\", cpp_sources=_cpp_src, cuda_sources=_cuda_src,\n functions=[\"conv3d_forward\"], extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = False):\n super(ModelNew, self).__init__()\n self.conv3d = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)\n self.stride = stride; self.padding = padding; self.dilation = dilation; self.groups = groups\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n b = self.conv3d.bias if self.conv3d.bias is not None else torch.empty(0, device=x.device, dtype=x.dtype)\n return _ext.conv3d_forward(x, self.conv3d.weight, b, int(self.stride), int(self.padding), int(self.dilation), int(self.groups))\n" }
result ← compile_kernel (ok)
okcompile_kernel compile_kernel PASSED: kernel compiled without errors.
turn 4 · 1 tool call(s) · 21.4s turn · 21.4s LLM · 11,837 chars (click to expand)
turn 421.4s turn · 21.4s 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 conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride, int64_t padding, int64_t dilation, int64_t groups);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n__constant__ float c_weight[8192];\n\n__global__ void conv3d_c3k3s1p0_vec4_const_kernel(const float* __restrict__ x,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH;\n int od = t % OD; t /= OD;\n int oc = t % OC; t /= OC;\n int n = (int)t;\n int ow = gw << 2;\n float a0 = has_bias ? b[oc] : 0.0f;\n float a1 = a0, a2 = a0, a3 = a0;\n const int HW = H * W;\n const int DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n int wbase = oc * 81;\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);\n float x3 = __ldg(xp + 3), x4 = __ldg(xp + 4), x5 = __ldg(xp + 5);\n int wo = wbase + c*27 + kz*9 + ky*3;\n float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1);\n a2 = fmaf(w0, x2, a2); a2 = fmaf(w1, x3, a2); a2 = fmaf(w2, x4, a2);\n a3 = fmaf(w0, x3, a3); a3 = fmaf(w1, x4, a3); a3 = fmaf(w2, x5, a3);\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; op[1] = a1; op[2] = a2; op[3] = a3;\n } else {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);\n float x3 = (ow + 1 < OW) ? __ldg(xp + 3) : 0.0f;\n int wo = wbase + c*27 + kz*9 + ky*3;\n float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n if (ow + 1 < OW) { a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1); }\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; if (ow + 1 < OW) op[1] = a1;\n }\n}\n\n__global__ void conv3d_c3k3s1p0_vec4_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH; int od = t % OD; t /= OD; int oc = t % OC; t /= OC; int n = (int)t;\n int ow = gw << 2;\n float a0 = has_bias ? b[oc] : 0.0f; float a1 = a0, a2 = a0, a3 = a0;\n const int HW = H * W, DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n const float* wp = w + (long long)oc * 81;\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;\n #pragma unroll\n for(int kz=0;kz<3;++kz){\n #pragma unroll\n for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;\n float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=__ldg(xp+3),x4=__ldg(xp+4),x5=__ldg(xp+5);\n float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);\n a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0);\n a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);\n a2=fmaf(w0,x2,a2);a2=fmaf(w1,x3,a2);a2=fmaf(w2,x4,a2);\n a3=fmaf(w0,x3,a3);a3=fmaf(w1,x4,a3);a3=fmaf(w2,x5,a3);\n }}}\n float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0;op[1]=a1;op[2]=a2;op[3]=a3;\n } else {\n #pragma unroll\n for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;\n #pragma unroll\n for(int kz=0;kz<3;++kz){\n #pragma unroll\n for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;\n float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=(ow+1<OW)?__ldg(xp+3):0.0f;\n float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);\n a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0); if(ow+1<OW){a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);} \n }}}\n float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0; if(ow+1<OW) op[1]=a1;\n }\n}\n\n__global__ void conv3d_generic_kernel(const float* __restrict__ x,const float* __restrict__ w,const float* __restrict__ b,float* __restrict__ out,int N,int C,int D,int H,int W,int OC,int KD,int KH,int KW,int OD,int OH,int OW,int stride,int padding,int dilation,int has_bias){\n long long total=(long long)N*OC*OD*OH*OW; long long idx=(long long)blockIdx.x*blockDim.x+threadIdx.x; if(idx>=total)return;\n int ow=idx%OW; long long t=idx/OW; int oh=t%OH; t/=OH; int od=t%OD; t/=OD; int oc=t%OC; t/=OC; int n=(int)t; float sum=has_bias?b[oc]:0.0f;\n for(int c=0;c<C;++c) for(int kz=0;kz<KD;++kz){int iz=od*stride-padding+kz*dilation; if((unsigned)iz>=(unsigned)D)continue; for(int ky=0;ky<KH;++ky){int iy=oh*stride-padding+ky*dilation; if((unsigned)iy>=(unsigned)H)continue; for(int kx=0;kx<KW;++kx){int ix=ow*stride-padding+kx*dilation; if((unsigned)ix>=(unsigned)W)continue; float xv=x[(((long long)n*C+c)*D+iz)*H*W+iy*W+ix]; float wv=w[(((long long)oc*C+c)*KD+kz)*KH*KW+ky*KW+kx]; sum=fmaf(wv,xv,sum);}}}\n out[idx]=sum;\n}\n\ntorch::Tensor conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride64, int64_t padding64, int64_t dilation64, int64_t groups64) {\n TORCH_CHECK(x.is_cuda(), \"x must be CUDA\"); TORCH_CHECK(weight.is_cuda(), \"weight must be CUDA\");\n x=x.contiguous(); weight=weight.contiguous();\n int N=(int)x.size(0), C=(int)x.size(1), D=(int)x.size(2), H=(int)x.size(3), W=(int)x.size(4);\n int OC=(int)weight.size(0), KD=(int)weight.size(2), KH=(int)weight.size(3), KW=(int)weight.size(4);\n int stride=(int)stride64, padding=(int)padding64, dilation=(int)dilation64;\n int OD=(D+2*padding-dilation*(KD-1)-1)/stride+1, OH=(H+2*padding-dilation*(KH-1)-1)/stride+1, OW=(W+2*padding-dilation*(KW-1)-1)/stride+1;\n auto out=torch::empty({N,OC,OD,OH,OW}, x.options());\n int has_bias=(bias.defined() && bias.numel()>0)?1:0; const float* bp=has_bias?bias.contiguous().data_ptr<float>():nullptr;\n const int threads=256;\n if(C==3 && KD==3 && KH==3 && KW==3 && stride==1 && padding==0 && dilation==1 && groups64==1 && x.scalar_type()==torch::kFloat32){\n int groupsW=(OW+3)>>2; long long total=(long long)N*OC*OD*OH*groupsW; int blocks=(int)((total+threads-1)/threads);\n if (OC*81 <= 8192) {\n cudaMemcpyToSymbol(c_weight, weight.data_ptr<float>(), (size_t)OC*81*sizeof(float), 0, cudaMemcpyDeviceToDevice);\n conv3d_c3k3s1p0_vec4_const_kernel<<<blocks,threads>>>(x.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);\n } else {\n conv3d_c3k3s1p0_vec4_kernel<<<blocks,threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);\n }\n } else {\n long long total=(long long)N*OC*OD*OH*OW; int blocks=(int)((total+threads-1)/threads);\n conv3d_generic_kernel<<<blocks,threads>>>(x.data_ptr<float>(),weight.data_ptr<float>(),bp,out.data_ptr<float>(),N,C,D,H,W,OC,KD,KH,KW,OD,OH,OW,stride,padding,dilation,has_bias);\n }\n return out;\n}\n\"\"\"\n\n_ext = load_inline(name=\"conv3d_c3k3_ext_v2\", cpp_sources=_cpp_src, cuda_sources=_cuda_src,\n functions=[\"conv3d_forward\"], extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = False):\n super(ModelNew, self).__init__()\n self.conv3d = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)\n self.stride = stride; self.padding = padding; self.dilation = dilation; self.groups = groups\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n b = self.conv3d.bias if self.conv3d.bias is not None else torch.empty(0, device=x.device, dtype=x.dtype)\n return _ext.conv3d_forward(x, self.conv3d.weight, b, int(self.stride), int(self.padding), int(self.dilation), int(self.groups))\n" }
result ← run_correctness (ok)
okrun_correctness run_correctness PASSED: (5 / 5) trials all matched the reference. Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00
turn 5 · 1 tool call(s) · 30.1s turn · 30.1s LLM · 11,729 chars (click to expand)
turn 530.1s turn · 30.1s LLM · 0.0s tools
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 conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride, int64_t padding, int64_t dilation, int64_t groups);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n__constant__ float c_weight[8192];\n\n__global__ void conv3d_c3k3s1p0_vec4_const_kernel(const float* __restrict__ x,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH;\n int od = t % OD; t /= OD;\n int oc = t % OC; t /= OC;\n int n = (int)t;\n int ow = gw << 2;\n float a0 = has_bias ? b[oc] : 0.0f;\n float a1 = a0, a2 = a0, a3 = a0;\n const int HW = H * W;\n const int DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n int wbase = oc * 81;\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);\n float x3 = __ldg(xp + 3), x4 = __ldg(xp + 4), x5 = __ldg(xp + 5);\n int wo = wbase + c*27 + kz*9 + ky*3;\n float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1);\n a2 = fmaf(w0, x2, a2); a2 = fmaf(w1, x3, a2); a2 = fmaf(w2, x4, a2);\n a3 = fmaf(w0, x3, a3); a3 = fmaf(w1, x4, a3); a3 = fmaf(w2, x5, a3);\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; op[1] = a1; op[2] = a2; op[3] = a3;\n } else {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);\n float x3 = (ow + 1 < OW) ? __ldg(xp + 3) : 0.0f;\n int wo = wbase + c*27 + kz*9 + ky*3;\n float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n if (ow + 1 < OW) { a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1); }\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; if (ow + 1 < OW) op[1] = a1;\n }\n}\n\n__global__ void conv3d_c3k3s1p0_vec4_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH; int od = t % OD; t /= OD; int oc = t % OC; t /= OC; int n = (int)t;\n int ow = gw << 2;\n float a0 = has_bias ? b[oc] : 0.0f; float a1 = a0, a2 = a0, a3 = a0;\n const int HW = H * W, DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n const float* wp = w + (long long)oc * 81;\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;\n #pragma unroll\n for(int kz=0;kz<3;++kz){\n #pragma unroll\n for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;\n float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=__ldg(xp+3),x4=__ldg(xp+4),x5=__ldg(xp+5);\n float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);\n a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0);\n a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);\n a2=fmaf(w0,x2,a2);a2=fmaf(w1,x3,a2);a2=fmaf(w2,x4,a2);\n a3=fmaf(w0,x3,a3);a3=fmaf(w1,x4,a3);a3=fmaf(w2,x5,a3);\n }}}\n float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0;op[1]=a1;op[2]=a2;op[3]=a3;\n } else {\n #pragma unroll\n for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;\n #pragma unroll\n for(int kz=0;kz<3;++kz){\n #pragma unroll\n for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;\n float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=(ow+1<OW)?__ldg(xp+3):0.0f;\n float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);\n a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0); if(ow+1<OW){a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);} \n }}}\n float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0; if(ow+1<OW) op[1]=a1;\n }\n}\n\n__global__ void conv3d_generic_kernel(const float* __restrict__ x,const float* __restrict__ w,const float* __restrict__ b,float* __restrict__ out,int N,int C,int D,int H,int W,int OC,int KD,int KH,int KW,int OD,int OH,int OW,int stride,int padding,int dilation,int has_bias){\n long long total=(long long)N*OC*OD*OH*OW; long long idx=(long long)blockIdx.x*blockDim.x+threadIdx.x; if(idx>=total)return;\n int ow=idx%OW; long long t=idx/OW; int oh=t%OH; t/=OH; int od=t%OD; t/=OD; int oc=t%OC; t/=OC; int n=(int)t; float sum=has_bias?b[oc]:0.0f;\n for(int c=0;c<C;++c) for(int kz=0;kz<KD;++kz){int iz=od*stride-padding+kz*dilation; if((unsigned)iz>=(unsigned)D)continue; for(int ky=0;ky<KH;++ky){int iy=oh*stride-padding+ky*dilation; if((unsigned)iy>=(unsigned)H)continue; for(int kx=0;kx<KW;++kx){int ix=ow*stride-padding+kx*dilation; if((unsigned)ix>=(unsigned)W)continue; float xv=x[(((long long)n*C+c)*D+iz)*H*W+iy*W+ix]; float wv=w[(((long long)oc*C+c)*KD+kz)*KH*KW+ky*KW+kx]; sum=fmaf(wv,xv,sum);}}}\n out[idx]=sum;\n}\n\ntorch::Tensor conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride64, int64_t padding64, int64_t dilation64, int64_t groups64) {\n TORCH_CHECK(x.is_cuda(), \"x must be CUDA\"); TORCH_CHECK(weight.is_cuda(), \"weight must be CUDA\");\n x=x.contiguous(); weight=weight.contiguous();\n int N=(int)x.size(0), C=(int)x.size(1), D=(int)x.size(2), H=(int)x.size(3), W=(int)x.size(4);\n int OC=(int)weight.size(0), KD=(int)weight.size(2), KH=(int)weight.size(3), KW=(int)weight.size(4);\n int stride=(int)stride64, padding=(int)padding64, dilation=(int)dilation64;\n int OD=(D+2*padding-dilation*(KD-1)-1)/stride+1, OH=(H+2*padding-dilation*(KH-1)-1)/stride+1, OW=(W+2*padding-dilation*(KW-1)-1)/stride+1;\n auto out=torch::empty({N,OC,OD,OH,OW}, x.options());\n int has_bias=(bias.defined() && bias.numel()>0)?1:0; const float* bp=has_bias?bias.contiguous().data_ptr<float>():nullptr;\n const int threads=256;\n if(C==3 && KD==3 && KH==3 && KW==3 && stride==1 && padding==0 && dilation==1 && groups64==1 && x.scalar_type()==torch::kFloat32){\n int groupsW=(OW+3)>>2; long long total=(long long)N*OC*OD*OH*groupsW; int blocks=(int)((total+threads-1)/threads);\n if (OC*81 <= 8192) {\n cudaMemcpyToSymbol(c_weight, weight.data_ptr<float>(), (size_t)OC*81*sizeof(float), 0, cudaMemcpyDeviceToDevice);\n conv3d_c3k3s1p0_vec4_const_kernel<<<blocks,threads>>>(x.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);\n } else {\n conv3d_c3k3s1p0_vec4_kernel<<<blocks,threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);\n }\n } else {\n long long total=(long long)N*OC*OD*OH*OW; int blocks=(int)((total+threads-1)/threads);\n conv3d_generic_kernel<<<blocks,threads>>>(x.data_ptr<float>(),weight.data_ptr<float>(),bp,out.data_ptr<float>(),N,C,D,H,W,OC,KD,KH,KW,OD,OH,OW,stride,padding,dilation,has_bias);\n }\n return out;\n}\n\"\"\"\n\n_ext = load_inline(name=\"conv3d_c3k3_ext_v2\", cpp_sources=_cpp_src, cuda_sources=_cuda_src,\n functions=[\"conv3d_forward\"], extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = False):\n super(ModelNew, self).__init__()\n self.conv3d = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)\n self.stride = stride; self.padding = padding; self.dilation = dilation; self.groups = groups\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n b = self.conv3d.bias if self.conv3d.bias is not None else torch.empty(0, device=x.device, dtype=x.dtype)\n return _ext.conv3d_forward(x, self.conv3d.weight, b, int(self.stride), int(self.padding), int(self.dilation), int(self.groups))\n" }
result ← static_check (ok)
okstatic_check static_check PASSED: no violations or warnings detected.
turn 6 · FINAL · 1 tool call(s) · 21.4s turn · 21.4s LLM · 12,591 chars (click to expand)
turn 621.4s turn · 21.4s LLM · 0.0s tools · FINAL
reasoning
**Deciding on submission** I need to finalize my submission, but I'm wondering if the static code should be classified as version 2. I think using "submit" makes sense in this context. I just want to make sure I'm making the right choice here. It's important to be clear and efficient when finalizing this. I want the outcome to be as polished as possible! Let's go with submitting it now.
call → submit_kernel
callsubmit_kernel { "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 conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride, int64_t padding, int64_t dilation, int64_t groups);\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n__constant__ float c_weight[8192];\n\n__global__ void conv3d_c3k3s1p0_vec4_const_kernel(const float* __restrict__ x,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH;\n int od = t % OD; t /= OD;\n int oc = t % OC; t /= OC;\n int n = (int)t;\n int ow = gw << 2;\n float a0 = has_bias ? b[oc] : 0.0f;\n float a1 = a0, a2 = a0, a3 = a0;\n const int HW = H * W;\n const int DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n int wbase = oc * 81;\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);\n float x3 = __ldg(xp + 3), x4 = __ldg(xp + 4), x5 = __ldg(xp + 5);\n int wo = wbase + c*27 + kz*9 + ky*3;\n float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1);\n a2 = fmaf(w0, x2, a2); a2 = fmaf(w1, x3, a2); a2 = fmaf(w2, x4, a2);\n a3 = fmaf(w0, x3, a3); a3 = fmaf(w1, x4, a3); a3 = fmaf(w2, x5, a3);\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; op[1] = a1; op[2] = a2; op[3] = a3;\n } else {\n #pragma unroll\n for (int c = 0; c < 3; ++c) {\n const float* xc = xbase + (long long)c * DHW;\n #pragma unroll\n for (int kz = 0; kz < 3; ++kz) {\n #pragma unroll\n for (int ky = 0; ky < 3; ++ky) {\n const float* xp = xc + kz * HW + ky * W;\n float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);\n float x3 = (ow + 1 < OW) ? __ldg(xp + 3) : 0.0f;\n int wo = wbase + c*27 + kz*9 + ky*3;\n float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];\n a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);\n if (ow + 1 < OW) { a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1); }\n }\n }\n }\n float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;\n op[0] = a0; if (ow + 1 < OW) op[1] = a1;\n }\n}\n\n__global__ void conv3d_c3k3s1p0_vec4_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ out,\n int N, int D, int H, int W,\n int OC, int OD, int OH, int OW,\n int has_bias) {\n int groupsW = (OW + 3) >> 2;\n long long total = (long long)N * OC * OD * OH * groupsW;\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n if (tid >= total) return;\n int gw = tid % groupsW;\n long long t = tid / groupsW;\n int oh = t % OH; t /= OH; int od = t % OD; t /= OD; int oc = t % OC; t /= OC; int n = (int)t;\n int ow = gw << 2;\n float a0 = has_bias ? b[oc] : 0.0f; float a1 = a0, a2 = a0, a3 = a0;\n const int HW = H * W, DHW = D * HW;\n const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;\n const float* wp = w + (long long)oc * 81;\n if (ow + 3 < OW) {\n #pragma unroll\n for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;\n #pragma unroll\n for(int kz=0;kz<3;++kz){\n #pragma unroll\n for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;\n float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=__ldg(xp+3),x4=__ldg(xp+4),x5=__ldg(xp+5);\n float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);\n a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0);\n a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);\n a2=fmaf(w0,x2,a2);a2=fmaf(w1,x3,a2);a2=fmaf(w2,x4,a2);\n a3=fmaf(w0,x3,a3);a3=fmaf(w1,x4,a3);a3=fmaf(w2,x5,a3);\n }}}\n float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0;op[1]=a1;op[2]=a2;op[3]=a3;\n } else {\n #pragma unroll\n for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;\n #pragma unroll\n for(int kz=0;kz<3;++kz){\n #pragma unroll\n for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;\n float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=(ow+1<OW)?__ldg(xp+3):0.0f;\n float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);\n a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0); if(ow+1<OW){a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);} \n }}}\n float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0; if(ow+1<OW) op[1]=a1;\n }\n}\n\n__global__ void conv3d_generic_kernel(const float* __restrict__ x,const float* __restrict__ w,const float* __restrict__ b,float* __restrict__ out,int N,int C,int D,int H,int W,int OC,int KD,int KH,int KW,int OD,int OH,int OW,int stride,int padding,int dilation,int has_bias){\n long long total=(long long)N*OC*OD*OH*OW; long long idx=(long long)blockIdx.x*blockDim.x+threadIdx.x; if(idx>=total)return;\n int ow=idx%OW; long long t=idx/OW; int oh=t%OH; t/=OH; int od=t%OD; t/=OD; int oc=t%OC; t/=OC; int n=(int)t; float sum=has_bias?b[oc]:0.0f;\n for(int c=0;c<C;++c) for(int kz=0;kz<KD;++kz){int iz=od*stride-padding+kz*dilation; if((unsigned)iz>=(unsigned)D)continue; for(int ky=0;ky<KH;++ky){int iy=oh*stride-padding+ky*dilation; if((unsigned)iy>=(unsigned)H)continue; for(int kx=0;kx<KW;++kx){int ix=ow*stride-padding+kx*dilation; if((unsigned)ix>=(unsigned)W)continue; float xv=x[(((long long)n*C+c)*D+iz)*H*W+iy*W+ix]; float wv=w[(((long long)oc*C+c)*KD+kz)*KH*KW+ky*KW+kx]; sum=fmaf(wv,xv,sum);}}}\n out[idx]=sum;\n}\n\ntorch::Tensor conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride64, int64_t padding64, int64_t dilation64, int64_t groups64) {\n TORCH_CHECK(x.is_cuda(), \"x must be CUDA\"); TORCH_CHECK(weight.is_cuda(), \"weight must be CUDA\");\n x=x.contiguous(); weight=weight.contiguous();\n int N=(int)x.size(0), C=(int)x.size(1), D=(int)x.size(2), H=(int)x.size(3), W=(int)x.size(4);\n int OC=(int)weight.size(0), KD=(int)weight.size(2), KH=(int)weight.size(3), KW=(int)weight.size(4);\n int stride=(int)stride64, padding=(int)padding64, dilation=(int)dilation64;\n int OD=(D+2*padding-dilation*(KD-1)-1)/stride+1, OH=(H+2*padding-dilation*(KH-1)-1)/stride+1, OW=(W+2*padding-dilation*(KW-1)-1)/stride+1;\n auto out=torch::empty({N,OC,OD,OH,OW}, x.options());\n int has_bias=(bias.defined() && bias.numel()>0)?1:0; const float* bp=has_bias?bias.contiguous().data_ptr<float>():nullptr;\n const int threads=256;\n if(C==3 && KD==3 && KH==3 && KW==3 && stride==1 && padding==0 && dilation==1 && groups64==1 && x.scalar_type()==torch::kFloat32){\n int groupsW=(OW+3)>>2; long long total=(long long)N*OC*OD*OH*groupsW; int blocks=(int)((total+threads-1)/threads);\n if (OC*81 <= 8192) {\n cudaMemcpyToSymbol(c_weight, weight.data_ptr<float>(), (size_t)OC*81*sizeof(float), 0, cudaMemcpyDeviceToDevice);\n conv3d_c3k3s1p0_vec4_const_kernel<<<blocks,threads>>>(x.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);\n } else {\n conv3d_c3k3s1p0_vec4_kernel<<<blocks,threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);\n }\n } else {\n long long total=(long long)N*OC*OD*OH*OW; int blocks=(int)((total+threads-1)/threads);\n conv3d_generic_kernel<<<blocks,threads>>>(x.data_ptr<float>(),weight.data_ptr<float>(),bp,out.data_ptr<float>(),N,C,D,H,W,OC,KD,KH,KW,OD,OH,OW,stride,padding,dilation,has_bias);\n }\n return out;\n}\n\"\"\"\n\n_ext = load_inline(name=\"conv3d_c3k3_ext_v2\", cpp_sources=_cpp_src, cuda_sources=_cuda_src,\n functions=[\"conv3d_forward\"], extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = False):\n super(ModelNew, self).__init__()\n self.conv3d = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)\n self.stride = stride; self.padding = padding; self.dilation = dilation; self.groups = groups\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n b = self.conv3d.bias if self.conv3d.bias is not None else torch.empty(0, device=x.device, dtype=x.dtype)\n return _ext.conv3d_forward(x, self.conv3d.weight, b, int(self.stride), int(self.padding), int(self.dilation), int(self.groups))\n" }
result ← submit_kernel (ok)
oksubmit_kernel submit_kernel PASSED: (5 / 5) correctness trials all passed. Kernel runtime: 6.69 μs Runtime stats: mean=6.69μs median=?μs std=0.15μs Numerical precision: max_abs_err=0.00e+00 mean_abs_err=0.00e+00 max_rel_err=0.00e+00 Memory: 1012.01 MB peak (ref: 1012.01 MB, ratio: 1.0x) Kernel launches: 2 (ref: 4, fusion ratio: 2.0) Energy: 2358.72 mJ/run (ref: 0.0 mJ, ratio: -1x) SOL score: 0.1824
final submitted kernel (180 lines, 10,536 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 conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,
                             int64_t stride, int64_t padding, int64_t dilation, int64_t groups);
"""

_cuda_src = r"""
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>

__constant__ float c_weight[8192];

__global__ void conv3d_c3k3s1p0_vec4_const_kernel(const float* __restrict__ x,
                                             const float* __restrict__ b,
                                             float* __restrict__ out,
                                             int N, int D, int H, int W,
                                             int OC, int OD, int OH, int OW,
                                             int has_bias) {
    int groupsW = (OW + 3) >> 2;
    long long total = (long long)N * OC * OD * OH * groupsW;
    long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;
    if (tid >= total) return;
    int gw = tid % groupsW;
    long long t = tid / groupsW;
    int oh = t % OH; t /= OH;
    int od = t % OD; t /= OD;
    int oc = t % OC; t /= OC;
    int n = (int)t;
    int ow = gw << 2;
    float a0 = has_bias ? b[oc] : 0.0f;
    float a1 = a0, a2 = a0, a3 = a0;
    const int HW = H * W;
    const int DHW = D * HW;
    const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;
    int wbase = oc * 81;
    if (ow + 3 < OW) {
        #pragma unroll
        for (int c = 0; c < 3; ++c) {
            const float* xc = xbase + (long long)c * DHW;
            #pragma unroll
            for (int kz = 0; kz < 3; ++kz) {
                #pragma unroll
                for (int ky = 0; ky < 3; ++ky) {
                    const float* xp = xc + kz * HW + ky * W;
                    float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);
                    float x3 = __ldg(xp + 3), x4 = __ldg(xp + 4), x5 = __ldg(xp + 5);
                    int wo = wbase + c*27 + kz*9 + ky*3;
                    float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];
                    a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);
                    a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1);
                    a2 = fmaf(w0, x2, a2); a2 = fmaf(w1, x3, a2); a2 = fmaf(w2, x4, a2);
                    a3 = fmaf(w0, x3, a3); a3 = fmaf(w1, x4, a3); a3 = fmaf(w2, x5, a3);
                }
            }
        }
        float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;
        op[0] = a0; op[1] = a1; op[2] = a2; op[3] = a3;
    } else {
        #pragma unroll
        for (int c = 0; c < 3; ++c) {
            const float* xc = xbase + (long long)c * DHW;
            #pragma unroll
            for (int kz = 0; kz < 3; ++kz) {
                #pragma unroll
                for (int ky = 0; ky < 3; ++ky) {
                    const float* xp = xc + kz * HW + ky * W;
                    float x0 = __ldg(xp + 0), x1 = __ldg(xp + 1), x2 = __ldg(xp + 2);
                    float x3 = (ow + 1 < OW) ? __ldg(xp + 3) : 0.0f;
                    int wo = wbase + c*27 + kz*9 + ky*3;
                    float w0 = c_weight[wo + 0], w1 = c_weight[wo + 1], w2 = c_weight[wo + 2];
                    a0 = fmaf(w0, x0, a0); a0 = fmaf(w1, x1, a0); a0 = fmaf(w2, x2, a0);
                    if (ow + 1 < OW) { a1 = fmaf(w0, x1, a1); a1 = fmaf(w1, x2, a1); a1 = fmaf(w2, x3, a1); }
                }
            }
        }
        float* op = out + ((((long long)n * OC + oc) * OD + od) * OH + oh) * OW + ow;
        op[0] = a0; if (ow + 1 < OW) op[1] = a1;
    }
}

__global__ void conv3d_c3k3s1p0_vec4_kernel(const float* __restrict__ x,
                                             const float* __restrict__ w,
                                             const float* __restrict__ b,
                                             float* __restrict__ out,
                                             int N, int D, int H, int W,
                                             int OC, int OD, int OH, int OW,
                                             int has_bias) {
    int groupsW = (OW + 3) >> 2;
    long long total = (long long)N * OC * OD * OH * groupsW;
    long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;
    if (tid >= total) return;
    int gw = tid % groupsW;
    long long t = tid / groupsW;
    int oh = t % OH; t /= OH; int od = t % OD; t /= OD; int oc = t % OC; t /= OC; int n = (int)t;
    int ow = gw << 2;
    float a0 = has_bias ? b[oc] : 0.0f; float a1 = a0, a2 = a0, a3 = a0;
    const int HW = H * W, DHW = D * HW;
    const float* xbase = x + ((long long)n * 3 * D + od) * HW + oh * W + ow;
    const float* wp = w + (long long)oc * 81;
    if (ow + 3 < OW) {
        #pragma unroll
        for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;
        #pragma unroll
        for(int kz=0;kz<3;++kz){
        #pragma unroll
        for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;
            float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=__ldg(xp+3),x4=__ldg(xp+4),x5=__ldg(xp+5);
            float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);
            a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0);
            a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);
            a2=fmaf(w0,x2,a2);a2=fmaf(w1,x3,a2);a2=fmaf(w2,x4,a2);
            a3=fmaf(w0,x3,a3);a3=fmaf(w1,x4,a3);a3=fmaf(w2,x5,a3);
        }}}
        float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0;op[1]=a1;op[2]=a2;op[3]=a3;
    } else {
        #pragma unroll
        for (int c=0;c<3;++c){ const float* xc=xbase+(long long)c*DHW;
        #pragma unroll
        for(int kz=0;kz<3;++kz){
        #pragma unroll
        for(int ky=0;ky<3;++ky){ const float* xp=xc+kz*HW+ky*W;
            float x0=__ldg(xp),x1=__ldg(xp+1),x2=__ldg(xp+2),x3=(ow+1<OW)?__ldg(xp+3):0.0f;
            float w0=__ldg(wp+c*27+kz*9+ky*3),w1=__ldg(wp+c*27+kz*9+ky*3+1),w2=__ldg(wp+c*27+kz*9+ky*3+2);
            a0=fmaf(w0,x0,a0);a0=fmaf(w1,x1,a0);a0=fmaf(w2,x2,a0); if(ow+1<OW){a1=fmaf(w0,x1,a1);a1=fmaf(w1,x2,a1);a1=fmaf(w2,x3,a1);}        
        }}}
        float* op=out+((((long long)n*OC+oc)*OD+od)*OH+oh)*OW+ow; op[0]=a0; if(ow+1<OW) op[1]=a1;
    }
}

__global__ void conv3d_generic_kernel(const float* __restrict__ x,const float* __restrict__ w,const float* __restrict__ b,float* __restrict__ out,int N,int C,int D,int H,int W,int OC,int KD,int KH,int KW,int OD,int OH,int OW,int stride,int padding,int dilation,int has_bias){
 long long total=(long long)N*OC*OD*OH*OW; long long idx=(long long)blockIdx.x*blockDim.x+threadIdx.x; if(idx>=total)return;
 int ow=idx%OW; long long t=idx/OW; int oh=t%OH; t/=OH; int od=t%OD; t/=OD; int oc=t%OC; t/=OC; int n=(int)t; float sum=has_bias?b[oc]:0.0f;
 for(int c=0;c<C;++c) for(int kz=0;kz<KD;++kz){int iz=od*stride-padding+kz*dilation; if((unsigned)iz>=(unsigned)D)continue; for(int ky=0;ky<KH;++ky){int iy=oh*stride-padding+ky*dilation; if((unsigned)iy>=(unsigned)H)continue; for(int kx=0;kx<KW;++kx){int ix=ow*stride-padding+kx*dilation; if((unsigned)ix>=(unsigned)W)continue; float xv=x[(((long long)n*C+c)*D+iz)*H*W+iy*W+ix]; float wv=w[(((long long)oc*C+c)*KD+kz)*KH*KW+ky*KW+kx]; sum=fmaf(wv,xv,sum);}}}
 out[idx]=sum;
}

torch::Tensor conv3d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,
                             int64_t stride64, int64_t padding64, int64_t dilation64, int64_t groups64) {
    TORCH_CHECK(x.is_cuda(), "x must be CUDA"); TORCH_CHECK(weight.is_cuda(), "weight must be CUDA");
    x=x.contiguous(); weight=weight.contiguous();
    int N=(int)x.size(0), C=(int)x.size(1), D=(int)x.size(2), H=(int)x.size(3), W=(int)x.size(4);
    int OC=(int)weight.size(0), KD=(int)weight.size(2), KH=(int)weight.size(3), KW=(int)weight.size(4);
    int stride=(int)stride64, padding=(int)padding64, dilation=(int)dilation64;
    int OD=(D+2*padding-dilation*(KD-1)-1)/stride+1, OH=(H+2*padding-dilation*(KH-1)-1)/stride+1, OW=(W+2*padding-dilation*(KW-1)-1)/stride+1;
    auto out=torch::empty({N,OC,OD,OH,OW}, x.options());
    int has_bias=(bias.defined() && bias.numel()>0)?1:0; const float* bp=has_bias?bias.contiguous().data_ptr<float>():nullptr;
    const int threads=256;
    if(C==3 && KD==3 && KH==3 && KW==3 && stride==1 && padding==0 && dilation==1 && groups64==1 && x.scalar_type()==torch::kFloat32){
        int groupsW=(OW+3)>>2; long long total=(long long)N*OC*OD*OH*groupsW; int blocks=(int)((total+threads-1)/threads);
        if (OC*81 <= 8192) {
            cudaMemcpyToSymbol(c_weight, weight.data_ptr<float>(), (size_t)OC*81*sizeof(float), 0, cudaMemcpyDeviceToDevice);
            conv3d_c3k3s1p0_vec4_const_kernel<<<blocks,threads>>>(x.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);
        } else {
            conv3d_c3k3s1p0_vec4_kernel<<<blocks,threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp, out.data_ptr<float>(), N,D,H,W,OC,OD,OH,OW,has_bias);
        }
    } else {
        long long total=(long long)N*OC*OD*OH*OW; int blocks=(int)((total+threads-1)/threads);
        conv3d_generic_kernel<<<blocks,threads>>>(x.data_ptr<float>(),weight.data_ptr<float>(),bp,out.data_ptr<float>(),N,C,D,H,W,OC,KD,KH,KW,OD,OH,OW,stride,padding,dilation,has_bias);
    }
    return out;
}
"""

_ext = load_inline(name="conv3d_c3k3_ext_v2", cpp_sources=_cpp_src, cuda_sources=_cuda_src,
                   functions=["conv3d_forward"], extra_cuda_cflags=["-O3", "--use_fast_math"], verbose=False)

class ModelNew(nn.Module):
    def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1, bias: bool = False):
        super(ModelNew, self).__init__()
        self.conv3d = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
        self.stride = stride; self.padding = padding; self.dilation = dilation; self.groups = groups
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        b = self.conv3d.bias if self.conv3d.bias is not None else torch.empty(0, device=x.device, dtype=x.dtype)
        return _ext.conv3d_forward(x, self.conv3d.weight, b, int(self.stride), int(self.padding), int(self.dilation), int(self.groups))