Type conversion routines transform data from higher to lower precision data types or vice-versa. These conversion functions can be used by the ML driver implementations for quantization and de-quantization.
Added driver routines for type conversion. These driver routines invoke the architecture specific functions. Signed-off-by: Srikanth Yalavarthi <syalavar...@marvell.com> --- drivers/common/ml/ml_utils.c | 132 +++++++++++++++++++ drivers/common/ml/ml_utils.h | 233 ++++++++++++++++++++++++++++++++++ drivers/common/ml/version.map | 16 +++ 3 files changed, 381 insertions(+) diff --git a/drivers/common/ml/ml_utils.c b/drivers/common/ml/ml_utils.c index 45c1f76a54..553e906172 100644 --- a/drivers/common/ml/ml_utils.c +++ b/drivers/common/ml/ml_utils.c @@ -2,6 +2,10 @@ * Copyright (c) 2022 Marvell. */ +#include <errno.h> +#include <stdint.h> + +#include <rte_common.h> #include <rte_mldev.h> #include "ml_utils.h" @@ -108,3 +112,131 @@ ml_io_format_to_str(enum rte_ml_io_format format, char *str, int len) rte_strlcpy(str, "invalid", len); } } + +int +ml_float32_to_int8(float scale, uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(scale); + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_int8_to_float32(float scale, uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(scale); + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_float32_to_uint8(float scale, uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(scale); + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_uint8_to_float32(float scale, uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(scale); + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_float32_to_int16(float scale, uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(scale); + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_int16_to_float32(float scale, uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(scale); + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_float32_to_uint16(float scale, uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(scale); + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_uint16_to_float32(float scale, uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(scale); + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_float32_to_float16(uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_float16_to_float32(uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_float32_to_bfloat16(uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} + +int +ml_bfloat16_to_float32(uint64_t nb_elements, void *input, void *output) +{ + RTE_SET_USED(nb_elements); + RTE_SET_USED(input); + RTE_SET_USED(output); + + return -ENOTSUP; +} diff --git a/drivers/common/ml/ml_utils.h b/drivers/common/ml/ml_utils.h index b6adb98e04..9726c6e3b5 100644 --- a/drivers/common/ml/ml_utils.h +++ b/drivers/common/ml/ml_utils.h @@ -47,4 +47,237 @@ void ml_io_type_to_str(enum rte_ml_io_type type, char *str, int len); __rte_internal void ml_io_format_to_str(enum rte_ml_io_format format, char *str, int len); +/** + * Convert a buffer containing numbers in single precision floating format (float32) to signed 8-bit + * integer format (INT8). + * + * @param[in] scale + * Scale factor for conversion. + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * @param[out] output + * Output buffer to store INT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_float32_to_int8(float scale, uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in signed 8-bit integer format (INT8) to single precision + * floating format (float32). + * + * @param[in] scale + * Scale factor for conversion. + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing INT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. + * @param[out] output + * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_int8_to_float32(float scale, uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in single precision floating format (float32) to unsigned + * 8-bit integer format (UINT8). + * + * @param[in] scale + * Scale factor for conversion. + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * @param[out] output + * Output buffer to store UINT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_float32_to_uint8(float scale, uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in unsigned 8-bit integer format (UINT8) to single precision + * floating format (float32). + * + * @param[in] scale + * Scale factor for conversion. + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing UINT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. + * @param[out] output + * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_uint8_to_float32(float scale, uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in single precision floating format (float32) to signed + * 16-bit integer format (INT16). + * + * @param[in] scale + * Scale factor for conversion. + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * @param[out] output + * Output buffer to store INT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_float32_to_int16(float scale, uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in signed 16-bit integer format (INT16) to single precision + * floating format (float32). + * + * @param[in] scale + * Scale factor for conversion. + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing INT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. + * @param[out] output + * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_int16_to_float32(float scale, uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in single precision floating format (float32) to unsigned + * 16-bit integer format (UINT16). + * + * @param[in] scale + * Scale factor for conversion. + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * @param[out] output + * Output buffer to store UINT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_float32_to_uint16(float scale, uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in unsigned 16-bit integer format (UINT16) to single + * precision floating format (float32). + * + * @param[in] scale + * Scale factor for conversion. + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing UINT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. + * @param[out] output + * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_uint16_to_float32(float scale, uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in single precision floating format (float32) to half + * precision floating point format (FP16). + * + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements *4) bytes. + * @param[out] output + * Output buffer to store float16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_float32_to_float16(uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in half precision floating format (FP16) to single precision + * floating point format (float32). + * + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing float16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. + * @param[out] output + * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_float16_to_float32(uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in single precision floating format (float32) to brain + * floating point format (bfloat16). + * + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements *4) bytes. + * @param[out] output + * Output buffer to store bfloat16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_float32_to_bfloat16(uint64_t nb_elements, void *input, void *output); + +/** + * Convert a buffer containing numbers in brain floating point format (bfloat16) to single precision + * floating point format (float32). + * + * @param[in] nb_elements + * Number of elements in the buffer. + * @param[in] input + * Input buffer containing bfloat16 numbers. Size of buffer is equal to (nb_elements * 2) + * bytes. + * @param[out] output + * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. + * + * @return + * - 0, Success. + * - < 0, Error code on failure. + */ +__rte_internal +int ml_bfloat16_to_float32(uint64_t nb_elements, void *input, void *output); + #endif /*_ML_UTILS_H_ */ diff --git a/drivers/common/ml/version.map b/drivers/common/ml/version.map index 7e33755f2f..35f270f637 100644 --- a/drivers/common/ml/version.map +++ b/drivers/common/ml/version.map @@ -5,5 +5,21 @@ INTERNAL { ml_io_type_to_str; ml_io_format_to_str; + ml_float32_to_int8; + ml_int8_to_float32; + ml_float32_to_uint8; + ml_uint8_to_float32; + + ml_float32_to_int16; + ml_int16_to_float32; + ml_float32_to_uint16; + ml_uint16_to_float32; + + ml_float32_to_float16; + ml_float16_to_float32; + + ml_float32_to_bfloat16; + ml_bfloat16_to_float32; + local: *; }; -- 2.17.1