Skip to content

Commit

Permalink
chore: review
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrery committed May 3, 2024
1 parent 3798728 commit b6d5c85
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions docs/guides/client_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,51 @@ The `FHEModelDev`, `FHEModelClient`, and `FHEModelServer` classes in the `concre

### Example Usage

<!--pytest-codeblocks:skip-->

```python
# Development machine
dev = FHEModelDev(path_dir='path/to/save', model=my_trained_model)
from concrete.ml.sklearn import DecisionTreeClassifier
from concrete.ml.deployment import FHEModelDev, FHEModelClient, FHEModelServer
import numpy as np

# Define the directory for FHE client/server files
fhe_directory = '/tmp/fhe_client_server_files/'

# Initialize the Decision Tree model
model = DecisionTreeClassifier()

# Generate some random data for training
X = np.random.rand(100, 20)
y = np.random.randint(0, 2, size=100)

# Train and compile the model
model.fit(X, y)
model.compile(X)

# Setup the development environment
dev = FHEModelDev(path_dir=fhe_directory, model=model)
dev.save()

# Client machine
client = FHEModelClient(path_dir='path/to/client/zip', key_dir='path/to/keys')
client.generate_private_and_evaluation_keys()
serialized_evaluation_keys = client.get_serialized_evaluation_keys()
# The client encrypts the data to be sent to the server for processing.
encrypted_data = client.quantize_encrypt_serialize(my_data)
# Setup the client
client = FHEModelClient(path_dir=fhe_directory, key_dir="/tmp/keys_client")
serialized_evaluation_keys = client.generate_private_and_evaluation_keys()

# Client pre-processes new data
X_new = np.random.rand(1, 20)
encrypted_data = client.quantize_encrypt_serialize(X_new)

# Server machine
server = FHEModelServer(path_dir='path/to/server/zip')
# Setup the server
server = FHEModelServer(path_dir=fhe_directory)
server.load()

# Run the FHE execution on the encrypted data.
# Server processes the encrypted data
encrypted_result = server.run(encrypted_data, serialized_evaluation_keys)

# Back to Client machine
# The client decrypts the result.
# Client decrypts the result
result = client.deserialize_decrypt_dequantize(encrypted_result)
```

> **Data Transfer Overview:**
>
> - **From Client to Server:** `serialized_evaluation_keys`, `encrypted_data`.
> - **From Client to Server:** `serialized_evaluation_keys` (once), `encrypted_data`.
> - **From Server to Client:** `encrypted_result`.
These objects are serialized into bytes to streamline the data transfer between the client and server.
Expand Down

0 comments on commit b6d5c85

Please sign in to comment.