You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

bgemm_kernel_2vlx4_neoversev1_impl.c 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /***************************************************************************
  2. * Copyright (c) 2025, The OpenBLAS Project
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in
  11. * the documentation and/or other materials provided with the
  12. * distribution.
  13. * 3. Neither the name of the OpenBLAS project nor the names of
  14. * its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. * *****************************************************************************/
  28. #include <arm_sve.h>
  29. #include <arm_neon.h>
  30. #include "common.h"
  31. #ifdef BGEMM
  32. #ifdef ALPHA_ONE
  33. #define TO16 vcvth_bf16_f32
  34. #define TO32 vcvtah_f32_bf16
  35. #define UPDATE_C(PG, PTR, DST, SRC) \
  36. do { \
  37. DST = svreinterpret_f32_u32(svld1uh_u32((pghalf), (uint16_t*)PTR)); \
  38. DST = svadd_z((PG), SRC, DST); \
  39. svtmp16 = svcvt_bf16_f32_z((PG), DST); \
  40. svtmp16 = svuzp1_bf16(svtmp16, svtmp16); \
  41. svst1_bf16((pghalf), (PTR), svtmp16); \
  42. } while (0);
  43. #define UPDATE_C2(ptr, tmp, vector) \
  44. *(ptr) = TO16(vector[0] + TO32(*ptr)); \
  45. *(ptr + 1) = TO16(vector[1] + TO32(*(ptr + 1)));
  46. #define UPDATE_C1(ptr, value) *ptr = TO16(TO32(*ptr) + (value))
  47. #else
  48. #define UPDATE_C(PG, PTR, DST, SRC) \
  49. do { \
  50. DST = svreinterpret_f32_u32(svld1uh_u32((pghalf), (uint16_t*)PTR)); \
  51. DST = svmad_z((PG), svalpha, SRC, DST); \
  52. svtmp16 = svcvt_bf16_f32_z((PG), DST); \
  53. svtmp16 = svuzp1_bf16(svtmp16, svtmp16); \
  54. svst1_bf16((pghalf), (PTR), svtmp16); \
  55. } while (0);
  56. #define UPDATE_C2(ptr, tmp, vector) \
  57. *(ptr) = TO16(vector[0] * alpha + TO32(*ptr)); \
  58. *(ptr + 1) = TO16(vector[1] * alpha + TO32(*(ptr + 1)));
  59. #define UPDATE_C1(ptr, value) *ptr = TO16(TO32(*ptr) + (value) * alpha)
  60. #endif
  61. #else
  62. #ifdef ALPHA_ONE
  63. #define UPDATE_C(PG, PTR, DST, SRC) \
  64. do { \
  65. DST = svld1_f32((PG), (PTR)); \
  66. DST = svadd_z((PG), SRC, DST); \
  67. svst1_f32((PG), (PTR), DST); \
  68. } while (0);
  69. #define UPDATE_C2(ptr, tmp, vector) \
  70. tmp = vld1_f32(ptr); \
  71. tmp = vadd_f32(vector, tmp); \
  72. vst1_f32(ptr, tmp);
  73. #define UPDATE_C1(ptr, value) *ptr = *ptr + (value)
  74. #else
  75. #define UPDATE_C(PG, PTR, DST, SRC) \
  76. do { \
  77. DST = svld1_f32((PG), (PTR)); \
  78. DST = svmad_z((PG), svalpha, SRC, DST); \
  79. svst1_f32((PG), (PTR), DST); \
  80. } while (0);
  81. #define UPDATE_C2(ptr, tmp, vector) \
  82. tmp = vld1_f32(ptr); \
  83. tmp = vmla_n_f32(tmp, vector, alpha); \
  84. vst1_f32(ptr, tmp);
  85. #define UPDATE_C1(ptr, value) *ptr = *ptr + (value) * alpha
  86. #endif
  87. #endif
  88. #ifdef BGEMM
  89. #define OUTPUT_FLOAT bfloat16_t
  90. #else
  91. #define OUTPUT_FLOAT float
  92. #endif
  93. #ifdef ALPHA_ONE
  94. static int bgemm_kernel_neoversev1_alpha_one(BLASLONG m, BLASLONG n, BLASLONG k,
  95. float alpha, IFLOAT *AA, IFLOAT *BB,
  96. FLOAT *CC, BLASLONG ldc)
  97. #else
  98. static int bgemm_kernel_neoversev1_alpha(BLASLONG m, BLASLONG n, BLASLONG k,
  99. float alpha, IFLOAT *AA, IFLOAT *BB, FLOAT *CC,
  100. BLASLONG ldc)
  101. #endif
  102. {
  103. BLASLONG pad_k = (k + 3) & ~3;
  104. #ifndef ALPHA_ONE
  105. svfloat32_t svalpha = svdup_f32(alpha);
  106. #endif
  107. bfloat16_t *ptr_a = (bfloat16_t *)AA;
  108. bfloat16_t *ptr_b = (bfloat16_t *)BB;
  109. OUTPUT_FLOAT *ptr_c =(OUTPUT_FLOAT*)CC;
  110. bfloat16_t *ptr_a0;
  111. bfloat16_t *ptr_b0;
  112. OUTPUT_FLOAT *ptr_c0, *ptr_c1, *ptr_c2, *ptr_c3;
  113. svfloat32_t tmp0, tmp1, tmp2, tmp3;
  114. #ifdef BGEMM
  115. svbfloat16_t svtmp16;
  116. #else
  117. float32x2_t tmp4, tmp5, tmp6, tmp7;
  118. #endif
  119. const int sve_size_bf16 = svcnth();
  120. const int num_accumulators = sve_size_bf16 >> 1;
  121. svbool_t pgtrue = svptrue_b16();
  122. #ifdef BGEMM
  123. // For BF16 load/store we use half the vector size
  124. svbool_t pghalf = svwhilelt_b16(0, num_accumulators);
  125. #endif
  126. // N values are 4x2 packed matrices
  127. int n_step = 0;
  128. const int n2 = n & -2;
  129. const int n4 = n & -4;
  130. // For 256-bit this would be 8
  131. const int m_acc = (m & -num_accumulators);
  132. const int m2 = m & -2;
  133. for (; n_step < n4; n_step += 4) {
  134. ptr_a = (bfloat16_t *)AA;
  135. ptr_c0 = ptr_c;
  136. ptr_c1 = ptr_c0 + ldc;
  137. ptr_c2 = ptr_c1 + ldc;
  138. ptr_c3 = ptr_c2 + ldc;
  139. ptr_c += 4 * ldc;
  140. int m_step = 0;
  141. for (; m_step < m_acc; m_step += num_accumulators) {
  142. svfloat32_t acc0 = svdup_f32(0);
  143. svfloat32_t acc1 = svdup_f32(0);
  144. svfloat32_t acc2 = svdup_f32(0);
  145. svfloat32_t acc3 = svdup_f32(0);
  146. ptr_a0 = ptr_a;
  147. ptr_b0 = ptr_b;
  148. ptr_a += num_accumulators * pad_k;
  149. // Load entire 2VL block
  150. for (BLASLONG p = 0; p < pad_k; p += 4) {
  151. svbfloat16_t ma0 = svld1_bf16(pgtrue, ptr_a0);
  152. svbfloat16_t ma1 = svld1_bf16(pgtrue, ptr_a0 + sve_size_bf16);
  153. svbfloat16_t mb0 = svld1rq_bf16(pgtrue, ptr_b0);
  154. svbfloat16_t mb1 = svld1rq_bf16(pgtrue, ptr_b0 + 8);
  155. acc0 = svbfmmla_f32(acc0, mb0, ma0);
  156. acc1 = svbfmmla_f32(acc1, mb0, ma1);
  157. acc2 = svbfmmla_f32(acc2, mb1, ma0);
  158. acc3 = svbfmmla_f32(acc3, mb1, ma1);
  159. ptr_a0 += sve_size_bf16 * 2;
  160. ptr_b0 += 16;
  161. }
  162. svfloat32_t out0 = svreinterpret_f32_u64(svuzp1_u64(svreinterpret_u64_f32(acc0), svreinterpret_u64_f32(acc1)));
  163. svfloat32_t out1 = svreinterpret_f32_u64(svuzp2_u64(svreinterpret_u64_f32(acc0), svreinterpret_u64_f32(acc1)));
  164. svfloat32_t out2 = svreinterpret_f32_u64(svuzp1_u64(svreinterpret_u64_f32(acc2), svreinterpret_u64_f32(acc3)));
  165. svfloat32_t out3 = svreinterpret_f32_u64(svuzp2_u64(svreinterpret_u64_f32(acc2), svreinterpret_u64_f32(acc3)));
  166. UPDATE_C(pgtrue, ptr_c0, tmp0, out0);
  167. UPDATE_C(pgtrue, ptr_c1, tmp1, out1);
  168. UPDATE_C(pgtrue, ptr_c2, tmp2, out2);
  169. UPDATE_C(pgtrue, ptr_c3, tmp3, out3);
  170. ptr_c0 += num_accumulators;
  171. ptr_c1 += num_accumulators;
  172. ptr_c2 += num_accumulators;
  173. ptr_c3 += num_accumulators;
  174. }
  175. for (; m_step < m2; m_step += 2) {
  176. float32x4_t acc0 = {0,0,0,0};
  177. float32x4_t acc1 = {0,0,0,0};
  178. ptr_a0 = ptr_a;
  179. ptr_b0 = ptr_b;
  180. ptr_a += 2 * pad_k;
  181. for (BLASLONG p = 0; p < pad_k; p += 4) {
  182. bfloat16x8_t ma0 = vld1q_bf16(ptr_a0);
  183. bfloat16x8_t mb0 = vld1q_bf16(ptr_b0);
  184. bfloat16x8_t mb1 = vld1q_bf16(ptr_b0 + 8);
  185. acc0 = vbfmmlaq_f32(acc0, mb0, ma0);
  186. acc1 = vbfmmlaq_f32(acc1, mb1, ma0);
  187. ptr_a0 += 8;
  188. ptr_b0 += 16;
  189. }
  190. UPDATE_C2(ptr_c0, tmp4, vget_low_f32(acc0));
  191. UPDATE_C2(ptr_c1, tmp5, vget_high_f32(acc0));
  192. UPDATE_C2(ptr_c2, tmp6, vget_low_f32(acc1));
  193. UPDATE_C2(ptr_c3, tmp7, vget_high_f32(acc1));
  194. ptr_c0 += 2;
  195. ptr_c1 += 2;
  196. ptr_c2 += 2;
  197. ptr_c3 += 2;
  198. }
  199. // Final row is always a contiguous single row
  200. if (m & 1) {
  201. ptr_a0 = ptr_a;
  202. ptr_b0 = ptr_b;
  203. float32x4_t acc0 = {0,0,0,0};
  204. float32x4_t acc1 = {0,0,0,0};
  205. for (BLASLONG p = 0; p < pad_k; p += 4) {
  206. /// Same A value can be used for both B values
  207. bfloat16x8_t ma0 = vreinterpretq_bf16_u64(vdupq_n_u64(
  208. *((uint64_t*)ptr_a0)
  209. ));
  210. bfloat16x8_t mb0 = vld1q_bf16(ptr_b0);
  211. bfloat16x8_t mb1 = vld1q_bf16(ptr_b0 + 8);
  212. acc0 = vbfmmlaq_f32(acc0, mb0, ma0);
  213. acc1 = vbfmmlaq_f32(acc1, mb1, ma0);
  214. ptr_a0 += 4;
  215. ptr_b0 += 16;
  216. }
  217. UPDATE_C1(ptr_c0, acc0[1]);
  218. UPDATE_C1(ptr_c1, acc0[3]);
  219. UPDATE_C1(ptr_c2, acc1[1]);
  220. UPDATE_C1(ptr_c3, acc1[3]);
  221. }
  222. ptr_b += 4 * pad_k;
  223. }
  224. for (; n_step < n2; n_step += 2) {
  225. ptr_a = (bfloat16_t *)AA;
  226. ptr_c0 = ptr_c;
  227. ptr_c1 = ptr_c0 + ldc;
  228. ptr_c += 2 * ldc;
  229. // Sets of two are contiguously packed so yay
  230. int m_step = 0;
  231. for (; m_step < m_acc; m_step += num_accumulators) {
  232. svfloat32_t acc0 = svdup_f32(0);
  233. svfloat32_t acc1 = svdup_f32(0);
  234. ptr_a0 = ptr_a;
  235. ptr_b0 = ptr_b;
  236. ptr_a += num_accumulators * pad_k;
  237. // Load entire 8x4 block
  238. for (BLASLONG p = 0; p < pad_k; p += 4) {
  239. svbfloat16_t ma0 = svld1_bf16(pgtrue, ptr_a0);
  240. svbfloat16_t ma1 = svld1_bf16(pgtrue, ptr_a0 + sve_size_bf16);
  241. svbfloat16_t mb0 = svld1rq_bf16(pgtrue, ptr_b0);
  242. acc0 = svbfmmla_f32(acc0, mb0, ma0);
  243. acc1 = svbfmmla_f32(acc1, mb0, ma1);
  244. ptr_a0 += sve_size_bf16 * 2;
  245. ptr_b0 += 8;
  246. }
  247. svfloat32_t out0 = svreinterpret_f32_u64(svuzp1_u64(svreinterpret_u64_f32(acc0), svreinterpret_u64_f32(acc1)));
  248. svfloat32_t out1 = svreinterpret_f32_u64(svuzp2_u64(svreinterpret_u64_f32(acc0), svreinterpret_u64_f32(acc1)));
  249. UPDATE_C(pgtrue, ptr_c0, tmp0, out0);
  250. UPDATE_C(pgtrue, ptr_c1, tmp1, out1);
  251. ptr_c0 += num_accumulators;
  252. ptr_c1 += num_accumulators;
  253. }
  254. for (; m_step < m2; m_step += 2) {
  255. float32x4_t acc = {0,0,0,0};
  256. ptr_a0 = ptr_a;
  257. ptr_b0 = ptr_b;
  258. ptr_a += 2 * pad_k;
  259. for (BLASLONG p = 0; p < pad_k; p += 4) {
  260. bfloat16x8_t ma0 = vld1q_bf16(ptr_a0);
  261. bfloat16x8_t mb0 = vld1q_bf16(ptr_b0);
  262. acc = vbfmmlaq_f32(acc, mb0, ma0);
  263. ptr_a0 += 8;
  264. ptr_b0 += 8;
  265. }
  266. UPDATE_C2(ptr_c0, tmp4, vget_low_f32(acc));
  267. UPDATE_C2(ptr_c1, tmp5, vget_high_f32(acc));
  268. ptr_c0 += 2;
  269. ptr_c1 += 2;
  270. }
  271. // Final row is always a contiguous single row
  272. if (m & 1) {
  273. ptr_a0 = ptr_a;
  274. ptr_b0 = ptr_b;
  275. float32x4_t acc = {0,0,0,0};
  276. for (BLASLONG p = 0; p < pad_k; p += 4) {
  277. /// Same A value can be used for both B values
  278. bfloat16x8_t ma0 = vreinterpretq_bf16_u64(vdupq_n_u64(
  279. *((uint64_t*)ptr_a0)
  280. ));
  281. bfloat16x8_t mb0 = vld1q_bf16(ptr_b0);
  282. acc = vbfmmlaq_f32(acc, mb0, ma0);
  283. ptr_a0 += 4;
  284. ptr_b0 += 8;
  285. }
  286. UPDATE_C1(ptr_c0, acc[0]);
  287. UPDATE_C1(ptr_c1, acc[2]);
  288. }
  289. ptr_b += 2 * pad_k;
  290. }
  291. if (n & 1) {
  292. ptr_a = (bfloat16_t *)AA;
  293. ptr_c0 = ptr_c;
  294. int m_step = 0;
  295. for (; m_step < m_acc; m_step += num_accumulators) {
  296. ptr_a0 = ptr_a;
  297. ptr_b0 = ptr_b;
  298. ptr_a += num_accumulators * pad_k;
  299. svfloat32_t acc0 = svdup_f32(0);
  300. svfloat32_t acc1 = svdup_f32(0);
  301. // Load entire 8x4 block
  302. for (BLASLONG p = 0; p < pad_k; p += 4) {
  303. uint64_t* ptr_b0_u64 = (uint64_t*)ptr_b0;
  304. svbfloat16_t ma0 = svld1_bf16(pgtrue, ptr_a0);
  305. svbfloat16_t ma1 = svld1_bf16(pgtrue, ptr_a0 + sve_size_bf16);
  306. svbfloat16_t mb0 = svreinterpret_bf16_u64(svdup_u64(*ptr_b0_u64));
  307. acc0 = svbfmmla_f32(acc0, mb0, ma0);
  308. acc1 = svbfmmla_f32(acc1, mb0, ma1);
  309. ptr_a0 += sve_size_bf16 * 2;
  310. ptr_b0 += 4;
  311. }
  312. svfloat32_t out0 = svreinterpret_f32_u64(svuzp1_u64(svreinterpret_u64_f32(acc0), svreinterpret_u64_f32(acc1)));
  313. UPDATE_C(pgtrue, ptr_c0, tmp0, out0);
  314. ptr_c0 += num_accumulators;
  315. }
  316. for (; m_step < m2; m_step += 2) {
  317. float32x4_t acc = {0, 0, 0, 0};
  318. ptr_a0 = ptr_a;
  319. ptr_b0 = ptr_b;
  320. ptr_a += 2 * pad_k;
  321. for (BLASLONG p = 0; p < pad_k; p += 4) {
  322. bfloat16x8_t ma0 = vld1q_bf16(ptr_a0);
  323. bfloat16x8_t mb0 = vcombine_bf16(vld1_bf16(ptr_b0), vdup_n_bf16(vcvth_bf16_f32(0.0f)));
  324. acc = vbfmmlaq_f32(acc, mb0, ma0);
  325. ptr_a0 += 8;
  326. ptr_b0 += 4;
  327. }
  328. UPDATE_C2(ptr_c0, tmp4, vget_low_f32(acc));
  329. ptr_c0 += 2;
  330. }
  331. if (m & 1) {
  332. ptr_a0 = ptr_a;
  333. ptr_b0 = ptr_b;
  334. float32x2_t acc = {0,0};
  335. for (BLASLONG p = 0; p < pad_k; p += 4) {
  336. bfloat16x4_t ma0 = vld1_bf16(ptr_a0);
  337. bfloat16x4_t mb0 = vld1_bf16(ptr_b0);
  338. acc = vbfdot_f32(acc, ma0, mb0);
  339. ptr_a0 += 4;
  340. ptr_b0 += 4;
  341. }
  342. UPDATE_C1(ptr_c0, acc[0] + acc[1]);
  343. }
  344. }
  345. return 0;
  346. }