Skip to content

Commit df6ea80

Browse files
committed
add graph_net/apply_sample_pass.py
1 parent 207cf71 commit df6ea80

File tree

3 files changed

+255
-47
lines changed

3 files changed

+255
-47
lines changed

graph_net/apply_sample_pass.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import argparse
2+
import traceback
3+
from graph_net.imp_util import load_module
4+
import logging
5+
import sys
6+
import json
7+
import subprocess
8+
9+
logging.basicConfig(
10+
level=logging.WARNING, format="%(asctime)s [%(levelname)s] %(message)s"
11+
)
12+
13+
14+
def _load_class_from_file(sample_pass_file_path, sample_pass_class_name):
15+
module = load_module(sample_pass_file_path)
16+
return getattr(module, sample_pass_class_name)
17+
18+
19+
def _get_handler(args):
20+
handler_class = _load_class_from_file(
21+
args.sample_pass_file_path, args.sample_pass_class_name
22+
)
23+
24+
sample_pass_config = {}
25+
if args.sample_pass_config:
26+
try:
27+
sample_pass_config = json.loads(args.sample_pass_config)
28+
except json.JSONDecodeError:
29+
print(
30+
f"Error: Failed to parse --sample_pass_config as JSON. Received: {args.sample_pass_config}"
31+
)
32+
sys.exit(1)
33+
34+
return handler_class(sample_pass_config)
35+
36+
37+
def main(args):
38+
handler = _get_handler(args)
39+
if args.model_path is not None:
40+
handle_model_path(handler, args.model_path)
41+
elif args.use_subprocess:
42+
handle_model_path_list_in_subprocess(args)
43+
else:
44+
handle_model_path_list_in_current_process(handler, args)
45+
46+
47+
def handle_model_path_list_in_current_process(handler, args):
48+
for model_path in _get_model_path_list(args):
49+
try:
50+
handle_model_path(handler, model_path)
51+
except KeyboardInterrupt:
52+
print("KeyboardInterrupt")
53+
return
54+
except Exception:
55+
print("------------[apply_sample_pass failed]------------", flush=True)
56+
traceback.print_exc()
57+
58+
59+
def handle_model_path_list_in_subprocess(args):
60+
for model_path in _get_model_path_list(args):
61+
# 构造新版参数格式的命令,方便 bash -x 调试
62+
cmd = (
63+
f"{sys.executable} -m graph_net.apply_sample_pass "
64+
f"--file-path {args.sample_pass_file_path} "
65+
f"--class-name {args.sample_pass_class_name} "
66+
f"--sample_pass_config '{args.sample_pass_config}' "
67+
f"--model-path {model_path}"
68+
)
69+
try:
70+
subprocess.Popen(cmd, shell=True).wait()
71+
except KeyboardInterrupt:
72+
print("KeyboardInterrupt")
73+
return
74+
75+
76+
def handle_model_path(handler, model_path):
77+
print(f"{model_path=}", flush=True)
78+
handler(model_path)
79+
80+
81+
def _get_model_path_list(args):
82+
assert args.model_path is None
83+
assert args.model_path_list is not None
84+
with open(args.model_path_list) as f:
85+
yield from (
86+
clean_line
87+
for line in f
88+
for clean_line in [line.strip()]
89+
if len(clean_line) > 0
90+
if not clean_line.startswith("#")
91+
)
92+
93+
94+
if __name__ == "__main__":
95+
parser = argparse.ArgumentParser(description="apply sample pass entry")
96+
parser.add_argument(
97+
"--sample-pass-file-path",
98+
type=str,
99+
required=True,
100+
help="Path to the python file containing the handler class.",
101+
)
102+
parser.add_argument(
103+
"--sample-pass-class-name",
104+
type=str,
105+
required=True,
106+
help="Name of the handler class to instantiate.",
107+
)
108+
parser.add_argument(
109+
"--sample-pass-config",
110+
type=str,
111+
required=False,
112+
default="{}",
113+
help="JSON string for handler sample_pass_configuration.",
114+
)
115+
116+
parser.add_argument(
117+
"--model-path",
118+
type=str,
119+
required=False,
120+
default=None,
121+
help="Path to single model folder.",
122+
)
123+
parser.add_argument(
124+
"--model-path-list",
125+
type=str,
126+
required=False,
127+
default=None,
128+
help="Path of file containing model paths.",
129+
)
130+
parser.add_argument(
131+
"--use-subprocess",
132+
action="store_true",
133+
default=False,
134+
help="Execute each model handling in a separate subprocess.",
135+
)
136+
137+
args = parser.parse_args()
138+
main(args=args)
Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
#!/bin/bash
22

