forked from ccf-ai-infra/GPUCodeForces
finish wingloss #14
This commit is contained in:
parent
f876a28ada
commit
83ca2ab166
|
|
@ -0,0 +1,81 @@
|
|||
Write a custom CUDA kernel to optimize `Wing Loss`.
|
||||
|
||||
Wing Loss is defined by the formula:
|
||||
loss(d) = w * ln(1 + |d|/epsilon) if |d| < w
|
||||
loss(d) = |d| - C otherwise
|
||||
where d = target - prediction, and C = w - w * ln(1 + w/epsilon).
|
||||
|
||||
Problem Analysis:
|
||||
1. Memory Bottleneck: The standard PyTorch implementation relies on element-wise operations chained together (subtraction, abs, comparison, log, masking/where). This creates multiple intermediate tensors that must be written to and read from global memory, consuming significant bandwidth.
|
||||
2. Branching Overhead: The conditional logic creates control flow divergence if not handled efficiently, though the memory access is the primary constraint.
|
||||
|
||||
Optimization Strategy: Fused Element-wise Kernel with Vectorization
|
||||
|
||||
The strategy is to fuse the entire loss calculation logic into a single CUDA kernel pass.
|
||||
|
||||
1. Constant Pre-calculation: The constant 'C' depends only on hyperparameters 'w' and 'epsilon'. It should be pre-calculated on the CPU and passed as a scalar argument to the kernel to save redundant computation.
|
||||
|
||||
2. One-Thread-per-Element: Launch a grid where each thread processes one element of the input tensors.
|
||||
|
||||
3. Vectorized Loads (float4): Use `float4` data types to load 4 floats (128 bits) at a time per thread. This drastically reduces the number of memory transactions and improves instruction throughput for this memory-bound operation.
|
||||
|
||||
4. In-Register Computation: Perform the difference calculation, absolute value, conditional check, and final formula application entirely within registers. This eliminates all intermediate global memory writes.
|
||||
|
||||
5. Reduction Handling: The kernel produces element-wise losses. Final reduction (mean or sum) is handled by the C++ wrapper using optimized ATen primitives.
|
||||
|
||||
Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is:
|
||||
|
||||
```python
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import math
|
||||
|
||||
# 维度:68 points * 2 coordinates (x, y) = 136
|
||||
NUM_LANDMARKS = 136
|
||||
BATCH_SIZE = 32768
|
||||
SHAPE = (BATCH_SIZE, NUM_LANDMARKS)
|
||||
|
||||
W_VAL = 10.0
|
||||
EPS_VAL = 2.0
|
||||
|
||||
class WingLoss(nn.Module):
|
||||
def __init__(self, w=10.0, epsilon=2.0, reduction='mean'):
|
||||
super(WingLoss, self).__init__()
|
||||
self.w = w
|
||||
self.epsilon = epsilon
|
||||
self.reduction = reduction
|
||||
# Constant C: w - w * ln(1 + w/epsilon)
|
||||
self.C = self.w - self.w * math.log(1 + self.w / self.epsilon)
|
||||
|
||||
def forward(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
||||
delta = (target - pred).abs()
|
||||
# Case 1: Small errors (|x| < w) -> ln form
|
||||
loss_small = self.w * torch.log(1 + delta / self.epsilon)
|
||||
# Case 2: Large errors (|x| >= w) -> linear form
|
||||
loss_large = delta - self.C
|
||||
# Combine
|
||||
loss = torch.where(delta < self.w, loss_small, loss_large)
|
||||
|
||||
if self.reduction == 'mean':
|
||||
return loss.mean()
|
||||
elif self.reduction == 'sum':
|
||||
return loss.sum()
|
||||
return loss
|
||||
|
||||
class Model(nn.Module):
|
||||
def __init__(self, w=10.0, epsilon=2.0, reduction='mean'):
|
||||
super(Model, self).__init__()
|
||||
self.loss_fn = WingLoss(w=w, epsilon=epsilon, reduction=reduction)
|
||||
|
||||
def forward(self, pred, target):
|
||||
return self.loss_fn(pred, target)
|
||||
|
||||
def get_inputs():
|
||||
|
||||
pred = torch.randn(SHAPE, dtype=torch.float32)
|
||||
target = torch.randn(SHAPE, dtype=torch.float32)
|
||||
|
||||
return [pred.contiguous(), target.contiguous()]
|
||||
|
||||
def get_init_inputs():
|
||||
return [W_VAL, EPS_VAL, 'none']
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
###########################################################
|
||||
# 性能和精度验证程序
|
||||
###########################################################
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import time
|
||||
from wingloss_torch import Model,get_inputs,get_init_inputs
|
||||
from wingloss_cuda import ModelNew
|
||||
|
||||
def run_benchmark():
|
||||
# 检查 CUDA 是否可用
|
||||
if not torch.cuda.is_available():
|
||||
print("CUDA 不可用,请确保您有可用的 NVIDIA GPU 并已正确安装 PyTorch CUDA 版本。")
|
||||
return
|
||||
else:
|
||||
device = torch.device("cuda")
|
||||
|
||||
# 初始化模型
|
||||
init_inputs = get_init_inputs()
|
||||
init_inputs = [
|
||||
x.cuda(device=device) if isinstance(x, torch.Tensor) else x for x in init_inputs
|
||||
]
|
||||
inputs = get_inputs()
|
||||
inputs = [
|
||||
x.cuda(device=device) if isinstance(x, torch.Tensor) else x for x in inputs
|
||||
]
|
||||
|
||||
torch_model = Model(*init_inputs).cuda()
|
||||
cuda_model = ModelNew(*init_inputs).cuda()
|
||||
|
||||
torch_model.eval()
|
||||
cuda_model.eval()
|
||||
|
||||
print("-------------------- 精度对齐验证 --------------------")
|
||||
with torch.no_grad():
|
||||
output_torch = torch_model( *inputs)
|
||||
output_cuda = cuda_model(*inputs)
|
||||
|
||||
precision_flag = torch.allclose(output_torch, output_cuda,rtol=1e-03)
|
||||
if precision_flag:
|
||||
print("✅ 精度对齐:两个模型的输出结果非常接近。")
|
||||
else:
|
||||
print("❌ 精度不一致!")
|
||||
|
||||
print("\n-------------------- 性能加速比测试 --------------------")
|
||||
num_iterations = 100
|
||||
|
||||
# PyTorch 模型计时
|
||||
torch.cuda.synchronize()
|
||||
start_time = time.time()
|
||||
for _ in range(num_iterations):
|
||||
_ = torch_model(*inputs)
|
||||
torch.cuda.synchronize()
|
||||
torch_time = (time.time() - start_time) / num_iterations
|
||||
|
||||
# 自定义 CUDA 内核计时
|
||||
torch.cuda.synchronize()
|
||||
start_time = time.time()
|
||||
for _ in range(num_iterations):
|
||||
_ = cuda_model(*inputs)
|
||||
torch.cuda.synchronize()
|
||||
cuda_time = (time.time() - start_time) / num_iterations
|
||||
|
||||
print(f"PyTorch torch.relu 平均执行时间: {torch_time:.6f} 秒")
|
||||
print(f"自定义 CUDA 内核 平均执行时间: {cuda_time:.6f} 秒")
|
||||
speedup = 0
|
||||
if cuda_time > 0:
|
||||
speedup = torch_time / cuda_time
|
||||
print(f"加速比 (Speedup): {speedup:.2f}x")
|
||||
else:
|
||||
print("CUDA 内核执行时间为0,无法计算加速比。")
|
||||
return precision_flag,speedup
|
||||
if __name__ == "__main__":
|
||||
precision_flag,speedup = run_benchmark()
|
||||
|
|
@ -0,0 +1,154 @@
|
|||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.utils.cpp_extension import load_inline
|
||||
import math
|
||||
|
||||
# C++ 源代码 wrapper
|
||||
cpp_source = """
|
||||
#include <torch/extension.h>
|
||||
#include <string>
|
||||
|
||||
torch::Tensor wing_loss_cuda_forward(
|
||||
const torch::Tensor& pred,
|
||||
const torch::Tensor& target,
|
||||
float w,
|
||||
float epsilon,
|
||||
float C,
|
||||
std::string reduction);
|
||||
"""
|
||||
|
||||
# CUDA 源代码
|
||||
cuda_source = """
|
||||
#include <torch/extension.h>
|
||||
#include <cuda_runtime.h>
|
||||
#include <math.h>
|
||||
|
||||
#define BLOCK_SIZE 256
|
||||
|
||||
struct __align__(16) Float4 {
|
||||
float x, y, z, w;
|
||||
};
|
||||
|
||||
__device__ __forceinline__ float compute_point(float p, float t, float w, float eps, float C) {
|
||||
float dist = fabsf(t - p);
|
||||
if (dist < w) {
|
||||
// w * ln(1 + |x|/eps)
|
||||
return w * logf(1.0f + dist / eps);
|
||||
} else {
|
||||
// |x| - C
|
||||
return dist - C;
|
||||
}
|
||||
}
|
||||
|
||||
__global__ void wing_loss_kernel(
|
||||
float* __restrict__ output,
|
||||
const float* __restrict__ pred,
|
||||
const float* __restrict__ target,
|
||||
const int n_elements,
|
||||
const float w,
|
||||
const float eps,
|
||||
const float C)
|
||||
{
|
||||
const int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
|
||||
const int vec_n = n_elements / 4;
|
||||
|
||||
// Grid-Stride Loop (以 float4 为步长)
|
||||
int i = idx;
|
||||
const int stride = blockDim.x * gridDim.x;
|
||||
|
||||
for (; i < vec_n; i += stride) {
|
||||
Float4 p_vec = reinterpret_cast<const Float4*>(pred)[i];
|
||||
Float4 t_vec = reinterpret_cast<const Float4*>(target)[i];
|
||||
Float4 out_vec;
|
||||
|
||||
out_vec.x = compute_point(p_vec.x, t_vec.x, w, eps, C);
|
||||
out_vec.y = compute_point(p_vec.y, t_vec.y, w, eps, C);
|
||||
out_vec.z = compute_point(p_vec.z, t_vec.z, w, eps, C);
|
||||
out_vec.w = compute_point(p_vec.w, t_vec.w, w, eps, C);
|
||||
|
||||
reinterpret_cast<Float4*>(output)[i] = out_vec;
|
||||
}
|
||||
|
||||
// 2. Scalar Part (Tail)
|
||||
int scalar_i = i * 4;
|
||||
|
||||
int tail_start = vec_n * 4;
|
||||
int global_tid = idx;
|
||||
|
||||
if (global_tid < (n_elements - tail_start)) {
|
||||
int real_idx = tail_start + global_tid;
|
||||
output[real_idx] = compute_point(pred[real_idx], target[real_idx], w, eps, C);
|
||||
}
|
||||
}
|
||||
|
||||
torch::Tensor wing_loss_cuda_forward(
|
||||
const torch::Tensor& pred,
|
||||
const torch::Tensor& target,
|
||||
float w,
|
||||
float epsilon,
|
||||
float C,
|
||||
std::string reduction)
|
||||
{
|
||||
TORCH_CHECK(pred.is_cuda() && target.is_cuda(), "Inputs must be CUDA tensors");
|
||||
TORCH_CHECK(pred.is_contiguous() && target.is_contiguous(), "Inputs must be contiguous");
|
||||
TORCH_CHECK(pred.numel() == target.numel(), "Shapes must match");
|
||||
|
||||
const int n = pred.numel();
|
||||
auto output = torch::empty_like(pred);
|
||||
|
||||
const int vec_n = n / 4;
|
||||
const int grid_size = (vec_n + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
||||
|
||||
int final_grid = (grid_size < 1) ? 1 : grid_size;
|
||||
|
||||
// 限制最大 Grid 以防过大
|
||||
if (final_grid > 65535) final_grid = 65535;
|
||||
|
||||
wing_loss_kernel<<<final_grid, BLOCK_SIZE>>>(
|
||||
output.data_ptr<float>(),
|
||||
pred.data_ptr<float>(),
|
||||
target.data_ptr<float>(),
|
||||
n,
|
||||
w,
|
||||
epsilon,
|
||||
C
|
||||
);
|
||||
|
||||
if (reduction == "mean") {
|
||||
return output.mean();
|
||||
} else if (reduction == "sum") {
|
||||
return output.sum();
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
"""
|
||||
|
||||
class ModelNew(nn.Module):
|
||||
def __init__(self, w=10.0, epsilon=2.0, reduction='none'):
|
||||
super(ModelNew, self).__init__()
|
||||
self.w = w
|
||||
self.epsilon = epsilon
|
||||
self.reduction = reduction
|
||||
# Pre-compute Constant C
|
||||
self.C = self.w - self.w * math.log(1 + self.w / self.epsilon)
|
||||
|
||||
self.op = load_inline(
|
||||
name='wing_loss_op',
|
||||
cpp_sources=cpp_source,
|
||||
cuda_sources=cuda_source,
|
||||
functions=['wing_loss_cuda_forward'],
|
||||
verbose=False,
|
||||
extra_cuda_cflags=['-O3']
|
||||
)
|
||||
|
||||
def forward(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
||||
return self.op.wing_loss_cuda_forward(
|
||||
pred.contiguous(),
|
||||
target.contiguous(),
|
||||
self.w,
|
||||
self.epsilon,
|
||||
self.C,
|
||||
self.reduction
|
||||
)
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
import torch
|
||||
import torch.nn as nn
|
||||
import math
|
||||
|
||||
# 维度:68 points * 2 coordinates (x, y) = 136
|
||||
NUM_LANDMARKS = 136
|
||||
BATCH_SIZE = 32768
|
||||
SHAPE = (BATCH_SIZE, NUM_LANDMARKS)
|
||||
|
||||
W_VAL = 10.0
|
||||
EPS_VAL = 2.0
|
||||
|
||||
class WingLoss(nn.Module):
|
||||
def __init__(self, w=10.0, epsilon=2.0, reduction='mean'):
|
||||
super(WingLoss, self).__init__()
|
||||
self.w = w
|
||||
self.epsilon = epsilon
|
||||
self.reduction = reduction
|
||||
# Constant C: w - w * ln(1 + w/epsilon)
|
||||
self.C = self.w - self.w * math.log(1 + self.w / self.epsilon)
|
||||
|
||||
def forward(self, pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
|
||||
delta = (target - pred).abs()
|
||||
# Case 1: Small errors (|x| < w) -> ln form
|
||||
loss_small = self.w * torch.log(1 + delta / self.epsilon)
|
||||
# Case 2: Large errors (|x| >= w) -> linear form
|
||||
loss_large = delta - self.C
|
||||
# Combine
|
||||
loss = torch.where(delta < self.w, loss_small, loss_large)
|
||||
|
||||
if self.reduction == 'mean':
|
||||
return loss.mean()
|
||||
elif self.reduction == 'sum':
|
||||
return loss.sum()
|
||||
return loss
|
||||
|
||||
class Model(nn.Module):
|
||||
def __init__(self, w=10.0, epsilon=2.0, reduction='mean'):
|
||||
super(Model, self).__init__()
|
||||
self.loss_fn = WingLoss(w=w, epsilon=epsilon, reduction=reduction)
|
||||
|
||||
def forward(self, pred, target):
|
||||
return self.loss_fn(pred, target)
|
||||
|
||||
def get_inputs():
|
||||
|
||||
pred = torch.randn(SHAPE, dtype=torch.float32)
|
||||
target = torch.randn(SHAPE, dtype=torch.float32)
|
||||
|
||||
return [pred.contiguous(), target.contiguous()]
|
||||
|
||||
def get_init_inputs():
|
||||
return [W_VAL, EPS_VAL, 'none']
|
||||
Loading…
Reference in New Issue