buffer.py 46.3 KB
Newer Older
Chenggang Zhao's avatar
Chenggang Zhao committed
1
import os
lijian6's avatar
lijian6 committed
2
3
from typing import Callable, List, Optional, Tuple, Union

Chenggang Zhao's avatar
Chenggang Zhao committed
4
5
6
import torch
import torch.distributed as dist

lijian6's avatar
lijian6 committed
7
8
9
from . import deep_ep_cpp
from .deep_ep_cpp import Config, EventHandle

10
from .utils import EventOverlap, check_nvlink_connections
Chenggang Zhao's avatar
Chenggang Zhao committed
11
12
13
14
15
16


class Buffer:
    """
    The core expert-parallel (EP) communication buffers for Mixture of Experts (MoE) model, which supports:
        - high-throughput intranode all-to-all (dispatch and combine, using NVLink)
17
18
        - high-throughput internode all-to-all (dispatch and combine, using RDMA and NVLink)
        - low-latency all-to-all (dispatch and combine, using RDMA)
Chenggang Zhao's avatar
Chenggang Zhao committed
19
20
21
22
23
24
25
26
27
28
29

    Attributes:
        num_sms: the SMs used in high-throughput kernels.
        rank: the local rank number.
        group_size: the number of ranks in the group.
        group: the communication group.
        num_nvl_bytes: the buffer size for intranode NVLink communication.
        num_rdma_bytes: the buffer size for internode (also for intranode with low-latency mode) RDMA communication.
        runtime: the C++ runtime.
    """

30
    num_sms: int = 24
Chenggang Zhao's avatar
Chenggang Zhao committed
31

lijian6's avatar
lijian6 committed
32
33
34
35
36
37
38
39
40
41
    def __init__(
        self,
        group: dist.ProcessGroup,
        num_nvl_bytes: int = 0,
        num_rdma_bytes: int = 0,
        low_latency_mode: bool = False,
        num_qps_per_rank: int = 24,
        allow_nvlink_for_low_latency_mode: bool = True,
        allow_mnnvl: bool = False,
        explicitly_destroy: bool = False,
42
        enable_shrink: bool = False,
43
44
        enable_dispatch_ll_layered: bool = False,
        enable_combine_overlap: bool = False,
lijian6's avatar
lijian6 committed
45
    ) -> None:
Chenggang Zhao's avatar
Chenggang Zhao committed
46
47
48
49
50
51
52
53
54
55
        """
        Initialize the communication buffer.

        Arguments:
            group: the communication group.
            num_nvl_bytes: the buffer size for intranode NVLink communication.
            num_rdma_bytes: the buffer size for internode (also for intranode with low-latency mode) RDMA communication.
            low_latency_mode: whether to enable low-latency mode.
            num_qps_per_rank: the number of QPs for RDMA, the low-latency mode requires that this number equals
                to the number of local experts.
Chenggang Zhao's avatar
Chenggang Zhao committed
56
57
58
59
            allow_nvlink_for_low_latency_mode: whether allow NVLink traffic for low-latency mode, you should notice
                this is somehow incompatible with the hook-based overlapping.
                Warning: PCIe connections may lead to errors due to memory ordering issues,
                please make sure all connections are via NVLink.
fzyzcjy's avatar
more  
fzyzcjy committed
60
            allow_mnnvl: whether to allow MNNVL
61
62
63
            explicitly_destroy: If this flag is set to True, you need to explicitly call `destroy()` to release resources;
                otherwise, the resources will be released by the destructor.
                Note: Releasing resources in the destructor may cause Python's exception handling process to hang.
64
            enable_shrink: whether to enable shrink mode. The enable mode allocates a mask buffer to support masking ranks dynamically.
65
66
            enable_dispatch_ll_layered: Enable low-latency mode with hierarchical dispatch operators.
            enable_combine_overlap: deepgemm DOWN gemm overlop combine send
Chenggang Zhao's avatar
Chenggang Zhao committed
67
        """
68
        check_nvlink_connections(group)
Chenggang Zhao's avatar
Chenggang Zhao committed
69
70

        # Initialize the CPP runtime
lijian6's avatar
lijian6 committed
71
72
73
        self.rank = group.rank()
        self.group_size = group.size()
        self.group = group
Chenggang Zhao's avatar
Chenggang Zhao committed
74
75
76
        self.num_nvl_bytes = num_nvl_bytes
        self.num_rdma_bytes = num_rdma_bytes
        self.low_latency_mode = low_latency_mode
77
        self.explicitly_destroy = explicitly_destroy
78
        self.enable_shrink = enable_shrink
79
80
81
82

        if enable_dispatch_ll_layered and enable_shrink:  # Currently, the layered algorithm for ll dispatch has been optimized, so the shrink mode is no longer supported.
            print("DeepEP [ERROR] not support shrink, disable it", flush=True)
            enable_shrink = False
lijian6's avatar
lijian6 committed
83
84
85
86
87
88
89
        self.runtime = deep_ep_cpp.Buffer(
            self.rank,
            self.group_size,
            num_nvl_bytes,
            num_rdma_bytes,
            low_latency_mode,
            explicitly_destroy,
90
91
92
            enable_shrink,
            enable_dispatch_ll_layered,
            enable_combine_overlap
lijian6's avatar
lijian6 committed
93
        )
Chenggang Zhao's avatar
Chenggang Zhao committed
94
95

        # Synchronize device IDs
lijian6's avatar
lijian6 committed
96
97
98
        device_ids = [
            None,
        ] * self.group_size
Chenggang Zhao's avatar
Chenggang Zhao committed
99
        local_device_id = self.runtime.get_local_device_id()
lijian6's avatar
lijian6 committed
100
        dist.all_gather_object(device_ids, local_device_id, group)
Chenggang Zhao's avatar
Chenggang Zhao committed
101
102

        # Synchronize IPC handles
lijian6's avatar
lijian6 committed
103
104
105
        ipc_handles = [
            None,
        ] * self.group_size
Chenggang Zhao's avatar
Chenggang Zhao committed
106
        local_ipc_handle = self.runtime.get_local_ipc_handle()
lijian6's avatar
lijian6 committed
107
        dist.all_gather_object(ipc_handles, local_ipc_handle, group)
Chenggang Zhao's avatar
Chenggang Zhao committed
108

lijian6's avatar
lijian6 committed
109
        # Synchronize DUSHMEM unique IDs
Chenggang Zhao's avatar
Chenggang Zhao committed
110
111
        root_unique_id = None
        if self.runtime.get_num_rdma_ranks() > 1 or low_latency_mode:
sky's avatar
sky committed
112
            # Enable IBGDA
lishen's avatar
lishen committed
113
            self._setup_device_hca_mapping()
114
            assert num_qps_per_rank > 0
lijian6's avatar
lijian6 committed
115
116
117
118
119
120
121
            os.environ["DUSHMEM_DISABLE_P2P"] = "0" if allow_nvlink_for_low_latency_mode else "1"
            os.environ["DUSHMEM_IB_ENABLE_IBGDA"] = "0"  # force_use_ibrc
            os.environ["DUSHMEM_IBGDA_NIC_HANDLER"] = "gpu"
            os.environ["DUSHMEM_ENABLE_NIC_PE_MAPPING"] = "1"
            os.environ["DUSHMEM_IBGDA_NUM_RC_PER_PE"] = f"{num_qps_per_rank}"
            os.environ["DUSHMEM_MAX_TEAMS"] = "7"
            os.environ["DUSHMEM_DISABLE_NVLS"] = "1"
Chenggang Zhao's avatar
Chenggang Zhao committed
122

lijian's avatar
lijian committed
123
            self.gda_num_qps_per_pe = max(int(os.environ.get('ROCSHMEM_GDA_NUM_QPS_PER_PE_DEFAULT_CTX', str(num_qps_per_rank))), num_qps_per_rank * self.group_size)
lijian's avatar
lijian committed
124
125
126
127
128
129
130
            os.environ["ROCSHMEM_GDA_NUM_QPS_DEFAULT_CTX"] = str(self.gda_num_qps_per_pe)
            if self.num_rdma_bytes > 1073741824:
                multiple = 2147483648
                rocshmem_num_rdma_bytes = ((self.num_rdma_bytes + multiple - 1) // multiple) * multiple
                os.environ["ROCSHMEM_HEAP_SIZE"] = str(rocshmem_num_rdma_bytes)
            if self.group_size <= 8:
                os.environ["ROCSHMEM_BACKEND"] = "ipc"
131

Chenggang Zhao's avatar
Chenggang Zhao committed
132
            # Synchronize using the root ID
lijian6's avatar
lijian6 committed
133
            dushmem_unique_ids = [
lijian6's avatar
lijian6 committed
134
135
136
137
138
                None,
            ] * self.group_size
            if (low_latency_mode and self.rank == 0) or (
                not low_latency_mode and self.runtime.get_rdma_rank() == 0
            ):
lijian6's avatar
lijian6 committed
139
140
141
                root_unique_id = self.runtime.get_local_dushmem_unique_id()
            dist.all_gather_object(dushmem_unique_ids, root_unique_id, group)
            root_unique_id = dushmem_unique_ids[
lijian6's avatar
lijian6 committed
142
143
                0 if low_latency_mode else self.runtime.get_root_rdma_rank(True)
            ]
Chenggang Zhao's avatar
Chenggang Zhao committed
144
145
146
147
148

        # Make CPP runtime available
        self.runtime.sync(device_ids, ipc_handles, root_unique_id)
        assert self.runtime.is_available()

lishen's avatar
lishen committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    def _setup_device_hca_mapping(self):
        """
        Set up device to NIC mapping using DEEP_EP_DEVICE_TO_HCA_MAPPING environment variable.
        The mapping format is: "0:mlx5_0:1,1:mlx5_1:1,..." where each entry maps a CUDA device ID
        to an HCA name separated by colon. HCA name can include additional suffixes like ":1".
        """
        if 'DEEP_EP_DEVICE_TO_HCA_MAPPING' in os.environ:
            device_mapping = {}
            mapping_str = os.environ['DEEP_EP_DEVICE_TO_HCA_MAPPING']
            # Parse mapping string like "0:mlx5_0:1,1:mlx5_1:1,..."
            for mapping in mapping_str.split(','):
                assert ':' in mapping, f"Invalid mapping format '{mapping}' in DEEP_EP_DEVICE_TO_HCA_MAPPING. Expected format: '<device_id>:<hca_name>'"
                parts = mapping.split(':', 1)  # Split only on first colon
                device_id = int(parts[0])
                hca_name = parts[1]  # Keep the rest as HCA name (including :1)
                device_mapping[device_id] = hca_name

            # Get current device and set appropriate HCA
            current_device = torch.cuda.current_device()
168
169
170
171
172
173
            # # Translate CUDA_VISIBLE_DEVICES
            # if 'CUDA_VISIBLE_DEVICES' in os.environ:
            #     visible_devices = os.environ['CUDA_VISIBLE_DEVICES'].split(",")
            #     assert len(visible_devices) > current_device, f"CUDA_VISIBLE_DEVICES has {len(visible_devices)} entries which is fewer than the current device {current_device}"
            #     assert visible_devices[current_device].isdigit(), f"DEEP_EP_DEVICE_TO_HCA_MAPPING requires CUDA_VISIBLE_DEVICES to contain integer indices"
            #     current_device = int(visible_devices[current_device])
lishen's avatar
lishen committed
174

lijian6's avatar
lijian6 committed
175
176
177
            assert current_device in device_mapping, f"Current HIP device {current_device} not found in DEEP_EP_DEVICE_TO_HCA_MAPPING"
            os.environ['DUSHMEM_ENABLE_PE_MAPPING'] = '1'
            os.environ['DUSHMEM_HCA_LIST'] = device_mapping[current_device]
lishen's avatar
lishen committed
178

179
180
181
    def destroy(self):
        """
        Destroy the cpp runtime and release resources.
sky's avatar
sky committed
182

183
184
        """

lijian6's avatar
lijian6 committed
185
        assert self.explicitly_destroy, "`explicitly_destroy` flag must be set"
186
187
188
189

        self.runtime.destroy()
        self.runtime = None

lijian6's avatar
lijian6 committed
190
191
192
    # @staticmethod
    # def is_sm90_compiled():
    #     return deep_ep_cpp.is_sm90_compiled()
193

Chenggang Zhao's avatar
Chenggang Zhao committed
194
195
196
197
198
199
200
201
202
    @staticmethod
    def set_num_sms(new_num_sms: int) -> None:
        """
        Set the number of SMs to use in high-throughput kernels.

        Arguments:
            new_num_sms: the new number to be set.
        """

203
204
        assert new_num_sms % 2 == 0, "The SM count must be new_num_sms % 2 == 0"
        assert new_num_sms % 3 == 0, "The SM count must be new_num_sms % 3 == 0"
Chenggang Zhao's avatar
Chenggang Zhao committed
205
206
207
208
209
210
211
212
213
214
215
216
217
        Buffer.num_sms = new_num_sms

    @staticmethod
    def capture() -> EventOverlap:
        """
        Capture a CUDA event on the current stream, i.e. `torch.cuda.current_stream()`.

        Returns:
            event: the captured event.
        """
        return EventOverlap(EventHandle())

    @staticmethod
lijian6's avatar
lijian6 committed
218
    def get_low_latency_rdma_size_hint(
219
220
        num_max_dispatch_tokens_per_rank: int, hidden: int, num_ranks: int, num_experts: int, 
        enable_dispatch_ll_layered: bool = False, quant_group_size: int = 0
lijian6's avatar
lijian6 committed
221
    ) -> int:
Chenggang Zhao's avatar
Chenggang Zhao committed
222
223
224
225
226
227
228
229
        """
        Get a minimum size requirement for the RDMA buffer. The size calculation will be done with BF16.

        Arguments:
            num_max_dispatch_tokens_per_rank: the maximum number of tokens to dispatch, all the ranks must hold the same value.
            hidden: the hidden dimension of each token.
            num_ranks: the number of EP group ranks.
            num_experts: the number of all experts.
lishen's avatar
lishen committed
230
            quant_group_size: the group size if use quant.
Chenggang Zhao's avatar
Chenggang Zhao committed
231
232
233
234

        Returns:
            size: the RDMA buffer size recommended.
        """
lijian6's avatar
lijian6 committed
235
        return deep_ep_cpp.get_low_latency_rdma_size_hint(
236
237
            num_max_dispatch_tokens_per_rank, hidden, num_ranks, num_experts, 
            enable_dispatch_ll_layered, quant_group_size
lijian6's avatar
lijian6 committed
238
        )
sky's avatar
sky committed
239

Shangyan Zhou's avatar
Shangyan Zhou committed
240
241
242
243
244
    def get_comm_stream(self) -> torch.Stream:
        """
        Get the communication stream.

        Returns:
sky's avatar
sky committed
245
            stream: the communication stream.
Shangyan Zhou's avatar
Shangyan Zhou committed
246
247
        """
        ts: torch.Stream = self.runtime.get_comm_stream()
lijian6's avatar
lijian6 committed
248
249
250
251
252
253
254
255
256
257
258
        return torch.cuda.Stream(
            stream_id=ts.stream_id, device_index=ts.device_index, device_type=ts.device_type
        )

    def get_local_buffer_tensor(
        self,
        dtype: torch.dtype,
        size: Optional[torch.Size] = None,
        offset: int = 0,
        use_rdma_buffer: bool = False,
    ) -> torch.Tensor:
Chenggang Zhao's avatar
Chenggang Zhao committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
        """
        Get the raw buffer (slice supported) as a PyTorch tensor.

        Argument:
            dtype: the data type (PyTorch `dtype`) for the tensor.
            size: the slice size (by elements) to get from the buffer.
            offset: the offset of the beginning element.
            use_rdma_buffer: whether to return the RDMA buffer.
        """
        tensor = self.runtime.get_local_buffer_tensor(dtype, offset, use_rdma_buffer)
        if size is None:
            return tensor

        assert tensor.numel() >= size.numel()
lijian6's avatar
lijian6 committed
273
        return tensor[: size.numel()].view(size)
Chenggang Zhao's avatar
Chenggang Zhao committed
274

Shangyan Zhou's avatar
Shangyan Zhou committed
275
276
277
278
279
280
281
282
283
284
    @staticmethod
    def _unpack_bias(bias: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]):
        bias_0, bias_1 = None, None
        if isinstance(bias, torch.Tensor):
            bias_0 = bias
        elif isinstance(bias, tuple):
            assert len(bias) == 2
            bias_0, bias_1 = bias
        return bias_0, bias_1

Chenggang Zhao's avatar
Chenggang Zhao committed
285
286
287
288
289
290
291
292
293
294
295
296
    @staticmethod
    def get_dispatch_config(num_ranks: int) -> Config:
        """
        Get a recommended dispatch config.

        Argument:
            num_ranks: the number of ranks.

        Returns:
            config: the recommended config.
        """

Shifang Xu's avatar
Shifang Xu committed
297
        # TODO: automatically tune
Chenggang Zhao's avatar
Chenggang Zhao committed
298
        config_map = {
299
300
            2: Config(Buffer.num_sms, 24, 256, 6, 128),
            4: Config(Buffer.num_sms, 6, 256, 6, 128),
Chenggang Zhao's avatar
Chenggang Zhao committed
301
            8: Config(Buffer.num_sms, 6, 256, 6, 128),
302
303
            # 16: Config(Buffer.num_sms, 36, 288, 20, 128),
            16: Config(Buffer.num_sms, 8, 512, 16, 128),
304
305
306
            24: Config(Buffer.num_sms, 8, 512, 32, 128),
            32: Config(Buffer.num_sms, 32, 512, 32, 128),
            64: Config(Buffer.num_sms, 20, 512, 28, 128),
lijian6's avatar
lijian6 committed
307
            128: Config(Buffer.num_sms, 20, 560, 32, 128),
Chenggang Zhao's avatar
Chenggang Zhao committed
308
309
310
            144: Config(Buffer.num_sms, 32, 720, 12, 128),
            160: Config(Buffer.num_sms, 28, 720, 12, 128),
        }
lijian6's avatar
lijian6 committed
311
        assert num_ranks in config_map, f"Unsupported number of EP ranks: {num_ranks}"
Chenggang Zhao's avatar
Chenggang Zhao committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
        return config_map[num_ranks]

    @staticmethod
    def get_combine_config(num_ranks: int) -> Config:
        """
        Get a recommended combine config.

        Argument:
            num_ranks: the number of ranks.

        Returns:
            config: the recommended config.
        """

Shifang Xu's avatar
Shifang Xu committed
326
        # TODO: automatically tune
Chenggang Zhao's avatar
Chenggang Zhao committed
327
        config_map = {
328
329
330
            2: Config(Buffer.num_sms, 10, 256, 6, 128),
            4: Config(Buffer.num_sms, 9, 256, 6, 128),
            8: Config(Buffer.num_sms, 4, 256, 6, 128),
331
332
            # 16: Config(Buffer.num_sms, 4, 288, 12, 128),
            16: Config(Buffer.num_sms, 8, 512, 16, 128),
333
334
            24: Config(Buffer.num_sms, 1, 288, 8, 128),
            32: Config(Buffer.num_sms, 1, 288, 8, 128),
lijian6's avatar
lijian6 committed
335
336
            64: Config(Buffer.num_sms, 1, 288, 20, 128),
            128: Config(Buffer.num_sms, 1, 560, 12, 128),
Chenggang Zhao's avatar
Chenggang Zhao committed
337
338
339
            144: Config(Buffer.num_sms, 2, 720, 8, 128),
            160: Config(Buffer.num_sms, 2, 720, 8, 128),
        }
lijian6's avatar
lijian6 committed
340
        assert num_ranks in config_map, f"Unsupported number of EP ranks: {num_ranks}"
Chenggang Zhao's avatar
Chenggang Zhao committed
341
342
343
        return config_map[num_ranks]

    # noinspection PyTypeChecker
lijian6's avatar
lijian6 committed
344
345
346
347
348
349
350
351
    def get_dispatch_layout(
        self,
        topk_idx: torch.Tensor,
        num_experts: int,
        previous_event: Optional[EventOverlap] = None,
        async_finish: bool = False,
        allocate_on_comm_stream: bool = False,
    ) -> Tuple[torch.Tensor, Optional[torch.Tensor], torch.Tensor, torch.Tensor, EventOverlap]:
