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#include <ATen/cuda/CUDAContext.h>\n#include <c10/cuda/CUDAGuard.h>\n#include <cublas_v2.h>\n\nvoid linear_epilogue_launch(torch::Tensor y, torch::Tensor bias, double bias_scale, double negative_slope);\n\ntorch::Tensor fused_linear_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,\n double multiplier, double negative_slope) {\n TORCH_CHECK(x.is_cuda(), \"x must be CUDA\");\n TORCH_CHECK(weight.is_cuda(), \"weight must be CUDA\");\n TORCH_CHECK(bias.is_cuda(), \"bias must be CUDA\");\n TORCH_CHECK(x.scalar_type() == at::kFloat, \"only fp32 supported\");\n TORCH_CHECK(weight.scalar_type() == at::kFloat, \"only fp32 supported\");\n TORCH_CHECK(bias.scalar_type() == at::kFloat, \"only fp32 supported\");\n TORCH_CHECK(x.dim() == 2 && weight.dim() == 2 && bias.dim() == 1, \"bad dimensions\");\n TORCH_CHECK(x.is_contiguous() && weight.is_contiguous() && bias.is_contiguous(), \"inputs must be contiguous\");\n\n const int64_t M64 = x.size(0);\n const int64_t K64 = x.size(1);\n const int64_t N64 = weight.size(0);\n TORCH_CHECK(weight.size(1) == K64 && bias.size(0) == N64, \"incompatible dimensions\");\n TORCH_CHECK(M64 <= INT_MAX && N64 <= INT_MAX && K64 <= INT_MAX, \"dimensions too large\");\n const int M = (int)M64;\n const int N = (int)N64;\n const int K = (int)K64;\n\n c10::cuda::CUDAGuard device_guard(x.device());\n auto y = torch::empty({M64, N64}, x.options());\n\n // Row-major Y = X * W^T. Interpreting the same storage as column-major,\n // compute Y^T (N x M) = W (N x K) * X^T (K x M), where W is obtained as\n // transpose of the column-major K x N view of the row-major weight tensor.\n cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle();\n const float alpha = static_cast<float>(multiplier);\n const float beta = 0.0f;\n cublasStatus_t stat = cublasSgemm(handle,\n CUBLAS_OP_T, CUBLAS_OP_N,\n N, M, K,\n &alpha,\n weight.data_ptr<float>(), K,\n x.data_ptr<float>(), K,\n &beta,\n y.data_ptr<float>(), N);\n TORCH_CHECK(stat == CUBLAS_STATUS_SUCCESS, \"cublasSgemm failed\");\n\n linear_epilogue_launch(y, bias, multiplier, negative_slope);\n return y;\n}\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\n__global__ void epilogue_scalar_kernel(float* __restrict__ y,\n const float* __restrict__ bias,\n long long total,\n int n,\n float bias_scale,\n float negative_slope) {\n long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long stride = (long long)blockDim.x * gridDim.x;\n for (; idx < total; idx += stride) {\n int col = (int)(idx % n);\n float v = y[idx] + bias[col] * bias_scale;\n y[idx] = (v >= 0.0f) ? v : v * negative_slope;\n }\n}\n\n__global__ void epilogue_vec4_kernel(float* __restrict__ y,\n const float* __restrict__ bias,\n long long vec_total,\n int n,\n float bias_scale,\n float negative_slope) {\n long long vidx = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long stride = (long long)blockDim.x * gridDim.x;\n float4* __restrict__ y4 = reinterpret_cast<float4*>(y);\n for (; vidx < vec_total; vidx += stride) {\n long long base = vidx * 4LL;\n int col = (int)(base % n);\n float4 v = y4[vidx];\n // Fast path used by the benchmark dimensions (n is a multiple of 4).\n float b0 = bias[col + 0] * bias_scale;\n float b1 = bias[col + 1] * bias_scale;\n float b2 = bias[col + 2] * bias_scale;\n float b3 = bias[col + 3] * bias_scale;\n v.x += b0; v.y += b1; v.z += b2; v.w += b3;\n v.x = (v.x >= 0.0f) ? v.x : v.x * negative_slope;\n v.y = (v.y >= 0.0f) ? v.y : v.y * negative_slope;\n v.z = (v.z >= 0.0f) ? v.z : v.z * negative_slope;\n v.w = (v.w >= 0.0f) ? v.w : v.w * negative_slope;\n y4[vidx] = v;\n }\n}\n\nvoid linear_epilogue_launch(torch::Tensor y, torch::Tensor bias, double bias_scale, double negative_slope) {\n const long long total = y.numel();\n const int n = (int)y.size(1);\n const int threads = 256;\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n float bs = static_cast<float>(bias_scale);\n float ns = static_cast<float>(negative_slope);\n\n // Vectorized path for aligned, four-divisible rows. The supplied problem has\n // N=8192 and CUDA allocations are suitably aligned.\n if ((total % 4LL) == 0 && (n % 4) == 0) {\n long long vt = total / 4LL;\n int blocks = (int)((vt + threads - 1) / threads);\n if (blocks > 65535) blocks = 65535;\n epilogue_vec4_kernel<<<blocks, threads, 0, stream>>>(y.data_ptr<float>(), bias.data_ptr<float>(), vt, n, bs, ns);\n } else {\n int blocks = (int)((total + threads - 1) / threads);\n if (blocks > 65535) blocks = 65535;\n epilogue_scalar_kernel<<<blocks, threads, 0, stream>>>(y.data_ptr<float>(), bias.data_ptr<float>(), total, n, bs, ns);\n }\n}\n'''\n\n_ext = load_inline(\n name=\"fused_linear_leakyrelu_ext_v1\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"fused_linear_forward\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"],\n extra_ldflags=[\"-lcublas\"],\n with_cuda=True,\n verbose=False,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self, in_features, out_features, multiplier, negative_slope):\n super(ModelNew, self).__init__()\n self.gemm = nn.Linear(in_features, out_features)\n self.multiplier = multiplier\n self.negative_slope = negative_slope\n\n def forward(self, x):\n return _ext.fused_linear_forward(x, self.gemm.weight, self.gemm.bias,\n float(self.multiplier), float(self.negative_slope))\n"
}