call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncpp_sources = r'''\n#include <torch/extension.h>\nvoid lt_bias_act_launcher(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, torch::Tensor y, double multiplier, double negative_slope);\ntorch::Tensor fused_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, double multiplier, double negative_slope) {\n auto y = torch::empty({x.size(0), weight.size(0)}, x.options());\n lt_bias_act_launcher(x, weight, bias, y, multiplier, negative_slope);\n return y;\n}\n'''\n\ncuda_sources = r'''\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cublasLt.h>\n\n__global__ void act_kernel(float* __restrict__ y, long long n, float mult, float slope) {\n long long idx4 = (long long)blockIdx.x * blockDim.x + threadIdx.x;\n long long i = idx4 * 4;\n if (i + 3 < n) {\n float4 v = *reinterpret_cast<float4*>(y + i);\n v.x *= mult; v.y *= mult; v.z *= mult; v.w *= mult;\n v.x = v.x >= 0.0f ? v.x : v.x * slope;\n v.y = v.y >= 0.0f ? v.y : v.y * slope;\n v.z = v.z >= 0.0f ? v.z : v.z * slope;\n v.w = v.w >= 0.0f ? v.w : v.w * slope;\n *reinterpret_cast<float4*>(y + i) = v;\n } else {\n for (; i < n; ++i) { float a = y[i] * mult; y[i] = a >= 0.0f ? a : a * slope; }\n }\n}\n\nstatic cublasLtHandle_t get_lt_handle() {\n static cublasLtHandle_t handle = nullptr;\n if (handle == nullptr) cublasLtCreate(&handle);\n return handle;\n}\n\nvoid lt_bias_act_launcher(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, torch::Tensor y, double multiplier, double negative_slope) {\n int m = (int)x.size(0), k = (int)x.size(1), n = (int)weight.size(0);\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n cublasLtHandle_t ltHandle = get_lt_handle();\n\n cublasLtMatmulDesc_t opDesc;\n cublasLtMatrixLayout_t aDesc, bDesc, cDesc, dDesc;\n cublasLtMatmulDescCreate(&opDesc, CUBLAS_COMPUTE_32F, CUDA_R_32F);\n cublasOperation_t transa = CUBLAS_OP_T, transb = CUBLAS_OP_N;\n cublasLtMatmulDescSetAttribute(opDesc, CUBLASLT_MATMUL_DESC_TRANSA, &transa, sizeof(transa));\n cublasLtMatmulDescSetAttribute(opDesc, CUBLASLT_MATMUL_DESC_TRANSB, &transb, sizeof(transb));\n cublasLtEpilogue_t epi = CUBLASLT_EPILOGUE_BIAS;\n cublasLtMatmulDescSetAttribute(opDesc, CUBLASLT_MATMUL_DESC_EPILOGUE, &epi, sizeof(epi));\n const void* bias_ptr = bias.data_ptr<float>();\n cublasLtMatmulDescSetAttribute(opDesc, CUBLASLT_MATMUL_DESC_BIAS_POINTER, &bias_ptr, sizeof(bias_ptr));\n\n cublasLtMatrixLayoutCreate(&aDesc, CUDA_R_32F, k, n, k);\n cublasLtMatrixLayoutCreate(&bDesc, CUDA_R_32F, k, m, k);\n cublasLtMatrixLayoutCreate(&cDesc, CUDA_R_32F, n, m, n);\n cublasLtMatrixLayoutCreate(&dDesc, CUDA_R_32F, n, m, n);\n\n cublasLtMatmulPreference_t pref;\n cublasLtMatmulPreferenceCreate(&pref);\n size_t workspaceSize = 0;\n cublasLtMatmulPreferenceSetAttribute(pref, CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES, &workspaceSize, sizeof(workspaceSize));\n cublasLtMatmulHeuristicResult_t heuristic;\n int returned = 0;\n cublasLtMatmulAlgoGetHeuristic(ltHandle, opDesc, aDesc, bDesc, cDesc, dDesc, pref, 1, &heuristic, &returned);\n const float alpha = 1.0f, beta = 0.0f;\n cublasLtMatmul(ltHandle, opDesc, &alpha,\n weight.data_ptr<float>(), aDesc,\n x.data_ptr<float>(), bDesc,\n &beta,\n y.data_ptr<float>(), cDesc,\n y.data_ptr<float>(), dDesc,\n returned ? &heuristic.algo : nullptr,\n nullptr, 0, stream);\n\n cublasLtMatmulPreferenceDestroy(pref);\n cublasLtMatrixLayoutDestroy(dDesc); cublasLtMatrixLayoutDestroy(cDesc); cublasLtMatrixLayoutDestroy(bDesc); cublasLtMatrixLayoutDestroy(aDesc);\n cublasLtMatmulDescDestroy(opDesc);\n\n long long total = (long long)m * n;\n int threads = 256;\n int blocks = (int)((total + threads * 4LL - 1) / (threads * 4LL));\n act_kernel<<<blocks, threads, 0, stream>>>(y.data_ptr<float>(), total, (float)multiplier, (float)negative_slope);\n}\n'''\n\n_ext = load_inline(name='gemm_fused_leaky_ext_v3', cpp_sources=cpp_sources, cuda_sources=cuda_sources, functions=['fused_forward'], extra_cflags=['-O3'], extra_cuda_cflags=['-O3'], verbose=False)\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 = float(multiplier)\n self.negative_slope = float(negative_slope)\n def forward(self, x):\n return _ext.fused_forward(x, self.gemm.weight, self.gemm.bias, self.multiplier, self.negative_slope)\n"
}