Skip to content

fix(tabulate): add c1 extrapolation outside tables#5601

Merged
njzjz merged 5 commits into
deepmodeling:masterfrom
njzjz:fix-tabulate-c1-extrapolation
Jun 29, 2026
Merged

fix(tabulate): add c1 extrapolation outside tables#5601
njzjz merged 5 commits into
deepmodeling:masterfrom
njzjz:fix-tabulate-c1-extrapolation

Conversation

@njzjz

@njzjz njzjz commented Jun 28, 2026

Copy link
Copy Markdown
Member

Fixes #5593.

This implements the C1 linear extrapolation suggested in #5593 (comment):

  • keep the original out-of-range coordinate and evaluate the endpoint value/slope at the proper boundary offset
  • linearly extend table values outside the covered range while keeping gradients at the constant boundary slope
  • apply the same behavior to se_a, se_r, se_t, and se_t_tebd on CPU and GPU paths
  • add focused lib tests for lower/high tails, boundary continuity, finite-difference force consistency, and zero tail curvature

Validation:

  • ruff check .
  • ruff format .
  • cmake --build source/build --target runUnitTests_lib -j2
  • source/build/lib/tests/runUnitTests_lib --gtest_filter='TestTabulateSeA.tabulate_fusion_se_a_cpu:TestTabulateSeA.tabulate_fusion_se_a_grad_cpu:TestTabulateSeR.tabulate_fusion_se_r_cpu:TestTabulateSeR.tabulate_fusion_se_r_grad_cpu:TestTabulateSeT.tabulate_fusion_se_t_cpu:TestTabulateSeT.tabulate_fusion_se_t_grad_cpu:TestTabulateSeTTebd.tabulate_fusion_se_t_tebd_cpu:TestTabulateSeTTebd.tabulate_fusion_se_t_tebd_grad_cpu:TabulateExtrapolate.*'

Note: I used the existing source/build directory reconfigured as TensorFlow-only locally because the previous cache had ENABLE_PYTORCH=ON with a CUDA PyTorch install but no CUDA toolkit available.

Summary by CodeRabbit

  • New Features

    • Tabulated SE polynomial evaluations now use an extrapolation-aware form for out-of-range inputs, improving smoothness and continuity at both lower and upper table boundaries across SE-a, SE-t, SE-t-tebd, and SE-r.
  • Bug Fixes

    • Corrected boundary/tail handling so value and gradient computations remain consistent near min/max, including off-grid max behavior; gradients now incorporate extrapolated results where needed.
  • Tests

    • Added CPU and GPU test coverage validating value/gradient/grad-grad consistency via polynomial reference comparisons and finite-difference checks for all supported SE variants.

@njzjz njzjz requested a review from wanghan-iapcm June 28, 2026 15:40
@dosubot dosubot Bot added the bug label Jun 28, 2026
@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: ff8e083f-bcf3-46f3-81ca-dd2334dbf1ea

📥 Commits

Reviewing files that changed from the base of the PR and between eed9a78 and 9c145f4.

📒 Files selected for processing (1)
  • source/lib/tests/test_tabulate_extrapolate.cc
🚧 Files skipped from review as they are similar to previous changes (1)
  • source/lib/tests/test_tabulate_extrapolate.cc

📝 Walkthrough

Walkthrough

Adds extrapolation-aware tabulate locators and polynomial helpers, rewires CPU and GPU SE-a/r/t/t-tebd kernels to use them, and adds new GTest coverage for out-of-range and boundary behavior.

Changes

Tabulate linear extrapolation

Layer / File(s) Summary
CPU locate helpers and polynomial utilities
source/lib/src/tabulate.cc
locate_xx and locate_xx_se_t gain extrapolate_delta, high-tail locator helpers are added, and degree-5 polynomial evaluation utilities are introduced.
CPU SE-a and SE-r kernels
source/lib/src/tabulate.cc
SE-a and SE-r CPU forward, gradient, and gradient-gradient paths pass extrapolate_delta and use the new polynomial helpers for values and derivatives.
CPU SE-t and SE-t-tebd kernels
source/lib/src/tabulate.cc
SE-t and SE-t-tebd CPU forward, gradient, and gradient-gradient paths pass extrapolate_delta and use the new polynomial helpers for values and derivatives.
GPU locate helpers and polynomial utilities
source/lib/src/gpu/tabulate.cu
All GPU locate_xx_se_* helpers gain extrapolate_delta, high-tail locators are added, and GPU polynomial evaluation utilities are introduced.
GPU SE-a, SE-r, SE-t, and SE-t-tebd kernels
source/lib/src/gpu/tabulate.cu
GPU forward, gradient, and gradient-gradient paths pass extrapolate_delta and use extrapolated polynomial values and gradient expressions.
Extrapolation regression tests
source/lib/tests/test_tabulate_extrapolate.cc
Reference utilities, CPU/GPU wrappers, and GTest cases check SE-a, SE-r, SE-t, and SE-t-tebd behavior at in-range and out-of-range inputs.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Suggested labels

