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.
Simulation
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.
from sklearn.datasets import fetch_openml, make_circlesfrom concrete.ml.sklearn import RandomForestClassifiern_bits =2X, y =make_circles(n_samples=1000, noise=0.1, factor=0.6, random_state=0)concrete_clf =RandomForestClassifier( n_bits=n_bits, n_estimators=10, max_depth=5)concrete_clf.fit(X, y)concrete_clf.compile(X)# Running the model using FHE-simulationy_preds_clear = concrete_clf.predict(X, fhe="simulate")
Caching keys during debugging
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:
Upon execution, the Compiler will raise the following error within the graph representation:
Function you are trying to compile cannot be compiled:
%0 = _x # EncryptedTensor<int7, shape=(1, 2)> ∈ [-64, 63]
%1 = [[ -9 18 ... 30 34]] # ClearTensor<int7, shape=(2, 120)> ∈ [-62, 63] @ /fc1/Gemm.matmul
%2 = matmul(%0, %1) # EncryptedTensor<int14, shape=(1, 120)> ∈ [-5834, 5770] @ /fc1/Gemm.matmul
%3 = subgraph(%2) # EncryptedTensor<uint7, shape=(1, 120)> ∈ [0, 127]
%4 = [[-36 6 ... 27 -11]] # ClearTensor<int7, shape=(120, 120)> ∈ [-63, 63] @ /fc2/Gemm.matmul
%5 = matmul(%3, %4) # EncryptedTensor<int17, shape=(1, 120)> ∈ [-34666, 37702] @ /fc2/Gemm.matmul
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this 17-bit value is used as an input to a table lookup
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.
Fixing compilation errors
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.
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: