Skip to content

fix(tf): avoid empty AMix neighbor mapping pointers#5791

Draft
njzjz wants to merge 4 commits into
deepmodeling:masterfrom
njzjz:codex/code-scan-5653
Draft

fix(tf): avoid empty AMix neighbor mapping pointers#5791
njzjz wants to merge 4 commits into
deepmodeling:masterfrom
njzjz:codex/code-scan-5653

Conversation

@njzjz

@njzjz njzjz commented Jul 14, 2026

Copy link
Copy Markdown
Member

Summary

  • pass the neighbor-index mapping only when periodic coordinate expansion populated it;
  • pass nullptr for non-mapping modes instead of forming an element pointer from an empty vector;
  • apply the contract to both the standard TensorFlow AMix CPU fallback and the NVNMD quantized AMix CPU path;
  • add direct CPU path coverage for external/LAMMPS neighbor lists and NVNMD no-PBC neighbor construction.

Why existing tests missed this

The ordinary AMix CPU no-PBC and PBC modes now use a batch fast path, so the affected legacy fallback is reached primarily by the external pointer-based neighbor-list mode. There was no direct test for that AMix path. The NVNMD AMix path also had no direct no-PBC test.

In release libstdc++, taking element zero from an empty vector can appear to work when the callee does not dereference the pointer, so higher-level numerical tests do not reliably expose the undefined behavior. The defect is diagnosed by static analysis or a libstdc++ debug/sanitizer build. The added tests ensure both affected call sites remain exercised and validate their neighbor indices, types, masks, valid entries, and padding.

Validation

  • TensorFlow-only deepmd_op target compiled and linked successfully against TensorFlow 2.21;
  • the two focused tests loaded the newly built op: 2 passed;
  • the full existing prod-env-mat test file plus the initial NVNMD coverage: 6 passed during development;
  • libstdc++ debug-mode reproduction confirms the pre-fix empty-vector expression aborts;
  • clang-format applied to changed C++ files;
  • ruff format .;
  • ruff check .;
  • git diff --check.

Closes #5653.

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

Summary by CodeRabbit

  • Bug Fixes

    • Fixed CPU neighbor/type/mask post-processing so neighbor-list index mapping is only provided when it’s valid, preventing unsafe pointer usage across AMix CPU modes (including NVNMD and external neighbor list scenarios).
  • Tests

    • Added new AMix test coverage for NVNMD without periodic boundaries, PBC/NVNMD remapping when neighbors are mapped, and an external (legacy ABI) neighbor list path.
    • Extended CI to run additional TensorFlow-gated AMix neighbor mapping tests with hardened C++ standard library settings when leak checks are enabled.

Pass a neighbor-index mapping only when periodic coordinate expansion actually populated it. Non-PBC, external, and tensor-provided neighbor-list modes now pass nullptr instead of forming an invalid pointer from an empty vector.

Add direct CPU coverage for the standard external-pointer AMix path and the NVNMD no-PBC path, including neighbor indices, types, masks, valid entries, and padding.

Fixes deepmodeling#5653.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.1
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
Copilot AI review requested due to automatic review settings July 14, 2026 17:26

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.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

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: a857adef-452b-4b14-8f4b-e21a5a0dc911

📥 Commits

Reviewing files that changed from the base of the PR and between 54abd29 and 64260c5.

📒 Files selected for processing (1)
  • .github/workflows/test_cc.yml
🚧 Files skipped from review as they are similar to previous changes (1)
  • .github/workflows/test_cc.yml

📝 Walkthrough

Walkthrough

The AMix CPU paths now pass nullptr when no neighbor-list mapping exists and use idx_mapping.data() when mapping is enabled. Tests cover NVNMD no-PBC and PBC remapping, external neighbor lists, and hardened libstdc++ execution.

Changes

AMix mapping safety

Layer / File(s) Summary
CPU mapping pointer handling
source/op/tf/prod_env_mat_multi_device.cc, source/op/tf/prod_env_mat_multi_device_nvnmd.cc
Both CPU paths call _map_nei_info_cpu unconditionally and pass a mapping buffer only when b_nlist_map is enabled.
AMix execution-mode coverage
source/tests/tf/test_prod_env_mat.py
Adds tests for NVNMD no-PBC and PBC remapping outputs, plus external neighbor-list indices, masks, and types.
Hardened CI validation
.github/workflows/test_cc.yml
Enables _GLIBCXX_ASSERTIONS with leak sanitization and runs targeted AMix mapping tests under the hardened configuration.

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

Possibly related PRs

Suggested labels: Core, C++

Suggested reviewers: iprozd, wanghan-iapcm

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 summarizes the main AMix CPU pointer fix and matches the change set.
Linked Issues check ✅ Passed The AMix CPU pointer fix is implemented in both standard and NVNMD paths, with tests and sanitizer coverage matching issue #5653.
Out of Scope Changes check ✅ Passed The added tests and CI hardening directly support the pointer fix and regression coverage.
✨ 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.

@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.44%. Comparing base (7d5ad38) to head (cb556ea).
⚠️ Report is 14 commits behind head on master.

