|
| 1 | +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 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, |
| 10 | +# software 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 | +from typing import Tuple |
| 16 | + |
| 17 | +import torch |
| 18 | +from torch.nn import Module |
| 19 | +from tqdm import tqdm |
| 20 | + |
| 21 | + |
| 22 | +__all__ = [ |
| 23 | + "is_module_quantized", |
| 24 | + "is_model_quantized", |
| 25 | + "iter_named_leaf_modules", |
| 26 | + "module_type", |
| 27 | + "calculate_compression_ratio", |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +def is_module_quantized(module: Module) -> bool: |
| 32 | + """ |
| 33 | + Check if a module is quantized, based on the existence of a non-empty quantization |
| 34 | + scheme |
| 35 | +
|
| 36 | + :param module: pytorch module to check |
| 37 | + :return: True if module is quantized, False otherwise |
| 38 | + """ |
| 39 | + if not hasattr(module, "quantization_scheme"): |
| 40 | + return False |
| 41 | + |
| 42 | + if module.quantization_scheme.weights is not None: |
| 43 | + return True |
| 44 | + |
| 45 | + if module.quantization_scheme.input_activations is not None: |
| 46 | + return True |
| 47 | + |
| 48 | + if module.quantization_scheme.output_activations is not None: |
| 49 | + return True |
| 50 | + |
| 51 | + return False |
| 52 | + |
| 53 | + |
| 54 | +def is_model_quantized(model: Module) -> bool: |
| 55 | + """ |
| 56 | + Check if any modules in a model are quantized, based on the existence of a non-empty |
| 57 | + quantization scheme in at least one module |
| 58 | +
|
| 59 | + :param model: pytorch model |
| 60 | + :return: True if model is quantized, False otherwise |
| 61 | + """ |
| 62 | + |
| 63 | + for _, submodule in iter_named_leaf_modules(model): |
| 64 | + if is_module_quantized(submodule): |
| 65 | + return True |
| 66 | + |
| 67 | + return False |
| 68 | + |
| 69 | + |
| 70 | +def module_type(module: Module) -> str: |
| 71 | + """ |
| 72 | + Gets a string representation of a module type |
| 73 | +
|
| 74 | + :module: pytorch module to get type of |
| 75 | + :return: module type as a string |
| 76 | + """ |
| 77 | + return type(module).__name__ |
| 78 | + |
| 79 | + |
| 80 | +def iter_named_leaf_modules(model: Module) -> Tuple[str, Module]: |
| 81 | + # yields modules that do not have any submodules |
| 82 | + # TODO: potentially expand to add list of allowed submodules such as observers |
| 83 | + for name, submodule in model.named_modules(): |
| 84 | + if len(list(submodule.children())) == 0: |
| 85 | + yield name, submodule |
| 86 | + |
| 87 | + |
| 88 | +def calculate_compression_ratio(model: Module) -> float: |
| 89 | + """ |
| 90 | + Calculates the quantization compression ratio of a pytorch model, based on the |
| 91 | + number of bits needed to represent the total weights in compressed form. Does not |
| 92 | + take into account activation quantizatons. |
| 93 | +
|
| 94 | + :param model: pytorch module to calculate compression ratio for |
| 95 | + :return: compression ratio of the whole model |
| 96 | + """ |
| 97 | + total_compressed = 0.0 |
| 98 | + total_uncompressed = 0.0 |
| 99 | + for name, submodule in tqdm( |
| 100 | + iter_named_leaf_modules(model), |
| 101 | + desc="Calculating quantization compression ratio", |
| 102 | + ): |
| 103 | + for parameter in model.parameters(): |
| 104 | + try: |
| 105 | + uncompressed_bits = torch.finfo(parameter.dtype).bits |
| 106 | + except TypeError: |
| 107 | + uncompressed_bits = torch.iinfo(parameter.dtype).bits |
| 108 | + compressed_bits = uncompressed_bits |
| 109 | + if is_module_quantized(submodule): |
| 110 | + compressed_bits = submodule.quantization_scheme.weights.num_bits |
| 111 | + else: |
| 112 | + print(name) |
| 113 | + num_weights = parameter.numel() |
| 114 | + total_compressed += compressed_bits * num_weights |
| 115 | + total_uncompressed += uncompressed_bits * num_weights |
| 116 | + |
| 117 | + return total_uncompressed / total_compressed |
0 commit comments