Skip to content

[Feature] Support Intern-S2-Preview training on NPU - #2052

Open
CyCle1024 wants to merge 9 commits into
InternLM:mainfrom
CyCle1024:ccy/npu/intern_s2_preview_support
Open

CyCle1024 wants to merge 9 commits into
InternLM:mainfrom
CyCle1024:ccy/npu/intern_s2_preview_support

Conversation

@CyCle1024

@CyCle1024 CyCle1024 commented Aug 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This PR adds NPU training support for Intern-S2-Preview while keeping the shared model and CUDA operator contracts stable.

  • Improve MTP recomputation, FSDP scheduling, parameter residency, and LM-head prefetch control.
  • Preserve device Tensor sequence boundaries and pass NPU CPU-list metadata through explicit parameters.
  • Add Triton-Ascend fused cross-entropy for large-vocabulary training.
  • Skip tail-padding tokens in the standard MoE dispatch path and cache the required indices.
  • Add self-contained NPU GatedDeltaNet causal-convolution, gated-delta-rule, RMSNormGated, metadata, and sequence-parallel support.
  • Integrate NPU CPU-affinity binding into the trainer lifecycle and retain host IRQ configuration helpers.
  • Remove the dependency on a version-specific private DTensor shard API.

Design notes

  • Public cumulative-length arguments remain device tensors. NPU-only CPU lists and chunk metadata use separate, explicit parameters, so CUDA kernels do not receive Python lists.
  • GatedDeltaNet uses [B, T, H, D] as its public layout. The NPU wrappers preserve native head-first storage with transpose views where possible and materialize only when required by a fixed-layout kernel.
  • Normal GatedDeltaNet forward keeps one mixed-QKV convolution. Sequence-parallel forward gathers the global packed-token axis, shards heads, and runs separate Q/K/V convolutions, matching the NPU kernel geometry.
  • GatedDeltaNet metadata is prepared once per sequence context and reused across layers. Cache reuse requires identical cumulative sequence boundaries, device, token count, and kernel block configuration.
  • Backend-specific implementations and dispatch stay under xtuner/v1/ops; model modules keep device-independent logical interfaces.
  • CUDA keeps its existing tensor inputs, loss-reduction behavior, and time-major GatedDeltaNet contract.

Known limitations

  • skip_dispatch_pad_tokens currently covers the standard single-context MoE forward, not the intra-layer multi-microbatch/domino-EP path.
  • GatedDeltaNet currently requires B == 1, num_value_heads divisible by num_key_heads, and equal key/value head dimensions.
  • In normal non-SP NPU GatedDeltaNet backward, PyTorch's generic mixed-QKV split backward produces a time-major contiguous gradient. The fixed-stride causal-convolution backward kernel therefore needs one additional layout materialization compared with the native-layout Tina prototype. The SP path does not have this mixed-QKV split. A stride-preserving custom autograd split is the intended follow-up optimization.
  • Dynamic device-task and IRQ discovery is used by export_bind_config(). Trainer run() uses the packaged binding configuration; host IRQ setup remains an explicit launch-script operation.

History cleanup (2026-09-23)

The branch was rebuilt on top of the latest upstream/main (fb51baeb) to replace the repeated main merges with a linear history. The previous branch head (921b7637) is backed up at https://github.com/jayhenry/xtuner/tree/backup/pr2052.

  • The 7 original commits are kept with their authors and messages. Conflict resolutions from the earlier merges are folded into the commits they belong to.
  • ejunered's MTPBlock single-tensor dispatch fix (a18595ae) is folded into Improve MTP compatibility and scheduling, so that commit is correct on its own.
  • ejunered's triton-ascend cache_results guard is kept as a separate final commit.
  • MTP activation checkpointing now follows upstream/main: every MTP layer uses apply_activation_checkpointing. The PR-only FSDPConfig.mtp_checkpoint_use_reentrant option and the checkpoint_wrapper non-reentrant branch are removed.
  • Apart from the MTP checkpointing change above, the final code tree is identical to the previous branch head.

