candle-mi 0.1.9

Mechanistic interpretability for language models in Rust, built on candle
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Fast-path RNN kernel — raw `f32` forward pass without `Tensor` overhead.
//!
//! Phase A benchmarks showed candle's per-tensor-operation overhead dominates
//! on tiny `AlgZoo` models (18–25× slower than `PyTorch`). This module
//! provides a raw `f32` forward pass that operates on `&[f32]` slices with
//! no heap allocation per timestep.
//!
//! All Phase B analysis modules (`ablation`, `piecewise`, `probing`,
//! `surprise`) use these functions internally for bulk forward passes.

use crate::error::{MIError, Result};
use crate::stoicheia::StoicheiaRnn;
use crate::stoicheia::config::StoicheiaConfig;

/// Maximum hidden size supported by the stack-allocated buffer.
///
/// Covers all `AlgZoo` hidden sizes (2, 4, 6, 8, 10, 12, 16, 20, 24, 32).
const MAX_H: usize = 32;

// ---------------------------------------------------------------------------
// RnnWeights
// ---------------------------------------------------------------------------

/// Raw `f32` weight storage for an `AlgZoo` RNN.
///
/// Extracted from [`Tensor`](candle_core::Tensor) once at load time, then
/// used by the fast-path kernel and all analysis modules without further
/// candle overhead.
#[derive(Debug, Clone)]
#[allow(clippy::similar_names)]
pub struct RnnWeights {
    /// Input-to-hidden weights, row-major `[hidden_size, 1]`.
    pub weight_ih: Vec<f32>,
    /// Hidden-to-hidden weights, row-major `[hidden_size, hidden_size]`.
    pub weight_hh: Vec<f32>,
    /// Output projection weights, row-major `[output_size, hidden_size]`.
    pub weight_oh: Vec<f32>,
    /// Hidden dimension.
    pub hidden_size: usize,
    /// Output dimension (`seq_len` for distribution tasks, 1 for scalar).
    pub output_size: usize,
}

impl RnnWeights {
    /// Extract raw `f32` weights from a loaded [`StoicheiaRnn`].
    ///
    /// Copies each weight tensor to a contiguous `Vec<f32>` on CPU.
    /// This is a one-time cost; all subsequent fast-path operations
    /// use these slices directly.
    ///
    /// # Errors
    ///
    /// Returns [`MIError::Model`](crate::MIError::Model) if tensor
    /// extraction fails.
    #[allow(clippy::similar_names)]
    pub fn from_model(model: &StoicheiaRnn) -> Result<Self> {
        let config = model.config();
        let weight_ih: Vec<f32> = model.weight_ih().flatten_all()?.to_vec1()?;
        let weight_hh: Vec<f32> = model.weight_hh().flatten_all()?.to_vec1()?;
        let weight_oh: Vec<f32> = model.weight_oh().flatten_all()?.to_vec1()?;

        Ok(Self {
            weight_ih,
            weight_hh,
            weight_oh,
            hidden_size: config.hidden_size,
            output_size: config.output_size(),
        })
    }

    /// Create from explicit weight vectors (for standardized or handcrafted
    /// models).
    ///
    /// # Panics
    ///
    /// Panics if `hidden_size > 32` (exceeds stack buffer) or if slice
    /// lengths do not match the declared dimensions.
    #[must_use]
    #[allow(clippy::similar_names)]
    pub fn new(
        weight_ih: Vec<f32>,
        weight_hh: Vec<f32>,
        weight_oh: Vec<f32>,
        hidden_size: usize,
        output_size: usize,
    ) -> Self {
        assert!(
            hidden_size <= MAX_H,
            "hidden_size {hidden_size} exceeds MAX_H {MAX_H}"
        );
        assert_eq!(weight_ih.len(), hidden_size);
        assert_eq!(weight_hh.len(), hidden_size * hidden_size);
        assert_eq!(weight_oh.len(), output_size * hidden_size);
        Self {
            weight_ih,
            weight_hh,
            weight_oh,
            hidden_size,
            output_size,
        }
    }
}

// ---------------------------------------------------------------------------
// Forward pass functions
// ---------------------------------------------------------------------------

