Runtime compression tuning utilities for PyTorch
Project description
rasnatune
rasnatune は、ハードウェア寄りの量子化やスパース化を PyTorch モデルで学習・検証するための軽量ツールキットです。
既存モデルに一時的な forward hook を直接取り付けるため、モデルオブジェクトそのものや state_dict() のキーは変わりません。
インストール
pip install rasnatune
クイックスタート
import torch
from rasnatune import compress, quantize, remove_compression
model = torch.nn.Sequential(
torch.nn.Linear(128, 64),
torch.nn.ReLU(),
torch.nn.Linear(64, 10),
)
compress(
model,
quantize(
activation=8,
weight=8,
accumulator=17,
weight_sparsity=0.9,
targets=torch.nn.Linear,
),
)
x = torch.randn(4, 128)
y = model(x)
torch.save(model.state_dict(), "model.pt")
remove_compression(model)
Recipe
Recipe は「モデルにどんな圧縮挙動を足すか」を表す設定です。
compress() は recipe をモデルへ適用し、同じモデルインスタンスを返します。
from rasnatune import compress, quantize, sparse
compress(
model,
quantize(
activation=8,
weight=8,
accumulator=17,
weight_sparsity=0.9,
targets=(torch.nn.Conv2d, torch.nn.Linear),
),
sparse(0.5, targets=torch.nn.Linear),
)
対象 module は recipe ごとに明示します。
targets= には module 型、module 型の tuple、または predicate を渡せます。
compress(
model,
quantize(activation=8, weight=8, accumulator=17, targets=torch.nn.Linear),
)
公開 API
トップレベルの recipe API:
rasnatune.compress(model, *recipes)rasnatune.remove_compression(model)rasnatune.compression_count(model)rasnatune.quantize(activation=8, weight=8, accumulator=17, targets=..., overflow="wrap", weight_sparsity=0.0)rasnatune.sparse(sparsity=0.5, targets=...)
低レベル compressor クラスも、独自ワークフロー向けに引き続き利用できます。
rasnatune.Compressorrasnatune.QuantizeWeightrasnatune.QuantizeActivationrasnatune.QuantizeMACrasnatune.SparseWeightUnstructured
量子化 MAC シミュレーション
quantize() は、実際の PyTorch 実行は FP32 のまま保ちつつ、signed integer MAC ハードウェアに近い挙動をシミュレートします。
- activation と weight は straight-through gradient 付きで量子化されます
weight_sparsityを指定すると、weight をスパース化してから量子化しますLinear/Conv2dの演算自体は FP32 で実行されます- layer の最終出力に signed accumulator 幅での wrap-around を適用します
- 同じ layer 内では出力 activation の再量子化は行いません
例えば quantize(activation=8, weight=8, accumulator=17, weight_sparsity=0.9, targets=torch.nn.Linear) は、weight を 90% スパース化し、signed 8-bit の入力・重みを使い、最終的な MAC 出力に 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.2.tar.gz.
File metadata
- Download URL: rasnatune-0.0.2.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0446b5a93e190f3a8e7c00aa550b687f91709f513365021c0869e57e676cd5e3
|
|
| MD5 |
b3828d7297c7acc115b790df7401d90e
|
|
| BLAKE2b-256 |
986ac18757f4242032f95fe0453edf6723538bd3e1f3c83531d16b723d8c55b6
|
Provenance
The following attestation bundles were made for rasnatune-0.0.2.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.2.tar.gz -
Subject digest:
0446b5a93e190f3a8e7c00aa550b687f91709f513365021c0869e57e676cd5e3 - Sigstore transparency entry: 1491963816
- Sigstore integration time:
-
Permalink:
spiceengine/rasnatune@4582c8c7ba52af65ea56ce318c328a20bba358ec -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/spiceengine
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
rasnatune-pypi-publish.yml@4582c8c7ba52af65ea56ce318c328a20bba358ec -
Trigger Event:
push
-
Statement type:
File details
Details for the file rasnatune-0.0.2-py3-none-any.whl.
File metadata
- Download URL: rasnatune-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.2 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 |
f1eeea5db2f62b1f21ec28098b054b6221ef048b523cd00d0e232555767e267f
|
|
| MD5 |
0bd3df984df5cbdd8655cf707acdf8fd
|
|
| BLAKE2b-256 |
7bf8e94b8e5436d85b30ee566717b309f61841adf0ee5c1c31a734d004c2c562
|
Provenance
The following attestation bundles were made for rasnatune-0.0.2-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.2-py3-none-any.whl -
Subject digest:
f1eeea5db2f62b1f21ec28098b054b6221ef048b523cd00d0e232555767e267f - Sigstore transparency entry: 1491963962
- Sigstore integration time:
-
Permalink:
spiceengine/rasnatune@4582c8c7ba52af65ea56ce318c328a20bba358ec -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/spiceengine
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
rasnatune-pypi-publish.yml@4582c8c7ba52af65ea56ce318c328a20bba358ec -
Trigger Event:
push
-
Statement type: