Skip to content

Commit ae25616

Browse files
committed
Update
[ghstack-poisoned]
1 parent 8fe4aff commit ae25616

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

torchtitan/models/qwen3_5/sharding.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ def _set_vision_encoder_sharding(ve_cfg: "Qwen35VisionEncoder.Config") -> None:
205205
ve_cfg.sharding_config = ShardingConfig(
206206
state_shardings={"pos_embed": dense_param_placement(tp=spmd.R)},
207207
)
208+
ve_cfg.rotary_pos_emb.sharding_config = ShardingConfig(
209+
state_shardings={"inv_freq": dense_param_placement(tp=spmd.I)},
210+
out_src_shardings=dense_param_placement(tp=spmd.I),
211+
)
208212

209213
# patch_embed receives plain pixel_values — wrap as DTensor(Replicate)
210214
ve_cfg.patch_embed_proj.sharding_config = ShardingConfig(

torchtitan/models/qwen3_5/vision_encoder.py

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
import torch
1111
import torch.nn as nn
1212
import torch.nn.functional as F
13+
from torch.distributed.tensor import DTensor
14+
from torch.distributed.tensor.experimental import local_map
1315
from torch.nn.attention.flex_attention import BlockMask, create_block_mask
1416

1517
from torchtitan.models.common import Linear
1618
from torchtitan.models.common.attention import FlexAttention
1719
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
1921
from torchtitan.protocols.module import Module, ModuleDict
2022

2123
_compiled_create_block_mask = torch.compile(create_block_mask)
@@ -88,12 +90,24 @@ def _compute_learned_pos_embeds(
8890
)
8991

9092
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+
97111
# (1, dim, h, w) → (h*w, dim)
98112
pos_hw = pos_hw.squeeze(0).permute(1, 2, 0).reshape(-1, dim).to(dtype)
99113

@@ -251,6 +265,7 @@ def forward(self, seqlen: int) -> torch.Tensor:
251265
seq = torch.arange(
252266
seqlen, device=self.inv_freq.device, dtype=self.inv_freq.dtype
253267
)
268+
seq = _maybe_wrap_positions(seq, self.inv_freq)
254269
return torch.outer(seq, self.inv_freq)
255270

256271

@@ -485,13 +500,25 @@ def compute_position_embeddings(
485500
self.config.dim,
486501
)
487502

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+
)
495522

496523
return learned_pos, rope_cache
497524

0 commit comments

Comments
 (0)