expert_layout_load(pack, n_layers, n_expert, n_embd, n_ff, err): each layer's NativeFmt is built with
the model file's widths instead of the compile-time 2560/640 (geometry-inventory.md row 108), and a
pack without native_experts.txt (the canonical Q2_0 form) is refused for any other geometry.
generate.cpp reads ModelGeometry from the --native file before the layout load, logs it for a
non-Flash-Next file, refuses the fused tensor forms by name, takes the router k and the embedding
dims from it, and reads the rope keys under the file's own architecture prefix.
tools/iq_pack.py: --layout-only writes native_experts.txt alone and prints the census; the draft
(nextn) block of an -MTP file is left out of the trunk layout; fused ffn_gate_up_exps refused;
FORM carries qwen35moe's attn_norm / post_attention_norm / output_norm (F32 as stored).
tests/data/native_experts/qwen36-{iq3_xxs,q4_k_xl}.txt from the real files (40 x 256; 10,594 and
18,726 MiB, mean blobs 1.035 and 1.829 MiB, as censused in docs/QWEN36-35B-A3B.md), and
expert_layout_test loads them at 2048 x 512 with every layer's types (IQ4_XS down on 34/38/39;
Q6_K down on 34/38/39, Q5_K gate/up on 39) and sees them refused at 2560 x 640.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017tPVMg2wGSNR1KANEiUtwr
88 lines
5.6 KiB
C++
88 lines
5.6 KiB
C++
// include/strata/kernels/cpu/expert_layout.hpp - plan v0.3 P6: where each routed expert lives in experts.bin.
|
|
//
|
|
// A Q2_0 pack (tools/strata_pack.py) has one blob size for every layer, `BLOB`, in the Strata expert form. A
|
|
// native pack (tools/iq_pack.py, the IQ2_XS / IQ3_XXS files) keeps each expert's raw GGUF slices, so the blob
|
|
// size and the formats change from layer to layer; `native_experts.txt` says how. Everything that touches an
|
|
// expert blob - the arena, the VRAM tier, the prompt path, the CPU pool, the GPU window - asks this table.
|
|
#pragma once
|
|
|
|
#include "strata/kernels/cpu/expert.hpp"
|
|
#include "strata/kernels/cpu/native_expert.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace strata::kernels::cpu {
|
|
|
|
struct ExpertLayout {
|
|
bool native = false;
|
|
int64_t n_layers = 0, n_expert = NE;
|
|
std::vector<NativeFmt> fmt; ///< per layer (native packs)
|
|
std::vector<uint64_t> offset, bytes; ///< per layer: where its 512 blobs start, bytes per blob
|
|
/// Plan v0.3 P6: per layer, the absolute offsets of the gate / up / down tensors in their GGUF files, so the
|
|
/// arena can be filled from the GGUF itself when the pack has no experts.bin (3 x n_layers, 0 = unknown).
|
|
std::vector<uint64_t> gguf_off;
|
|
/// Per layer PER ROLE (3 x n_layers, `3 * layer + role`, matching `gguf_off`), the GGUF file - a name beside
|
|
/// the --native shard - that holds that role's tensor. Empty = the --native shard itself. Per role because a
|
|
/// shard boundary can fall inside a layer: Unsloth's UD-Q4_K_XL has layer 11's down in shard 2 and its gate
|
|
/// and up in shard 3 (native_experts.txt v4, the `gate,up,down` shard column).
|
|
std::vector<std::string> gguf_file;
|
|
int version = 0; ///< native_experts.txt's header version (0 = none given)
|
|
uint64_t max_blob = BLOB;
|
|
uint64_t total = 0; ///< experts.bin size
|
|
|
|
uint64_t blob_bytes(int64_t layer) const { return native ? bytes[(size_t) layer] : (uint64_t) BLOB; }
|
|
uint64_t layer_offset(int64_t layer) const {
|
|
return native ? offset[(size_t) layer] : (uint64_t) layer * (uint64_t) n_expert * (uint64_t) BLOB;
|
|
}
|
|
uint64_t blob_offset(int64_t layer, int64_t expert) const {
|
|
return layer_offset(layer) + (uint64_t) expert * blob_bytes(layer);
|
|
}
|
|
};
|
|
|
|
/// Plan v0.3 P6: whether this CPU (and its OS) runs the AVX-512 kernels (F, BW, VL, VNNI, VBMI). Probed in a
|
|
/// file compiled without AVX-512, so asking is safe everywhere; STRATA_FORCE_AVX2=1 answers no (for tests).
|
|
bool cpu_avx512_ok();
|
|
/// Whether this CPU runs AVX-512F + AVX-512BW + AVX-512VL + AVX-512DQ (e.g. Skylake-X i9-9920X) for iq_avx512.cpp.
|
|
bool cpu_avx512bw_ok();
|
|
/// Whether this CPU (and its OS) runs the AVX2 kernels (AVX, AVX2, FMA, F16C): the floor of every expert kernel
|
|
/// (q2_avx2.cpp, iq_avx2.cpp, and ggml-cpu in the portable build). STRATA_FORCE_AVX2 does not change it.
|
|
bool cpu_avx2_ok();
|
|
/// Whether this CPU (and its OS) runs AVX (Sandy Bridge and newer): the AVX1 router dot (kq_avx1.cpp) and ggml-cpu in
|
|
/// a STRATA_ISA_FLOOR=avx build. The other expert rows then run on ggml-cpu (experimental, slow).
|
|
bool cpu_avx1_ok();
|
|
/// SSE4.2 + POPCNT: ggml-cpu in a STRATA_ISA_FLOOR=none build.
|
|
bool cpu_sse42_ok();
|
|
/// STRATA_FORCE_ISA=avx2|avx|sse caps the probes above (3 = no cap, 2 = AVX2, 1 = AVX, 0 = SSE4.2), so the older-CPU
|
|
/// paths run on any PC: tests and the experimental builds' checks. ggml-cpu keeps what it was compiled for.
|
|
int cpu_isa_cap();
|
|
/// The experimental older-CPU build this engine is ("avx", "sse4.2"), or "" for the normal build (AVX2 floor).
|
|
const char* isa_floor_build();
|
|
/// Whether a native pack's layer of this ggml type runs on Strata's own Q2_0 kernels (AVX2 / AVX-512). On a
|
|
/// CPU without AVX2 (a native, non-portable build: ggml-cpu compiled for this CPU's SSE) a Q2_0 layer takes
|
|
/// ggml-cpu's own Q2_0 vec_dot like every other native type: slower, but it runs.
|
|
inline bool q2_native_kernels(int type) { return type == 42 && cpu_avx2_ok(); }
|
|
/// The CPU's brand string (CPUID 0x80000002..4), for messages; "unknown" when it has none.
|
|
std::string cpu_name();
|
|
/// Q2_0 GGUF rows / activation quantizer on the kernels this CPU has.
|
|
void q2_rows_any(const uint8_t* w, size_t row_bytes, int nblocks, const ActQ* const* a, int nt, float* const* out,
|
|
int r0, int r1);
|
|
void act_quant_any(const float* x, int n, ActQ& a);
|
|
|
|
/// The process-wide layout (canonical Q2_0 until `expert_layout_load` finds a native pack).
|
|
const ExpertLayout& expert_layout();
|
|
/// Reads `<pack_dir>/native_experts.txt` when it exists (a native pack), else sets the canonical layout.
|
|
/// Versions up to kExpertLayoutVersion are read; a newer one is refused (a newer packer wrote it).
|
|
/// `n_embd` / `n_ff` are the MODEL's widths (ModelGeometry, read from the model file): each layer's `NativeFmt` is
|
|
/// built with them and its byte count checked against the pack's blob size, so a pack of another geometry is refused
|
|
/// here by its numbers. Until Phase 2 step 2 of the Qwen3.6 port this passed the compile-time 2560/640
|
|
/// (geometry-inventory.md row 108), which refused every 2048/512 pack as "blob is X B but its formats make Y".
|
|
bool expert_layout_load(const std::string& pack_dir, int64_t n_layers, int64_t n_expert, int64_t n_embd, int64_t n_ff,
|
|
std::string& err);
|
|
/// The newest native_experts.txt this engine reads. v4 = v3 plus the per-role shard column `gate,up,down`,
|
|
/// written only when some layer's roles are in different shards (every other pack stays v3, byte for byte).
|
|
inline constexpr int kExpertLayoutVersion = 4;
|
|
|
|
} // namespace strata::kernels::cpu
|