2024-02-21 20:17:52 -08:00
|
|
|
from typing import Type
|
|
|
|
|
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
|
|
|
|
2024-09-11 15:52:19 -04:00
|
|
|
from tests.kernels.utils import opcheck
|
2024-02-21 20:17:52 -08:00
|
|
|
from vllm.model_executor.layers.activation import (FastGELU, GeluAndMul,
|
2024-09-11 15:52:19 -04:00
|
|
|
NewGELU, QuickGELU,
|
|
|
|
SiluAndMul)
|
2023-04-02 00:30:17 -07:00
|
|
|
|
2024-05-13 22:50:09 +08:00
|
|
|
from .allclose_default import get_default_atol, get_default_rtol
|
|
|
|
|
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-02-02 07:46:39 +08:00
|
|
|
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
|
|
|
|
2024-03-12 22:06:17 -07:00
|
|
|
@pytest.mark.parametrize("activation", ["silu", "gelu", "gelu_tanh"])
|
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-02-02 07:46:39 +08:00
|
|
|
@pytest.mark.parametrize("device", CUDA_DEVICES)
|
2023-04-02 00:30:17 -07:00
|
|
|
@torch.inference_mode()
|
2024-02-21 20:17:52 -08:00
|
|
|
def test_act_and_mul(
|
2024-03-12 22:06:17 -07:00
|
|
|
activation: str,
|
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-02-02 07:46:39 +08:00
|
|
|
device: str,
|
2023-04-02 00:30:17 -07:00
|
|
|
) -> None:
|
2023-09-06 08:57:38 +09:00
|
|
|
torch.random.manual_seed(seed)
|
2024-02-02 07:46:39 +08:00
|
|
|
if torch.cuda.is_available():
|
|
|
|
torch.cuda.manual_seed(seed)
|
|
|
|
torch.set_default_device(device)
|
|
|
|
x = torch.randn(num_tokens, 2 * d, dtype=dtype)
|
2024-03-12 22:06:17 -07:00
|
|
|
if activation == "silu":
|
|
|
|
layer = SiluAndMul()
|
2024-09-11 15:52:19 -04:00
|
|
|
fn = torch.ops._C.silu_and_mul
|
2024-03-12 22:06:17 -07:00
|
|
|
elif activation == "gelu":
|
|
|
|
layer = GeluAndMul(approximate="none")
|
2024-09-11 15:52:19 -04:00
|
|
|
fn = torch.ops._C.gelu_and_mul
|
2024-03-12 22:06:17 -07:00
|
|
|
elif activation == "gelu_tanh":
|
|
|
|
layer = GeluAndMul(approximate="tanh")
|
2024-09-11 15:52:19 -04:00
|
|
|
fn = torch.ops._C.gelu_tanh_and_mul
|
2023-12-02 21:18:40 -08:00
|
|
|
out = layer(x)
|
2024-06-05 09:18:19 -07:00
|
|
|
ref_out = layer.forward_native(x)
|
2024-02-21 20:17:52 -08:00
|
|
|
# The SiLU and GELU implementations are equivalent to the native PyTorch
|
|
|
|
# implementations, so we can do exact comparison.
|
2024-08-15 21:24:04 -07:00
|
|
|
torch.testing.assert_close(out, ref_out, atol=0.0, rtol=0.0)
|
2023-04-02 00:30:17 -07:00
|
|
|
|
2024-09-11 15:52:19 -04:00
|
|
|
d = x.shape[-1] // 2
|
|
|
|
output_shape = (x.shape[:-1] + (d, ))
|
|
|
|
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
|
|
|
|
opcheck(fn, (out, x))
|
2023-04-02 00:30:17 -07:00
|
|
|
|
2024-09-11 15:52:19 -04:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("activation", [(FastGELU, torch.ops._C.gelu_fast),
|
|
|
|
(NewGELU, torch.ops._C.gelu_new),
|
|
|
|
(QuickGELU, torch.ops._C.gelu_quick)])
|
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-02-02 07:46:39 +08:00
|
|
|
@pytest.mark.parametrize("device", CUDA_DEVICES)
|
2023-08-23 07:43:21 +09:00
|
|
|
@torch.inference_mode()
|
2024-02-21 20:17:52 -08:00
|
|
|
def test_activation(
|
|
|
|
activation: Type[torch.nn.Module],
|
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-02-02 07:46:39 +08:00
|
|
|
device: str,
|
2023-08-23 07:43:21 +09:00
|
|
|
) -> None:
|
2023-09-06 08:57:38 +09:00
|
|
|
torch.random.manual_seed(seed)
|
2024-02-02 07:46:39 +08:00
|
|
|
if torch.cuda.is_available():
|
|
|
|
torch.cuda.manual_seed(seed)
|
|
|
|
torch.set_default_device(device)
|
|
|
|
x = torch.randn(num_tokens, d, dtype=dtype)
|
2024-09-11 15:52:19 -04:00
|
|
|
layer = activation[0]()
|
|
|
|
fn = activation[1]
|
2023-12-02 21:18:40 -08:00
|
|
|
out = layer(x)
|
2024-06-05 09:18:19 -07:00
|
|
|
ref_out = layer.forward_native(x)
|
2024-08-15 21:24:04 -07:00
|
|
|
torch.testing.assert_close(out,
|
|
|
|
ref_out,
|
|
|
|
atol=get_default_atol(out),
|
|
|
|
rtol=get_default_rtol(out))
|
2024-09-11 15:52:19 -04:00
|
|
|
|
|
|
|
out = torch.empty_like(x)
|
|
|
|
opcheck(fn, (out, x))
|