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

Interfaces to generalize noise calculation in FHE/FHELinalg ops #416

Merged
merged 2 commits into from
Sep 12, 2023

Conversation

mkmks
Copy link
Contributor

@mkmks mkmks commented May 11, 2023

Instead of having one getSQManp implementation per op with a lot of repetition, the noise calculation is now modular.

  • Ops that implements UnaryEint/BinaryInt/BinaryEint interfaces share the operand noise presence check.
  • For many scalar ops no further calculation is needed. If it's not the case, an op can override sqMANP.
  • Integer operand types lookups are abstracted into BinaryInt::operandIntType()
  • Finding largest operand value for a type is abstracted into BinaryInt::operandMaxConstant
  • Noise calculation for matmul ops is simplified and it's now general enough to work for matmul_eint_int, matmul_int_eint and dot_eint_int at once.

Unless a sqMANP implementation is very short, the bulk of the noise calculation is moved to pure functions that don't access OpState directly (such as sqMANP_matmul). A sqMANP implementation is, thus, responsible to pass the necessary information from OpState through parameters. This was done to make it explicit that only a small part of OpState is needed for calculating noise, which helped, for example, generalizing matmul_eint_int, matmul_int_eint and dot_eint_int to a single sqMANP_matmul.

I have a hunch that sqMANP_conv2d could also be expressed through sqMANP_matmul (or it can be generalized to express sqMANP_conv2d`) but I haven't done so yet.

In the future, one could also find a way to declare mlir::tensor ops as implementing UnaryEint/BinaryInt/BinaryEint to throw away even more boilerplate. But there doesn't seem to be a way to that through ODS, so I gave up on it for the time being.

@cla-bot
Copy link

cla-bot bot commented May 11, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @mkmks on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

@cla-bot
Copy link

cla-bot bot commented May 12, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @mkmks on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

@cla-bot
Copy link

cla-bot bot commented May 12, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @mkmks on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

@cla-bot
Copy link

cla-bot bot commented May 12, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @mkmks on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

@cla-bot
Copy link

cla-bot bot commented May 12, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @mkmks on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

@cla-bot
Copy link

cla-bot bot commented May 16, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @mkmks on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

@cla-bot
Copy link

cla-bot bot commented May 25, 2023

Thank you for your pull request. We require contributors to sign our Contributor License Agreement / Terms and Conditions, and we don't seem to have the users @mkmks on file. In order for us to review and merge your code, please sign:

  • For individual contribution: our CLA
  • for Bounty submission, if you are an individual: our T&C
  • for Bounty submission, if you are a company: our T&C
    to get yourself added.

If you already signed one of this document, just wait to be added to the bot config.

}

return llvm::APInt{1, 1, false};
}

/// Calculates the squared Minimal Arithmetic Noise Padding of an
/// `FHELinalg.dot_eint_eint` operation.
static llvm::APInt getSqMANP(mlir::concretelang::FHELinalg::DotEint op,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is not yet ported to the interface mechanism but there are no hard reason?

static llvm::APInt getSqMANP(mlir::tensor::FromElementsOp op,
llvm::ArrayRef<const MANPLattice *> operandMANPs) {
static std::optional<llvm::APInt>
getSqMANP(mlir::tensor::FromElementsOp op,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see you dan't use the interfaces mechanism for ops that we don't define, maybe there are a way to attach interface to ops like in the bufferization mechanism (maybe @andidr you know how to achieve that).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its is possible to do that with external models. The file compilers/concrete-compiler/compiler/lib/Dialect/Concrete/Transforms/BufferizableOpInterfaceImpl.cpp shows this for BufferizableOpInterface. You just need to ensure that the code attaching the interface is executed at the beginning (e.g., by adding something to CompilationContext::getMLIRContext() in compilers/concrete-compiler/compiler/lib/Support/CompilerEngine.cpp).

@aquint-zama
Copy link
Contributor

@cla-bot check

@cla-bot cla-bot bot added the cla-signed label May 25, 2023
@cla-bot
Copy link

cla-bot bot commented May 25, 2023

The cla-bot has been summoned, and re-checked this pull request!

@youben11 youben11 force-pushed the manp-linalg-generic branch 2 times, most recently from 2714a6d to 272b1d9 Compare September 8, 2023 05:33
@youben11
Copy link
Member

youben11 commented Sep 8, 2023

@slab-ci concrete-python-test

@slab-ci
Copy link

slab-ci bot commented Sep 8, 2023

Failed to run command: command 'concrete-python-test' not found in config file

@youben11
Copy link
Member

youben11 commented Sep 8, 2023

@slab-ci concrete-python-tests-linux

@youben11 youben11 force-pushed the manp-linalg-generic branch 3 times, most recently from ee46150 to ba396d4 Compare September 11, 2023 10:26
@youben11
Copy link
Member

@slab-ci concrete-python-tests-linux

@youben11
Copy link
Member

@slab-ci compiler-cpu-build

1 similar comment
@BourgerieQuentin
Copy link
Member

@slab-ci compiler-cpu-build

Instead of having one `getSQManp` implementation per op with a lot of repetition, the noise
calculation is now modular.

- Ops that implements`UnaryEint`/`BinaryInt`/`BinaryEint` interfaces share the operand noise
presence check.
- For many scalar ops no further calculation is needed. If it's not the case, an op can override
`sqMANP`.
- Integer operand types lookups are abstracted into `BinaryInt::operandIntType()`
- Finding largest operand value for a type is abstracted into `BinaryInt::operandMaxConstant`
- Noise calculation for matmul ops is simplified and it's now general enough to work for
`matmul_eint_int`, `matmul_int_eint` and `dot_eint_int` at once.
@youben11 youben11 merged commit d16ce81 into main Sep 12, 2023
24 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants