Enable "apply_lora_to_output" in models with tied embedding
Many families of models with small models (<=3B parameters) have tied embeddings, meaning that the output layer projection is the same as the input tok_embeddings. Examples are gemma, qwen and llama 3.2.
These models currently don't support "apply_lora_to_output". This happens because in the past we used to pass as the output_proj a lambda function, e.g. lambda x: x @ tok_embeddings.weight.
Recently, we changed it, and started passing the TiedLinear module.. We need the TiedLinear so it can work well with FSDP and other techniques.
This task is to enable LoRA on top of this TiedLinear, like we do it for nn.Linear in other models that do not have tied embeddings, e.g. in llama 3.1.
After adding this feature, the configs of models from llama 3.2, qwen and gemma have to be updated to include the flag.
Hi, is this still a desired contribution on current main? I'd like to add a LoRA-compatible tied output projection that reuses tok_embeddings.weight without registering a duplicate base parameter, wire it behind apply_lora_to_output for the tied-embedding Llama 3.2, Qwen, and Gemma builders, and update their configs. I would add CPU tests for disabled-adapter parity, preserved weight tying, adapter/state-dict behavior, and representative builder coverage, then run the relevant LoRA/model test suites. The main risk is preserving FSDP and checkpoint/adapter-merge semantics; I would keep quantized tied-output QLoRA out of scope unless you prefer otherwise. Would this scope be acceptable?
When adding a LoRATiedLinear module around tok_embeddings: nn.Embedding for apply_lora_to_output=True, there are two FSDP and checkpointing subtleties to watch out for:
-
Avoiding duplicate parameter registration in
state_dictand FSDP flat-param / DTensor sharding: InTiedLinear, holding a direct submodule referenceself.tied_module = tied_moduleregistersoutput.tied_module.weightas a shared parameter alias oftok_embeddings.weight. Under FSDP2 (fully_shard) and PEFT/HF checkpoint export, registering the base weight twice or mutatingtok_embeddings.weightduring adapter merge breaks weight-tying invariants (tok_embeddings.weight is output.weight) unlessLoRATiedLinearonly registerslora_aandlora_bas owned parameters and readsself.tied_module.weightinforward:def forward(self, x: torch.Tensor) -> torch.Tensor: out = F.linear(x, self.tied_module.weight) if self.disabled: return out return out + (self.scaling * self.lora_b(self.lora_a(self.dropout(x)))) -
Preventing tied-weight corruption when merging
outputLoRA into base weights: If a user merges the LoRA adapter into the base checkpoint (W_merged = W_base + (alpha / r) * B @ A), foldingoutput.lora_b @ output.lora_adirectly intotok_embeddings.weightin-place silently alters the input token embeddings unlesstie_word_embeddingsis untied on export orlm_head.weightis materialized as a separate tensor.
For auditing output / lm_head vs attention/MLP LoRA layers in exported .safetensors checkpoints (or merging/compressing lora_a and lora_b offline without touching tied base weights), we also built loraspectra, a zero-dependency Rust CLI that computes exact r x r SVD spectral rank (R_EFF, R_STABLE, K_99%) and merges or prunes adapters directly on .safetensors files.
Hi, I'd like to take this one. Plan: add a LoRATiedLinear module in torchtune/modules/peft/lora.py that wraps the tied embedding weight with a LoRA branch but only registers lora_a and lora_b as owned parameters, so the base weight stays shared and FSDP sharding is untouched. Then wire apply_lora_to_output through lora_llama3_2, lora_qwen2, lora_qwen3, lora_gemma and lora_gemma2, thread the flag through the size builders, and add it to the tied-size LoRA configs. Two things I am deliberately leaving out: DoRA on the tied output, and merging the output adapter into the checkpoint, since folding it into the shared embedding weight would corrupt the input embeddings. I will add CPU tests for disabled-adapter parity, preserved weight tying, adapter params, and the merge guard. Let me know if that scope looks right before I open the PR.