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

Add API for quantization-aware training in dygraph mode #49398

Merged
merged 3 commits into from
Jan 11, 2023

Conversation

wanghaoshuang
Copy link
Contributor

@wanghaoshuang wanghaoshuang commented Dec 28, 2022

PR types

Others

PR changes

APIs

Describe

Add API for quantization-aware training in dygraph mode

  1. Expose an API named paddle.quantization.QAT
  2. Define a wrapper class to insert quanters into model for QAT
  3. Add QuantedConv2D and QuantedLinear
  4. Add some functions in QuantConfig for QAT
  5. Add unittest for QAT

如下图所示,本PR主要新增三个模块:Wrapper、QuantedLayers(包括QuantedConv2D、QquantedLinear)、QAT
image

Wrapper

装饰器,将任意Layer和Quanter/Observer包装成一个Layer。
image
image

QuantedLayers

与Wrapper类似,装饰器类,用于在Layer前插入Observer/Qaunter. Wrapper针对无权重Layer,QuantedLayers针对带权重的特定Layer,比如QuantedConv2D.
image

QAT

类图
image

时序图
image

示例

以下示例来自paddle/tests/quantization/test_qat.py
原始模型

Model(
  (features): Sequential(
    (0): Conv2D(3, 6, kernel_size=[3, 3], padding=1, data_format=NCHW)
    (1): ReLU()
    (2): MaxPool2D(kernel_size=2, stride=2, padding=0)
    (3): Conv2D(6, 16, kernel_size=[5, 5], data_format=NCHW)
    (4): ReLU()
    (5): MaxPool2D(kernel_size=2, stride=2, padding=0)
  )
  (fc): Sequential(
    (0): Linear(in_features=576, out_features=120, dtype=float32)
    (1): Linear(in_features=120, out_features=84, dtype=float32)
    (2): Linear(in_features=84, out_features=10, dtype=float32)
  )
)

调用qat.quantize(mdoel)之后的模型:

Model(
  (features): Sequential(
    (0): QuantedConv2D(
      (weight_quanter): FakeQuanterWithAbsMaxObserverLayer()
      (activation_quanter): FakeQuanterWithAbsMaxObserverLayer()
    )
    (1): ObserveWrapper(
      (_observer): FakeQuanterWithAbsMaxObserverLayer()
      (_observed): ReLU()
    )
    (2): ObserveWrapper(
      (_observer): FakeQuanterWithAbsMaxObserverLayer()
      (_observed): MaxPool2D(kernel_size=2, stride=2, padding=0)
    )
    (3): QuantedConv2D(
      (weight_quanter): FakeQuanterWithAbsMaxObserverLayer()
      (activation_quanter): FakeQuanterWithAbsMaxObserverLayer()
    )
    (4): ObserveWrapper(
      (_observer): FakeQuanterWithAbsMaxObserverLayer()
      (_observed): ReLU()
    )
    (5): ObserveWrapper(
      (_observer): FakeQuanterWithAbsMaxObserverLayer()
      (_observed): MaxPool2D(kernel_size=2, stride=2, padding=0)
    )
  )
  (fc): Sequential(
    (0): QuantedLinear(
      (weight_quanter): FakeQuanterWithAbsMaxObserverLayer()
      (activation_quanter): FakeQuanterWithAbsMaxObserverLayer()
    )
    (1): QuantedLinear(
      (weight_quanter): FakeQuanterWithAbsMaxObserverLayer()
      (activation_quanter): FakeQuanterWithAbsMaxObserverLayer()
    )
    (2): QuantedLinear(
      (weight_quanter): FakeQuanterWithAbsMaxObserverLayer()
      (activation_quanter): FakeQuanterWithAbsMaxObserverLayer()
    )
  )
)

1. Expose an API named paddle.quantization.QAT
2. Define a wrapper class to insert quanters into model for QAT
3. Add some functions in QuantConfig for QAT
4. Add unittest for QAT
@paddle-bot
Copy link

paddle-bot bot commented Dec 28, 2022

你的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.

Copy link
Contributor

@yghstill yghstill left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@risemeup1 risemeup1 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@zhangbo9674 zhangbo9674 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@chenwhql chenwhql left a comment

Choose a reason for hiding this comment

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

LGTM for setup.py.in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants