示例依赖 fastai 的 vision_learner 和 dls,需自行准备 fastai 环境;量化需按目标平台选 x86 或 qnnpack 后端;README 未给出支持的最低 PyTorch 版本
fasterai
FasterAI-LabsApache-2.0活跃维护基于 FastAI 和 PyTorch 的模型压缩工具,提供剪枝、蒸馏和量化,帮你在边缘设备上跑更小更快的神经网络。
FasterAI: Prune and Distill your models with FastAI and PyTorch
基于 PyTorch 的模型压缩库,用几行代码把网络剪小、剪快
基于 FastAI 和 PyTorch 的模型压缩工具,提供剪枝、蒸馏和量化,帮你在边缘设备上跑更小更快的神经网络。
核心能力
- 用 SparsifyCallback 按稀疏度把权重置零
- 用 PruneCallback 从结构上移除零权重节点
- 用 KnowledgeDistillationCallback 做教师学生蒸馏
- 用 QuantizeCallback 支持静态、动态和 QAT 量化
- 稀疏与剪枝可选 weight、vector、kernel、filter 粒度
容易踩的坑
- v0.4.1fasterai 0.4.1最新正式版2026-09-16 · 2026-09-16 14:42 UTC
Bug Fixes
PruneCallbacknow re-pointslearn.optat the model's live parameters after every prune. Structured pruning replaces thenn.Parameterobjects of the layers it shrinks, and the optimizerLearner.fitbuilt still referenced the old ones: on a small convolutional model the optimizer held 1 of the model's 10 parameters after the first prune, and the pruned layers' weights did not change again for the rest of the fit (max |ΔW| = 0.0 over the following epoch). The optimizer object itself is kept and its parameter groups are rewritten in place, so the groups, the hypers and the state of this fit survive the prune, and fastai's no-weight-decay and force-train marks are re-applied. Frozen groups stay frozen: torch-pruning re-creates the parameters it replaces requiring grad, including those of a layer it touches only as a dependency of the one it prunes, so a frozen layer used to start training again after the first prune- The optimizer state of a parameter the prune replaced is now carried over instead of dropped: every state tensor shaped like the old parameter is sliced exactly as the parameter was (momentum, second moments), while scalars such as the step counter are kept, so the moments keep their history and the bias correction goes on. Every replacement torch-pruning performs goes through one function, which is wrapped for the duration of the prune to record the old parameter, the new one, the indices kept and the dimension pruned. No state key is left on a parameter no group holds, which made
learn.save()raise inside the wrapped optimizer'sstate_dict()
- v0.4.0fasterai 0.4.02026-09-11 · 2026-09-11 08:53 UTC
查看更新说明
0.4.0
New Features
FakeQuantizer(model, weight_bits=4, act_bits=8)rounds weights and activations onto the widths asked for in floating point, doing the arithmetic itself, so any width pair can be asked for on any model wherever that model already runs. It follows the shape of its siblings: the model at construction,qschemefor the scale axis (per_tensor,per_channel,per_group),symmetric,group_size,observer, the per-layerlayer_bitsandlayer_act_bitsdicts beside the defaults, mutation in place, andprint_precision()as the report.calibrate()observes the activation ranges and takes a fastaiDataLoadersor one of its loaders, refusing anything else by name;quantize_model()rounds in place and returns the same model;remove()puts the floating-point weights back bit for bit.Noneleaves a tensor in floating point,16is a real 16-bit grid, andobserver='dynamic'recomputes activation scales per batch instead of freezing them.FakeQuantSpecandfake_quant_spec(model)carry the provenance. The model keeps its floating-point dtype and its ordinary modules, so this measures what a width costs in accuracy, not what it saves: a floating-point copy of every rounded weight is held, biases stay in floating point where an integer kernel would carry them at int32, BatchNorm is not folded, and a rounded model is not a QAT model.Quantizer, the precision table and the ONNX exporter are untouched — this ships beside themFakeQuantizeCallbacktrains a model throughFakeQuantizer's rounding —learn.fit(5, cbs=[FakeQuantizeCallback(weight_bits=4, act_bits=8)])— taking the same grammar arguments as the quantizer it drives, plusfreeze_act_pct,act_averagingandmodel, and exposing that quantizer asself.fake_quantizer.before_fitregisters a weight parametrization on every layer with a width and installs the activation observers, so the weights are rounded at every forward and the straight-through gradient reaches the floating-point master the fastai optimizer already holds; activation ranges are observed with a moving average over training batches only and frozen atfreeze_act_pctof the fit or at its end;after_fitbakes the rounded weights into ordinary parameters, leaving the model in the statequantize_model()leaves, andbake()andstrip()are public so a fit that raised can still be finished or unwound. The model keeps its floating-point dtype: it is not smaller and not faster, and measured on ResNet-18 on one GPU, single run, the fit cost about 1.17× the step time and roughly 120 MiB more peak memory. While a parametrization is livem.weightis computed, so a write to it lands nowhere: six readers were corrected —Sparsifier's mask application, its lottery-ticket rewind, its weight snapshot and its sparsity report, and both criteria paths — andPrunerrefuses a parametrized model by nameFakeQuantizeCallback(weight_bits=4, weight_schedule=lin, weight_widths=(16, 8, 4))lowers the width the model trains at through a ladder of rungs, coarsest first, ending at the width the callback was already asked for. Any fasteraiScheduleselects the rung andcurrent_widthsreads back the pair the model is rounding at right now;act_scheduleandact_widthsdrive the activation side on their own timing, or not at all. The last training batch is detected explicitly rather than derived from progress, becausepct_trainis incremented after each batch and so never reaches 1.0 insidebefore_batch, andbake()sets the final rung too, so a fit cut short by an exception or aCancelFitdoes not bake a width the provenance does not name. A layer whose width was named bylayer_bitsorlayer_act_bitsis excluded from the ladder by name, resolved once atbefore_fit, rather than silently overwritten.FakeQuantSpecrecords the ladders that were asked for, not the rungs a given fit had time to visit, so a model that arrived at 4 bits th
- 0.3.0v0.3.02026-02-05 · 2026-02-05 19:07 UTC
查看更新说明
New Features
- Migrated to nbdev3 with
pyproject.toml(PEP 621) replacingsettings.ini - Renamed
analysismodule toanalyzefor verb-based naming consistency - Agnostic
Schedulesystem: schedules return progress (0→1), callbacks multiply by target - Per-layer sparsity targets via dict for
SparsifyCallbackandPruner - Sensitivity analysis module (
fasterai.analyze.sensitivity)
Enhancements
- Updated CI workflows to nbdev3 actions (
quarto-ghp3,nbdev3-ci) - Expanded PyPI keywords and classifiers
- Fixed repository URLs and metadata for FasterAI-Labs organization
- Migrated to nbdev3 with
- v0.2.52024-12-10 · 2024-12-10 09:23 UTC
这个版本没有写更新说明。
- v0.2.0New Pruner Capabilities2023-11-15 · 2023-11-15 16:23 UTC
查看更新说明
- Add Pruner and Pruning Callback based on this repo
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Overview
fasterAI is a PyTorch-based library that makes neural networks smaller, faster, and more efficient through state-of-the-art compression techniques. The library provides simple but powerful implementations of pruning, knowledge distillation, quantization, and other network optimization methods that can be applied with just a few lines of code.
Why compress your models with fasterai?
- Reduce model size by up to 90% with minimal accuracy loss
- Speed up inference for deployment on edge devices
- Lower energy consumption for more sustainable AI
- Simplify architectures while maintaining performance
Features
1. Sparsification
Make your model sparse by replacing selected weights with zeros using Sparsifier or SparsifyCallback.
| Parameter | Description | Options |
|---|---|---|
sparsity | Percentage of weights to zero out | 0-100% |
granularity | Level at which to apply sparsity | 'weight', 'vector', 'kernel', 'filter' |
context | Scope of sparsification | 'local' (per layer), 'global' (whole model) |
criteria | Method to select weights | 'magnitude', 'movement', 'gradient', etc. |
schedule | How sparsity evolves during training | 'one_shot', 'iterative', 'gradual', etc. |
2. Pruning
Remove zero-weight nodes from your network structure using Pruner or PruneCallback.
| Parameter | Description | Options |
|---|---|---|
pruning_ratio | Percentage of weights to remove | 0-100% |
context | Scope of sparsification | 'local' (per layer), 'global' (whole model) |
criteria | Method to select weights | 'magnitude', 'movement', 'gradient', etc. |
schedule | How sparsity evolves during training | 'one_shot', 'iterative', 'gradual', etc. |
3. Knowledge Distillation
Transfer knowledge from a large teacher to a smaller student using KnowledgeDistillationCallback.
| Parameter | Description | Options |
|---|---|---|
teacher | Teacher model | Any PyTorch model |
loss | Distillation loss function | 'SoftTarget', 'Logits', 'Attention', etc. |
activations_student | Student layers to match | Layer names as strings |
activations_teacher | Teacher layers to match | Layer names as strings |
weight | Balancing weight for distillation | 0.0-1.0 |
4. Regularization
Push weights toward zero during training using RegularizeCallback.
| Parameter | Description | Options |
|---|---|---|
criteria | Regularization criteria | Same as sparsification criteria |
granularity | Level of regularization | Same as sparsification granularity |
weight | Regularization strength | Floating point value |
schedule | How sparsity evolves during training | 'one_shot', 'iterative', 'gradual', etc. |
layer_types | Layer types to regularize | 'nn.Conv2d', 'nn.Linear', etc. |
5. Quantization
Reduce the precision of weights and activations using Quantizer or QuantizeCallback.
| Parameter | Description | Options |
|---|---|---|
backend | Target backend | 'x86', 'qnnpack' |
method | Quantization method | 'static', 'dynamic', 'qat' |
use_per_tensor | Force per-tensor quantization | True/False |
Quick Start
This is how easy it is to induce Sparsification in your PyTorch model:
from fasterai.sparse.all import *
learn = vision_learner(dls, model)
learn.fit_one_cycle(n_epochs, cbs=SparsifyCallback(sparsity, granularity, context, criteria, schedule))
Installation
pip install git+https://github.com/FasterAI-Labs/fasterai.git
or
pip install fasterai
Tutorials
- Get Started with FasterAI
- Create your own pruning schedule
- Find winning tickets using the Lottery Ticket Hypothesis
- Use Knowledge Distillation to help a student model to reach higher performance
- Sparsify Transformers
- Many more !
Join the community
Join our discord server to meet other FasterAI users and share your projects!
Citing
@software{Hubens,
author = {Nathan Hubens},
title = {fasterai},
year = 2022,
publisher = {Zenodo},
version = {v0.3.0},
doi = {10.5281/zenodo.6469868},
url = {https://doi.org/10.5281/zenodo.6469868}
}
License
Apache-2.0 License.