Skip to content
19 changes: 15 additions & 4 deletions megatron/core/fusions/fused_mla_yarn_rope_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,28 @@

@triton.jit
def _get_thd_token_idx(cu_seqlens, pid_m, seq_num, cp_rank, cp_size):
token_idx = -1
this_seq_len = 0
# Cast ``pid_m`` and ``cu_seqlens`` loads to a single shared dtype so
# the loop-body reassignments don't surface as
# "initial value is int32 but redefined as int64" in newer Triton
# versions (which promote ``// Python_int`` to int64).
pid_m = pid_m.to(tl.int64)
token_idx = tl.full((), -1, dtype=tl.int64)
this_seq_len = tl.full((), 0, dtype=tl.int64)
seq_idx = 0
last_cum_seqlen = tl.load(cu_seqlens) // cp_size
last_cum_seqlen = tl.load(cu_seqlens).to(tl.int64) // cp_size
while seq_idx < seq_num:
cur_cum_seqlen = tl.load(cu_seqlens + seq_idx + 1) // cp_size
cur_cum_seqlen = tl.load(cu_seqlens + seq_idx + 1).to(tl.int64) // cp_size
if token_idx == -1 and cur_cum_seqlen > pid_m:
token_idx = pid_m - last_cum_seqlen
this_seq_len = cur_cum_seqlen - last_cum_seqlen
last_cum_seqlen = cur_cum_seqlen
seq_idx += 1
# Padding tokens beyond cu_seqlens[-1] (from THD CUDA-graph padding)
# never match any sequence, leaving token_idx == -1. Clamp to 0 so
# the cos/sin table loads stay in-bounds; the wrong RoPE result is
# harmless because padding positions are excluded by loss_mask.
if token_idx == -1:
token_idx = tl.full((), 0, dtype=tl.int64)
if cp_size > 1:
if token_idx < this_seq_len // 2:
token_idx = token_idx + cp_rank * this_seq_len // 2
Expand Down
1 change: 1 addition & 0 deletions megatron/core/models/gpt/gpt_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ def _postprocess(
sequence_len_offset=sequence_len_offset,
padding_mask=padding_mask,
embedding=self.embedding,
loss_mask=loss_mask,
**(extra_block_kwargs or {}),
)

Expand Down
2 changes: 2 additions & 0 deletions megatron/core/models/hybrid/hybrid_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ def forward(
rotary_pos_emb=rotary_pos_emb,
packed_seq_params=packed_seq_params,
embedding=self.embedding,
padding_mask=padding_mask,
loss_mask=loss_mask,
)

if not self.post_process:
Expand Down
Loading
Loading