Skip to content

fix(jax): materialize default fparam in cxx api#5849

Open
njzjz-bot wants to merge 1 commit into
deepmodeling:masterfrom
njzjz-bot:fix/jax-default-fparam-cxx-api-5658
Open

fix(jax): materialize default fparam in cxx api#5849
njzjz-bot wants to merge 1 commit into
deepmodeling:masterfrom
njzjz-bot:fix/jax-default-fparam-cxx-api-5658

Conversation

@njzjz-bot

@njzjz-bot njzjz-bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Closes #5658.

Summary

  • load and validate get_default_fparam when initializing a JAX SavedModel in the C++ API
  • materialize either caller-provided or stored frame parameters, including one-frame-to-multiframe broadcasting, in both direct and neighbor-list paths
  • reject inconsistent tensor data/shape combinations, cast tensor rank explicitly, and report TensorFlow C API allocation failure before accessing tensor storage
  • generate a JAX default-fparam fixture and extend the shared C++ regression matrix with default, distinct explicit override, float/double, neighbor-list, multiframe, and invalid-size coverage

Why existing tests missed this

The shared C++ default-fparam suite only included TorchScript and PT2 artifacts. Existing JAX C++ fixtures either had dim_fparam == 0 or supplied frame parameters explicitly, while Python JAX inference materializes the saved default before invoking the model. Serialization coverage therefore proved that the flag/getter were exported, but never exercised C++ consumption of an omitted JAX fparam.

The explicit regression now uses fparam=[0.5], distinct from the stored default [0.25852028], so it also detects an implementation that ignores caller overrides.

Validation

  • ruff format .
  • ruff check .
  • clang-format --dry-run --Werror on all changed C++ files
  • TensorFlow/JAX-only C++ build of runUnitTests_cc and deepmd_backend_jax; both targets rebuilt successfully after the allocation-safety follow-up
  • 11 focused DefaultFParamDeepPotTest JAX SavedModel cases passed, covering direct and neighbor-list inference, float/double, stored defaults, distinct explicit overrides, two-frame broadcasting, metadata, and invalid sizes

Coding agent: Codex
Codex version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning effort: xhigh

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of frame parameters for JAX models, including embedded defaults and per-frame inputs.
    • Added validation for missing, incorrectly sized, or overflowing tensor and frame-parameter data.
    • Added support for broadcasting frame parameters across multiple frames.
    • Improved error reporting for invalid input shapes and unavailable defaults.
  • Tests

    • Expanded coverage for JAX frame-parameter defaults, overrides, broadcasting, and invalid inputs.
    • Added JAX SavedModel test data and reference results.

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

DeepPotJAX now loads embedded default frame parameters, validates tensor shapes and sizes, normalizes explicit or default fparam inputs, and applies the behavior across both compute paths. Generated JAX SavedModel fixtures and regression tests cover broadcasting, explicit overrides, and invalid input sizes.

Changes

JAX frame-parameter handling

Layer / File(s) Summary
Tensor and fparam normalization
source/api_cc/include/DeepPotJAX.h, source/api_cc/src/DeepPotJAX.cc
Adds stored default frame parameters, validates tensor dimensions and byte sizes, and normalizes full or per-frame fparam inputs.
Default loading and compute integration
source/api_cc/src/DeepPotJAX.cc
Loads and validates embedded defaults during initialization and routes both compute overloads through make_fparam_input.
Generated fixtures and regression coverage
source/tests/infer/gen_fparam_aparam.py, source/api_cc/tests/test_deeppot_universal.cc
Generates a JAX SavedModel and override references, then tests explicit values, broadcasting, LAMMPS neighbor-list paths, and invalid sizes.

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

Sequence Diagram(s)

sequenceDiagram
  participant DeepPotJAX
  participant JAXSavedModel
  participant make_fparam_input
  participant create_tensor
  DeepPotJAX->>JAXSavedModel: load model metadata and default_fparam
  JAXSavedModel-->>DeepPotJAX: return default values
  DeepPotJAX->>make_fparam_input: normalize supplied or default fparam
  make_fparam_input->>create_tensor: create validated model input tensor
  create_tensor-->>JAXSavedModel: provide fparam tensor for inference
Loading

Possibly related PRs

Suggested labels: bug, C++, Python, LAMMPS

Suggested reviewers: wanghan-iapcm

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR loads, stores, and materializes default fparam values, supports broadcasting, and validates tensor shapes as requested in #5658.
Out of Scope Changes check ✅ Passed The added tests, fixture generation, and tensor safety checks are all directly tied to the default fparam JAX fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: fixing JAX C++ API handling to materialize default fparam values.
✨ 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: 1

