lattice-inference 0.2.2

Pure Rust transformer inference engine — safetensors loading, SIMD matmul, BGE/Qwen3 embeddings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Absorb a [`RandomizedHadamard`] rotation into a linear-layer weight matrix
//! offline, so the runtime forward pass is unchanged after the input or output
//! activation gets rotated.
//!
//! See [ADR-044](../../../../../docs/adr/ADR-044-quarot-rotated-quantization.md) §Decision
//! for the absorption identity.
//!
//! ## Bias semantics — v0 scope
//!
//! This module rotates **weight matrices only**. It does not touch biases.
//! v0 targets bias-free linear projections (Qwen3 / Qwen3.5 attention QKV
//! and output projections, MLP up/gate/down — all bias-free in the
//! Llama-style architectures lattice currently supports). If a future
//! caller wires a model with biased projections, output-side absorption
//! is incomplete on its own: the output bias `b` would also need
//! `b ← R · b` so the post-bias activation matches `R · y`. Input-side
//! absorption is unaffected by biases (the bias adds AFTER the matmul).
//! A bias helper is a one-line follow-up when needed; left out of v0 to
//! keep the API surface minimal.
//!
//! ## Identity
//!
//! A linear layer (bias-free) computes `y = W · x`. If we rotate the
//! input by `R` (so the upstream activation becomes `R · x`), we want
//! the layer to produce the same output. Rewrite:
//!
//! ```text
//!   y = W · x = W · R^T · (R · x) = (W · R^T) · x'
//! ```
//!
//! where `x' = R · x` is the rotated input. So `W' := W · R^T` is the absorbed
//! weight: the layer now operates on the rotated input directly. We call this
//! **input-side absorption**.
//!
//! Similarly, if we want the output to be pre-rotated by `R` (so downstream
//! consumers see `R · y`), we set `W' := R · W`. We call this **output-side
//! absorption**.
//!
//! ## Implementation
//!
//! For `W` in row-major `[rows × cols]`, the row `W[i, :]` is the linear
//! functional that produces output coordinate `i`. Applying `R` to that row
//! vector computes `R · W[i, :]^T`, which equals the i-th row of `W · R^T`.
//! So **input-side absorption applies `R` to each row** of `W` in place.
//!
//! Output-side absorption is symmetric: each column `W[:, j]` becomes
//! `R · W[:, j]`, so we extract each column, apply `R`, and write back.
//!
//! Both routines support f32 (storage tier) and f64 (precision tier per
//! ADR-044 §Risks).

use crate::error::InferenceError;
use crate::quant::quarot::hadamard::RandomizedHadamard;

fn check_shape(
    fn_name: &str,
    weight_len: usize,
    rows: usize,
    cols: usize,
    rotation_dim: usize,
    rotation_dim_should_match: usize,
    matching_label: &str,
) -> Result<(), InferenceError> {
    let expected = rows.checked_mul(cols).ok_or_else(|| {
        InferenceError::Inference(format!(
            "{fn_name}: rows*cols overflow (rows={rows}, cols={cols})"
        ))
    })?;
    if weight_len != expected {
        return Err(InferenceError::Inference(format!(
            "{fn_name}: weight length {weight_len} != rows*cols {expected}"
        )));
    }
    if rotation_dim != rotation_dim_should_match {
        return Err(InferenceError::Inference(format!(
            "{fn_name}: rotation.dim()={rotation_dim} but weight.{matching_label}={rotation_dim_should_match}"
        )));
    }
    Ok(())
}

/// Absorb `R` into the input side of a row-major `[rows × cols]` weight matrix
/// in place: `W ← W · R^T`. After this, a layer computing `y = W · x` produces
/// the same output when fed the pre-rotated input `x' = R · x`.
///
/// The rotation dimension must match `cols` (the layer's input dimension).
pub fn absorb_input_rotation(
    weight: &mut [f32],
    rows: usize,
    cols: usize,
    rotation: &RandomizedHadamard,
) -> Result<(), InferenceError> {
    check_shape(
        "absorb_input_rotation",
        weight.len(),
        rows,
        cols,
        rotation.dim(),
        cols,
        "cols",
    )?;
    for r in 0..rows {
        let row = &mut weight[r * cols..(r + 1) * cols];
        rotation.apply(row)?;
    }
    Ok(())
}

/// Absorb `R` into the output side of a row-major `[rows × cols]` weight matrix
/// in place: `W ← R · W`. After this, a layer computing `y = W · x` produces
/// the rotated output `R · y`.
///
/// The rotation dimension must match `rows` (the layer's output dimension).
pub fn absorb_output_rotation(
    weight: &mut [f32],
    rows: usize,
    cols: usize,
    rotation: &RandomizedHadamard,
) -> Result<(), InferenceError> {
    check_shape(
        "absorb_output_rotation",
        weight.len(),
        rows,
        cols,
        rotation.dim(),
        rows,
        "rows",
    )?;
    let mut column = vec![0.0_f32; rows];
    for c in 0..cols {
        for r in 0..rows {
            column[r] = weight[r * cols + c];
        }
        rotation.apply(&mut column)?;
        for r in 0..rows {
            weight[r * cols + c] = column[r];
        }
    }
    Ok(())
}

/// `f64` variant of [`absorb_input_rotation`].
///
/// Use during offline conversion when the f32 quantization budget cannot
/// afford the rotation's accumulated rounding error — see ADR-044 §Risks.
pub fn absorb_input_rotation_f64(
    weight: &mut [f64],
    rows: usize,
    cols: usize,
    rotation: &RandomizedHadamard,
) -> Result<(), InferenceError> {
    check_shape(
        "absorb_input_rotation_f64",
        weight.len(),
        rows,
        cols,
        rotation.dim(),
        cols,
        "cols",
    )?;
    for r in 0..rows {
        let row = &mut weight[r * cols..(r + 1) * cols];
        rotation.apply_f64(row)?;
    }
    Ok(())
}

