native-cpu-expert: the kernel A/B switches, the CPU probes and the dispatch are a Setting catalogue in settings.rs
Eight files kept their own OnceLock next to a settings::var parse (or a CPUID probe); they now ask settings.rs for a named Setting (same variables, same defaults, same parse, computed on first use). Eight process-global exceptions are deleted, one for the Setting cell is added. STRA-338
This commit is contained in:
+2
-9
@@ -64,7 +64,7 @@
|
||||
{"crate": "leleka-model-format", "file": "src/settings.rs", "ratchet": "env-read", "reason": "the one reader of the process environment in this crate: it returns the operator's switches as typed values"},
|
||||
{"crate": "leleka-obs", "file": "src/level.rs", "ratchet": "env-read", "reason": "the one reader of the process environment in this crate: LELEKA_LOG* parsed once into Settings, and the request-log path"},
|
||||
{"crate": "leleka-cuda-driver", "file": "src/settings.rs", "ratchet": "env-read", "reason": "the one reader of the process environment in this crate: its catalogue holds the operator settings as named cells (EnvSwitch, EnvSetting, LazySetting from leleka_cuda_model::env_switch, MarkerFiles) read on first use; the option parsers that take several variables (stager, stream-all, chain flags, sm120 switches) call the pass-through var/var_os from their own parse functions"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/settings.rs", "ratchet": "env-read", "reason": "the one reader of the process environment in this crate (CPU expert pool, a hot path): call sites still parse the text they ask for with the pass-through var/var_os; a typed Settings value captured when the pool starts would replace them"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/settings.rs", "ratchet": "env-read", "reason": "the one reader of the process environment in this crate (CPU expert pool, a hot path): the Setting catalogue parses the A/B switches and the kernel dispatch once; the pool and report readers still call the pass-through var/var_os with their own parsing"},
|
||||
{"crate": "leleka-cpu-kernels", "file": "src/dispatch.rs", "ratchet": "env-read", "reason": "the one reader of the process environment in this crate: ISA_ENV is parsed by Isa::from_request into the selected instruction set"},
|
||||
{"crate": "leleka-libc-shim", "file": "src/cuda_trace/trace_state.rs", "ratchet": "env-read", "reason": "Windows interposer (FFI): the CUDA call tracer reads its own LELEKA_BRINGUP_TRACE switches and must not depend on other crates"},
|
||||
{"crate": "leleka-libc-shim", "file": "src/lib.rs", "ratchet": "env-read", "reason": "Windows interposer (FFI): the DLL search directories come from the process environment before any other crate can load"},
|
||||
@@ -255,14 +255,7 @@
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_expert_layout.rs", "ratchet": "process-global", "reason": "the process-wide expert layout the pool threads read; it is set at load time by the engine"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_general_pool/job_default.rs", "ratchet": "process-global", "reason": "the origin instant of the pool's job timestamps"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_general_pool.rs", "ratchet": "process-global", "reason": "the hook that releases the GPU waits, installed once by the engine (a function pointer)"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_ggml_native.rs", "ratchet": "process-global", "reason": "the admission verdict of the native ggml kernels on this CPU, computed once"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_iq/dot_multi512.rs", "ratchet": "process-global", "reason": "LELEKA_IQ_SINGLE: operator setting parsed once from the environment and cached for the process"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_iq/u16.rs", "ratchet": "process-global", "reason": "LELEKA_IQ_MULTI: operator setting parsed once from the environment and cached for the process"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_legacy/set_oracle_q8_zero.rs", "ratchet": "process-global", "reason": "STRATA_CPU_YMM: operator setting parsed once from the environment and cached for the process"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_native_general/group_dot_bytes_policy.rs", "ratchet": "process-global", "reason": "STRATA_KQ256: operator setting parsed once from the environment and cached for the process"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_pool/epoch_scheduling.rs", "ratchet": "process-global", "reason": "LELEKA_POOL_TASKS_PER_THREAD: operator setting parsed once from the environment and cached for the process"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_quant.rs", "ratchet": "process-global", "reason": "LELEKA_Q8K: operator setting parsed once from the environment and cached for the process"},
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/rust_simd.rs", "ratchet": "process-global", "reason": "the CPU feature dispatch and the STRATA_* switches parsed from it: operator setting parsed once from the environment and cached for the process"}
|
||||
{"crate": "leleka-native-cpu-expert", "file": "src/settings.rs", "ratchet": "process-global", "reason": "the cell of a Setting: the operator's switches and the CPU probes the kernels dispatch on are computed on the first read and kept for the process life (immutable; a hot path pays one atomic load, and kernel code carries no context to pass a settings value through)"}
|
||||
],
|
||||
"source_patterns": [
|
||||
{"id": "env-read", "regex": "\\benv::var(_os)?\\(", "why": "configuration must be parsed once into a Settings struct (S13), not read all over the code"},
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
//! The integration patch keeps the original AVX2 bodies as the unselected branch.
|
||||
use super::rust_ggml::Kind;
|
||||
use std::arch::{asm, x86_64::*};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
// These correspond to actual GGML compiler macros, not a runtime highest-ISA
|
||||
// heuristic. Root must seal compiler features together with the Original build.
|
||||
@@ -14,8 +13,7 @@ const V256: bool = cfg!(target_feature = "avxvnni");
|
||||
const SIGNED: bool = cfg!(target_feature = "avxvnniint8");
|
||||
|
||||
pub(super) fn compiled_admitted() -> bool {
|
||||
static ADMITTED: OnceLock<bool> = OnceLock::new();
|
||||
*ADMITTED.get_or_init(probe)
|
||||
crate::settings::GGML_ADMITTED.get()
|
||||
}
|
||||
/// The baseline AVX2 gate: CPUID leaf 7 present, AVX/OSXSAVE/FMA/F16C bits, XCR0 enabling XMM+YMM, AVX2 bit.
|
||||
/// Returns XCR0 and the leaf-7 registers when it holds.
|
||||
@@ -66,7 +64,7 @@ fn leaf1_ok(p: &CpuidResult) -> bool {
|
||||
let p1 = __cpuid_count(7, 1);
|
||||
!(BF && p1.eax & (1 << 5) == 0 || V256 && p1.eax & (1 << 4) == 0 || SIGNED && p1.edx & (1 << 4) == 0)
|
||||
}
|
||||
fn probe() -> bool {
|
||||
pub(super) fn probe() -> bool {
|
||||
// SAFETY: CPUID is available on x86_64; XGETBV follows OSXSAVE.
|
||||
// No STRATA_FORCE_AVX2 read here: that flag only controls Original explicit
|
||||
// IQ/Q2 dispatch and does not change compiled GGML type-trait arithmetic.
|
||||
|
||||
@@ -113,8 +113,7 @@ pub(super) unsafe fn dot_multi512_impl<const T: i32, const N: usize>(
|
||||
/// `LELEKA_IQ_SINGLE=legacy` keeps the generic single-token tree for IQ3_S / IQ3_XXS (A/B arm); the default is the
|
||||
/// register-built tree below (`dot_single_iq3`), the same integer lanes and float operations, so the same bits.
|
||||
pub(super) fn iq3_single_fast() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| crate::settings::var("LELEKA_IQ_SINGLE").as_deref() != Ok("legacy"))
|
||||
crate::settings::IQ3_SINGLE_FAST.get()
|
||||
}
|
||||
// GGML AVX2 T1: two alternating exact integer accumulators, factor AFTER final reduction.
|
||||
#[target_feature(enable = "avx2,fma,f16c")]
|
||||
|
||||
@@ -224,8 +224,7 @@ pub(in super::super) unsafe fn dot_multi512(t: i32, row: &[u8], ys: &[&[Q8K]], o
|
||||
/// drain per expert in `cpu_service_bench`, runs/cpu-service2 bench out-b) builds the multi-token AVX-512 IQ3
|
||||
/// halves in registers; the default keeps the stack arrays.
|
||||
pub(super) fn iq3_multi_fast() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| crate::settings::var("LELEKA_IQ_MULTI").as_deref() == Ok("register"))
|
||||
crate::settings::IQ3_MULTI_FAST.get()
|
||||
}
|
||||
/// One 32-weight half of an IQ3_S (21) / IQ3_XXS (18) block in a register: the eight grid words (lane l = word l,
|
||||
/// `decode`'s bytes 4l..4l+4), the sign word `decode` returns and its (uniform) 16-bit scale.
|
||||
|
||||
@@ -10,8 +10,8 @@ use super::{
|
||||
_mm512_cvtepi32_ps, _mm512_cvtepu16_epi64, _mm512_dpbusd_epi32, _mm512_fmadd_ps, _mm512_loadu_si512,
|
||||
_mm512_maskz_loadu_ps, _mm512_maskz_mov_ps, _mm512_mul_ps, _mm512_multishift_epi64_epi8, _mm512_permutexvar_ps,
|
||||
_mm512_reduce_add_ps, _mm512_set1_epi8, _mm512_set1_epi32, _mm512_set1_epi64, _mm512_setr_epi32, _mm512_setzero_ps,
|
||||
_mm512_setzero_si512, _xgetbv, ActQ, Error, FF, H, MAX_ROWS, ORACLE, OnceLock, Ordering, PlanarActivation,
|
||||
PlanarScratch, Result, SingleScratch, hsum_original,
|
||||
_mm512_setzero_si512, _xgetbv, ActQ, Error, FF, H, MAX_ROWS, ORACLE, Ordering, PlanarActivation, PlanarScratch,
|
||||
Result, SingleScratch, hsum_original,
|
||||
};
|
||||
|
||||
pub fn set_oracle_q8_zero(enabled: bool) {
|
||||
@@ -262,8 +262,7 @@ pub(super) fn row(codes: &[u8], scales: &[u8], a: &[&ActQ], out: &mut [f32], ora
|
||||
out[t] = row_oracle(codes, scales, a[t]);
|
||||
}
|
||||
} else {
|
||||
static ZMM: OnceLock<bool> = OnceLock::new();
|
||||
if *ZMM.get_or_init(|| crate::settings::var_os("STRATA_CPU_YMM").is_none()) {
|
||||
if crate::settings::CPU_ZMM.get() {
|
||||
row512(codes, scales, a, out);
|
||||
} else {
|
||||
row256(codes, scales, a, out);
|
||||
|
||||
@@ -45,10 +45,7 @@ pub(super) unsafe fn kq_dot(kind: Kind, row: &[u8], act: &[&NativeActivation], w
|
||||
}
|
||||
/// `STRATA_KQ256`: route multi-token Q4_K / Q5 / Q8 rows through the 256-bit kq kernels.
|
||||
fn kq256_enabled() -> bool {
|
||||
static KQ: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*KQ.get_or_init(|| {
|
||||
crate::settings::var("STRATA_KQ256").ok().is_some_and(|s| super::super::rust_simd::atoi(&s) != 0)
|
||||
})
|
||||
crate::settings::KQ256.get()
|
||||
}
|
||||
/// The kq kernel a multi-token row of original kind `k` takes under the env policy (gate/up Q4_K, down Q5/Q8).
|
||||
fn kq_route(k: i32, gu: bool, policy_nt: usize) -> bool {
|
||||
|
||||
@@ -132,12 +132,5 @@ impl Shared {
|
||||
/// Tasks per participating thread in a phase (pool.cpp `phase_tasks`: 3). `LELEKA_POOL_TASKS_PER_THREAD` (1..=16) is an
|
||||
/// A/B knob: every row is computed by the same code whatever task holds it, so the output does not depend on it.
|
||||
pub(super) fn tasks_per_thread() -> usize {
|
||||
static V: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
|
||||
*V.get_or_init(|| {
|
||||
crate::settings::var("LELEKA_POOL_TASKS_PER_THREAD")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.filter(|v| (1..=16).contains(v))
|
||||
.unwrap_or(3)
|
||||
})
|
||||
crate::settings::POOL_TASKS_PER_THREAD.get()
|
||||
}
|
||||
|
||||
@@ -40,8 +40,7 @@ pub(super) fn q8_k(x: &[f32], y: &mut [Q8K]) {
|
||||
}
|
||||
/// `LELEKA_Q8K=legacy` keeps the scalar `q8_k` on the borrowed-group path (A/B arm); the default there is `q8_k_avx2`.
|
||||
pub(super) fn q8k_fast() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| crate::settings::var("LELEKA_Q8K").as_deref() != Ok("legacy"))
|
||||
crate::settings::Q8K_FAST.get()
|
||||
}
|
||||
/// Horizontal maximum of the 8 lanes of `m` (high half first, then the pairwise reductions).
|
||||
#[target_feature(enable = "avx2,fma,f16c")]
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
//! x86 instruction leaves: copied arithmetic trees, no portable numerical fallback.
|
||||
use std::arch::x86_64::*;
|
||||
use std::sync::OnceLock;
|
||||
// SAFETY CONTRACT of every `#[target_feature]` leaf in this crate (rust_simd/quant/iq/q2/kq/k_blocks/ggml*/legacy):
|
||||
// a call requires the matching CPU gate to have succeeded first: avx2_ok() (AVX2+FMA+F16C+AVX+OSXSAVE+XCR0 and the
|
||||
// compiled GGML branch) for `avx2,fma,f16c` leaves; avx512_ok() && dq_ok() (or legacy_supported()) additionally for
|
||||
@@ -48,25 +47,25 @@ pub(super) struct Dispatch {
|
||||
pub avx512: bool,
|
||||
}
|
||||
pub(super) fn dispatch() -> &'static Dispatch {
|
||||
static D: OnceLock<Dispatch> = OnceLock::new();
|
||||
D.get_or_init(|| {
|
||||
let avx512 = avx512_ok();
|
||||
Dispatch {
|
||||
iq512: avx512 && crate::settings::var_os("STRATA_NO_IQ512").is_none(),
|
||||
iq256: crate::settings::var_os("STRATA_NO_IQ256").is_none(),
|
||||
iq4nl: crate::settings::var_os("STRATA_NO_IQ4NL").is_none(),
|
||||
mt_min: crate::settings::var("STRATA_IQ_MT_MIN").map(|s| atoi(&s)).unwrap_or(2),
|
||||
gather: crate::settings::var_os("STRATA_IQ_GATHER").is_some(),
|
||||
prefetch: crate::settings::var("STRATA_IQ_PREFETCH").map(|s| atoi(&s)).unwrap_or(2048),
|
||||
avx512,
|
||||
}
|
||||
})
|
||||
crate::settings::DISPATCH.get_ref()
|
||||
}
|
||||
/// The dispatch as the CPU and the process environment give it (what `settings::DISPATCH` caches).
|
||||
pub(super) fn dispatch_from_environment() -> Dispatch {
|
||||
let avx512 = avx512_ok();
|
||||
Dispatch {
|
||||
iq512: avx512 && crate::settings::var_os("STRATA_NO_IQ512").is_none(),
|
||||
iq256: crate::settings::var_os("STRATA_NO_IQ256").is_none(),
|
||||
iq4nl: crate::settings::var_os("STRATA_NO_IQ4NL").is_none(),
|
||||
mt_min: crate::settings::var("STRATA_IQ_MT_MIN").map(|s| atoi(&s)).unwrap_or(2),
|
||||
gather: crate::settings::var_os("STRATA_IQ_GATHER").is_some(),
|
||||
prefetch: crate::settings::var("STRATA_IQ_PREFETCH").map(|s| atoi(&s)).unwrap_or(2048),
|
||||
avx512,
|
||||
}
|
||||
}
|
||||
pub(super) fn avx2_ok() -> bool {
|
||||
static VALUE: OnceLock<bool> = OnceLock::new();
|
||||
*VALUE.get_or_init(probe_avx2) && super::rust_ggml_native::compiled_admitted()
|
||||
crate::settings::HOST_AVX2.get() && super::rust_ggml_native::compiled_admitted()
|
||||
}
|
||||
fn probe_avx2() -> bool {
|
||||
pub(super) fn probe_avx2() -> bool {
|
||||
// SAFETY: CPUID always available on x86_64; XGETBV only after OSXSAVE.
|
||||
unsafe {
|
||||
if __cpuid(0).eax < 7 {
|
||||
@@ -80,10 +79,9 @@ fn probe_avx2() -> bool {
|
||||
}
|
||||
}
|
||||
pub(super) fn avx512_ok() -> bool {
|
||||
static VALUE: OnceLock<bool> = OnceLock::new();
|
||||
*VALUE.get_or_init(probe_avx512)
|
||||
crate::settings::HOST_AVX512.get()
|
||||
}
|
||||
fn probe_avx512() -> bool {
|
||||
pub(super) fn probe_avx512() -> bool {
|
||||
if crate::settings::var("STRATA_FORCE_AVX2").is_ok_and(|v| v.as_bytes().first() == Some(&b'1')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
//! The only place of this crate that reads the process environment (`env-read` ratchet of `architecture.json`).
|
||||
//! The reads keep their call sites and their parsing; a later step replaces these functions by a parsed settings value.
|
||||
//! The only place of this crate that reads the process environment (`env-read` ratchet of `architecture.json`), and
|
||||
//! the catalogue of the operator settings and host probes the kernels dispatch on: each is a named [`Setting`] that is
|
||||
//! computed on first use and kept for the process life. A call site asks the catalogue; it never reads a variable or
|
||||
//! caches a parse of its own.
|
||||
|
||||
use std::env::{self, VarError};
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
/// `std::env::var`: the text of an environment variable.
|
||||
#[inline]
|
||||
@@ -15,3 +18,75 @@ pub(crate) fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
|
||||
pub(crate) fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
|
||||
env::var_os(key)
|
||||
}
|
||||
|
||||
/// A value that one function computes (from variables, or from the CPU) on the first read; a hot path pays one atomic
|
||||
/// load afterwards.
|
||||
pub(crate) struct Setting<T> {
|
||||
compute: fn() -> T,
|
||||
value: OnceLock<T>,
|
||||
}
|
||||
|
||||
impl<T> Setting<T> {
|
||||
pub(crate) const fn new(compute: fn() -> T) -> Self {
|
||||
Self { compute, value: OnceLock::new() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn get_ref(&self) -> &T {
|
||||
self.value.get_or_init(self.compute)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> Setting<T> {
|
||||
#[inline]
|
||||
pub(crate) fn get(&self) -> T {
|
||||
*self.get_ref()
|
||||
}
|
||||
}
|
||||
|
||||
// What the CPU offers (a probe of the host, immutable for the process).
|
||||
pub(crate) static HOST_AVX2: Setting<bool> = Setting::new(crate::rust_simd::probe_avx2);
|
||||
pub(crate) static HOST_AVX512: Setting<bool> = Setting::new(crate::rust_simd::probe_avx512);
|
||||
/// Whether the compiled GGML kernels are admitted on this CPU.
|
||||
pub(crate) static GGML_ADMITTED: Setting<bool> = Setting::new(crate::rust_ggml_native::probe);
|
||||
/// The kernel dispatch: the CPU plus the `STRATA_*` switches.
|
||||
pub(crate) static DISPATCH: Setting<crate::rust_simd::Dispatch> =
|
||||
Setting::new(crate::rust_simd::dispatch_from_environment);
|
||||
|
||||
// The operator's A/B switches of the kernels.
|
||||
/// `LELEKA_IQ_SINGLE=legacy` keeps the generic single-token tree for IQ3_S / IQ3_XXS.
|
||||
pub(crate) static IQ3_SINGLE_FAST: Setting<bool> = Setting::new(iq3_single_fast);
|
||||
/// `LELEKA_IQ_MULTI=register` builds the multi-token AVX-512 IQ3 halves in registers.
|
||||
pub(crate) static IQ3_MULTI_FAST: Setting<bool> = Setting::new(iq3_multi_fast);
|
||||
/// `LELEKA_Q8K=legacy` keeps the scalar `q8_k` on the borrowed-group path.
|
||||
pub(crate) static Q8K_FAST: Setting<bool> = Setting::new(q8k_fast);
|
||||
/// `STRATA_CPU_YMM` set: the legacy rows take the 256-bit kernel instead of the 512-bit one.
|
||||
pub(crate) static CPU_ZMM: Setting<bool> = Setting::new(cpu_zmm);
|
||||
/// `STRATA_KQ256`: route multi-token Q4_K / Q5 / Q8 rows through the 256-bit kq kernels.
|
||||
pub(crate) static KQ256: Setting<bool> = Setting::new(kq256);
|
||||
/// `LELEKA_POOL_TASKS_PER_THREAD` (1..=16, default 3): tasks per participating thread in a phase.
|
||||
pub(crate) static POOL_TASKS_PER_THREAD: Setting<usize> = Setting::new(pool_tasks_per_thread);
|
||||
|
||||
fn iq3_single_fast() -> bool {
|
||||
var("LELEKA_IQ_SINGLE").as_deref() != Ok("legacy")
|
||||
}
|
||||
|
||||
fn iq3_multi_fast() -> bool {
|
||||
var("LELEKA_IQ_MULTI").as_deref() == Ok("register")
|
||||
}
|
||||
|
||||
fn q8k_fast() -> bool {
|
||||
var("LELEKA_Q8K").as_deref() != Ok("legacy")
|
||||
}
|
||||
|
||||
fn cpu_zmm() -> bool {
|
||||
var_os("STRATA_CPU_YMM").is_none()
|
||||
}
|
||||
|
||||
fn kq256() -> bool {
|
||||
var("STRATA_KQ256").ok().is_some_and(|s| crate::rust_simd::atoi(&s) != 0)
|
||||
}
|
||||
|
||||
fn pool_tasks_per_thread() -> usize {
|
||||
var("LELEKA_POOL_TASKS_PER_THREAD").ok().and_then(|v| v.parse().ok()).filter(|v| (1..=16).contains(v)).unwrap_or(3)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user