correct
33_VanillaRNN.py
started
2026-05-05T02:10:29Z
timingkernelbench.timing
kernel mean / median / std
6.85 / — / 0.03 μs
ref mean / median / std
6.86 / — / 0.03 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
0.67x
energyNVML (pynvml)
ratio (ref / kernel)
1.17x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
2252.23 GB/s
warp stalls (top 5)nsight
reference PyTorch source (the prompt the agent sees)
import torch
import torch.nn as nn
class Model(nn.Module):
def __init__(self, input_size: int, hidden_size: int, output_size: int):
"""
Initialize the Vanilla RNN model.
:param input_size: The number of input features (int).
:param hidden_size: The size of the hidden state (int).
:param output_size: The number of output features (int).
"""
super(Model, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.hidden = torch.randn((batch_size, hidden_size))
# Define the RNN cell components (input to hidden, hidden to hidden, and hidden to output)
self.i2h = nn.Linear(input_size + hidden_size, hidden_size) # Input to hidden
self.h2o = nn.Linear(hidden_size, output_size) # Hidden to output
self.tanh = nn.Tanh() # Activation function for hidden state
def forward(self, x: torch.Tensor, initial_hidden=None) -> torch.Tensor:
"""
Forward pass of the Vanilla RNN.
:param x: Input tensor of shape (batch_size, input_size).
:param hidden: Hidden state tensor of shape (batch_size, hidden_size).
:return: Output tensor of shape (batch_size, output_size), and the new hidden state.
"""
if initial_hidden is not None:
self.hidden.copy_(initial_hidden)
self.hidden = self.hidden.to(x.device)
combined = torch.cat((x, self.hidden), dim=1) # Concatenate input and hidden state
self.hidden = self.tanh(self.i2h(combined)) # Update hidden state
output = self.h2o(self.hidden) # Compute output
return output
batch_size = 256
input_size = 16384
hidden_size = 16384
output_size = 8192
sequence_length = 256
def get_inputs():
return [torch.rand(batch_size, input_size),torch.rand(batch_size, hidden_size)]
def get_init_inputs():
return [input_size, hidden_size, output_size]
turn 064.8s turn · 64.8s LLM · 0.0s tools · FINAL
reasoning
**Evaluating tolerance levels**
I need to ensure a tolerance of 1e-4, either absolute or relative. It seems like the TF32 PyTorch default likely uses TF32 for matrix multiplication on Ampere, rather than the exact fp32. If I want precision, I can implement cuBLAS using at::linear, which should yield the same results as expected. I'm just double-checking my options to make sure I understand the implications of the different settings before proceeding.
call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n\n_cpp_src = r'''\n#include <torch/extension.h>\n#include <vector>\n\nvoid tanh_inplace_cuda(torch::Tensor t);\n\nstd::vector<torch::Tensor> rnn_forward_cuda(torch::Tensor x,\n torch::Tensor hidden,\n torch::Tensor w1,\n torch::Tensor b1,\n torch::Tensor w2,\n torch::Tensor b2,\n int64_t input_size) {\n TORCH_CHECK(x.is_cuda(), \"x must be CUDA\");\n TORCH_CHECK(hidden.is_cuda(), \"hidden must be CUDA\");\n TORCH_CHECK(w1.is_cuda() && b1.is_cuda() && w2.is_cuda() && b2.is_cuda(), \"weights must be CUDA\");\n TORCH_CHECK(x.scalar_type() == at::kFloat, \"only fp32 supported\");\n\n // Avoid materializing torch.cat([x, hidden], 1). The reference linear is\n // equivalent to two GEMMs accumulated into the same output. This removes\n // a 256 x 32768 temporary and one full memory pass over it.\n auto wx = w1.narrow(1, 0, input_size);\n auto wh = w1.narrow(1, input_size, w1.size(1) - input_size);\n\n auto hnew = at::addmm(b1, x, wx.t());\n hnew.addmm_(hidden, wh.t(), 1.0, 1.0);\n\n tanh_inplace_cuda(hnew);\n\n auto out = at::addmm(b2, hnew, w2.t());\n return {out, hnew};\n}\n'''\n\n_cuda_src = r'''\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <c10/cuda/CUDAException.h>\n#include <stdint.h>\n\n__global__ void tanh_inplace_kernel(float* __restrict__ p, int64_t n) {\n int64_t i = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n for (; i < n; i += stride) {\n p[i] = tanhf(p[i]);\n }\n}\n\nvoid tanh_inplace_cuda(torch::Tensor t) {\n const int64_t n = t.numel();\n if (n == 0) return;\n const int threads = 256;\n int blocks = (int)((n + threads - 1) / threads);\n if (blocks > 65535) blocks = 65535;\n tanh_inplace_kernel<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(t.data_ptr<float>(), n);\n C10_CUDA_KERNEL_LAUNCH_CHECK();\n}\n'''\n\n_ext = load_inline(\n name=\"rnn_split_tanh_ext\",\n cpp_sources=_cpp_src,\n cuda_sources=_cuda_src,\n functions=[\"rnn_forward_cuda\"],\n extra_cflags=[\"-O3\"],\n extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"],\n verbose=False,\n)\n\n\nclass ModelNew(nn.Module):\n def __init__(self, input_size: int, hidden_size: int, output_size: int):\n super(ModelNew, self).__init__()\n self.input_size = input_size\n self.hidden_size = hidden_size\n self.output_size = output_size\n self.hidden = torch.randn((256, hidden_size))\n self.i2h = nn.Linear(input_size + hidden_size, hidden_size)\n self.h2o = nn.Linear(hidden_size, output_size)\n self.tanh = nn.Tanh()\n\n def forward(self, x: torch.Tensor, initial_hidden=None) -> torch.Tensor:\n if initial_hidden is not None:\n if self.hidden.device != initial_hidden.device:\n self.hidden = self.hidden.to(initial_hidden.device)\n self.hidden.copy_(initial_hidden)\n if self.hidden.device != x.device:\n self.hidden = self.hidden.to(x.device)\n\n # Ensure the common fast path: benchmark inputs and module parameters are\n # contiguous fp32 CUDA tensors. The extension performs all heavy work and\n # returns both the RNN output and the updated hidden state.\n out, hnew = _ext.rnn_forward_cuda(\n x,\n self.hidden,\n self.i2h.weight,\n self.i2h.bias,\n self.h2o.weight,\n self.h2o.bias,\n self.input_size,\n )\n self.hidden = hnew\n return out\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 6.85 μs
Runtime stats: mean=6.85μs median=?μs std=0.03μs
Numerical precision: max_abs_err=6.26e-06 mean_abs_err=7.39e-07 max_rel_err=3.17e+01
Memory: 6905.19 MB peak (ref: 5281.19 MB, ratio: 1.3075x)
Kernel launches: 21 (ref: 14, fusion ratio: 0.6667)
Energy: 1450.78 mJ/run (ref: 1692.74 mJ, ratio: 1.1668x)
SOL score: 1.0
final submitted kernel (109 lines, 3,813 chars) — full source, untruncated
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline
_cpp_src = r'''
#include <torch/extension.h>
#include <vector>
void tanh_inplace_cuda(torch::Tensor t);
std::vector<torch::Tensor> rnn_forward_cuda(torch::Tensor x,
torch::Tensor hidden,
torch::Tensor w1,
torch::Tensor b1,
torch::Tensor w2,
torch::Tensor b2,
int64_t input_size) {
TORCH_CHECK(x.is_cuda(), "x must be CUDA");
TORCH_CHECK(hidden.is_cuda(), "hidden must be CUDA");
TORCH_CHECK(w1.is_cuda() && b1.is_cuda() && w2.is_cuda() && b2.is_cuda(), "weights must be CUDA");
TORCH_CHECK(x.scalar_type() == at::kFloat, "only fp32 supported");
// Avoid materializing torch.cat([x, hidden], 1). The reference linear is
// equivalent to two GEMMs accumulated into the same output. This removes
// a 256 x 32768 temporary and one full memory pass over it.
auto wx = w1.narrow(1, 0, input_size);
auto wh = w1.narrow(1, input_size, w1.size(1) - input_size);
auto hnew = at::addmm(b1, x, wx.t());
hnew.addmm_(hidden, wh.t(), 1.0, 1.0);
tanh_inplace_cuda(hnew);
auto out = at::addmm(b2, hnew, w2.t());
return {out, hnew};
}
'''
_cuda_src = r'''
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAException.h>
#include <stdint.h>
__global__ void tanh_inplace_kernel(float* __restrict__ p, int64_t n) {
int64_t i = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
int64_t stride = (int64_t)blockDim.x * gridDim.x;
for (; i < n; i += stride) {
p[i] = tanhf(p[i]);
}
}
void tanh_inplace_cuda(torch::Tensor t) {
const int64_t n = t.numel();
if (n == 0) return;
const int threads = 256;
int blocks = (int)((n + threads - 1) / threads);
if (blocks > 65535) blocks = 65535;
tanh_inplace_kernel<<<blocks, threads, 0, at::cuda::getCurrentCUDAStream()>>>(t.data_ptr<float>(), n);
C10_CUDA_KERNEL_LAUNCH_CHECK();
}
'''
_ext = load_inline(
name="rnn_split_tanh_ext",
cpp_sources=_cpp_src,
cuda_sources=_cuda_src,
functions=["rnn_forward_cuda"],
extra_cflags=["-O3"],
extra_cuda_cflags=["-O3", "--use_fast_math"],
verbose=False,
)
class ModelNew(nn.Module):
def __init__(self, input_size: int, hidden_size: int, output_size: int):
super(ModelNew, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.hidden = torch.randn((256, hidden_size))
self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
self.h2o = nn.Linear(hidden_size, output_size)
self.tanh = nn.Tanh()
def forward(self, x: torch.Tensor, initial_hidden=None) -> torch.Tensor:
if initial_hidden is not None:
if self.hidden.device != initial_hidden.device:
self.hidden = self.hidden.to(initial_hidden.device)
self.hidden.copy_(initial_hidden)
if self.hidden.device != x.device:
self.hidden = self.hidden.to(x.device)
# Ensure the common fast path: benchmark inputs and module parameters are
# contiguous fp32 CUDA tensors. The extension performs all heavy work and
# returns both the RNN output and the updated hidden state.
out, hnew = _ext.rnn_forward_cuda(
x,
self.hidden,
self.i2h.weight,
self.i2h.bias,
self.h2o.weight,
self.h2o.bias,
self.input_size,
)
self.hidden = hnew
return out