-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. add k210 pipeline docs in USAGE.md
2. add simulate.ipynb for compile and inference
- Loading branch information
yanghaoqi
committed
Jul 26, 2023
1 parent
b57f35b
commit 1d71270
Showing
4 changed files
with
394 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "82a8f9c1-c2bf-4270-9f1f-ac25c9fdd898", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Requirement already satisfied: pip in /usr/local/lib/python3.8/site-packages (23.2.1)\n", | ||
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", | ||
"\u001b[0mCollecting nncase==1.9.0.20230322\n", | ||
" Downloading nncase-1.9.0.20230322-cp38-cp38-manylinux_2_24_x86_64.whl (10.0 MB)\n", | ||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.0/10.0 MB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m0:01\u001b[0m0m\n", | ||
"\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.8/site-packages (from nncase==1.9.0.20230322) (1.23.5)\n", | ||
"Installing collected packages: nncase\n", | ||
" Attempting uninstall: nncase\n", | ||
" Found existing installation: nncase 2.1.0.20230703\n", | ||
" Uninstalling nncase-2.1.0.20230703:\n", | ||
" Successfully uninstalled nncase-2.1.0.20230703\n", | ||
"\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", | ||
"nncase-kpu 2.1.0.20230703 requires nncase>=2.1, but you have nncase 1.9.0.20230322 which is incompatible.\u001b[0m\u001b[31m\n", | ||
"\u001b[0mSuccessfully installed nncase-1.9.0.20230322\n", | ||
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n", | ||
"\u001b[0m" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"!pip install --upgrade pip\n", | ||
"# !pip uninstall -y nncase\n", | ||
"# !pip install nncase==1.8.0.20220929 --timeout=1000\n", | ||
"!pip install nncase==1.9.0.20230322 --timeout=1000\n", | ||
"#from versions: 1.0.0.20211029, 1.1.0.20211203, 1.3.0.20220127, 1.4.0.20220303, 1.5.0.20220331, \n", | ||
"# 1.6.0.20220505, 1.7.0.20220530, 1.7.1.20220701, 1.8.0.20220929, 1.9.0.20230322, 2.0.0.20230602, 2.1.0.20230703)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "44f95a15-936d-46f7-8740-9f7432a3231c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"env: PATH=$PATH:/usr/local/lib/python3.8/site-packages\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# export nncase lib path into PATH\n", | ||
"%env PATH=$PATH:/usr/local/lib/python3.8/site-packages" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "a7eff82e-295c-4cce-afbc-ce64c84dc40a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import nncase\n", | ||
"from nncase_base_func import *\n", | ||
"# from parse_model import *\n", | ||
"\n", | ||
"\n", | ||
"def compile_kmodel(model_path, dump_path, calib_data):\n", | ||
" \"\"\"\n", | ||
" Set compile options and ptq options.\n", | ||
" Compile kmodel.\n", | ||
" Dump the compile-time result to 'compile_options.dump_dir'\n", | ||
" \"\"\"\n", | ||
" print(\"----------model simplify----------\")\n", | ||
" model_file = model_simplify(model_path)\n", | ||
"\n", | ||
" print(\"---------- set options ----------\")\n", | ||
" # import_options\n", | ||
" import_options = nncase.ImportOptions()\n", | ||
" \n", | ||
" # compile_options\n", | ||
" compile_options = nncase.CompileOptions()\n", | ||
" compile_options.target = \"k210\" # \"cpu\"\n", | ||
" compile_options.dump_ir = True # if False, will not dump the compile-time result.\n", | ||
" compile_options.dump_asm = True\n", | ||
" compile_options.dump_dir = dump_path\n", | ||
"\n", | ||
" # preprocess args\n", | ||
" compile_options.preprocess = True\n", | ||
" if compile_options.preprocess:\n", | ||
" compile_options.input_type = \"uint8\" # \"uint8\"\n", | ||
" compile_options.swapRB = False\n", | ||
" compile_options.input_shape = [1,224,320,3]\n", | ||
" compile_options.input_range = [0,1]\n", | ||
" compile_options.mean = [0,0,0]\n", | ||
" compile_options.std = [1,1,1]\n", | ||
" compile_options.input_layout = \"NHWC\" # \"NHWC\"\n", | ||
" compile_options.output_layout = \"NHWC\" # \"NHWC\"\n", | ||
" \n", | ||
" # quant args\n", | ||
" compile_options.quant_type = \"uint8\" \n", | ||
" compile_options.w_quant_type = \"uint8\"\n", | ||
" compile_options.use_mse_quant_w = True\n", | ||
" compile_options.split_w_to_act = False\n", | ||
"\n", | ||
" # quant options\n", | ||
" ptq_options = nncase.PTQTensorOptions()\n", | ||
" ptq_options.calibrate_method = \"no_clip\" # \"kld_m2\" \"l2\" \"cdf\"\n", | ||
" ptq_options.samples_count = 80\n", | ||
" # print(len(calib_data[0]))\n", | ||
" # ptq_options.set_tensor_data(np.array(calib_data).tobytes())\n", | ||
" ptq_options.set_tensor_data(calib_data[:].tobytes())\n", | ||
"\n", | ||
" \n", | ||
" # set options\n", | ||
" compiler = nncase.Compiler(compile_options)\n", | ||
" compiler.use_ptq(ptq_options)\n", | ||
" \n", | ||
" print(\"---------- compile ----------\")\n", | ||
" # import\n", | ||
" model_content = read_model_file(model_file)\n", | ||
" if model_path.split(\".\")[-1] == \"onnx\":\n", | ||
" compiler.import_onnx(model_content, import_options)\n", | ||
" elif model_path.split(\".\")[-1] == \"tflite\":\n", | ||
" compiler.import_tflite(model_content, import_options)\n", | ||
"\n", | ||
" # compile\n", | ||
" compiler.compile()\n", | ||
" kmodel = compiler.gencode_tobytes()\n", | ||
" \n", | ||
" kmodel_path = os.path.join(dump_path, \"test.kmodel\")\n", | ||
" with open(kmodel_path, 'wb') as f:\n", | ||
" f.write(kmodel)\n", | ||
" print(\"---------- compile end ----------\")\n", | ||
" return kmodel_path\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c957fe20-99c9-4a54-bae8-38361a8f8830", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# compile kmodel\n", | ||
"model_path = \"./model_f32.tflite\"\n", | ||
"dump_path = \"./tmp_tflite\"\n", | ||
"\n", | ||
"# If model has multi inputs, calib_data format is \"[[x1, x2,...], [y1, y2,...], ...]\"\n", | ||
"# e.g. Model has three inputs (x, y, z), the calib_data is '[[x1, x2, x3],[y1, y2, y3],[z1, z2, z3]]'\n", | ||
"\n", | ||
"calib_data = [[np.random.rand(1,224,320,3).astype(np.float32), np.random.rand(1,224,320,3).astype(np.float32)]]\n", | ||
"# calib_data = np.load(\"./qual_data.npy\")\n", | ||
"# print(calib_data.shape)\n", | ||
"kmodel_path = compile_kmodel(model_path, dump_path, calib_data)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7f617edc-781c-4b8b-b45d-fef2f0b36a46", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# run kmodel(simulate)\n", | ||
"kmodel_path = \"./tmp_tflite/test.kmodel\"\n", | ||
"# input_data = [np.random.rand(1,224,320,3).astype(np.float32)]\n", | ||
"input_data = [np.load(\"./qual_data.npy\")[:1]]\n", | ||
"\n", | ||
"result = run_kmodel(kmodel_path, input_data)\n", | ||
"for idx, i in enumerate(result):\n", | ||
" print(i.shape)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.9" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.