Skip to content

[Qwen3VL] replace mask_scatter and boolean indexing by slice assignment - #3099

Merged
shuhuayu merged 1 commit into
pytorch:mainfrom
shuhuayu:moe
Apr 27, 2026
Merged

shuhuayu merged 1 commit into
pytorch:mainfrom
shuhuayu:moe

Conversation

@shuhuayu

Copy link
Copy Markdown
Contributor

Replace masked_scatter and boolean indexing in vision scatter and deepstack with slice assignment. This avoids DTensor-incompatible ops masked_scatter, and boolean indexing in the first place, and thus hidden states can now flow as DTensor(Replicate) throughout the decoder without to_local() conversions. This saves DTensor side changes to support new ops.

Since masked_scatter internally creates a new tensor, this new in-place mutation approach can save some memory operations. The canonical example in our config of 8b model training has an improved mfu of 37.7% compared to the previous 36.9%.

The new introduced _compute_vision_positions method shares some overlapped computation with _compute_mrope_freqs, but the current way it cleaner, and the additional cost is around 0.2ms compared to the total forward pass cost around 35ms.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 24, 2026
@shuhuayu
shuhuayu merged commit 99a2bcb into pytorch:main Apr 27, 2026
17 of 19 checks passed
fegin added a commit that referenced this pull request Apr 30, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

## Framework changes

### ``Module.from_nn_module`` auto-generates a ``Config``

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

### Centralize ``from_nn_module`` calls

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

### ``LocalMapConfig`` accepts ``None`` per-arg

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

## Vision encoder Configs

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

## ``set_qwen3_vl_sharding_config`` helper

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

## ``parallelize_qwen3_vl`` rewritten

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

## ``apply_moe_ep_tp`` deleted

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

## Verification

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

## Status of the three deferral blockers from sharding_known_fixes_and_pending

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: fa224e0
Pull-Request: #3165
fegin added a commit that referenced this pull request Apr 30, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

## Framework changes

### ``Module.from_nn_module`` auto-generates a ``Config``

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

### Centralize ``from_nn_module`` calls

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

### ``LocalMapConfig`` accepts ``None`` per-arg

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

## Vision encoder Configs

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

## ``set_qwen3_vl_sharding_config`` helper

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

## ``parallelize_qwen3_vl`` rewritten

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

## ``apply_moe_ep_tp`` deleted

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

## Verification

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

## Status of the three deferral blockers from sharding_known_fixes_and_pending

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 9b5afe9
Pull-Request: #3165
fegin added a commit that referenced this pull request Apr 30, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

## Framework changes

### ``Module.from_nn_module`` auto-generates a ``Config``

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

### Centralize ``from_nn_module`` calls

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

### ``LocalMapConfig`` accepts ``None`` per-arg

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

## Vision encoder Configs

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

## ``set_qwen3_vl_sharding_config`` helper

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

## ``parallelize_qwen3_vl`` rewritten

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

## ``apply_moe_ep_tp`` deleted

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

## Verification

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

## Status of the three deferral blockers from sharding_known_fixes_and_pending

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 7a88a5b
Pull-Request: #3165
fegin added a commit that referenced this pull request Apr 30, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 5e17765
Pull-Request: #3165
fegin added a commit that referenced this pull request May 1, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 25772c6
Pull-Request: #3165
fegin added a commit that referenced this pull request May 1, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: d156097
Pull-Request: #3165
fegin added a commit that referenced this pull request May 1, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 8327125
Pull-Request: #3165
fegin added a commit that referenced this pull request May 5, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: f995e3e
Pull-Request: #3165
fegin added a commit that referenced this pull request May 5, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 0f3e937
Pull-Request: #3165
fegin added a commit that referenced this pull request May 5, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 1f37242
Pull-Request: #3165
fegin added a commit that referenced this pull request May 5, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 94e7c67
Pull-Request: #3165
fegin added a commit that referenced this pull request May 6, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 04b2dde
Pull-Request: #3165
fegin added a commit that referenced this pull request May 7, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: ef6d09c
Pull-Request: #3165
fegin added a commit that referenced this pull request May 7, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: ef6d09c
Pull-Request: #3165
fegin added a commit that referenced this pull request May 12, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: c16441f
Pull-Request: #3165
fegin added a commit that referenced this pull request May 12, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 67d3a48
Pull-Request: #3165
fegin added a commit that referenced this pull request May 12, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 67d3a48
Pull-Request: #3165
fegin added a commit that referenced this pull request May 12, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 67d3a48
Pull-Request: #3165
fegin added a commit that referenced this pull request May 12, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 67d3a48
Pull-Request: #3165
fegin added a commit that referenced this pull request May 13, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 9f1403d
Pull-Request: #3165
fegin added a commit that referenced this pull request May 13, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 1c24118
Pull-Request: #3165
fegin added a commit that referenced this pull request May 13, 2026
Convert Qwen3-VL entirely to config-based sharding (decoder dense + MoE
+ vision encoder) and enable ``--training.full_dtensor`` for Qwen3-VL.

This closes the deferral that has tracked qwen3_vl across PR1-PR8. After
this PR, Qwen3-VL uses the same declarative ``model.parallelize
(parallel_dims)`` flow as qwen3 / llama4 / gpt_oss / deepseek_v3, and
``apply_moe_ep_tp`` is fully deleted (no remaining callers).

``Module.from_nn_module(nn.Foo)`` now also generates a ``Foo.Config``
subclass of ``Module.Config``. The Config inherits ``sharding_config``
and ``param_init`` slots; a custom ``build(*args, **kwargs)`` forwards
positional / keyword args to the underlying ``nn.Module`` constructor
and applies the slots to the resulting instance. Two usage patterns:

    LayerNorm = Module.from_nn_module(nn.LayerNorm)
    # Direct construction (no declarative sharding):
    norm = LayerNorm(dim, eps)
    # Config-based construction:
    norm = LayerNorm.Config(
        sharding_config=norm_config(enable_sp=False),
    ).build(dim, eps)

The cache key (``_created_classes``) stays keyed on ``nn_module_cls`` --
distinct sharding plans live on Config instances, not on the class.

New ``torchtitan/models/common/nn_modules.py`` is the single source of
truth for ``from_nn_module``-wrapped standard nn modules: ``Conv2d``,
``GELU``, ``GroupNorm``, ``Identity``, ``LayerNorm``, ``SiLU``. Top-of-
file docstring documents the two usage patterns. All five existing call
sites import from here.

``LocalMapConfig.in_placements`` / ``out_placements`` /
``in_grad_placements`` types loosen to ``tuple[NamedPlacement | None,
...]`` so non-tensor function args can pass through ``local_map``
unchanged. ``Module._needed_axes`` skips ``None`` entries; the framework
passes ``None`` to ``torch.distributed.tensor.experimental.local_map``.

Each of ``PatchEmbed``, ``VisionRotaryEmbedding``, ``PatchMerger``,
``VisionAttention``, ``VisionMLP``, ``VisionTransformerBlock`` now has
its own ``Config`` subclass of ``Module.Config``. ``Qwen3VLVisionEncoder``
constructs children via ``Sub.Config(...).build()`` instead of direct
positional construction, so the Config tree is walkable for
``set_qwen3_vl_sharding_config``. The flat fields on
``Qwen3VLVisionEncoder.Config`` (e.g. ``attn_qkv: Linear.Config``) are
preserved as the external contract; the parent ``__init__`` builds the
nested Configs from them.

New ``torchtitan/models/qwen3_vl/sharding.py`` covers:

- Decoder dense (mirrors ``set_qwen3_sharding_config`` with
  ``enable_sp=False`` -- VLM keeps Replicate hidden states for vision
  scatter and DeepStack).
- MoE FFN (delegates to ``set_moe_sharding_config`` from PR7 with
  ``enable_sp=False``).
- Vision encoder: ``patch_embed.proj`` Replicate, ``pos_embed`` declared
  via ``state_shardings`` on encoder root, vision blocks
  (``norm1/norm2``, ``attn.qkv`` colwise, ``attn.proj`` rowwise with
  Replicate output, ``mlp.linear_fc1`` colwise, ``mlp.linear_fc2``
  rowwise with Replicate output), and mergers (main + DeepStack).

Vision activations flow as ``DTensor(Replicate)`` throughout (no SP);
linears shard on TP for memory but redistribute back to Replicate on
output, matching the legacy ``_apply_tp_to_vision_encoder`` pattern.

Drops ``_apply_tp_to_vision_encoder``, ``_apply_non_moe_tp_to_decoder``,
the ``apply_moe_ep_tp`` call, and the manual inner-attention
``local_map`` wraps. Uses the llama3-style branch: ``validate_config``
+ ``model.parallelize(parallel_dims)`` under full_dtensor; legacy CP
wrap + parallelize otherwise. ``apply_fsdp`` accepts the new
``dp_mesh_dims`` / ``edp_mesh_dims`` kwargs from PR8.

The ``NotImplementedError("Context Parallel is not yet supported for
Qwen3-VL")`` gate now triggers only under ``full_dtensor=False`` --
under full_dtensor, CP flows through inner attention's ``LocalMapConfig``.

After Qwen3-VL migrates, no callers remain. The function is removed
from ``torchtitan/models/llama4/parallelize.py`` along with all its
imports (``parallelize_module``, ``PrepareModuleInputOutput``,
``RowwiseParallel``, ``ExpertParallel``, ``ExpertTensorParallel``,
``TensorParallel``, ``ColwiseParallelWithGradPlacement``, ``NoParallel``,
``AllToAllTokenDispatcher``, ``TorchAOTokenDispatcher``, ``Partial``,
``Replicate``).

- 33/33 ``test_module.py`` unit tests pass (4 new tests for
  ``Module.from_nn_module(...).Config``).
- ``pre-commit run --all-files`` clean.
- qwen3 EP=2 non-full_dtensor regression bit-identical to PR8 (loss
  8.15586 / grad_norm 3.0237).
- Full Qwen3-VL training-loop verification skipped (test environment
  lacks torchvision); construction tests confirm the new Config tree
  builds correctly. Real Qwen3-VL multimodal verification is owed to
  a follow-up validation run on the user's primary dev environment.

(a) ``ShardingConfig.local_output_grad_placements`` -- landed in
    Pre-PR7 (as part of the PR7 commit).
(b) ``Module.from_nn_module(...)`` auto-generates a Config so call sites
    can attach a sharding plan -- this PR.
(c) ``_compute_learned_pos_embeds`` and vision-embed boolean indexing:
    the boolean indexing was already replaced by slice assignment in
    PR #3099 (no lift needed); the ``_compute_learned_pos_embeds``
    ``local_map`` wrap remains imperative because the
    Module-owns-param-passed-through-local_map pattern doesn't compose
    cleanly with ``LocalMapConfig`` (the framework's ``local_map``
    converts function arguments, not module parameters accessed via
    ``self``).

ghstack-source-id: 43a4e41
Pull-Request: #3165
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.

2 participants