Skip to content

Commit 2e977e3

Browse files
authored
[Bug Fix] Use astor instead of ast to unparse source code of paddle model, to avoid unstable result between different python versions. (#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.
1 parent 01f909e commit 2e977e3

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

graph_net/paddle/validate.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import graph_net
1313
import os
1414
import ast
15+
import astor
1516
import paddle
1617

1718

@@ -29,7 +30,7 @@ def _get_sha_hash(content):
2930
return m.hexdigest()
3031

3132

32-
def _extract_forward_source(model_path):
33+
def _extract_forward_source(model_path, class_name):
3334
source = None
3435
with open(f"{model_path}/model.py", "r") as f:
3536
source = f.read()
@@ -38,18 +39,18 @@ def _extract_forward_source(model_path):
3839
forward_code = None
3940

4041
for node in tree.body:
41-
if isinstance(node, ast.ClassDef) and node.name == "GraphModule":
42+
if isinstance(node, ast.ClassDef) and node.name == class_name:
4243
for fn in node.body:
4344
if isinstance(fn, ast.FunctionDef) and fn.name == "forward":
44-
return ast.unparse(fn)
45+
return astor.to_source(fn)
4546
return None
4647

4748

4849
def check_graph_hash(args):
4950
model_path = args.model_path
5051
file_path = f"{model_path}/graph_hash.txt"
5152
if args.dump_graph_hash_key:
52-
model_str = _extract_forward_source(model_path)
53+
model_str = _extract_forward_source(model_path, class_name="GraphModule")
5354
assert model_str is not None, f"model_str of {args.model_path} is None."
5455
new_hash_text = _get_sha_hash(model_str)
5556

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pre-commit
2+
astor

tools/ci/check_validate.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function prepare_paddle_env() {
5656
env http_proxy="" https_proxy="" pip install -U pip > /dev/null
5757
[ $? -ne 0 ] && LOG "[FATAL] Update pip failed!" && exit -1
5858
# install paddle
59-
pip uninstall torch==2.7.0 --yes
59+
pip install astor
6060
LOG "[INFO] Install paddlepaddle-develop ..."
6161
python -m pip install --pre paddlepaddle-gpu -i https://www.paddlepaddle.org.cn/packages/nightly/cu118/ > /dev/null
6262
[ $? -ne 0 ] && LOG "[FATAL] Install paddlepaddle-develop failed!" && exit -1

0 commit comments

Comments
 (0)