Skip to content

Commit 1e621a5

Browse files
authored
fix: resolve issue with inability to correctly specify non-zero GPUs in multi-GPU systems (#404)
* Fix: Correctly specify non-zero GPUs in multi-GPU environments This commit resolves an issue where the Nunchaku model could not be correctly initialized and run on a user-specified non-zero GPU in multi-GPU systems. Key changes include: - Using CUDADeviceContext in the FluxModel constructor to ensure the model and its submodules are created within the specified GPU context. - Modifying the logic in FluxModel::forward for copying residual data from CPU back to GPU, ensuring it returns to the correct original GPU device. - Adding explicit CUDA context management in Tensor::copy_ for data copy operations involving CUDA devices (H2D, D2H, D2D) to guarantee cudaMemcpyAsync executes on the correct device. These changes allow users to reliably run Nunchaku on any specified GPU in a multi-GPU setup. * finish pre-commit
1 parent 3eabbd0 commit 1e621a5

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

src/FluxModel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,8 @@ std::tuple<Tensor, Tensor> JointTransformerBlock::forward(Tensor hidden_states,
778778

779779
FluxModel::FluxModel(bool use_fp4, bool offload, Tensor::ScalarType dtype, Device device)
780780
: dtype(dtype), offload(offload) {
781+
CUDADeviceContext model_construction_ctx(device.idx);
782+
781783
for (int i = 0; i < 19; i++) {
782784
transformer_blocks.push_back(
783785
std::make_unique<JointTransformerBlock>(3072, 24, 3072, false, use_fp4, dtype, device));

src/Tensor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,13 @@ class Tensor {
432432
return *this;
433433
}
434434

435+
std::optional<CUDADeviceContext> operation_ctx_guard;
436+
437+
if (this->device().type == Device::CUDA) {
438+
} else if (other.device().type == Device::CUDA) {
439+
operation_ctx_guard.emplace(other.device().idx);
440+
}
441+
435442
if (this->device().type == Device::CPU && other.device().type == Device::CPU) {
436443
memcpy(data_ptr<char>(), other.data_ptr<char>(), shape.size() * scalar_size());
437444
return *this;

0 commit comments

Comments
 (0)