fix(lora): expand path-pattern target modules to HF names - #261
Open
SakaiXue6666 wants to merge 1 commit into
Open
fix(lora): expand path-pattern target modules to HF names#261SakaiXue6666 wants to merge 1 commit into
SakaiXue6666 wants to merge 1 commit into
Conversation
Bridge's LoRA matcher accepts path patterns, and the Qwen3.5-35B-A3B SFT script uses them to scope adapters to the main decoder so the MTP layers stay frozen. convert_megatron_to_hf_target_modules looked up the whole string, so a pattern missed the mapping and took the passthrough branch, reaching adapter_config.json and SGLang's --lora-target-modules verbatim. Neither matches a glob, so the exported adapter config named no module at all. Reduce a pattern to its trailing segment before the lookup, and reject a pattern whose trailing segment is itself a wildcard, which has no HF equivalent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
convert_megatron_to_hf_target_modulesnow reduces a Bridge target-module pattern to the module name it selects before the Megatron→HF lookup, so patterns expand like bare names do. A pattern whose trailing segment is itself a wildcard is rejected instead of silently passed through.Why
Bridge's LoRA matcher accepts path patterns, and the tree already depends on that.
scripts/training/sft/run-qwen3.5-35B-A3B-pokemon-lora-mtp-8xgpu.shuses them to scope adapters to the main decoder, and says why:Injection honors the pattern; export does not.
convert_megatron_to_hf_target_moduleslooks the whole string up inMEGATRON_TO_HF_MODULES, so a pattern misses and takes the "already HF-style or custom" passthrough branch. The glob then reaches every HF-facing consumer verbatim:adapter_config.json, both from_save_lora_to_checkpoint(which runs on every save whenever LoRA is enabled) and fromLoraAdapterSync.write_adapter_dirfor the live adapter directoryLoRAConfig.from_dictinLoraAdapterSync.config_dict--lora-target-modulesinsglang_engineNone of these glob — PEFT and SGLang both match module names by suffix. So with the script above the exported adapter records
"target_modules": ["*decoder.layers.*.linear_qkv", "*decoder.layers.*.linear_proj"]where it should record["q_proj", "k_proj", "v_proj", "o_proj"], and nothing downstream matches a single module.How
Take the trailing dotted segment, then run the existing one-to-many expansion on it. Bare names contain no dot and are unchanged, so the common path keeps its current behavior; a fully qualified module path works like a pattern.
HF target module names carry no position, so a pattern's anchoring cannot be represented in the exported config. It does not need to be: the anchoring is already carried by which weights the exported adapter actually contains, which is what both PEFT and SGLang key off when they decide where an adapter applies.
The one case with no meaningful reduction is a wildcard inside the trailing segment itself (
*decoder.layers.*.linear_*). Passing it through would reproduce exactly the failure above, so it raisesValueErrornaming the offending pattern and pointing at the anchored form.Testing
Run inside the Relax training image (
sglang 0.5.12.post1,megatron.bridge 0.5.0,torch 2.11.0+cu129):Four cases were added to
TestConvertMegatronToHfTargetModules, covering the pattern from the shipped script, de-duplication between a pattern and the bare name it ends with, a fully qualified path, and the rejected trailing wildcard. Reverting onlyrelax/utils/megatron_peft_utils.pytomainand rerunning those four fails all four, so they do pin the change.ruff format --checkandruff checkare clean on both touched files.pre-commit run --all-filespasses (ruff format + ruff check run directly, see above)pytest tests/)Type of Change
Screenshots / Logs
Behavior of the conversion before and after, for the arguments the shipped MTP script passes: