SparseML Callback
All credits to Sean Narenthiran. I am merely using his code for demonstrating purposes.
SparseML allows you to leverage sparsity to improve inference times substantially.
SparseML requires you to fine-tune your model with the SparseMLCallback + a SparseML Recipe. By training with the SparseMLCallback, you can leverage the DeepSparse engine to exploit the introduced sparsity, resulting in large performance improvements.
The SparseML callback requires the model to be ONNX exportable. This can be tricky when the model requires dynamic sequence lengths such as RNNs.
To use leverage SparseML & DeepSparse follow the below steps:
Choose your Sparse Recipe
To choose a recipe, have a look at recipes and Sparse Zoo.
It may be easier to infer a recipe via the UI dashboard using Sparsify which allows you to tweak and configure a recipe.
This requires to import an ONNX model, which you can get from your LightningModule by doing model.to_onnx(output_path).
Train with SparseMLCallback
from pytorch_lightning import LightningModule, Trainer
from pl_bolts.callbacks import SparseMLCallback
class MyModel(LightningModule):
...
model = MyModel()
trainer = Trainer(
callbacks=SparseMLCallback(recipe_path='recipe.yaml')
)
Export to ONNX!
Using the helper function, we handle any quantization/pruning internally and export the model into ONNX format.
Note this assumes either you have implemented the property example_input_array in the model or you must provide a sample batch as below.
import torch
model = MyModel()
...
# export the onnx model, using the `model.example_input_array`
SparseMLCallback.export_to_sparse_onnx(model, 'onnx_export/')
# export the onnx model, providing a sample batch
SparseMLCallback.export_to_sparse_onnx(model, 'onnx_export/', sample_batch=torch.randn(1, 128, 128, dtype=torch.float32))
Once your model has been exported, you can import this into either Sparsify or DeepSparse.
Metadata
Release files for pl-hub-sparse-ml-callback 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pl_hub_sparse_ml_callback-0.0.2.tar.gz | 8.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pl_hub_sparse_ml_callback-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.1 kB
Release files / pl_hub_sparse_ml_callback-0.0.2.tar.gz
| Download URL | pl_hub_sparse_ml_callback-0.0.2.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fcd2a45e4f13980e0a6178d509bf5268c8c17c58cfa46689b49d54a4a6e67865
|
|
BLAKE2b-256 checksum How to use checksums |
1584994d5868632354e2635e4b811884b0a78d1aaec475832c82ba0c27cf7124
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8
|
Release files / pl_hub_sparse_ml_callback-0.0.2-py3-none-any.whl
| Download URL | pl_hub_sparse_ml_callback-0.0.2-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ab28a006f4af3ca9ea67037ea84df77b2e12ffee8d2da42570d73fbf0ca5d0c2
|
|
BLAKE2b-256 checksum How to use checksums |
4eece0eac4c760fa29b2236f28123ae8e70f634e5edc29cacac236703ec7b389
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.8
|