From d7b5f15c68cc4efb4a6aed5b0c5ff6a3e76a21b6 Mon Sep 17 00:00:00 2001 From: Umut Date: Tue, 27 Jun 2023 17:07:32 +0200 Subject: [PATCH 1/3] feat(frontend-python): enable multi precision and multi parameters by default --- frontends/concrete-python/Makefile | 7 +++++-- .../concrete/fhe/compilation/configuration.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/frontends/concrete-python/Makefile b/frontends/concrete-python/Makefile index 12b026c7b8..121b6a8956 100644 --- a/frontends/concrete-python/Makefile +++ b/frontends/concrete-python/Makefile @@ -30,14 +30,17 @@ pytest: export LD_PRELOAD=$(RUNTIME_LIBRARY) export PYTHONPATH=$(BINDINGS_DIRECTORY) - # test single precision + # test single precision, mono params pytest tests -svv -n auto \ + --precision=single \ + --strategy=mono \ --key-cache "${KEY_CACHE_DIRECTORY}" \ -m "${PYTEST_MARKERS}" - # test multi precision + # test multi precision, multi params pytest tests -svv -n auto \ --precision=multi \ + --strategy=multi \ --cov=concrete \ --cov-fail-under=100 \ --cov-report=term-missing:skip-covered \ diff --git a/frontends/concrete-python/concrete/fhe/compilation/configuration.py b/frontends/concrete-python/concrete/fhe/compilation/configuration.py index 0eda0fb4d6..7844d70cb6 100644 --- a/frontends/concrete-python/concrete/fhe/compilation/configuration.py +++ b/frontends/concrete-python/concrete/fhe/compilation/configuration.py @@ -90,10 +90,10 @@ def __init__( p_error: Optional[float] = None, global_p_error: Optional[float] = None, auto_adjust_rounders: bool = False, - single_precision: bool = True, + single_precision: bool = False, parameter_selection_strategy: Union[ ParameterSelectionStrategy, str - ] = ParameterSelectionStrategy.MONO, + ] = ParameterSelectionStrategy.MULTI, show_progress: bool = False, progress_title: str = "", progress_tag: Union[bool, int] = False, From 9bdd74dc646f42f793163e13a0c2abc93aca8e7a Mon Sep 17 00:00:00 2001 From: Umut Date: Thu, 3 Aug 2023 10:07:37 +0200 Subject: [PATCH 2/3] docs(frontend-python): update for multi precision and multi parameters by default --- docs/howto/configure.md | 4 ++-- docs/tutorial/multi_parameters.md | 6 +++--- docs/tutorial/multi_precision.md | 6 +----- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/docs/howto/configure.md b/docs/howto/configure.md index af02f3b753..b1803d762a 100644 --- a/docs/howto/configure.md +++ b/docs/howto/configure.md @@ -70,9 +70,9 @@ Additional kwargs to `compile` functions take higher precedence. So if you set t * Error probability for individual table lookups. If set, all table lookups will have the probability of a non-exact result smaller than the set value. See [Exactness](../getting-started/exactness.md) to learn more. * **global\_p\_error**: Optional\[float] = None * Global error probability for the whole circuit. If set, the whole circuit will have the probability of a non-exact result smaller than the set value. See [Exactness](../getting-started/exactness.md) to learn more. -* **single\_precision**: bool = True +* **single\_precision**: bool = False * Use single precision for the whole circuit. -* **parameter\_selection\_strategy**: (fhe.ParameterSelectionStrategy) = fhe.ParameterSelectionStrategy.MONO +* **parameter\_selection\_strategy**: (fhe.ParameterSelectionStrategy) = fhe.ParameterSelectionStrategy.MULTI * Set how cryptographic parameters are selected. * **jit**: bool = False * Enable JIT compilation. diff --git a/docs/tutorial/multi_parameters.md b/docs/tutorial/multi_parameters.md index adacbb2bba..9526063e48 100644 --- a/docs/tutorial/multi_parameters.md +++ b/docs/tutorial/multi_parameters.md @@ -1,11 +1,11 @@ # Multi Parameters -Integers in Concrete are encrypted and processed according to a set of cryptographic parameters. By default, only a single set of such parameters are selected by Concrete Optimizer. This is not the best approach for every use case so multi parameters are introduced. +Integers in Concrete are encrypted and processed according to a set of cryptographic parameters. By default, multiple of such parameters are selected by Concrete Optimizer. This might not be the best approach for every use case and there is the option to use mono parameters. -When they are enabled, a different set of parameters are selected for each bit-width in the circuit, which results in: +When multi parameters are enabled, a different set of parameters are selected for each bit-width in the circuit, which results in: - Faster execution (generally). - Slower key generation. - Larger keys. - Larger memory usage during execution. -To enable them, you can use `parameter_selection_strategy=fhe.ParameterSelectionStrategy.MULTI` configuration option. +To disable it, you can use `parameter_selection_strategy=fhe.ParameterSelectionStrategy.MONO` configuration option. diff --git a/docs/tutorial/multi_precision.md b/docs/tutorial/multi_precision.md index 8960f358b9..55b0b64458 100644 --- a/docs/tutorial/multi_precision.md +++ b/docs/tutorial/multi_precision.md @@ -70,8 +70,4 @@ return %4 In this case, we kept `x` as 2-bits, but set the table lookup result and `y` to be 6-bits, so that the addition can be performed. -This style of bit-width assignment is called multi-precision and it is currently disabled by default. To enable it, you can use the `single_precision=False` configuration option. - -{% hint style="info" %} -Multi precision will become the default option in the near future. -{% endhint %} +This style of bit-width assignment is called multi-precision, and it is enabled by default. To disable it and use a single precision across the circuit, you can use the `single_precision=True` configuration option. From c34b6d74b0d640b074ed3f4fa4ae5952378041df Mon Sep 17 00:00:00 2001 From: Bourgerie Quentin Date: Thu, 3 Aug 2023 17:41:55 +0200 Subject: [PATCH 3/3] fix(compiler): Fix conv2d with bias equals to zero in multi parameters The zero bias was folded and lead to empty loops, i.e. loops with copy only and make the TFHE parametrization fail --- .../TensorOpsToLinalg.cpp | 22 +++++++++++++++---- .../check_tests/BugReport/bug_report_483.mlir | 7 ++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 compilers/concrete-compiler/compiler/tests/check_tests/BugReport/bug_report_483.mlir diff --git a/compilers/concrete-compiler/compiler/lib/Conversion/FHETensorOpsToLinalg/TensorOpsToLinalg.cpp b/compilers/concrete-compiler/compiler/lib/Conversion/FHETensorOpsToLinalg/TensorOpsToLinalg.cpp index 7b44826ca3..09e142292e 100644 --- a/compilers/concrete-compiler/compiler/lib/Conversion/FHETensorOpsToLinalg/TensorOpsToLinalg.cpp +++ b/compilers/concrete-compiler/compiler/lib/Conversion/FHETensorOpsToLinalg/TensorOpsToLinalg.cpp @@ -1678,6 +1678,21 @@ mlir::LogicalResult createGroupedConv2D( return mlir::success(); } +bool isZeroConstant(mlir::Value value) { + auto cst = + mlir::dyn_cast_or_null(value.getDefiningOp()); + if (cst == nullptr) + return false; + auto values = cst->getAttrOfType("value"); + if (values == nullptr) + return false; + for (auto v : values) { + if (v != 0) + return false; + } + return true; +} + /// This rewrite pattern transforms any instance of operators /// `FHELinalg.conv2d` to one or multiple instances of /// `linalg.conv_2d_nchw_fchw`. The transformation consists of padding the input @@ -1767,14 +1782,13 @@ struct FHELinalgConv2dToLinalgConv2d .cast() .getShape(), inputElementType)); + forwardOptimizerID(conv2dOp, initTensor.getDefiningOp()); // Since linalg doesn't support a bias in the conv operation, we // initialize the output tensor to the bias values, so that conv results // get accumulated to it mlir::Value bias = conv2dOp.getBias(); /* optional of shape: Filters */ - mlir::Value biasInitTensor; - if (!bias) { // no bias was used - biasInitTensor = initTensor; - } else { + mlir::Value biasInitTensor = initTensor; + if (bias && !isZeroConstant(bias)) { // Fill the output tensor with bias values auto resultRank = initTensor.getType().cast().getRank(); diff --git a/compilers/concrete-compiler/compiler/tests/check_tests/BugReport/bug_report_483.mlir b/compilers/concrete-compiler/compiler/tests/check_tests/BugReport/bug_report_483.mlir new file mode 100644 index 0000000000..af0de1eaf8 --- /dev/null +++ b/compilers/concrete-compiler/compiler/tests/check_tests/BugReport/bug_report_483.mlir @@ -0,0 +1,7 @@ +// RUN: concretecompiler --action=dump-llvm-ir --optimizer-strategy=dag-multi %s +func.func @main(%arg0: tensor<1x1x4x4x!FHE.eint<4>>) -> tensor<1x1x2x2x!FHE.eint<4>> { + %cst = arith.constant dense<[[[[2, 0], [3, 1]]]]> : tensor<1x1x2x2xi5> + %cst_0 = arith.constant dense<0> : tensor<1xi5> + %0 = "FHELinalg.conv2d"(%arg0, %cst, %cst_0) {dilations = dense<1> : tensor<2xi64>, group = 1 : i64, padding = dense<0> : tensor<4xi64>, strides = dense<2> : tensor<2xi64>} : (tensor<1x1x4x4x!FHE.eint<4>>, tensor<1x1x2x2xi5>, tensor<1xi5>) -> tensor<1x1x2x2x!FHE.eint<4>> + return %0 : tensor<1x1x2x2x!FHE.eint<4>> +}