From c91b6b9064e1d88dc9cc019e51aed2efe8cf4593 Mon Sep 17 00:00:00 2001 From: youben11 Date: Mon, 18 Nov 2024 12:26:19 +0100 Subject: [PATCH] test(frontend): test tfhers-ml example --- .../examples/tfhers-ml/README.md | 22 +++---------------- .../examples/tfhers-ml/example.py | 8 +++---- .../tests/execution/test_examples.py | 12 +++++++--- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/frontends/concrete-python/examples/tfhers-ml/README.md b/frontends/concrete-python/examples/tfhers-ml/README.md index 9d4971dce9..2b2ff38f6e 100644 --- a/frontends/concrete-python/examples/tfhers-ml/README.md +++ b/frontends/concrete-python/examples/tfhers-ml/README.md @@ -53,35 +53,19 @@ python example.py keygen -s $TDIR/tfhers_sk -o $TDIR/concrete_sk -k $TDIR/concre ## Encrypt in TFHE-rs ```sh -../../tests/tfhers-utils/target/release/tfhers_utils encrypt-with-key --value 162 --ciphertext $TDIR/tfhers_ct_1 --client-key $TDIR/tfhers_client_key -../../tests/tfhers-utils/target/release/tfhers_utils encrypt-with-key --value 73 --ciphertext $TDIR/tfhers_ct_2 --client-key $TDIR/tfhers_client_key -``` - -{% hint style="info" %} - -If you have tensor inputs, then you can encrypt by passing your flat tensor in `--value`. Concrete will take care of reshaping the values to the corresponding shape. For example `--value=1,2,3,4` can represent a 2 by 2 tensor, or a flat vector of 4 values. - -{% endhint %} - -## Compute in TFHE-rs - -```sh -# encrypt value to add first -../../tests/tfhers-utils/target/release/tfhers_utils encrypt-with-key --value 9 --ciphertext $TDIR/tfhers_ct_inc --client-key $TDIR/tfhers_client_key -# add two ciphertexts -../../tests/tfhers-utils/target/release/tfhers_utils add --server-key $TDIR/tfhers_server_key --cts $TDIR/tfhers_ct_2 $TDIR/tfhers_ct_inc --output-ct $TDIR/tfhers_ct_2 +../../tests/tfhers-utils/target/release/tfhers_utils encrypt-with-key --value=1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 --ciphertext $TDIR/tfhers_ct --client-key $TDIR/tfhers_client_key ``` ## Run in Concrete ```sh -python example.py run -k $TDIR/concrete_keyset -c1 $TDIR/tfhers_ct_1 -c2 $TDIR/tfhers_ct_2 -o $TDIR/tfhers_ct_out +python example.py run -k $TDIR/concrete_keyset -c $TDIR/tfhers_ct -o $TDIR/tfhers_ct_out ``` ## Decrypt in TFHE-rs ```sh -../../tests/tfhers-utils/target/release/tfhers_utils decrypt-with-key --ciphertext $TDIR/tfhers_ct_out --client-key $TDIR/tfhers_client_key +../../tests/tfhers-utils/target/release/tfhers_utils decrypt-with-key --tensor --ciphertext $TDIR/tfhers_ct_out --client-key $TDIR/tfhers_client_key ``` ## Clean tmpdir diff --git a/frontends/concrete-python/examples/tfhers-ml/example.py b/frontends/concrete-python/examples/tfhers-ml/example.py index 4a87a9a024..d9355b9f50 100644 --- a/frontends/concrete-python/examples/tfhers-ml/example.py +++ b/frontends/concrete-python/examples/tfhers-ml/example.py @@ -68,7 +68,7 @@ def ml_inference(q_X: np.ndarray) -> np.ndarray: y_pred = q_X @ q_weights - weight_quantizer_zero_point * np.sum(q_X, axis=1, keepdims=True) y_pred += q_bias y_pred = fhe.round_bit_pattern(y_pred, rounder) - y_pred = (y_pred >> rounder.lsbs_to_remove) + y_pred = y_pred >> rounder.lsbs_to_remove return y_pred @@ -166,10 +166,10 @@ def keygen(output_secret_key: str, secret_key: str, concrete_keyset_path: str): @cli.command() -@click.option("-c1", "--rust-ct-1", type=str, required=True) +@click.option("-c", "--rust-ct", type=str, required=True) @click.option("-o", "--output-rust-ct", type=str, required=False) @click.option("-k", "--concrete-keyset-path", type=str, required=True) -def run(rust_ct_1: str, output_rust_ct: str, concrete_keyset_path: str): +def run(rust_ct: str, output_rust_ct: str, concrete_keyset_path: str): """Run circuit""" circuit, tfhers_bridge = ccompilee() @@ -179,7 +179,7 @@ def run(rust_ct_1: str, output_rust_ct: str, concrete_keyset_path: str): circuit.client.keys.load(concrete_keyset_path) # read tfhers int from file - with open(rust_ct_1, "rb") as f: + with open(rust_ct, "rb") as f: buff = f.read() # import fheuint8 and get its description tfhers_uint8_x = tfhers_bridge.import_value(buff, input_idx=0) diff --git a/frontends/concrete-python/tests/execution/test_examples.py b/frontends/concrete-python/tests/execution/test_examples.py index 37e9493758..fbc0beed68 100644 --- a/frontends/concrete-python/tests/execution/test_examples.py +++ b/frontends/concrete-python/tests/execution/test_examples.py @@ -408,9 +408,15 @@ def test_game_of_life(implementation, dimension, sample_input_output_pairs, help def test_tfhers_example(): - path_to_test_script = tfhers_utils = ( - f"{os.path.dirname(os.path.abspath(__file__))}/../../examples/tfhers/" - ) + 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" + + +def test_tfhers_ml_example(): + path_to_test_script = f"{os.path.dirname(os.path.abspath(__file__))}/../../examples/tfhers-ml/" test_script_filename = "test.sh" assert ( os.system(f"cd {path_to_test_script} && sh {test_script_filename}") == 0