|
| 1 | +# Mamba-Shedder |
| 2 | + |
| 3 | +Official implementation of [Mamba-Shedder: Post-Transformer Compression for Efficient Selective Structured State Space Models](). |
| 4 | + |
| 5 | +This repo contains the code for Mamba-Shedder, which explores the compression of the new Mamba-series architectures (and their hybrids). |
| 6 | +We study the sensitivity of these models to the removal of selected components at different granularities to reduce model size and computational overhead, thereby improving their efficiency while maintaining accuracy. |
| 7 | +Please refer to our paper for more details. |
| 8 | + |
| 9 | +## News |
| 10 | +- **[2025.01.23]** Support for the new hybrid architecture model **Hymba**, please refer to [Hymba-Pruning](./hybrid/Hymba-Pruning). |
| 11 | +- **[2025.01.23]** Support Zamba2 ([Zamba2-Pruning](./hybrid/Zamba2-Pruning)). |
| 12 | +- **[2025.01.22]** Release the code for **Mamba-Shedder**. :tada: |
| 13 | + |
| 14 | +## Released Pruned Models 🤗 |
| 15 | + |
| 16 | +Compressed models by Mamba-Shedder: |
| 17 | + |
| 18 | +| Source Model | Components Removed | Recovery Tuning | Relative Acc. | Pruned Model Link | Inference Speedup | |
| 19 | +|--------------------------------------------------------------------|--------------------|-----------------|---------------|----------------------------------------------------------------------------------------|-------------------| |
| 20 | +| [Hymba-1.5B-Base](https://huggingface.co/nvidia/Hymba-1.5B-Base) | 7 Hymba Blocks | ✘ | 97% | [Link]() | ~1.2x | |
| 21 | +| [Hymba-1.5B-Base](https://huggingface.co/nvidia/Hymba-1.5B-Base) | 7 Hymba Blocks | ✔ | 99% | [Link]() | ~1.2x | |
| 22 | +| [mamba-2.8b](https://huggingface.co/state-spaces/mamba-2.8b) | 14 Mamba Blocks | ✘ | 90% | [Link]() | ~1.3x | |
| 23 | +| [mamba2-2.7b](https://huggingface.co/state-spaces/mamba2-2.7b) | 22 SSMs | ✘ | 96% | [Link]() | ~1.2x | |
| 24 | +| [mamba2-2.7b](https://huggingface.co/state-spaces/mamba2-2.7b) | 22 SSMs | ✔ | 99% | [Link]() | ~1.2x | |
| 25 | + |
| 26 | +## Setup |
| 27 | + |
| 28 | +Use the following instructions to create a virtual environment with the required dependencies. |
| 29 | + |
| 30 | +``` |
| 31 | +# install dependencies |
| 32 | +bash install.sh |
| 33 | +``` |
| 34 | + |
| 35 | +## Run |
| 36 | + |
| 37 | +### Evaluation before Pruning |
| 38 | + |
| 39 | +```bash |
| 40 | +python eval.py --model_path <path to mamba model> |
| 41 | +``` |
| 42 | + |
| 43 | +### Prune |
| 44 | + |
| 45 | +#### Mamba Block Pruning |
| 46 | + |
| 47 | +An example command for [mamba-2.8b](https://huggingface.co/state-spaces/mamba-2.8b) with Mamba Block Pruning: |
| 48 | + |
| 49 | +```bash |
| 50 | +python prune.py \ |
| 51 | + --model_path state-spaces/mamba-2.8b \ |
| 52 | + --do_prune \ |
| 53 | + --output_path <path to pruning results> \ |
| 54 | + --prune_target mamba_block \ |
| 55 | + --target_pruning_steps 10 \ |
| 56 | + --importance_metric ppl \ |
| 57 | + --calibration_dataset alpaca \ |
| 58 | + --num_calibration_samples 256 \ |
| 59 | + --do_eval |
| 60 | +``` |
| 61 | + |
| 62 | +- `model_path`: Path to the pre-trained Mamba model. |
| 63 | +- `do_prune`: Flag to indicate whether to perform pruning. |
| 64 | +- `output_path`: Directory to save the pruning and evaluation results. |
| 65 | +- `prune_target`: "mamba_block" or "ssm". |
| 66 | +- `target_pruning_steps`: Number of pruning target modules (mamba blocks or SSMs). |
| 67 | +- `importance_metric`: Metric for calculating block importance, currently only supports PPL. |
| 68 | +- `calibration_dataset`: Calibration dataset name ("alpaca", "c4", "ptb" or "wikitext2"). |
| 69 | +- `num_calibration_samples`: Number of calibration samples for pruning. |
| 70 | +- `do_eval`: Flag to indicate whether to perform evaluation. |
| 71 | + |
| 72 | +#### SSM Pruning |
| 73 | + |
| 74 | +An example command for [mamba2-2.7b](https://huggingface.co/state-spaces/mamba2-2.7b) with SSM Pruning: |
| 75 | + |
| 76 | +```bash |
| 77 | +python prune.py \ |
| 78 | + --model_path state-spaces/mamba2-2.7b \ |
| 79 | + --do_prune \ |
| 80 | + --output_path <path to pruning results> \ |
| 81 | + --prune_target ssm \ |
| 82 | + --target_pruning_steps 20 \ |
| 83 | + --importance_metric ppl \ |
| 84 | + --calibration_dataset alpaca \ |
| 85 | + --num_calibration_samples 256 \ |
| 86 | + --do_eval |
| 87 | +``` |
| 88 | + |
| 89 | +### Extract the Pruned Model |
| 90 | + |
| 91 | +Extract the pruned model based on the optimal pruning configuration obtained from Mamba-Shedder. |
| 92 | +For more details, please refer to [here](./extract). |
| 93 | +Here is an example to extract a pruned [mamba2-2.7b](https://huggingface.co/state-spaces/mamba2-2.7b): |
| 94 | + |
| 95 | +```bash |
| 96 | +python extract/extract_mamba.py \ |
| 97 | + --model_path state-spaces/mamba2-2.7b \ |
| 98 | + --pruned_model_config_file <path to pruning results>/pruning_config.json \ |
| 99 | + --output_path <path to compressed model> |
| 100 | +``` |
| 101 | + |
| 102 | +### Recovery Fine-tuning |
| 103 | + |
| 104 | +After we have obtained the pruned model, we can use [Alpaca](https://huggingface.co/datasets/yahma/alpaca-cleaned) dataset for recovery fine-tuning: |
| 105 | + |
| 106 | +```bash |
| 107 | +# Finetune the compressed Mamba-2 |
| 108 | +python recovery/finetune_mamba.py \ |
| 109 | + --model_path <path to compressed model> \ |
| 110 | + --do_train \ |
| 111 | + --batch_size 32 \ |
| 112 | + --gradient_accumulation_steps 1 \ |
| 113 | + --num_train_epochs 1 \ |
| 114 | + --learning_rate 5e-5 \ |
| 115 | + --output_path <path to trained model> \ |
| 116 | + --do_eval |
| 117 | +``` |
| 118 | + |
| 119 | +## Results |
| 120 | + |
| 121 | +All run commands and pruning results can be found in [here](./results). |
| 122 | + |
| 123 | +### Loading the compressed model for evaluation |
| 124 | + |
| 125 | +```bash |
| 126 | +python eval.py --model_path <path to compressed model> |
| 127 | +``` |
| 128 | + |
| 129 | +## Citation |
| 130 | +If you find Mamba-Shedder's code and paper helpful, please kindly cite: |
| 131 | +```bibtex |
| 132 | +@article{munoz2025mambashedder, |
| 133 | + title = {Mamba-Shedder: Post-Transformer Compression for Efficient Selective Structured State Space Models}, |
| 134 | + author = {J. Pablo Munoz and Jinjie Yuan and Nilesh Jain}, |
| 135 | + journal = {}, |
| 136 | + year = {2025} |
| 137 | +} |
| 138 | +``` |
0 commit comments