arm_nnsupportfunctions.h 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2010-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /* ----------------------------------------------------------------------
  19. * Project: CMSIS NN Library
  20. * Title: arm_nnsupportfunctions.h
  21. * Description: Public header file of support functions for CMSIS NN Library
  22. *
  23. * $Date: 14 February 2024
  24. * $Revision: V.19.0.0
  25. *
  26. * Target : Arm(R) M-Profile Architecture
  27. * -------------------------------------------------------------------- */
  28. #ifndef ARM_NNSUPPORTFUNCTIONS_H
  29. #define ARM_NNSUPPORTFUNCTIONS_H
  30. #include "Internal/arm_nn_compiler.h"
  31. #include "arm_nn_math_types.h"
  32. #include "arm_nn_types.h"
  33. #include <stdbool.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #define USE_FAST_DW_CONV_S16_FUNCTION(dw_conv_params, filter_dims, input_dims) \
  38. (dw_conv_params->ch_mult == 1 && dw_conv_params->dilation.w == 1 && dw_conv_params->dilation.h == 1 && \
  39. filter_dims->w * filter_dims->h < 512)
  40. #define LEFT_SHIFT(_shift) (_shift > 0 ? _shift : 0)
  41. #define RIGHT_SHIFT(_shift) (_shift > 0 ? 0 : -_shift)
  42. #define MASK_IF_ZERO(x) (x) == 0 ? ~0 : 0
  43. #define MASK_IF_NON_ZERO(x) (x) != 0 ? ~0 : 0
  44. #define SELECT_USING_MASK(mask, a, b) ((mask) & (a)) ^ (~(mask) & (b))
  45. #define MAX(A, B) ((A) > (B) ? (A) : (B))
  46. #define MIN(A, B) ((A) < (B) ? (A) : (B))
  47. #define CLAMP(x, h, l) MAX(MIN((x), (h)), (l))
  48. #define REDUCE_MULTIPLIER(_mult) ((_mult < 0x7FFF0000) ? ((_mult + (1 << 15)) >> 16) : 0x7FFF)
  49. // Number of channels processed in a block for DW Conv(MVE)
  50. // Requirement: Greater than 0 & less than 128
  51. // This can be fine tuned to match number of input channels for best performance.
  52. // A layer with lower number of channels than CH_IN_BLOCK_MVE will result in higher
  53. // scratch buffer usage and a layer with higher number of channels than CH_IN_BLOCK_MVE
  54. // will result in lower scratch buffer usage.
  55. #define CH_IN_BLOCK_MVE (124)
  56. /**
  57. * @brief definition to pack four 8 bit values.
  58. */
  59. #define PACK_S8x4_32x1(v0, v1, v2, v3) \
  60. ((((int32_t)(v0) << 0) & (int32_t)0x000000FF) | (((int32_t)(v1) << 8) & (int32_t)0x0000FF00) | \
  61. (((int32_t)(v2) << 16) & (int32_t)0x00FF0000) | (((int32_t)(v3) << 24) & (int32_t)0xFF000000))
  62. /**
  63. * @brief definition to pack two 16 bit values.
  64. */
  65. #define PACK_Q15x2_32x1(v0, v1) (((int32_t)v0 & (int32_t)0xFFFF) | ((int32_t)v1 << 16))
  66. /**
  67. * @defgroup groupSupport Private
  68. *
  69. * Internal Support functions. Not intended to be called direclty by a CMSIS-NN user.
  70. *
  71. */
  72. /**
  73. * @defgroup genPrivTypes Structure Types
  74. * @ingroup groupSupport
  75. * @brief Data structure types used by private functions.
  76. * @{
  77. */
  78. /**
  79. * @brief Union for SIMD access of q31/s16/s8 types
  80. */
  81. union arm_nnword
  82. {
  83. int32_t word;
  84. /**< q31 type */
  85. int16_t half_words[2];
  86. /**< s16 type */
  87. int8_t bytes[4];
  88. /**< s8 type */
  89. };
  90. /**
  91. * @brief Union for data type long long
  92. */
  93. struct arm_nn_double
  94. {
  95. uint32_t low;
  96. int32_t high;
  97. };
  98. union arm_nn_long_long
  99. {
  100. int64_t long_long;
  101. struct arm_nn_double word;
  102. };
  103. /**
  104. * @} // end group groupPrivTypes
  105. */
  106. /**
  107. * @defgroup supportConversion Data Conversion
  108. *
  109. * Perform data type conversion in-between neural network operations
  110. *
  111. */
  112. /**
  113. * @brief Converts the elements from a s8 vector to a s16 vector with an added offset
  114. * @param[in] src pointer to the s8 input vector
  115. * @param[out] dst pointer to the s16 output vector
  116. * @param[in] block_size length of the input vector
  117. * @param[in] offset s16 offset to be added to each input vector element.
  118. *
  119. * \par Description:
  120. *
  121. * Output elements are ordered.
  122. * The equation used for the conversion process is:
  123. *
  124. * <pre>
  125. * dst[n] = (int16_t) src[n] + offset; 0 <= n < block_size.
  126. * </pre>
  127. *
  128. */
  129. void arm_q7_to_q15_with_offset(const int8_t *src, int16_t *dst, int32_t block_size, int16_t offset);
  130. #if defined(ARM_MATH_DSP)
  131. /**
  132. * @brief Converts the elements from a s8 vector to a s16 vector with an added offset
  133. * @param[in] src pointer to the s8 input vector
  134. * @param[out] dst pointer to the s16 output vector
  135. * @param[in] block_size length of the input vector
  136. * @param[in] offset s16 offset to be added to each input vector element.
  137. *
  138. * \par Description:
  139. *
  140. * No additonal ordering is done with the result that output elements are not in order.
  141. * Instead of ABCD order will be ACBD.
  142. * Note this is for processors with DSP extension only.
  143. * The equation used for the conversion process is:
  144. *
  145. * <pre>
  146. * dst[n - 0] = (int16_t) src[n - 0] + offset; 0 <= n < block_size.
  147. * dst[n - 1] = (int16_t) src[n - 2] + offset; 0 <= n < block_size.
  148. * dst[n - 2] = (int16_t) src[n - 1] + offset; 0 <= n < block_size.
  149. * dst[n - 3] = (int16_t) src[n - 3] + offset; 0 <= n < block_size.
  150. * </pre>
  151. *
  152. */
  153. void arm_s8_to_s16_unordered_with_offset(const int8_t *src, int16_t *dst, int32_t block_size, int16_t offset);
  154. #endif
  155. /**
  156. * @brief Get the required buffer size for optimized s8 depthwise convolution
  157. * function with constraint that in_channel equals out_channel.
  158. * This is for processors with DSP extension.
  159. * Refer to arm_depthwise_conv_s8_opt_get_buffer_size() for function argument details.
  160. *
  161. * @note Intended for compilation on Host. If compiling for an Arm target, use
  162. * arm_depthwise_conv_s8_opt_get_buffer_size(). Note also this is a support function,
  163. * so not recommended to call directly even on Host.
  164. *
  165. */
  166. int32_t arm_depthwise_conv_s8_opt_get_buffer_size_dsp(const cmsis_nn_dims *input_dims,
  167. const cmsis_nn_dims *filter_dims);
  168. /**
  169. * @brief Depthwise conv on an im2col buffer where the input channel equals output channel.
  170. * @param[in] row pointer to row
  171. * @param[in] col pointer to im2col buffer, always consists of 2 columns.
  172. * @param[in] num_ch number of channels
  173. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  174. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  175. * @param[in] out_offset output tensor offset.
  176. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  177. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  178. * @param[in] kernel_size number of elements in one column.
  179. * @param[in] output_bias per output channel bias. Range : int32
  180. * @param[out] out pointer to output
  181. * @return The function returns one of the two
  182. * 1. The incremented output pointer for a successful operation or
  183. * 2. NULL if implementation is not available.
  184. *
  185. * @details Supported framework: TensorFlow Lite micro.
  186. */
  187. int8_t *arm_nn_depthwise_conv_s8_core(const int8_t *row,
  188. const int16_t *col,
  189. const uint16_t num_ch,
  190. const int32_t *out_shift,
  191. const int32_t *out_mult,
  192. const int32_t out_offset,
  193. const int32_t activation_min,
  194. const int32_t activation_max,
  195. const uint16_t kernel_size,
  196. const int32_t *const output_bias,
  197. int8_t *out);
  198. /**
  199. * @brief General Matrix-multiplication function with per-channel requantization.
  200. * @param[in] input_row pointer to row operand
  201. * @param[in] input_col pointer to col operand
  202. * @param[in] output_ch number of rows of input_row
  203. * @param[in] col_batches number of column batches. Range: 1 to 4
  204. * @param[in] output_shift pointer to per output channel requantization shift parameter.
  205. * @param[in] output_mult pointer to per output channel requantization multiplier parameter.
  206. * @param[in] out_offset output tensor offset.
  207. * @param[in] col_offset input tensor(col) offset.
  208. * @param[in] row_offset kernel offset(row). Not used.
  209. * @param[in] out_activation_min minimum value to clamp the output to. Range : int8
  210. * @param[in] out_activation_max maximum value to clamp the output to. Range : int8
  211. * @param[in] row_len number of elements in each row
  212. * @param[in] bias per output channel bias. Range : int32
  213. * @param[in,out] out pointer to output
  214. * @return The function returns one of the two
  215. * 1. The incremented output pointer for a successful operation or
  216. * 2. NULL if implementation is not available.
  217. *
  218. * @details Supported framework: TensorFlow Lite
  219. */
  220. int8_t *arm_nn_mat_mult_s8(const int8_t *input_row,
  221. const int8_t *input_col,
  222. const uint16_t output_ch,
  223. const uint16_t col_batches,
  224. const int32_t *output_shift,
  225. const int32_t *output_mult,
  226. const int32_t out_offset,
  227. const int32_t col_offset,
  228. const int32_t row_offset,
  229. const int16_t out_activation_min,
  230. const int16_t out_activation_max,
  231. const uint16_t row_len,
  232. const int32_t *const bias,
  233. int8_t *out);
  234. /**
  235. * @brief Matrix-multiplication function for convolution with per-channel requantization for 16 bits convolution.
  236. * @param[in] input_a pointer to operand A
  237. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  238. * @param[in] output_ch number of rows of A
  239. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  240. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  241. * @param[in] activation_min minimum value to clamp the output to. Range : int16
  242. * @param[in] activation_max maximum value to clamp the output to. Range : int16
  243. * @param[in] num_col_a number of columns of A
  244. * @param[in] output_bias per output channel bias. Range : int64
  245. * @param[in,out] out_0 pointer to output
  246. * @return The function returns one of the two
  247. * 1. The incremented output pointer for a successful operation or
  248. * 2. NULL if implementation is not available.
  249. *
  250. * @details This function does the matrix multiplication of weight matrix for all output channels
  251. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  252. * clamped in the range provided by activation min and max.
  253. * Supported framework: TensorFlow Lite micro.
  254. */
  255. int16_t *arm_nn_mat_mult_kernel_s16(const int8_t *input_a,
  256. const int16_t *input_b,
  257. const int32_t output_ch,
  258. const int32_t *out_shift,
  259. const int32_t *out_mult,
  260. const int16_t activation_min,
  261. const int16_t activation_max,
  262. const int32_t num_col_a,
  263. const int64_t *const output_bias,
  264. int16_t *out_0);
  265. /**
  266. * @brief General Vector by Matrix multiplication with requantization and storage of result.
  267. * @param[in] row_elements number of row elements
  268. * @param[in] skipped_row_elements number of row elements skipped due to padding.
  269. * row_elements + skipped_row_elements = (kernel_x * kernel_y) * input_ch
  270. * @param[in] row_base_ref pointer to row operand
  271. * @param[in] col_base_ref pointer to col operand
  272. * @param[out] out_ch Number of output channels
  273. * @param[in] conv_params Pointer to convolution parameters like offsets and activation values
  274. * @param[in] quant_params Pointer to per-channel quantization parameters
  275. * @param[in] bias Pointer to optional per-channel bias
  276. * @param[out] output Pointer to output where int8 results are stored.
  277. * @return The function performs matrix(row_base_ref) multiplication with vector(col_base_ref) and
  278. * scaled result is stored in memory.
  279. *
  280. * @details Pseudo-code
  281. * *output = 0
  282. * sum_col = 0
  283. * for (j = 0; j < out_ch; j++)
  284. * for (i = 0; i < row_elements; i++)
  285. * *output += row_base_ref[i] * col_base_ref[i]
  286. * sum_col += col_base_ref[i]
  287. * scale sum_col using quant_params and bias
  288. * store result in 'output'
  289. *
  290. *
  291. */
  292. arm_cmsis_nn_status arm_nn_mat_mul_core_1x_s8(int32_t row_elements,
  293. const int32_t skipped_row_elements,
  294. const int8_t *row_base_ref,
  295. const int8_t *col_base_ref,
  296. const int32_t out_ch,
  297. const cmsis_nn_conv_params *conv_params,
  298. const cmsis_nn_per_channel_quant_params *quant_params,
  299. const int32_t *bias,
  300. int8_t *output);
  301. /**
  302. * @brief Matrix-multiplication with requantization & activation function for four rows and one column
  303. * @param[in] row_elements number of row elements
  304. * @param[in] offset offset between rows. Can be the same as row_elements.
  305. * For e.g, in a 1x1 conv scenario with stride as 1.
  306. * @param[in] row_base pointer to row operand
  307. * @param[in] col_base pointer to col operand
  308. * @param[in] out_ch Number of output channels
  309. * @param[in] conv_params Pointer to convolution parameters like offsets and activation values
  310. * @param[in] quant_params Pointer to per-channel quantization parameters
  311. * @param[in] bias Pointer to per-channel bias
  312. * @param[out] output Pointer to output where int8 results are stored.
  313. *
  314. * @return The function returns the updated output pointer or NULL if implementation is not available.
  315. *
  316. * @details Compliant to TFLM int8 specification. MVE implementation only
  317. */
  318. int8_t *arm_nn_mat_mul_core_4x_s8(const int32_t row_elements,
  319. const int32_t offset,
  320. const int8_t *row_base,
  321. const int8_t *col_base,
  322. const int32_t out_ch,
  323. const cmsis_nn_conv_params *conv_params,
  324. const cmsis_nn_per_channel_quant_params *quant_params,
  325. const int32_t *bias,
  326. int8_t *output);
  327. /**
  328. * @brief General Matrix-multiplication function with per-channel requantization.
  329. * This function assumes:
  330. * - LHS input matrix NOT transposed (nt)
  331. * - RHS input matrix transposed (t)
  332. * - RHS is int8 packed with 2x int4
  333. * - LHS is int8
  334. *
  335. * @note This operation also performs the broadcast bias addition before the requantization
  336. *
  337. * @param[in] lhs Pointer to the LHS input matrix
  338. * @param[in] rhs Pointer to the RHS input matrix
  339. * @param[in] bias Pointer to the bias vector. The length of this vector is equal to the number of
  340. * output columns (or RHS input rows)
  341. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  342. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  343. * The length of this vector is equal to the number of output columns (or RHS input
  344. * rows)
  345. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  346. * of this vector is equal to the number of output columns (or RHS input rows)
  347. * @param[in] lhs_rows Number of LHS input rows
  348. * @param[in] rhs_rows Number of RHS input rows
  349. * @param[in] rhs_cols Number of LHS/RHS input columns
  350. * @param[in] lhs_offset Offset to be applied to the LHS input value
  351. * @param[in] dst_offset Offset to be applied the output result
  352. * @param[in] activation_min Minimum value to clamp down the output. Range : int8
  353. * @param[in] activation_max Maximum value to clamp up the output. Range : int8
  354. * @param[in] lhs_cols_offset Column offset between subsequent lhs_rows
  355. *
  356. * @return The function returns <code>ARM_CMSIS_NN_SUCCESS</code>
  357. *
  358. */
  359. arm_cmsis_nn_status arm_nn_mat_mult_nt_t_s4(const int8_t *lhs,
  360. const int8_t *rhs,
  361. const int32_t *bias,
  362. int8_t *dst,
  363. const int32_t *dst_multipliers,
  364. const int32_t *dst_shifts,
  365. const int32_t lhs_rows,
  366. const int32_t rhs_rows,
  367. const int32_t rhs_cols,
  368. const int32_t lhs_offset,
  369. const int32_t dst_offset,
  370. const int32_t activation_min,
  371. const int32_t activation_max,
  372. const int32_t lhs_cols_offset);
  373. /**
  374. * @brief General Matrix-multiplication function with per-channel requantization.
  375. * This function assumes:
  376. * - LHS input matrix NOT transposed (nt)
  377. * - RHS input matrix transposed (t)
  378. *
  379. * @note This operation also performs the broadcast bias addition before the requantization
  380. *
  381. * @param[in] lhs Pointer to the LHS input matrix
  382. * @param[in] rhs Pointer to the RHS input matrix
  383. * @param[in] bias Pointer to the bias vector. The length of this vector is equal to the number of
  384. * output columns (or RHS input rows)
  385. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  386. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  387. * The length of this vector is equal to the number of output columns (or RHS input
  388. * rows)
  389. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  390. * of this vector is equal to the number of output columns (or RHS input rows)
  391. * @param[in] lhs_rows Number of LHS input rows
  392. * @param[in] rhs_rows Number of RHS input rows
  393. * @param[in] rhs_cols Number of LHS/RHS input columns
  394. * @param[in] lhs_offset Offset to be applied to the LHS input value
  395. * @param[in] dst_offset Offset to be applied the output result
  396. * @param[in] activation_min Minimum value to clamp down the output. Range : int8
  397. * @param[in] activation_max Maximum value to clamp up the output. Range : int8
  398. * @param[in] row_address_offset Address offset between rows in output. NOTE: Only used for MVEI extension.
  399. * @param[in] lhs_cols_offset Column offset between subsequent lhs_rows
  400. *
  401. * @return The function returns <code>ARM_CMSIS_NN_SUCCESS</code>
  402. *
  403. */
  404. arm_cmsis_nn_status arm_nn_mat_mult_nt_t_s8(const int8_t *lhs,
  405. const int8_t *rhs,
  406. const int32_t *bias,
  407. int8_t *dst,
  408. const int32_t *dst_multipliers,
  409. const int32_t *dst_shifts,
  410. const int32_t lhs_rows,
  411. const int32_t rhs_rows,
  412. const int32_t rhs_cols,
  413. const int32_t lhs_offset,
  414. const int32_t dst_offset,
  415. const int32_t activation_min,
  416. const int32_t activation_max,
  417. const int32_t row_address_offset,
  418. const int32_t lhs_cols_offset);
  419. /**
  420. * @brief General Matrix-multiplication function with int8 input and int32 output.
  421. * This function assumes:
  422. * - LHS input matrix NOT transposed (nt)
  423. * - RHS input matrix transposed (t)
  424. *
  425. * @note Dst/output buffer must be zeroed out before calling this function.
  426. *
  427. * @param[in] lhs Pointer to the LHS input matrix
  428. * @param[in] rhs Pointer to the RHS input matrix
  429. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  430. * @param[in] lhs_rows Number of LHS input rows
  431. * @param[in] rhs_rows Number of LHS input columns/RHS input rows
  432. * @param[in] rhs_cols Number of RHS input columns
  433. * @param[in] lhs_offset Offset to be applied to the LHS input value
  434. * @param[in] dst_idx_offset Offset between subsequent output results
  435. *
  436. * @return The function returns <code>ARM_CMSIS_NN_SUCCESS</code>
  437. *
  438. */
  439. arm_cmsis_nn_status arm_nn_mat_mult_nt_t_s8_s32(const int8_t *lhs,
  440. const int8_t *rhs,
  441. int32_t *dst,
  442. const int32_t lhs_rows,
  443. const int32_t rhs_rows,
  444. const int32_t rhs_cols,
  445. const int32_t lhs_offset,
  446. const int32_t dst_idx_offset);
  447. /**
  448. * @brief s4 Vector by Matrix (transposed) multiplication
  449. *
  450. * @param[in] lhs Input left-hand side vector
  451. * @param[in] packed_rhs Input right-hand side matrix (transposed)
  452. * @param[in] bias Input bias
  453. * @param[out] dst Output vector
  454. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side vector.
  455. * Range: -127 to 128
  456. * @param[in] dst_offset Offset to be added to the output values. Range: -127 to 128
  457. * @param[in] dst_multiplier Output multiplier
  458. * @param[in] dst_shift Output shift
  459. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  460. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  461. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  462. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  463. * @param[in] address_offset Memory position offset for dst. First output is stored at 'dst', the
  464. * second at 'dst + address_offset' and so on. Default value is typically 1.
  465. *
  466. * @return The function returns <code>ARM_CMSIS_NN_SUCCESS</code>
  467. *
  468. */
  469. arm_cmsis_nn_status arm_nn_vec_mat_mult_t_s4(const int8_t *lhs,
  470. const int8_t *packed_rhs,
  471. const int32_t *bias,
  472. int8_t *dst,
  473. const int32_t lhs_offset,
  474. const int32_t dst_offset,
  475. const int32_t dst_multiplier,
  476. const int32_t dst_shift,
  477. const int32_t rhs_cols,
  478. const int32_t rhs_rows,
  479. const int32_t activation_min,
  480. const int32_t activation_max,
  481. const int32_t address_offset);
  482. /**
  483. * @brief s8 Vector by Matrix (transposed) multiplication
  484. *
  485. * @param[in] lhs Input left-hand side vector
  486. * @param[in] rhs Input right-hand side matrix (transposed)
  487. * @param[in] kernel_sum Kernel sums of the kernels (rhs). See arm_vector_sum_s8 for more info.
  488. * @param[in] bias Input bias
  489. * @param[out] dst Output vector
  490. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side vector.
  491. * Range: -127 to 128
  492. * @param[in] dst_offset Offset to be added to the output values. Range: -127 to 128
  493. * @param[in] dst_multiplier Output multiplier
  494. * @param[in] dst_shift Output shift
  495. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  496. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  497. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  498. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  499. * @param[in] address_offset Memory position offset for dst. First output is stored at 'dst', the
  500. * second at 'dst + address_offset' and so on. Default value is typically 1.
  501. * @param[in] rhs_offset Offset to be added to the input values of the right-hand side vector.
  502. * Range: -127 to 128
  503. *
  504. * @return The function returns <code>ARM_CMSIS_NN_SUCCESS</code>
  505. *
  506. */
  507. arm_cmsis_nn_status arm_nn_vec_mat_mult_t_s8(const int8_t *lhs,
  508. const int8_t *rhs,
  509. const int32_t *kernel_sum,
  510. const int32_t *bias,
  511. int8_t *dst,
  512. const int32_t lhs_offset,
  513. const int32_t dst_offset,
  514. const int32_t dst_multiplier,
  515. const int32_t dst_shift,
  516. const int32_t rhs_cols,
  517. const int32_t rhs_rows,
  518. const int32_t activation_min,
  519. const int32_t activation_max,
  520. const int32_t address_offset,
  521. const int32_t rhs_offset);
  522. /**
  523. * @brief s16 Vector by Matrix (transposed) multiplication
  524. *
  525. * @param[in] lhs Input left-hand side vector
  526. * @param[in] rhs Input right-hand side matrix (transposed)
  527. * @param[in] bias Input bias
  528. * @param[out] dst Output vector
  529. * @param[in] dst_multiplier Output multiplier
  530. * @param[in] dst_shift Output shift
  531. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  532. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  533. * @param[in] activation_min Minimum value to clamp the output to. Range: int16
  534. * @param[in] activation_max Maximum value to clamp the output to. Range: int16
  535. *
  536. * @return The function returns <code>ARM_CMSIS_NN_SUCCESS</code>
  537. *
  538. */
  539. arm_cmsis_nn_status arm_nn_vec_mat_mult_t_s16(const int16_t *lhs,
  540. const int8_t *rhs,
  541. const int64_t *bias,
  542. int16_t *dst,
  543. const int32_t dst_multiplier,
  544. const int32_t dst_shift,
  545. const int32_t rhs_cols,
  546. const int32_t rhs_rows,
  547. const int32_t activation_min,
  548. const int32_t activation_max);
  549. /**
  550. * @brief s8 Vector by Matrix (transposed) multiplication with s16 output
  551. *
  552. * @param[in] lhs Input left-hand side vector
  553. * @param[in] rhs Input right-hand side matrix (transposed)
  554. * @param[out] dst Output vector
  555. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side
  556. * vector. Range: -127 to 128
  557. * @param[in] scatter_offset Address offset for dst. First output is stored at 'dst', the
  558. * second at 'dst + scatter_offset' and so on.
  559. * @param[in] dst_multiplier Output multiplier
  560. * @param[in] dst_shift Output shift
  561. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  562. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  563. * @param[in] activation_min Minimum value to clamp the output to. Range: int16
  564. * @param[in] activation_max Maximum value to clamp the output to. Range: int16
  565. *
  566. * @return The function returns <code>ARM_CMSIS_NN_SUCCESS</code>
  567. *
  568. */
  569. arm_cmsis_nn_status arm_nn_vec_mat_mult_t_svdf_s8(const int8_t *lhs,
  570. const int8_t *rhs,
  571. int16_t *dst,
  572. const int32_t lhs_offset,
  573. const int32_t scatter_offset,
  574. const int32_t dst_multiplier,
  575. const int32_t dst_shift,
  576. const int32_t rhs_cols,
  577. const int32_t rhs_rows,
  578. const int32_t activation_min,
  579. const int32_t activation_max);
  580. /**
  581. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in padded cases where
  582. * the padding is -lhs_offset(Range: int8). Dimensions are the same for lhs and rhs.
  583. *
  584. * @param[in] lhs Input left-hand side matrix
  585. * @param[in] rhs Input right-hand side matrix (transposed)
  586. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  587. * @param[in] active_ch Subset of total_ch processed
  588. * @param[in] total_ch Number of channels in LHS/RHS
  589. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels
  590. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels
  591. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  592. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  593. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  594. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  595. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels
  596. * @param[in] out Output pointer
  597. *
  598. * @return The function returns one of the two
  599. * - Updated output pointer if an implementation is available
  600. * - NULL if no implementation is available.
  601. *
  602. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  603. * out for the following.
  604. * - Output shift
  605. * - Output multiplier
  606. * - Output bias
  607. * - rhs
  608. */
  609. arm_cmsis_nn_status arm_nn_depthwise_conv_nt_t_padded_s8(const int8_t *lhs,
  610. const int8_t *rhs,
  611. const int32_t lhs_offset,
  612. const int32_t active_ch,
  613. const int32_t total_ch,
  614. const int32_t *out_shift,
  615. const int32_t *out_mult,
  616. const int32_t out_offset,
  617. const int32_t activation_min,
  618. const int32_t activation_max,
  619. const uint16_t row_x_col,
  620. const int32_t *const output_bias,
  621. int8_t *out);
  622. /**
  623. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in non-padded cases.
  624. * Dimensions are the same for lhs and rhs.
  625. *
  626. * @param[in] lhs Input left-hand side matrix
  627. * @param[in] rhs Input right-hand side matrix (transposed)
  628. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  629. * @param[in] active_ch Subset of total_ch processed
  630. * @param[in] total_ch Number of channels in LHS/RHS
  631. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels.
  632. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels.
  633. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  634. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  635. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  636. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  637. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels.
  638. * @param[in] out Output pointer
  639. *
  640. * @return The function returns one of the two
  641. * - Updated output pointer if an implementation is available
  642. * - NULL if no implementation is available.
  643. *
  644. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  645. * out for the following.
  646. * - Output shift
  647. * - Output multiplier
  648. * - Output bias
  649. * - rhs
  650. */
  651. arm_cmsis_nn_status arm_nn_depthwise_conv_nt_t_s8(const int8_t *lhs,
  652. const int8_t *rhs,
  653. const int32_t lhs_offset,
  654. const int32_t active_ch,
  655. const int32_t total_ch,
  656. const int32_t *out_shift,
  657. const int32_t *out_mult,
  658. const int32_t out_offset,
  659. const int32_t activation_min,
  660. const int32_t activation_max,
  661. const uint16_t row_x_col,
  662. const int32_t *const output_bias,
  663. int8_t *out);
  664. /**
  665. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in non-padded cases.
  666. * Dimensions are the same for lhs and rhs.
  667. *
  668. * @param[in] lhs Input left-hand side matrix
  669. * @param[in] rhs Input right-hand side matrix (transposed)
  670. * @param[in] num_ch Number of channels in LHS/RHS
  671. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels.
  672. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels.
  673. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  674. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  675. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  676. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels.
  677. * @param[in] out Output pointer
  678. *
  679. * @return The function returns one of the two
  680. * - Updated output pointer if an implementation is available
  681. * - NULL if no implementation is available.
  682. *
  683. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  684. * out for the following.
  685. * - Output shift
  686. * - Output multiplier
  687. * - Output bias
  688. * - rhs
  689. */
  690. int16_t *arm_nn_depthwise_conv_nt_t_s16(const int16_t *lhs,
  691. const int8_t *rhs,
  692. const uint16_t num_ch,
  693. const int32_t *out_shift,
  694. const int32_t *out_mult,
  695. const int32_t activation_min,
  696. const int32_t activation_max,
  697. const uint16_t row_x_col,
  698. const int64_t *const output_bias,
  699. int16_t *out);
  700. /**
  701. @brief Read 2 s16 elements and post increment pointer.
  702. @param[in] in_q15 Pointer to pointer that holds address of input.
  703. @return q31 value
  704. */
  705. __STATIC_FORCEINLINE int32_t arm_nn_read_q15x2_ia(const int16_t **in_q15)
  706. {
  707. int32_t val;
  708. memcpy(&val, *in_q15, 4);
  709. *in_q15 += 2;
  710. return (val);
  711. }
  712. /**
  713. @brief Read 4 s8 from s8 pointer and post increment pointer.
  714. @param[in] in_s8 Pointer to pointer that holds address of input.
  715. @return q31 value
  716. */
  717. __STATIC_FORCEINLINE int32_t arm_nn_read_s8x4_ia(const int8_t **in_s8)
  718. {
  719. int32_t val;
  720. memcpy(&val, *in_s8, 4);
  721. *in_s8 += 4;
  722. return (val);
  723. }
  724. /**
  725. @brief Read 2 s8 from s8 pointer and post increment pointer.
  726. @param[in] in_s8 Pointer to pointer that holds address of input.
  727. @return q31 value
  728. */
  729. __STATIC_FORCEINLINE int32_t arm_nn_read_s8x2_ia(const int8_t **in_s8)
  730. {
  731. int32_t val;
  732. memcpy(&val, *in_s8, 2);
  733. *in_s8 += 2;
  734. return (val);
  735. }
  736. /**
  737. @brief Read 2 int16 values from int16 pointer.
  738. @param[in] in pointer to address of input.
  739. @return s32 value
  740. */
  741. __STATIC_FORCEINLINE int32_t arm_nn_read_s16x2(const int16_t *in)
  742. {
  743. int32_t val;
  744. memcpy(&val, in, 4);
  745. return (val);
  746. }
  747. /**
  748. @brief Read 4 s8 values.
  749. @param[in] in_s8 pointer to address of input.
  750. @return s32 value
  751. */
  752. __STATIC_FORCEINLINE int32_t arm_nn_read_s8x4(const int8_t *in_s8)
  753. {
  754. int32_t val;
  755. memcpy(&val, in_s8, 4);
  756. return (val);
  757. }
  758. /**
  759. @brief Read 2 s8 values.
  760. @param[in] in_s8 pointer to address of input.
  761. @return s32 value
  762. */
  763. __STATIC_FORCEINLINE int32_t arm_nn_read_s8x2(const int8_t *in_s8)
  764. {
  765. int32_t val;
  766. memcpy(&val, in_s8, 2);
  767. return (val);
  768. }
  769. /**
  770. @brief Write four s8 to s8 pointer and increment pointer afterwards.
  771. @param[in] in Double pointer to input value
  772. @param[in] value Four bytes to copy
  773. */
  774. __STATIC_FORCEINLINE void arm_nn_write_s8x4_ia(int8_t **in, int32_t value)
  775. {
  776. memcpy(*in, &value, 4);
  777. *in += 4;
  778. }
  779. /**
  780. * @brief memset optimized for MVE
  781. * @param[in, out] dst Destination pointer
  782. * @param[in] val Value to set
  783. * @param[in] block_size Number of bytes to copy.
  784. *
  785. */
  786. __STATIC_FORCEINLINE void arm_memset_s8(int8_t *dst, const int8_t val, uint32_t block_size)
  787. {
  788. #if defined(ARM_MATH_MVEI)
  789. __asm volatile(" vdup.8 q0, %[set_val] \n"
  790. " wlstp.8 lr, %[cnt], 1f \n"
  791. "2: \n"
  792. " vstrb.8 q0, [%[in]], #16 \n"
  793. " letp lr, 2b \n"
  794. "1: \n"
  795. : [in] "+r"(dst)
  796. : [cnt] "r"(block_size), [set_val] "r"(val)
  797. : "q0", "memory", "r14");
  798. #else
  799. memset(dst, val, block_size);
  800. #endif
  801. }
  802. #if defined(ARM_MATH_DSP)
  803. /**
  804. * @brief read and expand one s4 word into two s8 words.
  805. */
  806. __STATIC_FORCEINLINE void read_and_pad_s4(const int8_t *source, int32_t *out1, int32_t *out2)
  807. {
  808. int16_t in = arm_nn_read_s8x2(source);
  809. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  810. *out1 = SXTB16_RORn(__sxtb16(inA << 4), 4);
  811. *out2 = SXTB16_RORn(__sxtb16(inA), 4);
  812. }
  813. /**
  814. * @brief read and expand one s4 word into two s8 words.
  815. * @details The s4 elements are not evenly aligned on the byte boundary, so 3 bytes need to be read instead of 2.
  816. * In other words first nibble to read start at the middle of a byte.
  817. * byte index, s4 element
  818. * 0, s4_x
  819. * 0, s4_0
  820. * 1, s4_1
  821. * 1, s4_2
  822. * 2, s4_3
  823. * 2, s4_x
  824. */
  825. __STATIC_FORCEINLINE void read_and_pad_s4_uneven(const int8_t *source, int32_t *out1, int32_t *out2)
  826. {
  827. int32_t inA1 = (source[0] & 0xFF) | ((source[1] & 0xFF) << 16);
  828. int32_t inA2 = (source[1] & 0xFF) | ((source[2] & 0xFF) << 16);
  829. *out1 = SXTB16_RORn(__sxtb16(inA2 << 4), 4);
  830. *out2 = SXTB16_RORn(__sxtb16(inA1), 4);
  831. }
  832. /**
  833. * @brief read and expand one s4 word into two s16 words with ordering.
  834. */
  835. __STATIC_FORCEINLINE void read_and_pad_s4_ordered(const int8_t *source, int32_t *out1, int32_t *out2)
  836. {
  837. int16_t in = arm_nn_read_s8x2(source);
  838. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  839. int32_t inAbuf1 = SXTB16_RORn(__sxtb16(inA), 4);
  840. int32_t inAbuf2 = SXTB16_RORn(__sxtb16(inA << 4), 4);
  841. #ifndef ARM_MATH_BIG_ENDIAN
  842. *out2 = (int32_t)(PKHTB(inAbuf1, inAbuf2, 16));
  843. *out1 = (int32_t)(PKHBT(inAbuf2, inAbuf1, 16));
  844. #else
  845. *out1 = (int32_t)(PKHTB(inAbuf1, inAbuf2, 16));
  846. *out2 = (int32_t)(PKHBT(inAbuf2, inAbuf1, 16));
  847. #endif
  848. }
  849. /**
  850. * @brief read and expand one s8 word into two s16 words with ordering.
  851. */
  852. __STATIC_FORCEINLINE const int8_t *read_and_pad(const int8_t *source, int32_t *out1, int32_t *out2)
  853. {
  854. int32_t inA = arm_nn_read_s8x4_ia(&source);
  855. int32_t inAbuf1 = SXTB16_RORn((uint32_t)inA, 8);
  856. int32_t inAbuf2 = SXTB16(inA);
  857. #ifndef ARM_MATH_BIG_ENDIAN
  858. *out2 = (int32_t)(PKHTB(inAbuf1, inAbuf2, 16));
  859. *out1 = (int32_t)(PKHBT(inAbuf2, inAbuf1, 16));
  860. #else
  861. *out1 = (int32_t)(PKHTB(inAbuf1, inAbuf2, 16));
  862. *out2 = (int32_t)(PKHBT(inAbuf2, inAbuf1, 16));
  863. #endif
  864. return source;
  865. }
  866. /**
  867. * @brief read and expand one s8 word into two s16 words with ordering and addition.
  868. */
  869. __STATIC_FORCEINLINE void read_pad_and_add_s8(const int8_t *source, int32_t *out1, int32_t *out2, const uint32_t add)
  870. {
  871. int32_t inA = arm_nn_read_s8x4(source);
  872. int32_t inAbuf1 = SXTAB16_RORn(add, (uint32_t)inA, 8);
  873. int32_t inAbuf2 = SXTAB16(add, inA);
  874. #ifndef ARM_MATH_BIG_ENDIAN
  875. *out2 = (int32_t)(PKHTB(inAbuf1, inAbuf2, 16));
  876. *out1 = (int32_t)(PKHBT(inAbuf2, inAbuf1, 16));
  877. #else
  878. *out1 = (int32_t)(PKHTB(inAbuf1, inAbuf2, 16));
  879. *out2 = (int32_t)(PKHBT(inAbuf2, inAbuf1, 16));
  880. #endif
  881. }
  882. /**
  883. * @brief read and expand two bytes into one word with ordering.
  884. */
  885. __STATIC_FORCEINLINE void read_and_pad_s8x2(const int8_t *source, int32_t *out)
  886. {
  887. int16_t in = arm_nn_read_s8x2(source);
  888. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  889. *out = SXTB16(inA);
  890. }
  891. /**
  892. * @brief read and expand two bytes into one word with ordering and addition.
  893. */
  894. __STATIC_FORCEINLINE void read_pad_and_add_s8x2(const int8_t *source, int32_t *out, const uint32_t add)
  895. {
  896. int16_t in = arm_nn_read_s8x2(source);
  897. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  898. *out = SXTAB16(add, inA);
  899. }
  900. /**
  901. * @brief read and expand one s8 word into two s16 words with no additional ordering.
  902. */
  903. __STATIC_FORCEINLINE const int8_t *read_and_pad_reordered(const int8_t *source, int32_t *out1, int32_t *out2)
  904. {
  905. int32_t inA = arm_nn_read_s8x4_ia(&source);
  906. #ifndef ARM_MATH_BIG_ENDIAN
  907. *out2 = SXTB16(ROR((uint32_t)inA, 8));
  908. *out1 = SXTB16(inA);
  909. #else
  910. *out1 = SXTB16(ROR((uint32_t)inA, 8));
  911. *out2 = SXTB16(inA);
  912. #endif
  913. return source;
  914. }
  915. #endif
  916. /**
  917. * @brief Matrix-multiplication function for convolution with per-channel requantization and 4 bit weights.
  918. * @param[in] input_a pointer to operand A, int8 packed with 2x int4.
  919. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  920. * @param[in] output_ch number of rows of A
  921. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  922. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  923. * @param[in] out_offset output tensor offset.
  924. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  925. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  926. * @param[in] num_col_a number of columns of A
  927. * @param[in] output_bias per output channel bias. Range : int32
  928. * @param[in,out] out_0 pointer to output
  929. * @return The function returns one of the two
  930. * 1. The incremented output pointer for a successful operation or
  931. * 2. NULL if implementation is not available.
  932. *
  933. * @details This function does the matrix multiplication of weight matrix for all output channels
  934. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  935. * clamped in the range provided by activation min and max.
  936. * Supported framework: TensorFlow Lite micro.
  937. */
  938. int8_t *arm_nn_mat_mult_kernel_s4_s16(const int8_t *input_a,
  939. const int16_t *input_b,
  940. const uint16_t output_ch,
  941. const int32_t *out_shift,
  942. const int32_t *out_mult,
  943. const int32_t out_offset,
  944. const int32_t activation_min,
  945. const int32_t activation_max,
  946. const int32_t num_col_a,
  947. const int32_t *const output_bias,
  948. int8_t *out_0);
  949. /**
  950. * @brief Matrix-multiplication function for convolution with per-channel requantization.
  951. * @param[in] input_a pointer to operand A
  952. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  953. * @param[in] output_ch number of rows of A
  954. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  955. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  956. * @param[in] out_offset output tensor offset.
  957. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  958. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  959. * @param[in] num_col_a number of columns of A
  960. * @param[in] aligned_num_col_a number of columns of A aligned by 4
  961. * @param[in] output_bias per output channel bias. Range : int32
  962. * @param[in,out] out_0 pointer to output
  963. * @return The function returns one of the two
  964. * 1. The incremented output pointer for a successful operation or
  965. * 2. NULL if implementation is not available.
  966. *
  967. * @details This function does the matrix multiplication of weight matrix for all output channels
  968. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  969. * clamped in the range provided by activation min and max.
  970. * Supported framework: TensorFlow Lite micro.
  971. */
  972. int8_t *arm_nn_mat_mult_kernel_s8_s16(const int8_t *input_a,
  973. const int16_t *input_b,
  974. const uint16_t output_ch,
  975. const int32_t *out_shift,
  976. const int32_t *out_mult,
  977. const int32_t out_offset,
  978. const int16_t activation_min,
  979. const int16_t activation_max,
  980. const int32_t num_col_a,
  981. const int32_t aligned_num_col_a,
  982. const int32_t *const output_bias,
  983. int8_t *out_0);
  984. /**
  985. * @brief Matrix-multiplication function for convolution with per-channel requantization, supporting an address offset
  986. * between rows.
  987. * @param[in] input_a pointer to operand A
  988. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  989. * @param[in] output_ch number of rows of A
  990. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  991. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  992. * @param[in] out_offset output tensor offset.
  993. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  994. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  995. * @param[in] num_col_a number of columns of A
  996. * @param[in] aligned_num_col_a number of columns of A aligned by 4
  997. * @param[in] output_bias per output channel bias. Range : int32
  998. * @param[in] row_address_offset address offset between rows in the output
  999. * @param[in,out] out_0 pointer to output
  1000. * @return The function returns one of the two
  1001. * 1. The incremented output pointer for a successful operation or
  1002. * 2. NULL if implementation is not available.
  1003. *
  1004. * @details This function does the matrix multiplication of weight matrix for all output channels
  1005. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  1006. * clamped in the range provided by activation min and max.
  1007. *
  1008. * This function is slighly less performant than arm_nn_mat_mult_kernel_s8_s16, but allows support for
  1009. * grouped convolution. Supported framework: TensorFlow Lite micro.
  1010. */
  1011. int8_t *arm_nn_mat_mult_kernel_row_offset_s8_s16(const int8_t *input_a,
  1012. const int16_t *input_b,
  1013. const uint16_t output_ch,
  1014. const int32_t *out_shift,
  1015. const int32_t *out_mult,
  1016. const int32_t out_offset,
  1017. const int16_t activation_min,
  1018. const int16_t activation_max,
  1019. const int32_t num_col_a,
  1020. const int32_t aligned_num_col_a,
  1021. const int32_t *const output_bias,
  1022. const int32_t row_address_offset,
  1023. int8_t *out_0);
  1024. /**
  1025. * @brief Common softmax function for s8 input and s8 or s16 output
  1026. * @param[in] input Pointer to the input tensor
  1027. * @param[in] num_rows Number of rows in the input tensor
  1028. * @param[in] row_size Number of elements in each input row
  1029. * @param[in] mult Input quantization multiplier
  1030. * @param[in] shift Input quantization shift within the range [0, 31]
  1031. * @param[in] diff_min Minimum difference with max in row. Used to check if
  1032. * the quantized exponential operation can be performed
  1033. * @param[in] int16_output Indicating s8 output if 0 else s16 output
  1034. * @param[out] output Pointer to the output tensor
  1035. *
  1036. * @note Supported framework: TensorFlow Lite micro (bit-accurate)
  1037. *
  1038. */
  1039. void arm_nn_softmax_common_s8(const int8_t *input,
  1040. const int32_t num_rows,
  1041. const int32_t row_size,
  1042. const int32_t mult,
  1043. const int32_t shift,
  1044. const int32_t diff_min,
  1045. const bool int16_output,
  1046. void *output);
  1047. /**
  1048. * @brief macro for adding rounding offset
  1049. */
  1050. #ifndef ARM_NN_TRUNCATE
  1051. #define NN_ROUND(out_shift) ((0x1 << out_shift) >> 1)
  1052. #else
  1053. #define NN_ROUND(out_shift) 0
  1054. #endif
  1055. // Macros for shortening quantization functions' names and avoid long lines
  1056. #define MUL_SAT(a, b) arm_nn_doubling_high_mult((a), (b))
  1057. #define MUL_SAT_MVE(a, b) arm_doubling_high_mult_mve_32x4((a), (b))
  1058. #define MUL_POW2(a, b) arm_nn_mult_by_power_of_two((a), (b))
  1059. #define DIV_POW2(a, b) arm_nn_divide_by_power_of_two((a), (b))
  1060. #define DIV_POW2_MVE(a, b) arm_divide_by_power_of_two_mve((a), (b))
  1061. #define EXP_ON_NEG(x) arm_nn_exp_on_negative_values((x))
  1062. #define ONE_OVER1(x) arm_nn_one_over_one_plus_x_for_x_in_0_1((x))
  1063. /**
  1064. * @brief Saturating doubling high multiply. Result matches
  1065. * NEON instruction VQRDMULH.
  1066. * @param[in] m1 Multiplicand. Range: {NN_Q31_MIN, NN_Q31_MAX}
  1067. * @param[in] m2 Multiplier. Range: {NN_Q31_MIN, NN_Q31_MAX}
  1068. * @return Result of multiplication.
  1069. *
  1070. */
  1071. __STATIC_FORCEINLINE int32_t arm_nn_doubling_high_mult(const int32_t m1, const int32_t m2)
  1072. {
  1073. int32_t result = 0;
  1074. // Rounding offset to add for a right shift of 31
  1075. int64_t mult = 1 << 30;
  1076. if ((m1 < 0) ^ (m2 < 0))
  1077. {
  1078. mult = 1 - mult;
  1079. }
  1080. // Gets resolved as a SMLAL instruction
  1081. mult = mult + (int64_t)m1 * m2;
  1082. // Utilize all of the upper 32 bits. This is the doubling step
  1083. // as well.
  1084. result = (int32_t)(mult / (1ll << 31));
  1085. if ((m1 == m2) && (m1 == (int32_t)NN_Q31_MIN))
  1086. {
  1087. result = NN_Q31_MAX;
  1088. }
  1089. return result;
  1090. }
  1091. /**
  1092. * @brief Doubling high multiply without saturation. This is intended
  1093. * for requantization where the scale is a positive integer
  1094. *
  1095. * @param[in] m1 Multiplicand. Range: {NN_Q31_MIN, NN_Q31_MAX}
  1096. * @param[in] m2 Multiplier Range: {NN_Q31_MIN, NN_Q31_MAX}
  1097. * @return Result of multiplication.
  1098. * @note The result of this matches that of neon instruction
  1099. * VQRDMULH for m1 in range {NN_Q31_MIN, NN_Q31_MAX} and m2 in
  1100. * range {NN_Q31_MIN + 1, NN_Q31_MAX}. Saturation occurs when
  1101. * m1 equals m2 equals NN_Q31_MIN and that is not handled by
  1102. * this function.
  1103. *
  1104. */
  1105. __STATIC_FORCEINLINE int32_t arm_nn_doubling_high_mult_no_sat(const int32_t m1, const int32_t m2)
  1106. {
  1107. int32_t result = 0;
  1108. union arm_nn_long_long mult;
  1109. // Rounding offset to add for a right shift of 31
  1110. mult.word.low = 1 << 30;
  1111. mult.word.high = 0;
  1112. // Gets resolved as a SMLAL instruction
  1113. mult.long_long = mult.long_long + (int64_t)m1 * m2;
  1114. // Utilize all of the upper 32 bits. This is the doubling step
  1115. // as well.
  1116. result = (int32_t)(mult.long_long >> 31);
  1117. return result;
  1118. }
  1119. /**
  1120. * @brief Rounding divide by power of two.
  1121. * @param[in] dividend - Dividend
  1122. * @param[in] exponent - Divisor = power(2, exponent)
  1123. * Range: [0, 31]
  1124. * @return Rounded result of division. Midpoint is rounded away from zero.
  1125. *
  1126. */
  1127. __STATIC_FORCEINLINE int32_t arm_nn_divide_by_power_of_two(const int32_t dividend, const int32_t exponent)
  1128. {
  1129. int32_t result = 0;
  1130. const int32_t remainder_mask = (1 << exponent) - 1;
  1131. int32_t remainder = remainder_mask & dividend;
  1132. // Basic division
  1133. result = dividend >> exponent;
  1134. // Adjust 'result' for rounding (mid point away from zero)
  1135. int32_t threshold = remainder_mask >> 1;
  1136. if (result < 0)
  1137. {
  1138. threshold++;
  1139. }
  1140. if (remainder > threshold)
  1141. {
  1142. result++;
  1143. }
  1144. return result;
  1145. }
  1146. /**
  1147. * @brief Requantize a given value.
  1148. * @param[in] val Value to be requantized
  1149. * @param[in] multiplier multiplier. Range {NN_Q31_MIN + 1, Q32_MAX}
  1150. * @param[in] shift left or right shift for 'val * multiplier'
  1151. *
  1152. * @return Returns (val * multiplier)/(2 ^ shift)
  1153. *
  1154. */
  1155. __STATIC_FORCEINLINE int32_t arm_nn_requantize(const int32_t val, const int32_t multiplier, const int32_t shift)
  1156. {
  1157. #ifdef CMSIS_NN_USE_SINGLE_ROUNDING
  1158. const int64_t total_shift = 31 - shift;
  1159. const int64_t new_val = val * (int64_t)multiplier;
  1160. int32_t result = new_val >> (total_shift - 1);
  1161. result = (result + 1) >> 1;
  1162. return result;
  1163. #else
  1164. return arm_nn_divide_by_power_of_two(arm_nn_doubling_high_mult_no_sat(val * (1 << LEFT_SHIFT(shift)), multiplier),
  1165. RIGHT_SHIFT(shift));
  1166. #endif
  1167. }
  1168. /**
  1169. * @brief Requantize a given 64 bit value.
  1170. * @param[in] val Value to be requantized in the range {-(1<<47)} to {(1<<47) - 1}
  1171. * @param[in] reduced_multiplier Reduced multiplier in the range {NN_Q31_MIN + 1, Q32_MAX} to {Q16_MIN + 1,
  1172. * Q16_MAX}
  1173. * @param[in] shift Left or right shift for 'val * multiplier' in the range {-31} to {7}
  1174. *
  1175. * @return Returns (val * multiplier)/(2 ^ shift)
  1176. *
  1177. */
  1178. __STATIC_FORCEINLINE int32_t arm_nn_requantize_s64(const int64_t val,
  1179. const int32_t reduced_multiplier,
  1180. const int32_t shift)
  1181. {
  1182. const int64_t new_val = val * reduced_multiplier;
  1183. int32_t result = new_val >> (14 - shift); // 64->32 bit reduction
  1184. result = (result + 1) >> 1; // Last shift position and insert round
  1185. return result;
  1186. }
  1187. /**
  1188. * @brief memcpy optimized for MVE
  1189. * @param[in, out] dst Destination pointer
  1190. * @param[in] src Source pointer.
  1191. * @param[in] block_size Number of bytes to copy.
  1192. *
  1193. */
  1194. __STATIC_FORCEINLINE void arm_memcpy_s8(int8_t *__RESTRICT dst, const int8_t *__RESTRICT src, uint32_t block_size)
  1195. {
  1196. #if defined(ARM_MATH_MVEI)
  1197. __asm volatile(" wlstp.8 lr, %[cnt], 1f \n"
  1198. "2: \n"
  1199. " vldrb.8 q0, [%[in]], #16 \n"
  1200. " vstrb.8 q0, [%[out]], #16 \n"
  1201. " letp lr, 2b \n"
  1202. "1: \n"
  1203. : [in] "+r"(src), [out] "+r"(dst)
  1204. : [cnt] "r"(block_size)
  1205. : "q0", "memory", "r14");
  1206. #else
  1207. memcpy(dst, src, block_size);
  1208. #endif
  1209. }
  1210. /**
  1211. * @brief memcpy wrapper for int16
  1212. * @param[in, out] dst Destination pointer
  1213. * @param[in] src Source pointer.
  1214. * @param[in] block_size Number of bytes to copy.
  1215. *
  1216. */
  1217. __STATIC_FORCEINLINE void arm_memcpy_q15(int16_t *__RESTRICT dst, const int16_t *__RESTRICT src, uint32_t block_size)
  1218. {
  1219. memcpy(dst, src, block_size);
  1220. }
  1221. #if defined(ARM_MATH_MVEI)
  1222. /**
  1223. * @brief Vector saturating doubling high multiply returning high half.
  1224. * @param[in] m1 Multiplicand
  1225. * @param[in] m2 Multiplier
  1226. * @return Result of multiplication.
  1227. *
  1228. */
  1229. __STATIC_FORCEINLINE int32x4_t arm_doubling_high_mult_mve(const int32x4_t m1, const int32_t m2)
  1230. {
  1231. return vqrdmulhq_n_s32(m1, m2);
  1232. }
  1233. /**
  1234. * @brief Vector rounding divide by power of two.
  1235. * @param[in] dividend - Dividend vector
  1236. * @param[in] exponent - Divisor = power(2, exponent)
  1237. * Range: [0, 31]
  1238. * @return Rounded result of division. Midpoint is rounded away from zero.
  1239. *
  1240. */
  1241. __STATIC_FORCEINLINE int32x4_t arm_divide_by_power_of_two_mve(const int32x4_t dividend, const int32_t exponent)
  1242. {
  1243. const int32x4_t shift = vdupq_n_s32(-exponent);
  1244. const int32x4_t fixup = vshrq_n_s32(vandq_s32(dividend, shift), 31);
  1245. const int32x4_t fixed_up_dividend = vqaddq_s32(dividend, fixup);
  1246. return vrshlq_s32(fixed_up_dividend, shift);
  1247. }
  1248. /**
  1249. * @brief Requantize a given vector.
  1250. * @param[in] val Vector to be requantized
  1251. * @param[in] multiplier multiplier
  1252. * @param[in] shift shift
  1253. *
  1254. * @return Returns (val * multiplier)/(2 ^ shift)
  1255. *
  1256. */
  1257. __STATIC_FORCEINLINE int32x4_t arm_requantize_mve(const int32x4_t val, const int32_t multiplier, const int32_t shift)
  1258. {
  1259. #ifdef CMSIS_NN_USE_SINGLE_ROUNDING
  1260. const int right_shift = MIN(-1, shift);
  1261. const int left_shift = shift - right_shift;
  1262. const int32x4_t left_shift_dup = vdupq_n_s32(left_shift);
  1263. const int32x4_t right_shift_dup = vdupq_n_s32(right_shift);
  1264. int32x4_t result = vqdmulhq_n_s32(vshlq_s32(val, left_shift_dup), multiplier);
  1265. result = vrshlq_s32(result, right_shift_dup);
  1266. return result;
  1267. #else
  1268. return arm_divide_by_power_of_two_mve(
  1269. arm_doubling_high_mult_mve(vshlq_s32(val, vdupq_n_s32(LEFT_SHIFT(shift))), multiplier), RIGHT_SHIFT(shift));
  1270. #endif
  1271. }
  1272. __STATIC_FORCEINLINE int32x4_t arm_doubling_high_mult_mve_32x4(const int32x4_t m1, const int32x4_t m2)
  1273. {
  1274. return vqrdmulhq_s32(m1, m2);
  1275. }
  1276. __STATIC_FORCEINLINE int32x4_t arm_divide_by_power_of_two_mve_32x4(const int32x4_t dividend, const int32x4_t exponent)
  1277. {
  1278. const int32x4_t shift = -exponent;
  1279. const int32x4_t fixup = vshrq_n_s32(vandq_s32(dividend, shift), 31);
  1280. const int32x4_t fixed_up_dividend = vqaddq_s32(dividend, fixup);
  1281. return vrshlq_s32(fixed_up_dividend, shift);
  1282. }
  1283. __STATIC_FORCEINLINE int32x4_t arm_requantize_mve_32x4(const int32x4_t val,
  1284. const int32x4_t multiplier,
  1285. const int32x4_t shift)
  1286. {
  1287. #ifdef CMSIS_NN_USE_SINGLE_ROUNDING
  1288. const int32x4_t right_shift = vminq_s32(vdupq_n_s32(-1), shift);
  1289. const int32x4_t left_shift = vqsubq_s32(shift, right_shift);
  1290. int32x4_t result = vqdmulhq_s32(vshlq_s32(val, left_shift), multiplier);
  1291. result = vrshlq_s32(result, right_shift);
  1292. return result;
  1293. #else
  1294. const int32x4_t zz = vdupq_n_s32(0);
  1295. const mve_pred16_t p = vcmpgtq_n_s32(shift, 0);
  1296. const int32x4_t left_shift = vpselq_s32(shift, zz, p);
  1297. const int32x4_t right_shift = -vpselq_s32(zz, shift, p);
  1298. return arm_divide_by_power_of_two_mve_32x4(arm_doubling_high_mult_mve_32x4(vshlq_s32(val, left_shift), multiplier),
  1299. right_shift);
  1300. #endif
  1301. }
  1302. #endif
  1303. // @note The following functions are used only for softmax layer, scaled bits = 5 assumed
  1304. __STATIC_FORCEINLINE int32_t arm_nn_exp_on_negative_values(int32_t val)
  1305. {
  1306. int32_t mask = 0;
  1307. int32_t shift = 24;
  1308. const int32_t val_mod_minus_quarter = (val & ((1 << shift) - 1)) - (1 << shift);
  1309. const int32_t remainder = val_mod_minus_quarter - val;
  1310. const int32_t x = (val_mod_minus_quarter << 5) + (1 << 28);
  1311. const int32_t x2 = MUL_SAT(x, x);
  1312. int32_t result = 1895147668 +
  1313. MUL_SAT(1895147668, x + DIV_POW2(MUL_SAT(DIV_POW2(MUL_SAT(x2, x2), 2) + MUL_SAT(x2, x), 715827883) + x2, 1));
  1314. #define SELECT_IF_NON_ZERO(x) \
  1315. { \
  1316. mask = MASK_IF_NON_ZERO(remainder & (1 << shift++)); \
  1317. result = SELECT_USING_MASK(mask, MUL_SAT(result, x), result); \
  1318. }
  1319. SELECT_IF_NON_ZERO(1672461947)
  1320. SELECT_IF_NON_ZERO(1302514674)
  1321. SELECT_IF_NON_ZERO(790015084)
  1322. SELECT_IF_NON_ZERO(290630308)
  1323. SELECT_IF_NON_ZERO(39332535)
  1324. SELECT_IF_NON_ZERO(720401)
  1325. SELECT_IF_NON_ZERO(242)
  1326. #undef SELECT_IF_NON_ZERO
  1327. mask = MASK_IF_ZERO(val);
  1328. return SELECT_USING_MASK(mask, NN_Q31_MAX, result);
  1329. }
  1330. __STATIC_FORCEINLINE int32_t arm_nn_mult_by_power_of_two(const int32_t val, const int32_t exp)
  1331. {
  1332. const int32_t thresh = ((1 << (31 - exp)) - 1);
  1333. int32_t result = val << exp;
  1334. result = SELECT_USING_MASK(MASK_IF_NON_ZERO(val > thresh), NN_Q31_MAX, result);
  1335. result = SELECT_USING_MASK(MASK_IF_NON_ZERO(val < -thresh), NN_Q31_MIN, result);
  1336. return result;
  1337. }
  1338. __STATIC_FORCEINLINE int32_t arm_nn_one_over_one_plus_x_for_x_in_0_1(int32_t val)
  1339. {
  1340. const int64_t sum = (int64_t)val + (int64_t)NN_Q31_MAX;
  1341. const int32_t half_denominator = (int32_t)((sum + (sum >= 0 ? 1 : -1)) / 2L);
  1342. int32_t x = 1515870810 + MUL_SAT(half_denominator, -1010580540);
  1343. const int32_t shift = (1 << 29);
  1344. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  1345. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  1346. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  1347. return MUL_POW2(x, 1);
  1348. }
  1349. /**
  1350. @brief Write 2 s16 elements and post increment pointer.
  1351. @param[in] dest_q15 Pointer to pointer that holds address of destination.
  1352. @param[in] src_q31 Input value to be written.
  1353. */
  1354. __STATIC_FORCEINLINE void arm_nn_write_q15x2_ia(int16_t **dest_q15, int32_t src_q31)
  1355. {
  1356. int32_t val = src_q31;
  1357. memcpy(*dest_q15, &val, 4);
  1358. *dest_q15 += 2;
  1359. }
  1360. /**
  1361. @brief Write 2 s8 elements and post increment pointer.
  1362. @param[in] dst Pointer to pointer that holds address of destination.
  1363. @param[in] src Input value to be written.
  1364. */
  1365. __STATIC_FORCEINLINE void arm_nn_write_s8x2_ia(int8_t **dst, int16_t src)
  1366. {
  1367. memcpy(*dst, &src, 2);
  1368. *dst += 2;
  1369. }
  1370. // Support functions for LSTM
  1371. /**
  1372. * @brief Update LSTM function for an iteration step
  1373. *
  1374. * @param[in] data_in Data input pointervoid
  1375. * @param[in] hidden_in Hidden state/ recurrent input pointer
  1376. * @param[out] hidden_out Hidden state/ recurrent output pointer
  1377. * @param[in] params Struct containg all information about the lstm operator, see
  1378. * arm_nn_types.
  1379. * @param[in] buffers Struct containg pointers to all temporary scratch buffers needed for the
  1380. * lstm operator, see arm_nn_types.
  1381. * @param[in] batch_offset Number of timesteps between consecutive batches.
  1382. * E.g for params->timing_major = true, all batches for t=0 are stored sequentially, so batch offset = 1.
  1383. * For params->time major = false, all time steps are stored continously before the next batch, so
  1384. * batch offset = params->time_steps.
  1385. * @return The function returns ARM_CMSIS_NN_SUCCESS
  1386. */
  1387. arm_cmsis_nn_status arm_nn_lstm_step_s8(const int8_t *data_in,
  1388. const int8_t *hidden_in,
  1389. int8_t *hidden_out,
  1390. const cmsis_nn_lstm_params *params,
  1391. cmsis_nn_lstm_context *buffers,
  1392. const int32_t batch_offset);
  1393. /**
  1394. * @brief Updates a LSTM gate for an iteration step of LSTM function, int8x8_16 version.
  1395. *
  1396. * @param[in] data_in Data input pointer
  1397. * @param[in] hidden_in Hidden state/ recurrent input pointer
  1398. * @param[in] gate_data Struct containing all information about the gate caluclation, see
  1399. * arm_nn_types.
  1400. * @param[in] params Struct containing all information about the lstm_operation, see
  1401. * arm_nn_types
  1402. * @param[out] output Hidden state/ recurrent output pointer
  1403. * @param[in] batch_offset Number of timesteps between consecutive batches, see
  1404. * arm_nn_lstm_step_s8.
  1405. * @return The function returns ARM_CMSIS_NN_SUCCESS
  1406. */
  1407. arm_cmsis_nn_status arm_nn_lstm_calculate_gate_s8_s16(const int8_t *data_in,
  1408. const int8_t *hidden_in,
  1409. const cmsis_nn_lstm_gate *gate_data,
  1410. const cmsis_nn_lstm_params *params,
  1411. int16_t *output,
  1412. const int32_t batch_offset);
  1413. /**
  1414. * @brief The result of the multiplication is accumulated to the passed result buffer.
  1415. * Multiplies a matrix by a "batched" vector (i.e. a matrix with a batch dimension composed by input vectors independent
  1416. * from each other).
  1417. *
  1418. * @param[in] lhs Batched vector
  1419. * @param[in] rhs Weights - input matrix (H(Rows)xW(Columns))
  1420. * @param[in] effective_bias Bias + lhs_offset * kernel_sum term precalculated into a constant vector.
  1421. * @param[out] dst Output
  1422. * @param[in] dst_multiplier Multiplier for quantization
  1423. * @param[in] dst_shift Shift for quantization
  1424. * @param[in] rhs_cols Vector/matarix column length
  1425. * @param[in] rhs_rows Row count of matrix
  1426. * @param[in] batches Batch size
  1427. * @param[in] batch_offset Number of timesteps between consecutive batches in input, see arm_nn_lstm_step_s8. Note
  1428. that the output is always stored with sequential batches.
  1429. * @return The function returns <code>ARM_CMSIS_NN_SUCCESS</code>
  1430. */
  1431. arm_cmsis_nn_status arm_nn_vec_mat_mul_result_acc_s8_s16(const int8_t *lhs,
  1432. const int8_t *rhs,
  1433. const int32_t *effective_bias,
  1434. int16_t *dst,
  1435. const int32_t dst_multiplier,
  1436. const int32_t dst_shift,
  1437. const int32_t rhs_cols,
  1438. const int32_t rhs_rows,
  1439. const int32_t batches,
  1440. const int32_t batch_offset);
  1441. /**
  1442. * @brief s16 elementwise multiplication with s8 output
  1443. * @param[in] input_1_vect pointer to input vector 1
  1444. * @param[in] input_2_vect pointer to input vector 2
  1445. * @param[in,out] output pointer to output vector
  1446. * @param[in] out_offset output offset
  1447. * @param[in] out_mult output multiplier
  1448. * @param[in] out_shift output shift
  1449. * @param[in] block_size number of samples per batch
  1450. * @param[in] batch_size number of samples per batch
  1451. * @param[in] batch_offset Number of timesteps between consecutive batches in output, see
  1452. * arm_nn_lstm_step_s8. Note that it is assumed that the input is stored with sequential batches.
  1453. * @return The function returns ARM_CMSIS_NN_SUCCESS
  1454. *
  1455. * @details Supported framework: TensorFlow Lite micro
  1456. */
  1457. arm_cmsis_nn_status arm_elementwise_mul_s16_s8(const int16_t *input_1_vect,
  1458. const int16_t *input_2_vect,
  1459. int8_t *output,
  1460. const int32_t out_offset,
  1461. const int32_t out_mult,
  1462. const int32_t out_shift,
  1463. const int32_t block_size,
  1464. const int32_t batch_size,
  1465. const int32_t batch_offset);
  1466. /**
  1467. * @brief s16 elementwise multiplication. The result of the multiplication is accumulated to the passed result buffer.
  1468. * @param[in] input_1_vect pointer to input vector 1
  1469. * @param[in] input_2_vect pointer to input vector 2
  1470. * @param[in] input_1_offset offset for input 1. Not used.
  1471. * @param[in] input_2_offset offset for input 2. Not used.
  1472. * @param[in,out] output pointer to output vector
  1473. * @param[in] out_offset output offset. Not used.
  1474. * @param[in] out_mult output multiplier
  1475. * @param[in] out_shift output shift
  1476. * @param[in] out_activation_min minimum value to clamp output to. Min: -32768
  1477. * @param[in] out_activation_max maximum value to clamp output to. Max: 32767
  1478. * @param[in] block_size number of samples
  1479. * @return The function returns ARM_CMSIS_NN_SUCCESS
  1480. *
  1481. * @details Supported framework: TensorFlow Lite micro
  1482. */
  1483. arm_cmsis_nn_status arm_elementwise_mul_acc_s16(const int16_t *input_1_vect,
  1484. const int16_t *input_2_vect,
  1485. const int32_t input_1_offset,
  1486. const int32_t input_2_offset,
  1487. int16_t *output,
  1488. const int32_t out_offset,
  1489. const int32_t out_mult,
  1490. const int32_t out_shift,
  1491. const int32_t out_activation_min,
  1492. const int32_t out_activation_max,
  1493. const int32_t block_size);
  1494. #ifdef __cplusplus
  1495. }
  1496. #endif
  1497. #endif /* ARM_NNSUPPORTFUNCTIONS_H */