correct
29_Matmul_Mish_Mish.py
started
2026-05-05T03:52:30Z
timingkernelbench.timing
kernel mean / median / std
2.73 / — / 0.01 μs
ref mean / median / std
2.74 / — / 0.01 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
1.40x
energyNVML (pynvml)
ratio (ref / kernel)
4.00x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
1115.03 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):
"""
Simple model that performs a matrix multiplication, applies Mish, and applies Mish again.
"""
def __init__(self, in_features, out_features):
super(Model, self).__init__()
self.linear = nn.Linear(in_features, out_features)
def forward(self, x):
x = self.linear(x)
x = torch.nn.functional.mish(x)
x = torch.nn.functional.mish(x)
return x
batch_size = 1024
in_features = 8192
out_features = 8192
def get_inputs():
return [torch.rand(batch_size, in_features)]
def get_init_inputs():
return [in_features, out_features]turn 015.6s turn · 15.6s LLM · 0.0s tools · FINAL
reasoning
**Evaluating model instantiation**
I’m considering whether the state dict should be copied from a reference. It seems that typically, I might instantiate ModelNew with the same arguments, and perhaps the weights are seeded as well. I’m a little unsure if this is the correct approach, and I should double-check the details around how states and weights interact during model creation. It’s important to get this right to ensure proper functioning.
call → submit_kernel
callsubmit_kernel
{
"kernel_code": "import torch\nimport torch.nn as nn\nfrom torch.utils.cpp_extension import load_inline\n\n_cpp_src = r'''\n#include <torch/extension.h>\n\n// CUDA launcher (defined in cuda_sources)\nvoid mish2_launcher(torch::Tensor y);\n\ntorch::Tensor linear_mish2(torch::Tensor x, torch::Tensor weight, torch::Tensor bias) {\n // Use PyTorch/ATen's optimized cuBLAS path for the large GEMM, then fuse the\n // two Mish applications into a single custom CUDA kernel.\n auto y = at::linear(x, weight, bias);\n mish2_launcher(y);\n return y;\n}\n'''\n\n_cuda_src = r'''\n#include <torch/extension.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <ATen/cuda/CUDAContext.h>\n\n__device__ __forceinline__ float softplus_approx(float x) {\n // Matches PyTorch softplus beta=1 threshold=20 closely for fp32.\n if (x > 20.0f) return x;\n if (x < -20.0f) return expf(x);\n return log1pf(expf(x));\n}\n\n__device__ __forceinline__ float mish_once(float x) {\n return x * tanhf(softplus_approx(x));\n}\n\n__global__ void mish2_kernel(float* __restrict__ y, int64_t n) {\n int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;\n int64_t stride = (int64_t)blockDim.x * gridDim.x;\n for (; idx < n; idx += stride) {\n float v = y[idx];\n v = mish_once(v);\n v = mish_once(v);\n y[idx] = v;\n }\n}\n\nvoid mish2_launcher(torch::Tensor y) {\n const int64_t n = y.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 cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n mish2_kernel<<<blocks, threads, 0, stream>>>(y.data_ptr<float>(), n);\n}\n'''\n\n_ext = load_inline(\n name='linear_mish2_ext',\n cpp_sources=[_cpp_src],\n cuda_sources=[_cuda_src],\n functions=['linear_mish2'],\n extra_cflags=['-O3'],\n extra_cuda_cflags=['-O3', '--use_fast_math'],\n verbose=False,\n)\n\nclass ModelNew(nn.Module):\n \"\"\"\n Drop-in replacement: linear layer followed by Mish twice. The GEMM uses\n ATen/cuBLAS and the two activations are fused in one CUDA kernel.\n \"\"\"\n def __init__(self, in_features, out_features):\n super(ModelNew, self).__init__()\n self.linear = nn.Linear(in_features, out_features)\n\n def forward(self, x):\n return _ext.linear_mish2(x, self.linear.weight, self.linear.bias)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 2.73 μs
Runtime stats: mean=2.73μs median=?μs std=0.01μs
Numerical precision: max_abs_err=1.41e-05 mean_abs_err=4.70e-07 max_rel_err=1.99e-05
Memory: 609.06 MB peak (ref: 641.06 MB, ratio: 0.9501x)
Kernel launches: 5 (ref: 7, fusion ratio: 1.4)
Energy: 241.42 mJ/run (ref: 966.3 mJ, ratio: 4.0026x)
SOL score: 1.0
final submitted kernel (80 lines, 2,351 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>
// CUDA launcher (defined in cuda_sources)
void mish2_launcher(torch::Tensor y);
torch::Tensor linear_mish2(torch::Tensor x, torch::Tensor weight, torch::Tensor bias) {
// Use PyTorch/ATen's optimized cuBLAS path for the large GEMM, then fuse the
// two Mish applications into a single custom CUDA kernel.
auto y = at::linear(x, weight, bias);
mish2_launcher(y);
return y;
}
'''
_cuda_src = r'''
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <ATen/cuda/CUDAContext.h>
__device__ __forceinline__ float softplus_approx(float x) {
// Matches PyTorch softplus beta=1 threshold=20 closely for fp32.
if (x > 20.0f) return x;
if (x < -20.0f) return expf(x);
return log1pf(expf(x));
}
__device__ __forceinline__ float mish_once(float x) {
return x * tanhf(softplus_approx(x));
}
__global__ void mish2_kernel(float* __restrict__ y, int64_t n) {
int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
int64_t stride = (int64_t)blockDim.x * gridDim.x;
for (; idx < n; idx += stride) {
float v = y[idx];
v = mish_once(v);
v = mish_once(v);
y[idx] = v;
}
}
void mish2_launcher(torch::Tensor y) {
const int64_t n = y.numel();
if (n == 0) return;
const int threads = 256;
int blocks = (int)((n + threads - 1) / threads);
if (blocks > 65535) blocks = 65535;
cudaStream_t stream = at::cuda::getCurrentCUDAStream();
mish2_kernel<<<blocks, threads, 0, stream>>>(y.data_ptr<float>(), n);
}
'''
_ext = load_inline(
name='linear_mish2_ext',
cpp_sources=[_cpp_src],
cuda_sources=[_cuda_src],
functions=['linear_mish2'],
extra_cflags=['-O3'],
extra_cuda_cflags=['-O3', '--use_fast_math'],
verbose=False,
)
class ModelNew(nn.Module):
"""
Drop-in replacement: linear layer followed by Mish twice. The GEMM uses
ATen/cuBLAS and the two activations are fused in one CUDA kernel.
"""
def __init__(self, in_features, out_features):
super(ModelNew, self).__init__()
self.linear = nn.Linear(in_features, out_features)
def forward(self, x):
return _ext.linear_mish2(x, self.linear.weight, self.linear.bias)