/// Run a batch of RNN forward passes on raw `f32` slices.
///
/// No [`Tensor`](candle_core::Tensor) objects, no heap allocation per
/// timestep. The inner loop is structured for auto-vectorization:
/// contiguous slices, no branches in the hot path, hoisted invariants.
///
/// # Shapes
/// - `inputs`: row-major `[n_inputs, seq_len]`
/// - `outputs`: row-major `[n_inputs, output_size]`
///
/// # Errors
///
/// Returns [`MIError::Config`](crate::MIError::Config) if slice lengths
/// do not match the declared dimensions, or if `hidden_size` exceeds the
/// stack buffer.
pub fn forward_fast(
    weights: &RnnWeights,
    inputs: &[f32],
    outputs: &mut [f32],
    n_inputs: usize,
    config: &StoicheiaConfig,
) -> Result<()> {
    validate_fast_args(weights, inputs, outputs, n_inputs, config)?;

    let h = weights.hidden_size;
    let seq_len = config.seq_len;
    let out_size = weights.output_size;

    for i in 0..n_inputs {
        let mut hidden = [0.0_f32; MAX_H];
        let mut pre_act = [0.0_f32; MAX_H];

        // RNN timestep loop
        for t in 0..seq_len {
            // INDEX: i * seq_len + t bounded by n_inputs * seq_len = inputs.len()
            #[allow(clippy::indexing_slicing)]
            let x_t = inputs[i * seq_len + t];

            rnn_cell(weights, x_t, &hidden, &mut pre_act, h);

            // ReLU
            for j in 0..h {
                // INDEX: j bounded by h <= MAX_H
                #[allow(clippy::indexing_slicing)]
                {
                    hidden[j] = pre_act[j].max(0.0);
                }
            }
        }

        // Output projection: W_oh @ hidden
        output_projection(weights, &hidden, outputs, i, h, out_size);
    }

    Ok(())
}

/// Run forward passes with neuron ablation.
///
/// Same as [`forward_fast`], but neurons marked `true` in `ablated`
/// have their hidden state forced to zero after each `ReLU` step.
///
/// # Shapes
/// - `ablated`: `[hidden_size]`, `true` = zero this neuron
///
/// # Errors
///
/// Returns [`MIError::Config`](crate::MIError::Config) if slice lengths
/// do not match the declared dimensions.
pub fn forward_fast_ablated(
    weights: &RnnWeights,
    inputs: &[f32],
    outputs: &mut [f32],
    n_inputs: usize,
    config: &StoicheiaConfig,
    ablated: &[bool],
) -> Result<()> {
    validate_fast_args(weights, inputs, outputs, n_inputs, config)?;
    if ablated.len() != weights.hidden_size {
        return Err(MIError::Config(format!(
            "ablated length {} != hidden_size {}",
            ablated.len(),
            weights.hidden_size
        )));
    }

    let h = weights.hidden_size;
    let seq_len = config.seq_len;
    let out_size = weights.output_size;

    for i in 0..n_inputs {
        let mut hidden = [0.0_f32; MAX_H];
        let mut pre_act = [0.0_f32; MAX_H];

        for t in 0..seq_len {
            // INDEX: i * seq_len + t bounded by n_inputs * seq_len = inputs.len()
            #[allow(clippy::indexing_slicing)]
            let x_t = inputs[i * seq_len + t];

            rnn_cell(weights, x_t, &hidden, &mut pre_act, h);

            // ReLU + ablation
            for j in 0..h {
                // INDEX: j bounded by h <= MAX_H
                #[allow(clippy::indexing_slicing)]
                {
                    hidden[j] = if ablated[j] { 0.0 } else { pre_act[j].max(0.0) };
                }
            }
        }

        output_projection(weights, &hidden, outputs, i, h, out_size);
    }

    Ok(())
}

