fix(pd): preserve fparam/aparam inputs when freezing models#5713
fix(pd): preserve fparam/aparam inputs when freezing models#5713wanghan-iapcm wants to merge 1 commit into
Conversation
The Paddle freeze entrypoint converted forward/forward_lower to static with fparam and aparam hardcoded to None in the input_spec. Models trained with required frame or atomic parameters therefore exported a static graph whose signature baked both values as None, so inference through the frozen model could not supply the required fparam/aparam. Build the fparam/aparam InputSpec from the model's get_dim_fparam/get_dim_aparam (None only when the dim is 0) and use it in both forward and forward_lower signatures. Add a unit test for the spec builder covering the unused, used, and fparam-only cases. Fix deepmodeling#5687
📝 WalkthroughWalkthroughModifies Paddle model serialization to conditionally include ChangesOptional fparam/aparam JIT export specs
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
source/tests/pd/model/test_serialization_fparam.py (1)
23-41: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd a symmetric aparam-only test case.
Coverage includes both-unused, both-used, and fparam-only, but not the mirror case (
aparamused,fparamunused). Adding it would fully cover the branch logic in_fparam_aparam_input_specs.✅ Suggested additional test
def test_only_fparam(self) -> None: fparam_spec, aparam_spec = _fparam_aparam_input_specs(_StubModel(2, 0)) self.assertIsNotNone(fparam_spec) self.assertIsNone(aparam_spec) + + def test_only_aparam(self) -> None: + fparam_spec, aparam_spec = _fparam_aparam_input_specs(_StubModel(0, 3)) + self.assertIsNone(fparam_spec) + self.assertIsNotNone(aparam_spec)🤖 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/tests/pd/model/test_serialization_fparam.py` around lines 23 - 41, Add the missing mirror coverage for _fparam_aparam_input_specs by introducing an aparam-only test in TestFparamAparamInputSpecs, analogous to test_only_fparam. Verify that the returned fparam_spec is None, aparam_spec is present, and the aparam_spec shape matches the stub model case where fparam is unused and aparam is used.
🤖 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/tests/pd/model/test_serialization_fparam.py`:
- Around line 23-41: Add the missing mirror coverage for
_fparam_aparam_input_specs by introducing an aparam-only test in
TestFparamAparamInputSpecs, analogous to test_only_fparam. Verify that the
returned fparam_spec is None, aparam_spec is present, and the aparam_spec shape
matches the stub model case where fparam is unused and aparam is used.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 8f597054-126d-4164-9258-aef26f69a559
📒 Files selected for processing (2)
deepmd/pd/utils/serialization.pysource/tests/pd/model/test_serialization_fparam.py
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5713 +/- ##
==========================================
- Coverage 81.97% 81.11% -0.86%
==========================================
Files 959 981 +22
Lines 105748 109867 +4119
Branches 4102 4234 +132
==========================================
+ Hits 86684 89117 +2433
- Misses 17573 19222 +1649
- Partials 1491 1528 +37 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Problem
The Paddle freeze entrypoint (
deepmd/pd/utils/serialization.py::deserialize_to_file) convertsmodel.forwardandmodel.forward_lowerto static graphs withfparamandaparamhardcoded toNonein theinput_spec. For models trained with required frame parameters (get_dim_fparam() > 0) or atomic parameters (get_dim_aparam() > 0), the exported static signature therefore bakes both values asNone, so inference through the frozen Paddle model cannot supply the requiredfparam/aparam.Fix
Build the
fparam/aparamInputSpecfrom the model'sget_dim_fparam()/get_dim_aparam()— a spec with shape[-1, dim_fparam]/[-1, -1, dim_aparam]when the dim is nonzero, andNoneotherwise — and use it in both theforwardandforward_lowerstatic signatures. Models that do not use these inputs keep theNoneplaceholders as before.Test
source/tests/pd/model/test_serialization_fparam.pycovers the spec builder for the unused case (bothNone), the used case (correct shapes and names), and the fparam-only case.Note on verification
Verified locally with
paddlepaddle==3.3.1. The spec-builder test is version-independent; the end-to-endpaddle.jit.saveexport (heavier and sensitive to the exact Paddle build) remains covered by CI's pinned nightly (paddlepaddle==3.4.0.dev20260310).Fix #5687
Summary by CodeRabbit
Bug Fixes
Tests