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

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

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