refactor transformer to avoid var mutation - #2
Merged
yiyixuxu merged 2 commits intoAug 12, 2026
Conversation
sayakpaul
commented
Aug 12, 2026
Comment on lines
-651
to
-660
| self.patch_size = patch_size | ||
| self.text_len = text_len | ||
| self.in_dim = in_dim | ||
| self.dim = dim | ||
| self.ffn_dim = ffn_dim | ||
| self.freq_dim = freq_dim | ||
| self.text_dim = text_dim | ||
| self.out_dim = out_dim | ||
| self.num_heads = num_heads | ||
| self.num_layers = num_layers |
Author
There was a problem hiding this comment.
Many of these can be accessed through self.config
sayakpaul
commented
Aug 12, 2026
| self.block_mask_grid_sizes = {} | ||
| self.rope_freqs_cache = {} | ||
|
|
||
| def _rope_freqs(self, offsets: tuple[int, int, int], device: torch.device) -> torch.Tensor: |
Author
There was a problem hiding this comment.
There are two places where we compute self.freqs. So, this is just a helper to circumvent that. Happy to fold them in the actual caller sites.
yiyixuxu
reviewed
Aug 12, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
To stop variable mutation from the forward as much as possible? I think it's a good practice. I have verified this with the example provided in huggingface#14413 to ensure that outputs bit-wise match.
Use the script below to confirm that the changes here don't cause any regressions:
Unfold