From 58324faafd8cba13c9f12d4275b9f6ccabed2bbd Mon Sep 17 00:00:00 2001 From: wut0n <3455534242@qq.com> Date: Tue, 9 Dec 2025 18:42:23 +0800 Subject: [PATCH] feat:add manhattan+swish #64 --- S1/wut0n_#64/manhattan_swish_cudacode.py | 294 ++++++++++++++++++++++ S1/wut0n_#64/manhattan_swish_torchcode.py | 49 ++++ S1/wut0n_#64/prompt.txt | 229 +++++++++++++++++ S1/wut0n_#64/run_code.py | 74 ++++++ 4 files changed, 646 insertions(+) create mode 100644 S1/wut0n_#64/manhattan_swish_cudacode.py create mode 100644 S1/wut0n_#64/manhattan_swish_torchcode.py create mode 100644 S1/wut0n_#64/prompt.txt create mode 100644 S1/wut0n_#64/run_code.py diff --git a/S1/wut0n_#64/manhattan_swish_cudacode.py b/S1/wut0n_#64/manhattan_swish_cudacode.py new file mode 100644 index 00000000..51b8cb8c --- /dev/null +++ b/S1/wut0n_#64/manhattan_swish_cudacode.py @@ -0,0 +1,294 @@ +import torch +from torch.utils.cpp_extension import load_inline + +manhattan_swish_source = """ +#include +#include + +// 纯CUDA实现:Manhattan + Swish融合 +__global__ void manhattan_swish_kernel( + const float* __restrict__ x, + const float* __restrict__ y, + float* __restrict__ distances, + float* __restrict__ swish_output, + int batch_size, + int feature_dim +) { + int sample_idx = blockIdx.x; + if (sample_idx >= batch_size) return; + + int tid = threadIdx.x; + int base = sample_idx * feature_dim; + + // 使用共享内存进行归约 + extern __shared__ float shared_sum[]; + shared_sum[tid] = 0.0f; + + // 每个线程处理4个元素(float4向量化) + int stride = blockDim.x * 4; + for (int dim = tid * 4; dim < feature_dim; dim += stride) { + // 确保不越界 + if (dim + 3 < feature_dim) { + float4 x_val = *reinterpret_cast(&x[base + dim]); + float4 y_val = *reinterpret_cast(&y[base + dim]); + + // 应用Swish激活: swish(x) = x * sigmoid(x) + float4 x_activated; + + // Swish(x) = x * sigmoid(x) = x / (1 + exp(-x)) + x_activated.x = x_val.x / (1.0f + expf(-x_val.x)); + x_activated.y = x_val.y / (1.0f + expf(-x_val.y)); + x_activated.z = x_val.z / (1.0f + expf(-x_val.z)); + x_activated.w = x_val.w / (1.0f + expf(-x_val.w)); + + // 存储激活后的输出 + *reinterpret_cast(&swish_output[base + dim]) = x_activated; + + // 计算Manhattan距离 + shared_sum[tid] += fabsf(x_activated.x - y_val.x) + fabsf(x_activated.y - y_val.y) + + fabsf(x_activated.z - y_val.z) + fabsf(x_activated.w - y_val.w); + } else { + // 处理剩余元素 + for (int i = dim; i < feature_dim; i++) { + float x_val = x[base + i]; + float y_val = y[base + i]; + + // 应用Swish激活 + float x_activated = x_val / (1.0f + expf(-x_val)); + swish_output[base + i] = x_activated; + + // 计算Manhattan距离 + float diff = x_activated - y_val; + shared_sum[tid] += fabsf(diff); + } + } + } + + __syncthreads(); + + // 块内归约求和 + for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) { + if (tid < stride) { + shared_sum[tid] += shared_sum[tid + stride]; + } + __syncthreads(); + } + + // 第一个线程写入结果 + if (tid == 0) { + distances[sample_idx] = shared_sum[0]; + } +} + +// Warp级优化版本 - Manhattan + Swish融合 +__global__ void manhattan_swish_kernel_warp( + const float* __restrict__ x, + const float* __restrict__ y, + float* __restrict__ distances, + float* __restrict__ swish_output, + int batch_size, + int feature_dim +) { + int sample_idx = blockIdx.x; + if (sample_idx >= batch_size) return; + + int tid = threadIdx.x; + int warp_id = tid / 32; + int lane_id = tid % 32; + + int base = sample_idx * feature_dim; + + // Warp级计算累加和 + float warp_sum = 0.0f; + + // 每个warp处理一部分特征 + int elements_per_warp = (feature_dim + 8 - 1) / 8; + int start_dim = warp_id * elements_per_warp; + int end_dim = min(start_dim + elements_per_warp, feature_dim); + + for (int dim = start_dim + lane_id; dim < end_dim; dim += 32) { + float x_val = x[base + dim]; + float y_val = y[base + dim]; + + // 应用Swish激活 + float x_activated = x_val / (1.0f + expf(-x_val)); + swish_output[base + dim] = x_activated; + + // 计算Manhattan距离 + float diff = x_activated - y_val; + warp_sum += fabsf(diff); + } + + // Warp级归约求和 + for (int offset = 16; offset > 0; offset /= 2) { + warp_sum += __shfl_down_sync(0xffffffff, warp_sum, offset); + } + + // 使用共享内存进行跨warp归约 + extern __shared__ float shared_data[]; + if (lane_id == 0) { + shared_data[warp_id] = warp_sum; + } + __syncthreads(); + + // 第一个线程找到全局总和 + if (tid == 0) { + float total_sum = 0.0f; + int num_warps = blockDim.x / 32; + for (int i = 0; i < num_warps; i++) { + total_sum += shared_data[i]; + } + distances[sample_idx] = total_sum; + } +} + +// 向量化Warp级优化版本 +__global__ void manhattan_swish_kernel_vectorized_warp( + const float* __restrict__ x, + const float* __restrict__ y, + float* __restrict__ distances, + float* __restrict__ swish_output, + int batch_size, + int feature_dim +) { + int sample_idx = blockIdx.x; + if (sample_idx >= batch_size) return; + + int tid = threadIdx.x; + int warp_id = tid / 32; + int lane_id = tid % 32; + + int base = sample_idx * feature_dim; + + // Warp级计算累加和 + float warp_sum = 0.0f; + + // 使用float4向量化 + const float4* x_vec = reinterpret_cast(x + base); + const float4* y_vec = reinterpret_cast(y + base); + float4* swish_vec = reinterpret_cast(swish_output + base); + + int feature_dim_vec = feature_dim / 4; + int elements_per_warp_vec = (feature_dim_vec + 8 - 1) / 8; + int start_vec = warp_id * elements_per_warp_vec; + int end_vec = min(start_vec + elements_per_warp_vec, feature_dim_vec); + + for (int vec_idx = start_vec + lane_id; vec_idx < end_vec; vec_idx += 32) { + float4 x_val = x_vec[vec_idx]; + float4 y_val = y_vec[vec_idx]; + + // 应用Swish激活: swish(x) = x * sigmoid(x) = x / (1 + exp(-x)) + float4 x_activated; + x_activated.x = x_val.x / (1.0f + expf(-x_val.x)); + x_activated.y = x_val.y / (1.0f + expf(-x_val.y)); + x_activated.z = x_val.z / (1.0f + expf(-x_val.z)); + x_activated.w = x_val.w / (1.0f + expf(-x_val.w)); + + // 存储激活后的输出 + swish_vec[vec_idx] = x_activated; + + // 计算Manhattan距离 + warp_sum += fabsf(x_activated.x - y_val.x) + fabsf(x_activated.y - y_val.y) + + fabsf(x_activated.z - y_val.z) + fabsf(x_activated.w - y_val.w); + } + + // 处理剩余元素 + int remaining_start = feature_dim_vec * 4; + for (int dim = remaining_start + warp_id * 32 + lane_id; dim < feature_dim; dim += 256) { + float x_val = x[base + dim]; + float y_val = y[base + dim]; + + // 应用Swish激活 + float x_activated = x_val / (1.0f + expf(-x_val)); + swish_output[base + dim] = x_activated; + + // 计算Manhattan距离 + float diff = x_activated - y_val; + warp_sum += fabsf(diff); + } + + // Warp级归约求和 + for (int offset = 16; offset > 0; offset /= 2) { + warp_sum += __shfl_down_sync(0xffffffff, warp_sum, offset); + } + + // 使用共享内存进行跨warp归约 + extern __shared__ float shared_data[]; + if (lane_id == 0) { + shared_data[warp_id] = warp_sum; + } + __syncthreads(); + + // 第一个线程找到全局总和 + if (tid == 0) { + float total_sum = 0.0f; + int num_warps = blockDim.x / 32; + for (int i = 0; i < num_warps; i++) { + total_sum += shared_data[i]; + } + distances[sample_idx] = total_sum; + } +} + +// 主函数 - 纯CUDA实现 +torch::Tensor manhattan_swish_cuda( + torch::Tensor x, + torch::Tensor y +) { + // 输入验证 + TORCH_CHECK(x.scalar_type() == torch::kFloat32, "X must be float32"); + TORCH_CHECK(y.scalar_type() == torch::kFloat32, "Y must be float32"); + TORCH_CHECK(x.sizes() == y.sizes(), "X and Y must have same shape"); + + auto x_contig = x.contiguous(); + auto y_contig = y.contiguous(); + + int batch_size = x_contig.size(0); + int feature_dim = x_contig.size(1); + + // 创建输出张量 + auto distances = torch::zeros({batch_size}, x.options()); + auto swish_output = torch::empty_like(x_contig); + + const int block_size = 256; // 8个warps + size_t shared_mem = 8 * sizeof(float); // 8个warp的结果 + + // 使用向量化Warp级优化版本 + manhattan_swish_kernel_vectorized_warp<<>>( + x_contig.data_ptr(), + y_contig.data_ptr(), + distances.data_ptr(), + swish_output.data_ptr(), + batch_size, + feature_dim + ); + + return distances; +} +""" + +manhattan_swish_cpp_source = """ +torch::Tensor manhattan_swish_cuda(torch::Tensor x, torch::Tensor y); +""" + +# 编译CUDA代码 +manhattan_swish = load_inline( + name="manhattan_swish", + cpp_sources=manhattan_swish_cpp_source, + cuda_sources=manhattan_swish_source, + functions=["manhattan_swish_cuda"], + extra_cuda_cflags=[ + "-O3", + "--use_fast_math", + "-gencode=arch=compute_80,code=sm_80" + ], + verbose=True +) + +class ModelNew(torch.nn.Module): + def __init__(self): + super(ModelNew, self).__init__() + self.manhattan_swish = manhattan_swish + + def forward(self, x, y): + return self.manhattan_swish.manhattan_swish_cuda(x, y) diff --git a/S1/wut0n_#64/manhattan_swish_torchcode.py b/S1/wut0n_#64/manhattan_swish_torchcode.py new file mode 100644 index 00000000..9fb04c75 --- /dev/null +++ b/S1/wut0n_#64/manhattan_swish_torchcode.py @@ -0,0 +1,49 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F + +class Model(nn.Module): + """ + Manhattan + Swish融合实现。 + 先对输入应用Swish激活,然后计算Manhattan距离。 + """ + def __init__(self): + super(Model, self).__init__() + + def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: + """ + Compute Manhattan + Swish fusion. + + Args: + x (torch.Tensor): First set of vectors [batch_size, feature_dim] + y (torch.Tensor): Second set of vectors [batch_size, feature_dim] + + Returns: + torch.Tensor: Manhattan distances after Swish activation [batch_size] + """ + # Input validation + if x.shape != y.shape: + raise ValueError(f"Input tensors must have the same shape, got {x.shape} and {y.shape}") + + if x.dim() != 2: + raise ValueError(f"Input tensors must be 2D, got {x.dim()}D") + + # Apply Swish activation to x + x_activated = F.silu(x) # Swish is also known as SiLU + + # Compute Manhattan distance: Σ|x_activated - y| + manhattan_dist = torch.sum(torch.abs(x_activated - y), dim=1) + + return manhattan_dist + +batch_size = 512 +feature_dim = 512 + +def get_inputs(): + # Generate two sets of vectors + x = torch.randn(batch_size, feature_dim) + y = torch.randn(batch_size, feature_dim) + return [x, y] + +def get_init_inputs(): + return [] # No special initialization inputs needed diff --git a/S1/wut0n_#64/prompt.txt b/S1/wut0n_#64/prompt.txt new file mode 100644 index 00000000..701c207b --- /dev/null +++ b/S1/wut0n_#64/prompt.txt @@ -0,0 +1,229 @@ +You write custom CUDA kernels to replace pytorch operators in given architecture to get speedups. You have complete freedom to choose 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. + +**SPECIAL INSTRUCTIONS FOR MANHATTAN + SWISH FUSION:** + +When implementing Manhattan Distance + Swish fusion, you MUST implement the following optimized strategy: + +1. **FUSION ARCHITECTURE**: Combine Swish activation and Manhattan distance computation in a single kernel: + - Apply Swish activation to input tensor x first + - Compute Manhattan distance between activated x and y + - Eliminate intermediate tensor storage for maximum efficiency + - Store both activated output and distance results + +2. **FLOAT4 VECTORIZATION**: Use float4 vectorization for maximum memory bandwidth utilization: + - Process 4 elements simultaneously using float4 loads/stores + - Apply Swish activation to all 4 components in parallel + - Compute Manhattan distance for all 4 components together + - Handle remaining elements with scalar processing + +3. **WARP-LEVEL OPTIMIZATION**: Use warp-level processing for maximum performance: + - Each block processes one sample from the batch + - Use 8 warps per block (256 threads) for optimal GPU utilization + - Use __shfl_down_sync for efficient warp-level reduction of sums + - Divide feature dimensions among warps for parallel processing + +4. **MEMORY COALESCING**: Ensure efficient memory access patterns: + - Use float4 vectorized loads for coalesced memory access + - Store Swish results using float4 vectorized stores + - Each thread processes multiple elements with stride pattern + - Minimize global memory accesses through fusion + +5. **EFFICIENT SUM REDUCTION**: Implement optimized sum reduction for Manhattan distance: +cpp +// Warp-level sum reduction +for (int offset = 16; offset > 0; offset /= 2) { + warp_sum += __shfl_down_sync(0xffffffff, warp_sum, offset); +} + +// Cross-warp reduction using shared memory +extern __shared__ float shared_data[]; +if (lane_id == 0) { + shared_data[warp_id] = warp_sum; +} +__syncthreads(); + + + +6. **SWISH FUSION**: Integrate Swish activation seamlessly with vectorization: +cpp +// Apply Swish activation to float4 vector +// Swish(x) = x * sigmoid(x) = x / (1 + exp(-x)) +float4 x_activated; +x_activated.x = x_val.x / (1.0f + expf(-x_val.x)); +x_activated.y = x_val.y / (1.0f + expf(-x_val.y)); +x_activated.z = x_val.z / (1.0f + expf(-x_val.z)); +x_activated.w = x_val.w / (1.0f + expf(-x_val.w)); + +// Store activated output +swish_vec[vec_idx] = x_activated; + +// Compute Manhattan distance for all 4 components +warp_sum += fabsf(x_activated.x - y_val.x) + fabsf(x_activated.y - y_val.y) + + fabsf(x_activated.z - y_val.z) + fabsf(x_activated.w - y_val.w); + + + +7. **SHARED MEMORY PATTERN**: Use efficient shared memory organization: +cpp +// For sum reduction across warps +extern __shared__ float shared_data[]; +if (lane_id == 0) { + shared_data[warp_id] = warp_sum; +} +__syncthreads(); + +// Final sum calculation +if (tid == 0) { + float total_sum = 0.0f; + int num_warps = blockDim.x / 32; + for (int i = 0; i < num_warps; i++) { + total_sum += shared_data[i]; + } + distances[sample_idx] = total_sum; +} + + + +8. **BLOCK CONFIGURATION**: Use optimal settings for vectorized processing: + - Block size: 256 threads (8 warps) + - Shared memory: 8 * sizeof(float) for warp reduction results + - One block per sample for maximum parallelism + - Elements per warp: (feature_dim + 8 - 1) / 8 + +9. **PRECISION REQUIREMENTS**: Ensure exact mathematical alignment: + - Swish Activation: swish(x) = x * sigmoid(x) = x / (1 + exp(-x)) + - Manhattan Distance: Σ|swish(x) - y| + - Use fabsf for absolute value computation + - Use expf for exponential computation + - Verify with torch.allclose(rtol=1e-03, atol=1e-6) + +10. **FUNCTION SIGNATURE**: The main CUDA function must accept all parameters: +cpp +torch::Tensor manhattan_swish_cuda( + torch::Tensor x, + torch::Tensor y +) + + + +11. **MATHEMATICAL FORMULAS**: Implement exact mathematical operations: + - Swish Activation: swish(x) = x * sigmoid(x) = x / (1 + exp(-x)) + - Absolute Difference: abs_diff = |swish(x) - y| + - Manhattan Distance: manhattan_dist = Σabs_diff + +12. **PYTHON CALLING CONVENTION**: The ModelNew forward method must pass parameters correctly: +python +def forward(self, x, y): + return self.manhattan_swish.manhattan_swish_cuda(x, y) + + + +13. **OUTPUT REQUIREMENTS**: Generate both distances and activated outputs: + - Primary output: Manhattan distances after Swish activation [batch_size] + - Secondary output: Swish activated tensor [batch_size, feature_dim] + - Both outputs must match PyTorch reference implementation exactly + +14. **PERFORMANCE OPTIMIZATIONS**: Include advanced optimizations: + - Use fast math optimizations (--use_fast_math) + - Optimize for compute capability 8.0+ (sm_80) + - Use -O3 optimization level + - Avoid bank conflicts in shared memory access + - Use efficient memory access patterns + +15. **ALGORITHM CHOICE**: Prioritize the vectorized fused approach: + - float4 vectorization is mandatory for this implementation + - Do NOT implement scalar-only versions + - The fusion must happen at the CUDA kernel level, not Python level + - Eliminate all intermediate tensor storage + +16. **BOUNDARY HANDLING**: Properly handle non-multiple-of-4 feature dimensions: + - Use float4 for vectorized processing of main portion + - Handle remaining elements with scalar processing + - Ensure no memory access violations + - Maintain mathematical correctness for all dimensions + +17. **SWISH OPTIMIZATION**: Use the most efficient Swish implementation: + - Swish(x) = x / (1 + exp(-x)) is more efficient than x * sigmoid(x) + - Pre-compute denominator: 1.0f + expf(-x) + - Avoid redundant sigmoid calculations + - Use single division per element + +Here's the target architecture to optimize: + +python +import torch +import torch.nn as nn + +class Model(nn.Module): +""" +Manhattan Distance implementation. +Computes the Manhattan distance (L1 distance) between two sets of vectors. +""" +def init(self): +super(Model, self).init() + +def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: + """ + Compute Manhattan distance between x and y. + + Args: + x (torch.Tensor): First set of vectors [batch_size, feature_dim] + y (torch.Tensor): Second set of vectors [batch_size, feature_dim] + + Returns: + torch.Tensor: Manhattan distances [batch_size] + """ + # Input validation + if x.shape != y.shape: + raise ValueError(f"Input tensors must have the same shape, got {x.shape} and {y.shape}") + + if x.dim() != 2: + raise ValueError(f"Input tensors must be 2D, got {x.dim()}D") + + # Compute Manhattan distance: Σ|x_i - y_i| + manhattan_dist = torch.sum(torch.abs(x - y), dim=1) + + return manhattan_dist + +batch_size = 512 +feature_dim = 512 + +def get_inputs(): +# Generate two sets of vectors +x = torch.randn(batch_size, feature_dim) +y = torch.randn(batch_size, feature_dim) +return [x, y] + +def get_init_inputs(): +return [] # No special initialization inputs needed + + + +**EXPECTED OUTPUT STRUCTURE**: +Generate two files: +1. `manhattan_swish_cudacode.py` - Contains ModelNew class with Manhattan+Swish fusion using pure CUDA +2. `manhattan_swish_torchcode.py` - Contains the reference PyTorch implementation with Swish fusion + +**KEY REQUIREMENTS**: +- The CUDA implementation must use pure CUDA functions only +- Must implement Swish activation before Manhattan distance computation +- Must use float4 vectorization for maximum performance +- Must use warp-level optimization for maximum performance +- Must use efficient sum reduction algorithm +- Must handle arbitrary tensor shapes (not just fixed dimensions) +- Must maintain mathematical precision with PyTorch implementation +- Must use optimal block configuration (256 threads, 8 warps) +- Expected speedup: 1.6-2.3x over PyTorch baseline +- Must use fast math optimizations for better performance +- Must be robust and handle edge cases properly +- Must use only pure CUDA functions (no PyTorch internal functions) +- Must use fabsf for absolute value computation +- Must use expf for exponential computation +- Must implement exact mathematical formulas for Swish and Manhattan distance +- Must generate both distance and activated output tensors +- Must use shared memory efficiently for warp-level sum reduction +- Must ensure coalesced memory access patterns +- Must eliminate intermediate tensor storage for maximum fusion benefits +- Must implement the complete fusion in a single CUDA kernel +- Must use float4 vectorization as the primary optimization strategy +- Must use the most efficient Swish formula: swish(x) = x / (1 + exp(-x)) diff --git a/S1/wut0n_#64/run_code.py b/S1/wut0n_#64/run_code.py new file mode 100644 index 00000000..be72995a --- /dev/null +++ b/S1/wut0n_#64/run_code.py @@ -0,0 +1,74 @@ +########################################################### +# 性能和精度验证程序 +########################################################### +import torch +import torch.nn as nn +import time +from manhattan_swish_torchcode import Model, get_inputs, get_init_inputs +from manhattan_swish_cudacode 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 manhattan_swish 平均执行时间: {torch_time:.6f} 秒") + print(f"自定义 CUDA manhattan_swish 平均执行时间: {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()