3+
# GRAPH_NET_ROOT=$(python3 -c "import graph_net; import os; print(
4+
# os.path.dirname(os.path.dirname(graph_net.__file__)))")
5+
6+
# python3 -m graph_net.model_path_handler \
7+
# --model-path-list $GRAPH_NET_ROOT/graph_net/config/small_sample_list_for_get_fusible_subgraph.txt \
8+
# --handler-config=$(base64 -w 0 <<EOF
9+
# {
10+
# "handler_path": "$GRAPH_NET_ROOT/graph_net/torch/sample_passes/dimension_symbolizer.py",
11+
# "handler_class_name": "DimensionSymbolizer",
12+
# "handler_config": {
13+
# "resume": false,
14+
# "output_dir": "/tmp/workspace_dimension_symbolizer",
15+
# "model_path_prefix": "$GRAPH_NET_ROOT",
16+
# "limits_handled_models": 10,
17+
# "last_model_log_file": "/tmp/a.py"
18+
# }
19+
# }
20+
# EOF
21+
# )
22+
323
GRAPH_NET_ROOT=$(python3 -c "import graph_net; import os; print(
424
os.path.dirname(os.path.dirname(graph_net.__file__)))")
525

6-
python3 -m graph_net.model_path_handler \
7-
--model-path-list $GRAPH_NET_ROOT/graph_net/config/small100_torch_samples_list.txt \
8-
--handler-config=$(base64 -w 0 <<EOF
26+
python3 -m graph_net.apply_sample_pass \
27+
--model-path-list $GRAPH_NET_ROOT/graph_net/config/small_sample_list_for_get_fusible_subgraph.txt \
28+
--sample-pass-file-path "$GRAPH_NET_ROOT/graph_net/torch/sample_passes/dimension_symbolizer.py" \
29+
--sample-pass-class-name "DimensionSymbolizer" \
30+
--sample-pass-config "$(cat <<EOF
931
{
10-
"handler_path": "$GRAPH_NET_ROOT/graph_net/torch/sample_passes/dimension_symbolizer.py",
11-
"handler_class_name": "DimensionSymbolizer",
12-
"handler_config": {
13-
"resume": false,
14-
"output_dir": "/tmp/workspace_dimension_symbolizer",
15-
"model_path_prefix": "$GRAPH_NET_ROOT",
16-
"limits_handled_models": 10,
17-
"last_model_log_file": "/tmp/a.py"
18-
}
32+
"resume": false,
33+
"output_dir": "/tmp/workspace_dimension_symbolizer",
34+
"model_path_prefix": "$GRAPH_NET_ROOT",
35+
"limits_handled_models": 10,
36+
"last_model_log_file": "/tmp/a.py"
1937
}
2038
EOF
21-
)
39+
)"
Lines changed: 86 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
11
#!/bin/bash
22

