Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Concrete-ML models can be easily deployed in a client/server setting, enabling the creation of privacy-preserving services in the cloud.
As seen in the concepts section, a Concrete-ML model, once compiled to FHE, generates machine code that performs the inference on private data. Furthermore, secret encryption keys are needed so that the user can securely encrypt their data and decrypt the inference result. An evaluation key is also needed for the server to securely process the user's encrypted data.
Keys are generated by the user once for each service they use, based on the model the service provides and its cryptographic parameters.
The overall communications protocol to enable cloud deployment of machine learning services can be summarized in the following diagram:
The steps detailed above are as follows:
The model developer deploys the compiled machine learning model to the server. This model includes the cryptographic parameters. The server is now ready to provide private inference.
The client requests the cryptographic parameters (also called "client specs"). Once it gets them from the server, the secret and evaluation keys are generated.
The client sends the evaluation key to the server. The server is now ready to accept requests from this client. The client sends their encrypted data.
The server uses the evaluation key to securely run inference on the user's data and sends back the encrypted result.
The client now decrypts the result and can send back new requests.
For more information on how to implement this basic secure inference protocol, refer to the Production Deployment section and to the client/server example.
Please note that not all hardware/OS combinations are supported. Determine your platform, OS version and Python version before referencing the table below.
Depending on your OS, Concrete-ML may be installed with Docker or with pip:
OS / HW | Available on Docker | Available on pip |
---|---|---|
Also, only some versions of python
are supported: in the current release, these are 3.8
and 3.9
. Please note that, at the time of this Concrete-ML version release, Kaggle or Google Colab use Python 3.7 which is a deprecated version and is not supported by Concrete-ML.
Most of these limits are shared with the rest of the Concrete stack (namely Concrete-Numpy and Concrete-Compiler). Support for more platforms will be added in the future.
Installing Concrete-ML using PyPi requires a Linux-based OS or macOS running on an x86 CPU. For Apple Silicon, Docker is the only currently supported option (see below).
Installing on Windows can be done using Docker or WSL. On WSL, Concrete-ML will work as long as the package is not installed in the /mnt/c/ directory, which corresponds to the host OS filesystem.
To install Concrete-ML from PyPi, run the following:
This will automatically install all dependencies, notably Concrete-Numpy.
Concrete-ML can be installed using Docker by either pulling the latest image or a specific version:
The image can be used with Docker volumes, see the Docker documentation here.
The image can then be used via the following command:
This will launch a Concrete-ML enabled Jupyter server in Docker that can be accessed directly from a browser.
Alternatively, a shell can be lauched in Docker, with or without volumes:
Concrete-ML provides partial support for Pandas, with most available models (linear and tree-based models) usable on Pandas dataframes the same way they would be used with NumPy arrays.
The table below summarizes the current compatibility:
Methods | Support Pandas dataframe |
---|
The following example uses a LogisticRegression
model on a simple classification problem. A more advanced example can be found in the .
| |
Concrete-ML is an open-source privacy-preserving machine learning inference framework based on fully homomorphic encryption (FHE). It enables data scientists without any prior knowledge of cryptography to automatically turn machine learning models into their FHE equivalent, using familiar APIs from Scikit-learn and PyTorch (see how it looks for , and ).
Fully Homomorphic Encryption (FHE) is an encryption technique that allows computing directly on encrypted data, without needing to decrypt it. With FHE, you can build private-by-design applications without compromising on features. You can learn more about FHE in , or by joining the community.
This example shows the typical flow of a Concrete-ML model:
The model is trained on unencrypted (plaintext) data using scikit-learn. As FHE operates over integers, Concrete-ML quantizes the model to use only integers during inference.
The quantized model is compiled to a FHE equivalent. Under the hood, the model is first converted to a Concrete-Numpy program, then compiled.
To make a model work with FHE, the only constraint is to make it run within the supported precision limitations of Concrete-ML (currently 8-bit integers). Thus, machine learning models are required to be quantized, which sometimes leads to a loss of accuracy versus the original model operating on plaintext.
Additionally, Concrete-ML currently only supports FHE inference. On the other hand, training has to be done on unencrypted data, producing a model which is then converted to a FHE equivalent that can perform encrypted inference, i.e. prediction over encrypted data.
Finally, in Concrete-ML there is currently no support for pre-processing model inputs and for post-processing model outputs. These processing stages may involve text to numerical feature transformation, dimensionality reduction, KNN or clustering, featurization, normalization, and the mixing of results of ensemble models.
All of these issues are currently being addressed and significant improvements are expected to be released in the coming months.
More generally, if you have built awesome projects using Concrete-ML, feel free to let us know and we'll link to it!
Concrete-ML provides simple neural networks models with a Scikit-learn interface through the NeuralNetClassifier
and NeuralNetRegressor
classes.
Concrete-ML |
---|
The neural network models are built with , which provides a scikit-learn like interface to Torch models (more ).
These models use a stack of linear layers and the activation function and the number of neurons in each layer is configurable. This approach is similar to what is available in Scikit-learn using the MLPClassifier
/MLPRegressor
classes. The built-in, fully connected neural network (FCNN) models train easily with a single call to .fit()
, which will automatically quantize the weights and activations. These models use Quantization Aware Training, allowing good performance for low precision (down to 2-3 bit) weights and activations.
While NeuralNetClassifier
and NeuralNetClassifier
provide scikit-learn like models, their architecture is somewhat restricted in order to make training easy and robust. If you need more advanced models, you can convert custom neural networks as described in the .
Good quantization parameter values are critical to make models . Weights and activations should be quantized to low precision (e.g. 2-4 bits). Furthermore, in cases of overflow, the sparsity of the network can be tuned .
To create an instance of a Fully Connected Neural Network you need to instantiate one of the NeuralNetClassifier
and NeuralNetRegressor
classes and configure a number of parameters that are passed to their constructor. Note that some parameters need to be prefixed by module__
, while others don't. Basically, the parameters that are related to the model, i.e. the underlying nn.Module
, must have the prefix. The parameters that are related to training options do not require the prefix.
The figure above shows, on the right, the Concrete-ML neural network, trained with Quantization Aware Training, in a FHE-compatible configuration. The figure compares this network to the floating point equivalent, trained with scikit-learn.
module__n_layers
: number of layers in the FCNN, must be at least 1. Note that this is the total number of layers. For a single hidden layer NN model, set module__n_layers=2
module__n_outputs
: number of outputs (classes or targets)
module__input_dim
: dimensionality of the input
n_w_bits
(default 3): number of bits for weights
n_a_bits
(default 3): number of bits for activations and inputs
max_epochs
: The number of epochs to train the network (default 10)
verbose
: Whether to log loss/metrics during training (default: False)
lr
: Learning rate (default 0.001)
When you have training data in the form of a NumPy array, and targets in a NumPy 1d array, you can set:
You can give weights to each class to use in training. Note that this must be supported by the underlying PyTorch loss function.
The n_hidden_neurons_multiplier
parameter influences training accuracy as it controls the number of non-zero neurons that are allowed in each layer. Increasing n_hidden_neurons_multiplier
improves accuracy, but should take into account precision limitations to avoid overflow in the accumulator. The default value is a good compromise that avoids overflow, in most cases, but you may want to change the value of this parameter to reduce the breadth of the network if you have overflow errors. A value of 1 should be completely safe with respect to overflow.
Concrete-ML provides several of the most popular classification
and regression
tree models that can be found in :
Concrete-ML | scikit-learn |
---|
In addition to support for scikit-learn, Concrete-ML also supports 's XGBClassifier
:
Concrete-ML | XGboost |
---|
Here's an example of how to use this model in FHE on a popular data-set using some of scikit-learn's pre-processing tools. A more complete example can be found in the .
This graph shows the impact of quantization over the decision boundaries in the Concrete-ML FHE decision tree models. In the 3-bits model, only a rough, highly-discrete decision function is observed. This results in a small decrease of accuracy of about 7% compared to the initial XGBoost classifier. Besides, using 6-bits of quantization makes the model reach 93% accuracy, drastically reducing this difference to only 1.7 percentage points.
In fact, the quantization process may sometimes create some artifacts that could lead to a decrease in performance. Still, as the quantization is done individually on each input feature, the artifacts are minor when considering small tree-based models with 5-6 bits quantization. Thus, FHE tree-based models reach similar scores as their equivalent floating point ones.
The following graph shows that using 5-6 bits of quantization is usually sufficient to reach the performance of a non-quantized XGBoost model on floating point data. The metrics plotted are accuracy and F1-score on the spambase
data-set.
Concrete-ML is built on top of Concrete-Numpy, which enables Numpy programs to be converted into FHE circuits.
Training. A model is trained using plaintext, non-encrypted, training data.
Quantization. The model is converted into an integer equivalent using quantization. Concrete-ML performs this step either during training (Quantization-Aware Training) or after training (Post-Training Quantization), depending on model type. Quantization converts inputs, model weights and all intermediate values of the inference computation to integers. More information is available .
Simulation using the Virtual Library. Testing FHE models on very large datasets can take a long time. Furthermore, not all models are compatible with FHE constraints out-of-the-box. Simulation using the Virtual Library allows you to execute a model that was quantized, to measure the accuracy it would have in FHE, but also to determine the modifications required to make it FHE compatible. Simulation is described in more details .
Compilation. Once the model is quantized, simulation can confirm it has good accuracy in FHE. The model then needs to be compiled using Concrete's FHE compiler to produce an equivalent FHE circuit. This circuit is represented as an MLIR program consisting of low level cryptographic operations. You can read more about FHE compilation , MLIR and about the low-level Concrete library .
Inference. The compiled model can then be executed on encrypted data, once the proper keys have been generated. The model can also be deployed to a server and used to run private inference on encrypted inputs.
You can see some examples of the model development workflow .
Client/Server deployment. In a client/server setting, the model can be exported in a way that:
allows the client to generate keys, encrypt and decrypt.
provides a compiled model that can run on the server to perform inference on encrypted data
Key generation. The data owner (client) needs to generate a pair of private keys (to encrypt/decrypt their data and results) and a public evaluation key (for the model's FHE evaluation on the server).
You can see an example of the model deployment workflow .
Concrete-ML and Concrete-Numpy are tools that hide away the details of the underlying cryptography scheme, called TFHE. However, some cryptography concepts are still useful when using these two toolkits:
Encryption/Decryption. These operations transform plaintext, i.e. human-readable information, into ciphertext, i.e. data that contains a form of the original plaintext that is unreadable by a human or computer without the proper key to decrypt it. Encryption takes plaintext and an encryption key and produces ciphertext, while decryption is the inverse operation.
Encrypted inference. FHE allows a third party to execute (i.e. run inference or predict) a machine learning model on encrypted data (a ciphertext). The result of the inference is also encrypted and can only be read by the person who gets the decryption key.
Keys. A key is a series of bits used within an encryption algorithm for encrypting data so that the corresponding ciphertext appears random.
Key generation. Cryptographic keys need to be generated using random number generators. Their size may be large and key generation may take a long time. However, keys only need to be generated once for each model a client uses.
Guaranteed correctness of encrypted computations. To achieve security, TFHE, the underlying encryption scheme, adds random noise as ciphertexts. This can induce errors during processing of encrypted data, depending on noise parameters. By default, Concrete-ML uses parameters that ensure the correctness of the encrypted computation, so you do not need to take into account the noise parametrization. Therefore, results on encrypted data will be the same as the results of simulation on clear data.
To respect FHE constraints, all numerical programs over encrypted data must have all inputs, constants and intermediate values represented with integers of a maximum of 8 bits.
In addition to the built-in models, Concrete-ML supports generic machine learning models implemented with Torch, or .
As is the most appropriate method of training neural networks that are compatible with , Concrete-ML works with , a library providing QAT support for PyTorch.
The following example uses a simple QAT PyTorch model that implements a fully connected neural network with two hidden layers. Due to its small size, making this model respect FHE constraints is relatively easy.
The model can now be used to perform encrypted inference. Next, the test data is quantized:
and the encrypted inference run using either:
quantized_numpy_module.forward_and_dequant()
to compute predictions in the clear, on quantized data and then de-quantize the result. The return value of this function contains the dequantized (float) output of running the model in the clear. Calling the forward function on the clear data is useful when debugging. The results in FHE will be the same as those on clear quantized data.
quantized_numpy_module.forward_fhe.encrypt_run_decrypt()
to perform the FHE inference. In this case, dequantization is done in a second stage using quantized_numpy_module.dequantize_output()
.
While the example above shows how to import a Brevitas/PyTorch model, Concrete-ML also provides an option to import generic QAT models implemented either in PyTorch or through ONNX. Interestingly, deep learning models made with TensorFlow or Keras should be usable, by preliminary converting them to ONNX.
QAT models contain quantizers in the PyTorch graph. These quantizers ensure that the inputs to the Linear/Dense and Conv layers are quantized.
When importing QAT models using this generic pipeline, a representative calibration set should be given as quantization parameters in the model need to be inferred from the statistics of the values encountered during inference.
Concrete-ML supports a variety of PyTorch operators that can be used to build fully connected or convolutional neural networks, with normalization and activation layers. Moreover, many element-wise operators are supported.
Please note that Concrete-ML supports these operators but also the Quantization Aware Training equivalents from Brevitas.
brevitas.nn.QuantLinear
brevitas.nn.QuantConv2d
brevitas.nn.QuantIdentity
Note that the equivalent versions from torch.functional
are also supported.
Concrete-ML provides several of the most popular linear models for regression
or classification
that can be found in :
Concrete-ML | scikit-learn |
---|
Using these models in FHE is extremely similar to what can be done with scikit-learn's , making it easy for data scientists who are used to this framework to get started with Concrete-ML.
Models are also compatible with some of scikit-learn's main workflows, such as Pipeline()
or GridSearch()
.
Here's an example of how to use this model in FHE on a simple data-set below. A more complete example can be found in the .
We can clearly observe the impact of quantization over the decision boundaries in the FHE model, separating the initial lines into broken lines with steps. However, this does not change the overall score as both models output the same accuracy (90%).
In fact, the quantization process may sometimes create some artifacts that could lead to a decrease in performance. Still, the impact of those artifacts is often minor when considering linear models as FHE models reach similar scores as their equivalent clear ones.
Here is a simple example of classification on encrypted data using logistic regression. More examples can be found .
Inference can then be done on encrypted data. The above example shows encrypted inference in the model development phase. Alternatively, in in a client/server setting, the data is encrypted by the client, processed securely by the server and then decrypted by the client.
Concrete-ML is built on top of Zama's Concrete framework. It uses , which itself uses the and the . To use these libraries directly, refer to the and documentations.
Various tutorials are proposed for the and for . In addition, we also list standalone use-cases:
: a Python and notebook showing a Quantization Aware Training (done with and following constraints of the package) and its corresponding use in Concrete-ML.
: a notebook, which gives a solution to the . Done with XGBoost from Concrete-ML. It comes as a companion of , and was the subject of a blogpost in .
Support forum: (we answer in less than 24 hours).
Live discussion on the FHE.org discord server: (inside the #concrete channel).
Do you have a question about Zama? You can write us on or send us an email at: hello@zama.ai
The shows the behavior of built-in neural networks on several synthetic datasets.
module__activation_function
: can be one of the Torch activations (e.g. nn.ReLU, see the full list )
n_accum_bits
(default 8): maximum accumulator bit-width that is desired. The implementation will attempt to keep accumulators under this bit-width through , i.e. setting some weights to zero
Other parameters from skorch are in the
module__n_hidden_neurons_multiplier
: The number of hidden neurons will be automatically set proportional to the dimensionality of the input (i.e. the value for module__input_dim
). This parameter controls the proportionality factor and is set to 4 by default. This value gives good accuracy while avoiding accumulator overflow. See the and sections for more info.
Using the above example, we can then plot how the model classifies the inputs and then compare those results with the XGBoost model executed in clear. A 6-bits model is also given in order to better understand the impact of quantization on classification. Similar plots can be found in the .
While Concrete-ML users only need to understand the cryptography concepts above, for a deeper understanding of the cryptography behind the Concrete stack, please see the or .
Thus, Concrete-ML quantizes the input data and model outputs in the same way as weights and activations. The main levers to control accumulator bit-width are the numbers of bits used for the inputs, weights and activations of the model. These parameters are crucial to comply with the constraint on accumulator bit-widths. Please refer to for more details about how to develop models with quantization in Concrete-ML.
However, these methods may cause a reduction in the accuracy of the model since its representative power is diminished. Most importantly, carefully choosing a quantization approach can alleviate accuracy loss, all the while allowing compilation to FHE. Concrete-ML offers built-in models that already include quantization algorithms, and users only need to configure some of their parameters, such as the number of bits, discussed above. See for information about configuring these parameters for various models.
Additional specific methods can help to make models compatible with FHE constraints. For instance, dimensionality reduction can reduce the number of input features and, thus, the maximum accumulator bit-width reached within a circuit. Similarly, sparsity-inducing training methods, such as pruning, de-activate some features during inference, which also helps. For now, dimensionality reduction is considered as a pre-processing step, while pruning is used in the .
The configuration of model quantization parameters is illustrated in the advanced examples for and dimensionality reduction is shown in the .
Once the model is trained, calling the from Concrete-ML will automatically perform conversion and compilation of a QAT network. Here, 3-bit quantization is used for both the weights and activations.
Suppose that n_bits_qat
is the bit-width of activations and weights during the QAT process. To import a PyTorch QAT network, you can use the library function, passing import_qat=True
:
Alternatively, if you want to import an ONNX model directly, please see . The also supports the import_qat
parameter.
-- partial support
We can then plot the decision boundary of the classifier and then compare those results with a scikit-learn model executed in clear. The complete code can be found in the .
Linux
Yes
Yes
Windows
Yes
Not currently
Windows Subsystem for Linux
Yes
Yes
macOS (Intel)
Yes
Yes
macOS (Apple Silicon, ie M1, M2 etc)
Yes
Not currently
fit | ✓ |
compile | ✗ |
predict (execute_in_fhe=False) | ✓ |
predict (execute_in_fhe=True) | ✓ |
In addition to Concrete-ML models and to custom models in torch, it is also possible to directly compile ONNX models. This can be particularly appealing, notably to import models trained with Keras.
ONNX models can be compiled by directly importing models that are already quantized with Quantization Aware Training (QAT). or by performing Post-Training Quantization (PTQ) with Concrete-ML.
The following example shows how to compile an ONNX model using PTQ. The model was initially trained using Keras before being exported to ONNX. The training code is not shown here.
This example uses Post-Training Quantization, i.e. the quantization is not performed during training. Thus this model would not have good performance in FHE. Quantization Aware Training should be added by the model developer and importing QAT ONNX models can be done as shown below.
While Keras was used in this example, it is not officially supported as additional work is needed to test all of Keras' types of layer and models.
QAT models contain quantizers in the ONNX graph. These quantizers ensure that the inputs to the Linear/Dense and Conv layers are quantized. Since these QAT models have quantizers that are configured during training to a specific number of bits, the ONNX graph will need to be imported using the same settings:
The following operators are supported for evaluation and conversion to an equivalent FHE circuit. Other operators were not implemented either due to FHE constraints, or because they are rarely used in PyTorch activations or scikit-learn models.
Abs
Acos
Acosh
Add
Asin
Asinh
Atan
Atanh
AveragePool
BatchNormalization
Cast
Celu
Clip
Constant
Conv
Cos
Cosh
Div
Elu
Equal
Erf
Exp
Flatten
Gemm
Greater
GreaterOrEqual
HardSigmoid
HardSwish
Identity
LeakyRelu
Less
LessOrEqual
Log
MatMul
Mul
Not
Or
PRelu
Pad
Pow
ReduceSum
Relu
Reshape
Round
Selu
Sigmoid
Sin
Sinh
Softplus
Sub
Tan
Tanh
ThresholdedRelu
Transpose
Where
onnx.brevitas.Quant
This section includes a complete example of converting a neural network to Quantization Aware Training (QAT). This tutorial uses PyTorch and Brevitas to train a simple network on a synthetic data-set. You can find the demo of the final network in the custom-model with quantization aware training demo. To see how to apply these network design principles for a real-world data-set, please see the MNIST use-case example.
For a more formal description of the usage of Brevitas to build FHE-compatible neural networks, please see the Brevitas usage reference.
This example shows how to train a fully-connected neural network on a synthetic 2D data-set with a checkerboard grid pattern of 100 x 100 points. The data is split into 9500 training and 500 test samples.
In PyTorch, using standard layers, this network would look as follows:
Once trained, this network can be imported using the compile_torch_model
function. This function uses simple Post-Training Quantization.
The network was trained using different numbers of neurons in the hidden layers, and quantized using 3-bits weights and activations. The mean accumulator size shown below was extracted using the Virtual Library.
This shows that the fp32 accuracy and accumulator size increases with the number of hidden neurons, while the 3-bit accuracy remains low irrespective of to the number of neurons. While all the configurations tried here were FHE-compatible (accumulator < 8 bits), it is sometimes preferable to have a lower accumulator size in order for the inference time to be faster.
The accumulator size is determined by Concrete-Numpy as being the maximum bit-width encountered anywhere in the encrypted circuit
Considering that FHE only works with limited integer precision, there is a risk of overflowing in the accumulator, resulting in unpredictable results.
This can be leveraged to train a network with more neurons, while not overflowing the accumulator, using a technique called pruning, where the developer can impose a number of zero-valued weights. Torch provides support for pruning out of the box.
The following code shows how to use pruning in the previous example:
Results with PrunedSimpleNet
, a pruned version of the SimpleNet
with 100 neurons on the hidden layers, are given below:
This shows that the fp32 accuracy has been improved while maintaining constant mean accumulator size.
When pruning a larger neural network during training, it is easier to obtain a low bit-width accumulator while maintaining better final accuracy. Thus, pruning is more robust than training a similar smaller network.
While pruning helps maintain the post-quantization level of accuracy in low-precision settings, it does not help maintain accuracy when quantizing from floating point models. The best way to guarantee accuracy is to use QAT (read more in the quantization documentation).
In this example, QAT is done using Brevitas, changing Linear
layers to QuantLinear
and adding quantizers on the inputs of linear layers using QuantIdentity.
The QAT import tool in Concrete-ML is a work in progress. While it has been tested with some networks built with Brevitas, it is possible to use other tools to obtain QAT networks.
Training this network with 30 out of 100 total non-zero neurons gives good accuracy while being FHE-compatible (accumulator size < 8 bits).
The PyTorch QAT training loop is the same as the standard floating point training loop, but hyper-parameters such as learning rate might need to be adjusted.
Quantization Aware Training is somewhat slower than normal training. QAT introduces quantization during both the forward and backward passes. The quantization process is inefficient on GPUs as its computational intensity is low with respect to data transfer time.
The following table summarizes the examples in this section:
Model | Data-set | Metric | Floating Point | Simulation | FHE |
---|---|---|---|---|---|
The following table summarizes the various examples in this section, along with their accuracies.
Model | Data-set | Metric | Floating Point | Simulation | FHE |
---|---|---|---|---|---|
A * means that FHE accuracy was calculated on a subset of the validation set.
Concrete-ML is a Python
library, so Python
should be installed to develop Concrete-ML. v3.8
and v3.9
are the only supported versions. Concrete-ML also uses Poetry
and Make
.
First of all, you need to git clone
the project:
Some tests require files tracked by git-lfs to be downloaded. To do so, please follow the instructions on , then run git lfs pull
.
A simple way to have everything installed is to use the development Docker (see the guide). On Linux and macOS, you have to run the script in ./script/make_utils/setup_os_deps.sh
. Specify the --linux-install-python
flag if you want to install python3.8 as well on apt-enabled Linux distributions. The script should install everything you need for Docker and bare OS development (you can first review the content of the file to check what it will do).
For Windows users, the setup_os_deps.sh
script does not install dependencies because of how many different installation methods there are/lack of a single package manager.
The first step is to (as some of the dev tools depend on it), then . In addition to installing Python, you are still going to need the following software available on path on Windows, as some of the basic dev tools depend on them:
git
jq
make
Development on Windows only works with the Docker environment. Follow .
To manually install Python, you can follow guide (alternatively, you can google how to install Python 3.8 (or 3.9)
).
As there is no concrete-compiler
package for Windows, only the dev dependencies can be installed. This requires Poetry >= 1.2.
The dev tools use make
to launch the various commands.
On Linux, you can install make
from your distribution's preferred package manager.
On macOS, you can install a more recent version of make
via brew:
In the following sections, be sure to use the proper make
tool for your system: make
, gmake
, or other.
To get the source code of Concrete-ML, clone the code repository using the link for your favourite communication protocol (ssh or https).
We are going to make use of virtual environments. This helps to keep the project isolated from other Python
projects in the system. The following commands will create a new virtual environment under the project directory and install dependencies to it.
The following command will not work on Windows if you don't have Poetry >= 1.2.
Finally, activate the newly created environment using the following command:
Docker automatically creates and sources a venv in ~/dev_venv/
The venv persists thanks to volumes. It also creates a volume for ~/.cache to speed up later reinstallations. You can check which Docker volumes exist with:
You can still run all make
commands inside Docker (to update the venv, for example). Be mindful of the current venv being used (the name in parentheses at the beginning of your command prompt).
After your work is done, you can simply run the following command to leave the environment:
From time to time, new dependencies will be added to the project or the old ones will be removed. The command below will make sure the project has the proper environment, so run it regularly!
If you are having issues, consider using the dev Docker exclusively (unless you are working on OS-specific bug fixes or features).
Here are the steps you can take on your OS to try and fix issues:
Here are the steps you can take in your Docker to try and fix issues:
If the problem persists at this point, you should ask for help. We're here and ready to assist!
Concrete-ML offers some features for advanced users that wish to adjust the cryptographic parameters that are generated by the Concrete stack for a certain machine learning model.
p_error
parameterConcrete-ML makes use of table lookup (TLU) to represent any non-linear operation (e.g. sigmoid). This TLU is implemented through the Programmable Bootstrapping (PBS) operation which will apply a non-linear operation in the cryptographic realm.
In Concrete-ML, the result of the TLU operation is obtained with a specific error probability:
A single PBS operation has 1 - DEFAULT_P_ERROR_PBS = 99.9936657516%
chances of being correct. This number plays a role in the cryptographic parameters. As such, the lower the p_error
, the more constraining the parameters will become. This has an impact on both key generation and, more importantly, on FHE execution time.
This number is set by default to be relatively low such that any user can build deep circuits without being impacted by this noise as . However, there might be use cases and specific circuits where the Gaussian noise can increase without being too dramatic for the circuit accuracy. In that case, increasing the p_error
can be relevant as it will reduce the execution time in FHE.
Here is a visualization of the effect of the p_error
over a simple linear regression with a p_error = 0.1
vs the default p_error
value:
The execution for the two models are 336 ms per example for the standard p_error
and 253 ms per example for a p_error = 0.1
(on a 8 cores Intel CPU machine). Obviously, this speedup is very dependent on model complexity. To obtain a speedup while maintaining good accuracy, it is possible to search for a good value of p_error
. Currently no heuristic has been proposed to find a good value a-priori.
Users have the possibility to change this p_error
as they choose fit, by passing an argument to the compile
function of any of the models. Here is an example:
Pruning is a method to reduce neural network complexity, usually applied in order to reduce the computation cost or memory size. Pruning is used in Concrete-ML to control the size of accumulators in neural networks, thus making them FHE-compatible. See for an explanation of accumulator bit-width constraints.
Pruning is used in Concrete-ML for two types of neural networks:
Built-in include a pruning mechanism that can be parameterized by the user. The pruning type is based on L1-norm. To comply with FHE constraints, Concrete-ML uses unstructured pruning, as the aim is not to eliminate neurons or convolutional filters completely, but to decrease their accumulator bit-width.
Custom neural networks, to work well under FHE constraints, should include pruning. When implemented with PyTorch, you can use the (e.g.L1-Unstructured) to good effect.
In neural networks, a neuron computes a linear combination of inputs and learned weights, then applies an activation function.
The neuron computes:
When building a full neural network, each layer will contain multiple neurons, which are connected to the neuron outputs of a previous layer or to the inputs.
Fixing some of the weights to 0 makes the network graph look more similar to the following:
In the formula above, in the worst-case, the maximum number of the input and weights that can make the result exceed $n$ bits is given by:
Concrete-ML provides functionality to deploy FHE machine learning models in a client/server setting. The deployment workflow and model serving pattern is as follows:
The training of the model and its compilation to FHE are performed on a development machine. Three different files are created when saving the model:
client.json
contains the secure cryptographic parameters needed for the client to generate private and evaluation keys.
server.json
contains the compiled model. This file is sufficient to run the model on a server.
serialized_processing.json
contains the metadata about pre- and post-processing, such as quantization parameters to quantize the input and de-quantize the output.
The compiled model (server.zip
) is deployed to a server and the cryptographic parameters (client.zip
) along with the model meta data (serialized_processing.json
) are shared with the clients.
The client obtains the cryptographic parameters (using client.zip
) and generates a private encryption/decryption key as well as a set of public evaluation keys. The public evaluation keys are then sent to the server, while the secret key remains on the client.
The private data is then encrypted using serialized_processing.json
by the client and sent to the server. Server-side, the FHE model inference is run on the encrypted inputs using the public evaluation keys.
The encrypted result is then returned by the server to the client, which decrypts it using its private key. Finally, the client performs any necessary post-processing of the decrypted result using serialized_processing.json
.
For a complete example, see
Before you start this section, you must install Docker by following official guide.
Once you have access to this repository and the dev environment is installed on your host OS (via make setup_env
once ), you should be able to launch the commands to build the dev Docker image with make docker_build
.
Once you do that, you can get inside the Docker environment using the following command:
After you finish your work, you can leave Docker by using the exit
command or by pressing CTRL + D
.
neurons | 10 | 30 | 100 |
---|---|---|---|
To understand how to overcome this limitation, consider a scenario where 2 bits are used for weights and layer inputs/outputs. The Linear
layer computes a dot product between weights and inputs . With 2 bits, no overflow can occur during the computation of the Linear
layer as long the number of neurons does not exceed 14, i.e. the sum of 14 products of 2-bit numbers does not exceed 7 bits.
By default, Concrete-ML uses symmetric quantization for model weights, with values in the interval . For example, for the possible values are , for the values can be .
However, in a typical setting, the weights will not all have the maximum or minimum values (e.g. ). Instead, weights typically have a normal distribution around 0, which is one of the motivating factors for their symmetric quantization. A symmetric distribution and many zero-valued weights are desirable because opposite sign weights can cancel each other out and zero weights do not increase the accumulator size.
non-zero neurons | 10 | 30 |
---|---|---|
non-zero neurons | 30 |
---|---|
Poetry
is used as the package manager. It drastically simplifies dependency and environment management. You can follow official guide to install it.
It is possible to install gmake
as make
. Check this for more info.
On Windows, check .
At this point, you should consider using Docker as nobody will have the exact same setup as you. If, however, you need to develop on your OS directly, you can .
For every neuron shown in each layer of the figure above, the linear combinations of inputs and learned weights are computed. Depending on the values of the inputs and weights, the sum - which for Concrete-ML neural networks is computed with integers - can take a range of different values.
To respect the bit-width constraint of the FHE , the values of the accumulator must remain small to be representable with only 8 bits. In other words, the values must be between 0 and 255.
Pruning a neural network entails fixing some of the weights to be zero during training. This is advantageous to meet FHE constraints, as irrespective of the distribution of , multiplying these input values by 0 does not increase the accumulator value.
While pruning weights can reduce the prediction performance of the neural network, studies show that a high level of pruning (above 50%) can often be applied. See here how Concrete-ML uses pruning in .
Here, is the maximum precision allowed.
For example, if and with , the worst case is where all inputs and weights are equal to their maximal value . In this case, there can be at most elements in the multi-sums.
In practice, the distribution of the weights of a neural network is Gaussian, with many weights either 0 or having a small value. This enables exceeding the worst-case number of active neurons without having to risk overflowing the bit-width. In built-in neural networks, the parameter n_hidden_neurons_multiplier
is multiplied with to determine the total number of non-zero weights that should be kept in a neuron.
fp32 accuracy
68.70%
83.32%
88.06%
3bit accuracy
56.44%
55.54%
56.50%
mean accumulator size
6.6
6.9
7.4
fp32 accuracy
82.50%
88.06%
3bit accuracy
57.74%
57.82%
mean accumulator size
6.6
6.8
3bit accuracy brevitas
95.4%
3bit accuracy in Concrete-ML
92.4%
accumulator size
7
Fully Connected NN
accuracy
0.95
0.94
0.94
QAT Fully Connected NN
Synthetic (Checkerboard)
accuracy
0.95
0.92
0.92
Convolutional NN
accuracy
0.97
0.91
0.91
Linear Regression
Synthetic 1D
R2
0.876
0.863
0.863
Logistic Regression
Synthetic 2D with 2 classes
accuracy
0.90
0.875
0.875
Poisson Regression
mean Poisson deviance
0.61
0.60
0.60
Gamma Regression
mean Gamma deviance
0.45
0.45
0.45
Tweedie Regression
mean Tweedie deviance (power=1.9)
33.42
34.18
34.18
Decision Tree
precision score
0.95
0.97
0.97*
XGBoost Classifier
MCC
0.48
0.52
0.52*
XGBoost Regressor
R2
0.92
0.90
0.90*
Documentation with GitBook is done mainly by pushing content on GitHub. GitBook then pulls the docs from the repository and publishes. In most cases, GitBook is just a mirror of what is available in GitHub.
There are, however, some use-cases where documentation can be modified directly in GitBook (and then, push the modifications to GitHub), for example when the documentation is modified by a person outside of Zama. In this case, a GitHub branch is created, and a GitHub space is associated to it: modifications are done in this space and automatically pushed to the branch. Once the modifications are done, one can simply create a pull-request, to finally merge modifications on the main branch.
Documentation can alternatively be built using Sphinx:
The documentation contains both files written by hand by developers (the .md files) and files automatically created by parsing the source files.
Then to open it, go to docs/_build/html/index.html
or use the follwing command:
To build and open the docs at the same time, use:
Concrete-ML is a constant work-in-progress, and thus may contain bugs or suboptimal APIs.
Before opening an issue or asking for support, please read this documentation to understand common issues and limitations of Concrete-ML. You can also check the outstanding issues on github.
Furthermore, undefined behavior may occur if the input-set, which is internally used by the compilation core to set bit-widths of some intermediate data, is not sufficiently representative of the future user inputs. With all the inputs in the input-set, it appears that intermediate data can be represented as an n-bit integer. But, for a particular computation, this same intermediate data needs additional bits to be represented. The FHE execution for this computation will result in an incorrect output, as typically occurs in integer overflows in classical programs.
If you didn't find an answer, you can ask a question on the Zama forum, or in the FHE.org discord.
When submitting an issue (here), ideally include as much information as possible. In addition to the Python script, the following information is useful:
the reproducibility rate you see on your side
any insight you might have on the bug
any workaround you have been able to find
If you would like to contribute to project and send pull requests, take a look at the contributor guide.
There are three ways to contribute to Concrete-ML:
You can open issues to report bugs and typos and to suggest ideas.
You can ask to become an official contributor by emailing hello@zama.ai. Only approved contributors can send pull requests (PR), so please make sure to get in touch before you do.
You can also provide new tutorials or use-cases, showing what can be done with the library. The more examples we have, the better and clearer it is for the other users.
Concrete-ML uses a consistent branch naming scheme, and you are expected to follow it as well. Here is the format, along with some examples:
e.g.
Each commit to Concrete-ML should conform to the standards of the project. You can let the development tools fix some issues automatically with the following command:
Conformance can be checked using the following command:
Your code must be well documented, containing tests and not breaking other tests:
You need to make sure you get 100% code coverage. The make pytest
command checks that by default and will fail with a coverage report at the end should some lines of your code not be executed during testing.
If your coverage is below 100%, you should write more tests and then create the pull request. If you ignore this warning and create the PR, GitHub actions will fail and your PR will not be merged.
There may be cases where covering your code is not possible (an exception that cannot be triggered in normal execution circumstances). In those cases, you may be allowed to disable coverage for some specific lines. This should be the exception rather than the rule, and reviewers will ask why some lines are not covered. If it appears they can be covered, then the PR won't be accepted in that state.
Concrete-ML uses a consistent commit naming scheme, and you are expected to follow it as well (the CI will make sure you do). The accepted format can be printed to your terminal by running:
e.g.
To learn more about conventional commits, check this page. Just a reminder that commit messages are checked in the comformance step and are rejected if they don't follow the rules.
You should rebase on top of the main
branch before you create your pull request. Merge commits are not allowed, so rebasing on main
before pushing gives you the best chance of avoiding having to rewrite parts of your PR later if conflicts arise with other PRs being merged. After you commit your changes to your new branch, you can use the following commands to rebase:
You can learn more about rebasing here.
Before any final release, Concrete-ML contributors go through a release candidate (RC) cycle. The idea is that once the codebase and documentations look ready for a release, you create an RC release by opening an issue with the release template here, starting with version vX.Y.Zrc1
and then with versions vX.Y.Zrc2
, vX.Y.Zrc3
...
Once the last RC is deemed ready, open an issue with the release template using the last RC version from which you remove the rc?
part (i.e. v12.67.19
if your last RC version was v12.67.19-rc4
) on github.
concrete.ml.common
: Module for shared data structures and code.
concrete.ml.common.check_inputs
: Check and conversion tools.
concrete.ml.common.debugging
: Module for debugging.
concrete.ml.common.debugging.custom_assert
: Provide some variants of assert.
concrete.ml.common.utils
: Utils that can be re-used by other pieces of code in the module.
concrete.ml.deployment
: Module for deployment of the FHE model.
concrete.ml.deployment.fhe_client_server
: APIs for FHE deployment.
concrete.ml.onnx
: ONNX module.
concrete.ml.onnx.convert
: ONNX conversion related code.
concrete.ml.onnx.onnx_model_manipulations
: Some code to manipulate models.
concrete.ml.onnx.onnx_utils
: Utils to interpret an ONNX model with numpy.
concrete.ml.onnx.ops_impl
: ONNX ops implementation in python + numpy.
concrete.ml.quantization
: Modules for quantization.
concrete.ml.quantization.base_quantized_op
: Base Quantized Op class that implements quantization for a float numpy op.
concrete.ml.quantization.post_training
: Post Training Quantization methods.
concrete.ml.quantization.quantized_module
: QuantizedModule API.
concrete.ml.quantization.quantized_ops
: Quantized versions of the ONNX operators for post training quantization.
concrete.ml.quantization.quantizers
: Quantization utilities for a numpy array/tensor.
concrete.ml.sklearn
: Import sklearn models.
concrete.ml.sklearn.base
: Module that contains base classes for our libraries estimators.
concrete.ml.sklearn.glm
: Implement sklearn's Generalized Linear Models (GLM).
concrete.ml.sklearn.linear_model
: Implement sklearn linear model.
concrete.ml.sklearn.protocols
: Protocols.
concrete.ml.sklearn.qnn
: Scikit-learn interface for concrete quantized neural networks.
concrete.ml.sklearn.rf
: Implements RandomForest models.
concrete.ml.sklearn.svm
: Implement Support Vector Machine.
concrete.ml.sklearn.torch_module
: Implement torch module.
concrete.ml.sklearn.tree
: Implement the sklearn tree models.
concrete.ml.sklearn.tree_to_numpy
: Implements the conversion of a tree model to a numpy function.
concrete.ml.sklearn.xgb
: Implements XGBoost models.
concrete.ml.torch
: Modules for torch to numpy conversion.
concrete.ml.torch.compile
: torch compilation function.
concrete.ml.torch.numpy_module
: A torch to numpy module.
concrete.ml.version
: File to manage the version of the package.
fhe_client_server.FHEModelClient
: Client API to encrypt and decrypt FHE data.
fhe_client_server.FHEModelDev
: Dev API to save the model and then load and run the FHE circuit.
fhe_client_server.FHEModelServer
: Server API to load and run the FHE circuit.
ops_impl.ONNXMixedFunction
: A mixed quantized-raw valued onnx function.
base_quantized_op.QuantizedOp
: Base class for quantized ONNX ops implemented in numpy.
post_training.ONNXConverter
: Base ONNX to Concrete ML computation graph conversion class.
post_training.PostTrainingAffineQuantization
: Post-training Affine Quantization.
post_training.PostTrainingQATImporter
: Converter of Quantization Aware Training networks.
quantized_module.QuantizedModule
: Inference for a quantized model.
quantized_ops.QuantizedAbs
: Quantized Abs op.
quantized_ops.QuantizedAdd
: Quantized Addition operator.
quantized_ops.QuantizedAvgPool
: Quantized Average Pooling op.
quantized_ops.QuantizedBatchNormalization
: Quantized Batch normalization with encrypted input and in-the-clear normalization params.
quantized_ops.QuantizedBrevitasQuant
: Brevitas uniform quantization with encrypted input.
quantized_ops.QuantizedCast
: Cast the input to the required data type.
quantized_ops.QuantizedCelu
: Quantized Celu op.
quantized_ops.QuantizedClip
: Quantized clip op.
quantized_ops.QuantizedConv
: Quantized Conv op.
quantized_ops.QuantizedDiv
: Div operator /.
quantized_ops.QuantizedElu
: Quantized Elu op.
quantized_ops.QuantizedErf
: Quantized erf op.
quantized_ops.QuantizedExp
: Quantized Exp op.
quantized_ops.QuantizedFlatten
: Quantized flatten for encrypted inputs.
quantized_ops.QuantizedGemm
: Quantized Gemm op.
quantized_ops.QuantizedGreater
: Comparison operator >.
quantized_ops.QuantizedGreaterOrEqual
: Comparison operator >=.
quantized_ops.QuantizedHardSigmoid
: Quantized HardSigmoid op.
quantized_ops.QuantizedHardSwish
: Quantized Hardswish op.
quantized_ops.QuantizedIdentity
: Quantized Identity op.
quantized_ops.QuantizedLeakyRelu
: Quantized LeakyRelu op.
quantized_ops.QuantizedLess
: Comparison operator <.
quantized_ops.QuantizedLessOrEqual
: Comparison operator <=.
quantized_ops.QuantizedLog
: Quantized Log op.
quantized_ops.QuantizedMatMul
: Quantized MatMul op.
quantized_ops.QuantizedMul
: Multiplication operator.
quantized_ops.QuantizedNot
: Quantized Not op.
quantized_ops.QuantizedOr
: Or operator ||.
quantized_ops.QuantizedPRelu
: Quantized PRelu op.
quantized_ops.QuantizedPad
: Quantized Padding op.
quantized_ops.QuantizedPow
: Quantized pow op.
quantized_ops.QuantizedReduceSum
: ReduceSum with encrypted input.
quantized_ops.QuantizedRelu
: Quantized Relu op.
quantized_ops.QuantizedReshape
: Quantized Reshape op.
quantized_ops.QuantizedRound
: Quantized round op.
quantized_ops.QuantizedSelu
: Quantized Selu op.
quantized_ops.QuantizedSigmoid
: Quantized sigmoid op.
quantized_ops.QuantizedSoftplus
: Quantized Softplus op.
quantized_ops.QuantizedSub
: Subtraction operator.
quantized_ops.QuantizedTanh
: Quantized Tanh op.
quantized_ops.QuantizedTranspose
: Transpose operator for quantized inputs.
quantized_ops.QuantizedWhere
: Where operator on quantized arrays.
quantizers.MinMaxQuantizationStats
: Calibration set statistics.
quantizers.QuantizationOptions
: Options for quantization.
quantizers.QuantizedArray
: Abstraction of quantized array.
quantizers.UniformQuantizationParameters
: Quantization parameters for uniform quantization.
quantizers.UniformQuantizer
: Uniform quantizer.
base.BaseTreeClassifierMixin
: Mixin class for tree-based classifiers.
base.BaseTreeEstimatorMixin
: Mixin class for tree-based estimators.
base.BaseTreeRegressorMixin
: Mixin class for tree-based regressors.
base.QuantizedTorchEstimatorMixin
: Mixin that provides quantization for a torch module and follows the Estimator API.
base.SklearnLinearClassifierMixin
: A Mixin class for sklearn linear classifiers with FHE.
base.SklearnLinearModelMixin
: A Mixin class for sklearn linear models with FHE.
glm.GammaRegressor
: A Gamma regression model with FHE.
glm.PoissonRegressor
: A Poisson regression model with FHE.
glm.TweedieRegressor
: A Tweedie regression model with FHE.
linear_model.ElasticNet
: An ElasticNet regression model with FHE.
linear_model.Lasso
: A Lasso regression model with FHE.
linear_model.LinearRegression
: A linear regression model with FHE.
linear_model.LogisticRegression
: A logistic regression model with FHE.
linear_model.Ridge
: A Ridge regression model with FHE.
protocols.ConcreteBaseClassifierProtocol
: Concrete classifier protocol.
protocols.ConcreteBaseEstimatorProtocol
: A Concrete Estimator Protocol.
protocols.ConcreteBaseRegressorProtocol
: Concrete regressor protocol.
protocols.Quantizer
: Quantizer Protocol.
qnn.FixedTypeSkorchNeuralNet
: A mixin with a helpful modification to a skorch estimator that fixes the module type.
qnn.NeuralNetClassifier
: Scikit-learn interface for quantized FHE compatible neural networks.
qnn.NeuralNetRegressor
: Scikit-learn interface for quantized FHE compatible neural networks.
qnn.QuantizedSkorchEstimatorMixin
: Mixin class that adds quantization features to Skorch NN estimators.
qnn.SparseQuantNeuralNetImpl
: Sparse Quantized Neural Network classifier.
rf.RandomForestClassifier
: Implements the RandomForest classifier.
rf.RandomForestRegressor
: Implements the RandomForest regressor.
svm.LinearSVC
: A Classification Support Vector Machine (SVM).
svm.LinearSVR
: A Regression Support Vector Machine (SVM).
tree.DecisionTreeClassifier
: Implements the sklearn DecisionTreeClassifier.
tree.DecisionTreeRegressor
: Implements the sklearn DecisionTreeClassifier.
tree_to_numpy.Task
: Task enumerate.
xgb.XGBClassifier
: Implements the XGBoost classifier.
xgb.XGBRegressor
: Implements the XGBoost regressor.
numpy_module.NumpyModule
: General interface to transform a torch.nn.Module to numpy module.
check_inputs.check_X_y_and_assert
: sklearn.utils.check_X_y with an assert.
check_inputs.check_array_and_assert
: sklearn.utils.check_array with an assert.
custom_assert.assert_false
: Provide a custom assert to check that the condition is False.
custom_assert.assert_not_reached
: Provide a custom assert to check that a piece of code is never reached.
custom_assert.assert_true
: Provide a custom assert to check that the condition is True.
utils.generate_proxy_function
: Generate a proxy function for a function accepting only *args type arguments.
utils.get_onnx_opset_version
: Return the ONNX opset_version.
utils.replace_invalid_arg_name_chars
: Sanitize arg_name, replacing invalid chars by _.
convert.get_equivalent_numpy_forward
: Get the numpy equivalent forward of the provided ONNX model.
convert.get_equivalent_numpy_forward_and_onnx_model
: Get the numpy equivalent forward of the provided torch Module.
onnx_model_manipulations.clean_graph_after_node
: Clean the graph of the onnx model by removing nodes after the given node name.
onnx_model_manipulations.keep_following_outputs_discard_others
: Keep the outputs given in outputs_to_keep and remove the others from the model.
onnx_model_manipulations.remove_identity_nodes
: Remove identity nodes from a model.
onnx_model_manipulations.remove_node_types
: Remove unnecessary nodes from the ONNX graph.
onnx_model_manipulations.remove_unused_constant_nodes
: Remove unused Constant nodes in the provided onnx model.
onnx_model_manipulations.simplify_onnx_model
: Simplify an ONNX model, removes unused Constant nodes and Identity nodes.
onnx_utils.execute_onnx_with_numpy
: Execute the provided ONNX graph on the given inputs.
onnx_utils.get_attribute
: Get the attribute from an ONNX AttributeProto.
onnx_utils.get_op_name
: Construct the qualified name of the ONNX operator.
ops_impl.cast_to_float
: Cast values to floating points.
ops_impl.numpy_abs
: Compute abs in numpy according to ONNX spec.
ops_impl.numpy_acos
: Compute acos in numpy according to ONNX spec.
ops_impl.numpy_acosh
: Compute acosh in numpy according to ONNX spec.
ops_impl.numpy_add
: Compute add in numpy according to ONNX spec.
ops_impl.numpy_asin
: Compute asin in numpy according to ONNX spec.
ops_impl.numpy_asinh
: Compute sinh in numpy according to ONNX spec.
ops_impl.numpy_atan
: Compute atan in numpy according to ONNX spec.
ops_impl.numpy_atanh
: Compute atanh in numpy according to ONNX spec.
ops_impl.numpy_batchnorm
: Compute the batch normalization of the input tensor.
ops_impl.numpy_cast
: Execute ONNX cast in Numpy.
ops_impl.numpy_celu
: Compute celu in numpy according to ONNX spec.
ops_impl.numpy_constant
: Return the constant passed as a kwarg.
ops_impl.numpy_cos
: Compute cos in numpy according to ONNX spec.
ops_impl.numpy_cosh
: Compute cosh in numpy according to ONNX spec.
ops_impl.numpy_div
: Compute div in numpy according to ONNX spec.
ops_impl.numpy_elu
: Compute elu in numpy according to ONNX spec.
ops_impl.numpy_equal
: Compute equal in numpy according to ONNX spec.
ops_impl.numpy_erf
: Compute erf in numpy according to ONNX spec.
ops_impl.numpy_exp
: Compute exponential in numpy according to ONNX spec.
ops_impl.numpy_flatten
: Flatten a tensor into a 2d array.
ops_impl.numpy_greater
: Compute greater in numpy according to ONNX spec.
ops_impl.numpy_greater_float
: Compute greater in numpy according to ONNX spec and cast outputs to floats.
ops_impl.numpy_greater_or_equal
: Compute greater or equal in numpy according to ONNX spec.
ops_impl.numpy_greater_or_equal_float
: Compute greater or equal in numpy according to ONNX specs and cast outputs to floats.
ops_impl.numpy_hardsigmoid
: Compute hardsigmoid in numpy according to ONNX spec.
ops_impl.numpy_hardswish
: Compute hardswish in numpy according to ONNX spec.
ops_impl.numpy_identity
: Compute identity in numpy according to ONNX spec.
ops_impl.numpy_leakyrelu
: Compute leakyrelu in numpy according to ONNX spec.
ops_impl.numpy_less
: Compute less in numpy according to ONNX spec.
ops_impl.numpy_less_float
: Compute less in numpy according to ONNX spec and cast outputs to floats.
ops_impl.numpy_less_or_equal
: Compute less or equal in numpy according to ONNX spec.
ops_impl.numpy_less_or_equal_float
: Compute less or equal in numpy according to ONNX spec and cast outputs to floats.
ops_impl.numpy_log
: Compute log in numpy according to ONNX spec.
ops_impl.numpy_matmul
: Compute matmul in numpy according to ONNX spec.
ops_impl.numpy_mul
: Compute mul in numpy according to ONNX spec.
ops_impl.numpy_not
: Compute not in numpy according to ONNX spec.
ops_impl.numpy_not_float
: Compute not in numpy according to ONNX spec and cast outputs to floats.
ops_impl.numpy_or
: Compute or in numpy according to ONNX spec.
ops_impl.numpy_or_float
: Compute or in numpy according to ONNX spec and cast outputs to floats.
ops_impl.numpy_pow
: Compute pow in numpy according to ONNX spec.
ops_impl.numpy_relu
: Compute relu in numpy according to ONNX spec.
ops_impl.numpy_round
: Compute round in numpy according to ONNX spec.
ops_impl.numpy_selu
: Compute selu in numpy according to ONNX spec.
ops_impl.numpy_sigmoid
: Compute sigmoid in numpy according to ONNX spec.
ops_impl.numpy_sin
: Compute sin in numpy according to ONNX spec.
ops_impl.numpy_sinh
: Compute sinh in numpy according to ONNX spec.
ops_impl.numpy_softmax
: Compute softmax in numpy according to ONNX spec.
ops_impl.numpy_softplus
: Compute softplus in numpy according to ONNX spec.
ops_impl.numpy_sub
: Compute sub in numpy according to ONNX spec.
ops_impl.numpy_tan
: Compute tan in numpy according to ONNX spec.
ops_impl.numpy_tanh
: Compute tanh in numpy according to ONNX spec.
ops_impl.numpy_thresholdedrelu
: Compute thresholdedrelu in numpy according to ONNX spec.
ops_impl.numpy_transpose
: Transpose in numpy according to ONNX spec.
ops_impl.numpy_where
: Compute the equivalent of numpy.where.
ops_impl.numpy_where_body
: Compute the equivalent of numpy.where.
ops_impl.onnx_func_raw_args
: Decorate a numpy onnx function to flag the raw/non quantized inputs.
ops_impl.torch_avgpool
: Compute Average Pooling using Torch.
quantizers.fill_from_kwargs
: Fill a parameter set structure from kwargs parameters.
tree_to_numpy.tree_to_numpy
: Convert the tree inference to a numpy functions using Hummingbird.
compile.compile_brevitas_qat_model
: Compile a Brevitas Quantization Aware Training model.
compile.compile_onnx_model
: Compile a torch module into an FHE equivalent.
compile.compile_torch_model
: Compile a torch module into an FHE equivalent.
compile.convert_torch_tensor_or_numpy_array_to_numpy_array
: Convert a torch tensor or a numpy array to a numpy array.
Hummingbird is a third-party, open-source library that converts machine learning models into tensor computations, and it can export these models to ONNX. The list of supported models can be found in the Hummingbird documentation.
Concrete-ML allows the conversion of an ONNX inference to NumPy inference (note that NumPy is always the entry point to run models in FHE with Concrete ML).
Hummingbird exposes a convert
function that can be imported as follows from the hummingbird.ml
package:
This function can be used to convert a machine learning model to an ONNX as follows:
In theory, the resulting onnx_model
could be used directly within Concrete-ML's get_equivalent_numpy_forward
method (as long as all operators present in the ONNX model are implemented in NumPy) and get the NumPy inference.
In practice, there are some steps needed to clean the ONNX output and make the graph compatible with Concrete-ML, such as applying quantization where needed or deleting/replacing non-FHE friendly ONNX operators (such as Softmax and ArgMax).
Concrete-ML uses Skorch to implement multi-layer, fully-connected PyTorch neural networks in a way that is compatible with the Scikit-learn API.
This wrapper implements Torch training boilerplate code, alleviating the work that needs to be done by the user. It is possible to add hooks during the training phase, for example once an epoch is finished.
Skorch allows the user to easily create a classifier or regressor around a neural network (NN), implemented in Torch as a nn.Module
, which is used by Concrete-ML to provide a fully-connected multi-layer NN with a configurable number of layers and optional pruning (see pruning and the neural network documentation for more information).
Under the hood, Concrete-ML uses a Skorch wrapper around a single PyTorch module, SparseQuantNeuralNetImpl
. More information can be found in the API guide.
Brevitas is a quantization aware learning toolkit built on top of PyTorch. It provides quantization layers that are one-to-one equivalents to PyTorch layers, but also contain operations that perform the quantization during training.
While Brevitas provides many types of quantization, for Concrete-ML, a custom "mixed integer" quantization applies. This "mixed integer" quantization is much simpler than the "integer only" mode of Brevitas. The "mixed integer" network design is defined as:
all weights and activations of convolutional, linear and pooling layers must be quantized (e.g. using Brevitas layers, QuantConv2D
, QuantAvgPool2D
, QuantLinear
)
PyTorch floating point versions of univariate functions can be used. E.g. torch.relu
, nn.BatchNormalization2D
, torch.max
(encrypted vs. constant), torch.add
, torch.exp
. See the PyTorch supported layers page for a full list.
The "mixed integer" mode used in Concrete-ML neural networks is based on the "integer only" Brevitas quantization that makes both weights and activations representable as integers during training. However, through the use of lookup tables in Concrete-ML, floating point univariate PyTorch functions are supported.
For "mixed integer" quantization to work, the first layer of a Brevitas nn.Module
must be a QuantIdentity
layer. However, you can then use functions such as torch.sigmoid
on the result of such a quantizing operation.
For examples of such a "mixed integer" network design, please see the Quantization Aware Training examples:
or go to the MNIST use-case example.
You can also refer to the SparseQuantNeuralNetImpl
class which is the basis of the built-in NeuralNetworkClassifier
.
Compilation of a model produces machine code that executes the model on encrypted data. In some cases, notably in the client/server setting, the compilation can be done by the server when loading the model for serving.
As FHE execution is much slower than execution on non-encrypted data, Concrete-ML has a simulation mode, using an execution mode named the Virtual Library. Since, by default, the cryptographic parameters are chosen such that the results obtained in FHE are the same as those on clear data, the Virtual Library allows you to benchmark models quickly during development.
Concrete-ML implements machine model inference using Concrete-Numpy as a backend. In order to execute in FHE, a numerical program written in Concrete-Numpy needs to be compiled. This functionality is described here, and Concrete-ML hides away most of the complexity of this step. The entire compilation process is done by Concrete-Numpy.
From the perspective of the Concrete-ML user, the compilation process performed by Concrete-Numpy can be broken up into 3 steps:
Numpy program tracing and creation of a Concrete-Numpy op-graph
checking that the op-graph is FHE compatible
producing machine code for the op-graph. This step automatically determines cryptographic parameters
Additionally, the client/server API packages the result of the last step in a way that allows the deployment of the encrypted circuit to a server and key generation, encryption and decryption on the client side.
The first step in the list above takes a Python function implemented using the Concrete-Numpy supported operation set and transforms it into an executable operation graph.
The result of this single step of the compilation pipeline allows the:
execution of the op-graph, which includes TLUs, on clear non-encrypted data. This is, of course, not secure, but it is much faster than executing in FHE. This mode is useful for debugging, i.e. to find the appropriate hyper-parameters. This mode is called the Virtual Library.
verification of the maximum bit-width of the op-graph, to determine FHE compatibility, without actually compiling the circuit to machine code.
Enabling Virtual Library execution requires the definition of a compilation Configuration
. As simulation does not execute in FHE, this can be considered unsafe:
Next, the following code uses the simulation mode for built-in models:
And finally, for custom models, it is possible to enable simulation using the following syntax:
Obtaining the simulated predictions of the models using the Virtual Library has the same syntax as execution in FHE:
Moreover, the maximum accumulator bit-width is determined as follows:
While Concrete-ML hides away all the Concrete-Numpy code that performs model inference, it can be useful to understand how Concrete-Numpy code works. Here is an toy example for a simple linear regression model on integers. Note that this is just an example to illustrate compilation concepts. Generally, it is recommended to use the built-in models, which provide linear regression out of the box.
Internally, Concrete-ML uses ONNX operators as intermediate representation (or IR) for manipulating machine learning models produced through export for PyTorch, Hummingbird and skorch.
As ONNX is becoming the standard exchange format for neural networks, this allows Concrete-ML to be flexible while also making model representation manipulation quite easy. In addition, it allows for straight-forward mapping to NumPy operators, supported by Concrete-Numpy to use Concrete stack's FHE conversion capabilities.
The diagram below gives an overview of the steps involved in the conversion of an ONNX graph to a FHE compatible format, i.e. a format that can be compiled to FHE through Concrete-Numpy.
All Concrete-ML built-in models follow the same pattern for FHE conversion:
The models are trained with sklearn or PyTorch
All models have a PyTorch implementation for inference. This implementation is provided either by a third-party tool such as Hummingbird or implemented directly in Concrete-ML.
The PyTorch model is exported to ONNX. For more information on the use of ONNX in Concrete-ML, see here.
The Concrete-ML ONNX parser checks that all the operations in the ONNX graph are supported and assigns reference NumPy operations to them. This step produces a NumpyModule
.
Quantization is performed on the NumpyModule
, producing a QuantizedModule
. Two steps are performed: calibration and assignment of equivalent QuantizedOp
objects to each ONNX operation. The QuantizedModule
class is the quantized counterpart of the NumpyModule
.
Once the QuantizedModule
is built, Concrete-Numpy is used to trace the ._forward()
function of the QuantizedModule
.
Moreover, by passing a user provided nn.Module
to step 2 of the above process, Concrete-ML supports custom user models. See the associated FHE-friendly model documentation for instructions about working with such models.
Once an ONNX model is imported, it is converted to a NumpyModule
, then to a QuantizedModule
and, finally, to a FHE circuit. However, as the diagram shows, it is perfectly possible to stop at the NumpyModule
level if you just want to run the PyTorch model as NumPy code without doing quantization.
Note that the NumpyModule
interpreter currently supports the following ONNX operators.
In order to better understand how Concrete-ML works under the hood, it is possible to access each model in their ONNX format and then either print it or visualize it by importing the associated file in Netron. For example, with LogisticRegression
:
The ONNX import section gave an overview of the conversion of a generic ONNX graph to a FHE compatible Concrete-ML op-graph. This section describes the implementation of operations in the Concrete-ML op-graph and the way floating point can be used in some parts of the op-graphs through table lookup operations.
Concrete, the underlying implementation of TFHE that powers Concrete-ML, enables two types of operations on integers:
arithmetic operations: the addition of two encrypted values and multiplication of encrypted values with clear scalars. These are used, for example, in dot-products, matrix multiplication (linear layers) and convolution.
table lookup operations (TLU): using an encrypted value as an index, return the value of a lookup table at that index. This is implemented using Programmable Bootstrapping. This operation is used to perform any non-linear computation such as activation functions, quantization and normalization.
Since machine learning models use floating point inputs and weights, they first need to be converted to integers using quantization.
Alternatively, it is possible to use a table lookup to avoid the quantization of the entire graph, by converting floating-point ONNX subgraphs into lambdas and computing their corresponding lookup tables to be evaluated directly in FHE. This operator-fusion technique only requires the input and output of the lambdas to be integers.
For example, in the following graph there is a single input, which must be an encrypted integer tensor. The following series of univariate functions is then fed into a matrix multiplication (MatMul) and fused into a single table lookup with integer inputs and outputs.
Concrete-ML implements ONNX operations using Concrete-Numpy, which can handle floating point operations, as long as they can be fused to an integer lookup table. The ONNX operations implementations are based on the QuantizedOp
class.
There are two modes of creation of a single table lookup for a chain of ONNX operations:
float mode: when the operation can be fused
mixed float/integer: when the ONNX operation needs to perform arithmetic operations
Thus, QuantizedOp
instances may need to quantize their inputs or the result of their computation, depending on their position in the graph.
The QuantizedOp
class provides a generic implementation of an ONNX operation, including the quantization of inputs and outputs, with the computation implemented in NumPy in ops_impl.py
. It is possible to picture at the architecture of the QuantizedOp
as the following structure:
This figure shows that the QuantizedOp
has a body that implements the computation of the operation, following the ONNX spec. The operation's body can take either integer or float inputs and can output float or integer values. Two quantizers are attached to the operation: one that takes float inputs and produces integer inputs and one that does the same for the output.
Depending on the position of the op in the graph and its inputs, the QuantizedOp
can be fully fused to a TLU.
Many ONNX ops are trivially univariate, as they multiply variable inputs with constants or apply univariate functions such as ReLU, Sigmoid, etc. This includes operations between the input and the MatMul in the graph above (subtraction, comparison, multiplication, etc. between inputs and constants).
Operations, such as matrix multiplication of encrypted inputs with a constant matrix or convolution with constant weights, require that the encrypted inputs be integers. In this case, the input quantizer of the QuantizedOp
is applied. These types of operations are implemented with a class that derives from QuantizedOp
and implements q_impl
, such as QuantizedGemm
and QuantizedConv
.
Finally, some operations produce graph outputs, which must be integers. These operations need to quantize their outputs as follows:
The diagram above shows that both float ops and integer ops need to quantize their outputs to integers, when placed at the end of the graph.
To chain the operation types described above following the ONNX graph, Concrete-ML constructs a function that calls the q_impl
of the QuantizedOp
instances in the graph in sequence, and uses Concrete-Numpy to trace the execution and compile to FHE. Thus, in this chain of function calls, all groups of that instruction that operate in floating point will be fused to TLUs. In FHE, this lookup table is computed with a PBS.
The red contours show the groups of elementary Concrete-Numpy instructions that will be converted to TLUs.
Note that the input is slightly different from the QuantizedOp
. Since the encrypted function takes integers as inputs, the input needs to be de-quantized first.
QuantizedOp
QuantizedOp
is the base class for all ONNX-quantized operators. It abstracts away many things to allow easy implementation of new quantized ops.
The QuantizedOp
class exposes a function can_fuse
that
helps to determine the type of implementation that will be traced
determines whether operations further in the graph, that depend on the results of this operation, can fuse
In most cases, ONNX ops have a single variable input and one or more constant inputs.
When the op implements elementwise operations between the inputs and constants (addition, subtract, multiplication, etc), the operation can be fused to a TLU. Thus, by default in QuantizedOp
, the can_fuse
function returns True
.
When the op implements operations that mix the various scalars in the input encrypted tensor, the operation cannot fuse, as table lookups are univariate. Thus, operations such as QuantizedGemm
, QuantizedConv
return False
in can_fuse
.
Some operations may be found in both settings above. A mechanism is implemented in Concrete-ML to determine if the inputs of a QuantizedOp
are produced by a unique integer tensor. Therefore, the can_fuse
function of some QuantizedOp
types (addition, subtraction) will allow fusion to take place if both operands are produced by a unique integer tensor:
You can check ops_impl.py
to see how some operations are implemented in NumPy. The declaration convention for these operations is as follows:
The required inputs should be positional arguments only before the /
, which marks the limit of the positional arguments
The optional inputs should be positional or keyword arguments between the /
and *
, which marks the limits of positional or keyword arguments
The operator attributes should be keyword arguments only after the *
The proper use of positional/keyword arguments is required to allow the QuantizedOp
class to properly populate metadata automatically. It uses Python inspect modules and stores relevant information for each argument related to its positional/keyword status. This allows using the Concrete-NumPy implementation as specifications for QuantizedOp
, which removes some data duplication and allows having a single source of truth for QuantizedOp
and ONNX-NumPy implementations.
In that case (unless the quantized implementation requires special handling like QuantizedGemm
), you can just set _impl_for_op_named
to the name of the ONNX op for which the quantized class is implemented (this uses the mapping ONNX_OPS_TO_NUMPY_IMPL
in onnx_utils.py
to get the correct implementation).
Providing an integer implementation requires sub-classing QuantizedOp
to create a new operation. This sub-class must override q_impl
in order to provide an integer implementation. QuantizedGemm
is an example of such a case where quantized matrix multiplication requires proper handling of scales and zero points. The q_impl
of that class reflects this.
In the body of q_impl
, in order to obtain quantized integer values you can use the _prepare_inputs_with_constants
function as such:
Here, prepared_inputs
will contain one or more QuantizedArray
of which the qvalues
are the quantized integers.
Once the required integer processing code is implemented, the output of the q_impl
function must be implemented as a single QuantizedArray
. Most commonly, this is built using the de-quantized results of the processing done in q_impl
.
In this case, in q_impl
you can check whether the current operation can be fused by calling self.can_fuse()
. You can then have both a floating point and an integer implementation. The traced execution path will depend on can_fuse()
:
concrete.ml.common.utils
Utils that can be re-used by other pieces of code in the module.
DEFAULT_P_ERROR_PBS
replace_invalid_arg_name_chars
Sanitize arg_name, replacing invalid chars by _.
This does not check that the starting character of arg_name is valid.
Args:
arg_name
(str): the arg name to sanitize.
Returns:
str
: the sanitized arg name, with only chars in _VALID_ARG_CHARS.
generate_proxy_function
Generate a proxy function for a function accepting only *args type arguments.
This returns a runtime compiled function with the sanitized argument names passed in desired_functions_arg_names as the arguments to the function.
Args:
function_to_proxy
(Callable): the function defined like def f(*args) for which to return a function like f_proxy(arg_1, arg_2) for any number of arguments.
desired_functions_arg_names
(Iterable[str]): the argument names to use, these names are sanitized and the mapping between the original argument name to the sanitized one is returned in a dictionary. Only the sanitized names will work for a call to the proxy function.
Returns:
Tuple[Callable, Dict[str, str]]
: the proxy function and the mapping of the original arg name to the new and sanitized arg names.
get_onnx_opset_version
Return the ONNX opset_version.
Args:
onnx_model
(onnx.ModelProto): the model.
Returns:
int
: the version of the model
concrete.ml.common.check_inputs
Check and conversion tools.
Utils that are used to check (including convert) some data types which are compatible with scikit-learn to numpy types.
check_array_and_assert
sklearn.utils.check_array with an assert.
Equivalent of sklearn.utils.check_array, with a final assert that the type is one which is supported by Concrete-ML.
Args:
X
(object): Input object to check / convert
Returns: The converted and validated array
check_X_y_and_assert
sklearn.utils.check_X_y with an assert.
Equivalent of sklearn.utils.check_X_y, with a final assert that the type is one which is supported by Concrete-ML.
Args:
X
(ndarray, list, sparse matrix): Input data
y
(ndarray, list, sparse matrix): Labels
*args
: The arguments to pass to check_X_y
**kwargs
: The keyword arguments to pass to check_X_y
Returns: The converted and validated arrays
Concrete-ML has support for quantized ML models and also provides quantization tools for Quantization Aware Training and Post-Training Quantization. The core of this functionality is the conversion of floating point values to integers and back. This is done using QuantizedArray
in concrete.ml.quantization
.
The class takes several arguments that determine how float values are quantized:
n_bits
that defines the precision of the quantization
values
are floating point values that will be converted to integers
is_signed
determines if the quantized integer values should allow negative values
is_symmetric
determines if the range of floating point values to be quantized should be taken as symmetric around zero
See also the reference for more information:
It is also possible to use symmetric quantization, where the integer values are centered around 0:
In the following example, showing the de-quantization of model outputs, the QuantizedArray
class is used in a different way. Here it uses pre-quantized integer values and has the scale
and zero-point
set explicitly. Once the QuantizedArray
is constructed, calling dequant()
will compute the floating point values corresponding to the integer values qvalues
, which are the output of the forward_fhe.encrypt_run_decrypt(..)
call.
Machine learning models are implemented with a diverse set of operations, such as convolution, linear transformations, activation functions and element-wise operations. When working with quantized values, these operations cannot be carried out in an equivalent way as for floating point values. With quantization, it is necessary to re-scale the input and output values of each operation to fit in the quantization domain.
In Concrete-ML, the quantized equivalent of a scikit-learn model or a PyTorch nn.Module
is the QuantizedModule
. Note that only inference is implemented in the QuantizedModule
, and it is built through a conversion of the inference function of the corresponding scikit-learn or PyTorch module.
Built-in neural networks expose the quantized_module
member, while a QuantizedModule
is also the result of the compilation of custom models through compile_torch_model
and compile_brevitas_qat_model
.
Calibration is the process of determining the typical distributions of values encountered for the intermediate values of a model during inference.
concrete.ml.common.debugging.custom_assert
Provide some variants of assert.
assert_true
Provide a custom assert to check that the condition is True.
Args:
condition
(bool): the condition. If False, raise AssertionError
on_error_msg
(str): optional message for precising the error, in case of error
error_type
(Type[Exception]): the type of error to raise, if condition is not fulfilled. Default to AssertionError
assert_false
Provide a custom assert to check that the condition is False.
Args:
condition
(bool): the condition. If True, raise AssertionError
on_error_msg
(str): optional message for precising the error, in case of error
error_type
(Type[Exception]): the type of error to raise, if condition is not fulfilled. Default to AssertionError
assert_not_reached
Provide a custom assert to check that a piece of code is never reached.
Args:
on_error_msg
(str): message for precising the error
error_type
(Type[Exception]): the type of error to raise, if condition is not fulfilled. Default to AssertionError
concrete.ml.deployment.fhe_client_server
APIs for FHE deployment.
CML_VERSION
AVAILABLE_MODEL
FHEModelServer
Server API to load and run the FHE circuit.
__init__
Initialize the FHE API.
Args:
path_dir
(str): the path to the directory where the circuit is saved
load
Load the circuit.
run
Run the model on the server over encrypted data.
Args:
serialized_encrypted_quantized_data
(cnp.PublicArguments): the encrypted, quantized and serialized data
serialized_evaluation_keys
(cnp.EvaluationKeys): the serialized evaluation keys
Returns:
cnp.PublicResult
: the result of the model
FHEModelDev
Dev API to save the model and then load and run the FHE circuit.
__init__
Initialize the FHE API.
Args:
path_dir
(str): the path to the directory where the circuit is saved
model
(Any): the model to use for the FHE API
save
Export all needed artifacts for the client and server.
Raises:
Exception
: path_dir is not empty
FHEModelClient
Client API to encrypt and decrypt FHE data.
__init__
Initialize the FHE API.
Args:
path_dir
(str): the path to the directory where the circuit is saved
key_dir
(str): the path to the directory where the keys are stored
deserialize_decrypt
Deserialize and decrypt the values.
Args:
serialized_encrypted_quantized_result
(cnp.PublicArguments): the serialized, encrypted and quantized result
Returns:
numpy.ndarray
: the decrypted and desarialized values
deserialize_decrypt_dequantize
Deserialize, decrypt and dequantize the values.
Args:
serialized_encrypted_quantized_result
(cnp.PublicArguments): the serialized, encrypted and quantized result
Returns:
numpy.ndarray
: the decrypted (dequantized) values
generate_private_and_evaluation_keys
Generate the private and evaluation keys.
Args:
force
(bool): if True, regenerate the keys even if they already exist
get_serialized_evaluation_keys
Get the serialized evaluation keys.
Returns:
cnp.EvaluationKeys
: the evaluation keys
load
Load the quantizers along with the FHE specs.
quantize_encrypt_serialize
Quantize, encrypt and serialize the values.
Args:
x
(numpy.ndarray): the values to quantize, encrypt and serialize
Returns:
cnp.PublicArguments
: the quantized, encrypted and serialized values
The quantized versions of floating point model operations are stored in the QuantizedModule
. The ONNX_OPS_TO_QUANTIZED_IMPL
dictionary maps ONNX floating point operators (e.g. Gemm) to their quantized equivalent (e.g. QuantizedGemm). For more information on implementing these operations, please see the .
The computation graph is taken from the corresponding floating point ONNX graph exported from scikit-learn , or from the ONNX graph exported by PyTorch. Calibration is used to obtain quantized parameters for the operations in the QuantizedModule
. Parameters are also determined for the quantization of inputs during model deployment.
To perform calibration, an interpreter goes through the ONNX graph in and stores the intermediate results as it goes. The statistics of these values determine quantization parameters.
That QuantizedModule
generates the Concrete-Numpy function that is compiled to FHE. The compilation will succeed if the intermediate values conform to the 8-bits precision limit of the Concrete stack. See for details.
Lei Mao's blog on quantization:
Google paper on neural network quantization and integer-only inference:
Quantization is the process of constraining an input from a continuous or otherwise large set of values (such as real numbers) to a discrete set (such as integers).
This means that some accuracy in the representation is lost (e.g. a simple approach is to eliminate least-significant bits). However, in many cases in machine learning, it is possible to adapt the models to give meaningful results while using these smaller data types. This significantly reduces the number of bits necessary for intermediary results during the execution of these machine learning models.
Since FHE is currently limited to 8-bit integers, it is necessary to quantize models to make them compatible. As a general rule, the smaller the precision models, the better the FHE performance.
Quantization implemented in Concrete-ML is applied in two ways:
Built-in models apply quantization internally and the user only needs to configure some quantization parameters. This approach requires little work by the user but may not be a one-size-fits-all solution for all types of models. The final quantized model is FHE friendly and ready to predict over encrypted data. In this setting, Post-Training Quantization (PTQ) is for linear models, data quantization is used for tree-based models and, finally, Quantization Aware Training (QAT) is included in the built-in neural network models.
For custom neural networks with more complex topology, obtaining FHE-compatible models with good accuracy requires QAT. Concrete-ML offers the possibility for the user to perform quantization before compiling to FHE. This can be achieved through a third-party library that offers QAT tools, such as Brevitas for PyTorch. In this approach, the user is responsible for implementing a full-integer model, respecting FHE constraints. Please refer to the advanced QAT tutorial for tips on designing FHE neural networks.
While Concrete-ML quantizes machine learning models, the data the client has is often in floating point. The Concrete-ML models provide APIs to quantize inputs and de-quantize outputs.
Please note that the floating point input is quantized in the clear, i.e. it is converted to integers before being encrypted. Moreover, the model's output are also integers and are decrypted before de-quantization.
Let be the range of a value to quantize where is the minimum and is the maximum. To quantize a range of floating point values (in ) to integer values (in ), the first step is to choose the data type that is going to be used. Concrete, the framework used by Concrete-ML, is currently limited to 8-bit integers, so this will be the value used in this example. Knowing the number of bits that can be used for a value in the range , the scale
can be computed :
where is the number of bits (). For the sake of example, let's take .
In practice, the quantization scale is then . This means the gap between consecutive representable values cannot be smaller than , which, in turn, means there can be a substantial loss of precision. Every interval of length will be represented by a value within the range .
The other important parameter from this quantization schema is the zero point
value. This essentially brings the 0 floating point value to a specific integer. If the quantization scheme is asymmetric (quantized values are not centered in 0), the resulting integer will be in .
When using quantized values in a matrix multiplication or convolution, the equations for computing the result become more complex. The IntelLabs distiller quantization documentation provides a more detailed explanation of the maths used to quantize values and how to keep computations consistent.
Built-in models provide a simple interface for configuring quantization parameters, most notably the number of bits used for inputs, model weights, intermediary and output values.
For linear models, the quantization is done post-training. Thus, the model is trained in floating point, and then, the best integer weight representations are found, depending on the distribution of inputs and weights. For these models, the user can select the value of the n_bits
parameter.
For linear models, n_bits
is used to quantize both model inputs and weights. Depending on the number of features, you can use a single integer value for the n_bits
parameter, e.g. a value between 2 and 7. When the number of features is high, the n_bits
parameter should be decreased if you encounter compilation errors. It is also possible to quantize inputs and weights with different number of bits by passing a dictionary to n_bits
, containing the op_inputs
and op_weights
keys.
For tree-based models, the training and test data is quantized. The maximum accumulator bit-width for a model trained with n_bits=n
for this type of model is known beforehand: it will need n+1
bits. Thus, as Concrete-ML only supports up to 8-bit integers, n
should be less than 8. Through experimentation, it was determined that in many cases a value of 5 or 6 bits gives the same accuracy as training in floating point.
Tree-based models can directly control the accumulator bit-width used. However, if 6 or 7 bits are not sufficient to obtain good accuracy on your data-set, one option is to use an ensemble model (RandomForest or XGBoost) and increase the number of trees in the ensemble. This, however, will have a detrimental impact on FHE execution speed.
For the built-in neural networks, several linear layers are used. Thus, the outputs of a layer are used as inputs to a new layer. Built-in neural networks use Quantization Aware Training. The parameters controlling the maximum accumulator bit-width are the number of weights and activation bits ( module__n_w_bits
, module__n_a_bits
), but also the pruning factor. This factor is determined automatically by specifying a desired accumulator bit-width module__n_accum_bits
and, optionally, a multiplier factor, module__n_hidden_neurons_multiplier
.
Note that for the built-in linear models and neural networks, the maximum accumulator bit-width can not be precisely controlled. To use many input features and a high number of bits is beneficial for model accuracy, but it can conflict with the 8-bit accumulator constraint. Finding the best quantization parameters to maximize accuracy can only be done through experimentation.
The models implemented in Concrete-ML provide features to let the user quantize the input data and de-quantize the output data.
In a client/server setting, the client is responsible for quantizing inputs before sending them, encrypted, to the server. Further, the client must de-quantize the encrypted integer results received from the server. See the Production Deployment section for more details.
Here is a simple example showing how to perform inference, starting from float values and ending up with float values. Note that the FHE engine that is compiled for the ML models does not support data batching.
IntelLabs distiller explanation of quantization: Distiller documentation
This section provides a set of tools and guidelines to help users build optimized FHE-compatible models.
The Virtual Lib in Concrete-ML is a prototype that provides drop-in replacements for Concrete-Numpy's compiler, allowing users to simulate what would happen when converting a model to FHE without the current bit-width constraint. Additionally, it quickly simulates the behavior with 8 bits or less without actually doing the FHE computations.
The Virtual Lib can be useful when developing and iterating on an ML model implementation. For example, you can check that your model is compatible in terms of operands (all integers) with the Virtual Lib compilation. Then, you can check how many bits your ML model would require, which can give you hints as to how it should be modified if you want to compile it to an actual FHE Circuit (not a simulated one) that only supports 8 bits of integer precision.
The Virtual Lib, being pure Python and not requiring crypto key generation, can be much faster than the actual compilation and FHE execution, thus allowing for faster iterations, debugging and FHE simulation, regardless of the bit-width used. For example, this was used for the red/blue contours in the Classifier Comparison notebook, as computing in FHE for the whole grid and all the classifiers would take significant time.
The following example shows how to use the Virtual Lib in Concrete-ML. Simply add use_virtual_lib = True
and enable_unsafe_features = True
in a Configuration
. The result of the compilation will then be a simulated circuit that allows for more precision or simulated FHE execution.
The following example produces a neural network that is not FHE-compatible:
Upon execution, the compiler will raise the following error:
Knowing that a linear/dense layer is implemented as a matrix multiplication, it can determine which parts of the op-graph listing in the exception message above correspond to which layers.
Layer weights initialization:
Input processing and quantization:
First dense layer and activation function:
Second dense layer and activation function:
Third dense layer and output quantization:
We can see here that the error is in the second layer. Reducing the number of neurons in this layer will resolve the error and make the network FHE-compatible:
In FHE, univariate functions are encoded as table lookups, which are then implemented using Programmable Bootstrapping (PBS). PBS is a powerful technique but will require significantly more computing resources, and thus time, than simpler encrypted operations such matrix multiplications, convolution or additions.
Furthermore, the cost of PBS will depend on the bit-width of the compiled circuit. Every additional bit in the maximum bit-width raises the complexity of the PBS by a significant factor. It may be of interest to the model developer, then, to determine the bit-width of the circuit and the amount of PBS it performs.
This can be done by inspecting the MLIR code produced by the compiler:
There are several calls to FHELinalg.apply_mapped_lookup_table
and FHELinalg.apply_lookup_table
. These calls apply PBS to the cells of their input tensors. Their inputs in the listing above are: tensor<1x2x!FHE.eint<8>>
for the first and last call and tensor<1x50x!FHE.eint<8>>
for the two calls in the middle. Thus, PBS is applied 104 times.
Getting the bit-width of the circuit is then simply:
Decreasing the number of bits and the number of PBS induces large reductions in the computation time of the compiled circuit.
concrete.ml.onnx.onnx_utils
Utils to interpret an ONNX model with numpy.
ATTR_TYPES
ATTR_GETTERS
ONNX_OPS_TO_NUMPY_IMPL
ONNX_COMPARISON_OPS_TO_NUMPY_IMPL_FLOAT
ONNX_COMPARISON_OPS_TO_NUMPY_IMPL_BOOL
ONNX_OPS_TO_NUMPY_IMPL_BOOL
IMPLEMENTED_ONNX_OPS
get_attribute
Get the attribute from an ONNX AttributeProto.
Args:
attribute
(onnx.AttributeProto): The attribute to retrieve the value from.
Returns:
Any
: The stored attribute value.
get_op_name
Construct the qualified name of the ONNX operator.
Args:
node
(Any): ONNX graph node
Returns:
result
(str): qualified name
execute_onnx_with_numpy
Execute the provided ONNX graph on the given inputs.
Args:
graph
(onnx.GraphProto): The ONNX graph to execute.
*inputs
: The inputs of the graph.
Returns:
Tuple[numpy.ndarray]
: The result of the graph's execution.
concrete.ml.onnx.convert
ONNX conversion related code.
IMPLEMENTED_ONNX_OPS
OPSET_VERSION_FOR_ONNX_EXPORT
get_equivalent_numpy_forward_and_onnx_model
Get the numpy equivalent forward of the provided torch Module.
Args:
torch_module
(torch.nn.Module): the torch Module for which to get the equivalent numpy forward.
dummy_input
(Union[torch.Tensor, Tuple[torch.Tensor, ...]]): dummy inputs for ONNX export.
output_onnx_file
(Optional[Union[Path, str]], optional): Path to save the ONNX file to. Will use a temp file if not provided. Defaults to None.
Returns:
Tuple[Callable[..., Tuple[numpy.ndarray, ...]], onnx.GraphProto]
: The function that will execute the equivalent numpy code to the passed torch_module and the generated ONNX model.
get_equivalent_numpy_forward
Get the numpy equivalent forward of the provided ONNX model.
Args:
onnx_model
(onnx.ModelProto): the ONNX model for which to get the equivalent numpy forward.
check_model
(bool): set to True to run the onnx checker on the model. Defaults to True.
Raises:
ValueError
: Raised if there is an unsupported ONNX operator required to convert the torch model to numpy.
Returns:
Callable[..., Tuple[numpy.ndarray, ...]]
: The function that will execute the equivalent numpy function.
concrete.ml.onnx.ops_impl
ONNX ops implementation in python + numpy.
cast_to_float
Cast values to floating points.
Args:
inputs
(Tuple[numpy.ndarray]): The values to consider.
Returns:
Tuple[numpy.ndarray]
: The float values.
onnx_func_raw_args
Decorate a numpy onnx function to flag the raw/non quantized inputs.
Args:
*args (tuple[Any])
: function argument names
Returns:
result
(ONNXMixedFunction): wrapped numpy function with a list of mixed arguments
numpy_where_body
Compute the equivalent of numpy.where.
This function is not mapped to any ONNX operator (as opposed to numpy_where). It is usable by functions which are mapped to ONNX operators, e.g. numpy_div or numpy_where.
Args:
c
(numpy.ndarray): Condition operand.
t
(numpy.ndarray): True operand.
f
(numpy.ndarray): False operand.
Returns:
numpy.ndarray
: numpy.where(c, t, f)
numpy_where
Compute the equivalent of numpy.where.
Args:
c
(numpy.ndarray): Condition operand.
t
(numpy.ndarray): True operand.
f
(numpy.ndarray): False operand.
Returns:
numpy.ndarray
: numpy.where(c, t, f)
numpy_add
Compute add in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Add-13
Args:
a
(numpy.ndarray): First operand.
b
(numpy.ndarray): Second operand.
Returns:
Tuple[numpy.ndarray]
: Result, has same element type as two inputs
numpy_constant
Return the constant passed as a kwarg.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Constant-13
Args:
**kwargs
: keyword arguments
Returns:
Any
: The stored constant.
numpy_matmul
Compute matmul in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#MatMul-13
Args:
a
(numpy.ndarray): N-dimensional matrix A
b
(numpy.ndarray): N-dimensional matrix B
Returns:
Tuple[numpy.ndarray]
: Matrix multiply results from A * B
numpy_relu
Compute relu in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Relu-14
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_sigmoid
Compute sigmoid in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Sigmoid-13
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_softmax
Compute softmax in numpy according to ONNX spec.
Softmax is currently not supported in FHE.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#softmax-13
Args:
x
(numpy.ndarray): Input tensor
axis
(None, int, tuple of ints): Axis or axes along which a softmax's sum is performed. If None, it will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis. Default to 1.
keepdims
(bool): If True, the axes which are reduced along the sum are left in the result as dimensions with size one. Default to True.
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_cos
Compute cos in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Cos-7
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_cosh
Compute cosh in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Cosh-9
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_sin
Compute sin in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Sin-7
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_sinh
Compute sinh in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Sinh-9
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_tan
Compute tan in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Tan-7
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_tanh
Compute tanh in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Tanh-13
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_acos
Compute acos in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Acos-7
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_acosh
Compute acosh in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Acosh-9
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_asin
Compute asin in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Asin-7
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_asinh
Compute sinh in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Asinh-9
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_atan
Compute atan in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Atan-7
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_atanh
Compute atanh in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Atanh-9
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_elu
Compute elu in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Elu-6
Args:
x
(numpy.ndarray): Input tensor
alpha
(float): Coefficient
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_selu
Compute selu in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Selu-6
Args:
x
(numpy.ndarray): Input tensor
alpha
(float): Coefficient
gamma
(float): Coefficient
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_celu
Compute celu in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Celu-12
Args:
x
(numpy.ndarray): Input tensor
alpha
(float): Coefficient
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_leakyrelu
Compute leakyrelu in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#LeakyRelu-6
Args:
x
(numpy.ndarray): Input tensor
alpha
(float): Coefficient
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_thresholdedrelu
Compute thresholdedrelu in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#ThresholdedRelu-10
Args:
x
(numpy.ndarray): Input tensor
alpha
(float): Coefficient
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_hardsigmoid
Compute hardsigmoid in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#HardSigmoid-6
Args:
x
(numpy.ndarray): Input tensor
alpha
(float): Coefficient
beta
(float): Coefficient
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_softplus
Compute softplus in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Softplus-1
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_abs
Compute abs in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Abs-13
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_div
Compute div in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Div-14
Args:
a
(numpy.ndarray): Input tensor
b
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_mul
Compute mul in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Mul-14
Args:
a
(numpy.ndarray): Input tensor
b
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_sub
Compute sub in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Sub-14
Args:
a
(numpy.ndarray): Input tensor
b
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_log
Compute log in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Log-13
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_erf
Compute erf in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Erf-13
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_hardswish
Compute hardswish in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#hardswish-14
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_exp
Compute exponential in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Exp-13
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: The exponential of the input tensor computed element-wise
numpy_equal
Compute equal in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Equal-11
Args:
x
(numpy.ndarray): Input tensor
y
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_not
Compute not in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Not-1
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_not_float
Compute not in numpy according to ONNX spec and cast outputs to floats.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Not-1
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_greater
Compute greater in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Greater-13
Args:
x
(numpy.ndarray): Input tensor
y
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_greater_float
Compute greater in numpy according to ONNX spec and cast outputs to floats.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Greater-13
Args:
x
(numpy.ndarray): Input tensor
y
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_greater_or_equal
Compute greater or equal in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#GreaterOrEqual-12
Args:
x
(numpy.ndarray): Input tensor
y
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_greater_or_equal_float
Compute greater or equal in numpy according to ONNX specs and cast outputs to floats.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#GreaterOrEqual-12
Args:
x
(numpy.ndarray): Input tensor
y
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_less
Compute less in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Less-13
Args:
x
(numpy.ndarray): Input tensor
y
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_less_float
Compute less in numpy according to ONNX spec and cast outputs to floats.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Less-13
Args:
x
(numpy.ndarray): Input tensor
y
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_less_or_equal
Compute less or equal in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#LessOrEqual-12
Args:
x
(numpy.ndarray): Input tensor
y
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_less_or_equal_float
Compute less or equal in numpy according to ONNX spec and cast outputs to floats.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#LessOrEqual-12
Args:
x
(numpy.ndarray): Input tensor
y
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_identity
Compute identity in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Identity-14
Args:
x
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_transpose
Transpose in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Transpose-13
Args:
x
(numpy.ndarray): Input tensor
perm
(numpy.ndarray): Permutation of the axes
Returns:
Tuple[numpy.ndarray]
: Output tensor
torch_avgpool
Compute Average Pooling using Torch.
Currently supports 2d average pooling with torch semantics. This function is ONNX compatible.
See: https://github.com/onnx/onnx/blob/release/0.4.x/docs/Operators.md#AveragePool
Args:
x
(numpy.ndarray): input data (many dtypes are supported). Shape is N x C x H x W for 2d
ceil_mode
(int): ONNX rounding parameter, expected 0 (torch style dimension computation)
kernel_shape
(Tuple[int]): shape of the kernel. Should have 2 elements for 2d conv
pads
(Tuple[int]): padding in ONNX format (begin, end) on each axis
strides
(Tuple[int]): stride of the convolution on each axis
Returns:
res
(numpy.ndarray): a tensor of size (N x InChannels x OutHeight x OutWidth).
See https
: //pytorch.org/docs/stable/generated/torch.nn.AvgPool2d.html
Raises:
AssertionError
: if the pooling arguments are wrong
numpy_cast
Execute ONNX cast in Numpy.
Supports only booleans for now, which are converted to integers.
See: https://github.com/onnx/onnx/blob/release/0.4.x/docs/Operators.md#Cast
Args:
data
(numpy.ndarray): Input encrypted tensor
to
(int): integer value of the onnx.TensorProto DataType enum
Returns:
result
(numpy.ndarray): a tensor with the required data type
numpy_batchnorm
Compute the batch normalization of the input tensor.
This can be expressed as:
Y = (X - input_mean) / sqrt(input_var + epsilon) * scale + B
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#BatchNormalization-14
Args:
x
(numpy.ndarray): tensor to normalize, dimensions are in the form of (N,C,D1,D2,...,Dn), where N is the batch size, C is the number of channels.
scale
(numpy.ndarray): scale tensor of shape (C,)
bias
(numpy.ndarray): bias tensor of shape (C,)
input_mean
(numpy.ndarray): mean values to use for each input channel, shape (C,)
input_var
(numpy.ndarray): variance values to use for each input channel, shape (C,)
epsilon
(float): avoids division by zero
momentum
(float): momentum used during training of the mean/variance, not used in inference
training_mode
(int): if the model was exported in training mode this is set to 1, else 0
Returns:
numpy.ndarray
: Normalized tensor
numpy_flatten
Flatten a tensor into a 2d array.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Flatten-13.
Args:
x
(numpy.ndarray): tensor to flatten
axis
(int): axis after which all dimensions will be flattened (axis=0 gives a 1D output)
Returns:
result
: flattened tensor
numpy_or
Compute or in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Or-7
Args:
a
(numpy.ndarray): Input tensor
b
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_or_float
Compute or in numpy according to ONNX spec and cast outputs to floats.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Or-7
Args:
a
(numpy.ndarray): Input tensor
b
(numpy.ndarray): Input tensor
Returns:
Tuple[numpy.ndarray]
: Output tensor
numpy_round
Compute round in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Round-11 Remark that ONNX Round operator is actually a rint, since the number of decimals is forced to be 0
Args:
a
(numpy.ndarray): Input tensor whose elements to be rounded.
Returns:
Tuple[numpy.ndarray]
: Output tensor with rounded input elements.
numpy_pow
Compute pow in numpy according to ONNX spec.
See https://github.com/onnx/onnx/blob/release/0.4.x/docs/Changelog.md#Pow-13
Args:
a
(numpy.ndarray): Input tensor whose elements to be raised.
b
(numpy.ndarray): The power to which we want to raise.
Returns:
Tuple[numpy.ndarray]
: Output tensor.
ONNXMixedFunction
A mixed quantized-raw valued onnx function.
ONNX functions will take inputs which can be either quantized or float. Some functions only take quantized inputs, but some functions take both types. For mixed functions we need to tag the parameters that do not need quantization. Thus quantized ops can know which inputs are not QuantizedArray and we avoid unnecessary wrapping of float values as QuantizedArrays.
__init__
Create the mixed function and raw parameter list.
Args:
function
(Any): function to be decorated
non_quant_params
: Set[str]: set of parameters that will not be quantized (stored as numpy.ndarray)
concrete.ml.onnx.onnx_model_manipulations
Some code to manipulate models.
simplify_onnx_model
Simplify an ONNX model, removes unused Constant nodes and Identity nodes.
Args:
onnx_model
(onnx.ModelProto): the model to simplify.
remove_unused_constant_nodes
Remove unused Constant nodes in the provided onnx model.
Args:
onnx_model
(onnx.ModelProto): the model for which we want to remove unused Constant nodes.
remove_identity_nodes
Remove identity nodes from a model.
Args:
onnx_model
(onnx.ModelProto): the model for which we want to remove Identity nodes.
keep_following_outputs_discard_others
Keep the outputs given in outputs_to_keep and remove the others from the model.
Args:
onnx_model
(onnx.ModelProto): the ONNX model to modify.
outputs_to_keep
(Iterable[str]): the outputs to keep by name.
remove_node_types
Remove unnecessary nodes from the ONNX graph.
Args:
onnx_model
(onnx.ModelProto): The ONNX model to modify.
op_types_to_remove
(List[str]): The node types to remove from the graph.
Raises:
ValueError
: Wrong replacement by an Identity node.
clean_graph_after_node
Clean the graph of the onnx model by removing nodes after the given node name.
Args:
onnx_model
(onnx.ModelProto): The onnx model.
node_name
(str): The node's name whose following nodes will be removed.
concrete.ml.quantization.base_quantized_op
Base Quantized Op class that implements quantization for a float numpy op.
ONNX_OPS_TO_NUMPY_IMPL
ALL_QUANTIZED_OPS
ONNX_OPS_TO_QUANTIZED_IMPL
DEFAULT_MODEL_BITS
QuantizedOp
Base class for quantized ONNX ops implemented in numpy.
Args:
n_bits_output
(int): The number of bits to use for the quantization of the output
int_input_names
(Set[str]): The set of names of integer tensors that are inputs to this op
constant_inputs
(Optional[Union[Dict[str, Any], Dict[int, Any]]]): The constant tensors that are inputs to this op
input_quant_opts
(QuantizationOptions): Input quantizer options, determine the quantization that is applied to input tensors (that are not constants)
__init__
calibrate
Create corresponding QuantizedArray for the output of the activation function.
Args:
*inputs (numpy.ndarray)
: Calibration sample inputs.
Returns:
numpy.ndarray
: the output values for the provided calibration samples.
call_impl
Call self.impl to centralize mypy bug workaround.
Args:
*inputs (numpy.ndarray)
: real valued inputs.
**attrs
: the QuantizedOp attributes.
Returns:
numpy.ndarray
: return value of self.impl
can_fuse
Determine if the operator impedes graph fusion.
This function shall be overloaded by inheriting classes to test self._int_input_names, to determine whether the operation can be fused to a TLU or not. For example an operation that takes inputs produced by a unique integer tensor can be fused to a TLU. Example: f(x) = x * (x + 1) can be fused. A function that does f(x) = x * (x @ w + 1) can't be fused.
Returns:
bool
: whether this instance of the QuantizedOp produces Concrete Numpy code that can be fused to TLUs
must_quantize_input
Determine if an input must be quantized.
Quantized ops and numpy onnx ops take inputs and attributes. Inputs can be either constant or variable (encrypted). Note that this does not handle attributes, which are handled by QuantizedOp classes separately in their constructor.
Args:
input_name_or_idx
(int): Index of the input to check.
Returns:
result
(bool): Whether the input must be quantized (must be a QuantizedArray
) or if it stays as a raw numpy.array
read from ONNX.
prepare_output
Quantize the output of the activation function.
The calibrate method needs to be called with sample data before using this function.
Args:
qoutput_activation
(numpy.ndarray): Output of the activation function.
Returns:
QuantizedArray
: Quantized output.
q_impl
Execute the quantized forward.
Args:
*q_inputs (QuantizedArray)
: Quantized inputs.
**attrs
: the QuantizedOp attributes.
Returns:
QuantizedArray
: The returned quantized value.
concrete.ml.quantization.quantizers
Quantization utilities for a numpy array/tensor.
STABILITY_CONST
fill_from_kwargs
Fill a parameter set structure from kwargs parameters.
Args:
obj
: an object of type klass, if None the object is created if any of the type's members appear in the kwargs
klass
: the type of object to fill
kwargs
: parameter names and values to fill into an instance of the klass type
Returns:
obj
: an object of type klass
kwargs
: remaining parameter names and values that were not filled into obj
Raises:
TypeError
: if the types of the parameters in kwargs could not be converted to the corresponding types of members of klass
QuantizationOptions
Options for quantization.
Determines the number of bits for quantization and the method of quantization of the values. Signed quantization allows negative quantized values. Symmetric quantization assumes the float values are distributed symmetrically around x=0 and assigns signed values around 0 to the float values. QAT (quantization aware training) quantization assumes the values are already quantized, taking a discrete set of values, and assigns these values to integers, computing only the scale.
__init__
property quant_options
Get a copy of the quantization parameters.
Returns:
UniformQuantizationParameters
: a copy of the current quantization parameters
copy_opts
Copy the options from a different structure.
Args:
opts
(QuantizationOptions): structure to copy parameters from.
MinMaxQuantizationStats
Calibration set statistics.
This class stores the statistics for the calibration set or for a calibration data batch. Currently we only store min/max to determine the quantization range. The min/max are computed from the calibration set.
property quant_stats
Get a copy of the calibration set statistics.
Returns:
MinMaxQuantizationStats
: a copy of the current quantization stats
compute_quantization_stats
Compute the calibration set quantization statistics.
Args:
values
(numpy.ndarray): Calibration set on which to compute statistics.
copy_stats
Copy the statistics from a different structure.
Args:
stats
(MinMaxQuantizationStats): structure to copy statistics from.
UniformQuantizationParameters
Quantization parameters for uniform quantization.
This class stores the parameters used for quantizing real values to discrete integer values. The parameters are computed from quantization options and quantization statistics.
property quant_params
Get a copy of the quantization parameters.
Returns:
UniformQuantizationParameters
: a copy of the current quantization parameters
compute_quantization_parameters
Compute the quantization parameters.
Args:
options
(QuantizationOptions): quantization options set
stats
(MinMaxQuantizationStats): calibrated statistics for quantization
copy_params
Copy the parameters from a different structure.
Args:
params
(UniformQuantizationParameters): parameter structure to copy
UniformQuantizer
Uniform quantizer.
Contains all information necessary for uniform quantization and provides quantization/dequantization functionality on numpy arrays.
Args:
options
(QuantizationOptions): Quantization options set
stats
(Optional[MinMaxQuantizationStats]): Quantization batch statistics set
params
(Optional[UniformQuantizationParameters]): Quantization parameters set (scale, zero-point)
__init__
property quant_options
Get a copy of the quantization parameters.
Returns:
UniformQuantizationParameters
: a copy of the current quantization parameters
property quant_params
Get a copy of the quantization parameters.
Returns:
UniformQuantizationParameters
: a copy of the current quantization parameters
property quant_stats
Get a copy of the calibration set statistics.
Returns:
MinMaxQuantizationStats
: a copy of the current quantization stats
compute_quantization_parameters
Compute the quantization parameters.
Args:
options
(QuantizationOptions): quantization options set
stats
(MinMaxQuantizationStats): calibrated statistics for quantization
compute_quantization_stats
Compute the calibration set quantization statistics.
Args:
values
(numpy.ndarray): Calibration set on which to compute statistics.
copy_opts
Copy the options from a different structure.
Args:
opts
(QuantizationOptions): structure to copy parameters from.
copy_params
Copy the parameters from a different structure.
Args:
params
(UniformQuantizationParameters): parameter structure to copy
copy_stats
Copy the statistics from a different structure.
Args:
stats
(MinMaxQuantizationStats): structure to copy statistics from.
dequant
Dequantize values.
Args:
qvalues
(numpy.ndarray): integer values to dequantize
Returns:
numpy.ndarray
: Dequantized float values.
quant
Quantize values.
Args:
values
(numpy.ndarray): float values to quantize
Returns:
numpy.ndarray
: Integer quantized values.
QuantizedArray
Abstraction of quantized array.
Contains float values and their quantized integer counter-parts. Quantization is performed by the quantizer member object. Float and int values are kept in sync. Having both types of values is useful since quantized operators in Concrete ML graphs might need one or the other depending on how the operator works (in float or in int). Moreover, when the encrypted function needs to return a value, it must return integer values.
See https://arxiv.org/abs/1712.05877.
Args:
values
(numpy.ndarray): Values to be quantized.
n_bits
(int): The number of bits to use for quantization.
value_is_float
(bool, optional): Whether the passed values are real (float) values or not. If False, the values will be quantized according to the passed scale and zero_point. Defaults to True.
options
(QuantizationOptions): Quantization options set
stats
(Optional[MinMaxQuantizationStats]): Quantization batch statistics set
params
(Optional[UniformQuantizationParameters]): Quantization parameters set (scale, zero-point)
kwargs
: Any member of the options, stats, params sets as a key-value pair. The parameter sets need to be completely parametrized if their members appear in kwargs.
__init__
dequant
Dequantize self.qvalues.
Returns:
numpy.ndarray
: Dequantized values.
quant
Quantize self.values.
Returns:
numpy.ndarray
: Quantized values.
update_quantized_values
Update qvalues to get their corresponding values using the related quantized parameters.
Args:
qvalues
(numpy.ndarray): Values to replace self.qvalues
Returns:
values
(numpy.ndarray): Corresponding values
update_values
Update values to get their corresponding qvalues using the related quantized parameters.
Args:
values
(numpy.ndarray): Values to replace self.values
Returns:
qvalues
(numpy.ndarray): Corresponding qvalues
concrete.ml.sklearn.glm
Implement sklearn's Generalized Linear Models (GLM).
PoissonRegressor
A Poisson regression model with FHE.
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
fit
Fit the GLM regression quantized model.
Args:
X
: The training data, which can be: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
post_processing
Post-processing the predictions.
Args:
y_preds
(numpy.ndarray): The predictions to post-process.
already_dequantized
(bool): Wether the inputs were already dequantized or not. Default to False.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict on user data.
Predict on user data using either the quantized clear model, implemented with tensors, or, if execute_in_fhe is set, using the compiled FHE circuit.
Args:
X
(numpy.ndarray): The input data.
execute_in_fhe
(bool): Whether to execute the inference in FHE. Default to False.
Returns:
numpy.ndarray
: The model's predictions.
GammaRegressor
A Gamma regression model with FHE.
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
fit
Fit the GLM regression quantized model.
Args:
X
: The training data, which can be: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
post_processing
Post-processing the predictions.
Args:
y_preds
(numpy.ndarray): The predictions to post-process.
already_dequantized
(bool): Wether the inputs were already dequantized or not. Default to False.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict on user data.
Predict on user data using either the quantized clear model, implemented with tensors, or, if execute_in_fhe is set, using the compiled FHE circuit.
Args:
X
(numpy.ndarray): The input data.
execute_in_fhe
(bool): Whether to execute the inference in FHE. Default to False.
Returns:
numpy.ndarray
: The model's predictions.
TweedieRegressor
A Tweedie regression model with FHE.
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
fit
Fit the GLM regression quantized model.
Args:
X
: The training data, which can be: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
post_processing
Post-processing the predictions.
Args:
y_preds
(numpy.ndarray): The predictions to post-process.
already_dequantized
(bool): Wether the inputs were already dequantized or not. Default to False.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict on user data.
Predict on user data using either the quantized clear model, implemented with tensors, or, if execute_in_fhe is set, using the compiled FHE circuit.
Args:
X
(numpy.ndarray): The input data.
execute_in_fhe
(bool): Whether to execute the inference in FHE. Default to False.
Returns:
numpy.ndarray
: The model's predictions.
concrete.ml.sklearn.base
Module that contains base classes for our libraries estimators.
DEFAULT_P_ERROR_PBS
OPSET_VERSION_FOR_ONNX_EXPORT
QuantizedTorchEstimatorMixin
Mixin that provides quantization for a torch module and follows the Estimator API.
This class should be mixed in with another that provides the full Estimator API. This class only provides modifiers for .fit() (with quantization) and .predict() (optionally in FHE)
__init__
property base_estimator_type
Get the sklearn estimator that should be trained by the child class.
property base_module_to_compile
Get the Torch module that should be compiled to FHE.
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[Quantizer]
: the input quantizers
property n_bits_quant
Get the number of quantization bits.
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
_onnx_model_
(onnx.ModelProto): the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
compile
Compile the model.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): whether to compile using the virtual library that allows higher bitwidths
p_error
(Optional[float]): probability of error of a PBS
Returns:
Circuit
: the compiled Circuit.
Raises:
ValueError
: if called before the model is trained
fit
Initialize and fit the module.
If the module was already initialized, by calling fit, the module will be re-initialized (unless warm_start
is True). In addition to the torch training step, this method performs quantization of the trained torch model.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): labels associated with training data
**fit_params
: additional parameters that can be used during training, these are passed to the torch training interface
Returns:
self
: the trained quantized estimator
fit_benchmark
Fit the quantized estimator and return reference estimator.
This function returns both the quantized estimator (itself), but also a wrapper around the non-quantized trained NN. This is useful in order to compare performance between the quantized and fp32 versions of the classifier
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): labels associated with training data
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
Returns:
self
: the trained quantized estimator
fp32_model
: trained raw (fp32) wrapped NN estimator
get_params_for_benchmark
Get the parameters to instantiate the sklearn estimator trained by the child class.
Returns:
params
(dict): dictionary with parameters that will initialize a new Estimator
post_processing
Post-processing the output.
Args:
y_preds
(numpy.ndarray): the output to post-process
Raises:
ValueError
: if unknown post-processing function
Returns:
numpy.ndarray
: the post-processed output
predict
Predict on user provided data.
Predicts using the quantized clear or FHE classifier
Args:
X
: input data, a numpy array of raw values (non quantized)
execute_in_fhe
: whether to execute the inference in FHE or in the clear
Returns:
y_pred
: numpy ndarray with predictions
predict_proba
Predict on user provided data, returning probabilities.
Predicts using the quantized clear or FHE classifier
Args:
X
: input data, a numpy array of raw values (non quantized)
execute_in_fhe
: whether to execute the inference in FHE or in the clear
Returns:
y_pred
: numpy ndarray with probabilities (if applicable)
Raises:
ValueError
: if the estimator was not yet trained or compiled
BaseTreeEstimatorMixin
Mixin class for tree-based estimators.
A place to share methods that are used on all tree-based estimators.
__init__
Initialize the TreeBasedEstimatorMixin.
Args:
n_bits
(int): number of bits used for quantization
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
compile
Compile the model.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): set to True to use the so called virtual lib simulating FHE computation. Defaults to False
p_error
(Optional[float]): probability of error of a PBS
Returns:
Circuit
: the compiled Circuit.
dequantize_output
Dequantize the integer predictions.
Args:
y_preds
(numpy.ndarray): the predictions
Returns: the dequantized predictions
fit_benchmark
Fit the sklearn tree-based model and the FHE tree-based model.
Args:
X
(numpy.ndarray): The input data.
y
(numpy.ndarray): The target data. random_state (Optional[Union[int, numpy.random.RandomState, None]]): The random state. Defaults to None.
*args
: args for super().fit
**kwargs
: kwargs for super().fit
Returns: Tuple[ConcreteEstimators, SklearnEstimators]: The FHE and sklearn tree-based models.
quantize_input
Quantize the input.
Args:
X
(numpy.ndarray): the input
Returns: the quantized input
BaseTreeRegressorMixin
Mixin class for tree-based regressors.
A place to share methods that are used on all tree-based regressors.
__init__
Initialize the TreeBasedEstimatorMixin.
Args:
n_bits
(int): number of bits used for quantization
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
compile
Compile the model.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): set to True to use the so called virtual lib simulating FHE computation. Defaults to False
p_error
(Optional[float]): probability of error of a PBS
Returns:
Circuit
: the compiled Circuit.
dequantize_output
Dequantize the integer predictions.
Args:
y_preds
(numpy.ndarray): the predictions
Returns: the dequantized predictions
fit
Fit the tree-based estimator.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
**kwargs
: args for super().fit
Returns:
Any
: The fitted model.
fit_benchmark
Fit the sklearn tree-based model and the FHE tree-based model.
Args:
X
(numpy.ndarray): The input data.
y
(numpy.ndarray): The target data. random_state (Optional[Union[int, numpy.random.RandomState, None]]): The random state. Defaults to None.
*args
: args for super().fit
**kwargs
: kwargs for super().fit
Returns: Tuple[ConcreteEstimators, SklearnEstimators]: The FHE and sklearn tree-based models.
post_processing
Apply post-processing to the predictions.
Args:
y_preds
(numpy.ndarray): The predictions.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict the probability.
Args:
X
(numpy.ndarray): The input data.
execute_in_fhe
(bool): Whether to execute in FHE. Defaults to False.
Returns:
numpy.ndarray
: The predicted probabilities.
quantize_input
Quantize the input.
Args:
X
(numpy.ndarray): the input
Returns: the quantized input
BaseTreeClassifierMixin
Mixin class for tree-based classifiers.
A place to share methods that are used on all tree-based classifiers.
__init__
Initialize the TreeBasedEstimatorMixin.
Args:
n_bits
(int): number of bits used for quantization
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
compile
Compile the model.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): set to True to use the so called virtual lib simulating FHE computation. Defaults to False
p_error
(Optional[float]): probability of error of a PBS
Returns:
Circuit
: the compiled Circuit.
dequantize_output
Dequantize the integer predictions.
Args:
y_preds
(numpy.ndarray): the predictions
Returns: the dequantized predictions
fit
Fit the tree-based estimator.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
**kwargs
: args for super().fit
Returns:
Any
: The fitted model.
fit_benchmark
Fit the sklearn tree-based model and the FHE tree-based model.
Args:
X
(numpy.ndarray): The input data.
y
(numpy.ndarray): The target data. random_state (Optional[Union[int, numpy.random.RandomState, None]]): The random state. Defaults to None.
*args
: args for super().fit
**kwargs
: kwargs for super().fit
Returns: Tuple[ConcreteEstimators, SklearnEstimators]: The FHE and sklearn tree-based models.
post_processing
Apply post-processing to the predictions.
Args:
y_preds
(numpy.ndarray): The predictions.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict the class with highest probability.
Args:
X
(numpy.ndarray): The input data.
execute_in_fhe
(bool): Whether to execute in FHE. Defaults to False.
Returns:
numpy.ndarray
: The predicted target values.
predict_proba
Predict the probability.
Args:
X
(numpy.ndarray): The input data.
execute_in_fhe
(bool): Whether to execute in FHE. Defaults to False.
Returns:
numpy.ndarray
: The predicted probabilities.
quantize_input
Quantize the input.
Args:
X
(numpy.ndarray): the input
Returns: the quantized input
SklearnLinearModelMixin
A Mixin class for sklearn linear models with FHE.
__init__
Initialize the FHE linear model.
Args:
n_bits
(int, Dict): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for activation, inputs and weights. If a dict is passed, then it should contain "model_inputs", "op_inputs", "op_weights" and "model_outputs" keys with corresponding number of quantization bits for: - model_inputs : number of bits for model input - op_inputs : number of bits to quantize layer input values - op_weights: learned parameters or constants in the network - model_outputs: final model output quantization bits Default to 2.
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
clean_graph
Clean the graph of the onnx model.
This will remove the Cast node in the model's onnx.graph since they have no use in quantized or FHE models.
compile
Compile the FHE linear model.
Args:
X
(numpy.ndarray): The input data.
configuration
(Optional[Configuration]): Configuration object to use during compilation
compilation_artifacts
(Optional[DebugArtifacts]): Artifacts object to fill during compilation
show_mlir
(bool): if set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo. Defaults to False.
use_virtual_lib
(bool): whether to compile using the virtual library that allows higher bitwidths with simulated FHE computation. Defaults to False
p_error
(Optional[float]): probability of error of a PBS
Returns:
Circuit
: the compiled Circuit.
fit
Fit the FHE linear model.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
Returns: Any
fit_benchmark
Fit the sklearn linear model and the FHE linear model.
Args:
X
(numpy.ndarray): The input data.
y
(numpy.ndarray): The target data. random_state (Optional[Union[int, numpy.random.RandomState, None]]): The random state. Defaults to None.
*args
: The arguments to pass to the sklearn linear model. or not (False). Default to False.
*args
: args for super().fit
**kwargs
: kwargs for super().fit
Returns: Tuple[SklearnLinearModelMixin, sklearn.linear_model.LinearRegression]: The FHE and sklearn LinearRegression.
post_processing
Post-processing the output.
Args:
y_preds
(numpy.ndarray): the output to post-process
Returns:
numpy.ndarray
: the post-processed output
predict
Predict on user data.
Predict on user data using either the quantized clear model, implemented with tensors, or, if execute_in_fhe is set, using the compiled FHE circuit
Args:
X
(numpy.ndarray): the input data
execute_in_fhe
(bool): whether to execute the inference in FHE
Returns:
numpy.ndarray
: the prediction as ordinals
SklearnLinearClassifierMixin
A Mixin class for sklearn linear classifiers with FHE.
__init__
Initialize the FHE linear model.
Args:
n_bits
(int, Dict): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for activation, inputs and weights. If a dict is passed, then it should contain "model_inputs", "op_inputs", "op_weights" and "model_outputs" keys with corresponding number of quantization bits for: - model_inputs : number of bits for model input - op_inputs : number of bits to quantize layer input values - op_weights: learned parameters or constants in the network - model_outputs: final model output quantization bits Default to 2.
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
clean_graph
Clean the graph of the onnx model.
Any operators following gemm, including the sigmoid, softmax and argmax operators, are removed from the graph. They will be executed in clear in the post-processing method.
compile
Compile the FHE linear model.
Args:
X
(numpy.ndarray): The input data.
configuration
(Optional[Configuration]): Configuration object to use during compilation
compilation_artifacts
(Optional[DebugArtifacts]): Artifacts object to fill during compilation
show_mlir
(bool): if set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo. Defaults to False.
use_virtual_lib
(bool): whether to compile using the virtual library that allows higher bitwidths with simulated FHE computation. Defaults to False
p_error
(Optional[float]): probability of error of a PBS
Returns:
Circuit
: the compiled Circuit.
decision_function
Predict confidence scores for samples.
Args:
X
(numpy.ndarray): Samples to predict.
execute_in_fhe
(bool): If True, the inference will be executed in FHE. Default to False.
Returns:
numpy.ndarray
: Confidence scores for samples.
fit
Fit the FHE linear model.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
Returns: Any
fit_benchmark
Fit the sklearn linear model and the FHE linear model.
Args:
X
(numpy.ndarray): The input data.
y
(numpy.ndarray): The target data. random_state (Optional[Union[int, numpy.random.RandomState, None]]): The random state. Defaults to None.
*args
: The arguments to pass to the sklearn linear model. or not (False). Default to False.
*args
: args for super().fit
**kwargs
: kwargs for super().fit
Returns: Tuple[SklearnLinearModelMixin, sklearn.linear_model.LinearRegression]: The FHE and sklearn LinearRegression.
post_processing
Post-processing the predictions.
This step may include a dequantization of the inputs if not done previously, in particular within the client-server workflow.
Args:
y_preds
(numpy.ndarray): The predictions to post-process.
already_dequantized
(bool): Wether the inputs were already dequantized or not. Default to False.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict on user data.
Predict on user data using either the quantized clear model, implemented with tensors, or, if execute_in_fhe is set, using the compiled FHE circuit.
Args:
X
(numpy.ndarray): Samples to predict.
execute_in_fhe
(bool): If True, the inference will be executed in FHE. Default to False.
Returns:
numpy.ndarray
: The prediction as ordinals.
predict_proba
Predict class probabilities for samples.
Args:
X
(numpy.ndarray): Samples to predict.
execute_in_fhe
(bool): If True, the inference will be executed in FHE. Default to False.
Returns:
numpy.ndarray
: Class probabilities for samples.
concrete.ml.quantization.quantized_ops
Quantized versions of the ONNX operators for post training quantization.
QuantizedSigmoid
Quantized sigmoid op.
QuantizedHardSigmoid
Quantized HardSigmoid op.
QuantizedRelu
Quantized Relu op.
QuantizedPRelu
Quantized PRelu op.
QuantizedLeakyRelu
Quantized LeakyRelu op.
QuantizedHardSwish
Quantized Hardswish op.
QuantizedElu
Quantized Elu op.
QuantizedSelu
Quantized Selu op.
QuantizedCelu
Quantized Celu op.
QuantizedClip
Quantized clip op.
QuantizedRound
Quantized round op.
QuantizedPow
Quantized pow op.
Only works for a float constant power. This operation will be fused to a (potentially larger) TLU.
__init__
can_fuse
Determine if this op can be fused.
Power raising can be fused and computed in float when a single integer tensor generates both the operands. For example in the formula: f(x) = x ** (x + 1) where x is an integer tensor.
Returns:
bool
: Can fuse
QuantizedGemm
Quantized Gemm op.
__init__
can_fuse
Determine if this op can be fused.
Gemm operation can not be fused since it must be performed over integer tensors and it combines different values of the input tensors.
Returns:
bool
: False, this operation can not be fused as it adds different encrypted integers
q_impl
QuantizedMatMul
Quantized MatMul op.
__init__
can_fuse
Determine if this op can be fused.
Gemm operation can not be fused since it must be performed over integer tensors and it combines different values of the input tensors.
Returns:
bool
: False, this operation can not be fused as it adds different encrypted integers
q_impl
QuantizedAdd
Quantized Addition operator.
Can add either two variables (both encrypted) or a variable and a constant
can_fuse
Determine if this op can be fused.
Add operation can be computed in float and fused if it operates over inputs produced by a single integer tensor. For example the expression x + x * 1.75, where x is an encrypted tensor, can be computed with a single TLU.
Returns:
bool
: Whether the number of integer input tensors allows computing this op as a TLU
q_impl
QuantizedTanh
Quantized Tanh op.
QuantizedSoftplus
Quantized Softplus op.
QuantizedExp
Quantized Exp op.
QuantizedLog
Quantized Log op.
QuantizedAbs
Quantized Abs op.
QuantizedIdentity
Quantized Identity op.
q_impl
QuantizedReshape
Quantized Reshape op.
q_impl
Reshape the input integer encrypted tensor.
Args:
q_inputs
: an encrypted integer tensor at index 0 and one constant shape at index 1
attrs
: additional optional reshape options
Returns:
result
(QuantizedArray): reshaped encrypted integer tensor
QuantizedConv
Quantized Conv op.
__init__
Construct the quantized convolution operator and retrieve parameters.
Args:
n_bits_output
: number of bits for the quantization of the outputs of this operator
int_input_names
: names of integer tensors that are taken as input for this operation
constant_inputs
: the weights and activations
input_quant_opts
: options for the input quantizer
attrs
: convolution options
dilations
(Tuple[int]): dilation of the kernel, default 1 on all dimensions.
group
(int): number of convolution groups, default 1
kernel_shape
(Tuple[int]): shape of the kernel. Should have 2 elements for 2d conv
pads
(Tuple[int]): padding in ONNX format (begin, end) on each axis
strides
(Tuple[int]): stride of the convolution on each axis
can_fuse
Determine if this op can be fused.
Conv operation can not be fused since it must be performed over integer tensors and it combines different elements of the input tensors.
Returns:
bool
: False, this operation can not be fused as it adds different encrypted integers
q_impl
Compute the quantized convolution between two quantized tensors.
Allows an optional quantized bias.
Args:
q_inputs
: input tuple, contains
x
(numpy.ndarray): input data. Shape is N x C x H x W for 2d
w
(numpy.ndarray): weights tensor. Shape is (O x I x Kh x Kw) for 2d
b
(numpy.ndarray, Optional): bias tensor, Shape is (O,)
attrs
: convolution options handled in constructor
Returns:
res
(QuantizedArray): result of the quantized integer convolution
QuantizedAvgPool
Quantized Average Pooling op.
__init__
can_fuse
Determine if this op can be fused.
Avg Pooling operation can not be fused since it must be performed over integer tensors and it combines different elements of the input tensors.
Returns:
bool
: False, this operation can not be fused as it adds different encrypted integers
q_impl
QuantizedPad
Quantized Padding op.
__init__
can_fuse
Determine if this op can be fused.
Pad operation can not be fused since it must be performed over integer tensors.
Returns:
bool
: False, this operation can not be fused as it is manipulates integer tensors
QuantizedWhere
Where operator on quantized arrays.
Supports only constants for the results produced on the True/False branches.
__init__
QuantizedCast
Cast the input to the required data type.
In FHE we only support a limited number of output types. Booleans are cast to integers.
QuantizedGreater
Comparison operator >.
Only supports comparison with a constant.
__init__
QuantizedGreaterOrEqual
Comparison operator >=.
Only supports comparison with a constant.
__init__
QuantizedLess
Comparison operator <.
Only supports comparison with a constant.
__init__
QuantizedLessOrEqual
Comparison operator <=.
Only supports comparison with a constant.
__init__
QuantizedOr
Or operator ||.
This operation is not really working as a quantized operation. It just works when things got fused, as in e.g. Act(x) = x || (x + 42))
__init__
can_fuse
Determine if this op can be fused.
Or can be fused and computed in float when a single integer tensor generates both the operands. For example in the formula: f(x) = x || (x + 1) where x is an integer tensor.
Returns:
bool
: Can fuse
QuantizedDiv
Div operator /.
This operation is not really working as a quantized operation. It just works when things got fused, as in e.g. Act(x) = 1000 / (x + 42))
__init__
can_fuse
Determine if this op can be fused.
Div can be fused and computed in float when a single integer tensor generates both the operands. For example in the formula: f(x) = x / (x + 1) where x is an integer tensor.
Returns:
bool
: Can fuse
QuantizedMul
Multiplication operator.
Only multiplies an encrypted tensor with a float constant for now. This operation will be fused to a (potentially larger) TLU.
__init__
can_fuse
Determine if this op can be fused.
Multiplication can be fused and computed in float when a single integer tensor generates both the operands. For example in the formula: f(x) = x * (x + 1) where x is an integer tensor.
Returns:
bool
: Can fuse
QuantizedSub
Subtraction operator.
This works the same as addition on both encrypted - encrypted and on encrypted - constant.
can_fuse
Determine if this op can be fused.
Add operation can be computed in float and fused if it operates over inputs produced by a single integer tensor. For example the expression x + x * 1.75, where x is an encrypted tensor, can be computed with a single TLU.
Returns:
bool
: Whether the number of integer input tensors allows computing this op as a TLU
q_impl
QuantizedBatchNormalization
Quantized Batch normalization with encrypted input and in-the-clear normalization params.
QuantizedFlatten
Quantized flatten for encrypted inputs.
can_fuse
Determine if this op can be fused.
Flatten operation can not be fused since it must be performed over integer tensors.
Returns:
bool
: False, this operation can not be fused as it is manipulates integer tensors.
q_impl
Flatten the input integer encrypted tensor.
Args:
q_inputs
: an encrypted integer tensor at index 0
attrs
: contains axis attribute
Returns:
result
(QuantizedArray): reshaped encrypted integer tensor
QuantizedReduceSum
ReduceSum with encrypted input.
This operator is currently an experimental feature.
__init__
Construct the quantized ReduceSum operator and retrieve parameters.
Args:
n_bits_output
(int): Number of bits for the operator's quantization of outputs.
int_input_names
(Optional[Set[str]]): Names of input integer tensors. Default to None.
constant_inputs
(Optional[Dict]): Input constant tensor.
axes
(Optional[numpy.ndarray]): Array of integers along which to reduce. The default is to reduce over all the dimensions of the input tensor if 'noop_with_empty_axes' is false, else act as an Identity op when 'noop_with_empty_axes' is true. Accepted range is [-r, r-1] where r = rank(data). Default to None.
input_quant_opts
(Optional[QuantizationOptions]): Options for the input quantizer. Default to None.
attrs
(dict): RecuseSum options.
keepdims
(int): Keep the reduced dimension or not, 1 means keeping the input dimension, 0 will reduce it along the given axis. Default to 1.
noop_with_empty_axes
(int): Defines behavior if 'axes' is empty or set to None. Default behavior with 0 is to reduce all axes. When axes is empty and this attribute is set to true 1, input tensor will not be reduced, and the output tensor would be equivalent to input tensor. Default to 0.
calibrate
Create corresponding QuantizedArray for the output of the activation function.
Args:
*inputs (numpy.ndarray)
: Calibration sample inputs.
Returns:
numpy.ndarray
: the output values for the provided calibration samples.
q_impl
Sum the encrypted tensor's values over axis 1.
Args:
q_inputs
(QuantizedArray): An encrypted integer tensor at index 0.
attrs
(Dict): Contains axis attribute.
Returns:
(QuantizedArray)
: The sum of all values along axis 1 as an encrypted integer tensor.
tree_sum
Large sum without overflow (only MSB remains).
Args:
input_qarray
: Enctyped integer tensor.
is_calibration
: Whether we are calibrating the tree sum. If so, it will create all the quantizers for the downscaling.
Returns:
(numpy.ndarray)
: The MSB (based on the precision self.n_bits) of the integers sum.
QuantizedErf
Quantized erf op.
QuantizedNot
Quantized Not op.
QuantizedBrevitasQuant
Brevitas uniform quantization with encrypted input.
__init__
Construct the Brevitas quantization operator.
Args:
n_bits_output
(int): Number of bits for the operator's quantization of outputs. Not used, will be overridden by the bit_width in ONNX
int_input_names
(Optional[Set[str]]): Names of input integer tensors. Default to None.
constant_inputs
(Optional[Dict]): Input constant tensor.
scale
(float): Quantizer scale
zero_point
(float): Quantizer zero-point
bit_width
(int): Number of bits of the integer representation
input_quant_opts
(Optional[QuantizationOptions]): Options for the input quantizer. Default to None. attrs (dict):
rounding_mode
(str): Rounding mode (default and only accepted option is "ROUND")
signed
(int): Whether this op quantizes to signed integers (default 1),
narrow
(int): Whether this op quantizes to a narrow range of integers e.g. [-2n_bits-1 .. 2n_bits-1] (default 0),
q_impl
Quantize values.
Args:
q_inputs
: an encrypted integer tensor at index 0 and one constant shape at index 1
attrs
: additional optional reshape options
Returns:
result
(QuantizedArray): reshaped encrypted integer tensor
QuantizedTranspose
Transpose operator for quantized inputs.
This operator performs quantization, transposes the encrypted data, then dequantizes again.
q_impl
Reshape the input integer encrypted tensor.
Args:
q_inputs
: an encrypted integer tensor at index 0 and one constant shape at index 1
attrs
: additional optional reshape options
Returns:
result
(QuantizedArray): reshaped encrypted integer tensor
concrete.ml.quantization.quantized_module
QuantizedModule API.
DEFAULT_P_ERROR_PBS
QuantizedModule
Inference for a quantized model.
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property is_compiled
Return the compiled status of the module.
Returns:
bool
: the compiled status of the module.
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
_onnx_model
(onnx.ModelProto): the ONNX model
property post_processing_params
Get the post-processing parameters.
Returns:
Dict[str, Any]
: the post-processing parameters
compile
Compile the forward function of the module.
Args:
q_inputs
(Union[Tuple[numpy.ndarray, ...], numpy.ndarray]): Needed for tracing and building the boundaries.
configuration
(Optional[Configuration]): Configuration object to use during compilation
compilation_artifacts
(Optional[DebugArtifacts]): Artifacts object to fill during
show_mlir
(bool): if set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo. Defaults to False.
use_virtual_lib
(bool): set to use the so called virtual lib simulating FHE computation. Defaults to False.
p_error
(Optional[float]): probability of error of a PBS.
Returns:
Circuit
: the compiled Circuit.
dequantize_output
Take the last layer q_out and use its dequant function.
Args:
qvalues
(numpy.ndarray): Quantized values of the last layer.
Returns:
numpy.ndarray
: Dequantized values of the last layer.
forward
Forward pass with numpy function only.
Args:
*qvalues (numpy.ndarray)
: numpy.array containing the quantized values.
Returns:
(numpy.ndarray)
: Predictions of the quantized model
forward_and_dequant
Forward pass with numpy function only plus dequantization.
Args:
*q_x (numpy.ndarray)
: numpy.ndarray containing the quantized input values. Requires the input dtype to be uint8.
Returns:
(numpy.ndarray)
: Predictions of the quantized model
post_processing
Post-processing of the quantized output.
Args:
qvalues
(numpy.ndarray): numpy.ndarray containing the quantized input values.
Returns:
(numpy.ndarray)
: Predictions of the quantized model
quantize_input
Take the inputs in fp32 and quantize it using the learned quantization parameters.
Args:
*values (numpy.ndarray)
: Floating point values.
Returns:
Union[numpy.ndarray, Tuple[numpy.ndarray, ...]]
: Quantized (numpy.uint32) values.
set_inputs_quantization_parameters
Set the quantization parameters for the module's inputs.
Args:
*input_q_params (UniformQuantizer)
: The quantizer(s) for the module.
concrete.ml.quantization.post_training
Post Training Quantization methods.
ONNX_OPS_TO_NUMPY_IMPL
DEFAULT_MODEL_BITS
ONNX_OPS_TO_QUANTIZED_IMPL
ONNXConverter
Base ONNX to Concrete ML computation graph conversion class.
This class provides a method to parse an ONNX graph and apply several transformations. First, it creates QuantizedOps for each ONNX graph op. These quantized ops have calibrated quantizers that are useful when the operators work on integer data or when the output of the ops is the output of the encrypted program. For operators that compute in float and will be merged to TLUs, these quantizers are not used. Second, this converter creates quantized tensors for initializer and weights stored in the graph.
This class should be sub-classed to provide specific calibration and quantization options depending on the usage (Post-training quantization vs Quantization Aware training).
Arguments:
n_bits
(int, Dict[str, int]): number of bits for quantization, can be a single value or a dictionary with the following keys : - "op_inputs" and "op_weights" (mandatory) - "model_inputs" and "model_outputs" (optional, default to 5 bits). When using a single integer for n_bits, its value is assigned to "op_inputs" and "op_weights" bits. The maximum between this value and a default value (5) is then assigned to the number of "model_inputs" "model_outputs". This default value is a compromise between model accuracy and runtime performance in FHE. "model_outputs" gives the precision of the final network's outputs, while "model_inputs" gives the precision of the network's inputs. "op_inputs" and "op_weights" both control the quantization for inputs and weights of all layers.
y_model
(NumpyModule): Model in numpy.
is_signed
(bool): Whether the weights of the layers can be signed. Currently, only the weights can be signed.
__init__
property n_bits_model_inputs
Get the number of bits to use for the quantization of the first layer's output.
Returns:
n_bits
(int): number of bits for input quantization
property n_bits_model_outputs
Get the number of bits to use for the quantization of the last layer's output.
Returns:
n_bits
(int): number of bits for output quantization
property n_bits_op_inputs
Get the number of bits to use for the quantization of any operators' inputs.
Returns:
n_bits
(int): number of bits for the quantization of the operators' inputs
property n_bits_op_weights
Get the number of bits to use for the quantization of any constants (usually weights).
Returns:
n_bits
(int): number of bits for quantizing constants used by operators
quantize_module
Quantize numpy module.
Following https://arxiv.org/abs/1712.05877 guidelines.
Args:
*calibration_data (numpy.ndarray)
: Data that will be used to compute the bounds, scales and zero point values for every quantized object.
Returns:
QuantizedModule
: Quantized numpy module
PostTrainingAffineQuantization
Post-training Affine Quantization.
Create the quantized version of the passed numpy module.
Args:
n_bits
(int, Dict): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for activation, inputs and weights. If a dict is passed, then it should contain "model_inputs", "op_inputs", "op_weights" and "model_outputs" keys with corresponding number of quantization bits for: - model_inputs : number of bits for model input - op_inputs : number of bits to quantize layer input values - op_weights: learned parameters or constants in the network - model_outputs: final model output quantization bits
numpy_model
(NumpyModule): Model in numpy.
is_signed
: Whether the weights of the layers can be signed. Currently, only the weights can be signed.
Returns:
QuantizedModule
: A quantized version of the numpy model.
__init__
property n_bits_model_inputs
Get the number of bits to use for the quantization of the first layer's output.
Returns:
n_bits
(int): number of bits for input quantization
property n_bits_model_outputs
Get the number of bits to use for the quantization of the last layer's output.
Returns:
n_bits
(int): number of bits for output quantization
property n_bits_op_inputs
Get the number of bits to use for the quantization of any operators' inputs.
Returns:
n_bits
(int): number of bits for the quantization of the operators' inputs
property n_bits_op_weights
Get the number of bits to use for the quantization of any constants (usually weights).
Returns:
n_bits
(int): number of bits for quantizing constants used by operators
quantize_module
Quantize numpy module.
Following https://arxiv.org/abs/1712.05877 guidelines.
Args:
*calibration_data (numpy.ndarray)
: Data that will be used to compute the bounds, scales and zero point values for every quantized object.
Returns:
QuantizedModule
: Quantized numpy module
PostTrainingQATImporter
Converter of Quantization Aware Training networks.
This class provides specific configuration for QAT networks during ONNX network conversion to Concrete ML computation graphs.
__init__
property n_bits_model_inputs
Get the number of bits to use for the quantization of the first layer's output.
Returns:
n_bits
(int): number of bits for input quantization
property n_bits_model_outputs
Get the number of bits to use for the quantization of the last layer's output.
Returns:
n_bits
(int): number of bits for output quantization
property n_bits_op_inputs
Get the number of bits to use for the quantization of any operators' inputs.
Returns:
n_bits
(int): number of bits for the quantization of the operators' inputs
property n_bits_op_weights
Get the number of bits to use for the quantization of any constants (usually weights).
Returns:
n_bits
(int): number of bits for quantizing constants used by operators
quantize_module
Quantize numpy module.
Following https://arxiv.org/abs/1712.05877 guidelines.
Args:
*calibration_data (numpy.ndarray)
: Data that will be used to compute the bounds, scales and zero point values for every quantized object.
Returns:
QuantizedModule
: Quantized numpy module
concrete.ml.sklearn.qnn
Scikit-learn interface for concrete quantized neural networks.
MAXIMUM_TLU_BIT_WIDTH
SparseQuantNeuralNetImpl
Sparse Quantized Neural Network classifier.
This class implements an MLP that is compatible with FHE constraints. The weights and activations are quantized to low bitwidth and pruning is used to ensure accumulators do not surpass an user-provided accumulator bit-width. The number of classes and number of layers are specified by the user, as well as the breadth of the network
__init__
Sparse Quantized Neural Network constructor.
Args:
input_dim
: Number of dimensions of the input data
n_layers
: Number of linear layers for this network
n_outputs
: Number of output classes or regression targets
n_w_bits
: Number of weight bits
n_a_bits
: Number of activation and input bits
n_accum_bits
: Maximal allowed bitwidth of intermediate accumulators
n_hidden_neurons_multiplier
: A factor that is multiplied by the maximal number of active (non-zero weight) neurons for every layer. The maximal number of neurons in the worst case scenario is: 2^n_max-1 max_active_neurons(n_max, n_w, n_a) = floor(---------------------) (2^n_w-1)*(2^n_a-1) ) The worst case scenario for the bitwidth of the accumulator is when all weights and activations are maximum simultaneously. We set, for each layer, the total number of neurons to be: n_hidden_neurons_multiplier * max_active_neurons(n_accum_bits, n_w_bits, n_a_bits) Through experiments, for typical distributions of weights and activations, the default value for n_hidden_neurons_multiplier, 4, is safe to avoid overflow.
activation_function
: a torch class that is used to construct activation functions in the network (e.g. torch.ReLU, torch.SELU, torch.Sigmoid, etc)
Raises:
ValueError
: if the parameters have invalid values or the computed accumulator bitwidth is zero
enable_pruning
Enable pruning in the network. Pruning must be made permanent to recover pruned weights.
Raises:
ValueError
: if the quantization parameters are invalid
forward
Forward pass.
Args:
x
(torch.Tensor): network input
Returns:
x
(torch.Tensor): network prediction
make_pruning_permanent
Make the learned pruning permanent in the network.
max_active_neurons
Compute the maximum number of active (non-zero weight) neurons.
The computation is done using the quantization parameters passed to the constructor. Warning: With the current quantization algorithm (asymmetric) the value returned by this function is not guaranteed to ensure FHE compatibility. For some weight distributions, weights that are 0 (which are pruned weights) will not be quantized to 0. Therefore the total number of active quantized neurons will not be equal to max_active_neurons.
Returns:
n
(int): maximum number of active neurons
on_train_end
Call back when training is finished, can be useful to remove training hooks.
QuantizedSkorchEstimatorMixin
Mixin class that adds quantization features to Skorch NN estimators.
property base_estimator_type
Get the sklearn estimator that should be trained by the child class.
property base_module_to_compile
Get the module that should be compiled to FHE. In our case this is a torch nn.Module.
Returns:
module
(nn.Module): the instantiated torch module
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[Quantizer]
: the input quantizers
property n_bits_quant
Return the number of quantization bits.
This is stored by the torch.nn.module instance and thus cannot be retrieved until this instance is created.
Returns:
n_bits
(int): the number of bits to quantize the network
Raises:
ValueError
: with skorch estimators, the module_
is not instantiated until .fit() is called. Thus this estimator needs to be .fit() before we get the quantization number of bits. If it is not trained we raise an exception
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
_onnx_model_
(onnx.ModelProto): the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
get_params_for_benchmark
Get parameters for benchmark when cloning a skorch wrapped NN.
We must remove all parameters related to the module. Skorch takes either a class or a class instance for the module
parameter. We want to pass our trained model, a class instance. But for this to work, we need to remove all module related constructor params. If not, skorch will instantiate a new class instance of the same type as the passed module see skorch net.py NeuralNet::initialize_instance
Returns:
params
(dict): parameters to create an equivalent fp32 sklearn estimator for benchmark
infer
Perform a single inference step on a batch of data.
This method is specific to Skorch estimators.
Args:
x
(torch.Tensor): A batch of the input data, produced by a Dataset
**fit_params (dict)
: Additional parameters passed to the forward
method of the module and to the self.train_split
call.
Returns: A torch tensor with the inference results for each item in the input
on_train_end
Call back when training is finished by the skorch wrapper.
Check if the underlying neural net has a callback for this event and, if so, call it.
Args:
net
: estimator for which training has ended (equal to self)
X
: data
y
: targets
kwargs
: other arguments
FixedTypeSkorchNeuralNet
A mixin with a helpful modification to a skorch estimator that fixes the module type.
get_params
Get parameters for this estimator.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators.
**kwargs
: any additional parameters to pass to the sklearn BaseEstimator class
Returns:
params
: dict, Parameter names mapped to their values.
NeuralNetClassifier
Scikit-learn interface for quantized FHE compatible neural networks.
This class wraps a quantized NN implemented using our Torch tools as a scikit-learn Estimator. It uses the skorch package to handle training and scikit-learn compatibility, and adds quantization and compilation functionality. The neural network implemented by this class is a multi layer fully connected network trained with Quantization Aware Training (QAT).
The datatypes that are allowed for prediction by this wrapper are more restricted than standard scikit-learn estimators as this class needs to predict in FHE and network inference executor is the NumpyModule.
__init__
property base_estimator_type
property base_module_to_compile
Get the module that should be compiled to FHE. In our case this is a torch nn.Module.
Returns:
module
(nn.Module): the instantiated torch module
property classes_
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property history
property input_quantizers
Get the input quantizers.
Returns:
List[Quantizer]
: the input quantizers
property n_bits_quant
Return the number of quantization bits.
This is stored by the torch.nn.module instance and thus cannot be retrieved until this instance is created.
Returns:
n_bits
(int): the number of bits to quantize the network
Raises:
ValueError
: with skorch estimators, the module_
is not instantiated until .fit() is called. Thus this estimator needs to be .fit() before we get the quantization number of bits. If it is not trained we raise an exception
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
_onnx_model_
(onnx.ModelProto): the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
fit
get_params
Get parameters for this estimator.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators.
**kwargs
: any additional parameters to pass to the sklearn BaseEstimator class
Returns:
params
: dict, Parameter names mapped to their values.
get_params_for_benchmark
Get parameters for benchmark when cloning a skorch wrapped NN.
We must remove all parameters related to the module. Skorch takes either a class or a class instance for the module
parameter. We want to pass our trained model, a class instance. But for this to work, we need to remove all module related constructor params. If not, skorch will instantiate a new class instance of the same type as the passed module see skorch net.py NeuralNet::initialize_instance
Returns:
params
(dict): parameters to create an equivalent fp32 sklearn estimator for benchmark
infer
Perform a single inference step on a batch of data.
This method is specific to Skorch estimators.
Args:
x
(torch.Tensor): A batch of the input data, produced by a Dataset
**fit_params (dict)
: Additional parameters passed to the forward
method of the module and to the self.train_split
call.
Returns: A torch tensor with the inference results for each item in the input
on_train_end
Call back when training is finished by the skorch wrapper.
Check if the underlying neural net has a callback for this event and, if so, call it.
Args:
net
: estimator for which training has ended (equal to self)
X
: data
y
: targets
kwargs
: other arguments
predict
Predict on user provided data.
Predicts using the quantized clear or FHE classifier
Args:
X
: input data, a numpy array of raw values (non quantized)
execute_in_fhe
: whether to execute the inference in FHE or in the clear
Returns:
y_pred
: numpy ndarray with predictions
NeuralNetRegressor
Scikit-learn interface for quantized FHE compatible neural networks.
This class wraps a quantized NN implemented using our Torch tools as a scikit-learn Estimator. It uses the skorch package to handle training and scikit-learn compatibility, and adds quantization and compilation functionality. The neural network implemented by this class is a multi layer fully connected network trained with Quantization Aware Training (QAT).
The datatypes that are allowed for prediction by this wrapper are more restricted than standard scikit-learn estimators as this class needs to predict in FHE and network inference executor is the NumpyModule.
__init__
property base_estimator_type
property base_module_to_compile
Get the module that should be compiled to FHE. In our case this is a torch nn.Module.
Returns:
module
(nn.Module): the instantiated torch module
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property history
property input_quantizers
Get the input quantizers.
Returns:
List[Quantizer]
: the input quantizers
property n_bits_quant
Return the number of quantization bits.
This is stored by the torch.nn.module instance and thus cannot be retrieved until this instance is created.
Returns:
n_bits
(int): the number of bits to quantize the network
Raises:
ValueError
: with skorch estimators, the module_
is not instantiated until .fit() is called. Thus this estimator needs to be .fit() before we get the quantization number of bits. If it is not trained we raise an exception
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
_onnx_model_
(onnx.ModelProto): the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
fit
get_params
Get parameters for this estimator.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators.
**kwargs
: any additional parameters to pass to the sklearn BaseEstimator class
Returns:
params
: dict, Parameter names mapped to their values.
get_params_for_benchmark
Get parameters for benchmark when cloning a skorch wrapped NN.
We must remove all parameters related to the module. Skorch takes either a class or a class instance for the module
parameter. We want to pass our trained model, a class instance. But for this to work, we need to remove all module related constructor params. If not, skorch will instantiate a new class instance of the same type as the passed module see skorch net.py NeuralNet::initialize_instance
Returns:
params
(dict): parameters to create an equivalent fp32 sklearn estimator for benchmark
infer
Perform a single inference step on a batch of data.
This method is specific to Skorch estimators.
Args:
x
(torch.Tensor): A batch of the input data, produced by a Dataset
**fit_params (dict)
: Additional parameters passed to the forward
method of the module and to the self.train_split
call.
Returns: A torch tensor with the inference results for each item in the input
on_train_end
Call back when training is finished by the skorch wrapper.
Check if the underlying neural net has a callback for this event and, if so, call it.
Args:
net
: estimator for which training has ended (equal to self)
X
: data
y
: targets
kwargs
: other arguments
concrete.ml.sklearn.protocols
Protocols.
Protocols are used to mix type hinting with duck-typing. Indeed we don't always want to have an abstract parent class between all objects. We are more interested in the behavior of such objects. Implementing a Protocol is a way to specify the behavior of objects.
To read more about Protocol please read: https://peps.python.org/pep-0544
Quantizer
Quantizer Protocol.
To use to type hint a quantizer.
dequant
Dequantize some values.
Args:
X
(numpy.ndarray): Values to dequantize
.. # noqa: DAR202
Returns:
numpy.ndarray
: Dequantized values
quant
Quantize some values.
Args:
values
(numpy.ndarray): Values to quantize
.. # noqa: DAR202
Returns:
numpy.ndarray
: The quantized values
ConcreteBaseEstimatorProtocol
A Concrete Estimator Protocol.
property onnx_model
onnx_model.
.. # noqa: DAR202
Results: onnx.ModelProto
property quantize_input
Quantize input function.
compile
Compiles a model to a FHE Circuit.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): whether to compile using the virtual library that allows higher bitwidths
p_error
(float): probability of error of a PBS
.. # noqa: DAR202
Returns:
Circuit
: the compiled Circuit.
fit
Initialize and fit the module.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): labels associated with training data
**fit_params
: additional parameters that can be used during training
.. # noqa: DAR202
Returns:
ConcreteBaseEstimatorProtocol
: the trained estimator
fit_benchmark
Fit the quantized estimator and return reference estimator.
This function returns both the quantized estimator (itself), but also a wrapper around the non-quantized trained NN. This is useful in order to compare performance between the quantized and fp32 versions of the classifier
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): labels associated with training data
*args
: The arguments to pass to the underlying model.
**kwargs
: The keyword arguments to pass to the underlying model.
.. # noqa: DAR202
Returns:
self
: self fitted
model
: underlying estimator
post_processing
Post-process models predictions.
Args:
y_preds
(numpy.ndarray): predicted values by model (clear-quantized)
.. # noqa: DAR202
Returns:
numpy.ndarray
: the post-processed predictions
ConcreteBaseClassifierProtocol
Concrete classifier protocol.
property onnx_model
onnx_model.
.. # noqa: DAR202
Results: onnx.ModelProto
property quantize_input
Quantize input function.
compile
Compiles a model to a FHE Circuit.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): whether to compile using the virtual library that allows higher bitwidths
p_error
(float): probability of error of a PBS
.. # noqa: DAR202
Returns:
Circuit
: the compiled Circuit.
fit
Initialize and fit the module.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): labels associated with training data
**fit_params
: additional parameters that can be used during training
.. # noqa: DAR202
Returns:
ConcreteBaseEstimatorProtocol
: the trained estimator
fit_benchmark
Fit the quantized estimator and return reference estimator.
This function returns both the quantized estimator (itself), but also a wrapper around the non-quantized trained NN. This is useful in order to compare performance between the quantized and fp32 versions of the classifier
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): labels associated with training data
*args
: The arguments to pass to the underlying model.
**kwargs
: The keyword arguments to pass to the underlying model.
.. # noqa: DAR202
Returns:
self
: self fitted
model
: underlying estimator
post_processing
Post-process models predictions.
Args:
y_preds
(numpy.ndarray): predicted values by model (clear-quantized)
.. # noqa: DAR202
Returns:
numpy.ndarray
: the post-processed predictions
predict
Predicts for each sample the class with highest probability.
Args:
X
(numpy.ndarray): Features
execute_in_fhe
(bool): Whether the inference should be done in fhe or not.
.. # noqa: DAR202
Returns: numpy.ndarray
predict_proba
Predicts for each sample the probability of each class.
Args:
X
(numpy.ndarray): Features
execute_in_fhe
(bool): Whether the inference should be done in fhe or not.
.. # noqa: DAR202
Returns: numpy.ndarray
ConcreteBaseRegressorProtocol
Concrete regressor protocol.
property onnx_model
onnx_model.
.. # noqa: DAR202
Results: onnx.ModelProto
property quantize_input
Quantize input function.
compile
Compiles a model to a FHE Circuit.
Args:
X
(numpy.ndarray): the dequantized dataset
configuration
(Optional[Configuration]): the options for compilation
compilation_artifacts
(Optional[DebugArtifacts]): artifacts object to fill during compilation
show_mlir
(bool): whether or not to show MLIR during the compilation
use_virtual_lib
(bool): whether to compile using the virtual library that allows higher bitwidths
p_error
(float): probability of error of a PBS
.. # noqa: DAR202
Returns:
Circuit
: the compiled Circuit.
fit
Initialize and fit the module.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): labels associated with training data
**fit_params
: additional parameters that can be used during training
.. # noqa: DAR202
Returns:
ConcreteBaseEstimatorProtocol
: the trained estimator
fit_benchmark
Fit the quantized estimator and return reference estimator.
This function returns both the quantized estimator (itself), but also a wrapper around the non-quantized trained NN. This is useful in order to compare performance between the quantized and fp32 versions of the classifier
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): labels associated with training data
*args
: The arguments to pass to the underlying model.
**kwargs
: The keyword arguments to pass to the underlying model.
.. # noqa: DAR202
Returns:
self
: self fitted
model
: underlying estimator
post_processing
Post-process models predictions.
Args:
y_preds
(numpy.ndarray): predicted values by model (clear-quantized)
.. # noqa: DAR202
Returns:
numpy.ndarray
: the post-processed predictions
predict
Predicts for each sample the expected value.
Args:
X
(numpy.ndarray): Features
execute_in_fhe
(bool): Whether the inference should be done in fhe or not.
.. # noqa: DAR202
Returns: numpy.ndarray
concrete.ml.sklearn.linear_model
Implement sklearn linear model.
LinearRegression
A linear regression model with FHE.
Arguments:
n_bits
(int): default is 2.
use_sum_workaround
(bool): indicate if the sum workaround should be used or not. This
feature is experimental and should be used carefully. Important note
: it only works for a LinearRegression model with N features, N a power of 2, for now. More information available in the QuantizedReduceSum operator. Default to False.
For more details on LinearRegression please refer to the scikit-learn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
fit
Fit the FHE linear model.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
*args
: The arguments to pass to the sklearn linear model.
**kwargs
: The keyword arguments to pass to the sklearn linear model.
Returns: Any
ElasticNet
An ElasticNet regression model with FHE.
Arguments:
n_bits
(int): default is 2.
For more details on ElasticNet please refer to the scikit-learn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.ElasticNet.html
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
Lasso
A Lasso regression model with FHE.
Arguments:
n_bits
(int): default is 2.
For more details on Lasso please refer to the scikit-learn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
Ridge
A Ridge regression model with FHE.
Arguments:
n_bits
(int): default is 2.
For more details on Ridge please refer to the scikit-learn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
LogisticRegression
A logistic regression model with FHE.
Arguments:
n_bits
(int): default is 2.
For more details on LogisticRegression please refer to the scikit-learn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
concrete.ml.sklearn.rf
Implements RandomForest models.
RandomForestClassifier
Implements the RandomForest classifier.
__init__
Initialize the RandomForestClassifier.
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
RandomForestRegressor
Implements the RandomForest regressor.
__init__
Initialize the RandomForestRegressor.
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
concrete.ml.sklearn.svm
Implement Support Vector Machine.
LinearSVR
A Regression Support Vector Machine (SVM).
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
LinearSVC
A Classification Support Vector Machine (SVM).
__init__
property fhe_circuit
Get the FHE circuit.
Returns:
Circuit
: the FHE circuit
property input_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
property output_quantizers
Get the input quantizers.
Returns:
List[QuantizedArray]
: the input quantizers
property quantize_input
Get the input quantization function.
Returns:
Callable
: function that quantizes the input
concrete.ml.sklearn.tree
Implement the sklearn tree models.
DecisionTreeClassifier
Implements the sklearn DecisionTreeClassifier.
__init__
Initialize the DecisionTreeClassifier.
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
DecisionTreeRegressor
Implements the sklearn DecisionTreeClassifier.
__init__
Initialize the DecisionTreeRegressor.
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
concrete.ml.sklearn.tree_to_numpy
Implements the conversion of a tree model to a numpy function.
MAXIMUM_TLU_BIT_WIDTH
OPSET_VERSION_FOR_ONNX_EXPORT
EXPECTED_NUMBER_OF_OUTPUTS_PER_TASK
tree_to_numpy
Convert the tree inference to a numpy functions using Hummingbird.
Args:
model
(onnx.ModelProto): The model to convert.
x
(numpy.ndarray): The input data.
framework
(str): The framework from which the onnx_model is generated.
(options
: 'xgboost', 'sklearn')
task
(Task): The task the model is solving
output_n_bits
(int): The number of bits of the output.
Returns:
Tuple[Callable, List[QuantizedArray], onnx.ModelProto]
: A tuple with a function that takes a numpy array and returns a numpy array, QuantizedArray object to quantize and dequantize the output of the tree, and the ONNX model.
Task
Task enumerate.
concrete.ml.sklearn.xgb
Implements XGBoost models.
XGBClassifier
Implements the XGBoost classifier.
__init__
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
post_processing
Apply post-processing to the predictions.
Args:
y_preds
(numpy.ndarray): The predictions.
Returns:
numpy.ndarray
: The post-processed predictions.
XGBRegressor
Implements the XGBoost regressor.
__init__
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
onnx.ModelProto
: the ONNX model
fit
Fit the tree-based estimator.
Args:
X
: training data By default, you should be able to pass: * numpy arrays * torch tensors * pandas DataFrame or Series
y
(numpy.ndarray): The target data.
**kwargs
: args for super().fit
Returns:
Any
: The fitted model.
post_processing
Apply post-processing to the predictions.
Args:
y_preds
(numpy.ndarray): The predictions.
Returns:
numpy.ndarray
: The post-processed predictions.
concrete.ml.torch.numpy_module
A torch to numpy module.
OPSET_VERSION_FOR_ONNX_EXPORT
NumpyModule
General interface to transform a torch.nn.Module to numpy module.
Args:
torch_model
(Union[nn.Module, onnx.ModelProto]): A fully trained, torch model along with its parameters or the onnx graph of the model.
dummy_input
(Union[torch.Tensor, Tuple[torch.Tensor, ...]]): Sample tensors for all the module inputs, used in the ONNX export to get a simple to manipulate nn representation.
debug_onnx_output_file_path
: (Optional[Union[Path, str]], optional): An optional path to indicate where to save the ONNX file exported by torch for debug. Defaults to None.
__init__
property onnx_model
Get the ONNX model.
.. # noqa: DAR201
Returns:
_onnx_model
(onnx.ModelProto): the ONNX model
forward
Apply a forward pass on args with the equivalent numpy function only.
Args:
*args
: the inputs of the forward function
Returns:
Union[numpy.ndarray, Tuple[numpy.ndarray, ...]]
: result of the forward on the given inputs
concrete.ml.torch.compile
torch compilation function.
MAXIMUM_TLU_BIT_WIDTH
DEFAULT_P_ERROR_PBS
OPSET_VERSION_FOR_ONNX_EXPORT
convert_torch_tensor_or_numpy_array_to_numpy_array
Convert a torch tensor or a numpy array to a numpy array.
Args:
torch_tensor_or_numpy_array
(Tensor): the value that is either a torch tensor or a numpy array.
Returns:
numpy.ndarray
: the value converted to a numpy array.
compile_torch_model
Compile a torch module into an FHE equivalent.
Take a model in torch, turn it to numpy, quantize its inputs / weights / outputs and finally compile it with Concrete-Numpy
Args:
torch_model
(torch.nn.Module): the model to quantize
torch_inputset
(Dataset): the inputset, can contain either torch tensors or numpy.ndarray, only datasets with a single input are supported for now.
import_qat
(bool): Set to True to import a network that contains quantizers and was trained using quantization aware training
configuration
(Configuration): Configuration object to use during compilation
compilation_artifacts
(DebugArtifacts): Artifacts object to fill during compilation
show_mlir
(bool): if set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo
n_bits
: the number of bits for the quantization
use_virtual_lib
(bool): set to use the so called virtual lib simulating FHE computation. Defaults to False
p_error
(Optional[float]): probability of error of a PBS
Returns:
QuantizedModule
: The resulting compiled QuantizedModule.
compile_onnx_model
Compile a torch module into an FHE equivalent.
Take a model in torch, turn it to numpy, quantize its inputs / weights / outputs and finally compile it with Concrete-Numpy
Args:
onnx_model
(onnx.ModelProto): the model to quantize
torch_inputset
(Dataset): the inputset, can contain either torch tensors or numpy.ndarray, only datasets with a single input are supported for now.
import_qat
(bool): Flag to signal that the network being imported contains quantizers in in its computation graph and that Concrete ML should not requantize it.
configuration
(Configuration): Configuration object to use during compilation
compilation_artifacts
(DebugArtifacts): Artifacts object to fill during compilation
show_mlir
(bool): if set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo
n_bits
: the number of bits for the quantization
use_virtual_lib
(bool): set to use the so called virtual lib simulating FHE computation. Defaults to False.
p_error
(Optional[float]): probability of error of a PBS
Returns:
QuantizedModule
: The resulting compiled QuantizedModule.
compile_brevitas_qat_model
Compile a Brevitas Quantization Aware Training model.
The torch_model parameter is a subclass of torch.nn.Module that uses quantized operations from brevitas.qnn. The model is trained before calling this function. This function compiles the trained model to FHE.
Args:
torch_model
(torch.nn.Module): the model to quantize
torch_inputset
(Dataset): the inputset, can contain either torch tensors or numpy.ndarray, only datasets with a single input are supported for now.
n_bits
(Union[int,dict]): the number of bits for the quantization
configuration
(Configuration): Configuration object to use during compilation
compilation_artifacts
(DebugArtifacts): Artifacts object to fill during compilation
show_mlir
(bool): if set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo
use_virtual_lib
(bool): set to use the so called virtual lib simulating FHE computation, defaults to False.
p_error
(Optional[float]): probability of error of a PBS
output_onnx_file
(str): temporary file to store ONNX model. If None a temporary file is generated
Returns:
QuantizedModule
: The resulting compiled QuantizedModule.