Chenggang Zhao's avatar
Chenggang Zhao committed
352
353
354
355
        """
        Calculate the layout required for later communication.

        Arguments:
lijian6's avatar
lijian6 committed
356
357
            topk_idx: `[num_tokens, num_topk]`, dtype must be `torch.int64`, the expert indices selected by each token,
                `-1` means no selections.
Chenggang Zhao's avatar
Chenggang Zhao committed
358
359
360
361
362
363
364
365
366
367
368
369
370
            num_experts: the number of experts.
            previous_event: the event to wait before actually executing the kernel.
            async_finish: the current stream will not wait for the communication kernels to be finished if set.
            allocate_on_comm_stream: control whether all the allocated tensors' ownership to be on the communication stream.

        Returns:
            num_tokens_per_rank: `[num_ranks]` with `torch.int`, the number of tokens to be sent to each rank.
            num_tokens_per_rdma_rank: `[num_rdma_ranks]` with `torch.int`, the number of tokens to be sent to each RDMA
                rank (with the same GPU index), return `None` for intranode settings.
            num_tokens_per_expert: `[num_experts]` with `torch.int`, the number of tokens to be sent to each expert.
            is_token_in_rank: `[num_tokens, num_ranks]` with `torch.bool`, whether a token be sent to a rank.
            event: the event after executing the kernel (valid only if `async_finish` is set).
        """
lijian6's avatar
lijian6 committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
        num_tokens_per_rank, num_tokens_per_rdma_rank, num_tokens_per_expert, is_token_in_rank, event = (
            self.runtime.get_dispatch_layout(
                topk_idx,
                num_experts,
                getattr(previous_event, "event", None),
                async_finish,
                allocate_on_comm_stream,
            )
        )
        return (
            num_tokens_per_rank,
            num_tokens_per_rdma_rank,
            num_tokens_per_expert,
            is_token_in_rank,
            EventOverlap(event),
        )
Chenggang Zhao's avatar
Chenggang Zhao committed
387
388

    # noinspection PyTypeChecker
lijian6's avatar
lijian6 committed
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
    def dispatch(
        self,
        x: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]],
        handle: Optional[Tuple] = None,
        num_tokens_per_rank: Optional[torch.Tensor] = None,
        num_tokens_per_rdma_rank: Optional[torch.Tensor] = None,
        is_token_in_rank: Optional[torch.Tensor] = None,
        num_tokens_per_expert: Optional[torch.Tensor] = None,
        topk_idx: Optional[torch.Tensor] = None,
        topk_weights: Optional[torch.Tensor] = None,
        expert_alignment: int = 1,
        num_worst_tokens: int = 0,
        config: Optional[Config] = None,
        previous_event: Optional[EventOverlap] = None,
        async_finish: bool = False,
        allocate_on_comm_stream: bool = False,
        num_recv_tokens_per_expert_as_cuda: bool = False,
    ) -> Tuple[
        Union[Tuple[torch.Tensor, torch.Tensor], torch.Tensor],
        Optional[torch.Tensor],
        Optional[torch.Tensor],
        List[int],
        torch.Tensor,
        Tuple,
        EventOverlap,
    ]:
Chenggang Zhao's avatar
Chenggang Zhao committed
415
416
417
418
        """
        Dispatch tokens to different ranks, both intranode and internode settings are supported.
        Intranode kernels require all the ranks should be visible via NVLink.
        Internode kernels require the ranks in a node should be visible via NVLink, while the ranks with the same GPU
419
            index should be visible via RDMA.
Chenggang Zhao's avatar
Chenggang Zhao committed
420
421
422
423
424
425
426
427
428
429
430
431

        Arguments:
            x: `torch.Tensor` or tuple of `torch.Tensor`, for the first type, the shape must be `[num_tokens, hidden]`,
                and type must be `torch.bfloat16`; for the second type, the first element of the tuple must be shaped as
                `[num_tokens, hidden]` with type `torch.float8_e4m3fn`, the second must be `[num_tokens, hidden // 128]`
                 (requiring divisible) with type `torch.float`.
            handle: an optional communication handle, if set, the CPU will reuse the layout information to save some time.
            num_tokens_per_rank: `[num_ranks]` with `torch.int`, the number of tokens to be sent to each rank.
            num_tokens_per_rdma_rank: `[num_rdma_ranks]` with `torch.int`, the number of tokens to be sent to each RDMA
                rank (with the same GPU index), return `None` for intranode settings.
            is_token_in_rank: `[num_tokens, num_ranks]` with `torch.bool`, whether a token be sent to a rank.
            num_tokens_per_expert: `[num_experts]` with `torch.int`, the number of tokens to be sent to each expert.
lijian6's avatar
lijian6 committed
432
433
            topk_idx: `[num_tokens, num_topk]` with `torch.int64`, the expert indices selected by each token,
                `-1` means no selections.
Chenggang Zhao's avatar
Chenggang Zhao committed
434
435
            topk_weights: `[num_tokens, num_topk]` with `torch.float`, the expert weights of each token to dispatch.
            expert_alignment: align the number of tokens received by each local expert to this variable.
436
437
            num_worst_tokens: the worst number of tokens to receive, if specified, there will be no CPU sync, and it
                will be CUDA-graph compatible. Please also notice that this flag is for intranode only.
Chenggang Zhao's avatar
Chenggang Zhao committed
438
439
440
441
            config: the performance tuning config.
            previous_event: the event to wait before actually executing the kernel.
            async_finish: the current stream will not wait for the communication kernels to be finished if set.
            allocate_on_comm_stream: control whether all the allocated tensors' ownership to be on the communication stream.
lijian6's avatar
lijian6 committed
442
            num_recv_tokens_per_expert_as_cuda: control return num_recv_tokens_per_expert as cuda tensor or python list.
Chenggang Zhao's avatar
Chenggang Zhao committed
443
444
445
446
447
        Returns:
            recv_x: received tokens, the same type and tuple as the input `x`, but the number of tokens equals to the
                received token count.
            recv_topk_idx: received expert indices.
            recv_topk_weights: received expert weights.
lijian6's avatar
lijian6 committed
448
            num_recv_tokens_per_expert: Python list or cuda tensor shaped `[num_local_experts]`, the received token count by
Chenggang Zhao's avatar
Chenggang Zhao committed
449
450
                each local expert, aligned to the input `expert_alignment`. If `num_worst_tokens` is specified, the list
                will be empty.
Chenggang Zhao's avatar
Chenggang Zhao committed
451
452
453
454
455
456
457
458
            handle: the returned communication handle.
            event: the event after executing the kernel (valid only if `async_finish` is set).
        """
        # Default config
        config = self.get_dispatch_config(self.group_size) if config is None else config

        # Internode
        if self.runtime.get_num_rdma_ranks() > 1:
