Skip to content

Commit

Permalink
test(frontend): test tfhers-ml example
Browse files Browse the repository at this point in the history
  • Loading branch information
youben11 committed Nov 18, 2024
1 parent b38d49c commit c91b6b9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
22 changes: 3 additions & 19 deletions frontends/concrete-python/examples/tfhers-ml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions frontends/concrete-python/examples/tfhers-ml/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions frontends/concrete-python/tests/execution/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c91b6b9

Please sign in to comment.