From a11335aeed99d30ebdef123d673b569eb8a1c299 Mon Sep 17 00:00:00 2001 From: psv666 <2693925048@qq.com> Date: Fri, 20 Nov 2026 10:09:28 +0800 Subject: [PATCH] modernbert: reject zero attention heads before deriving geometry A CPU loader regression test on the synced #152 tree reproduced a division-by-zero panic for num_attention_heads=0 without head_dim. Return a field-specific error before division; cover the 1024 family and Vela 0.3B HF layouts with derived and explicit head dimensions. Preserve all positive-profile geometry and numerical assertions. --- src/models/modernbert/src/adapter.rs | 4 ++++ tests/modernbert/structure.rs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/models/modernbert/src/adapter.rs b/src/models/modernbert/src/adapter.rs index 87e18bc5..68cdbdcb 100644 --- a/src/models/modernbert/src/adapter.rs +++ b/src/models/modernbert/src/adapter.rs @@ -77,6 +77,10 @@ fn derive_head_dim( config: &Json, provenance: &mut BTreeMap, ) -> Result { + ensure!( + heads > 0, + "num_attention_heads must be positive, got {heads}" + ); let head_dim = match config.get("head_dim") { Some(value) => value .as_u64() diff --git a/tests/modernbert/structure.rs b/tests/modernbert/structure.rs index f075b661..e372b357 100644 --- a/tests/modernbert/structure.rs +++ b/tests/modernbert/structure.rs @@ -50,6 +50,23 @@ fn parse(config: &Value) -> ModernBertStructure { .0 } +#[test] +fn zero_attention_heads_is_rejected_without_panicking() { + for base in [family_config(), vela_encoder_config()] { + for explicit_head_dim in [false, true] { + let mut config = base.clone(); + config["num_attention_heads"] = json!(0); + if explicit_head_dim { + config["head_dim"] = json!(64); + } + let error = ModernBertConfigAdapter::from_value(&config, "config.json") + .unwrap_err() + .to_string(); + assert!(error.contains("num_attention_heads"), "{error}"); + } + } +} + #[test] fn hf_adapter_parses_the_vela_profile_with_derived_schedule() { let (structure, meta) =