fix(tf): handle tensor model bias reshape in backend conversion#4957
Closed
Copilot wants to merge 5 commits into
Closed
fix(tf): handle tensor model bias reshape in backend conversion#4957Copilot wants to merge 5 commits into
Copilot wants to merge 5 commits into
Conversation
Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] [BUG] Cannot convert tensor pt model to tf model
fix(tf): handle dipole model bias reshape in backend conversion
Sep 3, 2025
njzjz
reviewed
Sep 4, 2025
Handle all GeneralFitting types (dipole, polar) vs InvarFitting types (ener, dos, property) correctly in backend conversion bias reshaping logic. Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com>
Copilot
AI
changed the title
fix(tf): handle dipole model bias reshape in backend conversion
fix(tf): handle tensor model bias reshape for all fitting types in backend conversion
Sep 4, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a backend conversion bug that prevented PyTorch models with GeneralFitting types (dipole, polar) from being converted to TensorFlow format. The issue occurred when the TensorFlow deserialization code attempted to reshape incompatible bias tensors.
- Adds conditional logic to distinguish between InvarFitting and GeneralFitting types during tensor model deserialization
- Applies the original reshape logic only to compatible InvarFitting types (ener, dos, property)
- Skips the problematic reshape operation for GeneralFitting types (dipole, polar) where bias tensors have different purposes and incompatible shapes
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@ustc.edu.cn>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## devel #4957 +/- ##
=======================================
Coverage 84.28% 84.28%
=======================================
Files 705 705
Lines 69097 69099 +2
Branches 3572 3573 +1
=======================================
+ Hits 58239 58241 +2
Misses 9717 9717
Partials 1141 1141 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@ustc.edu.cn>
Member
|
Close in favour of #4962. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a tensor model conversion bug that prevented PyTorch models with GeneralFitting types (dipole, polar) from being converted to TensorFlow format.
Problem
When converting PyTorch models with GeneralFitting types (dipole, polar) to TensorFlow using
dp convert-backend, the conversion failed with:This error occurred because the TensorFlow deserialization code attempted to reshape the
out_biastensor to match thebias_atom_etensor shape, but these tensors have fundamentally different purposes and incompatible shapes in GeneralFitting models:out_bias: Shape varies by fitting type (e.g.,[1, ntypes, 3]for dipole) - represents the output biasbias_atom_e: Shape[ntypes, embedding_width](e.g.,[2, 100]) - represents the internal fitting network biasSolution
Modified the deserialization logic in
deepmd/tf/model/model.pyto:out_biasandbias_atom_ehave compatible shapesFitting Types Handled
ener,energy,dos,property,direct_force,direct_force_enerdipole,polarReproduction Steps
The issue can be reproduced with:
Testing
Fixes #4411.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.