From be16dfdaf54570d7d8ef32698609d849a6eeae7f Mon Sep 17 00:00:00 2001 From: Ljy123 Date: Sat, 6 Dec 2025 17:01:04 +0800 Subject: [PATCH] finish cosaffine gate #43 --- S1/Ljy123_#43/cudacode.py | 88 ++++++++++++++++++++++++++++++++++++++ S1/Ljy123_#43/prompt.txt | 28 ++++++++++++ S1/Ljy123_#43/run_code.py | 57 ++++++++++++++++++++++++ S1/Ljy123_#43/torchcode.py | 28 ++++++++++++ 4 files changed, 201 insertions(+) create mode 100644 S1/Ljy123_#43/cudacode.py create mode 100644 S1/Ljy123_#43/prompt.txt create mode 100644 S1/Ljy123_#43/run_code.py create mode 100644 S1/Ljy123_#43/torchcode.py diff --git a/S1/Ljy123_#43/cudacode.py b/S1/Ljy123_#43/cudacode.py new file mode 100644 index 00000000..d6f5f84b --- /dev/null +++ b/S1/Ljy123_#43/cudacode.py @@ -0,0 +1,88 @@ +import torch +from torch.utils.cpp_extension import load_inline + +source = """ +#include +#include + +__global__ void cos_affine_gate_kernel(const float* x, const float* scale, const float* bias, float* y, int B, int D, float alpha, float beta){ + int b = blockIdx.x; + int tid = threadIdx.x; + int stride = blockDim.x; + int row_start = b * D; + const float* xr = x + row_start; + float* yr = y + row_start; + int aligned = ((((long long)xr & 15LL) == 0) && (((long long)yr & 15LL) == 0) && (((long long)scale & 15LL) == 0) && (((long long)bias & 15LL) == 0) && ((D & 3) == 0)); + if(aligned){ + int D4 = (D / 4) * 4; + #pragma unroll 4 + for(int i = tid * 4; i < D4; i += stride * 4){ + float4 xv = reinterpret_cast(xr)[i / 4]; + float4 sv = reinterpret_cast(scale)[i / 4]; + float4 bv = reinterpret_cast(bias)[i / 4]; + float4 yv; + float z0 = fmaf(xv.x, sv.x, bv.x); + float z1 = fmaf(xv.y, sv.y, bv.y); + float z2 = fmaf(xv.z, sv.z, bv.z); + float z3 = fmaf(xv.w, sv.w, bv.w); + yv.x = xv.x * cosf(alpha * z0 + beta); + yv.y = xv.y * cosf(alpha * z1 + beta); + yv.z = xv.z * cosf(alpha * z2 + beta); + yv.w = xv.w * cosf(alpha * z3 + beta); + reinterpret_cast(yr)[i / 4] = yv; + } + #pragma unroll 4 + for(int i = D4 + tid; i < D; i += stride){ + float z = fmaf(xr[i], scale[i], bias[i]); + yr[i] = xr[i] * cosf(alpha * z + beta); + } + } else { + #pragma unroll 4 + for(int i = tid; i < D; i += stride){ + float z = fmaf(xr[i], scale[i], bias[i]); + yr[i] = xr[i] * cosf(alpha * z + beta); + } + } +} + +torch::Tensor cos_affine_gate_cuda(torch::Tensor x, torch::Tensor scale, torch::Tensor bias, torch::Tensor alpha, torch::Tensor beta){ + auto xc = x.contiguous(); + auto sc = scale.contiguous(); + auto bc = bias.contiguous(); + auto y = torch::empty_like(xc); + int B = (int)xc.size(0); + int D = (int)xc.size(1); + float a = alpha.item(); + float be = beta.item(); + int block = 1024; + int grid = B; + cos_affine_gate_kernel<<>>(xc.data_ptr(), sc.data_ptr(), bc.data_ptr(), y.data_ptr(), B, D, a, be); + return y; +} +""" + +cpp_source = """ +torch::Tensor cos_affine_gate_cuda(torch::Tensor x, torch::Tensor scale, torch::Tensor bias, torch::Tensor alpha, torch::Tensor beta); +""" + +ops = load_inline( + name="cos_affine_gate", + cpp_sources=cpp_source, + cuda_sources=source, + functions=["cos_affine_gate_cuda"], + extra_cuda_cflags=["-O3","--use_fast_math"], + verbose=True +) + +class ModelNew(torch.nn.Module): + def __init__(self, scale: torch.Tensor, bias: torch.Tensor, alpha: float, beta: float): + super(ModelNew, self).__init__() + self.ops = ops + self.register_buffer("scale", scale) + self.register_buffer("bias", bias) + self.register_buffer("alpha", torch.tensor(float(alpha), dtype=torch.float32)) + self.register_buffer("beta", torch.tensor(float(beta), dtype=torch.float32)) + + def forward(self, x): + return self.ops.cos_affine_gate_cuda(x, self.scale, self.bias, self.alpha, self.beta) + diff --git a/S1/Ljy123_#43/prompt.txt b/S1/Ljy123_#43/prompt.txt new file mode 100644 index 00000000..4272d953 --- /dev/null +++ b/S1/Ljy123_#43/prompt.txt @@ -0,0 +1,28 @@ +融合算子:Cos-Affine-Gate(一次核内完成仿射与余弦门控,直接返回 y = x * cos(α*z + β),其中 z = x*scale + bias)。该设计通过融合计算减少显存往返与内核启动,适合需要相位敏感的按维门控场景。 + +目标与定义 +- 输入张量:`x[B, D]` +- 逐维参数:`scale[D]`、`bias[D]` +- 标量超参:`alpha`、`beta` +- 计算流程:`z = x*scale + bias`,`g = cos(alpha*z + beta)`,`y = x * g` + +参考实现(文件要求) +- `torchcode.py`:提供 PyTorch 参考 `Model`,实现上述公式,`get_inputs()` 生成 `x[B,D]`,`get_init_inputs()` 生成 `scale[D]、bias[D]、alpha、beta` +- `cudacode.py`:使用 `load_inline` 嵌入 CUDA,实现单核融合(一次遍历完成仿射+cos 门控+乘法),`-O3 --use_fast_math` +- `run_code.py`:100 次迭代统计平均耗时;精度判定:`torch.allclose(..., rtol=1e-03, atol=1e-06)`;输出加速比 + +CUDA 实现要点 +- 并行策略:`grid = B`(一行一个 block),块内沿 D 做连续访存;保证同一行数据局部性 +- 对齐向量化:在 `x/scale/bias/y` 都 16 字节对齐且 `D%4==0` 时走 `float4` 路径;否则安全回退标量路径 +- 指令级优化:仿射用 `fmaf` 合并乘加;三角函数走 `--use_fast_math`;循环 `#pragma unroll 4` +- 访存模式:读取 `x、scale、bias`,直接写 `y`;仅一次全行遍历,无额外中间缓冲 +- 线程配置:推荐 `block=1024` 起步;根据设备与 D 尺度可微调至 `256/512/1024` + +评估与目标 +- 精度:与 PyTorch 参考在 `rtol=1e-03, atol=1e-06` 范围内对齐 +- 性能:在常见 `B=16, D=16384` 规模下,期望 ≥1.0x 加速;更大 B/D 通常加速更显著 + +加分项(可选) +- 针对未对齐场景,减少分支开销与循环尾处理的代价 +- 根据设备 SM 架构试探最佳 `block`,并在 `D` 很大时考虑每线程处理多个元素的批量步长 + diff --git a/S1/Ljy123_#43/run_code.py b/S1/Ljy123_#43/run_code.py new file mode 100644 index 00000000..0c102f23 --- /dev/null +++ b/S1/Ljy123_#43/run_code.py @@ -0,0 +1,57 @@ +import torch +import time +from torchcode import Model, get_inputs, get_init_inputs +from cudacode import ModelNew + +def run_benchmark(): + if not torch.cuda.is_available(): + print("CUDA 不可用,请确保您有可用的 NVIDIA GPU 并已正确安装 PyTorch CUDA 版本。") + return + device = torch.device("cuda") + + init_inputs = [x.cuda(device=device) if isinstance(x, torch.Tensor) else x for x in get_init_inputs()] + inputs = [x.cuda(device=device) if isinstance(x, torch.Tensor) else x for x in get_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, atol=1e-06) + if precision_flag: + print("✅ 精度对齐:两个模型的输出结果非常接近。") + else: + print("❌ 精度不一致!") + diff = (output_torch - output_cuda).abs().max().item() + print(f"最大绝对误差: {diff}") + print(f"输出张量形状: torch={tuple(output_torch.shape)}, cuda={tuple(output_cuda.shape)}") + print(f"数据类型: torch={output_torch.dtype}, cuda={output_cuda.dtype}") + print(f"设备: torch={output_torch.device}, cuda={output_cuda.device}") + + print("\n-------------------- 性能加速比测试 --------------------") + num_iterations = 100 + 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 + + 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 Cos-Affine-Gate 平均执行时间: {torch_time:.6f} 秒") + print(f"自定义 CUDA 融合内核 平均执行时间: {cuda_time:.6f} 秒") + speedup = torch_time / cuda_time if cuda_time > 0 else 0 + if cuda_time > 0: + print(f"加速比 (Speedup): {speedup:.2f}x") + else: + print("CUDA 内核执行时间为0,无法计算加速比。") + return precision_flag, speedup + +if __name__ == "__main__": + run_benchmark() + diff --git a/S1/Ljy123_#43/torchcode.py b/S1/Ljy123_#43/torchcode.py new file mode 100644 index 00000000..53c1ec96 --- /dev/null +++ b/S1/Ljy123_#43/torchcode.py @@ -0,0 +1,28 @@ +import torch +import torch.nn as nn + +class Model(nn.Module): + def __init__(self, scale: torch.Tensor, bias: torch.Tensor, alpha: float, beta: float): + super(Model, self).__init__() + self.register_buffer("scale", scale) + self.register_buffer("bias", bias) + self.register_buffer("alpha", torch.tensor(float(alpha), dtype=torch.float32)) + self.register_buffer("beta", torch.tensor(float(beta), dtype=torch.float32)) + + def forward(self, x: torch.Tensor) -> torch.Tensor: + z = x * self.scale + self.bias + g = torch.cos(self.alpha * z + self.beta) + return x * g + +batch_size = 16 +dim = 16384 + +def get_inputs(): + x = torch.randn(batch_size, dim) + return [x] + +def get_init_inputs(): + scale = torch.randn(dim) + bias = torch.randn(dim) + return [scale, bias, 1.0, 0.0] +