Runtime compression tuning utilities for PyTorch
Project description
rasnatune
rasnatune は、ハードウェア寄りの量子化やスパース化を PyTorch モデルで学習・検証するための軽量ツールキットです。
既存モデルに一時的な forward hook を直接取り付けるため、モデルオブジェクトそのものや state_dict() のキーは変わりません。
インストール
pip install rasnatune
クイックスタート
import torch
import torchvision
import rasnatune
from rasnatune import QuantizeIntermidiate, SparseWeight, skip_first_layer
model = torchvision.models.resnet50()
rasnatune.apply(
model,
SparseWeight(sparsity=0.5),
targets=[torch.nn.Conv2d],
filters=[skip_first_layer],
)
rasnatune.apply(
model,
QuantizeIntermidiate(ab=8, wb=8, ib=17),
targets=[torch.nn.Conv2d, torch.nn.Linear],
filters=[],
)
# 以後は通常の学習を行います。
rasnatune.apply(
model,
SparseWeight(sparsity=0.8),
targets=[torch.nn.Conv2d],
filters=[skip_first_layer],
)
Apply
apply() は設定済み compressor インスタンスをモデルへ適用し、同じモデルインスタンスを返します。
渡された compressor は対象 module ごとに内部で複製されます。
from rasnatune import apply, QuantizeIntermidiate, SparseWeight, skip_first_layer
apply(
model,
QuantizeIntermidiate(ab=8, wb=8, ib=17),
targets=[torch.nn.Conv2d, torch.nn.Linear],
filters=[],
)
apply(
model,
SparseWeight(sparsity=0.5),
targets=[torch.nn.Conv2d],
filters=[skip_first_layer],
)
targets= には module 型の list / tuple を渡します。
filters= には predicate を渡せます。
skip_first_layer は、targets= で選ばれた最初の module を除外します。
apply(
model,
QuantizeIntermidiate(ab=8, wb=8, ib=17),
targets=[torch.nn.Linear],
filters=[],
)
同じ module に同じ種類の compressor を再適用すると、古い compressor は detach され、新しい compressor に差し替わります。
例えば学習途中で SparseWeight(sparsity=0.5) 相当から sparsity=0.8 へ変更すると、該当 Conv2d のスパース率だけが更新されます。
SparseWeight と QuantizeIntermidiate は別種類なので、同じ module 上で共存できます。
公開 API
トップレベル API:
rasnatune.apply(model, compressor, targets=..., filters=...)rasnatune.remove_compression(model)rasnatune.compression_count(model)rasnatune.skip_first_layer
compressor クラス:
rasnatune.Compressorrasnatune.QuantizeWeightrasnatune.QuantizeActivationrasnatune.QuantizeIntermidiaterasnatune.SparseWeightrasnatune.SparseActivation
AWI 量子化シミュレーション
QuantizeIntermidiate は、実際の PyTorch 実行は FP32 のまま保ちつつ、Activation / Weight / Intermediate の量子化挙動をシミュレートします。
- activation と weight は straight-through gradient 付きで量子化されます
- weight のスパース化は
SparseWeightを同じ module に重ねて表現します Linear/Conv2dの演算自体は FP32 で実行されます- layer の最終出力に signed intermediate 幅での wrap-around を適用します
- 同じ layer 内では出力 activation の再量子化は行いません
例えば apply(model, QuantizeIntermidiate(ab=8, wb=8, ib=17), targets=[torch.nn.Linear], filters=[]) は、signed 8-bit の入力・重みを使い、最終的な intermediate 出力に signed 17-bit の wrap-around を適用します。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rasnatune-0.0.3.tar.gz.
File metadata
- Download URL: rasnatune-0.0.3.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9cee19e1006ba5bc0cdce1cedb458670e3e63f1f8a4e2c6da87e4bd7e24e76ab
|
|
| MD5 |
f91836a0f0acaca039a64566738a1067
|
|
| BLAKE2b-256 |
9d2c6e607fdd510cc891f6432698e2039664d0de7b48ec9de4c3bd10ac75f715
|
Provenance
The following attestation bundles were made for rasnatune-0.0.3.tar.gz:
Publisher:
rasnatune-pypi-publish.yml on spiceengine/rasnatune
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rasnatune-0.0.3.tar.gz -
Subject digest:
9cee19e1006ba5bc0cdce1cedb458670e3e63f1f8a4e2c6da87e4bd7e24e76ab - Sigstore transparency entry: 1594900776
- Sigstore integration time:
-
Permalink:
spiceengine/rasnatune@0f01fb72a6e042ca7ce70b81019d1769ee6686c1 -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/spiceengine
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
rasnatune-pypi-publish.yml@0f01fb72a6e042ca7ce70b81019d1769ee6686c1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file rasnatune-0.0.3-py3-none-any.whl.
File metadata
- Download URL: rasnatune-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4323c2525c7fa69e5a258edb3ed76e92175790111cbca4f8c411811ff780942c
|
|
| MD5 |
67c401a1cd40cfb50ba7ecd7f7efbfd6
|
|
| BLAKE2b-256 |
0a227b96ed92dce6f12789cd223bbf57fd969f1e0652f122a4a3193b2b2c3b24
|
Provenance
The following attestation bundles were made for rasnatune-0.0.3-py3-none-any.whl:
Publisher:
rasnatune-pypi-publish.yml on spiceengine/rasnatune
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rasnatune-0.0.3-py3-none-any.whl -
Subject digest:
4323c2525c7fa69e5a258edb3ed76e92175790111cbca4f8c411811ff780942c - Sigstore transparency entry: 1594900816
- Sigstore integration time:
-
Permalink:
spiceengine/rasnatune@0f01fb72a6e042ca7ce70b81019d1769ee6686c1 -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/spiceengine
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
rasnatune-pypi-publish.yml@0f01fb72a6e042ca7ce70b81019d1769ee6686c1 -
Trigger Event:
push
-
Statement type: