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>\ntorch::Tensor min_forward(torch::Tensor x, int64_t dim);\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#include <vector>\n\n__global__ __launch_bounds__(256)\nvoid min_dim1_kernel(const float* __restrict__ x, float* __restrict__ out,\n int B, int D1, int D2) {\n int col = blockIdx.x * blockDim.x + threadIdx.x;\n int b = blockIdx.y;\n if (col >= D2) return;\n\n int base = (b * D1) * D2 + col;\n float m = CUDART_INF_F;\n\n // Hot path for the benchmark shape. Unrolling removes a sizeable amount\n // of loop/control overhead while preserving the fully coalesced access\n // pattern across columns in each warp.\n if (D1 == 4096) {\n int idx = base;\n #pragma unroll 1\n for (int k = 0; k < 4096; k += 8) {\n float v0 = x[idx]; idx += D2;\n float v1 = x[idx]; idx += D2;\n float v2 = x[idx]; idx += D2;\n float v3 = x[idx]; idx += D2;\n float v4 = x[idx]; idx += D2;\n float v5 = x[idx]; idx += D2;\n float v6 = x[idx]; idx += D2;\n float v7 = x[idx]; idx += D2;\n m = fminf(m, v0);\n m = fminf(m, v1);\n m = fminf(m, v2);\n m = fminf(m, v3);\n m = fminf(m, v4);\n m = fminf(m, v5);\n m = fminf(m, v6);\n m = fminf(m, v7);\n }\n } else {\n int idx = base;\n for (int k = 0; k < D1; ++k) {\n m = fminf(m, x[idx]);\n idx += D2;\n }\n }\n out[b * D2 + col] = m;\n}\n\n__global__ __launch_bounds__(256)\nvoid min_dim0_kernel(const float* __restrict__ x, float* __restrict__ out,\n int B, int D1, int D2) {\n int col = blockIdx.x * blockDim.x + threadIdx.x;\n int d1 = blockIdx.y;\n if (col >= D2) return;\n int plane = D1 * D2;\n int idx = d1 * D2 + col;\n float m = CUDART_INF_F;\n for (int b = 0; b < B; ++b) {\n m = fminf(m, x[idx + b * plane]);\n }\n out[d1 * D2 + col] = m;\n}\n\n__global__ __launch_bounds__(256)\nvoid min_dim2_kernel(const float* __restrict__ x, float* __restrict__ out,\n int B, int D1, int D2) {\n extern __shared__ float smem[];\n int row = blockIdx.x; // row over flattened B*D1\n int tid = threadIdx.x;\n int base = row * D2;\n float m = CUDART_INF_F;\n for (int j = tid; j < D2; j += blockDim.x) {\n m = fminf(m, x[base + j]);\n }\n smem[tid] = m;\n __syncthreads();\n\n for (int s = blockDim.x >> 1; s > 32; s >>= 1) {\n if (tid < s) smem[tid] = fminf(smem[tid], smem[tid + s]);\n __syncthreads();\n }\n if (tid < 32) {\n float v = smem[tid];\n v = fminf(v, smem[tid + 32]);\n unsigned mask = 0xffffffffu;\n v = fminf(v, __shfl_down_sync(mask, v, 16));\n v = fminf(v, __shfl_down_sync(mask, v, 8));\n v = fminf(v, __shfl_down_sync(mask, v, 4));\n v = fminf(v, __shfl_down_sync(mask, v, 2));\n v = fminf(v, __shfl_down_sync(mask, v, 1));\n if (tid == 0) out[row] = v;\n }\n}\n\ntorch::Tensor min_forward(torch::Tensor x, int64_t dim) {\n TORCH_CHECK(x.is_cuda(), \"x must be a CUDA tensor\");\n TORCH_CHECK(x.scalar_type() == at::kFloat, \"only float32 is supported\");\n TORCH_CHECK(x.dim() == 3, \"expected a 3D tensor\");\n TORCH_CHECK(x.is_contiguous(), \"expected contiguous input\");\n\n if (dim < 0) dim += 3;\n TORCH_CHECK(dim >= 0 && dim < 3, \"invalid reduction dim\");\n\n const int B = (int)x.size(0);\n const int D1 = (int)x.size(1);\n const int D2 = (int)x.size(2);\n const float* xp = x.data_ptr<float>();\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n\n if (dim == 1) {\n auto out = torch::empty({B, D2}, x.options());\n dim3 block(256);\n dim3 grid((D2 + block.x - 1) / block.x, B);\n min_dim1_kernel<<<grid, block, 0, stream>>>(xp, out.data_ptr<float>(), B, D1, D2);\n C10_CUDA_KERNEL_LAUNCH_CHECK();\n return out;\n } else if (dim == 0) {\n auto out = torch::empty({D1, D2}, x.options());\n dim3 block(256);\n dim3 grid((D2 + block.x - 1) / block.x, D1);\n min_dim0_kernel<<<grid, block, 0, stream>>>(xp, out.data_ptr<float>(), B, D1, D2);\n C10_CUDA_KERNEL_LAUNCH_CHECK();\n return out;\n } else { // dim == 2\n auto out = torch::empty({B, D1}, x.options());\n dim3 block(256);\n dim3 grid(B * D1);\n min_dim2_kernel<<<grid, block, 256 * sizeof(float), stream>>>(xp, out.data_ptr<float>(), B, D1, D2);\n C10_CUDA_KERNEL_LAUNCH_CHECK();\n return out;\n }\n}\n\"\"\"\n\n_min_reduce_ext = load_inline(\n name=\"min_reduce_dim1_ext_fast\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"min_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, dim: int):\n super(ModelNew, self).__init__()\n self.dim = dim\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n return _min_reduce_ext.min_forward(x, self.dim)\n"
}