Ramzes / tests /test_mapping.py
Bordoglor's picture
Upload folder using huggingface_hub
302920f verified
# Copyright 2025-present the HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
import torch
from peft import LoraConfig, get_peft_model
class TestGetPeftModel:
RELOAD_WARNING_EXPECTED_MATCH = r"You are trying to modify a model .*"
@pytest.fixture
def lora_config_0(self):
return LoraConfig(target_modules="0")
@pytest.fixture
def base_model(self):
return torch.nn.Sequential(torch.nn.Linear(10, 2), torch.nn.Linear(2, 10))
def test_get_peft_model_warns_when_reloading_model(self, lora_config_0, base_model):
get_peft_model(base_model, lora_config_0)
with pytest.warns(UserWarning, match=self.RELOAD_WARNING_EXPECTED_MATCH):
get_peft_model(base_model, lora_config_0)
def test_get_peft_model_proposed_fix_in_warning_helps(self, lora_config_0, base_model, recwarn):
peft_model = get_peft_model(base_model, lora_config_0)
peft_model.unload()
get_peft_model(base_model, lora_config_0)
warning_checker = pytest.warns(UserWarning, match=self.RELOAD_WARNING_EXPECTED_MATCH)
for warning in recwarn:
if warning_checker.matches(warning):
pytest.fail("Warning raised even though model was unloaded.")
def test_get_peft_model_repeated_invocation(self, lora_config_0, base_model):
peft_model = get_peft_model(base_model, lora_config_0)
# use direct-addressing of the other layer to accomodate for the nested model
lora_config_1 = LoraConfig(target_modules="base_model.model.1")
with pytest.warns(UserWarning, match=self.RELOAD_WARNING_EXPECTED_MATCH):
get_peft_model(peft_model, lora_config_1)