Skip to content

weight tying fix for qwen3 - #2253

Merged
tianyu-l merged 5 commits into
pytorch:mainfrom
francesco-bertolotti:f14-weight-tying-fix
Jan 26, 2026
Merged

tianyu-l merged 5 commits into
pytorch:mainfrom
francesco-bertolotti:f14-weight-tying-fix

Conversation

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator

PR for #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

Screenshot 2026-01-19 at 11 51 15

Let me know if something like this could work. If so I could check also the llama architecture for the same problem.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jan 19, 2026
Comment thread torchtitan/models/qwen3/model/model.py Outdated
@@ -451,6 +451,7 @@ def __init__(self, model_args: Qwen3ModelArgs):
self.n_layers = model_args.n_layers
self.eos_id = model_args.eos_id

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.

this is no longer used, please help remove

Comment thread torchtitan/models/qwen3/model/model.py Outdated

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)

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.

@francesco-bertolotti francesco-bertolotti Jan 19, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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

Comment thread torchtitan/models/qwen3/model/model.py Outdated
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)

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.

This would break PP-compatibility so please revert the change, although PP is not enabled for Qwen3 today.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I see, good point.

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator Author

@tianyu-l , your suggestion seems to work well. Here's the new plots
Screenshot 2026-01-19 at 14 28 29

@tianyu-l tianyu-l linked an issue Jan 19, 2026 that may be closed by this pull request
Comment thread torchtitan/models/qwen3/model/model.py Outdated
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

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.

given the comment, should we put the code block into else branch of if self.enable_weight_tying:?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yep, seems correct to me

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator Author

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

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.

Please also do this in __init__ so that if people load weights without calling init_weights, weight-tying is still effective.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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?

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.

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

My bad, I see what you mean.

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator Author

I’ve also added the weight tying in __init__. I think the only case where tying might fail is when the model is initialized on the meta device and init_weights is not called. However, that would already be problematic on its own, since the model still needs to be initialized after materialization on the target device.

@tianyu-l

Copy link
Copy Markdown
Contributor

please fix linting

Comment thread torchtitan/models/qwen3/model/model.py Resolved
@tianyu-l
tianyu-l merged commit d12ff2f into pytorch:main Jan 26, 2026
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
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]>
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weight tying not working

2 participants