Skip to content

Commit 57d92b5

Browse files
committed
Merge branch 'develop' of github.com:PaddlePaddle/GraphNet into develop
2 parents d074d2c + 5750ae7 commit 57d92b5

10 files changed

+244
-111
lines changed

graph_net/model_path_handler.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import traceback
23
from graph_net.imp_util import load_module
34
import logging
45
import sys
@@ -52,6 +53,9 @@ def handle_model_path_list_in_current_process(handler, args):
5253
except KeyboardInterrupt:
5354
print("KeyboardInterrupt")
5455
return
56+
except Exception:
57+
print("------------[model_path_handler failed]------------", flush=True)
58+
traceback.print_exc()
5559

5660

5761
def handle_model_path_list_in_subprocess(args):
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
samples/torchgeometric/RECT_L
2+
samples/transformers-auto-model/bge-small-en-v1.5
3+
samples/transformers-auto-model/distilbert_distilbert-base-multilingual-cased
4+
samples/transformers-auto-model/OFA-Sys_chinese-clip-vit-large-patch14
5+
samples/transformers-auto-model/opus-mt-ase-es
6+
samples/transformers-auto-model/opus-mt-en-gv
7+
samples/transformers-auto-model/opus-mt-en-phi
8+
samples/transformers-auto-model/opus-mt-en-sal
9+
samples/transformers-auto-model/opus-mt-en-tw
10+
samples/transformers-auto-model/opus-mt-fi-niu
11+
samples/transformers-auto-model/opus-mt-tc-bible-big-deu_eng_fra_por_spa-bat
12+
samples/transformers-auto-model/opus-mt-tc-bible-big-gmw-deu_eng_fra_por_spa

graph_net/test/naive_decomposer_and_post_extract_process_test.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/bash
2-
# bash graph_net/test/naive_decomposer_and_post_extract_process_test.sh
32

43
GRAPH_NET_ROOT=$(python3 -c "import graph_net; import os; print(
54
os.path.dirname(graph_net.__file__))")

graph_net/test/typical_sequence_decomposer_test.sh

100644100755
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,35 @@
22

33
GRAPH_NET_ROOT=$(python3 -c "import graph_net; import os; print(os.path.dirname(os.path.dirname(graph_net.__file__)))")
44
DECOMPOSE_PATH=/tmp/decompose_workspace
5+
# DECOMPOSE_PATH=$GRAPH_NET_ROOT/decompose_test_level5_100
56

67
mkdir -p "$DECOMPOSE_PATH"
78

8-
model_list="$GRAPH_NET_ROOT/graph_net/config/small100_torch_samples_list.txt"
9+
# model_list="$GRAPH_NET_ROOT/graph_net/config/small100_torch_samples_list.txt"
10+
model_list="$GRAPH_NET_ROOT/graph_net/test/dev_model_list/validation_error_model_list.txt"
11+
12+
op_names_extractor_config_json_str=$(cat <<EOF
13+
{
14+
"handler_path": "$GRAPH_NET_ROOT/graph_net/torch/typical_sequence_split_points.py",
15+
"handler_class_name": "OpNamesExtractor",
16+
"handler_config": {
17+
"resume": true,
18+
"model_path_prefix": "$GRAPH_NET_ROOT",
19+
"output_dir": "$DECOMPOSE_PATH"
20+
}
21+
}
22+
EOF
23+
)
24+
OP_NAMES_EXTRACTOR_CONFIG=$(echo $op_names_extractor_config_json_str | base64 -w 0)
25+
26+
python3 -m graph_net.model_path_handler \
27+
--model-path-list $model_list \
28+
--handler-config=$OP_NAMES_EXTRACTOR_CONFIG \
929

