量化工具¶
参考实现方法¶
-
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*SG – 分组数(如果
G == C,则函数执行通道级量化;如果1 < G < C,则函数执行分组级量化;如果G == 1,则函数执行每张量量化;)scales – 浮点尺度。大小应等于
Gzero_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, const InputType *rowwise_min_max = nullptr)¶ 将浮点(fp32 或 fp16)输入转换为逐行量化的输出。bit_rate 指定量化输出中的位数。Scale 和 Bias 为 fp16。每行的 Scale 和 Bias 都存储在行本身(融合)的末尾。
- 参数:
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 进行反量化。