"vscode:/vscode.git/clone" did not exist on "82de9b9d468dab451380d3e7dda88b0c40a31204"
test_activation.py 3.59 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
import random
4

5
import pytest
Woosuk Kwon's avatar
Woosuk Kwon committed
6
import torch
7

8
from tests.kernels.utils import opcheck
9
from vllm.model_executor.layers.activation import (FastGELU, FatreluAndMul,
10
11
12
                                                   GeluAndMul, MulAndSilu,
                                                   NewGELU, QuickGELU,
                                                   SiluAndMul)
13
from vllm.platforms import current_platform
Woosuk Kwon's avatar
Woosuk Kwon committed
14

15
16
from .allclose_default import get_default_atol, get_default_rtol

17
18
DTYPES = [torch.half, torch.bfloat16, torch.float]
NUM_TOKENS = [7, 83, 2048]  # Arbitrary values for testing
19
D = [512, 13824]  # Arbitrary values for testing
20
SEEDS = [0]
21
22
23
CUDA_DEVICES = [
    f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)
]
24

Woosuk Kwon's avatar
Woosuk Kwon committed
25

26
27
28
@pytest.mark.parametrize(
    "activation",
    ["silu_and_mul", "mul_and_silu", "gelu", "gelu_tanh", "fatrelu"])
29
30
31
32
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
33
@pytest.mark.parametrize("device", CUDA_DEVICES)
Woosuk Kwon's avatar
Woosuk Kwon committed
34
@torch.inference_mode()
35
def test_act_and_mul(
36
    activation: str,
Woosuk Kwon's avatar
Woosuk Kwon committed
37
38
39
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
40
    seed: int,
41
    device: str,
Woosuk Kwon's avatar
Woosuk Kwon committed
42
) -> None:
43
    current_platform.seed_everything(seed)
44
45
    torch.set_default_device(device)
    x = torch.randn(num_tokens, 2 * d, dtype=dtype)
46
    if activation == "silu_and_mul":
47
        layer = SiluAndMul()
48
        fn = torch.ops._C.silu_and_mul
49
50
51
    if activation == "mul_and_silu":
        layer = MulAndSilu()
        fn = torch.ops._C.mul_and_silu
52
53
    elif activation == "gelu":
        layer = GeluAndMul(approximate="none")
54
        fn = torch.ops._C.gelu_and_mul
55
56
    elif activation == "gelu_tanh":
        layer = GeluAndMul(approximate="tanh")
57
        fn = torch.ops._C.gelu_tanh_and_mul
58
59
60
61
    elif activation == "fatrelu":
        threshold = random.uniform(0, 1)
        layer = FatreluAndMul(threshold)
        fn = torch.ops._C.fatrelu_and_mul
62
    out = layer(x)
63
    ref_out = layer.forward_native(x)
64
65
66
    # The SiluAndMul, MulAndSilu, GELU and FatReLU implementations are
    # equivalent to the native PyTorch implementations, so we can do exact
    # comparison.
67
    torch.testing.assert_close(out, ref_out, atol=0.0, rtol=0.0)
Woosuk Kwon's avatar
Woosuk Kwon committed
68

69
70
71
    d = x.shape[-1] // 2
    output_shape = (x.shape[:-1] + (d, ))
    out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
72
73
74
75
    if activation == "fatrelu":
        opcheck(fn, (out, x, threshold))
    else:
        opcheck(fn, (out, x))
Woosuk Kwon's avatar
Woosuk Kwon committed
76

77
78
79
80

@pytest.mark.parametrize("activation", [(FastGELU, torch.ops._C.gelu_fast),
                                        (NewGELU, torch.ops._C.gelu_new),
                                        (QuickGELU, torch.ops._C.gelu_quick)])
81
82
83
84
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
85
@pytest.mark.parametrize("device", CUDA_DEVICES)
86
@torch.inference_mode()
87
def test_activation(
88
    activation: type[torch.nn.Module],
89
90
91
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
92
    seed: int,
93
    device: str,
94
) -> None:
95
    current_platform.seed_everything(seed)
96
97
    torch.set_default_device(device)
    x = torch.randn(num_tokens, d, dtype=dtype)
98
99
    layer = activation[0]()
    fn = activation[1]
100
    out = layer(x)
101
    ref_out = layer.forward_native(x)
102
103
104
105
    torch.testing.assert_close(out,
                               ref_out,
                               atol=get_default_atol(out),
                               rtol=get_default_rtol(out))
106
107
108

    out = torch.empty_like(x)
    opcheck(fn, (out, x))