fasterai

FasterAI-LabsApache-2.0活跃维护

基于 FastAI 和 PyTorch 的模型压缩工具,提供剪枝、蒸馏和量化,帮你在边缘设备上跑更小更快的神经网络。

FasterAI: Prune and Distill your models with FastAI and PyTorch

v0.4.1发布于 2026-09-16 14:42 UTC270star20fork同步于 2026-11-07 07:30 UTC

怎么上手

由 README 提炼,执行前请确认
pip install fasterai
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

基于 PyTorch 的模型压缩库,用几行代码把网络剪小、剪快

基于 FastAI 和 PyTorch 的模型压缩工具,提供剪枝、蒸馏和量化,帮你在边缘设备上跑更小更快的神经网络。

核心能力

  • 用 SparsifyCallback 按稀疏度把权重置零
  • 用 PruneCallback 从结构上移除零权重节点
  • 用 KnowledgeDistillationCallback 做教师学生蒸馏
  • 用 QuantizeCallback 支持静态、动态和 QAT 量化
  • 稀疏与剪枝可选 weight、vector、kernel、filter 粒度

容易踩的坑

示例依赖 fastai 的 vision_learner 和 dls,需自行准备 fastai 环境;量化需按目标平台选 x86 或 qnnpack 后端;README 未给出支持的最低 PyTorch 版本

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v0.4.1
  1. v0.4.1fasterai 0.4.1最新正式版2026-09-16 · 2026-09-16 14:42 UTC

    Bug Fixes

    • PruneCallback now re-points learn.opt at the model's live parameters after every prune. Structured pruning replaces the nn.Parameter objects of the layers it shrinks, and the optimizer Learner.fit built 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's state_dict()
  2. 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, qscheme for the scale axis (per_tensor, per_channel, per_group), symmetric, group_size, observer, the per-layer layer_bits and layer_act_bits dicts beside the defaults, mutation in place, and print_precision() as the report. calibrate() observes the activation ranges and takes a fastai DataLoaders or 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. None leaves a tensor in floating point, 16 is a real 16-bit grid, and observer='dynamic' recomputes activation scales per batch instead of freezing them. FakeQuantSpec and fake_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 them
    • FakeQuantizeCallback trains a model through FakeQuantizer's rounding — learn.fit(5, cbs=[FakeQuantizeCallback(weight_bits=4, act_bits=8)]) — taking the same grammar arguments as the quantizer it drives, plus freeze_act_pct, act_averaging and model, and exposing that quantizer as self.fake_quantizer. before_fit registers 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 at freeze_act_pct of the fit or at its end; after_fit bakes the rounded weights into ordinary parameters, leaving the model in the state quantize_model() leaves, and bake() and strip() 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 live m.weight is 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 — and Pruner refuses a parametrized model by name
    • FakeQuantizeCallback(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 fasterai Schedule selects the rung and current_widths reads back the pair the model is rounding at right now; act_schedule and act_widths drive the activation side on their own timing, or not at all. The last training batch is detected explicitly rather than derived from progress, because pct_train is incremented after each batch and so never reaches 1.0 inside before_batch, and bake() sets the final rung too, so a fit cut short by an exception or a CancelFit does not bake a width the provenance does not name. A layer whose width was named by layer_bits or layer_act_bits is excluded from the ladder by name, resolved once at before_fit, rather than silently overwritten. FakeQuantSpec records 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
  3. 0.3.0v0.3.02026-02-05 · 2026-02-05 19:07 UTC
    查看更新说明

    New Features

    • Migrated to nbdev3 with pyproject.toml (PEP 621) replacing settings.ini
    • Renamed analysis module to analyze for verb-based naming consistency
    • Agnostic Schedule system: schedules return progress (0→1), callbacks multiply by target
    • Per-layer sparsity targets via dict for SparsifyCallback and Pruner
    • 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
  4. v0.2.52024-12-10 · 2024-12-10 09:23 UTC

    这个版本没有写更新说明。

  5. v0.2.0New Pruner Capabilities2023-11-15 · 2023-11-15 16:23 UTC
    查看更新说明
    • Add Pruner and Pruning Callback based on this repo

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    header

    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.

    ParameterDescriptionOptions
    sparsityPercentage of weights to zero out0-100%
    granularityLevel at which to apply sparsity'weight', 'vector', 'kernel', 'filter'
    contextScope of sparsification'local' (per layer), 'global' (whole model)
    criteriaMethod to select weights'magnitude', 'movement', 'gradient', etc.
    scheduleHow sparsity evolves during training'one_shot', 'iterative', 'gradual', etc.

    2. Pruning

    Remove zero-weight nodes from your network structure using Pruner or PruneCallback.

    ParameterDescriptionOptions
    pruning_ratioPercentage of weights to remove0-100%
    contextScope of sparsification'local' (per layer), 'global' (whole model)
    criteriaMethod to select weights'magnitude', 'movement', 'gradient', etc.
    scheduleHow sparsity evolves during training'one_shot', 'iterative', 'gradual', etc.

    3. Knowledge Distillation

    Transfer knowledge from a large teacher to a smaller student using KnowledgeDistillationCallback.

    ParameterDescriptionOptions
    teacherTeacher modelAny PyTorch model
    lossDistillation loss function'SoftTarget', 'Logits', 'Attention', etc.
    activations_studentStudent layers to matchLayer names as strings
    activations_teacherTeacher layers to matchLayer names as strings
    weightBalancing weight for distillation0.0-1.0

    4. Regularization

    Push weights toward zero during training using RegularizeCallback.

    ParameterDescriptionOptions
    criteriaRegularization criteriaSame as sparsification criteria
    granularityLevel of regularizationSame as sparsification granularity
    weightRegularization strengthFloating point value
    scheduleHow sparsity evolves during training'one_shot', 'iterative', 'gradual', etc.
    layer_typesLayer types to regularize'nn.Conv2d', 'nn.Linear', etc.

    5. Quantization

    Reduce the precision of weights and activations using Quantizer or QuantizeCallback.

    ParameterDescriptionOptions
    backendTarget backend'x86', 'qnnpack'
    methodQuantization method'static', 'dynamic', 'qat'
    use_per_tensorForce per-tensor quantizationTrue/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


    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.

    footer