/// Run a single forward pass, returning the full activation trace.
///
/// Records pre-activation values at every (timestep, neuron) pair.
/// Used by piecewise-linear analysis to determine which neurons fire.
///
/// # Shapes
/// - `input`: `[seq_len]` (single input)
/// - `pre_activations`: row-major `[seq_len, hidden_size]` (output)
/// - `output`: `[output_size]` (output)
///
/// # Errors
///
/// Returns [`MIError::Config`](crate::MIError::Config) if slice lengths
/// do not match the declared dimensions.
// EXPLICIT: range loops with index arithmetic for SIMD-friendly layout.
#[allow(clippy::needless_range_loop)]
pub fn forward_fast_traced(
    weights: &RnnWeights,
    input: &[f32],
    pre_activations: &mut [f32],
    output: &mut [f32],
    config: &StoicheiaConfig,
) -> Result<()> {
    let h = weights.hidden_size;
    let seq_len = config.seq_len;
    let out_size = weights.output_size;

    if h > MAX_H {
        return Err(MIError::Config(format!(
            "hidden_size {h} exceeds MAX_H {MAX_H}"
        )));
    }
    if input.len() != seq_len {
        return Err(MIError::Config(format!(
            "input length {} != seq_len {seq_len}",
            input.len()
        )));
    }
    if pre_activations.len() != seq_len * h {
        return Err(MIError::Config(format!(
            "pre_activations length {} != seq_len * hidden_size {}",
            pre_activations.len(),
            seq_len * h
        )));
    }
    if output.len() != out_size {
        return Err(MIError::Config(format!(
            "output length {} != output_size {out_size}",
            output.len()
        )));
    }

    let mut hidden = [0.0_f32; MAX_H];

    for t in 0..seq_len {
        // INDEX: t bounded by seq_len = input.len()
        #[allow(clippy::indexing_slicing)]
        let x_t = input[t];

        // Compute pre-activation into the output trace buffer directly
        for j in 0..h {
            // INDEX: j bounded by h, array indices bounded by weight slice lengths
            #[allow(clippy::indexing_slicing)]
            {
                let mut acc = x_t * weights.weight_ih[j];
                for k in 0..h {
                    acc = weights.weight_hh[j * h + k].mul_add(hidden[k], acc);
                }
                pre_activations[t * h + j] = acc;
            }
        }

        // ReLU → hidden
        for j in 0..h {
            // INDEX: j bounded by h <= MAX_H
            #[allow(clippy::indexing_slicing)]
            {
                hidden[j] = pre_activations[t * h + j].max(0.0);
            }
        }
    }

    // Output projection
    for o in 0..out_size {
        let mut acc = 0.0_f32;
        for j in 0..h {
            // INDEX: o * h + j bounded by out_size * h = weight_oh.len()
            #[allow(clippy::indexing_slicing)]
            {
                acc = weights.weight_oh[o * h + j].mul_add(hidden[j], acc);
            }
        }
        // INDEX: o bounded by out_size = output.len()
        #[allow(clippy::indexing_slicing)]
        {
            output[o] = acc;
        }
    }

    Ok(())
}

