Skip to content

Commit d104a63

Browse files
authored
Merge pull request #32 from MegEngine/merge-tmconvert
merge tmconvert
2 parents 80c957b + 4f5d36e commit d104a63

File tree

124 files changed

+8809
-7402
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+8809
-7402
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,6 @@ jobs:
2828
- name: lint
2929
run: ./ci/lint.sh
3030

31-
pytest-transform:
32-
runs-on: ubuntu-latest
33-
container:
34-
image: enginesh233/mgeconvert_ci:v1.0
35-
defaults:
36-
run:
37-
shell: bash
38-
steps:
39-
- uses: actions/checkout@v2
40-
- name: test transform
41-
run: ./ci/pytest_transform.sh
42-
43-
pytest-cambricon:
44-
runs-on: ubuntu-latest
45-
container:
46-
image: enginesh233/mgeconvert_ci:v1.0
47-
defaults:
48-
run:
49-
shell: bash
50-
steps:
51-
- uses: actions/checkout@v2
52-
- name: build cambricon
53-
run: |
54-
source ./ci/utils.sh
55-
./ci/build_cambricon.sh
56-
- name: test cambricon
57-
run: |
58-
source ./ci/utils.sh
59-
./ci/pytest_cambricon.sh
60-
6131
pytest-caffe-and-onnx:
6232
runs-on: ubuntu-latest
6333
container:

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
__pycache__
2-
mgeconvert/caffe_converter/caffe_pb
3-
mgeconvert/tflite_converter/tflite/
2+
mgeconvert/backend/ir_to_caffe/caffe_pb
3+
mgeconvert/backend/ir_to_tflite/tflite/
44
*.tflite
55
*.onnx
66
*.so
77
.mypy_cache
8+
*.tm

README.md

Lines changed: 103 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
# MgeConvert
22

