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 */
127134void 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