量化工具¶
參考實作方法¶
-
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,則函數執行通道wise量化;如果 1 < G < C,則函數執行群組wise量化;如果 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) 輸入轉換為 rowwise 量化輸出。bitrate 指定量化輸出中的位元數。Scale 和 Bias 為 fp16。每行的 Scale 和 Bias 都儲存在行尾 (融合)。
- 參數:
bit_rate – 可以是 2、4 或 8
AVX-2 實作方法¶
-
uint32_t Xor128(void)¶
基於此論文的 [0, 9] 範圍內的亂數產生器。
-
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 重新量化,且 bias 已融合。
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 重新量化。