test_mamba_mixer2.py 3.9 KB
Newer Older
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
3
4
5
6
7
8
9

import unittest

import pytest
import torch

from tests.utils import multi_gpu_test
10
11
12
13
from vllm.distributed.parallel_state import (
    init_distributed_environment,
    initialize_model_parallel,
)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from vllm.model_executor.layers.mamba.mamba_mixer2 import Mixer2RMSNormGated
from vllm.platforms import current_platform
from vllm.utils import update_environment_variables


@multi_gpu_test(num_gpus=2)
@pytest.mark.parametrize("batch_size", [8])
@pytest.mark.parametrize("seq_len", [128])
@pytest.mark.parametrize(
    "hidden_size_n_groups",
    [
        (64, 1),
        (64, 2),
        (64, 4),  # hidden_size be divisible by num_gpus
        (100, 5),  # and n_groups must divide hidden_size
29
30
    ],
)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
31
32
33
34
@pytest.mark.parametrize("dtype", [torch.float16])
def test_mixer2_gated_norm_multi_gpu(
    batch_size: int,
    seq_len: int,
35
    hidden_size_n_groups: tuple[int, int],
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
36
    dtype: torch.dtype,
37
    device: str = "cuda",
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
38
39
40
41
42
43
44
):
    hidden_size, n_groups = hidden_size_n_groups
    num_processes = 2

    def run_torch_spawn(fn, nprocs):
        # need to use torch.mp.spawn otherwise will have problems with
        # torch.distributed and cuda
45
46
47
48
49
50
51
52
53
54
55
56
57
        torch.multiprocessing.spawn(
            fn,
            args=(
                num_processes,
                batch_size,
                seq_len,
                hidden_size,
                n_groups,
                dtype,
                device,
            ),
            nprocs=nprocs,
        )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

    run_torch_spawn(mixer2_gated_norm_tensor_parallel, 2)


def mixer2_gated_norm_tensor_parallel(
    local_rank: int,
    world_size: int,
    batch_size: int,
    seq_len: int,
    hidden_size: int,
    n_groups: int,
    dtype: torch.dtype,
    device: str,
):
    current_platform.seed_everything(0)

    device = torch.device(f"cuda:{local_rank}")
    torch.cuda.set_device(device)
    torch.set_default_device(device)
    torch.set_default_dtype(dtype)

79
80
81
82
83
84
85
86
87
    update_environment_variables(
        {
            "RANK": str(local_rank),
            "LOCAL_RANK": str(local_rank),
            "WORLD_SIZE": str(world_size),
            "MASTER_ADDR": "localhost",
            "MASTER_PORT": "12345",
        }
    )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
88
89
90
91
92
93

    # initialize distributed
    init_distributed_environment()
    initialize_model_parallel(tensor_model_parallel_size=world_size)

    # create random weights an inputs
94
    weight = torch.rand((hidden_size,), dtype=dtype, device=device)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
95
96
97
98
99
100
101
102
103
104
105
106
    hidden_states = torch.randn(batch_size, seq_len, hidden_size)
    gate_states = torch.randn(batch_size, seq_len, hidden_size)

    # create gated-norm with TP
    mixer = Mixer2RMSNormGated(
        full_hidden_size=hidden_size,
        full_n_groups=n_groups,
    )
    mixer.weight.weight_loader(mixer.weight, weight)  # load

    # create gated-norm without TP to compute reference
    # - utilize mock patching to disable TP when
107
108
    with (
        unittest.mock.patch(
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
109
110
            "vllm.model_executor.layers.mamba.mamba_mixer2."
            "get_tensor_model_parallel_world_size",
111
112
113
114
115
116
117
118
            return_value=1,
        ),
        unittest.mock.patch(
            "vllm.model_executor.layers.mamba.mamba_mixer2."
            "get_tensor_model_parallel_rank",
            return_value=0,
        ),
    ):
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
119
120
121
122
123
124
125
126
127
128
        mixer_single_gpu = Mixer2RMSNormGated(
            full_hidden_size=hidden_size,
            full_n_groups=n_groups,
        )
    # assign weight to single-gpu mixer
    mixer_single_gpu.weight.data = weight

    # generate and compare
    N = hidden_size // world_size
    output = mixer(
129
130
        hidden_states[..., local_rank * N : (local_rank + 1) * N],
        gate_states[..., local_rank * N : (local_rank + 1) * N],
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
131
132
    )
    ref_output = mixer_single_gpu(hidden_states, gate_states)
133
134
135
136
137
138
    torch.testing.assert_close(
        output,
        ref_output[..., local_rank * N : (local_rank + 1) * N],
        atol=5e-3,
        rtol=1e-3,
    )