Skip to content

Commit

Permalink
fix(compiler): levelledOP conversion to dot like for optimizer
Browse files Browse the repository at this point in the history
note: this cannot be done as before in the optimizer since the latter had more information.
here we assumed each input contribute equally to the resulting noise.

A better fix could be to provide the linear relation of inputs instead of manp and smanp.
  • Loading branch information
rudy-6-4 authored and BourgerieQuentin committed Aug 1, 2024
1 parent 9b034d9 commit 3adf658
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ jobs:
# Concrete-ML tests #############################
concrete-ml-tests-linux:
needs: file-change
if: needs.file-change.outputs.concrete-python == 'true' || needs.file-change.outputs.push-main
if: needs.file-change.outputs.concrete-python == 'true' || needs.file-change.outputs.compiler == 'true' || needs.file-change.outputs.push-main
uses: ./.github/workflows/start_slab.yml
secrets: inherit
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,35 +321,34 @@ struct FunctionToDag {
auto smanp_int = op.getAttrOfType<mlir::IntegerAttr>("SMANP");
auto loc = loc_to_string(op.getLoc());
assert(smanp_int && "Missing manp value on a crypto operation");
// TODO: use APIFloat.sqrt when it's available
double manp = sqrt(smanp_int.getValue().roundToDouble());
auto comment = std::string(op.getName().getStringRef()) + " " + loc;

double maxInputManp = 0.;
size_t n_inputs = 0;
double sq_sum = 0;
for (auto input : op.getOperands()) {
if (!fhe::utils::isEncryptedValue(input)) {
continue;
}
n_inputs += 1;
if (input.isa<mlir::BlockArgument>()) {
maxInputManp = fmax(1., maxInputManp);
sq_sum += 1.0;
} else {
auto inpSmanpInt =
input.getDefiningOp()->getAttrOfType<mlir::IntegerAttr>("SMANP");
const double inpManp = sqrt(inpSmanpInt.getValue().roundToDouble());
maxInputManp = fmax(inpManp, maxInputManp);
const double inpSManp = inpSmanpInt.getValue().roundToDouble();
sq_sum += inpSManp;
}
}
assert(inputs.size() == n_inputs);
double weight;
if (maxInputManp == 0) {
if (sq_sum == 0) {
// The max input manp is zero, meaning the inputs are all zero tensors
// with no noise. In this case it does not matter the weight since it will
// multiply zero.
weight = 0.;
weight = 1.;
} else {
weight = manp / maxInputManp;
double smanp_dbl = smanp_int.getValue().roundToDouble();
weight = std::max(sqrt(smanp_dbl / sq_sum), 1.0);
assert(!std::isnan(weight));
}
auto weights = std::vector<double>(n_inputs, weight);
Expand Down

0 comments on commit 3adf658

Please sign in to comment.