Merge pull request 'finish crossentropyloss #17' (#35) from gsd123/GPUCodeForces:crossentropyloss into main

This commit is contained in:
Kuohais 2025-11-06 09:50:35 +08:00
commit 11bcbc62db
4 changed files with 293 additions and 0 deletions

View File

@ -0,0 +1,119 @@
import torch
from torch.utils.cpp_extension import load_inline
from CrossEntropyLoss_torch import BATCH_SIZE, FEATURE_DIM
class ModelNew(torch.nn.Module):
def __init__(self):
super().__init__()
self._compile_cuda_kernel()
def _compile_cuda_kernel(self):
cpp_source = """
#include <torch/extension.h>
torch::Tensor cel_forward_cuda(torch::Tensor input, torch::Tensor target);
"""
cuda_source = """
#include <cuda_runtime.h>
#include <float.h>
#include <cmath>
#define BLOCK_SIZE 256
__global__ void cel_fused_kernel(
const float* __restrict__ input, // [B, C]
const int64_t* __restrict__ target,// [B]
float* __restrict__ loss_partial, // [B]
int num_classes)
{
extern __shared__ float smem[]; // 动态 shared mem: logits & exp
float* logits = smem;
float* exp_logits = smem + num_classes;
int sample_idx = blockIdx.x;
const float* row = input + sample_idx * num_classes;
int label = target[sample_idx];
// 1. load logits to shared memory
for (int j = threadIdx.x; j < num_classes; j += blockDim.x) {
logits[j] = row[j];
}
__syncthreads();
// 2. compute row max for numerical stability
float local_max = -FLT_MAX;
for (int j = threadIdx.x; j < num_classes; j += blockDim.x)
local_max = fmaxf(local_max, logits[j]);
// block reduce max
__shared__ float row_max;
if (threadIdx.x == 0) row_max = -FLT_MAX;
__syncthreads();
atomicMax((int*)&row_max, __float_as_int(local_max));
__syncthreads();
// 3. compute exp(x - max) and sum
float local_sum = 0.0f;
for (int j = threadIdx.x; j < num_classes; j += blockDim.x) {
float e = expf(logits[j] - row_max);
exp_logits[j] = e;
local_sum += e;
}
// block reduce sum
__shared__ float row_sum;
if (threadIdx.x == 0) row_sum = 0.0f;
__syncthreads();
atomicAdd(&row_sum, local_sum);
__syncthreads();
// 4. compute -log(p_correct)
float loss_val = 0.0f;
if (threadIdx.x == 0) {
float p_correct = exp_logits[label] / row_sum;
loss_val = -logf(p_correct);
loss_partial[sample_idx] = loss_val;
}
}
torch::Tensor cel_forward_cuda(torch::Tensor input, torch::Tensor target) {
TORCH_CHECK(input.is_cuda(), "input must be CUDA tensor");
TORCH_CHECK(target.is_cuda(), "target must be CUDA tensor");
input = input.contiguous();
target = target.contiguous();
const int batch_size = input.size(0);
const int num_classes = input.size(1);
auto loss_buf = torch::empty({batch_size}, input.options());
const dim3 grid(batch_size);
const dim3 block(BLOCK_SIZE);
const size_t shmem_bytes = 2 * num_classes * sizeof(float);
cel_fused_kernel<<<grid, block, shmem_bytes>>>(
input.data_ptr<float>(),
target.data_ptr<int64_t>(),
loss_buf.data_ptr<float>(),
num_classes
);
return loss_buf.mean();
}
"""
self.cel_op = load_inline(
name="cel_fused_op_v2",
cpp_sources=cpp_source,
cuda_sources=cuda_source,
functions=["cel_forward_cuda"],
extra_cuda_cflags=["-O3", "--use_fast_math", "-lineinfo"],
verbose=False
)
def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
return self.cel_op.cel_forward_cuda(input, target)

View File

@ -0,0 +1,29 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
BATCH_SIZE = 4096
NUM_CLASSES = 1000 # 假设分类类别数
FEATURE_DIM = NUM_CLASSES # CrossEntropyLoss输入最后一维为类别数
class Model(nn.Module):
def __init__(self):
super().__init__()
# CrossEntropyLoss 会自动包含 LogSoftmax + NLLLoss
self.criterion = nn.CrossEntropyLoss(reduction='mean')
def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
return self.criterion(input, target)
def get_inputs():
# CrossEntropyLoss 输入logits [N, C]
input_scores = torch.randn(BATCH_SIZE, FEATURE_DIM, dtype=torch.float32)
# 目标标签每个样本一个类别索引0 ~ C-1
target = torch.randint(0, FEATURE_DIM, (BATCH_SIZE,), dtype=torch.long)
return [input_scores, target]
def get_init_inputs():
return []

68
S1/17/prompt.txt Normal file
View File

@ -0,0 +1,68 @@
You write custom CUDA kernels to replace the pytorch operators in the given 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 matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination.
// Key optimization techniques used in this implementation:
// 1. Operator Fusion: Fused log_softmax + negative log likelihood into a single kernel
// 2. Shared Memory Optimization: Utilizes shared memory for logits and exp computations
// 3. Fast Math Functions: Employs optimized mathematical operations including expf, logf
// 4. Hierarchical Reduction: Implements warp-level and block-level reductions for statistics
// 5. Memory Access Coalescing: Organized thread-block mapping for optimal global memory access patterns
// 6. Numerical Stability: Proper handling of max subtraction for stable softmax computation
// The custom CUDA implementation provides significant performance improvements over the native PyTorch version
// by eliminating intermediate tensor allocations and leveraging GPU-specific optimizations.
// Specific Technical Optimizations:
// Memory Hierarchy Optimization:
// 1. Shared Memory: Stores logits and exp values for fast intra-block access
// 2. Global Memory: Coalesced access patterns for input and target tensors
// 3. Register Utilization: Extensive use of registers for temporary computations
// Computational Optimizations:
// 1. Fast Exponential: expf() with numerical stability considerations
// 2. Hierarchical Reduction: Warp-level and block-level reductions for max and sum
// 3. Parallel Statistics: Concurrent computation of max, sum, and final loss
// Parallelism Strategy:
// 1. Grid Structure: One block per sample in batch (B blocks)
// 2. Block Configuration: 256 threads per block for optimal occupancy
// 3. Workload Distribution: Dynamic workload balancing across classes
// Numerical Precision:
// 1. Maintains mathematical equivalence with reference implementation
// 2. Proper max subtraction for numerical stability in softmax
// 3. Exact loss computation preserved despite performance optimizations
// The implementation demonstrates how custom CUDA kernels can dramatically accelerate cross-entropy loss
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
BATCH_SIZE = 4096
NUM_CLASSES = 1000 # 假设分类类别数
FEATURE_DIM = NUM_CLASSES # CrossEntropyLoss输入最后一维为类别数
class Model(nn.Module):
def __init__(self):
super().__init__()
# CrossEntropyLoss 会自动包含 LogSoftmax + NLLLoss
self.criterion = nn.CrossEntropyLoss(reduction='mean')
def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
return self.criterion(input, target)
def get_inputs():
# CrossEntropyLoss 输入logits [N, C]
input_scores = torch.randn(BATCH_SIZE, FEATURE_DIM, dtype=torch.float32)
# 目标标签每个样本一个类别索引0 ~ C-1
target = torch.randint(0, FEATURE_DIM, (BATCH_SIZE,), dtype=torch.long)
return [input_scores, target]
def get_init_inputs():
return []

77
S1/17/run_code.py Normal file
View File

@ -0,0 +1,77 @@
###########################################################
# 性能和精度验证程序
###########################################################
import torch
import torch.nn as nn
import time
from CrossEntropyLoss_torch import Model, get_inputs, get_init_inputs
from CrossEntropyLoss_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()