Skip to content

Commit 3667b0c

Browse files
authored
Merge branch 'develop' into Hackathon_9th_No_125
2 parents cd08bc3 + ea928bb commit 3667b0c

36 files changed

+617
-554
lines changed

graph_net/log2json.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def parse_logs_to_json(log_file: str, output_dir: str):
5353
"datatype": {},
5454
"speedup": {},
5555
},
56+
"result": {
57+
"status": "unknown",
58+
},
5659
}
5760
continue
5861

@@ -102,16 +105,20 @@ def parse_logs_to_json(log_file: str, output_dir: str):
102105
result_status_match = patterns["result_status"].search(line)
103106
if result_status_match:
104107
status = result_status_match.group(1).strip()
108+
data["result"]["status"] = status
105109
if status == "failed" and (i + 1) < len(lines):
106110
error_reason_match = patterns["failure"].search(lines[i + 1])
107111
if error_reason_match:
108112
reason = error_reason_match.group(1).lower()
109113
if "eager" in reason:
110114
data["performance"]["failure"] = "eager"
115+
data["result"]["status"] = "eager_fail"
111116
elif "compiled" in reason:
112117
data["performance"]["failure"] = "compiled"
118+
data["result"]["status"] = "compile_fail"
113119
else:
114120
data["performance"]["failure"] = "other"
121+
data["result"]["status"] = "runtime_fail"
115122
continue
116123

117124
speedup_match = patterns["speedup"].search(line)
@@ -141,6 +148,20 @@ def parse_logs_to_json(log_file: str, output_dir: str):
141148
# filename = f"{model_name}_{subgraph_name}_{compiler_name}.json"
142149
filepath = os.path.join(output_dir, filename)
143150

151+
# Build result field with status and speedup
152+
if data["result"]["status"] == "success":
153+
speedup_data = {}
154+
if "e2e" in data["performance"]["speedup"]:
155+
speedup_data["e2e"] = {
156+
"mean": data["performance"]["speedup"]["e2e"]
157+
}
158+
if "gpu" in data["performance"]["speedup"]:
159+
speedup_data["gpu"] = {
160+
"mean": data["performance"]["speedup"]["gpu"]
161+
}
162+
if speedup_data:
163+
data["result"]["speedup"] = speedup_data
164+
144165
with open(filepath, "w", encoding="utf-8") as f:
145166
json.dump(data, f, indent=4)
146167

graph_net/test/chain_naive_graph_decomposer_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ EOF
2020
EXTRACTOR_CONFIG=$(echo $extractor_config_json_str | base64 -w 0)
2121

2222
mkdir -p /tmp/naive_decompose_workspace
23-
python3 -m graph_net.torch.single_device_runner --model-path $GRAPH_NET_ROOT/../samples/$MODEL_PATH_IN_SAMPLES --enable-extract True --extract-name resnet18 --dump-graph-hash-key --extractor-config=$EXTRACTOR_CONFIG
23+
python3 -m graph_net.torch.single_device_runner --model-path $GRAPH_NET_ROOT/../samples/$MODEL_PATH_IN_SAMPLES --enable-extract True --extract-name resnet18 --dump-graph-hash-key --extractor-config=$EXTRACTOR_CONFIG
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
GRAPH_NET_ROOT=$(python3 -c "import graph_net; import os; print(os.path.dirname(graph_net.__file__))")
4+
5+
if [ -z "$GRAPH_NET_DECOMPOSE_PATH" ]; then
6+
GRAPH_NET_DECOMPOSE_PATH="$(pwd)/graphnet_decompose"
7+
fi
8+
9+
MODEL_PATH_IN_SAMPLES=/timm/resnet18
10+
MODEL_NAME=$(basename "$MODEL_PATH_IN_SAMPLES")
11+
OUTPUT_DIR="${GRAPH_NET_DECOMPOSE_PATH:-$(pwd)}"
12+
cp -r "$GRAPH_NET_ROOT/../samples/$MODEL_PATH_IN_SAMPLES" "$OUTPUT_DIR/"
13+
14+
extractor_config_json_str=$(cat <<EOF
15+
{
16+
"custom_extractor_path": "$GRAPH_NET_ROOT/torch/naive_graph_decomposer.py",
17+
"custom_extractor_config": {
18+
"output_dir": "$OUTPUT_DIR/${MODEL_NAME}_decomposed",
19+
"split_positions": [8, 16, 32],
20+
"group_head_and_tail": true,
21+
"chain_style": true
22+
}
23+
}
24+
EOF
25+
)
26+
EXTRACTOR_CONFIG=$(echo $extractor_config_json_str | base64 -w 0)
27+
python3 -m graph_net.torch.single_device_runner --model-path $GRAPH_NET_ROOT/../samples/$MODEL_PATH_IN_SAMPLES --enable-extract True --extract-name $MODEL_NAME --dump-graph-hash-key --extractor-config=$EXTRACTOR_CONFIG
28+
29+
FILE_PATH=$GRAPH_NET_DECOMPOSE_PATH/decomposer
30+
mkdir -p "$(dirname "$FILE_PATH/log.log")"
31+
MODEL_PATH="$GRAPH_NET_DECOMPOSE_PATH/$MODEL_NAME"
32+
33+
python -m graph_net.torch.test_compiler \
34+
--model-path $MODEL_PATH \
35+
--compiler range_decomposer_validator \
36+
--device cuda > "$FILE_PATH/log.log" 2>&1
37+
38+
python -m graph_net.log2json \
39+
--log-file "$FILE_PATH/log.log" \
40+
--output-dir "$FILE_PATH/JSON_results/"
41+
42+
python -m graph_net.plot_ESt \
43+
--benchmark-path "$FILE_PATH/JSON_results/" \
44+
--output-dir "$FILE_PATH"
45+
46+
echo "=================================================="
47+
echo "Results saved in: $FILE_PATH/ES_result.png"
48+
echo ""
49+
echo "IMPORTANT: Please verify if the curve in ES_result.png is a straight line"
50+
echo "If the curve is NOT a straight line, please check the log file: $FILE_PATH/log.log"
51+
echo "=================================================="

graph_net/test/naive_graph_decomposer_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ EOF
2121
EXTRACTOR_CONFIG=$(echo $extractor_config_json_str | base64 -w 0)
2222

2323
mkdir -p /tmp/naive_decompose_workspace
24-
python3 -m graph_net.torch.single_device_runner --model-path $GRAPH_NET_ROOT/../samples/$MODEL_PATH_IN_SAMPLES --enable-extract True --extract-name resnet18 --dump-graph-hash-key --extractor-config=$EXTRACTOR_CONFIG
24+
python3 -m graph_net.torch.single_device_runner --model-path $GRAPH_NET_ROOT/../samples/$MODEL_PATH_IN_SAMPLES --enable-extract True --extract-name resnet18 --dump-graph-hash-key --extractor-config=$EXTRACTOR_CONFIG
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import torch
2+
import torch.nn as nn
3+
import os
4+
import sys
5+
import inspect
6+
import importlib.util
7+
import itertools
8+
from typing import List, Tuple, Dict, Any, Callable
9+
10+
11+
class ComposedModel(nn.Module):
12+
def __init__(self, subgraph: List[nn.Module]):
13+
super().__init__()
14+
self.subgraphs = nn.ModuleList(subgraph)
15+
16+
def forward(self, **kwargs):
17+
subgraph_intput = {
18+
key.replace("L", "l_l", 1): value
19+
for key, value in kwargs.items()
20+
if key.startswith("L")
21+
}
22+
23+
output = None
24+
for subgraph in self.subgraphs:
25+
if output is None:
26+
output = subgraph(**subgraph_intput)
27+
else:
28+
output = subgraph(*output)
29+
30+
return output
31+
32+
33+
class RangeDecomposerValidatorBackend:
34+
def _load_model_instance(self, path: str, device: str) -> torch.nn.Module:
35+
class_name = "GraphModule"
36+
model_file = os.path.join(path, "model.py")
37+
38+
spec = importlib.util.spec_from_file_location(class_name, model_file)
39+
module = importlib.util.module_from_spec(spec)
40+
spec.loader.exec_module(module)
41+
42+
ModelClass = getattr(module, class_name)
43+
instance = ModelClass().to(device)
44+
return instance
45+
46+
def __call__(self, model: torch.nn.Module) -> torch.nn.Module:
47+
model_file_path = model.__class__.__graph_net_file_path__
48+
model_dir = os.path.dirname(model_file_path)
49+
decomposed_parent_dir = model_dir + "_decomposed"
50+
subgraph_paths = []
51+
for name in sorted(os.listdir(decomposed_parent_dir)):
52+
full_path = os.path.join(decomposed_parent_dir, name)
53+
if os.path.isdir(full_path) and name[-1].isdigit():
54+
subgraph_paths.append(full_path)
55+
56+
print(
57+
f"[RangeDecomposerValidatorBackend] Found subgraphs: {[os.path.basename(p) for p in subgraph_paths]}"
58+
)
59+
60+
device = model.__class__.__graph_net_device__
61+
subgraph_instances = []
62+
63+
for path in subgraph_paths:
64+
instance = self._load_model_instance(path, device)
65+
subgraph_instances.append(instance)
66+
dir_name = os.path.basename(path)
67+
print(
68+
f"[RangeDecomposerValidatorBackend] Loaded and instantiated '{dir_name}'"
69+
)
70+
71+
composed_model = ComposedModel(subgraph_instances)
72+
return composed_model.eval()
73+
74+
def synchronize(self):
75+
if torch.cuda.is_available():
76+
torch.cuda.synchronize()

0 commit comments

Comments
 (0)