use super::{ArchName, GgmlType, TensorRef};
pub const DEEPSEEK4_AGENTIC_Q2_NAME: &str = "deepseek4-agentic-q2";
pub const DEEPSEEK4_AGENTIC_Q2_METADATA_KEY: &str = "hf2q.quantization.profile";
#[derive(Debug, Clone, Copy, Default)]
pub struct Deepseek4AgenticQ2Policy;
impl Deepseek4AgenticQ2Policy {
pub const fn new() -> Self {
Self
}
pub fn target_for(self, tensor: &TensorRef<'_>, base_type: GgmlType) -> GgmlType {
assert_eq!(
tensor.arch,
ArchName::Deepseek4,
"DeepSeek-V4 agentic quantization cannot be applied to another architecture"
);
if is_q8_pinned_tensor(tensor.name) {
GgmlType::Q8_0
} else {
base_type
}
}
}
fn is_q8_pinned_tensor(name: &str) -> bool {
if matches!(name, "output.weight" | "token_embd.weight") {
return true;
}
if name.starts_with("output_hc_") || name.contains(".hc_attn_") || name.contains(".hc_ffn_") {
return true;
}
if name.contains("indexer") {
return true;
}
name.contains(".attn_compressor_")
|| name.contains(".attn_q_a.weight")
|| name.contains(".attn_q_b.weight")
|| name.contains(".attn_kv.weight")
|| name.contains(".attn_output_a.weight")
|| name.contains(".attn_output_b.weight")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::quantize::ggml_quants::{ArchName, SourceDtype};
fn tensor(name: &str) -> TensorRef<'_> {
TensorRef {
name,
shape: &[4096, 4096],
source_dtype: SourceDtype::F32,
arch: ArchName::Deepseek4,
layer_index: Some(2),
}
}
#[test]
fn pins_every_deepseek_context_discrimination_family() {
let policy = Deepseek4AgenticQ2Policy::new();
for name in [
"output.weight",
"token_embd.weight",
"output_hc_fn.weight",
"blk.2.hc_attn_fn.weight",
"blk.2.hc_ffn_fn.weight",
"blk.2.attn_compressor_gate.weight",
"blk.2.attn_compressor_kv.weight",
"blk.2.attn_q_a.weight",
"blk.2.attn_q_b.weight",
"blk.2.attn_kv.weight",
"blk.2.attn_output_a.weight",
"blk.2.attn_output_b.weight",
"blk.2.indexer.attn_q_b.weight",
"blk.2.indexer.proj.weight",
"blk.2.indexer_compressor_gate.weight",
] {
assert_eq!(
policy.target_for(&tensor(name), GgmlType::Q2_K),
GgmlType::Q8_0,
"{name}"
);
}
}
#[test]
fn preserves_routed_and_shared_expert_body_types() {
let policy = Deepseek4AgenticQ2Policy::new();
for (name, base) in [
("blk.2.ffn_gate_exps.weight", GgmlType::Q2_K),
("blk.2.ffn_up_exps.weight", GgmlType::Q2_K),
("blk.2.ffn_down_exps.weight", GgmlType::Q3_K),
("blk.2.ffn_gate_shexp.weight", GgmlType::Q2_K),
("blk.2.ffn_up_shexp.weight", GgmlType::Q2_K),
("blk.2.ffn_down_shexp.weight", GgmlType::Q3_K),
] {
assert_eq!(policy.target_for(&tensor(name), base), base, "{name}");
}
}
#[test]
#[should_panic(expected = "cannot be applied to another architecture")]
fn rejects_non_deepseek_architectures_in_release_and_debug_builds() {
let tensor = TensorRef {
name: "output.weight",
shape: &[256, 256],
source_dtype: SourceDtype::F32,
arch: ArchName::Llama3,
layer_index: None,
};
let _ = Deepseek4AgenticQ2Policy::new().target_for(&tensor, GgmlType::Q2_K);
}
}