3+
# GRAPH_NET_ROOT=$(python3 -c "import graph_net; import os; print(os.path.dirname(os.path.dirname(graph_net.__file__)))")
4+
5+
# model_path_list=$GRAPH_NET_ROOT/graph_net/test/dev_model_list/cumsum_num_kernels_sample_list.txt
6+
# WORKSPACE_ROOT=/tmp/fusible_subgraphs
7+
# CUMSUM_NUM_KERNELS_WORKSPACE=$WORKSPACE_ROOT/workspace_cumsum_num_kernels
8+
# FUSIBLE_SUBGRAPH_RANGES_WORKSPACE=$WORKSPACE_ROOT/workspace_fusible_subgraph_ranges
9+
# FUSIBLE_SUBGRAPH_SAMPLES_WORKSPACE=$WORKSPACE_ROOT/workspace_fusible_subgraph_samples
10+
# resume=false
11+
12+
# python3 -m graph_net.model_path_handler \
13+
# --model-path-list "$model_path_list" \
14+
# --handler-config $(base64 -w 0 <<EOF
15+
# {
16+
# "handler_path": "$GRAPH_NET_ROOT/graph_net/torch/sample_passes/cumsum_num_kernels_generator.py",
17+
# "handler_class_name": "CumSumNumKernelsGenerator",
18+
# "handler_config": {
19+
# "output_json_file_name": "cumsum_num_kernels.json",
20+
# "model_path_prefix": "$GRAPH_NET_ROOT",
21+
# "output_dir": "$CUMSUM_NUM_KERNELS_WORKSPACE",
22+
# "resume": $resume
23+
# }
24+
# }
25+
# EOF
26+
# )
27+
28+
# python3 -m graph_net.model_path_handler \
29+
# --model-path-list "$model_path_list" \
30+
# --handler-config $(base64 -w 0 <<EOF
31+
# {
32+
# "handler_path": "$GRAPH_NET_ROOT/graph_net/sample_pass/fusible_subgraph_ranges_generator.py",
33+
# "handler_class_name": "FusibleSubgraphRangesGenerator",
34+
# "handler_config": {
35+
# "model_path_prefix": "$CUMSUM_NUM_KERNELS_WORKSPACE",
36+
# "input_json_file_name": "cumsum_num_kernels.json",
37+
# "output_json_file_name": "fusible_subgraph_ranges.json",
38+
# "output_dir": "$FUSIBLE_SUBGRAPH_RANGES_WORKSPACE",
39+
# "resume": $resume
40+
# }
41+
# }
42+
# EOF
43+
# )
44+
45+
# python3 -m graph_net.model_path_handler \
46+
# --model-path-list "$model_path_list" \
47+
# --handler-config $(base64 -w 0 <<EOF
48+
# {
49+
# "handler_path": "$GRAPH_NET_ROOT/graph_net/torch/sample_passes/subgraph_generator.py",
50+
# "handler_class_name": "SubgraphGenerator",
51+
# "handler_config": {
52+
# "model_path_prefix": "$GRAPH_NET_ROOT",
53+
# "output_dir": "$FUSIBLE_SUBGRAPH_SAMPLES_WORKSPACE",
54+
# "subgraph_ranges_json_root": "$FUSIBLE_SUBGRAPH_RANGES_WORKSPACE",
55+
# "resume": $resume
56+
# }
57+
# }
58+
# EOF
59+
# )
60+
361
GRAPH_NET_ROOT=$(python3 -c "import graph_net; import os; print(os.path.dirname(os.path.dirname(graph_net.__file__)))")
462

563
model_path_list=$GRAPH_NET_ROOT/graph_net/test/dev_model_list/cumsum_num_kernels_sample_list.txt
@@ -9,51 +67,45 @@ FUSIBLE_SUBGRAPH_RANGES_WORKSPACE=$WORKSPACE_ROOT/workspace_fusible_subgraph_ran
967
FUSIBLE_SUBGRAPH_SAMPLES_WORKSPACE=$WORKSPACE_ROOT/workspace_fusible_subgraph_samples
1068
resume=false
1169

12-
python3 -m graph_net.model_path_handler \
70+
python3 -m graph_net.apply_sample_pass \
1371
--model-path-list "$model_path_list" \
14-
--handler-config $(base64 -w 0 <<EOF
72+
--sample-pass-file-path "$GRAPH_NET_ROOT/graph_net/torch/sample_passes/cumsum_num_kernels_generator.py" \
73+
--sample-pass-class-name "CumSumNumKernelsGenerator" \
74+
--sample-pass-config "$(cat <<EOF
1575
{
16-
"handler_path": "$GRAPH_NET_ROOT/graph_net/torch/sample_passes/cumsum_num_kernels_generator.py",
17-
"handler_class_name": "CumSumNumKernelsGenerator",
18-
"handler_config": {
19-
"output_json_file_name": "cumsum_num_kernels.json",
20-
"model_path_prefix": "$GRAPH_NET_ROOT",
21-
"output_dir": "$CUMSUM_NUM_KERNELS_WORKSPACE",
22-
"resume": $resume
23-
}
76+
"output_json_file_name": "cumsum_num_kernels.json",
77+
"model_path_prefix": "$GRAPH_NET_ROOT",
78+
"output_dir": "$CUMSUM_NUM_KERNELS_WORKSPACE",
79+
"resume": $resume
2480
}
2581
EOF
26-
)
82+
)"
2783

28-
python3 -m graph_net.model_path_handler \
84+
python3 -m graph_net.apply_sample_pass \
2985
--model-path-list "$model_path_list" \
30-
--handler-config $(base64 -w 0 <<EOF
86+
--sample-pass-file-path "$GRAPH_NET_ROOT/graph_net/sample_pass/fusible_subgraph_ranges_generator.py" \
87+
--sample-pass-class-name "FusibleSubgraphRangesGenerator" \
88+
--sample-pass-config "$(cat <<EOF
3189
{
32-
"handler_path": "$GRAPH_NET_ROOT/graph_net/sample_pass/fusible_subgraph_ranges_generator.py",
33-
"handler_class_name": "FusibleSubgraphRangesGenerator",
34-
"handler_config": {
35-
"model_path_prefix": "$CUMSUM_NUM_KERNELS_WORKSPACE",
36-
"input_json_file_name": "cumsum_num_kernels.json",
37-
"output_json_file_name": "fusible_subgraph_ranges.json",
38-
"output_dir": "$FUSIBLE_SUBGRAPH_RANGES_WORKSPACE",
39-
"resume": $resume
40-
}
90+
"model_path_prefix": "$CUMSUM_NUM_KERNELS_WORKSPACE",
91+
"input_json_file_name": "cumsum_num_kernels.json",
92+
"output_json_file_name": "fusible_subgraph_ranges.json",
93+
"output_dir": "$FUSIBLE_SUBGRAPH_RANGES_WORKSPACE",
94+
"resume": $resume
4195
}
4296
EOF
43-
)
97+
)"
4498

45-
python3 -m graph_net.model_path_handler \
99+
python3 -m graph_net.apply_sample_pass \
46100
--model-path-list "$model_path_list" \
47-
--handler-config $(base64 -w 0 <<EOF
101+
--sample-pass-file-path "$GRAPH_NET_ROOT/graph_net/sample_pass/subgraph_generator.py" \
102+
--sample-pass-class-name "SubgraphGenerator" \
103+
--sample-pass-config "$(cat <<EOF
48104
{
49-
"handler_path": "$GRAPH_NET_ROOT/graph_net/torch/sample_passes/subgraph_generator.py",
50-
"handler_class_name": "SubgraphGenerator",
51-
"handler_config": {
52-
"model_path_prefix": "$GRAPH_NET_ROOT",
53-
"output_dir": "$FUSIBLE_SUBGRAPH_SAMPLES_WORKSPACE",
54-
"subgraph_ranges_json_root": "$FUSIBLE_SUBGRAPH_RANGES_WORKSPACE",
55-
"resume": $resume
56-
}
105+
"model_path_prefix": "$GRAPH_NET_ROOT",
106+
"output_dir": "$FUSIBLE_SUBGRAPH_SAMPLES_WORKSPACE",
107+
"subgraph_ranges_json_root": "$FUSIBLE_SUBGRAPH_RANGES_WORKSPACE",
108+
"resume": $resume
57109
}
58110
EOF
59-
)
111+
)"

0 commit comments

Comments
 (0)