Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xdoctest] reformat example code with google style No.116-119 #56118

Merged
merged 15 commits into from
Aug 15, 2023

Conversation

AndSonder
Copy link
Contributor

PR types

Others

PR changes

Others

Description

修改如下文件的示例代码为新的格式,并通过 xdoctest 检查:

  1. python/paddle/optimizer/optimizer.py
  2. python/paddle/quantization/config.py
  3. python/paddle/quantization/factory.py
  4. python/paddle/quantization/imperative/qat.py

@paddle-bot
Copy link

paddle-bot bot commented Aug 9, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Aug 9, 2023
... adam.set_lr(lr_list[i])
... lr = adam.get_lr()
... print("current lr is {}".format(lr))
Print:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

输出结果里没有 Print:

Comment on lines 173 to 174
>>> from paddle.static.quantization \
... import ImperativeQuantAware
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import 代码风格改一下吧

>>> from paddle.static.quantization import (
...     ImperativeQuantAware,
... )

print(q_config)
>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
>>> q_config = QuantConfig(activation=quanter, weight=quanter)
>>> print(q_config)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里输出方便打印么?方便的话最好加一下输出,如果输出随机可以考虑 skip,如果输出太多可以考虑用 ... 或者不写

@AndSonder
Copy link
Contributor Author

@SigureMo Done~

SigureMo
SigureMo previously approved these changes Aug 10, 2023
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

都一样哈,如果合适就贴一下输出,不合适就不用了

>>> 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里

>>> 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有这里

>>> 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

和这里

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Global config:
None
Layer prefix config:
{'linear_0': <paddle.quantization.config.SingleLayerConfig object at 0x7fe41a680ee0>}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有内存地址的肯定不能保证输出一致的,可以用 # doctest: +SKIP 跳过输出检查

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有内存地址的肯定不能保证输出一致的,可以用 # doctest: +SKIP 跳过输出检查

done

@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Aug 10, 2023
@SigureMo
Copy link
Member

code style 需要处理一下

@SigureMo
Copy link
Member

image

codestyle 流水线,应该是有 trailing whitespace,pre-commit 处理一下就行了

>>> arg1_value = "test"
>>> kwarg1_value = 20
>>> quanter = CustomizedQuanter(arg1_value, kwarg1=kwarg1_value)
>>> q_config = QuantConfig(activation=quanter, weight=quanter)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码看起来不能在 xdoctest 环境跑,skip 掉吧

SigureMo
SigureMo previously approved these changes Aug 12, 2023
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

@@ -155,20 +163,25 @@ def add_name_config(
Examples:
.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. code-block::python 要缩进

@@ -200,20 +213,25 @@ def add_type_config(
Examples:
.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要缩进

@@ -242,16 +260,16 @@ def add_qat_layer_mapping(self, source: type, target: type):
Examples:
.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要缩进

@@ -274,11 +292,11 @@ def add_customized_leaf(self, layer_type: type):
Examples:
.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要缩进

@@ -381,20 +399,20 @@ def _specify(self, model: Layer):
Examples:
.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要缩进

@@ -247,37 +250,38 @@ def quantize(self, model):
Examples:
.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要缩进

... input = paddle.randint(low=0, high=5, shape=[5])
... out = emb(input)
... out.backward()
... print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.01
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里要有输出~

Comment on lines +634 to +636
... print("Learning rate of step{}: {}".format(batch, adam.get_lr())) # 0.5->0.05...
... adam.step()
... scheduler.step()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里要有输出~

Comment on lines 653 to 655
... print("Learning rate of step{}: {}", 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要有输出~

Comment on lines 86 to 101
>>> # 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
>>> # doctest: +SKIP
>>> arg1_value = "test"
>>> kwarg1_value = 20
>>> quanter = CustomizedQuanter(arg1_value, kwarg1=kwarg1_value)
>>> q_config = QuantConfig(activation=quanter, weight=quanter)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

整段 skip 吧~

@AndSonder
Copy link
Contributor Author

麻烦再review一下~ @megemini

Copy link
Contributor

@megemini megemini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples 1: 要改一下哈~

Comment on lines 676 to 685
Learning rate of step{}: {} 0.5
Learning rate of step{}: {} 0.5
Learning rate of step{}: {} 0.05
Learning rate of step{}: {} 0.05
Learning rate of step{}: {} 0.005000000000000001
Learning rate of step{}: {} 0.005000000000000001
Learning rate of step{}: {} 0.0005000000000000001
Learning rate of step{}: {} 0.0005000000000000001
Learning rate of step{}: {} 5.000000000000001e-05
Learning rate of step{}: {} 5.000000000000001e-05
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print("Learning rate of step{}: {}", adam.get_lr()) -> print("Learning rate of step{}: {}", batch, adam.get_lr())

Comment on lines 114 to 115
Examples:
.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. code-block:: python 要有缩进~

Copy link
Member

@SigureMo SigureMo Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这居然还反向缩进了:joy:,缩退么 [doge]

整段 docstring 的缩进都有问题,不要多余的一个空格

Copy link
Contributor Author

@AndSonder AndSonder Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是原来的缩进就有问题,我改漏了, 已经改过来了, 实在不好意思,改的时候可能整段的缩进出问题了

>>> # Save quant model for the inference.
>>> imperative_qat.save_quantized_model(
... layer=model,
... model_path="./imperative_model_qat")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要写成

Examples:
    .. code-block:: python

        >>> xxxxx

Examples:
    .. code-block:: python

        >>> yyyyy

而是写成

Examples:
    .. code-block:: python
        :name: code-example-1

        >>> xxxxx

    .. code-block:: python
        :name: code-example-2

        >>> yyyyy

因为本 API 非公开 API,所以就不用添加 name 了,写成下面即可:

Examples:
    .. code-block:: python

        >>> xxxxx

    .. code-block:: python

        >>> yyyyy

简单来说就是删除第二个 Examples: 这一行

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

@luotao1 luotao1 merged commit 0c5781e into PaddlePaddle:develop Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants