Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/SETTINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ Off unless you set them. The last column says where each one is described.
| `STRATA_WDDM_BUDGET` | number | AMD, Intel Arc | [AMD_HIP.md](AMD_HIP.md) |
| `STRATA_WDDM_MARGIN_MIB` | number | AMD | [AMD_HIP.md](AMD_HIP.md) |

## Switches that go back to earlier behaviour (134)
## Switches that go back to earlier behaviour (135)

These behaviours are on by default. Setting the switch returns the earlier behaviour; the notes in the register say which
version made it the default.
Expand Down Expand Up @@ -491,6 +491,7 @@ version made it the default.
| `STRATA_QFUSE_HC_LOCAL` | text | NVIDIA and AMD | v0.2.16 | - |
| `STRATA_QSA_EARLY_EXIT` | number | NVIDIA and AMD, Intel Arc | v0.2.16 | - |
| `STRATA_QSA_FUSE_NR` | on / off | NVIDIA and AMD | v0.2.16 | - |
| `STRATA_QUANT_NVFP4_X4` | on / off | NVIDIA and AMD | - | [inkling.md](models/inkling.md) |
| `STRATA_RDNA_KQ_ALL` | on / off | NVIDIA and AMD | v0.2.19 | - |
| `STRATA_REMOTE_SPIN` | on / off | NVIDIA, AMD, Intel Arc | v0.2.16 | - |
| `STRATA_RESIDENT_PIN` | on / off | NVIDIA and AMD, Intel Arc | v0.2.16 | [DETAILS.md](DETAILS.md), [TROUBLESHOOTING.md](TROUBLESHOOTING.md), [UNSLOTH_Q4.md](UNSLOTH_Q4.md) |
Expand Down
15 changes: 15 additions & 0 deletions docs/models/inkling.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,21 @@ that costs nothing. With the experts' cache frozen (`--expert-swaps 0`) 40 token
same with and without it. Not measured: a card that holds every expert (nothing is staged there), a CPU share of the
experts, a Mac, an AMD card (the code is the same, not built), and `--layer-split`.

## Writing faster when the CPU computes experts: four NVFP4 rows at a time

The experts the card does not hold are computed on the CPU. UD-Q4_K_XL's are NVFP4 (layers 0 to 19), and the CPU's
NVFP4 dot product was bound by its instructions, not by the RAM: with 14 threads of an i9-12900H it read 42.2 GB/s of
weights where Q4_K and Q8_0 reach the RAM's 63-65 GB/s. The CPU now computes four NVFP4 weight rows against an
activation row at a time (AVX2, and AVX-VNNI where the CPU has it), which loads the activations once for the four
rows and puts a block's codes in order with fewer shuffles: 48.4 GB/s with 14 threads, 12.2 instead of 9.8 GB/s on
one performance core. Every value has the bits of the one-row kernel (`poly_cpu_x4_test`), so the answers do not change.

On an RTX 4060 Laptop (8 GB) with that i9-12900H, UD-Q4_K_XL, 14 threads, a short chat with the experts held still
(`STRATA_FREEZE_EXPERTS=1 --expert-swaps 0`, so the CPU computes the most): 21.3 instead of 19.7 tokens/s once the
runs settled (6 interleaved pairs, faster in each; CPU experts 27.9 instead of 30.8 ms a token), the same tokens in
all 12 runs. With the default settings the CPU experts took 9.0 instead of 9.6 ms a token and the speed was the same
within the noise (40.4 against 39.9 tokens/s, 4 runs each). `STRATA_QUANT_NVFP4_X4=0` turns it off.

## A new install starts from the experts the model picks most

On a card that holds only part of the experts, the engine fills the expert cache with the ones its profile counts as
Expand Down
1 change: 1 addition & 0 deletions engine/src/poly/cpu_x4_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ bool fill(Q::Type t, std::vector<uint8_t>& w, int blocks, std::mt19937& rng) {
case Q::Type::Q4_K: case Q::Type::Q5_K: put(0); put(2); break;
case Q::Type::Q6_K: put(208); break;
case Q::Type::MXFP4: b[0] = (uint8_t) (118 + rng() % 18); break;
case Q::Type::NVFP4: break; // any UE4M3 scale byte decodes to a finite value (0x7F and bit 7 included)
default: return false;
}
}
Expand Down
18 changes: 17 additions & 1 deletion engine/src/quant/dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,29 @@ DotRowsFn dot_rows_kernel(Type w, Arith a) noexcept {
return nullptr;
}

