HeModel#

class HeModel#

A class to represent an AI model that is encoded and possibly encrypted under HE, that supports running an inference and possibly training process.

static compile(plain: pyhelayers.PlainModel, he_run_req: pyhelayers.HeRunRequirements) pyhelayers.HeProfile | None#

Compiles a plain model under a given HE run requirements. Returns an optional HE profile that is populated if the compilation is possible given the plain model and requirements, or empty otherwise.

Parameters:
  • plain – The plain model. Must be initialized.

  • he_run_req – The HE run requirements.

static create_context(profile: pyhelayers.HeProfile) pyhelayers.HeContext#

Creates, initializes and returns an HE context given an HE profile that was obtained as an output of model compilation.

Parameters:

profile – The given HE profile.

create_model_io_encoder(self: pyhelayers.HeModel) pyhelayers.ModelIoEncoder#

Create a model IO encoder for this model.

decrypt_decode(self: pyhelayers.HeModel) pyhelayers.PlainModel#

Decrypts a trained model (after calling “fit”) to obtain a trained plain model. Requires the HE context to contain a secret key.

encode(*args, **kwargs)#

Overloaded function.

  1. encode(self: pyhelayers.HeModel, plain: pyhelayers.PlainModel, profile: pyhelayers.HeProfile, inputs: List[numpy.ndarray[numpy.float64]] = []) -> None

    Encodes a model given a plain model and an HE profile that was obtained as an output of model compilation. The resulting model will have non-encrypted weights.

    param plain:

    The plain model. Must be initialized.

    param profile:

    The given HE profile.

    param inputs:

    An optional representative plaintext inputs, to be used for preventing overflows. It is recommended to include representative inputs, including ones that might produce intermediate results with high values during the computation, as these inputs will be used to tune the model. NOTE: Providing inputs and running the overflows prevention mechanism may result in derivatives of the given inputs or model weights to be present in plaintext inside the model and in any IO processor created from it.

  2. encode(self: pyhelayers.HeModel, streams: List[bytes], files: List[str], he_run_req: pyhelayers.HeRunRequirements, hyper_params: pyhelayers.PlainModelHyperParams = <pyhelayers.PlainModelHyperParams object at 0x7f60252dbcb0>, inputs: List[numpy.ndarray[numpy.float64]] = []) -> None

    Loads the given model and prepared it to run with encrypted data, with the model itself not encrypted. Optional parameters can control various additional settings.

    Internally, this method initializes a plain model using PlainModel.init, compiles a model using HeModel.compile and calls HeModel.encode.

    param streams:

    List of streams with content to load, depending on the underlying model. All streams must be rewindable.

    param files:

    List of file names corresponding to each stream.

    param he_run_req:

    The HE run requirements.

    param hyper_params:

    The hyperparameters object.

    param inputs:

    An optional representative plaintext inputs, to be used for preventing overflows. It is recommended to include representative inputs, including ones that might produce intermediate results with high values during the computation, as these inputs will be used to tune the model. NOTE: Providing inputs and running the overflows prevention mechanism may result in derivatives of the given inputs or model weights to be present in plaintext inside the model and in any IO processor created from it.

  3. encode(self: pyhelayers.HeModel, files: List[str], he_run_req: pyhelayers.HeRunRequirements, hyper_params: pyhelayers.PlainModelHyperParams = <pyhelayers.PlainModelHyperParams object at 0x7f60231260b0>, inputs: List[numpy.ndarray[numpy.float64]] = []) -> None

    Loads the given model and prepared it to run with encrypted data, with the model itself not encrypted. Optional parameters can control various additional settings.

    Internally, this method initializes a plain model using PlainModel.init, compiles a model using HeModel.compile and calls HeModel.encode.

    param files:

    List of file names with content to load, depending on the underlying model.

    param he_run_req:

    The HE run requirements.

    param hyper_params:

    The hyperparameters object.

    param inputs:

    An optional representative plaintext inputs, to be used for preventing overflows. It is recommended to include representative inputs, including ones that might produce intermediate results with high values during the computation, as these inputs will be used to tune the model. NOTE: Providing inputs and running the overflows prevention mechanism may result in derivatives of the given inputs or model weights to be present in plaintext inside the model and in any IO processor created from it.

encode_encrypt(*args, **kwargs)#

