Transpile
Transpilation is a crucial process in the deployment of Verifiable Machine Learning models. It involves the transformation of an ONNX model into a Cairo model. These models can generate proofs that can be verified, ensuring the integrity and reliability of the model's predictions.
The transpilation of an ONNX model to a Cairo model is powered by β¨Orionβ¨
The transpilation process begins by reading the model from the specified path. The model is then sent for transpilation. By default, the output of this process is saved in the cairo_model/
folder. However, you can specify a different output path using the --output-path
option.
The result of the transpilation process is saved at the provided path, in this case, my_awesome_model/
.
When we transpile a model we have two possibilities: a fully compatible model and a partially compatible one.
A model is fully compatible when all the operators that the model uses are supported by the Transpiler and Orion, if this happens the model is compiled after transpilation and we save the .sierra.json file on behalf of the user to use later for deployment (endpoint docs). This will be shown in the output of the transpile command:
If a model is partially supported, we will create a warning in the output stating that not all the operators are supported right now. If it is partially supported the Cairo code can still be modified for later compilation and endpoint.
Supported Operators
Operator | Implemented |
---|---|
Abs | |
Acos | |
Acosh | |
Add | |
And | |
Div | |
Mul | |
Sub | |
Argmax | |
Argmin | |
Asin | |
Asinh | |
Atan | |
Relu | |
Constant | |
MatMul | |
Gemm | |
TreeEnsembleClassifier | |
LinearClassifier | |
LinearRegressor | |
Softmax | |
Sigmoid | |
Concat | |
Squeeze | |
Unsqueeze | |
Reshape |
How do we transpile a model?
There are three main methods for transpiling a model:
Method 1: Using the giza transpile
command
giza transpile
commandThis is the simplest method and is recommended for most users. When you run this command, Giza handles everything for you:
It first checks if a model with the specified name already exists. If not, it creates a new model and then transpiles it.
The output of this process is saved in the
cairo_model/
folder by default, but you can specify a different output path using the--output-path
option.
This is the strategy that we followed in the example before.
Method 2: Manually creating a model and then transpiling it
This method gives you more control over the process.
First, you create a model manually using the
giza models create
command.After the model is created, you can transpile it using the
giza transpile --model-id ...
This method is useful when you want to specify particular options or parameters during the model creation and transpilation process.
Method 3: Using a previous model
If you have a previously created model, you can transpile it by indicating the model-id in the giza transpile --model-id ...
or giza versions transpile --model-id
command.
This method is useful when you want to create a new version of an existing model.
The output of the transpilation process is saved in the same location as the original model.
Transpilation Results
When a version is transpiled the version can be in the following statuses:
FAILED
: the transpilation of the model failedCOMPLETED: the version transpilation is completed and the version is FULLY compatible. This means that the version operators all are supported during transpilation and Orion, and the model has been compiled to sierra and saved in the platform. The model can be directly deployed without the need to provide a sierra file. This model is "frozen" so it will not allow for code or model updates and if any changes are done to the model a new version should be created.
PARTIALLY_SUPPORTED
: not all the operators are supported in the transpilation but they might be supported in Orion, so a partially working code will be returned, allowing for modifications of the code to update the version into a fully compatible one. Once this version is updated we will compile the version and if it is successful the new code and the sierra will be uploaded to Giza, the status will be updated toCOMPLETED
and the version will be frozen not allowing any more modifications.
We try to support all the available operators on Orion but there might be a little lag between Orion's implementation and transpilation availability
How to update a transpilation
If your model version is in a PARTIALLY_SUPPORTED
status, you can work towards achieving a COMPLETED
status by updating the transpilation. The update process involves modifying the unsupported operators and compiling the model. Here's how to update a transpilation, from creation to fully supported:
Transpile the model with
giza transpile
Modify your cairo model to address the unsupported operators.
Execute the giza version update command. This command needs
scarb
to be installed (docs), compilation will be attempted and if successful code and sierra file will be updated in Giza.
Example: Updating a Version
Say you have an awesome_model.onnx
that is PARTIALLY_SUPPORTED
:
This version has some operators that are not available in the transpilation, but they might be supported in Orion. When a model is not fully compatible, in the inference/lib.cairo
a comment will be shown:
Let's say that LogSoftMax
is the unsupported operator, if we check the Orion Documentation, we can see that it is supported. Now we could add the necessary code to add our operator (including imports):
LogSoftMax serves as an example and does not mean that it is not currently supported
After the manual implementation, we can trigger the update with the update
command:
Here's what is going on:
We want to update the first version of the first model with our new code, the code is at
--model-path cairo_model
The CLI checks if
scarb
is available in the systemscarb build
is attemptedWe still have some errors that we have to fix
In this case, we purposely forgot to add the @
to showcase a common scenario:
Once everything is fixed we can attempt the update again:
The version has been updated successfully! Now we have a fully compatible model that generated a sierra and can be easily deployed! Now the version will be frozen so it won't allow for any more updates.
When we refer to a version of a model, we refer to the code/artifact of a specific model at a specific point in time. The model is frozen for tracking purposes.
What is happening with the models and versions?
In Giza, a model is essentially a container for versions. Each version represents a transpilation of a machine learning model at a specific point in time. This allows you to keep track of different versions of your model as it evolves and improves over time.
To check the current models and versions that have been created, you can use the following steps:
Use the
giza models list
command to list all the models that have been created.For each model, you can use the
giza versions list --model-id ...
command to list all the versions of that model.
Remember, each version represents a specific transpilation of the model. So, if you have made changes to your machine learning model and transpiled it again, it will create a new version.
This system of models and versions allows you to manage and keep track of the evolution of your machine learning models over time.
For example, let's say you have created a model called awesome_model
and transpiled it twice. This will create two versions of the model, version 1 and version 2. You can check the status of these versions using the giza versions list --model-id ...
command.
Last updated