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.

random_ops.h 16 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. /*!
  17. * \file random_ops.h
  18. * \brief
  19. */
  20. #ifndef GE_OP_RANDOM_OPS_H_
  21. #define GE_OP_RANDOM_OPS_H_
  22. #include <vector>
  23. #include "graph/operator_reg.h"
  24. namespace ge {
  25. /**
  26. *@brief Draws samples from a multinomial distribution.
  27. *@par Inputs:
  28. *Inputs include: \n
  29. * @li logits: A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, \n
  30. int64, bfloat16, uint16, half, uint32, uint64. 2-D Tensor with shape [batch_size, num_classes].
  31. * @li num_samples: A Tensor of type int32. 0-D. Number of independent samples to draw for each row slice.
  32. *@par Attributes:
  33. *@li output_dtype: An optional type from: int32, int64. Defaults to int64.
  34. *@li seed: An optional int. Defaults to 0.
  35. *@li seed2: An optional int. Defaults to 0.
  36. *@par Outputs:
  37. *y_indices: A Tensor of type output_dtype.
  38. *@attention Constraints:\n
  39. *-The implementation for Multinomial on Ascend uses AICPU, with bad performance.\n
  40. *@par Third-party framework compatibility
  41. *@li compatible with tensorflow Multinomial operator.
  42. */
  43. REG_OP(Multinomial)
  44. .INPUT(logits, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  45. .INPUT(num_samples, TensorType({DT_INT32}))
  46. .OUTPUT(y, TensorType({DT_INT32, DT_INT64}))
  47. .ATTR(dtype, Type, DT_INT64)
  48. .ATTR(seed, Int, 0)
  49. .ATTR(seed2, Int, 0)
  50. .OP_END_FACTORY_REG(Multinomial)
  51. /**
  52. *@brief Outputs random values from a normal distribution.
  53. *@par Inputs:
  54. *Inputs include: \n
  55. * @li shape: A Tensor. Must be one of the following types: int32, int64. \n
  56. The shape of the output tensor. Batches are indexed by the 0th dimension.
  57. * @li means: A Tensor. Must be one of the following types: half, bfloat16, float32, float64.
  58. * @li stdevs: A Tensor. Must have the same type as means.
  59. * @li min: A Tensor. Must have the same type as means. The minimum cutoff. May be -infinity.
  60. * @li max: A Tensor. Must have the same type as means.
  61. *@par Attributes:
  62. *@li seed: An optional int. Defaults to 0.
  63. *@li seed2: An optional int. Defaults to 0.
  64. *@par Outputs:
  65. *y: A Tensor. Has the same type as means.
  66. *@attention Constraints:\n
  67. *-The implementation for ParameterizedTruncatedNormal on Ascend uses AICPU, with bad performance.\n
  68. *@par Third-party framework compatibility
  69. *@li compatible with tensorflow ParameterizedTruncatedNormal operator.
  70. */
  71. REG_OP(ParameterizedTruncatedNormal)
  72. .INPUT(shape, TensorType({DT_INT32, DT_INT64}))
  73. .INPUT(means, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  74. .INPUT(stdevs, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  75. .INPUT(min, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  76. .INPUT(max, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  77. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  78. .ATTR(seed, Int, 0)
  79. .ATTR(seed2, Int, 0)
  80. .OP_END_FACTORY_REG(ParameterizedTruncatedNormal)
  81. /**
  82. *@brief Computes the derivative of a Gamma random sample w.r.t. alpha.
  83. *@par Inputs:
  84. *Inputs include: \n
  85. * @li alpha: A Tensor. Must be one of the following types: float32, float64.
  86. * @li sample: A Tensor. Must have the same type as alpha.
  87. *@par Outputs:
  88. *y: A Tensor. Has the same type as alpha.
  89. *@attention Constraints:\n
  90. *-The implementation for RandomGammaGrad on Ascend uses AICPU, with bad performance.\n
  91. *@par Third-party framework compatibility
  92. *@li compatible with tensorflow RandomGammaGrad operator.
  93. */
  94. REG_OP(RandomGammaGrad)
  95. .INPUT(alpha, TensorType({DT_FLOAT, DT_DOUBLE}))
  96. .INPUT(sample, TensorType({DT_FLOAT, DT_DOUBLE}))
  97. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  98. .OP_END_FACTORY_REG(RandomGammaGrad)
  99. /**
  100. *@brief Outputs random values from the Gamma distribution(s) described by alpha.
  101. *@par Inputs:
  102. *Inputs include: \n
  103. * @li shape: A Tensor. Must be one of the following types: int32, int64. 1-D integer tensor.
  104. * @li alpha: A Tensor. Must be one of the following types: half, float32, float64.
  105. *@par Attributes:
  106. *@li seed: An optional int. Defaults to 0.
  107. *@li seed2: An optional int. Defaults to 0.
  108. *@par Outputs:
  109. *y: A Tensor. Has the same type as alpha.
  110. *@attention Constraints:\n
  111. *-The implementation for RandomGamma on Ascend uses AICPU, with bad performance.\n
  112. *@par Third-party framework compatibility
  113. *@li compatible with tensorflow RandomGamma operator.
  114. */
  115. REG_OP(RandomGamma)
  116. .INPUT(shape, TensorType({DT_INT32, DT_INT64}))
  117. .INPUT(alpha, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  118. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  119. .ATTR(seed, Int, 0)
  120. .ATTR(seed2, Int, 0)
  121. .OP_END_FACTORY_REG(RandomGamma)
  122. /**
  123. *@brief Outputs random values from the Poisson distribution(s) described by rate.
  124. *@par Inputs:
  125. *Inputs include: \n
  126. * @li shape: A Tensor. Must be one of the following types: int32, int64. 1-D integer tensor.
  127. * @li rate: A Tensor. Must be one of the following types: half, float32, float64, int32, int64.
  128. *@par Attributes:
  129. *@li dtype: An optional type from: half, float32, float64, int32, int64. Defaults to int64.
  130. *@li seed: An optional int. Defaults to 0.
  131. *@li seed2: An optional int. Defaults to 0.
  132. *@par Outputs:
  133. *y: A Tensor of type dtype.
  134. *@attention Constraints:\n
  135. *-The implementation for RandomPoisson on Ascend uses AICPU, with bad performance.\n
  136. *@par Third-party framework compatibility
  137. *@li compatible with tensorflow RandomPoisson operator.
  138. */
  139. REG_OP(RandomPoisson)
  140. .INPUT(shape, TensorType({DT_INT32, DT_INT64}))
  141. .INPUT(rate, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, \
  142. DT_INT32, DT_INT64}))
  143. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE, \
  144. DT_INT32, DT_INT64}))
  145. .ATTR(dtype, Type, DT_INT64)
  146. .ATTR(seed, Int, 0)
  147. .ATTR(seed2, Int, 0)
  148. .OP_END_FACTORY_REG(RandomPoisson)
  149. /**
  150. *@brief Randomly shuffles a tensor along its first dimension.
  151. *@par Inputs:
  152. *Inputs include: \n
  153. *x: A Tensor. The tensor to be shuffled.
  154. *@par Attributes:
  155. *@li seed: An optional int. Defaults to 0.
  156. *@li seed2: An optional int. Defaults to 0.
  157. *@par Outputs:
  158. *y: A Tensor. Has the same type as x.
  159. *@attention Constraints:\n
  160. *-The implementation for RandomShuffle on Ascend uses AICPU, with bad performance.\n
  161. *@par Third-party framework compatibility
  162. *@li compatible with tensorflow RandomShuffle operator.
  163. */
  164. REG_OP(RandomShuffle)
  165. .INPUT(x, TensorType({DT_INT64, DT_INT32, DT_UINT16, DT_INT16,
  166. DT_UINT8, DT_INT8, DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64,
  167. DT_COMPLEX128, DT_BOOL, DT_STRING, DT_RESOURCE}))
  168. .OUTPUT(y, TensorType({DT_INT64, DT_INT32, DT_UINT16, DT_INT16,
  169. DT_UINT8, DT_INT8, DT_FLOAT16, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64,
  170. DT_COMPLEX128, DT_BOOL, DT_STRING, DT_RESOURCE}))
  171. .ATTR(seed, Int, 0)
  172. .ATTR(seed2, Int, 0)
  173. .OP_END_FACTORY_REG(RandomShuffle)
  174. /**
  175. *@brief Outputs random values from a normal distribution.
  176. *@par Inputs:
  177. *Inputs include: \n
  178. *shape: A Tensor. Must be one of the following types: int32, int64. The shape of the output tensor.
  179. *@par Attributes:
  180. *@li dtype: A type from: half, float16, float32, float64. The type of the output.
  181. *@li seed: An optional int. Defaults to 0.
  182. *@li seed2: An optional int. Defaults to 0.
  183. *@par Outputs:
  184. *y: A Tensor of type dtype.
  185. *@attention Constraints:\n
  186. *-The implementation for RandomStandardNormal on Ascend uses AICPU, with bad performance.\n
  187. *@par Third-party framework compatibility
  188. *@li compatible with tensorflow RandomStandardNormal operator.
  189. */
  190. REG_OP(RandomStandardNormal)
  191. .INPUT(shape, TensorType({DT_INT32, DT_INT64}))
  192. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  193. .REQUIRED_ATTR(dtype, Type)
  194. .ATTR(seed, Int, 0)
  195. .ATTR(seed2, Int, 0)
  196. .OP_END_FACTORY_REG(RandomStandardNormal)
  197. /**
  198. *@brief Outputs random integers from a uniform distribution.
  199. *@par Inputs:
  200. *Inputs include: \n
  201. * @li shape: A Tensor. Must be one of the following types: int32, int64. The shape of the output tensor.
  202. * @li min: A Tensor. Must be one of the following types: int32, int64. 0-D.
  203. * @li max: A Tensor. Must have the same type as minval. 0-D.
  204. *@par Attributes:
  205. *@li seed: An optional int. Defaults to 0.
  206. *@li seed2: An optional int. Defaults to 0.
  207. *@par Outputs:
  208. *y: A Tensor. Has the same type as min.
  209. *@attention Constraints:\n
  210. *-The implementation for RandomUniformInt on Ascend uses AICPU, with bad performance.\n
  211. *@par Third-party framework compatibility
  212. *@li compatible with tensorflow RandomUniformInt operator.
  213. */
  214. REG_OP(RandomUniformInt)
  215. .INPUT(shape, TensorType({DT_INT32, DT_INT64}))
  216. .INPUT(min, TensorType({DT_INT32, DT_INT64}))
  217. .INPUT(max, TensorType({DT_INT32, DT_INT64}))
  218. .OUTPUT(y, TensorType({DT_INT32, DT_INT64}))
  219. .ATTR(seed, Int, 0)
  220. .ATTR(seed2, Int, 0)
  221. .OP_END_FACTORY_REG(RandomUniformInt)
  222. /**
  223. *@brief Outputs random values from a uniform distribution.
  224. *@par Inputs:
  225. *Inputs include: \n
  226. *shape: A Tensor. Must be one of the following types: int32, int64. The shape of the output tensor.
  227. *@par Attributes:
  228. *@li dtype: A type from: half, float16, float32, float64. The type of the output.
  229. *@li seed: An optional int. Defaults to 0.
  230. *@li seed2: An optional int. Defaults to 0.
  231. *@par Outputs:
  232. *y: A Tensor of type dtype.
  233. *@attention Constraints:\n
  234. *-The implementation for RandomUniform on Ascend uses AICPU, with bad performance.\n
  235. *@par Third-party framework compatibility
  236. *@li compatible with tensorflow RandomUniform operator.
  237. */
  238. REG_OP(RandomUniform)
  239. .INPUT(shape, TensorType({DT_INT32, DT_INT64}))
  240. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  241. .REQUIRED_ATTR(dtype, Type)
  242. .ATTR(seed, Int, 0)
  243. .ATTR(seed2, Int, 0)
  244. .OP_END_FACTORY_REG(RandomUniform)
  245. /**
  246. *@brief Outputs random values from a truncated normal distribution.
  247. *@par Inputs:
  248. *Inputs include: \n
  249. *shape: A Tensor. Must be one of the following types: int32, int64.
  250. *@par Attributes:
  251. *@li seed: An optional int. Defaults to 0.
  252. *@li seed2: An optional int. Defaults to 0.
  253. *@par Outputs:
  254. *size: A Tensor of types: float16, float32, double.
  255. *@attention Constraints:\n
  256. *-The implementation for TruncatedNormal on Ascend uses AICPU, with bad performance.\n
  257. *@par Third-party framework compatibility
  258. *@li compatible with tensorflow TruncatedNormal operator.
  259. */
  260. REG_OP(TruncatedNormal)
  261. .INPUT(shape, TensorType({ DT_INT32, DT_INT64 }))
  262. .OUTPUT(y, TensorType({ DT_FLOAT16, DT_FLOAT, DT_DOUBLE }))
  263. .ATTR(seed, Int, 0)
  264. .ATTR(seed2, Int, 0)
  265. .OP_END_FACTORY_REG(TruncatedNormal)
  266. /**
  267. *@brief Generate random bit mask for dropout.
  268. *@par Inputs:
  269. include: \n
  270. *@li shape:The shape of the output tensor.
  271. *@li prob:0-D. Number of bit 1.
  272. *@par Attributes:
  273. *@li seed:If either seed or seed2 are set to be non-zero, the random number\n
  274. *generator is seeded by the given seed. Otherwise, it is seeded by a random seed.
  275. *@li seed2:A second seed to avoid seed collision.
  276. *@par Outputs:
  277. *y:Output (1-D) random number using uint data format.
  278. *@attention Constraints:\n
  279. *The output is aligned with 128 bits
  280. *@see DropOutGenMask()
  281. */
  282. REG_OP(DropOutGenMask)
  283. .INPUT(shape, TensorType({ DT_INT32, DT_INT64 }))
  284. .INPUT(prob, TensorType({ DT_FLOAT16, DT_FLOAT }))
  285. .OUTPUT(y, TensorType({ DT_UINT8 }))
  286. .ATTR(seed, Int, 0)
  287. .ATTR(seed2, Int, 0)
  288. .OP_END_FACTORY_REG(DropOutGenMask)
  289. /**
  290. *@brief Generates values in an interval.
  291. *@par Inputs:
  292. * Four ND inputs, including:
  293. *@li assist: A 1D Tensor of type float32.
  294. *@li start: A 1D Tensor of type float32, for the first entry in the range.
  295. *@li stop: A 1D Tensor of type float32, for the last entry in the range.
  296. *@li num: A 1D Tensor of type int32 or int64, for the common difference of the entries.
  297. *@par Outputs:
  298. *output_op: A 1D Tensor of type float32.
  299. *@attention Constraints:
  300. * "input_assist" is a sequence of "input_num" evenly-spaced values beginning at 0 with an common difference of 1.
  301. *@par Third-party framework compatibility
  302. * Compatible with the TensorFlow operator lin_space.
  303. */
  304. REG_OP(LinSpaceD)
  305. .INPUT(assist, TensorType({DT_FLOAT}))
  306. .INPUT(start, TensorType({DT_FLOAT}))
  307. .INPUT(stop, TensorType({DT_FLOAT}))
  308. .INPUT(num, TensorType::IndexNumberType())
  309. .OUTPUT(output, TensorType({DT_FLOAT}))
  310. .OP_END_FACTORY_REG(LinSpaceD)
  311. /**
  312. *@brief Generates values in an interval.
  313. *@par Inputs:
  314. * Four ND inputs, including:
  315. *@li start: A 1D Tensor of type float32, for the first entry in the range.
  316. *@li stop: A 1D Tensor of type float32, for the last entry in the range.
  317. *@li num: A 1D Tensor of type int32 or int64, for the common difference of the entries.
  318. *@par Outputs:
  319. *output_op: A 1D Tensor of type float32.
  320. *@attention Constraints:
  321. * "input_assist" is a sequence of "input_num" evenly-spaced values beginning at 0 with an common difference of 1.
  322. *@par Third-party framework compatibility
  323. * Compatible with the TensorFlow operator lin_space.
  324. */
  325. REG_OP(LinSpace)
  326. .INPUT(start, TensorType({DT_FLOAT, DT_DOUBLE}))
  327. .INPUT(stop, TensorType({DT_FLOAT, DT_DOUBLE}))
  328. .INPUT(num, TensorType::IndexNumberType())
  329. .OUTPUT(output, TensorType({DT_FLOAT, DT_DOUBLE}))
  330. .OP_END_FACTORY_REG(LinSpace)
  331. REG_OP(Dropout)
  332. .INPUT(x, TensorType{DT_FLOAT})
  333. .OUTPUT(y, TensorType{DT_FLOAT})
  334. .ATTR(dropout_ratio, Float, 0.5)
  335. .ATTR(scale_train, Bool, true)
  336. .ATTR(alpha, Float, 1.0)
  337. .ATTR(beta, Float, 0.0)
  338. .OP_END_FACTORY_REG(Dropout)
  339. /**
  340. *@brief Shuffle index of no-zero element.
  341. *@par Inputs:
  342. include: \n
  343. *x:A tensor <= 5-D.
  344. *@par Attributes:
  345. *@li count:the count of output, if 0, out all no-zero elements.
  346. *@li seed:If either seed or seed2 are set to be non-zero, the random number generator is seeded by the given seed.
  347. Otherwise, it is seeded by a random seed.
  348. *@li seed2:A second seed to avoid seed collision.
  349. *@par Outputs:
  350. *@li y:2-D tensor, no-zero element index.
  351. *@li mask:1-D, whether the corresponding index is valid.
  352. *@see RandomChoiceWithMask()
  353. */
  354. REG_OP(RandomChoiceWithMask)
  355. .INPUT(x, TensorType({DT_BOOL}))
  356. .OUTPUT(y, TensorType({DT_INT32}))
  357. .OUTPUT(mask, TensorType({DT_BOOL}))
  358. .ATTR(count, Int, 0)
  359. .ATTR(seed, Int, 0)
  360. .ATTR(seed2, Int, 0)
  361. .OP_END_FACTORY_REG(RandomChoiceWithMask)
  362. /**
  363. *@brief Permutes data in the channel dimension of the input
  364. *@par Inputs:
  365. *Inputs including: \n
  366. * @li x: A required Tensor. Must be one of the following types:
  367. float16, float32, int8, uint8, int16, uint16, int32, uint32, int64, uint64.
  368. *@par Attributes:
  369. *@li group: A required int32, specifying the number of groups to split the channel dimension into. Defaults to "1".
  370. *@par Outputs:
  371. *y: A required Tensor. Has same type and shape as "x". Must be one of the following types:
  372. float16, float32, int8, uint8, int16, uint16, int32, uint32, int64, uint64.
  373. *@attention Constraints:\n
  374. *@li "group" must be greater than 0 and must evenly divide the channel dimension size.
  375. *@li The format of input "x" must be NCHW.
  376. *@par Third-party framework compatibility
  377. * Compatible with the Caffe operator ShuffleChannel.
  378. */
  379. REG_OP(ShuffleChannel)
  380. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT,DT_INT8, DT_UINT8, DT_INT16,
  381. DT_UINT16, DT_INT32, DT_UINT32,DT_INT64,DT_UINT64}))
  382. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT,DT_INT8, DT_UINT8, DT_INT16,
  383. DT_UINT16, DT_INT32, DT_UINT32,DT_INT64,DT_UINT64}))
  384. .ATTR(group, Int, 1)
  385. .OP_END_FACTORY_REG(ShuffleChannel)
  386. } // namespace ge
  387. #endif // GE_OP_RANDOM_OPS_H_

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