concrete.ml.sklearn.base
Base classes for all estimators.
USE_OLD_VL
OPSET_VERSION_FOR_ONNX_EXPORT
QNN_AUTO_KWARGS
BaseEstimator
Base class for all estimators in Concrete ML.
This class does not inherit from sklearn.base.BaseEstimator as it creates some conflicts with skorch in QuantizedTorchEstimatorMixin's subclasses (more specifically, the get_params
method is not properly inherited).
Attributes:
_is_a_public_cml_model
(bool): Private attribute indicating if the class is a public model (as opposed to base or mixin classes).
__init__
Initialize the base class with common attributes used in all estimators.
An underscore "_" is appended to attributes that were created while fitting the model. This is done in order to follow scikit-Learn's standard format. More information available in their documentation: https://scikit-learn.org/stable/developers/develop.html#:~:text=Estimated%20Attributes%C2%B6
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
Compile the model.
Args:
X
(Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
configuration
(Optional[Configuration]): Options to use for compilation. Default to None.
artifacts
(Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
show_mlir
(bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
p_error
(Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
global_p_error
(Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
verbose
(bool): Indicate if compilation information should be printed during compilation. Default to False.
Returns:
Circuit
: The compiled Circuit.
dequantize_output
De-quantize the output.
This step ensures that the fit method has been called.
Args:
q_y_preds
(numpy.ndarray): The quantized output values to de-quantize.
Returns:
numpy.ndarray
: The de-quantized output values.
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
Fit the estimator.
This method trains a scikit-learn estimator, computes its ONNX graph and defines the quantization parameters needed for proper FHE inference.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The fitted estimator.
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
post_processing
Apply post-processing to the de-quantized predictions.
This post-processing step can include operations such as applying the sigmoid or softmax function for classifiers, or summing an ensemble's outputs. These steps are done in the clear because of current technical constraints. They most likely will be integrated in the FHE computations in the future.
For some simple models such a linear regression, there is no post-processing step but the method is kept to make the API consistent for the client-server API. Other models might need to use attributes stored in post_processing_params
.
Args:
y_preds
(numpy.ndarray): The de-quantized predictions to post-process.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict values for X, in FHE or in the clear.
Args:
X
(Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
fhe
(Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.
Returns:
np.ndarray
: The predicted values for X.
quantize_input
Quantize the input.
This step ensures that the fit method has been called.
Args:
X
(numpy.ndarray): The input values to quantize.
Returns:
numpy.ndarray
: The quantized input values.
BaseClassifier
Base class for linear and tree-based classifiers in Concrete ML.
This class inherits from BaseEstimator and modifies some of its methods in order to align them with classifier behaviors. This notably include applying a sigmoid/softmax post-processing to the predicted values as well as handling a mapping of classes in case they are not ordered.
__init__
Initialize the base class with common attributes used in all estimators.
An underscore "_" is appended to attributes that were created while fitting the model. This is done in order to follow scikit-Learn's standard format. More information available in their documentation: https://scikit-learn.org/stable/developers/develop.html#:~:text=Estimated%20Attributes%C2%B6
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property n_classes_
Get the model's number of classes.
Using this attribute is deprecated.
Returns:
int
: The model's number of classes.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
property target_classes_
Get the model's classes.
Using this attribute is deprecated.
Returns:
Optional[numpy.ndarray]
: The model's classes.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
Compile the model.
Args:
X
(Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
configuration
(Optional[Configuration]): Options to use for compilation. Default to None.
artifacts
(Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
show_mlir
(bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
p_error
(Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
global_p_error
(Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
verbose
(bool): Indicate if compilation information should be printed during compilation. Default to False.
Returns:
Circuit
: The compiled Circuit.
dequantize_output
De-quantize the output.
This step ensures that the fit method has been called.
Args:
q_y_preds
(numpy.ndarray): The quantized output values to de-quantize.
Returns:
numpy.ndarray
: The de-quantized output values.
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
post_processing
predict
predict_proba
Predict class probabilities.
Args:
X
(Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
fhe
(Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.
Returns:
numpy.ndarray
: The predicted class probabilities.
quantize_input
Quantize the input.
This step ensures that the fit method has been called.
Args:
X
(numpy.ndarray): The input values to quantize.
Returns:
numpy.ndarray
: The quantized input values.
QuantizedTorchEstimatorMixin
Mixin that provides quantization for a torch module and follows the Estimator API.
__init__
property base_module
Get the Torch module.
Returns:
SparseQuantNeuralNetwork
: The fitted underlying module.
property fhe_circuit
property input_quantizers
Get the input quantizers.
Returns:
List[UniformQuantizer]
: The input quantizers.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
property output_quantizers
Get the output quantizers.
Returns:
List[UniformQuantizer]
: The output quantizers.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
dequantize_output
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
Fit he estimator.
If the module was already initialized, the module will be re-initialized unless warm_start
is set to True. In addition to the torch training step, this method performs quantization of the trained Torch model using Quantization Aware Training (QAT).
Values of dtype float64 are not supported and will be casted to float32.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
**fit_parameters
: Keyword arguments to pass to skorch's fit method.
Returns: The fitted estimator.
fit_benchmark
Fit the quantized estimator as well as its equivalent float estimator.
This function returns both the quantized estimator (itself) as well as its non-quantized (float) equivalent, which are both trained separately. This method differs from the BaseEstimator's fit_benchmark
method as QNNs use QAT instead of PTQ. Hence, here, the float model is topologically equivalent as we have less control over the influence of QAT over the weights.
Values of dtype float64 are not supported and will be casted to float32.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. However, skorch does not handle such a parameter and setting it will have no effect. Defaults to None.
**fit_parameters
: Keyword arguments to pass to skorch's fit method.
Returns: The Concrete ML and equivalent skorch fitted estimators.
get_params
Get parameters for this estimator.
This method is overloaded in order to make sure that auto-computed parameters are not considered when cloning the model (e.g during a GridSearchCV call).
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators.
Returns:
params
(dict): Parameter names mapped to their values.
get_sklearn_params
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
post_processing
predict
Predict values for X, in FHE or in the clear.
Args:
X
(Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
fhe
(Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.
Returns:
np.ndarray
: The predicted values for X.
prune
Prune a copy of this Neural Network model.
This can be used when the number of neurons on the hidden layers is too high. For example, when creating a Neural Network model with n_hidden_neurons_multiplier
high (3-4), it can be used to speed up the model inference in FHE. Many times, up to 50% of neurons can be pruned without losing accuracy, when using this function to fine-tune an already trained model with good accuracy. This method should be used once good accuracy is obtained.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame Pandas Series or List.
n_prune_neurons_percentage
(float): The percentage of neurons to remove. A value of 0 (resp. 1.0) means no (resp. all) neurons will be removed.
fit_params
: Additional parameters to pass to the underlying nn.Module's forward method.
Returns: A new pruned copy of the Neural Network model.
Raises:
ValueError
: If the model has not been trained or has already been pruned.
quantize_input
BaseTreeEstimatorMixin
Mixin class for tree-based estimators.
This class inherits from sklearn.base.BaseEstimator in order to have access to scikit-learn's get_params
and set_params
methods.
__init__
Initialize the TreeBasedEstimatorMixin.
Args:
n_bits
(int): The number of bits used for quantization.
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
dequantize_output
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
post_processing
predict
quantize_input
BaseTreeRegressorMixin
Mixin class for tree-based regressors.
This class is used to create a tree-based regressor class that inherits from sklearn.base.RegressorMixin, which essentially gives access to scikit-learn's score
method for regressors.
__init__
Initialize the TreeBasedEstimatorMixin.
Args:
n_bits
(int): The number of bits used for quantization.
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
dequantize_output
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
post_processing
predict
quantize_input
BaseTreeClassifierMixin
Mixin class for tree-based classifiers.
This class is used to create a tree-based classifier class that inherits from sklearn.base.ClassifierMixin, which essentially gives access to scikit-learn's score
method for classifiers.
Additionally, this class adjusts some of the tree-based base class's methods in order to make them compliant with classification workflows.
__init__
Initialize the TreeBasedEstimatorMixin.
Args:
n_bits
(int): The number of bits used for quantization.
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property n_classes_
Get the model's number of classes.
Using this attribute is deprecated.
Returns:
int
: The model's number of classes.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
property target_classes_
Get the model's classes.
Using this attribute is deprecated.
Returns:
Optional[numpy.ndarray]
: The model's classes.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
dequantize_output
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
post_processing
predict
predict_proba
Predict class probabilities.
Args:
X
(Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
fhe
(Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.
Returns:
numpy.ndarray
: The predicted class probabilities.
quantize_input
SklearnLinearModelMixin
A Mixin class for sklearn linear models with FHE.
This class inherits from sklearn.base.BaseEstimator in order to have access to scikit-learn's get_params
and set_params
methods.
__init__
Initialize the FHE linear model.
Args:
n_bits
(int, Dict[str, int]): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for quantizing inputs and weights. If a dict is passed, then it should contain "op_inputs" and "op_weights" as keys with corresponding number of quantization bits so that: - op_inputs : number of bits to quantize the input values - op_weights: number of bits to quantize the learned parameters Default to 8.
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
Compile the model.
Args:
X
(Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
configuration
(Optional[Configuration]): Options to use for compilation. Default to None.
artifacts
(Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
show_mlir
(bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
p_error
(Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
global_p_error
(Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
verbose
(bool): Indicate if compilation information should be printed during compilation. Default to False.
Returns:
Circuit
: The compiled Circuit.
dequantize_output
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
from_sklearn_model
Build a FHE-compliant model using a fitted scikit-learn model.
Args:
sklearn_model
(sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
X
(Data): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
n_bits
(int, Dict[str, int]): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for quantizing inputs and weights. If a dict is passed, then it should contain "op_inputs" and "op_weights" as keys with corresponding number of quantization bits so that: - op_inputs : number of bits to quantize the input values - op_weights: number of bits to quantize the learned parameters Default to 8.
Returns: The FHE-compliant fitted model.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
post_processing
Apply post-processing to the de-quantized predictions.
This post-processing step can include operations such as applying the sigmoid or softmax function for classifiers, or summing an ensemble's outputs. These steps are done in the clear because of current technical constraints. They most likely will be integrated in the FHE computations in the future.
For some simple models such a linear regression, there is no post-processing step but the method is kept to make the API consistent for the client-server API. Other models might need to use attributes stored in post_processing_params
.
Args:
y_preds
(numpy.ndarray): The de-quantized predictions to post-process.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict values for X, in FHE or in the clear.
Args:
X
(Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
fhe
(Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.
Returns:
np.ndarray
: The predicted values for X.
quantize_input
SklearnLinearRegressorMixin
A Mixin class for sklearn linear regressors with FHE.
This class is used to create a linear regressor class that inherits from sklearn.base.RegressorMixin, which essentially gives access to scikit-learn's score
method for regressors.
__init__
Initialize the FHE linear model.
Args:
n_bits
(int, Dict[str, int]): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for quantizing inputs and weights. If a dict is passed, then it should contain "op_inputs" and "op_weights" as keys with corresponding number of quantization bits so that: - op_inputs : number of bits to quantize the input values - op_weights: number of bits to quantize the learned parameters Default to 8.
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
Compile the model.
Args:
X
(Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
configuration
(Optional[Configuration]): Options to use for compilation. Default to None.
artifacts
(Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
show_mlir
(bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
p_error
(Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
global_p_error
(Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
verbose
(bool): Indicate if compilation information should be printed during compilation. Default to False.
Returns:
Circuit
: The compiled Circuit.
dequantize_output
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
from_sklearn_model
Build a FHE-compliant model using a fitted scikit-learn model.
Args:
sklearn_model
(sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
X
(Data): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
n_bits
(int, Dict[str, int]): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for quantizing inputs and weights. If a dict is passed, then it should contain "op_inputs" and "op_weights" as keys with corresponding number of quantization bits so that: - op_inputs : number of bits to quantize the input values - op_weights: number of bits to quantize the learned parameters Default to 8.
Returns: The FHE-compliant fitted model.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
post_processing
Apply post-processing to the de-quantized predictions.
This post-processing step can include operations such as applying the sigmoid or softmax function for classifiers, or summing an ensemble's outputs. These steps are done in the clear because of current technical constraints. They most likely will be integrated in the FHE computations in the future.
For some simple models such a linear regression, there is no post-processing step but the method is kept to make the API consistent for the client-server API. Other models might need to use attributes stored in post_processing_params
.
Args:
y_preds
(numpy.ndarray): The de-quantized predictions to post-process.
Returns:
numpy.ndarray
: The post-processed predictions.
predict
Predict values for X, in FHE or in the clear.
Args:
X
(Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
fhe
(Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.
Returns:
np.ndarray
: The predicted values for X.
quantize_input
SklearnLinearClassifierMixin
A Mixin class for sklearn linear classifiers with FHE.
This class is used to create a linear classifier class that inherits from sklearn.base.ClassifierMixin, which essentially gives access to scikit-learn's score
method for classifiers.
Additionally, this class adjusts some of the tree-based base class's methods in order to make them compliant with classification workflows.
__init__
Initialize the FHE linear model.
Args:
n_bits
(int, Dict[str, int]): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for quantizing inputs and weights. If a dict is passed, then it should contain "op_inputs" and "op_weights" as keys with corresponding number of quantization bits so that: - op_inputs : number of bits to quantize the input values - op_weights: number of bits to quantize the learned parameters Default to 8.
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property n_classes_
Get the model's number of classes.
Using this attribute is deprecated.
Returns:
int
: The model's number of classes.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
property target_classes_
Get the model's classes.
Using this attribute is deprecated.
Returns:
Optional[numpy.ndarray]
: The model's classes.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
Compile the model.
Args:
X
(Data): A representative set of input values used for building cryptographic parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
configuration
(Optional[Configuration]): Options to use for compilation. Default to None.
artifacts
(Optional[DebugArtifacts]): Artifacts information about the compilation process to store for debugging. Default to None.
show_mlir
(bool): Indicate if the MLIR graph should be printed during compilation. Default to False.
p_error
(Optional[float]): Probability of error of a single PBS. A p_error value cannot be given if a global_p_error value is already set. Default to None, which sets this error to a default value.
global_p_error
(Optional[float]): Probability of error of the full circuit. A global_p_error value cannot be given if a p_error value is already set. This feature is not supported during the FHE simulation mode, meaning the probability is currently set to 0. Default to None, which sets this error to a default value.
verbose
(bool): Indicate if compilation information should be printed during compilation. Default to False.
Returns:
Circuit
: The compiled Circuit.
decision_function
Predict confidence scores.
Args:
X
(Data): The input values to predict, as a Numpy array, Torch tensor, Pandas DataFrame or List.
fhe
(Union[FheMode, str]): The mode to use for prediction. Can be FheMode.DISABLE for Concrete ML Python inference, FheMode.SIMULATE for FHE simulation and FheMode.EXECUTE for actual FHE execution. Can also be the string representation of any of these values. Default to FheMode.DISABLE.
Returns:
numpy.ndarray
: The predicted confidence scores.
dequantize_output
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
from_sklearn_model
Build a FHE-compliant model using a fitted scikit-learn model.
Args:
sklearn_model
(sklearn.base.BaseEstimator): The fitted scikit-learn model to convert.
X
(Data): A representative set of input values used for computing quantization parameters, as a Numpy array, Torch tensor, Pandas DataFrame or List. This is usually the training data-set or a sub-set of it.
n_bits
(int, Dict[str, int]): Number of bits to quantize the model. If an int is passed for n_bits, the value will be used for quantizing inputs and weights. If a dict is passed, then it should contain "op_inputs" and "op_weights" as keys with corresponding number of quantization bits so that: - op_inputs : number of bits to quantize the input values - op_weights: number of bits to quantize the learned parameters Default to 8.
Returns: The FHE-compliant fitted model.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
post_processing
predict
predict_proba
quantize_input
SklearnKNeighborsMixin
A Mixin class for sklearn KNeighbors models with FHE.
This class inherits from sklearn.base.BaseEstimator in order to have access to scikit-learn's get_params
and set_params
methods.
__init__
Initialize the FHE knn model.
Args:
n_bits
(int): Number of bits to quantize the model. IThe value will be used for quantizing inputs and X_fit. Default to 3.
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
dequantize_output
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
majority_vote
Determine the most common class among nearest neighborsfor each query.
Args:
nearest_classes
(numpy.ndarray): The class labels of the nearest neighbors for a query
Returns:
numpy.ndarray
: The majority-voted class label for the corresponding query.
post_processing
Perform the majority.
For KNN, the de-quantization step is not required. Because _inference returns the label of the k-nearest neighbors.
Args:
y_preds
(numpy.ndarray): The topk nearest labels
Returns:
numpy.ndarray
: The majority vote.
predict
quantize_input
SklearnKNeighborsClassifierMixin
A Mixin class for sklearn KNeighbors classifiers with FHE.
This class is used to create a KNeighbors classifier class that inherits from SklearnKNeighborsMixin and sklearn.base.ClassifierMixin. By inheriting from sklearn.base.ClassifierMixin, it allows this class to be recognized as a classifier."
__init__
Initialize the FHE knn model.
Args:
n_bits
(int): Number of bits to quantize the model. IThe value will be used for quantizing inputs and X_fit. Default to 3.
property fhe_circuit
Get the FHE circuit.
The FHE circuit combines computational graph, mlir, client and server into a single object. More information available in Concrete documentation (https://docs.zama.ai/concrete/getting-started/terminology_and_structure) Is None if the model is not fitted.
Returns:
Circuit
: The FHE circuit.
property is_compiled
Indicate if the model is compiled.
Returns:
bool
: If the model is compiled.
property is_fitted
Indicate if the model is fitted.
Returns:
bool
: If the model is fitted.
property onnx_model
Get the ONNX model.
Is None if the model is not fitted.
Returns:
onnx.ModelProto
: The ONNX model.
check_model_is_compiled
Check if the model is compiled.
Raises:
AttributeError
: If the model is not compiled.
check_model_is_fitted
Check if the model is fitted.
Raises:
AttributeError
: If the model is not fitted.
compile
dequantize_output
dump
Dump itself to a file.
Args:
file
(TextIO): The file to dump the serialized object into.
dump_dict
Dump the object as a dict.
Returns:
Dict[str, Any]
: Dict of serialized objects.
dumps
Dump itself to a string.
Returns:
metadata
(str): String of the serialized object.
fit
fit_benchmark
Fit both the Concrete ML and its equivalent float estimators.
Args:
X
(Data): The training data, as a Numpy array, Torch tensor, Pandas DataFrame or List.
y
(Target): The target data, as a Numpy array, Torch tensor, Pandas DataFrame, Pandas Series or List.
random_state
(Optional[int]): The random state to use when fitting. Defaults to None.
**fit_parameters
: Keyword arguments to pass to the float estimator's fit method.
Returns: The Concrete ML and float equivalent fitted estimators.
get_sklearn_params
Get parameters for this estimator.
This method is used to instantiate a scikit-learn model using the Concrete ML model's parameters. It does not override scikit-learn's existing get_params
method in order to not break its implementation of set_params
.
Args:
deep
(bool): If True, will return the parameters for this estimator and contained subobjects that are estimators. Default to True.
Returns:
params
(dict): Parameter names mapped to their values.
load_dict
Load itself from a dict.
Args:
metadata
(Dict[str, Any]): Dict of serialized objects.
Returns:
BaseEstimator
: The loaded object.
majority_vote
Determine the most common class among nearest neighborsfor each query.
Args:
nearest_classes
(numpy.ndarray): The class labels of the nearest neighbors for a query
Returns:
numpy.ndarray
: The majority-voted class label for the corresponding query.
post_processing
Perform the majority.
For KNN, the de-quantization step is not required. Because _inference returns the label of the k-nearest neighbors.
Args:
y_preds
(numpy.ndarray): The topk nearest labels
Returns:
numpy.ndarray
: The majority vote.
predict
quantize_input