-
Notifications
You must be signed in to change notification settings - Fork 49
2D Rope into develop #1540
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: develop
Are you sure you want to change the base?
2D Rope into develop #1540
Conversation
- Add copyright attribution for rotate_half() and apply_rotary_pos_emb() functions - Rename apply_rotary_pos_emb_2d() to rotary_pos_emb_2d() for consistency - Rename config parameter use_2D_rope to rope_2D for better extensibility when supporting different RoPE variants in the future
clessig
left a comment
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.
We can simplify the code if we change the syntax of the forward fct of the attention blocks slightly. This should probably be done consistently for all heads, even those without 2D rope.
| ) | ||
|
|
||
| return iter_start, iter_end | ||
| return iter_start, iter_end |
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.
Can we revert this
| def forward(self, tokens, coords=None, use_reentrant=False): | ||
| for block in self.ae_aggregation_blocks: | ||
| tokens = checkpoint(block, tokens, use_reentrant=use_reentrant) | ||
| if isinstance(block, MultiSelfAttentionHead | MultiSelfAttentionHeadLocal): |
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.
We could avoid the branches if we would follow the approach in the MLP:
| def forward(self, *args): |
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.
Or some compromise with *args and **kwargs
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.
Personal preference is the if statement
| def forward(self, tokens, coords=None, use_reentrant=False): | ||
| for block in self.ae_global_blocks: | ||
| tokens = checkpoint(block, tokens, use_reentrant=use_reentrant) | ||
| if isinstance(block, MultiSelfAttentionHead | MultiSelfAttentionHeadLocal): |
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.
Same as above: if-statement is not necessary
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.
but cleaner to read imho
| for _b_idx, block in enumerate(self.fe_blocks): | ||
| if isinstance(block, torch.nn.modules.normalization.LayerNorm): | ||
| tokens = block(tokens) | ||
| elif isinstance(block, MultiSelfAttentionHead | MultiSelfAttentionHeadLocal): |
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.
Same as above
| ) | ||
| self.pe_global = torch.nn.Parameter(pe, requires_grad=False) | ||
|
|
||
| ### ROPE COORDS ### |
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.
standard comment:
# rope coords
| ks = self.lnorm_k(self.proj_heads_k(x).reshape(s)).to(self.dtype) | ||
| vs = self.proj_heads_v(x).reshape(s).to(self.dtype) | ||
|
|
||
| if coords is not None: |
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.
MultiSelfAttenionHead is a vanilla self attention block. The 2D rope positional encoding makes strong assumption of the structure of the qs and ks. At the very least, we need some documentation on what the option means and when it makes sense. Not sure if there's another good way to ensure proper use of this option (one could have a separate class but then one should make sure there's no code duplication).
sophie-xhonneux
left a comment
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.
Did a first review of the code and left some comments that should be addressed.
Will check the correctness next
|
|
||
| if self.rope_2D: | ||
| # Precompute per-cell center coordinates (lat, lon in radians) for 2D RoPE. | ||
| # Shape: (num_healpix_cells, ae_local_num_queries, 2) |
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.
Why isn't the shape (num_healpix_cells, 2)?
| ### ROPE COORDS ### | ||
| self.rope_2D = cf.get("rope_2D", False) | ||
| if self.rope_2D: | ||
| self.rope_coords = torch.nn.Parameter( |
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 this should be a registered buffer, not parameters
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.
|
|
||
|
|
||
| #################################################################################################### | ||
| def rotary_embedding_2d(coords, dim, base=10000.0): |
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.
There should be an option to learn the frequencies, this proposed in the original RoPE (https://arxiv.org/pdf/2403.13298v1 section 3.2 at the end) or we should have a reference explaining why not
| k: Key tensor. | ||
| cos: Cosine embedding tensor. | ||
| sin: Sine embedding tensor. | ||
| position_ids: Deprecated and unused; present for API compatibility. |
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.
Let's delete, we have no requirement to be backwards compatible
| return torch.cat((-x2, x1), dim=-1) | ||
|
|
||
|
|
||
| def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): |
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.
This looks like 1D-rope to me?
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.
Can we please label the function as such?
| """ | ||
|
|
||
| if coords.shape[-1] != 2: | ||
| raise ValueError(f"coords last dimension must be 2 (lat, lon); got {coords.shape[-1]}") |
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.
can we just make these asserts? it is cleaner to read
| self.flex_attention = torch.compile(flex_attention, dynamic=False) | ||
|
|
||
| def forward(self, x, ada_ln_aux=None): | ||
| def forward(self, x, coords=None, ada_ln_aux=None): |
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.
From a code perspective I prefer if we decide at init time if this attention module has 2D rope, because a) we may wish to learn the frequencies theta_t and b) we should not use an attention module trained with 2D-rope without it (or at least be very intentional about it)
Description
This is @csjfwang 's implementation of 2D rope, coming from this orignal PR: #1445
Issue Number
Closes #1445
Closes #1109
Checklist before asking for review
./scripts/actions.sh lint./scripts/actions.sh unit-test./scripts/actions.sh integration-testlaunch-slurm.py --time 60