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

feat: Add tfhe-rs compatibility for model inputs /outputs #997

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

andrei-stoian-zama
Copy link
Collaborator

Adds the ciphertext_format parameter to compile calls.

Needs new fhext & new compiler to run.

@cla-bot cla-bot bot added the cla-signed label Jan 16, 2025
@andrei-stoian-zama andrei-stoian-zama changed the title chore: keygen and encrypt feat: Add tfhe-rs compatibility for model inputs /outputs Jan 16, 2025
@andrei-stoian-zama andrei-stoian-zama marked this pull request as ready for review January 22, 2025 17:39
@andrei-stoian-zama andrei-stoian-zama requested a review from a team as a code owner January 22, 2025 17:39
@@ -506,9 +514,33 @@ def _get_module_to_compile(self) -> Union[Compiler, QuantizedModule]:
Union[Compiler, QuantizedModule]: The module instance to compile.
"""

def _get_tfhres_module_to_compile(self, inputset):
Copy link
Contributor

Choose a reason for hiding this comment

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

typo? tfhres -> tfhers

# For now, use the virtual library when simulating
# circuits that use CRT encoding because the official simulation is too slow
# FIXME: https://github.com/zama-ai/concrete-ml-internal/issues/4391
if USE_OLD_VL or is_crt_encoding:
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't know USE_OLD_VL what still there

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's disabled but still there

Copy link
Contributor

Choose a reason for hiding this comment

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

is there a reason to keep it? do you still use it? (I guess it's not tested so certainly, soon, it's no more working, anyway)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Old vl is simply CP graph execution, so it is tested in Concrete. but you're right in the sense that we could easily remove it - we kept it thinking it might be useful at some point, but it's not anymore

Copy link
Contributor

Choose a reason for hiding this comment

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

it is tested in Concrete
I don't know if they still test it. At some point, they'll certainly remove it as well

model.compile(x)

# Check correctness with TFHE-rs inputs/outputs
assert numpy.all(y_pred_tfhers == y_pred_disable)
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't you also test vs purely concrete results?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fhe results (with tfhers inputs) should be equal disable for default p_error. Other tests check that concrete fhe and disable are also equal to disable. using fhe=disable here makes this test faster, it's already quite a slow one

Copy link
Contributor

Choose a reason for hiding this comment

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

I have not understood your answer.

My question is: do we test that results with results "with Concrete format" and "with TFHE-rs format" are the same? (either in simulate or in FHE). If we have that + existing tests that results in TFHE-rs are equivalent in simulate and fhe, that's fine with me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

we compare tfhe-rs inputs vs disable in this test and we compare concrete inputs vs simulate and disable in other tests.

def compile(
self,
X: Data,
ciphertext_format: CiphertextFormat = CiphertextFormat.CONCRETE,
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you allow simple string like we do for fhe arg? e.g. I would like to call compile with model.compile(X, ciphertext_format="tfhe-rs")

Comment on lines -659 to -663
q_y_pred_list = []
for q_X_i in q_X:
# Expected encrypt_run_decrypt output shape is (1, n_features) while q_X_i
# is of shape (n_features,)
q_X_i = numpy.expand_dims(q_X_i, 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

wasn't this necessary?


# Check that we can first compile to Concrete, then to
# TFHE-rs input/outputs then to concrete again
model.compile(x)
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could add model.predict with concrete maybe and add this to the comparisons. We expect them to be equal to disable and to execute with tfhe-rs input

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, as Benoit asks for it too I'll add it

@andrei-stoian-zama andrei-stoian-zama force-pushed the feat/add_tfhers_ciphertext_compilation branch from a29f818 to 81cad15 Compare January 29, 2025 07:09
Copy link

⚠️ This PR contains unsigned commits. To get your PR merged, please sign those commits (git rebase --exec 'git commit -S --amend --no-edit -n' @{upstream}) and force push them to this branch (git push --force-with-lease).

If you're new to commit signing, there are different ways to set it up:

Sign commits with gpg

Follow the steps below to set up commit signing with gpg:

  1. Generate a GPG key
  2. Add the GPG key to your GitHub account
  3. Configure git to use your GPG key for commit signing
Sign commits with ssh-agent

Follow the steps below to set up commit signing with ssh-agent:

  1. Generate an SSH key and add it to ssh-agent
  2. Add the SSH key to your GitHub account
  3. Configure git to use your SSH key for commit signing
Sign commits with 1Password

You can also sign commits using 1Password, which lets you sign commits with biometrics without the signing key leaving the local 1Password process.

Learn how to use 1Password to sign your commits.

Watch the demo

@andrei-stoian-zama andrei-stoian-zama force-pushed the feat/add_tfhers_ciphertext_compilation branch from 28050f6 to 7f2131f Compare January 29, 2025 09:12
Copy link

Coverage failed ❌

Coverage details

---------- coverage: platform linux, python 3.8.18-final-0 -----------
Name                              Stmts   Miss  Cover   Missing
---------------------------------------------------------------
src/concrete/ml/sklearn/base.py     781      1    99%   801
src/concrete/ml/sklearn/glm.py      160     83    48%   82-108, 114-138, 326-353, 359-383
---------------------------------------------------------------
TOTAL                              8619     84    99%

61 files skipped due to complete coverage.

Copy link

⚠️ Known flaky tests have been rerun ⚠️

One or several tests initially failed but were identified as known flaky. tests. Therefore, they have been rerun and passed. See below for more details.

Failed tests details

Known flaky tests that initially failed:

  • tests/torch/test_brevitas_qat.py::test_brevitas_tinymnist_cnn[True-True-3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants