metrale_gpu_runtime/buffers/
sizes_q2.rs1use metrale_config::ModelConfig;
12
13pub fn q2_dequant_scratch_bytes(config: &ModelConfig) -> usize {
20 let bf16 = 2;
21 let hd = config.head_dim;
22 let q_proj_mul = if config.attn_gated { 2 } else { 1 };
23 let max_n = config
24 .intermediate_size
25 .max(config.ssm_qkvz_size())
26 .max(config.mamba2_in_proj_size())
27 .max(config.num_attention_heads * q_proj_mul * hd)
28 .max(2 * config.num_key_value_heads * hd);
29 max_n * config.hidden_size * bf16
30}
31
32pub fn q2_scratch_sizes(config: &ModelConfig, m: usize, h: usize, hd: usize) -> (usize, usize) {
38 let dequant_enabled = std::env::var("METRALE_GGUF_NATIVE_Q2").ok().as_deref() == Some("1");
39 let mmq_enabled = std::env::var("METRALE_GGUF_NATIVE_Q2_MMQ").ok().as_deref() == Some("1");
40 q2_scratch_sizes_for(config, m, h, hd, dequant_enabled, mmq_enabled)
41}
42
43pub(super) fn q2_scratch_sizes_for(
44 config: &ModelConfig,
45 m: usize,
46 h: usize,
47 hd: usize,
48 dequant_enabled: bool,
49 mmq_enabled: bool,
50) -> (usize, usize) {
51 let q2_dequant_scratch = if dequant_enabled {
52 q2_dequant_scratch_bytes(config)
53 } else {
54 0
55 };
56
57 let q2_act_q8 = if mmq_enabled {
58 let kmax = h
59 .max(config.intermediate_size)
60 .max(config.num_attention_heads * hd);
61 let kpad = kmax.div_ceil(256) * 256;
62 m * kpad * 4 + (1 << 20)
63 } else {
64 0
65 };
66
67 (q2_dequant_scratch, q2_act_q8)
68}