Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0c5781e5
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
0c5781e5
编写于
8月 15, 2023
作者:
S
Sonder
提交者:
GitHub
8月 15, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[xdoctest] reformat example code with google style No.116-119 (#56118)
上级
786c6e99
变更
4
展开全部
隐藏空白更改
内联
并排
Showing
4 changed file
with
453 addition
and
403 deletion
+453
-403
python/paddle/optimizer/optimizer.py
python/paddle/optimizer/optimizer.py
+205
-177
python/paddle/quantization/config.py
python/paddle/quantization/config.py
+123
-105
python/paddle/quantization/factory.py
python/paddle/quantization/factory.py
+16
-15
python/paddle/quantization/imperative/qat.py
python/paddle/quantization/imperative/qat.py
+109
-106
未找到文件。
python/paddle/optimizer/optimizer.py
浏览文件 @
0c5781e5
此差异已折叠。
点击以展开。
python/paddle/quantization/config.py
浏览文件 @
0c5781e5
...
...
@@ -70,12 +70,15 @@ class QuantConfig:
Examples:
.. code-block:: python
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>>
from paddle.quantization import QuantConfig
>>>
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=quanter, weight=quanter)
print(q_config)
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> q_config = QuantConfig(activation=quanter, weight=quanter)
>>> print(q_config)
Global config:
activation: FakeQuanterWithAbsMaxObserver(name=None,moving_rate=0.9,bit_length=8,dtype=float32)
weight: FakeQuanterWithAbsMaxObserver(name=None,moving_rate=0.9,bit_length=8,dtype=float32)
"""
...
...
@@ -100,31 +103,36 @@ class QuantConfig:
weight
:
QuanterFactory
=
None
,
):
r
"""
Set the quantization config by layer. It has the highest priority among
all the setting methods.
Args:
layer(Union[Layer, list]): One or a list of layers.
activation(QuanterFactory): Quanter used for activations.
weight(QuanterFactory): Quanter used for weights.
Examples:
.. code-block:: python
import paddle
from paddle.nn import Linear
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
class Model(paddle.nn.Layer):
def __init__(self):
super().__init__()
self.fc = Linear(576, 120)
model = Model()
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
q_config.add_layer_config([model.fc], activation=quanter, weight=quanter)
print(q_config)
Set the quantization config by layer. It has the highest priority among
all the setting methods.
Args:
layer(Union[Layer, list]): One or a list of layers.
activation(QuanterFactory): Quanter used for activations.
weight(QuanterFactory): Quanter used for weights.
Examples:
.. code-block:: python
>>> import paddle
>>> from paddle.nn import Linear
>>> from paddle.quantization import QuantConfig
>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>> class Model(paddle.nn.Layer):
... def __init__(self):
... super().__init__()
... self.fc = Linear(576, 120)
>>> model = Model()
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> q_config = QuantConfig(activation=None, weight=None)
>>> q_config.add_layer_config([model.fc], activation=quanter, weight=quanter)
>>> # doctest: +SKIP
>>> print(q_config)
Global config:
None
Layer prefix config:
{'linear_0': <paddle.quantization.config.SingleLayerConfig object at 0x7fe41a680ee0>}
"""
if
isinstance
(
layer
,
list
):
...
...
@@ -144,31 +152,36 @@ class QuantConfig:
weight
:
QuanterFactory
=
None
,
):
r
"""
Set the quantization config by full name of layer. Its priority is
lower than `add_layer_config`.
Args:
layer_name(Union[str, list]): One or a list of layers' full name.
activation(QuanterFactory): Quanter used for activations.
weight(QuanterFactory): Quanter used for weights.
Examples:
.. code-block:: python
import paddle
from paddle.nn import Linear
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
class Model(paddle.nn.Layer):
def __init__(self):
super().__init__()
self.fc = Linear(576, 120)
model = Model()
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
q_config.add_name_config([model.fc.full_name()], activation=quanter, weight=quanter)
print(q_config)
Set the quantization config by full name of layer. Its priority is
lower than `add_layer_config`.
Args:
layer_name(Union[str, list]): One or a list of layers' full name.
activation(QuanterFactory): Quanter used for activations.
weight(QuanterFactory): Quanter used for weights.
Examples:
.. code-block:: python
>>> import paddle
>>> from paddle.nn import Linear
>>> from paddle.quantization import QuantConfig
>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>> class Model(paddle.nn.Layer):
... def __init__(self):
... super().__init__()
... self.fc = Linear(576, 120)
>>> model = Model()
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> q_config = QuantConfig(activation=None, weight=None)
>>> q_config.add_name_config([model.fc.full_name()], activation=quanter, weight=quanter)
>>> # doctest: +SKIP
>>> print(q_config)
Global config:
None
Layer prefix config:
{'linear_0': <paddle.quantization.config.SingleLayerConfig object at 0x7fe41a680fd0>}
"""
if
isinstance
(
layer_name
,
str
):
...
...
@@ -198,22 +211,27 @@ class QuantConfig:
weight(QuanterFactory): Quanter used for weights.
Examples:
.. code-block:: python
import paddle
from paddle.nn import Linear
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
class Model(paddle.nn.Layer):
def __init__(self):
super().__init__()
self.fc = Linear(576, 120)
model = Model()
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
q_config.add_type_config([Linear], activation=quanter, weight=quanter)
print(q_config)
.. code-block:: python
>>> import paddle
>>> from paddle.nn import Linear
>>> from paddle.quantization import QuantConfig
>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>> class Model(paddle.nn.Layer):
... def __init__(self):
... super().__init__()
... self.fc = Linear(576, 120)
>>> model = Model()
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> q_config = QuantConfig(activation=None, weight=None)
>>> q_config.add_type_config([Linear], activation=quanter, weight=quanter)
>>> # doctest: +SKIP
>>> print(q_config)
Global config:
None
Layer type config:
{<class 'paddle.nn.layer.common.Linear'>: <paddle.quantization.config.SingleLayerConfig object at 0x7fe41a680a60>}
"""
if
isinstance
(
layer_type
,
type
)
and
issubclass
(
...
...
@@ -240,18 +258,18 @@ class QuantConfig:
target(type): The type of layers that will be converted to.
Examples:
.. code-block:: python
from paddle.nn import Conv2D
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
class CustomizedQuantedConv2D:
def forward(self, x):
pass
# add some code for quantization simulation
q_config.add_qat_layer_mapping(Conv2D, CustomizedQuantedConv2D)
.. code-block:: python
>>>
from paddle.nn import Conv2D
>>>
from paddle.quantization import QuantConfig
>>>
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>>
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>>
q_config = QuantConfig(activation=None, weight=None)
>>>
class CustomizedQuantedConv2D:
...
def forward(self, x):
...
pass
...
# add some code for quantization simulation
>>>
q_config.add_qat_layer_mapping(Conv2D, CustomizedQuantedConv2D)
"""
assert
isinstance
(
source
,
type
)
and
issubclass
(
source
,
paddle
.
nn
.
Layer
...
...
@@ -272,13 +290,13 @@ class QuantConfig:
layer_type(type): The type of layer to be declared as leaf.
Examples:
.. code-block:: python
.. code-block:: python
from paddle.nn import Sequential
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
q_config = QuantConfig(activation=None, weight=None)
q_config.add_customized_leaf(Sequential)
>>>
from paddle.nn import Sequential
>>>
from paddle.quantization import QuantConfig
>>>
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>>
q_config = QuantConfig(activation=None, weight=None)
>>>
q_config.add_customized_leaf(Sequential)
"""
self
.
_customized_leaves
.
append
(
layer_type
)
...
...
@@ -379,22 +397,22 @@ class QuantConfig:
model(Layer): The model to be specified by the config.
Examples:
.. code-block:: python
import paddle
from paddle.nn import Linear, Sequential
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
class Model(paddle.nn.Layer):
def __init__(self):
super().__init__()
self.fc = Sequential(Linear(576, 120),Linear(576, 120))
model = Model()
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
q_config.add_layer_config([model.fc], activation=quanter, weight=quanter)
q_config._specify(model)
.. code-block:: python
>>>
import paddle
>>>
from paddle.nn import Linear, Sequential
>>>
from paddle.quantization import QuantConfig
>>>
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>>
class Model(paddle.nn.Layer):
...
def __init__(self):
...
super().__init__()
...
self.fc = Sequential(Linear(576, 120),Linear(576, 120))
>>>
model = Model()
>>>
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>>
q_config = QuantConfig(activation=None, weight=None)
>>>
q_config.add_layer_config([model.fc], activation=quanter, weight=quanter)
>>>
q_config._specify(model)
"""
self
.
_model
=
model
self
.
_specify_helper
(
self
.
_model
)
...
...
python/paddle/quantization/factory.py
浏览文件 @
0c5781e5
...
...
@@ -83,21 +83,22 @@ def quanter(class_name):
Examples:
.. code-block:: python
# Given codes in ./customized_quanter.py
from paddle.quantization import quanter
from paddle.quantization import BaseQuanter
@quanter("CustomizedQuanter")
class CustomizedQuanterLayer(BaseQuanter):
def __init__(self, arg1, kwarg1=None):
pass
# Used in ./test.py
# from .customized_quanter import CustomizedQuanter
from paddle.quantization import QuantConfig
arg1_value = "test"
kwarg1_value = 20
quanter = CustomizedQuanter(arg1_value, kwarg1=kwarg1_value)
q_config = QuantConfig(activation=quanter, weight=quanter)
>>> # doctest: +SKIP
>>> # Given codes in ./customized_quanter.py
>>> from paddle.quantization import quanter
>>> from paddle.quantization import BaseQuanter
>>> @quanter("CustomizedQuanter")
>>> class CustomizedQuanterLayer(BaseQuanter):
... def __init__(self, arg1, kwarg1=None):
... pass
>>> # Used in ./test.py
>>> # from .customized_quanter import CustomizedQuanter
>>> from paddle.quantization import QuantConfig
>>> arg1_value = "test"
>>> kwarg1_value = 20
>>> quanter = CustomizedQuanter(arg1_value, kwarg1=kwarg1_value)
>>> q_config = QuantConfig(activation=quanter, weight=quanter)
"""
...
...
python/paddle/quantization/imperative/qat.py
浏览文件 @
0c5781e5
...
...
@@ -135,79 +135,81 @@ class ImperativeQuantAware:
during training. If this attribute is not sets or the attribute is
false, the Layer would be qunatized in training.
Examples 1:
.. code-block:: python
import paddle
from paddle.static.quantization
\
import ImperativeQuantAware
from paddle.vision.models
\
import resnet
model = resnet.resnet50(pretrained=True)
imperative_qat = ImperativeQuantAware(
weight_quantize_type='abs_max',
activation_quantize_type='moving_average_abs_max')
# Add the fake quant logical.
# The original model will be rewrite.
# The outscale of outputs in supportted layers would be calculated.
imperative_qat.quantize(model)
# Fine-tune the quantized model
# ...
# Save quant model for the inference.
imperative_qat.save_quantized_model(
layer=model,
model_path="./resnet50_qat",
input_spec=[
paddle.static.InputSpec(
shape=[None, 3, 224, 224], dtype='float32')])
Examples 2:
.. code-block:: python
import paddle
from paddle.static.quantization
\
import ImperativeQuantAware
class ImperativeModel(paddle.nn.Layer):
def __init__(self):
super().__init__()
# self.linear_0 would skip the quantization.
self.linear_0 = paddle.nn.Linear(784, 400)
self.linear_0.skip_quant = True
# self.linear_1 would not skip the quantization.
self.linear_1 = paddle.nn.Linear(400, 10)
self.linear_1.skip_quant = False
def forward(self, inputs):
x = self.linear_0(inputs)
x = self.linear_1(inputs)
return x
model = ImperativeModel()
imperative_qat = ImperativeQuantAware(
weight_quantize_type='abs_max',
activation_quantize_type='moving_average_abs_max')
# Add the fake quant logical.
# The original model will be rewrite.
#
# There is only one Layer(self.linear1) would be added the
# fake quant logical.
imperative_qat.quantize(model)
# Fine-tune the quantized model
# ...
# Save quant model for the inference.
imperative_qat.save_quantized_model(
layer=model,
model_path="./imperative_model_qat")
Examples:
.. code-block:: python
>>> import paddle
>>> from paddle.static.quantization import (
... ImperativeQuantAware,
... )
>>> from paddle.vision.models import (
... resnet,
... )
>>> model = resnet.resnet50(pretrained=True)
>>> imperative_qat = ImperativeQuantAware(
... weight_quantize_type='abs_max',
... activation_quantize_type='moving_average_abs_max')
>>> # Add the fake quant logical.
>>> # The original model will be rewrite.
>>> # The outscale of outputs in supportted layers would be calculated.
>>> imperative_qat.quantize(model)
>>> # Fine-tune the quantized model
>>> # ...
>>> # Save quant model for the inference.
>>> imperative_qat.save_quantized_model(
... layer=model,
... model_path="./resnet50_qat",
... input_spec=[
... paddle.static.InputSpec(
... shape=[None, 3, 224, 224], dtype='float32')])
.. code-block:: python
>>> import paddle
>>> from paddle.static.quantization import (
... ImperativeQuantAware,
... )
>>> class ImperativeModel(paddle.nn.Layer):
... def __init__(self):
... super().__init__()
... # self.linear_0 would skip the quantization.
... self.linear_0 = paddle.nn.Linear(784, 400)
... self.linear_0.skip_quant = True
... # self.linear_1 would not skip the quantization.
... self.linear_1 = paddle.nn.Linear(400, 10)
... self.linear_1.skip_quant = False
... def forward(self, inputs):
... x = self.linear_0(inputs)
... x = self.linear_1(inputs)
... return x
>>> model = ImperativeModel()
>>> imperative_qat = ImperativeQuantAware(
... weight_quantize_type='abs_max',
... activation_quantize_type='moving_average_abs_max')
>>> # Add the fake quant logical.
>>> # The original model will be rewrite.
>>> #
>>> # There is only one Layer(self.linear1) would be added the
>>> # fake quant logical.
>>> imperative_qat.quantize(model)
>>> # Fine-tune the quantized model
>>> # ...
>>> # Save quant model for the inference.
>>> imperative_qat.save_quantized_model(
... layer=model,
... model_path="./imperative_model_qat")
"""
super
().
__init__
()
self
.
fuse_conv_bn
=
fuse_conv_bn
...
...
@@ -245,39 +247,40 @@ class ImperativeQuantAware:
None
Examples:
.. code-block:: python
import paddle
from paddle.static.quantization
\
import ImperativeQuantAware
class ImperativeModel(paddle.nn.Layer):
def __init__(self):
super().__init__()
# self.linear_0 would skip the quantization.
self.linear_0 = paddle.nn.Linear(784, 400)
self.linear_0.skip_quant = True
# self.linear_1 would not skip the quantization.
self.linear_1 = paddle.nn.Linear(400, 10)
self.linear_1.skip_quant = False
def forward(self, inputs):
x = self.linear_0(inputs)
x = self.linear_1(inputs)
return x
model = ImperativeModel()
imperative_qat = ImperativeQuantAware(
weight_quantize_type='abs_max',
activation_quantize_type='moving_average_abs_max')
# Add the fake quant logical.
# The original model will be rewrite.
#
# There is only one Layer(self.linear1) would be added the
# fake quant logical.
imperative_qat.quantize(model)
.. code-block:: python
>>> import paddle
>>> from paddle.static.quantization import (
... ImperativeQuantAware,
... )
>>> class ImperativeModel(paddle.nn.Layer):
... def __init__(self):
... super().__init__()
... # self.linear_0 would skip the quantization.
... self.linear_0 = paddle.nn.Linear(784, 400)
... self.linear_0.skip_quant = True
... # self.linear_1 would not skip the quantization.
... self.linear_1 = paddle.nn.Linear(400, 10)
... self.linear_1.skip_quant = False
... def forward(self, inputs):
... x = self.linear_0(inputs)
... x = self.linear_1(inputs)
... return x
>>> model = ImperativeModel()
>>> imperative_qat = ImperativeQuantAware(
... weight_quantize_type='abs_max',
... activation_quantize_type='moving_average_abs_max')
>>> # Add the fake quant logical.
>>> # The original model will be rewrite.
>>> #
>>> # There is only one Layer(self.linear1) would be added the
>>> # fake quant logical.
>>> imperative_qat.quantize(model)
"""
assert
isinstance
(
model
,
paddle
.
nn
.
Layer
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录