Files with missing lines Patch % Lines
source/op/tf/prod_env_mat_multi_device.cc 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5791      +/-   ##
==========================================
- Coverage   79.85%   78.44%   -1.41%     
==========================================
  Files        1022     1053      +31     
  Lines      117351   120891    +3540     
  Branches     4313     4383      +70     
==========================================
+ Hits        93706    94830    +1124     
- Misses      22101    24503    +2402     
- Partials     1544     1558      +14     

☔ 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 requested review from iProzd and wanghan-iapcm July 14, 2026 22:59
wanghan-iapcm
wanghan-iapcm previously approved these changes Jul 15, 2026

@wanghan-iapcm wanghan-iapcm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Correct fix. Forming &idx_mapping[0] on the empty mapping vector (non-PBC/external-nlist modes) is UB and aborts under a hardened libstdc++ (_GLIBCXX_ASSERTIONS), even though release builds tolerate it because the pointer is never dereferenced. Passing idx_mapping.data() only when b_nlist_map is set, else nullptr, is safe: I confirmed use_nei_info_cpu (neighbor_list.cc) dereferences nlist_map exclusively inside the if(b_nlist_map) branch, so nullptr in the false case is never touched. Both the standard AMix and NVNMD AMix call sites are fixed. One non-blocking note: the new tests exercise the two affected fallbacks (good coverage) and abort pre-fix under a debug/hardened libstdc++, but deepmd CI runs only -fsanitize=leak, which won't catch this UB -- so they won't guard against reintroduction in CI unless a _GLIBCXX_ASSERTIONS or ASAN job is added. LGTM.

@wanghan-iapcm
wanghan-iapcm dismissed their stale review July 15, 2026 15:38

Dismissing to re-review through the /code-review skill per process.

@wanghan-iapcm wanghan-iapcm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed via the code-review skill. The fix is correct and complete: forming &idx_mapping[0] on the empty mapping vector is UB (aborts under a hardened libstdc++), and passing idx_mapping.data() only when b_nlist_map is set else nullptr is safe -- use_nei_info_cpu (neighbor_list.cc) dereferences nlist_map exclusively inside if(b_nlist_map). Both AMix call sites are fixed and there are no missed siblings (the other &idx_mapping[0] sites are wrapped in if(b_nlist_map){}). Two non-blocking notes: (1) the NVNMD site's b_nlist_map==true branch is only covered indirectly by test_nvnmd_entrypoints; a direct PBC NVNMD AMix test would make that explicit. (2) The new tests are coverage-only -- pre-fix and post-fix produce identical output in a release build, and CI runs only -fsanitize=leak, so a reintroduction of this exact UB wouldn't fail in CI without a _GLIBCXX_ASSERTIONS/ASan job. Neither blocks; the fix itself is provably correct. LGTM.

@njzjz-bot

Copy link
Copy Markdown
Contributor

Reviewed the two optional follow-ups. The direct PBC NVNMD path is currently covered indirectly by the entrypoint suite, while a focused operator fixture would require the NVNMD reference/config setup; the UB regression itself is now covered by the affected fallback tests. I left the CI sanitizer-policy expansion out of this focused fix.

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

Exercise the periodic NVNMD AMix remapping branch directly and run both affected AMix paths in the C++ sanitizer matrix with libstdc++ bounds assertions enabled.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
@njzjz-bot

Copy link
Copy Markdown
Contributor

I addressed both non-blocking coverage notes in 54abd2932.

  • Added a direct periodic NVNMD AMix test that exercises b_nlist_map == true, verifies copied periodic neighbors are remapped into the original atom range, and checks neighbor types/masks.
  • Enabled _GLIBCXX_ASSERTIONS in the existing C++ sanitizer matrix and added a focused step that runs both the no-PBC and PBC NVNMD AMix regressions against the hardened custom op. This makes a reintroduced empty-vector operator[] access fail in CI instead of remaining release-build-only coverage.

The focused PBC test passed locally. Ruff, workflow formatting, and repository hooks also passed; the hardened execution is delegated to CI.

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

@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 @.github/workflows/test_cc.yml:
- Around line 90-95: Add the standard AMix test `test_external_nlist_amix` to
the pytest invocation in the “Test AMix neighbor mapping with hardened
libstdc++” workflow step, alongside the existing NVNMD tests, while preserving
the LD_LIBRARY_PATH configuration and current test selection.
🪄 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: b6bd332d-25cd-4a8f-8eab-fa58d62af6b6

📥 Commits

Reviewing files that changed from the base of the PR and between e7884e4 and 54abd29.

📒 Files selected for processing (2)
  • .github/workflows/test_cc.yml
  • source/tests/tf/test_prod_env_mat.py

Comment thread .github/workflows/test_cc.yml Outdated
Run the standard external-neighbor-list AMix test alongside both NVNMD mapping cases in the libstdc++ assertions job.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
@njzjz
njzjz enabled auto-merge July 16, 2026 15:48
@njzjz
njzjz marked this pull request as draft July 18, 2026 05:27
auto-merge was automatically disabled July 18, 2026 05:27

Pull request was converted to draft

Keep the AMix pointer fix scoped to implementation and regression tests. The existing leak-check matrix should not be repurposed as a global libstdc++ assertions job.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
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] Avoid empty idx_mapping pointers in AMix env-mat CPU paths

5 participants