1030
python3 -m graph_net.torch.typical_sequence_split_points \
31+
--enable-resume \
1132
--model-list "$model_list" \
33+
--op-names-path-prefix "$DECOMPOSE_PATH" \
1234
--device "cuda" \
1335
--window-size 10 \
1436
--fold-policy default \
@@ -20,6 +42,7 @@ decompose_config_json_str=$(cat <<EOF
2042
"handler_path": "$GRAPH_NET_ROOT/graph_net/torch/graph_decomposer.py",
2143
"handler_class_name": "RangeDecomposerExtractor",
2244
"handler_config": {
45+
"resume": true,
2346
"model_path_prefix": "$GRAPH_NET_ROOT",
2447
"output_dir": "$DECOMPOSE_PATH",
2548
"split_results_path": "$DECOMPOSE_PATH/split_results.json",
@@ -34,10 +57,10 @@ DECOMPOSE_CONFIG=$(echo $decompose_config_json_str | base64 -w 0)
3457
python3 -m graph_net.model_path_handler \
3558
--model-path-list $model_list \
3659
--handler-config=$DECOMPOSE_CONFIG \
37-
--use-subprocess
3860

3961
test_compiler_config_json_str=$(cat <<EOF
4062
{
63+
"model_path_prefix": "$GRAPH_NET_ROOT",
4164
"decomposed_root": "$DECOMPOSE_PATH"
4265
}
4366
EOF
@@ -50,8 +73,8 @@ python3 -m graph_net.torch.test_compiler \
5073
--device cuda \
5174
--config $TEST_COMPILER_CONFIG \
5275
--model-path-prefix $GRAPH_NET_ROOT \
53-
> "$DECOMPOSE_PATH/validation.log" 2>&1
76+
2>&1 | tee "$DECOMPOSE_PATH/validation.log"
5477

5578
python3 -m graph_net.plot_ESt \
5679
--benchmark-path "$DECOMPOSE_PATH/validation.log" \
57-
--output-dir "$DECOMPOSE_PATH"
80+
--output-dir "$DECOMPOSE_PATH"

graph_net/torch/backend/range_decomposer_validator_backend.py

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
import torch
2+
import inspect
23
import torch.nn as nn
34
import os
45
import importlib.util
56
from typing import List
67

78

89
class ComposedModel(nn.Module):
9-
def __init__(self, subgraph: List[nn.Module]):
10+
def __init__(self, subgraphs: List[nn.Module]):
1011
super().__init__()
11-
self.subgraphs = nn.ModuleList(subgraph)
12+
self.subgraphs = nn.ModuleList(subgraphs)
1213

1314
def forward(self, **kwargs):
1415
output = None
1516
for i, subgraph in enumerate(self.subgraphs):
16-
print(f"{i=} subgraph begin")
1717
if output is None:
18-
output = subgraph(**kwargs)
18+
output = subgraph(**self._convert_inputs(subgraph, kwargs))
1919
else:
2020
output = subgraph(*output)
21-
print(f"{i=} subgraph end")
2221

2322
return output
2423

24+
def _convert_inputs(self, subgraph, input_kwargs):
25+
input_keywords = set(name for name, _ in input_kwargs.items())
26+
sub_graph_arg_names = set(inspect.signature(subgraph.forward).parameters)
27+
assert (
28+
len(sub_graph_arg_names - input_keywords) == 0
29+
), f"{(sub_graph_arg_names - input_keywords)=}"
30+
for remainder in input_keywords - sub_graph_arg_names:
31+
assert remainder.startswith("s")
32+
assert remainder[1:].isdigit()
33+
return {
34+
name: value
35+
for name, value in input_kwargs.items()
36+
if name in sub_graph_arg_names
37+
}
38+
2539

2640
class RangeDecomposerValidatorBackend:
2741
def _load_model_instance(self, path: str, device: str) -> torch.nn.Module:
@@ -36,40 +50,56 @@ def _load_model_instance(self, path: str, device: str) -> torch.nn.Module:
3650
instance = ModelClass().to(device)
3751
return instance
3852

39-
def _make_config(self, decomposed_root, decomposed_model_name_suffix="_decomposed"):
53+
def _make_config(
54+
self,
55+
model_path_prefix: str,
56+
decomposed_root: str,
57+
decomposed_dentry: str = "_decomposed",
58+
):
4059
return {
60+
"model_path_prefix": model_path_prefix,
4161
"decomposed_root": decomposed_root,
42-
"decomposed_model_name_suffix": decomposed_model_name_suffix,
62+
"decomposed_dentry": decomposed_dentry,
4363
}
4464

65+
def _get_rel_model_path(self, model_path) -> str:
66+
model_path = os.path.realpath(model_path)
67+
model_path_prefix = os.path.realpath(self.config["model_path_prefix"])
68+
assert model_path.startswith(model_path_prefix)
69+
rel_model_path = model_path[len(model_path_prefix) :]
70+
if rel_model_path.startswith("/"):
71+
rel_model_path = rel_model_path[1:]
72+
assert not rel_model_path.startswith("/")
73+
return rel_model_path
74+
75+
def _get_model_name_order(self, name):
76+
lst = name.split("_")
77+
if not (len(lst) > 0):
78+
return -1
79+
if not (lst[-1].isdigit()):
80+
return -1
81+
return int(lst[-1])
82+
4583
def __call__(self, model: torch.nn.Module) -> torch.nn.Module:
4684
config = self._make_config(**self.config)
47-
model_file_path = model.__class__.__graph_net_file_path__
48-
model_dir = os.path.dirname(model_file_path)
49-
model_name = os.path.basename(model_dir)
85+
model_path = os.path.dirname(model.__class__.__graph_net_file_path__)
86+
rel_model_path = self._get_rel_model_path(model_path)
5087
decomposed_parent_dir = os.path.join(
51-
config["decomposed_root"], f"{model_name}_decomposed"
88+
config["decomposed_root"], rel_model_path, config["decomposed_dentry"]
5289
)
5390
subgraph_paths = []
54-
for name in sorted(os.listdir(decomposed_parent_dir)):
91+
dentries = os.listdir(decomposed_parent_dir)
92+
for name in sorted(dentries, key=self._get_model_name_order):
5593
full_path = os.path.join(decomposed_parent_dir, name)
56-
if os.path.isdir(full_path) and name[-1].isdigit():
94+
if os.path.isdir(full_path) and self._get_model_name_order(name) >= 0:
5795
subgraph_paths.append(full_path)
5896

59-
print(
60-
f"[RangeDecomposerValidatorBackend] Found subgraphs: {[os.path.basename(p) for p in subgraph_paths]}"
61-
)
62-
6397
device = model.__class__.__graph_net_device__
6498
subgraph_instances = []
6599

66100
for path in subgraph_paths:
67101
instance = self._load_model_instance(path, device)
68102
subgraph_instances.append(instance)
69-
dir_name = os.path.basename(path)
70-
print(
71-
f"[RangeDecomposerValidatorBackend] Loaded and instantiated '{dir_name}'"
72-
)
73103

74104
composed_model = ComposedModel(subgraph_instances)
75105
return composed_model.eval()

graph_net/torch/fx_graph_parse_util.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ def _get_name_pattern2replacement(names_from_signature, names_from_placeholder):
113113

114114

115115
def _rename_placeholder(name, pattern2replacement):
116-
assert name[:2] == "L_" or name[:2] == "l_", f"{name=}"
116+
if not (name[:2] == "L_" or name[:2] == "l_"):
117+
return name
117118
name = name[2:]
118119
if name[0] == "l":
119120
name = "L" + name[1:]
@@ -122,19 +123,28 @@ def _rename_placeholder(name, pattern2replacement):
122123
return name
123124

124125

125-
def parse_sole_graph_module(module, inputs):
126+
def parse_sole_graph_module_without_varify(module, inputs):
126127
traced_module = None
127128
traced_sample_inputs = None
128129

129130
def my_backend(gm, sample_inputs):
130131
nonlocal traced_module
131-
traced_module = gm
132132
nonlocal traced_sample_inputs
133+
assert traced_module is None
134+
assert traced_sample_inputs is None
135+
traced_module = gm
133136
traced_sample_inputs = sample_inputs
134137
return gm.forward
135138

136139
torch.compile(module, backend=my_backend)(*inputs)
137140
assert traced_module is not None
141+
return traced_module, traced_sample_inputs
142+
143+
144+
def parse_sole_graph_module(module, inputs):
145+
traced_module, traced_sample_inputs = parse_sole_graph_module_without_varify(
146+
module, inputs
147+
)
138148

139149
def get_input_names_from_signature():
140150
return inspect.signature(module.forward).parameters

0 commit comments

Comments
 (0)