lijian6's avatar
lijian6 committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
            assert num_worst_tokens == 0, "Internode dispatch does not support `num_worst_tokens > 0`"
            return self.internode_dispatch(
                x,
                handle,
                num_tokens_per_rank,
                num_tokens_per_rdma_rank,
                is_token_in_rank,
                num_tokens_per_expert,
                topk_idx,
                topk_weights,
                expert_alignment,
                config,
                previous_event,
                async_finish,
                allocate_on_comm_stream,
            )
Chenggang Zhao's avatar
Chenggang Zhao committed
475
476
477
478
479

        # Launch the kernel with cached or non-cached mode
        x, x_scales = x if isinstance(x, tuple) else (x, None)
        if handle is not None:
            assert topk_idx is None and topk_weights is None
lijian6's avatar
lijian6 committed
480
481
482
483
484
485
486
487
            (
                rank_prefix_matrix,
                channel_prefix_matrix,
                recv_channel_prefix_matrix,
                recv_src_idx,
                is_token_in_rank,
                send_head,
            ) = handle
Chenggang Zhao's avatar
Chenggang Zhao committed
488
            num_recv_tokens = recv_src_idx.size(0)
lijian6's avatar
lijian6 committed
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
            recv_x, recv_x_scales, _, _, _, _, _, _, _, _, _, event = self.runtime.intranode_dispatch(
                x,
                x_scales,
                None,
                None,
                None,
                is_token_in_rank,
                None,
                num_recv_tokens,
                rank_prefix_matrix,
                channel_prefix_matrix,
                expert_alignment,
                num_worst_tokens,
                config,
                getattr(previous_event, "event", None),
                async_finish,
                allocate_on_comm_stream,
            )
            return (
                (recv_x, recv_x_scales) if x_scales is not None else recv_x,
                None,
                None,
                None,
                None,
                EventOverlap(event),
            )
Chenggang Zhao's avatar
Chenggang Zhao committed
515
        else:
lijian6's avatar
lijian6 committed
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
            assert (
                num_tokens_per_rank is not None
                and is_token_in_rank is not None
                and num_tokens_per_expert is not None
            )
            (
                recv_x,
                recv_x_scales,
                recv_topk_idx,
                recv_topk_weights,
                num_recv_tokens_per_expert_list,
                num_recv_tokens_per_expert_cuda,
                rank_prefix_matrix,
                channel_prefix_matrix,
                recv_channel_prefix_matrix,
                recv_src_idx,
                send_head,
                event,
            ) = self.runtime.intranode_dispatch(
                x,
                x_scales,
                topk_idx,
                topk_weights,
                num_tokens_per_rank,
                is_token_in_rank,
                num_tokens_per_expert,
                0,
                None,
                None,
                expert_alignment,
                num_worst_tokens,
                config,
                getattr(previous_event, "event", None),
                async_finish,
                allocate_on_comm_stream,
            )
            handle = (
                rank_prefix_matrix,
                channel_prefix_matrix,
                recv_channel_prefix_matrix,
                recv_src_idx,
                is_token_in_rank,
                send_head,
            )
            return (
                (recv_x, recv_x_scales) if x_scales is not None else recv_x,
                recv_topk_idx,
                recv_topk_weights,
                (
                    num_recv_tokens_per_expert_cuda
                    if num_recv_tokens_per_expert_as_cuda
                    else num_recv_tokens_per_expert_list
                ),
                handle,
                EventOverlap(event),
            )
Chenggang Zhao's avatar
Chenggang Zhao committed
572
573

    # noinspection PyTypeChecker
lijian6's avatar
lijian6 committed
574
575
576
577
578
579
580
581
582
583
584
    def combine(
        self,
        x: torch.Tensor,
        handle: Tuple,
        topk_weights: Optional[torch.Tensor] = None,
        bias: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]] = None,
        config: Optional[Config] = None,
        previous_event: Optional[EventOverlap] = None,
        async_finish: bool = False,
        allocate_on_comm_stream: bool = False,
    ) -> Tuple[torch.Tensor, Optional[torch.Tensor], EventOverlap]:
Chenggang Zhao's avatar
Chenggang Zhao committed
585
586
587
588
589
        """
        Combine (reduce) tokens (addition **without** weights) from different ranks, both intranode and internode
            settings are supported.
        Intranode kernels require all the ranks should be visible via NVLink.
        Internode kernels require the ranks in a node should be visible via NVLink, while the ranks with the same GPU
590
            index should be visible via RDMA.
Chenggang Zhao's avatar
Chenggang Zhao committed
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610

        Arguments:
            x: `[num_tokens, hidden]` with `torch.bfloat16`, the tokens to send for reducing to its original ranks.
            handle: a must-set communication handle, you can obtain this from the dispatch function.
            topk_weights: `[num_tokens, num_topk]` with `torch.float`, the tokens' top-k weights for reducing to its original ranks.
            config: the performance tuning config.
            previous_event: the event to wait before actually executing the kernel.
            async_finish: the current stream will not wait for the communication kernels to be finished if set.
            allocate_on_comm_stream: control whether all the allocated tensors' ownership to be on the communication stream.

        Returns:
            recv_x: the reduced token from its dispatched ranks.
            recv_topk_weights: the reduced top-k weights from its dispatch ranks.
            event: the event after executing the kernel (valid only if `async_finish` is set).
        """
        # Default config
        config = self.get_combine_config(self.group_size) if config is None else config

        # Internode
        if self.runtime.get_num_rdma_ranks() > 1:
lijian6's avatar
lijian6 committed
611
612
613
            return self.internode_combine(
                x, handle, topk_weights, bias, config, previous_event, async_finish, allocate_on_comm_stream
            )
Chenggang Zhao's avatar
Chenggang Zhao committed
614
615
616

        # NOTES: the second `_` is for the sending side, so we should use the third one
        rank_prefix_matrix, _, channel_prefix_matrix, src_idx, is_recv_token_in_rank, send_head = handle
Shangyan Zhou's avatar
Shangyan Zhou committed
617
        bias_0, bias_1 = Buffer._unpack_bias(bias)
Chenggang Zhao's avatar
Chenggang Zhao committed
618
619
620

        # Launch the kernel
        recv_x, recv_topk_weights, event = self.runtime.intranode_combine(
lijian6's avatar
lijian6 committed
621
622
623
624
625
626
627
628
629
630
631
632
633
            x,
            topk_weights,
            bias_0,
            bias_1,
            src_idx,
            rank_prefix_matrix,
            channel_prefix_matrix,
            send_head,
            config,
            getattr(previous_event, "event", None),
            async_finish,
            allocate_on_comm_stream,
        )
Chenggang Zhao's avatar
Chenggang Zhao committed
634
635
636
        return recv_x, recv_topk_weights, EventOverlap(event)

    # noinspection PyTypeChecker
lijian6's avatar
lijian6 committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
    def internode_dispatch(
        self,
        x: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]],
        handle: Optional[Tuple] = None,
        num_tokens_per_rank: Optional[torch.Tensor] = None,
        num_tokens_per_rdma_rank: Optional[torch.Tensor] = None,
        is_token_in_rank: Optional[torch.Tensor] = None,
        num_tokens_per_expert: Optional[torch.Tensor] = None,
        topk_idx: Optional[torch.Tensor] = None,
        topk_weights: Optional[torch.Tensor] = None,
        expert_alignment: int = 1,
        config: Optional[Config] = None,
        previous_event: Optional[EventOverlap] = None,
        async_finish: bool = False,
        allocate_on_comm_stream: bool = False,
    ) -> Tuple[
        Union[Tuple[torch.Tensor, torch.Tensor], torch.Tensor],
        Optional[torch.Tensor],
        Optional[torch.Tensor],
        List[int],
        Tuple,
        EventOverlap,
    ]:
Chenggang Zhao's avatar
Chenggang Zhao committed
660
661
662
663
664
665
666
667
668
669
        """
        Internode dispatch implementation, for more details, please refer to the `dispatch` docs.
        Normally, you should not directly call this function.
        """
        assert config is not None

        # Launch the kernel with cached or non-cached mode
        x, x_scales = x if isinstance(x, tuple) else (x, None)
        if handle is not None:
            assert topk_idx is None and topk_weights is None
lijian6's avatar
lijian6 committed
670
671
672
673
674
675
676
677
678
679
680
681
            (
                is_token_in_rank,
                rdma_channel_prefix_matrix,
                gbl_channel_prefix_matrix,
                recv_rdma_channel_prefix_matrix,
                recv_rdma_rank_prefix_sum,
                recv_gbl_channel_prefix_matrix,
                recv_gbl_rank_prefix_sum,
                recv_src_meta,
                send_rdma_head,
                send_nvl_head,
            ) = handle
Chenggang Zhao's avatar
Chenggang Zhao committed
682
683
            num_recv_tokens = recv_src_meta.size(0)
            num_rdma_recv_tokens = send_nvl_head.size(0)
lijian6's avatar
lijian6 committed
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
            recv_x, recv_x_scales, _, _, _, _, _, _, _, _, _, _, _, _, event = (
                self.runtime.internode_dispatch(
                    x,
                    x_scales,
                    topk_idx,
                    topk_weights,
                    None,
                    None,
                    is_token_in_rank,
                    None,
                    num_recv_tokens,
                    num_rdma_recv_tokens,
                    rdma_channel_prefix_matrix,
                    recv_rdma_rank_prefix_sum,
                    gbl_channel_prefix_matrix,
                    recv_gbl_rank_prefix_sum,
                    expert_alignment,
                    config,
                    getattr(previous_event, "event", None),
                    async_finish,
                    allocate_on_comm_stream,
                )
            )
            return (
                (recv_x, recv_x_scales) if x_scales is not None else recv_x,
                None,
                None,
                None,
                None,
                EventOverlap(event),
            )
Chenggang Zhao's avatar
Chenggang Zhao committed
715
        else:
lijian6's avatar
lijian6 committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
            assert (
                num_tokens_per_rank is not None
                and is_token_in_rank is not None
                and num_tokens_per_expert is not None
            )
            (
                recv_x,
                recv_x_scales,
                recv_topk_idx,
                recv_topk_weights,
                num_recv_tokens_per_expert_list,
                rdma_channel_prefix_matrix,
                gbl_channel_prefix_matrix,
                recv_rdma_channel_prefix_matrix,
                recv_rdma_rank_prefix_sum,
                recv_gbl_channel_prefix_matrix,
                recv_gbl_rank_prefix_sum,
                recv_src_meta,
                send_rdma_head,
                send_nvl_head,
                event,
            ) = self.runtime.internode_dispatch(
                x,
                x_scales,
                topk_idx,
                topk_weights,
                num_tokens_per_rank,
                num_tokens_per_rdma_rank,
                is_token_in_rank,
                num_tokens_per_expert,
                0,
                0,
                None,
                None,
                None,
                None,
                expert_alignment,
                config,
                getattr(previous_event, "event", None),
                async_finish,
                allocate_on_comm_stream,
            )
            handle = (
                is_token_in_rank,
                rdma_channel_prefix_matrix,
                gbl_channel_prefix_matrix,
                recv_rdma_channel_prefix_matrix,
                recv_rdma_rank_prefix_sum,
                recv_gbl_channel_prefix_matrix,
                recv_gbl_rank_prefix_sum,
                recv_src_meta,
                send_rdma_head,
                send_nvl_head,
            )
            return (
                (recv_x, recv_x_scales) if x_scales is not None else recv_x,
                recv_topk_idx,
                recv_topk_weights,
                num_recv_tokens_per_expert_list,
                handle,
                EventOverlap(event),
            )
Chenggang Zhao's avatar
Chenggang Zhao committed
778
779

    # noinspection PyTypeChecker
lijian6's avatar
lijian6 committed
780
781
782
783
784
785
786
787
788
789
790
    def internode_combine(
        self,
        x: torch.Tensor,
        handle: Union[tuple, list],
        topk_weights: Optional[torch.Tensor] = None,
        bias: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]] = None,
        config: Optional[Config] = None,
        previous_event: Optional[EventOverlap] = None,
        async_finish: bool = False,
        allocate_on_comm_stream: bool = False,
    ) -> Tuple[torch.Tensor, Optional[torch.Tensor], EventOverlap]:
Chenggang Zhao's avatar
Chenggang Zhao committed
791
792
793
794
795
796
        """
        Internode combine implementation, for more details, please refer to the `combine` docs.
        Normally, you should not directly call this function.
        """
        assert config is not None

Shangyan Zhou's avatar
Shangyan Zhou committed
797
        # Unpack handle and bias
lijian6's avatar
lijian6 committed
798
799
800
801
802
803
804
805
806
807
808
809
        (
            is_combined_token_in_rank,
            _,
            _,
            rdma_channel_prefix_matrix,
            rdma_rank_prefix_sum,
            gbl_channel_prefix_matrix,
            gbl_rank_prefix_sum,
            src_meta,
            send_rdma_head,
            send_nvl_head,
        ) = handle
Shangyan Zhou's avatar
Shangyan Zhou committed
810
        bias_0, bias_1 = Buffer._unpack_bias(bias)
Chenggang Zhao's avatar
Chenggang Zhao committed
811
812
813

        # Launch the kernel
        combined_x, combined_topk_weights, event = self.runtime.internode_combine(
lijian6's avatar
lijian6 committed
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
            x,
            topk_weights,
            bias_0,
            bias_1,
            src_meta,
            is_combined_token_in_rank,
            rdma_channel_prefix_matrix,
            rdma_rank_prefix_sum,
            gbl_channel_prefix_matrix,
            send_rdma_head,
            send_nvl_head,
            config,
            getattr(previous_event, "event", None),
            async_finish,
            allocate_on_comm_stream,
        )
Chenggang Zhao's avatar
Chenggang Zhao committed
830
831
        return combined_x, combined_topk_weights, EventOverlap(event)

lijian6's avatar
lijian6 committed
832
    def clean_low_latency_buffer(
lishen's avatar
lishen committed
833
        self, num_max_dispatch_tokens_per_rank: int, hidden: int, num_experts: int, quant_group_size: int = 0
lijian6's avatar
lijian6 committed
834
    ) -> None:
Chenggang Zhao's avatar
Chenggang Zhao committed
835
836
837
838
839
840
841
842
843
844
        """
        As low-latency kernels require part of the buffer to be zero-initialized, so it is vital to clean the buffer
            if the buffer is dirty at some time.
        For example, after running the normal dispatch/combine, you must run this function before executing any
            low-latency kernel.

        Arguments:
            num_max_dispatch_tokens_per_rank: the maximum number of tokens to dispatch, all the ranks must hold the same value.
            hidden: the hidden dimension of each token.
            num_experts: the number of all experts.
lishen's avatar
lishen committed
845
            quant_group_size: the group size if use quant.
Chenggang Zhao's avatar
Chenggang Zhao committed
846
        """
lishen's avatar
lishen committed
847
        self.runtime.clean_low_latency_buffer(num_max_dispatch_tokens_per_rank, hidden, num_experts, quant_group_size)
Chenggang Zhao's avatar
Chenggang Zhao committed
848
849

    # noinspection PyTypeChecker
lishen's avatar
lishen committed
850
851
    def low_latency_dispatch(self, x: torch.Tensor, topk_idx: torch.Tensor,
                             num_max_dispatch_tokens_per_rank: int, num_experts: int,
852
                             quant_type: int = 1, quant_group_size: int = 0, fp8_round_scale: bool = False,
lishen's avatar
lishen committed
853
                             async_finish: bool = False, return_recv_hook: bool = False) -> \
lishen's avatar
lishen committed
854
            Tuple[Tuple[torch.Tensor, torch.Tensor], torch.Tensor, Tuple, EventOverlap, Callable]:
Chenggang Zhao's avatar
Chenggang Zhao committed
855
        """
856
        A low-latency implementation for dispatching with IBGDA.
Chenggang Zhao's avatar
Chenggang Zhao committed
857
858
        This kernel requires all the ranks (no matter intranode or internode) should be visible via RDMA
            (specifically, IBGDA must be enabled).
lishen's avatar
lishen committed
859
860
        Warning: as there are only two buffers, and the returned tensors reuse the buffer, you cannot hold more than 2
            low-latency kernels' result tensors at a single moment.
Chenggang Zhao's avatar
Chenggang Zhao committed
861
862
863
864

        Arguments:
            x: `torch.Tensor` with `torch.bfloat16`, shaped as `[num_tokens, hidden]`, only several hidden shapes are
                supported. The number of tokens to be dispatched must be less than `num_max_dispatch_tokens_per_rank`.
lishen's avatar
lishen committed
865
866
            topk_idx: `torch.Tensor` with `deep_ep.topk_idx_t` (typically `torch.int64`), shaped as `[num_tokens, num_topk]`,
                only several top-k shapes are supported. `-1` indices (not selecting any expert) are supported.
Chenggang Zhao's avatar
Chenggang Zhao committed
867
868
            num_max_dispatch_tokens_per_rank: the maximum number of tokens to dispatch, all the ranks must hold the same value.
            num_experts: the number of all experts.
869
870
871
872
873
874
875
876
877
878
879
880
881
882
            量化配置
            quant_type:          int 量化类型枚举
                                 0 -> None          不量化,保持原始精度
                                 1 -> Int8          使用 INT8 对称量化
                                 2 -> FP8_E4M3      使用 FP8 E4M3 格式 (__HIP_E4M3_FNUZ)
                                 3 -> FP8_UE8M0     使用 DeepSeekV3.1 提出的 UE8M0 格式 (仅支持round_scale=True)
                                 4 -> FP8_E5M2      使用 FP8 E5M2 格式 (__HIP_E5M2_FNUZ)
            quant_group_size:    int 量化分组大小
                                 0  -> 逐token量化 (per-channel) 
                                 128-> 每 128 元素一组 (per-group) 量化
            fp8_round_scale:     bool 是否将 FP8 缩放因子取整为 2 的幂
                                 true  -> 缩放因子 = 2^k,硬件零开销
                                 false -> 缩放因子 = 任意浮点,精度更高
            异步配置
Chenggang Zhao's avatar
Chenggang Zhao committed
883
884
885
            async_finish: the current stream will not wait for the communication kernels to be finished if set.
            return_recv_hook: return a receiving hook if set. If set, the kernel will just do the RDMA request issues,
                but **without actually receiving the data**. You must call the received hook to make sure the data's arrival.
lishen's avatar
lishen committed
886
                If you do not set this flag, the kernel will ensure the data's arrival.
Chenggang Zhao's avatar
Chenggang Zhao committed
887
888

        Returns:
889
            recv_x: a tensor or tuple with received tokens for each expert.
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
                - packed_recv_x:
                     存储接收到的 Token 数据,形状为
                     `[num_local_experts, num_ranks * num_max_dispatch_tokens_per_rank, hidden]`。
                     数据类型取决于 quant_type:
                      quant_type == 1 -> torch.int8
                         quant_type == 2 -> torch.float8_e4m3fnuz
                         quant_type == 3 -> torch.float8_e4m3fnuz (UE8M0 使用 E4M3 格式存储)
                         quant_type == 4 -> torch.float8_e5m2fnuz
                         其他 (非量化)   -> torch.bfloat16
                - packed_recv_x_scales (可选):
                     仅在 quant_type > 0 时存在,存储量化的 Scale 值。
                     形状为 `[num_local_experts, num_ranks * num_max_dispatch_tokens_per_rank, scales_col_size]`。
                     - 当 quant_type == 3 (UE8M0) 时:
                         scales_col_size = hidden // 512
                         数据类型为 torch.int (内部打包存储 4-bit scale)。
                         *注意:此模式强制要求 fp8_round_scale=True 且 group_size=128。
                     - 当 quant_type == 1, 2, 4 时:
                         scales_col_size = hidden // 128 (若使用 group_size) 或 1 (per-channel)。
                         数据类型为 torch.float32。
Chenggang Zhao's avatar
Chenggang Zhao committed
909
                Moreover, not all tokens are valid, only some of the `num_max_dispatch_tokens_per_rank * num_ranks` are,
910
                as we do not synchronize CPU received count with GPU (also not incompatible with CUDA graph if synced).
Chenggang Zhao's avatar
Chenggang Zhao committed
911
            recv_count: a tensor shaped `[num_local_experts]` with type `torch.int`, indicating how many tokens each
lishen's avatar
lishen committed
912
                expert receives. As mentioned before, not all tokens are valid in `recv_x`.
Chenggang Zhao's avatar
Chenggang Zhao committed
913
914
915
916
            handle: the communication handle to be used in the `low_latency_combine` function.
            event: the event after executing the kernel (valid only if `async_finish` is set).
            hook: the receiving hook function (valid only if `return_recv_hook` is set).
        """
