-
Notifications
You must be signed in to change notification settings - Fork 142
/
fhe_client_server.py
374 lines (285 loc) · 13 KB
/
fhe_client_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""APIs for FHE deployment."""
import json
import sys
import zipfile
from pathlib import Path
from typing import Any, Optional
import numpy
from concrete import fhe
from ..common.debugging.custom_assert import assert_true
from ..common.serialization.dumpers import dump
from ..common.serialization.loaders import load
from ..version import __version__ as CML_VERSION
try:
# 3.8 and above
# pylint: disable-next=no-name-in-module
from importlib.metadata import version
except ImportError: # pragma: no cover
# 3.7 and below
# pylint: disable-next=no-name-in-module
from importlib_metadata import version
def check_concrete_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 (Concrete Python, Concrete 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.
"""
with zipfile.ZipFile(zip_path) as zip_file:
with zip_file.open("versions.json", mode="r") as file:
versions = json.load(file)
# Check for package coherence
packages_to_check = {"concrete-python", "concrete-ml"}
errors = []
for package_name, package_version in versions.items():
if package_name not in packages_to_check:
continue
if package_name == "concrete-ml":
current_version = CML_VERSION
else:
current_version = version(package_name)
if package_version != current_version: # pragma: no cover
errors.append((package_name, package_version, current_version))
# Raise an error if at least one package version did not match the one currently installed
if errors: # pragma: no cover
raise ValueError(
"Version mismatch for packages: \n"
+ "\n".join(f"{error[0]}: {error[1]} != {error[2]}" for error in errors)
)
# Raise an error if the Python version do not match the one currently installed
if not versions["python"].startswith(
f"{sys.version_info.major}.{sys.version_info.minor}"
): # pragma: no cover
raise ValueError(
"Not the same Python version between the compiler and the server."
f"{versions['python']} != {sys.version_info.major}.{sys.version_info.minor}"
)
class FHEModelServer:
"""Server API to load and run the FHE circuit."""
server: fhe.Server
def __init__(self, path_dir: str):
"""Initialize the FHE API.
Args:
path_dir (str): the path to the directory where the circuit is saved
"""
self.path_dir = path_dir
# Load the FHE circuit
self.load()
def load(self):
"""Load the circuit."""
server_zip_path = Path(self.path_dir).joinpath("server.zip")
check_concrete_versions(server_zip_path)
self.server = fhe.Server.load(Path(self.path_dir).joinpath("server.zip"))
def run(
self,
serialized_encrypted_quantized_data: bytes,
serialized_evaluation_keys: bytes,
) -> bytes:
"""Run the model on the server over encrypted data.
Args:
serialized_encrypted_quantized_data (bytes): the encrypted, quantized
and serialized data
serialized_evaluation_keys (bytes): the serialized evaluation keys
Returns:
bytes: the result of the model
"""
assert_true(self.server is not None, "Model has not been loaded.")
deserialized_encrypted_quantized_data = fhe.Value.deserialize(
serialized_encrypted_quantized_data
)
deserialized_evaluation_keys = fhe.EvaluationKeys.deserialize(serialized_evaluation_keys)
result = self.server.run(
deserialized_encrypted_quantized_data, evaluation_keys=deserialized_evaluation_keys
)
serialized_result = result.serialize()
return serialized_result
class FHEModelDev:
"""Dev API to save the model and then load and run the FHE circuit."""
model: Any = None
def __init__(self, path_dir: str, model: Any = None):
"""Initialize the FHE API.
Args:
path_dir (str): the path to the directory where the circuit is saved
model (Any): the model to use for the FHE API
"""
self.path_dir = path_dir
self.model = model
Path(self.path_dir).mkdir(parents=True, exist_ok=True)
def _export_model_to_json(self) -> Path:
"""Export the quantizers to a json file.
Returns:
Path: the path to the json file
"""
serialized_processing = {
"model_type": self.model.__class__,
"model_post_processing_params": self.model.post_processing_params,
"input_quantizers": self.model.input_quantizers,
"output_quantizers": self.model.output_quantizers,
}
# Export the `is_fitted` attribute for built-in models
if hasattr(self.model, "is_fitted"):
serialized_processing["is_fitted"] = self.model.is_fitted
# Dump json
json_path = Path(self.path_dir).joinpath("serialized_processing.json")
with open(json_path, "w", encoding="utf-8") as file:
dump(serialized_processing, file)
return json_path
def save(self, via_mlir: bool = False):
"""Export all needed artifacts for the client and server.
Arguments:
via_mlir (bool): serialize with `via_mlir` option from Concrete-Python.
For more details on the topic please refer to Concrete-Python's documentation.
Raises:
Exception: path_dir is not empty
"""
# Check if the path_dir is empty with pathlib
listdir = list(Path(self.path_dir).glob("**/*"))
if len(listdir) > 0:
raise Exception(
f"path_dir: {self.path_dir} is not empty."
"Please delete it before saving a new model."
)
self.model.check_model_is_compiled()
# Model must be compiled with jit=False
# In a jit model, everything is in memory so it is not serializable.
assert_true(
not self.model.fhe_circuit.configuration.jit,
"The model must be compiled with the configuration option jit=False.",
)
# Export the quantizers
json_path = self._export_model_to_json()
# First save the circuit for the server
path_circuit_server = Path(self.path_dir).joinpath("server.zip")
self.model.fhe_circuit.server.save(path_circuit_server, via_mlir=via_mlir)
# Save the circuit for the client
path_circuit_client = Path(self.path_dir).joinpath("client.zip")
self.model.fhe_circuit.client.save(path_circuit_client)
with zipfile.ZipFile(path_circuit_client, "a") as zip_file:
zip_file.write(filename=json_path, arcname="serialized_processing.json")
# Add versions
versions_path = Path(self.path_dir).joinpath("versions.json")
versions = {
"concrete-python": version("concrete-python"),
"concrete-ml": CML_VERSION,
"python": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
}
with open(versions_path, "w", encoding="utf-8") as file:
json.dump(fp=file, obj=versions)
with zipfile.ZipFile(path_circuit_server, "a") as zip_file:
zip_file.write(filename=versions_path, arcname="versions.json")
with zipfile.ZipFile(path_circuit_client, "a") as zip_file:
zip_file.write(filename=versions_path, arcname="versions.json")
json_path.unlink()
class FHEModelClient:
"""Client API to encrypt and decrypt FHE data."""
client: fhe.Client
def __init__(self, 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 saved
key_dir (str): the path to the directory where the keys are stored
"""
self.path_dir = path_dir
self.key_dir = key_dir
# If path_dir does not exist raise
assert_true(
Path(path_dir).exists(), f"{path_dir} does not exist. Please specify a valid path."
)
# Load
self.load()
def load(self): # pylint: disable=no-value-for-parameter
"""Load the quantizers along with the FHE specs."""
client_zip_path = Path(self.path_dir).joinpath("client.zip")
self.client = fhe.Client.load(client_zip_path, self.key_dir)
# Load the quantizers
with zipfile.ZipFile(client_zip_path) as client_zip:
with client_zip.open("serialized_processing.json", mode="r") as file:
serialized_processing = load(file)
# Load and check versions
check_concrete_versions(client_zip_path)
# Initialize the model
self.model = serialized_processing["model_type"]()
self.model.input_quantizers = serialized_processing["input_quantizers"]
self.model.output_quantizers = serialized_processing["output_quantizers"]
# Load the `_is_fitted` private attribute for built-in models
if "is_fitted" in serialized_processing:
# This private access should be temporary as the Client-Server interface could benefit
# from built-in serialization load/dump methods
# FIXME: https://github.com/zama-ai/concrete-ml-internal/issues/3243
# pylint: disable-next=protected-access
self.model._is_fitted = serialized_processing["is_fitted"]
# Load model parameters
# Add some checks on post-processing-params
# FIXME: https://github.com/zama-ai/concrete-ml-internal/issues/3131
self.model.post_processing_params = serialized_processing["model_post_processing_params"]
def generate_private_and_evaluation_keys(self, force=False):
"""Generate the private and evaluation keys.
Args:
force (bool): if True, regenerate the keys even if they already exist
"""
self.client.keygen(force)
def get_serialized_evaluation_keys(self) -> bytes:
"""Get the serialized evaluation keys.
Returns:
bytes: the evaluation keys
"""
return self.client.evaluation_keys.serialize()
def quantize_encrypt_serialize(self, x: numpy.ndarray) -> bytes:
"""Quantize, encrypt and serialize the values.
Args:
x (numpy.ndarray): the values to quantize, encrypt and serialize
Returns:
bytes: the quantized, encrypted and serialized values
"""
# Quantize the values
quantized_x = self.model.quantize_input(x)
# Encrypt the values
enc_qx = self.client.encrypt(quantized_x)
# Serialize the encrypted values to be sent to the server
serialized_enc_qx = enc_qx.serialize()
return serialized_enc_qx
def deserialize_decrypt(self, serialized_encrypted_quantized_result: bytes) -> numpy.ndarray:
"""Deserialize and decrypt the values.
Args:
serialized_encrypted_quantized_result (bytes): the serialized, encrypted
and quantized result
Returns:
numpy.ndarray: the decrypted and deserialized values
"""
# Deserialize the encrypted values
deserialized_encrypted_quantized_result = fhe.Value.deserialize(
serialized_encrypted_quantized_result
)
# Decrypt the values
deserialized_decrypted_quantized_result = self.client.decrypt(
deserialized_encrypted_quantized_result
)
assert isinstance(deserialized_decrypted_quantized_result, numpy.ndarray)
return deserialized_decrypted_quantized_result
def deserialize_decrypt_dequantize(
self, serialized_encrypted_quantized_result: bytes
) -> numpy.ndarray:
"""Deserialize, decrypt and de-quantize the values.
Args:
serialized_encrypted_quantized_result (bytes): the serialized, encrypted
and quantized result
Returns:
numpy.ndarray: the decrypted (de-quantized) values
"""
# Decrypt and deserialize the values
deserialized_decrypted_quantized_result = self.deserialize_decrypt(
serialized_encrypted_quantized_result
)
# De-quantize the values
deserialized_decrypted_dequantized_result = self.model.dequantize_output(
deserialized_decrypted_quantized_result
)
# Apply post-processing the to de-quantized values
deserialized_decrypted_dequantized_result = self.model.post_processing(
deserialized_decrypted_dequantized_result
)
return deserialized_decrypted_dequantized_result