diff --git a/cpp/cmake/templates/cutlass_runtime_symbols.map b/cpp/cmake/templates/cutlass_runtime_symbols.map new file mode 100644 index 000000000000..c98565150e5c --- /dev/null +++ b/cpp/cmake/templates/cutlass_runtime_symbols.map @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* ClusterLauncher receives kernel entry points registered with this library's + * static CUDA runtime. Its inline helpers must not be interposed by another + * shared library's copies, which use a different runtime instance. + */ +{ + local: + _ZN7cutlass15ClusterLauncher*; +}; diff --git a/cpp/tensorrt_llm/CMakeLists.txt b/cpp/tensorrt_llm/CMakeLists.txt index 8445ac34e0c4..9622f19014b0 100644 --- a/cpp/tensorrt_llm/CMakeLists.txt +++ b/cpp/tensorrt_llm/CMakeLists.txt @@ -303,6 +303,16 @@ set_target_properties( PROPERTIES CXX_STANDARD "20" CXX_STANDARD_REQUIRED "YES" CXX_EXTENSIONS "NO" LINK_FLAGS "${AS_NEEDED_FLAG} ${UNDEFINED_FLAG}") +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + # Cluster launch helpers must use the CUDA runtime that registered the kernel. + set(CUTLASS_RUNTIME_SYMBOLS_MAP + "${PROJECT_SOURCE_DIR}/cmake/templates/cutlass_runtime_symbols.map") + target_link_options(${SHARED_TARGET} PRIVATE + "LINKER:--version-script,${CUTLASS_RUNTIME_SYMBOLS_MAP}") + set_property(TARGET ${SHARED_TARGET} APPEND + PROPERTY LINK_DEPENDS "${CUTLASS_RUNTIME_SYMBOLS_MAP}") +endif() + # Keep statically linked NVRTC symbols local so they cannot be interposed by a # dynamic NVRTC loaded elsewhere in the process. if(NOT WIN32 AND NOT NVRTC_DYNAMIC_LINKING) @@ -324,6 +334,8 @@ set(TLLM_FACADE_COMPONENT_TARGETS tllm_common_error tllm_common_logging tllm_common_native_memory + tllm_common_nccl + tllm_common_nccl_comm tllm_common_profiler tllm_context_transfer_consensus tllm_executor_agent_name @@ -341,6 +353,7 @@ set(TLLM_FACADE_COMPONENT_TARGETS tllm_runtime_distributed tllm_runtime_gdr tllm_runtime_host_accessible_memory + tllm_runtime_ipc tllm_runtime_locality tllm_runtime_lora tllm_runtime_lora_cache diff --git a/cpp/tensorrt_llm/common/CMakeLists.txt b/cpp/tensorrt_llm/common/CMakeLists.txt index 9b69c60be494..e1ab567fb85d 100644 --- a/cpp/tensorrt_llm/common/CMakeLists.txt +++ b/cpp/tensorrt_llm/common/CMakeLists.txt @@ -102,15 +102,37 @@ if(ENABLE_CUBLASLT_FP4_GEMM) target_compile_definitions(tllm_common_cublas PUBLIC ENABLE_CUBLASLT_FP4_GEMM) endif() +add_library(tllm_common_nccl STATIC ncclUtils.cpp) +add_library(tllm::common_nccl ALIAS tllm_common_nccl) +target_link_libraries(tllm_common_nccl PUBLIC tllm::common_cuda) +target_compile_features(tllm_common_nccl PUBLIC cxx_std_20) +target_compile_definitions( + tllm_common_nccl PUBLIC ENABLE_MULTI_DEVICE=$) +if(ENABLE_MULTI_DEVICE) + target_link_libraries( + tllm_common_nccl + PUBLIC ${NCCL_LIB} MPI::MPI_C torch_cpu c10 + PRIVATE tllm::runtime_distributed) +endif() +set_target_properties(tllm_common_nccl PROPERTIES POSITION_INDEPENDENT_CODE ON) + +add_library(tllm_common_nccl_comm STATIC ncclCommUtils.cpp) +add_library(tllm::common_nccl_comm ALIAS tllm_common_nccl_comm) +target_link_libraries( + tllm_common_nccl_comm + PUBLIC tllm::common_nccl + PRIVATE tllm::runtime_distributed tllm::runtime_ipc_nvls + tllm::common_native_memory) +if(ENABLE_MULTI_DEVICE) + target_link_libraries(tllm_common_nccl_comm PRIVATE pg_utils) +endif() +target_compile_features(tllm_common_nccl_comm PUBLIC cxx_std_20) +set_target_properties(tllm_common_nccl_comm PROPERTIES POSITION_INDEPENDENT_CODE + ON) + add_library( - common_src OBJECT - cudaArchitectures.cpp - cudaFp8Utils.cu - mcastDevMemUtils.cpp - ncclUtils.cpp - opUtils.cpp - safetensors.cpp - sageQuant.cu) + common_src OBJECT cudaArchitectures.cpp cudaFp8Utils.cu mcastDevMemUtils.cpp + opUtils.cpp safetensors.cpp sageQuant.cu) list(JOIN CMAKE_CUDA_ARCHITECTURES_ORIG "," TRTLLM_BUILT_CUDA_ARCHITECTURES) list(JOIN CMAKE_CUDA_ARCHITECTURES_WITH_KERNELS "," @@ -127,4 +149,4 @@ set_property(TARGET common_src PROPERTY POSITION_INDEPENDENT_CODE ON) set_property(TARGET common_src PROPERTY CUDA_RESOLVE_DEVICE_SYMBOLS ON) target_link_libraries( common_src PUBLIC trtllm_gen_fmha_interface tllm::common_cublas - tllm::common_native_memory) + tllm::common_native_memory tllm::common_nccl_comm) diff --git a/cpp/tensorrt_llm/common/ncclCommUtils.cpp b/cpp/tensorrt_llm/common/ncclCommUtils.cpp new file mode 100644 index 000000000000..b90384d5ca13 --- /dev/null +++ b/cpp/tensorrt_llm/common/ncclCommUtils.cpp @@ -0,0 +1,246 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2022-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "tensorrt_llm/common/nativeMemory.h" +#include "tensorrt_llm/common/ncclUtils.h" +#include "tensorrt_llm/common/opUtils.h" +#include "tensorrt_llm/common/tllmDataType.h" +#include "tensorrt_llm/runtime/ipcNvlsMemory.h" +#include "tensorrt_llm/runtime/utils/mpiTags.h" +#include "tensorrt_llm/runtime/utils/mpiUtils.h" +#if ENABLE_MULTI_DEVICE +#include "tensorrt_llm/runtime/utils/pgUtils.h" +#endif + +#include +#include +#include + +TRTLLM_NAMESPACE_BEGIN + +#if ENABLE_MULTI_DEVICE + +std::unordered_map* getDtypeMap() +{ + static std::unordered_map dtypeMap = { + {tensorrt_llm::DataType::kFLOAT, ncclFloat32}, + {tensorrt_llm::DataType::kHALF, ncclFloat16}, + {tensorrt_llm::DataType::kBF16, ncclBfloat16}, + {tensorrt_llm::DataType::kFP8, ncclInt8}, + {tensorrt_llm::DataType::kBOOL, ncclInt8}, + {tensorrt_llm::DataType::kINT32, ncclInt32}, + {tensorrt_llm::DataType::kINT64, ncclInt64}, + {tensorrt_llm::DataType::kUINT8, ncclUint8}, + {tensorrt_llm::DataType::kINT8, ncclInt8}, + }; + return &dtypeMap; +} + +namespace +{ + +// Get NCCL unique ID for a group of ranks. +ncclUniqueId getUniqueId(std::set const& group) +{ + if (!useMPI()) + { + auto const pg = pg_utils::get_world_pg(); + TLLM_CHECK_WITH_INFO(pg != nullptr, "Host process group is not initialized"); + pg_utils::PgHelper host(pg); + ncclUniqueId id{}; + auto bytes = at::from_blob(&id, {sizeof(id)}, at::TensorOptions().dtype(at::kByte)); + int const root = *group.begin(); + int const tag = static_cast(mpi::MpiTag::kNcclUniqueId); + if (pg->getRank() == root) + { + NCCLCHECK_THROW(ncclGetUniqueId(&id)); + for (auto const rank : group) + { + if (rank != root) + { + PGCHECK_THROW(host.send(bytes, rank, tag)); + } + } + } + else + { + PGCHECK_THROW(host.recv(bytes, root, tag)); + } + return id; + } + auto const rank = COMM_SESSION.getRank(); + TLLM_LOG_TRACE("%s start for rank %d", __PRETTY_FUNCTION__, rank); + ncclUniqueId id; + if (rank == *group.begin()) + { + NCCLCHECK_THROW(ncclGetUniqueId(&id)); + for (auto it = std::next(std::begin(group), 1); it != group.end(); ++it) + { + COMM_SESSION.sendValue(id, *it, tensorrt_llm::mpi::MpiTag::kNcclUniqueId); + } + } + else + { + COMM_SESSION.recvValue(id, *group.begin(), tensorrt_llm::mpi::MpiTag::kNcclUniqueId); + } + TLLM_LOG_TRACE("%s stop for rank %d", __PRETTY_FUNCTION__, rank); + return id; +} +} // namespace + +std::shared_ptr getComm(std::set const& group) +{ + 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::shared_ptr> commMap; + static std::mutex mutex; + std::lock_guard lock(mutex); + std::ostringstream oss; + int index = 0; + for (auto const& rank : group) + { + if (index != 0) + { + oss << ","; + } + oss << rank; + index++; + } + auto groupStr = oss.str(); + auto it = commMap.find(group); + if (it != commMap.end()) + { + auto ncclComm = it->second; + TLLM_LOG_TRACE("NCCL comm for group(%s) is cached for rank %d", groupStr.c_str(), rank); + return ncclComm; + } + + TLLM_LOG_TRACE("Init NCCL comm for group(%s) for rank %d", groupStr.c_str(), rank); + // Finish every environment update before getUniqueId(): on the group root, ncclGetUniqueId() starts NCCL's + // bootstrap thread, which reads the environment with getenv(), and a concurrent setenv() may reallocate + // environ underneath it. +#if defined(_WIN32) + // Need static connection initialization for accurate KV cache size estimation + if (getenv("NCCL_RUNTIME_CONNECT") == nullptr) + _putenv_s("NCCL_RUNTIME_CONNECT", "0"); + // Disable graph register to avoid startup hangs + if (getenv("NCCL_GRAPH_REGISTER") == nullptr) + _putenv_s("NCCL_GRAPH_REGISTER", "0"); +#else + setenv("NCCL_RUNTIME_CONNECT", "0", 0); + setenv("NCCL_GRAPH_REGISTER", "0", 0); + // NCCL aborts during init if it tries NVLS multicast but the fabric/IMEX + // plane can't bind it. Disable NVLS when the fabric is not usable so NCCL + // falls back to NVLink P2P. No-overwrite preserves an explicit user setting. + if (!tensorrt_llm::runtime::ipcNvlsFabricUsable()) + { + setenv("NCCL_NVLS_ENABLE", "0", 0); + } +#endif // _WIN32 + ncclUniqueId id = getUniqueId(group); + int groupRank = 0; + for (auto const& currentRank : group) + { + if (rank == currentRank) + break; + ++groupRank; + } + TLLM_CHECK(static_cast(groupRank) < group.size()); + auto nativeMemory = std::make_shared(); + std::shared_ptr ncclComm(new ncclComm_t, + [nativeMemory](ncclComm_t* comm) + { + if (!comm) + { + return; + } + + // STEP 1: Clean up resources and destroy NCCL communicator if it's valid + if (*comm) + { + // Clean up all registered resources FIRST + // The cleanupResources function uses a destruction guard to safely handle + // static destruction order issues - it will return early if the singleton + // is being destroyed (in which case the destructor handles cleanup proactively) + tensorrt_llm::common::nccl_util::NcclCommResourceManager::getInstance().cleanupResources(*comm); + + // Now destroy the NCCL communicator + ncclResult_t result = ncclCommDestroy(*comm); + if (result != ncclSuccess) + { + // Logging may fail during static destruction, so wrap in try-catch + try + { + TLLM_LOG_WARNING("ncclCommDestroy failed with error: %d", result); + } + catch (...) + { + // Ignore logging failures during static destruction + } + } + + // Clear the communicator value before freeing the pointer + *comm = nullptr; + } + + // STEP 2: Always free the pointer memory (regardless of whether *comm was valid) + delete comm; + tensorrt_llm::common::recordNativeRelease(*nativeMemory); + }); + auto const freeBefore = tensorrt_llm::common::currentDeviceFreeBytes(); +#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 29, 0) + ncclConfig_t config = NCCL_CONFIG_INITIALIZER; + config.graphUsageMode = 1; + NCCLCHECK_THROW(ncclCommInitRankConfig(ncclComm.get(), group.size(), id, groupRank, &config)); +#else + NCCLCHECK_THROW(ncclCommInitRank(ncclComm.get(), group.size(), id, groupRank)); +#endif // NCCL_VERSION_CODE >= NCCL_VERSION(2, 29, 0) + // With NCCL_RUNTIME_CONNECT=0 (set above) NCCL allocates its connection buffers here and reports no size. + *nativeMemory = tensorrt_llm::common::recordNativeAllocationMeasured( + tensorrt_llm::common::nativeMemoryLabels::kNcclCommunicators, freeBefore); +#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0) + if (common::nccl_util::isNcclWindowSupported()) + { + std::vector const ranks(group.begin(), group.end()); + if (useMPI()) + { + common::nccl_util::registerHostCommunicator(*ncclComm, COMM_SESSION, ranks); + } + else + { + common::nccl_util::registerHostCommunicator(*ncclComm, hostPg, ranks); + } + } +#endif + commMap[group] = ncclComm; + TLLM_LOG_TRACE("%s stop for rank %d", __PRETTY_FUNCTION__, rank); + return ncclComm; +} +#endif // ENABLE_MULTI_DEVICE + +void const* tensorrt_llm::common::op::getCommSessionHandle() +{ +#if ENABLE_MULTI_DEVICE + return &COMM_SESSION; +#else + return nullptr; +#endif // ENABLE_MULTI_DEVICE +} + +TRTLLM_NAMESPACE_END diff --git a/cpp/tensorrt_llm/common/ncclTorchUtils.h b/cpp/tensorrt_llm/common/ncclTorchUtils.h new file mode 100644 index 000000000000..cb734307bf2d --- /dev/null +++ b/cpp/tensorrt_llm/common/ncclTorchUtils.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2025-2026, NVIDIA CORPORATION. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include "tensorrt_llm/common/ncclUtils.h" + +#if ENABLE_MULTI_DEVICE +#include +#include + +TRTLLM_NAMESPACE_BEGIN + +namespace common::nccl_util +{ + +#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0) + +// Creates a PyTorch tensor backed by an NCCL window buffer. +// The tensor will automatically release the buffer back to the pool when destroyed. +// This is analogous to torch_ext::create_userbuffers_tensor() but for NCCLWindowAllocator. +inline std::pair createNCCLWindowTensor( + std::shared_ptr comm, at::IntArrayRef shape, torch::ScalarType dtype) +{ + // Calculate buffer size + int64_t buffer_size + = std::accumulate(shape.begin(), shape.end(), 1LL, std::multiplies()) * torch::elementSize(dtype); + + // Calculate strides + std::vector strides_vec(shape.size()); + if (!shape.empty()) + { + strides_vec[shape.size() - 1] = 1; + for (int64_t i = static_cast(shape.size()) - 1; i >= 1; --i) + { + strides_vec[i - 1] = strides_vec[i] * shape[i]; + } + } + + // Request buffer from allocator + auto& allocator = NCCLWindowAllocator::getInstance(); + NCCLWindowBuffer buffer; + + if (!comm || !*comm) + { + TLLM_LOG_DEBUG("[createNCCLWindowTensor] null comm; returning invalid buffer"); + return std::make_pair(torch::Tensor(), NCCLWindowBuffer()); + } + + // Expected resource failures, unknown capture state, and captures without a graph-pool owner + // return an invalid buffer so callers can use their existing unregistered fallback. Other + // programming errors must propagate instead of silently changing the collective implementation. + buffer = allocator.requestBuffer(*comm, buffer_size, at::cuda::getCurrentCUDAStream().stream()); + + // Defensive validation: ensure buffer is valid before proceeding + if (!buffer.isValid()) + { + TLLM_LOG_DEBUG("[createNCCLWindowTensor] invalid buffer returned from requestBuffer; returning invalid buffer"); + return std::make_pair(torch::Tensor(), NCCLWindowBuffer()); + } + + // Create custom deleter that releases the buffer + auto deleter = [comm, ptr = buffer.ptr, leaseId = buffer.leaseId](void*) + { NCCLWindowAllocator::getInstance().releaseBufferFromDestructor(*comm, ptr, leaseId); }; + + // Create tensor from the buffer + auto tensor = torch::from_blob(buffer.ptr, shape, strides_vec, deleter, torch::dtype(dtype).device(torch::kCUDA)); + auto const device = tensor.get_device(); + allocator.registerTensorLease(*comm, tensor.storage().unsafeGetStorageImpl(), buffer.ptr, buffer.leaseId, device); + + return std::make_pair(tensor, buffer); +} + +#endif // NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0) + +} // namespace common::nccl_util + +TRTLLM_NAMESPACE_END + +#endif // ENABLE_MULTI_DEVICE diff --git a/cpp/tensorrt_llm/common/ncclUtils.h b/cpp/tensorrt_llm/common/ncclUtils.h index 14a9f5b5bdc5..e1fa8178b945 100644 --- a/cpp/tensorrt_llm/common/ncclUtils.h +++ b/cpp/tensorrt_llm/common/ncclUtils.h @@ -22,12 +22,10 @@ #include "tensorrt_llm/runtime/utils/multiDeviceUtils.h" #if ENABLE_MULTI_DEVICE -#include #include #include #include #include -#include #endif #include @@ -544,61 +542,6 @@ class ScopedNCCLWindowBuffer NCCLWindowBuffer mBuffer; }; -// Creates a PyTorch tensor backed by an NCCL window buffer. -// The tensor will automatically release the buffer back to the pool when destroyed. -// This is analogous to torch_ext::create_userbuffers_tensor() but for NCCLWindowAllocator. -inline std::pair createNCCLWindowTensor( - std::shared_ptr comm, at::IntArrayRef shape, torch::ScalarType dtype) -{ - // Calculate buffer size - int64_t buffer_size - = std::accumulate(shape.begin(), shape.end(), 1LL, std::multiplies()) * torch::elementSize(dtype); - - // Calculate strides - std::vector strides_vec(shape.size()); - if (!shape.empty()) - { - strides_vec[shape.size() - 1] = 1; - for (int64_t i = static_cast(shape.size()) - 1; i >= 1; --i) - { - strides_vec[i - 1] = strides_vec[i] * shape[i]; - } - } - - // Request buffer from allocator - auto& allocator = NCCLWindowAllocator::getInstance(); - NCCLWindowBuffer buffer; - - if (!comm || !*comm) - { - TLLM_LOG_DEBUG("[createNCCLWindowTensor] null comm; returning invalid buffer"); - return std::make_pair(torch::Tensor(), NCCLWindowBuffer()); - } - - // Expected resource failures, unknown capture state, and captures without a graph-pool owner - // return an invalid buffer so callers can use their existing unregistered fallback. Other - // programming errors must propagate instead of silently changing the collective implementation. - buffer = allocator.requestBuffer(*comm, buffer_size, at::cuda::getCurrentCUDAStream().stream()); - - // Defensive validation: ensure buffer is valid before proceeding - if (!buffer.isValid()) - { - TLLM_LOG_DEBUG("[createNCCLWindowTensor] invalid buffer returned from requestBuffer; returning invalid buffer"); - return std::make_pair(torch::Tensor(), NCCLWindowBuffer()); - } - - // Create custom deleter that releases the buffer - auto deleter = [comm, ptr = buffer.ptr, leaseId = buffer.leaseId](void*) - { NCCLWindowAllocator::getInstance().releaseBufferFromDestructor(*comm, ptr, leaseId); }; - - // Create tensor from the buffer - auto tensor = torch::from_blob(buffer.ptr, shape, strides_vec, deleter, torch::dtype(dtype).device(torch::kCUDA)); - auto const device = tensor.get_device(); - allocator.registerTensorLease(*comm, tensor.storage().unsafeGetStorageImpl(), buffer.ptr, buffer.leaseId, device); - - return std::make_pair(tensor, buffer); -} - #endif // NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0) } // namespace common::nccl_util diff --git a/cpp/tensorrt_llm/common/opUtils.cpp b/cpp/tensorrt_llm/common/opUtils.cpp index 77d8000d861a..bba8a8616bfc 100644 --- a/cpp/tensorrt_llm/common/opUtils.cpp +++ b/cpp/tensorrt_llm/common/opUtils.cpp @@ -16,13 +16,6 @@ */ #include "tensorrt_llm/common/opUtils.h" -#include "tensorrt_llm/common/nativeMemory.h" -#include "tensorrt_llm/common/ncclUtils.h" -#include "tensorrt_llm/common/tllmDataType.h" -#include "tensorrt_llm/runtime/ipcNvlsMemory.h" -#include "tensorrt_llm/runtime/utils/mpiTags.h" -#include "tensorrt_llm/runtime/utils/mpiUtils.h" -#include "tensorrt_llm/runtime/utils/pgUtils.h" #include "cuda.h" #include @@ -36,216 +29,6 @@ #include TRTLLM_NAMESPACE_BEGIN -#if ENABLE_MULTI_DEVICE - -std::unordered_map* getDtypeMap() -{ - static std::unordered_map dtypeMap = { - {tensorrt_llm::DataType::kFLOAT, ncclFloat32}, - {tensorrt_llm::DataType::kHALF, ncclFloat16}, - {tensorrt_llm::DataType::kBF16, ncclBfloat16}, - {tensorrt_llm::DataType::kFP8, ncclInt8}, - {tensorrt_llm::DataType::kBOOL, ncclInt8}, - {tensorrt_llm::DataType::kINT32, ncclInt32}, - {tensorrt_llm::DataType::kINT64, ncclInt64}, - {tensorrt_llm::DataType::kUINT8, ncclUint8}, - {tensorrt_llm::DataType::kINT8, ncclInt8}, - }; - return &dtypeMap; -} - -namespace -{ - -// Get NCCL unique ID for a group of ranks. -ncclUniqueId getUniqueId(std::set const& group) -{ - if (!useMPI()) - { - auto const pg = pg_utils::get_world_pg(); - TLLM_CHECK_WITH_INFO(pg != nullptr, "Host process group is not initialized"); - pg_utils::PgHelper host(pg); - ncclUniqueId id{}; - auto bytes = at::from_blob(&id, {sizeof(id)}, at::TensorOptions().dtype(at::kByte)); - int const root = *group.begin(); - int const tag = static_cast(mpi::MpiTag::kNcclUniqueId); - if (pg->getRank() == root) - { - NCCLCHECK_THROW(ncclGetUniqueId(&id)); - for (auto const rank : group) - { - if (rank != root) - { - PGCHECK_THROW(host.send(bytes, rank, tag)); - } - } - } - else - { - PGCHECK_THROW(host.recv(bytes, root, tag)); - } - return id; - } - auto const rank = COMM_SESSION.getRank(); - TLLM_LOG_TRACE("%s start for rank %d", __PRETTY_FUNCTION__, rank); - ncclUniqueId id; - if (rank == *group.begin()) - { - NCCLCHECK_THROW(ncclGetUniqueId(&id)); - for (auto it = std::next(std::begin(group), 1); it != group.end(); ++it) - { - COMM_SESSION.sendValue(id, *it, tensorrt_llm::mpi::MpiTag::kNcclUniqueId); - } - } - else - { - COMM_SESSION.recvValue(id, *group.begin(), tensorrt_llm::mpi::MpiTag::kNcclUniqueId); - } - TLLM_LOG_TRACE("%s stop for rank %d", __PRETTY_FUNCTION__, rank); - return id; -} -} // namespace - -std::shared_ptr getComm(std::set const& group) -{ - 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::shared_ptr> commMap; - static std::mutex mutex; - std::lock_guard lock(mutex); - std::ostringstream oss; - int index = 0; - for (auto const& rank : group) - { - if (index != 0) - { - oss << ","; - } - oss << rank; - index++; - } - auto groupStr = oss.str(); - auto it = commMap.find(group); - if (it != commMap.end()) - { - auto ncclComm = it->second; - TLLM_LOG_TRACE("NCCL comm for group(%s) is cached for rank %d", groupStr.c_str(), rank); - return ncclComm; - } - - TLLM_LOG_TRACE("Init NCCL comm for group(%s) for rank %d", groupStr.c_str(), rank); - // Finish every environment update before getUniqueId(): on the group root, ncclGetUniqueId() starts NCCL's - // bootstrap thread, which reads the environment with getenv(), and a concurrent setenv() may reallocate - // environ underneath it. -#if defined(_WIN32) - // Need static connection initialization for accurate KV cache size estimation - if (getenv("NCCL_RUNTIME_CONNECT") == nullptr) - _putenv_s("NCCL_RUNTIME_CONNECT", "0"); - // Disable graph register to avoid startup hangs - if (getenv("NCCL_GRAPH_REGISTER") == nullptr) - _putenv_s("NCCL_GRAPH_REGISTER", "0"); -#else - setenv("NCCL_RUNTIME_CONNECT", "0", 0); - setenv("NCCL_GRAPH_REGISTER", "0", 0); - // NCCL aborts during init if it tries NVLS multicast but the fabric/IMEX - // plane can't bind it. Disable NVLS when the fabric is not usable so NCCL - // falls back to NVLink P2P. No-overwrite preserves an explicit user setting. - if (!tensorrt_llm::runtime::ipcNvlsFabricUsable()) - { - setenv("NCCL_NVLS_ENABLE", "0", 0); - } -#endif // _WIN32 - ncclUniqueId id = getUniqueId(group); - int groupRank = 0; - for (auto const& currentRank : group) - { - if (rank == currentRank) - break; - ++groupRank; - } - TLLM_CHECK(static_cast(groupRank) < group.size()); - auto nativeMemory = std::make_shared(); - std::shared_ptr ncclComm(new ncclComm_t, - [nativeMemory](ncclComm_t* comm) - { - if (!comm) - { - return; - } - - // STEP 1: Clean up resources and destroy NCCL communicator if it's valid - if (*comm) - { - // Clean up all registered resources FIRST - // The cleanupResources function uses a destruction guard to safely handle - // static destruction order issues - it will return early if the singleton - // is being destroyed (in which case the destructor handles cleanup proactively) - tensorrt_llm::common::nccl_util::NcclCommResourceManager::getInstance().cleanupResources(*comm); - - // Now destroy the NCCL communicator - ncclResult_t result = ncclCommDestroy(*comm); - if (result != ncclSuccess) - { - // Logging may fail during static destruction, so wrap in try-catch - try - { - TLLM_LOG_WARNING("ncclCommDestroy failed with error: %d", result); - } - catch (...) - { - // Ignore logging failures during static destruction - } - } - - // Clear the communicator value before freeing the pointer - *comm = nullptr; - } - - // STEP 2: Always free the pointer memory (regardless of whether *comm was valid) - delete comm; - tensorrt_llm::common::recordNativeRelease(*nativeMemory); - }); - auto const freeBefore = tensorrt_llm::common::currentDeviceFreeBytes(); -#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 29, 0) - ncclConfig_t config = NCCL_CONFIG_INITIALIZER; - config.graphUsageMode = 1; - NCCLCHECK_THROW(ncclCommInitRankConfig(ncclComm.get(), group.size(), id, groupRank, &config)); -#else - NCCLCHECK_THROW(ncclCommInitRank(ncclComm.get(), group.size(), id, groupRank)); -#endif // NCCL_VERSION_CODE >= NCCL_VERSION(2, 29, 0) - // With NCCL_RUNTIME_CONNECT=0 (set above) NCCL allocates its connection buffers here and reports no size. - *nativeMemory = tensorrt_llm::common::recordNativeAllocationMeasured( - tensorrt_llm::common::nativeMemoryLabels::kNcclCommunicators, freeBefore); -#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0) - if (common::nccl_util::isNcclWindowSupported()) - { - std::vector const ranks(group.begin(), group.end()); - if (useMPI()) - { - common::nccl_util::registerHostCommunicator(*ncclComm, COMM_SESSION, ranks); - } - else - { - common::nccl_util::registerHostCommunicator(*ncclComm, hostPg, ranks); - } - } -#endif - commMap[group] = ncclComm; - TLLM_LOG_TRACE("%s stop for rank %d", __PRETTY_FUNCTION__, rank); - return ncclComm; -} -#endif // ENABLE_MULTI_DEVICE - -void const* tensorrt_llm::common::op::getCommSessionHandle() -{ -#if ENABLE_MULTI_DEVICE - return &COMM_SESSION; -#else - return nullptr; -#endif // ENABLE_MULTI_DEVICE -} namespace { diff --git a/cpp/tensorrt_llm/kernels/CMakeLists.txt b/cpp/tensorrt_llm/kernels/CMakeLists.txt index 3c041334c039..e947166d9108 100644 --- a/cpp/tensorrt_llm/kernels/CMakeLists.txt +++ b/cpp/tensorrt_llm/kernels/CMakeLists.txt @@ -101,10 +101,6 @@ list(FILTER SRC_CU EXCLUDE REGEX # library below. list(FILTER SRC_CU EXCLUDE REGEX "minimaxM3Sm107(Tp[248])?/.*") -if(NOT ENABLE_MULTI_DEVICE) - list(FILTER SRC_CU EXCLUDE REGEX "customAllReduceKernels*.*cu$") -endif() - # Lower CUDA optimization level when FAST_BUILD is enabled to speed up # compilation. CMake by default adds -O3 to CMAKE_CUDA_FLAGS_RELEASE. We # override it here before creating the target to avoid the "incompatible diff --git a/cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemm.cu b/cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemm.cu similarity index 99% rename from cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemm.cu rename to cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemm.cu index e694d6c49379..fdc6338a4838 100644 --- a/cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemm.cu +++ b/cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemm.cu @@ -16,7 +16,7 @@ #include "cutlass/numeric_conversion.h" #include "tensorrt_llm/common/config.h" -#include "tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemm.h" +#include "tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemm.h" #include TRTLLM_NAMESPACE_BEGIN diff --git a/cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemm.h b/cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemm.h similarity index 97% rename from cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemm.h rename to cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemm.h index c2bf35175391..e334c35ec89e 100644 --- a/cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemm.h +++ b/cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemm.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2022-2026, NVIDIA CORPORATION. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemmNVFP4.cu b/cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemmNVFP4.cu similarity index 99% rename from cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemmNVFP4.cu rename to cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemmNVFP4.cu index 5c8f6615d8b2..5a4034dbe968 100644 --- a/cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemmNVFP4.cu +++ b/cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemmNVFP4.cu @@ -17,7 +17,7 @@ #include "cutlass/numeric_conversion.h" #include "tensorrt_llm/common/config.h" #include "tensorrt_llm/common/cudaFp8Utils.h" -#include "tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemmNVFP4.h" +#include "tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemmNVFP4.h" #include TRTLLM_NAMESPACE_BEGIN diff --git a/cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemmNVFP4.h b/cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemmNVFP4.h similarity index 97% rename from cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemmNVFP4.h rename to cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemmNVFP4.h index 6e901846ed25..32ecd66c9a8f 100644 --- a/cpp/tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemmNVFP4.h +++ b/cpp/tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemmNVFP4.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2025-2026, NVIDIA CORPORATION. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/cpp/tensorrt_llm/kernels/cutlass_kernels/CMakeLists.txt b/cpp/tensorrt_llm/kernels/cutlass_kernels/CMakeLists.txt index ad963ca2053c..11215ab65ad5 100644 --- a/cpp/tensorrt_llm/kernels/cutlass_kernels/CMakeLists.txt +++ b/cpp/tensorrt_llm/kernels/cutlass_kernels/CMakeLists.txt @@ -181,7 +181,7 @@ set(FP8_BLOCKSCALE_GEMM_SRC_CU file(GLOB_RECURSE FP4_SRC_CU fp4_gemm/*.cu) # Get the sources for AllReduce GEMM -file(GLOB_RECURSE ARGEMM_SRC_CU allreduce_gemm/*.cu) +set(ARGEMM_SRC_CU allreduce_gemm/allreduce_gemm_runner.cu) message( VERBOSE @@ -232,11 +232,23 @@ if(USING_OSS_CUTLASS_FP4_GEMM) endif() if(USING_OSS_CUTLASS_MOE_GEMM) - file(GLOB MOE_GEMM_SRC_CU "${MOE_CUTLASS_DIR}/*.cu") - list( - REMOVE_ITEM MOE_GEMM_SRC_CU "${MOE_CUTLASS_DIR}/moe_lora_pointer_expand.cu" - "${MOE_CUTLASS_DIR}/moe_lora_problem_builder.cu" - "${MOE_CUTLASS_DIR}/moe_lora_slot_expand.cu") + set(MOE_GEMM_SRC_CU + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_bf16_bf16.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_bf16_fp4.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_bf16_fp8.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_bf16_uint4.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_bf16_uint8.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_fp16_fp16.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_fp16_fp4.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_fp16_uint4.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_fp16_uint8.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_fp32_fp32.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_fp4_fp4.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_fp8_fp4.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_fp8_fp8.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_kernels_fp8_uint4.cu" + "${MOE_CUTLASS_DIR}/moe_gemm_tma_warp_specialized_input.cu" + "${MOE_CUTLASS_DIR}/moe_kernels.cu") set(MOE_GEMM_SRC_CU_LAUNCHER ${MOE_GEMM_SRC_CU}) list(FILTER MOE_GEMM_SRC_CU_LAUNCHER EXCLUDE REGEX ".*moe_gemm_kernels_.*") list(FILTER MOE_GEMM_SRC_CU INCLUDE REGEX ".*moe_gemm_kernels_.*") @@ -253,6 +265,12 @@ if(USING_OSS_CUTLASS_MOE_GEMM) list(FILTER MOE_GEMM_SRC_CU EXCLUDE REGEX ".*fp8.*") add_library(moe_gemm_src STATIC ${MOE_GEMM_SRC_CU} ${GROUPED_SRC_CPP}) + add_library(tllm::kernel_moe ALIAS moe_gemm_src) + target_link_libraries( + moe_gemm_src + PUBLIC tllm::kernel_gemm_utilities tllm::kernel_fp8_block_scale_gemm + tllm::kernel_lora tllm::kernel_pre_quant_scale + tllm::common_environment) add_library(_moe_gemm_launcher OBJECT ${MOE_GEMM_SRC_CU_LAUNCHER}) add_cuda_architectures(_moe_gemm_launcher 89) @@ -289,7 +307,14 @@ endif() if(USING_OSS_CUTLASS_ALLREDUCE_GEMM) add_library(ar_gemm_src STATIC ${ARGEMM_SRC_CU}) - target_link_libraries(ar_gemm_src PRIVATE ipc_nvls_memory_src) + add_library(tllm::kernel_gemm_all_reduce ALIAS ar_gemm_src) + target_link_libraries( + ar_gemm_src + PUBLIC tllm::kernel_cutlass_headers + PRIVATE tllm::runtime_ipc_nvls) + target_compile_features(ar_gemm_src PUBLIC cxx_std_20) + set_target_properties(ar_gemm_src PROPERTIES CUDA_STANDARD 20 + CUDA_STANDARD_REQUIRED ON) target_include_directories( ar_gemm_src PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../internal_cutlass_kernels/include) diff --git a/cpp/tensorrt_llm/kernels/kernelComponents.cmake b/cpp/tensorrt_llm/kernels/kernelComponents.cmake index 56260166f546..0f73dc404614 100644 --- a/cpp/tensorrt_llm/kernels/kernelComponents.cmake +++ b/cpp/tensorrt_llm/kernels/kernelComponents.cmake @@ -63,6 +63,32 @@ function(add_tllm_kernel_library target) PARENT_SCOPE) endfunction() +if(ENABLE_MULTI_DEVICE) + add_tllm_kernel_library( + tllm_kernel_custom_all_reduce SOURCES customAllReduceKernels.cu + LINK_LIBRARIES tllm::common_environment) +else() + list(REMOVE_ITEM SRC_CU "${CMAKE_CURRENT_LIST_DIR}/customAllReduceKernels.cu") +endif() + +add_tllm_kernel_library(tllm_kernel_rms_norm SOURCES rmsnormKernels.cu) + +add_tllm_kernel_library( + tllm_kernel_all_reduce_fusion + SOURCES + communicationKernels/allReduceFusionKernels.cu + communicationKernels/allReduceWorkspace.cu + LINK_LIBRARIES + tllm::common_environment + tllm::runtime_ipc) + +if(NOT USING_OSS_CUTLASS_ALLREDUCE_GEMM) + add_library(tllm_kernel_gemm_all_reduce INTERFACE) + add_library(tllm::kernel_gemm_all_reduce ALIAS tllm_kernel_gemm_all_reduce) + target_link_libraries(tllm_kernel_gemm_all_reduce + INTERFACE ${INTERNAL_CUTLASS_KERNELS_TARGET}) +endif() + add_tllm_kernel_library(tllm_kernel_logits SOURCES logitsBitmask.cu) add_tllm_kernel_library( @@ -124,7 +150,7 @@ add_tllm_kernel_library( LINK_LIBRARIES trtllm_gen_fmha_interface tllm::kernel_cutlass_headers) add_tllm_kernel_library( - tllm_kernel_cuda_core_gemm SOURCES weightOnlyBatchedGemv/cudaCoreGemm.cu + tllm_kernel_cuda_core_gemm SOURCES cudaCoreGemm/cudaCoreGemm.cu LINK_LIBRARIES tllm::common_environment tllm::kernel_cutlass_headers) add_tllm_kernel_library( @@ -182,3 +208,18 @@ add_tllm_kernel_library(tllm_kernel_quantization SOURCES quantization.cu add_tllm_kernel_library(tllm_kernel_pre_quant_scale SOURCES preQuantScaleKernel.cu) + +add_library(tllm_kernel_moe_internal INTERFACE) +add_library(tllm::kernel_moe_internal ALIAS tllm_kernel_moe_internal) +target_link_libraries( + tllm_kernel_moe_internal + INTERFACE ${INTERNAL_CUTLASS_KERNELS_TARGET} + CUDA::cuda_driver + tllm::kernel_gemm_utilities + tllm::kernel_fp8_block_scale_gemm + tllm::kernel_lora + tllm::kernel_pre_quant_scale + tllm::common_environment) +if(NOT USING_OSS_CUTLASS_MOE_GEMM) + add_library(tllm::kernel_moe ALIAS tllm_kernel_moe_internal) +endif() diff --git a/cpp/tensorrt_llm/runtime/CMakeLists.txt b/cpp/tensorrt_llm/runtime/CMakeLists.txt index 4bd99ba0ad0c..297ffba05449 100644 --- a/cpp/tensorrt_llm/runtime/CMakeLists.txt +++ b/cpp/tensorrt_llm/runtime/CMakeLists.txt @@ -14,10 +14,8 @@ # the License. include(FetchContent) -set(SRCS - bufferManagerNvls.cpp utils/debugUtils.cu ipcUtils.cpp - mcastDeviceMemory.cpp moeLoadBalancer/moeLoadBalancer.cpp - ncclCommunicator.cpp) +set(SRCS bufferManagerNvls.cpp utils/debugUtils.cu mcastDeviceMemory.cpp + moeLoadBalancer/moeLoadBalancer.cpp ncclCommunicator.cpp) include_directories(${API_INCLUDE_DIR}/tensorrt_llm/runtime) @@ -92,6 +90,18 @@ if(ENABLE_NVSHMEM) nvshmem::nvshmem_device) endif() +add_library(tllm_runtime_ipc STATIC ipcUtils.cpp) +add_library(tllm::runtime_ipc ALIAS tllm_runtime_ipc) +target_link_libraries( + tllm_runtime_ipc + PUBLIC tllm::runtime_buffers tllm::runtime_distributed + PRIVATE tllm::common_environment) +if(ENABLE_MULTI_DEVICE) + target_link_libraries(tllm_runtime_ipc PUBLIC tllm::kernel_custom_all_reduce) +endif() +target_compile_features(tllm_runtime_ipc PUBLIC cxx_std_20) +set_target_properties(tllm_runtime_ipc PROPERTIES POSITION_INDEPENDENT_CODE ON) + add_library(tllm_runtime_worker_pool STATIC workerPool.cpp) add_library(tllm::runtime_worker_pool ALIAS tllm_runtime_worker_pool) target_link_libraries( diff --git a/cpp/tensorrt_llm/thop/allreduceOp.cpp b/cpp/tensorrt_llm/thop/allreduceOp.cpp index cd01d858f230..a09425642278 100644 --- a/cpp/tensorrt_llm/thop/allreduceOp.cpp +++ b/cpp/tensorrt_llm/thop/allreduceOp.cpp @@ -20,7 +20,7 @@ #include "tensorrt_llm/common/customAllReduceUtils.h" #include "tensorrt_llm/common/dataType.h" #include "tensorrt_llm/common/mcastDevMemUtils.h" -#include "tensorrt_llm/common/ncclUtils.h" +#include "tensorrt_llm/common/ncclTorchUtils.h" #include "tensorrt_llm/common/nvmlWrapper.h" #include "tensorrt_llm/common/opUtils.h" #include "tensorrt_llm/common/tllmDataType.h" diff --git a/cpp/tensorrt_llm/thop/cublasScaledMM.cpp b/cpp/tensorrt_llm/thop/cublasScaledMM.cpp index cce16a297311..07f826d3f25b 100644 --- a/cpp/tensorrt_llm/thop/cublasScaledMM.cpp +++ b/cpp/tensorrt_llm/thop/cublasScaledMM.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (out) 1993-2026 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"); diff --git a/cpp/tensorrt_llm/thop/cudaNvfp4MM.cpp b/cpp/tensorrt_llm/thop/cudaNvfp4MM.cpp index d98bfc53986e..362a5fd914ca 100644 --- a/cpp/tensorrt_llm/thop/cudaNvfp4MM.cpp +++ b/cpp/tensorrt_llm/thop/cudaNvfp4MM.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (out) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,8 +15,8 @@ * limitations under the License. */ #include "tensorrt_llm/common/cudaUtils.h" +#include "tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemmNVFP4.h" #include "tensorrt_llm/kernels/userbuffers/ub_interface.h" -#include "tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemmNVFP4.h" #include "tensorrt_llm/runtime/torchUtils.h" #include "tensorrt_llm/thop/outputTensor.h" #include "tensorrt_llm/thop/thUtils.h" diff --git a/cpp/tensorrt_llm/thop/cudaScaledMM.cpp b/cpp/tensorrt_llm/thop/cudaScaledMM.cpp index ae21ee409aa1..b269492837a6 100644 --- a/cpp/tensorrt_llm/thop/cudaScaledMM.cpp +++ b/cpp/tensorrt_llm/thop/cudaScaledMM.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (out) 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"); @@ -15,8 +15,8 @@ * limitations under the License. */ #include "tensorrt_llm/common/cudaUtils.h" +#include "tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemm.h" #include "tensorrt_llm/kernels/userbuffers/ub_interface.h" -#include "tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemm.h" #include "tensorrt_llm/runtime/torchUtils.h" #include "tensorrt_llm/thop/outputTensor.h" #include "tensorrt_llm/thop/thUtils.h" diff --git a/cpp/tensorrt_llm/thop/cutlassScaledMM.cpp b/cpp/tensorrt_llm/thop/cutlassScaledMM.cpp index b314cb4d1657..7ad6b85a4164 100644 --- a/cpp/tensorrt_llm/thop/cutlassScaledMM.cpp +++ b/cpp/tensorrt_llm/thop/cutlassScaledMM.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright (out) 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"); diff --git a/cpp/tensorrt_llm/thop/outputTensor.h b/cpp/tensorrt_llm/thop/outputTensor.h index d9cde554852f..592bf7f65fc4 100644 --- a/cpp/tensorrt_llm/thop/outputTensor.h +++ b/cpp/tensorrt_llm/thop/outputTensor.h @@ -16,7 +16,7 @@ #pragma once #include "tensorrt_llm/common/logger.h" -#include "tensorrt_llm/common/ncclUtils.h" +#include "tensorrt_llm/common/ncclTorchUtils.h" #include "tensorrt_llm/common/opUtils.h" #include "tensorrt_llm/thop/userbuffersTensor.h" #include diff --git a/cpp/tests/unit_tests/common/CMakeLists.txt b/cpp/tests/unit_tests/common/CMakeLists.txt index b0cdbd04e94b..4af3d8624dc2 100644 --- a/cpp/tests/unit_tests/common/CMakeLists.txt +++ b/cpp/tests/unit_tests/common/CMakeLists.txt @@ -147,3 +147,14 @@ add_tllm_gtest( Threads::Threads LABELS "unit;common;host") + +add_tllm_gtest( + ncclWindowSupportTest + OWNER + common + SOURCES + ncclWindowSupportTest.cpp + LINK_LIBRARIES + tllm::common_nccl + LABELS + "unit;common;nccl;host") diff --git a/cpp/tests/unit_tests/common/ncclWindowSupportTest.cpp b/cpp/tests/unit_tests/common/ncclWindowSupportTest.cpp new file mode 100644 index 000000000000..6fb598de274e --- /dev/null +++ b/cpp/tests/unit_tests/common/ncclWindowSupportTest.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2025-2026, NVIDIA CORPORATION. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "tensorrt_llm/common/ncclUtils.h" + +#include + +#if ENABLE_MULTI_DEVICE +namespace nccl_util = tensorrt_llm::common::nccl_util; + +TEST(NCCLWindowSupportTest, RuntimeVersionAndGB10Gate) +{ +#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0) + EXPECT_FALSE(nccl_util::isNcclWindowSupportedForPlatform(121, true, NCCL_VERSION(2, 27, 9))); + EXPECT_FALSE(nccl_util::isNcclWindowSupportedForPlatform(121, true, NCCL_VERSION(2, 29, 2))); + EXPECT_FALSE(nccl_util::isNcclWindowSupportedForPlatform(121, true, NCCL_VERSION(2, 30, 3))); + + EXPECT_TRUE(nccl_util::isNcclWindowSupportedForPlatform(121, true, NCCL_VERSION(2, 30, 4))); + EXPECT_TRUE(nccl_util::isNcclWindowSupportedForPlatform(121, false, NCCL_VERSION(2, 29, 2))); + EXPECT_TRUE(nccl_util::isNcclWindowSupportedForPlatform(120, true, NCCL_VERSION(2, 29, 2))); + EXPECT_TRUE(nccl_util::isNcclWindowSupportedForPlatform(100, false, NCCL_VERSION(2, 29, 2))); +#else + GTEST_SKIP() << "NCCL window buffers are not compiled in"; +#endif +} + +#endif // ENABLE_MULTI_DEVICE diff --git a/cpp/tests/unit_tests/kernels/CMakeLists.txt b/cpp/tests/unit_tests/kernels/CMakeLists.txt index 92e96b26b928..c3ebb41d3ef2 100644 --- a/cpp/tests/unit_tests/kernels/CMakeLists.txt +++ b/cpp/tests/unit_tests/kernels/CMakeLists.txt @@ -70,16 +70,38 @@ macro(remove_compile_definition TARGET_NAME DEFINITION) endif() endmacro() -add_gtest(mixtureOfExpertsTest moe/mixtureOfExpertsTest.cu) +add_tllm_gtest( + mixtureOfExpertsTest + OWNER + kernel + SOURCES + moe/mixtureOfExpertsTest.cu + LINK_LIBRARIES + tllm::kernel_moe + tllm::kernel_quantization + tllm::runtime_buffers + LABELS + "unit;kernels;moe;gpu") # If we are using oss cutlass, build an explicit internal test if(USING_OSS_CUTLASS_MOE_GEMM) - target_link_libraries(mixtureOfExpertsTest PRIVATE moe_gemm_src) target_compile_definitions(mixtureOfExpertsTest PUBLIC USING_OSS_CUTLASS_MOE_GEMM) - add_gtest(mixtureOfExpertsInternalTest moe/mixtureOfExpertsTest.cu - TEST_PREFIX "Internal.") + add_tllm_gtest( + mixtureOfExpertsInternalTest + TEST_PREFIX + "Internal." + OWNER + kernel + SOURCES + moe/mixtureOfExpertsTest.cu + LINK_LIBRARIES + tllm::kernel_moe_internal + tllm::kernel_quantization + tllm::runtime_buffers + LABELS + "unit;kernels;moe;gpu") remove_compile_definition(mixtureOfExpertsInternalTest USING_OSS_CUTLASS_MOE_GEMM) endif() diff --git a/cpp/tests/unit_tests/kernels/cudaCoreGemm/cudaCoreGemmKernelTest.cpp b/cpp/tests/unit_tests/kernels/cudaCoreGemm/cudaCoreGemmKernelTest.cpp index 13a261cce505..579d0e17d343 100644 --- a/cpp/tests/unit_tests/kernels/cudaCoreGemm/cudaCoreGemmKernelTest.cpp +++ b/cpp/tests/unit_tests/kernels/cudaCoreGemm/cudaCoreGemmKernelTest.cpp @@ -24,7 +24,7 @@ #include "cutlass/numeric_types.h" #include "tensorrt_llm/common/cudaUtils.h" #include "tensorrt_llm/common/quantization.h" -#include "tensorrt_llm/kernels/weightOnlyBatchedGemv/cudaCoreGemm.h" +#include "tensorrt_llm/kernels/cudaCoreGemm/cudaCoreGemm.h" #include #include diff --git a/cpp/tests/unit_tests/multi_gpu/CMakeLists.txt b/cpp/tests/unit_tests/multi_gpu/CMakeLists.txt index 12ebe93594c4..83b31a64f83f 100644 --- a/cpp/tests/unit_tests/multi_gpu/CMakeLists.txt +++ b/cpp/tests/unit_tests/multi_gpu/CMakeLists.txt @@ -61,14 +61,23 @@ add_tllm_gtest( tllm::runtime_distributed LABELS "unit;kernels;userbuffers;multi-gpu;gpu") -add_tllm_gtest(ncclUtilsTest SOURCES ncclUtilsTest.cpp FULL_STACK LABELS - "unit;full-stack;gpu") -target_link_libraries(ncclUtilsTest PRIVATE ${Python3_LIBRARIES}) +add_tllm_gtest( + ncclUtilsTest + OWNER + common + SOURCES + ncclUtilsTest.cpp + LINK_LIBRARIES + tllm::common_nccl_comm + tllm::runtime_memory + LABELS + "unit;common;nccl;multi-gpu;gpu") if(BUILD_PYT) target_compile_definitions(cacheTransceiverTest PUBLIC BUILD_PYT) target_link_libraries(cacheTransceiverTest PUBLIC ${TORCH_LIBRARIES}) endif() if(BUILD_PYT) - target_compile_definitions(ncclUtilsTest PUBLIC BUILD_PYT) - target_link_libraries(ncclUtilsTest PUBLIC ${TORCH_LIBRARIES}) + target_compile_definitions(ncclUtilsTest PRIVATE BUILD_PYT) + target_link_libraries(ncclUtilsTest PRIVATE ${TORCH_LIBRARIES} + Python3::Python) endif() diff --git a/cpp/tests/unit_tests/multi_gpu/kernels/CMakeLists.txt b/cpp/tests/unit_tests/multi_gpu/kernels/CMakeLists.txt index c883cd35c5aa..82cc821d889a 100644 --- a/cpp/tests/unit_tests/multi_gpu/kernels/CMakeLists.txt +++ b/cpp/tests/unit_tests/multi_gpu/kernels/CMakeLists.txt @@ -13,15 +13,51 @@ # License for the specific language governing permissions and limitations under # the License. -if(NOT ENABLE_MULTI_DEVICE EQUAL 0) - add_tllm_gtest(allReduceKernelTest SOURCES allReduce/allReduceKernelTest.cu - FULL_STACK LABELS "unit;full-stack;gpu") - add_tllm_gtest(allReduceFusionTest SOURCES allReduce/allReduceFusionTest.cu - FULL_STACK LABELS "unit;full-stack;gpu") - add_tllm_gtest(gemmAllReduceTest SOURCES allReduce/gemmAllReduceTest.cu - FULL_STACK LABELS "unit;full-stack;gpu") +if(ENABLE_MULTI_DEVICE) + add_tllm_gtest( + allReduceKernelTest + OWNER + kernel + SOURCES + allReduce/allReduceKernelTest.cu + LINK_LIBRARIES + tllm::kernel_custom_all_reduce + tllm::runtime_ipc + tllm::runtime_buffers + tllm::runtime_distributed + LABELS + "unit;kernels;all-reduce;multi-gpu;gpu") + add_tllm_gtest( + allReduceFusionTest + OWNER + kernel + SOURCES + allReduce/allReduceFusionTest.cu + LINK_LIBRARIES + tllm::kernel_all_reduce_fusion + tllm::kernel_rms_norm + tllm::kernel_quantization + tllm::runtime_distributed + ${NCCL_LIB} + LABELS + "unit;kernels;all-reduce;multi-gpu;gpu") + add_tllm_gtest( + gemmAllReduceTest + OWNER + kernel + SOURCES + allReduce/gemmAllReduceTest.cu + LINK_LIBRARIES + tllm::kernel_gemm_all_reduce + tllm::kernel_cutlass_headers + tllm::kernel_userbuffers + tllm::runtime_ipc_nvls + tllm::runtime_distributed + ${NCCL_LIB} + LABELS + "unit;kernels;all-reduce;multi-gpu;gpu" + NO_GTEST_MAIN) if(USING_OSS_CUTLASS_ALLREDUCE_GEMM) - target_link_libraries(gemmAllReduceTest PRIVATE ar_gemm_src) target_compile_definitions(gemmAllReduceTest PRIVATE USING_OSS_CUTLASS_ALLREDUCE_GEMM) endif() diff --git a/cpp/tests/unit_tests/multi_gpu/kernels/allReduce/gemmAllReduceTest.cu b/cpp/tests/unit_tests/multi_gpu/kernels/allReduce/gemmAllReduceTest.cu index 2ff2c8c37b9c..7c7e3e9d2efd 100644 --- a/cpp/tests/unit_tests/multi_gpu/kernels/allReduce/gemmAllReduceTest.cu +++ b/cpp/tests/unit_tests/multi_gpu/kernels/allReduce/gemmAllReduceTest.cu @@ -955,6 +955,10 @@ TYPED_TEST(GemmAllReduceFixture, RunnerTest) int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); + if (::testing::GTEST_FLAG(list_tests)) + { + return RUN_ALL_TESTS(); + } bool notSupported = false; // CUDA 12 minimum required diff --git a/cpp/tests/unit_tests/multi_gpu/ncclUtilsTest.cpp b/cpp/tests/unit_tests/multi_gpu/ncclUtilsTest.cpp index 022b7d1b994f..7105c2bdc296 100644 --- a/cpp/tests/unit_tests/multi_gpu/ncclUtilsTest.cpp +++ b/cpp/tests/unit_tests/multi_gpu/ncclUtilsTest.cpp @@ -34,6 +34,7 @@ #include #if ENABLE_MULTI_DEVICE && BUILD_PYT +#include "tensorrt_llm/common/ncclTorchUtils.h" #include #endif @@ -237,22 +238,6 @@ TEST(NCCLWindowAllocatorUsageTest, UsageIsReadWithoutWaitingForAnAllocationInPro #endif } -TEST(NCCLWindowSupportTest, RuntimeVersionAndGB10Gate) -{ -#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0) - EXPECT_FALSE(nccl_util::isNcclWindowSupportedForPlatform(121, true, NCCL_VERSION(2, 27, 9))); - EXPECT_FALSE(nccl_util::isNcclWindowSupportedForPlatform(121, true, NCCL_VERSION(2, 29, 2))); - EXPECT_FALSE(nccl_util::isNcclWindowSupportedForPlatform(121, true, NCCL_VERSION(2, 30, 3))); - - EXPECT_TRUE(nccl_util::isNcclWindowSupportedForPlatform(121, true, NCCL_VERSION(2, 30, 4))); - EXPECT_TRUE(nccl_util::isNcclWindowSupportedForPlatform(121, false, NCCL_VERSION(2, 29, 2))); - EXPECT_TRUE(nccl_util::isNcclWindowSupportedForPlatform(120, true, NCCL_VERSION(2, 29, 2))); - EXPECT_TRUE(nccl_util::isNcclWindowSupportedForPlatform(100, false, NCCL_VERSION(2, 29, 2))); -#else - GTEST_SKIP() << "NCCL window buffers are not compiled in"; -#endif -} - // Helper function to create a split communicator for testing // This allows us to test cleanup behavior explicitly by controlling the lifetime std::shared_ptr createSplitComm(ncclComm_t parentComm, int color, int key)