APR port, stage 4: trellis decode and Hadamard rotation on the GPU paths

- third_party/ggml/ggml-common-tq.h: the fork's TQ2_T/TQK6/TQK7 block
  structs and 2048-point codebook (device table).
- iq_kernels.cu: Fmt<144..146> (the fork's tq.cuh lane decode against q8_1,
  4 q8_1 blocks per 128-block), dq_tq in dq_dispatch (prompt FP16 path,
  embedding), is_iq / row bytes; hadamard_rows / hadamard_rows_f16
  (shared-memory FWHT in the CPU's butterfly order) and
  hadamard_device_signs; native_expert_grouped rotates h when
  NativeExpertLayout::had_block is set.
- verify.cpp (local GPU), remote_experts.cpp (helper GPUs), prefill.cpp
  (FP16 fallback; folded layers never take MMQ) rotate x / h into their
  own buffers; the shared expert and the CPU doorbell copy keep x.
- native_expert_parity --synthetic-hadamard; ctests for tqk6/tqk7 and
  tq2_t when ggml is the agention fork.
- generate.cpp: APR packs run only with STRATA_APR=1 until validated end
  to end on a GPU.
- hip_backend.cmake: gfx1151 as an unvalidated architecture.

Rebased onto upstream 0.1.39: native_expert_grouped takes the separate
SwiGLU / rotate / q8_1 passes when the down weights are folded (the one-pass
swiglu_q8_1 kernel has no h to rotate); the new decode peer GPU
(peer_experts.cpp) rotates its private input copy and passes the down signs,
as remote_experts.cpp does.

Ported from agentionai/Strata rc2 6c7abde (rc2 = 9ff02c9), author Laurent Zuijdwijk.
This commit is contained in:
Laurent Zuijdwijk authored and Niko1221 committed 2026-10-15 21:29:38 +02:00
1 parent f47b0385f3
commit fbf045eb6a
13 files changed
+629 -26

No files matched your search

+19 -3
View File
@@ -21,6 +21,7 @@
#include "strata/core/peer_experts.hpp"
#include "strata/kernels/iq_kernels.hpp"
#include "strata/kernels/cpu/expert_layout.hpp"
#include "strata/kernels/cpu/hadamard.hpp"
#include "strata/kernels/bf16_gemv.hpp"
#include "strata/kernels/native_router.hpp"
#include "strata/kernels/native_moe.hpp"
@@ -714,6 +715,7 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
staging_ = b.take<uint8_t>((uint64_t) kStagingBlobs * strata::kernels::cpu::expert_layout().max_blob);
hit_xq_ = b.take<uint8_t>(T * (N / 32) * 34); hit_xs_ = b.take<float>(T * (N / 32));
nat_xq_ = b.take<uint8_t>(T * (N / 32) * 36);
nat_xr_ = b.take<float>(T * N);
hit_scratch_ = b.take<uint8_t>(std::max<uint64_t>(
strata::kernels::moe_hit_grouped_scratch_bytes((int64_t) (T * K), g.n_embd, g.n_ff),
strata::kernels::native_expert_scratch_bytes((int64_t) (T * K), g.n_ff)));
@@ -766,6 +768,10 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState&
return false;
}
}
{ // APR (prism.hadamard, hadamard.txt): the sign vectors on this device, before any graph capture
had_sx_ = strata::kernels::hadamard_device_signs(strata::kernels::cpu::hadamard_signs((int) g.n_embd), (int) g.n_embd);
had_sh_ = strata::kernels::hadamard_device_signs(strata::kernels::cpu::hadamard_signs((int) g.n_ff), (int) g.n_ff);
}
sink_.staging = (unsigned long long) staging_;
sink_.staging_cap = kStagingBlobs;
(void) TS;
@@ -1639,8 +1645,17 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
if (sh_fork) cudaEventRecord(ev_join_, sh_cs_);
}
if (strata::kernels::cpu::expert_layout().native) {
if (!qdedup)
if (!q8_ffn) quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);
// APR: Hadamard-folded gate/up take H (s * x); xm itself stays as it is (the shared expert above and the
// doorbell copy for the CPU pool, which rotates its own in native_quant_act). The q8_1 image of xm that
// STRATA_QFUSE / STRATA_VERIFY_QDEDUP may already have written is of the unrotated input, so a folded layer
// always rewrites it from the rotated rows (same stream, after the shared expert has read it).
const auto& f = strata::kernels::cpu::expert_layout().fmt[(size_t) l];
if (f.had_gu) {
hadamard_rows(xm, nat_xr_ + (size_t) tb * N, n, N, f.had_block, had_sx_, cs);
quantize_q8_1_rows(nat_xr_ + (size_t) tb * N, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);
} else if (!qdedup && !q8_ffn) {
quantize_q8_1_rows(xm, n, N, nat_xq_ + (size_t) tb * (N / 32) * 36, cs);
}
} else
quantize_q8_0_scaled(xm, hit_xq_ + (size_t) tb * (N / 32) * 34, hit_xs_ + (size_t) tb * (N / 32), (int64_t) n * N, cs);
stamp(l, 18, grp);
@@ -1676,7 +1691,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
if (lay.native) {
// the layer's GGUF formats (i-quant gate/up, Q2_0 / IQ4_NL down)
const auto& f = lay.fmt[(size_t) l];
const NativeExpertLayout L = native_expert_layout(f.gu_type, f.d_type, f.n_embd, f.n_ff);
NativeExpertLayout L = native_expert_layout(f.gu_type, f.d_type, f.n_embd, f.n_ff);
if (f.had_d) { L.had_block = f.had_block; L.d_signs = had_sh_; } // APR: h is rotated in there
native_expert_grouped(L, gp, gs, gn, p_dst, p_tok, cap, cap,
nat_xq_ + (size_t) tb * (N / 32) * 36, hit_scratch_, dst_buf, cs, gy);
} else {