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}" + ); + } + } }