|
| 1 | +# Copyright 2025-present the HuggingFace Inc. team. |
| 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 | + |
| 15 | +# NOTE: PEFT tests related to INC are handled under Optimum-Habana repository: |
| 16 | +# - LLMs: https://github.com/huggingface/optimum-habana/blob/main/tests/test_peft_inference.py |
| 17 | +# - Diffusers: https://github.com/huggingface/optimum-habana/blob/main/tests/test_diffusers.py |
| 18 | + |
| 19 | +from typing import Optional |
| 20 | + |
| 21 | +import torch |
| 22 | + |
| 23 | +from peft.import_utils import is_inc_available |
| 24 | +from peft.tuners.tuners_utils import BaseTunerLayer |
| 25 | + |
| 26 | +from .layer import Linear |
| 27 | + |
| 28 | + |
| 29 | +if is_inc_available(): |
| 30 | + |
| 31 | + class IncLoraLinear(Linear): |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + base_layer: torch.nn.Module, |
| 35 | + adapter_name: str, |
| 36 | + **kwargs, |
| 37 | + ): |
| 38 | + super().__init__(base_layer, adapter_name, **kwargs) |
| 39 | + |
| 40 | + def merge(self, safe_merge: bool = False, adapter_names: Optional[list[str]] = None) -> None: |
| 41 | + """ |
| 42 | + Merge the active adapter weights into the base weights |
| 43 | +
|
| 44 | + Args: |
| 45 | + safe_merge (`bool`, *optional*): |
| 46 | + If True, the merge operation will be performed in a copy of the original weights and check for NaNs |
| 47 | + before merging the weights. This is useful if you want to check if the merge operation will produce |
| 48 | + NaNs. Defaults to `False`. |
| 49 | + adapter_names (`list[str]`, *optional*): |
| 50 | + The list of adapter names that should be merged. If None, all active adapters will be merged. |
| 51 | + Defaults to `None`. |
| 52 | + """ |
| 53 | + raise NotImplementedError("Merging LoRA with INC layers is not yet implemented") |
| 54 | + |
| 55 | + def unmerge(self) -> None: |
| 56 | + """ |
| 57 | + This method unmerges all merged adapter layers from the base weights. |
| 58 | + """ |
| 59 | + raise NotImplementedError("Unmerging LoRA from INC layers is not yet implemented") |
| 60 | + |
| 61 | + |
| 62 | +def dispatch_inc(target: torch.nn.Module, adapter_name: str, **kwargs): |
| 63 | + new_module = None |
| 64 | + |
| 65 | + if isinstance(target, BaseTunerLayer): |
| 66 | + target_base_layer = target.get_base_layer() |
| 67 | + else: |
| 68 | + target_base_layer = target |
| 69 | + |
| 70 | + if is_inc_available(): |
| 71 | + from neural_compressor.torch.algorithms.fp8_quant._quant_common.helper_modules import ( |
| 72 | + PatchedLinear, |
| 73 | + ) |
| 74 | + |
| 75 | + if isinstance(target_base_layer, PatchedLinear): |
| 76 | + new_module = IncLoraLinear(target, adapter_name, **kwargs) |
| 77 | + |
| 78 | + return new_module |
0 commit comments