From d061454d79bac3356fa2127d093bb08982a87578 Mon Sep 17 00:00:00 2001 From: Luke Alonso Date: Thu, 3 Dec 2026 18:23:09 -0800 Subject: [PATCH] helios: derive NVFP4 engram row and scale widths from embedding_width Why: Helios's runtime-core engram table decoder hardcoded NVFP4 (`Nvfp4E4m3Global`) tables to a 160-element width (80 weight bytes and 10 scale bytes per row), matching Qwen3.8-Flash-Next (`ple_embed_dim=2560`, 16 heads). Qwen4-35B-A3B uses `ple_embed_dim=2048` across 16 heads (`embedding_width=128`, 64 weight bytes and 8 scale bytes per row), so core decoding rejected Qwen4-35B-A3B engram tables even though the protobuf metadata already carries `embedding_width`. What changed: In `helios/src/runtime_core/engram.rs`, `decode()` previously matched `Encoding::Nvfp4E4m3Global` to the fixed tuple `(160, 16, 80, 10)`. It now validates `table.embedding_width > 0 && table.embedding_width % 16 == 0` alongside the positive finite `global_scale` check and derives the row geometry `(width, 16, width / 2, width / 16)` from `table.embedding_width`. Added unit test `nvfp4_tables_accept_width_128_and_160_and_reject_unaligned_widths` covering valid widths 128 and 160 and rejecting 0, 8, and 120. Benefit: Existing 160-wide NVFP4 engram cores decode identically while 128-wide Qwen4-35B-A3B cores (and any other 16-aligned NVFP4 table width) load without model-specific runtime branches. Evidence: - `cargo check --locked --release --manifest-path helios/Cargo.toml --features runtime-core,server` succeeded against `origin/main` (`45e1c1ad`). - Limitation: `cargo test --lib` on `origin/main` (`45e1c1ad`) currently fails to compile on an unrelated `Core` literal missing `external_extents` at `helios/src/runtime_core/cache_memory.rs:1659` (fixed separately in #714), so the new unit test was compile-checked via inspection and requires #714 on `main` for `cargo test` execution. Design-Signed-Off-By: Gemini 4 Neon; design-review=2e5b --- helios/src/runtime_core/engram.rs | 59 +++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/helios/src/runtime_core/engram.rs b/helios/src/runtime_core/engram.rs index b054f8c84..d45f187f7 100644 --- a/helios/src/runtime_core/engram.rs +++ b/helios/src/runtime_core/engram.rs @@ -119,9 +119,13 @@ pub(super) fn decode( let (width, block, row_bytes, scale_bytes) = match encoding { wire::engram_table::Encoding::Fp8E4m3E8m0 => (256, 32, 256, 8), wire::engram_table::Encoding::Nvfp4E4m3Global - if table.global_scale.is_finite() && table.global_scale > 0.0 => + if table.global_scale.is_finite() + && table.global_scale > 0.0 + && table.embedding_width > 0 + && table.embedding_width % 16 == 0 => { - (160, 16, 80, 10) + let width = table.embedding_width; + (width, 16, (width / 2) as usize, (width / 16) as usize) } _ => return Err(invalid("invalid n-gram encoding or global scale")), }; @@ -790,4 +794,55 @@ mod tests { // Fixture buffers are sized for 128 tokens, so adding a 256-token scored_prefill must reject. assert!(decode(gpu_cat, &archive, &[gpu_core], 0, 4).is_err()); } + + #[test] + fn nvfp4_tables_accept_width_128_and_160_and_reject_unaligned_widths() { + let archive = StubFiles::new(); + let make = |width: u32, row_bytes: u64, scale_bytes: u64| { + let (mut catalog, mut core) = decode_fixture(1, 24, false, false); + let total_rows = catalog.tables[0].rows; + let tokens = 128u64; + let heads = 24u64; + let table = &mut catalog.tables[0]; + table.encoding = wire::engram_table::Encoding::Nvfp4E4m3Global as i32; + table.embedding_width = width; + table.scale_block = 16; + table.global_scale = 0.001; + table.weights = Some(wire::Span { + offset: 0, + nbytes: total_rows * row_bytes, + ..Default::default() + }); + table.scales = Some(wire::Span { + offset: total_rows * row_bytes, + nbytes: total_rows * scale_bytes, + ..Default::default() + }); + let buffers = &mut core.execution.as_mut().unwrap().buffers; + let w = buffers.iter_mut().find(|b| b.name == "engram_1_weights").unwrap(); + w.shape = vec![tokens * heads, row_bytes]; + w.strides = vec![row_bytes, 1]; + w.nbytes = tokens * heads * row_bytes; + let s = buffers.iter_mut().find(|b| b.name == "engram_1_scales").unwrap(); + s.shape = vec![tokens * heads, scale_bytes]; + s.strides = vec![scale_bytes, 1]; + s.nbytes = tokens * heads * scale_bytes; + (catalog, core) + }; + + for (width, row_bytes, scale_bytes) in [(128u32, 64u64, 8u64), (160, 80, 10)] { + let (catalog, core) = make(width, row_bytes, scale_bytes); + let decoded = decode(catalog, &archive, &[core], 0, 1).unwrap(); + assert_eq!(decoded.tables[0].planes[0].row_bytes, row_bytes as usize); + assert_eq!(decoded.tables[0].planes[1].row_bytes, scale_bytes as usize); + } + + for bad_width in [0u32, 8, 120] { + let (catalog, core) = make(bad_width, 64, 8); + assert!( + decode(catalog, &archive, &[core], 0, 1).is_err(), + "expected rejection for bad_width={bad_width}" + ); + } + } }