Skip to content

Commit 59f23e4

Browse files
Fix Megatron reference adapter loading
1 parent e7b50c3 commit 59f23e4

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

swift/megatron/trainers/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def _prepare_peft_model(self, models):
198198
if args.mcore_model is None:
199199
self.bridge.load_weights(models, args.model_dir)
200200
peft_models = [prepare_mcore_model(args, model) for model in models]
201-
if args.tuner_type == 'lora' and args.adapters and args.mcore_adapter is None:
201+
if args.tuner_type in {'lora', 'lora_llm'} and args.adapters and args.mcore_adapter is None:
202202
assert len(args.adapters) == 1, 'Currently only support one adapter.'
203203
self.bridge.load_weights(models, args.adapters[0], peft_format=True, adapter_name='default')
204204
return peft_models

swift/megatron/trainers/rlhf_mixin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def _load_checkpoint(self):
2121
if args.mcore_ref_model is not None:
2222
load_mcore_checkpoint(args, self.ref_models, load_arg='mcore_ref_model')
2323
if args.mcore_ref_adapter is not None:
24-
load_mcore_checkpoint(args, self.wrapped_models, load_arg='mcore_ref_adapter')
24+
load_mcore_checkpoint(args, self.wrapped_models, load_arg='mcore_ref_adapter', adapter_name='ref_adapter')
2525
super()._load_checkpoint()
2626

2727
def prepare_model(self):
@@ -39,10 +39,10 @@ def prepare_model(self):
3939
ref_model_id_or_path = args.ref_model or args.model
4040
ref_model_dir = safe_snapshot_download(ref_model_id_or_path, use_hf=args.use_hf, hub_token=args.hub_token)
4141
self.bridge.load_weights(self.ref_models, ref_model_dir)
42-
if args.tuner_type == 'lora' and args.ref_adapters and args.mcore_ref_adapter is None:
42+
if args.tuner_type in {'lora', 'lora_llm'} and args.ref_adapters and args.mcore_ref_adapter is None:
4343
assert len(args.ref_adapters) == 1, 'Currently only support one adapter.'
4444
self.bridge.load_weights(
45-
self.ref_models, args.ref_adapters[0], peft_format=True, adapter_name='ref_adapter')
45+
self.unwrapped_models, args.ref_adapters[0], peft_format=True, adapter_name='ref_adapter')
4646

4747
def _get_data_collator(self):
4848
if self.args.rlhf_type in ('grpo', 'gkd'):

swift/megatron/utils/megatron_lm_utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ def _load_optimizer_state_dict(optimizer, state_dict):
170170

171171
def _filter_adapter_state_dict(state_dict, peft_format: bool, adapter_name: str = 'default'):
172172
"""
173-
When peft_format is True, keep only the PEFT format state_dict;
173+
When peft_format is True, keep only the requested PEFT adapter and map its
174+
checkpoint lookup keys from the default adapter slot;
174175
when False, remove the PEFT format state_dict.
175176
176177
This function ensures it is called when tuner_type != 'full'.
@@ -191,7 +192,13 @@ def _filter_adapter_state_dict(state_dict, peft_format: bool, adapter_name: str
191192
state_dict_model = state_dict[model_key]
192193
for k, v in state_dict_model.items():
193194
if peft_format:
194-
if '.lora_A.' in k or '.lora_B.' in k or '.modules_to_save.' in k:
195+
adapter_modules = ('lora_A', 'lora_B', 'modules_to_save')
196+
if any(f'.{module}.{adapter_name}.' in k for module in adapter_modules):
197+
if adapter_name != 'default':
198+
# Keep the state-dict key for the target adapter, but read the tensor from the
199+
# default adapter slot used by the source checkpoint.
200+
for module in adapter_modules:
201+
v.key = v.key.replace(f'.{module}.{adapter_name}.', f'.{module}.default.')
195202
new_state_dict[k] = v
196203
else:
197204
if '.lora_A.' in k or '.lora_B.' in k or 'original_module.' in k:

0 commit comments

Comments
 (0)