-
Notifications
You must be signed in to change notification settings - Fork 157
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
Working on a tutorial on TFHE-rs <> Concrete ML interoperability: step 1 #1080
Conversation
@@ -0,0 +1,57 @@ | |||
#!/usr/bin/env bash | |||
|
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.
for mac to fix the OMP issues, I'll remove at the end
# Params: users shouldn't change them | ||
LWE_DIM = 909 | ||
GLWE_DIM = 1 | ||
POLY_SIZE = 4096 | ||
PBS_BASE_LOG = 15 | ||
PBS_LEVEL = 2 | ||
MSG_WIDTH = 2 | ||
CARRY_WIDTH = 3 | ||
ENCRYPTION_KEY_CHOICE = tfhers.EncryptionKeyChoice.BIG | ||
LWE_NOISE_DISTR = 0 | ||
GLWE_NOISE_DISTR = 2.168404344971009e-19 | ||
|
||
assert GLWE_DIM == 1, "glwe dim must be 1" |
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.
should we hide this and make it constant to users, if they shouldn't change it?
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.
These aren't constant. You will basically choose TFHE-rs parameters (using whatever method: I don't know what's the strategy there for choosing parameters), then you copy/paste those here. What would be better in the future is to have a TFHE-rs function to export those parameters (as json), then import them in Concrete. This way we will avoid seeing them all around the code.
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.
But our users will use constant TFHE-rs parameters, or not? Is it expected that TFHE-rs users change parameters?
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.
Yes, it's is expected.
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.
Hum, I'd like to discuss that part, I keep it for the weekly. Because, I mean, we have a lot of users who are far from being powerusers, they just want the simplest things.
# Options | ||
# FIXME: explain | ||
FHEUINT_PRECISION = 8 |
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.
to explain what can be modified here
tfhers_params = tfhers.CryptoParams( | ||
lwe_dimension=LWE_DIM, | ||
glwe_dimension=GLWE_DIM, | ||
polynomial_size=POLY_SIZE, | ||
pbs_base_log=PBS_BASE_LOG, | ||
pbs_level=PBS_LEVEL, | ||
lwe_noise_distribution=LWE_NOISE_DISTR, | ||
glwe_noise_distribution=GLWE_NOISE_DISTR, | ||
encryption_key_choice=ENCRYPTION_KEY_CHOICE, | ||
) | ||
tfhers_type = tfhers.TFHERSIntegerType( | ||
is_signed=False, | ||
bit_width=FHEUINT_PRECISION, | ||
carry_width=CARRY_WIDTH, | ||
msg_width=MSG_WIDTH, | ||
params=tfhers_params, | ||
) | ||
tfhers_int = partial(tfhers.TFHERSInteger, tfhers_type) |
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.
to hide to the user if they shouldn't change it?
65a49a7
to
f0cb443
Compare
|
||
from numpy.random import randint | ||
|
||
# FIXME: should we move this to Concrete library directly, hidden to the user |
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.
hide to the user if it's constant?
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.
|
||
return tfhers_params, tfhers_type, tfhers_int | ||
|
||
# FIXME Params: users shouldn't change them, should we hide it |
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.
hide to the user if it's constant?
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.
assert GLWE_DIM == 1, "glwe dim must be 1" | ||
|
||
# Options: the user can change the following | ||
# FIXME: explain FHEUINT_PRECISION, ie can it be changed |
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.
I'll ask about this, it's not clear to me if FHEUINT_PRECISION can change or not
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.
This value can change yes, but we mainly support 8 bits for now
|
||
assert GLWE_DIM == 1, "glwe dim must be 1" | ||
|
||
# Options: the user can change the following |
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.
I've set a single place where users have to apply their changes (as long as they have just 2 inputs in their functions)
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.
return (concrete_x + concrete_y) % 47 | ||
|
||
# The user must specify the range of the TFHE-rs inputs | ||
# FIXME: why can't we set the limit at 256? It's needed for FHEUint8 |
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.
There is an issue here if I try to pick a random 8b byte (not 7b), I'll ask for help
CC @youben11
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.
what's the issue?
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.
input_idx_to_key = {0: buff, 1: buff} | ||
tfhers_bridge.keygen_with_initial_keys(input_idx_to_key_buffer=input_idx_to_key) | ||
|
||
# FIXME: remove the secret key before saving. The secret key can be used for |
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.
I think there is an issue here, we shouldn't save secret key, it's dangerous if it goes into production, I'll fill an issue for the examples/tfhers file
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.
# FIXME: how does it decrypt? we are on the server side, we shouldn't have | ||
# the secret key. I think it's because the secret key is saved in concrete_keyset_path | ||
|
||
# x = circuit.decrypt(tfhers_uint8_x) |
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.
this part shouldn't work, ie shouldn't be able to decrypt, but it does
I did https://github.com/zama-ai/concrete-internal/issues/874
f0cb443
to
a914cbd
Compare
It's not the final step but I would be interested in having a review of the current state, if possible. CC @BourgerieQuentin @youben11 |
Next steps will be about trying to add CML in the loop. |
tfhe = { git = "https://github.com/zama-ai/tfhe-rs.git", rev = "cfb9532f6336c7e8fec754d2dbe2e1195b9c0de7", features = ["integer"] } | ||
|
||
[target.x86_64-unknown-linux-gnu.dependencies] | ||
tfhe = { git = "https://github.com/zama-ai/tfhe-rs.git", rev = "cfb9532f6336c7e8fec754d2dbe2e1195b9c0de7", features = ["integer", "x86_64-unix"] } |
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.
You can update to 0.8.0 now
assert GLWE_DIM == 1, "glwe dim must be 1" | ||
|
||
# Options: the user can change the following | ||
# FIXME: explain FHEUINT_PRECISION, ie can it be changed |
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.
This value can change yes, but we mainly support 8 bits for now
CC @youben11 : I wanted to move to TFHE-rs, but it's on hold for now, since
I have a crash in the |
so updating the CP is for later. |
I have a problem, https://github.com/zama-ai/concrete-internal/issues/885 |
|
||
const BLOCK_PARAMS: ClassicPBSParameters = tfhe::shortint::prelude::PARAM_MESSAGE_2_CARRY_3_KS_PBS; | ||
|
||
fn write_json_to_file<T: Serialize>(value: &T, filename: &str) { |
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.
Now in the hands of @youben11 . We've said with @BourgerieQuentin and @youben11 that the blockers for continuing this PR were:
|
Taking by @youben11 in other PR |
I think it's a bit sad to not have taken what I had done in frontends/concrete-python/examples/tfhers-cml/src/main.rs: having a single .rs with the calls to .py inside was really much clearer to me, and it was a way to have it tested in our CI. This .rs was really done in the way I understood our clients would use the functionality. I would consider doing something in this spirit, if you close this PR. I am available to discuss it more precisely if you want. |
@youben11 wdyt? |
The PR is doing something different than what we are doing in the Concrete CI. For Concrete examples/tests it was natural to work on an example similar to the first one, instead of changing toolings. The goal was to have something working e2e as quick as possible. When we have something working, it might be worth to have a Rust script for clients (if that's what they want), but I don't see it as necessary for now. |
Which tooling did I change?
To me:
|
But sure, I can let you do as you want, the most important thing is that the compatibility works for customers and that they know how to use it simply. |
We are using the tfhers_utils for tests. It's our Rust utility for testing compatibility. It's a single place to manage TFHE-rs versions etc. It would be harder to manage two Rust codebase which does the same thing.
They are in def test_tfhers_example():
path_to_test_script = f"{os.path.dirname(os.path.abspath(__file__))}/../../examples/tfhers/"
test_script_filename = "test.sh"
assert (
os.system(f"cd {path_to_test_script} && sh {test_script_filename}") == 0
), "test script failed"
Yes, but it was different. It's simpler to have two examples using the same tooling and steps, when all the difference resides in the computation they are performing (one is doing some simple scalar operations, while the other is performing ML on tensors). I'm also not against this PR or having something that runs entirely in Rust. This can be merged separately in a place where we see fit. However, It was just easier for me to start from the example we had than updating your PR while you where away. |
But yes, I let you do as you want, and we'll see how it is when it's ready and if it's simple enough for the customers. |
Like I said, I don't mind pushing this PR to have complete standalone ML example. It was just not the preferable way (at least to me) to have something working e2e in the CI as quickly as possible. The focus was to have a green test, not an example. |
Sure. Then, before the final release to the customer, we'll have maybe an example in another form |
Current state: I have something which does the job for Concrete.
I think it would be nice to review it and maybe merge (as a WIP). In particular, I have seen a few problems maybe (https://github.com/zama-ai/concrete-internal/issues/874) and few things I'd like to clarify (see the FIXME's). Maybe we can hide more to the users, for things which are not changeable by them.