use heapless::Vec as HVec;
pub const MAX_PRUNING_UNITS: usize = 64;
#[derive(Debug, Clone, Copy)]
pub struct PruningConfig {
pub target_sparsity: f32,
pub importance_threshold: i8,
pub structured: bool,
pub gradual_steps: usize,
}
impl Default for PruningConfig {
fn default() -> Self {
Self {
target_sparsity: 0.5,
importance_threshold: 8,
structured: true,
gradual_steps: 0,
}
}
}
pub const MAX_MASK_WORDS: usize = 64;
#[derive(Debug, Clone)]
pub struct PruningMask<const N: usize> {
pub mask: HVec<u32, MAX_MASK_WORDS>,
pub size: usize,
pub pruned_count: usize,
}
impl<const N: usize> PruningMask<N> {
pub fn new(size: usize) -> crate::Result<Self> {
let num_words = (size + 31) / 32;
let mut mask = HVec::new();
for i in 0..num_words {
let bits = if i == num_words - 1 && size % 32 != 0 {
(1u32 << (size % 32)) - 1
} else {
u32::MAX
};
mask.push(bits).map_err(|_| crate::Error::BufferOverflow)?;
}
Ok(Self { mask, size, pruned_count: 0 })
}
#[inline]
pub fn is_kept(&self, idx: usize) -> bool {
let word = idx / 32;
let bit = idx % 32;
(self.mask.get(word).copied().unwrap_or(0) >> bit) & 1 == 1
}
pub fn prune(&mut self, idx: usize) {
if idx < self.size && self.is_kept(idx) {
let word = idx / 32;
let bit = idx % 32;
if let Some(w) = self.mask.get_mut(word) {
*w &= !(1 << bit);
self.pruned_count += 1;
}
}
}
pub fn sparsity(&self) -> f32 {
self.pruned_count as f32 / self.size as f32
}
}
pub struct LayerPruner {
config: PruningConfig,
importance_scores: HVec<i16, MAX_PRUNING_UNITS>,
current_step: usize,
}
impl LayerPruner {
pub fn new(config: PruningConfig) -> Self {
Self {
config,
importance_scores: HVec::new(),
current_step: 0,
}
}
pub fn compute_magnitude_importance(&mut self, weights: &[i8]) {
self.importance_scores.clear();
for &w in weights.iter().take(MAX_PRUNING_UNITS) {
let importance = (w as i16).abs();
let _ = self.importance_scores.push(importance);
}
}
pub fn compute_gradient_importance(&mut self, weights: &[i8], activations: &[i8]) {
self.importance_scores.clear();
for (&w, &a) in weights.iter().zip(activations.iter()).take(MAX_PRUNING_UNITS) {
let importance = ((w as i32 * a as i32).abs() >> 4) as i16;
let _ = self.importance_scores.push(importance);
}
}
pub fn create_mask<const N: usize>(&self, size: usize) -> crate::Result<PruningMask<N>> {
let mut mask = PruningMask::new(size)?;
let threshold = self.compute_threshold(size);
for (idx, &score) in self.importance_scores.iter().enumerate() {
if score < threshold {
mask.prune(idx);
}
}
Ok(mask)
}
fn compute_threshold(&self, size: usize) -> i16 {
let target_pruned = (size as f32 * self.config.target_sparsity) as usize;
if target_pruned == 0 || self.importance_scores.is_empty() {
return 0;
}
let mut sorted: HVec<i16, MAX_PRUNING_UNITS> = HVec::new();
for &s in &self.importance_scores {
let _ = sorted.push(s);
}
for i in 0..sorted.len() {
for j in 0..sorted.len() - 1 - i {
if sorted[j] > sorted[j + 1] {
sorted.swap(j, j + 1);
}
}
}
let idx = target_pruned.min(sorted.len().saturating_sub(1));
sorted.get(idx).copied().unwrap_or(0)
}
pub fn apply_mask<const N: usize>(&self, weights: &mut [i8], mask: &PruningMask<N>) {
for (idx, weight) in weights.iter_mut().enumerate() {
if !mask.is_kept(idx) {
*weight = 0;
}
}
}
pub fn prune_neurons(
&mut self,
weights: &mut [i8],
input_dim: usize,
output_dim: usize,
) -> HVec<bool, MAX_PRUNING_UNITS> {
let mut neuron_importance: HVec<i32, MAX_PRUNING_UNITS> = HVec::new();
for out_idx in 0..output_dim.min(MAX_PRUNING_UNITS) {
let mut l1_sum: i32 = 0;
for in_idx in 0..input_dim {
let w_idx = out_idx * input_dim + in_idx;
if w_idx < weights.len() {
l1_sum += (weights[w_idx] as i32).abs();
}
}
let _ = neuron_importance.push(l1_sum);
}
let target_pruned = (output_dim as f32 * self.config.target_sparsity) as usize;
let mut sorted: HVec<i32, MAX_PRUNING_UNITS> = neuron_importance.clone();
for i in 0..sorted.len() {
for j in 0..sorted.len() - 1 - i {
if sorted[j] > sorted[j + 1] {
sorted.swap(j, j + 1);
}
}
}
let threshold = sorted.get(target_pruned).copied().unwrap_or(0);
let mut keep_mask: HVec<bool, MAX_PRUNING_UNITS> = HVec::new();
for &importance in &neuron_importance {
let _ = keep_mask.push(importance >= threshold);
}
for out_idx in 0..output_dim.min(keep_mask.len()) {
if !keep_mask[out_idx] {
for in_idx in 0..input_dim {
let w_idx = out_idx * input_dim + in_idx;
if w_idx < weights.len() {
weights[w_idx] = 0;
}
}
}
}
keep_mask
}
pub fn pruning_stats<const N: usize>(&self, mask: &PruningMask<N>) -> PruningStats {
PruningStats {
total_weights: mask.size,
pruned_weights: mask.pruned_count,
sparsity: mask.sparsity(),
memory_saved: mask.pruned_count, }
}
}
#[derive(Debug, Clone)]
pub struct PruningStats {
pub total_weights: usize,
pub pruned_weights: usize,
pub sparsity: f32,
pub memory_saved: usize,
}
pub struct MinCutScorer {
input_flow: HVec<i32, MAX_PRUNING_UNITS>,
output_flow: HVec<i32, MAX_PRUNING_UNITS>,
}
impl MinCutScorer {
pub fn new() -> Self {
Self {
input_flow: HVec::new(),
output_flow: HVec::new(),
}
}
pub fn compute_edge_importance(
&mut self,
weights: &[i8],
input_dim: usize,
output_dim: usize,
) -> HVec<i16, MAX_PRUNING_UNITS> {
self.input_flow.clear();
self.output_flow.clear();
for in_idx in 0..input_dim.min(MAX_PRUNING_UNITS) {
let mut flow: i32 = 0;
for out_idx in 0..output_dim {
let w_idx = out_idx * input_dim + in_idx;
if w_idx < weights.len() {
flow += (weights[w_idx] as i32).abs();
}
}
let _ = self.input_flow.push(flow);
}
for out_idx in 0..output_dim.min(MAX_PRUNING_UNITS) {
let mut flow: i32 = 0;
for in_idx in 0..input_dim {
let w_idx = out_idx * input_dim + in_idx;
if w_idx < weights.len() {
flow += (weights[w_idx] as i32).abs();
}
}
let _ = self.output_flow.push(flow);
}
let mut importance: HVec<i16, MAX_PRUNING_UNITS> = HVec::new();
for out_idx in 0..output_dim.min(self.output_flow.len()) {
let out_flow = self.output_flow[out_idx];
for in_idx in 0..input_dim.min(self.input_flow.len()) {
let in_flow = self.input_flow[in_idx];
let w_idx = out_idx * input_dim + in_idx;
if w_idx < weights.len() {
let w = (weights[w_idx] as i32).abs();
let bottleneck = in_flow.min(out_flow);
let edge_importance = ((w * bottleneck) >> 10) as i16;
if importance.len() < MAX_PRUNING_UNITS {
let _ = importance.push(edge_importance);
}
}
}
}
importance
}
}
impl Default for MinCutScorer {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pruning_mask() {
let mut mask = PruningMask::<64>::new(50).unwrap();
assert!(mask.is_kept(0));
assert!(mask.is_kept(49));
assert_eq!(mask.sparsity(), 0.0);
mask.prune(10);
mask.prune(20);
assert!(!mask.is_kept(10));
assert!(!mask.is_kept(20));
assert!(mask.is_kept(15));
assert_eq!(mask.pruned_count, 2);
}
#[test]
fn test_magnitude_pruning() {
let config = PruningConfig {
target_sparsity: 0.5,
..Default::default()
};
let mut pruner = LayerPruner::new(config);
let weights: [i8; 8] = [1, -2, 50, -60, 3, -4, 70, 5];
pruner.compute_magnitude_importance(&weights);
let mask = pruner.create_mask::<8>(8).unwrap();
assert!(mask.sparsity() >= 0.25 && mask.sparsity() <= 0.75);
assert!(mask.is_kept(2)); assert!(mask.is_kept(3)); assert!(mask.is_kept(6)); }
#[test]
fn test_structured_pruning() {
let config = PruningConfig {
target_sparsity: 0.5,
structured: true,
..Default::default()
};
let mut pruner = LayerPruner::new(config);
let mut weights: [i8; 16] = [
10, 10, 10, 10, 1, 1, 1, 1, 20, 20, 20, 20, 2, 2, 2, 2, ];
let keep_mask = pruner.prune_neurons(&mut weights, 4, 4);
assert!(keep_mask[0]); assert!(keep_mask[2]);
if !keep_mask[1] {
assert_eq!(weights[4], 0);
assert_eq!(weights[5], 0);
}
}
#[test]
fn test_mincut_scorer() {
let mut scorer = MinCutScorer::new();
let weights: [i8; 9] = [
10, 20, 30,
5, 10, 15,
1, 2, 3,
];
let importance = scorer.compute_edge_importance(&weights, 3, 3);
assert!(!importance.is_empty());
}
}