test_activation.py 2.25 KB
Newer Older
1
import pytest
Woosuk Kwon's avatar
Woosuk Kwon committed
2
import torch
3

4
from vllm.model_executor.layers.activation import FastGELU, NewGELU, SiluAndMul
Woosuk Kwon's avatar
Woosuk Kwon committed
5

6
7
8
9
DTYPES = [torch.half, torch.bfloat16, torch.float]
NUM_TOKENS = [7, 83, 2048]  # Arbitrary values for testing
D = [512, 4096, 5120, 13824]  # Arbitrary values for testing
SEEDS = [0]
10
DEVICES = [i for i in range(1 if torch.cuda.device_count() == 1 else 2)]
11

Woosuk Kwon's avatar
Woosuk Kwon committed
12

13
14
15
16
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
17
@pytest.mark.parametrize("device", DEVICES)
Woosuk Kwon's avatar
Woosuk Kwon committed
18
@torch.inference_mode()
19
def test_silu_and_mul(
Woosuk Kwon's avatar
Woosuk Kwon committed
20
21
22
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
23
    seed: int,
24
    device: int,
Woosuk Kwon's avatar
Woosuk Kwon committed
25
) -> None:
26
27
    torch.random.manual_seed(seed)
    torch.cuda.manual_seed(seed)
28
29
    gpu_id = f"cuda:{device}"
    x = torch.randn(num_tokens, 2 * d, dtype=dtype, device=gpu_id)
30
31
32
    layer = SiluAndMul()
    out = layer(x)
    ref_out = layer._forward(x)
Woosuk Kwon's avatar
Woosuk Kwon committed
33
34
35
    assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)


36
37
38
39
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
40
@pytest.mark.parametrize("device", DEVICES)
41
@torch.inference_mode()
42
def test_gelu_new(
43
44
45
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
46
    seed: int,
47
    device: int,
48
) -> None:
49
50
    torch.random.manual_seed(seed)
    torch.cuda.manual_seed(seed)
51
52
    gpu_id = f"cuda:{device}"
    x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
53
54
55
    layer = NewGELU()
    out = layer(x)
    ref_out = layer._forward(x)
56
57
58
    assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)


59
60
61
62
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
63
@pytest.mark.parametrize("device", DEVICES)
64
def test_gelu_fast(
65
66
67
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
68
    seed: int,
69
    device: int,
70
) -> None:
71
72
    torch.random.manual_seed(seed)
    torch.cuda.manual_seed(seed)
73
74
    gpu_id = f"cuda:{device}"
    x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
75
76
77
    layer = FastGELU()
    out = layer(x)
    ref_out = layer._forward(x)
78
    assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)