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

BFVTensor #285

Merged
merged 8 commits into from
Apr 22, 2021
Merged

BFVTensor #285

merged 8 commits into from
Apr 22, 2021

Conversation

comidan
Copy link
Contributor

@comidan comidan commented Apr 19, 2021

Description

This PR tries to add support for BFVTensor.

Checklist

@comidan comidan mentioned this pull request Apr 19, 2021
Copy link
Member

@bcebere bcebere 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 for the contribution. It is a major step in the right direction!

One significant problem is that we should reuse the encrypted_tensor interface, which should be the root class for BFVTensor.
There is also a bit of code duplication between CKKSTensor and BFVTensor, but we can think in another PR about templating the classes.

Regarding the failing tests, it looks like a linting problem. Make sure that you run
.github/workflows/scripts/lint_cpp.sh
and
.github/workflows/scripts/lint_python.sh

and they are successful before pushing. You might need to install clang-format, flake8 and black locally for that.

Other than that, awesome work so far!

@@ -16,7 +16,8 @@ if(${BUILD_TEST})
${TENSEAL_TESTS_BASEDIR}/tensors/ckksvector_test.cpp
${TENSEAL_TESTS_BASEDIR}/tensors/ckkstensor_test.cpp
${TENSEAL_TESTS_BASEDIR}/tensors/plaintensor_test.cpp
${TENSEAL_TESTS_BASEDIR}/tensors/bfvvector_test.cpp)
${TENSEAL_TESTS_BASEDIR}/tensors/bfvvector_test.cpp
Copy link
Member

Choose a reason for hiding this comment

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

Why the duplicate cpp file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No sorry probably I just copy pasted and changed again after having saved, those lines should be appearing:

${TENSEAL_TESTS_BASEDIR}/tensors/bfvvector_test.cpp
${TENSEAL_TESTS_BASEDIR}/tensors/bfvtensor_test.cpp)

"ckkstensor.h",
"ckksvector.h",
"encrypted_tensor.h",
"encrypted_tensor_bfv.h",
Copy link
Member

Choose a reason for hiding this comment

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

encrypted_tensor should be a generic interface for all tensors in TenSEAL? Why do we need encrypted_tensor_bfv too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right of course, sorry.
I just got a little confused with the scale method in encrypted_tensor.h given that it is not supposed to be used in BFV and indeed only now I've discovered that it's declared as not implemented for BFV (for the BFVVector counterpart I mean).
Also the same for polyval_inplace method which was using double so I initially changed the data type to the template plain_data_t but it was messing with BFVVector which was using double as template but the method itself was again declared as not implemented, so I did the same now for BFVTensor.

Sorry for the confusion

@@ -0,0 +1,263 @@
#ifndef TENSEAL_TENSOR_ENCRYPTED_TENSOR_BFV_H
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a separate interface from encrypted_tensor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No we don't sorry again, the wrong reason was the one mentioned above

// The serialized ciphertexts
repeated bytes ciphertexts = 2;
// Optional batch size. Exists only if batching is enabled
uint32 batch_size = 4;
Copy link
Member

Choose a reason for hiding this comment

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

Why 4? this can be index 3.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should stop copy-pasting to be faster and forgetting about it, sorry.
I deleted the scale which was not needed but forgot indeed to change the index to 3.

using namespace std;

template <class Iterable>
bool are_close(const Iterable& l, const std::vector<int64_t>& r) {
Copy link
Member

Choose a reason for hiding this comment

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

are_close is relevant only for CKKS. For BFV, you should get the exact result. See the BFVVector C++ tests for reference

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right thank you, I changed it now to assert/expect the exact result indeed

]


def _almost_equal_number(v1, v2, m_pow_ten):
Copy link
Member

Choose a reason for hiding this comment

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

Similarly, _almost_equal_number is only for CKKS. Here you should test for the exact result. See the test_bfv_vector tests for reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes right, thank you

@comidan
Copy link
Contributor Author

comidan commented Apr 20, 2021

Thank you for the contribution. It is a major step in the right direction!

One significant problem is that we should reuse the encrypted_tensor interface, which should be the root class for BFVTensor.
There is also a bit of code duplication between CKKSTensor and BFVTensor, but we can think in another PR about templating the classes.

Regarding the failing tests, it looks like a linting problem. Make sure that you run
.github/workflows/scripts/lint_cpp.sh
and
.github/workflows/scripts/lint_python.sh

and they are successful before pushing. You might need to install clang-format, flake8 and black locally for that.

Other than that, awesome work so far!

Ok thank you for the detailed explanation.
I run both of the scripts and it does not seem to give me any kind of errors (I run them after compiling and building everything).
The only output of the first script is the diff output from git, as the script indeed does after calling clang-format.
While the second one it tells me "All done!".

So I think it's ok and I'll proceed to push the changes.

@bcebere
Copy link
Member

bcebere commented Apr 20, 2021

Thank you for the contribution. It is a major step in the right direction!
One significant problem is that we should reuse the encrypted_tensor interface, which should be the root class for BFVTensor.
There is also a bit of code duplication between CKKSTensor and BFVTensor, but we can think in another PR about templating the classes.
Regarding the failing tests, it looks like a linting problem. Make sure that you run
.github/workflows/scripts/lint_cpp.sh
and
.github/workflows/scripts/lint_python.sh
and they are successful before pushing. You might need to install clang-format, flake8 and black locally for that.
Other than that, awesome work so far!

Ok thank you for the detailed explanation.
I run both of the scripts and it does not seem to give me any kind of errors (I run them after compiling and building everything).
The only output of the first script is the diff output from git, as the script indeed does after calling clang-format.
While the second one it tells me "All done!".

So I think it's ok and I'll proceed to push the changes.

The diff you are seeing contains the actual linting errors that break the build.

