Run on GPU
TFHE-rs now includes a GPU backend, featuring a CUDA implementation for performing integer arithmetics on encrypted data. In what follows, a simple tutorial is introduced: it shows how to update your existing program to use GPU acceleration, or how to start a new one using GPU.
Prerequisites
Importing to your project
To use the TFHE-rs GPU backend
in your project, you first need to add it as a dependency in your Cargo.toml
.
If you are using an x86
machine:
If you are using an ARM
machine:
When running code that uses TFHE-rs
, it is highly recommended to run in release mode with cargo's --release
flag to have the best possible performance
Supported platforms
TFHE-rs GPU backend is supported on Linux (x86, aarch64).
OS | x86 | aarch64 |
---|---|---|
Linux |
|
|
macOS | Unsupported | Unsupported* |
Windows | Unsupported | Unsupported |
A first example
Configuring and creating keys.
In comparison with the CPU example, the only difference lies into the key creation, which is detailed here
Here is a full example (combining the client and server parts):
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()
. From then on, there is no difference between the CPU and the GPU.
Encrypting data
On the client-side, the method to encrypt the data is exactly the same than the CPU one, i.e.:
Computation.
The server must first set its keys up, like in the CPU, with: set_server_key(gpu_key);
. Then, homomorphic computations are done with the same code than the one described here.
Decryption.
Finally, the client gets the decrypted results by computing:
Improving performance.
TFHE-rs includes the possibility to leverage the high number of threads given by a GPU. To do so, the configuration should be updated with
The complete example becomes:
List of available operations
The GPU backend includes the following operations:
name | symbol |
|
|
Neg |
| N/A | |
Add |
| ||
Sub |
| ||
Mul |
| ||
Div |
| ||
Rem |
| ||
Not |
| N/A | |
BitAnd |
| ||
BitOr |
| ||
BitXor |
| ||
Shr |
| ||
Shl |
| ||
Rotate right |
| ||
Rotate left |
| ||
Min |
| ||
Max |
| ||
Greater than |
| ||
Greater or equal than |
| ||
Lower than |
| ||
Lower or equal than |
| ||
Equal |
| ||
Cast (into dest type) |
| N/A | |
Cast (from src type) |
| N/A | |
Ternary operator |
|
The equivalent signed operations are also available.
All operations follow the same syntax than the one described in here.
Benchmarks
All GPU benchmarks presented here were obtained on a single H100 GPU, and rely on the multithreaded PBS algorithm. The cryptographic parameters PARAM_GPU_MULTI_BIT_MESSAGE_2_CARRY_2_GROUP_3_KS_PBS were used. Performance is the following when the inputs of the benchmarked operation are encrypted:
Operation \ Size |
|
|
|
|
|
|
Negation ( | 46 ms | 60 ms | 75 ms | 94 ms | 150 ms | 247 ms |
Add / Sub ( | 46 ms | 60 ms | 75 ms | 94 ms | 150 ms | 247 ms |
Mul ( | 83 ms | 121 ms | 195 ms | 456 ms | 1.35 s | 4.74 s |
Equal / Not Equal ( | 25 ms | 26 ms | 38 ms | 41 ms | 52 ms | 78 ms |
Comparisons ( | 46 ms | 60 ms | 74 ms | 90 ms | 109 ms | 153 ms |
Max / Min ( | 71 ms | 86 ms | 101 ms | 124 ms | 165 ms | 236 ms |
Bitwise operations ( | 11 ms | 12 ms | 13 ms | 15 ms | 23 ms | 32 ms |
Left / Right Shifts ( | 71 ms | 88 ms | 109 ms | 180 ms | 279 ms | 494 ms |
Left / Right Rotations ( | 71 ms | 88 ms | 109 ms | 180 ms | 279 ms | 494 ms |
Performance is the following when the left input of the benchmarked operation is encrypted and the other is a clear scalar of the same size:
Operation \ Size |
|
|
|
|
|
|
Add / Sub ( | 46 ms | 60 ms | 75 ms | 94 ms | 152 ms | 251 ms |
Mul ( | 67 ms | 101 ms | 149 ms | 282 ms | 727 ms | 2.11 s |
Equal / Not Equal ( | 26 ms | 27 ms | 27 ms | 41 ms | 45 ms | 57 ms |
Comparisons ( | 29 ms | 41 ms | 54 ms | 69 ms | 87 ms | 117 ms |
Max / Min ( | 53 ms | 65 ms | 81 ms | 102 ms | 142 ms | 200 ms |
Bitwise operations ( | 11 ms | 13 ms | 13 ms | 15 ms | 23 ms | 32 ms |
Left / Right Shifts ( | 11 ms | 12 ms | 13 ms | 15 ms | 23 ms | 32 ms |
Left / Right Rotations ( | 11 ms | 12 ms | 13 ms | 15 ms | 23 ms | 32 ms |
Last updated