51_Gemm_Subtract_GlobalAvgPool_LogSumExp_GELU_ResidualAdd
• modular_device_functions_optimized_base
import torch
import torch.nn as nn
import torch.nn.functional as F
def module_fn(
x: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
subtract: torch.Tensor,
) -> torch.Tensor:
"""
Performs a series of operations: Gemm, Subtract, GlobalAvgPool, LogSumExp, GELU, and ResidualAdd.
Args:
x (torch.Tensor): Input tensor of shape (batch_size, in_features)
weight (torch.Tensor): Weight matrix for linear layer of shape (out_features, in_features)
bias (torch.Tensor): Bias vector for linear layer of shape (out_features)
subtract (torch.Tensor): Vector to subtract of shape (out_features)
Returns:
torch.Tensor: Output tensor after applying all operations
"""
original_x = x.clone().detach()
# Gemm
x = F.linear(x, weight, bias)
# Subtract
x = x - subtract
# GlobalAvgPool
x = torch.mean(x, dim=1, keepdim=True)
# LogSumExp
x = torch.logsumexp(x, dim=1, keepdim=True)
# GELU
x = F.gelu(x)
# ResidualAdd
x = x + original_x
return x
class Model(nn.Module):
"""
Model that performs a series of operations: Gemm, Subtract, GlobalAvgPool, LogSumExp, GELU, and ResidualAdd.
"""
def __init__(self, in_features, out_features):
super(Model, self).__init__()
gemm = nn.Linear(in_features, out_features)
self.weight = nn.Parameter(gemm.weight)
self.bias = nn.Parameter(gemm.bias)
self.subtract = nn.Parameter(torch.randn(out_features) * 0.02)
def forward(self, x, fn=module_fn):
return fn(x, self.weight, self.bias, self.subtract)
batch_size = 128
in_features = 1024
out_features = 512
def get_inputs():
return [torch.randn(batch_size, in_features)]
def get_init_inputs():
return [in_features, out_features]
import torch
import torch.nn as nn
class Model(nn.Module):
"""
Model that performs a series of operations: Gemm, Subtract, GlobalAvgPool, LogSumExp, GELU, and ResidualAdd.
"""
def __init__(self, in_features, out_features, bias=True):
super(Model, self).__init__()
self.gemm = nn.Linear(in_features, out_features, bias=bias)
self.subtract = nn.Parameter(torch.randn(out_features) * 0.02)
def forward(self, x):
original_x = x.clone().detach()
# Gemm
x = self.gemm(x)
# Subtract
x = x - self.subtract
# GlobalAvgPool
x = torch.mean(x, dim=1, keepdim=True)
# LogSumExp
x = torch.logsumexp(x, dim=1, keepdim=True)
# GELU
x = torch.nn.functional.gelu(x)
# ResidualAdd
x = x + original_x
return x
batch_size = 128
in_features = 1024
out_features = 512
def get_inputs():
return [torch.randn(batch_size, in_features)]
def get_init_inputs():
return [in_features, out_features]
#include <torch/extension.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cmath>
#define MAX_OUT_FEATURES 4096
#define WARP_SIZE 32
#define BLOCK_SIZE 256
#define TILE_SIZE 16
__constant__ float c_bias[MAX_OUT_FEATURES];
__constant__ float c_subtract[MAX_OUT_FEATURES];
//------------------------------------------------------------------------------
// Modular device functions for core operations
namespace device {
// Efficient dot product computation
__device__ __forceinline__ float dot_product(
const float* __restrict__ a,
const float* __restrict__ b,
const int size
) {
float sum = 0.0f;
#pragma unroll
for (int i = 0; i < size; ++i) {
sum += a[i] * b[i];
}
return sum;
}
// Warp-level reduction
__device__ __forceinline__ float warp_reduce_sum(float val) {
#pragma unroll
for (int offset = WARP_SIZE/2; offset > 0; offset /= 2) {
val += __shfl_down_sync(0xffffffff, val, offset);
}
return val;
}
// GELU activation
__device__ __forceinline__ float gelu(float x) {
const float kAlpha = 0.044715f;
const float kBeta = 0.7978845608f;
float inner = kBeta * (x + kAlpha * x * x * x);
return x * 0.5f * (1.0f + tanhf(inner));
}
// Efficient block-level reduction
__device__ __forceinline__ float block_reduce_sum(
float val,
float* shared_mem
) {
const int tid = threadIdx.x;
const int lane_id = tid % WARP_SIZE;
const int warp_id = tid / WARP_SIZE;
const int warps_per_block = BLOCK_SIZE / WARP_SIZE;
// Warp-level reduction
val = warp_reduce_sum(val);
// Write reduced warp results to shared memory
if (lane_id == 0) {
shared_mem[warp_id] = val;
}
__syncthreads();
// Final reduction across warps
if (tid < warps_per_block) {
val = (tid < (BLOCK_SIZE / WARP_SIZE)) ? shared_mem[tid] : 0.0f;
val = warp_reduce_sum(val);
}
return val;
}
} // namespace device
//------------------------------------------------------------------------------
// Optimized GEMM kernel with tiling and fused operations
__global__ void fused_gemm_subtract_kernel(
const float* __restrict__ x,
const float* __restrict__ weight,
float* __restrict__ out,
const int batch_size,
const int in_features,
const int out_features
) {
__shared__ float x_shared[TILE_SIZE][TILE_SIZE + 1]; // +1 for bank conflicts
__shared__ float w_shared[TILE_SIZE][TILE_SIZE + 1];
const int tx = threadIdx.x;
const int ty = threadIdx.y;
const int row = blockIdx.x * TILE_SIZE + tx;
const int col = blockIdx.y * TILE_SIZE + ty;
float acc = 0.0f;
for (int t = 0; t < (in_features + TILE_SIZE - 1) / TILE_SIZE; ++t) {
// Collaborative loading of tiles into shared memory
if (row < batch_size && t * TILE_SIZE + ty < in_features) {
x_shared[tx][ty] = x[row * in_features + t * TILE_SIZE + ty];
} else {
x_shared[tx][ty] = 0.0f;
}
if (col < out_features && t * TILE_SIZE + tx < in_features) {
w_shared[ty][tx] = weight[col * in_features + t * TILE_SIZE + tx];
} else {
w_shared[ty][tx] = 0.0f;
}
__syncthreads();
// Compute partial dot products
acc += device::dot_product(x_shared[tx], w_shared[ty], TILE_SIZE);
__syncthreads();
}
// Write result with fused bias and subtract
if (row < batch_size && col < out_features) {
out[row * out_features + col] = acc + c_bias[col] - c_subtract[col];
}
}
//------------------------------------------------------------------------------
// Optimized average pooling kernel with fused logsumexp
__global__ void fused_avgpool_logsumexp_kernel(
const float* __restrict__ x,
float* __restrict__ out,
const int batch_size,
const int out_features
) {
extern __shared__ float shared_mem[];
const int tid = threadIdx.x;
const int bid = blockIdx.x;
float sum = 0.0f;
for (int j = tid; j < out_features; j += blockDim.x) {
sum += x[bid * out_features + j];
}
sum = device::block_reduce_sum(sum, shared_mem);
if (tid == 0) {
float avg = sum / static_cast<float>(out_features);
out[bid] = logf(expf(avg));
}
}
//------------------------------------------------------------------------------
// Optimized GELU and residual add kernel
__global__ void fused_gelu_residual_kernel(
const float* __restrict__ scalar,
const float* __restrict__ original_x,
float* __restrict__ out,
const int batch_size,
const int in_features
) {
const int idx = blockIdx.x * blockDim.x + threadIdx.x;
const int total = batch_size * in_features;
if (idx < total) {
const int i = idx / in_features;
out[idx] = original_x[idx] + device::gelu(scalar[i]);
}
}
torch::Tensor forward_cuda(
const torch::Tensor& x,
const torch::Tensor& weight,
const torch::Tensor& bias,
const torch::Tensor& subtract
) {
TORCH_CHECK(x.is_cuda() && weight.is_cuda() && bias.is_cuda() && subtract.is_cuda(),
"All tensors must be CUDA tensors");
const int64_t batch_size = x.size(0);
const int64_t in_features = x.size(1);
const int64_t out_features = weight.size(0);
auto x_ = x.contiguous();
auto w_ = weight.contiguous();
auto b_ = bias.contiguous();
auto s_ = subtract.contiguous();
cudaMemcpyToSymbol(c_bias, b_.data_ptr<float>(), out_features * sizeof(float));
cudaMemcpyToSymbol(c_subtract, s_.data_ptr<float>(), out_features * sizeof(float));
auto original_x = x_.clone();
auto gemm_out = torch::empty({batch_size, out_features}, x.options());
auto pool_out = torch::empty({batch_size}, x.options());
// Launch optimized GEMM kernel
dim3 blockGemm(TILE_SIZE, TILE_SIZE);
dim3 gridGemm(
(batch_size + TILE_SIZE - 1) / TILE_SIZE,
(out_features + TILE_SIZE - 1) / TILE_SIZE
);
fused_gemm_subtract_kernel<<<gridGemm, blockGemm>>>(
x_.data_ptr<float>(),
w_.data_ptr<float>(),
gemm_out.data_ptr<float>(),
batch_size,
in_features,
out_features
);
// Launch fused avgpool and logsumexp kernel
const int threads = BLOCK_SIZE;
const int shared_mem_size = (threads / WARP_SIZE) * sizeof(float);
fused_avgpool_logsumexp_kernel<<<batch_size, threads, shared_mem_size>>>(
gemm_out.data_ptr<float>(),
pool_out.data_ptr<float>(),
batch_size,
out_features
);
// Launch fused GELU and residual add kernel
auto out = torch::empty({batch_size, in_features}, x.options());
const int total = batch_size * in_features;
const int blocks = (total + threads - 1) / threads;
fused_gelu_residual_kernel<<<blocks, threads>>>(
pool_out.data_ptr<float>(),
original_x.data_ptr<float>(),
out.data_ptr<float>(),
batch_size,
in_features
);
return out;
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("forward", &forward_cuda,
"Modular CUDA implementation with optimized device functions");
}
Metric | Value | Unit | Variance | Samples |
---|---|---|---|---|
Executed Ipc Active | 1.086 | inst/cycle | 0.001 | 5 |
Executed Ipc Elapsed | 0.386 | inst/cycle | 0.000 | 5 |
Issue Slots Busy | 28.106 | % | 0.339 | 5 |
Issued Ipc Active | 1.124 | inst/cycle | 0.001 | 5 |
SM Busy | 28.106 | % | 0.339 | 5 |
Memory Throughput | 149851895044.044 | byte/second | 10597999412610832384.000 | 5 |
Mem Busy | 11.696 | % | 0.037 | 5 |
Max Bandwidth | 9.006 | % | 0.032 | 5 |
L1/TEX Hit Rate | 9.720 | % | 0.000 | 5 |
L2 Hit Rate | 75.322 | % | 0.113 | 5 |
Mem Pipes Busy | 8.592 | % | 0.036 | 5 |
Warp Cycles Per Issued Instruction | 23.994 | cycle | 0.013 | 5 |
Warp Cycles Per Executed Instruction | 24.874 | cycle | 0.012 | 5 |
Avg. Active Threads Per Warp | 32.000 | 0.000 | 5 | |
Avg. Not Predicated Off Threads Per Warp | 25.040 | 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 | 16.000 | block | 0.000 | 5 |
Block Limit Shared Mem | 32.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 | 42.064 | % | 0.322 | 5 |
Achieved Active Warps Per SM | 26.924 | warp | 0.131 | 5 |
Rule | Description |
---|---|
WRN HighPipeUtilization | All compute pipelines are under-utilized. Either this kernel is very small or it doesn't issue enough warps per scheduler. Check the Launch Statistics and Scheduler Statistics sections for further details. |
INF CPIStall | Check the Warp Stall Sampling (All Cycles) table for the top stall locations in your source based on sampling data. The Kernel Profiling Guide (https://docs.nvidia.com/nsight-compute/ProfilingGuide/index.html#metrics-reference) provides more details on each stall reason. |
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 (41.7%) 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 | 547328.75 | μs |
Device Time | 226.21 | μs |
Self CPU Time | 87.99 | μ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 | 547240.76 | μs |
Device Time | 226.21 | μs |
Self CPU Time | 157.04 | μ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 | 563895.23 | μs |
Device Time | 0.00 | μs |
Self CPU Time | 17576.98 | μ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 | 541601.80 | μs |
Device Time | 0.00 | μs |
Self CPU Time | 541601.80 | μ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 |
Memcpy DtoD (Device -> Device) | ||
CPU Time | 0.00 | μs |
Device Time | 55270.11 | μs |
Self CPU Time | 0.00 | μs |
Self Device Time | 55270.11 | μ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 | 492395.71 | μs |
Device Time | 43518.14 | μs |
Self CPU Time | 492395.71 | μs |
Self Device Time | 43518.14 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
fused_gemm_subtract_kernel(float const*, float const*, float*, int, int, int) | ||
CPU Time | 0.00 | μs |
Device Time | 338319.30 | μs |
Self CPU Time | 0.00 | μs |
Self Device Time | 338319.30 | μ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 | 57110.89 | μs |
Device Time | 550772.90 | μs |
Self CPU Time | 11344.75 | μ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 | 45767.51 | μs |
Device Time | 550772.90 | μs |
Self CPU Time | 14900.35 | μs |
Self Device Time | 550772.90 | μ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 | 550772.90 | μs |
Self CPU Time | 0.00 | μs |
Self Device Time | 550772.90 | μs |
CPU Memory Usage | 0 | B |
Device Memory Usage | 0 | B |
Self CPU Memory Usage | 0 | B |
Self Device Memory Usage | 0 | B |
45296 warnings generated when compiling for host. Suppressed 45324 warnings (45277 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.