vllm/tests/kernels/test_activation.py

84 lines
2.4 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]
CUDA_DEVICES = [
f"cuda:{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", CUDA_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: str,
2023-04-02 00:30:17 -07:00
) -> None:
2023-09-06 08:57:38 +09:00
torch.random.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.set_default_device(device)
x = torch.randn(num_tokens, 2 * d, dtype=dtype)
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", CUDA_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: str,
) -> None:
2023-09-06 08:57:38 +09:00
torch.random.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.set_default_device(device)
x = torch.randn(num_tokens, d, dtype=dtype)
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", CUDA_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: str,
) -> None:
2023-09-06 08:57:38 +09:00
torch.random.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.set_default_device(device)
x = torch.randn(num_tokens, d, dtype=dtype)
layer = FastGELU()
out = layer(x)
ref_out = layer._forward(x)
assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)