File size: 1,724 Bytes
2dcbc14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fee1bf4
2dcbc14
 
 
 
 
 
 
 
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
import onnx
import pandas as pd
from concrete.ml.deployment import FHEModelDev, FHEModelClient
from concrete.ml.onnx.convert import get_equivalent_numpy_forward
import json
import os
import shutil
from pathlib import Path


script_dir = Path(__file__).parent

print("Compiling the model...")

# Load the onnx model
model_onnx = onnx.load(Path.joinpath(script_dir, "sentiment_fhe_model/server_model.onnx"))

# Load the data from the csv file to be used for compilation
data = pd.read_csv(
    Path.joinpath(script_dir, "sentiment_fhe_model/samples_for_compilation.csv"), index_col=0
).values

# Convert the onnx model to a numpy model
_tensor_tree_predict = get_equivalent_numpy_forward(model_onnx)

model = FHEModelClient(
    Path.joinpath(script_dir, "sentiment_fhe_model/deployment"), ".fhe_keys"
).model

# Assign the numpy model and compile the model
model._tensor_tree_predict = _tensor_tree_predict

# Compile the model
model.compile(data)

# Load the serialized_processing.json file
with open(
    Path.joinpath(script_dir, "sentiment_fhe_model/deployment/serialized_processing.json"), "r"
) as f:
    serialized_processing = json.load(f)

# Delete the deployment folder if it exist
if Path.joinpath(script_dir, "sentiment_fhe_model/deployment").exists():
    shutil.rmtree(Path.joinpath(script_dir, "sentiment_fhe_model/deployment"))

fhe_api = FHEModelDev(
    model=model, path_dir=Path.joinpath(script_dir, "sentiment_fhe_model/deployment")
)
fhe_api.save(via_mlir=True)

# Write the serialized_processing.json file to the deployment folder
with open(
    Path.joinpath(script_dir, "sentiment_fhe_model/deployment/serialized_processing.json"), "w"
) as f:
    json.dump(serialized_processing, f)

print("Done!")