"benchmarks/experimental/experimental_async_approaches.py" did not exist on "3b0717eb49ea90ba23f66e876075655905a43719"
utils.py 895 Bytes
Newer Older
Sugon_ldc's avatar
Sugon_ldc committed
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
r"""
Utilities that may be used in the gates
"""
import torch
from fmoe.functions import count_by_gate
import fmoe_cuda as fmoe_native


def limit_by_capacity(topk_idx, num_expert, world_size, capacity):
    with torch.no_grad():
        capacity = torch.ones(num_expert, dtype=torch.int32,
                device=topk_idx.device) * capacity

        pos, lec, gec = count_by_gate(topk_idx, num_expert, world_size,
                require_pos=False)
        new_gec = fmoe_native.limit_by_capacity(gec, capacity,
                num_expert, world_size)
        if world_size > 1:
            new_lec = fmoe_native.expert_exchange(new_gec, num_expert,
                    world_size)
        else:
            new_lec = new_gec

        topk_idx = fmoe_native.prune_gate_by_capacity(topk_idx,
                new_lec.to(torch.int32), num_expert, world_size)
    return new_lec, new_gec, topk_idx