🧹 Nitpick comments (1)
source/api_cc/tests/test_deeppot_universal.cc (1)

1994-2050: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover the neighbor-list path in the JAX-specific regressions.

These tests exercise multiframe broadcasting and invalid-size rejection only through direct dp.compute. Add equivalent neighbor-list cases so regressions in that separate overload cannot pass unnoticed.

🤖 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/api_cc/tests/test_deeppot_universal.cc` around lines 1994 - 2050,
Extend the JAX-specific regression coverage in DefaultFParamDeepPotTest by
adding neighbor-list overload cases equivalent to
JAXBroadcastsFParamAcrossFrames and JAXRejectsInvalidFParamSize. Use the
existing multiframe/default-versus-override expectations and invalid fparam-size
assertion, but invoke the neighbor-list compute path so both broadcasting and
rejection are validated there.
🤖 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/api_cc/src/DeepPotJAX.cc`:
- Around line 443-469: Update the tensor creation code around TF_AllocateTensor
to cast shape.size() explicitly to the expected integer type, then check whether
TF_AllocateTensor returned nullptr before accessing TF_TensorData. Throw a
deepmd::deepmd_exception with an allocation-failure message when allocation
fails, while preserving the existing copy behavior for successful allocations.

---

Nitpick comments:
In `@source/api_cc/tests/test_deeppot_universal.cc`:
- Around line 1994-2050: Extend the JAX-specific regression coverage in
DefaultFParamDeepPotTest by adding neighbor-list overload cases equivalent to
JAXBroadcastsFParamAcrossFrames and JAXRejectsInvalidFParamSize. Use the
existing multiframe/default-versus-override expectations and invalid fparam-size
assertion, but invoke the neighbor-list compute path so both broadcasting and
rejection are validated there.
🪄 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: 489a8196-07d7-4e0e-a644-db1f89845485

📥 Commits

Reviewing files that changed from the base of the PR and between 6c3b985 and d23eb87.

📒 Files selected for processing (4)
  • source/api_cc/include/DeepPotJAX.h
  • source/api_cc/src/DeepPotJAX.cc
  • source/api_cc/tests/test_deeppot_universal.cc
  • source/tests/infer/gen_fparam_aparam.py

Comment thread source/api_cc/src/DeepPotJAX.cc
Load and validate default frame parameters from JAX SavedModels, materialize stored or caller-provided values for direct and neighbor-list inference, and broadcast one-frame values across multiple frames.

Validate TensorFlow C API tensor shapes and sizes before allocation, cast rank explicitly, and report allocation failure before accessing tensor storage. Cover default and override behavior, float and double paths, multiframe broadcasting, neighbor lists, and invalid sizes.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
@njzjz-bot
njzjz-bot force-pushed the fix/jax-default-fparam-cxx-api-5658 branch from d23eb87 to ffd8a5c Compare July 17, 2026 01:46
@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 65.28926% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.30%. Comparing base (6c3b985) to head (ffd8a5c).

Files with missing lines Patch % Lines
source/api_cc/src/DeepPotJAX.cc 56.52% 20 Missing and 10 partials ⚠️
source/api_cc/tests/test_deeppot_universal.cc 76.92% 9 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5849      +/-   ##
==========================================
- Coverage   78.58%   78.30%   -0.28%     
==========================================
  Files        1050     1050              
  Lines      120637   120753     +116     
  Branches     4356     4381      +25     
==========================================
- Hits        94801    94557     -244     
- Misses      24278    24616     +338     
- Partials     1558     1580      +22     

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

@njzjz

njzjz commented Jul 18, 2026

Copy link
Copy Markdown
Member

Possible reviewers based on changed lines, exact file history, and exact-file review history:

  • @wanghan-iapcm — 9 commits on changed files; 40 reviews on exact changed files (source/api_cc/include/DeepPotJAX.h, source/api_cc/src/DeepPotJAX.cc, source/api_cc/tests/test_deeppot_universal.cc, source/tests/infer/gen_fparam_aparam.py).
  • @iProzd — 14 reviews on exact changed files (source/api_cc/include/DeepPotJAX.h, source/api_cc/src/DeepPotJAX.cc, source/api_cc/tests/test_deeppot_universal.cc, source/tests/infer/gen_fparam_aparam.py).

No review request was made automatically.

Coding agent: Codex
Codex version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning effort: xhigh

@njzjz
njzjz requested review from iProzd and wanghan-iapcm July 18, 2026 07:20
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.

[Code scan] Materialize JAX default fparam in the C++ API

2 participants