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, inputs: List[pyhelayers.DoubleTensor] = []) 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.

  • inputs – An optional representative plaintext inputs.

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_io_processor(self: pyhelayers.HeModel) pyhelayers.ModelIoProcessor#

Creates and returns an IO processor object, to be used to prepare and encrypt the model’s inputs, and to decrypt the model’s outputs.

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

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

  • profile – The given HE profile.

  • 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(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.).

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

  • profile – The given HE profile.

  • 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_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_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.

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.