# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import torch from modelopt.torch.quantization import utils as quant_utils from modelopt.torch.quantization.calib.max import MaxCalibrator class PercentileCalibrator(MaxCalibrator): def __init__(self, num_bits=8, axis=None, unsigned=False, track_amax=False, **kwargs): super().__init__(num_bits, axis, unsigned, track_amax) self.percentile = kwargs["percentile"] self.total_step = kwargs["total_step"] self.collect_method = kwargs["collect_method"] self.data = {} self.i = 0 def collect(self, x): """Tracks the absolute max of all tensors. Args: x: A tensor Raises: RuntimeError: If amax shape changes """ # Swap axis to reduce. reduce_axis = quant_utils.convert_quantization_axis_to_reduce_axis(x, self._axis) local_amax = quant_utils.reduce_amax(x, axis=reduce_axis).detach() _cur_step = self.i % self.total_step if _cur_step not in self.data: self.data[_cur_step] = local_amax elif self.collect_method == "global_min": self.data[_cur_step] = torch.min(self.data[_cur_step], local_amax) elif self.collect_method in {"min-max", "mean-max"}: self.data[_cur_step] = torch.max(self.data[_cur_step], local_amax) else: self.data[_cur_step] += local_amax if self._track_amax: raise NotImplementedError self.i += 1 def compute_amax(self): """Return the absolute max of all tensors collected.""" up_lim = int(self.total_step * self.percentile) if self.collect_method == "min-mean": amaxs_values = [self.data[i] / self.total_step for i in range(up_lim)] else: amaxs_values = [self.data[i] for i in range(up_lim)] if self.collect_method == "mean-max": act_amax = torch.vstack(amaxs_values).mean(axis=0)[0] else: act_amax = torch.vstack(amaxs_values).min(axis=0)[0] self._calib_amax = act_amax return self._calib_amax def __str__(self): s = "PercentileCalibrator" return s.format(**self.__dict__) def __repr__(self): s = "PercentileCalibrator(" s += super(MaxCalibrator, self).__repr__() s += " calib_amax={_calib_amax}" if self._track_amax: s += " amaxs={_amaxs}" s += ")" return s.format(**self.__dict__)