From 9320f5a901ee8249f7554a89ff956d816be07aae Mon Sep 17 00:00:00 2001 From: Youxin Chen Date: Fri, 25 Apr 2025 18:56:34 +0800 Subject: [PATCH 1/4] [tools/onnx-subgraph] add functions for onnx model partition add functions definition for onnx model partition ONE-DCO-1.0-Signed-off-by: Youxin Chen --- tools/onnx_subgraph/include/partition.h | 53 +++++++++++++++++++++++ tools/onnx_subgraph/src/lib/partition.cpp | 28 ++++++++++++ tools/onnx_subgraph/src/main.cpp | 4 ++ 3 files changed, 85 insertions(+) create mode 100644 tools/onnx_subgraph/include/partition.h create mode 100644 tools/onnx_subgraph/src/lib/partition.cpp diff --git a/tools/onnx_subgraph/include/partition.h b/tools/onnx_subgraph/include/partition.h new file mode 100644 index 00000000000..8975aa2f1ab --- /dev/null +++ b/tools/onnx_subgraph/include/partition.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PARTITION_H +#define PARTITION_H + +#include "onnx.pb.h" +#include +#include +#include +#include +#include +#include "device.h" +#include "graph.h" + +// deprecated +enum PartitionStrategy +{ + SPILTE_CPU_STRUCTURE_FIRST, + SPILTE_NPU_STRUCTURE_FIRST, + AUTOMATIC_SEARCH +}; + +/** + * @brief Partition the ONNX graph into subgraphs and produce cutting instructions. + * + * @param [in] g The ONNX graph to be partitioned. + * @param [in] d The device information for partitioning. + * @param [in] strategy The partition strategy to be used (deprecated). + * @param [in] node_io_size The input/output size information for each node. + * @pre The ONNX graph should be valid and the device information should be properly set. + * @post The graph is partitioned into subgraphs, and the results are stored in Subgraphs and + * otherSubgraphs. + * @exception None + * @return None + */ +void PartitionGraph(const onnx::GraphProto &g, Device &d, PartitionStrategy strategy, + const std::unordered_map &node_io_size); + +#endif // PARTITION_H diff --git a/tools/onnx_subgraph/src/lib/partition.cpp b/tools/onnx_subgraph/src/lib/partition.cpp new file mode 100644 index 00000000000..9c82c47fb41 --- /dev/null +++ b/tools/onnx_subgraph/src/lib/partition.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "partition.h" +#include +#include +#include +#define MAX_DEPTH 1000 +std::vector Subgraphs; + +void PartitionGraph(const onnx::GraphProto &g, Device &d, PartitionStrategy strategy, + const std::unordered_map &node_io_size) +{ + return; +} diff --git a/tools/onnx_subgraph/src/main.cpp b/tools/onnx_subgraph/src/main.cpp index 1d41d9d72b7..39f2c5b6684 100644 --- a/tools/onnx_subgraph/src/main.cpp +++ b/tools/onnx_subgraph/src/main.cpp @@ -16,6 +16,7 @@ #include "graph.h" #include "device.h" +#include "partition.h" namespace fs = std::filesystem; @@ -79,6 +80,9 @@ int main(int argc, char *argv[]) Device target; target.updateOnnxFile(onnxFile); target.GetDeviceJson(confFile); + std::unordered_map node_io_size; + PartitionGraph(g, target, PartitionStrategy::SPILTE_CPU_STRUCTURE_FIRST, node_io_size); + std::cout << "PartitionGraph done." << std::endl; return 0; } From d54e72ec1606cb21288722ad6da699ca5321a877 Mon Sep 17 00:00:00 2001 From: Youxin Chen Date: Mon, 28 Apr 2025 08:49:41 +0800 Subject: [PATCH 2/4] update the uncompatible comment in code --- tools/onnx_subgraph/include/partition.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/onnx_subgraph/include/partition.h b/tools/onnx_subgraph/include/partition.h index 8975aa2f1ab..4511098802a 100644 --- a/tools/onnx_subgraph/include/partition.h +++ b/tools/onnx_subgraph/include/partition.h @@ -26,7 +26,7 @@ #include "device.h" #include "graph.h" -// deprecated +// Priority setting when OPs can be supported by both CPU & NPU, and performance is same on both enum PartitionStrategy { SPILTE_CPU_STRUCTURE_FIRST, From f59b3c2b338cf4f7102ff2f9e50576ea9cdf2e5a Mon Sep 17 00:00:00 2001 From: Youxin Chen Date: Mon, 28 Apr 2025 09:45:55 +0800 Subject: [PATCH 3/4] update code as review comment --- tools/onnx_subgraph/include/partition.h | 11 +++-------- tools/onnx_subgraph/src/lib/partition.cpp | 2 ++ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/tools/onnx_subgraph/include/partition.h b/tools/onnx_subgraph/include/partition.h index 4511098802a..4de5b53678a 100644 --- a/tools/onnx_subgraph/include/partition.h +++ b/tools/onnx_subgraph/include/partition.h @@ -14,15 +14,10 @@ * limitations under the License. */ -#ifndef PARTITION_H -#define PARTITION_H +#ifndef __TOOLS_ONNX_SUBGRAPH_PARTITION_H__ +#define __TOOLS_ONNX_SUBGRAPH_PARTITION_H__ #include "onnx.pb.h" -#include -#include -#include -#include -#include #include "device.h" #include "graph.h" @@ -50,4 +45,4 @@ enum PartitionStrategy void PartitionGraph(const onnx::GraphProto &g, Device &d, PartitionStrategy strategy, const std::unordered_map &node_io_size); -#endif // PARTITION_H +#endif // __TOOLS_ONNX_SUBGRAPH_PARTITION_H__ diff --git a/tools/onnx_subgraph/src/lib/partition.cpp b/tools/onnx_subgraph/src/lib/partition.cpp index 9c82c47fb41..3e2109435ff 100644 --- a/tools/onnx_subgraph/src/lib/partition.cpp +++ b/tools/onnx_subgraph/src/lib/partition.cpp @@ -18,7 +18,9 @@ #include #include #include + #define MAX_DEPTH 1000 + std::vector Subgraphs; void PartitionGraph(const onnx::GraphProto &g, Device &d, PartitionStrategy strategy, From 6ee56baa00e3ac9c7ebcc5b7635f44940fd91843 Mon Sep 17 00:00:00 2001 From: Youxin Chen Date: Mon, 28 Apr 2025 10:28:29 +0800 Subject: [PATCH 4/4] remove global variable using --- tools/onnx_subgraph/src/lib/partition.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/onnx_subgraph/src/lib/partition.cpp b/tools/onnx_subgraph/src/lib/partition.cpp index 3e2109435ff..4159932e72d 100644 --- a/tools/onnx_subgraph/src/lib/partition.cpp +++ b/tools/onnx_subgraph/src/lib/partition.cpp @@ -21,8 +21,6 @@ #define MAX_DEPTH 1000 -std::vector Subgraphs; - void PartitionGraph(const onnx::GraphProto &g, Device &d, PartitionStrategy strategy, const std::unordered_map &node_io_size) {