[Bug Fix] Use astor instead of ast to unparse source code of paddle model, to avoid unstable result between different python versions.#272
Merged
Conversation
…void unstable result between different python versions.
lixinqi
approved these changes
Sep 12, 2025
JewelRoam
pushed a commit
to JewelRoam/GraphNet
that referenced
this pull request
Oct 29, 2025
…odel, to avoid unstable result between different python versions. (PaddlePaddle#272) * Use astor instead of ast to unparse source code of paddle model, to avoid unstable result between different python versions. * Remove the install of astor for torch.
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.
PR Category
Bug Fix
Description
Paddle样本在执行validate时,通过从
model.py的forward函数中移除注释、解析模型结构,并依据模型结构的源码字符串生成样本的sha哈希值,写入graph_hash.txt。#266 改成用ast从model.py中解析python类型,并使用

ast.unparse将AST转成源码。ast.unparse在不同Python版本下,针对多输出场景的源码格式化风格不一样,比如:这是Python内置AST打印器的“非稳定行为”,会导致在不同Python版本下得到的模型字符串不一样,从而会得到不同的哈希值。
本PR改成使用第三方库
astor代替ast.unparse来生成源码,并修复CI上的依赖。astor.to_source和ast.unparse转换得到的源码的对比:astor.to_sourceast.unparse总结:
astor.to_source,更稳定、更简洁、跨版本统一ast.unparse,官方实现、格式更严格,但跨版本差异大