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: Multi precision multiplication #535

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -283,8 +283,9 @@ struct FHELinalgOpToLinalgGeneric : public mlir::OpRewritePattern<FHELinalgOp> {
auto bodyBuilder = [&](mlir::OpBuilder &nestedBuilder,
mlir::Location nestedLoc,
mlir::ValueRange blockArgs) {
FHEOp fheOp = nestedBuilder.create<FHEOp>(linalgOp.getLoc(), blockArgs[0],
blockArgs[1]);
FHEOp fheOp = nestedBuilder.create<FHEOp>(linalgOp.getLoc(),
resultTy.getElementType(),
blockArgs[0], blockArgs[1]);
forwardOptimizerID(linalgOp, fheOp);

nestedBuilder.create<mlir::linalg::YieldOp>(linalgOp.getLoc(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,10 @@ mlir::LogicalResult MulEintIntOp::verify() {
mlir::LogicalResult MulEintOp::verify() {
auto a = this->getRhs().getType().dyn_cast<FheIntegerInterface>();
auto b = this->getLhs().getType().dyn_cast<FheIntegerInterface>();
auto out = this->getResult().getType().dyn_cast<FheIntegerInterface>();

if (!verifyEncryptedIntegerInputsConsistency(*this->getOperation(), a, b)) {
return ::mlir::failure();
}
if (!verifyEncryptedIntegerInputAndResultConsistency(*this->getOperation(), a,
out)) {
return ::mlir::failure();
}

return ::mlir::success();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class EncryptedMulOpPattern : public mlir::OpConversionPattern<FHE::MulEintOp> {
// addMul function on ConcreteOptimizer.cpp

auto inputType = adaptor.getRhs().getType();
auto outputType = op->getResult(0).getType();

auto bitWidth = inputType.cast<FHE::FheIntegerInterface>().getWidth();
auto isSigned = inputType.cast<FHE::FheIntegerInterface>().isSigned();
mlir::Type signedType =
Expand Down Expand Up @@ -76,7 +78,7 @@ class EncryptedMulOpPattern : public mlir::OpConversionPattern<FHE::MulEintOp> {
rawSumLut.size(), rewriter.getIntegerType(64)),
rawSumLut));
auto sumTluOutput = rewriter.create<FHE::ApplyLookupTableEintOp>(
op->getLoc(), inputType, sum, sumLut);
op->getLoc(), outputType, sum, sumLut);
if (operatorIndexes != nullptr) {
std::vector<int32_t> sumTluIndexes{operatorIndexes[1]};
if (isSigned) {
Expand Down Expand Up @@ -111,7 +113,7 @@ class EncryptedMulOpPattern : public mlir::OpConversionPattern<FHE::MulEintOp> {
rawDiffLut.size(), rewriter.getIntegerType(64)),
rawDiffLut));
auto diffTluOutput = rewriter.create<FHE::ApplyLookupTableEintOp>(
op->getLoc(), inputType, diff, diffLut);
op->getLoc(), outputType, diff, diffLut);
if (operatorIndexes != nullptr) {
std::vector<int32_t> diffTluIndexes{operatorIndexes[3],
operatorIndexes[4]};
Expand All @@ -120,7 +122,7 @@ class EncryptedMulOpPattern : public mlir::OpConversionPattern<FHE::MulEintOp> {
}

// o = se - de
auto output = rewriter.create<FHE::SubEintOp>(op->getLoc(), inputType,
auto output = rewriter.create<FHE::SubEintOp>(op->getLoc(), outputType,
sumTluOutput, diffTluOutput);
if (operatorIndexes != nullptr)
output->setAttr("TFHE.OId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,6 @@ verifyDotInputsOutputsConsistency(mlir::concretelang::FHELinalg::DotEint &op,
*op.getOperation(), lhsEltType, rhsEltType)) {
return ::mlir::failure();
}
if (!FHE::verifyEncryptedIntegerInputAndResultConsistency(
*op.getOperation(), lhsEltType, resultType)) {
return ::mlir::failure();
}
return ::mlir::success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,3 @@ func.func @bad_inputs_width(%arg0: !FHE.eint<2>, %arg1: !FHE.eint<3>) -> !FHE.ei
%1 = "FHE.mul_eint"(%arg0, %arg1): (!FHE.eint<2>, !FHE.eint<3>) -> (!FHE.eint<2>)
return %1: !FHE.eint<2>
}

// -----

// CHECK-LABEL: error: 'FHE.mul_eint' op should have the width of encrypted inputs and result equal
func.func @bad_result_width(%arg0: !FHE.eint<2>, %arg1: !FHE.eint<2>) -> !FHE.eint<3> {
%1 = "FHE.mul_eint"(%arg0, %arg1): (!FHE.eint<2>, !FHE.eint<2>) -> (!FHE.eint<3>)
return %1: !FHE.eint<3>
}
1 change: 1 addition & 0 deletions frontends/concrete-python/.ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ignore = [
"**/__init__.py" = ["F401"]
"concrete/fhe/compilation/configuration.py" = ["ARG002"]
"concrete/fhe/mlir/processors/all.py" = ["F401"]
"concrete/fhe/mlir/processors/assign_bit_widths.py" = ["ARG002"]
rudy-6-4 marked this conversation as resolved.
Show resolved Hide resolved
"concrete/fhe/mlir/converter.py" = ["ARG002", "B011", "F403", "F405"]
"examples/**" = ["PLR2004"]
"tests/**" = ["PLR2004", "PLW0603", "SIM300", "S311"]
15 changes: 12 additions & 3 deletions frontends/concrete-python/concrete/fhe/mlir/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,10 @@ def dot(self, resulting_type: ConversionType, x: Conversion, y: Conversion) -> C

self.error(highlights)

assert self.is_bit_width_compatible(resulting_type, x, y)
if x.is_encrypted and y.is_encrypted:
rudy-6-4 marked this conversation as resolved.
Show resolved Hide resolved
assert self.is_bit_width_compatible(x, y)
else:
assert self.is_bit_width_compatible(resulting_type, x, y)

if x.is_scalar or y.is_scalar:
return self.mul(resulting_type, x, y)
Expand Down Expand Up @@ -1369,7 +1372,10 @@ def matmul(self, resulting_type: ConversionType, x: Conversion, y: Conversion) -

self.error(highlights)

assert self.is_bit_width_compatible(resulting_type, x, y)
if x.is_encrypted and y.is_encrypted:
assert self.is_bit_width_compatible(x, y)
else:
assert self.is_bit_width_compatible(resulting_type, x, y)

if resulting_type.shape == ():
if x.is_clear:
Expand Down Expand Up @@ -1489,7 +1495,10 @@ def mul(self, resulting_type: ConversionType, x: Conversion, y: Conversion) -> C

self.error(highlights)

assert self.is_bit_width_compatible(resulting_type, x, y)
if x.is_encrypted and y.is_encrypted:
assert self.is_bit_width_compatible(x, y)
else:
assert self.is_bit_width_compatible(resulting_type, x, y)

use_linalg = x.is_tensor or y.is_tensor

Expand Down
Loading