call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n_src = r'''\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n\n__global__ __launch_bounds__(256, 2)\nvoid deconv1d_fixed_vec8_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ bias,\n float* __restrict__ y,\n int B, int L, int Lout, int has_bias) {\n // Specialized for Cin=32, Cout=64, K=5, stride=1, padding=0, dilation=3.\n // One thread computes 8 output channels for one (batch, position). The\n // linearization keeps positions contiguous, so each of the 8 stores is\n // coalesced across the warp.\n const int groups = 8; // 64 / 8\n long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n const long long total = (long long)B * groups * Lout;\n if (idx >= total) return;\n\n int l = (int)(idx % Lout);\n long long t = idx / Lout;\n int g = (int)(t % groups);\n int b = (int)(t / groups);\n int oc = g * 8;\n\n float a0, a1, a2, a3, a4, a5, a6, a7;\n if (has_bias) {\n a0 = __ldg(bias + oc + 0); a1 = __ldg(bias + oc + 1);\n a2 = __ldg(bias + oc + 2); a3 = __ldg(bias + oc + 3);\n a4 = __ldg(bias + oc + 4); a5 = __ldg(bias + oc + 5);\n a6 = __ldg(bias + oc + 6); a7 = __ldg(bias + oc + 7);\n } else {\n a0 = a1 = a2 = a3 = a4 = a5 = a6 = a7 = 0.0f;\n }\n\n #pragma unroll\n for (int ic = 0; ic < 32; ++ic) {\n const float* __restrict__ xp = x + ((b * 32 + ic) * L);\n const float* __restrict__ wp0 = w + (ic * 64 + oc) * 5;\n\n int il = l;\n if ((unsigned)il < (unsigned)L) {\n float xv = __ldg(xp + il);\n const float* wp = wp0 + 0;\n a0 = fmaf(xv, __ldg(wp + 0*5), a0);\n a1 = fmaf(xv, __ldg(wp + 1*5), a1);\n a2 = fmaf(xv, __ldg(wp + 2*5), a2);\n a3 = fmaf(xv, __ldg(wp + 3*5), a3);\n a4 = fmaf(xv, __ldg(wp + 4*5), a4);\n a5 = fmaf(xv, __ldg(wp + 5*5), a5);\n a6 = fmaf(xv, __ldg(wp + 6*5), a6);\n a7 = fmaf(xv, __ldg(wp + 7*5), a7);\n }\n il = l - 3;\n if ((unsigned)il < (unsigned)L) {\n float xv = __ldg(xp + il);\n const float* wp = wp0 + 1;\n a0 = fmaf(xv, __ldg(wp + 0*5), a0);\n a1 = fmaf(xv, __ldg(wp + 1*5), a1);\n a2 = fmaf(xv, __ldg(wp + 2*5), a2);\n a3 = fmaf(xv, __ldg(wp + 3*5), a3);\n a4 = fmaf(xv, __ldg(wp + 4*5), a4);\n a5 = fmaf(xv, __ldg(wp + 5*5), a5);\n a6 = fmaf(xv, __ldg(wp + 6*5), a6);\n a7 = fmaf(xv, __ldg(wp + 7*5), a7);\n }\n il = l - 6;\n if ((unsigned)il < (unsigned)L) {\n float xv = __ldg(xp + il);\n const float* wp = wp0 + 2;\n a0 = fmaf(xv, __ldg(wp + 0*5), a0);\n a1 = fmaf(xv, __ldg(wp + 1*5), a1);\n a2 = fmaf(xv, __ldg(wp + 2*5), a2);\n a3 = fmaf(xv, __ldg(wp + 3*5), a3);\n a4 = fmaf(xv, __ldg(wp + 4*5), a4);\n a5 = fmaf(xv, __ldg(wp + 5*5), a5);\n a6 = fmaf(xv, __ldg(wp + 6*5), a6);\n a7 = fmaf(xv, __ldg(wp + 7*5), a7);\n }\n il = l - 9;\n if ((unsigned)il < (unsigned)L) {\n float xv = __ldg(xp + il);\n const float* wp = wp0 + 3;\n a0 = fmaf(xv, __ldg(wp + 0*5), a0);\n a1 = fmaf(xv, __ldg(wp + 1*5), a1);\n a2 = fmaf(xv, __ldg(wp + 2*5), a2);\n a3 = fmaf(xv, __ldg(wp + 3*5), a3);\n a4 = fmaf(xv, __ldg(wp + 4*5), a4);\n a5 = fmaf(xv, __ldg(wp + 5*5), a5);\n a6 = fmaf(xv, __ldg(wp + 6*5), a6);\n a7 = fmaf(xv, __ldg(wp + 7*5), a7);\n }\n il = l - 12;\n if ((unsigned)il < (unsigned)L) {\n float xv = __ldg(xp + il);\n const float* wp = wp0 + 4;\n a0 = fmaf(xv, __ldg(wp + 0*5), a0);\n a1 = fmaf(xv, __ldg(wp + 1*5), a1);\n a2 = fmaf(xv, __ldg(wp + 2*5), a2);\n a3 = fmaf(xv, __ldg(wp + 3*5), a3);\n a4 = fmaf(xv, __ldg(wp + 4*5), a4);\n a5 = fmaf(xv, __ldg(wp + 5*5), a5);\n a6 = fmaf(xv, __ldg(wp + 6*5), a6);\n a7 = fmaf(xv, __ldg(wp + 7*5), a7);\n }\n }\n\n float* yp = y + (b * 64 + oc) * Lout + l;\n yp[0 * Lout] = a0; yp[1 * Lout] = a1;\n yp[2 * Lout] = a2; yp[3 * Lout] = a3;\n yp[4 * Lout] = a4; yp[5 * Lout] = a5;\n yp[6 * Lout] = a6; yp[7 * Lout] = a7;\n}\n\n__global__\nvoid deconv1d_generic_kernel(const float* __restrict__ x,\n const float* __restrict__ w,\n const float* __restrict__ bias,\n float* __restrict__ y,\n int B, int Cin, int L, int Cout, int K, int Lout,\n int stride, int padding, int dilation, int has_bias) {\n long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long total = (long long)B * Cout * Lout;\n if (idx >= total) return;\n int l = (int)(idx % Lout);\n long long t = idx / Lout;\n int oc = (int)(t % Cout);\n int b = (int)(t / Cout);\n float acc = has_bias ? __ldg(bias + oc) : 0.0f;\n for (int ic = 0; ic < Cin; ++ic) {\n for (int k = 0; k < K; ++k) {\n int num = l + padding - k * dilation;\n if (num % stride == 0) {\n int il = num / stride;\n if ((unsigned)il < (unsigned)L) {\n acc = fmaf(__ldg(x + (b * Cin + ic) * L + il),\n __ldg(w + (ic * Cout + oc) * K + k), acc);\n }\n }\n }\n }\n y[idx] = acc;\n}\n\ntorch::Tensor conv_transpose1d_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n int64_t stride, int64_t padding, int64_t dilation) {\n TORCH_CHECK(x.is_cuda(), \"x must be CUDA\");\n TORCH_CHECK(weight.is_cuda(), \"weight must be CUDA\");\n TORCH_CHECK(x.scalar_type() == torch::kFloat32, \"only float32 supported\");\n int B = (int)x.size(0);\n int Cin = (int)x.size(1);\n int L = (int)x.size(2);\n int Cout = (int)weight.size(1);\n int K = (int)weight.size(2);\n int Lout = (int)((L - 1) * stride - 2 * padding + dilation * (K - 1) + 1);\n auto y = torch::empty({B, Cout, Lout}, x.options());\n int has_bias = (bias.numel() != 0);\n const float* bp = has_bias ? bias.data_ptr<float>() : nullptr;\n\n if (Cin == 32 && Cout == 64 && K == 5 && stride == 1 && padding == 0 && dilation == 3) {\n const int threads = 256;\n long long total = (long long)B * 8 * Lout;\n int blocks = (int)((total + threads - 1) / threads);\n deconv1d_fixed_vec8_kernel<<<blocks, threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp,\n y.data_ptr<float>(), B, L, Lout, has_bias);\n } else {\n const int threads = 256;\n long long total = (long long)B * Cout * Lout;\n int blocks = (int)((total + threads - 1) / threads);\n deconv1d_generic_kernel<<<blocks, threads>>>(x.data_ptr<float>(), weight.data_ptr<float>(), bp,\n y.data_ptr<float>(), B, Cin, L, Cout, K, Lout,\n (int)stride, (int)padding, (int)dilation, has_bias);\n }\n return y;\n}\n'''\n\n_ext = load_inline(\n name='conv_transpose1d_dil3_ext',\n cpp_sources='',\n cuda_sources=_src,\n functions=['conv_transpose1d_forward'],\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: int, stride: int = 1, padding: int = 0, dilation: int = 1, bias: bool = False):\n super(ModelNew, self).__init__()\n self.conv1d_transpose = nn.ConvTranspose1d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation, bias=bias)\n self.stride = stride\n self.padding = padding\n self.dilation = dilation\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n b = self.conv1d_transpose.bias\n if b is None:\n b = torch.empty(0, device=x.device, dtype=x.dtype)\n return _ext.conv_transpose1d_forward(x, self.conv1d_transpose.weight, b, self.stride, self.padding, self.dilation)\n"
}