123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import pytest
- import torch
- from aphrodite.modeling.layers.activation import FastGELU, NewGELU, SiluAndMul
- 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)]
- @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()
- def test_silu_and_mul(
- num_tokens: int,
- d: int,
- dtype: torch.dtype,
- seed: int,
- device: int,
- ) -> None:
- 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)
- assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)
- @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()
- def test_gelu_new(
- num_tokens: int,
- d: int,
- dtype: torch.dtype,
- seed: int,
- device: int,
- ) -> None:
- 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)
- @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)
- def test_gelu_fast(
- num_tokens: int,
- d: int,
- dtype: torch.dtype,
- seed: int,
- device: int,
- ) -> None:
- 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)
|