Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend-python): enable multi precision and multi parameters by default #483

Merged
merged 3 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,21 @@ mlir::LogicalResult createGroupedConv2D(
return mlir::success();
}

bool isZeroConstant(mlir::Value value) {
auto cst =
mlir::dyn_cast_or_null<mlir::arith::ConstantOp>(value.getDefiningOp());
if (cst == nullptr)
return false;
auto values = cst->getAttrOfType<mlir::DenseIntElementsAttr>("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
Expand Down Expand Up @@ -1767,14 +1782,13 @@ struct FHELinalgConv2dToLinalgConv2d
.cast<mlir::RankedTensorType>()
.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<mlir::RankedTensorType>().getRank();
Expand Down
Original file line number Diff line number Diff line change
@@ -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>>
}
4 changes: 2 additions & 2 deletions docs/howto/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorial/multi_parameters.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 1 addition & 5 deletions docs/tutorial/multi_precision.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 5 additions & 2 deletions frontends/concrete-python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down