use metrale_config::weight_quantization::{KernelCaps, LmHeadChoice, LmHeadFormat};
use metrale_config::{
QuantizationConfig, W4a4Downcast, WeightQuantPolicy, WeightQuantTier, WeightQuantization,
};
use super::lm_head_flags;
fn unsloth() -> QuantizationConfig {
let block: serde_json::Value = serde_json::from_str(include_str!(
"../../../../config/src/precision_plan/fixtures/unsloth_qwen3_8_27b_nvfp4.json"
))
.expect("fixture parses");
metrale_config::parse_quantization_config(&serde_json::json!({ "quantization_config": block }))
.expect("parses")
.expect("declares")
}
fn choice(tier: WeightQuantization, batched_head: bool) -> LmHeadChoice {
let qc = unsloth();
let caps = KernelCaps {
fp8_lm_head_batched: batched_head,
..KernelCaps::default()
};
WeightQuantPolicy::for_checkpoint(
WeightQuantTier::new(tier, W4a4Downcast::Off).expect("tier"),
Some(&qc),
caps,
)
.lm_head()
}
const ENGINE_DEFAULT: (Option<bool>, bool) = (None, false);
const FP8: (Option<bool>, bool) = (Some(false), true);
#[test]
fn declared_takes_the_fp8_head_only_with_the_batched_kernel() {
let without = choice(WeightQuantization::Declared, false);
assert_eq!(without, LmHeadChoice::PendingFp8Kernel);
assert_eq!(lm_head_flags("default", without).unwrap(), ENGINE_DEFAULT);
let with = choice(WeightQuantization::Declared, true);
assert_eq!(with, LmHeadChoice::Declared(LmHeadFormat::Fp8));
assert_eq!(lm_head_flags("default", with).unwrap(), FP8);
}
#[test]
fn nvfp4_keeps_the_engine_head() {
for batched in [false, true] {
let c = choice(WeightQuantization::Nvfp4, batched);
assert_eq!(c, LmHeadChoice::EngineDefault);
assert_eq!(lm_head_flags("default", c).unwrap(), ENGINE_DEFAULT);
}
}
#[test]
fn an_explicit_head_overrides_every_choice() {
for c in [
LmHeadChoice::EngineDefault,
LmHeadChoice::PendingFp8Kernel,
LmHeadChoice::Declared(LmHeadFormat::Fp8),
LmHeadChoice::Declared(LmHeadFormat::Bf16),
] {
assert_eq!(
lm_head_flags("bf16", c).unwrap(),
(Some(true), false),
"{c:?}"
);
assert_eq!(
lm_head_flags("nvfp4", c).unwrap(),
(Some(false), false),
"{c:?}"
);
assert_eq!(lm_head_flags("fp8", c).unwrap(), FP8, "{c:?}");
assert!(lm_head_flags("fp4", c).is_err(), "{c:?}");
}
}
#[test]
fn the_shipped_caps_take_the_declared_fp8_head() {
let qc = unsloth();
let caps = metrale_model_layers::layers::kernel_caps();
let pick = |tier| {
WeightQuantPolicy::for_checkpoint(
WeightQuantTier::new(tier, W4a4Downcast::Off).expect("tier"),
Some(&qc),
caps,
)
.lm_head()
};
let declared = pick(WeightQuantization::Declared);
assert_eq!(declared, LmHeadChoice::Declared(LmHeadFormat::Fp8));
assert_eq!(lm_head_flags("default", declared).unwrap(), FP8);
let nvfp4 = pick(WeightQuantization::Nvfp4);
assert_eq!(nvfp4, LmHeadChoice::EngineDefault);
assert_eq!(lm_head_flags("default", nvfp4).unwrap(), ENGINE_DEFAULT);
}