// STRATA_QUANT_NVFP4_X4=0: no four-row NVFP4 kernel (cpu_experts() then takes the one-row one, with the same bits)
static bool nvfp4_x4_on() noexcept {
static const bool on = [] {
const char* v = std::getenv("STRATA_QUANT_NVFP4_X4");
return !(v && v[0] == '0');
}();
return on;
}

const DotX4Kernel* dot_x4_kernel(Type w, Arith a, Isa max_isa) noexcept {
#ifdef STRATA_QUANT_X86_TIERS
const DotX4Table tables[] = {avx512_dot_x4_table(), avx2_dot_x4_table()};
const DotX4Table tables[] = {
avx512_dot_x4_table(),
#ifdef STRATA_QUANT_AVXVNNI
avxvnni_dot_x4_table(),
#endif
avx2_dot_x4_table(),
};
const DotX4Kernel* best = nullptr;
for (const DotX4Table& tb : tables) {
for (int i = 0; i < tb.n; ++i) {
const DotX4Kernel& k = tb.v[i];
if (k.weight == Type::NVFP4 && !nvfp4_x4_on()) continue; // STRATA_QUANT_NVFP4_X4=0
if (k.weight == w && k.arith == a && (int) k.isa <= (int) max_isa && isa_ok(k.isa)) {
if (!best || (int) k.isa > (int) best->isa) best = &k;
}
Expand Down
1 change: 1 addition & 0 deletions engine/src/quant/registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ActTable avx512_act_table() noexcept; // x86/act_avx512.cpp (Arith::Avx2 bi
DotTable avx512_dot_table() noexcept; // x86/dot_avx512.cpp (Arith::Avx2 bits in 512-bit lanes, Isa::Avx512)
DotTable avx512vbmi_dot_table() noexcept; // x86/dot_avx512vbmi.cpp (Arith::Avx2 bits with VBMI byte lookups, Isa::Avx512Vbmi)
DotTable avxvnni_dot_table() noexcept; // x86/dot_avxvnni.cpp (Arith::Avx2 bits with AVX-VNNI, Isa::AvxVnni), with STRATA_QUANT_AVXVNNI
DotX4Table avxvnni_dot_x4_table() noexcept; // x86/dot_avxvnni.cpp (its 4-row kernels), with STRATA_QUANT_AVXVNNI
DotRowsFn avx2_dot_rows(Type w) noexcept; // x86/dot_avx2.cpp: dot_rows_kernel() of the Avx2 contract, or nullptr
DotX4Table avx512_dot_x4_table() noexcept; // x86/dot_avx512.cpp (4-row shared-activation VPDPBUSD/VPDPWSSD kernels)
#endif
Expand Down
2 changes: 2 additions & 0 deletions engine/src/quant/x86/dot_avx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace avx2 {

#include "x86_helpers.inc"
#include "x86_dots.inc"
#include "x86_nvfp4_x4.inc"

// ---- Q2_0 x Q8_0 in AVX2 (ported from the Inkling 2 engine of PR #200, src/poly/cpu_lowbit.cpp) ----

Expand Down Expand Up @@ -1154,6 +1155,7 @@ const DotX4Kernel kX4Table[] = {
ENTRY_X4(Q8_0, Q8_0, q8_0_q8_0),
ENTRY_X4(Q4_K, Q8_K, q4_K_q8_K),
ENTRY_X4(IQ4_XS, Q8_K, iq4_xs_q8_K),
{avx2::dot_nvfp4_q8_0_x4, Type::NVFP4, Type::Q8_0, Arith::Avx2, Isa::Avx2, "avx2_nvfp4_q8_0_x4"},
};
#undef ENTRY_X4
#undef ENTRY_OPT
Expand Down
8 changes: 8 additions & 0 deletions engine/src/quant/x86/dot_avxvnni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace avxvnni {

#include "x86_helpers.inc"
#include "x86_dots.inc"
#include "x86_nvfp4_x4.inc"

} // namespace avxvnni

Expand All @@ -31,8 +32,15 @@ const DotKernel kTable[] = {
ENTRY(Q8_0, Q8_0, q8_0_q8_0), ENTRY(IQ4_NL, Q8_0, iq4_nl_q8_0), ENTRY(MXFP4, Q8_0, mxfp4_q8_0), ENTRY(NVFP4, Q8_0, nvfp4_q8_0),
};
#undef ENTRY

// four rows a call, for the formats above whose one-row kernel cpu_experts() takes from this tier (it takes a four-row
// kernel of the one-row kernel's own tier only)
const DotX4Kernel kX4Table[] = {
{avxvnni::dot_nvfp4_q8_0_x4, Type::NVFP4, Type::Q8_0, Arith::Avx2, Isa::AvxVnni, "avxvnni_nvfp4_q8_0_x4"},
};
} // namespace

DotTable avxvnni_dot_table() noexcept { return {kTable, (int) (sizeof kTable / sizeof kTable[0])}; }
DotX4Table avxvnni_dot_x4_table() noexcept { return {kX4Table, (int) (sizeof kX4Table / sizeof kX4Table[0])}; }

} // namespace strata::quant
61 changes: 61 additions & 0 deletions engine/src/quant/x86/x86_nvfp4_x4.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// src/quant/x86/x86_nvfp4_x4.inc - (PolyStrata) four NVFP4 weight rows against one Q8_0 activation row, included by
// dot_avx2.cpp (maddubs + madd) and dot_avxvnni.cpp (VPDPBUSD). Each row's result has the bits of dot_nvfp4_q8_0
// (x86_dots.inc, ggml's AVX2 branch) of the same tier:
// - the one-row kernel multiplies [values 0-15 | 16-31] of each Q8_0 activation block by the codes in that order
// (the 16-value sub-block s is qs[8s..8s+7], low nibbles first). Here a block's 32 code bytes come in one load,
// their 64-bit quarters go to [0 2 1 3], and one 64-bit unpack of the low and the high nibbles gives that order for
// sub-blocks 0 and 1, the other for 2 and 3: the same int32 sums of four products in the same lanes.
// - ggml takes |w| and puts w's sign on the activation; here |w| comes from a table of magnitudes and the sign from
// the code's bit 3 (kvalues_fp4 is sign and magnitude), so the products and their sums are the same.
// - the scales are the one-row kernel's float products (ue4m3 * the activation block's scale), and go into its fmadd
// chain, sub-blocks 0-1 then 2-3. (Decoding the four rows' scales by vector arithmetic instead gave the same bits
// and was slower.)
// - the activation blocks and their scales are loaded once for the four rows.
// Used by the tier whose one-row kernel it mirrors (cpu_experts' rows_of(): x4->isa == dot->isa); poly_cpu_x4_test
// checks the bits.

static inline __m256i nvfp4_x4_quads(const __m256i ax, const __m256i sy) {
#if defined(__AVXVNNI__) && !defined(__AVX512VNNI__)
return _mm256_dpbusd_avx_epi32(_mm256_setzero_si256(), ax, sy);
#else
return _mm256_madd_epi16(_mm256_maddubs_epi16(ax, sy), _mm256_set1_epi16(1));
#endif
}

void dot_nvfp4_q8_0_x4(int n, float out[4], const void* STRATA_RESTRICT w_base, size_t row_bytes,
const void* STRATA_RESTRICT vy) {
assert(n % QK_NVFP4 == 0);
const int nb = n / QK_NVFP4;
const block_q8_0* STRATA_RESTRICT y = (const block_q8_0*) vy;
const block_nvfp4* rows[4];
for (int r = 0; r < 4; ++r) rows[r] = (const block_nvfp4*) ((const uint8_t*) w_base + (size_t) r * row_bytes);
static const int8_t kAbsFp4[16] = {0, 1, 2, 3, 4, 6, 8, 12, 0, 1, 2, 3, 4, 6, 8, 12};
const __m256i abs256 = _mm256_broadcastsi128_si256(_mm_loadu_si128((const __m128i*) kAbsFp4));
const __m256i m4b = _mm256_set1_epi8(0x0f);
const __m256i k78 = _mm256_set1_epi8(0x78); // code + 0x78: negative exactly where bit 3 (the sign) is set
__m256 acc[4] = {_mm256_setzero_ps(), _mm256_setzero_ps(), _mm256_setzero_ps(), _mm256_setzero_ps()};
const __m256i idx01 = _mm256_setr_epi32(0, 0, 0, 0, 1, 1, 1, 1), idx23 = _mm256_setr_epi32(2, 2, 2, 2, 3, 3, 3, 3);
for (int ib = 0; ib < nb; ++ib) {
const __m256i y01 = _mm256_loadu_si256((const __m256i*) y[2 * ib].qs);
const __m256i y23 = _mm256_loadu_si256((const __m256i*) y[2 * ib + 1].qs);
const float dy0 = h2f(y[2 * ib].d), dy1 = h2f(y[2 * ib + 1].d);
const __m128 dy = _mm_setr_ps(dy0, dy0, dy1, dy1);
for (int r = 0; r < 4; ++r) {
const block_nvfp4& b = rows[r][ib];
const __m256i q = _mm256_permute4x64_epi64(_mm256_loadu_si256((const __m256i*) b.qs), 0xd8);
const __m256i lo = _mm256_and_si256(q, m4b);
const __m256i hi = _mm256_and_si256(_mm256_srli_epi16(q, 4), m4b);
const __m256i c01 = _mm256_unpacklo_epi64(lo, hi);
const __m256i c23 = _mm256_unpackhi_epi64(lo, hi);
const __m256i i01 = nvfp4_x4_quads(_mm256_shuffle_epi8(abs256, c01), _mm256_sign_epi8(y01, _mm256_add_epi8(c01, k78)));
const __m256i i23 = nvfp4_x4_quads(_mm256_shuffle_epi8(abs256, c23), _mm256_sign_epi8(y23, _mm256_add_epi8(c23, k78)));
const __m256 s8 = _mm256_castps128_ps256(_mm_mul_ps(_mm_setr_ps(ue4m3_to_fp32(b.d[0]), ue4m3_to_fp32(b.d[1]),
ue4m3_to_fp32(b.d[2]), ue4m3_to_fp32(b.d[3])), dy));
const __m256 s01 = _mm256_permutevar8x32_ps(s8, idx01);
const __m256 s23 = _mm256_permutevar8x32_ps(s8, idx23);
acc[r] = _mm256_fmadd_ps(s01, _mm256_cvtepi32_ps(i01), acc[r]);
acc[r] = _mm256_fmadd_ps(s23, _mm256_cvtepi32_ps(i23), acc[r]);
}
}
for (int r = 0; r < 4; ++r) out[r] = hsum_float_8(acc[r]);
}
35 changes: 35 additions & 0 deletions terminal/data/settings-register.json
Original file line number Diff line number Diff line change
Expand Up @@ -42439,6 +42439,41 @@
"retire_after": "",
"retired_in": "0.2.16"
},
{
"name": "STRATA_QUANT_NVFP4_X4",
"backends": [
"cuda-hip"
],
"reads": [
{
"file": "engine/src/quant/dispatch.cpp",
"line": 91,
"backend": "cuda-hip",
"fn": "getenv"
}
],
"parse": "flag",
"default_state": "on",
"code_default": null,
"engine_sets": [],
"engine_defaults": [],
"launch_files": [],
"launch_sets": [],
"launch_reads": [],
"doc_mentions": [
"docs/models/inkling.md"
],
"first_commit": null,
"first_date": null,
"since": null,
"last_commit": null,
"last_date": null,
"status": "rollback",
"owner": "kernels/cpu",
"note": "0: no four-row NVFP4 CPU kernel (the one-row one, the same bits)",
"retire_after": "",
"retired_in": ""
},
{
"name": "STRATA_RAM_ADAPT",
"backends": [
Expand Down