concrete.ml.sklearn.base

arrow-up-right

module concrete.ml.sklearn.base

Module that contains base classes for our libraries estimators.

Global Variables

  • DEFAULT_P_ERROR_PBS

  • OPSET_VERSION_FOR_ONNX_EXPORT


arrow-up-right

class 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)

arrow-up-right

method __init__

__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


arrow-up-right

method 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


arrow-up-right

method 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


arrow-up-right

method 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


arrow-up-right

method 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


arrow-up-right

method 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


arrow-up-right

method 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


arrow-up-right

method 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


arrow-up-right

class BaseTreeEstimatorMixin

Mixin class for tree-based estimators.

A place to share methods that are used on all tree-based estimators.

arrow-up-right

method __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


arrow-up-right

method 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.


arrow-up-right

method dequantize_output

Dequantize the integer predictions.

Args:

  • y_preds (numpy.ndarray): the predictions

Returns: the dequantized predictions


arrow-up-right

method 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.


arrow-up-right

method quantize_input

Quantize the input.

Args:

  • X (numpy.ndarray): the input

Returns: the quantized input


arrow-up-right

class BaseTreeRegressorMixin

Mixin class for tree-based regressors.

A place to share methods that are used on all tree-based regressors.

arrow-up-right

method __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


arrow-up-right

method 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.


arrow-up-right

method dequantize_output

Dequantize the integer predictions.

Args:

  • y_preds (numpy.ndarray): the predictions

Returns: the dequantized predictions


arrow-up-right

method 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.


arrow-up-right

method 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.


arrow-up-right

method post_processing

Apply post-processing to the predictions.

Args:

  • y_preds (numpy.ndarray): The predictions.

Returns:

  • numpy.ndarray: The post-processed predictions.


arrow-up-right

method 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.


arrow-up-right

method quantize_input

Quantize the input.

Args:

  • X (numpy.ndarray): the input

Returns: the quantized input


arrow-up-right

class BaseTreeClassifierMixin

Mixin class for tree-based classifiers.

A place to share methods that are used on all tree-based classifiers.

arrow-up-right

method __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


arrow-up-right

method 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.


arrow-up-right

method dequantize_output

Dequantize the integer predictions.

Args:

  • y_preds (numpy.ndarray): the predictions

Returns: the dequantized predictions


arrow-up-right

method 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.


arrow-up-right

method 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.


arrow-up-right

method post_processing

Apply post-processing to the predictions.

Args:

  • y_preds (numpy.ndarray): The predictions.

Returns:

  • numpy.ndarray: The post-processed predictions.


arrow-up-right

method 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.


arrow-up-right

method 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.


arrow-up-right

method quantize_input

Quantize the input.

Args:

  • X (numpy.ndarray): the input

Returns: the quantized input


arrow-up-right

class SklearnLinearModelMixin

A Mixin class for sklearn linear models with FHE.

arrow-up-right

method __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


arrow-up-right

method 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.


arrow-up-right

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.


arrow-up-right

method 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


arrow-up-right

method 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.


arrow-up-right

method post_processing

Post-processing the output.

Args:

  • y_preds (numpy.ndarray): the output to post-process

Returns:

  • numpy.ndarray: the post-processed output


arrow-up-right

method 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


arrow-up-right

class SklearnLinearClassifierMixin

A Mixin class for sklearn linear classifiers with FHE.

arrow-up-right

method __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


arrow-up-right

method 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.


arrow-up-right

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.


arrow-up-right

method 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.


arrow-up-right

method 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


arrow-up-right

method 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.


arrow-up-right

method 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.


arrow-up-right

method 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.


arrow-up-right

method 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.

Last updated

Was this helpful?