|
| 1 | +# 🚀 OptiPFair v0.2.2 - Selective Layer Width Pruning |
| 2 | + |
| 3 | +We're excited to announce **OptiPFair v0.2.2**, bringing powerful new capabilities for fine-grained control over model pruning! |
| 4 | + |
| 5 | +## 🎯 Headline Features |
| 6 | + |
| 7 | +### 1️⃣ Selective Layer Width Pruning |
| 8 | + |
| 9 | +The `layer_indices` parameter now works for **both DEPTH and MLP_GLU pruning**, giving you unprecedented control over which layers to optimize: |
| 10 | + |
| 11 | +```python |
| 12 | +from optipfair import prune_model |
| 13 | + |
| 14 | +# Prune neurons ONLY in specific layers (preserve first & last) |
| 15 | +pruned_model = prune_model( |
| 16 | + model=model, |
| 17 | + pruning_type="MLP_GLU", |
| 18 | + pruning_percentage=30, |
| 19 | + layer_indices=[5, 10, 15, 20], # Only these layers are pruned |
| 20 | + show_progress=True |
| 21 | +) |
| 22 | +``` |
| 23 | + |
| 24 | +**Key Benefits:** |
| 25 | +- 🛡️ **Preserve Critical Layers**: Keep embedding and output layers at full capacity |
| 26 | +- 🎯 **Targeted Optimization**: Prune only the layers that matter |
| 27 | +- 🔬 **Data-Driven Selection**: Combine with layer importance analysis |
| 28 | +- ⚡ **Full Feature Support**: Works with expansion_rate, expansion_divisor, dataloader, all methods |
| 29 | + |
| 30 | +### 2️⃣ Optimized Hybrid Importance Calculation |
| 31 | + |
| 32 | +We've streamlined the data-driven pruning algorithm for better performance: |
| 33 | + |
| 34 | +- **Simplified gate_proj & up_proj**: Now use the same fast MAW method as static pruning |
| 35 | +- **Focused Complexity**: Activation-weighted calculation only where it matters (down_proj) |
| 36 | +- **Faster Execution**: Reduced computational overhead while maintaining effectiveness |
| 37 | +- **Consistent Methodology**: Same MAW formula across static and hybrid approaches |
| 38 | + |
| 39 | +## 📊 What's New |
| 40 | + |
| 41 | +### Extended API |
| 42 | +- ✅ `layer_indices` parameter now contextual: removes layers for DEPTH, prunes neurons for MLP_GLU |
| 43 | +- ✅ Comprehensive validation: checks for valid indices, duplicates, empty lists, type errors |
| 44 | +- ✅ Enhanced statistics: reports `pruned_layers` and `total_layers` for selective pruning |
| 45 | + |
| 46 | +### Improved Performance |
| 47 | +- ⚡ Faster hybrid importance calculation |
| 48 | +- 💾 Selective hook registration (only on specified layers) |
| 49 | +- 🎯 More efficient calibration with layer_indices |
| 50 | + |
| 51 | +### Better Documentation |
| 52 | +- 📖 Complete "Selective Layer Width Pruning" guide in README |
| 53 | +- 📝 Extended reference manual with 4+ detailed examples |
| 54 | +- 💻 New example file with 5 practical use cases |
| 55 | +- 🧪 12 comprehensive test cases |
| 56 | + |
| 57 | +## 💡 Common Use Cases |
| 58 | + |
| 59 | +### Use Case 1: Preserve Embedding Layers |
| 60 | +```python |
| 61 | +# Prune all middle layers, preserve first and last 5 |
| 62 | +num_layers = len(model.model.layers) |
| 63 | +middle_layers = list(range(5, num_layers - 5)) |
| 64 | + |
| 65 | +pruned_model = prune_model( |
| 66 | + model=model, |
| 67 | + pruning_type="MLP_GLU", |
| 68 | + pruning_percentage=25, |
| 69 | + layer_indices=middle_layers |
| 70 | +) |
| 71 | +``` |
| 72 | + |
| 73 | +### Use Case 2: Importance-Based Pruning |
| 74 | +```python |
| 75 | +from optipfair import analyze_layer_importance |
| 76 | + |
| 77 | +# Step 1: Analyze which layers are least important |
| 78 | +importance_scores = analyze_layer_importance(model, dataloader) |
| 79 | +sorted_layers = sorted(importance_scores.items(), key=lambda x: x[1]) |
| 80 | +least_important = [idx for idx, score in sorted_layers[:10]] |
| 81 | + |
| 82 | +# Step 2: Prune only those layers |
| 83 | +pruned_model = prune_model( |
| 84 | + model=model, |
| 85 | + pruning_type="MLP_GLU", |
| 86 | + pruning_percentage=30, |
| 87 | + layer_indices=least_important |
| 88 | +) |
| 89 | +``` |
| 90 | + |
| 91 | +### Use Case 3: Data-Driven Selective Pruning |
| 92 | +```python |
| 93 | +# Combine calibration data with selective pruning |
| 94 | +pruned_model = prune_model( |
| 95 | + model=model, |
| 96 | + pruning_type="MLP_GLU", |
| 97 | + neuron_selection_method="MAW", |
| 98 | + pruning_percentage=20, |
| 99 | + dataloader=calibration_dataloader, # Hybrid importance |
| 100 | + layer_indices=[5, 10, 15, 20], # Only these layers |
| 101 | + show_progress=True |
| 102 | +) |
| 103 | +``` |
| 104 | + |
| 105 | +## 🔧 Technical Highlights |
| 106 | + |
| 107 | +### Modified Core Functions |
| 108 | +- `prune_model()`: Now passes layer_indices to MLP_GLU pruning |
| 109 | +- `prune_model_mlp_glu()`: Full selective pruning implementation with validation |
| 110 | +- `setup_mlp_hooks_for_importance()`: Selective hook registration |
| 111 | +- `compute_neuron_pair_importance_maw_hybrid()`: Simplified and optimized |
| 112 | +- `get_pruning_statistics()`: Detects and reports selective pruning |
| 113 | + |
| 114 | +### Enhanced CLI |
| 115 | +```bash |
| 116 | +# CLI now supports layer_indices for both pruning types |
| 117 | +optipfair prune \ |
| 118 | + --model-path meta-llama/Llama-3.2-1B \ |
| 119 | + --pruning-type MLP_GLU \ |
| 120 | + --pruning-percentage 30 \ |
| 121 | + --layer-indices "5,10,15,20" \ |
| 122 | + --output-path ./pruned-model |
| 123 | +``` |
| 124 | + |
| 125 | +## 🧪 Testing & Validation |
| 126 | + |
| 127 | +- ✅ 12 comprehensive test cases in `tests/test_selective_layer_pruning.py` |
| 128 | +- ✅ Tested with all neuron selection methods (MAW, VOW, PON) |
| 129 | +- ✅ Verified compatibility with expansion_rate, expansion_divisor, dataloader |
| 130 | +- ✅ Validated error handling and edge cases |
| 131 | +- ✅ Confirmed backward compatibility with v0.2.1 |
| 132 | + |
| 133 | +## 📦 Installation |
| 134 | + |
| 135 | +```bash |
| 136 | +pip install --upgrade optipfair |
| 137 | +``` |
| 138 | + |
| 139 | +Or with visualization support: |
| 140 | +```bash |
| 141 | +pip install --upgrade "optipfair[viz]" |
| 142 | +``` |
| 143 | + |
| 144 | +## 📚 Resources |
| 145 | + |
| 146 | +- **Documentation**: [https://peremartra.github.io/optipfair/](https://peremartra.github.io/optipfair/) |
| 147 | +- **GitHub**: [https://github.com/peremartra/optipfair](https://github.com/peremartra/optipfair) |
| 148 | +- **Examples**: Check out `examples/selective_layer_width_pruning.py` |
| 149 | +- **Tests**: See `tests/test_selective_layer_pruning.py` |
| 150 | + |
| 151 | +## 🙏 Acknowledgments |
| 152 | + |
| 153 | +Thank you to our community for the feedback and suggestions that made this release possible! |
| 154 | + |
| 155 | +## 📝 Full Changelog |
| 156 | + |
| 157 | +See [CHANGELOG.md](https://github.com/peremartra/optipfair/blob/main/CHANGELOG.md) for detailed changes. |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +**Upgrade today and take control of your model optimization!** 🚀 |
| 162 | + |
| 163 | +Questions or issues? Open an issue on [GitHub](https://github.com/peremartra/optipfair/issues). |
0 commit comments