Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0c5781e5
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
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
...
@@ -125,40 +125,40 @@ class Optimizer:
...
@@ -125,40 +125,40 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
#
Take the subclass adam as an example
>>> #
Take the subclass adam as an example
import paddle
>>>
import paddle
linear = paddle.nn.Linear(10, 10)
>>>
linear = paddle.nn.Linear(10, 10)
inp = paddle.uniform(shape=[10, 10], min=-0.1, max=0.1)
>>>
inp = paddle.uniform(shape=[10, 10], min=-0.1, max=0.1)
out = linear(inp)
>>>
out = linear(inp)
loss = paddle.mean(out)
>>>
loss = paddle.mean(out)
adam = paddle.optimizer.Adam(learning_rate=0.1,
>>>
adam = paddle.optimizer.Adam(learning_rate=0.1,
parameters=linear.parameters())
...
parameters=linear.parameters())
loss.backward()
>>>
loss.backward()
adam.step()
>>>
adam.step()
adam.clear_grad()
>>>
adam.clear_grad()
#Take the subclass sgd as an example
>>>
#Take the subclass sgd as an example
#optimize parameters in linear_1 and linear2 in different options.
>>>
#optimize parameters in linear_1 and linear2 in different options.
#Note that the learning_rate of linear_2 is 0.01.
>>>
#Note that the learning_rate of linear_2 is 0.01.
linear_1 = paddle.nn.Linear(10, 10)
>>>
linear_1 = paddle.nn.Linear(10, 10)
linear_2 = paddle.nn.Linear(10, 10)
>>>
linear_2 = paddle.nn.Linear(10, 10)
inp = paddle.uniform(shape=[10, 10], min=-0.1, max=0.1)
>>>
inp = paddle.uniform(shape=[10, 10], min=-0.1, max=0.1)
out = linear_1(inp)
>>>
out = linear_1(inp)
out = linear_2(out)
>>>
out = linear_2(out)
loss = paddle.mean(out)
>>>
loss = paddle.mean(out)
sgd = paddle.optimizer.SGD(
>>>
sgd = paddle.optimizer.SGD(
learning_rate=0.1,
...
learning_rate=0.1,
parameters=[{
...
parameters=[{
'params': linear_1.parameters()
...
'params': linear_1.parameters()
}, {
...
}, {
'params': linear_2.parameters(),
...
'params': linear_2.parameters(),
'weight_decay': 0.001,
...
'weight_decay': 0.001,
'learning_rate': 0.1
...
'learning_rate': 0.1
}],
...
}],
weight_decay=0.01)
...
weight_decay=0.01)
loss.backward()
>>>
loss.backward()
sgd.step()
>>>
sgd.step()
sgd.clear_grad()
>>>
sgd.clear_grad()
"""
"""
...
@@ -343,23 +343,23 @@ class Optimizer:
...
@@ -343,23 +343,23 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
emb = paddle.nn.Embedding(10, 10)
>>>
emb = paddle.nn.Embedding(10, 10)
layer_state_dict = emb.state_dict()
>>>
layer_state_dict = emb.state_dict()
paddle.save(layer_state_dict, "emb.pdparams")
>>>
paddle.save(layer_state_dict, "emb.pdparams")
scheduler = paddle.optimizer.lr.NoamDecay(
>>>
scheduler = paddle.optimizer.lr.NoamDecay(
d_model=0.01, warmup_steps=100, verbose=True)
...
d_model=0.01, warmup_steps=100, verbose=True)
adam = paddle.optimizer.Adam(
>>>
adam = paddle.optimizer.Adam(
learning_rate=scheduler,
...
learning_rate=scheduler,
parameters=emb.parameters())
...
parameters=emb.parameters())
opt_state_dict = adam.state_dict()
>>>
opt_state_dict = adam.state_dict()
paddle.save(opt_state_dict, "adam.pdopt")
>>>
paddle.save(opt_state_dict, "adam.pdopt")
opti_state_dict = paddle.load("adam.pdopt")
>>>
opti_state_dict = paddle.load("adam.pdopt")
adam.set_state_dict(opti_state_dict)
>>>
adam.set_state_dict(opti_state_dict)
'''
'''
if
isinstance
(
self
.
_learning_rate
,
LRScheduler
):
if
isinstance
(
self
.
_learning_rate
,
LRScheduler
):
...
@@ -500,23 +500,22 @@ class Optimizer:
...
@@ -500,23 +500,22 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>> import paddle
linear = paddle.nn.Linear(10, 10)
>>> linear = paddle.nn.Linear(10, 10)
adam = paddle.optimizer.Adam(0.1, parameters=linear.parameters())
>>> adam = paddle.optimizer.Adam(0.1, parameters=linear.parameters())
# set learning rate manually by python float value
>>> # set learning rate manually by python float value
lr_list = [0.2, 0.3, 0.4, 0.5, 0.6]
>>> lr_list = [0.2, 0.3, 0.4, 0.5, 0.6]
for i in range(5):
>>> for i in range(5):
adam.set_lr(lr_list[i])
... adam.set_lr(lr_list[i])
lr = adam.get_lr()
... lr = adam.get_lr()
print("current lr is {}".format(lr))
... print("current lr is {}".format(lr))
# Print:
current lr is 0.2
# current lr is 0.2
current lr is 0.3
# current lr is 0.3
current lr is 0.4
# current lr is 0.4
current lr is 0.5
# current lr is 0.5
current lr is 0.6
# current lr is 0.6
"""
"""
if
not
isinstance
(
value
,
(
int
,
float
)):
if
not
isinstance
(
value
,
(
int
,
float
)):
...
@@ -570,24 +569,24 @@ class Optimizer:
...
@@ -570,24 +569,24 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
linear = paddle.nn.Linear(10, 10)
>>>
linear = paddle.nn.Linear(10, 10)
adam = paddle.optimizer.Adam(0.1, parameters=linear.parameters())
>>>
adam = paddle.optimizer.Adam(0.1, parameters=linear.parameters())
# set learning rate manually by class LRScheduler
>>>
# set learning rate manually by class LRScheduler
scheduler = paddle.optimizer.lr.MultiStepDecay(learning_rate=0.5, milestones=[2,4,6], gamma=0.8)
>>>
scheduler = paddle.optimizer.lr.MultiStepDecay(learning_rate=0.5, milestones=[2,4,6], gamma=0.8)
adam.set_lr_scheduler(scheduler)
>>>
adam.set_lr_scheduler(scheduler)
lr = adam.get_lr()
>>>
lr = adam.get_lr()
print("current lr is {}".format(lr))
>>>
print("current lr is {}".format(lr))
#
current lr is 0.5
current lr is 0.5
# set learning rate manually by another LRScheduler
>>>
# set learning rate manually by another LRScheduler
scheduler = paddle.optimizer.lr.StepDecay(learning_rate=0.1, step_size=5, gamma=0.6)
>>>
scheduler = paddle.optimizer.lr.StepDecay(learning_rate=0.1, step_size=5, gamma=0.6)
adam.set_lr_scheduler(scheduler)
>>>
adam.set_lr_scheduler(scheduler)
lr = adam.get_lr()
>>>
lr = adam.get_lr()
print("current lr is {}".format(lr))
>>>
print("current lr is {}".format(lr))
#
current lr is 0.1
current lr is 0.1
"""
"""
from
paddle.optimizer.lr
import
LRScheduler
from
paddle.optimizer.lr
import
LRScheduler
...
@@ -611,50 +610,79 @@ class Optimizer:
...
@@ -611,50 +610,79 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
# train on default dynamic graph mode
>>> # train on default dynamic graph mode
import paddle
>>> import paddle
import numpy as np
>>> import numpy as np
emb = paddle.nn.Embedding(10, 3)
>>> emb = paddle.nn.Embedding(10, 3)
## example1: LRScheduler is not used, return the same value is all the same
>>> ## example1: LRScheduler is not used, return the same value is all the same
adam = paddle.optimizer.Adam(0.01, parameters = emb.parameters())
>>> adam = paddle.optimizer.Adam(0.01, parameters = emb.parameters())
for batch in range(10):
>>> for batch in range(10):
input = paddle.randint(low=0, high=5, shape=[5])
... input = paddle.randint(low=0, high=5, shape=[5])
out = emb(input)
... out = emb(input)
out.backward()
... out.backward()
print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.01
... print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.01
adam.step()
... adam.step()
Learning rate of step0: 0.01
## example2: StepDecay is used, return the scheduled learning rate
Learning rate of step1: 0.01
scheduler = paddle.optimizer.lr.StepDecay(learning_rate=0.5, step_size=2, gamma=0.1)
Learning rate of step2: 0.01
adam = paddle.optimizer.Adam(scheduler, parameters = emb.parameters())
Learning rate of step3: 0.01
for batch in range(10):
Learning rate of step4: 0.01
input = paddle.randint(low=0, high=5, shape=[5])
Learning rate of step5: 0.01
out = emb(input)
Learning rate of step6: 0.01
out.backward()
Learning rate of step7: 0.01
print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.5->0.05...
Learning rate of step8: 0.01
adam.step()
Learning rate of step9: 0.01
scheduler.step()
>>> ## example2: StepDecay is used, return the scheduled learning rate
# train on static graph mode
>>> scheduler = paddle.optimizer.lr.StepDecay(learning_rate=0.5, step_size=2, gamma=0.1)
paddle.enable_static()
>>> adam = paddle.optimizer.Adam(scheduler, parameters = emb.parameters())
main_prog = paddle.static.Program()
>>> for batch in range(10):
start_prog = paddle.static.Program()
... input = paddle.randint(low=0, high=5, shape=[5])
with paddle.static.program_guard(main_prog, start_prog):
... out = emb(input)
x = paddle.static.data(name='x', shape=[None, 10])
... out.backward()
z = paddle.static.nn.fc(x, 100)
... print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.5->0.05...
loss = paddle.mean(z)
... adam.step()
scheduler = paddle.optimizer.lr.StepDecay(learning_rate=0.5, step_size=2, gamma=0.1)
... scheduler.step()
adam = paddle.optimizer.Adam(learning_rate=scheduler)
Learning rate of step0: 0.5
adam.minimize(loss)
Learning rate of step1: 0.5
Learning rate of step2: 0.05
exe = paddle.static.Executor()
Learning rate of step3: 0.05
exe.run(start_prog)
Learning rate of step4: 0.005000000000000001
for batch in range(10):
Learning rate of step5: 0.005000000000000001
print("Learning rate of step{}: {}", adam.get_lr()) # 0.5->0.05->0.005...
Learning rate of step6: 0.0005000000000000001
out = exe.run(main_prog, feed={'x': np.random.randn(3, 10).astype('float32')})
Learning rate of step7: 0.0005000000000000001
scheduler.step()
Learning rate of step8: 5.000000000000001e-05
Learning rate of step9: 5.000000000000001e-05
>>> # train on static graph mode
>>> paddle.enable_static()
>>> main_prog = paddle.static.Program()
>>> start_prog = paddle.static.Program()
>>> with paddle.static.program_guard(main_prog, start_prog):
... x = paddle.static.data(name='x', shape=[None, 10])
... z = paddle.static.nn.fc(x, 100)
... loss = paddle.mean(z)
... scheduler = paddle.optimizer.lr.StepDecay(learning_rate=0.5, step_size=2, gamma=0.1)
... adam = paddle.optimizer.Adam(learning_rate=scheduler)
... adam.minimize(loss)
>>> exe = paddle.static.Executor()
>>> exe.run(start_prog)
>>> for batch in range(10):
... print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.5->0.05->0.005...
... out = exe.run(main_prog, feed={'x': np.random.randn(3, 10).astype('float32')})
... scheduler.step()
Learning rate of step0: 0.5
Learning rate of step1: 0.5
Learning rate of step2: 0.05
Learning rate of step3: 0.05
Learning rate of step4: 0.005000000000000001
Learning rate of step5: 0.005000000000000001
Learning rate of step6: 0.0005000000000000001
Learning rate of step7: 0.0005000000000000001
Learning rate of step8: 5.000000000000001e-05
Learning rate of step9: 5.000000000000001e-05
"""
"""
if
isinstance
(
self
.
_learning_rate
,
float
):
if
isinstance
(
self
.
_learning_rate
,
float
):
return
self
.
_learning_rate
return
self
.
_learning_rate
...
@@ -1146,17 +1174,17 @@ class Optimizer:
...
@@ -1146,17 +1174,17 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
x = paddle.arange(26, dtype="float32").reshape([2, 13])
>>>
x = paddle.arange(26, dtype="float32").reshape([2, 13])
linear = paddle.nn.Linear(13, 5)
>>>
linear = paddle.nn.Linear(13, 5)
# This can be any optimizer supported by dygraph.
>>>
# This can be any optimizer supported by dygraph.
adam = paddle.optimizer.Adam(learning_rate = 0.01,
>>>
adam = paddle.optimizer.Adam(learning_rate = 0.01,
parameters = linear.parameters())
...
parameters = linear.parameters())
out = linear(x)
>>>
out = linear(x)
out.backward()
>>>
out.backward()
adam.step()
>>>
adam.step()
adam.clear_grad()
>>>
adam.clear_grad()
"""
"""
act_no_grad_set
=
None
act_no_grad_set
=
None
if
framework
.
in_dygraph_mode
():
if
framework
.
in_dygraph_mode
():
...
@@ -1218,16 +1246,16 @@ class Optimizer:
...
@@ -1218,16 +1246,16 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
inp = paddle.uniform([10, 10], dtype="float32", min=-0.1, max=0.1)
>>>
inp = paddle.uniform([10, 10], dtype="float32", min=-0.1, max=0.1)
linear = paddle.nn.Linear(10, 10)
>>>
linear = paddle.nn.Linear(10, 10)
out = linear(inp)
>>>
out = linear(inp)
loss = paddle.mean(out)
>>>
loss = paddle.mean(out)
optimizer = paddle.optimizer.Adam(learning_rate=0.1,
>>>
optimizer = paddle.optimizer.Adam(learning_rate=0.1,
parameters=linear.parameters())
...
parameters=linear.parameters())
params_grads = optimizer.backward(loss)
>>>
params_grads = optimizer.backward(loss)
optimizer.apply_gradients(params_grads)
>>>
optimizer.apply_gradients(params_grads)
"""
"""
...
@@ -1436,17 +1464,17 @@ class Optimizer:
...
@@ -1436,17 +1464,17 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
a = paddle.arange(26, dtype="float32").reshape([2, 13])
>>>
a = paddle.arange(26, dtype="float32").reshape([2, 13])
linear = paddle.nn.Linear(13, 5)
>>>
linear = paddle.nn.Linear(13, 5)
# This can be any optimizer supported by dygraph.
>>>
# This can be any optimizer supported by dygraph.
adam = paddle.optimizer.Adam(learning_rate = 0.01,
>>>
adam = paddle.optimizer.Adam(learning_rate = 0.01,
parameters = linear.parameters())
...
parameters = linear.parameters())
out = linear(a)
>>>
out = linear(a)
out.backward()
>>>
out.backward()
adam.step()
>>>
adam.step()
adam.clear_grad()
>>>
adam.clear_grad()
"""
"""
param_list
=
[]
param_list
=
[]
...
@@ -1494,21 +1522,21 @@ class Optimizer:
...
@@ -1494,21 +1522,21 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
linear = paddle.nn.Linear(10, 10)
>>>
linear = paddle.nn.Linear(10, 10)
input = paddle.uniform(shape=[10, 10], min=-0.1, max=0.1)
>>>
input = paddle.uniform(shape=[10, 10], min=-0.1, max=0.1)
out = linear(input)
>>>
out = linear(input)
loss = paddle.mean(out)
>>>
loss = paddle.mean(out)
beta1 = paddle.to_tensor([0.9], dtype="float32")
>>>
beta1 = paddle.to_tensor([0.9], dtype="float32")
beta2 = paddle.to_tensor([0.99], dtype="float32")
>>>
beta2 = paddle.to_tensor([0.99], dtype="float32")
adam = paddle.optimizer.Adam(learning_rate=0.1,
>>>
adam = paddle.optimizer.Adam(learning_rate=0.1,
parameters=linear.parameters(),
...
parameters=linear.parameters(),
weight_decay=0.01)
...
weight_decay=0.01)
loss.backward()
>>>
loss.backward()
adam.minimize(loss)
>>>
adam.minimize(loss)
adam.clear_grad()
>>>
adam.clear_grad()
"""
"""
assert
isinstance
(
loss
,
Variable
),
"The loss should be an Tensor."
assert
isinstance
(
loss
,
Variable
),
"The loss should be an Tensor."
...
@@ -1562,17 +1590,17 @@ class Optimizer:
...
@@ -1562,17 +1590,17 @@ class Optimizer:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
a = paddle.arange(26, dtype="float32").reshape([2, 13])
>>>
a = paddle.arange(26, dtype="float32").reshape([2, 13])
linear = paddle.nn.Linear(13, 5)
>>>
linear = paddle.nn.Linear(13, 5)
# This can be any optimizer supported by dygraph.
>>>
# This can be any optimizer supported by dygraph.
adam = paddle.optimizer.Adam(learning_rate = 0.01,
>>>
adam = paddle.optimizer.Adam(learning_rate = 0.01,
parameters = linear.parameters())
...
parameters = linear.parameters())
out = linear(a)
>>>
out = linear(a)
out.backward()
>>>
out.backward()
adam.step()
>>>
adam.step()
adam.clear_grad()
>>>
adam.clear_grad()
"""
"""
if
paddle
.
fluid
.
dygraph
.
base
.
in_declarative_mode
():
if
paddle
.
fluid
.
dygraph
.
base
.
in_declarative_mode
():
self
.
_declarative_step
()
self
.
_declarative_step
()
...
...
python/paddle/quantization/config.py
浏览文件 @
0c5781e5
...
@@ -70,12 +70,15 @@ class QuantConfig:
...
@@ -70,12 +70,15 @@ class QuantConfig:
Examples:
Examples:
.. code-block:: python
.. code-block:: python
from paddle.quantization import QuantConfig
>>>
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>>
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=quanter, weight=quanter)
>>> q_config = QuantConfig(activation=quanter, weight=quanter)
print(q_config)
>>> 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:
...
@@ -100,31 +103,36 @@ class QuantConfig:
weight
:
QuanterFactory
=
None
,
weight
:
QuanterFactory
=
None
,
):
):
r
"""
r
"""
Set the quantization config by layer. It has the highest priority among
Set the quantization config by layer. It has the highest priority among
all the setting methods.
all the setting methods.
Args:
Args:
layer(Union[Layer, list]): One or a list of layers.
layer(Union[Layer, list]): One or a list of layers.
activation(QuanterFactory): Quanter used for activations.
activation(QuanterFactory): Quanter used for activations.
weight(QuanterFactory): Quanter used for weights.
weight(QuanterFactory): Quanter used for weights.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>> import paddle
from paddle.nn import Linear
>>> from paddle.nn import Linear
from paddle.quantization import QuantConfig
>>> from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
class Model(paddle.nn.Layer):
>>> class Model(paddle.nn.Layer):
def __init__(self):
... def __init__(self):
super().__init__()
... super().__init__()
self.fc = Linear(576, 120)
... self.fc = Linear(576, 120)
model = Model()
>>> model = Model()
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
>>> q_config = QuantConfig(activation=None, weight=None)
q_config.add_layer_config([model.fc], activation=quanter, weight=quanter)
>>> q_config.add_layer_config([model.fc], activation=quanter, weight=quanter)
print(q_config)
>>> # doctest: +SKIP
>>> print(q_config)
Global config:
None
Layer prefix config:
{'linear_0': <paddle.quantization.config.SingleLayerConfig object at 0x7fe41a680ee0>}
"""
"""
if
isinstance
(
layer
,
list
):
if
isinstance
(
layer
,
list
):
...
@@ -144,31 +152,36 @@ class QuantConfig:
...
@@ -144,31 +152,36 @@ class QuantConfig:
weight
:
QuanterFactory
=
None
,
weight
:
QuanterFactory
=
None
,
):
):
r
"""
r
"""
Set the quantization config by full name of layer. Its priority is
Set the quantization config by full name of layer. Its priority is
lower than `add_layer_config`.
lower than `add_layer_config`.
Args:
Args:
layer_name(Union[str, list]): One or a list of layers' full name.
layer_name(Union[str, list]): One or a list of layers' full name.
activation(QuanterFactory): Quanter used for activations.
activation(QuanterFactory): Quanter used for activations.
weight(QuanterFactory): Quanter used for weights.
weight(QuanterFactory): Quanter used for weights.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>> import paddle
from paddle.nn import Linear
>>> from paddle.nn import Linear
from paddle.quantization import QuantConfig
>>> from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
class Model(paddle.nn.Layer):
>>> class Model(paddle.nn.Layer):
def __init__(self):
... def __init__(self):
super().__init__()
... super().__init__()
self.fc = Linear(576, 120)
... self.fc = Linear(576, 120)
model = Model()
>>> model = Model()
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
>>> q_config = QuantConfig(activation=None, weight=None)
q_config.add_name_config([model.fc.full_name()], activation=quanter, weight=quanter)
>>> q_config.add_name_config([model.fc.full_name()], activation=quanter, weight=quanter)
print(q_config)
>>> # 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
):
if
isinstance
(
layer_name
,
str
):
...
@@ -198,22 +211,27 @@ class QuantConfig:
...
@@ -198,22 +211,27 @@ class QuantConfig:
weight(QuanterFactory): Quanter used for weights.
weight(QuanterFactory): Quanter used for weights.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>> import paddle
from paddle.nn import Linear
>>> from paddle.nn import Linear
from paddle.quantization import QuantConfig
>>> from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
class Model(paddle.nn.Layer):
>>> class Model(paddle.nn.Layer):
def __init__(self):
... def __init__(self):
super().__init__()
... super().__init__()
self.fc = Linear(576, 120)
... self.fc = Linear(576, 120)
model = Model()
>>> model = Model()
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
>>> q_config = QuantConfig(activation=None, weight=None)
q_config.add_type_config([Linear], activation=quanter, weight=quanter)
>>> q_config.add_type_config([Linear], activation=quanter, weight=quanter)
print(q_config)
>>> # 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
(
if
isinstance
(
layer_type
,
type
)
and
issubclass
(
...
@@ -240,18 +258,18 @@ class QuantConfig:
...
@@ -240,18 +258,18 @@ class QuantConfig:
target(type): The type of layers that will be converted to.
target(type): The type of layers that will be converted to.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
from paddle.nn import Conv2D
>>>
from paddle.nn import Conv2D
from paddle.quantization import QuantConfig
>>>
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>>
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>>
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
>>>
q_config = QuantConfig(activation=None, weight=None)
class CustomizedQuantedConv2D:
>>>
class CustomizedQuantedConv2D:
def forward(self, x):
...
def forward(self, x):
pass
...
pass
# add some code for quantization simulation
...
# add some code for quantization simulation
q_config.add_qat_layer_mapping(Conv2D, CustomizedQuantedConv2D)
>>>
q_config.add_qat_layer_mapping(Conv2D, CustomizedQuantedConv2D)
"""
"""
assert
isinstance
(
source
,
type
)
and
issubclass
(
assert
isinstance
(
source
,
type
)
and
issubclass
(
source
,
paddle
.
nn
.
Layer
source
,
paddle
.
nn
.
Layer
...
@@ -272,13 +290,13 @@ class QuantConfig:
...
@@ -272,13 +290,13 @@ class QuantConfig:
layer_type(type): The type of layer to be declared as leaf.
layer_type(type): The type of layer to be declared as leaf.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
from paddle.nn import Sequential
>>>
from paddle.nn import Sequential
from paddle.quantization import QuantConfig
>>>
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>>
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
q_config = QuantConfig(activation=None, weight=None)
>>>
q_config = QuantConfig(activation=None, weight=None)
q_config.add_customized_leaf(Sequential)
>>>
q_config.add_customized_leaf(Sequential)
"""
"""
self
.
_customized_leaves
.
append
(
layer_type
)
self
.
_customized_leaves
.
append
(
layer_type
)
...
@@ -379,22 +397,22 @@ class QuantConfig:
...
@@ -379,22 +397,22 @@ class QuantConfig:
model(Layer): The model to be specified by the config.
model(Layer): The model to be specified by the config.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
from paddle.nn import Linear, Sequential
>>>
from paddle.nn import Linear, Sequential
from paddle.quantization import QuantConfig
>>>
from paddle.quantization import QuantConfig
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>>
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
class Model(paddle.nn.Layer):
>>>
class Model(paddle.nn.Layer):
def __init__(self):
...
def __init__(self):
super().__init__()
...
super().__init__()
self.fc = Sequential(Linear(576, 120),Linear(576, 120))
...
self.fc = Sequential(Linear(576, 120),Linear(576, 120))
model = Model()
>>>
model = Model()
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>>
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
q_config = QuantConfig(activation=None, weight=None)
>>>
q_config = QuantConfig(activation=None, weight=None)
q_config.add_layer_config([model.fc], activation=quanter, weight=quanter)
>>>
q_config.add_layer_config([model.fc], activation=quanter, weight=quanter)
q_config._specify(model)
>>>
q_config._specify(model)
"""
"""
self
.
_model
=
model
self
.
_model
=
model
self
.
_specify_helper
(
self
.
_model
)
self
.
_specify_helper
(
self
.
_model
)
...
...
python/paddle/quantization/factory.py
浏览文件 @
0c5781e5
...
@@ -83,21 +83,22 @@ def quanter(class_name):
...
@@ -83,21 +83,22 @@ def quanter(class_name):
Examples:
Examples:
.. code-block:: python
.. code-block:: python
# Given codes in ./customized_quanter.py
>>> # doctest: +SKIP
from paddle.quantization import quanter
>>> # Given codes in ./customized_quanter.py
from paddle.quantization import BaseQuanter
>>> from paddle.quantization import quanter
@quanter("CustomizedQuanter")
>>> from paddle.quantization import BaseQuanter
class CustomizedQuanterLayer(BaseQuanter):
>>> @quanter("CustomizedQuanter")
def __init__(self, arg1, kwarg1=None):
>>> class CustomizedQuanterLayer(BaseQuanter):
pass
... def __init__(self, arg1, kwarg1=None):
... pass
# Used in ./test.py
# from .customized_quanter import CustomizedQuanter
>>> # Used in ./test.py
from paddle.quantization import QuantConfig
>>> # from .customized_quanter import CustomizedQuanter
arg1_value = "test"
>>> from paddle.quantization import QuantConfig
kwarg1_value = 20
>>> arg1_value = "test"
quanter = CustomizedQuanter(arg1_value, kwarg1=kwarg1_value)
>>> kwarg1_value = 20
q_config = QuantConfig(activation=quanter, weight=quanter)
>>> 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:
...
@@ -135,79 +135,81 @@ class ImperativeQuantAware:
during training. If this attribute is not sets or the attribute is
during training. If this attribute is not sets or the attribute is
false, the Layer would be qunatized in training.
false, the Layer would be qunatized in training.
Examples 1:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>> import paddle
from paddle.static.quantization
\
>>> from paddle.static.quantization import (
import ImperativeQuantAware
... ImperativeQuantAware,
from paddle.vision.models
\
... )
import resnet
>>> from paddle.vision.models import (
... resnet,
model = resnet.resnet50(pretrained=True)
... )
imperative_qat = ImperativeQuantAware(
>>> model = resnet.resnet50(pretrained=True)
weight_quantize_type='abs_max',
activation_quantize_type='moving_average_abs_max')
>>> imperative_qat = ImperativeQuantAware(
... weight_quantize_type='abs_max',
# Add the fake quant logical.
... activation_quantize_type='moving_average_abs_max')
# The original model will be rewrite.
# The outscale of outputs in supportted layers would be calculated.
>>> # Add the fake quant logical.
imperative_qat.quantize(model)
>>> # The original model will be rewrite.
>>> # The outscale of outputs in supportted layers would be calculated.
# Fine-tune the quantized model
>>> imperative_qat.quantize(model)
# ...
>>> # Fine-tune the quantized model
# Save quant model for the inference.
>>> # ...
imperative_qat.save_quantized_model(
layer=model,
>>> # Save quant model for the inference.
model_path="./resnet50_qat",
>>> imperative_qat.save_quantized_model(
input_spec=[
... layer=model,
paddle.static.InputSpec(
... model_path="./resnet50_qat",
shape=[None, 3, 224, 224], dtype='float32')])
... input_spec=[
... paddle.static.InputSpec(
Examples 2:
... shape=[None, 3, 224, 224], dtype='float32')])
.. code-block:: python
.. code-block:: python
import paddle
from paddle.static.quantization
\
>>> import paddle
import ImperativeQuantAware
>>> from paddle.static.quantization import (
... ImperativeQuantAware,
class ImperativeModel(paddle.nn.Layer):
... )
def __init__(self):
super().__init__()
>>> class ImperativeModel(paddle.nn.Layer):
# self.linear_0 would skip the quantization.
... def __init__(self):
self.linear_0 = paddle.nn.Linear(784, 400)
... super().__init__()
self.linear_0.skip_quant = True
... # self.linear_0 would skip the quantization.
... self.linear_0 = paddle.nn.Linear(784, 400)
# self.linear_1 would not skip the quantization.
... self.linear_0.skip_quant = True
self.linear_1 = paddle.nn.Linear(400, 10)
self.linear_1.skip_quant = False
... # self.linear_1 would not skip the quantization.
... self.linear_1 = paddle.nn.Linear(400, 10)
def forward(self, inputs):
... self.linear_1.skip_quant = False
x = self.linear_0(inputs)
x = self.linear_1(inputs)
... def forward(self, inputs):
return x
... x = self.linear_0(inputs)
... x = self.linear_1(inputs)
model = ImperativeModel()
... return x
imperative_qat = ImperativeQuantAware(
weight_quantize_type='abs_max',
>>> model = ImperativeModel()
activation_quantize_type='moving_average_abs_max')
>>> imperative_qat = ImperativeQuantAware(
... weight_quantize_type='abs_max',
# Add the fake quant logical.
... activation_quantize_type='moving_average_abs_max')
# The original model will be rewrite.
#
>>> # Add the fake quant logical.
# There is only one Layer(self.linear1) would be added the
>>> # The original model will be rewrite.
# fake quant logical.
>>> #
imperative_qat.quantize(model)
>>> # There is only one Layer(self.linear1) would be added the
>>> # fake quant logical.
# Fine-tune the quantized model
>>> imperative_qat.quantize(model)
# ...
>>> # Fine-tune the quantized model
# Save quant model for the inference.
>>> # ...
imperative_qat.save_quantized_model(
layer=model,
>>> # Save quant model for the inference.
model_path="./imperative_model_qat")
>>> imperative_qat.save_quantized_model(
... layer=model,
... model_path="./imperative_model_qat")
"""
"""
super
().
__init__
()
super
().
__init__
()
self
.
fuse_conv_bn
=
fuse_conv_bn
self
.
fuse_conv_bn
=
fuse_conv_bn
...
@@ -245,39 +247,40 @@ class ImperativeQuantAware:
...
@@ -245,39 +247,40 @@ class ImperativeQuantAware:
None
None
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>> import paddle
from paddle.static.quantization
\
>>> from paddle.static.quantization import (
import ImperativeQuantAware
... ImperativeQuantAware,
... )
class ImperativeModel(paddle.nn.Layer):
def __init__(self):
>>> class ImperativeModel(paddle.nn.Layer):
super().__init__()
... def __init__(self):
# self.linear_0 would skip the quantization.
... super().__init__()
self.linear_0 = paddle.nn.Linear(784, 400)
... # self.linear_0 would skip the quantization.
self.linear_0.skip_quant = True
... 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 would not skip the quantization.
self.linear_1.skip_quant = False
... self.linear_1 = paddle.nn.Linear(400, 10)
... self.linear_1.skip_quant = False
def forward(self, inputs):
x = self.linear_0(inputs)
... def forward(self, inputs):
x = self.linear_1(inputs)
... x = self.linear_0(inputs)
return x
... x = self.linear_1(inputs)
... return x
model = ImperativeModel()
imperative_qat = ImperativeQuantAware(
>>> model = ImperativeModel()
weight_quantize_type='abs_max',
>>> imperative_qat = ImperativeQuantAware(
activation_quantize_type='moving_average_abs_max')
... weight_quantize_type='abs_max',
... activation_quantize_type='moving_average_abs_max')
# Add the fake quant logical.
# The original model will be rewrite.
>>> # 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.
>>> # There is only one Layer(self.linear1) would be added the
imperative_qat.quantize(model)
>>> # fake quant logical.
>>> imperative_qat.quantize(model)
"""
"""
assert
isinstance
(
assert
isinstance
(
model
,
paddle
.
nn
.
Layer
model
,
paddle
.
nn
.
Layer
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录