|
10 | 10 | import torch |
11 | 11 | import torch.nn as nn |
12 | 12 | import torch.nn.functional as F |
| 13 | +from torch.distributed.tensor import DTensor |
| 14 | +from torch.distributed.tensor.experimental import local_map |
13 | 15 | from torch.nn.attention.flex_attention import BlockMask, create_block_mask |
14 | 16 |
|
15 | 17 | from torchtitan.models.common import Linear |
16 | 18 | from torchtitan.models.common.attention import FlexAttention |
17 | 19 | from torchtitan.models.common.nn_modules import GELU, LayerNorm |
18 | | -from torchtitan.models.common.rope import CosSinRoPE |
| 20 | +from torchtitan.models.common.rope import _maybe_wrap_positions, CosSinRoPE |
19 | 21 | from torchtitan.protocols.module import Module, ModuleDict |
20 | 22 |
|
21 | 23 | _compiled_create_block_mask = torch.compile(create_block_mask) |
@@ -88,12 +90,24 @@ def _compute_learned_pos_embeds( |
88 | 90 | ) |
89 | 91 |
|
90 | 92 | for (h, w), indices in hw_to_indices.items(): |
91 | | - pos_hw = F.interpolate( |
92 | | - pos_grid, |
93 | | - size=[h, w], |
94 | | - mode="bilinear", |
95 | | - align_corners=True, |
96 | | - ) |
| 93 | + if isinstance(pos_grid, DTensor): |
| 94 | + pos_hw = local_map( |
| 95 | + F.interpolate, |
| 96 | + out_placements=(pos_grid.placements,), |
| 97 | + )( |
| 98 | + pos_grid, |
| 99 | + size=[h, w], |
| 100 | + mode="bilinear", |
| 101 | + align_corners=True, |
| 102 | + ) |
| 103 | + else: |
| 104 | + pos_hw = F.interpolate( |
| 105 | + pos_grid, |
| 106 | + size=[h, w], |
| 107 | + mode="bilinear", |
| 108 | + align_corners=True, |
| 109 | + ) |
| 110 | + |
97 | 111 | # (1, dim, h, w) → (h*w, dim) |
98 | 112 | pos_hw = pos_hw.squeeze(0).permute(1, 2, 0).reshape(-1, dim).to(dtype) |
99 | 113 |
|
@@ -251,6 +265,7 @@ def forward(self, seqlen: int) -> torch.Tensor: |
251 | 265 | seq = torch.arange( |
252 | 266 | seqlen, device=self.inv_freq.device, dtype=self.inv_freq.dtype |
253 | 267 | ) |
| 268 | + seq = _maybe_wrap_positions(seq, self.inv_freq) |
254 | 269 | return torch.outer(seq, self.inv_freq) |
255 | 270 |
|
256 | 271 |
|
@@ -485,13 +500,25 @@ def compute_position_embeddings( |
485 | 500 | self.config.dim, |
486 | 501 | ) |
487 | 502 |
|
488 | | - rope_cache = _compute_2d_rope_cache( |
489 | | - self._cached_freq_table, |
490 | | - grid_thw, |
491 | | - max_num_patch, |
492 | | - self.spatial_merge_size, |
493 | | - head_dim, |
494 | | - ) |
| 503 | + if isinstance(self._cached_freq_table, DTensor): |
| 504 | + rope_cache = local_map( |
| 505 | + _compute_2d_rope_cache, |
| 506 | + out_placements=(self._cached_freq_table.placements,), |
| 507 | + )( |
| 508 | + self._cached_freq_table, |
| 509 | + grid_thw, |
| 510 | + max_num_patch, |
| 511 | + self.spatial_merge_size, |
| 512 | + head_dim, |
| 513 | + ) |
| 514 | + else: |
| 515 | + rope_cache = _compute_2d_rope_cache( |
| 516 | + self._cached_freq_table, |
| 517 | + grid_thw, |
| 518 | + max_num_patch, |
| 519 | + self.spatial_merge_size, |
| 520 | + head_dim, |
| 521 | + ) |
495 | 522 |
|
496 | 523 | return learned_pos, rope_cache |
497 | 524 |
|
|
0 commit comments