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

Inplace binary operation class for the OpenFHE IR + sum and sub inplace operations #1339

Merged
merged 2 commits into from
Feb 7, 2025

Conversation

ShirChoiEX
Copy link
Contributor

This is my attempt of adding the inplace operations for the Openfhe IR. I added inplace addition/subtraction definitions to check if it would build, which it did.

I would like to have some comments on the implementation, if it is correct, and how can it be improved.

Copy link

google-cla bot commented Jan 29, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@ShirChoiEX
Copy link
Contributor Author

This is my attempt of adding the inplace operations for the Openfhe IR. I added inplace addition/subtraction definitions to check if it would build, which it did.

I would like to have some comments on the implementation, if it is correct, and how can it be improved.

@ShirChoiEX ShirChoiEX closed this Jan 29, 2025
@ShirChoiEX ShirChoiEX reopened this Jan 29, 2025
@ShirChoiEX ShirChoiEX closed this Jan 29, 2025
@ShirChoiEX ShirChoiEX reopened this Jan 29, 2025
Copy link
Collaborator

@ZenithalHourlyRate ZenithalHourlyRate left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

Please install a pre-commit hook (check https://heir.dev/docs/contributing/) to make PR format clean.

Others are great!

@@ -57,6 +57,21 @@ class Openfhe_BinaryOp<string mnemonic, list<Trait> traits = []>
let results = (outs NewLWECiphertext:$output);
}

class Openfhe_BinaryInPlaceOp<string mnemonic, list<Trait> traits = []>
: Openfhe_Op<mnemonic, traits # [
AllTypesMatch<["lhs", "rhs"]>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

There is a recent PR (#1334) that relaxes the AllTypesMatch requirement to [SameOperandsAndResultRings, InferTypeOpAdaptor]

Discussion for maintainers: We might also need to register a MemoryEffects<[MemWrite]> to make liveness analysis happy, though I found --cse --canonicalize does not eliminate these inplace ops.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there a need to add [SameOperandsAndResultRings] trait for the inplace operations? It seems to be analyzing both inputs and the output, but there is no output for an inplace function. There seem to be no alternative trait like [SameOperandsRings], or does the [SameOperandsAndResultRings] ignore the result check if it is not created? Also, regarding the #1334 PR, he uses [SameOperandsAndResultRings] in AddOp and SubOp but not in the MulOp why is it so?

Copy link
Collaborator

@AlexanderViand-Intel AlexanderViand-Intel Jan 30, 2025

Choose a reason for hiding this comment

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

Also, regarding the #1334 PR, he uses [SameOperandsAndResultRings] in AddOp and SubOp but not in the MulOp why is it so?

If the question is why no SameOperandsAndResultRings is needed for openfhe.mul it's because
AllTypesMatch<["lhs", "rhs", "output"]> implies SameOperandsAndResultRings, but if the question is instead why I didn't "relax" the constraints on openfhe.mul to "only" SameOperandsAndResultRings, that's just because the op isn't used in our default lowering pipeline so there was no need to.

EDIT: Note that the AllTypesMatch<["lhs", "rhs", "output"]> was already applied to openfhe.mul before my PR, it was just previously inherited from Openfhe_BinaryOp

Copy link
Collaborator

Choose a reason for hiding this comment

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

or does the [SameOperandsAndResultRings] ignore the result check if it is not created?

From the following line we can know that if there is no result, the trait only checks the equivalence among operands.

static LogicalResult verifyTrait(Operation *op) {
::mlir::heir::polynomial::RingAttr rings = nullptr;
for (auto rTy : op->getResultTypes()) {
auto ct = dyn_cast<lwe::NewLWECiphertextType>(rTy);
if (!ct) continue;
if (rings == nullptr) {
rings = ct.getCiphertextSpace().getRing();
continue;
}
if (rings != ct.getCiphertextSpace().getRing()) {
return op->emitOpError()
<< "requires all operands and results to have the same rings";
}
}
for (auto oTy : op->getOperandTypes()) {
auto ct = dyn_cast<lwe::NewLWECiphertextType>(oTy);
if (!ct) continue; // Check only ciphertexts
if (rings == nullptr) {
rings = ct.getCiphertextSpace().getRing();
continue;
}
if (rings != ct.getCiphertextSpace().getRing()) {
return op->emitOpError()
<< "requires all operands and results to have the same rings";
}
}
return success();
}
};

@ShirChoiEX ShirChoiEX force-pushed the OpenfheInplaceOperations branch from 48daf13 to 97057a1 Compare January 30, 2025 12:26
@ShirChoiEX ShirChoiEX force-pushed the OpenfheInplaceOperations branch from 97057a1 to e44d36b Compare February 2, 2025 09:22
@ShirChoiEX
Copy link
Contributor Author

I rebased the branch + fixed merge conflicts. All tests should be passing now

Copy link
Collaborator

@j2kun j2kun left a comment

Choose a reason for hiding this comment

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

Sorry for the last minute comment. Otherwise, LGTM!

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

j2kun commented Feb 7, 2025

I'll get this in an patch the minor nitpick internally :)

@copybara-service copybara-service bot merged commit d372bf7 into google:main Feb 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.

None yet

4 participants