Skip to content

fix(triton): validate grid-pair launches before graph capture - #6095

Open
OutisLi wants to merge 1 commit into
deepmodeling:masterfrom
OutisLi:pr/grid-pair-capture-validation
Open

OutisLi wants to merge 1 commit into
deepmodeling:masterfrom
OutisLi:pr/grid-pair-capture-validation

Conversation

@OutisLi

@OutisLi OutisLi commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Validate Triton grid-pair launch resources before graph capture so cold compiled force training can select a feasible tile or retain the reference composition.

Previously, wrap_triton could record a call during fake-tensor tracing without loading its kernel. The launch retry loop treated successful tracing as successful resource validation and cached an invalid tile. The actual shared-memory error then arrived during Inductor backward compilation, outside the retry loop. An eager warmup could hide this failure.

Implementation

  • Separate candidate selection from execution. Compile candidates with dtype-only warmup inputs and load their handles to check device resource limits without allocating inputs or executing kernels.
  • Share specialization parameters across validation and execution, and reuse the selector for forward, first backward, and force-loss second backward.
  • Cache selections by kernel, device, static geometry, operand signature, and accumulator count. Keep pair counts and physical strides dynamic; cache unsuccessful searches as well.
  • Validate existing tuned choices through the same path. Only resource-limit errors reject a candidate; unrelated compiler errors remain visible. Preserve compiler-visible Triton operations and the reference composition when no candidate fits.

Validation on this branch

Based on master d07c097b; this PR contains one cherry-picked commit with an identical patch to the source change. It does not depend on the ASE or native-spin PRs.

Validation uses an NVIDIA RTX PRO 6000 Blackwell Server Edition with PyTorch 2.13.0+cu130 and Triton 3.7.1:

  • A process-local reproduction using the exact parent _launch implementation, with repository files unchanged, fails the cold FP32 force-backward regression: shared memory required is 118784 bytes, exceeding the 101376-byte device limit.
  • All 20 tests in source/tests/pt_expt/kernels/test_grid_pair_train.py pass in a fresh process on the unmodified branch, using separate compiler caches from the parent-launcher reproduction.
  • Coverage includes FP32/BF16 output and first/second-gradient checks against the float64 reference, dynamic layouts, and five cold-compile regressions. The new cases cover both production layouts, changing node counts with a single compiled graph, and a forced reference path.
  • Independent source review found no actionable issues. Scoped pre-commit hooks, repository Ruff, formatting checks, patch-identity checks, and whitespace checks passed.

Other GPU architectures and the project-pinned PyTorch 2.10 environment were not exercised. The original commit's full-training and layout-sweep results are historical and were not repeated for this PR. No throughput improvement is claimed.

Summary by CodeRabbit

  • Performance

    • Improved compiled execution for grid-pair operations by selecting suitable launch configurations before runtime.
    • Prioritizes optimized configurations for supported hardware while retaining bounded fallback options.
  • Reliability

    • Added resource validation to prevent unsuitable configurations from launching.
    • Preserves eager fallback behavior when no compiled configuration is viable.
  • Compatibility

    • Improved support for compiled higher-order gradients across dynamic batch shapes and float32 or bfloat16 autocast workflows.

Cold compiled FP32 force training can cache a grid-pair launch that exceeds
per-block shared memory. wrap_triton records a call during fake-tensor
tracing without loading the kernel, so the eager launch loop mistakes a
successful trace for a resource-validated configuration. The actual
OutOfResources exception arrives later in Inductor's backward compilation,
outside the retry loop. Reducing the training batch cannot fix the tile's
fixed resource footprint.

For preset Pro on RTX PRO 6000 Blackwell, the second-order kernel initially
requests 118784 bytes against a 101376-byte device limit. Eager execution
finds a smaller grid tile requiring 79872 bytes, but cold graph capture
cannot take that retry. Prewarming eagerly can consequently hide the bug.

Resolve launch feasibility before recording or executing a kernel:
- Separate candidate selection from the runtime launch and use the same
  specialization parameters for validation and execution.
- Compile candidates with Triton warmup using operand dtypes instead of
  tensor allocations, then load the compiled handles to enforce the target
  device's resource limits without executing the kernel.
- Represent runtime pair counts and physical strides by non-unit scalar
  placeholders during validation. Keep their actual values in the launch
  ABI so dynamic batches and degree-major views retain one compiled graph.
- Cache validated choices by kernel, device, static geometry, complete
  operand signature and differentiation-order accumulator count. Cache an
  unsuccessful search as well, avoiding repeated compilation for layouts
  served by the reference composition.
- Retain exact-device tuned configurations as preferred candidates, while
  subjecting them to the same validation and deduplicating fallback choices.
  Forward, first backward and force-loss second backward share the selector.
- Preserve compiler-visible Triton operators and the reference einsum path
  when no candidate fits. Only resource-limit errors reject a candidate;
  other compilation failures remain visible.

Add cold compiled force-loss regressions for the two production Pro grid
layouts in FP32 and BF16 autocast. Exercise batch-dependent physical strides
with changing node counts, compare outputs and second-order gradients with
eager execution, and verify that only one graph is compiled. Clear launch
selection state and disable the FX graph cache so prior eager tests cannot
mask an invalid first choice. Cover reference fallback through compilation
when no fused launch is available.

Validation on RTX PRO 6000 Blackwell Server Edition with
PyTorch 2.13.0+cu130 and Triton 3.7.1:
- All 20 grid-pair tests pass, including existing FP32/BF16 forward and
  first/second-gradient checks against the float64 reference, dynamic layout
  tests, and the five cold-compile/fallback regressions.
