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
|
|
|
|
2023-12-02 21:18:40 -08: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]
|
2024-01-03 11:09:59 +08:00
|
|
|
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)
|
2024-01-03 11:09:59 +08:00
|
|
|
@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,
|
2024-01-03 11:09:59 +08:00
|
|
|
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)
|
2024-01-03 11:09:59 +08:00
|
|
|
gpu_id = f"cuda:{device}"
|
|
|
|
x = torch.randn(num_tokens, 2 * d, dtype=dtype, device=gpu_id)
|
2023-12-02 21:18:40 -08:00
|
|
|
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)
|
2024-01-03 11:09:59 +08:00
|
|
|
@pytest.mark.parametrize("device", DEVICES)
|
2023-08-23 07:43:21 +09:00
|
|
|
@torch.inference_mode()
|
2023-09-06 08:57:38 +09:00
|
|
|
def test_gelu_new(
|
2023-08-23 07:43:21 +09:00
|
|
|
num_tokens: int,
|
|
|
|
d: int,
|
|
|
|
dtype: torch.dtype,
|
2023-09-06 08:57:38 +09:00
|
|
|
seed: int,
|
2024-01-03 11:09:59 +08:00
|
|
|
device: int,
|
2023-08-23 07:43:21 +09:00
|
|
|
) -> None:
|
2023-09-06 08:57:38 +09:00
|
|
|
torch.random.manual_seed(seed)
|
|
|
|
torch.cuda.manual_seed(seed)
|
2024-01-03 11:09:59 +08:00
|
|
|
gpu_id = f"cuda:{device}"
|
|
|
|
x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
|
2023-12-02 21:18:40 -08:00
|
|
|
layer = NewGELU()
|
|
|
|
out = layer(x)
|
|
|
|
ref_out = layer._forward(x)
|
2023-08-23 07:43:21 +09: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)
|
2024-01-03 11:09:59 +08:00
|
|
|
@pytest.mark.parametrize("device", DEVICES)
|
2023-09-06 08:57:38 +09:00
|
|
|
def test_gelu_fast(
|
2023-08-23 07:43:21 +09:00
|
|
|
num_tokens: int,
|
|
|
|
d: int,
|
|
|
|
dtype: torch.dtype,
|
2023-09-06 08:57:38 +09:00
|
|
|
seed: int,
|
2024-01-03 11:09:59 +08:00
|
|
|
device: int,
|
2023-08-23 07:43:21 +09:00
|
|
|
) -> None:
|
2023-09-06 08:57:38 +09:00
|
|
|
torch.random.manual_seed(seed)
|
|
|
|
torch.cuda.manual_seed(seed)
|
2024-01-03 11:09:59 +08:00
|
|
|
gpu_id = f"cuda:{device}"
|
|
|
|
x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
|
2023-12-02 21:18:40 -08:00
|
|
|
layer = FastGELU()
|
|
|
|
out = layer(x)
|
|
|
|
ref_out = layer._forward(x)
|
2023-08-23 07:43:21 +09:00
|
|
|
assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)
|