1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) 2022 Marvell. 3 */ 4 5 #ifndef RTE_MLDEV_UTILS_H 6 #define RTE_MLDEV_UTILS_H 7 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 /** 13 * @file 14 * 15 * ML Device PMD utility API 16 * 17 * These APIs for the use from ML drivers, user applications shouldn't use them. 18 */ 19 20 #include <rte_compat.h> 21 #include <rte_mldev.h> 22 23 /** 24 * @internal 25 * 26 * Get the size an ML IO type in bytes. 27 * 28 * @param[in] type 29 * Enumeration of ML IO data type. 30 * 31 * @return 32 * - > 0, Size of the data type in bytes. 33 * - < 0, Error code on failure. 34 */ 35 __rte_internal 36 int 37 rte_ml_io_type_size_get(enum rte_ml_io_type type); 38 39 /** 40 * @internal 41 * 42 * Get the name of an ML IO type. 43 * 44 * @param[in] type 45 * Enumeration of ML IO data type. 46 * @param[in] str 47 * Address of character array. 48 * @param[in] len 49 * Length of character array. 50 */ 51 __rte_internal 52 void 53 rte_ml_io_type_to_str(enum rte_ml_io_type type, char *str, int len); 54 55 /** 56 * @internal 57 * 58 * Convert a buffer containing numbers in single precision floating format (float32) to signed 8-bit 59 * integer format (INT8). 60 * 61 * @param[in] scale 62 * Scale factor for conversion. 63 * @param[in] nb_elements 64 * Number of elements in the buffer. 65 * @param[in] input 66 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 67 * @param[out] output 68 * Output buffer to store INT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 69 * 70 * @return 71 * - 0, Success. 72 * - < 0, Error code on failure. 73 */ 74 __rte_internal 75 int 76 rte_ml_io_float32_to_int8(float scale, uint64_t nb_elements, void *input, void *output); 77 78 /** 79 * @internal 80 * 81 * Convert a buffer containing numbers in signed 8-bit integer format (INT8) to single precision 82 * floating format (float32). 83 * 84 * @param[in] scale 85 * Scale factor for conversion. 86 * @param[in] nb_elements 87 * Number of elements in the buffer. 88 * @param[in] input 89 * Input buffer containing INT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 90 * @param[out] output 91 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 92 * 93 * @return 94 * - 0, Success. 95 * - < 0, Error code on failure. 96 */ 97 __rte_internal 98 int 99 rte_ml_io_int8_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 100 101 /** 102 * @internal 103 * 104 * Convert a buffer containing numbers in single precision floating format (float32) to unsigned 105 * 8-bit integer format (UINT8). 106 * 107 * @param[in] scale 108 * Scale factor for conversion. 109 * @param[in] nb_elements 110 * Number of elements in the buffer. 111 * @param[in] input 112 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 113 * @param[out] output 114 * Output buffer to store UINT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 115 * 116 * @return 117 * - 0, Success. 118 * - < 0, Error code on failure. 119 */ 120 __rte_internal 121 int 122 rte_ml_io_float32_to_uint8(float scale, uint64_t nb_elements, void *input, void *output); 123 124 /** 125 * @internal 126 * 127 * Convert a buffer containing numbers in unsigned 8-bit integer format (UINT8) to single precision 128 * floating format (float32). 129 * 130 * @param[in] scale 131 * Scale factor for conversion. 132 * @param[in] nb_elements 133 * Number of elements in the buffer. 134 * @param[in] input 135 * Input buffer containing UINT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 136 * @param[out] output 137 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 138 * 139 * @return 140 * - 0, Success. 141 * - < 0, Error code on failure. 142 */ 143 __rte_internal 144 int 145 rte_ml_io_uint8_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 146 147 /** 148 * @internal 149 * 150 * Convert a buffer containing numbers in single precision floating format (float32) to signed 151 * 16-bit integer format (INT16). 152 * 153 * @param[in] scale 154 * Scale factor for conversion. 155 * @param[in] nb_elements 156 * Number of elements in the buffer. 157 * @param[in] input 158 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 159 * @param[out] output 160 * Output buffer to store INT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 161 * 162 * @return 163 * - 0, Success. 164 * - < 0, Error code on failure. 165 */ 166 __rte_internal 167 int 168 rte_ml_io_float32_to_int16(float scale, uint64_t nb_elements, void *input, void *output); 169 170 /** 171 * @internal 172 * 173 * Convert a buffer containing numbers in signed 16-bit integer format (INT16) to single precision 174 * floating format (float32). 175 * 176 * @param[in] scale 177 * Scale factor for conversion. 178 * @param[in] nb_elements 179 * Number of elements in the buffer. 180 * @param[in] input 181 * Input buffer containing INT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 182 * @param[out] output 183 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 184 * 185 * @return 186 * - 0, Success. 187 * - < 0, Error code on failure. 188 */ 189 __rte_internal 190 int 191 rte_ml_io_int16_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 192 193 /** 194 * @internal 195 * 196 * Convert a buffer containing numbers in single precision floating format (float32) to unsigned 197 * 16-bit integer format (UINT16). 198 * 199 * @param[in] scale 200 * Scale factor for conversion. 201 * @param[in] nb_elements 202 * Number of elements in the buffer. 203 * @param[in] input 204 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 205 * @param[out] output 206 * Output buffer to store UINT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 207 * 208 * @return 209 * - 0, Success. 210 * - < 0, Error code on failure. 211 */ 212 __rte_internal 213 int 214 rte_ml_io_float32_to_uint16(float scale, uint64_t nb_elements, void *input, void *output); 215 216 /** 217 * @internal 218 * 219 * Convert a buffer containing numbers in unsigned 16-bit integer format (UINT16) to single 220 * precision floating format (float32). 221 * 222 * @param[in] scale 223 * Scale factor for conversion. 224 * @param[in] nb_elements 225 * Number of elements in the buffer. 226 * @param[in] input 227 * Input buffer containing UINT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 228 * @param[out] output 229 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 230 * 231 * @return 232 * - 0, Success. 233 * - < 0, Error code on failure. 234 */ 235 __rte_internal 236 int 237 rte_ml_io_uint16_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 238 239 /** 240 * @internal 241 * 242 * Convert a buffer containing numbers in single precision floating format (float32) to half 243 * precision floating point format (FP16). 244 * 245 * @param[in] nb_elements 246 * Number of elements in the buffer. 247 * @param[in] input 248 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements *4) bytes. 249 * @param[out] output 250 * Output buffer to store float16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 251 * 252 * @return 253 * - 0, Success. 254 * - < 0, Error code on failure. 255 */ 256 __rte_internal 257 int 258 rte_ml_io_float32_to_float16(uint64_t nb_elements, void *input, void *output); 259 260 /** 261 * @internal 262 * 263 * Convert a buffer containing numbers in half precision floating format (FP16) to single precision 264 * floating point format (float32). 265 * 266 * @param[in] nb_elements 267 * Number of elements in the buffer. 268 * @param[in] input 269 * Input buffer containing float16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 270 * @param[out] output 271 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 272 * 273 * @return 274 * - 0, Success. 275 * - < 0, Error code on failure. 276 */ 277 __rte_internal 278 int 279 rte_ml_io_float16_to_float32(uint64_t nb_elements, void *input, void *output); 280 281 /** 282 * @internal 283 * 284 * Convert a buffer containing numbers in single precision floating format (float32) to brain 285 * floating point format (bfloat16). 286 * 287 * @param[in] nb_elements 288 * Number of elements in the buffer. 289 * @param[in] input 290 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements *4) bytes. 291 * @param[out] output 292 * Output buffer to store bfloat16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 293 * 294 * @return 295 * - 0, Success. 296 * - < 0, Error code on failure. 297 */ 298 __rte_internal 299 int 300 rte_ml_io_float32_to_bfloat16(uint64_t nb_elements, void *input, void *output); 301 302 /** 303 * @internal 304 * 305 * Convert a buffer containing numbers in brain floating point format (bfloat16) to single precision 306 * floating point format (float32). 307 * 308 * @param[in] nb_elements 309 * Number of elements in the buffer. 310 * @param[in] input 311 * Input buffer containing bfloat16 numbers. Size of buffer is equal to (nb_elements * 2) 312 * bytes. 313 * @param[out] output 314 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 315 * 316 * @return 317 * - 0, Success. 318 * - < 0, Error code on failure. 319 */ 320 __rte_internal 321 int 322 rte_ml_io_bfloat16_to_float32(uint64_t nb_elements, void *input, void *output); 323 324 #ifdef __cplusplus 325 } 326 #endif 327 328 #endif /* RTE_MLDEV_UTILS_H */ 329