forked from ccf-ai-infra/GPUCodeForces
82 lines
2.8 KiB
Plaintext
82 lines
2.8 KiB
Plaintext
You write custom CUDA kernels to replace the PyTorch operators in the given EvoNorm architecture to get speedups.
|
|
You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining normalization+affine_transform+nonlinear_gating), or algorithmic changes (such as optimized memory access patterns). You are only limited by your imagination.
|
|
|
|
|
|
Overview
|
|
This implementation provides a highly optimized CUDA kernel for computing the Log-Cosh loss function, which is a smooth alternative to Mean Absolute Error (MAE) that is less sensitive to outliers than Mean Squared Error (MSE).
|
|
|
|
Mathematical Formulation
|
|
The Log-Cosh loss is defined as:
|
|
L(x, y) = log(cosh(x - y)) = |x-y| + log(1 + exp(-2|x-y|)) - log(2)
|
|
|
|
key Optimizations
|
|
1. Vectorized Memory Access
|
|
Uses float4 data type for coalesced memory operations
|
|
|
|
Processes 4 elements per thread simultaneously
|
|
|
|
Reduces memory transaction overhead by 75%
|
|
|
|
2. Numerical Stability
|
|
Implements the stable formulation: |diff| + log1p(exp(-2*|diff|)) - log(2)
|
|
|
|
Avoids numerical overflow in cosh() calculation
|
|
|
|
Uses log1p() for accurate logarithm of (1 + x)
|
|
|
|
3. Parallel Reduction Strategy
|
|
Warp-level reduction: 32-thread warp shuffle operations
|
|
|
|
Block-level reduction: Shared memory for intra-block reduction
|
|
|
|
Global reduction: Atomic operations for cross-block summation
|
|
|
|
4. Flexible Reduction Modes
|
|
reduction=0: Element-wise output (no reduction)
|
|
|
|
reduction=1: Mean reduction (sum / n_elements)
|
|
|
|
reduction=2: Sum reduction
|
|
|
|
|
|
Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is:
|
|
import torch
|
|
import torch.nn as nn
|
|
import math
|
|
|
|
N, C, H, W = 32, 64, 56, 56
|
|
|
|
|
|
class LogCoshLoss(nn.Module):
|
|
def __init__(self, reduction='mean'):
|
|
super().__init__()
|
|
self.reduction = reduction
|
|
|
|
def forward(self, input, target):
|
|
diff = input - target
|
|
loss = torch.abs(diff) + torch.nn.functional.softplus(-2. * torch.abs(diff)) - math.log(2.0)
|
|
|
|
if self.reduction == 'mean':
|
|
return loss.mean()
|
|
elif self.reduction == 'sum':
|
|
return loss.sum()
|
|
return loss
|
|
|
|
|
|
class Model(nn.Module):
|
|
def __init__(self, reduction='mean'):
|
|
super().__init__()
|
|
self.op = LogCoshLoss(reduction)
|
|
|
|
def forward(self, input, target):
|
|
return self.op(input, target)
|
|
|
|
|
|
def get_inputs():
|
|
input = torch.randn(N, C, H, W, dtype=torch.float32)
|
|
target = torch.randn(N, C, H, W, dtype=torch.float32)
|
|
return [input, target]
|
|
|
|
|
|
def get_init_inputs():
|
|
return ['mean'] |