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

Lattigo: Add Negate op and e2e CMUX example #1627

Merged
merged 1 commit into from
Apr 7, 2025

Conversation

ZenithalHourlyRate
Copy link
Collaborator

Fixes #1623

Depends on #1620.

This PR construct a Lattigo Negate Op via ring operation in Lattigo (they seem not to offer a high level one).

To better demonstrate its use a CMUX e2e example from #1620 is also added.

@ZenithalHourlyRate ZenithalHourlyRate force-pushed the lattigo-negate branch 2 times, most recently from 9fd8287 to e64986e Compare April 2, 2025 16:44
@ZenithalHourlyRate
Copy link
Collaborator Author

Rebased after #1620 is in. Ready for review now.

@ZenithalHourlyRate ZenithalHourlyRate marked this pull request as ready for review April 2, 2025 16:44
Copy link
Collaborator

@asraa asraa left a comment

Choose a reason for hiding this comment

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

Thank you! Very small nits

valueString = "[]int64{";
for (auto value : denseAttr.getValues<APInt>()) {
valueString += std::to_string(value.getSExtValue()) + ", ";
if (denseAttr.getType().getElementType().isInteger(1)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: i was noticing the i1 / bool case isn't handled for the IntegerAttr case above, but then in convertType it is handlded below.

Maybe you can remove this typeswitch and instead rely on auto res = convertType(valueAttr.getType())?

Copy link
Collaborator Author

@ZenithalHourlyRate ZenithalHourlyRate Apr 3, 2025

Choose a reason for hiding this comment

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

Now arith.constant is honest on type: what is in IR is converted to corresponding type using convertType. The reason int64/float64 is deliberately here was that Encode/Decode only accepts int64[]/float64[] and we have the following common pattern

cst = arith.constant <4xi16>
Decode(pt, cst)

so for convenience I directly convert i16 to i64. Now the extra step is done in DecodeOp.

@ZenithalHourlyRate ZenithalHourlyRate force-pushed the lattigo-negate branch 2 times, most recently from bfdefac to 7398502 Compare April 3, 2025 08:40
Copy link
Collaborator

@asraa asraa left a comment

Choose a reason for hiding this comment

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

Thanks as always!

@asraa asraa added the pull_ready Indicates whether a PR is ready to pull. The copybara worker will import for internal testing label Apr 4, 2025
@j2kun
Copy link
Collaborator

j2kun commented Apr 6, 2025

It looks like this is failing the cmux test at head

llvm::SmallVectorTemplateCommon<long>::operator[](size_type) [T = long]: idx < size()
*** Check failure stack trace: ***
    @     0x55af1f1a3e09  absl::log_internal::LogMessage::SendToLog()
    @     0x55af1f1a3d7e  absl::log_internal::LogMessage::Flush()
    @     0x55af1f177af4  __assert_fail
    @     0x55af1a658830  mlir::heir::ScaleAnalysisBackward<>::visitOperation()::{lambda()#1}::operator()<>()
    @     0x55af1a654e53  mlir::heir::ScaleAnalysisBackward<>::visitOperation()
    @     0x55af1b81c52d  mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::visitOperation()
    @     0x55af1b81b3ac  mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::initializeRecursively()
    @     0x55af1b81b559  mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::initializeRecursively()
    @     0x55af1b81b559  mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::initializeRecursively()
    @     0x55af1b81b559  mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::initializeRecursively()
    @     0x55af1b81d82e  mlir::DataFlowSolver::initializeAndRun()
    @     0x55af1a63a46f  mlir::heir::PopulateScaleBGV::runOnOperation()

This was internal so I will try to reproduce externally

heir/tools/heir-opt "--annotate-module=backend=lattigo scheme=bgv" --mlir-to-bgv --scheme-to-lattigo -o blaze-out/k8-fastbuild/bin/tests/Examples/lattigo/bgv/cmux/cmux_heir_opt.mlir tests/Examples/lattigo/bgv/cmux/cmux.mlir

@ZenithalHourlyRate
Copy link
Collaborator Author

Is this the same problem as #1671

@j2kun
Copy link
Collaborator

j2kun commented Apr 6, 2025

Confirmed. Here's the trace:

 bazel run //tools:heir-opt -- "--annotate-module=backend=lattigo scheme=bgv" --mlir-to-bgv --scheme-to-lattigo $PWD/tests/Examples/lattigo/bgv/cmux/cmux.mlir
...
heir-opt: external/llvm-project/llvm/include/llvm/ADT/SmallVector.h:291: reference llvm::SmallVectorTemplateCommon<long>::operator[](size_type) [T = long]: Assertion `idx < size()' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/j2kun/.cache/bazel/_bazel_j2kun/b07a48cf9da8c73a462d3608e5c508dd/execroot/_main/bazel-out/k8-dbg/bin/tools/heir-opt "--annotate-module=backend=lattigo scheme=bgv" --mlir-to-bgv --scheme-to-lattigo /home/j2kun/fhe/heir/tests/Examples/lattigo/bgv/cmux/cmux.mlir
<snip>
#12 0x000055ec18841c82 llvm::SmallVectorTemplateCommon<long, void>::operator[](unsigned long) /proc/self/cwd/external/llvm-project/llvm/include/llvm/ADT/SmallVector.h:0:5
#13 0x000055ec1a60f1df auto mlir::heir::ScaleAnalysisBackward<mlir::heir::BGVScaleModel>::visitOperation(mlir::Operation*, llvm::ArrayRef<mlir::heir::ScaleLattice*>, llvm::ArrayRef<mlir::heir::ScaleLattice const*>)::'lambda'(auto)::operator()<mlir::arith::MulIOp>(auto) const /proc/self/cwd/lib/Analysis/ScaleAnalysis/ScaleAnalysis.cpp:334:37
#14 0x000055ec1a60ee1c llvm::TypeSwitch<mlir::Operation&, void>& llvm::TypeSwitch<mlir::Operation&, void>::Case<mlir::arith::MulIOp, mlir::heir::ScaleAnalysisBackward<mlir::heir::BGVScaleModel>::visitOperation(mlir::Operation*, llvm::ArrayRef<mlir::heir::ScaleLattice*>, llvm::ArrayRef<mlir::heir::ScaleLattice const*>)::'lambda'(auto)&>(mlir::heir::ScaleAnalysisBackward<mlir::heir::BGVScaleModel>::visitOperation(mlir::Operation*, llvm::ArrayRef<mlir::heir::ScaleLattice*>, llvm::ArrayRef<mlir::heir::ScaleLattice const*>)::'lambda'(auto)&) /proc/self/cwd/external/llvm-project/llvm/include/llvm/ADT/TypeSwitch.h:149:7
#15 0x000055ec1a60a3bf mlir::heir::ScaleAnalysisBackward<mlir::heir::BGVScaleModel>::visitOperation(mlir::Operation*, llvm::ArrayRef<mlir::heir::ScaleLattice*>, llvm::ArrayRef<mlir::heir::ScaleLattice const*>) /proc/self/cwd/lib/Analysis/ScaleAnalysis/ScaleAnalysis.cpp:310:17
#16 0x000055ec1a5ce66d mlir::dataflow::SparseBackwardDataFlowAnalysis<mlir::heir::ScaleLattice>::visitOperationImpl(mlir::Operation*, llvm::ArrayRef<mlir::dataflow::AbstractSparseLattice*>, llvm::ArrayRef<mlir::dataflow::AbstractSparseLattice const*>) /proc/self/cwd/external/llvm-project/mlir/include/mlir/Analysis/DataFlow/SparseAnalysis.h:532:12
#17 0x000055ec1e9fbba1 mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::visitOperation(mlir::Operation*) /proc/self/cwd/external/llvm-project/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp:537:10
#18 0x000055ec1e9fa4a7 mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::initializeRecursively(mlir::Operation*) /proc/self/cwd/external/llvm-project/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp:343:14
#19 0x000055ec1e9fa5cc mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::initializeRecursively(mlir::Operation*) /proc/self/cwd/external/llvm-project/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp:354:20
#20 0x000055ec1e9fa5cc mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::initializeRecursively(mlir::Operation*) /proc/self/cwd/external/llvm-project/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp:354:20
#21 0x000055ec1e9fa5cc mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::initializeRecursively(mlir::Operation*) /proc/self/cwd/external/llvm-project/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp:354:20
#22 0x000055ec1e9fa46d mlir::dataflow::AbstractSparseBackwardDataFlowAnalysis::initialize(mlir::Operation*) /proc/self/cwd/external/llvm-project/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp:338:10
#23 0x000055ec1ea018bd mlir::DataFlowSolver::initializeAndRun(mlir::Operation*) /proc/self/cwd/external/llvm-project/mlir/lib/Analysis/DataFlowFramework.cpp:116:25
#24 0x000055ec1a5cc3e2 mlir::heir::PopulateScaleBGV::runOnOperation() /proc/self/cwd/lib/Transforms/PopulateScale/PopulateScaleBGV.cpp:81:23
<snip>

@j2kun
Copy link
Collaborator

j2kun commented Apr 6, 2025

ok I will remove my smoke test from that PR so it can go in on its own

@j2kun
Copy link
Collaborator

j2kun commented Apr 6, 2025

ok I will remove my smoke test from that PR so it can go in on its own

Cherry-picking #1671 indeed fixes the issue.

j2kun added a commit to j2kun/heir that referenced this pull request Apr 6, 2025
@ZenithalHourlyRate
Copy link
Collaborator Author

Do I need to rebase this (how is the internal patching status for it) (changes in #1661 also apply here so there will be some new change if I rebase)

@j2kun
Copy link
Collaborator

j2kun commented Apr 7, 2025

I am syncing the internal patch now, will let you know.

@j2kun
Copy link
Collaborator

j2kun commented Apr 7, 2025

Looks like the patch is working, and should land shortly

@copybara-service copybara-service bot merged commit d1ddd5e into google:main Apr 7, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pull_ready Indicates whether a PR is ready to pull. The copybara worker will import for internal testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support negating a ciphertext in lattigo
3 participants