Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 54 additions & 0 deletions tools/onnx_subgraph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# onnx_autosubgraph
onnx-subgraph tool provides model auto partitionioning of onnx model to several sub models by
operator, performance and model size limitations,with the order and input / output names of
sub models

# How to build the onnx-subgraph
## OS environment dependence
1. ubuntu >=20.04
2. GCC >= 9.4.0
3. cmake >= 3.10
4. python >= 3.8
5. apt-get install libprotobuf-dev protobuf-compiler libjsoncpp-dev

## Python packages dependence
onnx 1.16.0
onnxruntime 1.18.1
onnxsim 0.4.36
torch 2.3.1
scikit-image
scikit-learn
pandas
tqdm

## building the onnx-subgraph
1. cd onnx-subgraph
2. mkdir build & cd build
3. cmake .. & make
4. we can get following output at ./build
└── scripts
├── extract_onnx.py
└── test_model_download.sh
└── subgraphs_ios.txt

# How to use the onnx-subgraph
## Pre-steps
### Download the test AI models
1. bash scripts/test_model_download.sh, then "resnet-test.onnx" will be got in ./build
2. you can change to any other onnx files as your needs, or edit the download link in
"scripts/test_model_download.sh"

## Parse the onnx model
note: subgraphs_ios.txt will be generated in future code, suppose we already have it as
the example file now

## Split the onnx model to subgraphs
1. python scripts/extract_onnx.py \
-s ./scripts/subgraphs_ios.txt \
-m ./resnet-test.onnx
after extraction done, the subgraphs will be saved at './subgraphs'
subgraphs
├── CPUsubgraph0.onnx
└── CPUsubgraph1.onnx
├── NPUsubgraph0.onnx
└── NPUsubgraph1.onnx
11 changes: 7 additions & 4 deletions tools/onnx_subgraph/extract_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ def split_onnx_ios(instrfile, input_path, out_folder='subgraphs/'):
model.graph.value_info.append(output)
onnx.save(model, input_path)

f1 = open(instrfile, "r")
lines = f1.readlines()
try:
with open(instrfile, "r") as f1:
lines = f1.readlines()
except Exception as e:
print(e)
raise

cpu_count = 0
npu_count = 0
count = 0
Expand All @@ -61,8 +66,6 @@ def split_onnx_ios(instrfile, input_path, out_folder='subgraphs/'):
print("succeed", count)
count = count + 1

f1.close()


if __name__ == "__main__":
arg_parser = argparse.ArgumentParser()
Expand Down