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

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