Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tools/onnx_subgraph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ include_directories(${Python3_INCLUDE_DIRS})

set(ONNX_SUBGRAPH_FILES
test_model_download.sh
extract_onnx.py
subgraphs_ios.txt
)
foreach(ONNX_SUBGRAPH IN ITEMS ${ONNX_SUBGRAPH_FILES})
set(ONNX_SUBGRAPH_FILE ${ONNX_SUBGRAPH})
Expand Down
82 changes: 82 additions & 0 deletions tools/onnx_subgraph/extract_onnx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 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.

import onnx
import re
import os
import argparse


def split_subgraph_ios(iofile):
iolist = re.split('--input-name |;--output-name ', iofile)
in_ = iolist[1].split(';')
out_ = iolist[2].split(';')
del out_[-1]
type = iolist[0].split('subgraph')[0]
return in_, out_, type


def split_onnx_ios(instrfile, input_path, out_folder='subgraphs/'):
os.makedirs(out_folder, exist_ok=True)

model = onnx.load(input_path)
onnx.checker.check_model(input_path)
for output in model.graph.output:
model.graph.value_info.append(output)
onnx.save(model, input_path)

f1 = open(instrfile, "r")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional) we may need to check file open error, maybe later.

lines = f1.readlines()
cpu_count = 0
npu_count = 0
count = 0

if not os.path.exists(out_folder):
os.makedirs(out_folder)

for line in lines:
input_names, output_names, type = split_subgraph_ios(line)
if (type == 'CPU'):
count = cpu_count
cpu_count = cpu_count + 1
else:
count = npu_count
npu_count = npu_count + 1

output_path_folder = out_folder
if not os.path.exists(output_path_folder):
os.makedirs(output_path_folder)
output_path = output_path_folder + type + 'subgraph' + str(count) + '.onnx'

if ((input_names != ['']) and (output_names != [''])):
onnx.utils.extract_model(input_path, output_path, input_names, output_names)
print("succeed", count)
count = count + 1

f1.close()


if __name__ == "__main__":
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-s',
'--subio',
default='./scripts/subgraphs_ios.txt',
help="set subgraphs input/output node information")
arg_parser.add_argument('-m',
'--model',
default='./resnet-test.onnx',
help="set onnx model path")
args = arg_parser.parse_args()

split_onnx_ios(args.subio, args.model)
4 changes: 4 additions & 0 deletions tools/onnx_subgraph/subgraphs_ios.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NPUsubgraph0: order0--input-name x;--output-name /stem/conv3/bn/act/Mul_output_0;
NPUsubgraph1: order2--input-name /stem/pool/MaxPool_output_0;--output-name /stages/stages.3/stages.3.1/act/Mul_output_0;
CPUsubgraph0: order1--input-name /stem/conv3/bn/act/Mul_output_0;--output-name /stem/pool/MaxPool_output_0;
CPUsubgraph1: order3--input-name /stages/stages.3/stages.3.1/act/Mul_output_0;--output-name 316;