use crate::domain_mapper::DomainMap;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PruningMask {
pub zeroed: HashMap<usize, Vec<usize>>,
pub num_layers: usize,
}
impl PruningMask {
pub fn total_pruned(&self) -> usize {
self.zeroed.values().map(|v| v.len()).sum()
}
pub fn is_neuron_pruned(&self, layer: usize, neuron: usize) -> bool {
self.zeroed
.get(&layer)
.map(|v| v.binary_search(&neuron).is_ok())
.unwrap_or(false)
}
}
pub struct SurgicalPruner {
domains_to_drop: Vec<String>,
importance_threshold: f32,
}
impl SurgicalPruner {
pub fn new(domains_to_drop: Vec<String>) -> Self {
SurgicalPruner {
domains_to_drop,
importance_threshold: 0.05,
}
}
pub fn with_threshold(mut self, threshold: f32) -> Self {
self.importance_threshold = threshold;
self
}
pub fn build_mask(&self, domain_map: &DomainMap) -> Result<PruningMask> {
let mut zeroed: HashMap<usize, Vec<usize>> = HashMap::new();
for (layer_idx, layer_scores) in &domain_map.scores {
let mut pruned: Vec<usize> = layer_scores
.iter()
.filter(|s| {
self.domains_to_drop.contains(&s.domain)
&& s.importance >= self.importance_threshold
})
.map(|s| s.neuron_idx)
.collect();
pruned.sort_unstable();
pruned.dedup();
if !pruned.is_empty() {
zeroed.insert(*layer_idx, pruned);
}
}
Ok(PruningMask {
zeroed,
num_layers: domain_map.num_layers,
})
}
pub fn apply_to_weights(
mask: &PruningMask,
layer: usize,
weights: &mut [f32],
rows: usize,
cols: usize,
pruned_dim: PrunedDim,
) {
let Some(pruned_indices) = mask.zeroed.get(&layer) else {
return;
};
for &idx in pruned_indices {
match pruned_dim {
PrunedDim::Column => {
for row in 0..rows {
let flat = row * cols + idx;
if flat < weights.len() {
weights[flat] = 0.0;
}
}
}
PrunedDim::Row => {
let start = idx * cols;
let end = (start + cols).min(weights.len());
if start < weights.len() {
weights[start..end].fill(0.0);
}
}
}
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum PrunedDim {
Column,
Row,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain_mapper::DomainMapper;
#[test]
fn mask_zeros_target_domain() {
let mapper = DomainMapper::new(vec!["sql".to_string(), "python".to_string()]);
let domain_map = mapper.build(4, 32).unwrap();
let pruner = SurgicalPruner::new(vec!["sql".to_string()]);
let mask = pruner.build_mask(&domain_map).unwrap();
let total = mask.total_pruned();
assert!(total > 0, "expected some neurons pruned, got 0");
}
#[test]
fn apply_zeros_column() {
let mask = {
let mut m = PruningMask::default();
m.zeroed.insert(0, vec![1]); m.num_layers = 1;
m
};
let mut w = vec![1.0f32; 4 * 4]; SurgicalPruner::apply_to_weights(&mask, 0, &mut w, 4, 4, PrunedDim::Column);
assert_eq!(w[1], 0.0);
assert_eq!(w[5], 0.0);
assert_eq!(w[9], 0.0);
assert_eq!(w[13], 0.0);
assert_eq!(w[0], 1.0);
}
}