pnna/include/quantize.h

193 lines
5.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author: xun
* @Date: 2024-05-08 09:48:30
* @LastEditors: xun
* @LastEditTime: 2025-02-18 11:20:17
* @Description:
* Copyright (c) 2024 by CCYH, All Rights Reserved.
*/
#ifndef _QUANTIZE_H_
#define _QUANTIZE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define BITS_PER_BYTE 8
/**
* @brief 根据数据类型确定该数据类型所占的字节数,并返回该字节数值
*
* @param IN type 数据类型
* @return unsigned int 字节数值
*/
unsigned int type_get_bytes(const int type);
/**
* @brief 根据数据类型确定该数据类型所占的字节数,并返回该字节数值
*
* @param IN data_format 数据类型
* @return int 字节数值
*/
int get_stride(int data_format);
/**
* @brief 根据数据类型确定该数据类型所占的bit数并返回该bits数值
*
* @param IN type 数据类型
* @return unsigned int bits数值
*/
unsigned int type_get_bits(const int type);
/**
* @brief 根据维度和每个维度的大小,算出所有数据的个数
*
* @param IN sizes 每个维度的大小
* @param IN num_of_dims 维度
* @return unsigned int 所有数据的个数
*/
unsigned int get_element_num(unsigned int *sizes, unsigned int num_of_dims);
/**
* @brief 根据维度和每个维度的大小,算出所有数据的个数
*
* @param IN sizes 每个维度的大小
* @param IN num_of_dims 维度
* @return unsigned int 所有数据的个数
*/
int compare_values(unsigned char *a, unsigned char *b, int obj_index, const int data_format);
/**
* @brief 单个数据的反量化操作
*
* @param IN src 待反量化的数
* @param OUT dst 反量化后的数
* @param IN data_format 数据类型
* @param IN quant_format 量化类型
* @param IN fixed_point_pos 固定小数点位置
* @param IN scale 缩放因子
* @param IN zeroPoint 零点
* @return int 处理后的状态码0表示成功其他表示失败
*/
int dtype_to_float32(unsigned char *src, float *dst, const int data_format,\
const int quant_format, const signed char fixed_point_pos, \
const float scale, const int zeroPoint);
/**
* @brief 单个数据的量化操作
*
* @param IN src 待量化的数
* @param OUT dst 量化后的数
* @param IN data_format 数据类型
* @param IN quant_format 量化类型
* @param IN fixed_point_pos 固定小数点位置
* @param IN scale 缩放因子
* @param IN zeroPoint 零点
* @return int 处理后的状态码0表示成功其他表示失败
*/
int float32_to_dtype(float src, unsigned char *dst, const int data_format,\
const int quant_format, const signed char fl, const float scale, const int zerop);
/**
* @brief 批量数据的仿射量化操作
*
* @param IN input 待量化的数据
* @param IN size 待量化数据的大小
* @param IN stride 数据类型所占的字节数
* @param IN scale 缩放因子
* @param IN zeroPoint 零点
* @param IN data_type 数据类型
* @param OUT output 量化后的数据
* @return
*/
void asym_affine(float *input, const int size, const int stride, \
const float scale, const int zeropoint, int data_type, uint8_t *output);
/**
* @brief 批量数据的定点浮点量化操作
*
* @param IN input 待量化的数据
* @param IN size 待量化数据的大小
* @param IN stride 数据类型所占的字节数
* @param IN fl 固定小数点位置
* @param IN data_type 数据类型
* @param OUT output 量化后的数据
* @return
*/
void dynamic_fix_point(float *input, const int size, const int stride, \
const int fl, int data_type, uint8_t *output);
/**
* @brief 批量数据的仿射反量化操作
*
* @param IN input 待反量化的数据
* @param IN size 待反量化数据的大小
* @param IN scale 缩放因子
* @param IN zeroPoint 零点
* @param IN data_type 数据类型
* @param OUT output 反量化后的数据
* @return
*/
void asym_affine_inverse(void *input, const int size,\
const float scale, const int zeropoint, int data_type, float *output);
/**
* @brief 批量数据的定点浮点反量化操作
*
* @param IN input 待反量化的数据
* @param IN size 待反量化数据的大小
* @param IN fl 固定小数点位置
* @param IN data_type 数据类型
* @param OUT output 反量化后的数据
* @return
*/
void dynamic_fix_point_inverse(void *input, const int size,\
const int fl, int data_type, float *output);
/**
* @brief 批量数据的单精度浮点数到半精度浮点数的量化操作
*
* @param IN input 待反量化的数据
* @param IN size 待反量化数据的大小
* @param IN stride 待反量化数据的步长
* @param OUT output 反量化后的数据
* @return
*/
void convert_float_to_fp16(float *input, const int size,\
const int stride, uint8_t *output);
/**
* @brief 批量数据的半精度浮点数到单精度浮点数的反量化操作
*
* @param IN input 待反量化的数据
* @param IN size 待反量化数据的大小
* @param OUT output 反量化后的数据
* @return
*/
void convert_fp16_to_float(short *input, const int size,\
float *output);
#ifdef __cplusplus
}
#endif
#endif // !_QUANTIZE_H_