call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncpp_src = r'''\n#include <torch/extension.h>\n#include <vector>\n\ntorch::Tensor lstm_forward_cuda(torch::Tensor x, torch::Tensor h0, torch::Tensor c0,\n std::vector<torch::Tensor> wih, std::vector<torch::Tensor> whh,\n std::vector<torch::Tensor> bih, std::vector<torch::Tensor> bhh);\n\ntorch::Tensor lstm_forward(torch::Tensor x, torch::Tensor h0, torch::Tensor c0,\n std::vector<torch::Tensor> wih, std::vector<torch::Tensor> whh,\n std::vector<torch::Tensor> bih, std::vector<torch::Tensor> bhh) {\n return lstm_forward_cuda(x,h0,c0,wih,whh,bih,bhh);\n}\n'''\n\ncuda_src = r'''\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cooperative_groups.h>\n#include <vector>\nnamespace cg = cooperative_groups;\n\n#define CHECK_CUDA(x) TORCH_CHECK(x.is_cuda(), #x \" must be CUDA\")\n#define CHECK_CONTIGUOUS(x) TORCH_CHECK(x.is_contiguous(), #x \" must be contiguous\")\n\n__device__ __forceinline__ float sigmoidf_fast(float x) { return 1.0f / (1.0f + expf(-x)); }\n\n__global__ void lstm_coop_kernel(const float* __restrict__ x,\n const float* __restrict__ h0,\n const float* __restrict__ c0,\n float* __restrict__ h,\n float* __restrict__ c,\n const float** __restrict__ wih,\n const float** __restrict__ whh,\n const float** __restrict__ bih,\n const float** __restrict__ bhh,\n int B, int T, int I, int H, int L) {\n cg::grid_group grid = cg::this_grid();\n extern __shared__ float smem[];\n float* si = smem;\n float* sf = smem + blockDim.x;\n float* sg = smem + 2*blockDim.x;\n float* so = smem + 3*blockDim.x;\n int tid = threadIdx.x;\n int nout = B * H;\n\n // initialize h and c from h0/c0 in parallel\n int total = L * B * H;\n for (int idx = blockIdx.x*blockDim.x + tid; idx < total; idx += gridDim.x*blockDim.x) {\n h[idx] = h0[idx];\n c[idx] = c0[idx];\n }\n grid.sync();\n\n for (int t=0; t<T; ++t) {\n for (int l=0; l<L; ++l) {\n int K = (l == 0 ? I : H);\n const float* wi = wih[l];\n const float* wr = whh[l];\n const float* bi = bih[l];\n const float* br = bhh[l];\n for (int out = blockIdx.x; out < nout; out += gridDim.x) {\n int b = out / H;\n int j = out - b * H;\n float ai = 0.0f, af = 0.0f, ag = 0.0f, ao = 0.0f;\n // input contribution\n for (int k=tid; k<K; k += blockDim.x) {\n float xv;\n if (l == 0) {\n xv = x[(b*T + t)*I + k];\n } else {\n xv = h[((l-1)*B + b)*H + k];\n }\n ai += wi[(0*H + j)*K + k] * xv;\n af += wi[(1*H + j)*K + k] * xv;\n ag += wi[(2*H + j)*K + k] * xv;\n ao += wi[(3*H + j)*K + k] * xv;\n }\n // recurrent contribution\n const float* hp = h + ((l*B + b)*H);\n for (int k=tid; k<H; k += blockDim.x) {\n float hv = hp[k];\n ai += wr[(0*H + j)*H + k] * hv;\n af += wr[(1*H + j)*H + k] * hv;\n ag += wr[(2*H + j)*H + k] * hv;\n ao += wr[(3*H + j)*H + k] * hv;\n }\n si[tid]=ai; sf[tid]=af; sg[tid]=ag; so[tid]=ao;\n __syncthreads();\n for (int s=blockDim.x/2; s>0; s >>= 1) {\n if (tid < s) {\n si[tid] += si[tid+s]; sf[tid] += sf[tid+s];\n sg[tid] += sg[tid+s]; so[tid] += so[tid+s];\n }\n __syncthreads();\n }\n if (tid == 0) {\n float gi = si[0] + bi[0*H+j] + br[0*H+j];\n float gf = sf[0] + bi[1*H+j] + br[1*H+j];\n float gg = sg[0] + bi[2*H+j] + br[2*H+j];\n float go = so[0] + bi[3*H+j] + br[3*H+j];\n float iv = sigmoidf_fast(gi);\n float fv = sigmoidf_fast(gf);\n float gv = tanhf(gg);\n float ov = sigmoidf_fast(go);\n int idx = (l*B + b)*H + j;\n float cv = fv * c[idx] + iv * gv;\n c[idx] = cv;\n h[idx] = ov * tanhf(cv);\n }\n __syncthreads();\n }\n grid.sync();\n }\n }\n}\n\ntorch::Tensor lstm_forward_cuda(torch::Tensor x, torch::Tensor h0, torch::Tensor c0,\n std::vector<torch::Tensor> wih, std::vector<torch::Tensor> whh,\n std::vector<torch::Tensor> bih, std::vector<torch::Tensor> bhh) {\n CHECK_CUDA(x); CHECK_CUDA(h0); CHECK_CUDA(c0);\n x = x.contiguous(); h0 = h0.contiguous(); c0 = c0.contiguous();\n int B = x.size(0); int T = x.size(1); int I = x.size(2);\n int L = h0.size(0); int H = h0.size(2);\n TORCH_CHECK(L == (int)wih.size(), \"bad layers\");\n auto h = torch::empty_like(h0);\n auto c = torch::empty_like(c0);\n std::vector<const float*> hwih(L), hwhh(L), hbih(L), hbhh(L);\n for (int l=0;l<L;++l) {\n hwih[l] = wih[l].contiguous().data_ptr<float>();\n hwhh[l] = whh[l].contiguous().data_ptr<float>();\n hbih[l] = bih[l].contiguous().data_ptr<float>();\n hbhh[l] = bhh[l].contiguous().data_ptr<float>();\n }\n const float **dwih, **dwhh, **dbih, **dbhh;\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n cudaMallocAsync(&dwih, L*sizeof(float*), stream);\n cudaMallocAsync(&dwhh, L*sizeof(float*), stream);\n cudaMallocAsync(&dbih, L*sizeof(float*), stream);\n cudaMallocAsync(&dbhh, L*sizeof(float*), stream);\n cudaMemcpyAsync(dwih, hwih.data(), L*sizeof(float*), cudaMemcpyHostToDevice, stream);\n cudaMemcpyAsync(dwhh, hwhh.data(), L*sizeof(float*), cudaMemcpyHostToDevice, stream);\n cudaMemcpyAsync(dbih, hbih.data(), L*sizeof(float*), cudaMemcpyHostToDevice, stream);\n cudaMemcpyAsync(dbhh, hbhh.data(), L*sizeof(float*), cudaMemcpyHostToDevice, stream);\n int threads = 128;\n int dev; cudaGetDevice(&dev); cudaDeviceProp prop; cudaGetDeviceProperties(&prop, dev);\n int active=0; cudaOccupancyMaxActiveBlocksPerMultiprocessor(&active, lstm_coop_kernel, threads, 4*threads*sizeof(float));\n int blocks = active * prop.multiProcessorCount;\n void* args[] = {&x.data_ptr<float>(), &h0.data_ptr<float>(), &c0.data_ptr<float>(), &h.data_ptr<float>(), &c.data_ptr<float>(),\n &dwih, &dwhh, &dbih, &dbhh, &B, &T, &I, &H, &L};\n // cannot take address of temporary data_ptr directly in standard C++, use locals\n const float* xp = x.data_ptr<float>(); const float* h0p = h0.data_ptr<float>(); const float* c0p = c0.data_ptr<float>();\n float* hp = h.data_ptr<float>(); float* cp = c.data_ptr<float>();\n void* args2[] = {&xp, &h0p, &c0p, &hp, &cp, &dwih, &dwhh, &dbih, &dbhh, &B, &T, &I, &H, &L};\n cudaError_t err = cudaLaunchCooperativeKernel((void*)lstm_coop_kernel, blocks, threads, args2, 4*threads*sizeof(float), stream);\n TORCH_CHECK(err == cudaSuccess, cudaGetErrorString(err));\n cudaFreeAsync(dwih, stream); cudaFreeAsync(dwhh, stream); cudaFreeAsync(dbih, stream); cudaFreeAsync(dbhh, stream);\n return c;\n}\n'''\n\n_ext = load_inline(name='lstm_coop_ext_v1', cpp_sources=cpp_src, cuda_sources=cuda_src, functions=['lstm_forward'], extra_cuda_cflags=['-O3'], extra_cflags=['-O3'], verbose=False)\n\nclass ModelNew(nn.Module):\n def __init__(self, input_size, hidden_size, num_layers, output_size, dropout=0.0):\n super(ModelNew, self).__init__()\n self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, dropout=dropout, bidirectional=False)\n self.fc = nn.Linear(hidden_size, output_size)\n self.num_layers = num_layers\n def forward(self, x, h0, c0):\n wih=[]; whh=[]; bih=[]; bhh=[]\n for l in range(self.num_layers):\n wih.append(getattr(self.lstm, f'weight_ih_l{l}'))\n whh.append(getattr(self.lstm, f'weight_hh_l{l}'))\n bih.append(getattr(self.lstm, f'bias_ih_l{l}'))\n bhh.append(getattr(self.lstm, f'bias_hh_l{l}'))\n return _ext.lstm_forward(x, h0, c0, wih, whh, bih, bhh)\n"
}
result ← compile_kernel (fail)
failcompile_kernel
compile_kernel FAILED: RuntimeError.
RuntimeError: Error building extension 'lstm_coop_ext_v1': [1/3] /opt/conda/bin/x86_64-conda-linux-gnu-c++ -MMD -MF main.o.d -DTORCH_EXTENSION_NAME=lstm_coop_ext_v1 -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -fPIC -std=c++17 -O3 -c /scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/main.cpp -o main.o
[2/3] /usr/local/cuda/bin/nvcc -MD -MF cuda.cuda.o.d -ccbin /opt/conda/bin/x86_64-conda-linux-gnu-cc -DTORCH_EXTENSION_NAME=lstm_coop_ext_v1 -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -gencode=arch=compute_90,code=compute_90 -gencode=arch=compute_90,code=sm_90 --compiler-options '-fPIC' -O3 -std=c++17 -c /scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/cuda.cu -o cuda.cuda.o
FAILED: [code=2] cuda.cuda.o
/usr/local/cuda/bin/nvcc -MD -MF cuda.cuda.o.d -ccbin /opt/conda/bin/x86_64-conda-linux-gnu-cc -DTORCH_EXTENSION_NAME=lstm_coop_ext_v1 -DTORCH_API_INCLUDE_EXTENSION_H -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include -isystem /scratch/abaner/KernelBench/.venv/lib/python3.10/site-packages/torch/include/torch/csrc/api/include -isystem /usr/local/cuda/include -isystem /opt/conda/include/python3.10 -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__ --expt-relaxed-constexpr -gencode=arch=compute_90,code=compute_90 -gencode=arch=compute_90,code=sm_90 --compiler-options '-fPIC' -O3 -std=c++17 -c /scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/cuda.cu -o cuda.cuda.o
nvcc warning : incompatible redefinition for option 'compiler-bindir', the last value of this option was used
/scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/cuda.cu(125): error: namespace "at::cuda" has no member "getCurrentCUDAStream"
cudaStream_t stream = at::cuda::getCurrentCUDAStream();
^
/scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/cuda.cu(138): error: expression must be an lvalue or a function designator
void* args[] = {&x.data_ptr<float>(), &h0.data_ptr<float>(), &c0.data_ptr<float>(), &h.data_ptr<float>(), &c.data_ptr<float>(),
^
/scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/cuda.cu(138): error: expression must be an lvalue or a function designator
void* args[] = {&x.data_ptr<float>(), &h0.data_ptr<float>(), &c0.data_ptr<float>(), &h.data_ptr<float>(), &c.data_ptr<float>(),
^
/scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/cuda.cu(138): error: expression must be an lvalue or a function designator
void* args[] = {&x.data_ptr<float>(), &h0.data_ptr<float>(), &c0.data_ptr<float>(), &h.data_ptr<float>(), &c.data_ptr<float>(),
^
/scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/cuda.cu(138): error: expression must be an lvalue or a function designator
void* args[] = {&x.data_ptr<float>(), &h0.data_ptr<float>(), &c0.data_ptr<float>(), &h.data_ptr<float>(), &c.data_ptr<float>(),
^
/scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/cuda.cu(138): error: expression must be an lvalue or a function designator
void* args[] = {&x.data_ptr<float>(), &h0.data_ptr<float>(), &c0.data_ptr<float>(), &h.data_ptr<float>(), &c.data_ptr<float>(),
^
6 errors detected in the compilation of "/scratch/abaner/KernelBench/runs/l3_all_gpt/original/gpt-5.5-priority/level_3_problem_37_cache/k_c3653e994465/lstm_coop_ext_v1/cuda.cu".
ninja: build stopped: subcommand failed.