|
| 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 | +from . import base, consts |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.fake_quantize_per_channel_affine |
| 22 | +def test_fake_quantize_per_channel_affine(): |
| 23 | + class BenchmarkFakeQuantizePerChannelAffine(base.Benchmark): |
| 24 | + """ |
| 25 | + Benchmark fake_quantize_per_channel_affine operator |
| 26 | + """ |
| 27 | + |
| 28 | + axis_configs = (0, 1) |
| 29 | + DEFAULT_SHAPES = [ |
| 30 | + (4, 4), |
| 31 | + (64, 64), |
| 32 | + (128, 256), |
| 33 | + (512, 512), |
| 34 | + (1024, 1024), |
| 35 | + (2, 3, 128, 128), |
| 36 | + (8, 16, 64, 64), |
| 37 | + ] |
| 38 | + |
| 39 | + def set_shapes(self, shape_file_path=None): |
| 40 | + self.shapes = self.DEFAULT_SHAPES |
| 41 | + |
| 42 | + def get_input_iter(self, dtype): |
| 43 | + for shape in self.shapes: |
| 44 | + for axis in self.axis_configs: |
| 45 | + if axis >= len(shape): |
| 46 | + continue |
| 47 | + inp = torch.randn(shape, dtype=dtype, device="cuda") |
| 48 | + n_channels = shape[axis] |
| 49 | + scale = ( |
| 50 | + torch.rand(n_channels, dtype=torch.float32, device="cuda") * 0.1 |
| 51 | + + 0.01 |
| 52 | + ) |
| 53 | + zero_point = torch.zeros( |
| 54 | + n_channels, dtype=torch.int32, device="cuda" |
| 55 | + ) |
| 56 | + quant_min = 0 |
| 57 | + quant_max = 255 |
| 58 | + yield inp, scale, zero_point, axis, quant_min, quant_max |
| 59 | + |
| 60 | + def forward(self, inp, scale, zero_point, axis, quant_min, quant_max): |
| 61 | + return torch.fake_quantize_per_channel_affine( |
| 62 | + inp, scale, zero_point, axis, quant_min, quant_max |
| 63 | + ) |
| 64 | + |
| 65 | + bench = BenchmarkFakeQuantizePerChannelAffine( |
| 66 | + op_name="fake_quantize_per_channel_affine", |
| 67 | + torch_op=torch.fake_quantize_per_channel_affine, |
| 68 | + dtypes=consts.FLOAT_DTYPES, |
| 69 | + ) |
| 70 | + bench.run() |
0 commit comments