3-
适用于 [MegEngine](https://github.com/MegEngine/MegEngine) 的各种转换器, 目前支持的框架有 [Caffe](https://github.com/BVLC/caffe)[ONNX](https://github.com/onnx/onnx)、Cambricon 和 TFLite。
3+
适用于 [MegEngine](https://github.com/MegEngine/MegEngine) 的各种转换器, 目前支持的框架有 [Caffe](https://github.com/BVLC/caffe)[ONNX](https://github.com/onnx/onnx) 和 TFLite。
44

5-
支持的模型包括 ResNet、ResNext、ShuffleNet 等,如果需要适配其他模型, 可能需要添加更多的算子支持
5+
MgeConvert转换工具位于converters目录下,可直接调用其中的脚本将MegEngine导出的mge/TracedModule模型转换为第三方模型文件
66

7-
MgeConvert 前端的部分位于 `mge_context` 目录下, 可以直接将 MegEngine dump 出来的计算图转为图结构, 方便后端语言生成。
7+
MgeConvert转换器的结构包含前端、中间表示(IR)、后端三个部分:
8+
1. 前端的部分位于 `frontend` 目录下, 支持 mge 和 traced module 模型格式,可以将 MegEngine 序列化出来的计算图转为IR图结构
9+
2. IR部分位于 `converter_ir`目录下,包含图和 IR 算子定义、对计算图做变换的 transform rules 以及对量化模型处理的量化器
10+
3. 后端的部分位于 `backend` 目录下,包含caffe、onnx、tflite的转换器,可以将IR图结构转换为第三方框架的模型文件
811

12+
目前支持的模型包括 ResNet、ResNext、ShuffleNet 等,如果需要适配其他模型, 可能需要添加更多的算子支持。
913

1014
## 安装方式
1115

12-
MgeConvert 基于 MegEngine 工作,因此确保您的电脑已经安装 MegEngine。
16+
MgeConvert 基于 MegEngine 工作,因此确保您的电脑已经安装 MegEngine(>=1.0)
1317

14-
以 caffe 为例,下面这条指令将安装 caffe 转换器并处理相关依赖。
18+
以 caffe 为例,下面这条指令将安装开发版本的 caffe 转换器并处理相关依赖。
1519

1620
```bash
1721
python3 -m pip install git+https://github.com/MegEngine/mgeconvert.git --user --install-option="--targets=caffe"
1822
```
1923

20-
``--targets`` 的可选值有 ``caffe````onnx````cambricon````tflite````all``
24+
``--targets`` 的可选值有 ``caffe````onnx````tflite````all``
25+
26+
``tflite`` 转换器的schema默认使用r2.3版本,支持使用参数 ``tfversion`` 选择tflite schema的版本, 例如:
27+
28+
```bash
29+
--install-option="--targets=tflite --tfversion=r2.4"
30+
```
2131

2232
``all`` 代表安装全部转换器,不建议使用。可选值支持组合传入,比如 ``--targets=caffe,onnx``
2333

24-
> :warning: 由于 Cambricon SDK 未对外发布,寒武纪转换器将不会自动安装依赖,请事先查看依赖说明并配置环境。
34+
建议使用时指定版本号安装release版本的转换器,如安装0.4.2版本:
35+
36+
```bash
37+
python3 -m pip install git+https://github.com/MegEngine/[email protected] --user --install-option="--targets=caffe"
38+
```
39+
> :warning: 如果需要转换``TracedModule``模型,请安装v0.5.0以上版本
2540
2641
## 使用方式
2742

@@ -33,10 +48,50 @@ python3 -m pip install git+https://github.com/MegEngine/mgeconvert.git --user --
3348
convert -h
3449
```
3550

36-
以 caffe 为例,查询转换参数:
51+
mge模型转 caffe 为例,查询转换参数:
3752

3853
```bash
39-
convert caffe -h
54+
convert mge_to_caffe -h
55+
```
56+
57+
### Feature 支持说明
58+
59+
- :white_check_mark: 已支持,并完成测试
60+
- :memo: 未支持,或尚未测试完全
61+
- :boom: 明确不支持
62+
63+
| TracedModule | tflite | caffe | onnx |
64+
|---------------------|--------------------|--------------------|--------------------|
65+
| QAT | :white_check_mark: | :memo: | :memo: |
66+
| Quantized | :white_check_mark: | :boom: | :memo: |
67+
| Float32 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
68+
69+
| Mgo | tflite | caffe | onnx |
70+
|---------------------|--------------------|--------------------|--------------------|
71+
| QAT | :boom: | :boom: | :boom: |
72+
| Quantized | :memo: | :boom: | :memo: |
73+
| Float32 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
74+
75+
### TFLite转换
76+
TFlite转换器支持 Float32 和量化的 TracedModule 转换。
77+
对于QAT模型,可以通过设置tracedmodule_to_tflite转换器中的 `require_quantize=True` 选择转换出tflite支持的量化数据类型(int8/uint8/int16/int32)量化后的Quantized 模型:
78+
79+
```bash
80+
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --require_quantize
81+
```
82+
83+
也可设置 `require_quantize=False` 选择转换出float32模型和量化参数文件。
84+
85+
```bash
86+
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --quantize_file_path "quant_params.json"
87+
```
88+
89+
对于后者,还可以通过设置 `param_fake_quant` 参数来选择是否对参数进行假量化。
90+
91+
如果模型中没有QuantStub对输入数据进行量化处理,可以在转换时指定输入数据的量化类型、scale和zero_point量化参数 :
92+
93+
```bash
94+
convert tracedmodule_to_tflite -i tracedmodule.tm -o out.tflite --input_data_type "quint8" --input_scales 0.125 --input_zero_points 128 --require_quantize
4095
```
4196

4297
## 依赖说明
@@ -49,48 +104,49 @@ convert caffe -h
49104

50105
- Python packages: protobuf, onnx==1.7.0
51106

52-
3. cambricon
53-
54-
- Cambricon SDK: CNRT, CNML
55-
56-
4. tflite
107+
3. tflite
57108

58109
- Python packages: pybind11==2.6.2, tensorflow==2.4.0
59110
- third party: [flatbuffers](https://github.com/google/flatbuffers.git)
60111

61112

62113
## 算子支持列表
63114

64-
| |Caffe|ONNX|Cambricon|TFLite|
65-
|-- |-----|----|---------|------|
66-
|abs|||||
67-
|add|||||
68-
|average pool2d|||||
69-
|batchnorm|||| × |
70-
|broadcast|||| × |
71-
|ceil| × || × | × |
72-
|concat|||||
73-
|conv2d|||||
74-
|convtranspose2d|||||
75-
|div(true_div)|||||
76-
|exp|||||
77-
|elemwise max|||||
78-
|floor| × ||| × |
79-
|log|||| × |
80-
|matrix mul|||||
81-
|max pool2d|||||
82-
|mul|||||
83-
|pow|||||
84-
|reduce max|||||
85-
|reduce sum|||||
86-
|relu|||||
87-
|reshape|||||
88-
|sigmoid|||| × |
89-
|softmax|||||
90-
|leaky_relu|| × | × ||
91-
|sub|||||
92-
|slice(subtensor)|||||
93-
|squeeze(axis_add_remove)|||||
94-
|tanh|||||
95-
|typecvt|||||
96-
|transpose(dimshuffle)|||||
115+
| tracemodule:rocket:<br/>mgo:fire: | TFLite | Caffe | ONNX |
116+
|--------------------------|---------|---------|---------|
117+
| abs | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
118+
| average pool2d | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
119+
| batchnorm | ×<br/>× | ✓<br/>✓ | ✓<br/>✓ |
120+
| broadcast | ×<br/>× | ✓<br/>✓ | ✓<br/>✓ |
121+
| ceil | ×<br/>× | ×<br/>× | ✓<br/>✓ |
122+
| concat | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
123+
| conv2d | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
124+
| convtranspose2d | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
125+
| div(true_div) | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
126+
| exp | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
127+
| elemwise max | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
128+
| floor | ×<br/>× | ×<br/>× | ✓<br/>✓ |
129+
| log | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
130+
| matrix mul | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
131+
| max pool2d | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
132+
| mul | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
133+
| pow | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
134+
| reduce max | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
135+
| reduce min | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
136+
| reduce mean | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
137+
| reduce sum | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
138+
| relu | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
139+
| relu6 | ✓<br/>✓ | ×<br/>× | ✓<br/>✓ |
140+
| reshape | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
141+
| resize | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
142+
| sigmoid(logistic) | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
143+
| softmax | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
144+
| leaky_relu | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
145+
| sub | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
146+
| slice(subtensor) | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
147+
| squeeze(axis_add_remove) | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
148+
| tanh | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
149+
| typecvt | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
150+
| transpose(dimshuffle) | ✓<br/>✓ | ✓<br/>✓ | ✓<br/>✓ |
151+
| AdaptiveAvgPool2d | ×<br/>× | ×<br/>× | ✓<br/>✓ |
152+
| flatten | ×<br/>× | ×<br/>× | ✓<br/>✓ |

0 commit comments

Comments
 (0)