vllm/tests/kernels/test_activation.py

79 lines
2.3 KiB
Python
Raw Normal View History

2023-09-06 08:57:38 +09:00
import pytest
2023-04-02 00:30:17 -07:00
import torch
2023-09-06 08:57:38 +09:00
from vllm.model_executor.layers.activation import FastGELU, NewGELU, SiluAndMul
2023-04-02 00:30:17 -07:00
2023-09-06 08:57:38 +09:00
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]
DEVICES = [i for i in range(1 if torch.cuda.device_count() == 1 else 2)]
2023-09-06 08:57:38 +09:00
2023-04-02 00:30:17 -07:00
2023-09-06 08:57:38 +09:00
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
@pytest.mark.parametrize("device", DEVICES)
2023-04-02 00:30:17 -07:00
@torch.inference_mode()
2023-09-06 08:57:38 +09:00
def test_silu_and_mul(
2023-04-02 00:30:17 -07:00
num_tokens: int,
d: int,
dtype: torch.dtype,
2023-09-06 08:57:38 +09:00
seed: int,
device: int,
2023-04-02 00:30:17 -07:00
) -> None:
2023-09-06 08:57:38 +09:00
torch.random.manual_seed(seed)
torch.cuda.manual_seed(seed)
gpu_id = f"cuda:{device}"
x = torch.randn(num_tokens, 2 * d, dtype=dtype, device=gpu_id)
layer = SiluAndMul()
out = layer(x)
ref_out = layer._forward(x)
2023-04-02 00:30:17 -07:00
assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)
2023-09-06 08:57:38 +09:00
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
@pytest.mark.parametrize("device", DEVICES)
@torch.inference_mode()
2023-09-06 08:57:38 +09:00
def test_gelu_new(
num_tokens: int,
d: int,
dtype: torch.dtype,
2023-09-06 08:57:38 +09:00
seed: int,
device: int,
) -> None:
2023-09-06 08:57:38 +09:00
torch.random.manual_seed(seed)
torch.cuda.manual_seed(seed)
gpu_id = f"cuda:{device}"
x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
layer = NewGELU()
out = layer(x)
ref_out = layer._forward(x)
assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)
2023-09-06 08:57:38 +09:00
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
@pytest.mark.parametrize("device", DEVICES)
2023-09-06 08:57:38 +09:00
def test_gelu_fast(
num_tokens: int,
d: int,
dtype: torch.dtype,
2023-09-06 08:57:38 +09:00
seed: int,
device: int,
) -> None:
2023-09-06 08:57:38 +09:00
torch.random.manual_seed(seed)
torch.cuda.manual_seed(seed)
gpu_id = f"cuda:{device}"
x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
layer = FastGELU()
out = layer(x)
ref_out = layer._forward(x)
assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)