| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Unit tests for the get_fp8_min_max() helper function. |
| 4 | |
| 5 | These tests verify the FP8 min/max value logic for both standard |
| 6 | and fnuz (ROCm MI300) dtype handling. |
| 7 | """ |
| 8 | |
| 9 | from unittest.mock import patch |
| 10 | |
| 11 | import pytest |
| 12 | import torch |
| 13 | |
| 14 | from vllm.model_executor.layers.quantization.utils.quant_utils import ( |
| 15 | get_fp8_min_max, |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | class TestGetFp8MinMax: |
| 20 | """Test cases for get_fp8_min_max() function.""" |
| 21 | |
| 22 | @patch("vllm.model_executor.layers.quantization.utils.quant_utils.current_platform") |
| 23 | def test_standard_fp8_platform(self, mock_platform): |
| 24 | """Test that standard FP8 platform uses PyTorch's finfo values.""" |
| 25 | mock_platform.is_fp8_fnuz.return_value = False |
| 26 | mock_platform.fp8_dtype.return_value = torch.float8_e4m3fn |
| 27 | |
| 28 | fp8_min, fp8_max = get_fp8_min_max() |
| 29 | finfo = torch.finfo(torch.float8_e4m3fn) |
| 30 | |
| 31 | # Standard FP8 max is 448.0 for e4m3fn |
| 32 | assert fp8_max == finfo.max, f"Expected finfo.max={finfo.max}, got {fp8_max}" |
| 33 | assert fp8_min == finfo.min, f"Expected finfo.min={finfo.min}, got {fp8_min}" |
| 34 | |
| 35 | @patch("vllm.model_executor.layers.quantization.utils.quant_utils.current_platform") |
| 36 | def test_fnuz_platform_returns_224(self, mock_platform): |
| 37 | """Test that fnuz platform returns 224.0.""" |
| 38 | mock_platform.is_fp8_fnuz.return_value = True |
| 39 | |
| 40 | fp8_min, fp8_max = get_fp8_min_max() |
| 41 | |
| 42 | # fnuz on ROCm MI300 should return 224.0, not 240.0 |
| 43 | assert fp8_max == 224.0, f"Expected 224.0 for fnuz platform, got {fp8_max}" |
| 44 | assert fp8_min == -224.0, f"Expected -224.0 for fnuz platform, got {fp8_min}" |
| 45 | |
| 46 | @patch("vllm.model_executor.layers.quantization.utils.quant_utils.current_platform") |
| 47 | def test_non_fnuz_platform_uses_finfo(self, mock_platform): |
| 48 | """Test that non-fnuz platform uses finfo values.""" |
| 49 | mock_platform.is_fp8_fnuz.return_value = False |
| 50 | mock_platform.fp8_dtype.return_value = torch.float8_e4m3fn |
| 51 | |
| 52 | fp8_min, fp8_max = get_fp8_min_max() |
| 53 | finfo = torch.finfo(torch.float8_e4m3fn) |
| 54 | |
| 55 | assert fp8_max == finfo.max, ( |
| 56 | f"Non-fnuz platform should use finfo.max={finfo.max}, got {fp8_max}" |
| 57 | ) |
| 58 | assert fp8_min == finfo.min, ( |
| 59 | f"Non-fnuz platform should use finfo.min={finfo.min}, got {fp8_min}" |
| 60 | ) |
| 61 | |
| 62 | |
| 63 | if __name__ == "__main__": |
| 64 | pytest.main([__file__, "-v"]) |