-
Notifications
You must be signed in to change notification settings - Fork 57
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
BGV: Count Add/Keyswitch in one level for OpenFHE #1254
Draft
ZenithalHourlyRate
wants to merge
1
commit into
google:main
Choose a base branch
from
ZenithalHourlyRate:bgv-modreduce-count
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.
Draft
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
157 changes: 157 additions & 0 deletions
157
lib/Analysis/AddAndKeySwitchCountAnalysis/AddAndKeySwitchCountAnalysis.cpp
This file contains 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 |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#include "lib/Analysis/AddAndKeySwitchCountAnalysis/AddAndKeySwitchCountAnalysis.h" | ||
|
||
#include "lib/Analysis/SecretnessAnalysis/SecretnessAnalysis.h" | ||
#include "lib/Dialect/Mgmt/IR/MgmtAttributes.h" | ||
#include "lib/Dialect/Mgmt/IR/MgmtOps.h" | ||
#include "lib/Dialect/Secret/IR/SecretOps.h" | ||
#include "lib/Dialect/TensorExt/IR/TensorExtOps.h" | ||
#include "llvm/include/llvm/ADT/TypeSwitch.h" // from @llvm-project | ||
#include "mlir/include/mlir/Analysis/DataFlowFramework.h" // from @llvm-project | ||
#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project | ||
#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project | ||
#include "mlir/include/mlir/IR/Operation.h" // from @llvm-project | ||
#include "mlir/include/mlir/IR/Value.h" // from @llvm-project | ||
#include "mlir/include/mlir/IR/Visitors.h" // from @llvm-project | ||
#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace heir { | ||
|
||
LogicalResult CountAnalysis::visitOperation( | ||
Operation *op, ArrayRef<const CountLattice *> operands, | ||
ArrayRef<CountLattice *> results) { | ||
auto propagate = [&](Value value, const CountState &state) { | ||
auto *lattice = getLatticeElement(value); | ||
ChangeResult changed = lattice->join(state); | ||
propagateIfChanged(lattice, changed); | ||
}; | ||
|
||
llvm::TypeSwitch<Operation &>(*op) | ||
.Case<secret::GenericOp>([&](auto genericOp) { | ||
Block *body = genericOp.getBody(); | ||
for (auto i = 0; i != body->getNumArguments(); ++i) { | ||
auto blockArg = body->getArgument(i); | ||
// one Vfresh | ||
propagate(blockArg, CountState(1, 0)); | ||
} | ||
}) | ||
.Case<arith::AddIOp, arith::SubIOp, arith::AddFOp, arith::SubFOp>( | ||
[&](auto &op) { | ||
// condition on result secretness | ||
SmallVector<OpResult> secretResults; | ||
getSecretResults(op, secretResults); | ||
if (secretResults.empty()) { | ||
return; | ||
} | ||
|
||
CountState zeroState(0, 0); | ||
SmallVector<OpOperand *> secretOperands; | ||
getSecretOperands(op, secretOperands); | ||
for (auto *operand : secretOperands) { | ||
auto countState = | ||
operands[operand->getOperandNumber()]->getValue(); | ||
zeroState = zeroState + countState; | ||
} | ||
|
||
for (auto result : secretResults) { | ||
propagate(result, zeroState); | ||
} | ||
}) | ||
.Case<arith::MulIOp, arith::MulFOp>([&](auto &op) { | ||
SmallVector<OpResult> secretResults; | ||
getSecretResults(op, secretResults); | ||
if (secretResults.empty()) { | ||
return; | ||
} | ||
|
||
// now noise is Vmult | ||
// TODO(#1168): we can actually do a more fine grained analysis here | ||
// distinguishing ct-ct and ct-pt | ||
propagate(op.getResult(), CountState(1, 0)); | ||
}) | ||
.Case<mgmt::RelinearizeOp, tensor_ext::RotateOp>([&](auto &op) { | ||
auto secretness = ensureSecretness(op, op->getOperand(0)); | ||
if (!secretness) { | ||
return; | ||
} | ||
|
||
auto state = operands[0]->getValue(); | ||
if (!state.isInitialized()) { | ||
return; | ||
} | ||
|
||
propagate(op.getResult(), state.keySwitch()); | ||
}) | ||
// TODO(#1174): in BGV tensor::ExtractOp is assumed to be always | ||
// mul+const | ||
.Case<tensor::ExtractOp>([&](auto &op) { | ||
auto secretness = ensureSecretness(op, op.getResult()); | ||
if (!secretness) { | ||
return; | ||
} | ||
|
||
// now noise is Vmult + one Vks | ||
propagate(op.getResult(), CountState(1, 1)); | ||
}) | ||
.Case<mgmt::ModReduceOp>([&](auto modReduceOp) { | ||
// implicitly ensure that the operand is secret | ||
|
||
propagate(modReduceOp.getResult(), CountState(0, 0)); | ||
}); | ||
// should not propagate through mgmt::ModReduceOp | ||
return success(); | ||
} | ||
|
||
void annotateCount(Operation *top, DataFlowSolver *solver) { | ||
auto getIntegerAttr = [&](int level) { | ||
return IntegerAttr::get(IntegerType::get(top->getContext(), 64), level); | ||
}; | ||
|
||
auto maxAddCount = 0; | ||
auto maxKeySwitchCount = 0; | ||
|
||
auto getCount = [&](Value value) { | ||
auto state = solver->lookupState<CountLattice>(value)->getValue(); | ||
// update the max | ||
maxAddCount = std::max(maxAddCount, state.getAddCount()); | ||
maxKeySwitchCount = std::max(maxKeySwitchCount, state.getKeySwitchCount()); | ||
return std::make_tuple(state.getAddCount(), state.getKeySwitchCount()); | ||
}; | ||
|
||
top->walk<WalkOrder::PreOrder>([&](secret::GenericOp genericOp) { | ||
for (auto i = 0; i != genericOp.getBody()->getNumArguments(); ++i) { | ||
auto blockArg = genericOp.getBody()->getArgument(i); | ||
auto [addCount, keySwitchCount] = getCount(blockArg); | ||
if (addCount != 0) { | ||
genericOp.setArgAttr(i, "addCount", getIntegerAttr(addCount)); | ||
} | ||
if (keySwitchCount != 0) { | ||
genericOp.setArgAttr(i, "keySwitchCount", | ||
getIntegerAttr(keySwitchCount)); | ||
} | ||
} | ||
|
||
genericOp.getBody()->walk<WalkOrder::PreOrder>([&](Operation *op) { | ||
if (op->getNumResults() == 0) { | ||
return; | ||
} | ||
auto [addCount, keySwitchCount] = getCount(op->getResult(0)); | ||
if (addCount != 0) { | ||
op->setAttr("addCount", getIntegerAttr(addCount)); | ||
} | ||
if (keySwitchCount != 0) { | ||
op->setAttr("keySwitchCount", getIntegerAttr(keySwitchCount)); | ||
} | ||
}); | ||
|
||
// annotate mgmt::OpenfheParamsAttr to func::FuncOp containing the genericOp | ||
auto *funcOp = genericOp->getParentOp(); | ||
auto openfheParamAttr = mgmt::OpenfheParamsAttr::get( | ||
funcOp->getContext(), maxAddCount, maxKeySwitchCount); | ||
funcOp->setAttr(mgmt::MgmtDialect::kArgOpenfheParamsAttrName, | ||
openfheParamAttr); | ||
}); | ||
} | ||
|
||
} // namespace heir | ||
} // namespace mlir |
123 changes: 123 additions & 0 deletions
123
lib/Analysis/AddAndKeySwitchCountAnalysis/AddAndKeySwitchCountAnalysis.h
This file contains 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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#ifndef LIB_ANALYSIS_ADDANDKEYSWITCHCOUNTANALYSISANALYSIS_ADDANDKEYSWITCHCOUNTANALYSISANALYSIS_H_ | ||
#define LIB_ANALYSIS_ADDANDKEYSWITCHCOUNTANALYSISANALYSIS_ADDANDKEYSWITCHCOUNTANALYSISANALYSIS_H_ | ||
|
||
#include "lib/Analysis/SecretnessAnalysis/SecretnessAnalysis.h" | ||
#include "mlir/include/mlir/Analysis/DataFlow/SparseAnalysis.h" // from @llvm-project | ||
#include "mlir/include/mlir/IR/Diagnostics.h" // from @llvm-project | ||
#include "mlir/include/mlir/IR/Operation.h" // from @llvm-project | ||
#include "mlir/include/mlir/IR/Value.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace heir { | ||
|
||
// This analysis should be used after --secret-with-mgmt-bgv | ||
// but before --secret-distribute-generic | ||
// where a whole secret::GenericOp is assumed | ||
// | ||
// this follows strictly the strategy of modulus switch | ||
// right before multiplication including the first. | ||
// namely FLEXIBLEAUTOEXT in OpenFHE | ||
// | ||
// OpenFHE only supports setting EvalAddCount/EvalKeySwitchCount | ||
// for BGV/BFV, so HEIR only supports BGV here | ||
// | ||
// For not include-the-first, the addCount for the L-th level | ||
// might be overestimated, as we can not distinguish between | ||
// Vmult and Vfresh | ||
|
||
class CountState { | ||
public: | ||
CountState() : initialized(false), addCount(0), keySwitchCount(0) {} | ||
explicit CountState(int addCount, int keySwitchCount) | ||
: initialized(true), addCount(addCount), keySwitchCount(keySwitchCount) {} | ||
~CountState() = default; | ||
|
||
int getAddCount() const { | ||
assert(isInitialized()); | ||
return addCount; | ||
} | ||
|
||
int getKeySwitchCount() const { | ||
assert(isInitialized()); | ||
return keySwitchCount; | ||
} | ||
|
||
bool operator==(const CountState &rhs) const { | ||
return initialized == rhs.initialized && addCount == rhs.addCount && | ||
keySwitchCount == rhs.keySwitchCount; | ||
} | ||
|
||
bool isInitialized() const { return initialized; } | ||
|
||
CountState operator+(const CountState &rhs) const { | ||
assert(isInitialized() && rhs.isInitialized()); | ||
return CountState{addCount + rhs.addCount, | ||
keySwitchCount + rhs.keySwitchCount}; | ||
} | ||
|
||
CountState keySwitch() const { | ||
assert(isInitialized()); | ||
return CountState{addCount, keySwitchCount + 1}; | ||
} | ||
|
||
CountState max(const CountState &rhs) const { | ||
assert(isInitialized() && rhs.isInitialized()); | ||
return CountState{std::max(addCount, rhs.addCount), | ||
std::max(keySwitchCount, rhs.keySwitchCount)}; | ||
} | ||
|
||
static CountState join(const CountState &lhs, const CountState &rhs) { | ||
if (!lhs.isInitialized()) return rhs; | ||
if (!rhs.isInitialized()) return lhs; | ||
|
||
return lhs.max(rhs); | ||
} | ||
|
||
void print(llvm::raw_ostream &os) const { | ||
if (isInitialized()) { | ||
os << "CountState(" << addCount << ", " << keySwitchCount << ")"; | ||
} else { | ||
os << "CountState(uninitialized)"; | ||
} | ||
} | ||
|
||
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, | ||
const CountState &state) { | ||
state.print(os); | ||
return os; | ||
} | ||
|
||
private: | ||
bool initialized; | ||
int addCount; // how many Vmult or Vfresh (before first multiplication) | ||
// encountered | ||
int keySwitchCount; | ||
}; | ||
|
||
class CountLattice : public dataflow::Lattice<CountState> { | ||
public: | ||
using Lattice::Lattice; | ||
}; | ||
|
||
class CountAnalysis | ||
: public dataflow::SparseForwardDataFlowAnalysis<CountLattice>, | ||
public SecretnessAnalysisDependent<CountAnalysis> { | ||
public: | ||
using SparseForwardDataFlowAnalysis::SparseForwardDataFlowAnalysis; | ||
friend class SecretnessAnalysisDependent<CountAnalysis>; | ||
|
||
void setToEntryState(CountLattice *lattice) override { | ||
propagateIfChanged(lattice, lattice->join(CountState())); | ||
} | ||
|
||
LogicalResult visitOperation(Operation *op, | ||
ArrayRef<const CountLattice *> operands, | ||
ArrayRef<CountLattice *> results) override; | ||
}; | ||
|
||
void annotateCount(Operation *top, DataFlowSolver *solver); | ||
|
||
} // namespace heir | ||
} // namespace mlir | ||
|
||
#endif // LIB_ANALYSIS_ADDANDKEYSWITCHCOUNTANALYSISANALYSIS_ADDANDKEYSWITCHCOUNTANALYSISANALYSIS_H_ |
This file contains 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package( | ||
default_applicable_licenses = ["@heir//:license"], | ||
default_visibility = ["//visibility:public"], | ||
) | ||
|
||
cc_library( | ||
name = "AddAndKeySwitchCountAnalysis", | ||
srcs = ["AddAndKeySwitchCountAnalysis.cpp"], | ||
hdrs = ["AddAndKeySwitchCountAnalysis.h"], | ||
deps = [ | ||
"@heir//lib/Analysis/SecretnessAnalysis", | ||
"@heir//lib/Dialect/Mgmt/IR:Dialect", | ||
"@heir//lib/Dialect/Secret/IR:Dialect", | ||
"@heir//lib/Dialect/TensorExt/IR:Dialect", | ||
"@llvm-project//llvm:Support", | ||
"@llvm-project//mlir:Analysis", | ||
"@llvm-project//mlir:IR", | ||
"@llvm-project//mlir:Support", | ||
], | ||
) |
This file contains 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 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 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 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 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.
Related to my other comment, naively I would expect zero adds here, not 1, and below (line 53) it appears you're adding the countState once per operand, rather than per op.