Rebase onto e7299bbc (2026-09-24)

Rebased again onto the latest upstream/main (e7299bbc). The previous head (31d86fe4) is backed up at https://github.com/jayhenry/xtuner/tree/backup/pr2052-31d86fe4.

  • MTPConfig: kept main's loss_type / tv_loss_chunk_size alongside this PR's disable_lm_head_prefetch.

  • MoE.forward: this PR runs the main LM branch (norm + lm_head) after MTP, so the new e2e TV loss ([Feature] Add end-to-end TV loss for MTP SFT #2070) would have received un-normalized target states. The TV target is now self.norm(layer_hidden_states), matching the intra-layer micro-batch path.

  • Added tests/model/test_moe_mtp_e2e_tv.py as a regression test: it compares mtp_loss with an lm_head TV call on norm(main hidden states) and fails without the fix above.

@CyCle1024 CyCle1024 changed the title [Feature] Add comprehensive NPU support for GatedDeltaNet [WIP][Feature] Add comprehensive NPU support for GatedDeltaNet Aug 28, 2026
@CyCle1024
CyCle1024 force-pushed the ccy/npu/intern_s2_preview_support branch 2 times, most recently from f30f490 to 95043dc Compare August 28, 2026 06:59
Comment thread xtuner/v1/train/trainer.py Outdated
Comment thread xtuner/v1/optim/muon.py Outdated
Comment thread xtuner/v1/ops/rms_norm/__init__.py Outdated
Comment thread xtuner/v1/data_proto/sequence_context.py Outdated
Comment thread xtuner/v1/data_proto/sequence_context.py Outdated
Comment thread xtuner/v1/model/moe/moe.py
Comment thread xtuner/v1/model/moe/moe.py Outdated
layer_hidden_states = hidden_states
hidden_states = self.norm(hidden_states)

# Get LM loss context from dict

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

rollback

Comment thread xtuner/v1/model/moe/moe.py
Comment thread xtuner/v1/model/moe/moe.py Outdated
# leave the first post-MTP LM-head call to unshard on demand for now. A future
# optimization can explicitly prefetch the LM head in MTPBlock only after its final
# logical depth finishes.
if not mtp_config.share_weights:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

rollback

Comment thread xtuner/v1/model/moe/moe.py
Comment thread xtuner/v1/model/moe/qwen3vl_text.py Outdated
nonpad_indices = seq_ctx.nonpad_indices
non_pad_token = nonpad_indices.numel()
num_tokens_global, z_world_size = self._z_loss_dist_token_count(z_ctx, non_pad_token, seq_ctx.mask.device)
num_tokens_global, z_world_size = self._z_loss_dist_token_count(z_ctx, non_pad_token, nonpad_indices.device)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

rollback

Comment thread xtuner/v1/module/dispatcher/torch_all2all.py Outdated

@CyCle1024 CyCle1024 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fix

@CyCle1024 CyCle1024 changed the title [WIP][Feature] Add comprehensive NPU support for GatedDeltaNet [WIP][Feature] Add InternS2_35B_preview NPU support, along with some general optimization Aug 28, 2026
@CyCle1024
CyCle1024 force-pushed the ccy/npu/intern_s2_preview_support branch from 83a2861 to c64718d Compare August 30, 2026 09:59
@CyCle1024
CyCle1024 requested a review from jayhenry August 30, 2026 10:51
@CyCle1024
CyCle1024 force-pushed the ccy/npu/intern_s2_preview_support branch from c64718d to a0381c6 Compare August 30, 2026 10:53
@CyCle1024 CyCle1024 changed the title [WIP][Feature] Add InternS2_35B_preview NPU support, along with some general optimization [Feature] Add InternS2_35B_preview NPU support, along with some general optimization Aug 30, 2026
@CyCle1024 CyCle1024 changed the title [Feature] Add InternS2_35B_preview NPU support, along with some general optimization [Feature] Support Intern-S2-Preview training on NPU Aug 30, 2026
CyCle1024 pushed a commit to CyCle1024/xtuner that referenced this pull request Sep 20, 2026
…w PR InternLM#2052

Resolved 5 files:
- data_proto/__init__.py: merge DSATopKCacheState/GatedDeltaNetMetadata exports
- data_proto/sequence_context.py: keep PR's two new dataclasses
- model/moe/moe.py: MTP checkpoint toggle — reentrant branch adapted to main's
  apply_activation_checkpointing (its _checkpoint_pytree subsumes PR's
  pytree_reentrant_checkpoint + adds reuse_during_recompute frame); non-reentrant
  fallback uses torch checkpoint_wrapper(NO_REENTRANT); add CheckpointImpl/
  checkpoint_wrapper imports; add fsdp.mtp_checkpoint_use_reentrant config field
- module/decoder_layer/moe_decoder_layer.py: keep PR's pad-scatter logic, return
  via main's structured _build_output
- module/mtp/mtp_block.py: keep PR's always-list contract + shared return tail
  + _reshard_shared_layer
CyCle1024 added a commit to CyCle1024/xtuner that referenced this pull request Sep 20, 2026
[Merge] Resolve conflicts with upstream/main for PR InternLM#2052
ejunered added a commit to ejunered/xtuner that referenced this pull request Sep 21, 2026
…w PR InternLM#2052

Resolved 5 files:
- data_proto/__init__.py: merge DSATopKCacheState/GatedDeltaNetMetadata exports
- data_proto/sequence_context.py: keep PR's two new dataclasses
- model/moe/moe.py: MTP checkpoint toggle — reentrant branch adapted to main's
  apply_activation_checkpointing (its _checkpoint_pytree subsumes PR's
  pytree_reentrant_checkpoint + adds reuse_during_recompute frame); non-reentrant
  fallback uses torch checkpoint_wrapper(NO_REENTRANT); add CheckpointImpl/
  checkpoint_wrapper imports; add fsdp.mtp_checkpoint_use_reentrant config field
- module/decoder_layer/moe_decoder_layer.py: keep PR's pad-scatter logic, return
  via main's structured _build_output
- module/mtp/mtp_block.py: keep PR's always-list contract + shared return tail
  + _reshard_shared_layer
@jayhenry
jayhenry force-pushed the ccy/npu/intern_s2_preview_support branch from 921b763 to 31d86fe Compare September 23, 2026 14:15
Co-authored-by: CyCle1024 <cycle1024@gmail.com>
wentiange and others added 7 commits September 24, 2026 13:42
Co-authored-by: houyufeng4@huawei.com <houyufeng4@huawei.com>

Co-authored-by: CyCle1024 <cycle1024@gmail.com>
Co-authored-by: CyCle1024 <cycle1024@gmail.com>
Co-authored-by: CyCle1024 <cycle1024@gmail.com>
Co-authored-by: CyCle1024 <cycle1024@gmail.com>
Co-authored-by: CyCle1024 <cycle1024@gmail.com>
…totune patch

triton-ascend 3.2.0's autotune signature has no cache_results parameter,
so the unconditional kwargs["cache_results"] = False raised TypeError
when XTUNER_DETERMINISTIC=true. Probe original_autotune's signature and
only forward cache_results when the parameter exists.
@jayhenry
jayhenry force-pushed the ccy/npu/intern_s2_preview_support branch from 31d86fe to ad11739 Compare September 24, 2026 13:48
@jayhenry jayhenry closed this Sep 24, 2026
@jayhenry jayhenry reopened this Sep 24, 2026
The main LM branch (norm + lm_head) now runs after MTP, so the e2e TV loss
must normalize the main hidden states itself. The test compares the model's
mtp_loss with an lm_head TV call on norm(main hidden states) and fails when
the un-normalized states are used as the TV target.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants