|
| 1 | +# Copyright 2026 FlagOS Contributors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +import torch |
| 17 | + |
| 18 | +import flag_gems |
| 19 | + |
| 20 | +from .accuracy_utils import gems_assert_close, to_reference |
| 21 | + |
| 22 | +QUANT_SHAPES = [(4, 4), (16, 32), (2, 3, 4), (8, 16, 32)] |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.fake_quantize_per_channel_affine |
| 26 | +@pytest.mark.parametrize("shape", QUANT_SHAPES) |
| 27 | +@pytest.mark.parametrize("axis", [0, 1]) |
| 28 | +@pytest.mark.parametrize("dtype", [torch.float16, torch.float32, torch.bfloat16]) |
| 29 | +@pytest.mark.parametrize("quant_min, quant_max", [(0, 255), (-128, 127)]) |
| 30 | +def test_accuracy_fake_quantize_per_channel_affine( |
| 31 | + shape, axis, dtype, quant_min, quant_max |
| 32 | +): |
| 33 | + if axis >= len(shape): |
| 34 | + pytest.skip(f"axis {axis} >= ndim {len(shape)}") |
| 35 | + |
| 36 | + inp = torch.randn(shape, dtype=dtype, device=flag_gems.device) |
| 37 | + n_channels = shape[axis] |
| 38 | + scale = ( |
| 39 | + torch.rand(n_channels, dtype=torch.float32, device=flag_gems.device) * 0.1 |
| 40 | + + 0.01 |
| 41 | + ) |
| 42 | + zero_point = torch.randint( |
| 43 | + quant_min, |
| 44 | + quant_max + 1, |
| 45 | + (n_channels,), |
| 46 | + dtype=torch.int32, |
| 47 | + device=flag_gems.device, |
| 48 | + ) |
| 49 | + |
| 50 | + ref_inp = to_reference(inp) |
| 51 | + ref_scale = to_reference(scale) |
| 52 | + ref_zero_point = to_reference(zero_point) |
| 53 | + |
| 54 | + ref_out = torch.fake_quantize_per_channel_affine( |
| 55 | + ref_inp, ref_scale, ref_zero_point, axis, quant_min, quant_max |
| 56 | + ) |
| 57 | + |
| 58 | + with flag_gems.use_gems(): |
| 59 | + res_out = torch.fake_quantize_per_channel_affine( |
| 60 | + inp, scale, zero_point, axis, quant_min, quant_max |
| 61 | + ) |
| 62 | + |
| 63 | + gems_assert_close(res_out, ref_out, dtype=dtype) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.fake_quantize_per_channel_affine |
| 67 | +@pytest.mark.parametrize("shape", [(2, 3, 4, 5)]) |
| 68 | +@pytest.mark.parametrize("axis", [0, 1, 2, 3]) |
| 69 | +def test_accuracy_fake_quantize_per_channel_affine_multi_dim(shape, axis): |
| 70 | + inp = torch.randn(shape, dtype=torch.float32, device=flag_gems.device) |
| 71 | + n_channels = shape[axis] |
| 72 | + scale = ( |
| 73 | + torch.rand(n_channels, dtype=torch.float32, device=flag_gems.device) * 0.1 |
| 74 | + + 0.01 |
| 75 | + ) |
| 76 | + zero_point = torch.randint( |
| 77 | + 0, 255, (n_channels,), dtype=torch.int32, device=flag_gems.device |
| 78 | + ) |
| 79 | + |
| 80 | + ref_inp = to_reference(inp) |
| 81 | + ref_scale = to_reference(scale) |
| 82 | + ref_zero_point = to_reference(zero_point) |
| 83 | + |
| 84 | + ref_out = torch.fake_quantize_per_channel_affine( |
| 85 | + ref_inp, ref_scale, ref_zero_point, axis, 0, 255 |
| 86 | + ) |
| 87 | + |
| 88 | + with flag_gems.use_gems(): |
| 89 | + res_out = torch.fake_quantize_per_channel_affine( |
| 90 | + inp, scale, zero_point, axis, 0, 255 |
| 91 | + ) |
| 92 | + |
| 93 | + gems_assert_close(res_out, ref_out, dtype=torch.float32) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.fake_quantize_per_channel_affine |
| 97 | +def test_accuracy_fake_quantize_per_channel_affine_half_to_even(): |
| 98 | + inp = torch.tensor( |
| 99 | + [[-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5]], |
| 100 | + dtype=torch.float32, |
| 101 | + device=flag_gems.device, |
| 102 | + ) |
| 103 | + scale = torch.ones(8, dtype=torch.float32, device=flag_gems.device) |
| 104 | + zero_point = torch.zeros(8, dtype=torch.int32, device=flag_gems.device) |
| 105 | + ref_out = torch.fake_quantize_per_channel_affine( |
| 106 | + to_reference(inp), to_reference(scale), to_reference(zero_point), 1, -128, 127 |
| 107 | + ) |
| 108 | + |
| 109 | + with flag_gems.use_gems(): |
| 110 | + res_out = torch.fake_quantize_per_channel_affine( |
| 111 | + inp, scale, zero_point, 1, -128, 127 |
| 112 | + ) |
| 113 | + |
| 114 | + gems_assert_close(res_out, ref_out, dtype=torch.float32) |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.fake_quantize_per_channel_affine |
| 118 | +def test_accuracy_fake_quantize_per_channel_affine_empty(): |
| 119 | + inp = torch.empty((2, 0, 3), dtype=torch.float32, device=flag_gems.device) |
| 120 | + scale = torch.empty(0, dtype=torch.float32, device=flag_gems.device) |
| 121 | + zero_point = torch.empty(0, dtype=torch.int32, device=flag_gems.device) |
| 122 | + |
| 123 | + with flag_gems.use_gems(): |
| 124 | + result = torch.fake_quantize_per_channel_affine( |
| 125 | + inp, scale, zero_point, 1, 0, 255 |
| 126 | + ) |
| 127 | + |
| 128 | + assert result.shape == inp.shape |
| 129 | + assert result.dtype == inp.dtype |
0 commit comments