fix: biencoder PEFT adapter key remapping for merge_lora#1479
Merged
Conversation
The biencoder wraps the base model as `lm_q`, so PEFT adapter weights and target modules were saved with incorrect prefixes. The standalone base model (LlamaBidirectionalModel extends LlamaModel) uses bare module names like `layers.0.self_attn.q_proj` — no `model.` prefix. This caused merge_lora.py to fail because PEFT could not match the target modules or weight keys against the base model. Changes: - bidirectional.py: fix PEFT key path in to_hf via peft_dst parameter (base_model.model.lm_q.X → base_model.model.X, was incorrectly producing base_model.model.model.X). Fix from_hf to handle PEFT keys explicitly. - addons.py: strip lm_q. prefix from target modules in _extract_target_modules for biencoder so adapter_config.json has module names matching the standalone HF base model. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Strip lm_q. prefix from target modules in adapter_config.json so merge_lora.py can match them against the standalone base model - Fix to_hf to not add extra model. prefix for PEFT keys (lm_q. → bare) - Fix from_hf to only restore PEFT keys to lm_q (not lm_p) to avoid FSDP DTensor loading failures with shared encoder - Add apply_te_patches() to biencoder training recipe Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
|
/ok to test 1a6ee91 |
akoumpa
approved these changes
Mar 7, 2026
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.
Summary
Fixes
merge_lora.pyfailures when merging biencoder LoRA adapters into the standalone base model (nvidia/llama-nemotron-embed-1b-v2).Root cause
The biencoder wraps the base model as
self.lm_q/self.lm_p, so adapter checkpoint keys andadapter_config.jsontarget modules get prefixed withlm_q.. Whenmerge_lora.pyloads the adapter against the standalone base model, module names likelm_q.layers.0.self_attn.q_projdon't exist — the base model haslayers.0.self_attn.q_proj.Changes
addons.py–_extract_target_modules: Striplm_q.prefix from target module names when savingadapter_config.jsonfor biencoder models, somerge_lora.pycan match them against the base modelbidirectional.py–BiencoderStateDictAdapter.to_hf: PEFT keysbase_model.model.lm_q.Xare now mapped tobase_model.model.X(stripslm_q.without addingmodel.prefix, sinceLlamaBidirectionalModelextendsLlamaModelnotLlamaForCausalLM)bidirectional.py–BiencoderStateDictAdapter.from_hf: PEFT keys are only restored tolm_q(not fanned out tolm_p), which fixes FSDP DTensor loading failures with the shared encodertrain_biencoder.py: Addapply_te_patches()calllm_q.stripping, and nolm_pfanoutTest plan
test_state_dict_adapter.py,test_addons.py)merge_lora.pymerge🤖 Generated with Claude Code