lishen's avatar
lishen committed
917
918
919
        packed_recv_x, packed_recv_x_scales, packed_recv_count, packed_recv_src_info, packed_recv_layout_range, event, hook = \
            self.runtime.low_latency_dispatch(x, topk_idx,
                                              num_max_dispatch_tokens_per_rank, num_experts,
920
                                              quant_type, quant_group_size, fp8_round_scale,
lishen's avatar
lishen committed
921
                                              async_finish, return_recv_hook)
lishen's avatar
lishen committed
922
923
924
925
        handle = (packed_recv_src_info, packed_recv_layout_range, num_max_dispatch_tokens_per_rank, x.size(1), num_experts)
        tensors_to_record = (x, topk_idx,
                             packed_recv_x, packed_recv_x_scales, packed_recv_count,
                             packed_recv_src_info, packed_recv_layout_range)
926
927
928

        recv_x = (packed_recv_x, packed_recv_x_scales) if (quant_type > 0) else packed_recv_x
        return recv_x, packed_recv_count, handle, EventOverlap(event, tensors_to_record if async_finish else None), hook
Chenggang Zhao's avatar
Chenggang Zhao committed
929

930
931
932
933
934
    def low_latency_combine(self, x: torch.Tensor, topk_idx: torch.Tensor, topk_weights: torch.Tensor, handle: tuple,
                            # combine sbo params
                            packed_recv_count: torch.Tensor = None, comp_signal: torch.Tensor = None,
                            block_m: int = -1, threshold: int = -1, num_sms: int = -1,
                            use_logfmt: bool = False,
935
                            zero_copy: bool = False, async_finish: bool = False,
lishen's avatar
lishen committed
936
937
                            return_recv_hook: bool = False, out: Optional[torch.Tensor] = None,
                            combine_wait_recv_cost_stats: Optional[torch.Tensor] = None) -> \
lishen's avatar
lishen committed
938
            Tuple[torch.Tensor, EventOverlap, Callable]:
Chenggang Zhao's avatar
Chenggang Zhao committed
939
940
941
942
        """
        A low-latency implementation for combining tokens (reduce **with weights**) with IBGDA.
        This kernel requires all the ranks (no matter intranode or internode) should be visible via RDMA
            (specifically, IBGDA must be enabled).
lishen's avatar
lishen committed
943
944
945
        Even for ranks in the same node, NVLink are fully disabled for simplicity.
        Warning: as there are only two buffers, and the returned tensors reuse the buffer, you can not hold more than 2
            low-latency kernels' result tensor at a single moment.
Chenggang Zhao's avatar
Chenggang Zhao committed
946
947
948
949

        Arguments:
            x: `[num_local_experts, num_max_dispatch_tokens_per_rank * num_ranks, hidden]` with `torch.bfloat16`,
                the local calculated tokens to be sent to this original rank and reduced.
lijian6's avatar
lijian6 committed
950
951
952
            topk_idx: `[num_combined_tokens, num_topk]` with `torch.int64`, the expert indices selected by the dispatched
                tokens. `-1` indices (not selecting any expert) are supported. Note that, `num_combined_tokens` equals
                to the number of dispatched tokens.
Chenggang Zhao's avatar
Chenggang Zhao committed
953
954
955
            topk_weights: `[num_combined_tokens, num_topk]` with `torch.float`, the expert weights selected by the dispatched
                tokens. The received tokens will be reduced with the weights in this tensor.
            handle: the communication handle given by the `dispatch` function.
956
957
            zero_copy: whether the tensor is already copied into the RDMA buffer, should be cooperative
                with `get_next_low_latency_combine_buffer`.
Chenggang Zhao's avatar
Chenggang Zhao committed
958
959
960
            async_finish: the current stream will not wait for the communication kernels to be finished if set.
            return_recv_hook: return a receiving hook if set. If set, the kernel will just do the RDMA request issues,
                but **without actually receiving the data**. You must call the received hook to make sure the data's arrival.
lishen's avatar
lishen committed
961
                If you not set this flag, the kernel will ensure the data's arrival.
962
            use_logfmt: whether to use an internal "LogFMT with dynamic per-64-channel cast" format (10 bits).
963
            out: the in-place output tensor, if set, the kernel will write the result to this tensor and return it directly.
lishen's avatar
lishen committed
964
965
966
            combine_wait_recv_cost_stats: a cumulative time spent waiting to receive each token tensor for statistics,
                which should have shape `[num_ranks, num_ranks]` and be typed as `torch.int64`.
                This is useful for detecting and pre-cisely localizing slow anomalies.
Chenggang Zhao's avatar
Chenggang Zhao committed
967
968

        Returns:
lishen's avatar
lishen committed
969
            combined_x: the reduced token tensor, with shape `[num_combined_tokens, num_topk]` and type `torch.bfloat16`.
Chenggang Zhao's avatar
Chenggang Zhao committed
970
971
972
            event: the event after executing the kernel (valid only if `async_finish` is set).
            hook: the receiving hook function (valid only if `return_recv_hook` is set).
        """
973
        src_info, layout_range, num_max_dispatch_tokens_per_rank, hidden, num_experts = handle
lishen's avatar
lishen committed
974
        combined_x, event, hook = self.runtime.low_latency_combine(x, topk_idx, topk_weights, src_info, layout_range,
975
                                                                   packed_recv_count, comp_signal, block_m, threshold, num_sms,
lishen's avatar
lishen committed
976
                                                                   combine_wait_recv_cost_stats, 
lishen's avatar
lishen committed
977
                                                                   num_max_dispatch_tokens_per_rank, num_experts,
978
                                                                   use_logfmt, zero_copy, async_finish, return_recv_hook, out)
Chenggang Zhao's avatar
Chenggang Zhao committed
979
980
        tensors_to_record = (x, topk_idx, topk_weights, src_info, layout_range, combined_x)
        return combined_x, EventOverlap(event, tensors_to_record if async_finish else None), hook
981
982
983
984
985
986
987
988
989
990
991
992
993
994

    def get_next_low_latency_combine_buffer(self, handle: object):
        """
        Get the raw registered RDMA buffer tensor for next low-latency combine, so that the next combine kernel can skip the copying.

        Arguments:
            handle: the communication handle given by the `dispatch` function.

        Returns:
            buffer: the raw RDMA low-latency buffer as a BF16 PyTorch tensor with shape
                `[num_local_experts, num_ranks * num_max_dispatch_tokens_per_rank, hidden]`, you should fill this buffer
                by yourself.
        """
        src_info, layout_range, num_max_dispatch_tokens_per_rank, hidden, num_experts = handle
lishen's avatar
lishen committed
995
        return self.runtime.get_next_low_latency_combine_buffer(num_max_dispatch_tokens_per_rank, hidden, num_experts)