TensorRT 10.8.0
|
网络定义中的量化层。 更多...
#include <NvInfer.h>
公共成员函数 | |
int32_t | getAxis () const noexcept |
获取量化轴。 更多... | |
void | setAxis (int32_t axis) noexcept |
设置量化轴。 更多... | |
void | setToType (DataType toType) noexcept |
设置量化层输出类型。 更多... | |
DataType | getToType () const noexcept |
返回量化层输出类型。 更多... | |
![]() | |
LayerType | getType () const noexcept |
返回层的类型。 更多... | |
void | setName (char const *name) noexcept |
设置层的名称。 更多... | |
char const * | getName () const noexcept |
返回层的名称。 更多... | |
int32_t | getNbInputs () const noexcept |
获取层的输入数量。 更多... | |
ITensor * | getInput (int32_t index) const noexcept |
获取与给定索引对应的层输入。 更多... | |
int32_t | getNbOutputs () const noexcept |
获取层的输出数量。 更多... | |
ITensor * | getOutput (int32_t index) const noexcept |
获取与给定索引对应的层输出。 更多... | |
void | setInput (int32_t index, ITensor &tensor) noexcept |
使用特定张量替换此层的输入。 更多... | |
void | setPrecision (DataType dataType) noexcept |
在弱类型网络中设置此层的首选或必需的计算精度。 更多... | |
DataType | getPrecision () const noexcept |
获取此层的计算精度 更多... | |
bool | precisionIsSet () const noexcept |
是否已为此层设置计算精度 更多... | |
void | resetPrecision () noexcept |
重置此层的计算精度 更多... | |
void | setOutputType (int32_t index, DataType dataType) noexcept |
在弱类型网络中设置此层的输出类型。 更多... | |
DataType | getOutputType (int32_t index) const noexcept |
获取此层的输出类型 更多... | |
bool | outputTypeIsSet (int32_t index) const noexcept |
是否已为此层设置输出类型 更多... | |
void | resetOutputType (int32_t index) noexcept |
重置此层的输出类型 更多... | |
void | setMetadata (char const *metadata) noexcept |
为此层设置元数据。 更多... | |
char const * | getMetadata () const noexcept |
获取层的元数据。 更多... | |
保护成员函数 | |
virtual | ~IQuantizeLayer () noexcept=default |
![]() | |
virtual | ~ILayer () noexcept=default |
![]() | |
INoCopy ()=default | |
virtual | ~INoCopy ()=default |
INoCopy (INoCopy const &other)=delete | |
INoCopy & | operator= (INoCopy const &other)=delete |
INoCopy (INoCopy &&other)=delete | |
INoCopy & | operator= (INoCopy &&other)=delete |
保护属性 | |
apiv::VQuantizeLayer * | mImpl |
![]() | |
apiv::VLayer * | mLayer |
网络定义中的量化层。
此层接受浮点数据输入张量,并使用 scale 和 zeroPt 输入根据以下公式量化数据:output
= clamp(round(input
/ scale
) + zeroPt
)
舍入类型为四舍五入到最接近的偶数 (https://en.wikipedia.org/wiki/Rounding#Round_half_to_even)。根据数据类型限制范围
第一个输入(索引 0)是要量化的张量。第二个(索引 1)和第三个(索引 2)分别是 scale 和零点。scale
和 zeroPt
应具有相同的维度,且秩小于或等于 2。
zeroPt
张量是可选的,如果未设置,则假定为零。其数据类型必须与输出数据类型匹配。zeroPt
必须仅包含零值系数,因为仅支持对称量化。对于逐张量量化,scale
值必须是标量;对于逐通道量化,scale
值必须是一维张量;对于块量化,scale
值必须与输入张量的秩相同(仅 DataType::kINT4 支持)。所有 scale
系数都必须具有正值。一维 scale
张量的大小必须与量化轴的大小匹配。对于块量化,scale
张量的形状必须与输入的形状匹配,但一个维度(最后一个或倒数第二个维度)除外,在该维度中发生分块。zeroPt
的大小必须与 scale
的大小匹配。
以 scale
张量终止的子图必须是构建时常量。相同的限制适用于 zeroPt
。输出类型(如果受约束)必须约束为 DataType::kINT8、DataType::kFP8 或 DataType::kINT4。输入类型(如果受约束)必须约束为 DataType::kFLOAT、DataType::kHALF 或 DataType::kBF16。输出大小与输入大小相同。量化轴是指输入张量的维度。
IQuantizeLayer 支持 DataType::kFLOAT、DataType::kHALF 或 DataType::kBF16 精度,并且在实例化期间将默认使用 DataType::kFLOAT 精度。对于强类型网络,input
数据类型必须与 scale
数据类型匹配。
IQuantizeLayer 支持 DataType::kINT8、DataType::kFP8 或 DataType::kINT4 输出。
作为此层操作的示例,假设一个 4D NCHW 激活输入,可以使用单个 scale 系数进行量化(称为逐张量量化):对于 N 中的每个 n:对于 C 中的每个 c:对于 H 中的每个 h:对于 W 中的每个 w:output[n,c,h,w] = clamp(round(input
[n,c,h,w] / scale
) + zeroPt
)
仅权重输入支持逐通道量化。因此,激活不能按通道量化。作为逐通道操作的示例,假设一个 4D KCRS 权重输入,K(维度 0)作为量化轴。scale 是一个系数数组,并且必须具有与量化轴相同的大小。对于 K 中的每个 k:对于 C 中的每个 c:对于 R 中的每个 r:对于 S 中的每个 s:output[k,c,r,s] = clamp(round(input
[k,c,r,s] / scale
[k]) + zeroPt
[k])
仅 DataType::kINT4 的权重输入支持块量化。作为分块操作的示例,假设一个 2D RS 权重输入,R(维度 0)作为分块轴,B 作为块大小。scale 是一个 2D 系数数组,维度为 (R//B, S)。对于 R 中的每个 r:对于 S 中的每个 s:output[r,s] = clamp(round(input
[r,s] / scale
[r//B, s]) + zeroPt
[r//B, s])
scale
和 zeroPt
子图是
|
protectedvirtualdefaultnoexcept |
|
inlinenoexcept |
获取量化轴。
|
inlinenoexcept |
返回量化层输出类型。
|
inlinenoexcept |
设置量化轴。
设置量化轴的索引(参考输入张量的维度)。如果 scale 张量具有多个系数,则轴必须是有效轴。轴值仅用于逐轴(逐通道)量化。
|
inlinenoexcept |
设置量化层输出类型。
toType | 输出张量的 DataType。 |
设置量化层的输出类型。有效值是 DataType::kINT8 和 DataType::kFP8。如果网络是强类型的,则必须使用 setToType 来设置输出类型,并且使用 setOutputType 是错误的。否则,传递给 setOutputType 和 setToType 的类型必须相同。
|
protected |
Copyright © 2024 NVIDIA Corporation
隐私政策 | 管理我的隐私 | 请勿出售或分享我的数据 | 服务条款 | 无障碍访问 | 公司政策 | 产品安全 | 联系方式