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

[Docathon][Add CN Doc No.20-22] #6371

Merged
merged 11 commits into from
Jan 10, 2024
13 changes: 13 additions & 0 deletions docs/api/paddle/nn/Overview_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ paddle.nn 目录下包含飞桨框架支持的神经网络层和相关函数的
- :ref:`损失函数 <loss_functional>`
- :ref:`公用方法 <common_functional>`
- :ref:`初始化相关 <about_initializer>`
- :ref:`量化压缩 <about_quantization>`



Expand Down Expand Up @@ -552,3 +553,15 @@ Embedding 相关函数
" :ref:`paddle.nn.initializer.Uniform <cn_api_paddle_nn_initializer_Uniform>` ", "随机均匀分布初始化函数"
" :ref:`paddle.nn.initializer.XavierNormal <cn_api_paddle_nn_initializer_XavierNormal>` ", "实现 Xavier 权重初始化方法( Xavier weight initializer)"
" :ref:`paddle.nn.initializer.XavierUniform <cn_api_paddle_nn_initializer_XavierUniform>` ", "实现 Xavier 权重初始化方法( Xavier weight initializer)"

.. _about_quantization:

量化压缩
:::::::::::::::::::::::

.. csv-table::
:header: "API 名称", "API 功能"

" :ref:`paddle.nn.quant.llm_int8_linear <cn_api_paddle_nn_quant_llm_int8_linear>` ", "使用 int8 量化压缩的线性层"
" :ref:`paddle.nn.quant.weight_only_linear <cn_api_paddle_nn_quant_weight_only_linear>` ", "使用自定义的类型进行模型的量化压缩"
" :ref:`paddle.nn.quant.weight_quantize <cn_api_paddle_nn_quant_weight_quantize>` ", "weight_only 和 llm.int8 权重的量化函数"
33 changes: 33 additions & 0 deletions docs/api/paddle/nn/quant/llm_int8_linear_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.. _cn_api_paddle_nn_quant_llm_int8_linear:

llm_int8_linear
-------------------------------

.. py:function:: paddle.nn.quant.llm_int8_linear(x, weight, bias=None, weight_scale=None, threshold=6.0)

应用两个张量的矩阵乘法。若提供了偏置,则进行偏置加法。
zade23 marked this conversation as resolved.
Show resolved Hide resolved

细节可参考论文 `LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale <https://arxiv.org/abs/2208.07339>`_ 。

此方法要求 CUDA 版本不低于 11.2。

参数
::::::::::::
- **x** (Tensor) - 第一个输入张量,将被乘以,数据类型为 float16 或 bfloat16。
- **weight** (Tensor) - 第二个输入张量,将被乘以。其秩必须为 2。
- **bias** (Tensor|None) - 输入的偏置张量。如果为 None,则不执行偏置加法。否则,偏置将被加到矩阵乘法结果上。
- **weight_scale** (Tensor|None) - 提供给权重的输入比例张量,用于反量化。其秩必须为 1。
- **threshold** (float) - 激活中离群值的最小值,离群值的通道将应用与 x.dtype 的乘法。

返回
::::::::::::
- Tensor:输出张量,其数据类型与 x 相同。
zade23 marked this conversation as resolved.
Show resolved Hide resolved

返回类型
::::::::::::
Tensor

代码示例:
::::::::::

COPY-FROM: paddle.nn.quant.llm_int8_linear
33 changes: 33 additions & 0 deletions docs/api/paddle/nn/quant/weight_only_linear_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.. _cn_api_paddle_nn_quant_weight_only_linear:

weight_only_linear
-------------------------------

.. py:function:: paddle.nn.quant.weight_only_linear(x, weight, bias=None, weight_scale=None, weight_dtype='int8', arch=None)

应用两个张量的矩阵乘法。若提供了偏置,则进行偏置加法。

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.

@vivienfanghuagood 开发者您好,关于等价公式是否有相关论文或者相似功能API文档的参考呢?

Choose a reason for hiding this comment

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

我找了下,暂时没有找到特别官方的论文和相似的API,也可以先空着,待后续再补充~


此方法要求 CUDA 版本不低于 11.2。

参数
::::::::::::
- **x** (Tensor) - 第一个输入张量,将被乘以,数据类型为 float16 或 bfloat16。
- **weight** (Tensor) - 第二个输入张量,将被乘以。其秩必须为 2。
- **bias** (Tensor|None) - 输入的偏置张量。如果为 None,则不执行偏置加法。否则,偏置将被加到矩阵乘法结果上。
- **weight_scale** (Tensor|None) - 提供给权重的输入比例张量,用于反量化。其秩必须为 1。
- **weight_dtype** (str) - 权重张量的数据类型,必须是 'int8', 'int4' 之一,默认为 'int8'。
- **arch** (int) - 针对目标设备的计算架构。例如,A100 为 80,v100 为 70,如果您没有指定架构,我们将从您的设备获取架构,默认为 None。

返回
::::::::::::
- Tensor:输出张量,其数据类型与 x 相同。
zade23 marked this conversation as resolved.
Show resolved Hide resolved


返回类型
::::::::::::
Tensor

代码示例:
::::::::::

COPY-FROM: paddle.nn.quant.weight_only_linear
27 changes: 27 additions & 0 deletions docs/api/paddle/nn/quant/weight_quantize_cn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.. _cn_api_paddle_nn_quant_weight_quantize:

weight_quantize
-------------------------------
.. py:function:: paddle.nn.quant.weight_quantize(x, algo='weight_only_int8', arch=None)

weight_only 和 llm.int8 权重的量化函数。

参数
::::::::::::
- **x** (Tensor) - 待量化的输入张量,数据类型为 float16 或 bfloat16。
- **algo** (str) - 应用于 x 的算法,必须是 '`weight_only_int8`'、'`weight_only_int4`' 和 '`llm.int8`' 中的一个,默认为 '`weight_only_int8`'。
- **arch** (int) - 针对目标设备的计算架构。例如,A100 为 80,v100 为 70,如果您没有指定架构,我们将从您的设备获取架构,默认为 None。

返回
::::::::::::
- **out** (Tensor) - 量化结果的张量,数据类型为 int8,形状为 x 的转置。
- **scale** (Tensor) - 每个通道的比例张量,数据类型为 float32。

返回类型
::::::::::::
Tensor

代码示例:
::::::::::

COPY-FROM: paddle.nn.quant.weight_quantize