Skip to content

Commit

Permalink
refactor!: Add invariant to GpuIndex
Browse files Browse the repository at this point in the history
This commit does 2 things:

* It adds to GpuIndex the invariant that the index corresponds
  to a valid GPU, to do so, the inner u32 is made private
  and new/try_new method are now used to construct a GpuIndex
  these methods checks that the index is valid
* It makes GpuIndex transparent, allowing to safely cast a *const
  GpuIndex to *const u32, this is to save same copies made to transform
  Vec<GpuIndex> to Vec<u32> that was used to get a *const u32

BREAKING CHANGES: GpuIndex(some_value) is no longer valid and
GpuIndex::new(some_value) / GpuIndex::try_new(some_value) has to be
used
  • Loading branch information
tmontaigu committed Jan 29, 2025
1 parent 57386a1 commit 2481ee9
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 627 deletions.
4 changes: 2 additions & 2 deletions tfhe/benches/core_crypto/ks_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ mod cuda {
SecretRandomGenerator::<DefaultRandomGenerator>::new(seeder.seed());

let gpu_index = 0;
let streams = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

for (name, params) in parameters.iter() {
let lwe_dimension = params.lwe_dimension.unwrap();
Expand Down Expand Up @@ -433,7 +433,7 @@ mod cuda {
SecretRandomGenerator::<DefaultRandomGenerator>::new(seeder.seed());

let gpu_index = 0;
let streams = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

for (name, params) in parameters.iter() {
let lwe_dimension = params.lwe_dimension.unwrap();
Expand Down
8 changes: 4 additions & 4 deletions tfhe/benches/core_crypto/pbs_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ mod cuda {
SecretRandomGenerator::<DefaultRandomGenerator>::new(seeder.seed());

let gpu_index = 0;
let stream = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let stream = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

for (name, params) in parameters.iter() {
// Create the LweSecretKey
Expand Down Expand Up @@ -1022,7 +1022,7 @@ mod cuda {
SecretRandomGenerator::<DefaultRandomGenerator>::new(seeder.seed());

let gpu_index = 0;
let stream = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let stream = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

for (name, params, grouping_factor) in parameters.iter() {
// Create the LweSecretKey
Expand Down Expand Up @@ -1144,7 +1144,7 @@ mod cuda {
SecretRandomGenerator::<DefaultRandomGenerator>::new(seeder.seed());

let gpu_index = 0;
let stream = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let stream = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

for (name, params) in parameters.iter() {
let input_lwe_secret_key = allocate_and_generate_new_binary_lwe_secret_key(
Expand Down Expand Up @@ -1284,7 +1284,7 @@ mod cuda {
SecretRandomGenerator::<DefaultRandomGenerator>::new(seeder.seed());

let gpu_index = 0;
let stream = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let stream = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

for (name, params, grouping_factor) in parameters.iter() {
let input_lwe_secret_key = allocate_and_generate_new_binary_lwe_secret_key(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub unsafe fn cuda_extract_lwe_samples_from_glwe_ciphertext_list_async<Scalar>(
let nth_array: Vec<u32> = vec_nth.iter().map(|x| x.0 as u32).collect_vec();
let gpu_indexes = &streams.gpu_indexes;
unsafe {
let d_nth_array = CudaVec::from_cpu_async(&nth_array, streams, gpu_indexes[0].0);
let d_nth_array = CudaVec::from_cpu_async(&nth_array, streams, gpu_indexes[0].get());
extract_lwe_samples_from_glwe_ciphertext_list_async(
streams,
&mut output_lwe_list.0.d_vec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn glwe_encrypt_sample_extract_decrypt_custom_mod<Scalar: UnsignedTorus + Send +
let delta: Scalar = encoding_with_padding / msg_modulus;

let gpu_index = 0;
let streams = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let streams = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

let mut msgs = vec![];

Expand Down
2 changes: 1 addition & 1 deletion tfhe/src/core_crypto/gpu/algorithms/test/lwe_keyswitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn lwe_encrypt_ks_decrypt_custom_mod<Scalar: UnsignedTorus + CastFrom<usize>>(
let ks_decomp_base_log = params.ks_base_log;
let ks_decomp_level_count = params.ks_level;

let stream = CudaStreams::new_single_gpu(GpuIndex(0));
let stream = CudaStreams::new_single_gpu(GpuIndex::new(0));

let mut rsc = TestResources::new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn lwe_encrypt_multi_bit_pbs_decrypt_custom_mod<
let grouping_factor = params.grouping_factor;

let gpu_index = 0;
let stream = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let stream = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

let mut rsc = TestResources::new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ where
let delta: Scalar = encoding_with_padding / msg_modulus;

let gpu_index = 0;
let stream = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let stream = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

while msg != Scalar::ZERO {
msg = msg.wrapping_sub(Scalar::ONE);
Expand Down Expand Up @@ -152,7 +152,7 @@ where
let delta: Scalar = encoding_with_padding / msg_modulus;

let gpu_index = 0;
let stream = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let stream = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

while msg != Scalar::ZERO {
msg = msg.wrapping_sub(Scalar::ONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn lwe_encrypt_pbs_decrypt<
let decomp_level_count = params.pbs_level;

let gpu_index = 0;
let stream = CudaStreams::new_single_gpu(GpuIndex(gpu_index));
let stream = CudaStreams::new_single_gpu(GpuIndex::new(gpu_index));

let mut rsc = TestResources::new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
let grouping_factor = params.grouping_factor;
let number_of_messages = 1;

let gpu_index = GpuIndex(0);
let gpu_index = GpuIndex::new(0);
let stream = CudaStreams::new_single_gpu(gpu_index);

let modulus_as_f64 = if ciphertext_modulus.is_native_modulus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
let pbs_decomposition_level_count = params.pbs_level;
let number_of_messages = 1;

let gpu_index = GpuIndex(0);
let gpu_index = GpuIndex::new(0);
let stream = CudaStreams::new_single_gpu(gpu_index);

let modulus_as_f64 = if ciphertext_modulus.is_native_modulus() {
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/core_crypto/gpu/entities/lwe_ciphertext_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<T: UnsignedInteger> CudaLweCiphertextList<T> {
first_item.0.d_vec.as_c_ptr(0),
size as u64,
streams.ptr[0],
streams.gpu_indexes[0].0,
streams.gpu_indexes[0].get(),
);
ptr = ptr.wrapping_byte_add(size);
for list in cuda_ciphertexts_list_vec {
Expand All @@ -137,7 +137,7 @@ impl<T: UnsignedInteger> CudaLweCiphertextList<T> {
list.0.d_vec.as_c_ptr(0),
size as u64,
streams.ptr[0],
streams.gpu_indexes[0].0,
streams.gpu_indexes[0].get(),
);
ptr = ptr.wrapping_byte_add(size);
}
Expand Down
Loading

0 comments on commit 2481ee9

Please sign in to comment.