fp_quant.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright 2025 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. "FP-Quant integration file"
  15. from ..utils import (
  16. is_fp_quant_available,
  17. )
  18. if is_fp_quant_available():
  19. from fp_quant import FPQuantConfig as FPQuantLinearConfig
  20. from fp_quant import FPQuantDtype
  21. from transformers.utils.quantization_config import FPQuantConfig
  22. def adapt_fp_quant_config(config: FPQuantConfig):
  23. if config.forward_dtype == "mxfp4":
  24. forward_dtype = FPQuantDtype.MXFP4
  25. elif config.forward_dtype == "nvfp4":
  26. forward_dtype = FPQuantDtype.NVFP4
  27. else:
  28. raise ValueError(f"Unsupported forward dtype: {config.forward_dtype}")
  29. if config.backward_dtype == "bf16":
  30. backward_dtype = FPQuantDtype.BF16
  31. else:
  32. raise ValueError(f"Unsupported backward dtype: {config.backward_dtype}")
  33. return FPQuantLinearConfig(
  34. forward_dtype=forward_dtype,
  35. forward_method=config.forward_method,
  36. backward_dtype=backward_dtype,
  37. store_master_weights=config.store_master_weights,
  38. hadamard_group_size=config.hadamard_group_size,
  39. pseudoquantization=config.pseudoquantization,
  40. transform_init=config.transform_init,
  41. modules_to_not_convert=config.modules_to_not_convert,
  42. )