forked from ccf-ai-infra/GPUCodeForces
89 lines
3.5 KiB
Plaintext
89 lines
3.5 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.
|
||
|
||
Technical Overview: CUDA-Optimized Cosine Embedding Loss
|
||
This implementation provides a high-performance CUDA kernel for computing cosine embedding loss, designed for deep learning applications with optimized memory access and parallel computation.
|
||
Key Features:
|
||
Architecture:
|
||
Custom CUDA kernel with inline compilation using PyTorch C++ extensions
|
||
Optimized for NVIDIA GPUs with warp-level parallelism and shared memory utilization
|
||
Supports 4-element vectorization (float4) for memory coalescing
|
||
Performance Optimizations:
|
||
Vectorized Memory Access: Uses float4 data type to load 4 elements per instruction
|
||
Instruction-Level Parallelism (ILP): Processes 4 vectors simultaneously per thread
|
||
Warp Reduction: Efficient warp-level reduction operations using __shfl_down_sync
|
||
Shared Memory: Intermediate results stored in shared memory for block-level reduction
|
||
Memory Coalescing: Contiguous memory access patterns for optimal bandwidth utilization
|
||
Kernel Specifications:
|
||
Block size: 256 threads
|
||
Warp size: 32 threads
|
||
Grid dimension: N (batch size)
|
||
Input requirement: C×H×W must be divisible by 4 for vectorization
|
||
|
||
|
||
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 torch.nn.functional as F
|
||
|
||
|
||
N, C, H, W = 32, 64, 56, 56
|
||
EPS = 1e-8
|
||
|
||
class CosineEmbeddingLossCustom(nn.Module):
|
||
|
||
def __init__(self, margin=0.0, reduction='mean', eps=1e-8):
|
||
super().__init__()
|
||
self.margin = margin
|
||
self.reduction = reduction
|
||
self.eps = eps
|
||
|
||
def forward(self, x1: torch.Tensor, x2: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
||
|
||
|
||
dot_product = torch.sum(x1 * x2, dim=1)
|
||
norm_x1 = torch.norm(x1, p=2, dim=1)
|
||
norm_x2 = torch.norm(x2, p=2, dim=1)
|
||
|
||
cos_sim = dot_product / (norm_x1 * norm_x2 + self.eps)
|
||
|
||
|
||
|
||
loss_pos = 1.0 - cos_sim
|
||
loss_neg = F.relu(cos_sim - self.margin)
|
||
|
||
|
||
loss = torch.where(target == 1, loss_pos, loss_neg)
|
||
|
||
|
||
if self.reduction == 'mean':
|
||
return loss.mean()
|
||
elif self.reduction == 'sum':
|
||
return loss.sum()
|
||
else:
|
||
return loss
|
||
|
||
class Model(nn.Module):
|
||
def __init__(self, margin=0.5):
|
||
super().__init__()
|
||
self.op = CosineEmbeddingLossCustom(margin=margin, reduction='mean', eps=EPS)
|
||
|
||
def forward(self, x1: torch.Tensor, x2: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
||
|
||
x1_flat = x1.view(x1.size(0), -1)
|
||
x2_flat = x2.view(x2.size(0), -1)
|
||
return self.op(x1_flat, x2_flat, target)
|
||
|
||
def get_inputs():
|
||
|
||
x1 = torch.randn(N, C, H, W, dtype=torch.float32)
|
||
x2 = torch.randn(N, C, H, W, dtype=torch.float32)
|
||
|
||
|
||
target = torch.randint(0, 2, (N,), dtype=torch.float32) # 0 or 1
|
||
target = torch.where(target == 0, torch.tensor(-1.0), torch.tensor(1.0))
|
||
|
||
return [x1, x2, target]
|
||
|
||
def get_init_inputs():
|
||
return [0.5] # margin |