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) =