/// `f64` variant of [`absorb_output_rotation`].
pub fn absorb_output_rotation_f64(
    weight: &mut [f64],
    rows: usize,
    cols: usize,
    rotation: &RandomizedHadamard,
) -> Result<(), InferenceError> {
    check_shape(
        "absorb_output_rotation_f64",
        weight.len(),
        rows,
        cols,
        rotation.dim(),
        rows,
        "rows",
    )?;
    let mut column = vec![0.0_f64; rows];
    for c in 0..cols {
        for r in 0..rows {
            column[r] = weight[r * cols + c];
        }
        rotation.apply_f64(&mut column)?;
        for r in 0..rows {
            weight[r * cols + c] = column[r];
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Tiny matvec: y[i] = sum_j W[i,j] * x[j].
    fn matvec(weight: &[f32], rows: usize, cols: usize, x: &[f32]) -> Vec<f32> {
        assert_eq!(x.len(), cols);
        let mut y = vec![0.0_f32; rows];
        for r in 0..rows {
            let row = &weight[r * cols..(r + 1) * cols];
            y[r] = row.iter().zip(x.iter()).map(|(a, b)| a * b).sum();
        }
        y
    }

    fn max_abs_diff(a: &[f32], b: &[f32]) -> f32 {
        a.iter()
            .zip(b.iter())
            .map(|(x, y)| (x - y).abs())
            .fold(0.0_f32, f32::max)
    }

    fn synthetic_weight(rows: usize, cols: usize, seed: u64) -> Vec<f32> {
        let mut state = seed;
        (0..rows * cols)
            .map(|_| {
                state = state
                    .wrapping_mul(6364136223846793005)
                    .wrapping_add(1442695040888963407);
                let bits = (state >> 11) as u32;
                (bits as f32 / u32::MAX as f32) - 0.5
            })
            .collect()
    }

    #[test]
    fn input_side_absorption_preserves_forward_pass() {
        let rows = 16;
        let cols = 64;
        let r = RandomizedHadamard::new(0xCAFE_BABE, cols).unwrap();
        let weight = synthetic_weight(rows, cols, 1);
        let x: Vec<f32> = synthetic_weight(1, cols, 2).into_iter().collect();

        let y_original = matvec(&weight, rows, cols, &x);

        let mut weight_abs = weight.clone();
        absorb_input_rotation(&mut weight_abs, rows, cols, &r).unwrap();
        let mut x_rotated = x.clone();
        r.apply(&mut x_rotated).unwrap();
        let y_after = matvec(&weight_abs, rows, cols, &x_rotated);

        let delta = max_abs_diff(&y_original, &y_after);
        assert!(
            delta < 1e-4,
            "input-side absorption diverged: max delta {delta}"
        );
    }

    #[test]
    fn output_side_absorption_rotates_forward_pass() {
        let rows = 64;
        let cols = 16;
        let r = RandomizedHadamard::new(0xFEED_FACE, rows).unwrap();
        let weight = synthetic_weight(rows, cols, 3);
        let x: Vec<f32> = synthetic_weight(1, cols, 4).into_iter().collect();

        let y_original = matvec(&weight, rows, cols, &x);
        let mut y_rotated_expected = y_original.clone();
        r.apply(&mut y_rotated_expected).unwrap();

        let mut weight_abs = weight.clone();
        absorb_output_rotation(&mut weight_abs, rows, cols, &r).unwrap();
        let y_after = matvec(&weight_abs, rows, cols, &x);

        let delta = max_abs_diff(&y_rotated_expected, &y_after);
        assert!(
            delta < 1e-4,
            "output-side absorption diverged: max delta {delta}"
        );
    }

    #[test]
    fn two_layer_round_trip_preserves_output() {
        // Layer A: y_A = W_A · x   (cols=64, rows=32)
        // Layer B: y_B = W_B · y_A (cols=32, rows=8)
        // After absorbing R on the boundary between A and B:
        //   W_A' = R · W_A (output-side on A; produces R·y_A as the intermediate)
        //   W_B' = W_B · R^T (input-side on B; consumes R·y_A back to original output)
        // The end-to-end y_B must equal the original y_B.
        let cols_a = 64;
        let rows_a = 32; // = cols_b
        let rows_b = 8;

        let r = RandomizedHadamard::new(0xDEAD_F00D, rows_a).unwrap();
        let w_a = synthetic_weight(rows_a, cols_a, 5);
        let w_b = synthetic_weight(rows_b, rows_a, 6);
        let x: Vec<f32> = synthetic_weight(1, cols_a, 7).into_iter().collect();

        let y_a_original = matvec(&w_a, rows_a, cols_a, &x);
        let y_b_original = matvec(&w_b, rows_b, rows_a, &y_a_original);

        let mut w_a_abs = w_a.clone();
        absorb_output_rotation(&mut w_a_abs, rows_a, cols_a, &r).unwrap();
        let mut w_b_abs = w_b.clone();
        absorb_input_rotation(&mut w_b_abs, rows_b, rows_a, &r).unwrap();

        let y_a_rotated = matvec(&w_a_abs, rows_a, cols_a, &x);
        let y_b_after = matvec(&w_b_abs, rows_b, rows_a, &y_a_rotated);

        let delta = max_abs_diff(&y_b_original, &y_b_after);
        assert!(
            delta < 5e-4,
            "two-layer round trip diverged: max delta {delta}"
        );
    }

    #[test]
    fn input_side_dimension_mismatch_rejected() {
        let r = RandomizedHadamard::new(1, 32).unwrap();
        let mut w = vec![0.0_f32; 16 * 64];
        assert!(absorb_input_rotation(&mut w, 16, 64, &r).is_err());
    }

    #[test]
    fn output_side_dimension_mismatch_rejected() {
        let r = RandomizedHadamard::new(1, 32).unwrap();
        let mut w = vec![0.0_f32; 16 * 64];
        assert!(absorb_output_rotation(&mut w, 16, 64, &r).is_err());
    }

    #[test]
    fn input_side_weight_length_mismatch_rejected() {
        let r = RandomizedHadamard::new(1, 64).unwrap();
        let mut w = vec![0.0_f32; 100];
        assert!(absorb_input_rotation(&mut w, 16, 64, &r).is_err());
    }

    #[test]
    fn rows_cols_overflow_rejected() {
        let r = RandomizedHadamard::new(1, 64).unwrap();
        let mut w = vec![0.0_f32; 100];
        let err = absorb_input_rotation(&mut w, usize::MAX, 64, &r).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("overflow"),
            "expected overflow error, got: {msg}"
        );
    }

    fn synthetic_weight_f64(rows: usize, cols: usize, seed: u64) -> Vec<f64> {
        synthetic_weight(rows, cols, seed)
            .into_iter()
            .map(f64::from)
            .collect()
    }

    fn matvec_f64(weight: &[f64], rows: usize, cols: usize, x: &[f64]) -> Vec<f64> {
        assert_eq!(x.len(), cols);
        let mut y = vec![0.0_f64; rows];
        for r in 0..rows {
            let row = &weight[r * cols..(r + 1) * cols];
            y[r] = row.iter().zip(x.iter()).map(|(a, b)| a * b).sum();
        }
        y
    }

    fn max_abs_diff_f64(a: &[f64], b: &[f64]) -> f64 {
        a.iter()
            .zip(b.iter())
            .map(|(x, y)| (x - y).abs())
            .fold(0.0_f64, f64::max)
    }

    #[test]
    fn input_side_f64_absorption_preserves_forward_pass() {
        let rows = 16;
        let cols = 64;
        let r = RandomizedHadamard::new(0xCAFE_BABE, cols).unwrap();
        let weight = synthetic_weight_f64(rows, cols, 1);
        let x: Vec<f64> = synthetic_weight_f64(1, cols, 2);

        let y_original = matvec_f64(&weight, rows, cols, &x);

        let mut weight_abs = weight.clone();
        absorb_input_rotation_f64(&mut weight_abs, rows, cols, &r).unwrap();
        let mut x_rotated = x.clone();
        r.apply_f64(&mut x_rotated).unwrap();
        let y_after = matvec_f64(&weight_abs, rows, cols, &x_rotated);

        let delta = max_abs_diff_f64(&y_original, &y_after);
        assert!(
            delta < 1e-12,
            "f64 input-side absorption diverged: max delta {delta}"
        );
    }

    #[test]
    fn output_side_f64_absorption_rotates_forward_pass() {
        let rows = 64;
        let cols = 16;
        let r = RandomizedHadamard::new(0xFEED_FACE, rows).unwrap();
        let weight = synthetic_weight_f64(rows, cols, 3);
        let x: Vec<f64> = synthetic_weight_f64(1, cols, 4);

        let y_original = matvec_f64(&weight, rows, cols, &x);
        let mut y_rotated_expected = y_original.clone();
        r.apply_f64(&mut y_rotated_expected).unwrap();

        let mut weight_abs = weight.clone();
        absorb_output_rotation_f64(&mut weight_abs, rows, cols, &r).unwrap();
        let y_after = matvec_f64(&weight_abs, rows, cols, &x);

        let delta = max_abs_diff_f64(&y_rotated_expected, &y_after);
        assert!(
            delta < 1e-12,
            "f64 output-side absorption diverged: max delta {delta}"
        );
    }

    #[test]
    fn f64_absorption_more_precise_than_f32() {
        let rows = 32;
        let cols = 128;
        let r = RandomizedHadamard::new(42, cols).unwrap();
        let weight_f32 = synthetic_weight(rows, cols, 9);
        let weight_f64 = weight_f32.iter().map(|&v| f64::from(v)).collect::<Vec<_>>();
        let x_f32: Vec<f32> = synthetic_weight(1, cols, 10);
        let x_f64: Vec<f64> = x_f32.iter().map(|&v| f64::from(v)).collect();

        let y_original_f64 = matvec_f64(&weight_f64, rows, cols, &x_f64);

        let mut w_f32_abs = weight_f32.clone();
        absorb_input_rotation(&mut w_f32_abs, rows, cols, &r).unwrap();
        let mut x_f32_rot = x_f32.clone();
        r.apply(&mut x_f32_rot).unwrap();
        let y_f32 = matvec(&w_f32_abs, rows, cols, &x_f32_rot);

        let mut w_f64_abs = weight_f64.clone();
        absorb_input_rotation_f64(&mut w_f64_abs, rows, cols, &r).unwrap();
        let mut x_f64_rot = x_f64.clone();
        r.apply_f64(&mut x_f64_rot).unwrap();
        let y_f64 = matvec_f64(&w_f64_abs, rows, cols, &x_f64_rot);

        let err_f32 = y_f32
            .iter()
            .zip(y_original_f64.iter())
            .map(|(a, b)| (f64::from(*a) - b).abs())
            .fold(0.0_f64, f64::max);
        let err_f64 = y_f64
            .iter()
            .zip(y_original_f64.iter())
            .map(|(a, b)| (a - b).abs())
            .fold(0.0_f64, f64::max);

        assert!(
            err_f64 < err_f32,
            "f64 absorption should be no less precise than f32: f32 err {err_f32}, f64 err {err_f64}"
        );
    }
}