From 5650da6b59a2b3cc03401b0bad4c11b3db8d7fcb Mon Sep 17 00:00:00 2001 From: hli28146 Date: Mon, 17 Nov 2025 22:30:08 +0800 Subject: [PATCH] finish cross #9 --- S1/hli28146_#9/cross_cuda.py | 99 +++++++++++++++++++++++++++++++++++ S1/hli28146_#9/cross_torch.py | 27 ++++++++++ S1/hli28146_#9/prompt.txt | 47 +++++++++++++++++ S1/hli28146_#9/run_code.py | 74 ++++++++++++++++++++++++++ 4 files changed, 247 insertions(+) create mode 100644 S1/hli28146_#9/cross_cuda.py create mode 100644 S1/hli28146_#9/cross_torch.py create mode 100644 S1/hli28146_#9/prompt.txt create mode 100644 S1/hli28146_#9/run_code.py diff --git a/S1/hli28146_#9/cross_cuda.py b/S1/hli28146_#9/cross_cuda.py new file mode 100644 index 00000000..fbe37148 --- /dev/null +++ b/S1/hli28146_#9/cross_cuda.py @@ -0,0 +1,99 @@ +import torch +import torch.nn as nn +from torch.utils.cpp_extension import load_inline + +# C++ 源代码 +cpp_source = """ +#include + +torch::Tensor cross_cuda_forward(const torch::Tensor& x, const torch::Tensor& y, int64_t dim); +""" + +# CUDA 源代码 +cuda_source = """ +#include +#include + +template +__global__ void cross_kernel_stride( + T* output, + const T* x, + const T* y, + const int64_t num_vectors, + const int64_t dim_stride, + const int64_t slice_stride) +{{ + // 每个线程负责一个完整的叉积计算 + const int64_t idx = blockIdx.x * blockDim.x + threadIdx.x; + if (idx >= num_vectors) return; + + // --- 使用 stride 计算基地址 --- + const T* x_vec = x + idx * slice_stride; + const T* y_vec = y + idx * slice_stride; + T* out_vec = output + idx * slice_stride; + + // --- 使用 dim_stride 加载数据到寄存器 --- + const T x0 = x_vec[0]; + const T x1 = x_vec[dim_stride]; + const T x2 = x_vec[2 * dim_stride]; + const T y0 = y_vec[0]; + const T y1 = y_vec[dim_stride]; + const T y2 = y_vec[2 * dim_stride]; + + out_vec[0] = x1 * y2 - x2 * y1; + out_vec[dim_stride] = x2 * y0 - x0 * y2; + out_vec[2 * dim_stride] = x0 * y1 - x1 * y0; +}} + +torch::Tensor cross_cuda_forward(const torch::Tensor& x, const torch::Tensor& y, int64_t dim) { + auto sizes = x.sizes(); + int ndim = x.dim(); + if (dim < 0) dim += ndim; + + TORCH_CHECK(x.is_cuda() && y.is_cuda(), "Inputs must be CUDA tensors"); + TORCH_CHECK(x.sizes() == y.sizes(), "Input shapes must match"); + TORCH_CHECK(x.is_contiguous() && y.is_contiguous(), "Inputs must be contiguous for this implementation"); + TORCH_CHECK(sizes[dim] == 3, "Dimension for cross product must be 3"); + + // Stride 计算 + const int64_t dim_stride = x.stride(dim); + const int64_t num_vectors = x.numel() / 3; + + // 创建一个临时视图来计算 slice_stride + auto x_flat = x.transpose(dim, -1).flatten(0, -2); + const int64_t slice_stride = x_flat.stride(0); + + auto output = torch::empty_like(x); + + const int block_size = 256; + const int num_blocks = (num_vectors + block_size - 1) / block_size; + + AT_DISPATCH_FLOATING_TYPES(x.scalar_type(), "cross_kernel_stride", ([&] {{ + cross_kernel_stride<<>>( + output.data_ptr(), + x.data_ptr(), + y.data_ptr(), + num_vectors, + dim_stride, + slice_stride + ); + }})); + + return output; +} +""" + +class ModelNew(nn.Module): + def __init__(self, dim): + super(ModelNew, self).__init__() + self.dim = dim + self.op = load_inline( + name='cross_op_stride', + cpp_sources=cpp_source, + cuda_sources=cuda_source, + functions=['cross_cuda_forward'], + verbose=False + ) + + def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: + return self.op.cross_cuda_forward(x, y, self.dim) \ No newline at end of file diff --git a/S1/hli28146_#9/cross_torch.py b/S1/hli28146_#9/cross_torch.py new file mode 100644 index 00000000..982013c5 --- /dev/null +++ b/S1/hli28146_#9/cross_torch.py @@ -0,0 +1,27 @@ +import torch +import torch.nn as nn + +# --- 用于基准测试的配置 --- +BATCH_SIZE = 512 +VECTORS = 8192 +SHAPE = (BATCH_SIZE, VECTORS, 3) +DIM = -1 + +class Model(nn.Module): + """ + 使用 PyTorch 内置的 torch.linalg.cross 作为基准模型。 + """ + def __init__(self, dim): + super(Model, self).__init__() + self.dim = dim + + def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: + return torch.linalg.cross(x, y, dim=self.dim) + +def get_inputs(): + x = torch.randn(SHAPE, dtype=torch.float32) + y = torch.randn(SHAPE, dtype=torch.float32) + return [x.contiguous(), y.contiguous()] + +def get_init_inputs(): + return [DIM] \ No newline at end of file diff --git a/S1/hli28146_#9/prompt.txt b/S1/hli28146_#9/prompt.txt new file mode 100644 index 00000000..524c1bae --- /dev/null +++ b/S1/hli28146_#9/prompt.txt @@ -0,0 +1,47 @@ +Write a custom CUDA kernel to optimize `torch.linalg.cross`. + +The operation computes the cross product of two 3-dimensional vectors, batched over all other dimensions. The core formula for the output vector `c` from input vectors `a` and `b` is `c_1 = a_2*b_3 - a_3*b_2`, `c_2 = a_3*b_1 - a_1*b_3`, `c_3 = a_1*b_2 - a_2*b_1`. + +**Problem Analysis:** +`torch.linalg.cross` is a classic memory-bandwidth-bound operation. Its arithmetic intensity is very low (9 floating-point operations per 9 floats of memory I/O). A potential PyTorch implementation might involve slicing, element-wise multiplication, and subtraction, which could create intermediate tensors and add overhead. Even with a fused kernel, the overhead of the general PyTorch dispatcher can be significant for such a lightweight operation. + +**Optimization Strategy: Fused "One-Thread-per-Product" Kernel** + +The strategy is to create a minimalist, fully-fused CUDA kernel that maps the cross-product logic directly to the hardware with minimal overhead. + +1. **Parallelism Model**: The kernel is launched with one thread for every cross product to be computed. If the input shape is `(..., 3)`, the number of threads is `input.numel() / 3`. Each thread is completely independent. + +2. **Fully Fused In-Register Computation**: Each thread is responsible for one entire cross product calculation: + a. It computes the base address for its assigned 3-element input vectors in `x` and `y`. + b. It loads all 6 required float values (3 from `x`, 3 from `y`) from global memory directly into its private registers. + c. It performs all 6 multiplications and 3 subtractions entirely within registers, which is extremely fast. + d. It writes the 3 resulting float values directly to the correct locations in the output tensor. + +3. **Elimination of Overhead**: This approach constitutes a single pass over the data. It completely eliminates any intermediate tensors and bypasses the PyTorch dispatcher's general-purpose machinery, leading to a kernel whose performance is almost exclusively limited by the GPU's raw memory bandwidth. + +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 torch.nn.functional as F + + +class Model(nn.Module): + def __init__(self) -> None: + super().__init__() + + def forward(self, a, b): + return a + b + + +def get_inputs(): + # randomly generate input tensors based on the model architecture + a = torch.randn(1, 128).cuda() + b = torch.randn(1, 128).cuda() + return [a, b] + + +def get_init_inputs(): + # randomly generate tensors required for initialization based on the model architecture + return [] \ No newline at end of file diff --git a/S1/hli28146_#9/run_code.py b/S1/hli28146_#9/run_code.py new file mode 100644 index 00000000..d0e25664 --- /dev/null +++ b/S1/hli28146_#9/run_code.py @@ -0,0 +1,74 @@ +########################################################### +# 性能和精度验证程序 +########################################################### +import torch +import torch.nn as nn +import time +from cross_torch import Model,get_inputs,get_init_inputs +from cross_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() \ No newline at end of file