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.

compare_sgemm_sbgemm.c 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /***************************************************************************
  2. Copyright (c) 2020,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 CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *****************************************************************************/
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include "../common.h"
  30. #define SGEMM BLASFUNC(sgemm)
  31. #define SBGEMM BLASFUNC(sbgemm)
  32. #define SGEMV BLASFUNC(sgemv)
  33. #define SBGEMV BLASFUNC(sbgemv)
  34. typedef union
  35. {
  36. unsigned short v;
  37. #if defined(_AIX)
  38. struct __attribute__((packed))
  39. #else
  40. struct
  41. #endif
  42. {
  43. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  44. unsigned short s:1;
  45. unsigned short e:8;
  46. unsigned short m:7;
  47. #else
  48. unsigned short m:7;
  49. unsigned short e:8;
  50. unsigned short s:1;
  51. #endif
  52. } bits;
  53. } bfloat16_bits;
  54. typedef union
  55. {
  56. float v;
  57. #if defined(_AIX)
  58. struct __attribute__((packed))
  59. #else
  60. struct
  61. #endif
  62. {
  63. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  64. uint32_t s:1;
  65. uint32_t e:8;
  66. uint32_t m:23;
  67. #else
  68. uint32_t m:23;
  69. uint32_t e:8;
  70. uint32_t s:1;
  71. #endif
  72. } bits;
  73. } float32_bits;
  74. float
  75. float16to32 (bfloat16_bits f16)
  76. {
  77. float32_bits f32;
  78. f32.bits.s = f16.bits.s;
  79. f32.bits.e = f16.bits.e;
  80. f32.bits.m = (uint32_t) f16.bits.m << 16;
  81. return f32.v;
  82. }
  83. #define SBGEMM_LARGEST 256
  84. void *malloc_safe(size_t size)
  85. {
  86. if (size == 0)
  87. return malloc(1);
  88. else
  89. return malloc(size);
  90. }
  91. int
  92. main (int argc, char *argv[])
  93. {
  94. blasint m, n, k;
  95. int i, j, l;
  96. blasint x, y;
  97. int ret = 0;
  98. int loop = SBGEMM_LARGEST;
  99. char transA = 'N', transB = 'N';
  100. float alpha = 1.0, beta = 0.0;
  101. for (x = 0; x <= loop; x++)
  102. {
  103. if ((x > 100) && (x != SBGEMM_LARGEST)) continue;
  104. m = k = n = x;
  105. float *A = (float *)malloc_safe(m * k * sizeof(FLOAT));
  106. float *B = (float *)malloc_safe(k * n * sizeof(FLOAT));
  107. float *C = (float *)malloc_safe(m * n * sizeof(FLOAT));
  108. bfloat16_bits *AA = (bfloat16_bits *)malloc_safe(m * k * sizeof(bfloat16_bits));
  109. bfloat16_bits *BB = (bfloat16_bits *)malloc_safe(k * n * sizeof(bfloat16_bits));
  110. float *DD = (float *)malloc_safe(m * n * sizeof(FLOAT));
  111. float *CC = (float *)malloc_safe(m * n * sizeof(FLOAT));
  112. if ((A == NULL) || (B == NULL) || (C == NULL) || (AA == NULL) || (BB == NULL) ||
  113. (DD == NULL) || (CC == NULL))
  114. return 1;
  115. bfloat16 atmp,btmp;
  116. blasint one=1;
  117. for (j = 0; j < m; j++)
  118. {
  119. for (i = 0; i < k; i++)
  120. {
  121. A[j * k + i] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5;
  122. sbstobf16_(&one, &A[j*k+i], &one, &atmp, &one);
  123. AA[j * k + i].v = atmp;
  124. }
  125. }
  126. for (j = 0; j < n; j++)
  127. {
  128. for (i = 0; i < k; i++)
  129. {
  130. B[j * k + i] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5;
  131. sbstobf16_(&one, &B[j*k+i], &one, &btmp, &one);
  132. BB[j * k + i].v = btmp;
  133. }
  134. }
  135. for (y = 0; y < 4; y++)
  136. {
  137. if ((y == 0) || (y == 2)) {
  138. transA = 'N';
  139. } else {
  140. transA = 'T';
  141. }
  142. if ((y == 0) || (y == 1)) {
  143. transB = 'N';
  144. } else {
  145. transB = 'T';
  146. }
  147. memset(CC, 0, m * n * sizeof(FLOAT));
  148. memset(DD, 0, m * n * sizeof(FLOAT));
  149. memset(C, 0, m * n * sizeof(FLOAT));
  150. SGEMM (&transA, &transB, &m, &n, &k, &alpha, A,
  151. &m, B, &k, &beta, C, &m);
  152. SBGEMM (&transA, &transB, &m, &n, &k, &alpha, (bfloat16*) AA,
  153. &m, (bfloat16*)BB, &k, &beta, CC, &m);
  154. for (i = 0; i < n; i++)
  155. for (j = 0; j < m; j++)
  156. {
  157. for (l = 0; l < k; l++)
  158. if (transA == 'N' && transB == 'N')
  159. {
  160. DD[i * m + j] +=
  161. float16to32 (AA[l * m + j]) * float16to32 (BB[l + k * i]);
  162. } else if (transA == 'T' && transB == 'N')
  163. {
  164. DD[i * m + j] +=
  165. float16to32 (AA[k * j + l]) * float16to32 (BB[l + k * i]);
  166. } else if (transA == 'N' && transB == 'T')
  167. {
  168. DD[i * m + j] +=
  169. float16to32 (AA[l * m + j]) * float16to32 (BB[i + l * n]);
  170. } else if (transA == 'T' && transB == 'T')
  171. {
  172. DD[i * m + j] +=
  173. float16to32 (AA[k * j + l]) * float16to32 (BB[i + l * n]);
  174. }
  175. if (fabs (CC[i * m + j] - C[i * m + j]) > 1.0)
  176. ret++;
  177. if (fabs (CC[i * m + j] - DD[i * m + j]) > 1.0)
  178. ret++;
  179. }
  180. }
  181. free(A);
  182. free(B);
  183. free(C);
  184. free(AA);
  185. free(BB);
  186. free(DD);
  187. free(CC);
  188. }
  189. if (ret != 0) {
  190. fprintf (stderr, "FATAL ERROR SBGEMM - Return code: %d\n", ret);
  191. return ret;
  192. }
  193. for (beta = 0; beta < 3; beta += 1) {
  194. for (alpha = 0; alpha < 3; alpha += 1) {
  195. for (l = 0; l < 2; l++) { // l = 1 to test inc_x & inc_y not equal to one.
  196. for (x = 1; x <= loop; x++)
  197. {
  198. k = (x == 0) ? 0 : l + 1;
  199. float *A = (float *)malloc_safe(x * x * sizeof(FLOAT));
  200. float *B = (float *)malloc_safe(x * sizeof(FLOAT) << l);
  201. float *C = (float *)malloc_safe(x * sizeof(FLOAT) << l);
  202. bfloat16_bits *AA = (bfloat16_bits *)malloc_safe(x * x * sizeof(bfloat16_bits));
  203. bfloat16_bits *BB = (bfloat16_bits *)malloc_safe(x * sizeof(bfloat16_bits) << l);
  204. float *DD = (float *)malloc_safe(x * sizeof(FLOAT));
  205. float *CC = (float *)malloc_safe(x * sizeof(FLOAT) << l);
  206. if ((A == NULL) || (B == NULL) || (C == NULL) || (AA == NULL) || (BB == NULL) ||
  207. (DD == NULL) || (CC == NULL))
  208. return 1;
  209. bfloat16 atmp, btmp;
  210. blasint one = 1;
  211. for (j = 0; j < x; j++)
  212. {
  213. for (i = 0; i < x; i++)
  214. {
  215. A[j * x + i] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5;
  216. sbstobf16_(&one, &A[j*x+i], &one, &atmp, &one);
  217. AA[j * x + i].v = atmp;
  218. }
  219. B[j << l] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5;
  220. sbstobf16_(&one, &B[j << l], &one, &btmp, &one);
  221. BB[j << l].v = btmp;
  222. CC[j << l] = C[j << l] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5;
  223. }
  224. for (y = 0; y < 2; y++)
  225. {
  226. if (y == 0) {
  227. transA = 'N';
  228. } else {
  229. transA = 'T';
  230. }
  231. memset(CC, 0, x * sizeof(FLOAT) << l);
  232. memset(DD, 0, x * sizeof(FLOAT));
  233. memset(C, 0, x * sizeof(FLOAT) << l);
  234. SGEMV (&transA, &x, &x, &alpha, A, &x, B, &k, &beta, C, &k);
  235. SBGEMV (&transA, &x, &x, &alpha, (bfloat16*) AA, &x, (bfloat16*) BB, &k, &beta, CC, &k);
  236. for (int i = 0; i < x; i ++) DD[i] *= beta;
  237. for (j = 0; j < x; j++)
  238. for (i = 0; i < x; i++)
  239. if (transA == 'N') {
  240. DD[i] += alpha * float16to32 (AA[j * x + i]) * float16to32 (BB[j << l]);
  241. } else if (transA == 'T') {
  242. DD[j] += alpha * float16to32 (AA[j * x + i]) * float16to32 (BB[i << l]);
  243. }
  244. for (j = 0; j < x; j++) {
  245. if (fabs (CC[j << l] - C[j << l]) > 1.0)
  246. ret++;
  247. if (fabs (CC[j << l] - DD[j]) > 1.0)
  248. ret++;
  249. }
  250. }
  251. free(A);
  252. free(B);
  253. free(C);
  254. free(AA);
  255. free(BB);
  256. free(DD);
  257. free(CC);
  258. } // x
  259. } // l
  260. } // alpha
  261. } // beta
  262. if (ret != 0)
  263. fprintf (stderr, "FATAL ERROR SBGEMV - Return code: %d\n", ret);
  264. return ret;
  265. }