correct
14_GaussianProcessRBFKernel.py
started
2026-05-11T14:10:22Z
timingkernelbench.timing
kernel mean / median / std
0.06 / — / 0.01 μs
ref mean / median / std
0.12 / — / 0.02 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
3.70x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
warp stalls (top 5)nsight
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn
class Model(nn.Module):
"""
RBF (Squared Exponential) covariance matrix computation for Gaussian
Processes. Given two sets of input points, computes the full
kernel matrix K[i,j] = sigma^2 * exp(-||x_i - x_j||^2 / (2 * l^2)).
The dominant cost in GP inference and a natural target for fusion.
"""
def __init__(self, input_dim, lengthscale=1.0, variance=1.0):
super().__init__()
self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())
self.log_variance = nn.Parameter(torch.tensor(variance).log())
def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:
"""
Args:
X1: (B, N, D) – first set of inputs
X2: (B, M, D) – second set of inputs
Returns:
K: (B, N, M) – covariance matrix
"""
l2 = self.log_lengthscale.exp() ** 2
var = self.log_variance.exp()
# Squared distances via expansion
X1_sq = (X1 ** 2).sum(-1, keepdim=True) # (B, N, 1)
X2_sq = (X2 ** 2).sum(-1, keepdim=True) # (B, M, 1)
dist_sq = X1_sq - 2.0 * X1 @ X2.transpose(-1, -2) + X2_sq.transpose(-1, -2)
dist_sq = dist_sq.clamp(min=0.0)
return var * torch.exp(-0.5 * dist_sq / l2)
input_dim = 8
num_train = 512
num_test = 128
batch_size = 16
def get_inputs():
X1 = torch.randn(popcorn_pri.jitter_int(batch_size), popcorn_pri.jitter_int(num_train), input_dim)
X2 = torch.randn(popcorn_pri.jitter_int(batch_size), popcorn_pri.jitter_int(num_test), input_dim)
return [X1, X2]
def get_init_inputs():
return [input_dim]
turn 0404.3s turn · 3.3s LLM · 401.0s tools · tokens in=1,700 · out=212 · total=1,912
reasoning
Let me understand the task:
1. I need to replace the forward computation of a PyTorch model that computes RBF (Squared Exponential) covariance matrix for Gaussian Processes.
2. The model computes K[i,j] = sigma^2 * exp(-||x_i - x_j||^2 / (2 * l^2))
3. Input shapes: X1 is (B, N, D), X2 is (B, M, D)
4. Output shape: K is (B, N, M)
The computation involves:
1. Computing squared norms of X1 and X2
2. Computing pairwise squared distances via the formula: dist_sq = X1_sq - 2 * X1 @ X2^T + X2_sq^T
3. Clamping to non-negative values
4. Applying the RBF kernel: var * exp(-0.5 * dist_sq / l2)
Let me first check the GPU specs to understand what hardware I'm working with.
call → get_gpu_specs
callget_gpu_specs
{}
result ← get_gpu_specs (ok)
okget_gpu_specs
GPU specs for NVIDIA H100 80GB HBM3:
total memory (runtime): 79.1 GB
spec entry: H100
GPU Architecture: Hopper
GPU Memory: 80GB
Memory Bandwidth: 3.35 TB/s
FP64 TFLOPS: 34
FP64 Tensor Core TFLOPS: 67
FP32 TFLOPS: 67
TF32 Tensor Core TFLOPS: 989 with sparsity
BFLOAT16 Tensore Core TFLOPS: 1979 with sparsity
FP16 Tensor Core TFLOPS: 1979 with sparsity
FP8 Tensor Core TFLOPS: 3958 with sparsity
INT8 Tensor Core TOPS: 3958 with sparsity
Register File Size: 64K 32-bit registers per SM
Maximum number of registers per thread: 255
Maximum number of thread blocks per SM: 32
Shared memory capacity per SM: 228 KB
Maximum shared memory per thread block: 227 KB
turn 1 · 1 tool call(s) · 399.0s turn · 26.4s LLM · tokens in=1,948 · out=2,770 · total=4,718 · 10,976 chars (click to expand)
turn 1399.0s turn · 26.4s LLM · 372.6s tools · tokens in=1,948 · out=2,770 · total=4,718
reasoning
Now let me analyze the problem and write a custom CUDA kernel for RBF covariance matrix computation.
The computation is:
- X1: (B, N, D) - batch_size x num_train x input_dim
- X2: (B, M, D) - batch_size x num_test x input_dim
- Output: K: (B, N, M) - batch_size x num_train x num_test
The kernel formula is:
K[i,j] = sigma^2 * exp(-||x_i - x_j||^2 / (2 * l^2))
This can be computed as:
- X1_sq = sum(X1^2, dim=-1) -> (B, N, 1)
- X2_sq = sum(X2^2, dim=-1) -> (B, M, 1)
- dist_sq = X1_sq - 2 * (X1 @ X2.T) + X2_sq.T -> (B, N, M)
- K = variance * exp(-0.5 * dist_sq / l^2)
Given the dimensions (B=16, N=512, M=128, D=8), the output size is 16 * 512 * 128 = 1,048,576 elements.
I'll write a fused kernel that computes the squared distances and the exponential in one pass. Let me think about the tiling strategy:
For each batch element (b), we need to compute K[b, i, j] for i in [0, N) and j in [0, M).
A tiled approach could work where:
- Each thread block handles one batch and a tile of (N, M)
- Or each thread block handles multiple batch elements
Given the sizes (N=512, M=128), we could use:
- Block size of 128x128 for N x M tiles
- Or smaller tiles with batch parallelism
Actually, let me think more carefully. The inner dimension D=8 is small. We can compute the dot product and squared norms efficiently.
Let me design a kernel where:
1. Each thread block handles one (batch, i, j) output tile
2. Each thread computes one or more output elements K[b, i, j]
3. We use shared memory to cache X1 and X2 tiles
For D=8, we might not even need to tile the D dimension.
Let me write the kernel:
```python
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline
# Define CUDA kernel
cuda_source = """
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cmath>
// Tiled kernel for RBF covariance computation
// Each thread block handles a tile of the output matrix for one batch
template <typename scalar_t>
__global__ void rbf_kernel_forward(
const scalar_t* __restrict__ X1, // (B, N, D)
const scalar_t* __restrict__ X2, // (B, M, D)
scalar_t* __restrict__ K, // (B, N, M)
const scalar_t lengthscale_sq, // l^2
const scalar_t variance, // sigma^2
const int B, const int N, const int M, const int D
) {
// Batch and output position
const int batch = blockIdx.z;
const int tile_i = blockIdx.y; // tile index in N dimension
const int tile_j = blockIdx.x; // tile index in M dimension
const int tid = threadIdx.x;
const int TILE_SIZE = 16; // 16x16 tile
// Shared memory for X1 and X2 tiles
__shared__ scalar_t X1_tile[16][8]; // TILE_SIZE x D (D=8)
__shared__ scalar_t X2_tile[16][8]; // TILE_SIZE x D
// Output coordinates
const int i = tile_i * TILE_SIZE + (tid / 16);
const int j = tile_j * TILE_SIZE + (tid % 16);
// Load X1 tile (cooperative loading)
if (tid < 16 * 8) {
int load_i = tid / 8;
int load_d = tid % 8;
int global_i = tile_i * TILE_SIZE + load_i;
if (global_i < N) {
X1_tile[load_i][load_d] = X1[batch * N * D + global_i * D + load_d];
}
}
// Load X2 tile (cooperative loading)
// Need 16*8 = 128 threads to load one tile, but we have 256 threads
// ...
}
// Actually, let me think about this differently...
"""
I think I'm overcomplicating this. Let me start with a simpler approach:
1. Compute X1_sq and X2_sq in separate kernels or in PyTorch
2. Fuse the distance computation and exponential in the main kernel
Or even simpler, let's just fuse everything into one kernel where each thread computes one output element.
Actually, the biggest opportunity for optimization is to avoid materializing the intermediate tensors and fuse the computation. Let me write a clean implementation.
Given the dimensions:
- N=512, M=128, D=8
- Each output element needs D=8 multiply-adds
Let me use a 2D tiling strategy where each thread block handles a TILE_M x TILE_N output tile and cooperatively loads X1 and X2 tiles into shared memory.
assistantLet me write a CUDA kernel for RBF covariance matrix computation. I'll start with a tiled approach using shared memory to maximize data reuse.
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n// Tiled kernel for RBF covariance computation\n// Each thread block handles a tile of the output matrix (N x M) for one batch\n// Uses shared memory to cache X1 and X2 tiles\n\ntemplate <typename scalar_t, int TILE_N, int TILE_M, int BLOCK_SIZE>\n__global__ void rbf_kernel_forward(\n const scalar_t* __restrict__ X1, // (B, N, D)\n const scalar_t* __restrict__ X2, // (B, M, D)\n scalar_t* __restrict__ K, // (B, N, M)\n const scalar_t lengthscale_sq, // l^2\n const scalar_t variance, // sigma^2\n const int B, const int N, const int M, const int D\n) {\n const int batch = blockIdx.z;\n const int tile_i_start = blockIdx.y * TILE_N;\n const int tile_j_start = blockIdx.x * TILE_M;\n \n // Shared memory for tiles (padded to avoid bank conflicts)\n __shared__ scalar_t X1_tile[TILE_N][9]; // TILE_N x D (D=8, pad to 9)\n __shared__ scalar_t X2_tile[TILE_M][9]; // TILE_M x D\n \n // Thread coordinates within tile\n const int tid = threadIdx.x;\n const int lane_i = tid / TILE_M; // row within tile\n const int lane_j = tid % TILE_M; // col within tile\n \n // Global coordinates\n const int i = tile_i_start + lane_i;\n const int j = tile_j_start + lane_j;\n \n // Cooperative loading of X1 tile\n // Need TILE_N * D loads, distributed among BLOCK_SIZE threads\n const int X1_loads = TILE_N * D;\n for (int idx = tid; idx < X1_loads; idx += BLOCK_SIZE) {\n int row = idx / D;\n int col = idx % D;\n int global_row = tile_i_start + row;\n if (global_row < N) {\n X1_tile[row][col] = X1[batch * N * D + global_row * D + col];\n } else {\n X1_tile[row][col] = static_cast<scalar_t>(0);\n }\n }\n \n // Cooperative loading of X2 tile\n const int X2_loads = TILE_M * D;\n for (int idx = tid; idx < X2_loads; idx += BLOCK_SIZE) {\n int row = idx / D;\n int col = idx % D;\n int global_row = tile_j_start + row;\n if (global_row < M) {\n X2_tile[row][col] = X2[batch * M * D + global_row * D + col];\n } else {\n X2_tile[row][col] = static_cast<scalar_t>(0);\n }\n }\n \n __syncthreads();\n \n // Compute squared norms for the row/column this thread handles\n scalar_t x1_sq = static_cast<scalar_t>(0);\n scalar_t x2_sq = static_cast<scalar_t>(0);\n \n if (lane_i < TILE_N && lane_j < TILE_M) {\n for (int d = 0; d < D; d++) {\n x1_sq += X1_tile[lane_i][d] * X1_tile[lane_i][d];\n x2_sq += X2_tile[lane_j][d] * X2_tile[lane_j][d];\n }\n }\n \n // Compute dot product\n scalar_t dot = static_cast<scalar_t>(0);\n if (lane_i < TILE_N && lane_j < TILE_M) {\n for (int d = 0; d < D; d++) {\n dot += X1_tile[lane_i][d] * X2_tile[lane_j][d];\n }\n }\n \n // Compute output\n if (i < N && j < M) {\n scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;\n // Clamp to 0\n dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);\n // RBF kernel\n scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;\n K[batch * N * M + i * M + j] = variance * exp(exp_arg);\n }\n}\n\ntorch::Tensor rbf_forward_cuda(\n torch::Tensor X1,\n torch::Tensor X2,\n double lengthscale_sq,\n double variance\n) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n \n auto K = torch::empty({B, N, M}, X1.options());\n \n const int TILE_N = 16;\n const int TILE_M = 16;\n const int BLOCK_SIZE = 256;\n \n const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);\n const dim3 block(BLOCK_SIZE);\n \n AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), \"rbf_kernel_forward\", ([&] {\n rbf_kernel_forward<scalar_t, TILE_N, TILE_M, BLOCK_SIZE><<<grid, block>>>(\n X1.data_ptr<scalar_t>(),\n X2.data_ptr<scalar_t>(),\n K.data_ptr<scalar_t>(),\n static_cast<scalar_t>(lengthscale_sq),\n static_cast<scalar_t>(variance),\n B, N, M, D\n );\n }));\n \n return K;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);\n\"\"\"\n\nrbf_module = load_inline(\n name='rbf_cuda',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['rbf_forward_cuda'],\n extra_cuda_cflags=['-O3'],\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n self.input_dim = input_dim\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n \n # Ensure inputs are contiguous\n X1 = X1.contiguous()\n X2 = X2.contiguous()\n \n return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 2 · 1 tool call(s) · 139.2s turn · 12.9s LLM · tokens in=3,543 · out=1,566 · total=5,109 · 6,696 chars (click to expand)
turn 2139.2s turn · 12.9s LLM · 126.3s tools · tokens in=3,543 · out=1,566 · total=5,109
reasoning
Good, compilation succeeded. Now let me check correctness.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n// Tiled kernel for RBF covariance computation\n// Each thread block handles a tile of the output matrix (N x M) for one batch\n// Uses shared memory to cache X1 and X2 tiles\n\ntemplate <typename scalar_t, int TILE_N, int TILE_M, int BLOCK_SIZE>\n__global__ void rbf_kernel_forward(\n const scalar_t* __restrict__ X1, // (B, N, D)\n const scalar_t* __restrict__ X2, // (B, M, D)\n scalar_t* __restrict__ K, // (B, N, M)\n const scalar_t lengthscale_sq, // l^2\n const scalar_t variance, // sigma^2\n const int B, const int N, const int M, const int D\n) {\n const int batch = blockIdx.z;\n const int tile_i_start = blockIdx.y * TILE_N;\n const int tile_j_start = blockIdx.x * TILE_M;\n \n // Shared memory for tiles (padded to avoid bank conflicts)\n __shared__ scalar_t X1_tile[TILE_N][9]; // TILE_N x D (D=8, pad to 9)\n __shared__ scalar_t X2_tile[TILE_M][9]; // TILE_M x D\n \n // Thread coordinates within tile\n const int tid = threadIdx.x;\n const int lane_i = tid / TILE_M; // row within tile\n const int lane_j = tid % TILE_M; // col within tile\n \n // Global coordinates\n const int i = tile_i_start + lane_i;\n const int j = tile_j_start + lane_j;\n \n // Cooperative loading of X1 tile\n // Need TILE_N * D loads, distributed among BLOCK_SIZE threads\n const int X1_loads = TILE_N * D;\n for (int idx = tid; idx < X1_loads; idx += BLOCK_SIZE) {\n int row = idx / D;\n int col = idx % D;\n int global_row = tile_i_start + row;\n if (global_row < N) {\n X1_tile[row][col] = X1[batch * N * D + global_row * D + col];\n } else {\n X1_tile[row][col] = static_cast<scalar_t>(0);\n }\n }\n \n // Cooperative loading of X2 tile\n const int X2_loads = TILE_M * D;\n for (int idx = tid; idx < X2_loads; idx += BLOCK_SIZE) {\n int row = idx / D;\n int col = idx % D;\n int global_row = tile_j_start + row;\n if (global_row < M) {\n X2_tile[row][col] = X2[batch * M * D + global_row * D + col];\n } else {\n X2_tile[row][col] = static_cast<scalar_t>(0);\n }\n }\n \n __syncthreads();\n \n // Compute squared norms for the row/column this thread handles\n scalar_t x1_sq = static_cast<scalar_t>(0);\n scalar_t x2_sq = static_cast<scalar_t>(0);\n \n if (lane_i < TILE_N && lane_j < TILE_M) {\n for (int d = 0; d < D; d++) {\n x1_sq += X1_tile[lane_i][d] * X1_tile[lane_i][d];\n x2_sq += X2_tile[lane_j][d] * X2_tile[lane_j][d];\n }\n }\n \n // Compute dot product\n scalar_t dot = static_cast<scalar_t>(0);\n if (lane_i < TILE_N && lane_j < TILE_M) {\n for (int d = 0; d < D; d++) {\n dot += X1_tile[lane_i][d] * X2_tile[lane_j][d];\n }\n }\n \n // Compute output\n if (i < N && j < M) {\n scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;\n // Clamp to 0\n dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);\n // RBF kernel\n scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;\n K[batch * N * M + i * M + j] = variance * exp(exp_arg);\n }\n}\n\ntorch::Tensor rbf_forward_cuda(\n torch::Tensor X1,\n torch::Tensor X2,\n double lengthscale_sq,\n double variance\n) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n \n auto K = torch::empty({B, N, M}, X1.options());\n \n const int TILE_N = 16;\n const int TILE_M = 16;\n const int BLOCK_SIZE = 256;\n \n const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);\n const dim3 block(BLOCK_SIZE);\n \n AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), \"rbf_kernel_forward\", ([&] {\n rbf_kernel_forward<scalar_t, TILE_N, TILE_M, BLOCK_SIZE><<<grid, block>>>(\n X1.data_ptr<scalar_t>(),\n X2.data_ptr<scalar_t>(),\n K.data_ptr<scalar_t>(),\n static_cast<scalar_t>(lengthscale_sq),\n static_cast<scalar_t>(variance),\n B, N, M, D\n );\n }));\n \n return K;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);\n\"\"\"\n\nrbf_module = load_inline(\n name='rbf_cuda',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['rbf_forward_cuda'],\n extra_cuda_cflags=['-O3'],\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n self.input_dim = input_dim\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n \n # Ensure inputs are contiguous\n X1 = X1.contiguous()\n X2 = X2.contiguous()\n \n return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=8.64e-07 mean_abs_err=2.23e-09 max_rel_err=5.73e-06
turn 3 · 1 tool call(s) · 656.4s turn · 20.7s LLM · tokens in=5,155 · out=2,297 · total=7,452 · 9,750 chars (click to expand)
turn 3656.4s turn · 20.7s LLM · 635.7s tools · tokens in=5,155 · out=2,297 · total=7,452
reasoning
The correctness check passed. The numerical accuracy is well within tolerance. Now let me optimize the kernel further. The current approach uses 16x16 tiles with 256 threads per block. Let me think about optimizations:
1. The D dimension is only 8, and D=8 is hard-coded via padding in shared memory. I should make D a template parameter or use a loop.
2. Each thread computes just one output element. For N=512 and M=128, we have 65536 elements per batch, with 16 batches = 1,048,576 elements total. This is not huge.
3. The current tile size of 16x16 = 256 threads, with each thread computing 1 element.
4. Loading: TILE_N * D + TILE_M * D = 16*8 + 16*8 = 256 elements loaded from global memory per tile. Each element is used by 16 other threads, so we get good reuse.
5. Computation per thread: D=8 multiplications for x1_sq, D=8 for x2_sq, D=8 multiply-adds for dot. Then 1 exp. That's not very compute-heavy.
Let me try increasing the tile size and making each thread compute multiple outputs to better amortize memory traffic. Also, I can vectorize loads.
Actually, let me first try a different approach: process multiple D elements at once using vectorized loads, and have each thread compute multiple output elements. This can better amortize the memory traffic.
Let me also consider that the reference PyTorch implementation uses a different algorithm:
- X1_sq = (X1 ** 2).sum(-1) - shape (B, N, 1)
- X2_sq = (X2 ** 2).sum(-1) - shape (B, M, 1)
- dist_sq = X1_sq - 2 * X1 @ X2.T + X2_sq.T
The key insight is that PyTorch can use BLAS for the X1 @ X2.T matrix multiply, which is highly optimized. But it creates intermediate tensors. Our fused kernel should be faster by avoiding intermediate storage.
Let me try an optimized version with:
1. Larger tile size to increase shared memory reuse
2. Each thread handles multiple outputs
3. Vectorized memory access where possible
Actually, let me try a simpler optimization first: increase the tile size and use float4 loads for better memory throughput.
assistantThe kernel passes correctness. Let me try optimizing it with vectorized loads and better thread utilization. I'll use float4 for memory access and have each thread compute multiple output elements.
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n// Optimized RBF kernel with vectorized loads and multi-output per thread\n// D is fixed at 8, which is 2x float4\n\ntemplate <typename scalar_t, int TILE_N, int TILE_M, int VEC_SIZE>\n__global__ void rbf_kernel_forward_opt(\n const scalar_t* __restrict__ X1, // (B, N, D)\n const scalar_t* __restrict__ X2, // (B, M, D)\n scalar_t* __restrict__ K, // (B, N, M)\n const scalar_t lengthscale_sq, // l^2\n const scalar_t variance, // sigma^2\n const int B, const int N, const int M, const int D\n) {\n const int batch = blockIdx.z;\n const int tile_i_start = blockIdx.y * TILE_N;\n const int tile_j_start = blockIdx.x * TILE_M;\n \n // Shared memory: X1 norms and vectors, X2 norms and vectors\n __shared__ scalar_t X1_sq_cache[TILE_N]; // squared norms for X1 rows\n __shared__ scalar_t X2_sq_cache[TILE_M]; // squared norms for X2 rows\n __shared__ scalar_t X1_tile[TILE_N * 8]; // X1 vectors\n __shared__ scalar_t X2_tile[TILE_M * 8]; // X2 vectors\n \n const int tid = threadIdx.x;\n const int block_size = blockDim.x;\n \n // Cooperative loading and norm computation for X1\n for (int row = 0; row < TILE_N; row++) {\n int global_row = tile_i_start + row;\n scalar_t sq = static_cast<scalar_t>(0);\n scalar_t vec[8];\n \n // Load D=8 elements per row\n for (int d = tid; d < 8; d += block_size) {\n if (global_row < N) {\n vec[d] = X1[batch * N * 8 + global_row * 8 + d];\n } else {\n vec[d] = static_cast<scalar_t>(0);\n }\n X1_tile[row * 8 + d] = vec[d];\n sq += vec[d] * vec[d];\n }\n \n // Sum up across threads\n #pragma unroll\n for (int offset = block_size / 2; offset > 0; offset >>= 1) {\n sq += __shfl_down_sync(0xffffffff, sq, offset);\n }\n if (tid == 0) {\n X1_sq_cache[row] = sq;\n }\n }\n \n // Cooperative loading and norm computation for X2\n for (int row = 0; row < TILE_M; row++) {\n int global_row = tile_j_start + row;\n scalar_t sq = static_cast<scalar_t>(0);\n scalar_t vec[8];\n \n for (int d = tid; d < 8; d += block_size) {\n if (global_row < M) {\n vec[d] = X2[batch * M * 8 + global_row * 8 + d];\n } else {\n vec[d] = static_cast<scalar_t>(0);\n }\n X2_tile[row * 8 + d] = vec[d];\n sq += vec[d] * vec[d];\n }\n \n #pragma unroll\n for (int offset = block_size / 2; offset > 0; offset >>= 1) {\n sq += __shfl_down_sync(0xffffffff, sq, offset);\n }\n if (tid == 0) {\n X2_sq_cache[row] = sq;\n }\n }\n \n __syncthreads();\n \n // Each thread computes VEC_SIZE output elements\n // Threads are arranged in 2D: one per output in the tile\n const int total_outputs = TILE_N * TILE_M;\n for (int out_idx = tid; out_idx < total_outputs; out_idx += block_size) {\n const int lane_i = out_idx / TILE_M;\n const int lane_j = out_idx % TILE_M;\n \n const int i = tile_i_start + lane_i;\n const int j = tile_j_start + lane_j;\n \n if (i < N && j < M) {\n // Get precomputed squared norms\n scalar_t x1_sq = X1_sq_cache[lane_i];\n scalar_t x2_sq = X2_sq_cache[lane_j];\n \n // Compute dot product from shared memory\n scalar_t dot = static_cast<scalar_t>(0);\n for (int d = 0; d < 8; d++) {\n dot += X1_tile[lane_i * 8 + d] * X2_tile[lane_j * 8 + d];\n }\n \n // Squared distance\n scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;\n dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);\n \n // RBF kernel\n scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;\n K[batch * N * M + i * M + j] = variance * exp(exp_arg);\n }\n }\n}\n\ntorch::Tensor rbf_forward_cuda(\n torch::Tensor X1,\n torch::Tensor X2,\n double lengthscale_sq,\n double variance\n) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n \n auto K = torch::empty({B, N, M}, X1.options());\n \n const int TILE_N = 32;\n const int TILE_M = 32;\n const int VEC_SIZE = 1;\n const int BLOCK_SIZE = 256;\n \n const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);\n const dim3 block(BLOCK_SIZE);\n \n AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), \"rbf_kernel_forward\", ([&] {\n rbf_kernel_forward_opt<scalar_t, TILE_N, TILE_M, VEC_SIZE><<<grid, block>>>(\n X1.data_ptr<scalar_t>(),\n X2.data_ptr<scalar_t>(),\n K.data_ptr<scalar_t>(),\n static_cast<scalar_t>(lengthscale_sq),\n static_cast<scalar_t>(variance),\n B, N, M, D\n );\n }));\n \n return K;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);\n\"\"\"\n\nrbf_module = load_inline(\n name='rbf_cuda_opt',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['rbf_forward_cuda'],\n extra_cuda_cflags=['-O3'],\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n self.input_dim = input_dim\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n \n # Ensure inputs are contiguous\n X1 = X1.contiguous()\n X2 = X2.contiguous()\n \n return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 4 · 1 tool call(s) · 110.9s turn · 14.3s LLM · tokens in=6,952 · out=1,761 · total=8,713 · 7,820 chars (click to expand)
turn 4110.9s turn · 14.3s LLM · 96.6s tools · tokens in=6,952 · out=1,761 · total=8,713
reasoning
Good, the optimized version compiled. Let me test correctness.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n// Optimized RBF kernel with vectorized loads and multi-output per thread\n// D is fixed at 8, which is 2x float4\n\ntemplate <typename scalar_t, int TILE_N, int TILE_M, int VEC_SIZE>\n__global__ void rbf_kernel_forward_opt(\n const scalar_t* __restrict__ X1, // (B, N, D)\n const scalar_t* __restrict__ X2, // (B, M, D)\n scalar_t* __restrict__ K, // (B, N, M)\n const scalar_t lengthscale_sq, // l^2\n const scalar_t variance, // sigma^2\n const int B, const int N, const int M, const int D\n) {\n const int batch = blockIdx.z;\n const int tile_i_start = blockIdx.y * TILE_N;\n const int tile_j_start = blockIdx.x * TILE_M;\n \n // Shared memory: X1 norms and vectors, X2 norms and vectors\n __shared__ scalar_t X1_sq_cache[TILE_N]; // squared norms for X1 rows\n __shared__ scalar_t X2_sq_cache[TILE_M]; // squared norms for X2 rows\n __shared__ scalar_t X1_tile[TILE_N * 8]; // X1 vectors\n __shared__ scalar_t X2_tile[TILE_M * 8]; // X2 vectors\n \n const int tid = threadIdx.x;\n const int block_size = blockDim.x;\n \n // Cooperative loading and norm computation for X1\n for (int row = 0; row < TILE_N; row++) {\n int global_row = tile_i_start + row;\n scalar_t sq = static_cast<scalar_t>(0);\n scalar_t vec[8];\n \n // Load D=8 elements per row\n for (int d = tid; d < 8; d += block_size) {\n if (global_row < N) {\n vec[d] = X1[batch * N * 8 + global_row * 8 + d];\n } else {\n vec[d] = static_cast<scalar_t>(0);\n }\n X1_tile[row * 8 + d] = vec[d];\n sq += vec[d] * vec[d];\n }\n \n // Sum up across threads\n #pragma unroll\n for (int offset = block_size / 2; offset > 0; offset >>= 1) {\n sq += __shfl_down_sync(0xffffffff, sq, offset);\n }\n if (tid == 0) {\n X1_sq_cache[row] = sq;\n }\n }\n \n // Cooperative loading and norm computation for X2\n for (int row = 0; row < TILE_M; row++) {\n int global_row = tile_j_start + row;\n scalar_t sq = static_cast<scalar_t>(0);\n scalar_t vec[8];\n \n for (int d = tid; d < 8; d += block_size) {\n if (global_row < M) {\n vec[d] = X2[batch * M * 8 + global_row * 8 + d];\n } else {\n vec[d] = static_cast<scalar_t>(0);\n }\n X2_tile[row * 8 + d] = vec[d];\n sq += vec[d] * vec[d];\n }\n \n #pragma unroll\n for (int offset = block_size / 2; offset > 0; offset >>= 1) {\n sq += __shfl_down_sync(0xffffffff, sq, offset);\n }\n if (tid == 0) {\n X2_sq_cache[row] = sq;\n }\n }\n \n __syncthreads();\n \n // Each thread computes VEC_SIZE output elements\n // Threads are arranged in 2D: one per output in the tile\n const int total_outputs = TILE_N * TILE_M;\n for (int out_idx = tid; out_idx < total_outputs; out_idx += block_size) {\n const int lane_i = out_idx / TILE_M;\n const int lane_j = out_idx % TILE_M;\n \n const int i = tile_i_start + lane_i;\n const int j = tile_j_start + lane_j;\n \n if (i < N && j < M) {\n // Get precomputed squared norms\n scalar_t x1_sq = X1_sq_cache[lane_i];\n scalar_t x2_sq = X2_sq_cache[lane_j];\n \n // Compute dot product from shared memory\n scalar_t dot = static_cast<scalar_t>(0);\n for (int d = 0; d < 8; d++) {\n dot += X1_tile[lane_i * 8 + d] * X2_tile[lane_j * 8 + d];\n }\n \n // Squared distance\n scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;\n dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);\n \n // RBF kernel\n scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;\n K[batch * N * M + i * M + j] = variance * exp(exp_arg);\n }\n }\n}\n\ntorch::Tensor rbf_forward_cuda(\n torch::Tensor X1,\n torch::Tensor X2,\n double lengthscale_sq,\n double variance\n) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n \n auto K = torch::empty({B, N, M}, X1.options());\n \n const int TILE_N = 32;\n const int TILE_M = 32;\n const int VEC_SIZE = 1;\n const int BLOCK_SIZE = 256;\n \n const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);\n const dim3 block(BLOCK_SIZE);\n \n AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), \"rbf_kernel_forward\", ([&] {\n rbf_kernel_forward_opt<scalar_t, TILE_N, TILE_M, VEC_SIZE><<<grid, block>>>(\n X1.data_ptr<scalar_t>(),\n X2.data_ptr<scalar_t>(),\n K.data_ptr<scalar_t>(),\n static_cast<scalar_t>(lengthscale_sq),\n static_cast<scalar_t>(variance),\n B, N, M, D\n );\n }));\n \n return K;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);\n\"\"\"\n\nrbf_module = load_inline(\n name='rbf_cuda_opt',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['rbf_forward_cuda'],\n extra_cuda_cflags=['-O3'],\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n self.input_dim = input_dim\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n \n # Ensure inputs are contiguous\n X1 = X1.contiguous()\n X2 = X2.contiguous()\n \n return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())"
}
result ← run_correctness (fail)
failrun_correctness
run_correctness FAILED: (0 / 5) trials did not all match.
correctness_issue: Output mismatch
max_difference: ['0.878243', '0.851315', '0.864821', '0.895723', '0.887828']
avg_difference: ['0.012184', '0.012693', '0.012262', '0.012184', '0.012507']
turn 5 · 1 tool call(s) · 533.1s turn · 15.5s LLM · tokens in=8,811 · out=1,703 · total=10,514 · 7,590 chars (click to expand)
turn 5533.1s turn · 15.5s LLM · 517.6s tools · tokens in=8,811 · out=1,703 · total=10,514
reasoning
The optimized version has a bug. The issue is probably with how I'm computing the squared norms using warp reductions - the values are not being properly shared across the block. Let me fix this with a simpler and more robust approach.
The main issue is that I'm using __shfl_down_sync for reduction but the result is not visible to all threads after the syncthreads.
Let me go back to the simpler approach that works, but optimize it differently - maybe use vectorized loads with float4.
assistantThe optimized version has a synchronization issue. Let me fix it by using a cleaner approach with proper synchronization for the norm computation:
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n// RBF kernel - each thread computes one output element\n// Shared memory caches X1 and X2 tiles for data reuse\n\ntemplate <typename scalar_t, int TILE_N, int TILE_M>\n__global__ void rbf_kernel_forward(\n const scalar_t* __restrict__ X1, // (B, N, D)\n const scalar_t* __restrict__ X2, // (B, M, D)\n scalar_t* __restrict__ K, // (B, N, M)\n const scalar_t lengthscale_sq, // l^2\n const scalar_t variance, // sigma^2\n const int B, const int N, const int M, const int D\n) {\n const int batch = blockIdx.z;\n const int tile_i_start = blockIdx.y * TILE_N;\n const int tile_j_start = blockIdx.x * TILE_M;\n \n // Shared memory for X1 and X2 tile data\n // Pad columns to avoid bank conflicts\n __shared__ scalar_t X1_tile[TILE_N][12]; // TILE_N rows x D cols (D <= 8, pad to 12)\n __shared__ scalar_t X2_tile[TILE_M][12]; // TILE_M rows x D cols\n \n const int tid = threadIdx.x;\n const int block_size = blockDim.x;\n \n // Cooperative load of X1 tile into shared memory\n // Load in a way that maximizes memory coalescing\n for (int idx = tid; idx < TILE_N * D; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_i_start + row;\n if (global_row < N) {\n X1_tile[row][col] = X1[batch * N * D + global_row * D + col];\n } else {\n X1_tile[row][col] = static_cast<scalar_t>(0);\n }\n }\n \n // Cooperative load of X2 tile into shared memory\n for (int idx = tid; idx < TILE_M * D; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_j_start + row;\n if (global_row < M) {\n X2_tile[row][col] = X2[batch * M * D + global_row * D + col];\n } else {\n X2_tile[row][col] = static_cast<scalar_t>(0);\n }\n }\n \n __syncthreads();\n \n // Each thread computes one output element\n // Output tile is TILE_N x TILE_M\n const int outputs_per_thread = 1;\n const int total_outputs = TILE_N * TILE_M;\n \n for (int out_idx = tid; out_idx < total_outputs; out_idx += block_size) {\n const int lane_i = out_idx / TILE_M;\n const int lane_j = out_idx % TILE_M;\n \n const int i = tile_i_start + lane_i;\n const int j = tile_j_start + lane_j;\n \n if (i < N && j < M) {\n // Compute squared norm of X1 row\n scalar_t x1_sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n scalar_t val = X1_tile[lane_i][d];\n x1_sq += val * val;\n }\n \n // Compute squared norm of X2 row\n scalar_t x2_sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n scalar_t val = X2_tile[lane_j][d];\n x2_sq += val * val;\n }\n \n // Compute dot product\n scalar_t dot = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n dot += X1_tile[lane_i][d] * X2_tile[lane_j][d];\n }\n \n // Squared distance\n scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;\n dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);\n \n // RBF kernel\n scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;\n K[batch * N * M + i * M + j] = variance * exp(exp_arg);\n }\n }\n}\n\ntorch::Tensor rbf_forward_cuda(\n torch::Tensor X1,\n torch::Tensor X2,\n double lengthscale_sq,\n double variance\n) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n \n auto K = torch::empty({B, N, M}, X1.options());\n \n const int TILE_N = 32;\n const int TILE_M = 32;\n const int BLOCK_SIZE = 1024; // One thread per output in tile\n \n const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);\n const dim3 block(BLOCK_SIZE);\n \n AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), \"rbf_kernel_forward\", ([&] {\n rbf_kernel_forward<scalar_t, TILE_N, TILE_M><<<grid, block>>>(\n X1.data_ptr<scalar_t>(),\n X2.data_ptr<scalar_t>(),\n K.data_ptr<scalar_t>(),\n static_cast<scalar_t>(lengthscale_sq),\n static_cast<scalar_t>(variance),\n B, N, M, D\n );\n }));\n \n return K;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);\n\"\"\"\n\nrbf_module = load_inline(\n name='rbf_cuda_v2',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['rbf_forward_cuda'],\n extra_cuda_cflags=['-O3'],\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n self.input_dim = input_dim\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n \n # Ensure inputs are contiguous\n X1 = X1.contiguous()\n X2 = X2.contiguous()\n \n return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 6 · 1 tool call(s) · 255.2s turn · 13.4s LLM · tokens in=10,426 · out=1,593 · total=12,019 · 7,042 chars (click to expand)
turn 6255.2s turn · 13.4s LLM · 241.7s tools · tokens in=10,426 · out=1,593 · total=12,019
reasoning
Good, now let me run correctness to make sure this version is correct.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n// RBF kernel - each thread computes one output element\n// Shared memory caches X1 and X2 tiles for data reuse\n\ntemplate <typename scalar_t, int TILE_N, int TILE_M>\n__global__ void rbf_kernel_forward(\n const scalar_t* __restrict__ X1, // (B, N, D)\n const scalar_t* __restrict__ X2, // (B, M, D)\n scalar_t* __restrict__ K, // (B, N, M)\n const scalar_t lengthscale_sq, // l^2\n const scalar_t variance, // sigma^2\n const int B, const int N, const int M, const int D\n) {\n const int batch = blockIdx.z;\n const int tile_i_start = blockIdx.y * TILE_N;\n const int tile_j_start = blockIdx.x * TILE_M;\n \n // Shared memory for X1 and X2 tile data\n // Pad columns to avoid bank conflicts\n __shared__ scalar_t X1_tile[TILE_N][12]; // TILE_N rows x D cols (D <= 8, pad to 12)\n __shared__ scalar_t X2_tile[TILE_M][12]; // TILE_M rows x D cols\n \n const int tid = threadIdx.x;\n const int block_size = blockDim.x;\n \n // Cooperative load of X1 tile into shared memory\n // Load in a way that maximizes memory coalescing\n for (int idx = tid; idx < TILE_N * D; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_i_start + row;\n if (global_row < N) {\n X1_tile[row][col] = X1[batch * N * D + global_row * D + col];\n } else {\n X1_tile[row][col] = static_cast<scalar_t>(0);\n }\n }\n \n // Cooperative load of X2 tile into shared memory\n for (int idx = tid; idx < TILE_M * D; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_j_start + row;\n if (global_row < M) {\n X2_tile[row][col] = X2[batch * M * D + global_row * D + col];\n } else {\n X2_tile[row][col] = static_cast<scalar_t>(0);\n }\n }\n \n __syncthreads();\n \n // Each thread computes one output element\n // Output tile is TILE_N x TILE_M\n const int outputs_per_thread = 1;\n const int total_outputs = TILE_N * TILE_M;\n \n for (int out_idx = tid; out_idx < total_outputs; out_idx += block_size) {\n const int lane_i = out_idx / TILE_M;\n const int lane_j = out_idx % TILE_M;\n \n const int i = tile_i_start + lane_i;\n const int j = tile_j_start + lane_j;\n \n if (i < N && j < M) {\n // Compute squared norm of X1 row\n scalar_t x1_sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n scalar_t val = X1_tile[lane_i][d];\n x1_sq += val * val;\n }\n \n // Compute squared norm of X2 row\n scalar_t x2_sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n scalar_t val = X2_tile[lane_j][d];\n x2_sq += val * val;\n }\n \n // Compute dot product\n scalar_t dot = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n dot += X1_tile[lane_i][d] * X2_tile[lane_j][d];\n }\n \n // Squared distance\n scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;\n dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);\n \n // RBF kernel\n scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;\n K[batch * N * M + i * M + j] = variance * exp(exp_arg);\n }\n }\n}\n\ntorch::Tensor rbf_forward_cuda(\n torch::Tensor X1,\n torch::Tensor X2,\n double lengthscale_sq,\n double variance\n) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n \n auto K = torch::empty({B, N, M}, X1.options());\n \n const int TILE_N = 32;\n const int TILE_M = 32;\n const int BLOCK_SIZE = 1024; // One thread per output in tile\n \n const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);\n const dim3 block(BLOCK_SIZE);\n \n AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), \"rbf_kernel_forward\", ([&] {\n rbf_kernel_forward<scalar_t, TILE_N, TILE_M><<<grid, block>>>(\n X1.data_ptr<scalar_t>(),\n X2.data_ptr<scalar_t>(),\n K.data_ptr<scalar_t>(),\n static_cast<scalar_t>(lengthscale_sq),\n static_cast<scalar_t>(variance),\n B, N, M, D\n );\n }));\n \n return K;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);\n\"\"\"\n\nrbf_module = load_inline(\n name='rbf_cuda_v2',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['rbf_forward_cuda'],\n extra_cuda_cflags=['-O3'],\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n self.input_dim = input_dim\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n \n # Ensure inputs are contiguous\n X1 = X1.contiguous()\n X2 = X2.contiguous()\n \n return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=8.64e-07 mean_abs_err=2.23e-09 max_rel_err=5.73e-06
turn 7 · 1 tool call(s) · 476.2s turn · 16.1s LLM · tokens in=12,061 · out=1,843 · total=13,904 · 8,034 chars (click to expand)
turn 7476.2s turn · 16.1s LLM · 460.0s tools · tokens in=12,061 · out=1,843 · total=13,904
reasoning
Good, correctness passes. Now let me try to optimize further. The current approach loads each tile once but computes squared norms multiple times (once per thread per row). I should cache the squared norms in shared memory to avoid redundant computation.
Let me try a version that:
1. Caches squared norms in shared memory
2. Uses vectorized loads where possible
3. Tunes tile sizes
Actually, let me think about the computation more carefully. Each row of X1 participates in M outputs (one per X2 row), and each row of X2 participates in N outputs. So caching the squared norms should help.
Let me also try using float4 loads since D=8 is a multiple of 4.
assistantGood, it passes correctness. Now let me optimize further by precomputing squared norms in shared memory and using vectorized loads:
call → compile_kernel
callcompile_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n// RBF kernel with precomputed squared norms in shared memory\n// Each thread computes one output element\n\ntemplate <typename scalar_t, int TILE_N, int TILE_M, int D>\n__global__ void __launch_bounds__(1024) rbf_kernel_forward(\n const scalar_t* __restrict__ X1, // (B, N, D)\n const scalar_t* __restrict__ X2, // (B, M, D)\n scalar_t* __restrict__ K, // (B, N, M)\n const scalar_t lengthscale_sq, // l^2\n const scalar_t variance, // sigma^2\n const int B, const int N, const int M\n) {\n const int batch = blockIdx.z;\n const int tile_i_start = blockIdx.y * TILE_N;\n const int tile_j_start = blockIdx.x * TILE_M;\n \n // Shared memory: X1/X2 vectors and their squared norms\n // Pad to avoid bank conflicts\n __shared__ scalar_t X1_tile[TILE_N][D + 4];\n __shared__ scalar_t X2_tile[TILE_M][D + 4];\n __shared__ scalar_t X1_sq_cache[TILE_N];\n __shared__ scalar_t X2_sq_cache[TILE_M];\n \n const int tid = threadIdx.x;\n const int block_size = blockDim.x;\n \n // Cooperative load of X1 tile and compute squared norms\n // Each thread loads one element, multiple rounds if needed\n const int x1_elements = TILE_N * D;\n for (int idx = tid; idx < x1_elements; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_i_start + row;\n scalar_t val = static_cast<scalar_t>(0);\n if (global_row < N) {\n val = X1[batch * N * D + global_row * D + col];\n }\n X1_tile[row][col] = val;\n }\n \n // Cooperative load of X2 tile\n const int x2_elements = TILE_M * D;\n for (int idx = tid; idx < x2_elements; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_j_start + row;\n scalar_t val = static_cast<scalar_t>(0);\n if (global_row < M) {\n val = X2[batch * M * D + global_row * D + col];\n }\n X2_tile[row][col] = val;\n }\n \n __syncthreads();\n \n // Compute squared norms for X1 rows\n // Distribute across threads\n for (int row = tid; row < TILE_N; row += block_size) {\n scalar_t sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n sq += X1_tile[row][d] * X1_tile[row][d];\n }\n X1_sq_cache[row] = sq;\n }\n \n // Compute squared norms for X2 rows\n for (int row = tid; row < TILE_M; row += block_size) {\n scalar_t sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n sq += X2_tile[row][d] * X2_tile[row][d];\n }\n X2_sq_cache[row] = sq;\n }\n \n __syncthreads();\n \n // Each thread computes one output element\n const int total_outputs = TILE_N * TILE_M;\n \n for (int out_idx = tid; out_idx < total_outputs; out_idx += block_size) {\n const int lane_i = out_idx / TILE_M;\n const int lane_j = out_idx % TILE_M;\n \n const int i = tile_i_start + lane_i;\n const int j = tile_j_start + lane_j;\n \n if (i < N && j < M) {\n // Get precomputed squared norms\n scalar_t x1_sq = X1_sq_cache[lane_i];\n scalar_t x2_sq = X2_sq_cache[lane_j];\n \n // Compute dot product\n scalar_t dot = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n dot += X1_tile[lane_i][d] * X2_tile[lane_j][d];\n }\n \n // Squared distance\n scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;\n dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);\n \n // RBF kernel\n scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;\n K[batch * N * M + i * M + j] = variance * exp(exp_arg);\n }\n }\n}\n\ntorch::Tensor rbf_forward_cuda(\n torch::Tensor X1,\n torch::Tensor X2,\n double lengthscale_sq,\n double variance\n) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n \n auto K = torch::empty({B, N, M}, X1.options());\n \n const int TILE_N = 32;\n const int TILE_M = 32;\n const int BLOCK_SIZE = 1024;\n \n const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);\n const dim3 block(BLOCK_SIZE);\n \n AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), \"rbf_kernel_forward\", ([&] {\n rbf_kernel_forward<scalar_t, TILE_N, TILE_M, 8><<<grid, block>>>(\n X1.data_ptr<scalar_t>(),\n X2.data_ptr<scalar_t>(),\n K.data_ptr<scalar_t>(),\n static_cast<scalar_t>(lengthscale_sq),\n static_cast<scalar_t>(variance),\n B, N, M\n );\n }));\n \n return K;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);\n\"\"\"\n\nrbf_module = load_inline(\n name='rbf_cuda_v3',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['rbf_forward_cuda'],\n extra_cuda_cflags=['-O3'],\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n self.input_dim = input_dim\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n \n # Ensure inputs are contiguous\n X1 = X1.contiguous()\n X2 = X2.contiguous()\n \n return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 8 · 1 tool call(s) · 247.8s turn · 14.2s LLM · tokens in=13,772 · out=1,683 · total=15,455 · 7,329 chars (click to expand)
turn 8247.8s turn · 14.2s LLM · 233.6s tools · tokens in=13,772 · out=1,683 · total=15,455
reasoning
Let me run correctness to verify this optimization works.
call → run_correctness
callrun_correctness
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n// RBF kernel with precomputed squared norms in shared memory\n// Each thread computes one output element\n\ntemplate <typename scalar_t, int TILE_N, int TILE_M, int D>\n__global__ void __launch_bounds__(1024) rbf_kernel_forward(\n const scalar_t* __restrict__ X1, // (B, N, D)\n const scalar_t* __restrict__ X2, // (B, M, D)\n scalar_t* __restrict__ K, // (B, N, M)\n const scalar_t lengthscale_sq, // l^2\n const scalar_t variance, // sigma^2\n const int B, const int N, const int M\n) {\n const int batch = blockIdx.z;\n const int tile_i_start = blockIdx.y * TILE_N;\n const int tile_j_start = blockIdx.x * TILE_M;\n \n // Shared memory: X1/X2 vectors and their squared norms\n // Pad to avoid bank conflicts\n __shared__ scalar_t X1_tile[TILE_N][D + 4];\n __shared__ scalar_t X2_tile[TILE_M][D + 4];\n __shared__ scalar_t X1_sq_cache[TILE_N];\n __shared__ scalar_t X2_sq_cache[TILE_M];\n \n const int tid = threadIdx.x;\n const int block_size = blockDim.x;\n \n // Cooperative load of X1 tile and compute squared norms\n // Each thread loads one element, multiple rounds if needed\n const int x1_elements = TILE_N * D;\n for (int idx = tid; idx < x1_elements; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_i_start + row;\n scalar_t val = static_cast<scalar_t>(0);\n if (global_row < N) {\n val = X1[batch * N * D + global_row * D + col];\n }\n X1_tile[row][col] = val;\n }\n \n // Cooperative load of X2 tile\n const int x2_elements = TILE_M * D;\n for (int idx = tid; idx < x2_elements; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_j_start + row;\n scalar_t val = static_cast<scalar_t>(0);\n if (global_row < M) {\n val = X2[batch * M * D + global_row * D + col];\n }\n X2_tile[row][col] = val;\n }\n \n __syncthreads();\n \n // Compute squared norms for X1 rows\n // Distribute across threads\n for (int row = tid; row < TILE_N; row += block_size) {\n scalar_t sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n sq += X1_tile[row][d] * X1_tile[row][d];\n }\n X1_sq_cache[row] = sq;\n }\n \n // Compute squared norms for X2 rows\n for (int row = tid; row < TILE_M; row += block_size) {\n scalar_t sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n sq += X2_tile[row][d] * X2_tile[row][d];\n }\n X2_sq_cache[row] = sq;\n }\n \n __syncthreads();\n \n // Each thread computes one output element\n const int total_outputs = TILE_N * TILE_M;\n \n for (int out_idx = tid; out_idx < total_outputs; out_idx += block_size) {\n const int lane_i = out_idx / TILE_M;\n const int lane_j = out_idx % TILE_M;\n \n const int i = tile_i_start + lane_i;\n const int j = tile_j_start + lane_j;\n \n if (i < N && j < M) {\n // Get precomputed squared norms\n scalar_t x1_sq = X1_sq_cache[lane_i];\n scalar_t x2_sq = X2_sq_cache[lane_j];\n \n // Compute dot product\n scalar_t dot = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n dot += X1_tile[lane_i][d] * X2_tile[lane_j][d];\n }\n \n // Squared distance\n scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;\n dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);\n \n // RBF kernel\n scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;\n K[batch * N * M + i * M + j] = variance * exp(exp_arg);\n }\n }\n}\n\ntorch::Tensor rbf_forward_cuda(\n torch::Tensor X1,\n torch::Tensor X2,\n double lengthscale_sq,\n double variance\n) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n \n auto K = torch::empty({B, N, M}, X1.options());\n \n const int TILE_N = 32;\n const int TILE_M = 32;\n const int BLOCK_SIZE = 1024;\n \n const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);\n const dim3 block(BLOCK_SIZE);\n \n AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), \"rbf_kernel_forward\", ([&] {\n rbf_kernel_forward<scalar_t, TILE_N, TILE_M, 8><<<grid, block>>>(\n X1.data_ptr<scalar_t>(),\n X2.data_ptr<scalar_t>(),\n K.data_ptr<scalar_t>(),\n static_cast<scalar_t>(lengthscale_sq),\n static_cast<scalar_t>(variance),\n B, N, M\n );\n }));\n \n return K;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);\n\"\"\"\n\nrbf_module = load_inline(\n name='rbf_cuda_v3',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['rbf_forward_cuda'],\n extra_cuda_cflags=['-O3'],\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n self.input_dim = input_dim\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n \n # Ensure inputs are contiguous\n X1 = X1.contiguous()\n X2 = X2.contiguous()\n \n return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (5 / 5) trials all matched the reference.
Numerical precision: max_abs_err=8.64e-07 mean_abs_err=2.23e-09 max_rel_err=5.73e-06
turn 9 · FINAL · 1 tool call(s) · 254.1s turn · 14.5s LLM · tokens in=15,534 · out=1,753 · total=17,287 · 7,830 chars (click to expand)
turn 9254.1s turn · 14.5s LLM · 239.6s tools · tokens in=15,534 · out=1,753 · total=17,287 · FINAL
reasoning
The kernel passes correctness. Let me now submit it to get timing results. The current implementation should be reasonably optimized with:
1. Tiled computation with shared memory for X1 and X2 tiles
2. Precomputed squared norms in shared memory
3. One thread per output element
4. 32x32 tiles with 1024 threads per block
Let me submit and see the performance.
call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\ncuda_source = \"\"\"\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <cmath>\n\n// RBF kernel with precomputed squared norms in shared memory\n// Each thread computes one output element\n\ntemplate <typename scalar_t, int TILE_N, int TILE_M, int D>\n__global__ void __launch_bounds__(1024) rbf_kernel_forward(\n const scalar_t* __restrict__ X1, // (B, N, D)\n const scalar_t* __restrict__ X2, // (B, M, D)\n scalar_t* __restrict__ K, // (B, N, M)\n const scalar_t lengthscale_sq, // l^2\n const scalar_t variance, // sigma^2\n const int B, const int N, const int M\n) {\n const int batch = blockIdx.z;\n const int tile_i_start = blockIdx.y * TILE_N;\n const int tile_j_start = blockIdx.x * TILE_M;\n \n // Shared memory: X1/X2 vectors and their squared norms\n // Pad to avoid bank conflicts\n __shared__ scalar_t X1_tile[TILE_N][D + 4];\n __shared__ scalar_t X2_tile[TILE_M][D + 4];\n __shared__ scalar_t X1_sq_cache[TILE_N];\n __shared__ scalar_t X2_sq_cache[TILE_M];\n \n const int tid = threadIdx.x;\n const int block_size = blockDim.x;\n \n // Cooperative load of X1 tile and compute squared norms\n // Each thread loads one element, multiple rounds if needed\n const int x1_elements = TILE_N * D;\n for (int idx = tid; idx < x1_elements; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_i_start + row;\n scalar_t val = static_cast<scalar_t>(0);\n if (global_row < N) {\n val = X1[batch * N * D + global_row * D + col];\n }\n X1_tile[row][col] = val;\n }\n \n // Cooperative load of X2 tile\n const int x2_elements = TILE_M * D;\n for (int idx = tid; idx < x2_elements; idx += block_size) {\n const int row = idx / D;\n const int col = idx % D;\n const int global_row = tile_j_start + row;\n scalar_t val = static_cast<scalar_t>(0);\n if (global_row < M) {\n val = X2[batch * M * D + global_row * D + col];\n }\n X2_tile[row][col] = val;\n }\n \n __syncthreads();\n \n // Compute squared norms for X1 rows\n // Distribute across threads\n for (int row = tid; row < TILE_N; row += block_size) {\n scalar_t sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n sq += X1_tile[row][d] * X1_tile[row][d];\n }\n X1_sq_cache[row] = sq;\n }\n \n // Compute squared norms for X2 rows\n for (int row = tid; row < TILE_M; row += block_size) {\n scalar_t sq = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n sq += X2_tile[row][d] * X2_tile[row][d];\n }\n X2_sq_cache[row] = sq;\n }\n \n __syncthreads();\n \n // Each thread computes one output element\n const int total_outputs = TILE_N * TILE_M;\n \n for (int out_idx = tid; out_idx < total_outputs; out_idx += block_size) {\n const int lane_i = out_idx / TILE_M;\n const int lane_j = out_idx % TILE_M;\n \n const int i = tile_i_start + lane_i;\n const int j = tile_j_start + lane_j;\n \n if (i < N && j < M) {\n // Get precomputed squared norms\n scalar_t x1_sq = X1_sq_cache[lane_i];\n scalar_t x2_sq = X2_sq_cache[lane_j];\n \n // Compute dot product\n scalar_t dot = static_cast<scalar_t>(0);\n for (int d = 0; d < D; d++) {\n dot += X1_tile[lane_i][d] * X2_tile[lane_j][d];\n }\n \n // Squared distance\n scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;\n dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);\n \n // RBF kernel\n scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;\n K[batch * N * M + i * M + j] = variance * exp(exp_arg);\n }\n }\n}\n\ntorch::Tensor rbf_forward_cuda(\n torch::Tensor X1,\n torch::Tensor X2,\n double lengthscale_sq,\n double variance\n) {\n const int B = X1.size(0);\n const int N = X1.size(1);\n const int D = X1.size(2);\n const int M = X2.size(1);\n \n auto K = torch::empty({B, N, M}, X1.options());\n \n const int TILE_N = 32;\n const int TILE_M = 32;\n const int BLOCK_SIZE = 1024;\n \n const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);\n const dim3 block(BLOCK_SIZE);\n \n AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), \"rbf_kernel_forward\", ([&] {\n rbf_kernel_forward<scalar_t, TILE_N, TILE_M, 8><<<grid, block>>>(\n X1.data_ptr<scalar_t>(),\n X2.data_ptr<scalar_t>(),\n K.data_ptr<scalar_t>(),\n static_cast<scalar_t>(lengthscale_sq),\n static_cast<scalar_t>(variance),\n B, N, M\n );\n }));\n \n return K;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);\n\"\"\"\n\nrbf_module = load_inline(\n name='rbf_cuda_v3',\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=['rbf_forward_cuda'],\n extra_cuda_cflags=['-O3'],\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_dim, lengthscale=1.0, variance=1.0):\n super().__init__()\n self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())\n self.log_variance = nn.Parameter(torch.tensor(variance).log())\n self.input_dim = input_dim\n\n def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:\n l2 = self.log_lengthscale.exp() ** 2\n var = self.log_variance.exp()\n \n # Ensure inputs are contiguous\n X1 = X1.contiguous()\n X2 = X2.contiguous()\n \n return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 0.06 μs
Runtime stats: mean=0.06μs median=?μs std=0.01μs
Numerical precision: max_abs_err=8.64e-07 mean_abs_err=2.23e-09 max_rel_err=5.73e-06
Memory: 37.32 MB peak (ref: 45.35 MB, ratio: 0.8228x)
Kernel launches: 10 (ref: 37, fusion ratio: 3.7)
SOL score: 0.1753
final submitted kernel (182 lines, 5,976 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline
cuda_source = """
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cmath>
// RBF kernel with precomputed squared norms in shared memory
// Each thread computes one output element
template <typename scalar_t, int TILE_N, int TILE_M, int D>
__global__ void __launch_bounds__(1024) rbf_kernel_forward(
const scalar_t* __restrict__ X1, // (B, N, D)
const scalar_t* __restrict__ X2, // (B, M, D)
scalar_t* __restrict__ K, // (B, N, M)
const scalar_t lengthscale_sq, // l^2
const scalar_t variance, // sigma^2
const int B, const int N, const int M
) {
const int batch = blockIdx.z;
const int tile_i_start = blockIdx.y * TILE_N;
const int tile_j_start = blockIdx.x * TILE_M;
// Shared memory: X1/X2 vectors and their squared norms
// Pad to avoid bank conflicts
__shared__ scalar_t X1_tile[TILE_N][D + 4];
__shared__ scalar_t X2_tile[TILE_M][D + 4];
__shared__ scalar_t X1_sq_cache[TILE_N];
__shared__ scalar_t X2_sq_cache[TILE_M];
const int tid = threadIdx.x;
const int block_size = blockDim.x;
// Cooperative load of X1 tile and compute squared norms
// Each thread loads one element, multiple rounds if needed
const int x1_elements = TILE_N * D;
for (int idx = tid; idx < x1_elements; idx += block_size) {
const int row = idx / D;
const int col = idx % D;
const int global_row = tile_i_start + row;
scalar_t val = static_cast<scalar_t>(0);
if (global_row < N) {
val = X1[batch * N * D + global_row * D + col];
}
X1_tile[row][col] = val;
}
// Cooperative load of X2 tile
const int x2_elements = TILE_M * D;
for (int idx = tid; idx < x2_elements; idx += block_size) {
const int row = idx / D;
const int col = idx % D;
const int global_row = tile_j_start + row;
scalar_t val = static_cast<scalar_t>(0);
if (global_row < M) {
val = X2[batch * M * D + global_row * D + col];
}
X2_tile[row][col] = val;
}
__syncthreads();
// Compute squared norms for X1 rows
// Distribute across threads
for (int row = tid; row < TILE_N; row += block_size) {
scalar_t sq = static_cast<scalar_t>(0);
for (int d = 0; d < D; d++) {
sq += X1_tile[row][d] * X1_tile[row][d];
}
X1_sq_cache[row] = sq;
}
// Compute squared norms for X2 rows
for (int row = tid; row < TILE_M; row += block_size) {
scalar_t sq = static_cast<scalar_t>(0);
for (int d = 0; d < D; d++) {
sq += X2_tile[row][d] * X2_tile[row][d];
}
X2_sq_cache[row] = sq;
}
__syncthreads();
// Each thread computes one output element
const int total_outputs = TILE_N * TILE_M;
for (int out_idx = tid; out_idx < total_outputs; out_idx += block_size) {
const int lane_i = out_idx / TILE_M;
const int lane_j = out_idx % TILE_M;
const int i = tile_i_start + lane_i;
const int j = tile_j_start + lane_j;
if (i < N && j < M) {
// Get precomputed squared norms
scalar_t x1_sq = X1_sq_cache[lane_i];
scalar_t x2_sq = X2_sq_cache[lane_j];
// Compute dot product
scalar_t dot = static_cast<scalar_t>(0);
for (int d = 0; d < D; d++) {
dot += X1_tile[lane_i][d] * X2_tile[lane_j][d];
}
// Squared distance
scalar_t dist_sq = x1_sq - static_cast<scalar_t>(2) * dot + x2_sq;
dist_sq = dist_sq > static_cast<scalar_t>(0) ? dist_sq : static_cast<scalar_t>(0);
// RBF kernel
scalar_t exp_arg = static_cast<scalar_t>(-0.5) * dist_sq / lengthscale_sq;
K[batch * N * M + i * M + j] = variance * exp(exp_arg);
}
}
}
torch::Tensor rbf_forward_cuda(
torch::Tensor X1,
torch::Tensor X2,
double lengthscale_sq,
double variance
) {
const int B = X1.size(0);
const int N = X1.size(1);
const int D = X1.size(2);
const int M = X2.size(1);
auto K = torch::empty({B, N, M}, X1.options());
const int TILE_N = 32;
const int TILE_M = 32;
const int BLOCK_SIZE = 1024;
const dim3 grid((M + TILE_M - 1) / TILE_M, (N + TILE_N - 1) / TILE_N, B);
const dim3 block(BLOCK_SIZE);
AT_DISPATCH_FLOATING_TYPES(X1.scalar_type(), "rbf_kernel_forward", ([&] {
rbf_kernel_forward<scalar_t, TILE_N, TILE_M, 8><<<grid, block>>>(
X1.data_ptr<scalar_t>(),
X2.data_ptr<scalar_t>(),
K.data_ptr<scalar_t>(),
static_cast<scalar_t>(lengthscale_sq),
static_cast<scalar_t>(variance),
B, N, M
);
}));
return K;
}
"""
cpp_source = """
torch::Tensor rbf_forward_cuda(torch::Tensor X1, torch::Tensor X2, double lengthscale_sq, double variance);
"""
rbf_module = load_inline(
name='rbf_cuda_v3',
cpp_sources=cpp_source,
cuda_sources=cuda_source,
functions=['rbf_forward_cuda'],
extra_cuda_cflags=['-O3'],
)
class ModelNew(nn.Module):
def __init__(self, input_dim, lengthscale=1.0, variance=1.0):
super().__init__()
self.log_lengthscale = nn.Parameter(torch.tensor(lengthscale).log())
self.log_variance = nn.Parameter(torch.tensor(variance).log())
self.input_dim = input_dim
def forward(self, X1: torch.Tensor, X2: torch.Tensor) -> torch.Tensor:
l2 = self.log_lengthscale.exp() ** 2
var = self.log_variance.exp()
# Ensure inputs are contiguous
X1 = X1.contiguous()
X2 = X2.contiguous()
return rbf_module.rbf_forward_cuda(X1, X2, l2.item(), var.item())