量化实用程序¶
参考实现方法¶
-
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
。
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 重新量化。