correct
33_VectorizedPoseTransformFused.py
started
2026-05-11T21:17:06Z
timingkernelbench.timing
kernel mean / median / std
0.01 / — / 1.82e-04 μs
ref mean / median / std
0.33 / — / 0.02 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
91.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
import torch.nn.functional as F
# Default ModelNew is PyTorch-only. run_and_check: use check_kernel=False with backend=cuda
# until the candidate file contains a real CUDA kernel (see kernel_static_checker).
def _quaternion_to_rotation_matrix(q: torch.Tensor) -> torch.Tensor:
"""
Unit quaternion ``q = (w, x, y, z)`` (scalar-first) to rotation matrix ``R``,
shape ``(B, 3, 3)``, with ``p' = p @ R.T`` for row vectors ``p`` (``N×3``).
"""
q = F.normalize(q, dim=-1, eps=1e-8)
w, x, y, z = q.unbind(dim=-1)
xx = x * x
yy = y * y
zz = z * z
xy = x * y
xz = x * z
yz = y * z
wx = w * x
wy = w * y
wz = w * z
m00 = 1.0 - 2.0 * (yy + zz)
m01 = 2.0 * (xy - wz)
m02 = 2.0 * (xz + wy)
m10 = 2.0 * (xy + wz)
m11 = 1.0 - 2.0 * (xx + zz)
m12 = 2.0 * (yz - wx)
m20 = 2.0 * (xz - wy)
m21 = 2.0 * (yz + wx)
m22 = 1.0 - 2.0 * (xx + yy)
row0 = torch.stack((m00, m01, m02), dim=-1)
row1 = torch.stack((m10, m11, m12), dim=-1)
row2 = torch.stack((m20, m21, m22), dim=-1)
return torch.stack((row0, row1, row2), dim=-2)
class Model(nn.Module):
"""
**Fused vectorized pose transform**: rotation from unit quaternion ``q`` and
translation ``t`` applied to a batch of 3D points.
One forward fuses **quaternion → ``3×3``** (Hamilton, scalar-first ``w,x,y,z``)
with **batched matmul** and **broadcast translation**—typical for LiDAR / camera
extrinsics, hand-eye calibration, and world ↔ body frame changes in VLA stacks.
``points' = points @ R.T + t`` with ``points`` row-wise ``(B, N, 3)``.
"""
def __init__(self):
super().__init__()
def forward(
self,
points: torch.Tensor,
quat: torch.Tensor,
trans: torch.Tensor,
) -> torch.Tensor:
"""
Args:
points: (B, N, 3) points in the **source** frame.
quat: (B, 4) unit quaternion ``(w, x, y, z)`` mapping source → target rotation.
trans: (B, 3) translation in the **target** frame (same convention as ``R``).
Returns:
(B, N, 3) points in the **target** frame.
"""
B, N, _ = points.shape
assert quat.shape == (B, 4) and trans.shape == (B, 3)
R = _quaternion_to_rotation_matrix(quat)
return torch.matmul(points, R.transpose(-1, -2)) + trans.unsqueeze(1)
class ModelNew(nn.Module):
"""KernelBench candidate entry point; default delegates to ``Model``."""
def __init__(self):
super().__init__()
self._impl = Model()
def forward(
self,
points: torch.Tensor,
quat: torch.Tensor,
trans: torch.Tensor,
) -> torch.Tensor:
return self._impl(points, quat, trans)
batch_size = 8
num_points = 512
def get_inputs():
p = popcorn_pri
points = torch.randn(p.jitter_int(batch_size), p.jitter_int(num_points), 3)
quat = torch.randn(p.jitter_int(batch_size), 4)
trans = torch.randn(p.jitter_int(batch_size), 3)
return [points, quat, trans]
def get_init_inputs():
return []
turn 0134.4s turn · 6.6s LLM · 127.9s tools · tokens in=2,712 · out=6 · total=3,104
reasoning
The task is to replace the forward computation with a custom CUDA kernel that's faster than the PyTorch reference.
The task involves replacing the forward computation of a PyTorch model 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 1127.3s turn · 12.2s LLM · 115.1s tools · tokens in=3,337 · out=1,041 · total=4,418
reasoning
It's H100, high bandwidth.
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__device__ __forceinline__ void quaternion_to_rotation_matrix(const float* q, float* R) {\n float qw = q[0], qx = q[1], qy = q[2], qz = q[3];\n float norm = sqrtf(qw*qw + qx*qx + qy*qy + qz*qz);\n qw /= norm; qx /= norm; qy /= norm; qz /= norm;\n \n float xx = qx * qx;\n float yy = qy * qy;\n float zz = qz * qz;\n float xy = qx * qy;\n float xz = qx * qz;\n float yz = qy * qz;\n float wx = qw * qx;\n float wy = qw * qy;\n float wz = qw * qz;\n \n R[0] = 1.0f - 2.0f * (yy + zz);\n R[1] = 2.0f * (xy - wz);\n R[2] = 2.0f * (xz + wy);\n R[3] = 2.0f * (xy + wz);\n R[4] = 1.0f - 2.0f * (xx + zz);\n R[5] = 2.0f * (yz - wx);\n R[6] = 2.0f * (xz - wy);\n R[7] = 2.0f * (yz + wx);\n R[8] = 1.0f - 2.0f * (xx + yy);\n}\n\n__global__ void fused_transform_kernel(\n const float* __restrict__ points,\n const float* __restrict__ quats,\n const float* __restrict__ trans,\n float* __restrict__ output,\n int B, int N\n) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * N;\n if (idx >= total) return;\n \n int b = idx / N;\n int i = idx % N;\n \n float R[9];\n quaternion_to_rotation_matrix(quats + b * 4, R);\n \n float px = points[(b * N + i) * 3 + 0];\n float py = points[(b * N + i) * 3 + 1];\n float pz = points[(b * N + i) * 3 + 2];\n \n float tx = trans[b * 3 + 0];\n float ty = trans[b * 3 + 1];\n float tz = trans[b * 3 + 2];\n \n // points @ R.T i.e. dot with columns of R\n float ox = px * R[0] + py * R[3] + pz * R[6] + tx;\n float oy = px * R[1] + py * R[4] + pz * R[7] + ty;\n float oz = px * R[2] + py * R[5] + pz * R[8] + tz;\n \n output[(b * N + i) * 3 + 0] = ox;\n output[(b * N + i) * 3 + 1] = oy;\n output[(b * N + i) * 3 + 2] = oz;\n}\n\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans) {\n int B = points.size(0);\n int N = points.size(1);\n \n auto output = torch::empty({B, N, 3}, points.options());\n \n int total = B * N;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n \n fused_transform_kernel<<<blocks, threads>>>(\n points.data_ptr<float>(),\n quat.data_ptr<float>(),\n trans.data_ptr<float>(),\n output.data_ptr<float>(),\n B, N\n );\n \n return output;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans);\n\"\"\"\n\nmodule = load_inline(\n name=\"fused_transform\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"fused_transform_cuda\"],\n verbose=True\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n \n def forward(self, points, quat, trans):\n return module.fused_transform_cuda(points, quat, trans)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 2259.8s turn · 11.0s LLM · 248.8s tools · tokens in=4,435 · out=1,042 · total=5,484
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__device__ __forceinline__ void quaternion_to_rotation_matrix(const float* q, float* R) {\n float qw = q[0], qx = q[1], qy = q[2], qz = q[3];\n float norm = sqrtf(qw*qw + qx*qx + qy*qy + qz*qz);\n qw /= norm; qx /= norm; qy /= norm; qz /= norm;\n \n float xx = qx * qx;\n float yy = qy * qy;\n float zz = qz * qz;\n float xy = qx * qy;\n float xz = qx * qz;\n float yz = qy * qz;\n float wx = qw * qx;\n float wy = qw * qy;\n float wz = qw * qz;\n \n R[0] = 1.0f - 2.0f * (yy + zz);\n R[1] = 2.0f * (xy - wz);\n R[2] = 2.0f * (xz + wy);\n R[3] = 2.0f * (xy + wz);\n R[4] = 1.0f - 2.0f * (xx + zz);\n R[5] = 2.0f * (yz - wx);\n R[6] = 2.0f * (xz - wy);\n R[7] = 2.0f * (yz + wx);\n R[8] = 1.0f - 2.0f * (xx + yy);\n}\n\n__global__ void fused_transform_kernel(\n const float* __restrict__ points,\n const float* __restrict__ quats,\n const float* __restrict__ trans,\n float* __restrict__ output,\n int B, int N\n) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * N;\n if (idx >= total) return;\n \n int b = idx / N;\n int i = idx % N;\n \n float R[9];\n quaternion_to_rotation_matrix(quats + b * 4, R);\n \n float px = points[(b * N + i) * 3 + 0];\n float py = points[(b * N + i) * 3 + 1];\n float pz = points[(b * N + i) * 3 + 2];\n \n float tx = trans[b * 3 + 0];\n float ty = trans[b * 3 + 1];\n float tz = trans[b * 3 + 2];\n \n // points @ R.T i.e. dot with columns of R\n float ox = px * R[0] + py * R[3] + pz * R[6] + tx;\n float oy = px * R[1] + py * R[4] + pz * R[7] + ty;\n float oz = px * R[2] + py * R[5] + pz * R[8] + tz;\n \n output[(b * N + i) * 3 + 0] = ox;\n output[(b * N + i) * 3 + 1] = oy;\n output[(b * N + i) * 3 + 2] = oz;\n}\n\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans) {\n int B = points.size(0);\n int N = points.size(1);\n \n auto output = torch::empty({B, N, 3}, points.options());\n \n int total = B * N;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n \n fused_transform_kernel<<<blocks, threads>>>(\n points.data_ptr<float>(),\n quat.data_ptr<float>(),\n trans.data_ptr<float>(),\n output.data_ptr<float>(),\n B, N\n );\n \n return output;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans);\n\"\"\"\n\nmodule = load_inline(\n name=\"fused_transform\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"fused_transform_cuda\"],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n \n def forward(self, points, quat, trans):\n return module.fused_transform_cuda(points, quat, trans)\n"
}
result ← run_correctness (fail)
failrun_correctness
run_correctness FAILED: (0 / 3) trials did not all match.
correctness_issue: Output mismatch
max_difference: ['6.001563', '7.109252', '6.445373']
avg_difference: ['0.595853', '0.816774', '0.677133']
turn 3225.2s turn · 15.3s LLM · 209.9s tools · tokens in=5,557 · out=1,040 · total=7,058
reasoning
There's a bug. The rotation matrix application is wrong?
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__device__ __forceinline__ void quaternion_to_rotation_matrix(const float* q, float* R) {\n float qw = q[0], qx = q[1], qy = q[2], qz = q[3];\n float norm = sqrtf(qw*qw + qx*qx + qy*qy + qz*qz);\n qw /= norm; qx /= norm; qy /= norm; qz /= norm;\n \n float xx = qx * qx;\n float yy = qy * qy;\n float zz = qz * qz;\n float xy = qx * qy;\n float xz = qx * qz;\n float yz = qy * qz;\n float wx = qw * qx;\n float wy = qw * qy;\n float wz = qw * qz;\n \n R[0] = 1.0f - 2.0f * (yy + zz);\n R[1] = 2.0f * (xy - wz);\n R[2] = 2.0f * (xz + wy);\n R[3] = 2.0f * (xy + wz);\n R[4] = 1.0f - 2.0f * (xx + zz);\n R[5] = 2.0f * (yz - wx);\n R[6] = 2.0f * (xz - wy);\n R[7] = 2.0f * (yz + wx);\n R[8] = 1.0f - 2.0f * (xx + yy);\n}\n\n__global__ void fused_transform_kernel(\n const float* __restrict__ points,\n const float* __restrict__ quats,\n const float* __restrict__ trans,\n float* __restrict__ output,\n int B, int N\n) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * N;\n if (idx >= total) return;\n \n int b = idx / N;\n int i = idx % N;\n \n float R[9];\n quaternion_to_rotation_matrix(quats + b * 4, R);\n \n float px = points[(b * N + i) * 3 + 0];\n float py = points[(b * N + i) * 3 + 1];\n float pz = points[(b * N + i) * 3 + 2];\n \n float tx = trans[b * 3 + 0];\n float ty = trans[b * 3 + 1];\n float tz = trans[b * 3 + 2];\n \n // points @ R wait no: p dot rows of R\n float ox = px * R[0] + py * R[1] + pz * R[2] + tx;\n float oy = px * R[3] + py * R[4] + pz * R[5] + ty;\n float oz = px * R[6] + py * R[7] + pz * R[8] + tz;\n \n output[(b * N + i) * 3 + 0] = ox;\n output[(b * N + i) * 3 + 1] = oy;\n output[(b * N + i) * 3 + 2] = oz;\n}\n\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans) {\n int B = points.size(0);\n int N = points.size(1);\n \n auto output = torch::empty({B, N, 3}, points.options());\n \n int total = B * N;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n \n fused_transform_kernel<<<blocks, threads>>>(\n points.data_ptr<float>(),\n quat.data_ptr<float>(),\n trans.data_ptr<float>(),\n output.data_ptr<float>(),\n B, N\n );\n \n return output;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans);\n\"\"\"\n\nmodule = load_inline(\n name=\"fused_transform\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"fused_transform_cuda\"],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n \n def forward(self, points, quat, trans):\n return module.fused_transform_cuda(points, quat, trans)\n"
}
result ← compile_kernel (ok)
okcompile_kernel
compile_kernel PASSED: kernel compiled without errors.
turn 4243.7s turn · 9.7s LLM · 234.0s tools · tokens in=7,075 · out=1,026 · total=8,106
reasoning
Now test correctness again.
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__device__ __forceinline__ void quaternion_to_rotation_matrix(const float* q, float* R) {\n float qw = q[0], qx = q[1], qy = q[2], qz = q[3];\n float norm = sqrtf(qw*qw + qx*qx + qy*qy + qz*qz);\n qw /= norm; qx /= norm; qy /= norm; qz /= norm;\n \n float xx = qx * qx;\n float yy = qy * qy;\n float zz = qz * qz;\n float xy = qx * qy;\n float xz = qx * qz;\n float yz = qy * qz;\n float wx = qw * qx;\n float wy = qw * qy;\n float wz = qw * qz;\n \n R[0] = 1.0f - 2.0f * (yy + zz);\n R[1] = 2.0f * (xy - wz);\n R[2] = 2.0f * (xz + wy);\n R[3] = 2.0f * (xy + wz);\n R[4] = 1.0f - 2.0f * (xx + zz);\n R[5] = 2.0f * (yz - wx);\n R[6] = 2.0f * (xz - wy);\n R[7] = 2.0f * (yz + wx);\n R[8] = 1.0f - 2.0f * (xx + yy);\n}\n\n__global__ void fused_transform_kernel(\n const float* __restrict__ points,\n const float* __restrict__ quats,\n const float* __restrict__ trans,\n float* __restrict__ output,\n int B, int N\n) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * N;\n if (idx >= total) return;\n \n int b = idx / N;\n int i = idx % N;\n \n float R[9];\n quaternion_to_rotation_matrix(quats + b * 4, R);\n \n float px = points[(b * N + i) * 3 + 0];\n float py = points[(b * N + i) * 3 + 1];\n float pz = points[(b * N + i) * 3 + 2];\n \n float tx = trans[b * 3 + 0];\n float ty = trans[b * 3 + 1];\n float tz = trans[b * 3 + 2];\n \n float ox = px * R[0] + py * R[1] + pz * R[2] + tx;\n float oy = px * R[3] + py * R[4] + pz * R[5] + ty;\n float oz = px * R[6] + py * R[7] + pz * R[8] + tz;\n \n output[(b * N + i) * 3 + 0] = ox;\n output[(b * N + i) * 3 + 1] = oy;\n output[(b * N + i) * 3 + 2] = oz;\n}\n\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans) {\n int B = points.size(0);\n int N = points.size(1);\n \n auto output = torch::empty({B, N, 3}, points.options());\n \n int total = B * N;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n \n fused_transform_kernel<<<blocks, threads>>>(\n points.data_ptr<float>(),\n quat.data_ptr<float>(),\n trans.data_ptr<float>(),\n output.data_ptr<float>(),\n B, N\n );\n \n return output;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans);\n\"\"\"\n\nmodule = load_inline(\n name=\"fused_transform\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"fused_transform_cuda\"],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n \n def forward(self, points, quat, trans):\n return module.fused_transform_cuda(points, quat, trans)\n"
}
result ← run_correctness (ok)
okrun_correctness
run_correctness PASSED: (3 / 3) trials all matched the reference.
Numerical precision: max_abs_err=1.67e-06 mean_abs_err=5.82e-08 max_rel_err=2.62e-03
turn 5403.1s turn · 9.8s LLM · 393.3s tools · tokens in=8,166 · out=1,025 · total=9,267 · FINAL
reasoning
Correct now. To check if faster, I could profile, but since the task requires submitting when confident it's faster, and this is a simple fused kernel without redundant computations, it should be fast...
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__device__ __forceinline__ void quaternion_to_rotation_matrix(const float* q, float* R) {\n float qw = q[0], qx = q[1], qy = q[2], qz = q[3];\n float norm = sqrtf(qw*qw + qx*qx + qy*qy + qz*qz);\n qw /= norm; qx /= norm; qy /= norm; qz /= norm;\n \n float xx = qx * qx;\n float yy = qy * qy;\n float zz = qz * qz;\n float xy = qx * qy;\n float xz = qx * qz;\n float yz = qy * qz;\n float wx = qw * qx;\n float wy = qw * qy;\n float wz = qw * qz;\n \n R[0] = 1.0f - 2.0f * (yy + zz);\n R[1] = 2.0f * (xy - wz);\n R[2] = 2.0f * (xz + wy);\n R[3] = 2.0f * (xy + wz);\n R[4] = 1.0f - 2.0f * (xx + zz);\n R[5] = 2.0f * (yz - wx);\n R[6] = 2.0f * (xz - wy);\n R[7] = 2.0f * (yz + wx);\n R[8] = 1.0f - 2.0f * (xx + yy);\n}\n\n__global__ void fused_transform_kernel(\n const float* __restrict__ points,\n const float* __restrict__ quats,\n const float* __restrict__ trans,\n float* __restrict__ output,\n int B, int N\n) {\n int idx = blockIdx.x * blockDim.x + threadIdx.x;\n int total = B * N;\n if (idx >= total) return;\n \n int b = idx / N;\n int i = idx % N;\n \n float R[9];\n quaternion_to_rotation_matrix(quats + b * 4, R);\n \n float px = points[(b * N + i) * 3 + 0];\n float py = points[(b * N + i) * 3 + 1];\n float pz = points[(b * N + i) * 3 + 2];\n \n float tx = trans[b * 3 + 0];\n float ty = trans[b * 3 + 1];\n float tz = trans[b * 3 + 2];\n \n float ox = px * R[0] + py * R[1] + pz * R[2] + tx;\n float oy = px * R[3] + py * R[4] + pz * R[5] + ty;\n float oz = px * R[6] + py * R[7] + pz * R[8] + tz;\n \n output[(b * N + i) * 3 + 0] = ox;\n output[(b * N + i) * 3 + 1] = oy;\n output[(b * N + i) * 3 + 2] = oz;\n}\n\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans) {\n int B = points.size(0);\n int N = points.size(1);\n \n auto output = torch::empty({B, N, 3}, points.options());\n \n int total = B * N;\n int threads = 256;\n int blocks = (total + threads - 1) / threads;\n \n fused_transform_kernel<<<blocks, threads>>>(\n points.data_ptr<float>(),\n quat.data_ptr<float>(),\n trans.data_ptr<float>(),\n output.data_ptr<float>(),\n B, N\n );\n \n return output;\n}\n\"\"\"\n\ncpp_source = \"\"\"\ntorch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans);\n\"\"\"\n\nmodule = load_inline(\n name=\"fused_transform\",\n cpp_sources=cpp_source,\n cuda_sources=cuda_source,\n functions=[\"fused_transform_cuda\"],\n verbose=False\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super().__init__()\n \n def forward(self, points, quat, trans):\n return module.fused_transform_cuda(points, quat, trans)\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=1.67e-06 mean_abs_err=5.82e-08 max_rel_err=2.62e-03
Memory: 33.09 MB peak (ref: 33.14 MB, ratio: 0.9986x)
Kernel launches: 1 (ref: 91, fusion ratio: 91.0)
SOL score: 0.0572
Flagged for excessive speedup — this submission may have been rejected by automated review.
final submitted kernel (110 lines, 2,947 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>
__device__ __forceinline__ void quaternion_to_rotation_matrix(const float* q, float* R) {
float qw = q[0], qx = q[1], qy = q[2], qz = q[3];
float norm = sqrtf(qw*qw + qx*qx + qy*qy + qz*qz);
qw /= norm; qx /= norm; qy /= norm; qz /= norm;
float xx = qx * qx;
float yy = qy * qy;
float zz = qz * qz;
float xy = qx * qy;
float xz = qx * qz;
float yz = qy * qz;
float wx = qw * qx;
float wy = qw * qy;
float wz = qw * qz;
R[0] = 1.0f - 2.0f * (yy + zz);
R[1] = 2.0f * (xy - wz);
R[2] = 2.0f * (xz + wy);
R[3] = 2.0f * (xy + wz);
R[4] = 1.0f - 2.0f * (xx + zz);
R[5] = 2.0f * (yz - wx);
R[6] = 2.0f * (xz - wy);
R[7] = 2.0f * (yz + wx);
R[8] = 1.0f - 2.0f * (xx + yy);
}
__global__ void fused_transform_kernel(
const float* __restrict__ points,
const float* __restrict__ quats,
const float* __restrict__ trans,
float* __restrict__ output,
int B, int N
) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int total = B * N;
if (idx >= total) return;
int b = idx / N;
int i = idx % N;
float R[9];
quaternion_to_rotation_matrix(quats + b * 4, R);
float px = points[(b * N + i) * 3 + 0];
float py = points[(b * N + i) * 3 + 1];
float pz = points[(b * N + i) * 3 + 2];
float tx = trans[b * 3 + 0];
float ty = trans[b * 3 + 1];
float tz = trans[b * 3 + 2];
float ox = px * R[0] + py * R[1] + pz * R[2] + tx;
float oy = px * R[3] + py * R[4] + pz * R[5] + ty;
float oz = px * R[6] + py * R[7] + pz * R[8] + tz;
output[(b * N + i) * 3 + 0] = ox;
output[(b * N + i) * 3 + 1] = oy;
output[(b * N + i) * 3 + 2] = oz;
}
torch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans) {
int B = points.size(0);
int N = points.size(1);
auto output = torch::empty({B, N, 3}, points.options());
int total = B * N;
int threads = 256;
int blocks = (total + threads - 1) / threads;
fused_transform_kernel<<<blocks, threads>>>(
points.data_ptr<float>(),
quat.data_ptr<float>(),
trans.data_ptr<float>(),
output.data_ptr<float>(),
B, N
);
return output;
}
"""
cpp_source = """
torch::Tensor fused_transform_cuda(torch::Tensor points, torch::Tensor quat, torch::Tensor trans);
"""
module = load_inline(
name="fused_transform",
cpp_sources=cpp_source,
cuda_sources=cuda_source,
functions=["fused_transform_cuda"],
verbose=False
)
class ModelNew(nn.Module):
def __init__(self):
super().__init__()
def forward(self, points, quat, trans):
return module.fused_transform_cuda(points, quat, trans)