okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/kernels/cuda/elementwise.cu - P2.S5's glue kernels. See the header for why each exists. 2#include "strata/kernels/elementwise.hpp" 3#include "strata/kernels/dp4a.hpp" 4 5#include "strata/kernels/bf16_bits.hpp" 6#include "strata/kernels/f16_bits.hpp" 7 8#include <cuda_runtime.h> 9 10#include <cmath> 11#include <cstdio> 12#include <cstdlib> 13 14namespace strata::kernels { 15namespace { 16 17constexpr int THREADS = 256; 18 19__global__ void embedding_gather_kernel(const uint8_t* __restrict__ codes, 20 const float* __restrict__ scales, 21 const float* __restrict__ offsets, int64_t n, 22 int code_bits, int code_bias, int group_elems, 23 float* __restrict__ out) { 24 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 25 if (i >= n) return; 26 const int per_byte = 8 / code_bits; 27 const unsigned mask = (1u << code_bits) - 1u; 28 const int code = (codes[i / per_byte] >> ((i % per_byte) * code_bits)) & mask; 29 const int64_t group = i / group_elems; 30 const float product = __fmul_rn((float) (code + code_bias), scales[group]); 31 out[i] = __fadd_rn(product, offsets ? offsets[group] : 0.0f); 32} 33 34/// `ggml_compute_softplus_f32`: `log1p(exp(x))`, with the large-x branch that avoids overflow. 35/// 36/// The branch is not cosmetic. `exp(89)` overflows f32 and `exp(20)` is already 4.85e8 where `log1p` loses 37/// relative precision; above 20 the function is `x` to within f32 anyway. 38__device__ __forceinline__ float softplus_dev(float x) { return x > 20.0f ? x : log1pf(expf(x)); } 39 40__global__ void gdn_gate_kernel(const float* __restrict__ alpha, const float* __restrict__ dt, 41 const float* __restrict__ ssm_a, float* __restrict__ gate, int64_t h_v) { 42 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 43 if (i >= h_v) return; 44 const int64_t t = i / h_v; // `n_tokens` is the leading dim; the real call has one token 45 const int64_t h = i % h_v; 46 gate[i] = softplus_dev(alpha[i] + dt[h]) * ssm_a[h]; 47 (void) t; 48} 49 50__global__ void scale_kernel(float* __restrict__ x, int64_t n, float s) { 51 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 52 if (i < n) x[i] *= s; 53} 54 55// `dst[i] += src[i]`. See the header: this is what lets R4's GPU half and CPU half run at the same 56// time and still add up to one `parts` buffer. 57__global__ void add_kernel(float* __restrict__ dst, const float* __restrict__ src, long long n) { 58 const long long i = (long long) blockIdx.x * blockDim.x + threadIdx.x; 59 if (i < n) dst[i] += src[i]; 60} 61 62__global__ void to_f16_kernel(const float* __restrict__ x, uint16_t* __restrict__ y, int64_t n) { 63 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 64 if (i < n) y[i] = f16_from_f32(x[i]); 65} 66 67__global__ void to_bf16_kernel(const float* __restrict__ x, uint16_t* __restrict__ y, int64_t n) { 68 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 69 if (i < n) y[i] = bf16_from_f32(x[i]); 70} 71 72__global__ void silu_kernel(float* __restrict__ x, int64_t n) { 73 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 74 if (i >= n) return; 75 // DOUBLE then cast, matching `ref/gdn.py`'s numpy: its arrays are f32 but `np.exp` on an f32 array is 76 // computed to f32 precision by a different algorithm than `expf`, and the reference is the oracle. The 77 // difference is in the last bits and this is one line. 78 const double v = (double) x[i]; 79 x[i] = (float) (v / (1.0 + exp(-v))); 80} 81 82/// A bounded launch: a zero-length grid is illegal, and `n == 0` is a real call (an empty selection). 83inline unsigned grid_for(int64_t n) { return (unsigned) ((n + THREADS - 1) / THREADS); } 84 85/// One WARP per row, reduced through shuffles. The rows here are short and few - (24, 256), (2, 256), 86/// (4, 128) - so a block-per-row tree would spend its time in `__syncthreads` for 8 warps of 32, and the whole 87/// call is 30 rows. A warp reduction with NO shared memory and NO barrier is the shape that fits. 88__global__ void rms_norm_weighted_kernel(float* __restrict__ x, const float* __restrict__ w, int64_t rows, 89 int64_t cols, float eps) { 90 91 const int lane = threadIdx.x & 31; 92 const int64_t row = ((int64_t) blockIdx.x * (blockDim.x >> 5)) + (threadIdx.x >> 5); 93 // **THE ROW GUARD IS NOT DECORATION, AND ITS ABSENCE WAS THE QSA BUG.** The launcher rounds the grid up to 94 // whole 4-warp blocks, so a call with `rows` = 2 - the QSA k-norm, the only non-multiple-of-4 row count in 95 // the engine - runs EIGHT warps: rows 0 and 1 are the data, and rows 2 and 3 write 2*cols floats PAST the 96 // end of `b.kcur`, which in the arena is exactly where `b.vcur` begins. `b.vcur` was therefore silently 97 // replaced by two rows of `rms_norm(...) * attn_k_norm`: a normalized vector with l2 ~20.6 against V's 98 // ~5.7, which the attention then attended to. `rope_neox_kernel` has had this guard all along. 99 if (row >= rows) return; 100 float* r = x + row * cols; 101 float acc = 0.0f; 102 for (int64_t c = lane; c < cols; c += 32) acc += r[c] * r[c]; 103 for (int off = 16; off > 0; off >>= 1) acc += __shfl_down_sync(0xFFFFFFFFu, acc, off); 104 // The MEAN, not the sum: `ref/qsa.py::rms_norm` divides by `np.mean(np.square(x))`. Broadcasting the 105 // reciprocal from lane 0 keeps all 32 lanes on the same value - computing `rsqrt` per lane would be the 106 // same number but a needless 32-way divergence in the last bit. 107 float inv = 0.0f; 108 if (lane == 0) inv = rsqrtf(acc / (float) cols + eps); 109 inv = __shfl_sync(0xFFFFFFFFu, inv, 0); 110 for (int64_t c = lane; c < cols; c += 32) r[c] = (w ? r[c] * w[c] : r[c]) * inv; 111} 112 113void sync_if_needed(void* stream, const char* what) { 114 if (stream != nullptr) return; 115 const cudaError_t e = cudaDeviceSynchronize(); 116 if (e != cudaSuccess) { 117 std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e)); 118 std::exit(1); 119 } 120} 121 122bool check_launch(const char* what) { 123 const cudaError_t e = cudaGetLastError(); 124 if (e != cudaSuccess) { 125 std::fprintf(stderr, "%s launch: %s\n", what, cudaGetErrorString(e)); 126 std::exit(1); 127 } 128 return true; 129} 130 131} // namespace 132 133void embedding_gather(const uint8_t* codes, const float* scales, const float* offsets, 134 int64_t n, int code_bits, int code_bias, int group_elems, 135 float* out, void* stream) { 136 if (n <= 0) return; 137 embedding_gather_kernel<<<grid_for(n), THREADS, 0, (cudaStream_t) stream>>>( 138 codes, scales, offsets, n, code_bits, code_bias, group_elems, out); 139 check_launch("embedding_gather"); 140} 141 142void gdn_gate(const float* alpha, const float* dt, const float* ssm_a, float* gate, int64_t n_tokens, 143 int64_t h_v, void* stream) { 144 if (n_tokens <= 0 || h_v <= 0) return; 145 const int64_t n = n_tokens * h_v; 146 gdn_gate_kernel<<<grid_for(n), THREADS, 0, (cudaStream_t) stream>>>(alpha, dt, ssm_a, gate, h_v); 147 check_launch("gdn_gate"); 148 sync_if_needed(stream, "gdn_gate"); 149} 150 151void scale_inplace(float* x, int64_t n, float s, void* stream) { 152 if (n <= 0) return; 153 scale_kernel<<<grid_for(n), THREADS, 0, (cudaStream_t) stream>>>(x, n, s); 154 check_launch("scale_inplace"); 155 sync_if_needed(stream, "scale_inplace"); 156} 157 158void add_inplace(float* dst, const float* src, int64_t n, void* stream) { 159 if (n <= 0) return; 160 add_kernel<<<grid_for(n), THREADS, 0, (cudaStream_t) stream>>>(dst, src, n); 161 check_launch("add_inplace"); 162 sync_if_needed(stream, "add_inplace"); 163} 164 165void f32_to_f16_bulk(const float* x, uint16_t* y, int64_t n, void* stream) { 166 if (n <= 0) return; 167 to_f16_kernel<<<grid_for(n), THREADS, 0, (cudaStream_t) stream>>>(x, y, n); 168 check_launch("f32_to_f16_bulk"); 169 sync_if_needed(stream, "f32_to_f16_bulk"); 170} 171 172void f32_to_bf16_bulk(const float* x, uint16_t* y, int64_t n, void* stream) { 173 if (n <= 0) return; 174 to_bf16_kernel<<<grid_for(n), THREADS, 0, (cudaStream_t) stream>>>(x, y, n); 175 check_launch("f32_to_bf16_bulk"); 176 sync_if_needed(stream, "f32_to_bf16_bulk"); 177} 178 179void silu_inplace(float* x, int64_t n, void* stream) { 180 if (n <= 0) return; 181 silu_kernel<<<grid_for(n), THREADS, 0, (cudaStream_t) stream>>>(x, n); 182 check_launch("silu_inplace"); 183 sync_if_needed(stream, "silu_inplace"); 184} 185 186/// THE DOORBELL. One thread, one INCREMENT - the cost is the launch, and inside a graph that is paid once. 187/// 188/// **IT INCREMENTS THE MEMORY, AND IT DOES NOT TAKE THE VALUE AS AN ARGUMENT.** The first version did - 189/// doorbell_ring(d_seq, *(h_seq) + 1u) - and that is a CLONED LITERAL: the expression is evaluated on the HOST 190/// at CAPTURE time, so the graph rings 1 on every replay and a host waiting for a change waits forever. It 191/// would have looked like a working doorbell on the first token, which is the worst way for it to be wrong. 192/// **THE FENCE IS NOT DECORATION, AND ITS ABSENCE WAS COSTING ~10 ms PER TOKEN ON THE HOST SIDE.** 193/// 194/// The ring PUBLISHES three buffers the host is about to read - `h_x_f`, `h_ids`, `h_weights` - so the increment 195/// must be ordered after their writes, or the host can observe the ring and then read a payload that has not 196/// landed. `__threadfence_system()` is what orders them, and it covers the HOST as well as the device, which is 197/// the whole point: the reader is the CPU. 198/// 199/// Without it the host loop was compensating with a driver call. `session_loop` polled with `cudaEventQuery` 200/// on EVERY spin iteration, because round 195 had measured that a memory-only spin never saw the datum flip - 201/// and that measurement was right about the symptom and wrong about the cause. The cause is this missing fence: 202/// the write was not ordered into host-visible memory, so no amount of reading it would show it, and the driver 203/// call was flushing the whole pipeline enough to make it appear. A 10-22 us driver call per iteration is a 204/// very expensive substitute for one fence instruction. 205/// 206/// **THE STORE IS VOLATILE**, like `doorbell_publish_kernel`'s. On RDNA4 (gfx1201) a plain store to mapped pinned 207/// memory stays in the GPU's L2 until the stream is synchronized - the host never saw the ring (tests/hip/handoff: 208/// 0 of 100 rings seen without a sync; volatile, a system-scope atomic store or a fence after the store: 100 of 100). 209__global__ void doorbell_ring_kernel(uint32_t* seq) { 210 __threadfence_system(); 211 *(volatile uint32_t*) seq = *(volatile uint32_t*) seq + 1u; 212#if defined(__HIPCC__) // #697: HIP only; on RDNA4 the volatile store alone can sit in L2 until the stream syncs 213 __threadfence_system(); 214#endif 215} 216 217__global__ void doorbell_wait_kernel(const volatile uint32_t* flag, const volatile uint32_t* seq) { 218 const uint32_t want = *seq; 219 while (*flag != want) strata_spin_pause(); 220 __threadfence_system(); 221} 222 223void doorbell_wait(const uint32_t* d_flag, const uint32_t* d_seq, void* stream) { 224 if (d_flag == nullptr || d_seq == nullptr) return; 225 doorbell_wait_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(d_flag, d_seq); 226 check_launch("doorbell_wait"); 227} 228 229__global__ void copy_from_mapped_kernel(float4* __restrict__ dst, const volatile float4* src, int64_t n4) { 230 for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < n4; i += (int64_t) gridDim.x * blockDim.x) { 231 const float4 v = const_cast<const float4*>(src)[i]; 232 dst[i] = v; 233 } 234} 235 236// the CPU rows of a verify window, skipping the rows the GPU plan computes itself (the 237// pool writes +0.0 into those, so this writes +0.0 too): block = row, the plan's hit rows `dst[0, *count)`. 238__global__ void copy_rows_from_mapped_kernel(float4* __restrict__ dst, const volatile float4* src, int64_t row4, 239 const int32_t* __restrict__ hit_rows, const int32_t* __restrict__ count, 240 const volatile uint32_t* wait_flag = nullptr, uint32_t wait_value = 0) { 241 const int row = blockIdx.x; 242 __shared__ int hit; 243 if (threadIdx.x == 0) { 244 if (wait_flag != nullptr) { // 143-amdk: the wait_flag_ge launch folded in 245 while (*wait_flag < wait_value) strata_spin_pause(); 246 __threadfence_system(); 247 } 248 int h = 0; 249 const int c = *count; 250 for (int i = 0; i < c; ++i) h |= hit_rows[i] == row; 251 hit = h; 252 } 253 __syncthreads(); 254 float4* d = dst + (int64_t) row * row4; 255 if (hit) { 256 for (int64_t i = threadIdx.x; i < row4; i += blockDim.x) d[i] = make_float4(0.0f, 0.0f, 0.0f, 0.0f); 257 } else { 258 const volatile float4* sr = src + (int64_t) row * row4; 259 for (int64_t i = threadIdx.x; i < row4; i += blockDim.x) d[i] = const_cast<const float4*>(sr)[i]; 260 } 261} 262namespace { 263__global__ void scatter_rows_kernel(const float4* __restrict__ src, float4* dst, const int32_t* __restrict__ rows, 264 int64_t w4) { 265 const int64_t r = blockIdx.x; 266 const float4* s = src + r * w4; 267 float4* d = dst + (int64_t) rows[r] * w4; 268 for (int64_t i = threadIdx.x; i < w4; i += blockDim.x) d[i] = s[i]; 269} 270} // namespace 271void scatter_rows_f32(const float* src, float* dst, const int32_t* rows, int64_t n, int64_t width, void* stream) { 272 if (n <= 0) return; 273 if ((width & 3) != 0 || ((uintptr_t) dst & 15) != 0 || ((uintptr_t) src & 15) != 0) { 274 std::fprintf(stderr, "scatter_rows_f32: width must be a multiple of 4 and both pointers 16-byte aligned\n"); 275 std::exit(1); 276 } 277 scatter_rows_kernel<<<(unsigned) n, 128, 0, (cudaStream_t) stream>>>((const float4*) src, (float4*) dst, rows, width / 4); 278} 279namespace { 280// knife-0: a plain byte copy of mapped host memory into device memory, as a KERNEL. The host-issued 281// cudaMemcpyAsync inside a verify window (the GPU spinning on the flag it raises) is the path issue #31 shows 282// stuck on a driver lock (generate.cpp:10329); a kernel moves the same bytes with no host CUDA call in the window. 283__global__ void copy_bytes16_from_mapped_kernel(uint4* __restrict__ dst, const volatile uint4* src, int64_t n4) { 284 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 285 if (i < n4) { 286 const uint4 v = const_cast<const uint4*>(src)[i]; 287 dst[i] = v; 288 } 289} 290__global__ void copy_bytes_from_mapped_kernel(uint8_t* __restrict__ dst, const volatile uint8_t* src, int64_t n) { 291 const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 292 if (i < n) dst[i] = const_cast<const uint8_t*>(src)[i]; 293} 294} // namespace 295void copy_bytes_from_mapped(void* dst, const void* src, int64_t bytes, void* stream) { 296 if (bytes <= 0) return; 297 const cudaStream_t s = (cudaStream_t) stream; 298 if ((bytes & 15) == 0 && ((uintptr_t) dst & 15) == 0 && ((uintptr_t) src & 15) == 0) { 299 const int64_t n4 = bytes / 16; 300 copy_bytes16_from_mapped_kernel<<<(unsigned) ((n4 + 255) / 256), 256, 0, s>>>( 301 (uint4*) dst, (const volatile uint4*) src, n4); 302 } else { 303 copy_bytes_from_mapped_kernel<<<(unsigned) ((bytes + 255) / 256), 256, 0, s>>>( 304 (uint8_t*) dst, (const volatile uint8_t*) src, bytes); 305 } 306 check_launch("copy_bytes_from_mapped"); 307} 308void copy_rows_from_mapped(float* dst, const float* src, int64_t rows, int64_t width, const int32_t* hit_rows, 309 const int32_t* count, void* stream) { 310 if (rows <= 0) return; 311 if ((width & 3) != 0 || ((uintptr_t) dst & 15) != 0 || ((uintptr_t) src & 15) != 0) { 312 std::fprintf(stderr, "copy_rows_from_mapped: width must be a multiple of 4 and both pointers 16-byte aligned\n"); 313 std::exit(1); 314 } 315 copy_rows_from_mapped_kernel<<<(unsigned) rows, 128, 0, (cudaStream_t) stream>>>((float4*) dst, (const volatile float4*) src, 316 width / 4, hit_rows, count); 317} 318void copy_rows_from_mapped_after(const uint32_t* flag, uint32_t value, float* dst, const float* src, int64_t rows, 319 int64_t width, const int32_t* hit_rows, const int32_t* count, void* stream) { 320 if (rows <= 0) return; 321 if ((width & 3) != 0 || ((uintptr_t) dst & 15) != 0 || ((uintptr_t) src & 15) != 0) { 322 std::fprintf(stderr, "copy_rows_from_mapped_after: width must be a multiple of 4 and both pointers 16-byte aligned\n"); 323 std::exit(1); 324 } 325 copy_rows_from_mapped_kernel<<<(unsigned) rows, 128, 0, (cudaStream_t) stream>>>( 326 (float4*) dst, (const volatile float4*) src, width / 4, hit_rows, count, (const volatile uint32_t*) flag, value); 327} 328void copy_from_mapped(float* dst, const float* src, int64_t n, void* stream) { 329 if (n <= 0) return; 330 if ((n & 3) != 0 || ((uintptr_t) dst & 15) != 0 || ((uintptr_t) src & 15) != 0) { 331 std::fprintf(stderr, "copy_from_mapped: n must be a multiple of 4 and both pointers 16-byte aligned\n"); 332 std::exit(1); 333 } 334 const int64_t n4 = n / 4; 335 const int blocks = (int) ((n4 + 255) / 256 < 64 ? (n4 + 255) / 256 : 64); 336 copy_from_mapped_kernel<<<blocks, 256, 0, (cudaStream_t) stream>>>((float4*) dst, (const volatile float4*) src, n4); 337 check_launch("copy_from_mapped"); 338} 339 340__global__ void doorbell_publish_kernel(const float* __restrict__ x, const int32_t* __restrict__ ids, 341 const float* __restrict__ w, int n, int k, float* x_out, int32_t* ids_out, 342 float* w_out, uint32_t* seq) { 343 for (int i = threadIdx.x; i < n; i += blockDim.x) x_out[i] = x[i]; 344 if ((int) threadIdx.x < k) { 345 ids_out[threadIdx.x] = ids[threadIdx.x]; 346 if (w_out != nullptr) w_out[threadIdx.x] = w[threadIdx.x]; 347 } 348 __threadfence_system(); 349 __syncthreads(); 350 if (threadIdx.x == 0) { 351 *(volatile uint32_t*) seq = *(volatile uint32_t*) seq + 1u; 352#if defined(__HIPCC__) // #697: HIP only; on RDNA4 the volatile store alone can sit in L2 until the stream syncs 353 __threadfence_system(); 354#endif 355 } 356} 357 358__global__ void doorbell_publish_res_kernel(const float* __restrict__ x, const int32_t* __restrict__ ids, 359 const int32_t* __restrict__ d_res, int n_expert, int n, int k, 360 float* x_out, int32_t* ids_out, uint32_t* seq) { 361 int any_miss = 0; 362 if ((int) threadIdx.x < k) { 363 const int32_t id = ids[threadIdx.x]; 364 ids_out[threadIdx.x] = id; 365 if (d_res == nullptr || id < 0 || id >= n_expert || d_res[id] < 0) any_miss = 1; 366 } 367 if (__syncthreads_or(any_miss)) { 368 for (int i = threadIdx.x; i < n; i += blockDim.x) x_out[i] = x[i]; 369 __threadfence_system(); 370 } else if ((int) threadIdx.x < k) { 371 __threadfence_system(); 372 } 373 __syncthreads(); 374 if (threadIdx.x == 0) { 375 *(volatile uint32_t*) seq = *(volatile uint32_t*) seq + 1u; 376#if defined(__HIPCC__) // #697: HIP only; on RDNA4 the volatile store alone can sit in L2 until the stream syncs 377 __threadfence_system(); 378#endif 379 } 380} 381 382// #649 (HIP, STRATA_DOORBELL_STORE=1): the same publish, but the ring is STORED (the step's own number, known at 383// capture) instead of read-modify-written over PCIe - one store, no read of host memory from the GPU. 384__global__ void doorbell_publish_value_kernel(const float* __restrict__ x, const int32_t* __restrict__ ids, 385 const float* __restrict__ w, int n, int k, float* x_out, int32_t* ids_out, 386 float* w_out, uint32_t* seq, uint32_t value) { 387 for (int i = threadIdx.x; i < n; i += blockDim.x) x_out[i] = x[i]; 388 if ((int) threadIdx.x < k) { ids_out[threadIdx.x] = ids[threadIdx.x]; w_out[threadIdx.x] = w[threadIdx.x]; } 389 __threadfence_system(); 390 __syncthreads(); 391 if (threadIdx.x == 0) { 392 __threadfence_system(); 393 *(volatile uint32_t*) seq = value; 394 __threadfence_system(); 395 } 396} 397 398void doorbell_publish_value(const float* x, const int32_t* ids, const float* weights, int64_t n, int64_t k, 399 float* x_out, int32_t* ids_out, float* weights_out, uint32_t* d_seq, uint32_t value, 400 void* stream) { 401 if (k > 1024) { std::fprintf(stderr, "doorbell_publish: k too large\n"); std::exit(1); } 402 doorbell_publish_value_kernel<<<1, 1024, 0, (cudaStream_t) stream>>>(x, ids, weights, (int) n, (int) k, x_out, 403 ids_out, weights_out, d_seq, value); 404} 405 406void doorbell_publish(const float* x, const int32_t* ids, const float* weights, int64_t n, int64_t k, float* x_out, 407 int32_t* ids_out, float* weights_out, uint32_t* d_seq, void* stream) { 408 if (k > 1024) { std::fprintf(stderr, "doorbell_publish: k too large\n"); std::exit(1); } 409 doorbell_publish_kernel<<<1, 1024, 0, (cudaStream_t) stream>>>(x, ids, weights, (int) n, (int) k, x_out, ids_out, 410 weights_out, d_seq); 411 check_launch("doorbell_publish"); 412} 413 414void doorbell_publish_res(const float* x, const int32_t* ids, const int32_t* d_res, int n_expert, int64_t n, int64_t k, 415 float* x_out, int32_t* ids_out, uint32_t* d_seq, void* stream) { 416 if (k > 1024) { std::fprintf(stderr, "doorbell_publish_res: k too large\n"); std::exit(1); } 417 doorbell_publish_res_kernel<<<1, 1024, 0, (cudaStream_t) stream>>>(x, ids, d_res, n_expert, (int) n, (int) k, 418 x_out, ids_out, d_seq); 419 check_launch("doorbell_publish_res"); 420} 421 422__global__ void copy_i32_from_mapped_kernel(int32_t* __restrict__ dst, const volatile int32_t* src, int64_t n) { 423 for (int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; i < n; 424 i += (int64_t) gridDim.x * blockDim.x) 425 dst[i] = src[i]; 426} 427 428struct MappedCopies { 429 MappedCopy c[8]; 430}; 431 432__global__ void copy_from_mapped_multi_kernel(MappedCopies a) { 433 const MappedCopy c = a.c[blockIdx.y]; 434 const int64_t i0 = (int64_t) blockIdx.x * blockDim.x + threadIdx.x, st = (int64_t) gridDim.x * blockDim.x; 435 if ((c.words & 3) == 0 && ((uintptr_t) c.dst & 15) == 0 && ((uintptr_t) c.src & 15) == 0) { 436 const volatile float4* s = (const volatile float4*) c.src; 437 for (int64_t i = i0; i < c.words / 4; i += st) ((float4*) c.dst)[i] = const_cast<const float4*>(s)[i]; 438 } else { 439 const volatile int32_t* s = (const volatile int32_t*) c.src; 440 for (int64_t i = i0; i < c.words; i += st) ((int32_t*) c.dst)[i] = s[i]; 441 } 442} 443 444void copy_i32_from_mapped(int32_t* dst, const int32_t* src, int64_t n, void* stream) { 445 if (n <= 0) return; 446 const unsigned bx = (unsigned) ((n + 127) / 128 < 64 ? (n + 127) / 128 : 64); 447 copy_i32_from_mapped_kernel<<<bx < 1 ? 1 : bx, 128, 0, (cudaStream_t) stream>>>( 448 dst, (const volatile int32_t*) src, n); 449 check_launch("copy_i32_from_mapped"); 450} 451 452void copy_from_mapped_multi(const MappedCopy* copies, int n, void* stream) { 453 if (n <= 0) return; 454 if (n > 8) { std::fprintf(stderr, "copy_from_mapped_multi: at most 8 copies\n"); std::exit(1); } 455 const char* e = std::getenv("STRATA_MTP_MULTI_COPY"); 456 const bool multi_on = e == nullptr || e[0] != '0'; 457 if (!multi_on) { 458 for (int i = 0; i < n; ++i) { 459 const MappedCopy& c = copies[i]; 460 if (c.words > 1024 && (c.words & 3) == 0 && ((uintptr_t) c.dst & 15) == 0 && ((uintptr_t) c.src & 15) == 0) 461 copy_from_mapped((float*) c.dst, (const float*) c.src, c.words, stream); 462 else 463 copy_i32_from_mapped((int32_t*) c.dst, (const int32_t*) c.src, c.words, stream); 464 } 465 return; 466 } 467 MappedCopies a{}; 468 int64_t most = 0; 469 for (int i = 0; i < n; ++i) { 470 a.c[i] = copies[i]; 471 most = copies[i].words > most ? copies[i].words : most; 472 } 473 const int64_t units = (most + 3) / 4; 474 const unsigned bx = (unsigned) ((units + 255) / 256 < 32 ? (units + 255) / 256 : 32); 475 copy_from_mapped_multi_kernel<<<dim3(bx < 1 ? 1 : bx, (unsigned) n), 256, 0, (cudaStream_t) stream>>>(a); 476 check_launch("copy_from_mapped_multi"); 477} 478 479void doorbell_ring(uint32_t* d_seq, void* stream) { 480 if (d_seq == nullptr) return; 481 doorbell_ring_kernel<<<1, 1, 0, (cudaStream_t) stream>>>(d_seq); 482 check_launch("doorbell_ring"); 483 sync_if_needed(stream, "doorbell_ring"); 484} 485 486void rms_norm_weighted(float* x, const float* w, int64_t rows, int64_t cols, float eps, void* stream) { 487 if (rows <= 0 || cols <= 0) return; 488 // 4 warps per block, so a row count that is not a multiple of 4 wastes at most 3 warps rather than 489 // launching a block per row for a 2-row call. 490 const unsigned warps_per_block = 4; 491 const unsigned grid = (unsigned) ((rows + warps_per_block - 1) / warps_per_block); 492 rms_norm_weighted_kernel<<<grid, warps_per_block * 32, 0, (cudaStream_t) stream>>>(x, w, rows, cols, eps); 493 check_launch("rms_norm_weighted"); 494 sync_if_needed(stream, "rms_norm_weighted"); 495} 496 497} // namespace strata::kernels