riscv_nnsupportfunctions.h 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2010-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
  3. * Copyright (c) 2022 Nuclei Limited. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the License); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /* ----------------------------------------------------------------------
  20. * Project: NMSIS NN Library
  21. * Title: riscv_nnsupportfunctions.h
  22. * Description: Public header file of support functions for NMSIS NN Library
  23. *
  24. * $Date: 30 April 2024
  25. * $Revision: V.22.0.0
  26. *
  27. * Target Processor: RISC-V Cores
  28. * -------------------------------------------------------------------- */
  29. #ifndef RISCV_NNSUPPORTFUNCTIONS_H
  30. #define RISCV_NNSUPPORTFUNCTIONS_H
  31. #include "riscv_nn_math_types.h"
  32. #include "riscv_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. /*
  50. * Use RVV may help if data length > RVV_OPT_THRESHOLD, otherwise use pure C version
  51. * RVV_OPT_THRESHOLD could be {0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F ...}
  52. */
  53. #define RVV_OPT_THRESHOLD 0xF
  54. // For input of int16 when number of columns are above this limit int64 accumulation is needed
  55. // to not loose precision.
  56. #define MAX_COL_COUNT (512)
  57. // NMSIS-NN has two implementations of the transpose conv operator, selected depending on the number of input
  58. // channels. This is based on heuristics and may be finetuned depending on other parameters of the operator
  59. #define REVERSE_TCOL_EFFICIENT_THRESHOLD (16)
  60. // By default this will have no effect. During compilation this may be set to __restrict,
  61. // which may be beneficial for performance. See README.md for more intformation.
  62. #ifndef OPTIONAL_RESTRICT_KEYWORD
  63. #define OPTIONAL_RESTRICT_KEYWORD
  64. #endif
  65. /**
  66. * @brief definition to pack four 8 bit values.
  67. */
  68. #define PACK_S8x4_32x1(v0, v1, v2, v3) \
  69. ((((int32_t)(v0) << 0) & (int32_t)0x000000FF) | (((int32_t)(v1) << 8) & (int32_t)0x0000FF00) | \
  70. (((int32_t)(v2) << 16) & (int32_t)0x00FF0000) | (((int32_t)(v3) << 24) & (int32_t)0xFF000000))
  71. /**
  72. * @brief definition to pack two 16 bit values.
  73. */
  74. #define PACK_Q15x2_32x1(v0, v1) (((int32_t)v0 & (int32_t)0xFFFF) | ((int32_t)v1 << 16))
  75. /**
  76. * @defgroup groupSupport Private
  77. *
  78. * Internal Support functions. Not intended to be called direclty by a NMSIS-NN user.
  79. *
  80. */
  81. /**
  82. * @defgroup genPrivTypes Structure Types
  83. * @ingroup groupSupport
  84. * @brief Data structure types used by private functions.
  85. * @{
  86. */
  87. /**
  88. * @brief Union for SIMD access of q31/s16/s8 types
  89. */
  90. union riscv_nnword
  91. {
  92. int32_t word;
  93. /**< q31 type */
  94. int16_t half_words[2];
  95. /**< s16 type */
  96. int8_t bytes[4];
  97. /**< s8 type */
  98. };
  99. /**
  100. * @brief Union for data type long long
  101. */
  102. struct riscv_nn_double
  103. {
  104. uint32_t low;
  105. int32_t high;
  106. };
  107. union riscv_nn_long_long
  108. {
  109. int64_t long_long;
  110. struct riscv_nn_double word;
  111. };
  112. #ifndef RISCV_MATH_DSP
  113. /**
  114. * @brief definition to pack two 16 bit values.
  115. */
  116. #define __NN_PKHBT(ARG1, ARG2, ARG3) ( (((int32_t)(ARG1) << 0) & (int32_t)0x0000FFFF) | \
  117. (((int32_t)(ARG2) << ARG3) & (int32_t)0xFFFF0000) )
  118. #define __NN_PKHTB(ARG1, ARG2, ARG3) ( (((int32_t)(ARG1) << 0) & (int32_t)0xFFFF0000) | \
  119. (((int32_t)(ARG2) >> ARG3) & (int32_t)0x0000FFFF) )
  120. /**
  121. * @brief Clips Q63 to Q31 values.
  122. */
  123. __STATIC_FORCEINLINE q31_t nn_clip_q63_to_q31(
  124. q63_t x)
  125. {
  126. return ((q31_t) (x >> 32) != ((q31_t) x >> 31)) ?
  127. ((0x7FFFFFFF ^ ((q31_t) (x >> 63)))) : (q31_t) x;
  128. }
  129. /*
  130. * @brief C custom defined QADD
  131. */
  132. __STATIC_FORCEINLINE int32_t __NN_QADD(
  133. int32_t x,
  134. int32_t y)
  135. {
  136. return ((int32_t)(nn_clip_q63_to_q31((q63_t)x + (q31_t)y)));
  137. }
  138. /*
  139. * @brief C custom defined QADD16
  140. */
  141. __STATIC_FORCEINLINE uint32_t __NN_QADD16(
  142. uint32_t x,
  143. uint32_t y)
  144. {
  145. /* q31_t r, s; without initialisation 'riscv_offset_q15 test' fails but 'intrinsic' tests pass! */
  146. q31_t r = 0, s = 0;
  147. r = __SSAT(((((q31_t)x << 16) >> 16) + (((q31_t)y << 16) >> 16)), 16) & (int32_t)0x0000FFFF;
  148. s = __SSAT(((((q31_t)x ) >> 16) + (((q31_t)y ) >> 16)), 16) & (int32_t)0x0000FFFF;
  149. return ((uint32_t)((s << 16) | (r )));
  150. }
  151. /*
  152. * @brief C custom defined SXTB16
  153. */
  154. __STATIC_FORCEINLINE uint32_t __NN_SXTB16(
  155. uint32_t x)
  156. {
  157. return ((uint32_t)(((((q31_t)x << 24) >> 24) & (q31_t)0x0000FFFF) |
  158. ((((q31_t)x << 8) >> 8) & (q31_t)0xFFFF0000) ));
  159. }
  160. #else
  161. #define __NN_PKHBT __PKHBT
  162. #define __NN_PKHTB __PKHTB
  163. #define __NN_QADD __QADD
  164. #define __NN_QADD16 __QADD16
  165. #define __NN_SXTB16 __SXTB16
  166. #endif
  167. /**
  168. * @brief definition to pack four 8 bit values.
  169. */
  170. #define __NN_PACKq7(v0,v1,v2,v3) ( (((int32_t)(v0) << 0) & (int32_t)0x000000FF) | \
  171. (((int32_t)(v1) << 8) & (int32_t)0x0000FF00) | \
  172. (((int32_t)(v2) << 16) & (int32_t)0x00FF0000) | \
  173. (((int32_t)(v3) << 24) & (int32_t)0xFF000000) )
  174. /**
  175. * @} // end group groupPrivTypes
  176. */
  177. /**
  178. * @defgroup supportConversion Data Conversion
  179. *
  180. * Perform data type conversion in-between neural network operations
  181. *
  182. */
  183. /**
  184. * @brief Converts the elements of the q7 vector to q15 vector without left-shift
  185. * @param[in] *pSrc points to the q7 input vector
  186. * @param[out] *pDst points to the q15 output vector
  187. * @param[in] blockSize length of the input vector
  188. *
  189. */
  190. void riscv_q7_to_q15_no_shift(const q7_t *pSrc, q15_t *pDst, uint32_t blockSize);
  191. void riscv_q7_to_q7_no_shift(const q7_t * pSrc, q7_t * pDst, uint32_t blockSize);
  192. /**
  193. * @brief Non-saturating addition of elements of a q7 vector
  194. * @param[in] *input Pointer to the q7 input vector
  195. * @param[out] *output Pointer to the q31 output variable.
  196. * @param[in] block_size length of the input vector
  197. * \par Description:
  198. *
  199. * 2^24 samples can be added without saturating the result.
  200. *
  201. * The equation used for the conversion process is:
  202. *
  203. * <pre>
  204. * sum = input[0] + input[1] + .. + input[block_size -1]
  205. * </pre>
  206. *
  207. * */
  208. void riscv_nn_add_q7(const q7_t *input, q31_t *output, uint32_t block_size);
  209. /**
  210. * @brief Converts the elements of the s8 vector to reordered q15 vector without left-shift
  211. * @param[in] *pSrc points to the s8 input vector
  212. * @param[out] *pDst points to the s16 output vector
  213. * @param[in] blockSize length of the input vector
  214. * @return none.
  215. *
  216. */
  217. void riscv_q7_to_q15_reordered_no_shift(const q7_t *pSrc, q15_t *pDst, uint32_t blockSize);
  218. void riscv_q7_to_q7_reordered_no_shift(const q7_t * pSrc, q7_t * pDst, uint32_t blockSize);
  219. /**
  220. * @brief Converts the elements from a s8 vector to a s16 vector with an added offset
  221. * @param[in] src pointer to the s8 input vector
  222. * @param[out] dst pointer to the s16 output vector
  223. * @param[in] block_size length of the input vector
  224. * @param[in] offset s16 offset to be added to each input vector element.
  225. *
  226. * \par Description:
  227. *
  228. * Output elements are ordered.
  229. * The equation used for the conversion process is:
  230. *
  231. * <pre>
  232. * dst[n] = (int16_t) src[n] + offset; 0 <= n < block_size.
  233. * </pre>
  234. *
  235. */
  236. void riscv_q7_to_q15_with_offset(const int8_t *src, int16_t *dst, int32_t block_size, int16_t offset);
  237. #if defined(RISCV_MATH_DSP)
  238. /**
  239. * @brief Converts the elements from a s8 vector to a s16 vector with an added offset
  240. * @param[in] src pointer to the s8 input vector
  241. * @param[out] dst pointer to the s16 output vector
  242. * @param[in] block_size length of the input vector
  243. * @param[in] offset s16 offset to be added to each input vector element.
  244. *
  245. * \par Description:
  246. *
  247. * No additonal ordering is done with the result that output elements are not in order.
  248. * Instead of ABCD order will be ACBD.
  249. * Note this is for processors with DSP extension only.
  250. * The equation used for the conversion process is:
  251. *
  252. * <pre>
  253. * dst[n - 0] = (int16_t) src[n - 0] + offset; 0 <= n < block_size.
  254. * dst[n - 1] = (int16_t) src[n - 2] + offset; 0 <= n < block_size.
  255. * dst[n - 2] = (int16_t) src[n - 1] + offset; 0 <= n < block_size.
  256. * dst[n - 3] = (int16_t) src[n - 3] + offset; 0 <= n < block_size.
  257. * </pre>
  258. *
  259. */
  260. void riscv_s8_to_s16_unordered_with_offset(const int8_t *src, int16_t *dst, int32_t block_size, int16_t offset);
  261. #endif
  262. /**
  263. * @brief Get the required buffer size for optimized s8 depthwise convolution
  264. * function with constraint that in_channel equals out_channel.
  265. * This is for processors with DSP extension.
  266. * Refer to riscv_depthwise_conv_s8_opt_get_buffer_size() for function argument details.
  267. *
  268. * @note Intended for compilation on Host. If compiling for an Riscv target, use
  269. * riscv_depthwise_conv_s8_opt_get_buffer_size(). Note also this is a support function,
  270. * so not recommended to call directly even on Host.
  271. *
  272. */
  273. int32_t riscv_depthwise_conv_s8_opt_get_buffer_size_dsp(const nmsis_nn_dims *input_dims,
  274. const nmsis_nn_dims *filter_dims);
  275. /**
  276. * @brief Converts the elements from a s8 vector to a s16 vector with an added offset
  277. * @param[in] src pointer to the s8 input vector
  278. * @param[out] dst pointer to the s16 output vector
  279. * @param[in] block_size length of the input vector
  280. * @param[in] offset offset to be added to each input vector element.
  281. * @return none.
  282. *
  283. * @details This function does the q7 to q15 expansion with re-ordering of bytes. Re-ordering is a consequence of
  284. * the sign extension intrinsic(DSP extension). The tail (i.e., last (N % 4) elements) retains its
  285. * original order.
  286. *
  287. */
  288. void riscv_q7_to_q15_reordered_with_offset(const q7_t *src, q15_t *dst, uint32_t block_size, q15_t offset);
  289. /**
  290. * @brief Converts the elements from a q7 vector and accumulate to a q15 vector
  291. * @param[in] *src points to the q7 input vector
  292. * @param[out] *dst points to the q15 output vector
  293. * @param[in] block_size length of the input vector
  294. *
  295. * \par Description:
  296. *
  297. * The equation used for the conversion process is:
  298. *
  299. * <pre>
  300. * dst[n] += (q15_t) src[n] ; 0 <= n < block_size.
  301. * </pre>
  302. *
  303. */
  304. void riscv_nn_accumulate_q7_to_q15(q15_t *dst, const q7_t *src, uint32_t block_size);
  305. /**
  306. * @brief Depthwise conv on an im2col buffer where the input channel equals output channel.
  307. * @param[in] row pointer to row
  308. * @param[in] col pointer to im2col buffer, always consists of 2 columns.
  309. * @param[in] num_ch number of channels
  310. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  311. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  312. * @param[in] out_offset output tensor offset.
  313. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  314. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  315. * @param[in] kernel_size number of elements in one column.
  316. * @param[in] output_bias per output channel bias. Range : int32
  317. * @param[out] out pointer to output
  318. * @return The function returns one of the two
  319. * 1. The incremented output pointer for a successful operation or
  320. * 2. NULL if implementation is not available.
  321. *
  322. * @details Supported framework: TensorFlow Lite micro.
  323. */
  324. int8_t *riscv_nn_depthwise_conv_s8_core(const int8_t *row,
  325. const int16_t *col,
  326. const uint16_t num_ch,
  327. const int32_t *out_shift,
  328. const int32_t *out_mult,
  329. const int32_t out_offset,
  330. const int32_t activation_min,
  331. const int32_t activation_max,
  332. const uint16_t kernel_size,
  333. const int32_t *const output_bias,
  334. int8_t *out);
  335. /**
  336. * @brief General Matrix-multiplication function with per-channel requantization.
  337. * @param[in] input_row pointer to row operand
  338. * @param[in] input_col pointer to col operand
  339. * @param[in] output_ch number of rows of input_row
  340. * @param[in] col_batches number of column batches. Range: 1 to 4
  341. * @param[in] output_shift pointer to per output channel requantization shift parameter.
  342. * @param[in] output_mult pointer to per output channel requantization multiplier parameter.
  343. * @param[in] out_offset output tensor offset.
  344. * @param[in] col_offset input tensor(col) offset.
  345. * @param[in] row_offset kernel offset(row). Not used.
  346. * @param[in] out_activation_min minimum value to clamp the output to. Range : int8
  347. * @param[in] out_activation_max maximum value to clamp the output to. Range : int8
  348. * @param[in] row_len number of elements in each row
  349. * @param[in] bias per output channel bias. Range : int32
  350. * @param[in,out] out pointer to output
  351. * @return The function returns one of the two
  352. * 1. The incremented output pointer for a successful operation or
  353. * 2. NULL if implementation is not available.
  354. *
  355. * @details Supported framework: TensorFlow Lite
  356. */
  357. int8_t *riscv_nn_mat_mult_s8(const int8_t *input_row,
  358. const int8_t *input_col,
  359. const uint16_t output_ch,
  360. const uint16_t col_batches,
  361. const int32_t *output_shift,
  362. const int32_t *output_mult,
  363. const int32_t out_offset,
  364. const int32_t col_offset,
  365. const int32_t row_offset,
  366. const int16_t out_activation_min,
  367. const int16_t out_activation_max,
  368. const uint16_t row_len,
  369. const int32_t *const bias,
  370. int8_t *out);
  371. /**
  372. * @brief Matrix-multiplication function for convolution with per-channel requantization for 16 bits convolution.
  373. * @param[in] input_a pointer to operand A
  374. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  375. * @param[in] output_ch number of rows of A
  376. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  377. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  378. * @param[in] activation_min minimum value to clamp the output to. Range : int16
  379. * @param[in] activation_max maximum value to clamp the output to. Range : int16
  380. * @param[in] num_col_a number of columns of A
  381. * @param[in] bias_data pointer to struct with bias vector. The length of this vector is equal to the number
  382. * of output columns (or RHS input rows). The vector can be int32 or int64 indicated by a
  383. * flag in the struct.
  384. * @param[in,out] out_0 pointer to output
  385. * @return The function returns one of the two
  386. * 1. The incremented output pointer for a successful operation or
  387. * 2. NULL if implementation is not available.
  388. *
  389. * @details This function does the matrix multiplication of weight matrix for all output channels
  390. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  391. * clamped in the range provided by activation min and max.
  392. * Supported framework: TensorFlow Lite micro.
  393. */
  394. int16_t *riscv_nn_mat_mult_kernel_s16(const int8_t *input_a,
  395. const int16_t *input_b,
  396. const int32_t output_ch,
  397. const int32_t *out_shift,
  398. const int32_t *out_mult,
  399. const int32_t activation_min,
  400. const int32_t activation_max,
  401. const int32_t num_col_a,
  402. const nmsis_nn_bias_data *const bias_data,
  403. int16_t *out_0);
  404. /**
  405. * @brief General Vector by Matrix multiplication with requantization and storage of result.
  406. * @param[in] row_elements number of row elements
  407. * @param[in] skipped_row_elements number of row elements skipped due to padding.
  408. * row_elements + skipped_row_elements = (kernel_x * kernel_y) * input_ch
  409. * @param[in] row_base_ref pointer to row operand
  410. * @param[in] col_base_ref pointer to col operand
  411. * @param[out] out_ch Number of output channels
  412. * @param[in] conv_params Pointer to convolution parameters like offsets and activation values
  413. * @param[in] quant_params Pointer to per-channel quantization parameters
  414. * @param[in] bias Pointer to optional per-channel bias
  415. * @param[out] output Pointer to output where int8 results are stored.
  416. * @return The function performs matrix(row_base_ref) multiplication with vector(col_base_ref) and
  417. * scaled result is stored in memory.
  418. *
  419. * @details Pseudo-code
  420. * *output = 0
  421. * sum_col = 0
  422. * for (j = 0; j < out_ch; j++)
  423. * for (i = 0; i < row_elements; i++)
  424. * *output += row_base_ref[i] * col_base_ref[i]
  425. * sum_col += col_base_ref[i]
  426. * scale sum_col using quant_params and bias
  427. * store result in 'output'
  428. *
  429. *
  430. */
  431. riscv_nmsis_nn_status riscv_nn_mat_mul_core_1x_s8(int32_t row_elements,
  432. const int32_t skipped_row_elements,
  433. const int8_t *row_base_ref,
  434. const int8_t *col_base_ref,
  435. const int32_t out_ch,
  436. const nmsis_nn_conv_params *conv_params,
  437. const nmsis_nn_per_channel_quant_params *quant_params,
  438. const int32_t *bias,
  439. int8_t *output);
  440. /**
  441. * @brief General Vector by Matrix multiplication with requantization, storage of result and int4 weights packed into an
  442. * int8 buffer.
  443. * @param[in] row_elements number of row elements
  444. * @param[in] skipped_row_elements number of row elements skipped due to padding.
  445. * row_elements + skipped_row_elements = (kernel_x * kernel_y) * input_ch
  446. * @param[in] row_base_ref pointer to row operand
  447. * @param[in] col_base_ref pointer to col operand as packed int4
  448. * @param[out] out_ch Number of output channels
  449. * @param[in] conv_params Pointer to convolution parameters like offsets and activation values
  450. * @param[in] quant_params Pointer to per-channel quantization parameters
  451. * @param[in] bias Pointer to optional per-channel bias
  452. * @param[out] output Pointer to output where int8 results are stored.
  453. * @return The function performs matrix(row_base_ref) multiplication with vector(col_base_ref) and
  454. * scaled result is stored in memory.
  455. *
  456. * @details Pseudo-code as int8 example. Int4 filter data will be unpacked.
  457. * *output = 0
  458. * sum_col = 0
  459. * for (j = 0; j < out_ch; j++)
  460. * for (i = 0; i < row_elements; i++)
  461. * *output += row_base_ref[i] * col_base_ref[i]
  462. * sum_col += col_base_ref[i]
  463. * scale sum_col using quant_params and bias
  464. * store result in 'output'
  465. *
  466. *
  467. */
  468. riscv_nmsis_nn_status riscv_nn_mat_mul_core_1x_s4(int32_t row_elements,
  469. const int32_t skipped_row_elements,
  470. const int8_t *row_base_ref,
  471. const int8_t *col_base_ref,
  472. const int32_t out_ch,
  473. const nmsis_nn_conv_params *conv_params,
  474. const nmsis_nn_per_channel_quant_params *quant_params,
  475. const int32_t *bias,
  476. int8_t *output);
  477. /**
  478. * @brief Matrix-multiplication with requantization & activation function for four rows and one column
  479. * @param[in] row_elements number of row elements
  480. * @param[in] offset offset between rows. Can be the same as row_elements.
  481. * For e.g, in a 1x1 conv scenario with stride as 1.
  482. * @param[in] row_base pointer to row operand
  483. * @param[in] col_base pointer to col operand
  484. * @param[in] out_ch Number of output channels
  485. * @param[in] conv_params Pointer to convolution parameters like offsets and activation values
  486. * @param[in] quant_params Pointer to per-channel quantization parameters
  487. * @param[in] bias Pointer to per-channel bias
  488. * @param[out] output Pointer to output where int8 results are stored.
  489. *
  490. * @return The function returns the updated output pointer or NULL if implementation is not available.
  491. *
  492. * @details Compliant to TFLM int8 specification. MVE implementation only
  493. */
  494. int8_t *riscv_nn_mat_mul_core_4x_s8(const int32_t row_elements,
  495. const int32_t offset,
  496. const int8_t *row_base,
  497. const int8_t *col_base,
  498. const int32_t out_ch,
  499. const nmsis_nn_conv_params *conv_params,
  500. const nmsis_nn_per_channel_quant_params *quant_params,
  501. const int32_t *bias,
  502. int8_t *output);
  503. /**
  504. * @brief General Matrix-multiplication function with per-channel requantization.
  505. * This function assumes:
  506. * - LHS input matrix NOT transposed (nt)
  507. * - RHS input matrix transposed (t)
  508. * - RHS is int8 packed with 2x int4
  509. * - LHS is int8
  510. *
  511. * @note This operation also performs the broadcast bias addition before the requantization
  512. *
  513. * @param[in] lhs Pointer to the LHS input matrix
  514. * @param[in] rhs Pointer to the RHS input matrix
  515. * @param[in] bias Pointer to the bias vector. The length of this vector is equal to the number of
  516. * output columns (or RHS input rows)
  517. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  518. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  519. * The length of this vector is equal to the number of output columns (or RHS input
  520. * rows)
  521. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  522. * of this vector is equal to the number of output columns (or RHS input rows)
  523. * @param[in] lhs_rows Number of LHS input rows
  524. * @param[in] rhs_rows Number of RHS input rows
  525. * @param[in] rhs_cols Number of LHS/RHS input columns
  526. * @param[in] lhs_offset Offset to be applied to the LHS input value
  527. * @param[in] dst_offset Offset to be applied the output result
  528. * @param[in] activation_min Minimum value to clamp down the output. Range : int8
  529. * @param[in] activation_max Maximum value to clamp up the output. Range : int8
  530. * @param[in] lhs_cols_offset Column offset between subsequent lhs_rows
  531. *
  532. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  533. *
  534. */
  535. riscv_nmsis_nn_status riscv_nn_mat_mult_nt_t_s4(const int8_t *lhs,
  536. const int8_t *rhs,
  537. const int32_t *bias,
  538. int8_t *dst,
  539. const int32_t *dst_multipliers,
  540. const int32_t *dst_shifts,
  541. const int32_t lhs_rows,
  542. const int32_t rhs_rows,
  543. const int32_t rhs_cols,
  544. const int32_t lhs_offset,
  545. const int32_t dst_offset,
  546. const int32_t activation_min,
  547. const int32_t activation_max,
  548. const int32_t lhs_cols_offset);
  549. /**
  550. * @brief General Matrix-multiplication function with per-channel requantization.
  551. * This function assumes:
  552. * - LHS input matrix NOT transposed (nt)
  553. * - RHS input matrix transposed (t)
  554. * - RHS is int8 packed with 2x int4
  555. * - LHS is int8
  556. * - LHS/RHS input columns must be even numbered
  557. * - LHS must be interleaved. Compare to riscv_nn_mat_mult_nt_t_s4 where LHS is not interleaved.
  558. *
  559. * @note This operation also performs the broadcast bias addition before the requantization
  560. *
  561. * @param[in] lhs Pointer to the LHS input matrix
  562. * @param[in] rhs Pointer to the RHS input matrix
  563. * @param[in] bias Pointer to the bias vector. The length of this vector is equal to the number of
  564. * output columns (or RHS input rows)
  565. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  566. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  567. * The length of this vector is equal to the number of output columns (or RHS input
  568. * rows)
  569. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  570. * of this vector is equal to the number of output columns (or RHS input rows)
  571. * @param[in] lhs_rows Number of LHS input rows
  572. * @param[in] rhs_rows Number of RHS input rows
  573. * @param[in] rhs_cols Number of LHS/RHS input columns. Note this must be even.
  574. * @param[in] lhs_offset Offset to be applied to the LHS input value
  575. * @param[in] dst_offset Offset to be applied the output result
  576. * @param[in] activation_min Minimum value to clamp down the output. Range : int8
  577. * @param[in] activation_max Maximum value to clamp up the output. Range : int8
  578. * @param[in] lhs_cols_offset Column offset between subsequent lhs_rows
  579. *
  580. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  581. *
  582. */
  583. riscv_nmsis_nn_status riscv_nn_mat_mult_nt_interleaved_t_even_s4(const int8_t *lhs,
  584. const int8_t *rhs,
  585. const int32_t *bias,
  586. int8_t *dst,
  587. const int32_t *dst_multipliers,
  588. const int32_t *dst_shifts,
  589. const int32_t lhs_rows,
  590. const int32_t rhs_rows,
  591. const int32_t rhs_cols,
  592. const int32_t lhs_offset,
  593. const int32_t dst_offset,
  594. const int32_t activation_min,
  595. const int32_t activation_max,
  596. const int32_t lhs_cols_offset);
  597. /**
  598. * @brief General Matrix-multiplication function with per-channel requantization.
  599. * This function assumes:
  600. * - LHS input matrix NOT transposed (nt)
  601. * - RHS input matrix transposed (t)
  602. *
  603. * @note This operation also performs the broadcast bias addition before the requantization
  604. *
  605. * @param[in] lhs Pointer to the LHS input matrix
  606. * @param[in] rhs Pointer to the RHS input matrix
  607. * @param[in] bias Pointer to the bias vector. The length of this vector is equal to the number of
  608. * output columns (or RHS input rows)
  609. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  610. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  611. * The length of this vector is equal to the number of output columns (or RHS input
  612. * rows)
  613. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  614. * of this vector is equal to the number of output columns (or RHS input rows)
  615. * @param[in] lhs_rows Number of LHS input rows
  616. * @param[in] rhs_rows Number of RHS input rows
  617. * @param[in] rhs_cols Number of LHS/RHS input columns
  618. * @param[in] lhs_offset Offset to be applied to the LHS input value
  619. * @param[in] dst_offset Offset to be applied the output result
  620. * @param[in] activation_min Minimum value to clamp down the output. Range : int8
  621. * @param[in] activation_max Maximum value to clamp up the output. Range : int8
  622. * @param[in] row_address_offset Address offset between rows in output.
  623. * @param[in] lhs_cols_offset Column offset between subsequent lhs_rows
  624. *
  625. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  626. *
  627. */
  628. riscv_nmsis_nn_status riscv_nn_mat_mult_nt_t_s8(const int8_t *lhs,
  629. const int8_t *rhs,
  630. const int32_t *bias,
  631. int8_t *dst,
  632. const int32_t *dst_multipliers,
  633. const int32_t *dst_shifts,
  634. const int32_t lhs_rows,
  635. const int32_t rhs_rows,
  636. const int32_t rhs_cols,
  637. const int32_t lhs_offset,
  638. const int32_t dst_offset,
  639. const int32_t activation_min,
  640. const int32_t activation_max,
  641. const int32_t row_address_offset,
  642. const int32_t lhs_cols_offset);
  643. /**
  644. * @brief General Matrix-multiplication function with per-channel requantization and int16 input (LHS) and output.
  645. * This function assumes:
  646. * - LHS input matrix NOT transposed (nt)
  647. * - RHS input matrix transposed (t)
  648. *
  649. * @note This operation also performs the broadcast bias addition before the requantization
  650. *
  651. * @param[in] lhs Pointer to the LHS input matrix
  652. * @param[in] rhs Pointer to the RHS input matrix
  653. * @param[in] bias_data Pointer to struct with bias vector. The length of this vector is equal to the number
  654. * of output columns (or RHS input rows). The vector can be int32 or int64 indicated by a
  655. * flag in the struct.
  656. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  657. * @param[in] dst_multipliers Pointer to the multipliers vector needed for the per-channel requantization.
  658. * The length of this vector is equal to the number of output columns (or RHS input
  659. * rows)
  660. * @param[in] dst_shifts Pointer to the shifts vector needed for the per-channel requantization. The length
  661. * of this vector is equal to the number of output columns (or RHS input rows)
  662. * @param[in] lhs_rows Number of LHS input rows
  663. * @param[in] rhs_rows Number of RHS input rows
  664. * @param[in] rhs_cols Number of LHS/RHS input columns
  665. * @param[in] activation_min Minimum value to clamp down the output. Range : int16
  666. * @param[in] activation_max Maximum value to clamp up the output. Range : int16
  667. *
  668. * @details MVE implementation only.
  669. *
  670. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code> or
  671. * <code>RISCV_NMSIS_NN_NO_IMPL_ERROR</code> if not for MVE
  672. *
  673. */
  674. riscv_nmsis_nn_status riscv_nn_mat_mult_nt_t_s16(const int16_t *lhs,
  675. const int8_t *rhs,
  676. const nmsis_nn_bias_data *bias_data,
  677. int16_t *dst,
  678. const int32_t *dst_multipliers,
  679. const int32_t *dst_shifts,
  680. const int32_t lhs_rows,
  681. const int32_t rhs_rows,
  682. const int32_t rhs_cols,
  683. const int32_t activation_min,
  684. const int32_t activation_max);
  685. /**
  686. * @brief General Matrix-multiplication function with int8 input and int32 output.
  687. * This function assumes:
  688. * - LHS input matrix NOT transposed (nt)
  689. * - RHS input matrix transposed (t)
  690. *
  691. * @note Dst/output buffer must be zeroed out before calling this function.
  692. *
  693. * @param[in] lhs Pointer to the LHS input matrix
  694. * @param[in] rhs Pointer to the RHS input matrix
  695. * @param[out] dst Pointer to the output matrix with "m" rows and "n" columns
  696. * @param[in] lhs_rows Number of LHS input rows
  697. * @param[in] rhs_rows Number of LHS input columns/RHS input rows
  698. * @param[in] rhs_cols Number of RHS input columns
  699. * @param[in] lhs_offset Offset to be applied to the LHS input value
  700. * @param[in] dst_idx_offset Offset between subsequent output results
  701. *
  702. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  703. *
  704. */
  705. riscv_nmsis_nn_status riscv_nn_mat_mult_nt_t_s8_s32(const int8_t *lhs,
  706. const int8_t *rhs,
  707. int32_t *dst,
  708. const int32_t lhs_rows,
  709. const int32_t rhs_rows,
  710. const int32_t rhs_cols,
  711. const int32_t lhs_offset,
  712. const int32_t dst_idx_offset);
  713. /**
  714. * @brief s4 Vector by Matrix (transposed) multiplication
  715. *
  716. * @param[in] lhs Input left-hand side vector
  717. * @param[in] packed_rhs Input right-hand side matrix (transposed)
  718. * @param[in] bias Input bias
  719. * @param[out] dst Output vector
  720. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side vector.
  721. * Range: -127 to 128
  722. * @param[in] dst_offset Offset to be added to the output values. Range: -127 to 128
  723. * @param[in] dst_multiplier Output multiplier
  724. * @param[in] dst_shift Output shift
  725. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  726. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  727. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  728. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  729. *
  730. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  731. *
  732. */
  733. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_s4(const int8_t *lhs,
  734. const int8_t *packed_rhs,
  735. const int32_t *bias,
  736. int8_t *dst,
  737. const int32_t lhs_offset,
  738. const int32_t dst_offset,
  739. const int32_t dst_multiplier,
  740. const int32_t dst_shift,
  741. const int32_t rhs_cols,
  742. const int32_t rhs_rows,
  743. const int32_t activation_min,
  744. const int32_t activation_max);
  745. /**
  746. * @brief s8 Vector by Matrix (transposed) multiplication
  747. *
  748. * @param[in] lhs Input left-hand side vector
  749. * @param[in] rhs Input right-hand side matrix (transposed)
  750. * @param[in] kernel_sum Kernel sums of the kernels (rhs). See riscv_vector_sum_s8 for more info.
  751. * @param[in] bias Input bias
  752. * @param[out] dst Output vector
  753. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side vector.
  754. * Range: -127 to 128
  755. * @param[in] dst_offset Offset to be added to the output values. Range: -127 to 128
  756. * @param[in] dst_multiplier Output multiplier
  757. * @param[in] dst_shift Output shift
  758. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  759. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  760. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  761. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  762. * @param[in] address_offset Memory position offset for dst. First output is stored at 'dst', the
  763. * second at 'dst + address_offset' and so on. Default value is typically 1.
  764. * @param[in] rhs_offset Offset to be added to the input values of the right-hand side vector.
  765. * Range: -127 to 128
  766. *
  767. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  768. *
  769. */
  770. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_s8(const int8_t *lhs,
  771. const int8_t *rhs,
  772. const int32_t *kernel_sum,
  773. const int32_t *bias,
  774. int8_t *dst,
  775. const int32_t lhs_offset,
  776. const int32_t dst_offset,
  777. const int32_t dst_multiplier,
  778. const int32_t dst_shift,
  779. const int32_t rhs_cols,
  780. const int32_t rhs_rows,
  781. const int32_t activation_min,
  782. const int32_t activation_max,
  783. const int32_t address_offset,
  784. const int32_t rhs_offset);
  785. /**
  786. * @brief s8 Vector by Matrix (transposed) multiplication using per channel quantization for output
  787. *
  788. * @param[in] lhs Input left-hand side vector
  789. * @param[in] rhs Input right-hand side matrix (transposed)
  790. * @param[in] kernel_sum Kernel sums of the kernels (rhs). See riscv_vector_sum_s8 for more info.
  791. * @param[in] bias Input bias
  792. * @param[out] dst Output vector
  793. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side vector.
  794. * Range: -127 to 128
  795. * @param[in] dst_offset Offset to be added to the output values. Range: -127 to 128
  796. * @param[in] dst_multiplier Output multipliers
  797. * @param[in] dst_shift Output shifts
  798. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  799. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  800. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  801. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  802. * @param[in] address_offset Memory position offset for dst. First output is stored at 'dst', the
  803. * second at 'dst + address_offset' and so on. Default value is typically 1.
  804. * @param[in] rhs_offset Offset to be added to the input values of the right-hand side vector.
  805. * Range: -127 to 128
  806. *
  807. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  808. *
  809. */
  810. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_per_ch_s8(const int8_t *lhs,
  811. const int8_t *rhs,
  812. const int32_t *kernel_sum,
  813. const int32_t *bias,
  814. int8_t *dst,
  815. const int32_t lhs_offset,
  816. const int32_t dst_offset,
  817. const int32_t *dst_multiplier,
  818. const int32_t *dst_shift,
  819. const int32_t rhs_cols,
  820. const int32_t rhs_rows,
  821. const int32_t activation_min,
  822. const int32_t activation_max,
  823. const int32_t address_offset,
  824. const int32_t rhs_offset);
  825. /**
  826. * @brief s16 Vector by s8 Matrix (transposed) multiplication
  827. *
  828. * @param[in] lhs Input left-hand side vector
  829. * @param[in] rhs Input right-hand side matrix (transposed)
  830. * @param[in] bias Input bias
  831. * @param[out] dst Output vector
  832. * @param[in] dst_multiplier Output multiplier
  833. * @param[in] dst_shift Output shift
  834. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  835. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  836. * @param[in] activation_min Minimum value to clamp the output to. Range: int16
  837. * @param[in] activation_max Maximum value to clamp the output to. Range: int16
  838. *
  839. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  840. *
  841. */
  842. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_s16(const int16_t *lhs,
  843. const int8_t *rhs,
  844. const int64_t *bias,
  845. int16_t *dst,
  846. const int32_t dst_multiplier,
  847. const int32_t dst_shift,
  848. const int32_t rhs_cols,
  849. const int32_t rhs_rows,
  850. const int32_t activation_min,
  851. const int32_t activation_max);
  852. /**
  853. * @brief s16 Vector by s16 Matrix (transposed) multiplication
  854. *
  855. * @param[in] lhs Input left-hand side vector
  856. * @param[in] rhs Input right-hand side matrix (transposed)
  857. * @param[in] bias Input bias
  858. * @param[out] dst Output vector
  859. * @param[in] dst_multiplier Output multiplier
  860. * @param[in] dst_shift Output shift
  861. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  862. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  863. * @param[in] activation_min Minimum value to clamp the output to. Range: int16
  864. * @param[in] activation_max Maximum value to clamp the output to. Range: int16
  865. *
  866. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  867. *
  868. */
  869. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_s16_s16(const int16_t *lhs,
  870. const int16_t *rhs,
  871. const int64_t *bias,
  872. int16_t *dst,
  873. const int32_t dst_multiplier,
  874. const int32_t dst_shift,
  875. const int32_t rhs_cols,
  876. const int32_t rhs_rows,
  877. const int32_t activation_min,
  878. const int32_t activation_max);
  879. /**
  880. * @brief s8 Vector by Matrix (transposed) multiplication with s16 output
  881. *
  882. * @param[in] lhs Input left-hand side vector
  883. * @param[in] rhs Input right-hand side matrix (transposed)
  884. * @param[out] dst Output vector
  885. * @param[in] lhs_offset Offset to be added to the input values of the left-hand side
  886. * vector. Range: -127 to 128
  887. * @param[in] scatter_offset Address offset for dst. First output is stored at 'dst', the
  888. * second at 'dst + scatter_offset' and so on.
  889. * @param[in] dst_multiplier Output multiplier
  890. * @param[in] dst_shift Output shift
  891. * @param[in] rhs_cols Number of columns in the right-hand side input matrix
  892. * @param[in] rhs_rows Number of rows in the right-hand side input matrix
  893. * @param[in] activation_min Minimum value to clamp the output to. Range: int16
  894. * @param[in] activation_max Maximum value to clamp the output to. Range: int16
  895. *
  896. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  897. *
  898. */
  899. riscv_nmsis_nn_status riscv_nn_vec_mat_mult_t_svdf_s8(const int8_t *lhs,
  900. const int8_t *rhs,
  901. int16_t *dst,
  902. const int32_t lhs_offset,
  903. const int32_t scatter_offset,
  904. const int32_t dst_multiplier,
  905. const int32_t dst_shift,
  906. const int32_t rhs_cols,
  907. const int32_t rhs_rows,
  908. const int32_t activation_min,
  909. const int32_t activation_max);
  910. /**
  911. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in padded cases where
  912. * the padding is -lhs_offset(Range: int8). Dimensions are the same for lhs and rhs.
  913. *
  914. * @param[in] lhs Input left-hand side matrix
  915. * @param[in] rhs Input right-hand side matrix (transposed)
  916. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  917. * @param[in] active_ch Subset of total_ch processed
  918. * @param[in] total_ch Number of channels in LHS/RHS
  919. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels
  920. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels
  921. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  922. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  923. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  924. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  925. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels
  926. * @param[in] out Output pointer
  927. *
  928. * @return The function returns one of the two
  929. * - Updated output pointer if an implementation is available
  930. * - NULL if no implementation is available.
  931. *
  932. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  933. * out for the following.
  934. * - Output shift
  935. * - Output multiplier
  936. * - Output bias
  937. * - rhs
  938. */
  939. riscv_nmsis_nn_status riscv_nn_depthwise_conv_nt_t_padded_s8(const int8_t *lhs,
  940. const int8_t *rhs,
  941. const int32_t lhs_offset,
  942. const int32_t active_ch,
  943. const int32_t total_ch,
  944. const int32_t *out_shift,
  945. const int32_t *out_mult,
  946. const int32_t out_offset,
  947. const int32_t activation_min,
  948. const int32_t activation_max,
  949. const uint16_t row_x_col,
  950. const int32_t *const output_bias,
  951. int8_t *out);
  952. /**
  953. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in non-padded cases.
  954. * Dimensions are the same for lhs and rhs.
  955. *
  956. * @param[in] lhs Input left-hand side matrix
  957. * @param[in] rhs Input right-hand side matrix (transposed)
  958. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  959. * @param[in] active_ch Subset of total_ch processed
  960. * @param[in] total_ch Number of channels in LHS/RHS
  961. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels.
  962. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels.
  963. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  964. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  965. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  966. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  967. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels.
  968. * @param[in] out Output pointer
  969. *
  970. * @return The function returns one of the two
  971. * - Updated output pointer if an implementation is available
  972. * - NULL if no implementation is available.
  973. *
  974. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  975. * out for the following.
  976. * - Output shift
  977. * - Output multiplier
  978. * - Output bias
  979. * - rhs
  980. */
  981. riscv_nmsis_nn_status riscv_nn_depthwise_conv_nt_t_s8(const int8_t *lhs,
  982. const int8_t *rhs,
  983. const int32_t lhs_offset,
  984. const int32_t active_ch,
  985. const int32_t total_ch,
  986. const int32_t *out_shift,
  987. const int32_t *out_mult,
  988. const int32_t out_offset,
  989. const int32_t activation_min,
  990. const int32_t activation_max,
  991. const uint16_t row_x_col,
  992. const int32_t *const output_bias,
  993. int8_t *out);
  994. /**
  995. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in non-padded cases. rhs
  996. * consists of packed int4 data. Dimensions are the same for lhs and rhs.
  997. *
  998. * @param[in] lhs Input left-hand side matrix
  999. * @param[in] rhs Input right-hand side matrix (transposed). Consists of int4 data packed in an int8
  1000. * buffer.
  1001. * @param[in] lhs_offset LHS matrix offset(input offset). Range: -127 to 128
  1002. * @param[in] active_ch Subset of total_ch processed
  1003. * @param[in] total_ch Number of channels in LHS/RHS
  1004. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels.
  1005. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels.
  1006. * @param[in] out_offset Offset to be added to the output values. Range: -127 to 128
  1007. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  1008. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  1009. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  1010. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels.
  1011. * @param[in] out Output pointer
  1012. *
  1013. * @return The function returns one of the two
  1014. * - Updated output pointer if an implementation is available
  1015. * - NULL if no implementation is available.
  1016. *
  1017. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  1018. * out for the following.
  1019. * - Output shift
  1020. * - Output multiplier
  1021. * - Output bias
  1022. * - rhs
  1023. */
  1024. riscv_nmsis_nn_status riscv_nn_depthwise_conv_nt_t_s4(const int8_t *lhs,
  1025. const int8_t *rhs,
  1026. const int32_t lhs_offset,
  1027. const int32_t active_ch,
  1028. const int32_t total_ch,
  1029. const int32_t *out_shift,
  1030. const int32_t *out_mult,
  1031. const int32_t out_offset,
  1032. const int32_t activation_min,
  1033. const int32_t activation_max,
  1034. const uint16_t row_x_col,
  1035. const int32_t *const output_bias,
  1036. int8_t *out);
  1037. /**
  1038. * @brief Depthwise convolution of transposed rhs matrix with 4 lhs matrices. To be used in non-padded cases.
  1039. * Dimensions are the same for lhs and rhs.
  1040. *
  1041. * @param[in] lhs Input left-hand side matrix
  1042. * @param[in] rhs Input right-hand side matrix (transposed)
  1043. * @param[in] num_ch Number of channels in LHS/RHS
  1044. * @param[in] out_shift Per channel output shift. Length of vector is equal to number of channels.
  1045. * @param[in] out_mult Per channel output multiplier. Length of vector is equal to number of channels.
  1046. * @param[in] activation_min Minimum value to clamp the output to. Range: int8
  1047. * @param[in] activation_max Maximum value to clamp the output to. Range: int8
  1048. * @param[in] row_x_col (row_dimension * col_dimension) of LHS/RHS matrix
  1049. * @param[in] output_bias Per channel output bias. Length of vector is equal to number of channels.
  1050. * @param[in] out Output pointer
  1051. *
  1052. * @return The function returns one of the two
  1053. * - Updated output pointer if an implementation is available
  1054. * - NULL if no implementation is available.
  1055. *
  1056. * @note If number of channels is not a multiple of 4, upto 3 elements outside the boundary will be read
  1057. * out for the following.
  1058. * - Output shift
  1059. * - Output multiplier
  1060. * - Output bias
  1061. * - rhs
  1062. */
  1063. int16_t *riscv_nn_depthwise_conv_nt_t_s16(const int16_t *lhs,
  1064. const int8_t *rhs,
  1065. const uint16_t num_ch,
  1066. const int32_t *out_shift,
  1067. const int32_t *out_mult,
  1068. const int32_t activation_min,
  1069. const int32_t activation_max,
  1070. const uint16_t row_x_col,
  1071. const int64_t *const output_bias,
  1072. int16_t *out);
  1073. /**
  1074. * @brief Row of s8 scalars multiplicated with a s8 matrix ad accumulated into a s32 rolling scratch buffer.
  1075. * Helpfunction for transposed convolution.
  1076. *
  1077. * @param[in] lhs Input left-hand side scalars
  1078. * @param[in] rhs Input right-hand side matrix
  1079. * @param[out] output_start Output buffer start
  1080. * @param[in] output_index Output buffer current index
  1081. * @param[in] output_max Output buffer size
  1082. * @param[in] rhs_rows Number of rows in rhs matrix
  1083. * @param[in] rhs_cols Number of columns in rhs matrix
  1084. * @param[in] input_channels Number of input channels
  1085. * @param[in] output_channels Number of output channels
  1086. * @param[in] lhs_offset Offset added to lhs before multiplication
  1087. * @param[in] row_offset Address offset between each row of data output
  1088. * @param[in] input_x Length of lhs scalar row.
  1089. * @param[in] stride_x Address offset between each scalar-matrix multiplication result.
  1090. * @param[in] skip_row_top Skip rows on top of the filter, used for padding.
  1091. * @param[in] skip_row_bottom Skip rows in the bottom of the filter, used for padding.
  1092. *
  1093. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  1094. *
  1095. * @note Rolling buffer refers to how the function wraps around the scratch buffer, e.g. it starts writing at
  1096. * [output_start + output_index], writes to [output_start + output_max] and then continues at [output_start] again.
  1097. */
  1098. riscv_nmsis_nn_status riscv_nn_transpose_conv_row_s8_s32(const int8_t *lhs,
  1099. const int8_t *rhs,
  1100. int32_t *output_start,
  1101. const int32_t output_index,
  1102. const int32_t output_max,
  1103. const int32_t rhs_rows,
  1104. const int32_t rhs_cols,
  1105. const int32_t input_channels,
  1106. const int32_t output_channels,
  1107. const int32_t lhs_offset,
  1108. const int32_t row_offset,
  1109. const int32_t input_x,
  1110. const int32_t stride_x,
  1111. const int32_t skip_row_top,
  1112. const int32_t skip_row_bottom);
  1113. /**
  1114. *@brief Matrix-multiplication function for convolution with reordered columns
  1115. *@param[in] pA pointer to operand A
  1116. *@param[in] pInBuffer pointer to operand B, always conssists of 2 vectors
  1117. *@param[in] ch_im_out numRow of A
  1118. *@param[in] numCol_A numCol of A
  1119. *@param[in] bias_shift amount of left-shift for bias
  1120. *@param[in] out_shift amount of right-shift for output
  1121. *@param[in] bias the bias
  1122. *@param[in,out] pOut pointer to output
  1123. *@return The function returns the incremented output pointer
  1124. *
  1125. *@details This function assumes that data in pInBuffer are reordered
  1126. */
  1127. q7_t *riscv_nn_mat_mult_kernel_q7_q15_reordered(const q7_t *pA,
  1128. const q15_t *pInBuffer,
  1129. const uint16_t ch_im_out,
  1130. const uint16_t numCol_A,
  1131. const uint16_t bias_shift,
  1132. const uint16_t out_shift,
  1133. const q7_t *bias,
  1134. q7_t *pOut);
  1135. q7_t *riscv_nn_mat_mult_kernel_q7_reordered(const q7_t * pA,
  1136. const q7_t * pInBuffer,
  1137. const uint16_t ch_im_out,
  1138. const uint16_t numCol_A,
  1139. const uint16_t bias_shift,
  1140. const uint16_t out_shift,
  1141. const q7_t * bias,
  1142. q7_t * pOut);
  1143. #define __SIMD32_TYPE int32_t
  1144. #define __SIMD32(addr) (*(__SIMD32_TYPE **) & (addr))
  1145. #define __SIMD32_CONST(addr) ( (__SIMD32_TYPE * ) (addr))
  1146. #define _SIMD32_OFFSET(addr) (*(__SIMD32_TYPE * ) (addr))
  1147. #define __SIMD64(addr) (*( int64_t **) & (addr))
  1148. /**
  1149. @brief Read 2 s16 elements and post increment pointer.
  1150. @param[in] in_q15 Pointer to pointer that holds address of input.
  1151. @return q31 value
  1152. */
  1153. __STATIC_FORCEINLINE int32_t riscv_nn_read_q15x2_ia(const int16_t **in_q15)
  1154. {
  1155. int32_t val;
  1156. #ifdef __RISCV_FEATURE_UNALIGNED
  1157. memcpy(&val, *in_q15, 4);
  1158. #else
  1159. __ASM volatile ("lw %0, 0(%1)" : "=r" (val) : "r" (*in_q15));
  1160. #endif
  1161. *in_q15 += 2;
  1162. return (val);
  1163. }
  1164. /**
  1165. @brief Read 4 s8 from s8 pointer and post increment pointer.
  1166. @param[in] in_s8 Pointer to pointer that holds address of input.
  1167. @return q31 value
  1168. */
  1169. __STATIC_FORCEINLINE int32_t riscv_nn_read_s8x4_ia(const int8_t **in_s8)
  1170. {
  1171. int32_t val;
  1172. #ifdef __RISCV_FEATURE_UNALIGNED
  1173. memcpy (&val, *in_s8, 4);
  1174. #else
  1175. val = __LW((int8_t *)(* in_s8));
  1176. #endif
  1177. *in_s8 += 4;
  1178. return (val);
  1179. }
  1180. /**
  1181. @brief Read 2 s8 from s8 pointer and post increment pointer.
  1182. @param[in] in_s8 Pointer to pointer that holds address of input.
  1183. @return q31 value
  1184. */
  1185. __STATIC_FORCEINLINE int32_t riscv_nn_read_s8x2_ia(const int8_t **in_s8)
  1186. {
  1187. int32_t val;
  1188. memcpy(&val, *in_s8, 2);
  1189. *in_s8 += 2;
  1190. return (val);
  1191. }
  1192. __STATIC_FORCEINLINE int64_t riscv_nn_read_s8x8_ia(const int8_t **in_s8)
  1193. {
  1194. int64_t val;
  1195. #ifndef __RISCV_FEATURE_UNALIGNED
  1196. #if __RISCV_XLEN == 64
  1197. val = __LD((int8_t *)(*in_s8));
  1198. #else
  1199. val = *((int64_t *)(*in_s8));
  1200. #endif /* __RISCV_XLEN == 64 */
  1201. #else
  1202. memcpy(&val, *in_s8, 8);
  1203. #endif
  1204. *in_s8 += 8;
  1205. return (val);
  1206. }
  1207. /**
  1208. @brief Read 2 int16 values from int16 pointer.
  1209. @param[in] in pointer to address of input.
  1210. @return s32 value
  1211. */
  1212. __STATIC_FORCEINLINE int32_t riscv_nn_read_s16x2(const int16_t *in)
  1213. {
  1214. int32_t val;
  1215. #ifdef __RISCV_FEATURE_UNALIGNED
  1216. memcpy (&val, in, 4);
  1217. #else
  1218. val = __LW((int16_t *)in);
  1219. #endif
  1220. return (val);
  1221. }
  1222. /**
  1223. @brief Read 2 int16 values from int16 pointer and increment pointer afterwards.
  1224. @param[in] in points to input value
  1225. @return int64 value
  1226. */
  1227. __STATIC_FORCEINLINE int32_t riscv_nn_read_s16x2_ia(const int16_t ** in)
  1228. {
  1229. int64_t val;
  1230. val = riscv_nn_read_s16x2(*in);
  1231. *in += 2;
  1232. return (val);
  1233. }
  1234. /**
  1235. @brief Write 2 int16 values to int16 pointer.
  1236. @param[in] in points to input value
  1237. @param[in] value int32 value
  1238. @return none
  1239. */
  1240. __STATIC_FORCEINLINE void riscv_nn_write_s16x2(int16_t * in, int32_t value)
  1241. {
  1242. #ifdef __RISCV_FEATURE_UNALIGNED
  1243. memcpy (in, &value, 4);
  1244. #else
  1245. __SW(in, value);
  1246. #endif
  1247. }
  1248. /**
  1249. @brief Write 2 int16 values to int16 pointer and increment pointer afterwards.
  1250. @param[in] in points to input value
  1251. @param[in] value int32 value
  1252. @return none
  1253. */
  1254. __STATIC_FORCEINLINE void riscv_nn_write_s16x2_ia(int16_t ** in, int32_t value)
  1255. {
  1256. riscv_nn_write_s16x2(*in, value);
  1257. *in += 2;
  1258. }
  1259. /**
  1260. @brief Read 4 int16 values from int16 pointer.
  1261. @param[in] in pointer to address of input.
  1262. @return s32 value
  1263. */
  1264. __STATIC_FORCEINLINE int64_t riscv_nn_read_s16x4(const int16_t *in)
  1265. {
  1266. int64_t val;
  1267. #ifndef __RISCV_FEATURE_UNALIGNED
  1268. #if __RISCV_XLEN == 64
  1269. val = __LD((int16_t *)in);
  1270. #else
  1271. val = *((int64_t *)in);
  1272. #endif /* __RISCV_XLEN == 64 */
  1273. #else
  1274. memcpy((void *)(&val), (void *)(in), 8);
  1275. #endif
  1276. return (val);
  1277. }
  1278. /**
  1279. @brief Read 4 int16 values from int16 pointer and increment pointer afterwards.
  1280. @param[in] in points to input value
  1281. @return S64 value
  1282. */
  1283. __STATIC_FORCEINLINE int64_t riscv_nn_read_s16x4_ia(const int16_t ** in)
  1284. {
  1285. int64_t val;
  1286. val = riscv_nn_read_s16x4(*in);
  1287. *in += 4;
  1288. return (val);
  1289. }
  1290. /**
  1291. @brief Write 4 int16 values to int16 pointer.
  1292. @param[in] in points to input value
  1293. @param[in] value S64 value
  1294. @return none
  1295. */
  1296. __STATIC_FORCEINLINE void riscv_nn_write_s16x4(int16_t * in, int64_t value)
  1297. {
  1298. #ifndef __RISCV_FEATURE_UNALIGNED
  1299. #if __RISCV_XLEN == 64
  1300. __SD(in, value);
  1301. #else
  1302. *((int64_t *)in) = value;
  1303. #endif
  1304. #else
  1305. memcpy((void *)(in), (void *)(&value), 8);
  1306. #endif
  1307. }
  1308. /**
  1309. @brief Write 4 int16 values to int16 pointer and increment pointer afterwards.
  1310. @param[in] in points to input value
  1311. @param[in] value int64_t value
  1312. @return none
  1313. */
  1314. __STATIC_FORCEINLINE void riscv_nn_write_s16x4_ia(int16_t ** in, int64_t value)
  1315. {
  1316. riscv_nn_write_s16x4(*in, value);
  1317. *in += 4;
  1318. }
  1319. /**
  1320. @brief Read 4 s8 values.
  1321. @param[in] in_s8 pointer to address of input.
  1322. @return s32 value
  1323. */
  1324. __STATIC_FORCEINLINE int32_t riscv_nn_read_s8x4(const int8_t *in_s8)
  1325. {
  1326. int32_t val;
  1327. #ifdef __RISCV_FEATURE_UNALIGNED
  1328. memcpy(&val, in_s8, 4);
  1329. #else
  1330. val = __LW((int8_t *)(in_s8));
  1331. #endif
  1332. return (val);
  1333. }
  1334. /**
  1335. @brief Read 2 s8 values.
  1336. @param[in] in_s8 pointer to address of input.
  1337. @return s32 value
  1338. */
  1339. __STATIC_FORCEINLINE int32_t riscv_nn_read_s8x2(const int8_t *in_s8)
  1340. {
  1341. int32_t val;
  1342. memcpy(&val, in_s8, 2);
  1343. return (val);
  1344. }
  1345. /**
  1346. @brief Write four s8 to s8 pointer and increment pointer afterwards.
  1347. @param[in] in Double pointer to input value
  1348. @param[in] value Four bytes to copy
  1349. */
  1350. __STATIC_FORCEINLINE void riscv_nn_write_s8x4_ia(int8_t **in, int32_t value)
  1351. {
  1352. #ifdef __RISCV_FEATURE_UNALIGNED
  1353. memcpy(*in, &value, 4);
  1354. #else
  1355. __SW(*in, value);
  1356. #endif
  1357. *in += 4;
  1358. }
  1359. /**
  1360. @brief Read 4 Q15 from Q15 pointer.
  1361. @param[in] pQ15 points to input value
  1362. @return Q63 value
  1363. */
  1364. __STATIC_FORCEINLINE q63_t riscv_nn_read_q15x4 (
  1365. q15_t const * pQ15)
  1366. {
  1367. q63_t val;
  1368. #ifndef __RISCV_FEATURE_UNALIGNED
  1369. #if __RISCV_XLEN == 64
  1370. val = __LD((q15_t *)pQ15);
  1371. #else
  1372. val = *((q63_t *)pQ15);
  1373. #endif /* __RISCV_XLEN == 64 */
  1374. #else
  1375. memcpy((void *)(&val), (void *)(pQ15), 8);
  1376. #endif
  1377. return (val);
  1378. }
  1379. /**
  1380. @brief Read 4 Q15 from Q15 pointer and increment pointer afterwards.
  1381. @param[in] pQ15 points to input value
  1382. @return Q63 value
  1383. */
  1384. __STATIC_FORCEINLINE q63_t riscv_nn_read_q15x4_ia (
  1385. q15_t ** pQ15)
  1386. {
  1387. q63_t val;
  1388. val = riscv_nn_read_q15x4(*pQ15);
  1389. *pQ15 += 4;
  1390. return (val);
  1391. }
  1392. /**
  1393. @brief Write 4 Q15 to Q15 pointer.
  1394. @param[in] pQ15 points to input value
  1395. @param[in] value Q31 value
  1396. @return none
  1397. */
  1398. __STATIC_FORCEINLINE void riscv_nn_write_q15x4 (
  1399. q15_t * pQ15,
  1400. q63_t value)
  1401. {
  1402. #ifndef __RISCV_FEATURE_UNALIGNED
  1403. #if __RISCV_XLEN == 64
  1404. __SD(pQ15, value);
  1405. #else
  1406. *((q63_t *)pQ15) = value;
  1407. #endif
  1408. #else
  1409. memcpy((void *)(pQ15), (void *)(&value), 8);
  1410. #endif
  1411. }
  1412. /**
  1413. @brief Write 4 Q15 to Q15 pointer and increment pointer afterwards.
  1414. @param[in] pQ15 points to input value
  1415. @param[in] value Q31 value
  1416. @return none
  1417. */
  1418. __STATIC_FORCEINLINE void riscv_nn_write_q15x4_ia (
  1419. q15_t ** pQ15,
  1420. q63_t value)
  1421. {
  1422. riscv_nn_write_q15x4(*pQ15, value);
  1423. *pQ15 += 4;
  1424. }
  1425. /**
  1426. @brief Read 4 q7 from q7 pointer and post increment pointer.
  1427. @param[in] in_q7 Pointer to pointer that holds address of input.
  1428. @return q31 value
  1429. */
  1430. __STATIC_FORCEINLINE q31_t riscv_nn_read_q7x4_ia(const q7_t **in_q7)
  1431. {
  1432. q31_t val;
  1433. #ifdef __RISCV_FEATURE_UNALIGNED
  1434. memcpy (&val, *in_q7, 4);
  1435. #else
  1436. __ASM volatile ("lw %0, 0(%1)" : "=r" (val) : "r" (*in_q7));
  1437. #endif
  1438. *in_q7 += 4;
  1439. return (val);
  1440. }
  1441. /**
  1442. @brief Read 2 q15 from q15 pointer.
  1443. @param[in] in_q15 pointer to address of input.
  1444. @return q31 value
  1445. */
  1446. __STATIC_FORCEINLINE q31_t riscv_nn_read_q15x2(const q15_t *in_q15)
  1447. {
  1448. q31_t val;
  1449. #ifdef __RISCV_FEATURE_UNALIGNED
  1450. memcpy (&val, in_q15, 4);
  1451. #else
  1452. __ASM volatile ("lw %0, 0(%1)" : "=r" (val) : "r" (in_q15));
  1453. #endif
  1454. return (val);
  1455. }
  1456. /**
  1457. @brief Read 4 q7 values.
  1458. @param[in] in_q7 pointer to address of input.
  1459. @return q31 value
  1460. */
  1461. __STATIC_FORCEINLINE q31_t riscv_nn_read_q7x4(const q7_t *in_q7)
  1462. {
  1463. q31_t val;
  1464. #ifdef __RISCV_FEATURE_UNALIGNED
  1465. memcpy (&val, in_q7, 4);
  1466. #else
  1467. __ASM volatile ("lw %0, 0(%1)" : "=r" (val) : "r" (in_q7));
  1468. #endif
  1469. return (val);
  1470. }
  1471. /**
  1472. @brief Read 8 Q7 from Q7 pointer.
  1473. @param[in] pQ7 points to input value
  1474. @return Q63 value
  1475. */
  1476. __STATIC_FORCEINLINE q63_t riscv_nn_read_q7x8 (
  1477. q7_t const * pQ7)
  1478. {
  1479. q63_t val;
  1480. #ifndef __RISCV_FEATURE_UNALIGNED
  1481. #if __RISCV_XLEN == 64
  1482. val = __LD((q7_t *)pQ7);
  1483. #else
  1484. val = *((q63_t *)pQ7);
  1485. #endif
  1486. #else
  1487. memcpy((void *)(&val), (void *)pQ7, 8);
  1488. #endif
  1489. return val;
  1490. }
  1491. /**
  1492. @brief Read 8 Q7 from Q7 pointer and increment pointer afterwards.
  1493. @param[in] pQ7 points to input value
  1494. @return Q63 value
  1495. */
  1496. __STATIC_FORCEINLINE q63_t riscv_nn_read_q7x8_ia (
  1497. q7_t ** pQ7)
  1498. {
  1499. q63_t val;
  1500. val = riscv_nn_read_q7x8(*pQ7);
  1501. *pQ7 += 8;
  1502. return val;
  1503. }
  1504. /**
  1505. @brief Write four q7 to q7 pointer and increment pointer afterwards.
  1506. @param[in] in Double pointer to input value
  1507. @param[in] value Four bytes to copy
  1508. */
  1509. __STATIC_FORCEINLINE void riscv_nn_write_q7x4_ia(q7_t **in, q31_t value)
  1510. {
  1511. memcpy(*in, &value, 4);
  1512. *in += 4;
  1513. }
  1514. /**
  1515. @brief Write 8 Q7 to Q7 pointer and increment pointer afterwards.
  1516. @param[in] pQ7 points to input value
  1517. @param[in] value Q63 value
  1518. @return none
  1519. */
  1520. __STATIC_FORCEINLINE void riscv_nn_write_q7x8_ia (
  1521. q7_t ** pQ7,
  1522. q63_t value)
  1523. {
  1524. #ifndef __RISCV_FEATURE_UNALIGNED
  1525. #if __RISCV_XLEN == 64
  1526. __SD(*pQ7,value);
  1527. #else
  1528. *((q63_t *)*pQ7) = value;
  1529. #endif
  1530. #else
  1531. memcpy((void *)(*pQ7), (void *)(&value), 8);
  1532. #endif
  1533. *pQ7 += 8;
  1534. }
  1535. /**
  1536. * @brief memset
  1537. * @param[in, out] dst Destination pointer
  1538. * @param[in] val Value to set
  1539. * @param[in] block_size Number of bytes to copy.
  1540. *
  1541. */
  1542. __STATIC_FORCEINLINE void riscv_memset_s8(int8_t *dst, const int8_t val, uint32_t block_size)
  1543. {
  1544. memset(dst, val, block_size);
  1545. }
  1546. #if defined(RISCV_MATH_DSP)
  1547. /**
  1548. * @brief read and expand one s4 word into two s8 words.
  1549. */
  1550. __STATIC_FORCEINLINE void read_and_pad_s4(const int8_t *source, int32_t *out1, int32_t *out2)
  1551. {
  1552. int16_t in = riscv_nn_read_s8x2(source);
  1553. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  1554. *out1 = __SXTB16_RORn(__SXTB16(inA << 4), 4);
  1555. *out2 = __SXTB16_RORn(__SXTB16(inA), 4);
  1556. }
  1557. /**
  1558. * @brief read and expand one s4 word into two s8 words.
  1559. * @details The s4 elements are not evenly aligned on the byte boundary, so 3 bytes need to be read instead of 2.
  1560. * In other words first nibble to read start at the middle of a byte.
  1561. * byte index, s4 element
  1562. * 0, s4_x
  1563. * 0, s4_0
  1564. * 1, s4_1
  1565. * 1, s4_2
  1566. * 2, s4_3
  1567. * 2, s4_x
  1568. */
  1569. __STATIC_FORCEINLINE void read_and_pad_s4_uneven(const int8_t *source, int32_t *out1, int32_t *out2)
  1570. {
  1571. int32_t inA1 = (source[0] & 0xFF) | ((source[1] & 0xFF) << 16);
  1572. int32_t inA2 = (source[1] & 0xFF) | ((source[2] & 0xFF) << 16);
  1573. *out1 = __SXTB16_RORn(__SXTB16(inA2 << 4), 4);
  1574. *out2 = __SXTB16_RORn(__SXTB16(inA1), 4);
  1575. }
  1576. /**
  1577. * @brief read and expand one s4 word into two s16 words with ordering.
  1578. */
  1579. __STATIC_FORCEINLINE void read_and_pad_s4_ordered(const int8_t *source, int32_t *out1, int32_t *out2)
  1580. {
  1581. int16_t in = riscv_nn_read_s8x2(source);
  1582. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  1583. int32_t inAbuf1 = __SXTB16_RORn(__SXTB16(inA), 4);
  1584. int32_t inAbuf2 = __SXTB16_RORn(__SXTB16(inA << 4), 4);
  1585. *out2 = (int32_t)(__PKHTB(inAbuf1, inAbuf2, 16));
  1586. *out1 = (int32_t)(__PKHBT(inAbuf2, inAbuf1, 16));
  1587. }
  1588. /**
  1589. * @brief read and expand two s8 word into four s16 words with ordering.
  1590. */
  1591. #if __RISCV_XLEN == 64
  1592. __STATIC_FORCEINLINE const int8_t *read_and_pad64(const int8_t *source, int64_t *out1, int64_t *out2)
  1593. {
  1594. int64_t inA = riscv_nn_read_s8x8_ia(&source);
  1595. int64_t tmp1 = __SXTB16(__ROR64((uint64_t)inA, 8)); // __RV_SUNPKD820
  1596. int64_t tmp2 = __SXTB16(inA);
  1597. int64_t inAbuf1 = (int64_t)(__PKHBT64(tmp2, tmp1, 16));
  1598. int64_t inAbuf2 = (int64_t)(__PKHTB64(tmp1, tmp2, 16));
  1599. *out2 = __RV_PKTT32(inAbuf2, inAbuf1);
  1600. *out1 = __RV_PKBB32(inAbuf2, inAbuf1);
  1601. return source;
  1602. }
  1603. #else
  1604. #if defined (NUCLEI_DSP_N2)
  1605. __STATIC_FORCEINLINE const int8_t *read_and_pad64(const int8_t *source, int64_t *out1, int64_t *out2)
  1606. {
  1607. int64_t inA = riscv_nn_read_s8x8_ia(&source);
  1608. int64_t tmp1 = __RV_DSUNPKD820(__ROR64((uint64_t)inA, 8));
  1609. int64_t tmp2 = __RV_DSUNPKD820(inA);
  1610. int64_t inAbuf1 = (int64_t)(__PKHBT64(tmp2, tmp1, 16));
  1611. int64_t inAbuf2 = (int64_t)(__PKHTB64(tmp1, tmp2, 16));
  1612. *out1 = __RV_DPKBB32(inAbuf2, inAbuf1);
  1613. *out2 = __RV_DPKTT32(inAbuf2, inAbuf1);
  1614. return source;
  1615. }
  1616. #endif /* defined (NUCLEI_DSP_N2) */
  1617. #endif /* __RISCV_XLEN == 64 */
  1618. /**
  1619. * @brief read and expand one s8 word into two s16 words with ordering.
  1620. */
  1621. __STATIC_FORCEINLINE const int8_t *read_and_pad(const int8_t *source, int32_t *out1, int32_t *out2)
  1622. {
  1623. int32_t inA = riscv_nn_read_s8x4_ia(&source);
  1624. int32_t inAbuf1 = __SXTB16_RORn((uint32_t)inA, 8);
  1625. int32_t inAbuf2 = __SXTB16(inA);
  1626. *out2 = (int32_t)(__NN_PKHTB(inAbuf1, inAbuf2, 16));
  1627. *out1 = (int32_t)(__NN_PKHBT(inAbuf2, inAbuf1, 16));
  1628. return source;
  1629. }
  1630. /**
  1631. * @brief read and expand one s8 word into two s16 words with ordering and addition.
  1632. */
  1633. __STATIC_FORCEINLINE void read_pad_and_add_s8(const int8_t *source, int32_t *out1, int32_t *out2, const uint32_t add)
  1634. {
  1635. int32_t inA = riscv_nn_read_s8x4(source);
  1636. int32_t inAbuf1 = __SXTAB16_RORn(add, (uint32_t)inA, 8);
  1637. int32_t inAbuf2 = __SXTAB16(add, inA);
  1638. #ifndef RISCV_MATH_BIG_ENDIAN
  1639. *out2 = (int32_t)(__PKHTB(inAbuf1, inAbuf2, 16));
  1640. *out1 = (int32_t)(__PKHBT(inAbuf2, inAbuf1, 16));
  1641. #else
  1642. *out1 = (int32_t)(__PKHTB(inAbuf1, inAbuf2, 16));
  1643. *out2 = (int32_t)(__PKHBT(inAbuf2, inAbuf1, 16));
  1644. #endif
  1645. }
  1646. /**
  1647. * @brief read and expand two bytes into one word with ordering.
  1648. */
  1649. __STATIC_FORCEINLINE void read_and_pad_s8x2(const int8_t *source, int32_t *out)
  1650. {
  1651. int16_t in = riscv_nn_read_s8x2(source);
  1652. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  1653. *out = __SXTB16(inA);
  1654. }
  1655. /**
  1656. * @brief read and expand two bytes into one word with ordering and addition.
  1657. */
  1658. __STATIC_FORCEINLINE void read_pad_and_add_s8x2(const int8_t *source, int32_t *out, const uint32_t add)
  1659. {
  1660. int16_t in = riscv_nn_read_s8x2(source);
  1661. int32_t inA = (in & 0x00FF) | ((in & 0xFF00) << 8);
  1662. *out = __SXTAB16(add, inA);
  1663. }
  1664. /**
  1665. * @brief read and expand two s8 word into four s16 words with no additional ordering.
  1666. */
  1667. #if __RISCV_XLEN == 64
  1668. __STATIC_FORCEINLINE const int8_t *read_and_pad_reordered64(const int8_t *source, int64_t *out1, int64_t *out2)
  1669. {
  1670. int64_t inA = riscv_nn_read_s8x8_ia(&source);
  1671. int64_t tmp2 = __RV_SUNPKD820(__ROR64((uint64_t)inA, 8));
  1672. int64_t tmp1 = __RV_SUNPKD820(inA);
  1673. *out1 = __RV_PKBB32(tmp2, tmp1);
  1674. *out2 = __RV_PKTT32(tmp2, tmp1);
  1675. return source;
  1676. }
  1677. #else
  1678. #if defined (NUCLEI_DSP_N2)
  1679. __STATIC_FORCEINLINE const int8_t *read_and_pad_reordered64(const int8_t *source, int64_t *out1, int64_t *out2)
  1680. {
  1681. int64_t inA = riscv_nn_read_s8x8_ia(&source);
  1682. int64_t tmp2 = __RV_DSUNPKD820(__ROR64((uint64_t)inA, 8));
  1683. int64_t tmp1 = __RV_DSUNPKD820(inA);
  1684. *out1 = __RV_DPKBB32(tmp2, tmp1);
  1685. *out2 = __RV_DPKTT32(tmp2, tmp1);
  1686. return source;
  1687. }
  1688. #endif /* defined (NUCLEI_DSP_N2) */
  1689. #endif /* __RISCV_XLEN == 64 */
  1690. /**
  1691. * @brief read and expand one s8 word into two s16 words with no additional ordering.
  1692. */
  1693. __STATIC_FORCEINLINE const int8_t *read_and_pad_reordered(const int8_t *source, int32_t *out1, int32_t *out2)
  1694. {
  1695. int32_t inA = riscv_nn_read_s8x4_ia(&source);
  1696. *out2 = __SXTB16(__ROR((uint32_t)inA, 8));
  1697. *out1 = __SXTB16(inA);
  1698. return source;
  1699. }
  1700. /**
  1701. * @brief read and expand one q7 word into two q15 words with reordering and add an offset
  1702. */
  1703. __STATIC_FORCEINLINE const q7_t *
  1704. read_and_pad_reordered_with_offset(const q7_t *source, q31_t *out1, q31_t *out2, q31_t offset)
  1705. {
  1706. q31_t inA = riscv_nn_read_q7x4_ia(&source);
  1707. *out2 = __SXTB16(__ROR((uint32_t)inA, 8));
  1708. *out1 = __SXTB16(inA);
  1709. *out1 = __NN_QADD16(*out1, offset);
  1710. *out2 = __NN_QADD16(*out2, offset);
  1711. return source;
  1712. }
  1713. #endif
  1714. /**
  1715. * @brief Matrix-multiplication function for convolution with per-channel requantization and 4 bit weights.
  1716. * @param[in] input_a pointer to operand A, int8 packed with 2x int4.
  1717. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  1718. * @param[in] output_ch number of rows of A
  1719. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  1720. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  1721. * @param[in] out_offset output tensor offset.
  1722. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  1723. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  1724. * @param[in] num_col_a number of columns of A
  1725. * @param[in] output_bias per output channel bias. Range : int32
  1726. * @param[in,out] out_0 pointer to output
  1727. * @return The function returns one of the two
  1728. * 1. The incremented output pointer for a successful operation or
  1729. * 2. NULL if implementation is not available.
  1730. *
  1731. * @details This function does the matrix multiplication of weight matrix for all output channels
  1732. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  1733. * clamped in the range provided by activation min and max.
  1734. * Supported framework: TensorFlow Lite micro.
  1735. */
  1736. int8_t *riscv_nn_mat_mult_kernel_s4_s16(const int8_t *input_a,
  1737. const int16_t *input_b,
  1738. const uint16_t output_ch,
  1739. const int32_t *out_shift,
  1740. const int32_t *out_mult,
  1741. const int32_t out_offset,
  1742. const int32_t activation_min,
  1743. const int32_t activation_max,
  1744. const int32_t num_col_a,
  1745. const int32_t *const output_bias,
  1746. int8_t *out_0);
  1747. /**
  1748. * @defgroup NNBasicMath Basic Math Functions for Neural Network Computation
  1749. *
  1750. * Basic Math Functions for Neural Network Computation
  1751. *
  1752. */
  1753. /**
  1754. * @brief q7 vector multiplication with variable output shifts
  1755. * @param[in] *pSrcA pointer to the first input vector
  1756. * @param[in] *pSrcB pointer to the second input vector
  1757. * @param[out] *pDst pointer to the output vector
  1758. * @param[in] out_shift amount of right-shift for output
  1759. * @param[in] blockSize number of samples in each vector
  1760. * @return none.
  1761. *
  1762. * <b>Scaling and Overflow Behavior:</b>
  1763. * \par
  1764. * The function uses saturating arithmetic.
  1765. * Results outside of the allowable q15 range [0x8000 0x7FFF] will be saturated.
  1766. */
  1767. void riscv_nn_mult_q15(q15_t *pSrcA, q15_t *pSrcB, q15_t *pDst, const uint16_t out_shift, uint32_t blockSize);
  1768. /**
  1769. * @brief q7 vector multiplication with variable output shifts
  1770. * @param[in] *pSrcA pointer to the first input vector
  1771. * @param[in] *pSrcB pointer to the second input vector
  1772. * @param[out] *pDst pointer to the output vector
  1773. * @param[in] out_shift amount of right-shift for output
  1774. * @param[in] blockSize number of samples in each vector
  1775. * @return none.
  1776. *
  1777. * <b>Scaling and Overflow Behavior:</b>
  1778. * \par
  1779. * The function uses saturating arithmetic.
  1780. * Results outside of the allowable q7 range [0x80 0x7F] will be saturated.
  1781. */
  1782. void riscv_nn_mult_q7(q7_t *pSrcA, q7_t *pSrcB, q7_t *pDst, const uint16_t out_shift, uint32_t blockSize);
  1783. /**
  1784. * @brief Matrix-multiplication function for convolution with per-channel requantization.
  1785. * @param[in] input_a pointer to operand A
  1786. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  1787. * @param[in] output_ch number of rows of A
  1788. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  1789. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  1790. * @param[in] out_offset output tensor offset.
  1791. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  1792. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  1793. * @param[in] num_col_a number of columns of A
  1794. * @param[in] aligned_num_col_a number of columns of A aligned by 4
  1795. * @param[in] output_bias per output channel bias. Range : int32
  1796. * @param[in,out] out_0 pointer to output
  1797. * @return The function returns one of the two
  1798. * 1. The incremented output pointer for a successful operation or
  1799. * 2. NULL if implementation is not available.
  1800. *
  1801. * @details This function does the matrix multiplication of weight matrix for all output channels
  1802. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  1803. * clamped in the range provided by activation min and max.
  1804. * Supported framework: TensorFlow Lite micro.
  1805. */
  1806. int8_t *riscv_nn_mat_mult_kernel_s8_s16(const int8_t *input_a,
  1807. const int16_t *input_b,
  1808. const uint16_t output_ch,
  1809. const int32_t *out_shift,
  1810. const int32_t *out_mult,
  1811. const int32_t out_offset,
  1812. const int16_t activation_min,
  1813. const int16_t activation_max,
  1814. const int32_t num_col_a,
  1815. const int32_t aligned_num_col_a,
  1816. const int32_t *const output_bias,
  1817. int8_t *out_0);
  1818. /**
  1819. * @brief Matrix-multiplication function for convolution with per-channel requantization, supporting an address offset
  1820. * between rows.
  1821. * @param[in] input_a pointer to operand A
  1822. * @param[in] input_b pointer to operand B, always consists of 2 vectors.
  1823. * @param[in] output_ch number of rows of A
  1824. * @param[in] out_shift pointer to per output channel requantization shift parameter.
  1825. * @param[in] out_mult pointer to per output channel requantization multiplier parameter.
  1826. * @param[in] out_offset output tensor offset.
  1827. * @param[in] activation_min minimum value to clamp the output to. Range : int8
  1828. * @param[in] activation_max maximum value to clamp the output to. Range : int8
  1829. * @param[in] num_col_a number of columns of A
  1830. * @param[in] aligned_num_col_a number of columns of A aligned by 4
  1831. * @param[in] output_bias per output channel bias. Range : int32
  1832. * @param[in] row_address_offset address offset between rows in the output
  1833. * @param[in,out] out_0 pointer to output
  1834. * @return The function returns one of the two
  1835. * 1. The incremented output pointer for a successful operation or
  1836. * 2. NULL if implementation is not available.
  1837. *
  1838. * @details This function does the matrix multiplication of weight matrix for all output channels
  1839. * with 2 columns from im2col and produces two elements/output_channel. The outputs are
  1840. * clamped in the range provided by activation min and max.
  1841. *
  1842. * This function is slighly less performant than riscv_nn_mat_mult_kernel_s8_s16, but allows support for
  1843. * grouped convolution. Supported framework: TensorFlow Lite micro.
  1844. */
  1845. int8_t *riscv_nn_mat_mult_kernel_row_offset_s8_s16(const int8_t *input_a,
  1846. const int16_t *input_b,
  1847. const uint16_t output_ch,
  1848. const int32_t *out_shift,
  1849. const int32_t *out_mult,
  1850. const int32_t out_offset,
  1851. const int16_t activation_min,
  1852. const int16_t activation_max,
  1853. const int32_t num_col_a,
  1854. const int32_t aligned_num_col_a,
  1855. const int32_t *const output_bias,
  1856. const int32_t row_address_offset,
  1857. int8_t *out_0);
  1858. /**
  1859. * @brief Common softmax function for s8 input and s8 or s16 output
  1860. * @param[in] input Pointer to the input tensor
  1861. * @param[in] num_rows Number of rows in the input tensor
  1862. * @param[in] row_size Number of elements in each input row
  1863. * @param[in] mult Input quantization multiplier
  1864. * @param[in] shift Input quantization shift within the range [0, 31]
  1865. * @param[in] diff_min Minimum difference with max in row. Used to check if
  1866. * the quantized exponential operation can be performed
  1867. * @param[in] int16_output Indicating s8 output if 0 else s16 output
  1868. * @param[out] output Pointer to the output tensor
  1869. *
  1870. * @note Supported framework: TensorFlow Lite micro (bit-accurate)
  1871. *
  1872. */
  1873. void riscv_nn_softmax_common_s8(const int8_t *input,
  1874. const int32_t num_rows,
  1875. const int32_t row_size,
  1876. const int32_t mult,
  1877. const int32_t shift,
  1878. const int32_t diff_min,
  1879. const bool int16_output,
  1880. void *output);
  1881. /**
  1882. * @brief macro for adding rounding offset
  1883. */
  1884. #ifndef RISCV_NN_TRUNCATE
  1885. #define NN_ROUND(out_shift) ((0x1 << out_shift) >> 1)
  1886. #else
  1887. #define NN_ROUND(out_shift) 0
  1888. #endif
  1889. // Macros for shortening quantization functions' names and avoid long lines
  1890. #define MUL_SAT(a, b) riscv_nn_doubling_high_mult((a), (b))
  1891. #define MUL_POW2(a, b) riscv_nn_mult_by_power_of_two((a), (b))
  1892. #define DIV_POW2(a, b) riscv_nn_divide_by_power_of_two((a), (b))
  1893. #define EXP_ON_NEG(x) riscv_nn_exp_on_negative_values((x))
  1894. #define ONE_OVER1(x) riscv_nn_one_over_one_plus_x_for_x_in_0_1((x))
  1895. /**
  1896. * @brief Saturating doubling high multiply. Result matches
  1897. * NEON instruction VQRDMULH.
  1898. * @param[in] m1 Multiplicand. Range: {NN_Q31_MIN, NN_Q31_MAX}
  1899. * @param[in] m2 Multiplier. Range: {NN_Q31_MIN, NN_Q31_MAX}
  1900. * @return Result of multiplication.
  1901. *
  1902. */
  1903. __STATIC_FORCEINLINE int32_t riscv_nn_doubling_high_mult(const int32_t m1, const int32_t m2)
  1904. {
  1905. int32_t result = 0;
  1906. // Rounding offset to add for a right shift of 31
  1907. int64_t mult = 1 << 30;
  1908. if ((m1 < 0) ^ (m2 < 0))
  1909. {
  1910. mult = 1 - mult;
  1911. }
  1912. // Gets resolved as a SMLAL instruction
  1913. mult = mult + (int64_t)m1 * m2;
  1914. // Utilize all of the upper 32 bits. This is the doubling step
  1915. // as well.
  1916. result = (int32_t)(mult / (1ll << 31));
  1917. if ((m1 == m2) && (m1 == (int32_t)NN_Q31_MIN))
  1918. {
  1919. result = NN_Q31_MAX;
  1920. }
  1921. return result;
  1922. }
  1923. /**
  1924. * @brief Doubling high multiply without saturation. This is intended
  1925. * for requantization where the scale is a positive integer
  1926. *
  1927. * @param[in] m1 Multiplicand. Range: {NN_Q31_MIN, NN_Q31_MAX}
  1928. * @param[in] m2 Multiplier Range: {NN_Q31_MIN, NN_Q31_MAX}
  1929. * @return Result of multiplication.
  1930. * @note The result of this matches that of neon instruction
  1931. * VQRDMULH for m1 in range {NN_Q31_MIN, NN_Q31_MAX} and m2 in
  1932. * range {NN_Q31_MIN + 1, NN_Q31_MAX}. Saturation occurs when
  1933. * m1 equals m2 equals NN_Q31_MIN and that is not handled by
  1934. * this function.
  1935. *
  1936. */
  1937. __STATIC_FORCEINLINE int32_t riscv_nn_doubling_high_mult_no_sat(const int32_t m1, const int32_t m2)
  1938. {
  1939. int32_t result = 0;
  1940. union riscv_nn_long_long mult;
  1941. // Rounding offset to add for a right shift of 31
  1942. mult.word.low = 1 << 30;
  1943. mult.word.high = 0;
  1944. // Gets resolved as a SMLAL instruction
  1945. mult.long_long = mult.long_long + (int64_t)m1 * m2;
  1946. // Utilize all of the upper 32 bits. This is the doubling step
  1947. // as well.
  1948. result = (int32_t)(mult.long_long >> 31);
  1949. return result;
  1950. }
  1951. /**
  1952. * @brief Rounding divide by power of two.
  1953. * @param[in] dividend - Dividend
  1954. * @param[in] exponent - Divisor = power(2, exponent)
  1955. * Range: [0, 31]
  1956. * @return Rounded result of division. Midpoint is rounded away from zero.
  1957. *
  1958. */
  1959. __STATIC_FORCEINLINE int32_t riscv_nn_divide_by_power_of_two(const int32_t dividend, const int32_t exponent)
  1960. {
  1961. int32_t result = 0;
  1962. const int32_t remainder_mask = (1 << exponent) - 1;
  1963. int32_t remainder = remainder_mask & dividend;
  1964. // Basic division
  1965. result = dividend >> exponent;
  1966. // Adjust 'result' for rounding (mid point away from zero)
  1967. int32_t threshold = remainder_mask >> 1;
  1968. if (result < 0)
  1969. {
  1970. threshold++;
  1971. }
  1972. if (remainder > threshold)
  1973. {
  1974. result++;
  1975. }
  1976. return result;
  1977. }
  1978. /**
  1979. * @brief Requantize a given value.
  1980. * @details Essentially returns (val * multiplier)/(2 ^ shift) with different rounding depending if
  1981. * NMSIS_NN_USE_SINGLE_ROUNDING is defined or not.
  1982. * @param[in] val Value to be requantized
  1983. * @param[in] multiplier Multiplier. Range {NN_Q31_MIN + 1, Q32_MAX}
  1984. * @param[in] shift Shift. Range: {-31, 30}
  1985. * Default branch:
  1986. * If shift is positive left shift 'val * multiplier' with shift
  1987. * If shift is negative right shift 'val * multiplier' with abs(shift)
  1988. * Single round branch:
  1989. * Input for total_shift in divide by '2 ^ total_shift'
  1990. *
  1991. * @return Default branch:
  1992. * Returns (val * multiplier) with rounding divided by (2 ^ shift) with rounding
  1993. * Single round branch:
  1994. * Returns (val * multiplier)/(2 ^ (31 - shift)) with rounding
  1995. *
  1996. */
  1997. __STATIC_FORCEINLINE int32_t riscv_nn_requantize(const int32_t val, const int32_t multiplier, const int32_t shift)
  1998. {
  1999. return riscv_nn_divide_by_power_of_two(riscv_nn_doubling_high_mult_no_sat(val * (1 << LEFT_SHIFT(shift)), multiplier),
  2000. RIGHT_SHIFT(shift));
  2001. }
  2002. #if defined(RISCV_MATH_VECTOR)
  2003. __STATIC_FORCEINLINE vint32m4_t riscv_nn_requantize_m4_rvv(vint32m4_t valm4, size_t l, const q31_t multiplier, const q31_t shift)
  2004. {
  2005. if (shift >= 0) {
  2006. valm4 = __riscv_vsmul_vx_i32m4(__riscv_vsll_vx_i32m4(valm4, shift, l), multiplier, __RISCV_VXRM_RNU, l);
  2007. } else {
  2008. q31_t exponent = -shift;
  2009. q31_t remainder_mask = (1 << exponent) - 1;
  2010. q31_t threshold = remainder_mask >> 1;
  2011. vint32m4_t b32m4, c32m4;
  2012. valm4 = __riscv_vsmul_vx_i32m4(valm4, multiplier, __RISCV_VXRM_RNU, l);
  2013. b32m4 = __riscv_vsra_vx_i32m4(valm4, exponent, l);
  2014. valm4 = __riscv_vand_vx_i32m4(valm4, remainder_mask, l);
  2015. c32m4 = __riscv_vmv_v_x_i32m4(threshold, l);
  2016. vbool8_t mask = __riscv_vmslt_vx_i32m4_b8(b32m4, 0, l);
  2017. c32m4 = __riscv_vadd_vx_i32m4_tumu(mask, c32m4, c32m4, 1, l);
  2018. mask = __riscv_vmsgt_vv_i32m4_b8(valm4, c32m4, l);
  2019. valm4 = __riscv_vadd_vx_i32m4_tumu(mask, b32m4, b32m4, 1, l);
  2020. }
  2021. return valm4;
  2022. }
  2023. __STATIC_FORCEINLINE vint32m8_t riscv_nn_requantize_m8_rvv(vint32m8_t valm8, size_t l, const q31_t multiplier, const q31_t shift)
  2024. {
  2025. if (shift >= 0) {
  2026. valm8 = __riscv_vsmul_vx_i32m8(__riscv_vsll_vx_i32m8(valm8, shift, l), multiplier, __RISCV_VXRM_RNU, l);
  2027. } else {
  2028. q31_t exponent = -shift;
  2029. q31_t remainder_mask = (1 << exponent) - 1;
  2030. q31_t threshold = remainder_mask >> 1;
  2031. vint32m8_t b32m8, c32m8;
  2032. valm8 = __riscv_vsmul_vx_i32m8(valm8, multiplier, __RISCV_VXRM_RNU, l);
  2033. b32m8 = __riscv_vsra_vx_i32m8(valm8, exponent, l);
  2034. valm8 = __riscv_vand_vx_i32m8(valm8, remainder_mask, l);
  2035. c32m8 = __riscv_vmv_v_x_i32m8(threshold, l);
  2036. vbool4_t mask = __riscv_vmslt_vx_i32m8_b4(b32m8, 0, l);
  2037. c32m8 = __riscv_vadd_vx_i32m8_tumu(mask, c32m8, c32m8, 1, l);
  2038. mask = __riscv_vmsgt_vv_i32m8_b4(valm8, c32m8, l);
  2039. valm8 = __riscv_vadd_vx_i32m8_tumu(mask, b32m8, b32m8, 1, l);
  2040. }
  2041. return valm8;
  2042. }
  2043. #endif
  2044. /**
  2045. * @brief Requantize a given 64 bit value.
  2046. * @param[in] val Value to be requantized in the range {-(1<<47)} to {(1<<47) - 1}
  2047. * @param[in] reduced_multiplier Reduced multiplier in the range {NN_Q31_MIN + 1, Q32_MAX} to {Q16_MIN + 1,
  2048. * Q16_MAX}
  2049. * @param[in] shift Left or right shift for 'val * multiplier' in the range {-31} to {7}
  2050. *
  2051. * @return Returns (val * multiplier)/(2 ^ shift)
  2052. *
  2053. */
  2054. __STATIC_FORCEINLINE int32_t riscv_nn_requantize_s64(const int64_t val,
  2055. const int32_t reduced_multiplier,
  2056. const int32_t shift)
  2057. {
  2058. const int64_t new_val = val * reduced_multiplier;
  2059. int32_t result = new_val >> (14 - shift); // 64->32 bit reduction
  2060. result = (result + 1) >> 1; // Last shift position and insert round
  2061. return result;
  2062. }
  2063. /**
  2064. * @brief memcpy
  2065. * @param[in, out] dst Destination pointer
  2066. * @param[in] src Source pointer.
  2067. * @param[in] block_size Number of bytes to copy.
  2068. *
  2069. */
  2070. __STATIC_FORCEINLINE void riscv_memcpy_s8(int8_t *__RESTRICT dst, const int8_t *__RESTRICT src, uint32_t block_size)
  2071. {
  2072. memcpy(dst, src, block_size);
  2073. }
  2074. /**
  2075. * @brief memcpy
  2076. * @param[in, out] dst Destination pointer
  2077. * @param[in] src Source pointer.
  2078. * @param[in] block_size Number of bytes to copy.
  2079. *
  2080. */
  2081. __STATIC_FORCEINLINE void riscv_memcpy_q7(q7_t *__RESTRICT dst, const q7_t *__RESTRICT src, uint32_t block_size)
  2082. {
  2083. memcpy(dst, src, block_size);
  2084. }
  2085. /**
  2086. * @brief memcpy wrapper for int16
  2087. * @param[in, out] dst Destination pointer
  2088. * @param[in] src Source pointer.
  2089. * @param[in] block_size Number of bytes to copy.
  2090. *
  2091. */
  2092. __STATIC_FORCEINLINE void riscv_memcpy_q15(int16_t *__RESTRICT dst, const int16_t *__RESTRICT src, uint32_t block_size)
  2093. {
  2094. memcpy(dst, src, block_size);
  2095. }
  2096. // @note The following functions are used only for softmax layer, scaled bits = 5 assumed
  2097. __STATIC_FORCEINLINE int32_t riscv_nn_exp_on_negative_values(int32_t val)
  2098. {
  2099. int32_t mask = 0;
  2100. int32_t shift = 24;
  2101. const int32_t val_mod_minus_quarter = (val & ((1 << shift) - 1)) - (1 << shift);
  2102. const int32_t remainder = val_mod_minus_quarter - val;
  2103. const int32_t x = (val_mod_minus_quarter << 5) + (1 << 28);
  2104. const int32_t x2 = MUL_SAT(x, x);
  2105. int32_t result = 1895147668 +
  2106. MUL_SAT(1895147668, x + DIV_POW2(MUL_SAT(DIV_POW2(MUL_SAT(x2, x2), 2) + MUL_SAT(x2, x), 715827883) + x2, 1));
  2107. #define SELECT_IF_NON_ZERO(x) \
  2108. { \
  2109. mask = MASK_IF_NON_ZERO(remainder & (1 << shift++)); \
  2110. result = SELECT_USING_MASK(mask, MUL_SAT(result, x), result); \
  2111. }
  2112. SELECT_IF_NON_ZERO(1672461947)
  2113. SELECT_IF_NON_ZERO(1302514674)
  2114. SELECT_IF_NON_ZERO(790015084)
  2115. SELECT_IF_NON_ZERO(290630308)
  2116. SELECT_IF_NON_ZERO(39332535)
  2117. SELECT_IF_NON_ZERO(720401)
  2118. SELECT_IF_NON_ZERO(242)
  2119. #undef SELECT_IF_NON_ZERO
  2120. mask = MASK_IF_ZERO(val);
  2121. return SELECT_USING_MASK(mask, NN_Q31_MAX, result);
  2122. }
  2123. __STATIC_FORCEINLINE int32_t riscv_nn_mult_by_power_of_two(const int32_t val, const int32_t exp)
  2124. {
  2125. const int32_t thresh = ((1 << (31 - exp)) - 1);
  2126. int32_t result = val << exp;
  2127. result = SELECT_USING_MASK(MASK_IF_NON_ZERO(val > thresh), NN_Q31_MAX, result);
  2128. result = SELECT_USING_MASK(MASK_IF_NON_ZERO(val < -thresh), NN_Q31_MIN, result);
  2129. return result;
  2130. }
  2131. __STATIC_FORCEINLINE int32_t riscv_nn_one_over_one_plus_x_for_x_in_0_1(int32_t val)
  2132. {
  2133. const int64_t sum = (int64_t)val + (int64_t)NN_Q31_MAX;
  2134. const int32_t half_denominator = (int32_t)((sum + (sum >= 0 ? 1 : -1)) / 2L);
  2135. int32_t x = 1515870810 + MUL_SAT(half_denominator, -1010580540);
  2136. const int32_t shift = (1 << 29);
  2137. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  2138. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  2139. x += MUL_POW2(MUL_SAT(x, shift - MUL_SAT(half_denominator, x)), 2);
  2140. return MUL_POW2(x, 1);
  2141. }
  2142. /**
  2143. @brief Write 2 s16 elements and post increment pointer.
  2144. @param[in] dest_q15 Pointer to pointer that holds address of destination.
  2145. @param[in] src_q31 Input value to be written.
  2146. */
  2147. __STATIC_FORCEINLINE void riscv_nn_write_q15x2_ia(int16_t **dest_q15, int32_t src_q31)
  2148. {
  2149. int32_t val = src_q31;
  2150. memcpy(*dest_q15, &val, 4);
  2151. *dest_q15 += 2;
  2152. }
  2153. /**
  2154. @brief Write 2 s8 elements and post increment pointer.
  2155. @param[in] dst Pointer to pointer that holds address of destination.
  2156. @param[in] src Input value to be written.
  2157. */
  2158. __STATIC_FORCEINLINE void riscv_nn_write_s8x2_ia(int8_t **dst, int16_t src)
  2159. {
  2160. memcpy(*dst, &src, 2);
  2161. *dst += 2;
  2162. }
  2163. /**
  2164. * @brief Copies the elements of a Q7 vector.
  2165. * @param[in] pSrc input pointer
  2166. * @param[out] pDst output pointer
  2167. * @param[in] blockSize number of samples to process
  2168. */
  2169. void riscv_nn_copy_q7(
  2170. const q7_t * pSrc,
  2171. q7_t * pDst,
  2172. uint32_t blockSize);
  2173. /**
  2174. * @brief Copies the elements of a Q15 vector.
  2175. * @param[in] pSrc input pointer
  2176. * @param[out] pDst output pointer
  2177. * @param[in] blockSize number of samples to process
  2178. */
  2179. void riscv_nn_copy_q15(
  2180. const q15_t * pSrc,
  2181. q15_t * pDst,
  2182. uint32_t blockSize);
  2183. /**
  2184. * @brief Fills a constant value into a Q7 vector.
  2185. * @param[in] value input value to be filled
  2186. * @param[out] pDst output pointer
  2187. * @param[in] blockSize number of samples to process
  2188. */
  2189. void riscv_nn_fill_q7(
  2190. q7_t value,
  2191. q7_t * pDst,
  2192. uint32_t blockSize);
  2193. /**
  2194. * @brief Fills a constant value into a Q15 vector.
  2195. * @param[in] value input value to be filled
  2196. * @param[out] pDst output pointer
  2197. * @param[in] blockSize number of samples to process
  2198. */
  2199. void riscv_nn_fill_q15(
  2200. q15_t value,
  2201. q15_t * pDst,
  2202. uint32_t blockSize);
  2203. // Support functions for LSTM
  2204. /**
  2205. * @brief Update LSTM function for an iteration step using s8 input and output, and s16 internally.
  2206. *
  2207. * @param[in] data_in Data input pointer
  2208. * @param[in] hidden_in Hidden state/ recurrent input pointer
  2209. * @param[out] hidden_out Hidden state/ recurrent output pointer
  2210. * @param[in] params Struct containg all information about the lstm operator, see
  2211. * riscv_nn_types.
  2212. * @param[in] buffers Struct containg pointers to all temporary scratch buffers needed for the
  2213. * lstm operator, see riscv_nn_types.
  2214. * @param[in] batch_offset Number of timesteps between consecutive batches.
  2215. * E.g for params->timing_major = true, all batches for t=0 are stored sequentially, so batch offset = 1.
  2216. * For params->time major = false, all time steps are stored continously before the next batch, so
  2217. * batch offset = params->time_steps.
  2218. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2219. */
  2220. riscv_nmsis_nn_status riscv_nn_lstm_step_s8(const int8_t *data_in,
  2221. const int8_t *hidden_in,
  2222. int8_t *hidden_out,
  2223. const nmsis_nn_lstm_params *params,
  2224. nmsis_nn_lstm_context *buffers,
  2225. const int32_t batch_offset);
  2226. /**
  2227. * @brief Update LSTM function for an iteration step using s16 input and output, and s16 internally.
  2228. *
  2229. * @param[in] data_in Data input pointer
  2230. * @param[in] hidden_in Hidden state/ recurrent input pointer
  2231. * @param[out] hidden_out Hidden state/ recurrent output pointer
  2232. * @param[in] params Struct containg all information about the lstm operator, see
  2233. * riscv_nn_types.
  2234. * @param[in] buffers Struct containg pointers to all temporary scratch buffers needed for the
  2235. * lstm operator, see riscv_nn_types.
  2236. * @param[in] batch_offset Number of timesteps between consecutive batches.
  2237. * E.g for params->timing_major = true, all batches for t=0 are stored sequentially, so batch offset = 1.
  2238. * For params->time major = false, all time steps are stored continously before the next batch, so
  2239. * batch offset = params->time_steps.
  2240. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2241. */
  2242. riscv_nmsis_nn_status riscv_nn_lstm_step_s16(const int16_t *data_in,
  2243. const int16_t *hidden_in,
  2244. int16_t *hidden_out,
  2245. const nmsis_nn_lstm_params *params,
  2246. nmsis_nn_lstm_context *buffers,
  2247. const int32_t batch_offset);
  2248. /**
  2249. * @brief Updates a LSTM gate for an iteration step of LSTM function, int8x8_16 version.
  2250. *
  2251. * @param[in] data_in Data input pointer
  2252. * @param[in] hidden_in Hidden state/ recurrent input pointer
  2253. * @param[in] gate_data Struct containing all information about the gate caluclation, see
  2254. * riscv_nn_types.
  2255. * @param[in] params Struct containing all information about the lstm_operation, see
  2256. * riscv_nn_types
  2257. * @param[out] output Hidden state/ recurrent output pointer
  2258. * @param[in] batch_offset Number of timesteps between consecutive batches, see
  2259. * riscv_nn_lstm_step_s8.
  2260. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2261. */
  2262. riscv_nmsis_nn_status riscv_nn_lstm_calculate_gate_s8_s16(const int8_t *data_in,
  2263. const int8_t *hidden_in,
  2264. const nmsis_nn_lstm_gate *gate_data,
  2265. const nmsis_nn_lstm_params *params,
  2266. int16_t *output,
  2267. const int32_t batch_offset);
  2268. /**
  2269. * @brief Updates a LSTM gate for an iteration step of LSTM function, int16x8_16 version.
  2270. *
  2271. * @param[in] data_in Data input pointer
  2272. * @param[in] hidden_in Hidden state/ recurrent input pointer
  2273. * @param[in] gate_data Struct containing all information about the gate caluclation, see
  2274. * riscv_nn_types.
  2275. * @param[in] params Struct containing all information about the lstm_operation, see
  2276. * riscv_nn_types
  2277. * @param[out] output Hidden state/ recurrent output pointer
  2278. * @param[in] batch_offset Number of timesteps between consecutive batches, see
  2279. * riscv_nn_lstm_step_s16.
  2280. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2281. */
  2282. riscv_nmsis_nn_status riscv_nn_lstm_calculate_gate_s16(const int16_t *data_in,
  2283. const int16_t *hidden_in,
  2284. const nmsis_nn_lstm_gate *gate_data,
  2285. const nmsis_nn_lstm_params *params,
  2286. int16_t *output,
  2287. const int32_t batch_offset);
  2288. /**
  2289. * @brief The result of the multiplication is accumulated to the passed result buffer.
  2290. * Multiplies a matrix by a "batched" vector (i.e. a matrix with a batch dimension composed by input vectors independent
  2291. * from each other).
  2292. *
  2293. * @param[in] lhs Batched vector
  2294. * @param[in] rhs Weights - input matrix (H(Rows)xW(Columns))
  2295. * @param[in] effective_bias Bias + lhs_offset * kernel_sum term precalculated into a constant vector.
  2296. * @param[out] dst Output
  2297. * @param[in] dst_multiplier Multiplier for quantization
  2298. * @param[in] dst_shift Shift for quantization
  2299. * @param[in] rhs_cols Vector/matarix column length
  2300. * @param[in] rhs_rows Row count of matrix
  2301. * @param[in] batches Batch size
  2302. * @param[in] batch_offset Number of timesteps between consecutive batches in input, see riscv_nn_lstm_step_s8. Note
  2303. that the output is always stored with sequential batches.
  2304. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  2305. */
  2306. riscv_nmsis_nn_status riscv_nn_vec_mat_mul_result_acc_s8_s16(const int8_t *lhs,
  2307. const int8_t *rhs,
  2308. const int32_t *effective_bias,
  2309. int16_t *dst,
  2310. const int32_t dst_multiplier,
  2311. const int32_t dst_shift,
  2312. const int32_t rhs_cols,
  2313. const int32_t rhs_rows,
  2314. const int32_t batches,
  2315. const int32_t batch_offset);
  2316. /**
  2317. * @brief The result of the multiplication is accumulated to the passed result buffer.
  2318. * Multiplies a matrix by a "batched" vector (i.e. a matrix with a batch dimension composed by input vectors independent
  2319. * from each other).
  2320. *
  2321. * @param[in] lhs Batched vector
  2322. * @param[in] rhs Weights - input matrix (H(Rows)xW(Columns))
  2323. * @param[in] effective_bias Bias + lhs_offset * kernel_sum term precalculated into a constant vector.
  2324. * @param[out] dst Output
  2325. * @param[in] dst_multiplier Multiplier for quantization
  2326. * @param[in] dst_shift Shift for quantization
  2327. * @param[in] rhs_cols Vector/matarix column length
  2328. * @param[in] rhs_rows Row count of matrix
  2329. * @param[in] batches Batch size
  2330. * @param[in] batch_offset Number of timesteps between consecutive batches in input, see riscv_nn_lstm_step_s16.
  2331. Note that the output is always stored with sequential batches.
  2332. * @return The function returns <code>RISCV_NMSIS_NN_SUCCESS</code>
  2333. */
  2334. riscv_nmsis_nn_status riscv_nn_vec_mat_mul_result_acc_s16(const int16_t *lhs,
  2335. const int8_t *rhs,
  2336. const int64_t *effective_bias,
  2337. int16_t *dst,
  2338. const int32_t dst_multiplier,
  2339. const int32_t dst_shift,
  2340. const int32_t rhs_cols,
  2341. const int32_t rhs_rows,
  2342. const int32_t batches,
  2343. const int32_t batch_offset);
  2344. /**
  2345. * @brief s16 elementwise multiplication with s8 output
  2346. * @param[in] input_1_vect pointer to input vector 1
  2347. * @param[in] input_2_vect pointer to input vector 2
  2348. * @param[in,out] output pointer to output vector
  2349. * @param[in] out_offset output offset
  2350. * @param[in] out_mult output multiplier
  2351. * @param[in] out_shift output shift
  2352. * @param[in] block_size number of samples per batch
  2353. * @param[in] batch_size number of samples per batch
  2354. * @param[in] batch_offset Number of timesteps between consecutive batches in output, see
  2355. * riscv_nn_lstm_step_s8. Note that it is assumed that the input is stored with sequential batches.
  2356. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2357. *
  2358. * @details Supported framework: TensorFlow Lite micro
  2359. */
  2360. riscv_nmsis_nn_status riscv_elementwise_mul_s16_s8(const int16_t *input_1_vect,
  2361. const int16_t *input_2_vect,
  2362. int8_t *output,
  2363. const int32_t out_offset,
  2364. const int32_t out_mult,
  2365. const int32_t out_shift,
  2366. const int32_t block_size,
  2367. const int32_t batch_size,
  2368. const int32_t batch_offset);
  2369. /**
  2370. * @brief s16 elementwise multiplication with s16 output
  2371. * @param[in] input_1_vect pointer to input vector 1
  2372. * @param[in] input_2_vect pointer to input vector 2
  2373. * @param[in,out] output pointer to output vector
  2374. * @param[in] out_offset output offset
  2375. * @param[in] out_mult output multiplier
  2376. * @param[in] out_shift output shift
  2377. * @param[in] block_size number of samples per batch
  2378. * @param[in] batch_size number of samples per batch
  2379. * @param[in] batch_offset Number of timesteps between consecutive batches in output, see
  2380. * riscv_nn_lstm_step_s16. Note that it is assumed that the input is stored with sequential batches.
  2381. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2382. *
  2383. * @details Supported framework: TensorFlow Lite micro
  2384. */
  2385. riscv_nmsis_nn_status riscv_elementwise_mul_s16_batch_offset(const int16_t *input_1_vect,
  2386. const int16_t *input_2_vect,
  2387. int16_t *output,
  2388. const int32_t out_offset,
  2389. const int32_t out_mult,
  2390. const int32_t out_shift,
  2391. const int32_t block_size,
  2392. const int32_t batch_size,
  2393. const int32_t batch_offset);
  2394. /**
  2395. * @brief s16 elementwise multiplication. The result of the multiplication is accumulated to the passed result buffer.
  2396. * @param[in] input_1_vect pointer to input vector 1
  2397. * @param[in] input_2_vect pointer to input vector 2
  2398. * @param[in] input_1_offset offset for input 1. Not used.
  2399. * @param[in] input_2_offset offset for input 2. Not used.
  2400. * @param[in,out] output pointer to output vector
  2401. * @param[in] out_offset output offset. Not used.
  2402. * @param[in] out_mult output multiplier
  2403. * @param[in] out_shift output shift
  2404. * @param[in] out_activation_min minimum value to clamp output to. Min: -32768
  2405. * @param[in] out_activation_max maximum value to clamp output to. Max: 32767
  2406. * @param[in] block_size number of samples
  2407. * @return The function returns RISCV_NMSIS_NN_SUCCESS
  2408. *
  2409. * @details Supported framework: TensorFlow Lite micro
  2410. */
  2411. riscv_nmsis_nn_status riscv_elementwise_mul_acc_s16(const int16_t *input_1_vect,
  2412. const int16_t *input_2_vect,
  2413. const int32_t input_1_offset,
  2414. const int32_t input_2_offset,
  2415. int16_t *output,
  2416. const int32_t out_offset,
  2417. const int32_t out_mult,
  2418. const int32_t out_shift,
  2419. const int32_t out_activation_min,
  2420. const int32_t out_activation_max,
  2421. const int32_t block_size);
  2422. /**
  2423. * @brief Check if a broadcast is required between 2 nmsis_nn_dims.
  2424. * @param[in] shape_1 pointer to input tensor 1
  2425. * @param[in] shape_2 pointer to input tensor 2
  2426. * @return The function returns 1 if a broadcast is required, or 0 if not.
  2427. *
  2428. * @details Compares each dimension and returns 1 if any dimension does not match.
  2429. * This function does not check that broadcast rules are met.
  2430. */
  2431. __STATIC_FORCEINLINE int32_t riscv_check_broadcast_required(const nmsis_nn_dims *shape_1, const nmsis_nn_dims *shape_2)
  2432. {
  2433. if ((shape_1->n != shape_2->n) || (shape_1->h != shape_2->h) || (shape_1->w != shape_2->w) ||
  2434. (shape_1->c != shape_2->c))
  2435. {
  2436. return 1;
  2437. }
  2438. return 0;
  2439. }
  2440. #ifdef __cplusplus
  2441. }
  2442. #endif
  2443. #endif /* RISCV_NNSUPPORTFUNCTIONS_H */