okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
2.5 KiB49 linesraw
1// src/prefill/moe_ws.cuh - the weight-stationary expert kernels' shared pieces (see moe_ws.hpp): included after
2// ggml-cuda's common.cuh and mmq.cuh by moe_ws.cu and the moe_ws_*.cu that hold the kernels (a few formats per file,
3// so the instances compile in parallel).
4#pragma once
5
6#include "strata/prefill/moe_ws.hpp"
7
8#include <cuda_runtime.h>
9
10namespace strata::prefill::ws::detail {
11
12// The two ways every kernel multiplies: int8 values sign-extended to int16 pairs (one PRMT per pair: bytes 0,2 and 1,3)
13// and a pair of products added to a 32-bit sum by two XMAD (mad.wide.s16) - MMQ's dp4a on sm_60, one product at a time.
14// A 32-value block's integer sum is exact (|sum| < 2^22 for every format here), so the order of its MACs is free.
15__device__ __forceinline__ void split2(const int v, int& lo02, int& hi13) {
16    asm("prmt.b32 %0, %1, 0, 0xA280;" : "=r"(lo02) : "r"(v));
17    asm("prmt.b32 %0, %1, 0, 0xB391;" : "=r"(hi13) : "r"(v));
18}
19__device__ __forceinline__ int mad2(const int w, const int a, int c) {
20    asm("{\n\t.reg .s16 al, ah, bl, bh;\n\t"
21        "mov.b32 {al, ah}, %1;\n\tmov.b32 {bl, bh}, %2;\n\t"
22        "mad.wide.s16 %0, al, bl, %0;\n\tmad.wide.s16 %0, ah, bh, %0;\n\t}"
23        : "+r"(c) : "r"(w), "r"(a));
24    return c;
25}
26// float(sum) for |sum| < 2^22 without the quarter-rate I2F: a chain seeded with 1.5 * 2^23 holds the float's bits
27constexpr int kMagicI = 0x4B400000;
28constexpr float kMagicF = 12582912.0f;
29
30// CTA b's chunk and first weight row: chunk b / pieces, piece b % pieces (`pieces` = weight rows / rows per CTA) - the
31// chunks' order, each chunk's pieces next to each other
32template <int PIECES, int ROWS>
33__device__ __forceinline__ Chunk cta_work(const Chunk* __restrict__ chunks, int& r0) {
34    r0 = (int) (blockIdx.x % PIECES) * ROWS;
35    const int4 c = __ldg((const int4*) chunks + blockIdx.x / PIECES);
36    return Chunk{c.x, c.y, c.z, c.w};
37}
38
39// one launch of a layer's product, the variant picked by u (rows per routed expert); grid = nchunks x pieces
40void gu_iq2_xxs(const Layer& L, double u, cudaStream_t s);
41void gu_iq2_xs(const Layer& L, double u, cudaStream_t s);
42void gu_iq2_s(const Layer& L, double u, cudaStream_t s);
43void gu_iq3_xxs(const Layer& L, double u, cudaStream_t s);
44void gu_iq3_s(const Layer& L, double u, cudaStream_t s);
45void dn_q2_0(const Layer& L, double u, cudaStream_t s);
46// `fast`: ws::fast() (STRATA_WS_FAST=1) - the 32-token tier's fast kernel, the same Dm rows
47void dn_iq4_nl(const Layer& L, double u, cudaStream_t s, bool fast);
48
49}  // namespace strata::prefill::ws::detail