Skip to content
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

refactor(gpu): configure GPU parameters automatically to multi-bit #1447

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 4 additions & 44 deletions tfhe/docs/guides/run_on_gpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ fn main() {
}
```

Beware that when the GPU feature is activated, when calling: `let config = ConfigBuilder::default().build();`,
the cryptographic parameters differ from the CPU ones, used when the GPU feature is not activated.
Indeed, TFHE-rs uses dedicated parameters for the GPU in order to achieve better performance.

### Setting the keys

The configuration of the key is different from the CPU. More precisely, if both client and server keys are still generated by the client (which is assumed to run on a CPU), the server key has then to be decompressed by the server to be converted into the right format. To do so, the server should run this function: `decompressed_to_gpu()`.
Expand Down Expand Up @@ -128,50 +132,6 @@ Finally, the client decrypts the results using:
let decrypted_result: u8 = result.decrypt(&client_key);
```

### Improving performance

**TFHE-rs** allows to leverage the high number of threads given by a GPU. To maximize the number of GPU threads, update your configuration accordingly:

```Rust
let config = ConfigBuilder::with_custom_parameters(PARAM_GPU_MULTI_BIT_MESSAGE_2_CARRY_2_GROUP_3_KS_PBS).build();
```

Here's the complete example:

```rust
use tfhe::{ConfigBuilder, set_server_key, FheUint8, ClientKey, CompressedServerKey};
use tfhe::prelude::*;
use tfhe::shortint::parameters::PARAM_GPU_MULTI_BIT_MESSAGE_2_CARRY_2_GROUP_3_KS_PBS;

fn main() {

let config = ConfigBuilder::with_custom_parameters(PARAM_GPU_MULTI_BIT_MESSAGE_2_CARRY_2_GROUP_3_KS_PBS).build();

let client_key= ClientKey::generate(config);
let compressed_server_key = CompressedServerKey::new(&client_key);

let gpu_key = compressed_server_key.decompress_to_gpu();

let clear_a = 27u8;
let clear_b = 128u8;

let a = FheUint8::encrypt(clear_a, &client_key);
let b = FheUint8::encrypt(clear_b, &client_key);

//Server-side

set_server_key(gpu_key);
let result = a + b;

//Client-side
let decrypted_result: u8 = result.decrypt(&client_key);

let clear_result = clear_a + clear_b;

assert_eq!(decrypted_result, clear_result);
}
```

## List of available operations

The GPU backend includes the following operations for both signed and unsigned encrypted integers:
Expand Down
4 changes: 4 additions & 0 deletions tfhe/src/high_level_api/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ impl Config {
///
/// The configuration is needed to select parameters you wish to use for these types
/// (whether it is the default parameters or some custom parameters).
/// The default parameters are specialized for GPU execution
/// in case the gpu feature is activated.
#[derive(Clone)]
pub struct ConfigBuilder {
config: Config,
Expand All @@ -43,6 +45,8 @@ impl ConfigBuilder {
}

/// Use default parameters with big encryption
/// The returned parameters are specialized for the GPU
/// in case the gpu feature is activated
///
/// For more information see [crate::core_crypto::prelude::PBSOrder::KeyswitchBootstrap]
pub fn default_with_big_encryption() -> Self {
Expand Down
8 changes: 7 additions & 1 deletion tfhe/src/high_level_api/keys/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ impl IntegerConfig {
}

pub(in crate::high_level_api) fn default_big() -> Self {
#[cfg(not(feature = "gpu"))]
let params = crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS.into();
#[cfg(feature = "gpu")]
let params =
crate::shortint::parameters::PARAM_GPU_MULTI_BIT_MESSAGE_2_CARRY_2_GROUP_3_KS_PBS
.into();
Self {
block_parameters: crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS.into(),
block_parameters: params,
dedicated_compact_public_key_parameters: None,
compression_parameters: None,
}
Expand Down
Loading