From 593abc6d9a85d8efdd8c3f5312f3459ec1515383 Mon Sep 17 00:00:00 2001 From: Umut Date: Mon, 12 Sep 2022 15:49:54 +0200 Subject: [PATCH] feat: add various properties about the circuit --- concrete/numpy/compilation/circuit.py | 42 +++++++++++++++++++++++++ concrete/numpy/compilation/server.py | 45 +++++++++++++++++++++++++++ tests/compilation/test_circuit.py | 22 +++++++++++++ 3 files changed, 109 insertions(+) diff --git a/concrete/numpy/compilation/circuit.py b/concrete/numpy/compilation/circuit.py index 700c2efc5..6adb526e1 100644 --- a/concrete/numpy/compilation/circuit.py +++ b/concrete/numpy/compilation/circuit.py @@ -161,3 +161,45 @@ class Circuit: """ self.server.cleanup() + + @property + def complexity(self) -> float: + """ + Get complexity of the circuit. + """ + return self.server.complexity + + @property + def size_of_secret_keys(self) -> int: + """ + Get size of the secret keys of the circuit. + """ + return self.server.size_of_secret_keys + + @property + def size_of_bootstrap_keys(self) -> int: + """ + Get size of the bootstrap keys of the circuit. + """ + return self.server.size_of_bootstrap_keys + + @property + def size_of_keyswitch_keys(self) -> int: + """ + Get size of the key switch keys of the circuit. + """ + return self.server.size_of_keyswitch_keys + + @property + def size_of_inputs(self) -> int: + """ + Get size of the inputs of the circuit. + """ + return self.server.size_of_inputs + + @property + def size_of_outputs(self) -> int: + """ + Get size of the outputs of the circuit. + """ + return self.server.size_of_outputs diff --git a/concrete/numpy/compilation/server.py b/concrete/numpy/compilation/server.py index fdb282727..f35d47174 100644 --- a/concrete/numpy/compilation/server.py +++ b/concrete/numpy/compilation/server.py @@ -9,6 +9,7 @@ from pathlib import Path from typing import List, Optional, Union from concrete.compiler import ( + CompilationFeedback, CompilationOptions, EvaluationKeys, JITCompilationResult, @@ -36,6 +37,7 @@ class Server: _output_dir: Optional[tempfile.TemporaryDirectory] _support: Union[JITSupport, LibrarySupport] _compilation_result: Union[JITCompilationResult, LibraryCompilationResult] + _compilation_feedback: CompilationFeedback _server_lambda: Union[JITLambda, LibraryLambda] _mlir: Optional[str] @@ -54,6 +56,7 @@ class Server: self._output_dir = output_dir self._support = support self._compilation_result = compilation_result + self._compilation_feedback = self._support.load_compilation_feedback(compilation_result) self._server_lambda = server_lambda self._mlir = None @@ -252,3 +255,45 @@ class Server: if self._output_dir is not None: self._output_dir.cleanup() + + @property + def complexity(self) -> float: + """ + Get complexity of the compiled program. + """ + return self._compilation_feedback.complexity + + @property + def size_of_secret_keys(self) -> int: + """ + Get size of the secret keys of the compiled program. + """ + return self._compilation_feedback.total_secret_keys_size + + @property + def size_of_bootstrap_keys(self) -> int: + """ + Get size of the bootstrap keys of the compiled program. + """ + return self._compilation_feedback.total_bootstrap_keys_size + + @property + def size_of_keyswitch_keys(self) -> int: + """ + Get size of the key switch keys of the compiled program. + """ + return self._compilation_feedback.total_keyswitch_keys_size + + @property + def size_of_inputs(self) -> int: + """ + Get size of the inputs of the compiled program. + """ + return self._compilation_feedback.total_inputs_size + + @property + def size_of_outputs(self) -> int: + """ + Get size of the outputs of the compiled program. + """ + return self._compilation_feedback.total_output_size diff --git a/tests/compilation/test_circuit.py b/tests/compilation/test_circuit.py index 7f3766a9d..19f3c5d2a 100644 --- a/tests/compilation/test_circuit.py +++ b/tests/compilation/test_circuit.py @@ -38,6 +38,28 @@ return %2 ) +def test_circuit_feedback(helpers): + """ + Test feedback properties of `Circuit` class. + """ + + configuration = helpers.configuration() + + @compiler({"x": "encrypted", "y": "encrypted"}) + def f(x, y): + return x + y + + inputset = [(np.random.randint(0, 2**4), np.random.randint(0, 2**5)) for _ in range(100)] + circuit = f.compile(inputset, configuration) + + assert isinstance(circuit.complexity, float) + assert isinstance(circuit.size_of_secret_keys, int) + assert isinstance(circuit.size_of_bootstrap_keys, int) + assert isinstance(circuit.size_of_keyswitch_keys, int) + assert isinstance(circuit.size_of_inputs, int) + assert isinstance(circuit.size_of_outputs, int) + + def test_circuit_bad_run(helpers): """ Test `run` method of `Circuit` class with bad parameters.