Skip to content

Commit

Permalink
dd
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrery committed Dec 9, 2024
1 parent 8476425 commit db100fa
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ tmp_directory_for_cml_tests

# Temp file
frontends/concrete-python/examples/sha1/tmp_sha1_test_file.txt

.venv/
4 changes: 2 additions & 2 deletions frontends/concrete-python/examples/tfhers-ml/compute_error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import click

import numpy as np
@click.command()
@click.option('--plaintext-file', '-p', required=True, help='Path to the rescaled plaintext values file.')
@click.option('--quantized-predictions-file', '-q', required=True, help='Path to the test_values.json file containing quantized predictions.')
Expand Down Expand Up @@ -33,7 +33,7 @@ def compute_error(plaintext_file, quantized_predictions_file):
print(f"output: {a}, expected: {b}")
if a != b:
num_differences += 1
error_in_units = round((a - b) / (1 << 10))
error_in_units = np.rint((a - b) / (1 << 10))
errors.append((i, error_in_units))

print("Number of differing values: {}".format(num_differences))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
echo "Setting up working directory..."
start_time=$(date +%s)

mkdir -p "$TDIR"
echo "Directory created at: $TDIR"

end_time=$(date +%s)
echo "Setup completed in $(($end_time - $start_time)) seconds."
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -e # Exit on any error
echo "Building TFHE-rs utilities..."
start_time=$(date +%s)

cd ../../../tests/tfhers-utils/ || exit 1
make build || exit 1
cd - || exit 1

end_time=$(date +%s)
echo "TFHE-rs utilities built in $(($end_time - $start_time)) seconds."
60 changes: 60 additions & 0 deletions frontends/concrete-python/examples/tfhers-ml/demo/03_keygen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash
set -e # Exit on any error

# Function to generate keys starting from Concrete
generate_concrete_keys() {
if [[ -f $TDIR/concrete_sk && -f $TDIR/concrete_keyset && -f $TDIR/tfhers_sk && -f $TDIR/tfhers_client_key && -f $TDIR/tfhers_server_key ]]; then
echo "Keys already exist. Skipping key generation for Concrete."
return
fi
echo "Generating secret key in Concrete..."
start_time=$(date +%s)

python ../example.py keygen -o $TDIR/concrete_sk -k $TDIR/concrete_keyset || exit 1
../../../tests/tfhers-utils/target/release/tfhers_utils keygen \
--lwe-sk $TDIR/concrete_sk \
--output-lwe-sk $TDIR/tfhers_sk \
-c $TDIR/tfhers_client_key \
-s $TDIR/tfhers_server_key || exit 1

end_time=$(date +%s)
echo "Key generation (Concrete) completed in $(($end_time - $start_time)) seconds."
}

# Function to generate keys starting from TFHE-rs
generate_tfhers_keys() {
if [[ -f $TDIR/tfhers_sk && -f $TDIR/tfhers_client_key && -f $TDIR/tfhers_server_key && -f $TDIR/concrete_sk && -f $TDIR/concrete_keyset ]]; then
echo "Keys already exist. Skipping key generation for TFHE-rs."
return
fi
echo "Generating secret key in TFHE-rs..."
start_time=$(date +%s)

../../../tests/tfhers-utils/target/release/tfhers_utils keygen \
--output-lwe-sk $TDIR/tfhers_sk \
-c $TDIR/tfhers_client_key \
-s $TDIR/tfhers_server_key || exit 1
python ../example.py keygen -s $TDIR/tfhers_sk -o $TDIR/concrete_sk -k $TDIR/concrete_keyset || exit 1

end_time=$(date +%s)
echo "Key generation (TFHE-rs) completed in $(($end_time - $start_time)) seconds."
}

# Ask user which method to use
echo "Choose key generation method:"
echo "1) Generate keys starting from Concrete"
echo "2) Generate keys starting from TFHE-rs"
read -p "Enter choice (1 or 2): " choice

case $choice in
1)
generate_concrete_keys
;;
2)
generate_tfhers_keys
;;
*)
echo "Invalid choice. Exiting."
exit 1
;;
esac
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e # Exit on any error
echo "Quantizing input values..."

../../../tests/tfhers-utils/target/release/tfhers_utils quantize \
--value=5.1,3.5,1.4,0.2,4.9,3,1.4,0.2,4.7,3.2,1.3,0.2,4.6,3.1,1.5,0.2,5,3.6,1.4,0.2 \
--config ../input_quantizer.json \
-o $TDIR/quantized_values || exit 1