OP

Suggested reviewers

  • wanghan-iapcm
  • OutisLi
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning For [#5593], the PR extrapolates linearly instead of masking the interpolation derivative to zero outside the table range. Either mask interpolation derivatives outside the table bounds as requested, or document that extrapolation semantics intentionally replace the clamp behavior.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.91% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main change: C1 extrapolation for tabulated values outside bounds.
Out of Scope Changes check ✅ Passed The changes stay within tabulate extrapolation logic and regression tests, with no unrelated feature work.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@source/lib/src/tabulate.cc`:
- Around line 42-45: `locate_xx()` and the table-building logic in `tabulate.cc`
assume `upper`, `max`, and the stride values form an exact grid, but the current
`int(...)` truncation and `np.arange(...)` usage can break that invariant. Add
validation or rounding so the grid counts are only accepted when `(max - upper)
/ stride1` (and the related span calculations) are integral, and make the tail
extrapolation branch use the same segment as the `xx < max` path in
`locate_xx()` so the continuation at `max` remains C1.

In `@source/lib/tests/test_tabulate_extrapolate.cc`:
- Around line 232-268: Add GPU parity coverage for the extrapolation behavior in
the tabulation tests: the current `TabulateExtrapolate` cases only verify the
`*_cpu` paths, so a regression in the GPU implementation could slip through.
Extend the existing tests or add matching GPU assertions using the same helpers
and symbols (`se_a_value`, `se_r_value`, `se_t_value`, `se_t_tebd_value`, and
their gradient counterparts) so the tail behavior is validated against the GPU
entrypoints as well.
- Around line 248-259: The SeT extrapolation test coverage is incomplete in
`SeTUsesLinearLookupTails` and `se_t_grad` validation. Extend the existing
`TEST(TabulateExtrapolate, SeTUsesLinearLookupTails)` to include the exact lower
boundary (`kLower`/`kMin`) and add an assertion that the observable derivative
contract matches `central_diff(se_t_value, xx)` by comparing it against the sum
of `dy_dem_x` and `dy_dem`. Use the existing helpers `locate_se_t`,
`expected_table_value`, `expected_table_grad`, `se_t_value`, and `se_t_grad` so
the test exercises the split derivative behavior consistently across the
boundary and tail cases.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f2a96cc4-d660-4022-8d1b-54f2b03c1f83

📥 Commits

Reviewing files that changed from the base of the PR and between a9bcbc5 and 7fd0fb1.

📒 Files selected for processing (3)
  • source/lib/src/gpu/tabulate.cu
  • source/lib/src/tabulate.cc
  • source/lib/tests/test_tabulate_extrapolate.cc

Comment thread source/lib/src/tabulate.cc
Comment thread source/lib/tests/test_tabulate_extrapolate.cc
Comment thread source/lib/tests/test_tabulate_extrapolate.cc

@coderabbitai coderabbitai 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
source/lib/tests/test_tabulate_extrapolate.cc (1)

472-528: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Add direct *_grad_grad regression coverage.

These cases only hit the forward and *_grad_* APIs. The linked bug and this PR also change the *_grad_grad_* kernels, but nothing here invokes them, so a second-derivative regression can still pass as long as grad remains correct. Please add CPU/GPU wrappers for the se_a, se_r, se_t, and se_t_tebd grad-grad entrypoints and assert the out-of-range interpolation curvature is masked there too.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@source/lib/tests/test_tabulate_extrapolate.cc` around lines 472 - 528, The
current tests in TabulateExtrapolate only exercise the forward and grad paths,
so they miss regressions in the grad-grad kernels. Add CPU and GPU test coverage
for the se_a, se_r, se_t, and se_t_tebd *_grad_grad_* entrypoints, using the
same out-of-range boundary/tail cases already covered here. Reuse the existing
helpers such as expect_linear_tail, expect_boundary, locate_se_a_or_r, and
locate_se_t to verify the second-derivative behavior is masked correctly for
both boundary and extrapolated inputs.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@source/lib/tests/test_tabulate_extrapolate.cc`:
- Around line 472-528: The current tests in TabulateExtrapolate only exercise
the forward and grad paths, so they miss regressions in the grad-grad kernels.
Add CPU and GPU test coverage for the se_a, se_r, se_t, and se_t_tebd
*_grad_grad_* entrypoints, using the same out-of-range boundary/tail cases
already covered here. Reuse the existing helpers such as expect_linear_tail,
expect_boundary, locate_se_a_or_r, and locate_se_t to verify the
second-derivative behavior is masked correctly for both boundary and
extrapolated inputs.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a06d52d0-af9f-48bc-9d4c-cb17a65bccf6

📥 Commits

Reviewing files that changed from the base of the PR and between 7fd0fb1 and 76520b1.

📒 Files selected for processing (3)
  • source/lib/src/gpu/tabulate.cu
  • source/lib/src/tabulate.cc
  • source/lib/tests/test_tabulate_extrapolate.cc
🚧 Files skipped from review as they are similar to previous changes (2)
  • source/lib/src/gpu/tabulate.cu
  • source/lib/src/tabulate.cc

@codecov

codecov Bot commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.47826% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.41%. Comparing base (a9bcbc5) to head (9c145f4).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
source/lib/tests/test_tabulate_extrapolate.cc 92.65% 17 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5601      +/-   ##
==========================================
+ Coverage   82.35%   82.41%   +0.06%     
==========================================
  Files         896      903       +7     
  Lines      100952   101844     +892     
  Branches     4059     4071      +12     
==========================================
+ Hits        83138    83936     +798     
- Misses      16349    16439      +90     
- Partials     1465     1469       +4     

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

Comment thread source/lib/tests/test_tabulate_extrapolate.cc
@njzjz njzjz requested a review from wanghan-iapcm June 29, 2026 05:55

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@source/lib/tests/test_tabulate_extrapolate.cc`:
- Around line 663-676: The current tail test only covers the CPU grad_grad path,
so add matching GPU grad_grad tail assertions in TabulateExtrapolate using the
existing se_a/se_r/se_t helpers and the *_grad_grad_gpu entrypoints. Extend
GradGradKernelsMatchDyFiniteDifferenceInTails to exercise the GPU
second-derivative kernels at the same xx tail values, mirroring the existing CPU
finite-difference checks so regressions in the GPU tabulate path are caught.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 45b8d38e-3dde-45ce-b8fb-7427fcfb51a9

📥 Commits

Reviewing files that changed from the base of the PR and between 76520b1 and 2209936.

📒 Files selected for processing (1)
  • source/lib/tests/test_tabulate_extrapolate.cc

Comment thread source/lib/tests/test_tabulate_extrapolate.cc

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (2)
source/lib/tests/test_tabulate_extrapolate.cc (2)

290-300: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add a tail check for se_a grad-grad with two_embed.

Line 299 always passes two_embed = nullptr, so the grad-grad attention branch with dz_dy_dtwo is still untested even though the forward/grad two-embed tail path is now covered.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@source/lib/tests/test_tabulate_extrapolate.cc` around lines 290 - 300, The
se_a grad-grad helper in test_tabulate_extrapolate.cc only exercises the
non-two_embed path because se_a_grad_grad_dy always passes nullptr for
two_embed, leaving the dz_dy_dtwo branch untested. Add a dedicated test case
using deepmd::tabulate_fusion_se_a_grad_grad_cpu in se_a_grad_grad_dy (or a
sibling helper) that supplies a real two_embed buffer and asserts the tail
behavior for the attention grad-grad path, so the two_embed branch is covered
alongside the existing em/em_x checks.

663-676: 🎯 Functional Correctness | 🔵 Trivial | 🏗️ Heavy lift

Mirror these grad-grad tail checks on GPU.

This new regression test only exercises CPU grad-grad entrypoints, while the PR also rewires GPU grad-grad kernels. Add CUDA/ROCm variants so CPU/GPU parity covers the same tail derivative contract.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@source/lib/tests/test_tabulate_extrapolate.cc` around lines 663 - 676, The
grad-grad tail regression currently covers only the CPU entrypoints in
GradGradKernelsMatchDyFiniteDifferenceInTails, so extend it to exercise the
corresponding GPU kernels as well. Reuse the same tail points and
finite-difference helper, but add CUDA/ROCm-specific checks for the matching
grad-grad symbols so CPU and GPU parity is validated against the same derivative
contract.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@source/lib/tests/test_tabulate_extrapolate.cc`:
- Around line 290-300: The se_a grad-grad helper in test_tabulate_extrapolate.cc
only exercises the non-two_embed path because se_a_grad_grad_dy always passes
nullptr for two_embed, leaving the dz_dy_dtwo branch untested. Add a dedicated
test case using deepmd::tabulate_fusion_se_a_grad_grad_cpu in se_a_grad_grad_dy
(or a sibling helper) that supplies a real two_embed buffer and asserts the tail
behavior for the attention grad-grad path, so the two_embed branch is covered
alongside the existing em/em_x checks.
- Around line 663-676: The grad-grad tail regression currently covers only the
CPU entrypoints in GradGradKernelsMatchDyFiniteDifferenceInTails, so extend it
to exercise the corresponding GPU kernels as well. Reuse the same tail points
and finite-difference helper, but add CUDA/ROCm-specific checks for the matching
grad-grad symbols so CPU and GPU parity is validated against the same derivative
contract.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 45b8d38e-3dde-45ce-b8fb-7427fcfb51a9

📥 Commits

Reviewing files that changed from the base of the PR and between 76520b1 and 2209936.

📒 Files selected for processing (1)
  • source/lib/tests/test_tabulate_extrapolate.cc

@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Jun 29, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 29, 2026
@njzjz njzjz added this pull request to the merge queue Jun 29, 2026
Merged via the queue into deepmodeling:master with commit 1550599 Jun 29, 2026
70 checks passed
@njzjz njzjz deleted the fix-tabulate-c1-extrapolation branch June 29, 2026 18:05
SchrodingersCattt pushed a commit to SchrodingersCattt/deepmd-kit that referenced this pull request Jun 30, 2026
Fixes deepmodeling#5593.

This implements the C1 linear extrapolation suggested in
deepmodeling#5593 (comment):

- keep the original out-of-range coordinate and evaluate the endpoint
value/slope at the proper boundary offset
- linearly extend table values outside the covered range while keeping
gradients at the constant boundary slope
- apply the same behavior to se_a, se_r, se_t, and se_t_tebd on CPU and
GPU paths
- add focused lib tests for lower/high tails, boundary continuity,
finite-difference force consistency, and zero tail curvature

Validation:

- ruff check .
- ruff format .
- cmake --build source/build --target runUnitTests_lib -j2
- source/build/lib/tests/runUnitTests_lib
--gtest_filter='TestTabulateSeA.tabulate_fusion_se_a_cpu:TestTabulateSeA.tabulate_fusion_se_a_grad_cpu:TestTabulateSeR.tabulate_fusion_se_r_cpu:TestTabulateSeR.tabulate_fusion_se_r_grad_cpu:TestTabulateSeT.tabulate_fusion_se_t_cpu:TestTabulateSeT.tabulate_fusion_se_t_grad_cpu:TestTabulateSeTTebd.tabulate_fusion_se_t_tebd_cpu:TestTabulateSeTTebd.tabulate_fusion_se_t_tebd_grad_cpu:TabulateExtrapolate.*'

Note: I used the existing source/build directory reconfigured as
TensorFlow-only locally because the previous cache had ENABLE_PYTORCH=ON
with a CUDA PyTorch install but no CUDA toolkit available.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Tabulated SE polynomial evaluations now use an extrapolation-aware
form for out-of-range inputs, improving smoothness and continuity at
both lower and upper table boundaries across SE-a, SE-t, SE-t-tebd, and
SE-r.

* **Bug Fixes**
* Corrected boundary/tail handling so value and gradient computations
remain consistent near min/max, including off-grid max behavior;
gradients now incorporate extrapolated results where needed.

* **Tests**
* Added CPU and GPU test coverage validating value/gradient/grad-grad
consistency via polynomial reference comparisons and finite-difference
checks for all supported SE variants.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

Tabulate grad kernels ignore clamp derivative outside table data range

2 participants