File size: 1,670 Bytes
42832bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Your implementation goes here.

Replace the body of `node_dispatch_pack` with a FAST implementation that reproduces the output of /app/reference.py
(same signature, same numerics within tolerance) but is much faster — see /app/instruction.md.
Run `python /app/measure.py` to check your correctness and achieved GB/s.

You may add helper modules, Triton kernels, CUDA extensions, caches, etc. — only this function's name,
signature and returned value are fixed by the contract.
"""


def node_dispatch_pack(x, topk_ids, num_nodes, experts_per_node):
    """Node-limited MoE dispatch: dedup to node sets and pack the all-to-all send buffer.

    Args:
        x:                (T, H) bfloat16 — token hidden states.
        topk_ids:         (T, K) int32    — the K expert ids chosen for each token (distinct within a row).
        num_nodes:        int             — N, the number of expert-parallel nodes.
        experts_per_node: int             — EPN; expert e lives on node e // EPN.

    Returns:
        (send_buf, src_token, node_counts, node_offsets) where
          send_buf:     (T*M, H) bfloat16 — hidden states in NODE-MAJOR order, tokens ascending inside a node
          src_token:    (T*M,)   int32    — the token each packed row came from
          node_counts:  (N,)     int32    — rows destined for each node
          node_offsets: (N+1,)   int32    — node n owns rows [node_offsets[n], node_offsets[n+1])
        M is the (fixed) number of distinct nodes every token's experts span, so T*M rows are always
        produced.
    """
    raise NotImplementedError("Implement a fast node_dispatch_pack in /app/dispatch_pack.py")