Skip to content

[bugfix] fix GDN sequence parallel and CP-aware cu_seqlens resolution - #162

Merged
hjh0119 merged 1 commit into
modelscope:mainfrom
sunyi0505:main
Aug 10, 2026
Merged

[bugfix] fix GDN sequence parallel and CP-aware cu_seqlens resolution#162
hjh0119 merged 1 commit into
modelscope:mainfrom
sunyi0505:main

Conversation

@sunyi0505

Copy link
Copy Markdown
Contributor

Summary

Fix modelscope/ms-swift#9791

  • Add Sequence Parallel (SP) support in GatedDeltaNet.forward: when SP is enabled, gather the complete sequence first, temporarily disable the sequence_parallel flag of linear layers, and scatter the output after computation completes.
  • Add _resolve_cu_seqlens to validate and normalize cu_seqlens under CP + packed sequence (THD) scenarios (supporting padded offsets, leading‑zero padding, and CP alignment checks for sequence lengths); fall back to the original cu_seqlens_q when parsing fails.
  • Add _set_linear_sequence_parallel / _restore_linear_sequence_parallel to centrally manage the SP flags of in_proj / in_proj_qkvz / in_proj_ba / out_proj, preventing behavioral conflicts between full‑sequence computation after gather and linear‑layer SP.
  • Fix the calculation logic for seq_len under SP mode: multiply seq_len by cp_size when SP is enabled; keep seq_len *= sp_size * cp_size when SP is disabled.

tensor_parallel_output_grad=False,
group=tp_group,
)
saved_linear_sp = self._set_linear_sequence_parallel(False)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider wrapping this in try/finally to make the temporary attribute mutation safe.

Comment on lines +150 to +162
if cu_seqlens.numel() > 0 and int(cu_seqlens[0].item()) != 0:
total_cu = int(cu_seqlens[-1].item())
if total_cu == total_seq_len:
cu_seqlens = torch.cat([
torch.zeros(1, dtype=cu_seqlens.dtype, device=cu_seqlens.device),
cu_seqlens,
])
elif total_cu - int(cu_seqlens[0].item()) == total_seq_len:
cu_seqlens = cu_seqlens - cu_seqlens[0]
seq_lengths = cu_seqlens[1:] - cu_seqlens[:-1]
if not bool(cu_seqlens[-1].eq(total_seq_len) & (seq_lengths % cp_size).eq(0).all()):
return None
return cu_seqlens

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int() / bool() here force a device sync, once per GDN layer per forward. These
comparisons work directly on tensors, e.g. cu_seqlens[0] != 0

@sunyi0505
sunyi0505 requested a review from hjh0119 August 10, 2026 11:22
@hjh0119

hjh0119 commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

thanks!

@hjh0119
hjh0119 merged commit 86250c3 into modelscope:main Aug 10, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qwen3.5-27B SFT report error found NaN in local grad norm for bucket #0 in backward pass before data-parallel communication collective'

3 participants