import torch
import torch.nn as nn
import torch.nn.functional as F
import math
def module_fn(x: torch.Tensor) -> torch.Tensor:
"""
Implementation of the Gaussian Error Linear Units (GELU) activation function currently in Google BERT repo (identical to OpenAI GPT).
Args:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output tensor.
"""
return (
0.5
* x
* (
1.0
+ torch.tanh(math.sqrt(2.0 / math.pi) * (x + 0.044715 * torch.pow(x, 3.0)))
)
)
class Model(nn.Module):
"""
Implementation of the GELU activation function currently in Google BERT repo (identical to OpenAI GPT).
Reference: Gaussian Error Linear Units (GELU) paper: https://arxiv.org/abs/1606.08415
"""
def __init__(self):
super(Model, self).__init__()
def forward(self, x, fn=module_fn):
return fn(x)
batch_size = 2000
dim = 2000
def get_inputs():
return [torch.randn(batch_size, dim)]
def get_init_inputs():
return []
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
# From https://github.com/karpathy/minGPT/blob/master/mingpt/model.py
class Model(nn.Module):
"""
Implementation of the GELU activation function currently in Google BERT repo (identical to OpenAI GPT).
Reference: Gaussian Error Linear Units (GELU) paper: https://arxiv.org/abs/1606.08415
"""
def __init__(self):
super(Model, self).__init__()
def forward(self, x):
return (
0.5
* x
* (
1.0
+ torch.tanh(
math.sqrt(2.0 / math.pi) * (x + 0.044715 * torch.pow(x, 3.0))
)
)
)
batch_size = 2000
dim = 2000
def get_inputs():
return [torch.randn(batch_size, dim)]
def get_init_inputs():
return []
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <math.h>
// Inline device function for GELU activation
__device__ __forceinline__ float gelu_act(float x) {
const float sqrt_2_over_pi = 0.7978845608f;
const float coeff = 0.044715f;
float x_cubed = x * x * x;
float inner = (x + coeff * x_cubed) * sqrt_2_over_pi;
// Use tanh intrinsic for better performance
return 0.5f * x * (1.0f + tanhf(inner));
}
// Optimized kernel using shared memory tiling and loop unrolling
__global__ void gelu_kernel_tile_inline(const float* __restrict__ x,
float* __restrict__ y, int n) {
const int unroll = 4;
int tileSize = blockDim.x * unroll;
int base = blockIdx.x * tileSize;
extern __shared__ float tile[];
int tid = threadIdx.x;
// Cooperative loading of a tile from global memory to shared memory
#pragma unroll
for (int i = 0; i < unroll; i++) {
int idx = base + tid + i * blockDim.x;
if (idx < n) {
tile[tid + i * blockDim.x] = x[idx];
}
}
__syncthreads();
// Compute GELU using the data loaded into shared memory
#pragma unroll
for (int i = 0; i < unroll; i++) {
int idx = base + tid + i * blockDim.x;
if (idx < n) {
float val = tile[tid + i * blockDim.x];
y[idx] = gelu_act(val);
}
}
}
// Host function to launch the kernel
torch::Tensor gelu_forward(torch::Tensor x) {
TORCH_CHECK(x.is_cuda(), "Input tensor must be on CUDA");
TORCH_CHECK(x.is_contiguous(), "Input tensor must be contiguous");
auto y = torch::empty_like(x);
int n = x.numel();
const int threads = 256;
const int unroll = 4;
int tileSize = threads * unroll;
int blocks = (n + tileSize - 1) / tileSize;
// Allocate shared memory: one tile per block
size_t sharedMemSize = tileSize * sizeof(float);
gelu_kernel_tile_inline<<<blocks, threads, sharedMemSize>>>(
x.data_ptr<float>(), y.data_ptr<float>(), n);
return y;
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("forward", &gelu_forward, "Optimized GELU forward CUDA implementation using shared memory tiling and inline device function");
}
Metric | Value | Unit | Variance | Samples |
---|---|---|---|---|
Executed Ipc Active | 2.830 | inst/cycle | 0.001 | 5 |
Executed Ipc Elapsed | 2.276 | inst/cycle | 0.000 | 5 |
Issue Slots Busy | 70.920 | % | 0.618 | 5 |
Issued Ipc Active | 2.836 | inst/cycle | 0.001 | 5 |
SM Busy | 70.920 | % | 0.618 | 5 |
Memory Throughput | 1369477786511.682 | byte/second | 23082830633634189312.000 | 5 |
Mem Busy | 33.836 | % | 0.014 | 5 |
Max Bandwidth | 40.968 | % | 0.020 | 5 |
L1/TEX Hit Rate | 0.000 | % | 0.000 | 5 |
L2 Hit Rate | 50.468 | % | 0.033 | 5 |
Mem Pipes Busy | 32.544 | % | 0.010 | 5 |
Warp Cycles Per Issued Instruction | 19.066 | cycle | 0.004 | 5 |
Warp Cycles Per Executed Instruction | 19.112 | cycle | 0.003 | 5 |
Avg. Active Threads Per Warp | 25.350 | 0.000 | 5 | |
Avg. Not Predicated Off Threads Per Warp | 24.340 | 0.000 | 5 | |
Max Active Clusters | 0.000 | cluster | 0.000 | 5 |
Max Cluster Size | 8.000 | block | 0.000 | 5 |
Overall GPU Occupancy | 0.000 | % | 0.000 | 5 |
Cluster Occupancy | 0.000 | % | 0.000 | 5 |
Block Limit SM | 32.000 | block | 0.000 | 5 |
Block Limit Registers | 10.000 | block | 0.000 | 5 |
Block Limit Shared Mem | 20.000 | block | 0.000 | 5 |
Block Limit Warps | 8.000 | block | 0.000 | 5 |
Theoretical Active Warps per SM | 64.000 | warp | 0.000 | 5 |
Theoretical Occupancy | 100.000 | % | 0.000 | 5 |
Achieved Occupancy | 85.568 | % | 0.001 | 5 |
Achieved Active Warps Per SM | 54.766 | warp | 0.001 | 5 |
Rule | Description |
---|---|
INF HighPipeUtilization | FMA is the highest-utilized pipeline (31.3%) based on active cycles, taking into account the rates of its different instructions. It executes 32-bit floating point (FADD, FMUL, FMAD, ...) and integer (IMUL, IMAD) operations. It is well-utilized, but should not be a bottleneck. |
WRN Occupancy | This kernel's theoretical occupancy is not impacted by any block limit. The difference between calculated theoretical (100.0%) and measured achieved occupancy (85.6%) can be the result of warp scheduling overheads or workload imbalances during the kernel execution. Load imbalances can occur between warps within a block as well as across blocks of the same kernel. See the CUDA Best Practices Guide (https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html#occupancy) for more details on optimizing occupancy. |
Operation / Metric | Value | Unit |
---|---|---|
aten::to | ||
CPU Time | 503784.94 | μs |
Device Time | 1639.04 | μs |
Self CPU Time | 51.28 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::_to_copy | ||
CPU Time | 503733.66 | μs |
Device Time | 1639.04 | μs |
Self CPU Time | 136.40 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::empty_strided | ||
CPU Time | 517647.50 | μs |
Device Time | 0.00 | μs |
Self CPU Time | 14909.25 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
cudaDeviceGetStreamPriorityRange | ||
CPU Time | 501392.13 | μs |
Device Time | 0.00 | μs |
Self CPU Time | 501392.13 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
cudaLaunchKernel | ||
CPU Time | 447870.47 | μs |
Device Time | 17811.02 | μs |
Self CPU Time | 447870.47 | μs |
Self Device Time | 17811.02 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
gelu_kernel_tile_inline(float const*, float*, int) | ||
CPU Time | 0.00 | μs |
Device Time | 89905.67 | μs |
Self CPU Time | 0.00 | μs |
Self Device Time | 89905.67 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
cudaEventRecord | ||
CPU Time | 17964.89 | μs |
Device Time | 34387.28 | μs |
Self CPU Time | 17964.89 | μs |
Self Device Time | 34387.28 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::zero_ | ||
CPU Time | 74025.09 | μs |
Device Time | 511740.79 | μs |
Self CPU Time | 9947.19 | μs |
Self Device Time | 0.00 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
aten::fill_ | ||
CPU Time | 64079.37 | μs |
Device Time | 511740.79 | μs |
Self CPU Time | 14478.24 | μs |
Self Device Time | 511740.79 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
void at::native::vectorized_elementwise_kernel<4, at::native::FillFunctor<int>, at::detail::Array<char*, 1> >(int, at::native::FillFunctor<int>, at::detail::Array<char*, 1>) | ||
CPU Time | 0.00 | μs |
Device Time | 511740.79 | μs |
Self CPU Time | 0.00 | μs |
Self Device Time | 511740.79 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
45281 warnings generated when compiling for host. Suppressed 45321 warnings (45274 in non-user code, 47 NOLINT). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.