-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathncclUtils.cpp
More file actions
733 lines (641 loc) · 24.6 KB
/
Copy pathncclUtils.cpp
File metadata and controls
733 lines (641 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
* 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"
#if ENABLE_MULTI_DEVICE
#include "tensorrt_llm/common/assert.h"
#include "tensorrt_llm/common/cudaUtils.h"
#include "tensorrt_llm/common/logger.h"
#include <limits>
#include <stdexcept>
namespace
{
// RAII guard for cudaMalloc. Frees the pointer on destruction, logging a warning on failure.
struct CudaMallocGuard
{
void* ptr{nullptr};
explicit CudaMallocGuard(void* p) noexcept
: ptr(p)
{
}
~CudaMallocGuard()
{
if (ptr)
{
TLLM_CUDA_CHECK_WARN(cudaFree(ptr));
}
}
void* release() noexcept
{
void* p = ptr;
ptr = nullptr;
return p;
}
CudaMallocGuard(CudaMallocGuard const&) = delete;
CudaMallocGuard& operator=(CudaMallocGuard const&) = delete;
};
// RAII guard for ncclMemAlloc. Frees the pointer on destruction, logging a warning on failure.
struct NcclMemGuard
{
void* ptr{nullptr};
explicit NcclMemGuard(void* p) noexcept
: ptr(p)
{
}
~NcclMemGuard()
{
if (ptr)
{
TLLM_NCCL_CHECK_WARN(ncclMemFree(ptr));
}
}
void* release() noexcept
{
void* p = ptr;
ptr = nullptr;
return p;
}
NcclMemGuard(NcclMemGuard const&) = delete;
NcclMemGuard& operator=(NcclMemGuard const&) = delete;
};
} // namespace
namespace tensorrt_llm::common::nccl_util
{
//==============================================================================
// NcclCommResourceManager Implementation
//==============================================================================
NcclCommResourceManager& NcclCommResourceManager::getInstance() noexcept
{
static NcclCommResourceManager instance;
return instance;
}
NcclCommResourceManager::~NcclCommResourceManager()
{
// Mark that we're in destruction to prevent cleanup attempts from deleters
// that may run during static destruction
mIsDestroying.store(true, std::memory_order_release);
// Proactively clean up all resources before destruction
// This ensures cleanup happens in a controlled manner before static destruction
std::vector<std::pair<ncclComm_t, std::vector<ResourceEntry>>> allResources;
{
std::lock_guard<std::mutex> lock(mMutex);
// Move all resources out of the map
allResources.reserve(mCommResources.size());
for (auto& [comm, resources] : mCommResources)
{
allResources.emplace_back(comm, std::move(resources));
}
mCommResources.clear();
}
// Clean up all resources outside the lock
// Note: We don't call ncclCommDestroy here - that's the responsibility
// of the shared_ptr deleter. We just clean up registered resources.
for (auto& [comm, resources] : allResources)
{
for (auto& [cleanup, name] : resources)
{
try
{
cleanup();
}
catch (...)
{
// Ignore exceptions during destruction
}
}
}
}
void NcclCommResourceManager::registerResource(ncclComm_t comm, ResourceCleanupFunc cleanup, char const* debugName)
{
if (!comm)
{
TLLM_LOG_WARNING("[NCCLUtil] Attempted to register resource for null NCCL comm");
return;
}
std::lock_guard<std::mutex> lock(mMutex);
auto& resources = mCommResources[comm];
resources.emplace_back(std::move(cleanup), debugName ? debugName : "unnamed");
TLLM_LOG_TRACE("[NCCLUtil] Registered resource '%s' for NCCL comm %p (total: %zu)",
debugName ? debugName : "unnamed", static_cast<void*>(comm), resources.size());
}
void NcclCommResourceManager::cleanupResources(ncclComm_t comm) noexcept
{
if (!comm)
{
return;
}
// Check if we're in the process of being destroyed
// If so, skip cleanup - the destructor will handle it proactively
if (mIsDestroying.load(std::memory_order_acquire))
{
return;
}
std::vector<ResourceEntry> resourcesToClean;
{
// During static destruction, mutex and logging may not be safe.
// Use try-catch to handle any issues gracefully.
try
{
std::lock_guard<std::mutex> lock(mMutex);
// Double-check after acquiring lock (destruction may have started)
if (mIsDestroying.load(std::memory_order_acquire))
{
return;
}
auto it = mCommResources.find(comm);
if (it == mCommResources.end())
{
// Nothing registered for this comm, nothing to clean up
return;
}
// Move resources out (preserves order) and remove from map
resourcesToClean = std::move(it->second);
mCommResources.erase(it);
// Logging may fail during static destruction, so wrap in try-catch
try
{
TLLM_LOG_TRACE("[NCCLUtil] Cleaning up %zu resources for NCCL comm %p", resourcesToClean.size(),
static_cast<void*>(comm));
}
catch (...)
{
// Ignore logging failures during static destruction
}
}
catch (...)
{
// If mutex access fails during static destruction, just return.
// This prevents segfaults when the singleton is being destroyed.
return;
}
}
// Clean up outside the lock to avoid deadlocks if cleanup functions try to access the manager
// Order is preserved: resources are cleaned up in registration order
for (auto& [cleanup, name] : resourcesToClean)
{
try
{
// Logging may fail during static destruction, so wrap in try-catch
try
{
TLLM_LOG_TRACE(
"[NCCLUtil] Cleaning up resource '%s' for NCCL comm %p", name.c_str(), static_cast<void*>(comm));
}
catch (...)
{
// Ignore logging failures during static destruction
}
cleanup();
}
catch (std::exception const& e)
{
try
{
TLLM_LOG_ERROR("[NCCLUtil] Exception during cleanup of resource '%s' for NCCL comm %p: %s",
name.c_str(), static_cast<void*>(comm), e.what());
}
catch (...)
{
// Ignore logging failures during static destruction
}
}
catch (...)
{
try
{
TLLM_LOG_ERROR("[NCCLUtil] Unknown exception during cleanup of resource '%s' for NCCL comm %p",
name.c_str(), static_cast<void*>(comm));
}
catch (...)
{
// Ignore logging failures during static destruction
}
}
}
}
bool NcclCommResourceManager::hasResources(ncclComm_t comm) const noexcept
{
std::lock_guard<std::mutex> lock(mMutex);
return mCommResources.find(comm) != mCommResources.end();
}
size_t NcclCommResourceManager::getResourceCount(ncclComm_t comm) const noexcept
{
std::lock_guard<std::mutex> lock(mMutex);
auto it = mCommResources.find(comm);
return it != mCommResources.end() ? it->second.size() : 0;
}
//==============================================================================
// NCCLWindowAllocator Implementation
//==============================================================================
#if NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0)
NCCLWindowAllocator& NCCLWindowAllocator::getInstance()
{
static NCCLWindowAllocator instance;
return instance;
}
NCCLWindowBuffer NCCLWindowAllocator::requestBuffer(ncclComm_t comm, size_t size)
{
// One-time runtime version check: the runtime NCCL library must also support window buffers.
static std::once_flag versionCheckFlag;
static bool runtimeVersionOk = false;
std::call_once(versionCheckFlag,
[]()
{
int version = 0;
if (ncclGetVersion(&version) == ncclSuccess && version >= NCCL_VERSION(2, 28, 0))
{
runtimeVersionOk = true;
}
else
{
TLLM_LOG_WARNING(
"[NCCLUtil] NCCL runtime version %d.%d.%d does not support window buffers; "
"falling back to regular tensors.",
version / 10000, (version % 10000) / 100, version % 100);
}
});
if (!runtimeVersionOk)
{
return NCCLWindowBuffer();
}
TLLM_CHECK_WITH_INFO(comm != nullptr, "NCCL communicator cannot be null");
TLLM_CHECK_WITH_INFO(size > 0, "Buffer size must be greater than 0");
std::lock_guard<std::mutex> lock(mMutex);
// Register cleanup callback for this communicator if not already registered
// This is cheap even if no buffers exist yet - cleanup will just return early
registerBufferCleanup(comm);
// Check if we have an available buffer of at least the requested size for this communicator
// Use best-fit: find the smallest buffer that's >= requested size
auto& commBuffers = mBufferPool[comm];
auto bestFit = commBuffers.end();
size_t bestFitSize = std::numeric_limits<size_t>::max();
for (auto it = commBuffers.begin(); it != commBuffers.end(); ++it)
{
if (!it->inUse && it->buffer.size >= size && it->buffer.size < bestFitSize)
{
bestFit = it;
bestFitSize = it->buffer.size;
}
}
if (bestFit != commBuffers.end())
{
bestFit->inUse = true;
TLLM_LOG_TRACE(
"[NCCLUtil] Reusing NCCL window buffer for comm %p: handle=%d, ptr=%p, size=%zu (requested: %zu)",
static_cast<void*>(comm), bestFit->buffer.handle, bestFit->buffer.ptr, bestFit->buffer.size, size);
return bestFit->buffer;
}
// If a previous allocateAndRegisterBuffer call collectively failed for this comm at a size
// no larger than this request, do not retry the known-failing new allocation path. Smaller
// requests and already-pooled buffers can still use NCCL windows.
auto const failureIt = mMinSymmetricFailureSize.find(comm);
if (failureIt != mMinSymmetricFailureSize.end() && size >= failureIt->second)
{
TLLM_LOG_DEBUG("[NCCLUtil] Skipping NCCL window allocation for comm %p, size=%zu; known failure threshold=%zu",
static_cast<void*>(comm), size, failureIt->second);
return NCCLWindowBuffer();
}
// No available buffer found, avoid registration during CUDA graph capture
auto stream = at::cuda::getCurrentCUDAStream();
cudaStreamCaptureStatus capture_status = cudaStreamCaptureStatusNone;
auto capture_err = cudaStreamIsCapturing(stream, &capture_status);
if (capture_err != cudaSuccess)
{
TLLM_LOG_DEBUG("[NCCLUtil] cudaStreamIsCapturing failed: %s", cudaGetErrorString(capture_err));
}
if (capture_err == cudaSuccess && capture_status != cudaStreamCaptureStatusNone)
{
TLLM_LOG_DEBUG("[NCCLUtil] Skipping NCCL window allocation during capture for comm %p (requested: %zu)",
static_cast<void*>(comm), size);
return NCCLWindowBuffer();
}
// No available buffer found, allocate a new one
TLLM_LOG_TRACE(
"[NCCLUtil] Allocating new NCCL window buffer for comm %p, size=%zu", static_cast<void*>(comm), size);
int handle = static_cast<int>(commBuffers.size());
NCCLWindowBuffer buffer = allocateAndRegisterBuffer(comm, size, handle);
// Only cache valid buffers. allocateAndRegisterBuffer returns an empty buffer when any rank
// failed ncclMemAlloc (collective fallback to plain allreduce); caching it would leak a
// permanently "in use" empty entry per request because releaseBuffer is a no-op for nullptr.
if (buffer.isValid())
{
commBuffers.push_back({buffer, true});
}
else
{
// The collective allreduce inside allocateAndRegisterBuffer agreed that this request
// cannot use symmetric memory on at least one rank. Remember the smallest failing
// request size so repeated too-large autotuner probes do not keep stressing this path.
recordSymmetricFailureLocked(comm, size);
}
return buffer;
}
void NCCLWindowAllocator::recordSymmetricFailureLocked(ncclComm_t comm, size_t size)
{
auto failureIt = mMinSymmetricFailureSize.find(comm);
if (failureIt == mMinSymmetricFailureSize.end())
{
mMinSymmetricFailureSize.emplace(comm, size);
}
else if (size < failureIt->second)
{
failureIt->second = size;
}
}
cudaError_t NCCLWindowAllocator::clearCudaErrorIfSymmetricAllocationFailed(
int localAllocOk, CudaGetLastErrorFunc getLastError) noexcept
{
if (localAllocOk == 0)
{
return getLastError();
}
return cudaSuccess;
}
NCCLWindowBuffer NCCLWindowAllocator::searchBuffer(ncclComm_t comm, void* ptr) const
{
if (!comm || !ptr)
{
return NCCLWindowBuffer();
}
std::lock_guard<std::mutex> lock(mMutex);
return searchBufferLocked(comm, ptr);
}
void NCCLWindowAllocator::releaseBuffer(ncclComm_t comm, void* ptr)
{
if (!comm || !ptr)
{
return;
}
std::lock_guard<std::mutex> lock(mMutex);
auto commIt = mBufferPool.find(comm);
if (commIt == mBufferPool.end())
{
TLLM_LOG_WARNING(
"[NCCLUtil] Attempted to release buffer %p for unknown comm %p", ptr, static_cast<void*>(comm));
return;
}
for (auto& entry : commIt->second)
{
if (entry.buffer.ptr == ptr)
{
entry.inUse = false;
TLLM_LOG_TRACE("[NCCLUtil] Released NCCL window buffer for comm %p: ptr=%p", static_cast<void*>(comm), ptr);
return;
}
}
TLLM_LOG_WARNING("[NCCLUtil] Attempted to release unknown buffer %p for comm %p", ptr, static_cast<void*>(comm));
}
ncclWindow_t NCCLWindowAllocator::getWindow(ncclComm_t comm, void* ptr) const
{
std::lock_guard<std::mutex> lock(mMutex);
NCCLWindowBuffer buffer = searchBufferLocked(comm, ptr);
return buffer.isValid() ? buffer.window : nullptr;
}
size_t NCCLWindowAllocator::getSize(ncclComm_t comm, void* ptr) const
{
std::lock_guard<std::mutex> lock(mMutex);
NCCLWindowBuffer buffer = searchBufferLocked(comm, ptr);
return buffer.isValid() ? buffer.size : 0;
}
NCCLWindowBuffer NCCLWindowAllocator::getBufferInfo(ncclComm_t comm, void* ptr) const
{
std::lock_guard<std::mutex> lock(mMutex);
return searchBufferLocked(comm, ptr);
}
size_t NCCLWindowAllocator::getBufferCount(ncclComm_t comm) const
{
std::lock_guard<std::mutex> lock(mMutex);
auto commIt = mBufferPool.find(comm);
return commIt != mBufferPool.end() ? commIt->second.size() : 0;
}
size_t NCCLWindowAllocator::getBufferInUseCount(ncclComm_t comm) const
{
std::lock_guard<std::mutex> lock(mMutex);
auto commIt = mBufferPool.find(comm);
if (commIt == mBufferPool.end())
{
return 0;
}
size_t count = 0;
for (auto const& entry : commIt->second)
{
if (entry.inUse)
{
++count;
}
}
return count;
}
bool NCCLWindowAllocator::isCommValid(ncclComm_t comm) const noexcept
{
// Simply check for null - all non-null comms are valid
// We don't track cleaned-up comms because NCCL can reuse memory addresses,
// making pointer-based tracking unreliable. New comms will be registered when used.
return comm != nullptr;
}
NCCLWindowBuffer NCCLWindowAllocator::allocateAndRegisterBuffer(ncclComm_t comm, size_t size, int handle)
{
// Step 1: Pre-allocate the rank-sync flag before ncclMemAlloc. ncclMemAlloc can fail
// asymmetrically with ncclUnhandledCudaError on configurations where the symmetric/VMM path
// is unavailable; that failure may leave a sticky CUDA last-error on the device. If we
// deferred this cudaMalloc until after the failure, the sticky error would propagate into
// cudaMalloc, TLLM_CUDA_CHECK would throw, and the failing rank would never reach the
// collective ncclAllReduce(min) below, hanging every other rank that did succeed.
int* rankSyncFlag = nullptr;
TLLM_CUDA_CHECK(cudaMalloc(&rankSyncFlag, sizeof(int)));
CudaMallocGuard flagGuard{rankSyncFlag}; // frees rankSyncFlag on any early return or exception
auto stream = at::cuda::getCurrentCUDAStream().stream();
TLLM_CUDA_CHECK(cudaMemsetAsync(rankSyncFlag, 0, sizeof(int), stream));
// Step 2: Allocate symmetric memory. This per-rank, non-collective call can fail
// asymmetrically. When it fails, NCCL may leave a sticky CUDA error behind; clear it before
// the stream-ordered flag copy and collective fallback so the failing rank still reaches
// ncclAllReduce with the other ranks.
void* ncclPtr = nullptr;
TLLM_NCCL_CHECK_WARN(ncclMemAlloc(&ncclPtr, size));
int const localAllocOk = (ncclPtr != nullptr) ? 1 : 0;
NcclMemGuard ncclGuard{ncclPtr}; // frees ncclPtr on any early return or exception
clearCudaErrorIfSymmetricAllocationFailed(localAllocOk);
// Step 3: ncclCommWindowRegister is collective. If any rank skips it, all other ranks hang.
// Populate flag, reduce with min across ranks (0 if any rank failed), then read back.
// The flag is initialized to 0, so H2D failure is non-fatal and conservatively falls back
// to regular NCCL while still reaching the collective. allreduce and D2H failures throw.
if (localAllocOk != 0)
{
TLLM_CUDA_CHECK_WARN(
cudaMemcpyAsync(rankSyncFlag, &localAllocOk, sizeof(localAllocOk), cudaMemcpyHostToDevice, stream));
}
TLLM_NCCL_CHECK(ncclAllReduce(rankSyncFlag, rankSyncFlag, 1, ncclInt32, ncclMin, comm, stream));
TLLM_CUDA_CHECK_WARN(cudaStreamSynchronize(stream));
int allAllocOk = 0;
TLLM_CUDA_CHECK(cudaMemcpy(&allAllocOk, rankSyncFlag, sizeof(int), cudaMemcpyDeviceToHost));
// flagGuard frees rankSyncFlag here at end of its scope
if (!allAllocOk)
{
if (localAllocOk)
{
TLLM_LOG_WARNING(
"[NCCLUtil] ncclMemAlloc failed on at least one other rank; "
"freeing local allocation (size=%zu) and aborting window registration on all ranks.",
size);
}
return NCCLWindowBuffer{}; // ncclGuard frees ncclPtr
}
// Step 4: Register with NCCL as a window. This is collective, so all ranks must reach it.
// Failure here is non-fatal: warn and fall back to regular allreduce.
// ncclGuard frees ncclPtr on return.
ncclWindow_t window = nullptr;
ncclResult_t const regResult = ncclCommWindowRegister(comm, ncclPtr, size, &window, NCCL_WIN_COLL_SYMMETRIC);
TLLM_NCCL_CHECK_WARN(regResult);
if (regResult != ncclSuccess)
{
return NCCLWindowBuffer{};
}
// Step 5: Success. Transfer ownership to the returned buffer.
ncclGuard.release();
NCCLWindowBuffer buffer{ncclPtr, handle, size, window};
TLLM_LOG_TRACE("[NCCLUtil] Allocated and registered NCCL window buffer: handle=%d, ptr=%p, size=%zu, window=%p",
handle, buffer.ptr, buffer.size, static_cast<void*>(buffer.window));
return buffer;
}
NCCLWindowBuffer NCCLWindowAllocator::searchBufferLocked(ncclComm_t comm, void* ptr) const
{
auto commIt = mBufferPool.find(comm);
if (commIt == mBufferPool.end())
{
return NCCLWindowBuffer();
}
for (auto const& entry : commIt->second)
{
if (entry.buffer.ptr == ptr)
{
return entry.buffer;
}
}
return NCCLWindowBuffer();
}
void NCCLWindowAllocator::registerBufferCleanup(ncclComm_t comm)
{
// Don't register if already registered
if (mRegisteredComms.find(comm) != mRegisteredComms.end())
{
return;
}
mRegisteredComms.insert(comm);
// Register cleanup with the resource manager
NcclCommResourceManager::getInstance().registerResource(
comm, [this, comm]() { this->cleanupBuffersForComm(comm); }, "NCCLWindowAllocator");
}
void NCCLWindowAllocator::cleanupBuffersForComm(ncclComm_t comm) noexcept
{
if (!comm)
{
return;
}
// Synchronize CUDA to ensure all operations using these buffers are complete
// before we deregister windows and free memory
cudaError_t cudaErr = cudaDeviceSynchronize();
if (cudaErr != cudaSuccess)
{
TLLM_LOG_WARNING("[NCCLUtil] cudaDeviceSynchronize failed with error: %d before cleanup for comm %p", cudaErr,
static_cast<void*>(comm));
// Continue anyway - the sync failure might be from a previous error
}
std::lock_guard<std::mutex> lock(mMutex);
// Check if we've already cleaned up this communicator
if (mRegisteredComms.find(comm) == mRegisteredComms.end())
{
// Already cleaned up or never registered
return;
}
auto commIt = mBufferPool.find(comm);
if (commIt == mBufferPool.end())
{
// No buffers to clean up, but mark as cleaned
mRegisteredComms.erase(comm);
mMinSymmetricFailureSize.erase(comm);
return;
}
TLLM_LOG_TRACE(
"[NCCLUtil] Cleaning up %zu NCCL window buffers for comm %p", commIt->second.size(), static_cast<void*>(comm));
// Check for buffers still in use - this shouldn't happen if cleanup is called properly,
// but we log a warning if it does
size_t inUseCount = 0;
size_t totalBytes = 0;
for (auto const& entry : commIt->second)
{
totalBytes += entry.buffer.size;
if (entry.inUse)
{
++inUseCount;
}
}
if (inUseCount > 0)
{
TLLM_LOG_WARNING(
"[NCCLUtil] Cleaning up %zu buffers still marked as in-use for comm %p. "
"This may indicate buffers weren't properly released before cleanup.",
inUseCount, static_cast<void*>(comm));
}
TLLM_LOG_DEBUG("[NCCLUtil] NCCL window allocator teardown for comm %p: %zu buffers, %zu bytes total",
static_cast<void*>(comm), commIt->second.size(), totalBytes);
for (auto& entry : commIt->second)
{
if (entry.buffer.isValid())
{
// Deregister the window - the communicator is still valid at this point
// (cleanup happens before ncclCommDestroy), but we need to be careful
// if buffers are still in use by active operations
if (entry.buffer.window && comm)
{
// Note: Even if buffer is marked inUse, we must deregister since
// the communicator is being destroyed. The communicator is valid,
// but we should handle potential errors gracefully.
ncclResult_t result = ncclCommWindowDeregister(comm, entry.buffer.window);
if (result != ncclSuccess)
{
TLLM_LOG_WARNING(
"[NCCLUtil] ncclCommWindowDeregister failed with error: %d for comm %p, "
"window %p (buffer inUse: %d)",
result, static_cast<void*>(comm), static_cast<void*>(entry.buffer.window), entry.inUse);
}
}
// Free device memory using ncclMemFree
// This should be safe even if deregister failed
if (entry.buffer.ptr)
{
try
{
ncclResult_t ncclResult = ncclMemFree(entry.buffer.ptr);
if (ncclResult != ncclSuccess)
{
TLLM_LOG_WARNING("[NCCLUtil] ncclMemFree failed with error: %d", ncclResult);
}
}
catch (...)
{
TLLM_LOG_ERROR("[NCCLUtil] Exception during ncclMemFree for ptr %p", entry.buffer.ptr);
}
}
TLLM_LOG_TRACE(
"[NCCLUtil] Freed NCCL window buffer: ptr=%p, size=%zu", entry.buffer.ptr, entry.buffer.size);
}
}
mBufferPool.erase(commIt);
mRegisteredComms.erase(comm);
mMinSymmetricFailureSize.erase(comm);
}
#endif // NCCL_VERSION_CODE >= NCCL_VERSION(2, 28, 0)
} // namespace tensorrt_llm::common::nccl_util
#endif // ENABLE_MULTI_DEVICE