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.

fp16_math.cc 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/math/fp16_math.h"
  17. #include "external/register/register_types.h"
  18. namespace ge {
  19. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t sqrt(fp16_t fp) {
  20. fp16_t ret;
  21. // Convert half precision float number to double
  22. double dVal = fp;
  23. // Calculate double number square root
  24. double dSqrt = std::sqrt(dVal);
  25. // calculate result
  26. ret = dSqrt;
  27. return ret;
  28. }
  29. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t rsqrt(fp16_t fp) {
  30. fp16_t ret;
  31. // Convert half precision float number to double
  32. double dVal = fp;
  33. // Calculate double number square root and reciprocal
  34. double drSqrt = 1.0 / std::sqrt(dVal);
  35. // calculate result
  36. ret = drSqrt;
  37. return ret;
  38. }
  39. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t rcp(fp16_t fp) {
  40. fp16_t ret;
  41. // Convert half precision float number to double
  42. double dVal = fp;
  43. // Calculate double number reciprocal
  44. double dRcp = 1.0 / dVal;
  45. // calculate result
  46. ret = dRcp;
  47. return ret;
  48. }
  49. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t exp(fp16_t fp) {
  50. fp16_t ret;
  51. // Convert half precision float number to double
  52. double dVal = fp;
  53. // Calculate double number exponential
  54. double dExp = std::exp(dVal);
  55. // calculate result
  56. ret = dExp;
  57. return ret;
  58. }
  59. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t pow2(fp16_t fp) {
  60. fp16_t ret;
  61. // Convert half precision float number to double
  62. double dVal = fp;
  63. // Calculate double number binary exponential
  64. double dExp2 = std::pow(kDim2, dVal);
  65. // calculate result
  66. ret = dExp2;
  67. return ret;
  68. }
  69. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t pow10(fp16_t fp) {
  70. fp16_t ret;
  71. // Convert half precision float number to double
  72. double dVal = fp;
  73. // Calculate double number decimal exponential
  74. double dExp10 = std::pow(kDim10, dVal);
  75. // calculate result
  76. ret = dExp10;
  77. return ret;
  78. }
  79. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t ln(fp16_t fp) {
  80. fp16_t ret;
  81. // Convert half precision float number to double
  82. double dVal = fp;
  83. // Calculate double number natural logarithm
  84. double dLn = std::log(dVal);
  85. // calculate result
  86. ret = dLn;
  87. return ret;
  88. }
  89. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t log2(fp16_t fp) {
  90. fp16_t ret;
  91. // Convert half precision float number to double
  92. double dVal = fp;
  93. // Calculate double number binary logarithm
  94. double dLog2 = std::log2(dVal);
  95. // calculate result
  96. ret = dLog2;
  97. return ret;
  98. }
  99. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t log10(fp16_t fp) {
  100. fp16_t ret;
  101. // Convert half precision float number to double
  102. double dVal = fp;
  103. // Calculate double number binary logarithm
  104. double dLog10 = std::log10(dVal);
  105. // calculate result
  106. ret = dLog10;
  107. return ret;
  108. }
  109. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t cos(fp16_t fp) {
  110. fp16_t ret;
  111. // Convert half precision float number to double
  112. double dVal = fp;
  113. // Calculate double number cos
  114. double dCos = std::cos(dVal);
  115. // calculate result
  116. ret = dCos;
  117. return ret;
  118. }
  119. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t sin(fp16_t fp) {
  120. fp16_t ret;
  121. // Convert half precision float number to double
  122. double dVal = fp;
  123. // Calculate double number sine
  124. double dSin = std::sin(dVal);
  125. // calculate result
  126. ret = dSin;
  127. return ret;
  128. }
  129. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t abs(fp16_t fp) {
  130. fp16_t ret;
  131. ret.val = (fp.val & kFp16AbsMax);
  132. return ret;
  133. }
  134. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t max(fp16_t fp1, fp16_t fp2) {
  135. if (fp1 >= fp2) {
  136. return fp1;
  137. } else {
  138. return fp2;
  139. }
  140. }
  141. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY fp16_t min(fp16_t fp1, fp16_t fp2) {
  142. if (fp1 <= fp2) {
  143. return fp1;
  144. } else {
  145. return fp2;
  146. }
  147. }
  148. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示