From 25b302252b5167d8d3aa092d61e1d051e5f6cac8 Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Wed, 20 Dec 2023 20:21:22 +0800 Subject: [PATCH 01/10] add cn docs of qat --- docs/api/paddle/quantization/qat_cn.rst | 114 ++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 docs/api/paddle/quantization/qat_cn.rst diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst new file mode 100644 index 00000000000..c09789dd1d0 --- /dev/null +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -0,0 +1,114 @@ +.. _cn_api_paddle_quantization_qat: +qat +------------------------------- + +.. py:class::class QAT(Quantization) +用于为量化感知训练准备模型的工具。 + +参数 +::::::::: + - **config** (QuantConfig) - 量化配置,通常指的是设置和调整模型量化过程中的参数和选项。 + +代码示例 +:::::::::: + +>>> from paddle.quantization import QAT, QuantConfig +>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver +>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9) +>>> q_config = QuantConfig(activation=quanter, weight=quanter) +>>> qat = QAT(q_config) + +.. py:class::class QAT(Quantization) +创建一个适用于量化感知训练的模型。 + +量化配置将在模型中传播。并且它将在模型中插入伪量化器以模拟量化过程。 + +参数 +::::::::: + - **model(Layer)**- + - **inplace(tool)**- + +返回 +::::::::: +为量化感知训练准备好的模型。 + +代码示例 +:::::::::: + +>>> from paddle.quantization import QAT, QuantConfig +>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver +>>> from paddle.vision.models import LeNet + +>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9) +>>> q_config = QuantConfig(activation=quanter, weight=quanter) +>>> qat = QAT(q_config) +>>> model = LeNet() +>>> quant_model = qat.quantize(model) +>>> print(quant_model) +LeNet( + (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() + ) + ) +) + +.. convert(model: paddle.nn.layer.layers.Layer, inplace=False, remain_weight=False) +将量化模型转换为ONNX格式。转换后的模型可以通过调用paddle.jit.save保存为推理模型。:参数模型::类型模型: Layer:原地操作参数: +:原理类型: bool, optional:剩余权重参数::类型剩余权重: bool, optional + +返回 +::::::::: +转换后的模型 + +代码示例 +:::::::::: + +>>> import paddle +>>> from paddle.quantization import QAT, QuantConfig +>>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver +>>> from paddle.vision.models import LeNet + +>>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9) +>>> q_config = QuantConfig(activation=quanter, weight=quanter) +>>> qat = QAT(q_config) +>>> model = LeNet() +>>> quantized_model = qat.quantize(model) +>>> converted_model = qat.convert(quantized_model) +>>> dummy_data = paddle.rand([1, 1, 32, 32], dtype="float32") +>>> paddle.jit.save(converted_model, "./quant_deploy", [dummy_data]) \ No newline at end of file From 46594e884324c7113b8c9bae8f9a64d489dbabc2 Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Wed, 20 Dec 2023 21:02:19 +0800 Subject: [PATCH 02/10] Update qat_cn.rst --- docs/api/paddle/quantization/qat_cn.rst | 70 ++++--------------------- 1 file changed, 9 insertions(+), 61 deletions(-) diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst index c09789dd1d0..8b51939b003 100644 --- a/docs/api/paddle/quantization/qat_cn.rst +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -1,8 +1,9 @@ .. _cn_api_paddle_quantization_qat: + qat ------------------------------- -.. py:class::class QAT(Quantization) +.. py:class:: paddle.quantization.QAT(config: paddle.quantization.config.QuantConfig) 用于为量化感知训练准备模型的工具。 参数 @@ -12,13 +13,9 @@ qat 代码示例 :::::::::: ->>> from paddle.quantization import QAT, QuantConfig ->>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver ->>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9) ->>> q_config = QuantConfig(activation=quanter, weight=quanter) ->>> qat = QAT(q_config) +COPY-FROM: paddle.quantization.qat -.. py:class::class QAT(Quantization) +.. py:class:: quantize(model: paddle.nn.layer.layers.Layer, inplace=False) 创建一个适用于量化感知训练的模型。 量化配置将在模型中传播。并且它将在模型中插入伪量化器以模拟量化过程。 @@ -35,60 +32,9 @@ qat 代码示例 :::::::::: ->>> from paddle.quantization import QAT, QuantConfig ->>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver ->>> from paddle.vision.models import LeNet - ->>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9) ->>> q_config = QuantConfig(activation=quanter, weight=quanter) ->>> qat = QAT(q_config) ->>> model = LeNet() ->>> quant_model = qat.quantize(model) ->>> print(quant_model) -LeNet( - (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() - ) - ) -) +COPY-FROM: paddle.quantization.qat -.. convert(model: paddle.nn.layer.layers.Layer, inplace=False, remain_weight=False) +.. py:function:: convert(model: paddle.nn.layer.layers.Layer, inplace=False, remain_weight=False) 将量化模型转换为ONNX格式。转换后的模型可以通过调用paddle.jit.save保存为推理模型。:参数模型::类型模型: Layer:原地操作参数: :原理类型: bool, optional:剩余权重参数::类型剩余权重: bool, optional @@ -111,4 +57,6 @@ LeNet( >>> quantized_model = qat.quantize(model) >>> converted_model = qat.convert(quantized_model) >>> dummy_data = paddle.rand([1, 1, 32, 32], dtype="float32") ->>> paddle.jit.save(converted_model, "./quant_deploy", [dummy_data]) \ No newline at end of file +>>> paddle.jit.save(converted_model, "./quant_deploy", [dummy_data]) +>>> dummy_data = paddle.rand([1, 1, 32, 32], dtype="float32") +>>> paddle.jit.save(converted_model, "./quant_deploy", [dummy_data]) From b29bee17d9d9c35ab1ad786344113a8d61607a61 Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Wed, 3 Jan 2024 11:32:26 +0800 Subject: [PATCH 03/10] Update qat_cn.rst --- docs/api/paddle/quantization/qat_cn.rst | 57 +++++++++---------------- 1 file changed, 19 insertions(+), 38 deletions(-) diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst index 8b51939b003..4df14600c11 100644 --- a/docs/api/paddle/quantization/qat_cn.rst +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -1,62 +1,43 @@ .. _cn_api_paddle_quantization_qat: -qat +QAT ------------------------------- .. py:class:: paddle.quantization.QAT(config: paddle.quantization.config.QuantConfig) 用于为量化感知训练准备模型的工具。 -参数 -::::::::: +**参数** - **config** (QuantConfig) - 量化配置,通常指的是设置和调整模型量化过程中的参数和选项。 -代码示例 -:::::::::: +**代码示例** -COPY-FROM: paddle.quantization.qat +COPY-FROM: paddle.quantization.QAT.quantize -.. py:class:: quantize(model: paddle.nn.layer.layers.Layer, inplace=False) +方法 +:::::::::::: +quantize(model: Layer, inplace=False) +''''''''' 创建一个适用于量化感知训练的模型。 量化配置将在模型中传播。并且它将在模型中插入伪量化器以模拟量化过程。 -参数 -::::::::: - - **model(Layer)**- - - **inplace(tool)**- +**参数** + - **model(Layer)** - 待量化的模型 + - **inplace(bool)** - 是否对模型进行原地修改 -返回 -::::::::: +**返回** 为量化感知训练准备好的模型。 -代码示例 -:::::::::: +**代码示例** -COPY-FROM: paddle.quantization.qat +COPY-FROM: paddle.quantization.QAT.quantize -.. py:function:: convert(model: paddle.nn.layer.layers.Layer, inplace=False, remain_weight=False) +方法 +:::::::::::: +convert(model: paddle.nn.layer.layers.Layer, inplace=False, remain_weight=False) +''''''''' 将量化模型转换为ONNX格式。转换后的模型可以通过调用paddle.jit.save保存为推理模型。:参数模型::类型模型: Layer:原地操作参数: :原理类型: bool, optional:剩余权重参数::类型剩余权重: bool, optional -返回 -::::::::: +**返回** 转换后的模型 - -代码示例 -:::::::::: - ->>> import paddle ->>> from paddle.quantization import QAT, QuantConfig ->>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver ->>> from paddle.vision.models import LeNet - ->>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9) ->>> q_config = QuantConfig(activation=quanter, weight=quanter) ->>> qat = QAT(q_config) ->>> model = LeNet() ->>> quantized_model = qat.quantize(model) ->>> converted_model = qat.convert(quantized_model) ->>> dummy_data = paddle.rand([1, 1, 32, 32], dtype="float32") ->>> paddle.jit.save(converted_model, "./quant_deploy", [dummy_data]) ->>> dummy_data = paddle.rand([1, 1, 32, 32], dtype="float32") ->>> paddle.jit.save(converted_model, "./quant_deploy", [dummy_data]) From 9d3e58314afdc49ffb25d59e25d0ba5f4077dfa5 Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:27:23 +0800 Subject: [PATCH 04/10] Update docs/api/paddle/quantization/qat_cn.rst Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- docs/api/paddle/quantization/qat_cn.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst index 4df14600c11..77f645c2d4b 100644 --- a/docs/api/paddle/quantization/qat_cn.rst +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -1,4 +1,4 @@ -.. _cn_api_paddle_quantization_qat: +.. _cn_api_paddle_quantization_QAT: QAT ------------------------------- From 3c304407e34b82591f3e66a4a452deba07eab474 Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:27:33 +0800 Subject: [PATCH 05/10] Update docs/api/paddle/quantization/qat_cn.rst Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- docs/api/paddle/quantization/qat_cn.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst index 77f645c2d4b..59d04806f8f 100644 --- a/docs/api/paddle/quantization/qat_cn.rst +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -6,7 +6,8 @@ QAT .. py:class:: paddle.quantization.QAT(config: paddle.quantization.config.QuantConfig) 用于为量化感知训练准备模型的工具。 -**参数** +参数 +:::::::::::: - **config** (QuantConfig) - 量化配置,通常指的是设置和调整模型量化过程中的参数和选项。 **代码示例** From 05439ab7366d1654c19e45126aabbe94db902447 Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:28:53 +0800 Subject: [PATCH 06/10] Update qat_cn.rst --- docs/api/paddle/quantization/qat_cn.rst | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst index 59d04806f8f..a01b62d9a79 100644 --- a/docs/api/paddle/quantization/qat_cn.rst +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -35,10 +35,14 @@ COPY-FROM: paddle.quantization.QAT.quantize 方法 :::::::::::: -convert(model: paddle.nn.layer.layers.Layer, inplace=False, remain_weight=False) +convert(self, model:layer, inplace=False, remain_weight=False): ''''''''' -将量化模型转换为ONNX格式。转换后的模型可以通过调用paddle.jit.save保存为推理模型。:参数模型::类型模型: Layer:原地操作参数: -:原理类型: bool, optional:剩余权重参数::类型剩余权重: bool, optional +将量化模型转换为ONNX格式。转换后的模型可以通过调用 paddle.jit.save 保存为推理模型。 + +**参数** + - **model**(Layer) - 待量化的模型。 + - **inplace**(bool, optional) - 是否要对模型进行就地修改,默认为false。 + - **remain_weight**(bool, optional) - 是否宝石权重为floats,默认为false。 **返回** 转换后的模型 From 127c2cf8cf3a8bdddce228cf0cc3a0a6bfd84f19 Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:29:17 +0800 Subject: [PATCH 07/10] Update docs/api/paddle/quantization/qat_cn.rst Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- docs/api/paddle/quantization/qat_cn.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst index a01b62d9a79..4618de9ac6d 100644 --- a/docs/api/paddle/quantization/qat_cn.rst +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -33,8 +33,6 @@ quantize(model: Layer, inplace=False) COPY-FROM: paddle.quantization.QAT.quantize -方法 -:::::::::::: convert(self, model:layer, inplace=False, remain_weight=False): ''''''''' From 0f60bbdeaf7c5e79eb440801351a355cdffe4ac8 Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:47:55 +0800 Subject: [PATCH 08/10] Update qat_cn.rst --- docs/api/paddle/quantization/qat_cn.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst index 4618de9ac6d..5491b06f71c 100644 --- a/docs/api/paddle/quantization/qat_cn.rst +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -44,3 +44,6 @@ convert(self, model:layer, inplace=False, remain_weight=False): - **remain_weight**(bool, optional) - 是否宝石权重为floats,默认为false。 **返回** 转换后的模型 + +**代码示例** +COPY-FROM: paddle.quantization.QAT.quantize From 6cd86f8954e7c665dd8f2c7195851cd324eb82a0 Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Wed, 17 Jan 2024 11:50:15 +0800 Subject: [PATCH 09/10] Update qat_cn.rst --- docs/api/paddle/quantization/qat_cn.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst index 5491b06f71c..34d07de05c6 100644 --- a/docs/api/paddle/quantization/qat_cn.rst +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -46,4 +46,4 @@ convert(self, model:layer, inplace=False, remain_weight=False): 转换后的模型 **代码示例** -COPY-FROM: paddle.quantization.QAT.quantize +COPY-FROM: paddle.quantization.QAT.convert From f6c119cc173985f3634e7a4c0cbed5fe7d03664e Mon Sep 17 00:00:00 2001 From: jiangziyan-693 <150317638+jiangziyan-693@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:33:44 +0800 Subject: [PATCH 10/10] Update qat_cn.rst --- docs/api/paddle/quantization/qat_cn.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/api/paddle/quantization/qat_cn.rst b/docs/api/paddle/quantization/qat_cn.rst index 34d07de05c6..642c6b834d8 100644 --- a/docs/api/paddle/quantization/qat_cn.rst +++ b/docs/api/paddle/quantization/qat_cn.rst @@ -23,10 +23,12 @@ quantize(model: Layer, inplace=False) 量化配置将在模型中传播。并且它将在模型中插入伪量化器以模拟量化过程。 **参数** + - **model(Layer)** - 待量化的模型 - **inplace(bool)** - 是否对模型进行原地修改 **返回** + 为量化感知训练准备好的模型。 **代码示例** @@ -39,11 +41,14 @@ convert(self, model:layer, inplace=False, remain_weight=False): 将量化模型转换为ONNX格式。转换后的模型可以通过调用 paddle.jit.save 保存为推理模型。 **参数** + - **model**(Layer) - 待量化的模型。 - **inplace**(bool, optional) - 是否要对模型进行就地修改,默认为false。 - **remain_weight**(bool, optional) - 是否宝石权重为floats,默认为false。 **返回** + 转换后的模型 **代码示例** + COPY-FROM: paddle.quantization.QAT.convert