Skip to content

[Do not review] Enable spmd_types backend for Qwen3.5 dense - #3973

Closed
Jeremalloch wants to merge 1 commit into
pytorch:mainfrom
Jeremalloch:jm/qwen35-spmd-types
Closed

Jeremalloch wants to merge 1 commit into
pytorch:mainfrom
Jeremalloch:jm/qwen35-spmd-types

Conversation

@Jeremalloch

@Jeremalloch Jeremalloch commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

This was my heavily Claude aided attempt at adding spmd_types to Qwen3.5, prompted by @tianyu-l 's comment on #3801

Opened as a reference / proof of concept alongside #3895.

Claude's PR Description

Wire the dense Qwen3.5 model for the spmd_types backend, which CI previously blocked (#3901: not wired for the spmd_types mesh axes).

  • parallelize.py: resolve the FSDP mesh via resolve_fsdp_mesh / resolve_sparse_fsdp_mesh under spmd_types (there is no flattened "fsdp" axis; FSDP shards the multi-axis dense storage mesh along DataParallelMeshDims), pass dp_mesh_dims through to the decoder and vision-encoder fully_shard calls, and run model.parallelize() even for pure-FSDP topologies since the SPMD parameter annotations are what fully_shard translates into DTensor params.
  • sharding.py: annotate vision params without explicit state shardings as Replicate (FLUX's annotate_dp_cp_params_as_r pattern); add the missing out_src/in_src shardings that spmd_types requires to be explicit; redistribute the first decoder block's input R -> SP at the module boundary (the layer-0 residual previously relied on implicit DTensor redistribution, which spmd_types local tensors cannot do); vision rotary inv_freq R instead of I (same Replicate DTensor placement, but I cannot mix with R under typechecking); scaled-bias rowwise config for the vision rowwise-with-bias linears.
  • init.py: vision attn.proj, mlp.fc2, and merger.fc2 become ScaledBiasRowwiseLinear (gpt_oss pattern) so the per-rank bias/tp contribution sums correctly through the TP all-reduce; addmm cannot type a Partial matmul plus Replicate bias.
  • model.py: run the depthwise deltanet conv with local groups under spmd_types (module groups is the global channel count); local-region head-split views with boundary re-typing (SeparateQKVLinear pattern); register the FLA kernel autograd Functions as local-only for typechecking; annotate multimodal extras (pixel values, grid dims, mrope positions) that the trainer's input annotation does not cover; keep vision position/scatter index bookkeeping out of typechecking.
  • vision_encoder.py: local-region head splits, position-embedding and block-mask construction outside typechecking with boundary re-typing.
  • rope.py: build the MRoPE interleaved cache in a local region and re-type at the boundary.

Original (pre-rebase) validation on 4x H100 (dp_shard=2, tp=2): spmd_types runs qwen35_debugmodel, spmd_types + --debug.spmd_typechecking runs with AC disabled, and seeded deterministic loss/grad_norm stays within float noise of the default backend. Default-backend numerics compared against unmodified main with the same seed. (Superseded by the post-rebase validation below, which also covers MoE, MoE+PP, and the varlen path; lifting the qwen3.5 CI spmd_types exclusion remains a follow-up.)

Update (2026-07-27): rebased onto post-#3801 main

Rebased onto current main (b3a13eed) now that #3801 (Qwen3.5 varlen attention) merged; still a single commit (47ab085). Reconciliation notes:

  • Qwen 3.5 Varlen Attention #3801 renamed Qwen3.5 forward params and sharding-map keys to shape-suffix names; the branch's added DeltaNet kernel in_src_shardings and the layer-0 R -> SP boundary config now use the new keys (xq_BLNK/xk_BLNK/xv_BLNV/g_BLN/beta_BLN, x_BLD).
  • The local-region head-split views in GatedDeltaNet.forward now use Qwen 3.5 Varlen Attention #3801's varlen-aware (kernel_B, kernel_L) shapes, so the same local_head_split covers both the dense/flex path and the flattened (1, B*L) varlen layout.
  • The spmd_types depthwise-conv local region moved into the restructured _causal_conv, next to main's local_map DTensor path.

Revalidated on 4x H100 (dp_shard=2, tp=2; torch 2.14 nightly + public spmd_types), fresh checkout of 47ab085:

run result
flex + spmd_types, 10 steps green, final loss 3.87353
flex + spmd_types + --debug.spmd_typechecking (AC none), 3 steps green
MoE (EP=4, no PP) + spmd_types, 3 steps green
MoE + PP (pp2 / dp_shard2 / tp2 / ep4, the qwen3_5_moe_fsdp+tp+ep+pp CI shape) + spmd_types, 10 steps green (8x H100, 2026-07-29)
varlen + spmd_types, seeded, 10 steps green; step-1 loss bitwise equal to the default backend (12.77439), final loss within rerun noise
default backend, branch vs unmodified main, seeded (flex and varlen) step-1 loss bitwise identical; grad_norm differs in the 4th digit from step 1 (the ScaledBiasRowwiseLinear bias/tp rounding, deterministic across reruns); loss drifts from step 3 within the identical-code rerun envelope (confirmed with a main-vs-main control run)

New data point: #3801's varlen path runs under spmd_types unchanged -- previously untested in any qwen3.5 spmd enablement.

Known gaps:

  • varlen + spmd_types + --debug.spmd_typechecking fails: the varlen (B, L) -> (1, B*L) flatten drops the DP axis annotation, so the FLA conv input hits SpmdTypeError: args[0] missing axis mesh_dp ... has axes {} in _causal_conv1d_varlen. Supporting typechecking there means treating the flattened varlen span as a local region with boundary re-typing; follow-up.
  • (Correction, 2026-07-29) An earlier version of this description reported a MoE + PP pre-step-1 NCCL hang. That was an artifact of an under-provisioned validation job (1 CPU allocated for 8 ranks starves the pipeline p2p handshake), not this branch: properly provisioned, MoE + PP + spmd_types completes 10/10 steps (table above).

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 23, 2026
@pytorch-bot

pytorch-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/8gpu

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@Jeremalloch Jeremalloch changed the title [Do Not Merge] Enable spmd_types backend for Qwen3.5 dense [DO NOT REVIEW] Enable spmd_types backend for Qwen3.5 dense Jul 23, 2026
@tianyu-l
tianyu-l requested review from pianpwk and shuhuayu July 23, 2026 23:01
Wire the dense Qwen3.5 model for the spmd_types backend, which CI
previously blocked (pytorch#3901: not wired for the spmd_types mesh axes).

- parallelize.py: resolve the FSDP mesh via resolve_fsdp_mesh /
  resolve_sparse_fsdp_mesh under spmd_types (there is no flattened
  "fsdp" axis; FSDP shards the multi-axis dense storage mesh along
  DataParallelMeshDims), pass dp_mesh_dims through to the decoder and
  vision-encoder fully_shard calls, and run model.parallelize() even
  for pure-FSDP topologies since the SPMD parameter annotations are
  what fully_shard translates into DTensor params.
- sharding.py: annotate vision params without explicit state shardings
  as Replicate (FLUX's annotate_dp_cp_params_as_r pattern); add the
  missing out_src/in_src shardings that spmd_types requires to be
  explicit; redistribute the first decoder block's input R -> SP at the
  module boundary (the layer-0 residual previously relied on implicit
  DTensor redistribution, which spmd_types local tensors cannot do);
  vision rotary inv_freq R instead of I (same Replicate DTensor
  placement, but I cannot mix with R under typechecking); scaled-bias
  rowwise config for the vision rowwise-with-bias linears.
- __init__.py: vision attn.proj, mlp.fc2, and merger.fc2 become
  ScaledBiasRowwiseLinear (gpt_oss pattern) so the per-rank bias/tp
  contribution sums correctly through the TP all-reduce; addmm cannot
  type a Partial matmul plus Replicate bias.
- model.py: run the depthwise deltanet conv with local groups under
  spmd_types (module groups is the global channel count); local-region
  head-split views with boundary re-typing (SeparateQKVLinear pattern);
  register the FLA kernel autograd Functions as local-only for
  typechecking; annotate multimodal extras (pixel values, grid dims,
  mrope positions) that the trainer's input annotation does not cover;
  keep vision position/scatter index bookkeeping out of typechecking.
- vision_encoder.py: local-region head splits, position-embedding and
  block-mask construction outside typechecking with boundary re-typing.
- rope.py: build the MRoPE interleaved cache in a local region and
  re-type at the boundary.

Validated on 4x H100 (dp_shard=2, tp=2): spmd_types runs
qwen35_debugmodel, spmd_types + --debug.spmd_typechecking runs with AC
disabled, and seeded deterministic loss/grad_norm stays within float
noise of the default backend. Default-backend numerics compared against
unmodified main with the same seed. MoE (EP token dispatcher) and the
qwen3.5 CI spmd_types exclusion are follow-ups.
@Jeremalloch
Jeremalloch force-pushed the jm/qwen35-spmd-types branch from cc4ca56 to 47ab085 Compare July 27, 2026 17:55
@Jeremalloch Jeremalloch changed the title [DO NOT REVIEW] Enable spmd_types backend for Qwen3.5 dense Enable spmd_types backend for Qwen3.5 dense Jul 29, 2026
@Jeremalloch
Jeremalloch marked this pull request as ready for review July 29, 2026 23:14
@Jeremalloch
Jeremalloch marked this pull request as draft July 29, 2026 23:18
@Jeremalloch Jeremalloch changed the title Enable spmd_types backend for Qwen3.5 dense [Do not review] Enable spmd_types backend for Qwen3.5 dense Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant