-
Notifications
You must be signed in to change notification settings - Fork 573
[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
base: gh/fegin/7/base
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,8 +92,7 @@ def reshape_for_broadcast(freqs_cis: torch.Tensor, x: torch.Tensor) -> torch.Ten | |
| This function reshapes the frequency tensor to have the same shape as the target tensor 'x' | ||
| for the purpose of broadcasting the frequency tensor during element-wise operations. | ||
|
|
||
| The input freqs_cis tensor is assumed to be of shape (max_seqlen, dim), | ||
| and the first seqlen elements will be sliced, but dim must match x. | ||
| The input freqs_cis tensor is assumed to be of shape (batch_size, seqlen, dim). | ||
|
|
||
| Args: | ||
| freqs_cis (torch.Tensor): Frequency tensor to be reshaped. | ||
|
|
@@ -104,10 +103,10 @@ def reshape_for_broadcast(freqs_cis: torch.Tensor, x: torch.Tensor) -> torch.Ten | |
| """ | ||
| ndim = x.ndim | ||
| assert ndim > 1 | ||
| batch_size = x.shape[0] | ||
| seqlen = x.shape[1] | ||
| freqs_cis = freqs_cis[0:seqlen] | ||
| assert freqs_cis.shape == (seqlen, x.shape[-1]) | ||
| shape = [d if i == 1 or i == ndim - 1 else 1 for i, d in enumerate(x.shape)] | ||
| assert freqs_cis.shape == (batch_size, seqlen, x.shape[-1]) | ||
| shape = [d if i in (0, 1, ndim - 1) else 1 for i, d in enumerate(x.shape)] | ||
| return freqs_cis.view(*shape) | ||
|
|
||
|
|
||
|
|
@@ -474,9 +473,18 @@ def get_attention_masks( | |
| and_masks(*mask_mods), B, None, input_batch.shape[1], input_batch.shape[1] | ||
| ) | ||
|
|
||
| def get_order_sensitive_buffers( | ||
| self, | ||
| batch_size: int, | ||
| seq_len: int, | ||
| ) -> tuple[dict[str, torch.Tensor], dict[str, int]]: | ||
| freqs_cis = self.freqs_cis[:seq_len].repeat(batch_size, 1, 1) | ||
|
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": freqs_cis}, {"freqs_cis": 1}) | ||
|
|
||
| def forward( | ||
| self, | ||
| tokens: torch.Tensor, | ||
| freqs_cis: torch.Tensor, | ||
| attention_masks: AttentionMasksType | None = None, | ||
| input_batch: torch.Tensor | None = None, | ||
| ): | ||
|
|
@@ -501,7 +509,7 @@ def forward( | |
| h = self.tok_embeddings(tokens) if self.tok_embeddings else tokens | ||
|
|
||
| for layer in self.layers.values(): | ||
| h = layer(h, self.freqs_cis, attention_masks=attention_masks) | ||
| h = layer(h, freqs_cis, attention_masks=attention_masks) | ||
|
|
||
| h = self.norm(h) if self.norm else h | ||
| output = self.output(h) if self.output else h | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,3 +70,10 @@ def get_attention_masks( | |
| raise NotImplementedError( | ||
| "This model does not support attention masking/Flex Attention." | ||
| ) | ||
|
|
||
| def get_order_sensitive_buffers( | ||
|
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. Naming is a bit vague. I think here we are only targeting "sequence dim" order-sensitive buffers, not the batch dim. |
||
| self, | ||
| batch_size: int, | ||
| seq_len: int, | ||
| ) -> tuple[dict[str, torch.Tensor], dict[str, int]]: | ||
| return ({}, {}) | ||
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.