Convert exported Torch module to circle
Project description
TICO
TICO (Torch IR to Circle ONE) is a python library for converting Pytorch modules into a circle model that is a lightweight and efficient representation in ONE designed for optimized on-device neural network inference.
Table of Contents
For Users
For Developers
For Users
Installation
- Prerequisites
- Python 3.10
- one-compiler nightly
- This project depends on ONE Compiler, and it uses nightly features that are not yet available in the official release. Until one-compiler 1.30.0 is released, you must use a prebuilt nighlty version of ONE Compiler.
We highly recommend to use a virtual env, e.g., conda.
-
Clone this repo
-
Build python package
./ccex build
This will generate build and dist directories in the root directory.
- Install generated package
./ccex install
Available options
--distTo install the package from .whl (without this option, TICO is installed in an editable mode)--torch_ver <torch version>To install a specific torch version (default: 2.6).- Available : 2.5, 2.6, nightly
- Now you can convert a torch module to a
.circle.
Getting started
This tutorial explains how you can use TICO to generate a circle model from a torch module.
Let's assume we have a torch module.
import tico
import torch
class AddModule(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, x, y):
return x + y
NOTE TICO internally uses torch.export. Therefore, the torch module must be 'export'able. Please see this document if you have any trouble to export.
From torch module
You can convert a torch module to a circle model with these steps.
torch_module = AddModule()
example_inputs = (torch.ones(4), torch.ones(4))
circle_model = tico.convert(torch_module, example_inputs)
circle_model.save('add.circle')
Compile with configuration
from test.modules.op.add import AddWithCausalMaskFolded
torch_module = AddWithCausalMaskFolded()
example_inputs = torch_module.get_example_inputs()
config = tico.CompileConfigV1()
config.legalize_causal_mask_value = True
circle_model = tico.convert(torch_module, example_inputs, config = config)
circle_model.save('add_causal_mask_m120.circle')
With legalize_causal_mask_value option on, causal mask value is converted from
-inf to -120, creating a more quantization-friendly circle model with the cost of
slight accuracy drop.
From .pt2
The torch module can be exported and saved as .pt2 file (from PyTorch 2.1).
module = AddModule()
example_inputs = (torch.ones(4), torch.ones(4))
exported_program = torch.export.export(module, example_inputs)
torch.export.save(exported_program, 'add.pt2')
There are two ways to convert .pt2 file: python api, command line tool.
- Python API
circle_model = tico.convert_from_pt2('add.pt2')
circle_model.save('add.circle')
- Command Line Tool
pt2-to-circle -i add.pt2 -o add.circle
- Command Line Tool with configuration
pt2-to-circle -i add.pt2 -o add.circle -c config.yaml
# config.yaml
version: '1.0' # You must specify the config version.
legalize_causal_mask_value: True
Running circle models directly in Python
After circle export, you can run the model directly in Python.
Note that you should install one-compiler package first.
The output types are numpy.ndarray.
torch_module = AddModule()
example_inputs = (torch.ones(4), torch.ones(4))
circle_model = tico.convert(torch_module, example_inputs)
circle_model(*example_inputs)
# numpy.ndarray([2., 2., 2., 2.], dtype=float32)
For Developers
Testing & Code Formatting
Run below commands to configure testing or formatting environment.
Refer to the dedicated section to have more fine-grained control.
$ ./ccex configure # to set up testing & formatting environment
$ ./ccex configure format # to set up only formatting environment
$ ./ccex configure test # to set up only testing environment
Available options
--torch_ver <torch version>To install a specific torch family package(ex. torchvision) version (default: 2.6)- Available : '2.5', '2.6', 'nightly'
$ ./ccex configure # to set up testing & formatting environment with stable2.6.x version
$ ./ccex configure test # to set up only testing environment with stable 2.6.x version
$ ./ccex configure test --torch_ver 2.5 # to set up only testing environment with stable 2.5.x version
$ ./ccex configure test --torch_ver nightly # to set up only testing environment with nightly version
Testing
Test congifure
Run below commands to install requirements for testing.
NOTE TICO will be installed in an editable mode.
./ccex configure test
# without editable install
./ccex configure test --dist
Test All
Run below commands to run the all unit tests.
NOTE Unit tests don't include model test.
./ccex test
# OR
./ccex test run-all-tests
Test Subset
To run subset of test.modules.*,
Run ./ccex test -k <keyword>
For example, to run tests in specific sub-directory (op, net, ..)
# To run tests in specific sub-directory (op/, net/ ..)
./ccex test -k op
./ccex test -k net
# To run tests in one file (single/op/add, single/op/sub, ...)
./ccex test -k add
./ccex test -k sub
# To run SimpleAdd test in test/modules/single/op/add.py
./ccex test -k SimpleAdd
To see the full debug log, add -v or TICO_LOG=4.
TICO_LOG=4 ./ccex test -k add
# OR
./ccex test -v -k add
Test Model
If you want to test them locally, you can do so by navigating to each model directory,
installing the dependencies listed in its requirements.txt, and running the tests one by one.
$ pip install -r test/modules/model/<model_name>/requirements.txt
# Run test for a single model
$ ./ccex test -m <model_name>
For example, to run a single model
./ccex test -m InceptionV3
Runtime Options
By default, ./ccex test runs all modules with the circle-interpreter engine.
You can override this and run tests using the onert runtime instead.
0. Installing ONERT Nightly
Some ONERT features are only available in the nightly build until the next official release. To install the ONERT wheel from the issue comment:
- Download the
.whlfile linked in the relevant Github issue comment. - Install it with pip, for example:
pip install /path/to/onert_nightly.whl
1. Command-Line Flag
Use the --runtime (or -r) flag to select a runtime:
# Run with the default circle-interpreter
./ccex test
# Run all tests with onert
./ccex test --runtime onert
# or
./ccex test -r onert
2. Environment Variable
You can also set the CCEX_RUNTIME environment variable:
# Temporarily override for one command
CCEX_RUNTIME=onert ./ccex test
# Persist in your shell session
export CCEX_RUNTIME=onert
./ccex test
Supported Runtimes
- circle-interpreter (default): uses the Circle interpreter for inference.
- onert: uses the ONERT package for inference, useful when the Circle interpreter cannot run a given module.
Code Formatting
Format configure
Run below commands to install requirements for formatting.
./ccex configure format
Format run
./ccex format
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 Distributions
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 tico-0.1.0.dev250605-py3-none-any.whl.
File metadata
- Download URL: tico-0.1.0.dev250605-py3-none-any.whl
- Upload date:
- Size: 294.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d50acf3a7276f0f74f0f1525e22c986c9e7a635d33a2f54484691132a3fcfa2
|
|
| MD5 |
3609bfecbca7d2ef20a31555f75df22b
|
|
| BLAKE2b-256 |
8eb6d7ba741f57c8a8950e2087c3a14f0354a500b011071a0523401d5383180d
|
Provenance
The following attestation bundles were made for tico-0.1.0.dev250605-py3-none-any.whl:
Publisher:
publish-nightly-package.yaml on Samsung/TICO
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tico-0.1.0.dev250605-py3-none-any.whl -
Subject digest:
9d50acf3a7276f0f74f0f1525e22c986c9e7a635d33a2f54484691132a3fcfa2 - Sigstore transparency entry: 230375393
- Sigstore integration time:
-
Permalink:
Samsung/TICO@4fd3867d6132874076cc189e4725ce89b25afb87 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Samsung
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-nightly-package.yaml@4fd3867d6132874076cc189e4725ce89b25afb87 -
Trigger Event:
schedule
-
Statement type: