Skip to content

fix(train): dispatch registered learning rate schedules - #5776

Merged
OutisLi merged 3 commits into
deepmodeling:masterfrom
njzjz-bot:fix/backend-learning-rate-schedules
Jul 19, 2026
Merged

fix(train): dispatch registered learning rate schedules#5776
OutisLi merged 3 commits into
deepmodeling:masterfrom
njzjz-bot:fix/backend-learning-rate-schedules

Conversation

@njzjz-bot

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

Copy link
Copy Markdown
Contributor

Summary

  • add a shared learning-rate schedule factory backed by the BaseLR registry
  • use it in JAX, TF2, and pt_expt training so exp, cosine, and wsd follow the common schema
  • preserve the caller's learning-rate configuration while injecting the training step count
  • add regression coverage for all registered schema variants

Addresses the learning-rate checklist items in #5755, #5756, and #5757. The remaining checklist items in those issues are intentionally left open.

Tests

  • venv/bin/pytest source/tests/universal/dpmodel/utils/test_learning_rate.py -q
  • venv/bin/pytest source/tests/pt_expt/test_training.py::TestTraining::test_training_loop -q
  • DP_TEST_TF2_ONLY=1 venv/bin/pytest source/tests/tf2/test_training.py -q
  • venv/bin/ruff check .
  • venv/bin/ruff format .

JAX integration tests were not run locally because the existing virtual environment does not include JAX/Optax; the shared dispatch regression test covers the schedule factory used by the JAX trainer.

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

Summary by CodeRabbit

  • New Features

    • Added a shared learning-rate schedule builder (exp/cosine/WSD) and standardized schedule creation across backends.
  • Bug Fixes

    • Fixed learning-rate initialization to use the schedule’s correct starting value, including during resume and warmup starting from zero.
    • Prevented learning-rate configuration inputs from being mutated during schedule creation.
  • Tests

    • Added coverage for schedule selection and parameter immutability, plus a smoke test ensuring zero-start warmup scheduler construction for cosine and WSD.

Use the shared BaseLR registry in JAX, TF2, and pt_expt so cosine and WSD schedules are honored consistently with the input schema.

Coding-Agent: Codex
Codex-Version: codex-cli 0.144.1
Model: gpt-5.6-sol
Reasoning-Effort: xhigh
@coderabbitai

coderabbitai Bot commented Jul 12, 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: 33c16316-75c8-4ef6-8e84-9b9998d0d634

📥 Commits

Reviewing files that changed from the base of the PR and between 28dfd4e and 5d5804d.

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

📝 Walkthrough

Walkthrough

A shared make_learning_rate_schedule factory constructs registered schedules without mutating input parameters. JAX, PyTorch, and TensorFlow trainers use it instead of direct exponential schedule construction, with tests covering dispatch, warmup initialization, and resume metadata.

Changes

Learning-rate schedule construction

Layer / File(s) Summary
Schedule factory and validation
deepmd/dpmodel/utils/learning_rate.py, source/tests/universal/dpmodel/utils/test_learning_rate.py
Adds a factory that copies parameters, defaults the type to exp, injects num_steps, constructs registered schedules, and tests dispatch for exp, cosine, and wsd without mutating inputs.
Trainer schedule integration and initialization
deepmd/jax/train/trainer.py, deepmd/pt_expt/train/training.py, deepmd/tf2/train/trainer.py, source/tests/pt_expt/test_training.py
Updates all three trainers to use the shared factory; PyTorch derives initial_lr from start_lr, restores optimizer-group metadata on resume, and tests zero-step warmup behavior.

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

Sequence Diagram(s)

sequenceDiagram
  participant Trainer
  participant make_learning_rate_schedule
  participant BaseLR
  Trainer->>make_learning_rate_schedule: pass learning-rate parameters and num_steps
  make_learning_rate_schedule->>BaseLR: construct registered schedule
  BaseLR-->>Trainer: return selected schedule
Loading

Possibly related PRs

Suggested labels: bug

Suggested reviewers: wanghan-iapcm, njzjz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: training now dispatches registered learning-rate schedules through the shared factory.
Docstring Coverage ✅ Passed Docstring coverage is 88.89% which is sufficient. The required threshold is 80.00%.
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 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 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.31%. Comparing base (0c5a914) to head (5d5804d).
⚠️ Report is 18 commits behind head on master.

Files with missing lines Patch % Lines
deepmd/pt_expt/train/training.py 80.00% 1 Missing ⚠️
deepmd/tf2/train/trainer.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5776      +/-   ##
==========================================
- Coverage   79.69%   78.31%   -1.39%     
==========================================
  Files        1020     1050      +30     
  Lines      116359   120550    +4191     
  Branches     4303     4353      +50     
==========================================
+ Hits        92736    94404    +1668     
- Misses      22076    24575    +2499     
- Partials     1547     1571      +24     

☔ 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 OutisLi and wanghan-iapcm July 12, 2026 14:45

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

The fix is correct across all three backends: routing through make_learning_rate_schedule -> BaseLR(**params) dispatches by type (BaseLR.new resolves the registered subclass and init absorbs the extra type key via **kwargs), so cosine/wsd now work where JAX previously raised and TF2/pt_expt silently forced exp. Copying the config into a fresh dict before adding num_steps also fixes the in-place config mutation. The new regression test genuinely locks the fix: it fails on the buggy behavior (an exp-always factory returns the wrong class for cosine/wsd; a mutating factory leaves num_steps in the input).

Non-blocking: the regression test covers the shared factory but not the trainer call sites that were actually broken. A trainer-level test (construct a JAX/TF2/pt_expt trainer with type: cosine/wsd and assert self.lr_schedule is the expected class) would guard against a future re-hardcoding of a backend to exp. The trainers are now one-line delegations to the tested factory, so this is a coverage note rather than a correctness concern.

Approving.

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

Requesting changes for one pt_expt integration blocker: valid cosine/WSD warmup schedules crash while constructing LambdaLR, before training starts.

Comment thread deepmd/pt_expt/train/training.py

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

One additional compatibility issue in the shared factory: its documented/schema default for an omitted learning-rate type is not implemented.

Comment thread deepmd/dpmodel/utils/learning_rate.py
Use each schedule's nonzero start_lr as the PyTorch LambdaLR base, preserve the documented exponential default when type is omitted, and add trainer-level cosine/WSD warmup regressions.

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: 2

🤖 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/tests/pt_expt/test_training.py`:
- Around line 314-315: Add the pytest timeout marker to
test_zero_start_warmup_schedulers_construct, using a 60-second limit while
preserving the test’s existing behavior.
- Around line 314-315: Add the pytest timeout decorator to
test_zero_start_warmup_schedulers_construct, using a 60-second limit while
preserving the test’s existing behavior.
🪄 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: 2f99ac77-9b95-4fb4-be64-6c0e932d052a

📥 Commits

Reviewing files that changed from the base of the PR and between a09e557 and 28dfd4e.

📒 Files selected for processing (4)
  • deepmd/dpmodel/utils/learning_rate.py
  • deepmd/pt_expt/train/training.py
  • source/tests/pt_expt/test_training.py
  • source/tests/universal/dpmodel/utils/test_learning_rate.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • deepmd/dpmodel/utils/learning_rate.py
  • source/tests/universal/dpmodel/utils/test_learning_rate.py
  • deepmd/pt_expt/train/training.py

Comment thread source/tests/pt_expt/test_training.py
Keep the trainer-level warmup regression within the repository's validation timeout.

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 Author

Also addressed the approved review's non-blocking trainer-coverage note: the pt_expt trainer now has an integration regression for cosine and WSD warmup schedules, while the shared factory remains covered for all registered variants.

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

@njzjz
njzjz requested a review from OutisLi July 18, 2026 05:44

@OutisLi OutisLi 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 the latest head. The zero-start warmup LambdaLR issue and omitted-type compatibility issue are both fixed. The shared schedule semantics now match the PT backend for dispatch, warmup, step progression, and restart. Targeted learning-rate, pt_expt warmup/restart, and cross-backend checks pass. Approving.

@OutisLi
OutisLi added this pull request to the merge queue Jul 19, 2026
Merged via the queue into deepmodeling:master with commit 20c7b75 Jul 19, 2026
57 checks passed
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.

3 participants