metrale_model_layers/quant_format/
compressed_tensors.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! 2026-09-25: The compressed-tensors NVFP4 layout, mapped to
4//! [`Nvfp4Variant::CompressedTensors`]. Its packed weights are
5//! `.weight_packed` with a per-tensor `.weight_global_scale`, which the loader
6//! inverts into `weight_scale_2` (`weight_map::quantized_v2`).
7//!
8//! Owner: model-layers (weight loading).
9//! Invariants: none beyond the types.
10
11use crate::quant_format::{IgnoreList, QuantFormat};
12use crate::weight_map::Nvfp4Variant;
13use metrale_config::precision_plan::IgnoreDialect;
14
15/// 2026-09-25: A compressed-tensors checkpoint.
16#[derive(Debug)]
17pub struct CompressedTensorsFormat {
18    /// 2026-09-25: The config's `format` string; nothing in this module reads
19    /// it.
20    pub format: String,
21    /// 2026-09-30: The modules that ship unquantized, matched as the format specifies.
22    pub ignore: IgnoreList,
23}
24
25impl CompressedTensorsFormat {
26    /// 2026-09-30: A malformed ignore entry is refused.
27    pub fn new(format: String, ignore_modules: &[String]) -> anyhow::Result<Self> {
28        Ok(Self {
29            format,
30            ignore: IgnoreList::new(IgnoreDialect::CompressedTensors, ignore_modules)?,
31        })
32    }
33}
34
35impl QuantFormat for CompressedTensorsFormat {
36    fn name(&self) -> &'static str {
37        "compressed-tensors"
38    }
39
40    fn base_variant(&self) -> Nvfp4Variant {
41        Nvfp4Variant::CompressedTensors
42    }
43
44    fn is_ignored(&self, module_path: &str) -> bool {
45        self.ignore.matches(module_path)
46    }
47}