Skip to main content

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.Compressor
  • rasnatune.QuantizeWeight
  • rasnatune.QuantizeActivation
  • rasnatune.QuantizeMAC
  • rasnatune.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rasnatune-0.0.2.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rasnatune-0.0.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

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

Hashes for rasnatune-0.0.2.tar.gz
Algorithm Hash digest
SHA256 0446b5a93e190f3a8e7c00aa550b687f91709f513365021c0869e57e676cd5e3
MD5 b3828d7297c7acc115b790df7401d90e
BLAKE2b-256 986ac18757f4242032f95fe0453edf6723538bd3e1f3c83531d16b723d8c55b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for rasnatune-0.0.2.tar.gz:

Publisher: rasnatune-pypi-publish.yml on spiceengine/rasnatune

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for rasnatune-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f1eeea5db2f62b1f21ec28098b054b6221ef048b523cd00d0e232555767e267f
MD5 0bd3df984df5cbdd8655cf707acdf8fd
BLAKE2b-256 7bf8e94b8e5436d85b30ee566717b309f61841adf0ee5c1c31a734d004c2c562

See more details on using hashes here.

Provenance

The following attestation bundles were made for rasnatune-0.0.2-py3-none-any.whl:

Publisher: rasnatune-pypi-publish.yml on spiceengine/rasnatune

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page