Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ff226ba1
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
ff226ba1
编写于
8月 03, 2023
作者:
C
cyberslack_lee
提交者:
GitHub
8月 03, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[xdoctest] reformat example code with google style in No.95-99 (#55834)
* test=docs_preview * test=docs_preview
上级
9db219d1
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
94 addition
and
75 deletion
+94
-75
python/paddle/nn/quant/quant_layers.py
python/paddle/nn/quant/quant_layers.py
+15
-13
python/paddle/nn/quant/stub.py
python/paddle/nn/quant/stub.py
+35
-20
python/paddle/nn/utils/clip_grad_norm_.py
python/paddle/nn/utils/clip_grad_norm_.py
+12
-10
python/paddle/nn/utils/clip_grad_value_.py
python/paddle/nn/utils/clip_grad_value_.py
+11
-10
python/paddle/nn/utils/spectral_norm_hook.py
python/paddle/nn/utils/spectral_norm_hook.py
+21
-22
未找到文件。
python/paddle/nn/quant/quant_layers.py
浏览文件 @
ff226ba1
...
@@ -611,19 +611,21 @@ class QuantizedConv2DTranspose(Layer):
...
@@ -611,19 +611,21 @@ class QuantizedConv2DTranspose(Layer):
The only difference is that its inputs are all fake quantized.
The only difference is that its inputs are all fake quantized.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>> import paddle
import paddle.nn as nn
>>> import paddle.nn as nn
from paddle.nn.quant.quant_layers import QuantizedConv2DTranspose
>>> from paddle.nn.quant.quant_layers import QuantizedConv2DTranspose
x_var = paddle.uniform((2, 4, 8, 8), dtype='float32', min=-1., max=1.)
>>> x_var = paddle.uniform((2, 4, 8, 8), dtype='float32', min=-1., max=1.)
conv = nn.Conv2DTranspose(4, 6, (3, 3))
>>> conv = nn.Conv2DTranspose(4, 6, (3, 3))
conv_quantized = QuantizedConv2DTranspose(conv)
>>> conv_quantized = QuantizedConv2DTranspose(conv)
y_quantized = conv_quantized(x_var)
>>> y_quantized = conv_quantized(x_var)
y_var = conv(x_var)
>>> y_var = conv(x_var)
print(y_var.shape, y_quantized.shape)
>>> print(y_var.shape)
# [2, 6, 10, 10], [2, 6, 10, 10]
[2, 6, 10, 10]
>>> print(y_quantized.shape)
[2, 6, 10, 10]
"""
"""
...
...
python/paddle/nn/quant/stub.py
浏览文件 @
ff226ba1
...
@@ -23,31 +23,46 @@ class Stub(Layer):
...
@@ -23,31 +23,46 @@ class Stub(Layer):
the forward of a layer. Instead, we can create a stub and add it to the sublayers of the layer.
the forward of a layer. Instead, we can create a stub and add it to the sublayers of the layer.
And call the stub before the functional API in the forward. The observer held by the
And call the stub before the functional API in the forward. The observer held by the
stub will observe or quantize the inputs of the functional API.
stub will observe or quantize the inputs of the functional API.
Args:
Args:
observer(QuanterFactory) - The configured information of the observer to be inserted.
observer(QuanterFactory) - The configured information of the observer to be inserted.
It will use a global configuration to create the observers if the 'observer' is none.
It will use a global configuration to create the observers if the 'observer' is none.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
from paddle.nn.quant import Stub
>>> import paddle
from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
>>> from paddle.nn.quant import Stub
from paddle.nn import Conv2D
>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
from paddle.quantization import QAT, QuantConfig
>>> from paddle.nn import Conv2D
quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> from paddle.quantization import QAT, QuantConfig
class Model(paddle.nn.Layer):
def __init__(self, num_classes=10):
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
super().__init__()
>>> class Model(paddle.nn.Layer):
self.conv = Conv2D(3, 6, 3, stride=1, padding=1)
... def __init__(self, num_classes=10):
self.quant = Stub(quanter)
... super().__init__()
def forward(self, inputs):
... self.conv = Conv2D(3, 6, 3, stride=1, padding=1)
out = self.conv(inputs)
... self.quant = Stub(quanter)
out = self.quant(out)
...
return paddle.nn.functional.relu(out)
... def forward(self, inputs):
model = Model()
... out = self.conv(inputs)
q_config = QuantConfig(activation=quanter, weight=quanter)
... out = self.quant(out)
qat = QAT(q_config)
... return paddle.nn.functional.relu(out)
quant_model = qat.quantize(model)
print(quant_model)
>>> model = Model()
>>> q_config = QuantConfig(activation=quanter, weight=quanter)
>>> qat = QAT(q_config)
>>> quant_model = qat.quantize(model)
>>> print(quant_model)
Model(
(conv): QuantedConv2D(
(weight_quanter): FakeQuanterWithAbsMaxObserverLayer()
(activation_quanter): FakeQuanterWithAbsMaxObserverLayer()
)
(quant): QuanterStub(
(_observer): FakeQuanterWithAbsMaxObserverLayer()
)
)
"""
"""
def
__init__
(
self
,
observer
=
None
):
def
__init__
(
self
,
observer
=
None
):
...
...
python/paddle/nn/utils/clip_grad_norm_.py
浏览文件 @
ff226ba1
...
@@ -43,21 +43,23 @@ def clip_grad_norm_(
...
@@ -43,21 +43,23 @@ def clip_grad_norm_(
Returns:
Returns:
Total norm of the parameter gradients (treated as a single vector).
Total norm of the parameter gradients (treated as a single vector).
Example:
Example:
.. code-block:: python
.. code-block:: python
import paddle
x = paddle.uniform([10, 10], min=-1.0, max=1.0, dtype='float32')
>>> import paddle
max_norm = float(5.0)
linear = paddle.nn.Linear(in_features=10, out_features=10)
>>> x = paddle.uniform([10, 10], min=-1.0, max=1.0, dtype='float32')
out = linear(x)
>>> max_norm = float(5.0)
loss = paddle.mean(out)
>>> linear = paddle.nn.Linear(in_features=10, out_features=10)
loss.backward()
>>> out = linear(x)
>>> loss = paddle.mean(out)
>>> loss.backward()
paddle.nn.utils.clip_grad_norm_(linear.parameters(), max_norm)
>>>
paddle.nn.utils.clip_grad_norm_(linear.parameters(), max_norm)
sdg = paddle.optimizer.SGD(learning_rate=0.1, parameters=linear.parameters())
>>>
sdg = paddle.optimizer.SGD(learning_rate=0.1, parameters=linear.parameters())
sdg.step()
>>>
sdg.step()
"""
"""
if
not
paddle
.
in_dynamic_mode
():
if
not
paddle
.
in_dynamic_mode
():
raise
RuntimeError
(
'this API can only run in dynamic mode.'
)
raise
RuntimeError
(
'this API can only run in dynamic mode.'
)
...
...
python/paddle/nn/utils/clip_grad_value_.py
浏览文件 @
ff226ba1
...
@@ -31,19 +31,20 @@ def clip_grad_value_(
...
@@ -31,19 +31,20 @@ def clip_grad_value_(
clip_value (float or int): maximum allowed value of the gradients.
clip_value (float or int): maximum allowed value of the gradients.
The gradients are clipped in the range
The gradients are clipped in the range
:math:`\left[\text{-clip\_value}, \text{clip\_value}\right]`
:math:`\left[\text{-clip\_value}, \text{clip\_value}\right]`
Example:
Example:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
x = paddle.uniform([10, 10], min=-10.0, max=10.0, dtype='float32')
>>>
x = paddle.uniform([10, 10], min=-10.0, max=10.0, dtype='float32')
clip_value = float(5.0)
>>>
clip_value = float(5.0)
linear = paddle.nn.Linear(in_features=10, out_features=10)
>>>
linear = paddle.nn.Linear(in_features=10, out_features=10)
out = linear(x)
>>>
out = linear(x)
loss = paddle.mean(out)
>>>
loss = paddle.mean(out)
loss.backward()
>>>
loss.backward()
paddle.nn.utils.clip_grad_value_(linear.parameters(), clip_value)
>>>
paddle.nn.utils.clip_grad_value_(linear.parameters(), clip_value)
sdg = paddle.optimizer.SGD(learning_rate=0.1, parameters=linear.parameters())
>>>
sdg = paddle.optimizer.SGD(learning_rate=0.1, parameters=linear.parameters())
sdg.step()
>>>
sdg.step()
"""
"""
if
not
paddle
.
in_dynamic_mode
():
if
not
paddle
.
in_dynamic_mode
():
raise
RuntimeError
(
'this API can only run in dynamic mode.'
)
raise
RuntimeError
(
'this API can only run in dynamic mode.'
)
...
...
python/paddle/nn/utils/spectral_norm_hook.py
浏览文件 @
ff226ba1
...
@@ -182,28 +182,27 @@ def spectral_norm(
...
@@ -182,28 +182,27 @@ def spectral_norm(
Layer, the original layer with the spectral norm hook.
Layer, the original layer with the spectral norm hook.
Examples:
Examples:
.. code-block:: python
.. code-block:: python
from paddle.nn import Conv2D
>>> from paddle.nn import Conv2D
from paddle.nn.utils import spectral_norm
>>> from paddle.nn.utils import spectral_norm
>>> paddle.seed(2023)
conv = Conv2D(3, 1, 3)
>>> conv = Conv2D(3, 1, 3)
sn_conv = spectral_norm(conv)
>>> sn_conv = spectral_norm(conv)
print(sn_conv)
>>> print(sn_conv)
# Conv2D(3, 1, kernel_size=[3, 3], data_format=NCHW)
Conv2D(3, 1, kernel_size=[3, 3], data_format=NCHW)
print(sn_conv.weight)
>>> # Conv2D(3, 1, kernel_size=[3, 3], data_format=NCHW)
# Tensor(shape=[1, 3, 3, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=False,
>>> print(sn_conv.weight)
# [[[[-0.21090528, 0.18563725, -0.14127982],
Tensor(shape=[1, 3, 3, 3], dtype=float32, place=Place(cpu), stop_gradient=False,
# [-0.02310637, 0.03197737, 0.34353802],
[[[[ 0.01668976, 0.30305523, 0.11405435],
# [-0.17117859, 0.33152047, -0.28408015]],
[-0.06765547, -0.50396705, -0.40925547],
#
[ 0.47344422, 0.03628403, 0.45277366]],
# [[-0.13336606, -0.01862637, 0.06959272],
[[-0.15177251, -0.16305730, -0.15723954],
# [-0.02236020, -0.27091628, -0.24532901],
[-0.28081197, -0.09183260, -0.08081978],
# [ 0.27254242, 0.15516677, 0.09036587]],
[-0.40895155, 0.18298769, -0.29325116]],
#
[[ 0.21819633, -0.01822380, -0.50351536],
# [[ 0.30169338, -0.28146112, -0.11768346],
[-0.06262003, 0.17713565, 0.20517939],
# [-0.45765871, -0.12504843, -0.17482486],
[ 0.16659889, -0.14333329, 0.05228264]]]])
# [-0.36866254, -0.19969313, 0.08783543]]]])
"""
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录