Skip to content

Commit 807b228

Browse files
committed
Benchmark parallel and fusion variants
1 parent e13836a commit 807b228

4 files changed

Lines changed: 105 additions & 4 deletions

File tree

app/Graph/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ target_include_directories(BuildGraph PUBLIC ${CMAKE_SOURCE_DIR}/3rdparty/Json/i
1919

2020
add_executable(Graph_Build graph_build.cpp)
2121
target_link_libraries(Graph_Build BuildGraph)
22+
target_link_libraries(Graph_Build graphT_lib)
2223

2324
add_executable(ACC acc_check.cpp)
2425
target_link_libraries(ACC BuildGraph)

app/Graph/graph_build.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,55 @@
33
#include <unordered_map>
44

55
#include "build.hpp"
6+
#include "graph_transformations/graph_transformations.hpp"
7+
#include "layers_fused/ConvRelu.hpp"
68

79
namespace fs = std::filesystem;
810
using namespace it_lab_ai;
911

12+
namespace {
13+
14+
enum class FusionMode { kOff, kPostops, kConvRelu };
15+
16+
FusionMode parse_fusion_mode(const std::string& value) {
17+
if (value == "off") {
18+
return FusionMode::kOff;
19+
}
20+
if (value == "postops") {
21+
return FusionMode::kPostops;
22+
}
23+
if (value == "convrelu") {
24+
return FusionMode::kConvRelu;
25+
}
26+
throw std::invalid_argument("Unknown fusion mode: " + value);
27+
}
28+
29+
void apply_conv_relu_fusion(Graph& graph, Tensor& output,
30+
const RuntimeOptions& options) {
31+
if (options.backend == Backend::kOneDnn) {
32+
throw std::invalid_argument(
33+
"convrelu fusion is not supported with oneDNN backend");
34+
}
35+
36+
Graph subgraph;
37+
Tensor dummy_input = make_tensor(std::vector<int>({0}));
38+
auto conv = std::make_shared<ConvolutionalLayer>();
39+
auto relu = std::make_shared<EWLayer>("relu");
40+
subgraph.setInput(conv, dummy_input);
41+
subgraph.makeConnection(conv, relu);
42+
43+
Graph fused_graph;
44+
auto fused_layer = std::make_shared<ConvReluLayer>();
45+
changed_subgraphs(graph, subgraph, fused_layer, fused_graph, output, options);
46+
graph = std::move(fused_graph);
47+
}
48+
49+
} // namespace
50+
1051
int main(int argc, char* argv[]) {
1152
std::string model_name = "alexnet_mnist";
1253
RuntimeOptions options;
54+
FusionMode fusion_mode = FusionMode::kPostops;
1355

1456
for (int i = 1; i < argc; ++i) {
1557
if (std::string(argv[i]) == "--model" && i + 1 < argc) {
@@ -47,6 +89,8 @@ int main(int argc, char* argv[]) {
4789
}
4890
} else if (std::string(argv[i]) == "--threads" && i + 1 < argc) {
4991
options.threads = std::stoi(argv[++i]);
92+
} else if (std::string(argv[i]) == "--fusion" && i + 1 < argc) {
93+
fusion_mode = parse_fusion_mode(argv[++i]);
5094
}
5195
}
5296

@@ -92,7 +136,11 @@ int main(int argc, char* argv[]) {
92136
std::vector<float> vec(75, 3);
93137
it_lab_ai::Tensor output = it_lab_ai::make_tensor(vec, sh1);
94138
Graph graph;
95-
build_graph_linear(graph, input, output, options, true);
139+
build_graph_linear(graph, input, output, options, true,
140+
fusion_mode == FusionMode::kPostops);
141+
if (fusion_mode == FusionMode::kConvRelu) {
142+
apply_conv_relu_fusion(graph, output, options);
143+
}
96144

97145
std::cout << "Starting inference..." << '\n';
98146
try {
@@ -133,6 +181,9 @@ int main(int argc, char* argv[]) {
133181

134182
Graph graph;
135183
build_graph(graph, input, output, json_path, options, false);
184+
if (fusion_mode == FusionMode::kConvRelu) {
185+
apply_conv_relu_fusion(graph, output, options);
186+
}
136187

137188
std::cout << "Starting inference..." << '\n';
138189
try {

benchmarks/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ Run selected models and variants:
4242
```bash
4343
python3 benchmarks/model_performance.py \
4444
--model googlenet,resnet \
45-
--variant seq \
46-
--variant parallel-tbb \
45+
--variant target \
4746
--repeat 3 \
4847
--warmup 1
4948
```
@@ -52,5 +51,10 @@ The JSON report includes `memory_samples` for every run. PNG plots are written
5251
to `benchmark_results/memory_plots` by default. Use `--samples-csv-out` to export
5352
the memory timeline to CSV and `--plots-dir` to choose another plot directory.
5453

54+
Use `--variant target` for the full target matrix: every supported parallel
55+
backend with fusion off/on, plus oneDNN with fusion off/on. Fusion-on uses the
56+
existing `Conv+Relu` fused layer for naive/parallel backends and existing
57+
post-ops mode for oneDNN.
58+
5559
Use `--strict-assets` to fail when a model JSON or input image directory is
5660
missing instead of skipping that model.

benchmarks/model_performance.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,46 @@
5757
"parallel-threads": ["--parallel", "threads"],
5858
"parallel-omp": ["--parallel", "omp"],
5959
"parallel-kokkos": ["--parallel", "kokkos"],
60+
"seq-fusion-off": ["--fusion", "off"],
61+
"seq-fusion-on": ["--fusion", "convrelu"],
62+
"parallel-tbb-fusion-off": ["--parallel", "tbb", "--fusion", "off"],
63+
"parallel-tbb-fusion-on": ["--parallel", "tbb", "--fusion", "convrelu"],
64+
"parallel-threads-fusion-off": ["--parallel", "threads", "--fusion", "off"],
65+
"parallel-threads-fusion-on": [
66+
"--parallel",
67+
"threads",
68+
"--fusion",
69+
"convrelu",
70+
],
71+
"parallel-omp-fusion-off": ["--parallel", "omp", "--fusion", "off"],
72+
"parallel-omp-fusion-on": ["--parallel", "omp", "--fusion", "convrelu"],
73+
"parallel-kokkos-fusion-off": ["--parallel", "kokkos", "--fusion", "off"],
74+
"parallel-kokkos-fusion-on": [
75+
"--parallel",
76+
"kokkos",
77+
"--fusion",
78+
"convrelu",
79+
],
80+
"onednn-fusion-off": ["--onednn", "--fusion", "off"],
81+
"onednn-fusion-on": ["--onednn", "--fusion", "postops"],
82+
}
83+
84+
VARIANT_GROUPS = {
85+
"all": list(VARIANT_ARGS),
86+
"target": [
87+
"seq-fusion-off",
88+
"seq-fusion-on",
89+
"parallel-tbb-fusion-off",
90+
"parallel-tbb-fusion-on",
91+
"parallel-threads-fusion-off",
92+
"parallel-threads-fusion-on",
93+
"parallel-omp-fusion-off",
94+
"parallel-omp-fusion-on",
95+
"parallel-kokkos-fusion-off",
96+
"parallel-kokkos-fusion-on",
97+
"onednn-fusion-off",
98+
"onednn-fusion-on",
99+
],
60100
}
61101

62102

@@ -142,10 +182,15 @@ def expand_choices(values: Sequence[str], choices: dict[str, object], default: s
142182
continue
143183
if item == "all":
144184
expanded.extend(choices)
185+
elif item in VARIANT_GROUPS and choices is VARIANT_ARGS:
186+
expanded.extend(VARIANT_GROUPS[item])
145187
elif item in choices:
146188
expanded.append(item)
147189
else:
148-
raise SystemExit(f"Unknown value '{item}'. Valid: all, {', '.join(choices)}")
190+
valid = ["all", *choices]
191+
if choices is VARIANT_ARGS:
192+
valid.extend(name for name in VARIANT_GROUPS if name != "all")
193+
raise SystemExit(f"Unknown value '{item}'. Valid: {', '.join(valid)}")
149194
return dedupe(expanded)
150195

151196

0 commit comments

Comments
 (0)