Skip to content

fix: make training examples self-contained - #386

Open
njzjz-bot wants to merge 1 commit into
deepmodeling:masterfrom
njzjz-bot:fix/issue-360-self-contained-examples
Open

njzjz-bot wants to merge 1 commit into
deepmodeling:masterfrom
njzjz-bot:fix/issue-360-self-contained-examples

Conversation

@njzjz-bot

Copy link
Copy Markdown

Summary

  • add a shared repository DPA-2 training template under examples/
  • update every previously missing train.json reference to resolve to that template
  • validate that each tested example references existing, valid JSON templates

Tests

  • PYTHONPATH=tests python -m unittest -v tests.test_check_examples
  • repository-wide script verifying every example template_script path resolves
  • ruff format --check tests/test_check_examples.py
  • isort --check-only tests/test_check_examples.py
  • python -m json.tool examples/train.json
  • git diff --check

Closes #360

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

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Aug 23, 2026
@coderabbitai

coderabbitai Bot commented Aug 23, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 59 minutes.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 29a1bab2-bec0-4ba5-ba0a-5770a9a695d8

📥 Commits

Reviewing files that changed from the base of the PR and between 6b01f29 and d32656e.

📒 Files selected for processing (11)
  • examples/calypso/input.test.json
  • examples/ch4/input_dist.json
  • examples/diffcsp/dpgen.json
  • examples/train.json
  • examples/water/input_distill.json
  • examples/water/input_dpgen.json
  • examples/water/input_dpgen_abacus.json
  • examples/water/input_dpgen_cp2k.json
  • examples/water/input_dpgen_slurm.json
  • examples/water/input_multitask.json
  • tests/test_check_examples.py

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.

Add a shared repository training template, point previously broken example references to it, and validate referenced templates in the example test.

Closes deepmodeling#360

