aprender-core 0.29.2

Next-generation machine learning library in pure Rust
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
//! Minitron Width Pruning: Channel removal based on activation importance.
//!
//! # Toyota Way: Heijunka (Level Loading)
//! Balanced channel importance ensures no single dimension is over-pruned.
//!
//! # Algorithm
//! For each hidden dimension d:
//!   importance(d) = mean(|activations[:, d]|^2)
//!
//! Channels with lowest activation magnitude are pruned across all layers.
//!
//! # Constraint
//! Attention heads must be pruned as complete units to maintain validity.
//! Target hidden dimension must be divisible by number of attention heads.
//!
//! # References
//! - Muralidharan, S., et al. (2024). Compact language models via pruning
//!   and knowledge distillation. arXiv:2407.14679.

use super::error::PruningError;
use crate::autograd::Tensor;

/// Result of width (channel) pruning operation.
#[derive(Debug, Clone)]
pub struct WidthPruningResult {
    /// Original hidden dimension.
    pub original_hidden_dim: usize,
    /// Final hidden dimension after pruning.
    pub final_hidden_dim: usize,
    /// Original intermediate (FFN) dimension.
    pub original_intermediate_dim: usize,
    /// Final intermediate dimension after pruning.
    pub final_intermediate_dim: usize,
    /// Indices of hidden channels kept (sorted).
    pub hidden_channels_kept: Vec<usize>,
    /// Indices of intermediate channels kept (sorted).
    pub intermediate_channels_kept: Vec<usize>,
}

impl WidthPruningResult {
    /// Create a new width pruning result.
    #[must_use]
    pub fn new(
        original_hidden_dim: usize,
        final_hidden_dim: usize,
        original_intermediate_dim: usize,
        final_intermediate_dim: usize,
        hidden_channels_kept: Vec<usize>,
        intermediate_channels_kept: Vec<usize>,
    ) -> Self {
        Self {
            original_hidden_dim,
            final_hidden_dim,
            original_intermediate_dim,
            final_intermediate_dim,
            hidden_channels_kept,
            intermediate_channels_kept,
        }
    }

    /// Get hidden dimension compression ratio.
    #[must_use]
    pub fn hidden_compression_ratio(&self) -> f32 {
        if self.final_hidden_dim == 0 {
            f32::INFINITY
        } else {
            self.original_hidden_dim as f32 / self.final_hidden_dim as f32
        }
    }

    /// Get intermediate dimension compression ratio.
    #[must_use]
    pub fn intermediate_compression_ratio(&self) -> f32 {
        if self.final_intermediate_dim == 0 {
            f32::INFINITY
        } else {
            self.original_intermediate_dim as f32 / self.final_intermediate_dim as f32
        }
    }

    /// Get percentage of hidden channels removed.
    #[must_use]
    pub fn hidden_removal_percentage(&self) -> f32 {
        if self.original_hidden_dim == 0 {
            0.0
        } else {
            let removed = self.original_hidden_dim - self.final_hidden_dim;
            removed as f32 / self.original_hidden_dim as f32 * 100.0
        }
    }

    /// Get percentage of intermediate channels removed.
    #[must_use]
    pub fn intermediate_removal_percentage(&self) -> f32 {
        if self.original_intermediate_dim == 0 {
            0.0
        } else {
            let removed = self.original_intermediate_dim - self.final_intermediate_dim;
            removed as f32 / self.original_intermediate_dim as f32 * 100.0
        }
    }
}

/// Channel importance scores.
#[derive(Debug, Clone)]
pub struct ChannelImportance {
    /// Importance scores for hidden dimension channels.
    pub hidden: Tensor,
    /// Importance scores for intermediate (FFN) dimension channels.
    pub intermediate: Tensor,
    /// Number of calibration samples used.
    pub num_samples: usize,
}

impl ChannelImportance {
    /// Create new channel importance scores.
    #[must_use]
    pub fn new(hidden: Tensor, intermediate: Tensor, num_samples: usize) -> Self {
        Self {
            hidden,
            intermediate,
            num_samples,
        }
    }

    /// Get hidden dimension size.
    #[must_use]
    pub fn hidden_dim(&self) -> usize {
        self.hidden.data().len()
    }

