This section provides a set of tools and guidelines to help users build optimized FHE-compatible models. It discusses FHE simulation, the key-cache functionality that helps speed-up FHE result debugging, and gives a guide to evaluate circuit complexity.
The simulation functionality of Concrete ML provides a way to evaluate, using clear data, the results that ML models produce on encrypted data. The simulation includes any probabilistic behavior FHE may induce. The simulation is implemented with Concrete's simulation.
The simulation mode can be useful when developing and iterating on an ML model implementation. As FHE non-linear models work with integers up to 16 bits, with a trade-off between the number of bits and the FHE execution speed, the simulation can help to find the optimal model design.
Simulation is much faster than FHE execution. This allows for faster debugging and model optimization. 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 simulation mode in Concrete ML.
It is possible to avoid re-generating the keys of the models you are debugging. This feature is unsafe and should not be used in production. Here is an example that shows how to enable key-caching:
Error message: this [N]-bit value is used as an input to a table lookup
Cause: This error can occur when rounding_threshold_bits
is not used and accumulated intermediate values in the computation exceed 16 bits.
Possible solutions:
Reduce quantization n_bits
. However, this may reduce accuracy. When quantization n_bits
must be below 6, it is best to use Quantization Aware Training.
Use rounding_threshold_bits
. This feature is described here. It is recommended to use the fhe.Exactness.APPROXIMATE
setting, and set the rounding bits to 1 or 2 bits higher than the quantization n_bits
Use pruning
Error message: RuntimeError: NoParametersFound
Cause: This error occurs when using rounding_threshold_bits
in the compile_torch_model
function.
Possible solutions: The solutions in this case are similar to the ones for the previous error.
Error message: Error occurred during quantization aware training (QAT) import [...] Could not determine a unique scale for the quantization!
.
Cause: This error occurs when the model imported as a quantized-aware training model lacks quantization operators. See this guide on how to use Brevitas layers. This error message indicates that some layers do not take inputs quantized through QuantIdentity
layers.
A common example is related to the concatenation operator. Suppose two tensors x
and y
are produced by two layers and need to be concatenated:
In the example above, the x
and y
layers need quantization before being concatenated.
Possible solutions:
If the error occurs for the first layer of the model: Add a QuantIdentity
layer in your model and apply it on the input of the forward
function, before the first layer is computed.
If the error occurs for a concatenation or addition layer: Add a new QuantIdentity
layer in your model. Suppose it is called quant_concat
. In the forward
function, before concatenation of x
and y
, apply it to both tensors that are concatenated. The usage of a common Quantidentity
layer to quantize both tensors that are concatenated ensures that they have the same scale:
Compilation errors due to FHE incompatible models, such as maximum bit-width exceeded or NoParametersFound
can be debugged by examining the bit-widths associated with various intermediate values of the FHE computation.
The following produces a neural network that is not FHE-compatible:
Upon execution, the Compiler will raise the following error within the graph representation:
The error this 17-bit value is used as an input to a table lookup
indicates that the 16-bit limit on the input of the Table Lookup (TLU) operation has been exceeded. To pinpoint the model layer that causes the error, Concrete ML provides the bitwidth_and_range_report helper function. First, the model must be compiled so that it can be simulated.
On the other hand, NoParametersFound
is encountered when using rounding_threshold_bits
. When using this setting, the 16-bit accumulator limit is relaxed. However, reducing bit-width, or reducing the rounding_threshold_bits
, or using using the fhe.Exactness.APPROXIMATE
rounding method can help.
To make this network FHE-compatible one can apply several techniques:
use rounded accumulators by specifying the rounding_threshold_bits
parameter. Please evaluate the accuracy of the model using simulation if you use this feature, as it may impact accuracy. Setting a value 2-bit higher than the quantization n_bits
should be a good start.
reduce the accumulator bit-width of the second layer named fc2
. To do this, a simple solution is to reduce the number of neurons, as it is proportional to the bit-width.
adjust the tolerance for one-off errors using the p_error
parameter. See this section for more explanation on this tolerance.
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, compared to simpler encrypted operations such as 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.
Retrieving the bit-width of the circuit is then simply:
Decreasing the number of bits and the number of PBS applications induces large reductions in the computation time of the compiled circuit.