forked from ccf-ai-infra/GPUCodeForces
finish exp-square #42
This commit is contained in:
parent
10eed82956
commit
8c67c2d687
|
|
@ -0,0 +1,95 @@
|
|||
import torch
|
||||
from torch.utils.cpp_extension import load_inline
|
||||
|
||||
source = """
|
||||
#include <torch/extension.h>
|
||||
|
||||
__global__ void exp_square_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;
|
||||
int aligned = ((row_start & 3) == 0);
|
||||
if(aligned){
|
||||
int D4 = (D / 4) * 4;
|
||||
#pragma unroll 4
|
||||
for(int i = tid * 4; i < D4; i += stride * 4){
|
||||
int base = row_start + i;
|
||||
float4 xv = reinterpret_cast<const float4*>(x)[base / 4];
|
||||
float4 sv = reinterpret_cast<const float4*>(scale)[i / 4];
|
||||
float4 bv = reinterpret_cast<const float4*>(bias)[i / 4];
|
||||
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);
|
||||
float s0 = expf(z0 * z0);
|
||||
float s1 = expf(z1 * z1);
|
||||
float s2 = expf(z2 * z2);
|
||||
float s3 = expf(z3 * z3);
|
||||
float4 yv;
|
||||
yv.x = xv.x * (1.0f / (1.0f + expf(-(alpha * s0 + beta))));
|
||||
yv.y = xv.y * (1.0f / (1.0f + expf(-(alpha * s1 + beta))));
|
||||
yv.z = xv.z * (1.0f / (1.0f + expf(-(alpha * s2 + beta))));
|
||||
yv.w = xv.w * (1.0f / (1.0f + expf(-(alpha * s3 + beta))));
|
||||
reinterpret_cast<float4*>(y)[base / 4] = yv;
|
||||
}
|
||||
#pragma unroll 4
|
||||
for(int i = D4 + tid; i < D; i += stride){
|
||||
int base = row_start + i;
|
||||
float z = fmaf(x[base], scale[i], bias[i]);
|
||||
float s = expf(z * z);
|
||||
float g = 1.0f / (1.0f + expf(-(alpha * s + beta)));
|
||||
y[base] = x[base] * g;
|
||||
}
|
||||
} else {
|
||||
#pragma unroll 4
|
||||
for(int i = tid; i < D; i += stride){
|
||||
int base = row_start + i;
|
||||
float z = fmaf(x[base], scale[i], bias[i]);
|
||||
float s = expf(z * z);
|
||||
float g = 1.0f / (1.0f + expf(-(alpha * s + beta)));
|
||||
y[base] = x[base] * g;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
torch::Tensor exp_square_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>();
|
||||
float be = beta.item<float>();
|
||||
int block = 1024;
|
||||
int grid = B;
|
||||
exp_square_affine_gate_kernel<<<grid, block>>>(xc.data_ptr<float>(), sc.data_ptr<float>(), bc.data_ptr<float>(), y.data_ptr<float>(), B, D, a, be);
|
||||
return y;
|
||||
}
|
||||
"""
|
||||
|
||||
cpp_source = """
|
||||
torch::Tensor exp_square_affine_gate_cuda(torch::Tensor x, torch::Tensor scale, torch::Tensor bias, torch::Tensor alpha, torch::Tensor beta);
|
||||
"""
|
||||
|
||||
ops = load_inline(
|
||||
name="exp_square_affine_gate",
|
||||
cpp_sources=cpp_source,
|
||||
cuda_sources=source,
|
||||
functions=["exp_square_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: torch.Tensor):
|
||||
return self.ops.exp_square_affine_gate_cuda(x, self.scale, self.bias, self.alpha, self.beta)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
You write custom CUDA kernels to replace PyTorch operators for speedups.
|
||||
Implement Exp-Square Affine Gate on x[B,D]: Compute z = x*scale + bias, s = exp(z^2), gate g = sigmoid(alpha*s + beta), output y = x * g. Fuse affine, square, exp, sigmoid, and multiplication in a single grid-stride kernel. Provide a PyTorch reference with nn.Parameters for scale, bias, alpha, beta. Accuracy rtol=1e-3.
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
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():
|
||||
out_torch = torch_model(*inputs)
|
||||
out_cuda = cuda_model(*inputs)
|
||||
flag = torch.allclose(out_torch, out_cuda, rtol=1e-03)
|
||||
if flag:
|
||||
print("✅ 精度对齐:两个模型的输出结果非常接近。")
|
||||
else:
|
||||
print("❌ 精度不一致!")
|
||||
print(f"最大绝对误差: {(out_torch - out_cuda).abs().max().item()}" )
|
||||
|
||||
print("\n-------------------- 性能加速比测试 --------------------")
|
||||
iters = 100
|
||||
torch.cuda.synchronize(); t0 = time.time()
|
||||
for _ in range(iters):
|
||||
_ = torch_model(*inputs)
|
||||
torch.cuda.synchronize(); t_torch = (time.time() - t0) / iters
|
||||
|
||||
torch.cuda.synchronize(); t0 = time.time()
|
||||
for _ in range(iters):
|
||||
_ = cuda_model(*inputs)
|
||||
torch.cuda.synchronize(); t_cuda = (time.time() - t0) / iters
|
||||
|
||||
print(f"PyTorch Exp-Square-Affine-Gate 平均执行时间: {t_torch:.6f} 秒")
|
||||
print(f"自定义 CUDA 融合内核 平均执行时间: {t_cuda:.6f} 秒")
|
||||
sp = t_torch / t_cuda if t_cuda > 0 else 0
|
||||
if t_cuda > 0:
|
||||
print(f"加速比 (Speedup): {sp:.2f}x")
|
||||
else:
|
||||
print("CUDA 内核执行时间为0,无法计算加速比。")
|
||||
return flag, sp
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_benchmark()
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
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.scale = nn.Parameter(scale)
|
||||
self.bias = nn.Parameter(bias)
|
||||
self.alpha = nn.Parameter(torch.tensor(float(alpha), dtype=torch.float32))
|
||||
self.beta = nn.Parameter(torch.tensor(float(beta), dtype=torch.float32))
|
||||
|
||||
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
||||
z = x * self.scale.view(1,-1) + self.bias.view(1,-1)
|
||||
s = torch.exp(z * z)
|
||||
g = torch.sigmoid(self.alpha * s + self.beta)
|
||||
return x * g
|
||||
|
||||
B, D = 64, 8192
|
||||
|
||||
def get_inputs():
|
||||
x = torch.randn(B, D)
|
||||
return [x]
|
||||
|
||||
def get_init_inputs():
|
||||
scale = torch.randn(D)
|
||||
bias = torch.randn(D)
|
||||
alpha = 1.0
|
||||
beta = 0.0
|
||||
return [scale, bias, alpha, beta]
|
||||
Loading…
Reference in New Issue