|
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...") |
|
|
|
|
|
model_onnx = onnx.load(Path.joinpath(script_dir, "sentiment_fhe_model/server_model.onnx")) |
|
|
|
|
|
data = pd.read_csv( |
|
Path.joinpath(script_dir, "sentiment_fhe_model/samples_for_compilation.csv"), index_col=0 |
|
).values |
|
|
|
|
|
_tensor_tree_predict = get_equivalent_numpy_forward(model_onnx) |
|
|
|
model = FHEModelClient( |
|
Path.joinpath(script_dir, "sentiment_fhe_model/deployment"), ".fhe_keys" |
|
).model |
|
|
|
|
|
model._tensor_tree_predict = _tensor_tree_predict |
|
|
|
|
|
model.compile(data) |
|
|
|
|
|
with open( |
|
Path.joinpath(script_dir, "sentiment_fhe_model/deployment/serialized_processing.json"), "r" |
|
) as f: |
|
serialized_processing = json.load(f) |
|
|
|
|
|
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) |
|
|
|
|
|
with open( |
|
Path.joinpath(script_dir, "sentiment_fhe_model/deployment/serialized_processing.json"), "w" |
|
) as f: |
|
json.dump(serialized_processing, f) |
|
|
|
print("Done!") |
|
|