未验证 提交 3a9ed427 编写于 作者: X XGZhang 提交者: GitHub

update quant docs (#841)

* update quant docs

* Update quantization_api.rst
上级 068119c9
...@@ -74,16 +74,35 @@ quant_post_dynamic ...@@ -74,16 +74,35 @@ quant_post_dynamic
.. warning:: .. warning::
示例不能直接运行,因为需要加载 ``${model_dir}`` 下的模型,所以不能直接运行 api需要加载 ``${model_path}`` 下的模型,所以示例中首先导出了inference model
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.dataset.mnist as reader import paddle.dataset.mnist as reader
from paddleslim.models import MobileNet
from paddleslim.quant import quant_post_dynamic from paddleslim.quant import quant_post_dynamic
paddle.enable_static() paddle.enable_static()
image = paddle.static.data(name='image', shape=[None, 1, 28, 28], dtype='float32')
model = MobileNet()
out = model.net(input=image, class_dim=10)
main_prog = paddle.static.default_main_program()
val_prog = main_prog.clone(for_test=True)
place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
paddle.fluid.io.save_inference_model(
dirname='./model_path',
feeded_var_names=[image.name],
target_vars=[out],
main_program=val_prog,
executor=exe,
model_filename='__model__',
params_filename='__params__')
quant_post_dynamic( quant_post_dynamic(
model_dir='./model_path', model_dir='./model_path',
save_model_dir='./save_path', save_model_dir='./save_path',
...@@ -164,29 +183,48 @@ quant_post_static ...@@ -164,29 +183,48 @@ quant_post_static
.. warning:: .. warning::
示例不能直接运行,因为需要加载 ``${model_dir}`` 下的模型,所以不能直接运行 api需要加载 ``${model_path}`` 下的模型,所以示例中首先导出了inference model
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.dataset.mnist as reader import paddle.dataset.mnist as reader
from paddleslim.quant import quant_post_static from paddleslim.models import MobileNet
paddle.enable_static() from paddleslim.quant import quant_post_static
val_reader = reader.train()
use_gpu = True paddle.enable_static()
place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() val_reader = reader.test()
use_gpu = True
exe = fluid.Executor(place) place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
quant_post_static( exe = fluid.Executor(place)
executor=exe, paddle.enable_static()
model_dir='./model_path', image = paddle.static.data(name='image', shape=[None, 1, 28, 28], dtype='float32')
quantize_model_path='./save_path', model = MobileNet()
sample_generator=val_reader, out = model.net(input=image, class_dim=10)
model_filename='__model__', main_prog = paddle.static.default_main_program()
params_filename='__params__', val_prog = main_prog.clone(for_test=True)
batch_size=16, place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace()
batch_nums=10) exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
paddle.fluid.io.save_inference_model(
dirname='./model_path',
feeded_var_names=[image.name],
target_vars=[out],
main_program=val_prog,
executor=exe,
model_filename='__model__',
params_filename='__params__')
quant_post_static(
executor=exe,
model_dir='./model_path',
quantize_model_path='./save_path',
sample_generator=val_reader,
model_filename='__model__',
params_filename='__params__',
batch_size=16,
batch_nums=10)
更详细的用法请参考 `离线量化demo <https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/quant/quant_post>`_ 更详细的用法请参考 `离线量化demo <https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/quant/quant_post>`_
...@@ -269,11 +307,10 @@ convert ...@@ -269,11 +307,10 @@ convert
.. code-block:: python .. code-block:: python
#encoding=utf8 #encoding=utf8
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddleslim.quant as quant import paddleslim.quant as quant
paddle.enable_static()
train_program = fluid.Program() train_program = fluid.Program()
with fluid.program_guard(train_program): with fluid.program_guard(train_program):
...@@ -402,7 +439,7 @@ fluid.Program ...@@ -402,7 +439,7 @@ fluid.Program
**代码示例** **代码示例**
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import paddleslim.quant as quant import paddleslim.quant as quant
......
...@@ -17,15 +17,38 @@ ...@@ -17,15 +17,38 @@
主要代码实现如下: 主要代码实现如下:
```python ```python
import paddleslim import paddle
model_dir = path/to/fp32_model_params import paddle.fluid as fluid
save_model_dir = path/to/save_model_path import paddle.dataset.mnist as reader
paddleslim.quant.quant_post_dynamic(model_dir=model_dir, from paddleslim.models import MobileNet
save_model_dir=save_model_dir, from paddleslim.quant import quant_post_dynamic
weight_bits=8,
quantizable_op_type=['conv2d', 'mul'], paddle.enable_static()
weight_quantize_type="channel_wise_abs_max", image = paddle.static.data(name='image', shape=[None, 1, 28, 28], dtype='float32')
generate_test_model=False) model = MobileNet()
out = model.net(input=image, class_dim=10)
main_prog = paddle.static.default_main_program()
val_prog = main_prog.clone(for_test=True)
place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
paddle.fluid.io.save_inference_model(
dirname='./model_path',
feeded_var_names=[image.name],
target_vars=[out],
main_program=val_prog,
executor=exe,
model_filename='__model__',
params_filename='__params__')
quant_post_dynamic(
model_dir='./model_path',
save_model_dir='./save_path',
model_filename='__model__',
params_filename='__params__',
save_model_filename='__model__',
save_params_filename='__params__')
``` ```
## 静态离线量化 ## 静态离线量化
...@@ -47,16 +70,44 @@ paddleslim.quant.quant_post_dynamic(model_dir=model_dir, ...@@ -47,16 +70,44 @@ paddleslim.quant.quant_post_dynamic(model_dir=model_dir,
主要代码实现如下: 主要代码实现如下:
```python ```python
import paddleslim import paddle
import paddle.fluid as fluid
import paddle.dataset.mnist as reader
from paddleslim.models import MobileNet
from paddleslim.quant import quant_post_static
paddle.enable_static()
val_reader = reader.test()
use_gpu = True
place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
paddle.enable_static()
image = paddle.static.data(name='image', shape=[None, 1, 28, 28], dtype='float32')
model = MobileNet()
out = model.net(input=image, class_dim=10)
main_prog = paddle.static.default_main_program()
val_prog = main_prog.clone(for_test=True)
place = paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace()
exe = paddle.static.Executor(place) exe = paddle.static.Executor(place)
paddleslim.quant.quant_post( exe.run(paddle.static.default_startup_program())
executor=exe,
model_dir=model_path, paddle.fluid.io.save_inference_model(
quantize_model_path=save_path, dirname='./model_path',
sample_generator=reader, feeded_var_names=[image.name],
model_filename=model_filename, target_vars=[out],
params_filename=params_filename, main_program=val_prog,
batch_nums=batch_num) executor=exe,
model_filename='__model__',
params_filename='__params__')
quant_post_static(
executor=exe,
model_dir='./model_path',
quantize_model_path='./save_path',
sample_generator=val_reader,
model_filename='__model__',
params_filename='__params__',
batch_size=16,
batch_nums=10)
``` ```
详细代码与例程请参考:[静态离线量化](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/quant/quant_post) 详细代码与例程请参考:[静态离线量化](https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/quant/quant_post)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册