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

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