test_layernorm.py 1.83 KB
Newer Older
1
import pytest
2
3
import torch

4
from vllm.model_executor.layers.layernorm import RMSNorm
5

6
7
DTYPES = [torch.half, torch.bfloat16, torch.float]
NUM_TOKENS = [7, 83, 4096]  # Arbitrary values for testing
8
9
HIDDEN_SIZES = [768, 5120, 8192]  # Arbitrary values for testing
ADD_RESIDUAL = [False, True]
10
SEEDS = [0]
11
DEVICES = [i for i in range(1 if torch.cuda.device_count() == 1 else 2)]
12

13

14
15
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("hidden_size", HIDDEN_SIZES)
16
@pytest.mark.parametrize("add_residual", ADD_RESIDUAL)
17
18
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
19
@pytest.mark.parametrize("device", DEVICES)
20
@torch.inference_mode()
21
def test_rms_norm(
22
23
    num_tokens: int,
    hidden_size: int,
24
    add_residual: bool,
25
    dtype: torch.dtype,
26
    seed: int,
27
    device: int,
28
) -> None:
29
30
    torch.random.manual_seed(seed)
    torch.cuda.manual_seed(seed)
31
32
    gpu_id = f"cuda:{device}"
    layer = RMSNorm(hidden_size).to(dtype=dtype, device=gpu_id)
33
34
    layer.weight.data.normal_(mean=1.0, std=0.1)
    scale = 1 / (2 * hidden_size)
35
    x = torch.randn(num_tokens, hidden_size, dtype=dtype, device=gpu_id)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    x *= scale
    residual = torch.randn_like(x) * scale if add_residual else None

    # NOTE(woosuk): The reference implementation should be executed first
    # because the custom kernel is in-place.
    ref_out = layer._forward(x, residual)
    out = layer(x, residual)
    # NOTE(woosuk): LayerNorm operators (including RMS) typically have larger
    # numerical errors than other operators because they involve reductions.
    # Therefore, we use a larger tolerance.
    if add_residual:
        assert torch.allclose(out[0], ref_out[0], atol=1e-2, rtol=1e-2)
        assert torch.allclose(out[1], ref_out[1], atol=1e-2, rtol=1e-2)
    else:
        assert torch.allclose(out, ref_out, atol=1e-2, rtol=1e-2)