Skip to content

docs(training): add DSpark speculative decoding design - #5233

Merged
kamran-nvidia merged 4 commits into
NVIDIA-NeMo:mainfrom
khazic:khazic/docs/dspark-speculative-design
Aug 13, 2026
Merged

kamran-nvidia merged 4 commits into
NVIDIA-NeMo:mainfrom
khazic:khazic/docs/dspark-speculative-design

Conversation

@khazic

@khazic khazic commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds a design proposal (RFC) page, docs/training/dspark-speculative-decoding.md,
for DSpark speculative-decoding draft training in Megatron-Bridge. Tracks #5230.

Why a design PR first

Megatron-Bridge has no draft-training subsystem today (MTP is the only
draft-adjacent path, and it is a Megatron-Core native module). DSpark's draft (a
semi-autoregressive parallel backbone plus Markov and confidence heads) is a new
neural module that must live either in Megatron-Core (like MTP) or as a
ModelOpt-injected module, and its parallel/loss integration can only be validated
on multi-GPU. So the draft module and its trainer belong in later, separately
reviewed PRs; this PR establishes the algorithm spec, the Bridge integration
shape, and the plan.

Contents

  • The DSpark draft: a semi-autoregressive block (parallel backbone attending to
    the frozen target's hidden states as context K/V) plus a Markov/RNN sequential
    head and a confidence head; the draft shares and freezes the target embedding
    and LM head.
  • Training objective: position-weighted CE (0.1) + total-variation (0.9) +
    confidence BCE (1.0); acceptance = 1 - TV, tau = sum cumprod(accept) + 1.
  • Bridge mapping: follows the ModelOpt distillation subsystem pattern (provider
    hook, forward_step_dspark, a post_training/dspark.py loss, a thin
    training/dspark.py entry, and dspark.* HF weight mappings).

Reference implementation: NeMo AutoModel
nemo_automodel/components/speculative/dspark/. Paper:
arXiv:2607.05147.

Docs-only; the page is registered in the "Training and Customization" toctree.

@copy-pr-bot

copy-pr-bot Bot commented Aug 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.

head regresses to `accept_rate`; the paper applies a post-hoc Sequential
Temperature Scaling calibration for serving.

### Reference implementation (NeMo AutoModel)

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 needed...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed that section. The pointer to the AutoModel reference in the overview stays.

yaoyu-33
yaoyu-33 previously approved these changes Aug 3, 2026
@aroshanghias-nvd aroshanghias-nvd added the waiting-on-maintainers Waiting on maintainers to respond label Aug 6, 2026
@yaoyu-33 yaoyu-33 removed the needs-review PR is ready for code review and waiting on a reviewer label Aug 6, 2026
khazic added 3 commits August 6, 2026 17:16
Design proposal (RFC) for adding DSpark speculative-decoding draft training to
Megatron-Bridge, tracking NVIDIA-NeMo#5230. It describes the DSpark draft (a
semi-autoregressive parallel backbone plus Markov and confidence heads), its
three-term CE / TV / confidence training objective and acceptance metric, and
how it maps onto the existing ModelOpt distillation subsystem pattern (provider
hook, custom forward step, loss, thin entry, HF weight mapping). It includes a
phased implementation plan and references the NeMo AutoModel reference
implementation.

Signed-off-by: khazic <khazzz1c@gmail.com>
Requested in review: the Megatron-Bridge design doc does not need a breakdown
of the AutoModel implementation; the pointer in the overview is enough.

Signed-off-by: khazic <khazzz1c@gmail.com>
The design page carried a four-phase implementation plan and an open-questions
list. Neither describes what this PR (or the repo) actually contains: they
promise work that is not scheduled and has no owner, so they go stale the moment
priorities move. The sequencing and the open decisions belong in the tracking
issue (NVIDIA-NeMo#5230), which is where they can be updated.

The remaining page is the design itself: the algorithm, where it fits in
Megatron-Bridge, and the proposed file surface. The frozen-target supervision
section keeps both capture options, since the trade-off between them is design
content rather than a schedule.

Signed-off-by: khazic <khazzz1c@gmail.com>
@yaoyu-33
yaoyu-33 force-pushed the khazic/docs/dspark-speculative-design branch from 097efcf to a4047be Compare August 7, 2026 00:21

@yaoyu-33 yaoyu-33 left a comment

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.

Architecture disposition: changes requested. This is a useful docs-only RFC, but it does not yet define an implementable stable contract. Before merge, it should correct the objective scaling, choose one end-to-end owner (or specify two complete alternative contracts), and define the frozen-weight, distributed-loss, checkpoint, and export boundaries. No DSpark core implementation is requested in this PR.


- `L_ce` (weight 0.1): cross-entropy of the draft logits against the target's
teacher-forced next tokens.
- `L_tv` (weight 0.9): total-variation distance `0.5 * ||p_draft - p_target||_1`

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.

[High] This halves the dominant loss relative to the cited DSpark objective. Paper Eq. 10 minimizes the weighted raw L1 distance ||p_draft - p_target||_1 with coefficient 0.9; the factor 1/2 appears in Eq. 8 only when forming the analytical acceptance/confidence target. The primary DeepSpec implementation and current NeMo AutoModel implementation make the same distinction. With the documented coefficient, an implementation following this line gets half the intended training gradient (the proposed follow-up #5241 makes exactly that change). Please define the training term as weighted raw L1 and retain 0.5 * L1 only for the acceptance/confidence target, or explicitly document and validate a different coefficient contract.

Plus an `examples/distillation/` recipe and a `docs/training/` page. This
provider-hook plus custom-forward-step plus custom-loss plus thin-entry pattern is
the established, low-friction way to add a training subsystem here, and it is the
recommended shape for DSpark.

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.

[High] The recommended extension surface is already deprecated and conflates two different owners. Current Bridge main warns that ModelProviderMixin-based construction must migrate to ModelConfig + ModelBuilder, so a new dynamic provider subclass cannot be the promised stable API. The two choices above also lead to different contracts: an MCore-native design makes MCore own the parallel module/loss/sharded checkpoint, while a ModelOpt-injected design makes ModelOpt own the transformation/module/loss/export and Bridge only configures it. Current ModelOpt main already has an HF DSpark plugin and DSparkExporter, while Bridge currently pins ModelOpt 0.46.0rc1, which predates that support. Please choose one end-to-end owner, or give each alternative its own complete module/loss/construction/checkpoint/export contract, and make the Bridge hook builder-backed rather than prescribing the legacy provider path.


Because the draft reuses the target's embedding and LM head and adds `fc`,
`markov_head`, `confidence_head`, and the draft decoder layers, the bridge mapping
registry must gain `dspark.*` entries (QKV and gated-MLP splits for the draft

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.

[High] dspark.* Bridge mappings are not implied until the frozen-weight and export schema are defined. The references do not physically alias target parameters: AutoModel creates draft-owned embed_tokens/lm_head, copies the target weights, and freezes the replicas, then controls their trainability separately. That distinction determines PP placement, sharded save/resume, and whether serving export stores or reconstructs them. It also differs by owner: ModelOpt already owns its DSpark deployment schema/exporter, whereas an MCore-native checkpoint would need a separately specified HF schema before AutoBridge can map it. Please define alias-versus-replica storage, TP/vocab-sharded exact-L1 computation, selected/final-hidden PP transport, CP masking, online checkpoint versus offline-cache schema, and the repository that owns export; then list only the Bridge mappings that remain necessary.

@khazic

khazic commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @yaoyu-33, all three are addressed in 02156f9. I verified each against the sources you cited before changing anything.

Objective scaling. You are right and the page was wrong. Eq. 10 minimizes the weighted raw L1 at coefficient 0.9, and the 1/2 belongs to the Eq. 8 acceptance label. AutoModel's _compute_local_l1_term accumulates l1_dist_per_token * loss_weight_mask with no 1/2, and the 0.5 appears only in _compute_accept_rate_3d; DeepSpec splits it the same way. The page now defines L_l1 as the raw weighted L1 and keeps 0.5 * L1 only in the acceptance label, and #5234 adds a regression asserting the pair together (disjoint distributions give l1_loss == 2, accept_rate == 0) so the two cannot drift again. I have closed #5241, which would have made exactly the change you flagged.

Owner. Settled on Megatron-Core native, with the reasoning written into a new "Decision" section rather than left implicit. The deciding fact is that ModelOpt's DSpark is HF-path only today: plugins/ has hf_dspark.py and modeling_dspark.py and export has DSparkExporter, but the Megatron-side plugins are megatron_eagle.py and megatron_medusa.py, with no megatron_dspark.py. Going the ModelOpt route would mean landing a Megatron-native DSpark in Model-Optimizer and then waiting on the nvidia-modelopt==0.46.0rc1 pin before any Bridge milestone could move. The consequence is spelled out too: Bridge owns the HF export schema, because DSparkExporter covers ModelOpt's own artifacts and not an MCore-native checkpoint.

Deprecated extension surface. Dropped. The construction path is now ModelConfig + ModelBuilder (a DSparkModelConfig naming a DSparkModelBuilder, mirroring MambaModelConfig/MambaModelBuilder), and the dspark_provider.py row is gone from the table along with the distillation-provider blueprint section that recommended it.

Contracts that were missing. The page now specifies replica-not-alias storage for the frozen embedding and LM head and why that choice drives PP placement, sharded save/resume, and export; shard-local exact L1 under TP (with gathered or top-k paths ruled out, since the acceptance label depends on its exactness); selected-hidden transport under PP; block-aligned anchor sampling under CP; the versioned offline feature-cache schema; and the checkpoint/export ownership split. It also documents the Megatron-Core loss return contract and its calculate_per_token_loss=True requirement, plus the measured memory contract for the probability L1. Both of those carry numbers from #5234's validation rather than estimates.

khazic added a commit to khazic/Megatron-Bridge_lao that referenced this pull request Aug 13, 2026
First increment of DSpark speculative-decoding draft training (NVIDIA-NeMo#5230; design in
NVIDIA-NeMo#5233): the architecture-agnostic, pure-torch core.

Adds src/megatron/bridge/training/post_training/dspark/:
* heads.py: the sequential head (VanillaMarkov / GatedMarkovHead / RNNHead) that
  injects intra-block token dependency onto the parallel-backbone logits, plus the
  confidence head.
* loss.py: the three-term CE + total-variation + confidence-BCE objective with
  per-position decay, and the analytical acceptance metrics (accept_rate = 1 - TV,
  tau = sum cumprod(accept) + 1). The data-parallel denominator reduction is an
  injectable dp_group, so the same math wires to a Megatron-Core forward step later.

CPU unit tests cover the head math and the loss / acceptance math. The
parallel-aware draft backbone and the forward-step / provider integration (which
need Megatron-Core and multi-GPU validation) are follow-ups.

Signed-off-by: khazic <khazzz1c@gmail.com>
…racts

Select Megatron-Core native ownership and give the reasons. ModelOpt's DSpark
support is HF-path only (hf_dspark and modeling_dspark plugins plus
DSparkExporter, with megatron_eagle and megatron_medusa but no megatron_dspark),
so routing through ModelOpt would put a Model-Optimizer contribution and a pin
bump in front of every Bridge milestone.

Replace the ModelProviderMixin recommendation with ModelConfig + ModelBuilder.
Provider-based configuration is deprecated in current main, so it could not be
the stable API this page promises.

Correct the objective: the training term is the raw weighted L1 of paper Eq. 10,
and the factor 1/2 belongs only to the Eq. 8 acceptance label. Training half the
L1 at the published coefficient would halve the dominant gradient.

Define the contracts the previous revision left open: replica-not-alias storage
for the frozen embedding and LM head, shard-local exact L1 under tensor
parallelism, selected-hidden transport under pipeline parallelism, block-aligned
anchor sampling under context parallelism, the versioned offline feature-cache
schema, the Megatron-Core loss return contract with its calculate_per_token_loss
requirement, the measured memory contract for the probability L1, and Bridge as
the owner of the HF export schema.

Signed-off-by: khazic <khazzz1c@gmail.com>
khazic added a commit to khazic/Megatron-Bridge_lao that referenced this pull request Aug 13, 2026
First increment of DSpark speculative-decoding draft training (NVIDIA-NeMo#5230; design in
NVIDIA-NeMo#5233): the architecture-agnostic, pure-torch core.

Adds src/megatron/bridge/training/post_training/dspark/:
* heads.py: the sequential head (VanillaMarkov / GatedMarkovHead / RNNHead) that
  injects intra-block token dependency onto the parallel-backbone logits, plus the
  confidence head.
* loss.py: the three-term CE + total-variation + confidence-BCE objective with
  per-position decay, and the analytical acceptance metrics (accept_rate = 1 - TV,
  tau = sum cumprod(accept) + 1). The data-parallel denominator reduction is an
  injectable dp_group, so the same math wires to a Megatron-Core forward step later.

CPU unit tests cover the head math and the loss / acceptance math. The
parallel-aware draft backbone and the forward-step / provider integration (which
need Megatron-Core and multi-GPU validation) are follow-ups.

Signed-off-by: khazic <khazzz1c@gmail.com>
@khazic
khazic force-pushed the khazic/docs/dspark-speculative-design branch from 02156f9 to ac09592 Compare August 13, 2026 02:53
@kamran-nvidia
kamran-nvidia requested a review from yaoyu-33 August 13, 2026 11:26
@kamran-nvidia kamran-nvidia added needs-review PR is ready for code review and waiting on a reviewer and removed waiting-on-maintainers Waiting on maintainers to respond labels Aug 13, 2026
@yaoyu-33 yaoyu-33 added ready-to-merge PR is approved, current, and only waiting for CI to pass before merge and removed needs-review PR is ready for code review and waiting on a reviewer labels Aug 13, 2026
@kamran-nvidia

Copy link
Copy Markdown
Contributor

/ok to test ac09592

@kamran-nvidia
kamran-nvidia merged commit 9d31c98 into NVIDIA-NeMo:main Aug 13, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:training Training loop, callbacks, and runtime integration community-request docs Documentation-only updates or documentation debt docs-only With great power comes great responsibility. ready-to-merge PR is approved, current, and only waiting for CI to pass before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants