|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (c) Microsoft Corporation. |
| 5 | +# Licensed under the MIT License. |
| 6 | +# |
| 7 | +# Copyright (c) 2025 Intel Corporation |
| 8 | +# |
| 9 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +# you may not use this file except in compliance with the License. |
| 11 | +# You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, software |
| 16 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | +"""Utils for quantization.""" |
| 21 | + |
| 22 | +import types |
| 23 | +from typing import Any |
| 24 | + |
| 25 | +import torch |
| 26 | +import torch.nn as nn |
| 27 | + |
| 28 | +from .quant_linear import QuantLinear |
| 29 | +from .tensor_quantizer import TensorQuantizer |
| 30 | + |
| 31 | + |
| 32 | +def convert(module: nn.Module, quant_cfg=None, quant_module=None): |
| 33 | + """Convert the model to a quantized one with quant config.""" |
| 34 | + |
| 35 | + # update class |
| 36 | + original_cls = type(module) |
| 37 | + module.__class__ = quant_module |
| 38 | + module.forward = types.MethodType(quant_module.forward, module) |
| 39 | + |
| 40 | + # setup quantizers |
| 41 | + module._setup(quant_cfg) |
| 42 | + |
| 43 | + return module |
| 44 | + |
| 45 | + |
| 46 | +def replace_with_quant_linear(model, quant_cfg=None): |
| 47 | + """Recursively replace the module with quantized module.""" |
| 48 | + |
| 49 | + # TODO: support more modules, like kv. |
| 50 | + for name, child in model.named_children(): |
| 51 | + if isinstance(child, nn.Linear): |
| 52 | + if "lm_head" in name: |
| 53 | + continue |
| 54 | + # REPLACE on the parent (model), not on child |
| 55 | + quantized = convert(child, quant_cfg, QuantLinear) |
| 56 | + setattr(model, name, quantized) |
| 57 | + |
| 58 | + # now recurse into whichever module is now at `model.name` |
| 59 | + replace_with_quant_linear(getattr(model, name), quant_cfg=quant_cfg) |
| 60 | + |
| 61 | + return model |
| 62 | + |
| 63 | + |
| 64 | +def get_quant_config_with_scheme(scheme: str): |
| 65 | + """Get quantization config.""" |
| 66 | + |
| 67 | + try: |
| 68 | + # use scheme definitions from AutoRound since we utilize the quantization functions now |
| 69 | + from auto_round.schemes import preset_name_to_scheme |
| 70 | + |
| 71 | + quant_cfg = preset_name_to_scheme(scheme) |
| 72 | + return quant_cfg |
| 73 | + except ImportError: |
| 74 | + return None |
| 75 | + |
| 76 | + |
| 77 | +def convert_model_with_mapping(model, mapping=None): |
| 78 | + """Process mapping to quant config.""" |
| 79 | + # key is torch module, TODO: support more key format, like layer name. |
| 80 | + for key in mapping: |
| 81 | + # TODO: support more torch modules |
| 82 | + if key == nn.Linear: |
| 83 | + quant_cfg = get_quant_config_with_scheme(mapping[key]) |
| 84 | + if quant_cfg is None: |
| 85 | + continue |
| 86 | + replace_with_quant_linear(model, quant_cfg) |
| 87 | + |
| 88 | + replaced_modules = sum(isinstance(m, TensorQuantizer) for _, m in model.named_modules()) |
| 89 | + print(f"Inserted {replaced_modules} quantizers") |
| 90 | + |
| 91 | + |
| 92 | +def get_quant_config(scheme: str) -> dict[str, Any]: |
| 93 | + """Generate quantization config for a torch model. |
| 94 | +
|
| 95 | + Args: |
| 96 | + model: The PyTorch model to analyze |
| 97 | +
|
| 98 | + Returns: |
| 99 | + Dictionary containing the quantization configuration |
| 100 | + """ |
| 101 | + |
| 102 | + # TODO: support more quant config |
| 103 | + try: |
| 104 | + from auto_round.export.export_to_llmcompressor.config import initialize_quantization |
| 105 | + |
| 106 | + quantization_config = initialize_quantization(scheme=scheme) |
| 107 | + quantization_config = quantization_config.to_dict() |
| 108 | + quantization_config["provider"] = "auto-round" |
| 109 | + quantization_config["config_groups"]["group_0"]["weights"]["is_mx"] = True |
| 110 | + quantization_config["config_groups"]["group_0"]["input_activations"]["is_mx"] = True |
| 111 | + |
| 112 | + except ImportError: |
| 113 | + quantization_config = None |
| 114 | + |
| 115 | + return quantization_config |
| 116 | + |
| 117 | + |
| 118 | +def get_quantization_format(module) -> str | None: |
| 119 | + """Gets the quantization string. |
| 120 | +
|
| 121 | + Gets the quantization string by iterating through the module and its children. |
| 122 | + The first non-None quantization string is returned. |
| 123 | + """ |
| 124 | + |
| 125 | + def _get_quantization_from_layer(layer): |
| 126 | + weight_quantizer = getattr(layer, "weight_quantizer", None) |
| 127 | + input_quantizer = getattr(layer, "input_quantizer", None) |
| 128 | + |
| 129 | + if weight_quantizer is None or weight_quantizer._disabled: |
| 130 | + return None |
| 131 | + |
| 132 | + # TODO: support more quant format |
| 133 | + if weight_quantizer.num_bits == 8 and weight_quantizer.data_type == "mx_fp8": |
| 134 | + return "MXFP8" |
| 135 | + |
| 136 | + # Raise error for unsupported num_bits |
| 137 | + raise NotImplementedError(f"Unsupported quantizer with num_bits: {weight_quantizer.num_bits}") |
| 138 | + |
| 139 | + quantization = _get_quantization_from_layer(module) |
| 140 | + if quantization is not None: |
| 141 | + return quantization |
| 142 | + |
| 143 | + for _, layer in module.named_children(): |
| 144 | + format = get_quantization_format(layer) |
| 145 | + if format is not None: |
| 146 | + return format |
| 147 | + |
| 148 | + return None |
| 149 | + |
| 150 | + |
| 151 | +def is_quantlinear(module: nn.Module) -> bool: |
| 152 | + """Returns whether the module is a quantized linear layer.""" |
| 153 | + return "QuantLinear" in type(module).__name__ |
0 commit comments