use std::collections::HashMap;
use metrale_gpu_runtime::gpu::DevicePtr;
use metrale_model_weights::weights::{WeightDtype, WeightStore, WeightTensor};
use super::{Fp8KvScaleSource, fp8_kv_scale_source};
fn store_named(names: impl IntoIterator<Item = String>) -> WeightStore {
WeightStore::from_map(
names
.into_iter()
.map(|n| {
let t = WeightTensor {
ptr: DevicePtr::NULL,
shape: vec![1],
dtype: WeightDtype::BF16,
};
(n, t)
})
.collect::<HashMap<_, _>>(),
)
}
#[test]
fn the_unsloth_27b_layout_is_checkpoint_on_every_layer() {
let store = store_named((0..16).flat_map(|i| {
let p = format!("model.language_model.layers.{}.self_attn", 4 * i + 3);
[format!("{p}.k_scale"), format!("{p}.v_scale")]
}));
let census = store.kv_scale_census().expect("census");
assert_eq!(census.spellings(), ["k_scale"]);
assert_eq!(
fp8_kv_scale_source(0, census.len(), 16),
Fp8KvScaleSource::Checkpoint { layers: 16 }
);
}
#[test]
fn one_missing_layer_is_partial_not_checkpoint() {
assert_eq!(
fp8_kv_scale_source(0, 15, 16),
Fp8KvScaleSource::Partial {
from_checkpoint: 15,
attention_layers: 16
}
);
assert_eq!(
fp8_kv_scale_source(0, 0, 16),
Fp8KvScaleSource::Unscaled {
attention_layers: 16
}
);
}
#[test]
fn calibration_wins_and_extra_scaled_heads_are_not_partial() {
assert_eq!(
fp8_kv_scale_source(256, 16, 16),
Fp8KvScaleSource::Calibration {
tokens: 256,
shadowed: 16
}
);
assert_eq!(
fp8_kv_scale_source(0, 17, 16),
Fp8KvScaleSource::Checkpoint { layers: 17 }
);
}