    /// Get intermediate dimension size.
    #[must_use]
    pub fn intermediate_dim(&self) -> usize {
        self.intermediate.data().len()
    }

    /// Get top-k hidden channel indices by importance.
    #[must_use]
    pub fn top_hidden_channels(&self, k: usize) -> Vec<usize> {
        top_k_indices(self.hidden.data(), k)
    }

    /// Get top-k intermediate channel indices by importance.
    #[must_use]
    pub fn top_intermediate_channels(&self, k: usize) -> Vec<usize> {
        top_k_indices(self.intermediate.data(), k)
    }
}

/// Get indices of top-k elements by value.
fn top_k_indices(data: &[f32], k: usize) -> Vec<usize> {
    let mut indexed: Vec<(usize, f32)> = data.iter().copied().enumerate().collect();
    indexed.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

    let mut result: Vec<usize> = indexed.into_iter().take(k).map(|(idx, _)| idx).collect();
    result.sort_unstable(); // Return sorted indices
    result
}

/// Minitron Width Pruner: Channel removal based on activation importance.
///
/// # Algorithm
/// 1. Compute channel importance from calibration activations
/// 2. Select top-k channels to keep for each dimension type
/// 3. Ensure head divisibility constraint for attention
///
/// # Toyota Way: Poka-Yoke
/// Validates head divisibility to prevent invalid model configurations.
#[derive(Debug, Clone)]
pub struct WidthPruner {
    /// Target hidden dimension (must be divisible by `num_heads`).
    target_hidden_dim: usize,
    /// Target intermediate dimension for FFN.
    target_intermediate_dim: usize,
    /// Number of attention heads (for divisibility constraint).
    num_attention_heads: usize,
}

impl WidthPruner {
    /// Create a new width pruner.
    ///
    /// # Arguments
    /// * `target_hidden_dim` - Target hidden dimension (must be divisible by `num_heads`)
    /// * `target_intermediate_dim` - Target intermediate (FFN) dimension
    /// * `num_attention_heads` - Number of attention heads
    #[must_use]
    pub fn new(
        target_hidden_dim: usize,
        target_intermediate_dim: usize,
        num_attention_heads: usize,
    ) -> Self {
        Self {
            target_hidden_dim,
            target_intermediate_dim,
            num_attention_heads,
        }
    }

    /// Get target hidden dimension.
    #[must_use]
    pub fn target_hidden_dim(&self) -> usize {
        self.target_hidden_dim
    }

    /// Get target intermediate dimension.
    #[must_use]
    pub fn target_intermediate_dim(&self) -> usize {
        self.target_intermediate_dim
    }

    /// Get number of attention heads.
    #[must_use]
    pub fn num_attention_heads(&self) -> usize {
        self.num_attention_heads
    }

    /// Validate configuration.
    ///
    /// # Errors
    /// * If `target_hidden_dim` is not divisible by `num_attention_heads`
    /// * If target dimensions exceed original dimensions
    pub fn validate(
        &self,
        original_hidden_dim: usize,
        original_intermediate_dim: usize,
    ) -> Result<(), PruningError> {
        // Check head divisibility
        if !self
            .target_hidden_dim
            .is_multiple_of(self.num_attention_heads)
        {
            return Err(PruningError::InvalidPattern {
                message: format!(
                    "target_hidden_dim ({}) must be divisible by num_attention_heads ({})",
                    self.target_hidden_dim, self.num_attention_heads
                ),
            });
        }

        // Check target doesn't exceed original
        if self.target_hidden_dim > original_hidden_dim {
            return Err(PruningError::InvalidSparsity {
                value: self.target_hidden_dim as f32,
                constraint: format!(
                    "target_hidden_dim ({}) exceeds original ({})",
                    self.target_hidden_dim, original_hidden_dim
                ),
            });
        }

        if self.target_intermediate_dim > original_intermediate_dim {
            return Err(PruningError::InvalidSparsity {
                value: self.target_intermediate_dim as f32,
                constraint: format!(
                    "target_intermediate_dim ({}) exceeds original ({})",
                    self.target_intermediate_dim, original_intermediate_dim
                ),
            });
        }

        Ok(())
    }

