-
Notifications
You must be signed in to change notification settings - Fork 598
[RFC] Lift freqs_cis as an input of models #1797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8a42c0c
Update
fegin d6d06d5
Update
fegin 705eebd
Update
fegin e9c4cf7
Update
fegin d60c3d6
Update
fegin 827ba00
Update
fegin f0dfa69
Update
fegin b05f40d
Update
fegin 5b813a2
Update
fegin 321fe63
Update
fegin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update
[ghstack-poisoned]
- Loading branch information
commit 827ba00cc677e8795d47040f58c0acf4364a07cd
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -477,9 +477,9 @@ def get_order_sensitive_buffers( | |
| self, | ||
| batch_size: int, | ||
| seq_len: int, | ||
| ) -> tuple[tuple[torch.Tensor, ...], tuple[int, ...]]: | ||
| ) -> tuple[dict[str, torch.Tensor], dict[str, int]]: | ||
| freqs_cis = self.freqs_cis[:seq_len].repeat(batch_size, 1, 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the (maybe unlikely) case when both seq len and batch size are large, this code is creating a big buffer (which may not peak the memory anyway?). |
||
| return ((freqs_cis,), (1,)) | ||
| return ({"freqs_cis": freqs_cis}, {"freqs_cis": 1}) | ||
|
|
||
| def forward( | ||
| self, | ||
|
|
||
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
Oops, something went wrong.
You are viewing a condensed version of this merge commit. You can view the full changes here.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what's the benefit of keeping
self.freqs_cis.If seq len changes from iteration to iteration (e.g. in forge), it might be good to keep a central
self.freqs_cisinstead of computing it each iteration. The other benefit is that we may not want torchtitan model definition to deviate from "original" / "conventional" model definitions too much.On the other hand, the dependency sounds indirect and error-prone:
self.freqs_cisin model codefreqs_cis, which technically is outside the modelfreqs_cisinto modelWould like to hear your thoughts.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-computation is the main reason why I decided to keep
self.freqs_cis. I agree it's a bit awkward.One alternative is to sill keep
self.freqs_cisbut set it as an optional field (self.freqs_cis: torch.Tensor | None) for bookkeeping only. And we only initialize it in this function. So the creation logic flow (precompute and slicing) is mainly in this function. The model code still provides precompute function. So this way we do not change the code structure too much while keeping the logic together. Not a perfect solution though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine to keep the current way for now, as it sounds more lightweight change, and as I mentioned downstream application (e.g. forge, and simple generation) may change
seq_lenfrom iteration to iteration, where we can avoid recomputation this way.