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>\n\ntorch::Tensor conv_transpose3d_forward(torch::Tensor x, torch::Tensor w, torch::Tensor b,\n int64_t sD, int64_t sH, int64_t sW,\n int64_t pD, int64_t pH, int64_t pW,\n int64_t opD, int64_t opH, int64_t opW,\n int64_t groups, bool has_bias);\n'''\n\n_cuda_src = r'''\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <c10/cuda/CUDAException.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n#define KDV 3\n#define KHV 5\n#define KWV 5\n#define KVOL 75\n\n__global__ __launch_bounds__(128, 2)\nvoid ct3d_default_interior8_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ y,\n int N, int Cin, int D, int H, int W,\n int Cout, int Dout, int Hout, int Wout,\n int Dint, int Hint, int Wint,\n int has_bias) {\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long total = (long long)N * (Cout >> 3) * Dint * Hint * Wint;\n if (tid >= total) return;\n\n int sp = (int)(tid % ((long long)Dint * Hint * Wint));\n int cb = (int)((tid / ((long long)Dint * Hint * Wint)) % (Cout >> 3));\n int n = (int)(tid / (((long long)(Cout >> 3)) * Dint * Hint * Wint));\n\n int ow = sp % Wint + (KWV - 1);\n int t = sp / Wint;\n int oh = t % Hint + (KHV - 1);\n int od = t / Hint + (KDV - 1);\n int co = cb << 3;\n\n float a0 = has_bias ? __ldg(b + co + 0) : 0.0f;\n float a1 = has_bias ? __ldg(b + co + 1) : 0.0f;\n float a2 = has_bias ? __ldg(b + co + 2) : 0.0f;\n float a3 = has_bias ? __ldg(b + co + 3) : 0.0f;\n float a4 = has_bias ? __ldg(b + co + 4) : 0.0f;\n float a5 = has_bias ? __ldg(b + co + 5) : 0.0f;\n float a6 = has_bias ? __ldg(b + co + 6) : 0.0f;\n float a7 = has_bias ? __ldg(b + co + 7) : 0.0f;\n\n const long long xN = (long long)n * Cin * D * H * W;\n #pragma unroll 1\n for (int ci = 0; ci < Cin; ++ci) {\n const long long xC = xN + (long long)ci * D * H * W;\n const long long wC = (long long)ci * Cout * KVOL + (long long)co * KVOL;\n #pragma unroll\n for (int kd = 0; kd < KDV; ++kd) {\n int id = od - kd;\n #pragma unroll\n for (int kh = 0; kh < KHV; ++kh) {\n int ih = oh - kh;\n const float* xp = x + xC + ((long long)id * H + ih) * W + ow;\n const float* wp = w + wC + (kd * KHV + kh) * KWV;\n #pragma unroll\n for (int kw = 0; kw < KWV; ++kw) {\n float xv = __ldg(xp - kw);\n int kk = kw;\n a0 = fmaf(xv, __ldg(wp + 0 * KVOL + kk), a0);\n a1 = fmaf(xv, __ldg(wp + 1 * KVOL + kk), a1);\n a2 = fmaf(xv, __ldg(wp + 2 * KVOL + kk), a2);\n a3 = fmaf(xv, __ldg(wp + 3 * KVOL + kk), a3);\n a4 = fmaf(xv, __ldg(wp + 4 * KVOL + kk), a4);\n a5 = fmaf(xv, __ldg(wp + 5 * KVOL + kk), a5);\n a6 = fmaf(xv, __ldg(wp + 6 * KVOL + kk), a6);\n a7 = fmaf(xv, __ldg(wp + 7 * KVOL + kk), a7);\n }\n }\n }\n }\n\n long long ybase = ((long long)n * Cout * Dout * Hout * Wout) + ((long long)co * Dout + od) * Hout * Wout + (long long)oh * Wout + ow;\n long long cstride = (long long)Dout * Hout * Wout;\n y[ybase + 0 * cstride] = a0;\n y[ybase + 1 * cstride] = a1;\n y[ybase + 2 * cstride] = a2;\n y[ybase + 3 * cstride] = a3;\n y[ybase + 4 * cstride] = a4;\n y[ybase + 5 * cstride] = a5;\n y[ybase + 6 * cstride] = a6;\n y[ybase + 7 * cstride] = a7;\n}\n\n__global__ __launch_bounds__(128, 2)\nvoid ct3d_default_edge8_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ b,\n float* __restrict__ y,\n int N, int Cin, int D, int H, int W,\n int Cout, int Dout, int Hout, int Wout,\n int Dint, int Hint, int Wint,\n int has_bias) {\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long spatial = (long long)Dout * Hout * Wout;\n long long total = (long long)N * (Cout >> 3) * spatial;\n if (tid >= total) return;\n\n int sp = (int)(tid % spatial);\n int cb = (int)((tid / spatial) % (Cout >> 3));\n int n = (int)(tid / (((long long)(Cout >> 3)) * spatial));\n int ow = sp % Wout;\n int t = sp / Wout;\n int oh = t % Hout;\n int od = t / Hout;\n\n if (od >= KDV-1 && od < D && oh >= KHV-1 && oh < H && ow >= KWV-1 && ow < W) return;\n\n int co = cb << 3;\n float a0 = has_bias ? __ldg(b + co + 0) : 0.0f;\n float a1 = has_bias ? __ldg(b + co + 1) : 0.0f;\n float a2 = has_bias ? __ldg(b + co + 2) : 0.0f;\n float a3 = has_bias ? __ldg(b + co + 3) : 0.0f;\n float a4 = has_bias ? __ldg(b + co + 4) : 0.0f;\n float a5 = has_bias ? __ldg(b + co + 5) : 0.0f;\n float a6 = has_bias ? __ldg(b + co + 6) : 0.0f;\n float a7 = has_bias ? __ldg(b + co + 7) : 0.0f;\n\n const long long xN = (long long)n * Cin * D * H * W;\n for (int ci = 0; ci < Cin; ++ci) {\n const long long xC = xN + (long long)ci * D * H * W;\n const long long wC = (long long)ci * Cout * KVOL + (long long)co * KVOL;\n #pragma unroll\n for (int kd = 0; kd < KDV; ++kd) {\n int id = od - kd;\n if ((unsigned)id >= (unsigned)D) continue;\n #pragma unroll\n for (int kh = 0; kh < KHV; ++kh) {\n int ih = oh - kh;\n if ((unsigned)ih >= (unsigned)H) continue;\n const float* xp = x + xC + ((long long)id * H + ih) * W;\n const float* wp = w + wC + (kd * KHV + kh) * KWV;\n #pragma unroll\n for (int kw = 0; kw < KWV; ++kw) {\n int iw = ow - kw;\n if ((unsigned)iw >= (unsigned)W) continue;\n float xv = __ldg(xp + iw);\n a0 = fmaf(xv, __ldg(wp + 0 * KVOL + kw), a0);\n a1 = fmaf(xv, __ldg(wp + 1 * KVOL + kw), a1);\n a2 = fmaf(xv, __ldg(wp + 2 * KVOL + kw), a2);\n a3 = fmaf(xv, __ldg(wp + 3 * KVOL + kw), a3);\n a4 = fmaf(xv, __ldg(wp + 4 * KVOL + kw), a4);\n a5 = fmaf(xv, __ldg(wp + 5 * KVOL + kw), a5);\n a6 = fmaf(xv, __ldg(wp + 6 * KVOL + kw), a6);\n a7 = fmaf(xv, __ldg(wp + 7 * KVOL + kw), a7);\n }\n }\n }\n }\n long long ybase = ((long long)n * Cout * Dout * Hout * Wout) + ((long long)co * Dout + od) * Hout * Wout + (long long)oh * Wout + ow;\n long long cstride = (long long)Dout * Hout * Wout;\n y[ybase + 0 * cstride] = a0;\n y[ybase + 1 * cstride] = a1;\n y[ybase + 2 * cstride] = a2;\n y[ybase + 3 * cstride] = a3;\n y[ybase + 4 * cstride] = a4;\n y[ybase + 5 * cstride] = a5;\n y[ybase + 6 * cstride] = a6;\n y[ybase + 7 * cstride] = a7;\n}\n\n__global__\nvoid ct3d_generic_kernel(const float* __restrict__ x, const float* __restrict__ w,\n const float* __restrict__ b, float* __restrict__ y,\n int N, int Cin, int D, int H, int W,\n int Cout, int KD, int KH, int KW,\n int Dout, int Hout, int Wout,\n int sD, int sH, int sW, int pD, int pH, int pW,\n int groups, int has_bias) {\n long long tid = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long total = (long long)N * Cout * Dout * Hout * Wout;\n if (tid >= total) return;\n int ow = tid % Wout;\n long long q = tid / Wout;\n int oh = q % Hout; q /= Hout;\n int od = q % Dout; q /= Dout;\n int co = q % Cout;\n int n = q / Cout;\n int cpg_in = Cin / groups;\n int cpg_out = Cout / groups;\n int g = co / cpg_out;\n int co_g = co - g * cpg_out;\n float acc = has_bias ? __ldg(b + co) : 0.0f;\n for (int ci0 = 0; ci0 < cpg_in; ++ci0) {\n int ci = g * cpg_in + ci0;\n for (int kd = 0; kd < KD; ++kd) {\n int td = od + pD - kd;\n if (td % sD) continue;\n int id = td / sD;\n if ((unsigned)id >= (unsigned)D) continue;\n for (int kh = 0; kh < KH; ++kh) {\n int th = oh + pH - kh;\n if (th % sH) continue;\n int ih = th / sH;\n if ((unsigned)ih >= (unsigned)H) continue;\n for (int kw = 0; kw < KW; ++kw) {\n int tw = ow + pW - kw;\n if (tw % sW) continue;\n int iw = tw / sW;\n if ((unsigned)iw >= (unsigned)W) continue;\n float xv = __ldg(x + ((long long)n*Cin*D*H*W + (long long)ci*D*H*W + ((long long)id*H+ih)*W + iw));\n float wv = __ldg(w + ((((long long)ci*cpg_out + co_g)*KD + kd)*KH + kh)*KW + kw);\n acc = fmaf(xv, wv, acc);\n }\n }\n }\n }\n y[tid] = acc;\n}\n\ntorch::Tensor conv_transpose3d_forward(torch::Tensor x, torch::Tensor w, torch::Tensor b,\n int64_t sD64, int64_t sH64, int64_t sW64,\n int64_t pD64, int64_t pH64, int64_t pW64,\n int64_t opD64, int64_t opH64, int64_t opW64,\n int64_t groups64, bool has_bias_bool) {\n\n int N = (int)x.size(0), Cin = (int)x.size(1), D = (int)x.size(2), H = (int)x.size(3), W = (int)x.size(4);\n int Cout_per_g = (int)w.size(1);\n int KD = (int)w.size(2), KH = (int)w.size(3), KW = (int)w.size(4);\n int groups = (int)groups64;\n int Cout = Cout_per_g * groups;\n int sD=(int)sD64, sH=(int)sH64, sW=(int)sW64, pD=(int)pD64, pH=(int)pH64, pW=(int)pW64;\n int Dout = (D - 1) * sD - 2 * pD + KD + (int)opD64;\n int Hout = (H - 1) * sH - 2 * pH + KH + (int)opH64;\n int Wout = (W - 1) * sW - 2 * pW + KW + (int)opW64;\n auto y = torch::empty({N, Cout, Dout, Hout, Wout}, x.options());\n const float* bp = has_bias_bool ? b.data_ptr<float>() : nullptr;\n int has_bias = has_bias_bool ? 1 : 0;\n int threads = 128;\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n\n if (x.scalar_type() == at::kFloat && w.scalar_type() == at::kFloat &&\n groups == 1 && sD == 1 && sH == 1 && sW == 1 && pD == 0 && pH == 0 && pW == 0 &&\n KD == 3 && KH == 5 && KW == 5 && (Cout % 8) == 0) {\n int Dint = D - KD + 1;\n int Hint = H - KH + 1;\n int Wint = W - KW + 1;\n if (Dint > 0 && Hint > 0 && Wint > 0) {\n long long total_int = (long long)N * (Cout >> 3) * Dint * Hint * Wint;\n ct3d_default_interior8_kernel<<<(unsigned)((total_int + threads - 1) / threads), threads, 0, stream>>>(\n x.data_ptr<float>(), w.data_ptr<float>(), bp, y.data_ptr<float>(),\n N, Cin, D, H, W, Cout, Dout, Hout, Wout, Dint, Hint, Wint, has_bias);\n }\n long long total_edge = (long long)N * (Cout >> 3) * Dout * Hout * Wout;\n ct3d_default_edge8_kernel<<<(unsigned)((total_edge + threads - 1) / threads), threads, 0, stream>>>(\n x.data_ptr<float>(), w.data_ptr<float>(), bp, y.data_ptr<float>(),\n N, Cin, D, H, W, Cout, Dout, Hout, Wout, Dint, Hint, Wint, has_bias);\n } else {\n long long total = (long long)N * Cout * Dout * Hout * Wout;\n ct3d_generic_kernel<<<(unsigned)((total + threads - 1) / threads), threads, 0, stream>>>(\n x.data_ptr<float>(), w.data_ptr<float>(), bp, y.data_ptr<float>(),\n N, Cin, D, H, W, Cout, KD, KH, KW, Dout, Hout, Wout,\n sD, sH, sW, pD, pH, pW, groups, has_bias);\n }\n C10_CUDA_KERNEL_LAUNCH_CHECK();\n return y;\n}\n'''\n\n_ext = load_inline(\n name='ct3d_ext_fast_default_v1',\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=['conv_transpose3d_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, in_channels: int, out_channels: int, kernel_size: tuple, stride: tuple = (1, 1, 1), padding: tuple = (0, 0, 0), output_padding: tuple = (0, 0, 0), groups: int = 1, bias: bool = False):\n super(ModelNew, self).__init__()\n self.conv_transpose3d = nn.ConvTranspose3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, output_padding=output_padding, groups=groups, bias=bias)\n self.stride = stride\n self.padding = padding\n self.output_padding = output_padding\n self.groups = groups\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n bias = self.conv_transpose3d.bias\n has_bias = bias is not None\n if bias is None:\n bias = torch.empty(0, device=x.device, dtype=x.dtype)\n return _ext.conv_transpose3d_forward(\n x, self.conv_transpose3d.weight, bias,\n int(self.stride[0]), int(self.stride[1]), int(self.stride[2]),\n int(self.padding[0]), int(self.padding[1]), int(self.padding[2]),\n int(self.output_padding[0]), int(self.output_padding[1]), int(self.output_padding[2]),\n int(self.groups), bool(has_bias)\n )\n"
}