Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions swift/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from .version import __version__, __release_datetime__
from .tuners import (
Adapter, AdapterConfig, AdapterModule, SwiftModel, LoRA, LoRAConfig,
SWIFT_MAPPING, LoraConfig, PeftConfig, PeftModel, PeftModelForCausalLM,
ResTuningConfig, SideConfig, PeftModelForSeq2SeqLM,
PeftModelForSequenceClassification, PeftModelForTokenClassification,
PrefixTuningConfig, PromptEncoderConfig, PromptLearningConfig,
PromptTuningConfig, get_peft_config, get_peft_model,
get_peft_model_state_dict, Prompt, PromptConfig, PromptModule,
SwiftConfig, SwiftOutput, Swift, SwiftTuners, LongLoRAConfig, LongLoRA,
LongLoRAModelType)
SWIFT_MAPPING, AdaLoraConfig, IA3Config, LoftQConfig, LoHaConfig,
LoKrConfig, LoraConfig, OFTConfig, PeftConfig, PeftModel,
PeftModelForCausalLM, ResTuningConfig, SideConfig,
PeftModelForSeq2SeqLM, PeftModelForSequenceClassification,
PeftModelForTokenClassification, PrefixTuningConfig,
PromptEncoderConfig, PromptLearningConfig, PromptTuningConfig,
get_peft_config, get_peft_model, get_peft_model_state_dict, Prompt,
PromptConfig, PromptModule, SwiftConfig, SwiftOutput, Swift,
SwiftTuners, LongLoRAConfig, LongLoRA, LongLoRAModelType)
from .hub import snapshot_download, push_to_hub, push_to_hub_async, push_to_hub_in_queue
from .trainers import (EvaluationStrategy, FSDPOption, HPSearchBackend,
HubStrategy, IntervalStrategy, SchedulerType,
Expand All @@ -30,9 +31,10 @@
],
'tuners': [
'Adapter', 'AdapterConfig', 'AdapterModule', 'SwiftModel', 'LoRA',
'LoRAConfig', 'SWIFT_MAPPING', 'LoraConfig', 'PeftConfig',
'ResTuningConfig', 'SideConfig', 'PeftModel',
'PeftModelForCausalLM', 'PeftModelForSeq2SeqLM',
'LoRAConfig', 'SWIFT_MAPPING', 'LoraConfig', 'AdaLoraConfig',
'IA3Config', 'LoftQConfig', 'LoHaConfig', 'LoKrConfig',
'OFTConfig', 'PeftConfig', 'ResTuningConfig', 'SideConfig',
'PeftModel', 'PeftModelForCausalLM', 'PeftModelForSeq2SeqLM',
'PeftModelForSequenceClassification',
'PeftModelForTokenClassification', 'PrefixTuningConfig',
'PromptEncoderConfig', 'PromptLearningConfig',
Expand Down
11 changes: 7 additions & 4 deletions swift/tuners/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
from .neftune import NEFTune, NEFTuneConfig
from .longlora.longlora import LongLoRAModelType, LongLoRAConfig, LongLoRA
from .restuning import ResTuning, ResTuningConfig, ResTuningBypassModule
from .peft import (LoraConfig, PeftConfig, PeftModel, PeftModelForCausalLM,
PeftModelForSeq2SeqLM,
from .peft import (AdaLoraConfig, IA3Config, LoftQConfig, LoHaConfig,
LoKrConfig, LoraConfig, OFTConfig, PeftConfig,
PeftModel, PeftModelForCausalLM, PeftModelForSeq2SeqLM,
PeftModelForSequenceClassification,
PeftModelForTokenClassification, PrefixTuningConfig,
PromptEncoderConfig, PromptLearningConfig,
Expand All @@ -33,8 +34,10 @@
'neftune': ['NEFTune', 'NEFTuneConfig'],
'restuning': ['ResTuning', 'ResTuningConfig', 'ResTuningBypassModule'],
'peft': [
'LoraConfig', 'PeftConfig', 'PeftModel', 'PeftModelForCausalLM',
'PeftModelForSeq2SeqLM', 'PeftModelForSequenceClassification',
'AdaLoraConfig', 'IA3Config', 'LoftQConfig', 'LoHaConfig',
'LoKrConfig', 'LoraConfig', 'OFTConfig', 'PeftConfig', 'PeftModel',
'PeftModelForCausalLM', 'PeftModelForSeq2SeqLM',
'PeftModelForSequenceClassification',
'PeftModelForTokenClassification', 'PrefixTuningConfig',
'PromptEncoderConfig', 'PromptLearningConfig',
'PromptTuningConfig', 'get_peft_config', 'get_peft_model',
Expand Down
12 changes: 10 additions & 2 deletions swift/tuners/peft.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import os.path
from typing import Optional

from peft import (LoraConfig, PeftConfig, PeftModel, PeftModelForCausalLM,
PeftModelForSeq2SeqLM, PeftModelForSequenceClassification,
from peft import (AdaLoraConfig, IA3Config, LoftQConfig, LoHaConfig,
LoKrConfig, LoraConfig, OFTConfig, PeftConfig, PeftModel,
PeftModelForCausalLM, PeftModelForSeq2SeqLM,
PeftModelForSequenceClassification,
PeftModelForTokenClassification, PrefixTuningConfig,
PromptEncoderConfig, PromptLearningConfig,
PromptTuningConfig, get_peft_config, get_peft_model,
Expand Down Expand Up @@ -60,6 +62,12 @@ def wrap_module(module):
PrefixTuningConfig = wrap_module(PrefixTuningConfig)
PromptLearningConfig = wrap_module(PromptLearningConfig)
LoraConfig = wrap_module(LoraConfig)
AdaLoraConfig = wrap_module(AdaLoraConfig)
IA3Config = wrap_module(IA3Config)
LoHaConfig = wrap_module(LoHaConfig)
LoKrConfig = wrap_module(LoKrConfig)
LoftQConfig = wrap_module(LoftQConfig)
OFTConfig = wrap_module(OFTConfig)
get_peft_config = get_peft_config
get_peft_model_state_dict = get_peft_model_state_dict
get_peft_model = get_peft_model
23 changes: 22 additions & 1 deletion tests/tuners/test_peft.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SbertForSequenceClassification)
from peft.utils import WEIGHTS_NAME

from swift import LoraConfig, Swift
from swift import AdaLoraConfig, LoraConfig, Swift


class TestPeft(unittest.TestCase):
Expand Down Expand Up @@ -44,3 +44,24 @@ def test_peft_lora_injection(self):
all(
torch.isclose(state_dict[key],
state_dict2[key]).flatten().detach().cpu()))

def test_peft_adalora_injection(self):
model = SbertForSequenceClassification(SbertConfig())
model2 = copy.deepcopy(model)
adalora_config = AdaLoraConfig(
target_modules=['query', 'key', 'value'])
model = Swift.prepare_model(model, adalora_config)
model.save_pretrained(self.tmp_dir, safe_serialization=False)
with open(os.path.join(self.tmp_dir, 'configuration.json'), 'w') as f:
f.write('{}')
self.assertTrue(
os.path.exists(os.path.join(self.tmp_dir, WEIGHTS_NAME)))
model2 = Swift.from_pretrained(model2, self.tmp_dir)
state_dict = model.state_dict()
state_dict2 = model2.state_dict()
for key in state_dict:
self.assertTrue(key in state_dict2)
self.assertTrue(
all(
torch.isclose(state_dict[key],
state_dict2[key]).flatten().detach().cpu()))