- A cold FP32 second-order reproducer with one graph node passes without
  an eager warmup. The validated and final compiled backward kernels agree on
  their resource footprint; the Pro second-order tile uses 79872 bytes.
- A preset Pro fine-tuning smoke run completes distributed precompilation
  and two optimizer steps with one DDP rank, ZeRO Stage 1, a 300-atom batch
  budget, AMP/TF32 disabled, and both CUDA/Triton training gates enabled.
  All 278 floating-point model tensors in the saved checkpoint are finite.
- An independent review compares 120 actual layout signatures across five
  geometries, two dtypes, three differentiation orders and four storage
  layouts. Their shared-memory usage matches the validation signatures.
- Ruff and whitespace checks pass. Other GPU architectures and the
  project-pinned PyTorch 2.10 environment were not exercised.
Copilot AI balanced review requested due to automatic review settings October 21, 2026 08:11

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@coderabbitai

coderabbitai Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a32bdf7e-c4cf-4f24-bed7-cd4d3374441c
📥 Commits

Reviewing files that changed from the base of the PR and between d07c097 and 1351a4f.

📒 Files selected for processing: 2
  • deepmd/pt_expt/kernels/triton/sezm/grid_pair.py
  • source/tests/pt_expt/kernels/test_grid_pair_train.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

📝 Walkthrough

The change validates Triton launch configurations with warmup before execution. If no candidate passes resource validation, _launch raises _NoViableConfig. New tests cover compiled outputs and gradients across dynamic batch shapes and input modes.

Changes: Grid pair launch configuration

Layer / File(s) Summary
Select and execute a validated launch
deepmd/pt_expt/kernels/triton/sezm/grid_pair.py
Typed helpers build launch arguments and candidate configurations. Cached selection compiles candidates with Triton warmup and validates resource handles. Execution uses the selected configuration instead of retrying launches; no viable candidate raises _NoViableConfig.
Validate cold compiled backward passes
source/tests/pt_expt/kernels/test_grid_pair_train.py
Parameterized tests compare compiled outputs and gradients with eager results across float32 and bfloat16 modes, input layouts, and batch sizes. The tests also cover the reference fallback and require one compilation.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Launch as grid_pair._launch
  participant Select as grid_pair._select_launch_config
  participant Warmup as Triton warmup
  participant Kernel as fused kernel
  participant Fallback as _eager_packed
  Launch->>Select: Provide shape key and typed signature
  Select->>Warmup: Compile and validate candidate resources
  Warmup-->>Select: Return resource result
  Select-->>Launch: Return selected configuration or None
  alt Configuration selected
    Launch->>Kernel: Execute with validated configuration and shared launch arguments
  else No viable configuration
    Launch-->>Fallback: Raise _NoViableConfig for eager fallback
  end
Loading

Suggested reviewers: wanghan-iapcm

Merge Risk: ⚪ Minimal · up to 1351a

No actionable failure is established, so the change is mergeable after normal checks. Validation on the pinned GPU environment remains worthwhile.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 64.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: validating Triton grid-pair launches before graph capture.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@njzjz-bot njzjz-bot 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.

Agent: dot

Source LGTM at 1351a4f. Reviewed both changed files and the forward/first-backward/second-backward integration, including the no-viable-config reference paths. Resource selection now occurs before recording wrap_triton; tuned choices receive the same validation, only OutOfResources triggers retries, and successful/unsuccessful choices are cached without putting node counts or physical strides into the selector key. Shared launch kwargs keep the static geometry and launch settings consistent. The five added cold-compile cases cover both layouts, FP32/BF16, changing node counts, force-loss differentiation and a forced reference path.

Bounded independent checks: exact-source helper extraction with test doubles passed preferred-tile rejection at handle loading, positive/negative caching, shared validation kwargs, propagation of unrelated compiler errors, and slot splitting. Also inspected Triton 3.6.0 upstream warmup and _init_handles implementations for the dtype-only compilation/resource-check interfaces. These checks are not GPU execution or an end-to-end pinned-environment qualification.

Reusing the PR's reported fresh-process 20-test Blackwell / PyTorch 2.13 / Triton 3.7.1 results rather than requesting another full GPU run. My executor has no PyTorch installed; I did not independently reproduce GPU numerics, pinned PyTorch 2.10 behavior, other architectures or throughput. Those remain explicitly unverified, not newly observed failures. Current CUDA CI is successful; Python and C++ test workflows are still running. No actionable source defect found in this scope.

@njzjz-bot njzjz-bot 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.

Agent: dot

Qualification clarification for my review above: the Pass testing on CUDA aggregate is successful, but the actual Test Python on CUDA and Test C++ on CUDA jobs were skipped. Thus this CI result is not an independently executed GPU test. GPU evidence remains the author's reported Blackwell results, with the independent-validation limitations already stated. The source LGTM is unchanged.

@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 17.94872% with 32 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.78%. Comparing base (d07c097) to head (1351a4f).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
deepmd/pt_expt/kernels/triton/sezm/grid_pair.py 17.94% 32 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6095      +/-   ##
==========================================
- Coverage   78.08%   77.78%   -0.31%     
==========================================
  Files        1171     1171              
  Lines      141661   141677      +16     
  Branches     5079     5078       -1     
==========================================
- Hits       110620   110199     -421     
- Misses      29103    29540     +437     
  Partials     1938     1938              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants