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.

candidate_sampling_ops.h 17 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 candidate_sampling_ops.h
  18. * \brief
  19. */
  20. #ifndef OPS_BUILT_IN_OP_PROTO_INC_CANDIDATE_SAMPLING_OPS_H_
  21. #define OPS_BUILT_IN_OP_PROTO_INC_CANDIDATE_SAMPLING_OPS_H_
  22. #include "graph/operator_reg.h"
  23. namespace ge {
  24. /**
  25. *@brief Generates labels for candidate sampling with
  26. a learned unigram distribution. \n
  27. *@par Inputs:
  28. *Input "true_classes" is a 2D matrix.
  29. *true_classes: A "batch_size * num_true" matrix, in which each row contains
  30. the IDs of the "num_true" "target_classes" in the corresponding original label. \n
  31. *@par Attributes:
  32. *@li num_true: Number of true labels per context.
  33. *@li num_sampled: Number of candidates to randomly sample.
  34. *@li unique: If "unique" is true, samples with rejection,
  35. so that all sampled candidates in a batch are unique.
  36. *This requires some approximation to estimate the post-rejection
  37. sampling probabilities.
  38. *@li range_max: The sampler will sample integers from the interval
  39. [0, range_max).
  40. *@li seed: If either "seed" or "seed2" are set to be non-zero.
  41. *@li seed2: A second seed to avoid seed collision. \n
  42. *@par Outputs:
  43. *@li sampled_candidates: A vector of length "num_sampled", in which each
  44. element is the ID of a sampled candidate.
  45. *@li true_expected_count: A "batch_size * num_true" matrix, representing
  46. the number of times each candidate is expected to occur in a batch of sampled
  47. candidates. If "unique" is true, then this is a probability.
  48. *@li sampled_expected_count: A vector of length "num_sampled",
  49. for each sampled candidate.
  50. *representing the number of times the candidate is expected to occur
  51. in a batch of sampled candidates.
  52. * If "unique" is true, then this is a probability.
  53. *@attention Constraints:
  54. *ThreadUnsafeUnigramCandidateSampler runs on the Ascend AI CPU,
  55. which delivers poor performance. \n
  56. *@par Third-party framework compatibility
  57. *Compatible with the TensorFlow operator ThreadUnsafeUnigramCandidateSampler. \n
  58. *@par Restrictions:
  59. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  60. */
  61. REG_OP(ThreadUnsafeUnigramCandidateSampler)
  62. .INPUT(true_classes, TensorType({ DT_INT64 }))
  63. .OUTPUT(sampled_candidates, TensorType({ DT_INT64 }))
  64. .OUTPUT(true_expected_count, TensorType({ DT_FLOAT }))
  65. .OUTPUT(sampled_expected_count, TensorType({ DT_FLOAT }))
  66. .REQUIRED_ATTR(num_true, Int)
  67. .REQUIRED_ATTR(num_sampled, Int)
  68. .REQUIRED_ATTR(unique, Bool)
  69. .REQUIRED_ATTR(range_max, Int)
  70. .ATTR(seed, Int, 0)
  71. .ATTR(seed2, Int, 0)
  72. .OP_END_FACTORY_REG(ThreadUnsafeUnigramCandidateSampler)
  73. /**
  74. *@brief Generates labels for candidate sampling with a learned
  75. unigram distribution. \n
  76. *@par Inputs:
  77. *true_classes: A "batch_size * num_true" matrix, in which each row contains
  78. the IDs of the "num_true" "target_classes" in the corresponding original label.
  79. *Input "true_classes" is a 2D matrix. \n
  80. *@par Attributes:
  81. *@li num_true: Number of true labels per context.
  82. *@li num_sampled: Number of candidates to randomly sample.
  83. *@li unique: If "unique" is true, samples with rejection,
  84. so that all sampled candidates in a batch are unique.
  85. *This requires some approximation to estimate the post-rejection
  86. sampling probabilities.
  87. *@li range_max: The sampler will sample integers from the interval
  88. [0, range_max).
  89. *@li seed: If either "seed" or "seed2" are set to be non-zero.
  90. *@li seed2: A second seed to avoid seed collision. \n
  91. *@par Outputs:
  92. *@li sampled_candidates: A vector of length "num_sampled",
  93. in which each element is the ID of a sampled candidate.
  94. *@li true_expected_count: A "batch_size * num_true" matrix, representing the
  95. number of times each candidate is expected to occur
  96. in a batch of sampled candidates.
  97. *If "unique" is true, then this is a probability.
  98. *@li sampled_expected_count: A vector of length "num_sampled", for each
  99. sampled candidate representing the number of times.
  100. * the candidate is expected to occur in a batch of sampled candidates.
  101. *If "unique" is true, then this is a probability. \n
  102. *@attention Constraints:
  103. *UniformCandidateSampler runs on the Ascend AI CPU,
  104. which delivers poor performance. \n
  105. *@par Third-party framework compatibility
  106. *Compatible with the TensorFlow operator UniformCandidateSampler. \n
  107. *@par Restrictions:
  108. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  109. */
  110. REG_OP(UniformCandidateSampler)
  111. .INPUT(true_classes, TensorType({ DT_INT64 }))
  112. .OUTPUT(sampled_candidates, TensorType({ DT_INT64 }))
  113. .OUTPUT(true_expected_count, TensorType({ DT_FLOAT }))
  114. .OUTPUT(sampled_expected_count, TensorType({ DT_FLOAT }))
  115. .REQUIRED_ATTR(num_true, Int)
  116. .REQUIRED_ATTR(num_sampled, Int)
  117. .REQUIRED_ATTR(unique, Bool)
  118. .REQUIRED_ATTR(range_max, Int)
  119. .ATTR(seed, Int, 0)
  120. .ATTR(seed2, Int, 0)
  121. .OP_END_FACTORY_REG(UniformCandidateSampler)
  122. /**
  123. *@brief Generates labels for candidate sampling with a learned
  124. unigram distribution. \n
  125. *@par Inputs:
  126. *true_classes: A "batch_size * num_true" matrix, in which each row contains
  127. the IDs of the "num_true" "target_classes" in the corresponding original label.
  128. * Input "true_classes" is a 2D matrix. \n
  129. *@par Attributes:
  130. *@li num_true: Number of true labels per context.
  131. *@li num_sampled: Number of candidates to randomly sample.
  132. *@li unique: If "unique" is true, samples with rejection,
  133. so that all sampled candidates in a batch are unique. This requires
  134. some approximation to estimate the post-rejection sampling probabilities.
  135. *@li range_max: The sampler will sample integers from the interval [0, range_max).
  136. *@li vocab_file: Each valid line in this file (which should have a
  137. CSV-like format) corresponds to a valid word ID.
  138. *IDs are in sequential order, starting from num_reserved_ids.
  139. *@li distortion: The distortion is used to skew the unigram probability
  140. distribution. Each weight is first raised to the distortion's power before
  141. adding to the internal unigram distribution.
  142. *@li num_reserved_ids: Optionally some reserved IDs can be added in the range
  143. [0, ..., num_reserved_ids) by the users.
  144. * One use case is that a special unknown word token is used as ID 0.
  145. *@li num_shards: A sampler can be used to sample from a subset of the
  146. original range. in order to speed up the whole computation through parallelism.
  147. *@li shard: A sampler can be used to sample from a subset of the original
  148. range in order to speed up the whole computation through parallelism.
  149. *@li unigrams: A list of unigram counts or probabilities, one per ID in
  150. sequential order.
  151. *@li seed: If either "seed" or "seed2" are set to be non-zero.
  152. *@li seed2: A second seed to avoid seed collision. \n
  153. *@par Outputs:
  154. *@li sampled_candidates: A vector of length "num_sampled", in which each
  155. element is the ID of a sampled candidate.
  156. *@li true_expected_count: A "batch_size * num_true" matrix, representing the
  157. number of times each candidate is expected to occur in a batch of sampled
  158. candidates. If "unique" is true, then this is a probability.
  159. *@li sampled_expected_count: A vector of length "num_sampled",
  160. for each sampled candidate representing the number of times the candidate is
  161. expected to occur in a batch of sampled candidates.
  162. If "unique" is true, then this is a probability. \n
  163. *@attention Constraints:
  164. * FixedUnigramCandidateSampler runs on the Ascend AI CPU,
  165. which delivers poor performance. \n
  166. *@par Third-party framework compatibility
  167. *Compatible with the TensorFlow operator FixedUnigramCandidateSampler. \n
  168. *@par Restrictions:
  169. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  170. */
  171. REG_OP(FixedUnigramCandidateSampler)
  172. .INPUT(true_classes, TensorType({ DT_INT64 }))
  173. .OUTPUT(sampled_candidates, TensorType({ DT_INT64 }))
  174. .OUTPUT(true_expected_count, TensorType({ DT_FLOAT }))
  175. .OUTPUT(sampled_expected_count, TensorType({ DT_FLOAT }))
  176. .ATTR(num_true, Int, 0)
  177. .ATTR(num_sampled, Int, 0)
  178. .ATTR(unique, Bool, false)
  179. .ATTR(range_max, Int, 0)
  180. .ATTR(vocab_file, String, "")
  181. .ATTR(distortion, Float, 1.0)
  182. .ATTR(num_reserved_ids, Int, 0)
  183. .ATTR(num_shards, Int, 1)
  184. .ATTR(shard, Int, 0)
  185. .REQUIRED_ATTR(unigrams, ListFloat)
  186. .ATTR(seed, Int, 0)
  187. .ATTR(seed2, Int, 0)
  188. .OP_END_FACTORY_REG(FixedUnigramCandidateSampler)
  189. /**
  190. *@brief Generates labels for candidate sampling with a learned
  191. unigram distribution. \n
  192. *@par Inputs:
  193. *true_classes: A "batch_size * num_true" matrix, in which each row contains
  194. the IDs of the "num_true" "target_classes" in the corresponding original label.
  195. * Input "true_classes" is a 2D matrix. \n
  196. *@par Attributes:
  197. *@li num_true: Number of true labels per context.
  198. *@li num_sampled: Number of candidates to randomly sample.
  199. *@li unique: If "unique" is true, samples with rejection,
  200. so that all sampled candidates in a batch are unique.
  201. *This requires some approximation to estimate the post-rejection
  202. sampling probabilities.
  203. *@li range_max: The sampler will sample integers from the interval
  204. [0, range_max).
  205. *@li seed: If either "seed" or "seed2" are set to be non-zero.
  206. *@li seed2: A second seed to avoid seed collision. \n
  207. *@par Outputs:
  208. *@li sampled_candidates: A vector of length "num_sampled", in which each
  209. element is the ID of a sampled candidate.
  210. *@li true_expected_count: A "batch_size * num_true" matrix, representing
  211. the number of times each candidate is expected to occur in a batch of sampled candidates.
  212. *If "unique" is true, then this is a probability.
  213. *@li sampled_expected_count: A vector of length "num_sampled", for each
  214. sampled candidate representing the number of times the candidate is expected
  215. to occur in a batch of sampled candidates.
  216. *If "unique" is true, then this is a probability. \n
  217. *@attention Constraints:
  218. *LearnedUnigramCandidateSampler runs on the Ascend AI CPU, which delivers
  219. poor performance. \n
  220. *@par Third-party framework compatibility
  221. *Compatible with the TensorFlow operator LearnedUnigramCandidateSampler. \n
  222. *@par Restrictions:
  223. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  224. */
  225. REG_OP(LearnedUnigramCandidateSampler)
  226. .INPUT(true_classes, TensorType({ DT_INT64 }))
  227. .OUTPUT(sampled_candidates, TensorType({ DT_INT64 }))
  228. .OUTPUT(true_expected_count, TensorType({ DT_FLOAT }))
  229. .OUTPUT(sampled_expected_count, TensorType({ DT_FLOAT }))
  230. .REQUIRED_ATTR(num_true, Int)
  231. .REQUIRED_ATTR(num_sampled, Int)
  232. .REQUIRED_ATTR(unique, Bool)
  233. .REQUIRED_ATTR(range_max, Int)
  234. .ATTR(seed, Int, 0)
  235. .ATTR(seed2, Int, 0)
  236. .OP_END_FACTORY_REG(LearnedUnigramCandidateSampler)
  237. /**
  238. *@brief Generates labels for candidate sampling with a log-uniform
  239. distribution. \n
  240. *@par Inputs:
  241. *true_classes: A "batch_size * num_true" matrix, in which each row contains
  242. the IDs of the "num_true" "target_classes" in the corresponding original label.
  243. * Input "true_classes" is a 2D matrix. \n
  244. *@par Attributes:
  245. *@li num_true: Number of true labels per context.
  246. *@li num_sampled: Number of candidates to randomly sample.
  247. *@li unique: If "unique" is true, samples with rejection, so that all
  248. sampled candidates in a batch are unique. This requires some approximation
  249. to estimate the post-rejection sampling probabilities.
  250. *@li range_max: The sampler will sample integers from the interval
  251. [0, range_max).
  252. *@li seed: If either "seed" or "seed2" are set to be non-zero.
  253. *@li seed2: A second seed to avoid seed collision. \n
  254. *@par Outputs:
  255. *@li sampled_candidates: A vector of length "num_sampled", in which each
  256. element is the ID of a sampled candidate.
  257. *@li true_expected_count: A "batch_size * num_true" matrix, representing
  258. the number of times each candidate is expected to occur in a batch of sampled
  259. candidates. If "unique" is true, then this is a probability.
  260. *@li sampled_expected_count: A vector of length "num_sampled", for each
  261. sampled candidate representing the number of times the candidate is expected
  262. to occur in a batch of sampled candidates.
  263. *If "unique" is true, then this is a probability. \n
  264. *@attention Constraints:
  265. *LogUniformCandidateSampler runs on the Ascend AI CPU, which delivers
  266. poor performance. \n
  267. *@par Third-party framework compatibility
  268. *Compatible with the TensorFlow operator LogUniformCandidateSampler. \n
  269. *@par Restrictions:
  270. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  271. */
  272. REG_OP(LogUniformCandidateSampler)
  273. .INPUT(true_classes, TensorType({ DT_INT64 }))
  274. .OUTPUT(sampled_candidates, TensorType({ DT_INT64 }))
  275. .OUTPUT(true_expected_count, TensorType({ DT_FLOAT }))
  276. .OUTPUT(sampled_expected_count, TensorType({ DT_FLOAT }))
  277. .REQUIRED_ATTR(num_true, Int)
  278. .REQUIRED_ATTR(num_sampled, Int)
  279. .REQUIRED_ATTR(unique, Bool)
  280. .REQUIRED_ATTR(range_max, Int)
  281. .ATTR(seed, Int, 0)
  282. .ATTR(seed2, Int, 0)
  283. .OP_END_FACTORY_REG(LogUniformCandidateSampler)
  284. /**
  285. *@brief Generates labels for candidate sampling with a learned
  286. unigram distribution. \n
  287. *@par Inputs:
  288. *true_classes: A "batch_size * num_true" matrix, in which each row contains
  289. the IDs of the "num_true" "target_classes" in the corresponding original label.
  290. * Input "true_classes" is a 2D matrix. \n
  291. *@par Attributes:
  292. *@li num_true: Number of true labels per context.
  293. *@li num_sampled: Number of candidates to randomly sample.
  294. *@li unique: If "unique" is true, samples with rejection,
  295. so that all sampled candidates in a batch are unique. This requires some
  296. approximation to estimate the post-rejection sampling probabilities.
  297. *@li seed: If either "seed" or "seed2" are set to be non-zero.
  298. *@li seed2: A second seed to avoid seed collision. \n
  299. *@par Outputs:
  300. *@li sampled_candidates: A vector of length "num_sampled",
  301. in which each element is the ID of a sampled candidate.
  302. *@li true_expected_count: A "batch_size * num_true" matrix, representing the
  303. number of times each candidate is expected to occur in a batch of sampled candidates.
  304. *If "unique" is true, then this is a probability.
  305. *@li sampled_expected_count: A vector of length "num_sampled", for each
  306. sampled candidate representing the number of times the candidate is expected
  307. to occur in a batch of sampled candidates. If "unique" is true, then this is a probability. \n
  308. *@attention Constraints:
  309. *AllCandidateSampler runs on the Ascend AI CPU, which delivers poor performance.
  310. *@par Third-party framework compatibility
  311. *Compatible with the TensorFlow operator AllCandidateSampler. \n
  312. *@par Restrictions:
  313. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  314. */
  315. REG_OP(AllCandidateSampler)
  316. .INPUT(true_classes, TensorType({ DT_INT64 }))
  317. .OUTPUT(sampled_candidates, TensorType({ DT_INT64 }))
  318. .OUTPUT(true_expected_count, TensorType({ DT_FLOAT }))
  319. .OUTPUT(sampled_expected_count, TensorType({ DT_FLOAT }))
  320. .REQUIRED_ATTR(num_true, Int)
  321. .REQUIRED_ATTR(num_sampled, Int)
  322. .REQUIRED_ATTR(unique, Bool)
  323. .ATTR(seed, Int, 0)
  324. .ATTR(seed2, Int, 0)
  325. .OP_END_FACTORY_REG(AllCandidateSampler)
  326. /**
  327. *@brief Computes the "ids" of the positions in "sampled_candidates" that
  328. match "true_labels". \n
  329. *@par Inputs:
  330. * @li Input "true_classes" is a 2D matrix.
  331. * @li true_classes: The "true_classes" output of UnpackSparseLabels.
  332. * @li sampled_candidates: The "sampled_candidates" output of CandidateSampler. \n
  333. *@par Attributes:
  334. *@li num_true: Number of true labels per context.
  335. *@li seed: If either "seed" or "seed2" are set to be non-zero.
  336. *@li seed2: A second seed to avoid seed collision. \n
  337. *@par Outputs:
  338. * @li indices: A vector of indices corresponding to rows of "true_candidates".
  339. * @li ids: A vector of IDs of positions in "sampled_candidates" that match a
  340. "true_label" for the row with the corresponding index in indices.
  341. * @li weights: A vector of the same length as "indices" and "ids", in which
  342. each element is -FLOAT_MAX. \n
  343. *@attention Constraints:
  344. *ComputeAccidentalHits runs on the Ascend AI CPU, which delivers poor performance.
  345. *@par Third-party framework compatibility
  346. *Compatible with the TensorFlow operator ComputeAccidentalHits. \n
  347. *@par Restrictions:
  348. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  349. */
  350. REG_OP(ComputeAccidentalHits)
  351. .INPUT(true_classes, TensorType({ DT_INT64 }))
  352. .INPUT(sampled_candidates, TensorType({ DT_INT64 }))
  353. .OUTPUT(indices, TensorType({ DT_INT32 }))
  354. .OUTPUT(ids, TensorType({ DT_INT64 }))
  355. .OUTPUT(weights, TensorType({ DT_FLOAT }))
  356. .REQUIRED_ATTR(num_true, Int)
  357. .ATTR(seed, Int, 0)
  358. .ATTR(seed2, Int, 0)
  359. .OP_END_FACTORY_REG(ComputeAccidentalHits)
  360. } // namespace ge
  361. #endif // OPS_BUILT_IN_OP_PROTO_INC_CANDIDATE_SAMPLING_OPS_H_

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