#include <gtest/gtest.h>
#include <torch/csrc/distributed/c10d/symm_mem/nccl_dev_cap.hpp>
#ifdef NCCL_HAS_SYMMEM_DEVICE_SUPPORT
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <c10/cuda/CUDAStream.h>
#include <c10/util/Exception.h>
#include <c10/util/ScopeExit.h>
#include <torch/csrc/distributed/c10d/symm_mem/nccl_devcomm_manager.hpp>
using namespace c10d::symmetric_memory;
void test_register_and_get_comm() {
const std::string group_name = "test_group_comm";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
ncclComm_t mock_comm = nullptr;
manager.register_comm(group_name, mock_comm);
ncclComm_t retrieved_comm = manager.get_comm(group_name);
EXPECT_EQ(retrieved_comm, mock_comm);
manager.register_comm(group_name, mock_comm);
EXPECT_THROW(manager.get_comm("nonexistent_group"), c10::Error);
}
void test_register_and_get_default_key() {
const std::string group_name = "test_group";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
ncclDevComm devcomm0 = {};
auto devcomm_opt = manager.register_devcomm(group_name, devcomm0);
EXPECT_TRUE(devcomm_opt.has_value());
auto retrieved_opt = manager.get_devcomm(group_name);
EXPECT_TRUE(retrieved_opt.has_value());
}
void test_register_and_get_custom_key() {
const std::string group_name = "test_group_custom";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
ncclDevComm devcomm0 = {};
ncclDevComm devcomm1 = {};
auto devcomm0_opt =
manager.register_devcomm(group_name, devcomm0, "custom_key0");
EXPECT_TRUE(devcomm0_opt.has_value());
auto devcomm1_opt =
manager.register_devcomm(group_name, devcomm1, "custom_key1");
EXPECT_TRUE(devcomm1_opt.has_value());
auto retrieved0_opt = manager.get_devcomm(group_name, "custom_key0");
EXPECT_TRUE(retrieved0_opt.has_value());
auto retrieved1_opt = manager.get_devcomm(group_name, "custom_key1");
EXPECT_TRUE(retrieved1_opt.has_value());
}
void test_duplicate_registration_throws() {
const std::string group_name = "test_group_duplicate";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
ncclDevComm devcomm0 = {};
ncclDevComm devcomm1 = {};
auto devcomm0_opt =
manager.register_devcomm(group_name, devcomm0, "duplicate_key");
EXPECT_TRUE(devcomm0_opt.has_value());
EXPECT_THROW(
manager.register_devcomm(group_name, devcomm1, "duplicate_key"),
c10::Error);
}
void test_get_nonexistent_key() {
const std::string group_name = "test_group_nonexistent";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
auto retrieved_opt = manager.get_devcomm(group_name, "nonexistent_key");
EXPECT_FALSE(retrieved_opt.has_value());
auto retrieved_group_opt =
manager.get_devcomm("nonexistent_group", "any_key");
EXPECT_FALSE(retrieved_group_opt.has_value());
}
void test_multiple_groups() {
const std::string group1 = "group1";
const std::string group2 = "group2";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
ncclDevComm devcomm1a = {};
ncclDevComm devcomm1b = {};
ncclDevComm devcomm2a = {};
auto devcomm1a_opt = manager.register_devcomm(group1, devcomm1a, "key1a");
EXPECT_TRUE(devcomm1a_opt.has_value());
auto devcomm1b_opt = manager.register_devcomm(group1, devcomm1b, "key1b");
EXPECT_TRUE(devcomm1b_opt.has_value());
auto devcomm2a_opt = manager.register_devcomm(group2, devcomm2a, "key2a");
EXPECT_TRUE(devcomm2a_opt.has_value());
auto retrieved1a_opt = manager.get_devcomm(group1, "key1a");
EXPECT_TRUE(retrieved1a_opt.has_value());
auto retrieved1b_opt = manager.get_devcomm(group1, "key1b");
EXPECT_TRUE(retrieved1b_opt.has_value());
auto retrieved2a_opt = manager.get_devcomm(group2, "key2a");
EXPECT_TRUE(retrieved2a_opt.has_value());
auto cross_group_opt = manager.get_devcomm(group1, "key2a");
EXPECT_FALSE(cross_group_opt.has_value());
}
void test_example_pattern() {
const std::string group_name = "example_group";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
auto foo = [&manager, &group_name]() {
auto devcomm_opt = manager.get_devcomm(group_name);
if (!devcomm_opt) {
ncclDevComm devcomm = {};
devcomm_opt = manager.register_devcomm(group_name, devcomm);
}
ncclDevComm devcomm_copy = *devcomm_opt;
(void)devcomm_copy;
EXPECT_TRUE(devcomm_opt.has_value());
};
foo();
auto bar = [&manager, &group_name]() {
ncclDevComm devcomm0 = {};
ncclDevComm devcomm1 = {};
manager.register_devcomm(group_name, devcomm0, "bar0");
manager.register_devcomm(group_name, devcomm1, "bar1");
auto bar0_opt = manager.get_devcomm(group_name, "bar0");
EXPECT_TRUE(bar0_opt.has_value());
auto bar1_opt = manager.get_devcomm(group_name, "bar1");
EXPECT_TRUE(bar1_opt.has_value());
};
bar();
}
TEST(NCCLDevCommManagerTest, RegisterAndGetComm) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "CUDA not available, skipping test";
}
test_register_and_get_comm();
}
TEST(NCCLDevCommManagerTest, RegisterAndGetDefaultKey) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "CUDA not available, skipping test";
}
test_register_and_get_default_key();
}
TEST(NCCLDevCommManagerTest, RegisterAndGetCustomKey) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "CUDA not available, skipping test";
}
test_register_and_get_custom_key();
}
TEST(NCCLDevCommManagerTest, DuplicateRegistrationThrows) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "CUDA not available, skipping test";
}
test_duplicate_registration_throws();
}
TEST(NCCLDevCommManagerTest, GetNonexistentKey) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "CUDA not available, skipping test";
}
test_get_nonexistent_key();
}
TEST(NCCLDevCommManagerTest, MultipleGroups) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "CUDA not available, skipping test";
}
test_multiple_groups();
}
TEST(NCCLDevCommManagerTest, ExamplePattern) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "CUDA not available, skipping test";
}
test_example_pattern();
}
TEST(NCCLDevCommManagerTest, ReplacementRegistrationEvictsDevComms) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "CUDA not available, skipping test";
}
c10::cuda::CUDAGuard guard(0);
ncclUniqueId first_id;
ncclUniqueId successor_id;
ASSERT_EQ(ncclGetUniqueId(&first_id), ncclSuccess);
ASSERT_EQ(ncclGetUniqueId(&successor_id), ncclSuccess);
ncclComm_t first_comm = nullptr;
ncclComm_t successor_comm = nullptr;
ASSERT_EQ(ncclCommInitRank(&first_comm, 1, first_id, 0), ncclSuccess);
ASSERT_EQ(ncclCommInitRank(&successor_comm, 1, successor_id, 0), ncclSuccess);
const std::string group_name = "replacement_evicts_devcomms";
const std::string bystander_name = "replacement_evicts_devcomms_bystander";
const std::string key = "evict_key";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
auto cleanup = c10::make_scope_exit([&]() {
manager.unregister_comm(group_name);
manager.unregister_comm(bystander_name);
EXPECT_EQ(ncclCommDestroy(first_comm), ncclSuccess);
EXPECT_EQ(ncclCommDestroy(successor_comm), ncclSuccess);
});
ncclDevComm devcomm = {};
manager.register_comm(group_name, first_comm);
ASSERT_TRUE(manager.register_devcomm(group_name, devcomm, key).has_value());
ASSERT_TRUE(manager.get_devcomm(group_name, key).has_value());
manager.register_comm(bystander_name, first_comm);
ASSERT_TRUE(
manager.register_devcomm(bystander_name, devcomm, key).has_value());
manager.register_comm(group_name, first_comm);
EXPECT_TRUE(manager.get_devcomm(group_name, key).has_value());
manager.register_comm(group_name, successor_comm);
EXPECT_FALSE(manager.get_devcomm(group_name, key).has_value());
EXPECT_TRUE(manager.get_devcomm(bystander_name, key).has_value());
EXPECT_TRUE(manager.register_devcomm(group_name, devcomm, key).has_value());
manager.unregister_comm(group_name, first_comm);
EXPECT_EQ(manager.get_comm(group_name), successor_comm);
}
#ifdef USE_ROCM
TEST(NCCLDevCommManagerTest, IdentitySafeUnregisterPreservesSuccessor) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "ROCm not available, skipping test";
}
c10::cuda::CUDAGuard guard(0);
ncclUniqueId first_id;
ncclUniqueId successor_id;
ASSERT_EQ(ncclGetUniqueId(&first_id), ncclSuccess);
ASSERT_EQ(ncclGetUniqueId(&successor_id), ncclSuccess);
ncclComm_t first_comm = nullptr;
ncclComm_t successor_comm = nullptr;
ASSERT_EQ(ncclCommInitRank(&first_comm, 1, first_id, 0), ncclSuccess);
ASSERT_EQ(ncclCommInitRank(&successor_comm, 1, successor_id, 0), ncclSuccess);
const std::string group_name = "identity_safe_unregister";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
manager.register_comm(group_name, first_comm);
const auto first_generation =
manager.get_comm_generation(group_name, first_comm);
manager.register_comm(group_name, successor_comm);
const auto successor_generation =
manager.get_comm_generation(group_name, successor_comm);
EXPECT_NE(first_generation, successor_generation);
EXPECT_FALSE(manager.comm_registration_is_live(
group_name, first_comm, first_generation));
EXPECT_TRUE(manager.comm_registration_is_live(
group_name, successor_comm, successor_generation));
manager.unregister_comm(group_name, first_comm);
EXPECT_EQ(manager.get_comm(group_name), successor_comm);
manager.unregister_comm(group_name, successor_comm);
EXPECT_THROW(manager.get_comm(group_name), c10::Error);
EXPECT_EQ(ncclCommDestroy(first_comm), ncclSuccess);
EXPECT_EQ(ncclCommDestroy(successor_comm), ncclSuccess);
}
TEST(NCCLDevCommManagerTest, SamePointerReregistrationKeepsGeneration) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "ROCm not available, skipping test";
}
c10::cuda::CUDAGuard guard(0);
ncclUniqueId comm_id;
ASSERT_EQ(ncclGetUniqueId(&comm_id), ncclSuccess);
ncclComm_t comm = nullptr;
ASSERT_EQ(ncclCommInitRank(&comm, 1, comm_id, 0), ncclSuccess);
const std::string group_name = "same_pointer_reregistration";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
manager.register_comm(group_name, comm);
const auto generation = manager.get_comm_generation(group_name, comm);
ASSERT_TRUE(manager.comm_registration_is_live(group_name, comm, generation));
manager.register_comm(group_name, comm);
EXPECT_EQ(manager.get_comm_generation(group_name, comm), generation);
EXPECT_TRUE(manager.comm_registration_is_live(group_name, comm, generation));
manager.unregister_comm(group_name, comm);
EXPECT_THROW(manager.get_comm(group_name), c10::Error);
EXPECT_FALSE(manager.comm_registration_is_live(group_name, comm, generation));
EXPECT_EQ(ncclCommDestroy(comm), ncclSuccess);
}
TEST(NCCLDevCommManagerTest, ReregistrationAfterUnregisterAdvancesGeneration) {
if (!at::cuda::is_available()) {
GTEST_SKIP() << "ROCm not available, skipping test";
}
c10::cuda::CUDAGuard guard(0);
ncclUniqueId comm_id;
ASSERT_EQ(ncclGetUniqueId(&comm_id), ncclSuccess);
ncclComm_t comm = nullptr;
ASSERT_EQ(ncclCommInitRank(&comm, 1, comm_id, 0), ncclSuccess);
const std::string group_name = "reregistration_after_unregister";
c10::Device device(c10::DeviceType::CUDA, 0);
auto& manager = NCCLDevCommManager::get(device);
manager.register_comm(group_name, comm);
const auto stale_generation = manager.get_comm_generation(group_name, comm);
manager.unregister_comm(group_name, comm);
ASSERT_THROW(manager.get_comm(group_name), c10::Error);
manager.register_comm(group_name, comm);
const auto fresh_generation = manager.get_comm_generation(group_name, comm);
EXPECT_NE(stale_generation, fresh_generation);
EXPECT_FALSE(
manager.comm_registration_is_live(group_name, comm, stale_generation));
EXPECT_TRUE(
manager.comm_registration_is_live(group_name, comm, fresh_generation));
manager.unregister_comm(group_name, comm, stale_generation);
EXPECT_TRUE(
manager.comm_registration_is_live(group_name, comm, fresh_generation));
manager.unregister_comm(group_name, comm);
EXPECT_EQ(ncclCommDestroy(comm), ncclSuccess);
}
#endif
#else
TEST(NCCLDevCommManagerTest, Skipped) {
GTEST_SKIP() << "NCCL symmetric memory device support not available";
}
#endif