test_activation.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import pytest
  2. import torch
  3. from aphrodite.modeling.layers.activation import FastGELU, NewGELU, SiluAndMul
  4. DTYPES = [torch.half, torch.bfloat16, torch.float]
  5. NUM_TOKENS = [7, 83, 2048] # Arbitrary values for testing
  6. D = [512, 4096, 5120, 13824] # Arbitrary values for testing
  7. SEEDS = [0]
  8. DEVICES = [i for i in range(1 if torch.cuda.device_count() == 1 else 2)]
  9. @pytest.mark.parametrize("num_tokens", NUM_TOKENS)
  10. @pytest.mark.parametrize("d", D)
  11. @pytest.mark.parametrize("dtype", DTYPES)
  12. @pytest.mark.parametrize("seed", SEEDS)
  13. @pytest.mark.parametrize("device", DEVICES)
  14. @torch.inference_mode()
  15. def test_silu_and_mul(
  16. num_tokens: int,
  17. d: int,
  18. dtype: torch.dtype,
  19. seed: int,
  20. device: int,
  21. ) -> None:
  22. torch.random.manual_seed(seed)
  23. torch.cuda.manual_seed(seed)
  24. gpu_id = f"cuda:{device}"
  25. x = torch.randn(num_tokens, 2 * d, dtype=dtype, device=gpu_id)
  26. layer = SiluAndMul()
  27. out = layer(x)
  28. ref_out = layer._forward(x)
  29. assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)
  30. @pytest.mark.parametrize("num_tokens", NUM_TOKENS)
  31. @pytest.mark.parametrize("d", D)
  32. @pytest.mark.parametrize("dtype", DTYPES)
  33. @pytest.mark.parametrize("seed", SEEDS)
  34. @pytest.mark.parametrize("device", DEVICES)
  35. @torch.inference_mode()
  36. def test_gelu_new(
  37. num_tokens: int,
  38. d: int,
  39. dtype: torch.dtype,
  40. seed: int,
  41. device: int,
  42. ) -> None:
  43. torch.random.manual_seed(seed)
  44. torch.cuda.manual_seed(seed)
  45. gpu_id = f"cuda:{device}"
  46. x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
  47. layer = NewGELU()
  48. out = layer(x)
  49. ref_out = layer._forward(x)
  50. assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)
  51. @pytest.mark.parametrize("num_tokens", NUM_TOKENS)
  52. @pytest.mark.parametrize("d", D)
  53. @pytest.mark.parametrize("dtype", DTYPES)
  54. @pytest.mark.parametrize("seed", SEEDS)
  55. @pytest.mark.parametrize("device", DEVICES)
  56. def test_gelu_fast(
  57. num_tokens: int,
  58. d: int,
  59. dtype: torch.dtype,
  60. seed: int,
  61. device: int,
  62. ) -> None:
  63. torch.random.manual_seed(seed)
  64. torch.cuda.manual_seed(seed)
  65. gpu_id = f"cuda:{device}"
  66. x = torch.randn(num_tokens, d, dtype=dtype, device=gpu_id)
  67. layer = FastGELU()
  68. out = layer(x)
  69. ref_out = layer._forward(x)
  70. assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)