Upload 6 files
Browse files- compiled_model/client.zip +3 -0
- compiled_model/server.zip +3 -0
- compiled_model/versions.json +1 -0
- create_zipfiles_and_check_local_endpoint.py +85 -0
- handler.py +2 -2
compiled_model/client.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bf41c99b06817bd28d64c681a4294ca4e910e95c0b07837374420643bcec50f7
|
3 |
+
size 7496
|
compiled_model/server.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:20941390f0eb4c8ac177344a9e5dbdafc93d99c01d16c6ebdb2b3d278fecc36b
|
3 |
+
size 1258
|
compiled_model/versions.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"concrete-python": "2.5.0rc1", "concrete-ml": "1.3.0", "python": "3.9.15"}
|
create_zipfiles_and_check_local_endpoint.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from handler import EndpointHandler
|
2 |
+
import numpy as np
|
3 |
+
import shutil
|
4 |
+
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
from sklearn.datasets import make_classification
|
8 |
+
from sklearn.model_selection import train_test_split
|
9 |
+
|
10 |
+
from concrete.ml.sklearn import LogisticRegression
|
11 |
+
from concrete.ml.deployment import FHEModelClient, FHEModelDev
|
12 |
+
|
13 |
+
|
14 |
+
def to_json(python_object):
|
15 |
+
if isinstance(python_object, bytes):
|
16 |
+
return {"__class__": "bytes", "__value__": list(python_object)}
|
17 |
+
raise TypeError(repr(python_object) + " is not JSON serializable")
|
18 |
+
|
19 |
+
|
20 |
+
def from_json(python_object):
|
21 |
+
if "__class__" in python_object:
|
22 |
+
return bytes(python_object["__value__"])
|
23 |
+
|
24 |
+
|
25 |
+
# Fit a model. In the future, we should find an existing model on HF repository
|
26 |
+
path_to_model = Path("compiled_model")
|
27 |
+
do_training_and_compilation = True
|
28 |
+
|
29 |
+
x, y = make_classification(n_samples=1000, class_sep=2, n_features=30, random_state=42)
|
30 |
+
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
|
31 |
+
|
32 |
+
if do_training_and_compilation:
|
33 |
+
model_dev = LogisticRegression()
|
34 |
+
model_dev.fit(X_train, y_train)
|
35 |
+
|
36 |
+
# Compile into FHE
|
37 |
+
model_dev.compile(X_train)
|
38 |
+
|
39 |
+
# Saving the model
|
40 |
+
shutil.rmtree(path_to_model, ignore_errors=True)
|
41 |
+
fhemodel_dev = FHEModelDev(path_to_model, model_dev)
|
42 |
+
fhemodel_dev.save(via_mlir=True)
|
43 |
+
|
44 |
+
# Init the handler (compilation of the model is done on HF side)
|
45 |
+
my_handler = EndpointHandler(path=".")
|
46 |
+
|
47 |
+
# Recover parameters for client side
|
48 |
+
fhemodel_client = FHEModelClient(path_to_model)
|
49 |
+
|
50 |
+
# Generate the keys
|
51 |
+
fhemodel_client.generate_private_and_evaluation_keys()
|
52 |
+
evaluation_keys = fhemodel_client.get_serialized_evaluation_keys()
|
53 |
+
|
54 |
+
# Test the handler
|
55 |
+
nb_good = 0
|
56 |
+
nb_samples = len(X_test)
|
57 |
+
verbose = False
|
58 |
+
|
59 |
+
for i in range(nb_samples):
|
60 |
+
|
61 |
+
# Quantize the input and encrypt it
|
62 |
+
encrypted_inputs = fhemodel_client.quantize_encrypt_serialize([X_test[i]])
|
63 |
+
|
64 |
+
# Prepare the payload, including the evaluation keys which are needed server side
|
65 |
+
payload = {
|
66 |
+
"inputs": "fake",
|
67 |
+
"encrypted_inputs": to_json(encrypted_inputs),
|
68 |
+
"evaluation_keys": to_json(evaluation_keys),
|
69 |
+
}
|
70 |
+
|
71 |
+
# Run the inference on HF servers
|
72 |
+
encrypted_prediction = my_handler(payload)
|
73 |
+
encrypted_prediction = from_json(encrypted_prediction)
|
74 |
+
|
75 |
+
# Decrypt the result and dequantize
|
76 |
+
prediction_proba = fhemodel_client.deserialize_decrypt_dequantize(encrypted_prediction)[0]
|
77 |
+
prediction = np.argmax(prediction_proba)
|
78 |
+
|
79 |
+
if verbose:
|
80 |
+
print(f"for i-th input, {prediction=} with expected {y_test[i]}")
|
81 |
+
|
82 |
+
# Measure accuracy
|
83 |
+
nb_good += y_test[i] == prediction
|
84 |
+
|
85 |
+
print(f"Accuracy on {nb_samples} samples is {nb_good * 1. / nb_samples}")
|
handler.py
CHANGED
@@ -17,10 +17,10 @@ def to_json(python_object):
|
|
17 |
|
18 |
|
19 |
class EndpointHandler:
|
20 |
-
def __init__(self, path="
|
21 |
|
22 |
# For server
|
23 |
-
self.fhemodel_server = FHEModelServer(path)
|
24 |
|
25 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
26 |
"""
|
|
|
17 |
|
18 |
|
19 |
class EndpointHandler:
|
20 |
+
def __init__(self, path=""):
|
21 |
|
22 |
# For server
|
23 |
+
self.fhemodel_server = FHEModelServer(path + "/compiled_model")
|
24 |
|
25 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
26 |
"""
|