-
Notifications
You must be signed in to change notification settings - Fork 152
Preserve linear_transform in Lattigo backend #3315
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
Open
copybara-service
wants to merge
1
commit into
main
Choose a base branch
from
test_960437546
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -697,6 +697,97 @@ struct ConvertOrionChebyshevOp | |
| } | ||
| }; | ||
|
|
||
| struct ConvertKernelLinearTransformOp | ||
| : public OpConversionPattern<kernel::LinearTransformOp> { | ||
| using OpConversionPattern<kernel::LinearTransformOp>::OpConversionPattern; | ||
|
|
||
| LogicalResult matchAndRewrite( | ||
| kernel::LinearTransformOp op, OpAdaptor adaptor, | ||
| ConversionPatternRewriter& rewriter) const override { | ||
| LLVM_DEBUG(llvm::dbgs() << "Lowering Kernel LinearTransformOp\n"); | ||
|
|
||
| FailureOr<Value> evaluatorResult = | ||
| getContextualEvaluator<lattigo::CKKSEvaluatorType>(op.getOperation()); | ||
| if (failed(evaluatorResult)) { | ||
| return rewriter.notifyMatchFailure( | ||
| op, "CKKS evaluator not found in function context"); | ||
| } | ||
| Value evaluator = evaluatorResult.value(); | ||
|
|
||
| FailureOr<Value> encoderResult = | ||
| getContextualEvaluator<lattigo::CKKSEncoderType>(op.getOperation()); | ||
| if (failed(encoderResult)) { | ||
| return rewriter.notifyMatchFailure( | ||
| op, "CKKS encoder not found in function context"); | ||
| } | ||
| Value encoder = encoderResult.value(); | ||
|
|
||
| // Extract level from input LWE ciphertext type | ||
| auto lweType = dyn_cast<lwe::LWECiphertextType>(op.getInput().getType()); | ||
| if (!lweType) { | ||
| return rewriter.notifyMatchFailure(op, "input is not LWE ciphertext"); | ||
| } | ||
| auto modulusChain = lweType.getModulusChain(); | ||
| if (!modulusChain) { | ||
| return rewriter.notifyMatchFailure(op, | ||
| "input LWE type has no modulus chain"); | ||
| } | ||
| int64_t levelQ = | ||
| modulusChain.getElements().size() - 1 - modulusChain.getCurrent(); | ||
|
|
||
| // Convert diagonal_indices from I64 to I32 (Lattigo CKKSLinearTransformOp | ||
| // expects I32) | ||
| auto diagonalIndicesAttr = op.getDiagonalIndices(); | ||
| std::vector<int32_t> diagonalIndicesI32; | ||
| for (auto val : diagonalIndicesAttr) { | ||
| diagonalIndicesI32.push_back(static_cast<int32_t>(val)); | ||
| } | ||
| auto diagonalIndicesI32Attr = | ||
| rewriter.getDenseI32ArrayAttr(diagonalIndicesI32); | ||
|
|
||
| // logBabyStepGiantStepRatio | ||
| // For now default to 0. | ||
| int64_t logBSGSRatio = 0; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this used anywhere?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is an input to the lattigo linear-transform op. |
||
|
|
||
| auto levelQAttr = rewriter.getI64IntegerAttr(levelQ); | ||
| auto logBSGSRatioAttr = rewriter.getI64IntegerAttr(logBSGSRatio); | ||
|
|
||
| auto diagonalsAttr = op.getDiagonals(); | ||
| Value diagonalsValue = | ||
| rewriter.create<arith::ConstantOp>(op.getLoc(), diagonalsAttr); | ||
|
|
||
| auto linearTransformOp = rewriter.create<lattigo::CKKSLinearTransformOp>( | ||
| op.getLoc(), adaptor.getInput().getType(), evaluator, encoder, | ||
| adaptor.getInput(), diagonalsValue, diagonalIndicesI32Attr, levelQAttr, | ||
| logBSGSRatioAttr); | ||
|
|
||
| auto outputLweType = | ||
| dyn_cast<lwe::LWECiphertextType>(op.getResult().getType()); | ||
| if (!outputLweType) { | ||
| return rewriter.notifyMatchFailure(op, "output is not LWE ciphertext"); | ||
| } | ||
| auto outputModulusChain = outputLweType.getModulusChain(); | ||
| if (!outputModulusChain) { | ||
| return rewriter.notifyMatchFailure( | ||
| op, "output LWE type has no modulus chain"); | ||
| } | ||
|
|
||
| Value result = linearTransformOp.getResult(); | ||
| if (outputModulusChain.getCurrent() < modulusChain.getCurrent()) { | ||
| int64_t diff = | ||
| modulusChain.getCurrent() - outputModulusChain.getCurrent(); | ||
| for (int64_t i = 0; i < diff; ++i) { | ||
| auto rescaleOp = rewriter.create<lattigo::CKKSRescaleNewOp>( | ||
| op.getLoc(), result.getType(), evaluator, result); | ||
| result = rescaleOp.getResult(); | ||
| } | ||
| } | ||
|
|
||
| rewriter.replaceOp(op, result); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| struct ConvertKernelEvalChebyshevOp | ||
| : public OpConversionPattern<kernel::EvalChebyshevOp> { | ||
| using OpConversionPattern<kernel::EvalChebyshevOp>::OpConversionPattern; | ||
|
|
@@ -918,7 +1009,7 @@ struct LWEToLattigo : public impl::LWEToLattigoBase<LWEToLattigo> { | |
| .addIllegalOp<lwe::RLWEEncryptOp, lwe::RLWEDecryptOp, lwe::RLWEEncodeOp, | ||
| lwe::RLWEDecodeOp, lwe::RAddOp, lwe::RSubOp, lwe::RMulOp, | ||
| lwe::RMulPlainOp, lwe::RSubPlainOp, lwe::RAddPlainOp, | ||
| kernel::EvalChebyshevOp>(); | ||
| kernel::EvalChebyshevOp, kernel::LinearTransformOp>(); | ||
|
|
||
| RewritePatternSet patterns(context); | ||
| addStructuralConversionPatterns(typeConverter, patterns, target); | ||
|
|
@@ -1098,8 +1189,8 @@ struct LWEToLattigo : public impl::LWEToLattigoBase<LWEToLattigo> { | |
| ConvertCKKSEncryptOp, ConvertCKKSDecryptOp, ConvertCKKSEncodeOp, | ||
| ConvertCKKSDecodeOp, ConvertCKKSLevelReduceOp, | ||
| ConvertCKKSBootstrappingOp, ConvertOrionLinearTransformOp, | ||
| ConvertOrionChebyshevOp, ConvertKernelEvalChebyshevOp>(typeConverter, | ||
| context); | ||
| ConvertOrionChebyshevOp, ConvertKernelEvalChebyshevOp, | ||
| ConvertKernelLinearTransformOp>(typeConverter, context); | ||
| } | ||
| // Misc | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be modulusChain.getCurrent()? the LattigoCKKSOps.td documentation says that what needs to be passed to the CKKSLinearTransformOp is the level at which the operation should be performed