trueno/backends/q6k/
colmajor.rs1use super::{f16_to_f32, SUPER_BLOCK_BYTES, SUPER_BLOCK_SIZE};
7
8#[inline(always)]
11fn extract_q6k_value(ql: &[u8], qh: &[u8], idx: usize) -> i8 {
12 let (n, r) = (idx / 128, idx % 128);
13 let (k, l) = (r / 32, r % 32);
14 let ql_byte = ql[64 * n + l + 32 * (k & 1)];
15 let low4 = if k < 2 { ql_byte & 0x0F } else { ql_byte >> 4 };
16 let high2 = (qh[32 * n + l] >> (2 * k)) & 0x03;
17 (low4 | (high2 << 4)) as i8 - 32
18}
19
20#[inline]
22fn accumulate_q6k_superblock_colmajor(
23 sb_data: &[u8],
24 x_j: f32,
25 output: &mut [f32],
26 output_offset: usize,
27 ne0: usize,
28) {
29 let ql = sb_data.get(0..128).expect("Q6_K: need ≥128 bytes for ql");
30 let qh = sb_data.get(128..192).expect("Q6_K: need ≥192 bytes for qh");
31 let scales = sb_data.get(192..208).expect("Q6_K: need ≥208 bytes for scales");
32 let d = f16_to_f32(u16::from_le_bytes([sb_data[208], sb_data[209]]));
33
34 for group in 0..16 {
35 let scale = (scales[group] as i8) as f32;
36 let group_offset = group * 16;
37
38 for j in 0..16 {
39 let idx = group_offset + j;
40 let output_idx = output_offset + idx;
41 if output_idx >= ne0 {
42 continue;
43 }
44 let q6 = extract_q6k_value(ql, qh, idx);
45 output[output_idx] += x_j * d * scale * q6 as f32;
46 }
47 }
48}
49
50#[deprecated(
64 since = "0.15.0",
65 note = "LAYOUT-001: Use row-major kernels. APR/GGUF data is transposed at import boundary."
66)]
67pub fn matmul_q6k_f32_colmajor(
68 q6k_data: &[u8],
69 input: &[f32],
70 ne0: usize, ne1: usize, ) -> Vec<f32> {
73 assert_eq!(input.len(), ne1, "Input length must match ne1 (input dimension)");
74
75 let blocks_per_col = (ne0 + SUPER_BLOCK_SIZE - 1) / SUPER_BLOCK_SIZE;
76 let col_bytes = blocks_per_col * SUPER_BLOCK_BYTES;
77
78 let mut output = vec![0.0f32; ne0];
79
80 for col_idx in 0..ne1 {
81 let col_start = col_idx * col_bytes;
82 let x_j = input[col_idx];
83
84 if x_j == 0.0 {
85 continue;
86 }
87
88 for sb_idx in 0..blocks_per_col {
89 let sb_start = col_start + sb_idx * SUPER_BLOCK_BYTES;
90 if sb_start + SUPER_BLOCK_BYTES > q6k_data.len() {
91 break;
92 }
93 let sb_data = &q6k_data[sb_start..sb_start + SUPER_BLOCK_BYTES];
94 let output_offset = sb_idx * SUPER_BLOCK_SIZE;
95 accumulate_q6k_superblock_colmajor(sb_data, x_j, &mut output, output_offset, ne0);
96 }
97 }
98
99 output
100}
101
102#[deprecated(
107 since = "0.15.0",
108 note = "LAYOUT-001: Use row-major kernels. APR/GGUF data is transposed at import boundary."
109)]
110#[inline]
111pub fn matmul_q6k_f32_colmajor_dispatch(
112 q6k_data: &[u8],
113 input: &[f32],
114 ne0: usize,
115 ne1: usize,
116) -> Vec<f32> {
117 #[allow(deprecated)]
118 matmul_q6k_f32_colmajor(q6k_data, input, ne0, ne1)
119}