echo "Quantized values saved to: $TDIR/quantized_values"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -e # Exit on any error
echo "Encrypting quantized values with TFHE-rs..."

../../../tests/tfhers-utils/target/release/tfhers_utils encrypt-with-key \
--signed \
--value=$(cat $TDIR/quantized_values) \
--ciphertext $TDIR/tfhers_ct \
--client-key $TDIR/tfhers_client_key || exit 1

echo "Ciphertext saved to: $TDIR/tfhers_ct"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e # Exit on any error
echo "Running computation in Concrete..."
start_time=$(date +%s)

python ../example.py run -k $TDIR/concrete_keyset -c $TDIR/tfhers_ct -o $TDIR/tfhers_ct_out || exit 1

echo "Computation output saved to: $TDIR/tfhers_ct_out"
end_time=$(date +%s)
echo "Computation completed in $(($end_time - $start_time)) seconds."
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -e # Exit on any error
echo "Decrypting output with TFHE-rs..."

../../../tests/tfhers-utils/target/release/tfhers_utils decrypt-with-key \
--tensor \
--signed \
--ciphertext $TDIR/tfhers_ct_out \
--client-key $TDIR/tfhers_client_key \
--plaintext $TDIR/result_plaintext || exit 1

echo "Decrypted plaintext saved to: $TDIR/result_plaintext"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e # Exit on any error
echo "Rescaling output values..."

rescaled_values=$(python -c "print(','.join(map(lambda x: str(int(x) << 10), [$(cat $TDIR/result_plaintext)])))" || exit 1)
echo "$rescaled_values" > $TDIR/rescaled_plaintext || exit 1

echo "Rescaled plaintext saved to: $TDIR/rescaled_plaintext"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -e # Exit on any error
echo "Dequantizing rescaled values and print expected values..."

../../../tests/tfhers-utils/target/release/tfhers_utils dequantize \
--value=$(cat $TDIR/rescaled_plaintext) \
--shape=5,3 \
--config ../output_quantizer.json || exit 1

echo
echo "Expected values:"
jq -r '.dequantized_predictions[] | @csv' ../test_values.json
Empty file.
37 changes: 37 additions & 0 deletions frontends/concrete-python/examples/tfhers-ml/demo/run_demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
set -e # Exit on any error

export TDIR="./my_temp_dir"

# Function to execute a step and check its result
run_step() {
local step=$1
local description=$2

echo "================================================================"
echo "$description"
read -r

# Source the script instead of running it in a subshell
source "./${step}_"*.sh
if [ $? -ne 0 ]; then
echo "Step $step failed! Aborting..."
exit 1
fi
echo "================================================================"
echo
}

# Main demo sequence
echo "Starting demo sequence..."
echo

# run_step "01" "Setup temporary directory"
# run_step "02" "Build TFHE-rs utilities"
run_step "03" "Generate keys"
run_step "04" "Quantize values"
run_step "05" "Encrypt with TFHE-rs"
run_step "06" "Run computation in Concrete"
run_step "07" "Decrypt with TFHE-rs"
run_step "08" "Rescale output"
run_step "09" "Dequantize values"
8 changes: 3 additions & 5 deletions frontends/concrete-python/examples/tfhers-ml/example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from functools import partial
from pathlib import Path

import click
import numpy as np
Expand All @@ -10,7 +11,7 @@
### Options ###########################
# These parameters were saved by running the tfhers_utils utility:
# tfhers_utils save-params tfhers_params.json
TFHERS_PARAMS_FILE = "tfhers_params.json"
TFHERS_PARAMS_FILE = Path(__file__).parent / "tfhers_params.json"
FHEUINT_PRECISION = 8
IS_SIGNED = True
#######################################
Expand Down Expand Up @@ -88,10 +89,7 @@ def ccompilee():
# Add the auto-adjustment before compilation
fhe.AutoRounder.adjust(compute, inputset)

# Print the number of bits rounded
print(f"lsbs_to_remove: {rounder.lsbs_to_remove}")

circuit = compiler.compile(inputset, show_graph=True, show_mlir=True)
circuit = compiler.compile(inputset, show_graph=False, show_mlir=False)

tfhers_bridge = tfhers.new_bridge(circuit=circuit)
return circuit, tfhers_bridge
Expand Down

0 comments on commit db100fa

Please sign in to comment.