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 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /***************************************************************************
  2. Copyright (c) 2020, 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. typedef union
  33. {
  34. unsigned short v;
  35. struct
  36. {
  37. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  38. unsigned short s:1;
  39. unsigned short e:8;
  40. unsigned short m:7;
  41. #else
  42. unsigned short m:7;
  43. unsigned short e:8;
  44. unsigned short s:1;
  45. #endif
  46. } bits;
  47. } bfloat16_bits;
  48. typedef union
  49. {
  50. float v;
  51. struct
  52. {
  53. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  54. uint32_t s:1;
  55. uint32_t e:8;
  56. uint32_t m:23;
  57. #else
  58. uint32_t m:23;
  59. uint32_t e:8;
  60. uint32_t s:1;
  61. #endif
  62. } bits;
  63. } float32_bits;
  64. float
  65. float16to32 (bfloat16_bits f16)
  66. {
  67. float32_bits f32;
  68. f32.bits.s = f16.bits.s;
  69. f32.bits.e = f16.bits.e;
  70. f32.bits.m = (uint32_t) f16.bits.m << 16;
  71. return f32.v;
  72. }
  73. int
  74. main (int argc, char *argv[])
  75. {
  76. int m, n, k;
  77. int i, j, l;
  78. int x;
  79. int ret = 0;
  80. int loop = 100;
  81. char transA = 'N', transB = 'N';
  82. float alpha = 1.0, beta = 0.0;
  83. for (x = 0; x <= loop; x++)
  84. {
  85. m = k = n = x;
  86. float A[m * k];
  87. float B[k * n];
  88. float C[m * n];
  89. bfloat16_bits AA[m * k], BB[k * n];
  90. float DD[m * n], CC[m * n];
  91. for (j = 0; j < m; j++)
  92. {
  93. for (i = 0; i < m; i++)
  94. {
  95. A[j * k + i] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5;
  96. B[j * k + i] = ((FLOAT) rand () / (FLOAT) RAND_MAX) + 0.5;
  97. C[j * k + i] = 0;
  98. AA[j * k + i].v = *(uint32_t *) & A[j * k + i] >> 16;
  99. BB[j * k + i].v = *(uint32_t *) & B[j * k + i] >> 16;
  100. CC[j * k + i] = 0;
  101. DD[j * k + i] = 0;
  102. }
  103. }
  104. SGEMM (&transA, &transB, &m, &n, &k, &alpha, A,
  105. &m, B, &k, &beta, C, &m);
  106. SBGEMM (&transA, &transB, &m, &n, &k, &alpha, AA,
  107. &m, BB, &k, &beta, CC, &m);
  108. for (i = 0; i < n; i++)
  109. for (j = 0; j < m; j++)
  110. for (l = 0; l < k; l++)
  111. if (fabs (CC[i * m + j] - C[i * m + j]) > 1.0)
  112. ret++;
  113. if (transA == 'N' && transB == 'N')
  114. {
  115. for (i = 0; i < n; i++)
  116. for (j = 0; j < m; j++)
  117. for (l = 0; l < k; l++)
  118. {
  119. DD[i * m + j] +=
  120. float16to32 (AA[l * m + j]) * float16to32 (BB[l + k * i]);
  121. }
  122. for (i = 0; i < n; i++)
  123. for (j = 0; j < m; j++)
  124. for (l = 0; l < k; l++)
  125. if (CC[i * m + j] != DD[i * m + j])
  126. ret++;
  127. }
  128. }
  129. if (ret != 0)
  130. fprintf (stderr, "FATAL ERROR SBGEMM - Return code: %d\n", ret);
  131. return ret;
  132. }