Skip to content

Commit

Permalink
更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinwei committed Sep 13, 2024
1 parent 1643284 commit dff5fe6
Show file tree
Hide file tree
Showing 126 changed files with 3,847 additions and 934 deletions.
1 change: 1 addition & 0 deletions doc/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ api/
*.torchscript
*.onnx
tuning_logs/
*.pb
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
164 changes: 164 additions & 0 deletions doc/temp-tutorials/ai/pytorch-model.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# PyTorch 模型"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.extend([\"..\"])\n",
"import set_env"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from tvm_book.tools.frontends import Frontend, TrainInputConfig\n",
"from tvm_book.tools import display"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"pytorch 前端模型配置:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(display.Tree(\"| \")(\"models/pytorch_demo\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"{icon}`fa-solid fa-folder-open` `pytorch_demo/` 文件夹下存在如下内容:\n",
"\n",
"- {icon}`fa-solid fa-file` `save.py` 存储 PyTorch 模型为 `resnet18.pth`\n",
" ```{include} models/pytorch_demo/save.py\n",
" :code: python\n",
" ```\n",
"- {icon}`fa-solid fa-file` `resnet18.pth` 存储 PyTorch 模型结构与参数\n",
"- {icon}`fa-solid fa-file` `config.toml` 存储 PyTorch 模型配置信息\n",
" ```{include} models/pytorch_demo/config.toml\n",
" :code: toml\n",
" ```"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import toml\n",
"\n",
"config_path = \"models/pytorch_demo/config.toml\"\n",
"\n",
"config_path = Path(config_path) \n",
"config = toml.load(config_path)\n",
"model_type = config['model'][\"model_type\"]\n",
"if len(config['train_inputs']) == 1:\n",
" input_config = config['train_inputs'][0]\n",
"if model_type == \"pytorch\":\n",
" shape_dict = {input_config[\"name\"]: input_config[\"shape\"]}\n",
" model = Frontend(model_type).load(f\"{config_path.parent}/{config['model']['path']}\", shape_dict=shape_dict)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import tvm\n",
"import tvm.relay as relay\n",
"from tvm.relay.build_module import bind_params_by_name\n",
"from tvm.ir.instrument import (\n",
" PassTimingInstrument,\n",
" pass_instrument,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"def get_calibration_dataset(mod, input_name):\n",
" dataset = []\n",
" input_shape = [int(x) for x in mod[\"main\"].checked_type.arg_types[0].shape]\n",
" for i in range(5):\n",
" data = np.random.uniform(size=input_shape)\n",
" dataset.append({input_name: data})\n",
" return dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataset = get_calibration_dataset(model.mod, \"x\")\n",
"BASE_CFG = {\n",
" \"skip_conv_layers\": [],\n",
" \"skip_dense_layers\": False,\n",
" \"dtype_input\": \"int8\",\n",
" \"dtype_weight\": \"int8\",\n",
" \"dtype_activation\": \"int32\",\n",
"}\n",
"with tvm.transform.PassContext(opt_level=3):\n",
" with relay.quantize.qconfig(**BASE_CFG, calibrate_mode=\"percentile\"):\n",
" qmod = relay.quantize.quantize(model.mod, params=model.params, dataset=dataset)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "xx",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
6 changes: 2 additions & 4 deletions doc/temp-tutorials/frontend/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# 前端模型
# 前端模型(temp)

```{toctree}
mxnet
tensorflow-tvm/index
tf-tvm/index
pytorch-tvm/index
onnx-tvm/index
onnxscript/index
caffe-tvm/index
relax-fx
```
7 changes: 0 additions & 7 deletions doc/temp-tutorials/frontend/onnx-tvm/index.md

This file was deleted.

24 changes: 0 additions & 24 deletions doc/temp-tutorials/frontend/onnx-tvm/onnxruntime-start.ipynb

This file was deleted.

146 changes: 0 additions & 146 deletions doc/temp-tutorials/frontend/tensorflow-tvm/from-tensorflow.ipynb

This file was deleted.

Loading

0 comments on commit dff5fe6

Please sign in to comment.