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

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

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