|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD 3-Clause license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import logging |
| 8 | +from dataclasses import dataclass |
| 9 | + |
| 10 | +import torch |
| 11 | + |
| 12 | +import torchao |
| 13 | +from torchao.core.config import AOBaseConfig |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | +import types |
| 17 | + |
| 18 | +from torchao.quantization.quant_api import _linear_extra_repr |
| 19 | +from torchao.quantization.quantize_.workflows import ( |
| 20 | + Int4ChooseQParamsAlgorithm, |
| 21 | +) |
| 22 | +from torchao.quantization.transform_module import ( |
| 23 | + register_quantize_module_handler, |
| 24 | +) |
| 25 | + |
| 26 | +from .int4_opaque_tensor import Int4OpaqueTensor |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class Int4WeightOnlyOpaqueTensorConfig(AOBaseConfig): |
| 31 | + """ |
| 32 | + Configuration for int4 weight only quantization, only groupwise quantization is supported right now. |
| 33 | +
|
| 34 | + Args: |
| 35 | + `group_size`: parameter for quantization, controls the granularity of quantization, smaller size is more fine grained, choices are [256, 128, 64, 32] |
| 36 | + `int4_choose_qparams_algorithm`: variants of choose qparams algorithm to use for int4, currently support TINYGEMM ("tinygemm") and HQQ ("hqq") |
| 37 | + `set_inductor_config`: if True, adjusts `torchinductor` settings to recommended values |
| 38 | + """ |
| 39 | + |
| 40 | + group_size: int = 128 |
| 41 | + int4_choose_qparams_algorithm: Int4ChooseQParamsAlgorithm = ( |
| 42 | + Int4ChooseQParamsAlgorithm.TINYGEMM |
| 43 | + ) |
| 44 | + set_inductor_config: bool = True |
| 45 | + |
| 46 | + def __post_init__(self): |
| 47 | + torch._C._log_api_usage_once( |
| 48 | + "torchao.prototype.int4_opaque_tensor.Int4WeightOnlyOpaqueTensorConfig" |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def _int4_weight_only_opaque_tensor_quantize(weight, config): |
| 53 | + group_size = config.group_size |
| 54 | + int4_choose_qparams_algorithm = config.int4_choose_qparams_algorithm |
| 55 | + |
| 56 | + if weight.shape[-1] % group_size != 0: |
| 57 | + logger.info( |
| 58 | + f"Skipping quantizing weight with int4 weight only quantization because the shape of weight {weight.shape} is not compatible with group_size {group_size}" |
| 59 | + ) |
| 60 | + return weight |
| 61 | + |
| 62 | + block_size = tuple([1 for _ in range(weight.ndim - 1)] + [group_size]) |
| 63 | + |
| 64 | + block_size = list(block_size) |
| 65 | + |
| 66 | + new_weight = Int4OpaqueTensor.from_hp( |
| 67 | + weight, |
| 68 | + block_size, |
| 69 | + int4_choose_qparams_algorithm=int4_choose_qparams_algorithm, |
| 70 | + ) |
| 71 | + return new_weight |
| 72 | + |
| 73 | + |
| 74 | +@register_quantize_module_handler(Int4WeightOnlyOpaqueTensorConfig) |
| 75 | +def _int4_weight_only_transform( |
| 76 | + module: torch.nn.Module, config: Int4WeightOnlyOpaqueTensorConfig |
| 77 | +) -> torch.nn.Module: |
| 78 | + if config.set_inductor_config: |
| 79 | + torchao.quantization.utils.recommended_inductor_config_setter() |
| 80 | + |
| 81 | + assert hasattr(module, "weight"), ( |
| 82 | + "applying int4 weight only quant requires module to have weight attribute" |
| 83 | + + " but {module} does not have one" |
| 84 | + ) |
| 85 | + new_weight = _int4_weight_only_opaque_tensor_quantize(module.weight, config) |
| 86 | + module.weight = torch.nn.Parameter(new_weight, requires_grad=False) |
| 87 | + module.extra_repr = types.MethodType(_linear_extra_repr, module) |
| 88 | + return module |
0 commit comments