Skip to content

[bugfix] Fix Qwen3.5 LoRA merge export producing wrong state_dict keys#9057

Open
Redamency wants to merge 1 commit intomodelscope:mainfrom
Redamency:fix/qwen3.5-lora-merge-export-keys
Open

[bugfix] Fix Qwen3.5 LoRA merge export producing wrong state_dict keys#9057
Redamency wants to merge 1 commit intomodelscope:mainfrom
Redamency:fix/qwen3.5-lora-merge-export-keys

Conversation

@Redamency
Copy link
Copy Markdown

Issue

Fixes #9046

Root Cause

When using , the method applies which uses the model's conversion mapping (e.g. ) in reverse during saving. For composite models like Qwen3.5 (which has a submodule), this causes state_dict keys to be incorrectly prefixed.

For example:

  • becomes
  • becomes

This makes the exported model unusable.

Fix

Pass to to skip the buggy step. The fix uses to check parameter availability for backward compatibility with older transformers versions.

Testing

Verified with Qwen3.5-0.8B + LoRA merge export:

  • Before fix: All 473 keys had wrong prefixes
  • After fix: All 473 keys are correct and model loads successfully

modelscope#9046)

In transformers>=5.5.0, `save_pretrained` calls `revert_weight_conversion`
which incorrectly applies weight key renaming for composite models like
Qwen3.5. The conversion mapping `^model.language_model -> model` causes
keys like `model.language_model.layers.X.*` to be doubly prefixed as
`model.language_model.language_model.language_model.layers.X.*`, and
`model.visual.*` to become `model.language_model.visual.*`.

Fix: Pass `save_original_format=False` to `save_pretrained` to skip the
buggy `revert_weight_conversion` step. The in-memory state_dict already
has correct keys matching the model's safetensors format. A version check
via `inspect.signature` ensures backward compatibility with older
transformers versions that lack this parameter.
Copy link
Copy Markdown
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 updates save_checkpoint in swift/model/utils.py to conditionally pass save_original_format=False to save_pretrained when supported, preventing a weight conversion bug in transformers>=5.5. Regarding the review feedback, the import inspect statement should be moved to the top of the file to adhere to PEP 8 standards and avoid redundant imports during function execution.

# that corrupts state_dict keys for composite models (e.g. Qwen3.5).
# See: https://github.com/modelscope/ms-swift/issues/9046
save_kwargs = {}
import inspect
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

According to PEP 8 style guidelines, imports should be placed at the top of the file. Please move import inspect to the top-level imports of this module. This improves code organization and avoids re-importing the module on every function call.

References
  1. PEP 8 states that imports should be at the top of the file, just after any module comments and docstrings, and before module globals and constants. Placing imports inside functions is discouraged. (link)

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.

Qwen3.5 lora merge export using wrong keys

1 participant