Skip to content

[CUDA] Add FoldMid2 and FoldTail speculative state fold contrib operators - #33474

Open
Akshay Sonawane (apsonawane) wants to merge 8 commits into
mainfrom
asonawane/main-ops-mtp-fold-state
Open

Akshay Sonawane (apsonawane) wants to merge 8 commits into
mainfrom
asonawane/main-ops-mtp-fold-state

Conversation

@apsonawane

@apsonawane Akshay Sonawane (apsonawane) commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the com.microsoft::FoldMid2 and com.microsoft::FoldTail (version 1) CUDA contrib operators for speculative decoding and $S \in [2, 4]$ DFlash2 verification (squashing #33550 and removing unused MtpMoe):

  • com.microsoft::FoldMid2 (onnxruntime/contrib_ops/cuda/llm/speculative_fold.{h,cc,cu}):
    • Commits accepted speculative prefix tokens (head_rows 2 through 4, head_capacity = 262144), prepares head token IDs / cache slots / rotary slices, and updates control/context state (MayInplace or in-place 7-output DFlash2 mode) and the mapped host result ring.
  • com.microsoft::FoldTail (onnxruntime/contrib_ops/cuda/llm/speculative_fold.{h,cc,cu}):
    • Aggregates downstream validation status bits, optionally updates the configured draft-control slot (ctl_slot in {-1, 2, 8, 9}), and publishes the result-ring sequence word.

Copilot AI 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.

🟡 Changes recommended

Unresolved memory-safety, state-propagation, uninitialized-output, and documentation-validation issues block approval.

6 open issues
What changed in this PR

Adds CUDA contrib operators for multi-token prediction and speculative decoding directly on main, replacing the earlier stacked PR.

Changes:

  • Implements NVFP4 MtpMoe projections and FoldMid2/FoldTail state updates.
  • Adds schemas, CUDA registrations, and operator documentation.
  • Adds internal kernel tests; fresh GPU execution remains outstanding.
File Description
onnxruntime/​test/​contrib_ops/​cuda_kernels/​speculative_fold_test.cc Tests acceptance, state updates, and muted calls.
onnxruntime/​test/​contrib_ops/​cuda_kernels/​mtp_moe_test.cc Tests routing and reference projection arithmetic.
onnxruntime/​core/​graph/​contrib_ops/​ms_opset.h Registers the three schemas.
onnxruntime/​core/​graph/​contrib_ops/​contrib_defs.cc Defines operator contracts and types.
onnxruntime/​contrib_ops/​cuda/​llm/​speculative_fold.h Declares fold launchers.
onnxruntime/​contrib_ops/​cuda/​llm/​speculative_fold.cu Implements speculative state kernels.
onnxruntime/​contrib_ops/​cuda/​llm/​speculative_fold.cc Adds fold validation and CUDA wrappers.
onnxruntime/​contrib_ops/​cuda/​llm/​mtp_moe.h Defines dimensions and launcher interfaces.
onnxruntime/​contrib_ops/​cuda/​llm/​mtp_moe.cu Implements routing and expert projections.
onnxruntime/​contrib_ops/​cuda/​llm/​mtp_moe.cc Adds validation and output allocation.
onnxruntime/​contrib_ops/​cuda/​cuda_contrib_kernels.cc Registers CUDA implementations.
docs/​ContribOperators.md Documents the new operators.
cmake/​onnxruntime_unittests.cmake Includes internal tests and kernel sources.

Review effort: Balanced

Comment thread docs/ContribOperators.md Outdated
Comment thread onnxruntime/contrib_ops/cuda/llm/mtp_moe.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/llm/mtp_moe.cu Outdated
Comment thread onnxruntime/contrib_ops/cuda/llm/speculative_fold.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/llm/speculative_fold.cu Outdated
Comment thread onnxruntime/contrib_ops/cuda/llm/speculative_fold.cu Outdated
Keep router expert indices valid for non-finite logits, initialize the public scratch buffer, return fold state through declared reusable outputs, and guard rotary and ring metadata before committing. Add session-level and kernel regressions and regenerate operator documentation.

Copilot AI 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.

Comment thread docs/ContribOperators.md Outdated
Comment thread onnxruntime/contrib_ops/cuda/llm/mtp_moe.cu Outdated
@apsonawane

Copy link
Copy Markdown
Contributor Author

Pushed 24625bf to address both outstanding review threads and the common root cause of all six failed CUDA/TensorRT/plugin pipelines.

  • Fixed speculative-fold session test compilation: explicitly include the status assertion header and reuse typed output names for the gsl::span-based Run overload in all three calls.
  • Restore experimental operators to the correct support-level group in the generated documentation TOC.
  • Validate the documented MtpMoe 1–65535 row range before output allocation and before any low-level CUDA access; add boundary regressions.

Validation: full-PR lintrunner apply/check-only passes, git diff --check passes, affected schema/kernel documentation matches the official Python renderer with source-derived metadata, entire TOC support-level/name ordering passes, and static validation placement checks pass.

No local builds or compilation were run. The new native tests were not executed locally. Fresh CI is triggered by this push and is not claimed green.

Preserve FoldMid2/FoldTail v1, add explicit aliased state outputs and deferred ring publication, and cover S1-S4 acceptance and captured replay.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ty, optional in-place ctl/ctx) and remove unused MtpMoe

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tianleiwu
Tianlei Wu (tianleiwu) force-pushed the asonawane/main-ops-mtp-fold-state branch from efa5ab5 to a602c0c Compare October 23, 2026 23:39
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tianleiwu Tianlei Wu (tianleiwu) changed the title Add fused MTP MoE and speculative state fold CUDA operators [CUDA] Add FoldMid2 and FoldTail speculative state fold contrib operators Oct 24, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants