From f7f39f33bfa0b158277e28928aec3d5ce71894bd Mon Sep 17 00:00:00 2001 From: chenyx113 Date: Thu, 20 Feb 2025 20:47:16 +0800 Subject: [PATCH 1/6] [tools/onnx-subgraph] Add readme.md for environment and existing code 1. add exception process for file open and IO 2. add readme.md for previous submittings ONE-DCO-1.0-Signed-off-by: Youxin Chen --- tools/onnx_subgraph/README.md | 54 +++++++++++++++++++++++++++++ tools/onnx_subgraph/extract_onnx.py | 11 +++--- 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tools/onnx_subgraph/README.md diff --git a/tools/onnx_subgraph/README.md b/tools/onnx_subgraph/README.md new file mode 100644 index 00000000000..7d5b00b21c3 --- /dev/null +++ b/tools/onnx_subgraph/README.md @@ -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 diff --git a/tools/onnx_subgraph/extract_onnx.py b/tools/onnx_subgraph/extract_onnx.py index 4204901155b..76133885b2f 100644 --- a/tools/onnx_subgraph/extract_onnx.py +++ b/tools/onnx_subgraph/extract_onnx.py @@ -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 @@ -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() From fdbb6e60eb2e9d2c4bd8fd8bee5f270ec0d69a90 Mon Sep 17 00:00:00 2001 From: chenyx113 Date: Fri, 21 Feb 2025 09:19:33 +0800 Subject: [PATCH 2/6] [tools/onnx-subgraph]recovery last version code commit file read exception protection in future PR --- tools/onnx_subgraph/extract_onnx.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tools/onnx_subgraph/extract_onnx.py b/tools/onnx_subgraph/extract_onnx.py index 76133885b2f..4204901155b 100644 --- a/tools/onnx_subgraph/extract_onnx.py +++ b/tools/onnx_subgraph/extract_onnx.py @@ -36,13 +36,8 @@ def split_onnx_ios(instrfile, input_path, out_folder='subgraphs/'): model.graph.value_info.append(output) onnx.save(model, input_path) - try: - with open(instrfile, "r") as f1: - lines = f1.readlines() - except Exception as e: - print(e) - raise - + f1 = open(instrfile, "r") + lines = f1.readlines() cpu_count = 0 npu_count = 0 count = 0 @@ -66,6 +61,8 @@ 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() From b9140f7e95f44131d6d1c8b5c7c526dd01bace6c Mon Sep 17 00:00:00 2001 From: chenyx113 Date: Fri, 21 Feb 2025 10:05:51 +0800 Subject: [PATCH 3/6] [tools/onnx-subgraph] update format issue as review comments [tools/onnx-subgraph] update format issue as review comments --- tools/onnx_subgraph/README.md | 38 +++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/tools/onnx_subgraph/README.md b/tools/onnx_subgraph/README.md index 7d5b00b21c3..9db4d65307c 100644 --- a/tools/onnx_subgraph/README.md +++ b/tools/onnx_subgraph/README.md @@ -1,7 +1,7 @@ # 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 +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 @@ -22,33 +22,41 @@ sub models 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 + ''' + cd onnx-subgraph + mkdir build & cd build + cmake .. & make + ''' + 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 + 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 + 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 \ + ''' + 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 + ''' From 892aea4196dd49a3e6268d16a083fc8da9127974 Mon Sep 17 00:00:00 2001 From: chenyx113 Date: Fri, 21 Feb 2025 11:39:47 +0800 Subject: [PATCH 4/6] update the format checking issue in readme.md update the format checking issue in readme.md --- tools/onnx_subgraph/README.md | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/tools/onnx_subgraph/README.md b/tools/onnx_subgraph/README.md index 9db4d65307c..3cfa25959ce 100644 --- a/tools/onnx_subgraph/README.md +++ b/tools/onnx_subgraph/README.md @@ -22,41 +22,41 @@ sub models. tqdm ## building the onnx-subgraph - ''' + ``` cd onnx-subgraph mkdir build & cd build cmake .. & make - ''' + ``` we can get following output at './build' - ''' - scripts - ├── extract_onnx.py - └── test_model_download.sh - └── subgraphs_ios.txt - ''' + ``` + 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" + 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 + 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 - ''' + ``` 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 - ''' + ``` + after extraction done, the subgraphs will be saved at './subgraphs' + ``` + subgraphs + ├── CPUsubgraph0.onnx + └── CPUsubgraph1.onnx + ├── NPUsubgraph0.onnx + └── NPUsubgraph1.onnx + ``` From 9fa575ac82a383c40f47c8fcbc63a8a30a40e8b8 Mon Sep 17 00:00:00 2001 From: chenyx113 Date: Fri, 21 Feb 2025 13:06:46 +0800 Subject: [PATCH 5/6] Update README.md format with target script type Update README.md format with target script type --- tools/onnx_subgraph/README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/onnx_subgraph/README.md b/tools/onnx_subgraph/README.md index 3cfa25959ce..d21eda4517f 100644 --- a/tools/onnx_subgraph/README.md +++ b/tools/onnx_subgraph/README.md @@ -22,18 +22,18 @@ sub models. tqdm ## building the onnx-subgraph - ``` +```bash cd onnx-subgraph mkdir build & cd build cmake .. & make - ``` +``` we can get following output at './build' - ``` +```bash scripts ├── extract_onnx.py └── test_model_download.sh └── subgraphs_ios.txt - ``` +``` # How to use the onnx-subgraph ## Pre-steps @@ -47,16 +47,16 @@ sub models. the example file now. ## Split the onnx model to subgraphs - ``` +```bash python scripts/extract_onnx.py \ -s ./scripts/subgraphs_ios.txt \ -m ./resnet-test.onnx - ``` +``` after extraction done, the subgraphs will be saved at './subgraphs' - ``` +```bash subgraphs ├── CPUsubgraph0.onnx └── CPUsubgraph1.onnx ├── NPUsubgraph0.onnx └── NPUsubgraph1.onnx - ``` +``` From bf9596158be4e90da57f42006d95efee0b68d87b Mon Sep 17 00:00:00 2001 From: chenyx113 Date: Fri, 21 Feb 2025 13:24:57 +0800 Subject: [PATCH 6/6] Update README.md remove blank space Update README.md remove blank space --- tools/onnx_subgraph/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/onnx_subgraph/README.md b/tools/onnx_subgraph/README.md index d21eda4517f..b3b3ae67acd 100644 --- a/tools/onnx_subgraph/README.md +++ b/tools/onnx_subgraph/README.md @@ -1,6 +1,6 @@ # 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 +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