test_numerical.py 3.89 KB
Newer Older
Sengxian's avatar
Sengxian committed
1
2
3
4
5
from fmoe.layers import FMoE
from fmoe.transformer import _Expert
from fmoe.gates import NaiveGate

from moe import BruteForceMoELinear
Rick Ho's avatar
Rick Ho committed
6
7
8
9
import torch
import sys
import os

Sengxian's avatar
Sengxian committed
10
11
rank = 0
world_size = 1
Rick Ho's avatar
Rick Ho committed
12
13


Sengxian's avatar
Sengxian committed
14
def test_fmoe_linear():
Rick Ho's avatar
Rick Ho committed
15
16
17
18
    torch.manual_seed(42 + rank)
    torch.cuda.manual_seed(42 + rank)
    batch_size = 4
    num_expert = 2
Sengxian's avatar
Sengxian committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    d_model = 6
    d_hidden = 8
    top_k = 2
    activation = torch.nn.functional.gelu

    experts = _Expert(num_expert, d_model, d_hidden, activation).cuda()

    def expert_fn(inp, gate):
        return experts(inp, gate)

    moe = FMoE(
        num_expert=num_expert,
        d_model=d_model,
        gate=NaiveGate,
        world_size=world_size,
        mp_group=None,
        expert_fn=expert_fn,
        top_k=top_k,
    ).cuda()
Rick Ho's avatar
Rick Ho committed
38

Sengxian's avatar
Sengxian committed
39
40
41
42
43
44
    moe_raw = BruteForceMoELinear(
        activation=activation,
        num_expert=num_expert,
        d_model=d_model,
        world_size=world_size,
    ).cuda()
Rick Ho's avatar
Rick Ho committed
45
46

    if world_size == 1:
Sengxian's avatar
Sengxian committed
47
48
        moe_raw.weight_htoh4.data = experts.htoh4.weight.data.clone()
        moe_raw.weight_h4toh.data = experts.h4toh.weight.data.clone()
Rick Ho's avatar
Rick Ho committed
49
    else:
Sengxian's avatar
Sengxian committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        weight_htoh4_array = [
            torch.empty_like(experts.htoh4.weight.data) for _ in range(world_size)
        ]
        torch.distributed.all_gather(weight_htoh4_array, experts.htoh4.weight.data)
        moe_raw.weight_htoh4.data = torch.cat(weight_htoh4_array, dim=0)

        weight_h4toh_array = [
            torch.empty_like(experts.h4toh.weight.data) for _ in range(world_size)
        ]
        torch.distributed.all_gather(weight_h4toh_array, experts.h4toh.weight.data)
        moe_raw.weight_h4toh.data = torch.cat(weight_h4toh_array, dim=0)

    inp = torch.rand(batch_size, d_model).cuda()

    gate_idx, gate_score = moe.gate(inp)
    print(gate_idx.shape, gate_score.shape)
    inp_repeated = inp.repeat_interleave(repeats=top_k, dim=0)

    moe_out = moe(inp).mean()
    raw_out = moe_raw(inp_repeated, gate_idx, gate_score).mean()

    moe_out.backward()
    raw_out.backward()

    moe_out = moe_out, experts.htoh4.weight.grad, experts.h4toh.weight.grad
    raw_out = raw_out, moe_raw.weight_htoh4.grad, moe_raw.weight_h4toh.grad

    names = ["output", "htoh4 weight grad", "h4toh weight grad"]
Rick Ho's avatar
Rick Ho committed
78
    if world_size > 1:
Sengxian's avatar
Sengxian committed
79
80
81
82
83
84
        ou, htoh4_grad, h4toh_grad = raw_out
        torch.distributed.all_reduce(htoh4_grad)
        torch.distributed.all_reduce(h4toh_grad)
        htoh4_grad = htoh4_grad[rank * num_expert : (rank + 1) * num_expert]
        h4toh_grad = h4toh_grad[rank * num_expert : (rank + 1) * num_expert]
        raw_out = ou, htoh4_grad, h4toh_grad
Rick Ho's avatar
Rick Ho committed
85
86
    for name, mo, ro in zip(names, moe_out, raw_out):
        err = (mo - ro).abs().sum()
Sengxian's avatar
Sengxian committed
87
        print("Rank {} {} abs err {}".format(rank, name, err))
Rick Ho's avatar
Rick Ho committed
88
        if err > 1e-3:
Sengxian's avatar
Sengxian committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
            sys.stderr.write("=========== moe out ==============\n")
            sys.stderr.write("{}\n".format(mo))
            sys.stderr.write("=========== raw out ==============\n")
            sys.stderr.write("{}\n".format(ro))
            assert False
    torch.cuda.synchronize()


def test_fmoe_linear_distributed():
    import subprocess
    import os

    os.environ["MASTER_ADDR"] = "localhost"
    os.environ["MASTER_PORT"] = "36666"
    ps, n = [], 2
    os.environ["WORLD_SIZE"] = str(n)

    for i in range(n):
        os.environ["RANK"] = str(i)
        os.environ["CUDA_VISIBLE_DEVICES"] = str(i)
        p = subprocess.Popen([sys.executable, __file__], stdout=subprocess.PIPE)
        ps.append(p)

    for p in ps:
        p.wait()
        retc = p.poll()
        assert retc == 0


if __name__ == "__main__":
    # os.environ["RANK"] = os.environ.get("OMPI_COMM_WORLD_RANK", "0")
    # os.environ["WORLD_SIZE"] = os.environ.get("OMPI_COMM_WORLD_SIZE", "1")
    if int(os.environ["WORLD_SIZE"]) > 1:
        torch.distributed.init_process_group(backend="nccl")
Rick Ho's avatar
Rick Ho committed
123
124
        rank = torch.distributed.get_rank()
        world_size = torch.distributed.get_world_size()
Sengxian's avatar
Sengxian committed
125
    test_fmoe_linear()