/// Compute model accuracy on a batch of inputs.
///
/// Runs [`forward_fast`], takes argmax of each output row, compares
/// with targets.
///
/// # Returns
///
/// Fraction of correct predictions (0.0 to 1.0).
///
/// # Errors
///
/// Returns [`MIError::Config`](crate::MIError::Config) if slice lengths
/// do not match.
pub fn accuracy(
    weights: &RnnWeights,
    inputs: &[f32],
    targets: &[u32],
    n_inputs: usize,
    config: &StoicheiaConfig,
) -> Result<f32> {
    if targets.len() != n_inputs {
        return Err(MIError::Config(format!(
            "targets length {} != n_inputs {n_inputs}",
            targets.len()
        )));
    }

    let out_size = weights.output_size;
    let mut outputs = vec![0.0_f32; n_inputs * out_size];
    forward_fast(weights, inputs, &mut outputs, n_inputs, config)?;

    let mut correct = 0_usize;
    for (i, target) in targets.iter().enumerate() {
        // INDEX: slice bounds valid because outputs.len() == n_inputs * out_size
        #[allow(clippy::indexing_slicing)]
        let row = &outputs[i * out_size..(i + 1) * out_size];
        let pred = argmax_f32(row);
        if *target == pred {
            correct += 1;
        }
    }

    // CAST: usize → f32, counts are small (≤ n_inputs)
    #[allow(clippy::cast_precision_loss, clippy::as_conversions)]
    let acc = correct as f32 / n_inputs as f32;
    Ok(acc)
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Single RNN cell: `pre_act = x_t * W_ih + W_hh @ hidden`.
// EXPLICIT: range loops with index arithmetic are SIMD-friendly;
// iterators would obscure the memory-access pattern.
#[inline]
#[allow(clippy::needless_range_loop)]
fn rnn_cell(
    weights: &RnnWeights,
    x_t: f32,
    hidden: &[f32; MAX_H],
    pre_act: &mut [f32; MAX_H],
    h: usize,
) {
    for j in 0..h {
        // INDEX: j bounded by h <= MAX_H; j * h + k bounded by h * h = weight_hh.len()
        #[allow(clippy::indexing_slicing)]
        {
            let mut acc = x_t * weights.weight_ih[j];
            for k in 0..h {
                acc = weights.weight_hh[j * h + k].mul_add(hidden[k], acc);
            }
            pre_act[j] = acc;
        }
    }
}

/// Output projection: `outputs[i] = W_oh @ hidden`.
// EXPLICIT: range loops with index arithmetic for SIMD-friendly layout.
#[inline]
#[allow(clippy::needless_range_loop)]
fn output_projection(
    weights: &RnnWeights,
    hidden: &[f32; MAX_H],
    outputs: &mut [f32],
    i: usize,
    h: usize,
    out_size: usize,
) {
    for o in 0..out_size {
        let mut acc = 0.0_f32;
        for j in 0..h {
            // INDEX: o * h + j bounded by out_size * h = weight_oh.len()
            #[allow(clippy::indexing_slicing)]
            {
                acc = weights.weight_oh[o * h + j].mul_add(hidden[j], acc);
            }
        }
        // INDEX: i * out_size + o bounded by n_inputs * out_size = outputs.len()
        #[allow(clippy::indexing_slicing)]
        {
            outputs[i * out_size + o] = acc;
        }
    }
}

/// Argmax over a `f32` slice. Returns 0 for empty slices.
///
/// Used by [`accuracy`] and by the `ablation` and `surprise` modules.
#[must_use]
pub fn argmax_f32(slice: &[f32]) -> u32 {
    debug_assert!(
        !slice.iter().any(|v| v.is_nan()),
        "argmax_f32: input contains NaN"
    );
    let mut best_idx = 0_u32;
    let mut best_val = f32::NEG_INFINITY;
    for (i, &v) in slice.iter().enumerate() {
        if v > best_val {
            best_val = v;
            // CAST: usize → u32, output_size fits in u32 (AlgZoo max = 10)
            #[allow(clippy::cast_possible_truncation, clippy::as_conversions)]
            {
                best_idx = i as u32;
            }
        }
    }
    best_idx
}

/// Validate arguments shared by [`forward_fast`] and [`forward_fast_ablated`].
fn validate_fast_args(
    weights: &RnnWeights,
    inputs: &[f32],
    outputs: &[f32],
    n_inputs: usize,
    config: &StoicheiaConfig,
) -> Result<()> {
    let h = weights.hidden_size;
    let seq_len = config.seq_len;
    let out_size = weights.output_size;

    if h > MAX_H {
        return Err(MIError::Config(format!(
            "hidden_size {h} exceeds MAX_H {MAX_H}"
        )));
    }
    if inputs.len() != n_inputs * seq_len {
        return Err(MIError::Config(format!(
            "inputs length {} != n_inputs * seq_len {}",
            inputs.len(),
            n_inputs * seq_len
        )));
    }
    if outputs.len() != n_inputs * out_size {
        return Err(MIError::Config(format!(
            "outputs length {} != n_inputs * output_size {}",
            outputs.len(),
            n_inputs * out_size
        )));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    /// Construct a tiny M₂,₂ model with known weights for testing.
    ///
    /// Weights chosen so the model is a simple comparator:
    /// - `W_ih = [1, -1]` — neuron 0 excited by input, neuron 1 inhibited
    /// - `W_hh = [[0, 0], [0, 0]]` — no recurrence (memoryless)
    /// - `W_oh = [[1, -1], [-1, 1]]` — output differentiates the two neurons
    fn test_weights_2_2() -> RnnWeights {
        RnnWeights::new(
            vec![1.0, -1.0],            // W_ih [2, 1]
            vec![0.0, 0.0, 0.0, 0.0],   // W_hh [2, 2]
            vec![1.0, -1.0, -1.0, 1.0], // W_oh [2, 2]
            2,
            2,
        )
    }

    fn test_config_2_2() -> StoicheiaConfig {
        StoicheiaConfig::from_task(crate::stoicheia::config::StoicheiaTask::SecondArgmax, 2, 2)
    }

    #[test]
    fn forward_fast_shape_and_output() {
        let weights = test_weights_2_2();
        let config = test_config_2_2();

        // Single input: [0.5, -0.3]
        let inputs = vec![0.5_f32, -0.3];
        let mut outputs = vec![0.0_f32; 2];

        forward_fast(&weights, &inputs, &mut outputs, 1, &config).unwrap();

        // With memoryless RNN (W_hh = 0):
        // t=0: pre = [0.5, -0.5], h = [0.5, 0.0] (ReLU)
        // t=1: pre = [-0.3, 0.3], h = [0.0, 0.3] (ReLU)
        // output = W_oh @ [0, 0.3] = [-0.3, 0.3]
        assert!((outputs[0] - (-0.3)).abs() < 1e-6);
        assert!((outputs[1] - 0.3).abs() < 1e-6);
    }

    #[test]
    fn forward_fast_batch() {
        let weights = test_weights_2_2();
        let config = test_config_2_2();

        let inputs = vec![0.5, -0.3, 1.0, 2.0];
        let mut outputs = vec![0.0_f32; 4];

        forward_fast(&weights, &inputs, &mut outputs, 2, &config).unwrap();

        // First input same as above
        assert!((outputs[0] - (-0.3)).abs() < 1e-6);
        assert!((outputs[1] - 0.3).abs() < 1e-6);
    }

    #[test]
    fn forward_fast_traced_matches_forward() {
        let weights = test_weights_2_2();
        let config = test_config_2_2();

        let input = vec![0.5_f32, -0.3];
        let mut pre_acts = vec![0.0_f32; 4]; // [2, 2]
        let mut traced_out = vec![0.0_f32; 2];

        forward_fast_traced(&weights, &input, &mut pre_acts, &mut traced_out, &config).unwrap();

        let mut fast_out = vec![0.0_f32; 2];
        forward_fast(&weights, &input, &mut fast_out, 1, &config).unwrap();

        // Outputs must match
        for (a, b) in traced_out.iter().zip(&fast_out) {
            assert!((a - b).abs() < 1e-6, "traced={a}, fast={b}");
        }

        // Pre-activations at t=0: [0.5, -0.5]
        assert!((pre_acts[0] - 0.5).abs() < 1e-6);
        assert!((pre_acts[1] - (-0.5)).abs() < 1e-6);
    }

    #[test]
    fn ablated_all_zeros_gives_zero_output() {
        let weights = test_weights_2_2();
        let config = test_config_2_2();

        let inputs = vec![0.5_f32, -0.3];
        let mut outputs = vec![0.0_f32; 2];
        let ablated = vec![true, true]; // ablate all neurons

        forward_fast_ablated(&weights, &inputs, &mut outputs, 1, &config, &ablated).unwrap();

        // All neurons zeroed → output = W_oh @ [0, 0] = [0, 0]
        assert!((outputs[0]).abs() < 1e-6);
        assert!((outputs[1]).abs() < 1e-6);
    }

    #[test]
    fn ablated_none_matches_normal() {
        let weights = test_weights_2_2();
        let config = test_config_2_2();

        let inputs = vec![0.5_f32, -0.3];
        let mut normal_out = vec![0.0_f32; 2];
        let mut ablated_out = vec![0.0_f32; 2];
        let ablated = vec![false, false]; // no ablation

        forward_fast(&weights, &inputs, &mut normal_out, 1, &config).unwrap();
        forward_fast_ablated(&weights, &inputs, &mut ablated_out, 1, &config, &ablated).unwrap();

        for (a, b) in normal_out.iter().zip(&ablated_out) {
            assert!((a - b).abs() < 1e-6);
        }
    }

    #[test]
    fn accuracy_perfect() {
        let weights = test_weights_2_2();
        let config = test_config_2_2();

        // Input where output[1] > output[0], so argmax = 1
        let inputs = vec![0.5_f32, -0.3];
        let targets = vec![1_u32]; // correct prediction

        let acc = accuracy(&weights, &inputs, &targets, 1, &config).unwrap();
        assert!((acc - 1.0).abs() < 1e-6);
    }

    #[test]
    fn accuracy_wrong() {
        let weights = test_weights_2_2();
        let config = test_config_2_2();

        let inputs = vec![0.5_f32, -0.3];
        let targets = vec![0_u32]; // wrong prediction (model predicts 1)

        let acc = accuracy(&weights, &inputs, &targets, 1, &config).unwrap();
        assert!(acc.abs() < 1e-6);
    }

    #[test]
    fn validation_rejects_wrong_input_length() {
        let weights = test_weights_2_2();
        let config = test_config_2_2();

        let inputs = vec![0.5_f32]; // too short
        let mut outputs = vec![0.0_f32; 2];

        let result = forward_fast(&weights, &inputs, &mut outputs, 1, &config);
        assert!(result.is_err());
    }
}