Skip to content

Commit a51a2e3

Browse files
committed
[Common][PyTorch] Take a caller-provided workspace for the overlapped MoE EP GEMMs
Signed-off-by: Phuong Nguyen <phuonguyen@nvidia.com>
1 parent ffe3240 commit a51a2e3

7 files changed

Lines changed: 324 additions & 129 deletions

File tree

‎transformer_engine/common/ep/ep_backend.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ void EPBackend::shutdown_locked() {
316316
nccl_ep::group_destroy(ep_group_);
317317
ep_group_ = nullptr;
318318
}
319-
release_gemm_scratch();
320319
ep_comm_ = nullptr; // borrowed; caller destroys
321320
initialized_ = false;
322321
}

‎transformer_engine/common/ep/ep_backend.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ struct EpSignal {
3636
size_t granularity;
3737
};
3838

39-
/*! \brief Free the current device's grouped GEMM scratch. A no-op without the EP GEMMs. */
40-
void release_gemm_scratch();
41-
4239
/*! \brief EP backend per CUDA device; owns the NCCL EP group, borrows the comm. */
4340
class EPBackend {
4441
public:

‎transformer_engine/common/ep/ep_gemm_api.cpp‎

Lines changed: 156 additions & 87 deletions
Large diffs are not rendered by default.

‎transformer_engine/common/include/transformer_engine/ep_gemm.h‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@
2828
* default 1024 when it divides hidden_dim, else the whole row); smaller chunks let the combine
2929
* start earlier but cost more flags.
3030
*
31+
* Each call takes a caller-owned uint8 workspace holding its readiness flags and GEMM state;
32+
* nvte_ep_overlap_workspace_size returns the bytes that fit every call of a group, so one buffer
33+
* can be reused across them. Allocate it once, keep it alive, and reuse it: it must not be freed
34+
* or reallocated between calls that a CUDA graph captures.
35+
*
3136
* Requirements: SM 10.0, 10.3 or 10.7, EP within one NVLink domain, num_comm_sms > 0, zero_copy
3237
* off, dispatch_output_per_expert_alignment 256, 128 (not MXFP8 on SM 10.7) or (bfloat16 only)
3338
* 64, and NVTE_ENABLE_CUTEDSL_BACKEND=1 with NVTE_MOE_OVERLAP. MXFP8 needs hidden_dim a multiple
34-
* of 512 and ffn_dim a multiple of 256. The first call per shape compiles the GEMM and allocates
35-
* scratch, freed by nvte_ep_shutdown: make it at warmup, outside CUDA graph capture. Issue the calls of one EP group on one
36-
* stream, in the same order on every rank. A call throws when a requirement does not hold.
39+
* of 512 and ffn_dim a multiple of 256. The first call per shape compiles the GEMM: make it at
40+
* warmup, outside CUDA graph capture. Issue the calls of one EP group on one stream, in the same
41+
* order on every rank. A call throws when a requirement does not hold.
3742
*
3843
* An input with a symm-mem window (tokens_win, grad_result_win) is read in place by the peers:
3944
* every rank passes a window, at the same data and scale offsets, or none, and the input is not
@@ -91,6 +96,7 @@ extern "C" {
9196
* prepare writes it; null when the dispatch writes
9297
* expert_token_offsets, where expert_token_offsets[-1] is
9398
* the padded total of the kept rows.
99+
* \param[in,out] workspace uint8 scratch, nvte_ep_overlap_workspace_size bytes.
94100
* \param[in] layer_cfg Per-layer EP configuration.
95101
* \param[in] stream CUDA stream.
96102
*/
@@ -100,8 +106,8 @@ void nvte_ep_dispatch_gemm_fwd(NVTETensor handle_mem, const NVTETensor topk_idx,
100106
NVTETensor recv_tokens, NVTETensor recv_topk_weights,
101107
NVTETensor gemm_out, NVTETensor act_out,
102108
NVTETensor recv_tokens_per_expert, NVTETensor expert_token_offsets,
103-
NVTETensor total_recv_tokens, const NVTEEpLayerConfig* layer_cfg,
104-
cudaStream_t stream);
109+
NVTETensor total_recv_tokens, NVTETensor workspace,
110+
const NVTEEpLayerConfig* layer_cfg, cudaStream_t stream);
105111

