correct
30_Softsign.py
started
2026-05-05T00:48:27Z
timingkernelbench.timing
kernel mean / median / std
4.55 / — / 0.26 μs
ref mean / median / std
14.90 / — / 0.09 μs
numerical precisioneval_kernel_against_ref
gpu memoryextended_metrics.measure_memory
kernel launches / fusiontorch.profiler
fusion ratio (ref / kernel)
3.00x
energyNVML (pynvml)
ratio (ref / kernel)
1.71x
SOL (speed-of-light)nsight_hardware_counters
roofline / occupancynsight_hardware_counters
dram bandwidth
2898.18 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 Softsign activation.
"""
def __init__(self):
super(Model, self).__init__()
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Applies Softsign activation to the input tensor.
Args:
x (torch.Tensor): Input tensor of any shape.
Returns:
torch.Tensor: Output tensor with Softsign applied, same shape as input.
"""
return x / (1 + torch.abs(x))
batch_size = 4096
dim = 393216
def get_inputs():
x = torch.rand(batch_size, dim)
return [x]
def get_init_inputs():
return [] # No special initialization inputs neededturn 015.9s turn · 15.9s LLM · 0.0s tools · FINAL
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\nvoid softsign_cuda(torch::Tensor x, torch::Tensor y);\n\ntorch::Tensor softsign(torch::Tensor x) {\n auto y = torch::empty_like(x);\n softsign_cuda(x, y);\n return y;\n}\n\"\"\"\n\n_cuda_src = r\"\"\"\n#include <torch/extension.h>\n#include <ATen/cuda/CUDAContext.h>\n#include <cuda.h>\n#include <cuda_runtime.h>\n#include <stdint.h>\n\n__device__ __forceinline__ float softsign_f(float v) {\n return v / (1.0f + fabsf(v));\n}\n\n__global__ void softsign_scalar_kernel(const float* __restrict__ x, 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 = x[idx];\n y[idx] = softsign_f(v);\n }\n}\n\n__global__ void softsign_float4_kernel(const float4* __restrict__ x4, float4* __restrict__ y4, int64_t n4) {\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 < n4; idx += stride) {\n float4 v = x4[idx];\n v.x = softsign_f(v.x);\n v.y = softsign_f(v.y);\n v.z = softsign_f(v.z);\n v.w = softsign_f(v.w);\n y4[idx] = v;\n }\n}\n\nvoid softsign_cuda(torch::Tensor x, torch::Tensor y) {\n const int64_t n = x.numel();\n if (n == 0) return;\n\n const int threads = 256;\n cudaStream_t stream = at::cuda::getCurrentCUDAStream();\n\n const float* xp = x.data_ptr<float>();\n float* yp = y.data_ptr<float>();\n\n // Vectorized path for the common contiguous, aligned fp32 case.\n uintptr_t xa = reinterpret_cast<uintptr_t>(xp);\n uintptr_t ya = reinterpret_cast<uintptr_t>(yp);\n if (((xa | ya) & 15) == 0 && (n % 4 == 0)) {\n int64_t n4 = n / 4;\n int blocks = (int)((n4 + threads - 1) / threads);\n if (blocks > 65535) blocks = 65535;\n softsign_float4_kernel<<<blocks, threads, 0, stream>>>(\n reinterpret_cast<const float4*>(xp), reinterpret_cast<float4*>(yp), n4);\n } else {\n int blocks = (int)((n + threads - 1) / threads);\n if (blocks > 65535) blocks = 65535;\n softsign_scalar_kernel<<<blocks, threads, 0, stream>>>(xp, yp, n);\n }\n}\n\"\"\"\n\n_ext = load_inline(\n name=\"softsign_inline_ext\",\n cpp_sources=[_cpp_src],\n cuda_sources=[_cuda_src],\n functions=[\"softsign\"],\n extra_cuda_cflags=[\"-O3\", \"--use_fast_math\"],\n extra_cflags=[\"-O3\"],\n verbose=False,\n)\n\nclass ModelNew(nn.Module):\n def __init__(self):\n super(ModelNew, self).__init__()\n\n def forward(self, x: torch.Tensor) -> torch.Tensor:\n return _ext.softsign(x)\n"
}
result ← submit_kernel (ok)
oksubmit_kernel
submit_kernel PASSED: (5 / 5) correctness trials all passed.
Kernel runtime: 4.55 μs
Runtime stats: mean=4.55μs median=?μs std=0.26μs
Numerical precision: max_abs_err=5.96e-08 mean_abs_err=8.32e-09 max_rel_err=1.60e-07
Memory: 12321.0 MB peak (ref: 18465.0 MB, ratio: 0.6673x)
Kernel launches: 2 (ref: 6, fusion ratio: 3.0)
Energy: 1684.48 mJ/run (ref: 2887.12 mJ, ratio: 1.714x)
SOL score: 0.8651
final submitted kernel (93 lines, 2,729 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>
void softsign_cuda(torch::Tensor x, torch::Tensor y);
torch::Tensor softsign(torch::Tensor x) {
auto y = torch::empty_like(x);
softsign_cuda(x, y);
return y;
}
"""
_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdint.h>
__device__ __forceinline__ float softsign_f(float v) {
return v / (1.0f + fabsf(v));
}
__global__ void softsign_scalar_kernel(const float* __restrict__ x, 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 = x[idx];
y[idx] = softsign_f(v);
}
}
__global__ void softsign_float4_kernel(const float4* __restrict__ x4, float4* __restrict__ y4, int64_t n4) {
int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
int64_t stride = (int64_t)blockDim.x * gridDim.x;
for (; idx < n4; idx += stride) {
float4 v = x4[idx];
v.x = softsign_f(v.x);
v.y = softsign_f(v.y);
v.z = softsign_f(v.z);
v.w = softsign_f(v.w);
y4[idx] = v;
}
}
void softsign_cuda(torch::Tensor x, torch::Tensor y) {
const int64_t n = x.numel();
if (n == 0) return;
const int threads = 256;
cudaStream_t stream = at::cuda::getCurrentCUDAStream();
const float* xp = x.data_ptr<float>();
float* yp = y.data_ptr<float>();
// Vectorized path for the common contiguous, aligned fp32 case.
uintptr_t xa = reinterpret_cast<uintptr_t>(xp);
uintptr_t ya = reinterpret_cast<uintptr_t>(yp);
if (((xa | ya) & 15) == 0 && (n % 4 == 0)) {
int64_t n4 = n / 4;
int blocks = (int)((n4 + threads - 1) / threads);
if (blocks > 65535) blocks = 65535;
softsign_float4_kernel<<<blocks, threads, 0, stream>>>(
reinterpret_cast<const float4*>(xp), reinterpret_cast<float4*>(yp), n4);
} else {
int blocks = (int)((n + threads - 1) / threads);
if (blocks > 65535) blocks = 65535;
softsign_scalar_kernel<<<blocks, threads, 0, stream>>>(xp, yp, n);
}
}
"""
_ext = load_inline(
name="softsign_inline_ext",
cpp_sources=[_cpp_src],
cuda_sources=[_cuda_src],
functions=["softsign"],
extra_cuda_cflags=["-O3", "--use_fast_math"],
extra_cflags=["-O3"],
verbose=False,
)
class ModelNew(nn.Module):
def __init__(self):
super(ModelNew, self).__init__()
def forward(self, x: torch.Tensor) -> torch.Tensor:
return _ext.softsign(x)