Skip to content

feat(model): add Shensi model support - #6278

Open
zongzhilou wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
zongzhilou:shensi
Open

zongzhilou wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
zongzhilou:shensi

Conversation

@zongzhilou

Copy link
Copy Markdown

What does this PR do ?

Add Shensi model support to Megatron-Bridge: a sparse-attention Mixture-of-Experts family with its Hugging Face configuration, Megatron-Core provider, AutoBridge parameter mapping and tests.

Shensi is a sparse-attention Mixture-of-Experts family: every decoder layer picks one of three attention paths from a per-layer plan (compressed sparse attention, heavily compressed attention or a sliding window), the residual stream is a pool of streams that sublayers read from and write back into, layers are grouped into attention-residual blocks that may span pipeline stages, the leading layers are token-embedding-gated dense MLPs, and the routed experts of the remaining layers run through a low-rank bottleneck with an ERC auxiliary loss.

The hybrid attention itself is Megatron-Core's dsv4_hybrid path; this bridge adds what sits around it and maps the checkpoint both ways.

Changelog

  • configuration_shensi.py: HF-side config with the per-layer attention/MLP plans, the split RoPE parameters and validation, plus the Megatron-Core transformer config carrying the Shensi fields
  • modeling_shensi.py: mHC hyper-connections, attention residuals (including their pipeline-state transfer), the MoE layout, selective fp32 keeping, the decoder layer, layer specs and the model
  • shensi_provider.py: provider deriving the Megatron-Core fields from the config and building the decoder block and MTP specs
  • shensi_bridge.py: AutoBridge entry for config and parameter mapping, covering the mHC mixers, attention-residual readers, compressor and indexer weights and the hash-MoE layers
  • tests: unit tests for the attention-residual module and the bridge mappings, functional tests for the HF <-> Megatron round trip, the forward pass, right-padding behaviour and the provider/MTP configuration, registered in tests/functional_tests/launch_scripts/h100/active/L0_Launch_models_shensi.sh
  • docs and registry: models/shensi/README.md documents the model, models/init.py registers the package and README.md lists it

GitHub Actions CI

See the CI section in the Contributing doc for how to trigger the CI. A Nvidia developer will need to approve and trigger the CI for external contributors.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you add or update any necessary documentation?
  • Does the PR affect components that are optional to install? (Ex: Numba, Pynini, Apex etc)
    • Reviewer: Does the PR have correct import guards for all optional libraries?

If you haven't finished some of the above items you can still open "Draft" PR.

Additional Information

  • Related to # (issue) — none.
  • Unit tests: pytest tests/unit_tests/models/shensi
  • Functional tests: bash tests/functional_tests/launch_scripts/h100/active/L0_Launch_models_shensi.sh

@copy-pr-bot

copy-pr-bot Bot commented Oct 1, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@cuichenx cuichenx added feature New capabilities, enhancements, or enablement work area:model Model implementations and HF bridge logic needs-review PR is ready for code review and waiting on a reviewer labels Oct 1, 2026
@cuichenx

cuichenx commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Hi @zongzhilou is there a public HF model for Shensi? i could not find it

@zongzhilou

Copy link
Copy Markdown
Author

Hi @zongzhilou is there a public HF model for Shensi? i could not find it

Hi! Shensi is our in-house architecture, so there's no public HF model yet. Merging a model PR into transformers requires the full weights to be open-sourced, but I'm still running experiments. Since Shensi was trained with mbridge, I've submitted the PR to mbridge first to add support for the architecture, and the two implementations are identical. Once the experiments are done, I'll release the weights and submit the PR to transformers.



@dataclass
class ShensiModelProvider(ShensiTransformerConfig, GPTModelProvider):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not blocking: GPTModel is deprecated, and we recommend migrating to the HybridModel class.
Also, we recommend moving to the model config + model builder set up (see MuseGlimmerModelBuilder for an example)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done. The model rides HybridModel (ShensiModel, provider ShensiModelProvider(HybridModelProvider, ShensiTransformerConfig)), and shensi_builder.py adds the model-config/model-builder path
(ShensiModelConfig + ShensiModelBuilder(HybridModelBuilder)). Both delegate to
shensi_hybrid.build_shensi_model, so the provider and the builder construct the same model.

def _hf_attention_residual():
"""The HF reference module (skips the test when the shensi HF implementation is absent)."""
pytest.importorskip("transformers.models.shensi.modeling_shensi")
from transformers.models.shensi.configuration_shensi import ShensiConfig

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this will throw an error right now. Better guard it

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Guarded: pytest.importorskip for both shensi HF modules in
tests/unit_tests/models/shensi/test_shensi_attn_res_parity.py, plus skip-with-reason when
ShensiConfig(...) cannot be built — the test skips explicitly instead of failing or passing
vacuously.

APPLY_GUARD_ATTR = "_shensi_orig_apply"
ENV_SWITCH = "SHENSI_HC_FP32_KEEP"
ENV_GROUPS = "SHENSI_FP32_KEEP_GROUPS"
ENV_NO_CAST = "SHENSI_FP32_KEEP_NO_CAST"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we usually don't use environment variables to control these settings; either hardcode one option or expose it properly if both options are needed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed, including the test-side ones (the old TE_FL_PREFER). Kernel selection belongs to the
launcher; grep -rn "environ" is empty across the shensi model package and its unit/functional
tests.

return header, state


class ShensiAttnResPPHandoff:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is fine, but just noting that upstream has NVIDIA/Megatron-LM#6840 as a draft, so this can be removed after 6840 lands

if ranks is None:
return
if ranks not in _GROUP_CACHE:
_GROUP_CACHE[ranks] = torch.distributed.new_group(ranks=list(ranks))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is a little suspicious. can you check how MCore 6840 handles this without creating new process groups?

@zongzhilou
zongzhilou force-pushed the shensi branch 21 times, most recently from bbceb61 to 87ceabb Compare October 11, 2026 09:05
Shensi is a sparse-attention MoE family: every decoder layer picks one of three attention
paths from a per-layer plan (compressed sparse / heavily compressed / sliding window), the
residual stream is a pool of streams, layers group into attention-residual blocks that may
span pipeline stages, the leading layers are token-embedding-gated dense MLPs, and routed
experts use a low-rank bottleneck with an ERC auxiliary loss. The hybrid attention rides
Megatron-Core's dsv4_hybrid path.

- configuration_shensi: HF-side config plus the Megatron-Core transformer config (no mHC: the
  residual path is the attention-residual read, so the V4 stream fields/knobs are not carried)
- modeling_shensi: attention residuals with pipeline-stage state
  transfer (the block history is kept as one tensor per block, without a full-buffer copy at
  every block boundary), MoE layout, selective fp32 keeping, decoder layer/stack and layer specs
- shensi_bridge / shensi_provider: config and parameter mapping, model provider
- shensi_builder: HybridModelConfig + HybridModelBuilder wiring so a training recipe can
  build the model through Megatron-Core's model-config / model-builder API
- unit tests (attention-residual parity against the HF reference, per-slot block layout,
  hybrid stack, provider, ERC gather, pipeline state transfer) and functional tests with an
  L0 launcher
- model registry, README and docs entries

Follow-ups folded in while validating with a real 5-step smoke run:
- defaults follow the 2.78B / 1.24B-active geometry (routed_expert_hidden_size 320,
  attn_res_block_size 2, o_groups 2, o_lora_rank 320, moe_intermediate_size 800, 1 MTP layer)
- attention-residual block writes are functional: the in-place variant bumped the storage
  version of the buffer the read path holds in the autograd graph and failed in backward
- csa_dense_mode and the loss coefficients (indexer KL / aux / ERC) remain training-side
  settings: they are no longer forced by hard defaults or by HF-side injection
- the provider path (AutoBridge conversion / provider smoke) builds again after the HybridModel
  migration: it no longer reads the GPT-only `scatter_embedding_sequence_parallel`, and the MTP
  depths map their inner decoder layer under `mtp.layers.{d}.mtp_model_layer.layers.0.` instead of
  `mtp.layers.{d}.mtp_model_layer.`
- the MTP attention-residual state is bound on the inner decoder layers as well as on the wrapper
  (the layers read it in forward, the wrapper is where the MTP layer and its test read it)
- `gradient_accumulation_fusion` re-declares the upstream auto-detect default on the provider, so a
  host without apex fuses nothing instead of failing at model construction
- `finalize()` rejects Megatron-Core's own stream machinery (`enable_mhc_connections`,
  `wide_residual`) the way Megatron-Core rejects an incompatible combination: shensi owns its
  residual path inside the decoder layers, so the stock knobs would add a second one
- the functional tests no longer set `TE_FL_PREFER`; the model and test packages are env-free
- the attention-residual state crosses a pipeline stage boundary inside the stage payload itself
  (`pack_attn_res_payload` / `unpack_attn_res_payload`, the Megatron-LM#6840 layout): the schedule
  ships whatever the model returns, so the side-channel handoff, its private process group and its
  header/serialization code are gone, and the state's gradients return through the same tensor; the
  provider turns on `variable_seq_lengths` when the layout actually crosses, since the payload shape
  then depends on the microbatch

The model carries no mHC: the attention-residual read is the residual path, so the V4
stream fields (`hc_mult` and friends), the mHC modules, their parameter mappings and the
per-stream state layout are gone (state, payload rows and fp32-keeping now describe one
residual stream).
- the hash-MoE deep embedding lives in the routed-expert bottleneck (a per-token scale
  inside the low-rank space, RWKV-8 DeepEmbed's 4x form) and is applied between the
  activation and the down projection, TP-sharded like the FC1 output

Signed-off-by: 313892219+zongzhilou <313892219+zongzhilou@users.noreply.github.com>
@zongzhilou
zongzhilou force-pushed the shensi branch 3 times, most recently from c7987b2 to 22fe063 Compare October 11, 2026 17:56

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

area:model Model implementations and HF bridge logic community-request feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants