Skip to content

Commit

Permalink
chore(gpu): replace asserts with panic
Browse files Browse the repository at this point in the history
  • Loading branch information
agnesLeroy committed Feb 25, 2025
1 parent 0216e64 commit 8b3d0d0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "integer/radix_ciphertext.h"
#include "keyswitch.h"
#include "pbs/programmable_bootstrap.cuh"
#include <cassert>
#include <cmath>
#include <functional>

Expand Down Expand Up @@ -295,7 +294,8 @@ template <typename Torus> struct int_radix_lut {
std::memcpy(gpu_indexes, input_gpu_indexes, gpu_count * sizeof(uint32_t));

// base lut object should have bigger or equal memory than current one
assert(num_radix_blocks <= base_lut_object->num_blocks);
if (num_radix_blocks > base_lut_object->num_blocks)
PANIC("Cuda error: lut does not have enough blocks")
// pbs
buffer = base_lut_object->buffer;
// Keyswitch
Expand Down Expand Up @@ -477,7 +477,8 @@ template <typename Torus> struct int_radix_lut {
auto lut = lut_vec[gpu_index];
size_t lut_size = (params.glwe_dimension + 1) * params.polynomial_size;

assert(lut != nullptr);
if (lut == nullptr)
PANIC("Cuda error: invalid lut pointer")
return &lut[idx * lut_size];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define CNCRT_FAST_KS_CUH

#undef NDEBUG
#include <assert.h>

#include "device.h"
#include "gadget.cuh"
Expand Down
35 changes: 24 additions & 11 deletions backends/tfhe-cuda-backend/cuda/src/integer/div_rem.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ template <typename Torus> struct lwe_ciphertext_list {

// return block with `index`
Torus *get_block(size_t index) {
assert(index < len);
if (index >= len)
PANIC("Cuda error: out of bound index")
return &data[index * big_lwe_size];
}

Expand All @@ -103,15 +104,17 @@ template <typename Torus> struct lwe_ciphertext_list {
void pop() {
if (len > 0)
len--;
else
assert(len > 0);
else if (len <= 0)
PANIC("Cuda error: invalid length")
}

// insert ciphertext at index `ind`
void insert(size_t ind, Torus *ciphertext_block, cudaStream_t stream,
uint32_t gpu_index) {
assert(ind <= len);
assert(len < max_blocks);
if (ind > len)
PANIC("Cuda error: invalid index")
if (len >= max_blocks)
PANIC("Cuda error: invalid length")

size_t insert_offset = ind * big_lwe_size;

Expand All @@ -129,7 +132,8 @@ template <typename Torus> struct lwe_ciphertext_list {

// push ciphertext at the end of `data`
void push(Torus *ciphertext_block, cudaStream_t stream, uint32_t gpu_index) {
assert(len < max_blocks);
if (len >= max_blocks)
PANIC("Cuda error: invalid length")

size_t offset = len * big_lwe_size;
cuda_memcpy_async_gpu_to_gpu(&data[offset], ciphertext_block,
Expand All @@ -140,7 +144,8 @@ template <typename Torus> struct lwe_ciphertext_list {
// duplicate ciphertext into `number_of_blocks` ciphertexts
void fill_with_same_ciphertext(Torus *ciphertext, size_t number_of_blocks,
cudaStream_t stream, uint32_t gpu_index) {
assert(number_of_blocks <= max_blocks);
if (number_of_blocks > max_blocks)
PANIC("Cuda error: invalid number of blocks")

for (size_t i = 0; i < number_of_blocks; i++) {
Torus *dest = &data[i * big_lwe_size];
Expand Down Expand Up @@ -414,7 +419,9 @@ __host__ void host_unsigned_integer_div_rem_kb(
merged_interesting_remainder, 0, merged_interesting_remainder.len - 1,
streams[0], gpu_indexes[0]);

assert(merged_interesting_remainder.len == interesting_divisor.len);
if (merged_interesting_remainder.len != interesting_divisor.len)
PANIC("Cuda error: merged interesting remainder and interesting divisor "
"should have the same number of blocks")

// `new_remainder` is not initialized yet, so need to set length
new_remainder.len = merged_interesting_remainder.len;
Expand Down Expand Up @@ -575,16 +582,22 @@ __host__ void host_unsigned_integer_div_rem_kb(
cuda_synchronize_stream(mem_ptr->sub_streams_3[j], gpu_indexes[j]);
}

assert(first_trivial_block == cleaned_merged_interesting_remainder.len);
assert(first_trivial_block == new_remainder.len);
if (first_trivial_block != cleaned_merged_interesting_remainder.len)
PANIC("Cuda error: first_trivial_block should be equal to "
"clean_merged_interesting_remainder num blocks")
if (first_trivial_block != new_remainder.len)
PANIC("Cuda error: first_trivial_block should be equal to new_remainder "
"num blocks")

remainder1.copy_from(cleaned_merged_interesting_remainder, 0,
first_trivial_block - 1, streams[0], gpu_indexes[0]);
remainder2.copy_from(new_remainder, 0, first_trivial_block - 1, streams[0],
gpu_indexes[0]);
}

assert(remainder1.len == remainder2.len);
if (remainder1.len != remainder2.len)
PANIC("Cuda error: remainder1 and remainder2 should have the same number "
"of blocks")

// Clean the quotient and remainder
// as even though they have no carries, they are not at nominal noise level
Expand Down
9 changes: 6 additions & 3 deletions backends/tfhe-cuda-backend/cuda/src/integer/integer.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,8 @@ uint64_t generate_many_lookup_table(

size_t fn_counts = functions.size();

assert(fn_counts <= modulus_sup / 2);
if (fn_counts > modulus_sup / 2)
PANIC("Cuda error: invalid number of functions")

// Space used for each sub lut
uint32_t single_function_sub_lut_size = (modulus_sup / fn_counts) * box_size;
Expand Down Expand Up @@ -2749,7 +2750,8 @@ void legacy_host_single_borrow_propagate(
auto lut_stride = mem->lut_stride;
auto num_many_lut = mem->num_many_lut;

assert(mem->num_groups >= num_groups);
if (mem->num_groups < num_groups)
PANIC("Cuda error: inconsistency in num_groups")
if (uses_input_borrow == 1) {
legacy_host_unchecked_sub_with_correcting_term<Torus>(
streams[0], gpu_indexes[0], lhsrhs_array, lhsrhs_array, input_borrow,
Expand Down Expand Up @@ -2875,7 +2877,8 @@ void host_single_borrow_propagate(
auto lut_stride = mem->lut_stride;
auto num_many_lut = mem->num_many_lut;

assert(mem->num_groups >= num_groups);
if (mem->num_groups < num_groups)
PANIC("Cuda error: inconsistency in num_groups")
if (uses_input_borrow == 1) {
host_unchecked_sub_with_correcting_term<Torus>(
streams[0], gpu_indexes[0], lwe_array, lwe_array, input_borrow, 1,
Expand Down
4 changes: 2 additions & 2 deletions backends/tfhe-cuda-backend/cuda/src/integer/scalar_shifts.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ __host__ void legacy_host_integer_radix_arithmetic_scalar_shift_kb_inplace(
lut_bivariate->params.message_modulus);
}
// Since our CPU threads will be working on different streams we shall
// assert the work in the main stream is completed
// Ensure the work in the main stream is completed
for (uint j = 0; j < gpu_count; j++) {
cuda_synchronize_stream(streams[j], gpu_indexes[j]);
}
Expand Down Expand Up @@ -419,7 +419,7 @@ __host__ void host_integer_radix_arithmetic_scalar_shift_kb_inplace(
lut_bivariate->params.message_modulus);
}
// Since our CPU threads will be working on different streams we shall
// assert the work in the main stream is completed
// Ensure the work in the main stream is completed
for (uint j = 0; j < gpu_count; j++) {
cuda_synchronize_stream(streams[j], gpu_indexes[j]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef GPU_BOOTSTRAP_COMMON_CUH
#define GPU_BOOTSTRAP_COMMON_CUH

#include <cassert>
#include <cstdint>
#include <cstdio>

Expand Down

0 comments on commit 8b3d0d0

Please sign in to comment.