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
19 changes: 16 additions & 3 deletions cpp/tensorrt_llm/common/ncclCommUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,19 @@ ncclUniqueId getUniqueId(std::set<int> const& group)

std::shared_ptr<ncclComm_t> getComm(std::set<int> const& group)
{
return getComm(group, 0);
}

std::shared_ptr<ncclComm_t> getComm(std::set<int> const& group, int minCtas)
{
#if NCCL_VERSION_CODE < NCCL_VERSION(2, 29, 0)
TLLM_CHECK_WITH_INFO(minCtas == 0, "getComm: minCtas requires NCCL >= 2.29");
#endif
auto const hostPg = useMPI() ? nullptr : pg_utils::get_world_pg();
TLLM_CHECK_WITH_INFO(useMPI() || hostPg != nullptr, "Host process group is not initialized");
auto const rank = useMPI() ? COMM_SESSION.getRank() : hostPg->getRank();
TLLM_LOG_TRACE("%s start for rank %d", __PRETTY_FUNCTION__, rank);
static std::map<std::set<int>, std::shared_ptr<ncclComm_t>> commMap;
static std::map<std::pair<std::set<int>, int>, std::shared_ptr<ncclComm_t>> commMap;
static std::mutex mutex;
std::lock_guard<std::mutex> lock(mutex);
std::ostringstream oss;
Expand All @@ -123,7 +131,8 @@ std::shared_ptr<ncclComm_t> getComm(std::set<int> const& group)
index++;
}
auto groupStr = oss.str();
auto it = commMap.find(group);
auto const cacheKey = std::make_pair(group, minCtas);
auto it = commMap.find(cacheKey);
if (it != commMap.end())
{
auto ncclComm = it->second;
Expand Down Expand Up @@ -207,6 +216,10 @@ std::shared_ptr<ncclComm_t> getComm(std::set<int> const& group)
#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 29, 0)
ncclConfig_t config = NCCL_CONFIG_INITIALIZER;
config.graphUsageMode = 1;
if (minCtas > 0)
{
config.minCTAs = minCtas;
}
NCCLCHECK_THROW(ncclCommInitRankConfig(ncclComm.get(), group.size(), id, groupRank, &config));
#else
NCCLCHECK_THROW(ncclCommInitRank(ncclComm.get(), group.size(), id, groupRank));
Expand All @@ -228,7 +241,7 @@ std::shared_ptr<ncclComm_t> getComm(std::set<int> const& group)
}
}
#endif
commMap[group] = ncclComm;
commMap[cacheKey] = ncclComm;
TLLM_LOG_TRACE("%s stop for rank %d", __PRETTY_FUNCTION__, rank);
return ncclComm;
}
Expand Down
3 changes: 2 additions & 1 deletion cpp/tensorrt_llm/common/opUtils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-FileCopyrightText: Copyright (c) 1993-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -153,6 +153,7 @@ void const* getCommSessionHandle();
std::unordered_map<tensorrt_llm::DataType, ncclDataType_t>* getDtypeMap();

std::shared_ptr<ncclComm_t> getComm(std::set<int> const& group);
std::shared_ptr<ncclComm_t> getComm(std::set<int> const& group, int minCtas);

#endif // ENABLE_MULTI_DEVICE

Expand Down
60 changes: 58 additions & 2 deletions cpp/tensorrt_llm/thop/reducescatterOp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-FileCopyrightText: Copyright (c) 1993-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,11 +15,13 @@
* limitations under the License.
*/

#include "tensorrt_llm/common/envUtils.h"
#include "tensorrt_llm/common/opUtils.h"
#include "tensorrt_llm/runtime/torchUtils.h"
#include "tensorrt_llm/runtime/utils/mpiUtils.h"
#include "tensorrt_llm/runtime/utils/pgUtils.h"

#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAStream.h>
#include <torch/extension.h>
#if ENABLE_MULTI_DEVICE
Expand All @@ -29,6 +31,7 @@
#endif // ENABLE_MULTI_DEVICE

