Three performance optimizations targeting decode throughput: 1. Decode Attention Kernel (csrc/attention/flash_attention.cu): - Specialized kernel for Q_len=1 (decode step) - 256 threads parallelize across KV sequence dimension - Online softmax with block-level warp-shuffle reduction - Replaces FA2 kernel which wasted 63/64 threads for decode - flash_attention() auto-dispatches when q_len==1 2. Fused SiLU×Mul (csrc/activation/activations.cu): - Single kernel: out = silu(gate) * up - Saves 1 HBM read + 1 HBM write per FFN layer (N elements) - Eliminates intermediate tensor allocation 3. Fused Add+RMSNorm (csrc/normalization/rmsnorm.cu): - Single kernel: (normed, sum) = (rmsnorm(x+residual), x+residual) - Saves 1 full HBM round-trip per attention block - Eliminates separate add + rmsnorm kernel pair Performance analysis: - At current short sequences (max 79 tokens), these optimizations provide marginal benefit because the bottleneck is cuBLAS GEMV overhead: 252 weight matrix reads × ~32MB each = 15.5 GB per decode step. Theoretical minimum at 1.79 TB/s = 8.7ms, actual ~78ms (9x gap). - The fused kernels and decode attention will show larger gains at longer sequences where attention and element-wise ops dominate. - Next optimization target: CUDA Graphs to eliminate kernel launch overhead, or custom GEMV kernels to replace cuBLAS for M=1. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
155 lines
5.9 KiB
Plaintext
155 lines
5.9 KiB
Plaintext
#include <cuda_bf16.h>
|
||
#include <math.h>
|
||
|
||
// GELU (tanh approximation):
|
||
// gelu(x) = 0.5 * x * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3)))
|
||
__device__ __forceinline__ float gelu_f(float x) {
|
||
const float SQRT_2_OVER_PI = 0.7978845608f;
|
||
float cube = x * x * x;
|
||
float inner = SQRT_2_OVER_PI * (x + 0.044715f * cube);
|
||
return 0.5f * x * (1.0f + tanhf(inner));
|
||
}
|
||
|
||
// SiLU (Swish): silu(x) = x * sigmoid(x) = x / (1 + exp(-x))
|
||
__device__ __forceinline__ float silu_f(float x) {
|
||
return x / (1.0f + expf(-x));
|
||
}
|
||
|
||
__global__ void gelu_f32(const float* x, float* out, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = gelu_f(x[idx]);
|
||
}
|
||
|
||
__global__ void gelu_bf16(const __nv_bfloat16* x, __nv_bfloat16* out, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = __float2bfloat16(gelu_f(__bfloat162float(x[idx])));
|
||
}
|
||
|
||
__global__ void silu_f32(const float* x, float* out, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = silu_f(x[idx]);
|
||
}
|
||
|
||
__global__ void silu_bf16(const __nv_bfloat16* x, __nv_bfloat16* out, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = __float2bfloat16(silu_f(__bfloat162float(x[idx])));
|
||
}
|
||
|
||
__global__ void scale_f32_kernel(const float* x, float* out, float scale, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = x[idx] * scale;
|
||
}
|
||
|
||
__global__ void scale_bf16_kernel(const __nv_bfloat16* x, __nv_bfloat16* out, float scale, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = __float2bfloat16(__bfloat162float(x[idx]) * scale);
|
||
}
|
||
|
||
// Fused SiLU×Mul: out = silu(gate) * up
|
||
__global__ void silu_mul_bf16_kernel(const __nv_bfloat16* gate, const __nv_bfloat16* up,
|
||
__nv_bfloat16* out, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) {
|
||
float g = __bfloat162float(gate[idx]);
|
||
float u = __bfloat162float(up[idx]);
|
||
float silu_g = g / (1.0f + expf(-g));
|
||
out[idx] = __float2bfloat16(silu_g * u);
|
||
}
|
||
}
|
||
|
||
// Element-wise add: out = a + b
|
||
__global__ void add_f32_kernel(const float* a, const float* b, float* out, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = a[idx] + b[idx];
|
||
}
|
||
__global__ void add_bf16_kernel(const __nv_bfloat16* a, const __nv_bfloat16* b, __nv_bfloat16* out, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = __float2bfloat16(__bfloat162float(a[idx]) + __bfloat162float(b[idx]));
|
||
}
|
||
|
||
// Element-wise mul: out = a * b
|
||
__global__ void mul_f32_kernel(const float* a, const float* b, float* out, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = a[idx] * b[idx];
|
||
}
|
||
__global__ void mul_bf16_kernel(const __nv_bfloat16* a, const __nv_bfloat16* b, __nv_bfloat16* out, int n) {
|
||
int idx = blockIdx.x * blockDim.x + threadIdx.x;
|
||
if (idx < n) out[idx] = __float2bfloat16(__bfloat162float(a[idx]) * __bfloat162float(b[idx]));
|
||
}
|
||
|
||
extern "C" {
|
||
|
||
void launch_gelu_f32(const void* x, void* out, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
gelu_f32<<<grid, block, 0, (cudaStream_t)stream>>>((const float*)x, (float*)out, n);
|
||
}
|
||
|
||
void launch_gelu_bf16(const void* x, void* out, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
gelu_bf16<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(const __nv_bfloat16*)x, (__nv_bfloat16*)out, n);
|
||
}
|
||
|
||
void launch_silu_f32(const void* x, void* out, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
silu_f32<<<grid, block, 0, (cudaStream_t)stream>>>((const float*)x, (float*)out, n);
|
||
}
|
||
|
||
void launch_silu_bf16(const void* x, void* out, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
silu_bf16<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(const __nv_bfloat16*)x, (__nv_bfloat16*)out, n);
|
||
}
|
||
|
||
void launch_scale_f32(const void* x, void* out, float scale, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
scale_f32_kernel<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(const float*)x, (float*)out, scale, n);
|
||
}
|
||
|
||
void launch_scale_bf16(const void* x, void* out, float scale, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
scale_bf16_kernel<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(const __nv_bfloat16*)x, (__nv_bfloat16*)out, scale, n);
|
||
}
|
||
|
||
void launch_add_f32(const void* a, const void* b, void* out, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
add_f32_kernel<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(const float*)a, (const float*)b, (float*)out, n);
|
||
}
|
||
void launch_add_bf16(const void* a, const void* b, void* out, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
add_bf16_kernel<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(const __nv_bfloat16*)a, (const __nv_bfloat16*)b, (__nv_bfloat16*)out, n);
|
||
}
|
||
void launch_mul_f32(const void* a, const void* b, void* out, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
mul_f32_kernel<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(const float*)a, (const float*)b, (float*)out, n);
|
||
}
|
||
void launch_mul_bf16(const void* a, const void* b, void* out, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
mul_bf16_kernel<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(const __nv_bfloat16*)a, (const __nv_bfloat16*)b, (__nv_bfloat16*)out, n);
|
||
}
|
||
|
||
void launch_silu_mul_bf16(const void* gate, const void* up, void* out, int n, void* stream) {
|
||
int block = 256;
|
||
int grid = (n + block - 1) / block;
|
||
silu_mul_bf16_kernel<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(const __nv_bfloat16*)gate, (const __nv_bfloat16*)up, (__nv_bfloat16*)out, n);
|
||
}
|
||
|
||
}
|