File size: 15,247 Bytes
32b700f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | // DiT bf16 helpers β Phase 5a-2 (R0: pure additions, no edits to existing
// fp16 kernels). Mirrors layer_norm_no_affine_fp16 / ada_layer_norm_fp16 /
// add_bias_fp16 but with __nv_bfloat162 vectorized math, plus cast helpers
// for fp16 β bf16 boundaries.
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cuda_bf16.h>
#include <cuda_fp8.h>
#include <cstdint>
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// LayerNorm (no affine) β bf16
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
__global__ void layer_norm_no_affine_bf16_kernel(const __nv_bfloat16* __restrict__ x,
__nv_bfloat16* __restrict__ out,
int dim, float eps) {
int row = blockIdx.x;
const __nv_bfloat162* x2 = reinterpret_cast<const __nv_bfloat162*>(x + row * dim);
__nv_bfloat162* out2 = reinterpret_cast<__nv_bfloat162*>(out + row * dim);
int dim2 = dim >> 1;
extern __shared__ float shared[];
float local_sum = 0.0f;
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 val = x2[i];
local_sum += __bfloat162float(val.x) + __bfloat162float(val.y);
}
float val = local_sum;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
int lane = threadIdx.x & 31, wid = threadIdx.x >> 5;
if (!lane) shared[wid] = val;
__syncthreads();
if (!wid) { val = (lane < (blockDim.x >> 5)) ? shared[lane] : 0;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o); }
__syncthreads();
if (!threadIdx.x) shared[0] = val;
__syncthreads();
float mean = shared[0] / dim;
float local_var = 0.0f;
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 v = x2[i];
float d0 = __bfloat162float(v.x) - mean, d1 = __bfloat162float(v.y) - mean;
local_var += d0 * d0 + d1 * d1;
}
val = local_var;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
if (!lane) shared[wid] = val;
__syncthreads();
if (!wid) { val = (lane < (blockDim.x >> 5)) ? shared[lane] : 0;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o); }
__syncthreads();
if (!threadIdx.x) shared[0] = val;
__syncthreads();
float inv_std = rsqrtf(shared[0] / dim + eps);
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 xv = x2[i];
float v0 = (__bfloat162float(xv.x) - mean) * inv_std;
float v1 = (__bfloat162float(xv.y) - mean) * inv_std;
out2[i] = __floats2bfloat162_rn(v0, v1);
}
}
void layer_norm_no_affine_bf16(const __nv_bfloat16* x, __nv_bfloat16* out,
int seq_len, int dim, float eps,
cudaStream_t stream) {
layer_norm_no_affine_bf16_kernel<<<seq_len, 256, 256 * sizeof(float), stream>>>(
x, out, dim, eps);
}
__global__ void layer_norm_no_affine_fp8_static_bf16_kernel(
const __nv_bfloat16* __restrict__ x,
__nv_fp8_e4m3* __restrict__ out,
const float* __restrict__ d_scale,
int dim, float eps) {
int row = blockIdx.x;
const __nv_bfloat162* x2 =
reinterpret_cast<const __nv_bfloat162*>(x + (long long)row * dim);
__nv_fp8_e4m3* out_row = out + (long long)row * dim;
int dim2 = dim >> 1;
extern __shared__ float shared[];
float local_sum = 0.0f;
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 val = x2[i];
local_sum += __bfloat162float(val.x) + __bfloat162float(val.y);
}
float val = local_sum;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
int lane = threadIdx.x & 31, wid = threadIdx.x >> 5;
if (!lane) shared[wid] = val;
__syncthreads();
if (!wid) {
val = (lane < (blockDim.x >> 5)) ? shared[lane] : 0.0f;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
}
__syncthreads();
if (!threadIdx.x) shared[0] = val;
__syncthreads();
float mean = shared[0] / dim;
float local_var = 0.0f;
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 v = x2[i];
float d0 = __bfloat162float(v.x) - mean;
float d1 = __bfloat162float(v.y) - mean;
local_var += d0 * d0 + d1 * d1;
}
val = local_var;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
if (!lane) shared[wid] = val;
__syncthreads();
if (!wid) {
val = (lane < (blockDim.x >> 5)) ? shared[lane] : 0.0f;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
}
__syncthreads();
if (!threadIdx.x) shared[0] = val;
__syncthreads();
float inv_std = rsqrtf(shared[0] / dim + eps);
float inv_a = 1.0f / fmaxf(*d_scale, 1e-12f);
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 xv = x2[i];
float v0 = (__bfloat162float(xv.x) - mean) * inv_std;
float v1 = (__bfloat162float(xv.y) - mean) * inv_std;
v0 = __bfloat162float(__float2bfloat16(v0)) * inv_a;
v1 = __bfloat162float(__float2bfloat16(v1)) * inv_a;
__nv_fp8_e4m3 pair[2];
pair[0] = __nv_fp8_e4m3(fminf(fmaxf(v0, -448.0f), 448.0f));
pair[1] = __nv_fp8_e4m3(fminf(fmaxf(v1, -448.0f), 448.0f));
*reinterpret_cast<uint16_t*>(out_row + 2 * i) =
*reinterpret_cast<uint16_t*>(pair);
}
}
void layer_norm_no_affine_fp8_static_bf16(
const __nv_bfloat16* x, __nv_fp8_e4m3* out, const float* d_scale,
int seq_len, int dim, float eps, cudaStream_t stream) {
layer_norm_no_affine_fp8_static_bf16_kernel
<<<seq_len, 256, 256 * sizeof(float), stream>>>(
x, out, d_scale, dim, eps);
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// AdaLayerNorm β bf16
// out = LN(x, no_affine) * (1 + scale) + shift
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
__global__ void ada_layer_norm_bf16_kernel(const __nv_bfloat16* __restrict__ x,
const __nv_bfloat16* __restrict__ scale,
const __nv_bfloat16* __restrict__ shift,
__nv_bfloat16* __restrict__ out,
int dim, float eps) {
int row = blockIdx.x;
const __nv_bfloat162* x2 = reinterpret_cast<const __nv_bfloat162*>(x + row * dim);
const __nv_bfloat162* sc2 = reinterpret_cast<const __nv_bfloat162*>(scale);
const __nv_bfloat162* sh2 = reinterpret_cast<const __nv_bfloat162*>(shift);
__nv_bfloat162* out2 = reinterpret_cast<__nv_bfloat162*>(out + row * dim);
int dim2 = dim >> 1;
extern __shared__ float shared[];
float local_sum = 0.0f;
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 val = x2[i];
local_sum += __bfloat162float(val.x) + __bfloat162float(val.y);
}
float val = local_sum;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
int lane = threadIdx.x & 31, wid = threadIdx.x >> 5;
if (!lane) shared[wid] = val;
__syncthreads();
if (!wid) { val = (lane < (blockDim.x >> 5)) ? shared[lane] : 0;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o); }
__syncthreads(); if (!threadIdx.x) shared[0] = val; __syncthreads();
float mean = shared[0] / dim;
float local_var = 0.0f;
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 v = x2[i];
float d0 = __bfloat162float(v.x) - mean, d1 = __bfloat162float(v.y) - mean;
local_var += d0 * d0 + d1 * d1;
}
val = local_var;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
if (!lane) shared[wid] = val;
__syncthreads();
if (!wid) { val = (lane < (blockDim.x >> 5)) ? shared[lane] : 0;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o); }
__syncthreads(); if (!threadIdx.x) shared[0] = val; __syncthreads();
float inv_std = rsqrtf(shared[0] / dim + eps);
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 xv = x2[i], sv = sc2[i], hv = sh2[i];
float n0 = (__bfloat162float(xv.x) - mean) * inv_std;
float n1 = (__bfloat162float(xv.y) - mean) * inv_std;
float v0 = n0 * (1.0f + __bfloat162float(sv.x)) + __bfloat162float(hv.x);
float v1 = n1 * (1.0f + __bfloat162float(sv.y)) + __bfloat162float(hv.y);
out2[i] = __floats2bfloat162_rn(v0, v1);
}
}
void ada_layer_norm_bf16(const __nv_bfloat16* x,
const __nv_bfloat16* scale, const __nv_bfloat16* shift,
__nv_bfloat16* out, int seq_len, int dim, float eps,
cudaStream_t stream) {
ada_layer_norm_bf16_kernel<<<seq_len, 256, 256 * sizeof(float), stream>>>(
x, scale, shift, out, dim, eps);
}
// Per-row AdaLayerNorm β bf16
// out[row] = LN(x[row], no_affine) * (1 + scale[row]) + shift[row]
__global__ void ada_layer_norm_bf16_per_token_kernel(
const __nv_bfloat16* __restrict__ x,
const __nv_bfloat16* __restrict__ scale,
const __nv_bfloat16* __restrict__ shift,
__nv_bfloat16* __restrict__ out,
int dim, float eps) {
int row = blockIdx.x;
const __nv_bfloat162* x2 = reinterpret_cast<const __nv_bfloat162*>(x + row * dim);
const __nv_bfloat162* sc2 = reinterpret_cast<const __nv_bfloat162*>(scale + row * dim);
const __nv_bfloat162* sh2 = reinterpret_cast<const __nv_bfloat162*>(shift + row * dim);
__nv_bfloat162* out2 = reinterpret_cast<__nv_bfloat162*>(out + row * dim);
int dim2 = dim >> 1;
extern __shared__ float shared[];
float local_sum = 0.0f;
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 val = x2[i];
local_sum += __bfloat162float(val.x) + __bfloat162float(val.y);
}
float val = local_sum;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
int lane = threadIdx.x & 31, wid = threadIdx.x >> 5;
if (!lane) shared[wid] = val;
__syncthreads();
if (!wid) { val = (lane < (blockDim.x >> 5)) ? shared[lane] : 0;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o); }
__syncthreads(); if (!threadIdx.x) shared[0] = val; __syncthreads();
float mean = shared[0] / dim;
float local_var = 0.0f;
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 v = x2[i];
float d0 = __bfloat162float(v.x) - mean, d1 = __bfloat162float(v.y) - mean;
local_var += d0 * d0 + d1 * d1;
}
val = local_var;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o);
if (!lane) shared[wid] = val;
__syncthreads();
if (!wid) { val = (lane < (blockDim.x >> 5)) ? shared[lane] : 0;
for (int o = 16; o > 0; o >>= 1) val += __shfl_xor_sync(0xffffffff, val, o); }
__syncthreads(); if (!threadIdx.x) shared[0] = val; __syncthreads();
float inv_std = rsqrtf(shared[0] / dim + eps);
for (int i = threadIdx.x; i < dim2; i += blockDim.x) {
__nv_bfloat162 xv = x2[i], sv = sc2[i], hv = sh2[i];
float n0 = (__bfloat162float(xv.x) - mean) * inv_std;
float n1 = (__bfloat162float(xv.y) - mean) * inv_std;
float v0 = n0 * (1.0f + __bfloat162float(sv.x)) + __bfloat162float(hv.x);
float v1 = n1 * (1.0f + __bfloat162float(sv.y)) + __bfloat162float(hv.y);
out2[i] = __floats2bfloat162_rn(v0, v1);
}
}
void ada_layer_norm_bf16_per_token(
const __nv_bfloat16* x,
const __nv_bfloat16* scale, const __nv_bfloat16* shift,
__nv_bfloat16* out, int seq_len, int dim, float eps,
cudaStream_t stream) {
ada_layer_norm_bf16_per_token_kernel
<<<seq_len, 256, 256 * sizeof(float), stream>>>(
x, scale, shift, out, dim, eps);
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Bias add: x[i] += b[i % D] β bf16
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
__global__ void add_bias_bf16_kernel(__nv_bfloat16* x, const __nv_bfloat16* b,
int S, int D) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < S * D) {
x[i] = __float2bfloat16(__bfloat162float(x[i]) + __bfloat162float(b[i % D]));
}
}
void add_bias_bf16(__nv_bfloat16* x, const __nv_bfloat16* b,
int S, int D, cudaStream_t stream) {
add_bias_bf16_kernel<<<(S * D + 255) / 256, 256, 0, stream>>>(x, b, S, D);
}
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Cast fp16 β bf16 (DiT input/output boundary)
// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
__global__ void cast_fp16_to_bf16_kernel(const __half* in, __nv_bfloat16* out, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) out[i] = __float2bfloat16(__half2float(in[i]));
}
void cast_fp16_to_bf16(const __half* in, __nv_bfloat16* out, int n,
cudaStream_t stream) {
cast_fp16_to_bf16_kernel<<<(n + 255) / 256, 256, 0, stream>>>(in, out, n);
}
__global__ void cast_bf16_to_fp16_kernel(const __nv_bfloat16* in, __half* out, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) out[i] = __float2half(__bfloat162float(in[i]));
}
void cast_bf16_to_fp16(const __nv_bfloat16* in, __half* out, int n,
cudaStream_t stream) {
cast_bf16_to_fp16_kernel<<<(n + 255) / 256, 256, 0, stream>>>(in, out, n);
}
|