#include <cassert>
#include <mutex>
#include <set>
#include <vector>

Expand Down Expand Up @@ -57,6 +60,51 @@ class ReducescatterOp
{
TLLM_LOG_TRACE("%s start for rank %d", __PRETTY_FUNCTION__, -1);
mNcclComm = getComm(mGroup);
#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 29, 0)
static bool const enablePrefill = common::getBoolEnv("TRTLLM_ENABLE_PREFILL_REDUCESCATTER");
constexpr size_t kMinGroupSize = 8;
if (enablePrefill && mGroup.size() >= kMinGroupSize)
{
static std::map<std::set<int>, std::shared_ptr<ncclComm_t>> prefillComms;
static std::mutex prefillMutex;
std::lock_guard<std::mutex> const lock(prefillMutex);
auto cached = prefillComms.find(mGroup);
if (cached == prefillComms.end())
{
cudaStreamCaptureStatus captureStatus = cudaStreamCaptureStatusNone;
TLLM_CUDA_CHECK(cudaStreamIsCapturing(at::cuda::getCurrentCUDAStream(), &captureStatus));
if (captureStatus != cudaStreamCaptureStatusNone)
{
// Capture can reuse the base communicator but cannot run the cold eligibility synchronization.
// Leave the cache empty so a later eager call still agrees on eligibility across all ranks.
TLLM_LOG_TRACE("%s stop for rank %d", __PRETTY_FUNCTION__, -1);
return 0;
}
constexpr int kTargetSmMajor = 10;
constexpr int kTargetSmMinor = 3;
constexpr int kMinRuntimeVersion = NCCL_VERSION(2, 31, 0);
constexpr int kMaxRuntimeVersionExclusive = NCCL_VERSION(2, 32, 0);
// TODO: Remove this tuning once the upstream NCCL ReduceScatter regression is fixed.
constexpr int kPrefillCtas = 64;
auto const* properties = at::cuda::getCurrentDeviceProperties();
int runtimeVersion = 0;
NCCLCHECK_THROW(ncclGetVersion(&runtimeVersion));
bool const locallyEligible = properties->major == kTargetSmMajor && properties->minor == kTargetSmMinor
&& runtimeVersion >= kMinRuntimeVersion && runtimeVersion < kMaxRuntimeVersionExclusive;
auto eligible = torch::full(
{1}, locallyEligible ? 1 : 0, torch::TensorOptions().dtype(torch::kInt32).device(torch::kCUDA));
NCCLCHECK_THROW(ncclAllReduce(eligible.data_ptr(), eligible.mutable_data_ptr(), 1, ncclInt32, ncclMin,
*mNcclComm, at::cuda::getCurrentCUDAStream()));
std::shared_ptr<ncclComm_t> prefillComm;
if (eligible.item<int32_t>() != 0)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
BowenFu marked this conversation as resolved.
Resolved
{
prefillComm = getComm(mGroup, kPrefillCtas);
Comment thread
trtllm-agent marked this conversation as resolved.
Resolved
}
cached = prefillComms.emplace(mGroup, std::move(prefillComm)).first;
}
mPrefillNcclComm = cached->second;
}
#endif
TLLM_LOG_TRACE("%s stop for rank %d", __PRETTY_FUNCTION__, -1);
return 0;
}
Expand Down Expand Up @@ -98,8 +146,15 @@ class ReducescatterOp
auto output = torch::empty(outputShape, input.options());
if (use_nccl_reducescatter)
{
constexpr size_t kMinPrefillBytes = 2 * 1024 * 1024;
constexpr size_t kMaxPrefillBytes = 16 * 1024 * 1024;
auto const outputBytes = output.nbytes();
auto const& communicator = mPrefillNcclComm != nullptr && input.scalar_type() == at::kBFloat16
&& outputBytes >= kMinPrefillBytes && outputBytes <= kMaxPrefillBytes
? mPrefillNcclComm
: mNcclComm;
ncclReduceScatter(input.data_ptr(), output.mutable_data_ptr(), output.numel(), (*getDtypeMap())[type],
ncclSum, *mNcclComm, stream);
ncclSum, *communicator, stream);
}
else
{
Expand Down Expand Up @@ -129,6 +184,7 @@ class ReducescatterOp
private:
std::set<int> mGroup;
std::shared_ptr<ncclComm_t> mNcclComm;
std::shared_ptr<ncclComm_t> mPrefillNcclComm;
};

