Skip to content

Commit 58fee31

Browse files
committed
[Common] EP overlap: allocate NCCL EP handle scratch at compile time
NCCL EP allocates a group's handle scan scratch lazily, on the first handle init or import. In a MaxText train step (XLA command buffers with CUSTOM_CALL) the first overlap call that imports a handle ran inside a CUDA graph capture, and the allocation failed: "EP combine_gemm_fwd failed: CUDA error nccl_ep.cc:4370 'operation not permitted when stream is capturing'". nvte_ep_overlap_compile, which already runs at trace time once per layer configuration, now opens and closes a handle for the layer configuration (EPBackend::warm_handle_scratch) so the scratch exists before any captured call. No behaviour change otherwise. Measured (4x GB300, DeepSeek-V3 proxy, 1 dense + 4 MoE layers, EP4, FP8 + MXFP8 MoE, pdb 6, GA 2, command buffers incl. CUSTOM_CALL): before, arm B failed at the first step; after, it runs (step 0.983 s, same as with CUSTOM_CALL removed from the command buffers). Depends on upstream PR NVIDIA#3906. Signed-off-by: Deep Patel <deepatel@nvidia.com>
1 parent 023680c commit 58fee31

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎transformer_engine/common/ep/ep_backend.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,21 @@ ncclEpHandle_t EPBackend::open_handle(void* handle_mem, size_t handle_mem_size,
341341
return handle;
342342
}
343343

344+
void EPBackend::warm_handle_scratch(NVTEEpLayerConfig layer_cfg) {
345+
const size_t size = handle_mem_size(layer_cfg);
346+
void* handle_mem = nullptr;
347+
NVTE_CHECK_CUDA(cudaMalloc(&handle_mem, size));
348+
{
349+
std::lock_guard<std::mutex> lock(mutex_);
350+
NVTE_CHECK(initialized_, "EPBackend not initialized");
351+
ScopedHandle guard(open_handle(handle_mem, size, layer_cfg.top_k,
352+
layer_cfg.dispatch_output_per_expert_alignment),
353+
/*owned=*/true);
354+
}
355+
NVTE_CHECK_CUDA(cudaDeviceSynchronize());
356+
NVTE_CHECK_CUDA(cudaFree(handle_mem));
357+
}
358+
344359
ncclEpHandle_t EPBackend::import_handle_locked(void* handle_mem, size_t handle_mem_size,
345360
int num_tokens, NVTEEpLayerConfig layer_cfg) {
346361
size_t hm_sizes[1] = {handle_mem_size};

‎transformer_engine/common/ep/ep_backend.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ class EPBackend {
109109
const EpSignal* dispatch_signal, const NVTEEpLayerConfig* layer_cfg,
110110
cudaStream_t stream);
111111

112+
// Opens and closes a handle for layer_cfg outside any CUDA graph capture, so NCCL EP allocates
113+
// the group's lazily created handle scratch before a captured call needs it.
114+
void warm_handle_scratch(NVTEEpLayerConfig layer_cfg);
115+
112116
const NVTEEpGroupConfig& group_config() const { return group_config_; }
113117

114118
~EPBackend();

‎transformer_engine/common/ep/ep_gemm_api.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ void nvte_ep_overlap_compile(size_t hidden_dim, size_t ffn_dim, size_t num_local
636636
check_mxfp8_dims(hidden_dim, ffn_dim,
637637
ep::normalize_layer_config(layer_cfg).dispatch_output_per_expert_alignment);
638638
}
639+
// The overlapped calls run inside CUDA graphs (XLA command buffers); let NCCL EP allocate the
640+
// group's handle scratch now, at trace time, instead of on a first captured call.
641+
ep::EPBackend::get().warm_handle_scratch(ep::normalize_layer_config(layer_cfg));
639642
// The weight-gradient launches of a backward call (see make_wgrad and launch_wgrad).
640643
const auto compile_wgrad = [&](const EpGemmContext& ctx, size_t m, size_t n,
641644
std::initializer_list<std::tuple<int, bool, size_t>> launches) {

0 commit comments

Comments
 (0)