Instructions to use Angshul/higher-order-pruner with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Angshul/higher-order-pruner with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Angshul/higher-order-pruner", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download tests/test_core.py from Angshul/higher-order-pruner: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/Angshul/higher-order-pruner/resolve/main/tests/test_core.py
- Command line
-
hf download hf://Angshul/higher-order-pruner/tests/test_core.py
-
curl -L -o test_core.py https://huggingface.co/Angshul/higher-order-pruner/resolve/main/tests/test_core.py
1.1 kB
| """Tiny CPU smoke test; run with pytest after installing requirements.""" | |
| import torch | |
| from torch import nn | |
| from higher_order_pruner import PruneConfig, discover_linear_groups, prune | |
| class TinyNet(nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| self.layers = nn.ModuleList([nn.Linear(5, 5), nn.Linear(5, 5)]) | |
| def forward(self, x): | |
| return self.layers[1](self.layers[0](x).tanh()) | |
| def test_exact_budget_and_general_loss(): | |
| torch.manual_seed(17) | |
| model = TinyNet() | |
| batches = [(torch.randn(4, 5), torch.randn(4, 5)) for _ in range(3)] | |
| def mse(m, batch): | |
| x, y = batch | |
| return (m(x) - y).square().mean() | |
| assert len(discover_linear_groups(model)) == 2 | |
| report = prune(model, batches[:2], mse, PruneConfig(sparsity=0.3, probes=1), | |
| validation_batches=batches[2:]) | |
| assert report.prunable_weights == 50 | |
| assert report.exact_zero_weights == 15 | |
| assert sum(int((p.weight == 0).sum()) for p in model.layers) == 15 | |
| assert all(row["loss_after"] <= row["loss_before"] + 1e-5 for row in report.layers) | |