Skip to content

[bugfix] fix PeftModel save_pretrained selected_adapters when using custom adapter_name#8337

Open
alphadl wants to merge 1 commit intomodelscope:mainfrom
alphadl:fix/8336-peft-selected-adapters
Open

[bugfix] fix PeftModel save_pretrained selected_adapters when using custom adapter_name#8337
alphadl wants to merge 1 commit intomodelscope:mainfrom
alphadl:fix/8336-peft-selected-adapters

Conversation

@alphadl
Copy link

@alphadl alphadl commented Mar 14, 2026

[bugfix] fix PeftModel save_pretrained selected_adapters when using custom adapter_name

#8336

When users pass a custom adapter_name (e.g. vision_only_lora) to Swift.prepare_model(), checkpoint save was still passing selected_adapters=['default'] to PeftModel.save_pretrained(), causing:

ValueError: You passed an invalid `selected_adapters` arguments, current supported adapter names are ['vision_only_lora'] - got ['default'].

Changes:

  • swift/trainers/mixin.py: Add _get_peft_selected_adapters(model) to resolve active_adapter / active_adapters from the PeftModel; use it in _save_model() for both PeftModel save paths instead of hardcoded ['default'].
  • tests/tuners/test_peft.py: Add tests for default adapter, custom adapter_name, and mock (None/str/list).

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the Swift framework's model saving mechanism, specifically impacting PeftModel instances configured with custom adapter names. Previously, attempts to save such models would result in a ValueError because the save_pretrained method was hardcoded to look for a 'default' adapter. The changes introduce a robust way to identify the currently active adapter(s) and ensure these are correctly passed during the saving process, thereby preventing errors and allowing seamless saving of models with custom PEFT configurations.

Highlights

  • Bug Fix for PeftModel Saving: Resolved an issue where PeftModel.save_pretrained failed when a custom adapter_name was used, due to an incorrect selected_adapters=['default'] argument.
  • New Adapter Resolution Function: Introduced _get_peft_selected_adapters to dynamically determine the active adapter name(s) from a PeftModel instance.
  • Integration into Save Logic: Updated the _save_model function to utilize _get_peft_selected_adapters, ensuring the correct adapter names are passed to PeftModel.save_pretrained.
  • Comprehensive Test Coverage: Added new unit tests to test_peft.py to validate the behavior of _get_peft_selected_adapters for default, custom, and mocked adapter scenarios.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • swift/trainers/mixin.py
    • Added _get_peft_selected_adapters to resolve active adapter names for PeftModel.save_pretrained.
    • Modified _save_model to use the new _get_peft_selected_adapters function instead of a hardcoded ['default'] for PeftModel saving.
  • tests/tuners/test_peft.py
    • Added test_get_peft_selected_adapters_custom_name to verify correct handling of custom adapter names.
    • Added test_get_peft_selected_adapters_default to confirm fallback to 'default' when no custom name is specified.
    • Added test_get_peft_selected_adapters_mock to test various active_adapter and active_adapters scenarios.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a bug where saving a PeftModel with a custom adapter name would fail. The fix correctly introduces a helper function, _get_peft_selected_adapters, to determine the active adapters and uses this function when calling save_pretrained. The changes are logical and are accompanied by new unit tests. I have identified a potential issue in the implementation of the new helper function that could affect multi-adapter scenarios and have provided a suggestion for a more robust implementation.

we must pass the same name(s) to save_pretrained; otherwise PEFT raises because
supported adapter names do not include 'default'. See: gh#8336.
"""
active = getattr(model, 'active_adapter', None) or getattr(model, 'active_adapters', None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current logic for determining active adapters might be incorrect in a multi-adapter scenario. active_adapter is a property that returns the first adapter from active_adapters. By checking active_adapter first, you might only get the first active adapter instead of all of them if multiple are active.

To correctly retrieve all active adapters, you should check for active_adapters first.

Suggested change
active = getattr(model, 'active_adapter', None) or getattr(model, 'active_adapters', None)
active = getattr(model, 'active_adapters', None) or getattr(model, 'active_adapter', None)

@alphadl
Copy link
Author

alphadl commented Mar 14, 2026

Please take a look when you have time. This fixes the PeftModel save error when using a custom adapter_name (fixes #8336). cc @Jintao-Huang @hjh0119

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant