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

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