okami.mom/strata

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

Niko1221Merge f-strix-p1a: P1A prompt experts for Windows gfx1151, on by default there4786a2e7d

main
12.5 KiB207 linesraw
1// src/prefill/moe_fused_iq_q36.cu - Strata's fused int8 prompt-expert kernels (moe_fused_iq.cu) compiled a second time
2// for Qwen3.6-35B-A3B's expert shape (n_embd 2048, n_ff 512), beside the copy for the compiled geometry (Flash-Next:
3// 2560 / 640), so the Qwen3.6 engine (src/qwen35/q36_engine.cu) reads its prompt experts with them instead of llama.cpp's
4// MMQ.  moe_fused_iq.cu is not changed: it is included here with the shape override of moe_fused.hpp and its namespace
5// renamed to strata::prefill::fused_q36, so every function of the original copy keeps its code and every name here is
6// this copy's own.  The API the engine calls is declared in include/strata/prefill/moe_fused_q36.hpp.
7//
8// CUDA, and HIP on gfx12 (the native_w11_kernel launches; the gfx11 F16 kernels of moe_f16_kq.cu are not part of this copy: its f16kq namespace is a stub below).
9
10// the headers the original includes, processed once here before the rename (none declares a name `fused`)
11#include <cuda_fp16.h>
12#include <cuda_runtime.h>
13
14#include <algorithm>
15#include <atomic>
16#include <cstddef>
17#include <cstdint>
18#include <cstdio>
19#include <cstdlib>
20#include <cstring>
21#include <mutex>
22#include <type_traits>
23
24#include "ggml.h"
25#include "strata/core/emulate.hpp"
26#include "strata/core/gfx12_platform_defaults.hpp"
27#include "strata/core/sm120_series.hpp"
28#include "strata/kernels/gfx_arch.hpp"
29#include "strata/prefill/fused_tile_rule.hpp"
30#include "strata/prefill/pf_fused_override.hpp"
31
32#define STRATA_FUSED_GEO_N_EMBD 2048
33#define STRATA_FUSED_GEO_N_FF 512
34#define fused fused_q36
35#include "moe_fused_iq.cu"
36#undef fused
37#undef STRATA_FUSED_GEO_N_EMBD
38#undef STRATA_FUSED_GEO_N_FF
39
40#define STRATA_FUSED_Q36_TU 1
41#include "strata/prefill/moe_fused_q36.hpp"
42
43// the hand-written API header (moe_fused_q36.hpp) and this copy's types agree
44namespace strata::prefill::fused_q36 {
45static_assert(kMoeEmbd == kQ36Embd && kMoeFF == kQ36FF && kMaxBatch == kQ36MaxBatch, "the Qwen3.6 shape");
46static_assert(sizeof(Batch) == 2 * sizeof(int) + kQ36MaxBatch * sizeof(const uint8_t*) && offsetof(Batch, blob) == 2 * sizeof(int),
47              "fused_q36::Batch as moe_fused_q36.hpp declares it");
48static_assert(sizeof(NativeGeom) == 2 * sizeof(int) + 4 * sizeof(size_t) && offsetof(NativeGeom, up_off) == 2 * sizeof(int) + 2 * sizeof(size_t),
49              "fused_q36::NativeGeom as moe_fused_q36.hpp declares it");
50}  // namespace strata::prefill::fused_q36
51
52#if defined(__HIPCC__)
53// The gfx11 F16 K-quant kernels (moe_f16_kq.cu) are the original copy's: this copy's f16kq namespace (declared by moe_f16_kq.hpp inside the rename) has no kernels, so it covers nothing and
54// the int8 kernels run.  They are only reached under `!d.w12 && f16::requested()`, and this copy's layers are taken on gfx12 only (native_layer_ext).
55namespace strata::prefill::fused_q36::f16kq {
56bool covers(int) { return false; }
57bool covers_down(int) { return false; }
58void gate_up(const Batch&, const NativeGeom&, const int32_t*, const int2*, const int32_t*, int64_t, int, int64_t, const uint8_t*, const int32_t*, uint8_t*, cudaStream_t) {}
59void down(const Batch&, const NativeGeom&, const int32_t*, const int2*, const int32_t*, int64_t, int, int64_t, const uint8_t*, float*, cudaStream_t, bool) {}
60}  // namespace strata::prefill::fused_q36::f16kq
61// moe_p1a.cu's kernels likewise: the original copy's only (this copy's p1a_input is never called, so its experts_native never reaches these)
62namespace strata::prefill::fused_q36::p1a {
63bool takes(int, int, bool) { return false; }
64void gate_up(const Batch&, const NativeGeom&, const int32_t*, const int2*, const int32_t*, int64_t, const void*, const int32_t*, void*, cudaStream_t) {}
65void down(const Batch&, const NativeGeom&, const int32_t*, const int2*, const int32_t*, int64_t, const void*, void*, cudaStream_t) {}
66}  // namespace strata::prefill::fused_q36::p1a
67#endif
68
69namespace strata::prefill::fused {
70bool requested();
71}  // namespace strata::prefill::fused
72
73namespace strata::prefill::fused_q36 {
74// the one function of moe_fused.cu (outside the renamed copy) the kernels' rules call: the same answer for both copies
75bool requested() { return strata::prefill::fused::requested(); }
76
77// ---- the K-quant downs of UD-Q4_K_XL (Q5_K on 38 layers, Q6_K on 3), item C --------------------------------------------------------------------------
78// native_kernel<T, false, ...> walks a K-quant down row's 256-value super-blocks (GU256's `D_ROWS_K % 256 == 0` arm: K = 512 here, two blocks a row) and reads
79// the minimum's code sums the Q4_K / Q5_K gate/up epilogue writes into H's spare bytes (has_min); the dispatch is not in the copy's native_supported /
80// experts_native (and moe_fused_iq.cu is not edited for it), so it is here: the pair rule of the K-quant gate/up (native_supported(gu, T_Q5_1): the same
81// prerequisites - STRATA_PF_FUSED, the K-quant instances set up, no NT = 256 / warp-specialized kernels) plus this file's own set-up of the two down instances,
82// then the K-quant launch of experts_native with the new down.  (pick_ww takes the staged / wide arguments as experts_native computes them: both false for the Q4_K / Q5_K gate/up of this path.)
83namespace {
84#if !defined(__HIPCC__)
85struct KqDown {
86    bool ok = false;
87    int occ5 = 1, occ6 = 1;   // blocks per multiprocessor of the Q5_K / Q6_K down instances
88};
89const KqDown& kqdown_info() {
90    static std::mutex mu;
91    static KqDown per[32];
92    static bool done[32] = {};
93    int dev = 0;
94    cudaGetDevice(&dev);
95    std::lock_guard<std::mutex> lk(mu);
96    KqDown& k = per[dev & 31];
97    if (done[dev & 31]) return k;
98    done[dev & 31] = true;
99    const DevInfo& d = dev_info();
100    int o5 = 1 << 20, o6 = 1 << 20;
101    if (d.ok && d.kq && setup_one<T_Q5_K, false>(o5) && setup_one<T_Q6_K, false>(o6)) {
102        k.ok = true;
103        k.occ5 = o5;
104        k.occ6 = o6;
105    }
106    cudaGetLastError();
107    return k;
108}
109#endif
110bool is_kq_down_pair(int gu_type, int d_type) {
111    return (gu_type == T_Q4_K || gu_type == T_Q5_K) && (d_type == T_Q5_K || d_type == T_Q6_K);
112}
113}  // namespace
114
115bool native_supported_ext(int gu_type, int d_type) {
116#if defined(__HIPCC__)
117    // gfx12: native_w11_kernel's K-quant gate/up (native_supported(gu, Q5_1): STRATA_PF_FUSED not 0, STRATA_PF_FUSED_KQ=1 as the engine sets it, the card's kernels set up) and, for the
118    // K-quant downs, native_w11_kernel<Q5_K / Q6_K, false> (KQD256); no per-format set-up (the instances are the copy's own and the same kernel body as the gate/up's)
119    if (is_kq_down_pair(gu_type, d_type)) return native_supported(gu_type, T_Q5_1) && dev_info().w12;
120#else
121    if (is_kq_down_pair(gu_type, d_type)) return native_supported(gu_type, T_Q5_1) && kqdown_info().ok;
122#endif
123    return native_supported(gu_type, d_type);
124}
125
126// Whether the engine takes the fused kernels for a layer of this pair on the current device: STRATA_PF_FUSED=1 on every card whose kernels cover the pair, unset on the cards
127// the Flash-Next engine's default rule takes (fused_tile::q36_takes, which is takes_by_default for the card, the staged gate/up and the staged down), =0 never.
128bool native_layer_ext(int gu_type, int d_type) {
129    static const int env = strata::prefill::fused_tile::pf_env(std::getenv("STRATA_PF_FUSED"));
130    static const int sm40_env = strata::prefill::fused_tile::sm40_env_of(std::getenv("STRATA_PF_FUSED_SM40"));
131    if (env == 0 || !native_supported_ext(gu_type, d_type)) return false;
132    const DevInfo& d = dev_info();
133#if defined(__HIPCC__)
134    // HIP: gfx12 only, and only with STRATA_PF_FUSED=1, which strata-q36 sets itself in a build without MMQ (fused_tile::q36_start_env); a build with MMQ keeps its route unless the user sets it
135    (void) sm40_env;
136    return strata::prefill::fused_tile::q36_takes_gfx12(env, d.w12, true);
137#else
138    const bool down_ok = sw_on(d_type, false) || d_type == T_IQ4_NL || d_type == T_Q2_0;
139    return strata::prefill::fused_tile::q36_takes(env, {d.s120, d.s121, d.sms, d.s86}, sm40_env, sw_on(gu_type, true), down_ok, true);
140#endif
141}
142
143#if defined(__HIPCC__)
144// gfx12: the K-quant gate/up (native_w11_kernel<Q4_K / Q5_K, true>, experts_native's own launch) and the K-quant down on the same kernel (<Q5_K / Q6_K, false>: KQD256, two super-blocks a row at
145// K = 512), the plain launch (no occupancy variant: the K-quant kernels spill under the cap).  The H the first writes, with the code sums the minimum terms read, is the second's input.
146void experts_native_ext(const Batch& b, const NativeGeom& g, int n_expert, int64_t n, const void* scratch, const void* xa, const int32_t* src, void* ha,
147                        float* dm, void* stream) {
148    if (!is_kq_down_pair(g.gu_type, g.d_type)) {
149        experts_native(b, g, n_expert, n, scratch, xa, src, ha, dm, stream);
150        return;
151    }
152    g_dm16_used = false;
153    if (b.e1 <= b.e0 || n <= 0) return;
154    const DevInfo& d = dev_info();
155    if (!d.ok || !d.w12 || !gu_covered(g.gu_type)) {
156        std::fprintf(stderr, "prefill fused experts (Qwen3.6): types %d / %d are not covered here\n", g.gu_type, g.d_type);
157        std::exit(1);
158    }
159    if ((reinterpret_cast<uintptr_t>(dm) & 15) != 0) {   // gfx12's down store: two float4 per lane
160        std::fprintf(stderr, "prefill fused experts (Qwen3.6): the down rows (%p) must be 16-byte aligned\n", (void*) dm);
161        std::exit(1);
162    }
163    const cudaStream_t s = (cudaStream_t) stream;
164    const Tables tb = tables(const_cast<void*>(scratch), n_expert);
165    const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0);
166    const unsigned g_gu = (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / NW_ROWS), (int64_t) d.sms * wgp_blocks(d.occ));
167    const unsigned g_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / NW_ROWS), (int64_t) d.sms * wgp_blocks(d.occ));
168    const uint8_t* xa8 = (const uint8_t*) xa;
169    uint8_t* ha8 = (uint8_t*) ha;
170    if (g.gu_type == T_Q4_K) native_w11_kernel<T_Q4_K, true><<<g_gu, NW_THREADS, 0, s>>>(b, g, tb, xa8, src, ha8, nullptr);
171    else native_w11_kernel<T_Q5_K, true><<<g_gu, NW_THREADS, 0, s>>>(b, g, tb, xa8, src, ha8, nullptr);
172    if (g.d_type == T_Q5_K) native_w11_kernel<T_Q5_K, false><<<g_d, NW_THREADS, 0, s>>>(b, g, tb, ha8, src, nullptr, dm);
173    else native_w11_kernel<T_Q6_K, false><<<g_d, NW_THREADS, 0, s>>>(b, g, tb, ha8, src, nullptr, dm);
174    ck(cudaGetLastError(), "experts_native_ext");
175}
176#else
177void experts_native_ext(const Batch& b, const NativeGeom& g, int n_expert, int64_t n, const void* scratch, const void* xa, const int32_t* src, void* ha,
178                        float* dm, void* stream) {
179    if (!is_kq_down_pair(g.gu_type, g.d_type)) {
180        experts_native(b, g, n_expert, n, scratch, xa, src, ha, dm, stream);
181        return;
182    }
183    if (b.e1 <= b.e0 || n <= 0) return;
184    const DevInfo& d = dev_info();
185    const KqDown& kd = kqdown_info();
186    if (!d.ok || !d.kq || !kd.ok) {
187        std::fprintf(stderr, "prefill fused experts (Qwen3.6): types %d / %d are not covered here\n", g.gu_type, g.d_type);
188        std::exit(1);
189    }
190    const cudaStream_t s = (cudaStream_t) stream;
191    const Tables tb = tables(const_cast<void*>(scratch), n_expert);
192    // staged: the launch's gate/up or down kernel is a weight-staging instance (the K-quant downs are, K = 512); the staged instances are the 128-row tile's.  launch_ww (as in
193    // experts_native) runs the staging check for such a launch: one whose expert blobs are not device memory at 2-byte aligned addresses takes the unstaged 64-row kernel of the
194    // same formats (same output bits), sticky for the process, one log line, no exit.  The wide argument is fused_tile::wide_pair: true for this path's K-quant pairs, so sm_86 cards with 40+ SMs take the 128-row tile above 112 rows an expert (fused_tile_rule.hpp; the 80+ SM ones used to take 64 there).
195    const bool staged = sw_on(g.gu_type, true) || sw_on(g.d_type, false);
196    const int ww = launch_ww(b, g, n, n_expert, staged, strata::prefill::fused_tile::wide_pair(g.gu_type, g.d_type));
197    const int64_t tiles = (n + kTileRows - 1) / kTileRows + (b.e1 - b.e0);
198    const unsigned k_gu = (unsigned) std::min<int64_t>(tiles * (GU_ROWS_N / weight_rows(ww)), (int64_t) d.sms * d.occ_kq);
199    const unsigned k_d = (unsigned) std::min<int64_t>(tiles * (D_ROWS_N / weight_rows(ww)), (int64_t) d.sms * (g.d_type == T_Q5_K ? kd.occ5 : kd.occ6));
200    if (g.gu_type == T_Q4_K) launch<T_Q4_K, true>(ww, k_gu, b, g, tb, xa, src, ha, nullptr, s);
201    else launch<T_Q5_K, true>(ww, k_gu, b, g, tb, xa, src, ha, nullptr, s);
202    if (g.d_type == T_Q5_K) launch<T_Q5_K, false>(ww, k_d, b, g, tb, ha, src, nullptr, dm, s);
203    else launch<T_Q6_K, false>(ww, k_d, b, g, tb, ha, src, nullptr, dm, s);
204    ck(cudaGetLastError(), "experts_native_ext");
205}
206#endif
207}  // namespace strata::prefill::fused_q36