Skip to content

Commit

Permalink
feat(frontend-python): Enable multi output programs
Browse files Browse the repository at this point in the history
  • Loading branch information
BourgerieQuentin committed Nov 13, 2023
1 parent 09af803 commit d224f3e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,6 @@ def compile(
self._evaluate("Compiling", inputset)
assert self.graph is not None

if len(self.graph.output_nodes) > 1:
fmtd_graph = self.graph.format(
highlighted_result=["multiple outputs are not supported"],
show_bounds=False,
)
message = "Function you are trying to compile cannot be compiled\n\n" + fmtd_graph
raise RuntimeError(message)

# in-memory MLIR module
mlir_context = self.compilation_context.mlir_context()
mlir_module = GraphConverter().convert(self.graph, self.configuration, mlir_context)
Expand Down
62 changes: 0 additions & 62 deletions frontends/concrete-python/tests/compilation/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,68 +347,6 @@ def g(x):
)


def test_direct_circuit_multi_output(helpers):
"""
Test `run` method of `Circuit` class with bad parameters.
"""

configuration = helpers.configuration()
with pytest.raises(RuntimeError) as excinfo:

@fhe.circuit({"x": "encrypted", "y": "encrypted"}, configuration)
def f(x: fhe.uint4, y: fhe.uint4):
return x, y

assert (
str(excinfo.value)
== """\
Function you are trying to compile cannot be compiled
%0 = x # EncryptedScalar<uint4>
%1 = y # EncryptedScalar<uint4>
return %0, %1
^^^^^^^^^^^^^ multiple outputs are not supported\
"""
)


def test_circuit_multi_output(helpers):
"""
Test `run` method of `Circuit` class with bad parameters.
"""

configuration = helpers.configuration()

@fhe.compiler({"x": "encrypted", "y": "encrypted"})
def f(x, y):
return x, y

inputset = [(0, 0), (15, 15)]
graph = f.trace(inputset)
assert (
graph.format()
== """\
%0 = x # EncryptedScalar<uint4> ∈ [0, 15]
%1 = y # EncryptedScalar<uint4> ∈ [0, 15]
return %0, %1\
"""
)

with pytest.raises(RuntimeError) as excinfo:
f.compile(inputset, configuration)
assert (
str(excinfo.value)
== """\
Function you are trying to compile cannot be compiled
%0 = x # EncryptedScalar<uint4>
%1 = y # EncryptedScalar<uint4>
return %0, %1
^^^^^^^^^^^^^ multiple outputs are not supported\
"""
)


def test_compiler_compile_with_single_tuple_inputset(helpers):
"""
Test compiling a single argument function with an inputset made of single element tuples.
Expand Down
47 changes: 47 additions & 0 deletions frontends/concrete-python/tests/execution/test_multi_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Tests of execution with multiple outputs.
"""

import pytest

from concrete import fhe


@pytest.mark.parametrize(
"function,parameters",
[
pytest.param(
lambda x: (x * 2, x - 2),
{
"x": {"range": [0, 10], "status": "encrypted"},
},
),
pytest.param(
lambda x, y: (x * 2, y - 2),
{
"x": {"range": [0, 2], "status": "encrypted"},
"y": {"range": [0, 15], "status": "encrypted"},
},
),
pytest.param(
lambda x, y: (x * 2, y - 2),
{
"x": {"range": [0, 2], "shape": (2, 3), "status": "encrypted"},
"y": {"range": [0, 15], "status": "encrypted"},
},
),
],
)
def test_multi_output(function, parameters, helpers):
"""
Test functions with multiple outputs.
"""

parameter_encryption_statuses = helpers.generate_encryption_statuses(parameters)
configuration = helpers.configuration()
compiler = fhe.Compiler(function, parameter_encryption_statuses)
inputset = helpers.generate_inputset(parameters)
circuit = compiler.compile(inputset, configuration)

sample = helpers.generate_sample(parameters)
helpers.check_execution(circuit, function, sample)

0 comments on commit d224f3e

Please sign in to comment.