量化工具¶
参考实现方法¶
-
template<typename T, layout_t LAYOUT = layout_t::KCX>
void QuantizeGroupwise(const float *src, int K, int C, int X, int G, const float *scales, const std::int32_t *zero_points, T *dst)¶ 将
src
中的浮点数据量化为T
类型。- 模板参数:
T – 输出量化数据类型(支持
int8_t
、uint8_t
和int32_t
)LAYOUT –
src
中输入张量的布局。(支持KCX
和KXC
)。KCX
对应于KCRS
或KCTRS
(对于带时间维度的权重张量)。KXC
对应于KRSC
或KTRSC
(对于带时间维度的权重张量)。
- 参数:
K – 权重张量的输出通道数
C – 通道数
X –
R*S
或T*R*S
G – 组数(如果
G == C
,函数执行逐通道量化;如果1 < G < C
,函数执行逐组量化;如果G == 1
,函数执行逐张量量化;)scales – 浮点缩放因子。大小应等于
G
。zero_points – 零点(应可在
T
类型中表示)。大小应等于G
。
-
template<typename T>
void FusedQuantizeDequantize(const float *src, float *dst, std::int64_t len, const TensorQuantizationParams &qparams, int thread_id = 0, int num_threads = 1, float noise_ratio = 0.0f)¶ 用于加速量化感知训练的融合整数量化反量化核。使用提供的 qparams 将 src 中的
fp32
值量化为(u)int8
,并将量化后的整数值反量化回fp32
。
-
template<typename InputType>
void FloatOrHalfToFusedNBitRowwiseQuantizedSBHalf(int bit_rate, const InputType *input, size_t input_rows, int input_columns, std::uint8_t *output)¶ 将浮点(fp32 或 fp16)输入转换为逐行量化输出。bitrate 指定量化输出的位数。缩放和偏差(Scale and Bias)采用 fp16 格式。每行的缩放和偏差存储在该行末尾(融合存储)。
- 参数:
bit_rate – 可为 2、4 或 8
AVX-2 实现方法¶
-
void FindMinMax(const float *m, float *min, float *max, int64_t len)¶
查找浮点矩阵中的最小值和最大值。
-
template<bool A_SYMMETRIC, bool B_SYMMETRIC, QuantizationGranularity Q_GRAN, bool HAS_BIAS, bool FUSE_RELU, typename BIAS_TYPE = std::int32_t, bool DIRECT = false>
void requantizeOutputProcessingAvx2(std::uint8_t *out, const std::int32_t *inp, const block_type_t &block, int ld_out, int ld_in, const requantizationParams_t<BIAS_TYPE> &r)¶ 使用 avx2 进行再量化,并融合偏差。
AVX-512 实现方法¶
-
template<bool A_SYMMETRIC, bool B_SYMMETRIC, QuantizationGranularity Q_GRAN, bool HAS_BIAS, bool FUSE_RELU, int C_PER_G, typename BIAS_TYPE = std::int32_t>
void requantizeOutputProcessingGConvAvx512(std::uint8_t *out, const std::int32_t *inp, const block_type_t &block, int ld_out, int ld_in, const requantizationParams_t<BIAS_TYPE> &r)¶ 使用 AVX512 进行再量化。