Coding-Agent: Codex
Codex-Version: codex-cli 0.149.1
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
@njzjz-bot
njzjz-bot force-pushed the fix/issue-360-self-contained-examples branch from 399efa9 to d32656e Compare August 26, 2026 11:00
@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.43%. Comparing base (6b01f29) to head (d32656e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #386   +/-   ##
=======================================
  Coverage   84.43%   84.43%           
=======================================
  Files         104      104           
  Lines        6110     6110           
=======================================
  Hits         5159     5159           
  Misses        951      951           

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

wanghan-iapcm

This comment was marked as outdated.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Aug 27, 2026
@wanghan-iapcm
wanghan-iapcm dismissed their stale review August 27, 2026 03:33

Retracted: submitted without the maintainer's decision. Will re-review and let the maintainer choose the action.

wanghan-iapcm

This comment was marked as outdated.

@wanghan-iapcm
wanghan-iapcm dismissed their stale review August 27, 2026 04:12

Retracted. This review was produced without running the mandated /code-review fan-out (the loop skill's section 2); the substitute process used instead has since been shown to miss findings and, in one case, to state a verified-sounding falsehood. Re-reviewing properly.

@wanghan-iapcm wanghan-iapcm left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The dangling references were real and worth fixing, and the new guard in tests/test_check_examples.py is a genuine one: reverting all nine paths and deleting examples/train.json makes it fail, naming each missing file individually. My objection is to the choice of a single generic template, which leaves several of these examples unrunnable for new reasons, and to one gap in the guard itself.

One template cannot serve these nine examples

They are not variations on a theme. Two of them run a different backend, one is multi-task, one is a distillation run initialised from a frozen model, and their element maps differ by two orders of magnitude. The shared file is a copy of examples/calypso/dpa2_train.json's descriptor with model.type_map removed, and it fits none of those four cases.

1. Two examples take the TensorFlow default, but the template is DPA-2. run_dp_train.py defaults impl to tensorflow (alias backend). Seven of the nine examples set it to pytorch; examples/ch4/input_dist.json and examples/water/input_distill.json set neither, so they take the default. In deepmd-kit, dpa2 is registered only under deepmd/pt/, deepmd/pd/, deepmd/jax/ and deepmd/dpmodel/ — there is no registration anywhere under deepmd/tf/, and deepmd/utils/argcheck.py tags the descriptor (Supported Backend: PyTorch). These two now fail deterministically at descriptor construction.

2. The multi-task example is repointed at a single-task template. examples/water/input_multitask.json sets multitask: true and head: "water_1", so submit.py sets train_config["multitask"]=True and RunDPTrain.write_data_to_input_script runs

for k, v in odict["training"]["data_dict"].items():

The shared template has a flat training.training_data and a scalar loss, no data_dict and no model.model_dict. I ran it:

template training keys: ['training_data', 'numb_steps', 'seed', 'disp_file', 'disp_freq', 'save_freq']
RAISED: KeyError 'data_dict'

Before this PR that example failed at submit time with a missing file. Now it passes the new guard, gets a workflow built and dispatched, and dies inside the training step with an error that says nothing about the real cause. grep -rl data_dict tests/ is empty, so nothing catches it.

3. The distillation example needs a template matching its frozen student model. examples/ch4/input_dist.json is dp-dist with student_model_path: student_model.pb and init_model_policy: yes. submit.py wires that path in as the init model and forces numb_models to 1; run_dp_train.py then runs dp train --init-frz-model student_model.pb <script>. A frozen .pb is a TensorFlow graph and can never match a DPA-2 script. Note this one does not go away if you fix point 1 — under impl: pytorch the same .pb fails to load for a different reason. This example needs its own small student-architecture template.

4. model.type_map was dropped, and dpgen2 never puts it back. Every other template in this repository carries it: almg/dp_template.json, chno/dpa_manyi.json, calypso/dp_dpa1_train.json, calypso/dpa2_train.json. There is no write to type_map anywhere in run_dp_train.py or prep_dp_train.py; inputs.type_map reaches only the explore, fp and collect-data steps. Eight of the nine repointed examples set mixed_type: true, where collect_data.py writes real_atom_types.npy holding integer indices into inputs.type_map — six of them against a 118-element map, two against a 2-element one. deepmd's own argcheck declares model.type_map optional, so this will not be rejected at validation; whatever it does instead happens silently. I could not run deepmd-kit here to establish which, so I am flagging the omission rather than asserting the consequence, but a single template with no type_map cannot be right for both a 118-element and a 2-element map.

The guard misses one of the nine files it was added for

input_files in tests/test_check_examples.py does not list examples/water/input_dpgen_slurm.json, though this PR repoints it. Hiding examples/train.json and re-running produces failures for eight files; that one stays green. Adding the entry is one line. There is precedent: on #238 njzjz asked a contributor to "add new example files to tests/check_examples.py" for exactly this reason. Open PR #399 adds that same entry, so watch the ordering.

Not blocking

  • The calypso example already has a local template. 2e17cf9 (#208) renamed examples/calypso/train.json to dp_dpa1_train.json and added dpa2_train.json, without updating input.test.json — that rename is the origin of this one dangling reference. After this PR both files are referenced by nothing. To be fair, dpa2_train.json is not a drop-in replacement: it carries type_map: ["Mg","Al"] against a 118-element inputs.type_map, and a hardcoded personal path in validation_data.systems. So it needed work either way; I mention it because #360's first suggestion was to point at files that exist in their own directories.
  • ../train.json is resolved against the process CWD, not the config's directory. submit.py does a bare Path(...).read_text() and nothing chdirs. The new test uses fn.parent / template_script. Under the documented invocation (cd into the example directory, then dpgen2 submit input.json) the two agree, and that CWD sensitivity predates this PR — but ../ escaping the example directory is new, and it is in tension with "self-contained".

One last thing worth knowing before merging: the lgtm label here was applied by dosubot, mirroring two reviews of mine that have since been dismissed. No human approval stands on this PR.

Comment thread examples/train.json
"_comment": "Shared self-contained DPA-2 template for repository examples; tune model and training parameters for production runs.",
"model": {
"descriptor": {
"type": "dpa2",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Two problems with this template as a shared default.

"type": "dpa2" is PyTorch-only. In deepmd-kit the descriptor is registered under deepmd/pt/, deepmd/pd/, deepmd/jax/ and deepmd/dpmodel/ and nowhere under deepmd/tf/; deepmd/utils/argcheck.py tags it (Supported Backend: PyTorch). Two of the nine examples now pointing here set neither impl nor its alias backend, so they take run_dp_train.py's tensorflow default and cannot construct this descriptor.

Separately, this file has no model.type_map, and dpgen2 never injects one — there is no write to it in run_dp_train.py or prep_dp_train.py, and inputs.type_map only reaches the explore, fp and collect-data steps. Every other template in the repo has it (almg/dp_template.json, chno/dpa_manyi.json, and both calypso ones, from which this descriptor block was copied verbatim). Eight of the nine repointed examples set mixed_type: true, where atom types are integer indices into inputs.type_map; six declare 118 elements and two declare 2. deepmd's argcheck marks type_map optional, so nothing will reject this at validation.

"_comment": "all"
},
"template_script": "train.json",
"template_script": "../train.json",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This example needs its own template; the shared one is wrong for it twice over.

It sets no impl/backend, so it takes the tensorflow default, and the shared template is a DPA-2 model that has no TensorFlow implementation.

It is also "type": "dp-dist" with "student_model_path": "student_model.pb" and init_model_policy: yes (lines 121-122 above). submit.py wires that path in as the init model and forces numb_models to 1, and run_dp_train.py then runs dp train --init-frz-model student_model.pb <script>. A frozen .pb is a TensorFlow graph; a DPA-2 script can never match it. Setting impl: pytorch does not rescue this — the .pb then fails to load on the PyTorch side instead. What this example wants is a small student-architecture template that matches the frozen model.

"_comment": "all"
},
"template_script": "train.json",
"template_script": "../train.json",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same backend mismatch as ch4/input_dist.json: this config sets neither impl nor backend, so it takes run_dp_train.py's tensorflow default, while the shared template's descriptor is dpa2, which deepmd-kit implements only for PyTorch, Paddle and JAX. Either add "impl": "pytorch" to train.config here, or give this example a TensorFlow-compatible template.

"_comment": "all"
},
"template_script": "train.json",
"template_script": "../train.json",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is the multi-task example (multitask: true, head: "water_1"), and the template it now points at is single-task.

submit.py sets train_config["multitask"]=True, and RunDPTrain.write_data_to_input_script then does for k, v in odict["training"]["data_dict"].items(). The shared template has a flat training.training_data and a scalar loss — no data_dict, no model.model_dict. I ran it directly:

template training keys: ['training_data', 'numb_steps', 'seed', 'disp_file', 'disp_freq', 'save_freq']
RAISED: KeyError 'data_dict'

Before this change the example failed at submit time with a missing file. Now it passes the new guard and fails later, inside the running workflow, with an error that gives no hint of the real cause. Nothing in tests/ references data_dict, so CI will not catch it. This example needs a multi-task template with model.model_dict and training.data_dict keyed by the head names.

for template_script in template_scripts:
template_path = fn.parent / template_script
self.assertTrue(
template_path.is_file(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This assertion never runs against examples/water/input_dpgen_slurm.json, which this PR also repoints — it is missing from the input_files tuple at lines 13-27. I hid examples/train.json and re-ran: eight files fail, that one stays green. So the one file the guard cannot see is one of the nine the PR is fixing.

Adding p_examples / "water" / "input_dpgen_slurm.json" to the tuple closes it. Same ask njzjz made on #238 ("Please add new example files to tests/check_examples.py"). Note open PR #399 adds that identical entry, so whichever lands second will need a rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code scan] Make example template_script references self-contained and tested

2 participants