class ReducescatterPgOp
Expand Down
17 changes: 17 additions & 0 deletions cpp/tests/unit_tests/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,20 @@ if(BUILD_PYT)
TRTLLM_PROFILER_SELECTOR_CASES="${TOP_LEVEL_DIR}/tests/unittest/profiler_selector_cases.json"
)
endif()

if(ENABLE_MULTI_DEVICE AND BUILD_PYT)
add_tllm_gtest(
getCommLegacyConfigTest
OWNER
common
SOURCES
getCommLegacyConfigTest.cpp
FULL_STACK
LINK_LIBRARIES
pg_utils
${TORCH_LIBRARIES}
${Python3_LIBRARIES}
LABELS
"unit;common;full-stack;gpu")
target_compile_definitions(getCommLegacyConfigTest PRIVATE BUILD_PYT)
endif()
54 changes: 54 additions & 0 deletions cpp/tests/unit_tests/common/getCommLegacyConfigTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include <nccl.h>

#undef NCCL_VERSION_CODE
#define NCCL_VERSION_CODE NCCL_VERSION(2, 28, 0)

#include "tensorrt_llm/common/ncclCommUtils.cpp"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace
{

class GetCommLegacyConfigTest : public ::testing::Test
{
protected:
void SetUp() override
{
auto const& session = tensorrt_llm::mpi::MpiComm::session();
int deviceCount = 0;
ASSERT_EQ(cudaGetDeviceCount(&deviceCount), cudaSuccess);
ASSERT_GT(deviceCount, 0);
ASSERT_EQ(cudaSetDevice(session.getRank() % deviceCount), cudaSuccess);
for (int groupRank = 0; groupRank < session.getSize(); ++groupRank)
{
mGroup.insert(groupRank);
}
}

std::set<int> mGroup;
};

TEST_F(GetCommLegacyConfigTest, DefaultAndZeroHintShareCommunicator)
{
auto const defaultComm = tensorrt_llm::getComm(mGroup);
ASSERT_NE(defaultComm, nullptr);
ASSERT_NE(*defaultComm, nullptr);
EXPECT_EQ(defaultComm, tensorrt_llm::getComm(mGroup, 0));
}

TEST_F(GetCommLegacyConfigTest, RejectsUnsupportedHint)
{
constexpr int kMinCtas = 64;
EXPECT_THAT([this]() { tensorrt_llm::getComm(mGroup, kMinCtas); },
::testing::ThrowsMessage<tensorrt_llm::common::TllmException>(
::testing::HasSubstr("minCtas requires NCCL >= 2.29")));
}

} // namespace
22 changes: 22 additions & 0 deletions cpp/tests/unit_tests/multi_gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,25 @@ if(BUILD_PYT)
target_link_libraries(ncclUtilsTest PRIVATE ${TORCH_LIBRARIES}
Python3::Python)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux"
AND ENABLE_MULTI_DEVICE
AND BUILD_PYT)
add_tllm_gtest(
reduceScatterPrefillTest
OWNER
common
SOURCES
reduceScatterPrefillTest.cpp
FULL_STACK
LINK_LIBRARIES
pg_utils
${TORCH_LIBRARIES}
${Python3_LIBRARIES}
${CMAKE_DL_LIBS}
LABELS
"unit;multi-gpu;prefill-reducescatter;gpu")
target_compile_definitions(reduceScatterPrefillTest PRIVATE BUILD_PYT)
target_link_options(reduceScatterPrefillTest PRIVATE
"-Wl,--export-dynamic-symbol=ncclGetVersion")
endif()
Loading
Loading