okami.mom/strata

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

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
7.7 KiB137 linesraw
1// Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17).
2// Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file.
3// src/ds4/ds4_iq.cuh - a GGUF's routed experts in ggml's low-bit block types on the device for maya-ds4: the i-quants
4// IQ2_XXS, IQ2_S, IQ3_XXS, IQ3_S, IQ4_XS and the K-quants Q2_K, Q3_K, Q4_K (256 values a block, ggml's layouts:
5// ggml-common.h, llama.cpp MIT).  ds4_gguf.cu sends an expert layer here when its experts are not MXFP4
6// (x3::ExpertLayout::fmt kFmtMXFP4 with K[r] = role r's ggml type; K[r] == kTypeMXFP4: ds4_gguf.cu's own kernels).
7//
8// Every weight is decoded exactly (ggml's dequantize_row_*: a block scale times an integer scale times a grid or code
9// value) and every product takes the activations rounded to int8 per 32 values with an F32 scale (ds4_gguf.cu's
10// q8_quant): a 32-value sub-block's sum is an exact integer dot (dp4a) times its scales in F32 - no integer division as
11// llama.cpp's vecdotq takes (its sums truncate), so these are the double reference's sums to F32 rounding.  A window's
12// entry gets the token path's bits (the same kernel, the same order).
13#pragma once
14
15#include <cstddef>
16#include <cstdint>
17#include <cuda_runtime.h>
18
19namespace strata::ds4::gguf::iq {
20
21// ggml type ids
22constexpr int kQ5_K = 13, kQ6_K = 14;   // (the dense matrices' K-quants, GEMV only)
23constexpr int kQ2_K = 10, kQ3_K = 11, kQ4_K = 12, kIQ2_XXS = 16, kIQ2_XS = 17, kIQ3_XXS = 18, kIQ1_S = 19,
24              kIQ4_NL = 20, kIQ3_S = 21, kIQ2_S = 22, kIQ4_XS = 23, kIQ1_M = 29;
25
26/// The trellis types (Strata's, ds4_tq.cu): ggml's TQK6 / TQK7 (145 / 146, the table codebook) and the experimental
27/// TQK5X / TQK4X (224 / 225, computed codebook) and TQK5T / TQK4T (226 / 227, table): a block of 128 values is
28/// {fp16 d; 32 steps of K bits}, 2 + 4 K bytes (tools/trellis/tqenc.hpp).  They go through the same entry points below.
29constexpr int kTQK6 = 145, kTQK7 = 146, kTQK5X = 224, kTQK4X = 225, kTQK5T = 226, kTQK4T = 227;
30constexpr int tq_kbits(int type) {
31    return type == kTQK6 ? 6 : type == kTQK7 ? 7 : (type == kTQK5X || type == kTQK5T) ? 5 : (type == kTQK4X || type == kTQK4T) ? 4 : 0;
32}
33constexpr bool is_tq(int type) { return tq_kbits(type) > 0; }
34
35/// Whether the device kernels take this ggml type.
36bool supported(int type);
37/// Whether the type's block layout is known (its row bytes can be computed).  It covered more than supported() while
38/// IQ1_S, IQ1_M and IQ4_NL had no device kernel; they have one now, so both take the same types.
39bool known(int type);
40/// Values a block of the type holds (256; the trellis types 128; 0: not one of these).
41int block_values(int type);
42/// Bytes of a block of the type (0: not one of these).
43int block_bytes(int type);
44/// Bytes of a row of k values (k % block_values == 0).
45inline size_t row_bytes(int type, int64_t k) { return (size_t) (k / block_values(type)) * (size_t) block_bytes(type); }
46/// The type's name ("?" when unknown).
47const char* name(int type);
48
49/// The token / window experts (ds4_gguf.cu's MxArgs, with the roles' types): group g = an expert (grp_ptr null: expert g
50/// of the plan, plan_ptr[g] its blob or 0, its entry g, input row 0; else blob grp_ptr[g], entries grp_ent[grp_start[g]
51/// ..], entry e's input row e >> 3).  x / h as int8 rows with an F32 scale per 32 (xq, xd: rows of n_embd; hq, hd: row e
52/// = entry e, rows of n_ff).
53struct Args {
54    const unsigned long long* plan_ptr = nullptr;
55    const unsigned long long* grp_ptr = nullptr;
56    const int *grp_start = nullptr, *grp_ent = nullptr, *n_groups = nullptr;
57    size_t tr[3] = {0, 0, 0};   // the roles' offsets in a blob
58    int n_embd = 0, n_ff = 0;
59    float limit = 0.0f;
60    const int8_t* xq = nullptr;
61    const float* xd = nullptr;
62    const int8_t* hq = nullptr;
63    const float* hd = nullptr;
64    float* h = nullptr;      // [entries][n_ff]: the clamped swiglu
65    float* part = nullptr;   // [entries][n_embd]: each entry's down output
66    int tg = 0, td = 0;      // gate / up's type, down's
67};
68/// grid (n_ff / 8, groups): h of every entry (ne_max: a group's most entries).  false: a type not covered.
69bool gate_up(const Args& a, int groups, int ne_max, cudaStream_t s);
70/// grid (n_embd / 8, groups): part of every entry.
71bool down(const Args& a, int groups, int ne_max, cudaStream_t s);
72
73/// The prompt path's expert sets (ds4_gguf.cu's SetK): expert i's blob at wbase + i stride owns the sorted rows
74/// [bounds[i], bounds[i + 1]) (local row = sorted row - row0); gate / up: h16[local] = F16 swiglu over x[idx[local]]
75/// (F32 rows, ldx); down: y[local] = down(h16[local]) (F16 rows in, ldx; F32 out, ldo).  A set's experts of at most 16
76/// rows take the token kernels' form (each weight read once, the rows' inputs rounded to int8 per 32 as the token path
77/// rounds them; STRATA_DS4_NO_LIGHT=1: never), the others GEMM tiles (tensor cores on CUDA, F16 operands).
78struct SetArgs {
79    const uint8_t* wbase = nullptr;
80    size_t stride = 0;
81    size_t tr[3] = {0, 0, 0};
82    const int* bounds = nullptr;
83    int row0 = 0, max_rows = 0, n_exp = 0;
84    int n_embd = 0, n_ff = 0;
85    float limit = 0.0f;
86    const float* xf = nullptr;
87    const int* idx = nullptr;
88    const uint16_t* xh = nullptr;
89    int ldx = 0;
90    uint16_t* h = nullptr;
91    float* y = nullptr;
92    int ldo = 0;
93    int tg = 0, td = 0;
94    int skip_le = 0;   // (the tiles) the experts of at most this many rows are left out: the light kernels took them
95};
96bool set_gate_up(const SetArgs& a, cudaStream_t s);
97bool set_down(const SetArgs& a, cudaStream_t s);
98/// Test hooks for the matrix-core (HIP gfx11 / gfx12) prompt tiles: hipk_force(-1 default rules, 0 scalar tiles, 1 matrix-core tiles) and the
99/// count of matrix-core launches so far (both do nothing / return -1 where there are no such tiles; hipk_force returns whether this device has them)
100bool hipk_force(int mode);
101long long hipk_launches();
102/// The light form alone (the set's experts of at most 16 rows; the others untouched), for any of these types and MXFP4
103/// (tg / td 39: ds4_gguf.cu's experts): false (nothing launched) when it is switched off - the caller's tiles take every
104/// expert then; true: they take those of more rows (skip_le 16).
105bool light_gate_up(const SetArgs& a, cudaStream_t s);
106bool light_down(const SetArgs& a, cudaStream_t s);
107
108/// GEMV jobs on such matrices (an expert's views on the host-routed path): y[c][r] = alpha (row r . x_c) + bias[r] for
109/// ncol activations, x as int8 rows (xq + c k, scales xd + c k / 32).
110struct MvJob {
111    const uint8_t* w = nullptr;
112    size_t ld = 0;   // bytes a row
113    int type = 0, k = 0, n = 0, ncol = 1;
114    const int8_t* xq = nullptr;
115    const float* xd = nullptr;
116    float* y = nullptr;
117    size_t ycs = 0;
118    const float* bias = nullptr;
119    float alpha = 1.0f;
120};
121bool mv(const MvJob* jobs, int n, cudaStream_t s);
122
123/// n rows of k values of a matrix of the type (ld bytes a row) as BF16 (bf16) or F16 bits, dst[n][k]: the chunked prompt path's
124/// dense GEMM operand.  false: a type not covered.
125bool dequant_rows(int type, const void* raw, size_t ld, int k, int n, uint16_t* dst, bool bf16, cudaStream_t s);
126
127/// The trellis types' kernels (ds4_tq.cu), reached from the entry points above for those types: arithmetic as the CPU lane's
128/// (ds4_tq_cpu.cpp: int8 activations with an F32 scale a 32, exact weights), on the tensor cores of gfx12 (WMMA) or in FMAs.
129namespace tq {
130bool gate_up(const Args& a, int groups, int ne_max, cudaStream_t s);
131bool down(const Args& a, int groups, int ne_max, cudaStream_t s);
132bool set_gate_up(const SetArgs& a, cudaStream_t s);
133bool set_down(const SetArgs& a, cudaStream_t s);
134bool mv(const MvJob* jobs, int n, cudaStream_t s);
135}  // namespace tq
136
137}  // namespace strata::ds4::gguf::iq