Repository navigation
weight tying fix for qwen3 - #2253
Conversation
| @@ -451,6 +451,7 @@ def __init__(self, model_args: Qwen3ModelArgs): | |||
| self.n_layers = model_args.n_layers | |||
| self.eos_id = model_args.eos_id | |||
There was a problem hiding this comment.
this is no longer used, please help remove
|
|
||
| self.output = nn.Linear(model_args.dim, model_args.vocab_size, bias=False) | ||
| if self.enable_weight_tying: | ||
| self.output = lambda x: F.linear(x, weight=self.tok_embeddings.weight) |
There was a problem hiding this comment.
I'm not sure if it plays well with TP, as in https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/qwen3/infra/parallelize.py#L206
If not, I'd prefer we try #2252 (comment)
There was a problem hiding this comment.
Yep you are correct, it does not work even with fsdp.
I tried
class TiedOutputHead(torch.nn.Module):
def __init__(self, tok_embeddings):
super().__init__()
self.tok_embeddings = tok_embeddings
def forward(self, x):
return F.linear(x, self.tok_embeddings.weight)and seems to work well.
Regardless, your proposal seems better. I am going with your suggestion
| h = self.norm(h) if self.norm else h | ||
| # pyrefly: ignore[not-callable, invalid-argument] | ||
| output = self.output(h) if self.output else h | ||
| output = self.output(h) |
There was a problem hiding this comment.
This would break PP-compatibility so please revert the change, although PP is not enabled for Qwen3 today.
There was a problem hiding this comment.
I see, good point.
80d1ddc to
bb0282f
Compare
|
@tianyu-l , your suggestion seems to work well. Here's the new plots |
| assert self.tok_embeddings is not None and self.output is not None | ||
| self.output.weight = self.tok_embeddings.weight | ||
|
|
||
| # If weight tying is enabled, we don't need to initialize the output layer |
There was a problem hiding this comment.
given the comment, should we put the code block into else branch of if self.enable_weight_tying:?
There was a problem hiding this comment.
yep, seems correct to me
|
Hi @tianyu-l, I’m not very familiar with pyrefly, but I noticed that the linting action failed. From what I can tell, the failure doesn’t appear to be related to the file I modified. Please let me know if there’s any action required on my side. Thanks! |
| ) | ||
| if self.enable_weight_tying: | ||
| assert self.tok_embeddings is not None and self.output is not None | ||
| self.output.weight = self.tok_embeddings.weight |
There was a problem hiding this comment.
Please also do this in __init__ so that if people load weights without calling init_weights, weight-tying is still effective.
There was a problem hiding this comment.
I’ll try that; however, it’s possible we’ll run into the original issue. If the model is initialized on the meta device, then self.output.weight = self.tok_embeddings.weight may not take effect once the final model is materialized.
Either way, I’ll give it a try tomorrow.
I can also do something like #2253 (comment) if this does not work?
There was a problem hiding this comment.
I'm saying we should do them in BOTH places. As long as we don't delete it here, I don't think it'll cause regression?
There was a problem hiding this comment.
My bad, I see what you mean.
|
I’ve also added the weight tying in |
|
please fix linting |
Co-authored-by: tianyu-l <[email protected]>
PR for pytorch#2252 The PR addresses the weight tying issue by using a lambda function. Whenever `enable_weight_tying` is enabled instead of using a `nn.Linear` layer, we can use the functional interface `F.linear` using `tok_embedding` as weights. I have run a small model to verify the implementation. `pre-varlen-note-qwen3-600M` refers to the model pre-PR without weight tying `pre-varlen-te-qwen3-600M` refers to the model pre-PR with weight tying `varlen-note-qwen3-600M` refers to the model post-PR without weight tying `varlen-te-qwen3-600M` refers to the model post-PR with weight tying <img width="1085" height="464" alt="Screenshot 2026-01-19 at 11 51 15" src="https://github.com/user-attachments/assets/ecbaa311-35b7-466e-9486-f82f46e476fa" /> Let me know if something like this could work. If so I could check also the llama architecture for the same problem. --------- Co-authored-by: tianyu-l <[email protected]>
PR for pytorch#2252 The PR addresses the weight tying issue by using a lambda function. Whenever `enable_weight_tying` is enabled instead of using a `nn.Linear` layer, we can use the functional interface `F.linear` using `tok_embedding` as weights. I have run a small model to verify the implementation. `pre-varlen-note-qwen3-600M` refers to the model pre-PR without weight tying `pre-varlen-te-qwen3-600M` refers to the model pre-PR with weight tying `varlen-note-qwen3-600M` refers to the model post-PR without weight tying `varlen-te-qwen3-600M` refers to the model post-PR with weight tying <img width="1085" height="464" alt="Screenshot 2026-01-19 at 11 51 15" src="https://github.com/user-attachments/assets/ecbaa311-35b7-466e-9486-f82f46e476fa" /> Let me know if something like this could work. If so I could check also the llama architecture for the same problem. --------- Co-authored-by: tianyu-l <[email protected]>

PR for #2252
The PR addresses the weight tying issue by using a lambda function.
Whenever
enable_weight_tyingis enabled instead of using ann.Linearlayer, we can use the functional interfaceF.linearusingtok_embeddingas weights.I have run a small model to verify the implementation.
pre-varlen-note-qwen3-600Mrefers to the model pre-PR without weight tyingpre-varlen-te-qwen3-600Mrefers to the model pre-PR with weight tyingvarlen-note-qwen3-600Mrefers to the model post-PR without weight tyingvarlen-te-qwen3-600Mrefers to the model post-PR with weight tyingLet me know if something like this could work. If so I could check also the llama architecture for the same problem.