10 KiB
module torus.ml.deployment.fhe_client_server
APIs for FHE deployment.
Global Variables
- CML_VERSION
function check_torus_versions
check_torus_versions(zip_path: Path)
Check that current versions match the ones used in development.
This function loads the version JSON file found in client.zip or server.zip files and then checks that current package versions (Torus Python, Torus ML) as well as the Python current version all match the ones that are currently installed.
Args:
zip_path(Path): The path to the client or server zip file that contains the version.json file to check.
Raises:
ValueError: If at least one version mismatch is found.
class DeploymentMode
Mode for the FHE API.
class FHEModelServer
Server API to load and run the FHE circuit.
method __init__
__init__(path_dir: str)
Initialize the FHE API.
Args:
path_dir(str): the path to the directory where the circuit is saved
method load
load()
Load the circuit.
method run
run(
serialized_encrypted_quantized_data: Union[bytes, Value, Tuple[bytes, ], Tuple[Value, ]],
serialized_evaluation_keys: bytes
) → Union[bytes, Value, Tuple[bytes, ], Tuple[Value, ]]
Run the model on the server over encrypted data.
Args:
serialized_encrypted_quantized_data(Union[bytes, fhe.Value, Tuple[bytes, ...], Tuple[fhe.Value, ...]]): The encrypted and quantized values to consider. If these values are serialized (in bytes), they are first deserialized.serialized_evaluation_keys(bytes): The evaluation keys. If they are serialized (in bytes), they are first deserialized.
Returns:
Union[bytes, fhe.Value, Tuple[bytes, ...], Tuple[fhe.Value, ...]]: The model's encrypted and quantized results. If the inputs were initially serialized, the outputs are also serialized.
class FHEModelDev
Dev API to save the model and then load and run the FHE circuit.
method __init__
__init__(path_dir: str, model: Any = None)
Initialize the FHE API.
Args:
path_dir(str): the path to the directory where the circuit is savedmodel(Any): the model to use for the FHE API
method save
save(
mode: DeploymentMode = <DeploymentMode.INFERENCE: 'inference'>,
via_mlir: bool = True
)
Export all needed artifacts for the client and server.
Arguments:
mode(DeploymentMode): the mode to save the FHE circuit, either "inference" or "training".via_mlir(bool): serialize withvia_mliroption from Torus-Python.
Raises:
Exception: path_dir is not empty or training module does not existValueError: if mode is not "inference" or "training"
class FHEModelClient
Client API to encrypt and decrypt FHE data.
method __init__
__init__(path_dir: str, key_dir: Optional[str] = None)
Initialize the FHE API.
Args:
path_dir(str): the path to the directory where the circuit is savedkey_dir(str): the path to the directory where the keys are stored
method deserialize_decrypt
deserialize_decrypt(
*serialized_encrypted_quantized_result: Optional[bytes]
) → Union[Any, Tuple[Any, ]]
Deserialize and decrypt the values.
Args:
serialized_encrypted_quantized_result(Optional[bytes]): The serialized, encrypted and quantized values.
Returns:
Union[Any, Tuple[Any, ...]]: The decrypted and deserialized values.
method deserialize_decrypt_dequantize
deserialize_decrypt_dequantize(
*serialized_encrypted_quantized_result: Optional[bytes]
) → Union[ndarray, Tuple[ndarray, ]]
Deserialize, decrypt and de-quantize the values.
Args:
serialized_encrypted_quantized_result(Optional[bytes]): The serialized, encrypted and quantized result
Returns:
Union[numpy.ndarray, Tuple[numpy.ndarray, ...]]: The clear float values.
method generate_private_and_evaluation_keys
generate_private_and_evaluation_keys(force=False)
Generate the private and evaluation keys.
Args:
force(bool): if True, regenerate the keys even if they already exist
method get_serialized_evaluation_keys
get_serialized_evaluation_keys(
include_tfhers_key: bool = False
) → Union[bytes, Tuple[bytes, Union[bytes, NoneType]]]
Get the serialized evaluation keys.
Args:
include_tfhers_key(bool): Whether to include the serialized TFHE-rs public key.
Returns:
bytes: the evaluation keys
method get_tfhers_secret_key
get_tfhers_secret_key() → Union[bytes, NoneType]
Get the secret key that decrypts TFHE-rs ciphertexts.
This secret key use used by the client object to encrypt TFHE-rs ciphertexts that are sent to the server, but also to decrypt outputs produced by a server-side computation, that can be a Torus ML model or a TFHE-rs program.
Returns:
bytes: the serialized TFHE-rs secret key
method load
load()
Load the quantizers along with the FHE specs.
method quantize_encrypt_serialize
quantize_encrypt_serialize(
*x: Optional[ndarray]
) → Union[bytes, NoneType, Tuple[Union[bytes, NoneType], ]]
Quantize, encrypt and serialize the values.
Args:
x(Optional[numpy.ndarray]): The values to quantize, encrypt and serialize.
Returns:
Union[bytes, Tuple[bytes, ...]]: The quantized, encrypted and serialized values.