correct
19_PairwiseDistanceMatrix.py
started
2026-05-11T22:26:08Z
timingkernelbench.timing
kernel mean / median / std
0.01 / — / 5.01e-04 μs
ref mean / median / std
0.08 / — / 0.00 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
12.00x
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):
"""
Compute all-pairs Euclidean distance matrix from a 3-D point cloud.
A core primitive in molecular dynamics simulations, docking, and
distance-geometry-based structure prediction.
"""
def __init__(self):
super().__init__()
def forward(self, coords: torch.Tensor) -> torch.Tensor:
"""
Args:
coords: (B, N, 3) – 3-D coordinates of N atoms / residues
Returns:
dist: (B, N, N) – pairwise Euclidean distance matrix
"""
diff = coords.unsqueeze(2) - coords.unsqueeze(1) # (B, N, N, 3)
dist = torch.sqrt((diff ** 2).sum(dim=-1) + 1e-8)
return dist
num_atoms = 512
batch_size = 8
def get_inputs():
p = popcorn_pri
return [torch.randn(p.jitter_int(batch_size), p.jitter_int(num_atoms), 3)]
def get_init_inputs():
return []
turn 0203.2s turn · 3.0s LLM · 200.2s tools · tokens in=1,987 · out=6 · total=2,238
reasoning
The task is to replace the forward computation of a PyTorch model that computes all-pairs Euclidean distance matrix from a 3-D point cloud with a custom CUDA kernel.
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 1147.9s turn · 5.8s LLM · 142.0s tools · tokens in=2,471 · out=471 · total=3,048
reasoning
H100, high bandwidth.
I need to write a CUDA kernel for pairwise distances.
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\n__global__ void pairwise_dist_kernel(const float* coords, float* dist, int B, int N) {\n int b = blockIdx.x;\n int i = blockIdx.y * blockDim.x + threadIdx.x;\n if (i >= N) return;\n \n for (int j = 0; j < N; j++) {\n float dx = coords[b*N*3 + i*3 + 0] - coords[b*N*3 + j*3 + 0];\n float dy = coords[b*N*3 + i*3 + 1] - coords[b*N*3 + j*3 + 1];\n float dz = coords[b*N*3 + i*3 + 2] - coords[b*N*3 + j*3 + 2];\n float d2 = dx*dx + dy*dy + dz*dz;\n dist[b*N*N + i*N + j] = sqrtf(d2 + 1e-8f);\n }\n}\n\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords) {\n int B = coords.size(0);\n int N = coords.size(1);\n auto dist = torch::empty({B, N, N}, coords.options());\n \n dim3 blocks(B, (N + 255)/256);\n dim3 threads(256);\n \n pairwise_dist_kernel<<<blocks, threads>>>(\n coords.data_ptr<float>(),\n dist.data_ptr<float>(),\n B, N\n );\n return dist;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords);\n'''\n\nmodule = load_inline(\n name='pairwise_dist',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['pairwise_dist_cuda'],\n verbose=True,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n \n def forward(self, coords):\n return self.module.pairwise_dist_cuda(coords)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 228.8s turn · 4.5s LLM · 24.4s tools · tokens in=3,065 · out=472 · total=3,544
reasoning
Compiled successfully. Now 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\n__global__ void pairwise_dist_kernel(const float* coords, float* dist, int B, int N) {\n int b = blockIdx.x;\n int i = blockIdx.y * blockDim.x + threadIdx.x;\n if (i >= N) return;\n \n for (int j = 0; j < N; j++) {\n float dx = coords[b*N*3 + i*3 + 0] - coords[b*N*3 + j*3 + 0];\n float dy = coords[b*N*3 + i*3 + 1] - coords[b*N*3 + j*3 + 1];\n float dz = coords[b*N*3 + i*3 + 2] - coords[b*N*3 + j*3 + 2];\n float d2 = dx*dx + dy*dy + dz*dz;\n dist[b*N*N + i*N + j] = sqrtf(d2 + 1e-8f);\n }\n}\n\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords) {\n int B = coords.size(0);\n int N = coords.size(1);\n auto dist = torch::empty({B, N, N}, coords.options());\n \n dim3 blocks(B, (N + 255)/256);\n dim3 threads(256);\n \n pairwise_dist_kernel<<<blocks, threads>>>(\n coords.data_ptr<float>(),\n dist.data_ptr<float>(),\n B, N\n );\n return dist;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords);\n'''\n\nmodule = load_inline(\n name='pairwise_dist',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['pairwise_dist_cuda'],\n verbose=True,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n \n def forward(self, coords):\n return self.module.pairwise_dist_cuda(coords)\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (3 / 3) trials all matched the reference.
Numerical precision: max_abs_err=4.77e-07 mean_abs_err=2.82e-08 max_rel_err=1.65e-07
turn 317.2s turn · 5.8s LLM · 11.4s tools · tokens in=3,604 · out=471 · total=4,156
reasoning
Correct! Now, is it faster? I need to profile or check time, but since submit will do timing, I should optimize it.
call → profile_kernel
callprofile_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\n__global__ void pairwise_dist_kernel(const float* coords, float* dist, int B, int N) {\n int b = blockIdx.x;\n int i = blockIdx.y * blockDim.x + threadIdx.x;\n if (i >= N) return;\n \n for (int j = 0; j < N; j++) {\n float dx = coords[b*N*3 + i*3 + 0] - coords[b*N*3 + j*3 + 0];\n float dy = coords[b*N*3 + i*3 + 1] - coords[b*N*3 + j*3 + 1];\n float dz = coords[b*N*3 + i*3 + 2] - coords[b*N*3 + j*3 + 2];\n float d2 = dx*dx + dy*dy + dz*dz;\n dist[b*N*N + i*N + j] = sqrtf(d2 + 1e-8f);\n }\n}\n\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords) {\n int B = coords.size(0);\n int N = coords.size(1);\n auto dist = torch::empty({B, N, N}, coords.options());\n \n dim3 blocks(B, (N + 255)/256);\n dim3 threads(256);\n \n pairwise_dist_kernel<<<blocks, threads>>>(\n coords.data_ptr<float>(),\n dist.data_ptr<float>(),\n B, N\n );\n return dist;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords);\n'''\n\nmodule = load_inline(\n name='pairwise_dist',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['pairwise_dist_cuda'],\n verbose=True,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n \n def forward(self, coords):\n return self.module.pairwise_dist_cuda(coords)\n"
}
result ← profile_kernel (ok)
okprofile_kernel
profile_kernel PASSED: profiling complete.
=== Kernel Profile Summary ===
--- Kernel Breakdown (by GPU time) ---
#1: aten::empty 1793.0μs (100%) calls=1
--- Timing ---
GPU time: 171.07 μs
--- Memory Bandwidth ---
DRAM BW: 0.3/3350 GB/s (0.0%)
read=0.3 write=0.0 GB/s
Hit rates: L2=100.2%
--- Compute Throughput ---
Pipe util: FMA=3.7%, Tensor=0.0%
--- Roofline Analysis ---
Ridge point (FP32): 20.00 FLOPs/byte
Classification: UNKNOWN
--- Occupancy ---
Achieved: 12.5%
Eligible warps/cycle: 0.09
WARNING: <1 eligible warp/cycle = latency-bound, needs more parallelism
Launch: regs/thread=24, smem/blk=1024B, block=256, grid=16
--- Memory Access Quality ---
Global ld coalescing: 6.50 sectors/req (6.5x wasted BW)
--- Warp Stall Reasons (% of issue cycles) ---
long_scoreboard 1864.7% ####################
short_scoreboard 2.6%
--- Optimization Hints ---
-> 1865% stalls: global memory latency (waiting for DRAM/L2). + sectors/req=6.5 -> uncoalesced global loads, restructure access pattern or use __ldg Try shared-memory tiling, prefetching, or reducing working set.
-> Global load coalescing=6.5x (ideal=1.0) -> 6x wasted DRAM BW. Make per-warp accesses contiguous.
-> Low occupancy (12%). Consider __launch_bounds__ or reducing register pressure.
-> Low eligible warps + low BW% + low compute% = LATENCY-BOUND. Need more parallelism (larger grid, more ILP, prefetching).
turn 4111.5s turn · 6.9s LLM · 104.6s tools · tokens in=4,588 · out=690 · total=5,343
reasoning
It's very slow, 171us but mostly empty? Wait, the kernel itself is not profiled well because of the loop.
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\n#define TILE_SIZE 32\n\n__global__ void pairwise_dist_kernel(const float* __restrict__ coords, float* __restrict__ dist, int B, int N) {\n __shared__ float tile_j[ TILE_SIZE * 3 ];\n \n int b = blockIdx.z;\n int i_tile = blockIdx.y;\n int j_tile = blockIdx.x;\n \n int i = i_tile * TILE_SIZE + threadIdx.y;\n int j = j_tile * TILE_SIZE + threadIdx.x;\n \n float sum = 0.0f;\n if (i < N && j < N) {\n float xi = coords[b * N * 3 + i * 3 + 0];\n float yi = coords[b * N * 3 + i * 3 + 1];\n float zi = coords[b * N * 3 + i * 3 + 2];\n \n // Load tile for j\n if (threadIdx.y == 0) {\n int jj = j_tile * TILE_SIZE + threadIdx.x;\n if (jj < N) {\n tile_j[threadIdx.x * 3 + 0] = coords[b * N * 3 + jj * 3 + 0];\n tile_j[threadIdx.x * 3 + 1] = coords[b * N * 3 + jj * 3 + 1];\n tile_j[threadIdx.x * 3 + 2] = coords[b * N * 3 + jj * 3 + 2];\n }\n }\n __syncthreads();\n \n for (int t = 0; t < TILE_SIZE; t++) {\n if (j_tile * TILE_SIZE + t < N) {\n float xj = tile_j[t * 3 + 0];\n float yj = tile_j[t * 3 + 1];\n float zj = tile_j[t * 3 + 2];\n float dx = xi - xj;\n float dy = yi - yj;\n float dz = zi - zj;\n sum = dx*dx + dy*dy + dz*dz;\n }\n }\n // Wait, this is wrong, need per thread j\n }\n // Better rewrite\n}\n\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords) {\n // placeholder\n int B = coords.size(0);\n int N = coords.size(1);\n auto dist = torch::empty({B, N, N}, coords.options());\n return dist;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords);\n'''\n\nmodule = load_inline(\n name='pairwise_dist',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['pairwise_dist_cuda'],\n verbose=True,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n \n def forward(self, coords):\n return self.module.pairwise_dist_cuda(coords)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 5151.7s turn · 10.5s LLM · 141.1s tools · tokens in=5,360 · out=842 · total=6,310
reasoning
I messed up the kernel code. I need a correct tiled kernel.
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\n#define TILE 16\n\n__global__ void pairwise_dist_kernel(const float* __restrict__ coords, float* __restrict__ dist, int B, int N) {\n __shared__ float s_j[TILE * 3];\n \n int b = blockIdx.z;\n int tile_i = blockIdx.y;\n int tile_j = blockIdx.x;\n \n int local_i = threadIdx.y;\n int local_j = threadIdx.x;\n \n int i = tile_i * TILE + local_i;\n int j = tile_j * TILE + local_j;\n \n float xi = 0, yi = 0, zi = 0;\n if (i < N) {\n xi = coords[b * N * 3 + i * 3 + 0];\n yi = coords[b * N * 3 + i * 3 + 1];\n zi = coords[b * N * 3 + i * 3 + 2];\n }\n \n float result = 0.0f;\n \n for (int t = 0; t < (N + TILE - 1) / TILE; t++) {\n // Load j tile\n int jj = t * TILE + local_j;\n if (local_i == 0 && jj < N) {\n s_j[local_j * 3 + 0] = coords[b * N * 3 + jj * 3 + 0];\n s_j[local_j * 3 + 1] = coords[b * N * 3 + jj * 3 + 1];\n s_j[local_j * 3 + 2] = coords[b * N * 3 + jj * 3 + 2];\n }\n __syncthreads();\n \n if (i < N && j < N) {\n for (int k = 0; k < TILE; k++) {\n if (t * TILE + k < N) {\n float xj = s_j[k * 3 + 0];\n float yj = s_j[k * 3 + 1];\n float zj = s_j[k * 3 + 2];\n float dx = xi - xj;\n float dy = yi - yj;\n float dz = zi - zj;\n result = dx*dx + dy*dy + dz*dz; // only last tile's? NO, bug\n }\n }\n }\n __syncthreads();\n }\n if (i < N && j < N) {\n dist[b * N * N + i * N + j] = sqrtf(result + 1e-8f);\n }\n}\n\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords) {\n int B = coords.size(0);\n int N = coords.size(1);\n auto dist = torch::empty({B, N, N}, coords.options());\n \n dim3 blocks((N + TILE - 1) / TILE, (N + TILE - 1) / TILE, B);\n dim3 threads(TILE, TILE);\n \n pairwise_dist_kernel<<<blocks, threads>>>(\n coords.data_ptr<float>(),\n dist.data_ptr<float>(),\n B, N\n );\n return dist;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords);\n'''\n\nmodule = load_inline(\n name='pairwise_dist',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['pairwise_dist_cuda'],\n verbose=True,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n \n def forward(self, coords):\n return self.module.pairwise_dist_cuda(coords)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 6159.4s turn · 8.5s LLM · 150.9s tools · tokens in=6,327 · out=690 · total=7,133
reasoning
Still has bug in the accumulation. The result is overwritten only in inner loop, and only last.
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\n#define TILE 16\n\n__global__ void pairwise_dist_kernel(const float* __restrict__ coords, float* __restrict__ dist, int B, int N) {\n __shared__ float s_i[TILE * 3];\n __shared__ float s_j[TILE * 3];\n \n int b = blockIdx.z;\n int tile_i = blockIdx.y;\n int tile_j = blockIdx.x;\n \n int local_i = threadIdx.y;\n int local_j = threadIdx.x;\n \n int i = tile_i * TILE + local_i;\n int j = tile_j * TILE + local_j;\n \n float xi = 0, yi = 0, zi = 0;\n if (i < N) {\n xi = coords[b * N * 3 + i * 3 + 0];\n yi = coords[b * N * 3 + i * 3 + 1];\n zi = coords[b * N * 3 + i * 3 + 2];\n }\n \n // Load i tile? But since per thread i fixed, can use register.\n \n float xj = 0, yj = 0, zj = 0;\n if (j < N) {\n xj = coords[b * N * 3 + j * 3 + 0];\n yj = coords[b * N * 3 + j * 3 + 1];\n zj = coords[b * N * 3 + j * 3 + 2];\n }\n \n float dx = xi - xj;\n float dy = yi - yj;\n float dz = zi - zj;\n float d2 = dx*dx + dy*dy + dz*dz;\n \n if (i < N && j < N) {\n dist[b * N * N + i * N + j] = sqrtf(d2 + 1e-8f);\n }\n}\n\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords) {\n int B = coords.size(0);\n int N = coords.size(1);\n auto dist = torch::empty({B, N, N}, coords.options());\n \n dim3 blocks((N + TILE - 1) / TILE, (N + TILE - 1) / TILE, B);\n dim3 threads(TILE, TILE);\n \n pairwise_dist_kernel<<<blocks, threads>>>(\n coords.data_ptr<float>(),\n dist.data_ptr<float>(),\n B, N\n );\n return dist;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords);\n'''\n\nmodule = load_inline(\n name='pairwise_dist',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['pairwise_dist_cuda'],\n verbose=True,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n \n def forward(self, coords):\n return self.module.pairwise_dist_cuda(coords)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 7274.6s turn · 4.6s LLM · 270.0s tools · tokens in=7,150 · out=673 · total=7,866
reasoning
This is not using shared, basically same as naive but with 2D blocks.
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\n#define TILE 16\n\n__global__ void pairwise_dist_kernel(const float* __restrict__ coords, float* __restrict__ dist, int B, int N) {\n __shared__ float s_i[TILE * 3];\n __shared__ float s_j[TILE * 3];\n \n int b = blockIdx.z;\n int tile_i = blockIdx.y;\n int tile_j = blockIdx.x;\n \n int local_i = threadIdx.y;\n int local_j = threadIdx.x;\n \n int i = tile_i * TILE + local_i;\n int j = tile_j * TILE + local_j;\n \n float xi = 0, yi = 0, zi = 0;\n if (i < N) {\n xi = coords[b * N * 3 + i * 3 + 0];\n yi = coords[b * N * 3 + i * 3 + 1];\n zi = coords[b * N * 3 + i * 3 + 2];\n }\n \n float xj = 0, yj = 0, zj = 0;\n if (j < N) {\n xj = coords[b * N * 3 + j * 3 + 0];\n yj = coords[b * N * 3 + j * 3 + 1];\n zj = coords[b * N * 3 + j * 3 + 2];\n }\n \n float dx = xi - xj;\n float dy = yi - yj;\n float dz = zi - zj;\n float d2 = dx*dx + dy*dy + dz*dz;\n \n if (i < N && j < N) {\n dist[b * N * N + i * N + j] = sqrtf(d2 + 1e-8f);\n }\n}\n\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords) {\n int B = coords.size(0);\n int N = coords.size(1);\n auto dist = torch::empty({B, N, N}, coords.options());\n \n dim3 blocks((N + TILE - 1) / TILE, (N + TILE - 1) / TILE, B);\n dim3 threads(TILE, TILE);\n \n pairwise_dist_kernel<<<blocks, threads>>>(\n coords.data_ptr<float>(),\n dist.data_ptr<float>(),\n B, N\n );\n return dist;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords);\n'''\n\nmodule = load_inline(\n name='pairwise_dist',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['pairwise_dist_cuda'],\n verbose=True,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n \n def forward(self, coords):\n return self.module.pairwise_dist_cuda(coords)\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (3 / 3) trials all matched the reference.
Numerical precision: max_abs_err=4.77e-07 mean_abs_err=2.82e-08 max_rel_err=1.65e-07
turn 8239.6s turn · 5.5s LLM · 234.2s tools · tokens in=7,926 · out=645 · total=8,628 · FINAL
reasoning
Good, correct. This should be faster because more threads, better occupancy.
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\n#define TILE 16\n\n__global__ void pairwise_dist_kernel(const float* __restrict__ coords, float* __restrict__ dist, int B, int N) {\n int b = blockIdx.z;\n int tile_i = blockIdx.y;\n int tile_j = blockIdx.x;\n \n int local_i = threadIdx.y;\n int local_j = threadIdx.x;\n \n int i = tile_i * TILE + local_i;\n int j = tile_j * TILE + local_j;\n \n float xi = 0, yi = 0, zi = 0;\n if (i < N) {\n xi = coords[b * N * 3 + i * 3 + 0];\n yi = coords[b * N * 3 + i * 3 + 1];\n zi = coords[b * N * 3 + i * 3 + 2];\n }\n \n float xj = 0, yj = 0, zj = 0;\n if (j < N) {\n xj = coords[b * N * 3 + j * 3 + 0];\n yj = coords[b * N * 3 + j * 3 + 1];\n zj = coords[b * N * 3 + j * 3 + 2];\n }\n \n float dx = xi - xj;\n float dy = yi - yj;\n float dz = zi - zj;\n float d2 = dx*dx + dy*dy + dz*dz;\n \n if (i < N && j < N) {\n dist[b * N * N + i * N + j] = sqrtf(d2 + 1e-8f);\n }\n}\n\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords) {\n int B = coords.size(0);\n int N = coords.size(1);\n auto dist = torch::empty({B, N, N}, coords.options());\n \n dim3 blocks((N + TILE - 1) / TILE, (N + TILE - 1) / TILE, B);\n dim3 threads(TILE, TILE);\n \n pairwise_dist_kernel<<<blocks, threads>>>(\n coords.data_ptr<float>(),\n dist.data_ptr<float>(),\n B, N\n );\n return dist;\n}\n'''\n\ncpp_source = '''\ntorch::Tensor pairwise_dist_cuda(torch::Tensor coords);\n'''\n\nmodule = load_inline(\n name='pairwise_dist',\n cpp_sources=[cpp_source],\n cuda_sources=[cuda_source],\n functions=['pairwise_dist_cuda'],\n verbose=True,\n extra_cuda_cflags=['-O3']\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n self.module = module\n \n def forward(self, coords):\n return self.module.pairwise_dist_cuda(coords)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (3 / 3) correctness trials all passed.
Kernel runtime: 0.01 μs
Runtime stats: mean=0.01μs median=?μs std=0.00μs
Numerical precision: max_abs_err=4.77e-07 mean_abs_err=2.82e-08 max_rel_err=1.65e-07
Memory: 41.05 MB peak (ref: 89.05 MB, ratio: 0.461x)
Kernel launches: 1 (ref: 12, fusion ratio: 12.0)
SOL score: 0.2109
final submitted kernel (84 lines, 2,045 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>
#define TILE 16
__global__ void pairwise_dist_kernel(const float* __restrict__ coords, float* __restrict__ dist, int B, int N) {
int b = blockIdx.z;
int tile_i = blockIdx.y;
int tile_j = blockIdx.x;
int local_i = threadIdx.y;
int local_j = threadIdx.x;
int i = tile_i * TILE + local_i;
int j = tile_j * TILE + local_j;
float xi = 0, yi = 0, zi = 0;
if (i < N) {
xi = coords[b * N * 3 + i * 3 + 0];
yi = coords[b * N * 3 + i * 3 + 1];
zi = coords[b * N * 3 + i * 3 + 2];
}
float xj = 0, yj = 0, zj = 0;
if (j < N) {
xj = coords[b * N * 3 + j * 3 + 0];
yj = coords[b * N * 3 + j * 3 + 1];
zj = coords[b * N * 3 + j * 3 + 2];
}
float dx = xi - xj;
float dy = yi - yj;
float dz = zi - zj;
float d2 = dx*dx + dy*dy + dz*dz;
if (i < N && j < N) {
dist[b * N * N + i * N + j] = sqrtf(d2 + 1e-8f);
}
}
torch::Tensor pairwise_dist_cuda(torch::Tensor coords) {
int B = coords.size(0);
int N = coords.size(1);
auto dist = torch::empty({B, N, N}, coords.options());
dim3 blocks((N + TILE - 1) / TILE, (N + TILE - 1) / TILE, B);
dim3 threads(TILE, TILE);
pairwise_dist_kernel<<<blocks, threads>>>(
coords.data_ptr<float>(),
dist.data_ptr<float>(),
B, N
);
return dist;
}
'''
cpp_source = '''
torch::Tensor pairwise_dist_cuda(torch::Tensor coords);
'''
module = load_inline(
name='pairwise_dist',
cpp_sources=[cpp_source],
cuda_sources=[cuda_source],
functions=['pairwise_dist_cuda'],
verbose=True,
extra_cuda_cflags=['-O3']
)
class ModelNew(nn.Module):
def __init__(self):
super().__init__()
self.module = module
def forward(self, coords):
return self.module.pairwise_dist_cuda(coords)