But my bad, it can be a bit confusing:

  • lint_cpp.sh runs clang-format inplace, you should commit the diff, if any.
  • lint_python.sh does a dry-run, but it doesn't change anything. If lint_python.sh outputs anything at all, just run in the root of the repository black . to lint the py scripts.

@comidan
Copy link
Contributor Author

comidan commented Apr 20, 2021

Thank you for the contribution. It is a major step in the right direction!
One significant problem is that we should reuse the encrypted_tensor interface, which should be the root class for BFVTensor.
There is also a bit of code duplication between CKKSTensor and BFVTensor, but we can think in another PR about templating the classes.
Regarding the failing tests, it looks like a linting problem. Make sure that you run
.github/workflows/scripts/lint_cpp.sh
and
.github/workflows/scripts/lint_python.sh
and they are successful before pushing. You might need to install clang-format, flake8 and black locally for that.
Other than that, awesome work so far!

Ok thank you for the detailed explanation.
I run both of the scripts and it does not seem to give me any kind of errors (I run them after compiling and building everything).
The only output of the first script is the diff output from git, as the script indeed does after calling clang-format.
While the second one it tells me "All done!".
So I think it's ok and I'll proceed to push the changes.

The diff you are seeing contains the actual linting errors that break the build.

But my bad, it can be a bit confusing:

  • lint_cpp.sh runs clang-format inplace, you should commit the diff, if any.
  • lint_python.sh does a dry-run, but it doesn't change anything. If lint_python.sh outputs anything at all, just run in the root of the repository black . to lint the py scripts.

Ok thank you, I run black . at the root of the repository and it considered two more files than the ones considered by the script to reformat. It also prints at then end of the log "All done!".
Regarding the diff, ok I'll then commit them!

@comidan
Copy link
Contributor Author

comidan commented Apr 20, 2021

Ok now with the new commit it shows that by running the python tests some of them pass but then it stops at one giving indeed the same error I get locally: ValueError: SEAL: couldn't find context_data from params_id,

add polyval to BFVTensor
BFVTensor tests bugs: remove scaling factor for BFV, use int64_t instead of int, fix context construction
@bcebere
Copy link
Member

bcebere commented Apr 21, 2021

Ok now with the new commit it shows that by running the python tests some of them pass but then it stops at one giving indeed the same error I get locally: ValueError: SEAL: couldn't find context_data from params_id,

I pushed some fixes, you can see the summary from the commit.

  • you don't need rescaling or coeff modulus for BFV
  • BFV doesn't need modulus switch, that's why you saw that error.
  • the C++ tests weren't compiling because you were using vector instead of vector<int64_t>

I haven't checked the python tests, though.

@comidan
Copy link
Contributor Author

comidan commented Apr 21, 2021

Ok now with the new commit it shows that by running the python tests some of them pass but then it stops at one giving indeed the same error I get locally: ValueError: SEAL: couldn't find context_data from params_id,

I pushed some fixes, you can see the summary from the commit.

  • you don't need rescaling or coeff modulus for BFV
  • BFV doesn't need modulus switch, that's why you saw that error.
  • the C++ tests weren't compiling because you were using vector instead of vector<int64_t>

I haven't checked the python tests, though.

Thank you!

Yes my bad, I indeed removed rescaling and coefficient modulus from various methods but not everywhere, sorry for the confusion.

Ok I'll wait for the tests to run here on GitHub and then I'll further check the python tests for problems, if any.

@comidan
Copy link
Contributor Author

comidan commented Apr 21, 2021

I pushed fixes to the python tests of BFVTensor, now locally they all work! Should also then work everything here in theory.
There were few issues with the shapes passed to numpy randint and some tests thought to be for CKKS polyval which did not make sense here.

Copy link
Member

@bcebere bcebere left a comment

Choose a reason for hiding this comment

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

LGTM! Great work! Thank you for your contribution, @comidan!

I've left just one minor observation, after that we can merge the PR.

public:
using EncryptedTensor<int64_t, shared_ptr<BFVTensor>>::decrypt;
/**
* Create a new BFVTensor from an 1D vector.
Copy link
Member

Choose a reason for hiding this comment

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

The comment here is not correct, the BFVTensor can be created from tensors of any shape

Copy link
Contributor Author

@comidan comidan Apr 21, 2021

Choose a reason for hiding this comment

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

LGTM! Great work! Thank you for your contribution, @comidan!

I've left just one minor observation, after that we can merge the PR.

Thank you for the help!

Sure ok, I've just pushed the new header file with the fixed comment.

* @param[in] input vector.
* @param[in] input vector.
* @param[in] input vector.
*/
Copy link
Member

@bcebere bcebere Apr 21, 2021

Choose a reason for hiding this comment

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

Sorry, but the comments still don't make sense.

Why the

* @param[in] input vector.
* @param[in] input vector.
* @param[in] input vector.

3 times as a vector ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok @bcebere I'll change it accordingly, I just left the same comments which were already present in CKKSTensor.h assuming they were correct.

Should I then change also the ones for CKKSTensor?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please. Thank you so much for improving this!

@bcebere
Copy link
Member

bcebere commented Apr 22, 2021

Thank you for your contribution, @comidan!

@bcebere bcebere merged commit 0e5f119 into OpenMined:master Apr 22, 2021
@comidan
Copy link
Contributor Author

comidan commented Apr 22, 2021

Thank you for your contribution, @comidan!

Thank you for the help in making it!

pierreeliseeflory pushed a commit to pierreeliseeflory/TenSEAL that referenced this pull request Apr 27, 2022
* BFVTensor
* Fix BFVTensor python tests
* Update bfvtensor.h
* Update ckkstensor.h


Co-authored-by: Bogdan Cebere <bogdan.cebere@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants