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.

stateful_random_ops.h 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * Copyright 2019-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. #ifndef GE_OP_STATEFUL_RANDOM_OPS_H
  17. #define GE_OP_STATEFUL_RANDOM_OPS_H
  18. #include "graph/operator.h"
  19. #include "graph/operator_reg.h"
  20. namespace ge {
  21. /**
  22. *@brief Non-deterministically generates some integers.
  23. *@par Inputs:
  24. *This op may use some OS-provided source of non-determinism (e.g. an RNG), \n
  25. *so each execution will give different results. Inputs included:
  26. *@li shape: The shape of the output tensor.
  27. *@par Outputs:
  28. *y:A Returns Non-deterministic integer values with specified shape.
  29. *@par Third-party framework compatibility
  30. *Compatible with tensorflow NonDeterministicInts operator.
  31. */
  32. REG_OP(NonDeterministicInts)
  33. .INPUT(shape, TensorType({DT_INT32,DT_INT64}))
  34. .OUTPUT(y, TensorType({DT_INT32,DT_INT64}))
  35. .REQUIRED_ATTR(dtype, Type)
  36. .OP_END_FACTORY_REG(NonDeterministicInts)
  37. /**
  38. *@brief Advance the counter of a counter-based RNG. The state of the RNG after \n
  39. *`rng_skip(n)` will be the same as that after `stateful_uniform([n])` \n
  40. *(or any other distribution). The actual increment added to the \n
  41. *counter is an unspecified implementation detail.
  42. *@par Inputs:
  43. *@li resource: The handle of the resource variable that stores the state of the RNG.
  44. *@li algorithm: The RNG algorithm.
  45. *@li delta: The amount of advancement.
  46. *@par Outputs:
  47. *y:A Returns the created operation.
  48. *@par Third-party framework compatibility
  49. * Compatible with tensorflow RngSkip operator.
  50. */
  51. REG_OP(RngSkip)
  52. .INPUT(x, TensorType({DT_RESOURCE}))
  53. .INPUT(algorithm, TensorType({DT_INT64}))
  54. .INPUT(delta, TensorType({DT_INT64}))
  55. .OP_END_FACTORY_REG(RngSkip)
  56. /**
  57. *@brief Outputs random integers from a uniform distribution. \n
  58. The generated values are uniform integers in the range `[minval, maxval)`. \n
  59. The lower bound `minval` is included in the range, while the upper bound \n
  60. `maxval` is excluded. \n
  61. The random integers are slightly biased unless `maxval - minval` is an exact \n
  62. power of two. The bias is small for values of `maxval - minval` significantly \n
  63. smaller than the range of the output (either `2^32` or `2^64`).
  64. *@par Inputs:
  65. *@li resource: The handle of the resource variable that stores the state of the RNG.
  66. *@li algorithm: The RNG algorithm.
  67. *@li shape: The shape of the output tensor.
  68. *@li minval: Minimum value (inclusive, scalar).
  69. *@li maxval: Maximum value (exclusive, scalar).
  70. *@par Outputs:
  71. *y:A Returns Random values with specified shape.
  72. *@par Third-party framework compatibility
  73. * Compatible with tensorflow StatefulRandomBinomial operator.
  74. */
  75. REG_OP(StatefulRandomBinomial)
  76. .INPUT(x, TensorType({DT_RESOURCE}))
  77. .INPUT(algorithm, TensorType({DT_INT64}))
  78. .INPUT(shape, TensorType({DT_INT32}))
  79. .INPUT(counts, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  80. .INPUT(probs, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  81. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_INT32, DT_INT64}))
  82. .REQUIRED_ATTR(dtype, Type)
  83. .OP_END_FACTORY_REG(StatefulRandomBinomial)
  84. /**
  85. *@brief Outputs random values from a normal distribution. \n
  86. *The generated values will have mean 0 and standard deviation 1.
  87. *@par Inputs:
  88. *@li resource: The handle of the resource variable that stores the state of the RNG.
  89. *@li algorithm: The RNG algorithm.
  90. *@li shape: The shape of the output tensor.
  91. *@par Outputs:
  92. *y:A Returns A tensor of the specified shape filled with random normal values.
  93. *@par Third-party framework compatibility
  94. * Compatible with tensorflow StatefulStandardNormalV2 operator.
  95. */
  96. REG_OP(StatefulStandardNormalV2)
  97. .INPUT(x, TensorType({DT_RESOURCE}))
  98. .INPUT(algorithm, TensorType({DT_INT64}))
  99. .INPUT(shape, TensorType({DT_INT32,DT_INT64}))
  100. .OUTPUT(y, TensorType({DT_FLOAT}))
  101. .OP_END_FACTORY_REG(StatefulStandardNormalV2)
  102. /**
  103. *@brief Outputs random values from a truncated normal distribution. \n
  104. *The generated values follow a normal distribution with mean 0 and standard \n
  105. *deviation 1, except that values whose magnitude is more than 2 standard \n
  106. *deviations from the mean are dropped and re-picked.
  107. *@par Inputs:
  108. *@li resource: The handle of the resource variable that stores the state of the RNG.
  109. *@li algorithm: The RNG algorithm.
  110. *@li shape: The shape of the output tensor.
  111. *@par Outputs:
  112. *y:A Returns Random values with specified shape.
  113. *@par Third-party framework compatibility
  114. * Compatible with tensorflow StatefulTruncatedNormal operator.
  115. */
  116. REG_OP(StatefulTruncatedNormal)
  117. .INPUT(x, TensorType({DT_RESOURCE}))
  118. .INPUT(algorithm, TensorType({DT_INT64}))
  119. .INPUT(shape, TensorType({DT_INT32,DT_INT64}))
  120. .OUTPUT(y, TensorType({DT_FLOAT}))
  121. .OP_END_FACTORY_REG(StatefulTruncatedNormal)
  122. /**
  123. *@brief Outputs random values from a uniform distribution. \n
  124. The generated values follow a uniform distribution in the range `[0, 1)`. The \n
  125. lower bound 0 is included in the range, while the upper bound 1 is excluded. \n
  126. *@par Inputs:
  127. *@li resource: The handle of the resource variable that stores the state of the RNG.
  128. *@li algorithm: The RNG algorithm.
  129. *@li shape: The shape of the output tensor.
  130. *@par Outputs:
  131. *y:A Returns Random values with specified shape.
  132. *@par Third-party framework compatibility
  133. * Compatible with tensorflow StatefulUniform operator.
  134. */
  135. REG_OP(StatefulUniform)
  136. .INPUT(x, TensorType({DT_RESOURCE}))
  137. .INPUT(algorithm, TensorType({DT_INT64}))
  138. .INPUT(shape, TensorType({DT_INT32,DT_INT64}))
  139. .OUTPUT(y, TensorType({DT_FLOAT}))
  140. .OP_END_FACTORY_REG(StatefulUniform)
  141. /**
  142. *@brief Outputs random integers from a uniform distribution. \n
  143. The generated values are uniform integers covering the whole range of `dtype`.
  144. *@par Inputs:
  145. *@li resource: The handle of the resource variable that stores the state of the RNG.
  146. *@li algorithm: The RNG algorithm.
  147. *@li shape: The shape of the output tensor.
  148. *@par Outputs:
  149. *y:A Returns Random values with specified shape.
  150. *@par Third-party framework compatibility
  151. * Compatible with tensorflow StatefulUniformFullInt operator.
  152. */
  153. REG_OP(StatefulUniformFullInt)
  154. .INPUT(x, TensorType({DT_RESOURCE}))
  155. .INPUT(algorithm, TensorType({DT_INT64}))
  156. .INPUT(shape, TensorType({DT_INT32,DT_INT64}))
  157. .OUTPUT(y, TensorType({DT_UINT64}))
  158. .OP_END_FACTORY_REG(StatefulUniformFullInt)
  159. /**
  160. *@brief Outputs random integers from a uniform distribution. \n
  161. The generated values are uniform integers in the range `[minval, maxval)`. \n
  162. The lower bound `minval` is included in the range, while the upper bound \n
  163. `maxval` is excluded. \n
  164. The random integers are slightly biased unless `maxval - minval` is an exact \n
  165. power of two. The bias is small for values of `maxval - minval` significantly \n
  166. smaller than the range of the output (either `2^32` or `2^64`).
  167. *@par Inputs:
  168. *@li resource: The handle of the resource variable that stores the state of the RNG.
  169. *@li algorithm: The RNG algorithm.
  170. *@li shape: The shape of the output tensor.
  171. *@li minval: Minimum value (inclusive, scalar).
  172. *@li maxval: Maximum value (exclusive, scalar).
  173. *@par Outputs:
  174. *y:A Returns Random values with specified shape.
  175. *@par Third-party framework compatibility
  176. * Compatible with tensorflow StatefulUniformInt operator.
  177. */
  178. REG_OP(StatefulUniformInt)
  179. .INPUT(x, TensorType({DT_RESOURCE}))
  180. .INPUT(algorithm, TensorType({DT_INT64}))
  181. .INPUT(shape, TensorType({DT_INT32,DT_INT64}))
  182. .INPUT(minval, TensorType({DT_INT64}))
  183. .INPUT(maxval, TensorType({DT_INT64}))
  184. .OUTPUT(y, TensorType({DT_INT64}))
  185. .OP_END_FACTORY_REG(StatefulUniformInt)
  186. } // namespace ge
  187. #endif //GE_OP_STATELESS_RANDOM_OPS_H

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