aqlm.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright 2024 The HuggingFace Team. 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, 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. "AQLM (Additive Quantization of Language Model) integration file"
  15. from ..quantizers.quantizers_utils import should_convert_module
  16. from ..utils import is_torch_available, logging
  17. if is_torch_available():
  18. import torch
  19. import torch.nn as nn
  20. logger = logging.get_logger(__name__)
  21. def replace_with_aqlm_linear(model, modules_to_not_convert: list[str] | None = None, quantization_config=None):
  22. """
  23. Public method that recursively replaces the Linear layers of the given model with AQLM quantized layers.
  24. Args:
  25. model (`torch.nn.Module`):
  26. The model to convert, can be any `torch.nn.Module` instance.
  27. modules_to_not_convert (`list[str]`, *optional*, defaults to `None`):
  28. A list of nn.Linear weights to not convert. If a parameter path is in the list (e.g. `lm_head.weight`), the corresponding module will not be
  29. converted.
  30. quantization_config (`AqlmConfig`):
  31. The quantization config object that contains the quantization parameters.
  32. """
  33. from aqlm import QuantizedLinear
  34. has_been_replaced = False
  35. # we need this to correctly materialize the weights during quantization
  36. for module_name, module in model.named_modules():
  37. if not should_convert_module(module_name, modules_to_not_convert):
  38. continue
  39. with torch.device("meta"):
  40. if isinstance(module, nn.Linear):
  41. new_module = QuantizedLinear(
  42. module.in_features,
  43. module.out_features,
  44. bias=module.bias is not None,
  45. in_group_size=quantization_config.in_group_size,
  46. out_group_size=quantization_config.out_group_size,
  47. num_codebooks=quantization_config.num_codebooks,
  48. nbits_per_codebook=quantization_config.nbits_per_codebook,
  49. )
  50. new_module.source_cls = type(module)
  51. new_module.requires_grad_(False)
  52. model.set_submodule(module_name, new_module)
  53. has_been_replaced = True
  54. if not has_been_replaced:
  55. logger.warning(
  56. "You are loading your model using eetq but no linear modules were found in your model."
  57. " Please double check your model architecture, or submit an issue on github if you think this is"
  58. " a bug."
  59. )
  60. return model