Skip to content

Commit 24567e3

Browse files
authored
[Bug Fix] Use ast to generate the hash of Paddle models. (#266)
* Use ast to generate the hash of Paddle models. * Update all the hash value of paddle samples.
1 parent 2b62d8f commit 24567e3

File tree

226 files changed

+264
-245
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+264
-245
lines changed

graph_net/paddle/validate.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import numpy as np
1212
import graph_net
1313
import os
14-
import re
14+
import ast
1515
import paddle
1616

1717

@@ -29,29 +29,49 @@ def _get_sha_hash(content):
2929
return m.hexdigest()
3030

3131

32-
def _save_to_model_path(dump_dir, hash_text):
33-
file_path = f"{dump_dir}/graph_hash.txt"
34-
with open(file_path, "w") as f:
35-
f.write(hash_text)
32+
def _extract_forward_source(model_path):
33+
source = None
34+
with open(f"{model_path}/model.py", "r") as f:
35+
source = f.read()
3636

37+
tree = ast.parse(source)
38+
forward_code = None
3739

38-
def extract_from_forward_regex(text, case_sensitive=True):
39-
pattern = r"forward.*"
40-
flags = 0 if case_sensitive else re.IGNORECASE
40+
for node in tree.body:
41+
if isinstance(node, ast.ClassDef) and node.name == "GraphModule":
42+
for fn in node.body:
43+
if isinstance(fn, ast.FunctionDef) and fn.name == "forward":
44+
return ast.unparse(fn)
45+
return None
4146

42-
match = re.search(pattern, text, flags)
43-
if match:
44-
return match.group(0)
47+
48+
def check_graph_hash(args):
49+
model_path = args.model_path
50+
file_path = f"{model_path}/graph_hash.txt"
51+
if args.dump_graph_hash_key:
52+
model_str = _extract_forward_source(model_path)
53+
assert model_str is not None, f"model_str of {args.model_path} is None."
54+
new_hash_text = _get_sha_hash(model_str)
55+
56+
old_hash_text = None
57+
if os.path.exists(file_path):
58+
with open(file_path, "r") as f:
59+
old_hash_text = f.read()
60+
61+
if old_hash_text is None or new_hash_text != old_hash_text:
62+
print(f"Writing to {file_path}.")
63+
with open(file_path, "w") as f:
64+
f.write(new_hash_text)
65+
if old_hash_text is not None:
66+
assert (
67+
new_hash_text == old_hash_text
68+
), f"Hash value for {model_path} is not consistent."
4569
else:
46-
raise ValueError("Erroneous case occurs.")
70+
assert os.path.exists(file_path), f"{file_path} does not exist."
4771

4872

4973
def main(args):
50-
model_path = args.model_path
51-
with open(f"{model_path}/model.py", "r") as fp:
52-
model_str = fp.read()
53-
model_str = extract_from_forward_regex(model_str)
54-
_save_to_model_path(model_path, _get_sha_hash(model_str))
74+
check_graph_hash(args)
5575

5676
model_path = args.model_path
5777
model_class = load_class_from_file(
@@ -100,17 +120,16 @@ def main(args):
100120
required=True,
101121
help="Path to folder e.g '../test_dataset'",
102122
)
103-
104123
parser.add_argument(
105124
"--no-check-redundancy",
106125
action="store_true",
126+
default=False,
107127
help="whether check model graph redundancy",
108128
)
109-
110129
parser.add_argument(
111130
"--dump-graph-hash-key",
112131
action="store_true",
113-
default=False,
132+
default=True,
114133
help="Dump graph hash key",
115134
)
116135
parser.add_argument(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c1e7e52eab55414cee7c44a9e8c4f81bbd59e3837b185e179e6317efa04f69ec
1+
f2b5a332b1b19703e7ccfb450de96c9c12244144c7b9d305d20587f772fb6672
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c1e7e52eab55414cee7c44a9e8c4f81bbd59e3837b185e179e6317efa04f69ec
1+
f2b5a332b1b19703e7ccfb450de96c9c12244144c7b9d305d20587f772fb6672
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c1e7e52eab55414cee7c44a9e8c4f81bbd59e3837b185e179e6317efa04f69ec
1+
f2b5a332b1b19703e7ccfb450de96c9c12244144c7b9d305d20587f772fb6672
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c1e7e52eab55414cee7c44a9e8c4f81bbd59e3837b185e179e6317efa04f69ec
1+
f2b5a332b1b19703e7ccfb450de96c9c12244144c7b9d305d20587f772fb6672
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c1e7e52eab55414cee7c44a9e8c4f81bbd59e3837b185e179e6317efa04f69ec
1+
f2b5a332b1b19703e7ccfb450de96c9c12244144c7b9d305d20587f772fb6672
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1bad8e4fab570ff456bad864ef45a755f07b2e466cced7983a8383abccc8fc7a
1+
02fa10efca360c8ba7818c367cdeb9979e2af8c72cf489913396a1f241bbad07
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1bad8e4fab570ff456bad864ef45a755f07b2e466cced7983a8383abccc8fc7a
1+
02fa10efca360c8ba7818c367cdeb9979e2af8c72cf489913396a1f241bbad07
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c1e7e52eab55414cee7c44a9e8c4f81bbd59e3837b185e179e6317efa04f69ec
1+
f2b5a332b1b19703e7ccfb450de96c9c12244144c7b9d305d20587f772fb6672
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13585d923bee689fb22489ff8e5dde8cfeb28cce83f6caf8b12ecaf8c5194de8
1+
482fd9e9f201b45c2ce0b22b3037878aa3d139cc203fb35c781fd470140ec962

0 commit comments

Comments
 (0)