Skip to content

Commit

Permalink
Merge branch 'master' into kernel_bug_fixv2
Browse files Browse the repository at this point in the history
  • Loading branch information
lerenhua authored Aug 28, 2023
2 parents 2d93e5e + 2cff944 commit a7df8ac
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
12 changes: 11 additions & 1 deletion docs/USAGE_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ Type "help", "copyright", "credits" or "license" for more information.

k230模型编译推理参考Jupyter脚本:[User_guide](../examples/user_guide/k230_simulate.ipynb),脚本中包含了单输入和多输入的示例。

如果在Docker中运行Jupyter脚本,可以参考[配置Jupyter lab](https://github.com/kunjing96/docker-jupyterlab#32-%E9%85%8D%E7%BD%AEjupyter-lab)进行配置。
如果在Docker中运行Jupyter脚本,可以参考以下命令,之后在浏览器窗口打开即可。

```shell
docker run -it --rm --privileged=true -p 8889:8889 --name Kendryte -v `pwd`:/mnt -w /mnt ghcr.io/kendryte/k230_sdk /bin/bash -c "/bin/bash

pip install jupyterlab

jupyter-lab --ip 0.0.0.0 --allow-root
```

在执行脚本之前需要根据自身需求修改以下内容:

Expand Down Expand Up @@ -153,6 +161,8 @@ subgraph A
end

```
##### 动态shape参数
详见[动态shape参数说明](./shape_bucket.md)

#### 代码示例

Expand Down
14 changes: 13 additions & 1 deletion docs/USAGE_v2_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ Type "help", "copyright", "credits" or "license" for more information.

Model compilation, inference for k230 can be found in the Jupyter script [User_guide](../examples/user_guide/k230_simulate.ipynb), this script contains single and multiple input examples.

If you run Jupyter scripts in Docker, you can refer to [Configure Jupyter lab](https://github.com/kunjing96/docker-jupyterlab#32-%E9%85%8D%E7%BD%AEjupyter-lab) to configure them.
If you run the Jupyter script in Docker, you can refer to the command and then open it in your browser.

```shell
docker run -it --rm --privileged=true -p 8889:8889 --name Kendryte -v `pwd`:/mnt -w /mnt ghcr.io/kendryte/k230_sdk /bin/bash -c "/bin/bash

pip install jupyterlab

jupyter-lab --ip 0.0.0.0 --allow-root
```


You need to modify the following to suit your needs before executing the script:

Expand Down Expand Up @@ -154,6 +163,9 @@ subgraph A
```

##### Dynamice shape args
Refer to[Dynamic shape args description](./shape_bucket.md)

#### Example

```python
Expand Down
48 changes: 48 additions & 0 deletions docs/shape_bucket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# ShapeBucket使用说明

ShapeBucket是针对动态shape的一种解决方案,会根据输入长度的范围以及指定的段的数量来对动态shape进行优化。该功能默认为false,需要打开对应的option才能生效,除了指定对应的字段信息,其他流程与编译静态模型没有区别。

对应的不同CompileOptions中的字段

| 字段名称 | 类型 | 是否必须 | 描述 |
| --------------------------- | --------------------- | -------- | --------------------------------------------------------------- |
| shape_bucket_enable | bool || 是否开启ShapeBucket功能,默认为False。在 `dump_ir=True`时生效 |
| shape_bucket_range_info | Dict[str, [int, int]] || 每个输入shape维度信息中的变量的范围,最小值必须大于等于1 |
| shape_bucket_segments_count | int || 输入变量的范围划分为几段 |
| shape_bucket_fix_var_map | Dict[str, int] || 固定shape维度信息中的变量为特定的值 |

## onnx

在模型的shape中会有些维度为变量名字,这里以一个onnx模型的输入为例

> tokens: int64[batch_size, tgt_seq_len]
>
> step: float32[seq_len, batch_size]
对应这个输入有如下的配置

```python
shape_bucket_options = nncase.ShapeBucketOptions()
shape_bucket_options.shape_bucket_enable = True
shape_bucket_options.shape_bucket_range_info = {"seq_len":[1, 100], "tgt_seq_len":[1, 100]}
shape_bucket_options.shape_bucket_segments_count = 2
shape_bucket_options.shape_bucket_fix_var_map = {"batch_size" : 3}
```

shape的维度信息中存在seq_len,tgt_seq_len,batch_size这三个变量。首先是batch_size,虽然是变量的但实际应用的时候固定为3,因此在**fix_var_map**中添加batch_size = 3,在运行的时候会将这个维度固定为3。

seq_len,tgt_seq_len两个是实际会发生改变的,因此需要配置这两个变量的实际范围,也就是**range_info**的信息。**segments_count**是实际分段的数量,会根据范围等分为几份,对应的编译时间也会相应增加几倍。

## tflite

tflite的模型与onnx不同,shape上暂未标注维度的名称,目前只支持输入中具有一个维度是动态的,并且名称统一配置为-1,配置方式如下

```cpp
shape_bucket_options = nncase.ShapeBucketOptions()
shape_bucket_options.shape_bucket_enable = True
shape_bucket_options.shape_bucket_range_info = {"-1":[1, 100]}
shape_bucket_options.shape_bucket_segments_count = 2
shape_bucket_options.shape_bucket_fix_var_map = {"batch_size" : 3}
```
配置完这些选项后整个编译的流程和静态shape一致。
8 changes: 8 additions & 0 deletions python/nncase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ class CompileOptions:
dump_asm: bool
dump_ir: bool
dump_dir: str
shape_bucket_enable: bool
shape_bucket_range_info: dict
shape_bucket_segments_count: int
shape_bucket_fix_var_map: dict

def __init__(self) -> None:

Expand All @@ -375,6 +379,10 @@ def __init__(self) -> None:
self.dump_asm = True
self.dump_ir = False
self.dump_dir = "tmp"
self.shape_bucket_enable = False
self.shape_bucket_range_info = {}
self.shape_bucket_segments_count = 2
self.shape_bucket_fix_var_map = {}


class ShapeBucketOptions:
Expand Down

0 comments on commit a7df8ac

Please sign in to comment.