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_bgemm.c 5.6 kB

2 months ago
2 months ago
2 months ago
2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 SUBSTITUTE
  22. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  25. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *****************************************************************************/
  27. #include "../common.h"
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include "test_helpers.h"
  31. #define SGEMM BLASFUNC(sgemm)
  32. #define BGEMM BLASFUNC(bgemm)
  33. #define BGEMM_LARGEST 256
  34. int
  35. main (int argc, char *argv[])
  36. {
  37. blasint m, n, k;
  38. int i, j, l;
  39. blasint x, y;
  40. blasint one = 1;
  41. int ret = 0;
  42. int loop = BGEMM_LARGEST;
  43. char transA = 'N', transB = 'N';
  44. float alpha = 1.0, beta = 0.0;
  45. bfloat16 alpha_bf16;
  46. sbstobf16_(&one, &alpha, &one, &alpha_bf16, &one);
  47. bfloat16 beta_bf16;
  48. sbstobf16_(&one, &beta, &one, &beta_bf16, &one);
  49. for (x = 0; x <= loop; x++)
  50. {
  51. if ((x > 100) && (x != BGEMM_LARGEST)) continue;
  52. m = k = n = x;
  53. float *A = (float *)malloc_safe(m * k * sizeof(FLOAT));
  54. float *B = (float *)malloc_safe(k * n * sizeof(FLOAT));
  55. float *C = (float *)malloc_safe(m * n * sizeof(FLOAT));
  56. bfloat16 *AA = (bfloat16 *)malloc_safe(m * k * sizeof(bfloat16));
  57. bfloat16 *BB = (bfloat16 *)malloc_safe(k * n * sizeof(bfloat16));
  58. bfloat16 *CC = (bfloat16 *)malloc_safe(k * n * sizeof(bfloat16));
  59. FLOAT *DD = (FLOAT *)malloc_safe(m * n * sizeof(FLOAT));
  60. if ((A == NULL) || (B == NULL) || (C == NULL) || (AA == NULL) || (BB == NULL) ||
  61. (DD == NULL) || (CC == NULL))
  62. return 1;
  63. for (j = 0; j < m; j++)
  64. {
  65. for (i = 0; i < k; i++)
  66. {
  67. A[j * k + i] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5;
  68. sbstobf16_(&one, &A[j*k+i], &one, &AA[j * k + i], &one);
  69. }
  70. }
  71. for (j = 0; j < n; j++)
  72. {
  73. for (i = 0; i < k; i++)
  74. {
  75. B[j * k + i] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5;
  76. sbstobf16_(&one, &B[j*k+i], &one, &BB[j * k + i], &one);
  77. }
  78. }
  79. for (y = 0; y < 4; y++)
  80. {
  81. if ((y == 0) || (y == 2)) {
  82. transA = 'N';
  83. } else {
  84. transA = 'T';
  85. }
  86. if ((y == 0) || (y == 1)) {
  87. transB = 'N';
  88. } else {
  89. transB = 'T';
  90. }
  91. memset(CC, 0, m * n * sizeof(bfloat16));
  92. memset(DD, 0, m * n * sizeof(FLOAT));
  93. memset(C, 0, m * n * sizeof(FLOAT));
  94. SGEMM (&transA, &transB, &m, &n, &k, &alpha, A,
  95. &m, B, &k, &beta, C, &m);
  96. BGEMM (&transA, &transB, &m, &n, &k, &alpha_bf16, (bfloat16*) AA,
  97. &m, (bfloat16*)BB, &k, &beta_bf16, (bfloat16*)CC, &m);
  98. for (i = 0; i < n; i++)
  99. for (j = 0; j < m; j++)
  100. {
  101. for (l = 0; l < k; l++)
  102. if (transA == 'N' && transB == 'N')
  103. {
  104. DD[i * m + j] +=
  105. float16to32 (AA[l * m + j]) * float16to32 (BB[l + k * i]);
  106. } else if (transA == 'T' && transB == 'N')
  107. {
  108. DD[i * m + j] +=
  109. float16to32 (AA[k * j + l]) * float16to32 (BB[l + k * i]);
  110. } else if (transA == 'N' && transB == 'T')
  111. {
  112. DD[i * m + j] +=
  113. float16to32 (AA[l * m + j]) * float16to32 (BB[i + l * n]);
  114. } else if (transA == 'T' && transB == 'T')
  115. {
  116. DD[i * m + j] +=
  117. float16to32 (AA[k * j + l]) * float16to32 (BB[i + l * n]);
  118. }
  119. if (!is_close(float16to32(CC[i * m + j]), truncate_float32_to_bfloat16(C[i * m + j]), 0.01, 0.001)) {
  120. #ifdef DEBUG
  121. printf("Mismatch at i=%d, j=%d, k=%ld: CC=%.6f, C=%.6f\n",
  122. i, j, k, float16to32(CC[i * m + j]), truncate_float32_to_bfloat16(C[i * m + j]));
  123. #endif
  124. ret++;
  125. }
  126. if (!is_close(float16to32(CC[i * m + j]), truncate_float32_to_bfloat16(DD[i * m + j]), 0.0001, 0.00001)) {
  127. #ifdef DEBUG
  128. printf("Mismatch at i=%d, j=%d, k=%ld: CC=%.6f, DD=%.6f\n",
  129. i, j, k, float16to32(CC[i * m + j]), truncate_float32_to_bfloat16(DD[i * m + j]));
  130. #endif
  131. ret++;
  132. }
  133. }
  134. }
  135. free(A);
  136. free(B);
  137. free(C);
  138. free(AA);
  139. free(BB);
  140. free(CC);
  141. free(DD);
  142. }
  143. if (ret != 0) {
  144. fprintf (stderr, "FATAL ERROR BGEMM - Return code: %d\n", ret);
  145. }
  146. return ret;
  147. }