Overloaded function.

  1. encode_encrypt(self: pyhelayers.HeModel, plain: pyhelayers.PlainModel, profile: pyhelayers.HeProfile, inputs: List[numpy.ndarray[numpy.float64]] = []) -> None

    Encodes and encrypts a model given a plain model and an HE profile that was obtained as an output of model compilation. The resulting model will have encrypted weights.

    NOTE: while the model weights are encrypted, the model architecture and/or structure is not hidden. For example, in neural network the non-hidden architecture includes the number of layers, the order of the layers, each layer’s type, input shape, output shape and internal parameters (e.g. number of neurons in Dense layer; size of kernel, strides and padding in Conv2D layer; the degree of a polynomial activation layer and the sign of its leading coefficient, etc.).

    param plain:

    The plain model. Must be initialized.

    param profile:

    The given HE profile.

    param inputs:

    An optional representative plaintext inputs, to be used for preventing overflows. It is recommended to include representative inputs, including ones that might produce intermediate results with high values during the computation, as these inputs will be used to tune the model. NOTE: Providing inputs and running the overflows prevention mechanism may result in derivatives of the given inputs or model weights to be present in plaintext inside the model and in any IO processor created from it.

  2. encode_encrypt(self: pyhelayers.HeModel, streams: List[bytes], files: List[str], he_run_req: pyhelayers.HeRunRequirements, hyper_params: pyhelayers.PlainModelHyperParams = <pyhelayers.PlainModelHyperParams object at 0x7f60252dbb30>, inputs: List[numpy.ndarray[numpy.float64]] = []) -> None

    Loads the given model and prepared it to run with encrypted data, and encrypted model. Optional parameters can control various additional

    settings.

    Internally, this method initializes a plain model using PlainModel.init, compiles a model using HeModel.compile and calls HeModel.encode_encrypt.

    param streams:

    List of streams with content to load, depending on the underlying model. All streams must be rewindable.

    param files:

    List of file names corresponding to each stream.

    param he_run_req:

    The HE run requirements.

    param hyper_params:

    The hyperparameters object.

    param inputs:

    An optional representative plaintext inputs, to be used for preventing overflows. It is recommended to include representative inputs, including ones that might produce intermediate results with high values during the computation, as these inputs will be used to tune the model. NOTE: Providing inputs and running the overflows prevention mechanism may result in derivatives of the given inputs or model weights to be present in plaintext inside the model and in any IO processor created from it.

  3. encode_encrypt(self: pyhelayers.HeModel, files: List[str], he_run_req: pyhelayers.HeRunRequirements, hyper_params: pyhelayers.PlainModelHyperParams = <pyhelayers.PlainModelHyperParams object at 0x7f60252dbfb0>, inputs: List[numpy.ndarray[numpy.float64]] = []) -> None

    Loads the given model and prepared it to run with encrypted data, and encrypted model. Optional parameters can control various additional

    settings.

    Internally, this method initializes a plain model using PlainModel.init, compiles a model using HeModel.compile and calls HeModel.encode_encrypt.

    param files:

    List of file names with content to load, depending on the underlying model.

    param he_run_req:

    The HE run requirements.

    param hyper_params:

    The hyperparameters object.

    param inputs:

    An optional representative plaintext inputs, to be used for preventing overflows. It is recommended to include representative inputs, including ones that might produce intermediate results with high values during the computation, as these inputs will be used to tune the model. NOTE: Providing inputs and running the overflows prevention mechanism may result in derivatives of the given inputs or model weights to be present in plaintext inside the model and in any IO processor created from it.

fit(self: pyhelayers.HeModel, inputs: pyhelayers.EncryptedData) None#

Fit (train) the model under FHE.

Parameters:

inputs – The encrypted training inputs.

get_can_switch_between_predict_fit_modes(self: pyhelayers.HeModel) bool#

Check whether this model can be switched from fit to predict mode.

get_created_he_context(self: pyhelayers.HeModel) pyhelayers.HeContext#

Returns a shared pointer to the HeContext object that was created while initializing this model using encode() or encodeEncrypt() method directly from plain model files or streams. Returns null if this model was initialized with an HeContext created beforehand.

get_encrypted_internals(self: pyhelayers.HeModel) pyhelayers.EncryptedData#

Returns the encrypted internals (e.g., weights) of this model. The packing of the encrypted internals depends on the model type. The encrypted internals can be decrypted and decoded independently, and be given as input to the method get_plain_model_from_decoded_internals().

get_fit_hyperparams(self: pyhelayers.HeModel) pyhelayers.FitHyperParams#

Returns common training related hyper parameters

get_he_context(self: pyhelayers.HeModel) pyhelayers.HeContext#

Returns the HE context object this model uses

get_plain_model_from_decoded_internals(self: pyhelayers.HeModel, decoded_internals: list) pyhelayers.PlainModel#

Constructs a plain model from the given decoded internals (decrypted and decoded from the output of getEncryptedInternals()). The same HeModel object should be used for both getEncryptedInternals() and this method.

Parameters:

decoded_internals – The decoded internals which were decrypted and

decoded from the output of getEncryptedInternals().

get_profile(self: pyhelayers.HeModel) pyhelayers.HeProfile#

Returns the HE profile that was used to build the model.

load_encrypted_data(self: pyhelayers.HeModel, buf: bytes) pyhelayers.EncryptedData#

Loads encrypted data from the given buffer.

Parameters:

buf – The buffer to load from.

Raises:

RuntimeError – If the given buffer does not contain an EncryptedData object, or if the object was saved with a mismatching HElayers version.

load_encrypted_data_from_file(self: pyhelayers.HeModel, path: str) pyhelayers.EncryptedData#

Loads encrypted data from the given file.

Parameters:

path – The path of the file to load from.

Raises:

RuntimeError – If the given file does not contain an EncryptedData object, or if the object was saved with a mismatching HElayers version.

predict(*args, **kwargs)#

Overloaded function.

  1. predict(self: pyhelayers.HeModel, res: pyhelayers.EncryptedData, inputs: pyhelayers.EncryptedData) -> None

    Run prediction (inference) over the model under FHE.

    param res:

    An output parameter to store the prediction result (encrypted predictions) in.

    param inputs:

    The encrypted inputs.

  2. predict(self: pyhelayers.HeModel, res: pyhelayers.EncryptedBatch, inputs: pyhelayers.EncryptedBatch) -> None

    Run prediction (inference) of a single batch over the model under FHE.

    param res:

    An output parameter to store the prediction result (encrypted predictions of a single batch) in.

    param inputs:

    A single batch of encrypted inputs.

switch_to_predict_mode(self: pyhelayers.HeModel) None#

Switch this model from fit to predict mode.