Skip to content
Open
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
2 changes: 2 additions & 0 deletions include/ctranslate2/devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace ctranslate2 {
void synchronize_device(Device device, int index);
void synchronize_stream(Device device);

void destroy_context(Device device);

class ScopedDeviceSetter {
public:
ScopedDeviceSetter(Device device, int index)
Expand Down
2 changes: 2 additions & 0 deletions include/ctranslate2/replica_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ namespace ctranslate2 {

void finalize() override {
_replica.reset();

destroy_context(_device);
}

private:
Expand Down
7 changes: 6 additions & 1 deletion src/cuda/random.cu
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ namespace ctranslate2 {
curandState* _states;
};

static thread_local std::unique_ptr<ScopedCurandStates<curandStatePhilox4_32_10_t>> states;

curandStatePhilox4_32_10_t* get_curand_states(size_t num_states) {
static thread_local std::unique_ptr<ScopedCurandStates<curandStatePhilox4_32_10_t>> states;
if (!states || num_states > states->num_states())
states = std::make_unique<ScopedCurandStates<curandStatePhilox4_32_10_t>>(num_states);
return states->states();
}

void free_curand_states() {
states.reset();
}

}
}
1 change: 1 addition & 0 deletions src/cuda/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace ctranslate2 {
namespace cuda {

curandStatePhilox4_32_10_t* get_curand_states(size_t num_states);
void free_curand_states();

}
}
12 changes: 12 additions & 0 deletions src/devices.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#ifdef CT2_WITH_CUDA
# include "cuda/utils.h"
# include "cuda/random.h"
#endif
#ifdef CT2_WITH_TENSOR_PARALLEL
# include <unistd.h>
Expand Down Expand Up @@ -118,6 +119,17 @@ namespace ctranslate2 {
(void)device;
#endif
}

void destroy_context(Device device) {
#ifdef CT2_WITH_CUDA
if (device == Device::CUDA) {
cuda::free_curand_states();
}
#else
(void)device;
#endif
}

// Initialize the static member variable
#ifdef CT2_WITH_TENSOR_PARALLEL
std::vector<ncclComm_t*> ScopedMPISetter::_nccl_comms;
Expand Down
Loading