106112
/*! \brief Second grouped GEMM overlapped with the combine.
107113
*
@@ -121,13 +127,14 @@ void nvte_ep_dispatch_gemm_fwd(NVTETensor handle_mem, const NVTETensor topk_idx,
121127
* \param[in] weight [num_local_experts, hidden_dim, ffn_dim] weights.
122128
* \param[out] expert_out [recv_capacity, hidden_dim] GEMM output.
123129
* \param[out] result [T, hidden_dim] combined output.
130+
* \param[in,out] workspace uint8 scratch, nvte_ep_overlap_workspace_size bytes.
124131
* \param[in] layer_cfg Per-layer EP configuration.
125132
* \param[in] stream CUDA stream.
126133
*/
127134
void nvte_ep_combine_gemm_fwd(NVTETensor handle_mem, const NVTETensor gemm_in,
128135
const NVTETensor recv_topk_weights,
129136
const NVTETensor expert_token_offsets, const NVTETensor weight,
130-
NVTETensor expert_out, NVTETensor result,
137+
NVTETensor expert_out, NVTETensor result, NVTETensor workspace,
131138
const NVTEEpLayerConfig* layer_cfg, cudaStream_t stream);
132139

133140
/*! \brief Backward of nvte_ep_combine_gemm_fwd and of the SwiGLU: the combine backward
@@ -170,6 +177,7 @@ void nvte_ep_combine_gemm_fwd(NVTETensor handle_mem, const NVTETensor gemm_in,
170177
* \param[in,out] weight_grad [num_local_experts, hidden_dim, ffn_dim] bfloat16 or
171178
* float32 gradient of weight, or null.
172179
* \param[in] accumulate_weight_grad Add to weight_grad instead of writing it.
180+
* \param[in,out] workspace uint8 scratch, nvte_ep_overlap_workspace_size bytes.
173181
* \param[in] layer_cfg Per-layer EP configuration.
174182
* \param[in] stream CUDA stream.
175183
*/
@@ -180,8 +188,8 @@ void nvte_ep_combine_gemm_bwd(NVTETensor handle_mem, const NVTETensor grad_resul
180188
const NVTETensor wgrad_input, NVTETensor grad_expert_out,
181189
NVTETensor grad_gemm_out, NVTETensor grad_recv_topk_weights,
182190
NVTETensor act_out, NVTETensor weight_grad,
183-
int accumulate_weight_grad, const NVTEEpLayerConfig* layer_cfg,
184-
cudaStream_t stream);
191+
int accumulate_weight_grad, NVTETensor workspace,
192+
const NVTEEpLayerConfig* layer_cfg, cudaStream_t stream);
185193

186194
/*! \brief Backward of nvte_ep_dispatch_gemm_fwd: the first GEMM's data gradient overlapped with
187195
* the dispatch backward.
@@ -205,6 +213,7 @@ void nvte_ep_combine_gemm_bwd(NVTETensor handle_mem, const NVTETensor grad_resul
205213
* \param[in,out] weight_grad [num_local_experts, 2 * ffn_dim, hidden_dim] bfloat16 or
206214
* float32 gradient of weight, or null.
207215
* \param[in] accumulate_weight_grad Add to weight_grad instead of writing it.
216+
* \param[in,out] workspace uint8 scratch, nvte_ep_overlap_workspace_size bytes.
208217
* \param[in] layer_cfg Per-layer EP configuration.
209218
* \param[in] stream CUDA stream.
210219
*/
@@ -214,7 +223,25 @@ void nvte_ep_dispatch_gemm_bwd(NVTETensor handle_mem, const NVTETensor grad_gemm
214223
const NVTETensor wgrad_input, NVTETensor grad_recv_tokens,
215224
NVTETensor grad_tokens, NVTETensor grad_topk_weights,
216225
NVTETensor weight_grad, int accumulate_weight_grad,
217-
const NVTEEpLayerConfig* layer_cfg, cudaStream_t stream);
226+
NVTETensor workspace, const NVTEEpLayerConfig* layer_cfg,
227+
cudaStream_t stream);
228+
229+
/*! \brief Bytes of the workspace that fits every overlapped GEMM call of a group, so one buffer can
230+
* be reused across them. Queryable once the EP group is bootstrapped; call it at warmup, as
231+
* it compiles the group's GEMMs.
232+
*
233+
* \param[in] hidden_dim Hidden size.
234+
* \param[in] ffn_dim Expert FFN size.
235+
* \param[in] num_local_experts Experts held by this rank.
236+
* \param[in] recv_capacity Rows of the receive buffers (recv_capacity).
237+
* \param[in] with_weight_grad Nonzero to fit the backward weight-gradient workspace.
238+
* \param[in] mxfp8 Nonzero for MXFP8 operands instead of bfloat16.
239+
* \param[in] layer_cfg Per-layer EP configuration.
240+
* \return Required workspace size in bytes.
241+
*/
242+
size_t nvte_ep_overlap_workspace_size(size_t hidden_dim, size_t ffn_dim, size_t num_local_experts,
243+
size_t recv_capacity, int with_weight_grad, int mxfp8,
244+
const NVTEEpLayerConfig* layer_cfg);
218245

219246
#ifdef __cplusplus
220247
}

0 commit comments

Comments
 (0)