18 from typing
import Tuple, Optional, Union
19 from pathlib
import Path
25 from onnxruntime
import InferenceSession
30 from .._folder_manager
import folder_manager
33 async
def genWitness(inputPath: Path, circuit: Path, witnessPath: Path) ->
None:
34 await ezkl.gen_witness(inputPath, circuit, witnessPath)
37 async
def getSrs(settings: Path) ->
None:
38 await ezkl.get_srs(settings)
44 compiledModelPath: Optional[Path] =
None,
45 proveKey: Optional[Path] =
None,
46 settingsPath: Optional[Path] =
None,
47 ) -> Union[np.ndarray, Tuple[np.ndarray, Path]]:
50 Performs inference on the provided onnx model with the provided data and also generates
51 a zero knowledge proof if a compiled model and key are passed.
52 This can be used to verify that the result was gained by
53 combining this specific model and input data.
58 data which will be directly fed to the model
60 path to the onnx model
62 path to the settigs.json file
63 compiledModelPath : Optional[Path]
64 path to the compiled model
65 proveKey : Optional[Path]
66 path to the proving key file of the model
70 Union[np.ndarray, Tuple[np.ndarray, Path]]
71 output of the model or, if compiledModelPath and proveKey are passed, output of the model and path to the proof
74 inferenceId = str(uuid.uuid1())
76 session = InferenceSession(onnxPath)
77 inputName = session.get_inputs()[0].name
78 result = np.array(session.run(
None, {inputName: data}))
80 if compiledModelPath
is None and proveKey
is None and settingsPath
is None:
83 if compiledModelPath
is None or proveKey
is None or settingsPath
is None:
84 raise ValueError(f
">> [Coretex] Parameters compiledModelPath, proveKey and settingsPath have to either all be passed (for verified inference) or none of them (for regular inference)")
86 inferenceDir = folder_manager.createTempFolder(inferenceId)
87 witnessPath = inferenceDir /
"witness.json"
88 inputPath = inferenceDir /
"input.json"
89 proofPath = inferenceDir /
"proof.pf"
91 flattenedData = np.array(data).reshape(-1).tolist()
92 inputData = dict(input_data = [flattenedData])
93 with inputPath.open(
"w")
as file:
94 json.dump(inputData, file)
96 asyncio.run(genWitness(inputPath, compiledModelPath, witnessPath))
97 asyncio.run(getSrs(settingsPath))
106 return result, proofPath