    /// Compute channel importance from activation data.
    ///
    /// importance(d) = mean(|activations[:, d]|^2)
    ///
    /// # Arguments
    /// * `hidden_activations` - Hidden state activations [`num_samples`, `hidden_dim`]
    /// * `intermediate_activations` - FFN intermediate activations [`num_samples`, `intermediate_dim`]
    ///
    /// # Returns
    /// Channel importance scores
    pub fn compute_channel_importance(
        &self,
        hidden_activations: &Tensor,
        intermediate_activations: &Tensor,
    ) -> Result<ChannelImportance, PruningError> {
        let h_shape = hidden_activations.shape();
        let i_shape = intermediate_activations.shape();

        // Validate shapes
        if h_shape.len() != 2 {
            return Err(PruningError::ShapeMismatch {
                expected: vec![0, 0], // 2D expected
                got: h_shape.to_vec(),
            });
        }

        if i_shape.len() != 2 {
            return Err(PruningError::ShapeMismatch {
                expected: vec![0, 0],
                got: i_shape.to_vec(),
            });
        }

        // Validate consistent sample counts
        if h_shape[0] != i_shape[0] {
            return Err(PruningError::ShapeMismatch {
                expected: vec![h_shape[0], i_shape[1]],
                got: vec![i_shape[0], i_shape[1]],
            });
        }

        let num_samples = h_shape[0];
        let hidden_dim = h_shape[1];
        let intermediate_dim = i_shape[1];

        // Compute hidden importance: mean(x^2) per channel
        let h_data = hidden_activations.data();
        let mut hidden_importance = vec![0.0f32; hidden_dim];

        for sample in 0..num_samples {
            for d in 0..hidden_dim {
                let val = h_data[sample * hidden_dim + d];
                hidden_importance[d] += val * val;
            }
        }

        if num_samples > 0 {
            for imp in &mut hidden_importance {
                *imp /= num_samples as f32;
            }
        }

        // Compute intermediate importance
        let i_data = intermediate_activations.data();
        let mut intermediate_importance = vec![0.0f32; intermediate_dim];

        for sample in 0..num_samples {
            for d in 0..intermediate_dim {
                let val = i_data[sample * intermediate_dim + d];
                intermediate_importance[d] += val * val;
            }
        }

        if num_samples > 0 {
            for imp in &mut intermediate_importance {
                *imp /= num_samples as f32;
            }
        }

        Ok(ChannelImportance::new(
            Tensor::new(&hidden_importance, &[hidden_dim]),
            Tensor::new(&intermediate_importance, &[intermediate_dim]),
            num_samples,
        ))
    }

    /// Select channels to keep based on importance.
    ///
    /// # Arguments
    /// * `importance` - Channel importance scores
    ///
    /// # Returns
    /// Indices of channels to keep for hidden and intermediate dimensions
    pub fn select_channels_to_keep(
        &self,
        importance: &ChannelImportance,
    ) -> Result<(Vec<usize>, Vec<usize>), PruningError> {
        // Validate configuration
        self.validate(importance.hidden_dim(), importance.intermediate_dim())?;

        // Select top-k channels
        let hidden_keep = importance.top_hidden_channels(self.target_hidden_dim);
        let intermediate_keep = importance.top_intermediate_channels(self.target_intermediate_dim);

        Ok((hidden_keep, intermediate_keep))
    }

    /// Generate pruning mask for hidden dimension.
    ///
    /// # Arguments
    /// * `original_dim` - Original hidden dimension
    /// * `channels_to_keep` - Indices of channels to keep
    ///
    /// # Returns
    /// Binary mask tensor \[`original_dim`\]
    #[must_use]
    pub fn generate_hidden_mask(&self, original_dim: usize, channels_to_keep: &[usize]) -> Tensor {
        let mut mask = vec![0.0f32; original_dim];
        for &idx in channels_to_keep {
            if idx < original_dim {
                mask[idx] = 1.0;
            }
        }
        Tensor::new(&mask, &[original_dim])
    }

    /// Compute head dimension after pruning.
    #[must_use]
    pub fn head_dim_after_pruning(&self) -> usize {
        if self.num_attention_heads == 0 {
            0
        } else {
            self.target_hidden_dim / self.num_attention_heads
        }
    }
}

impl Default for WidthPruner {
    fn default() -> Self {
        Self::new(0, 0, 1)
    }
}

#[cfg(test)]
#[path = "width_tests.rs"]
mod tests;