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.

batch_ops.h 5.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_BATCH_OPS_H_
  17. #define GE_OP_BATCH_OPS_H_
  18. #include "graph/operator_reg.h"
  19. namespace ge {
  20. /**
  21. *@brief Creates batches of tensors in "x_tensors".
  22. *@par Inputs:
  23. *Input "x_tensors" is a list or a dictionary of tensors. \n
  24. *x_tensors: The list or dictionary of tensors to enqueue.
  25. *@par Attributes:
  26. *@li num_batch_threads: The number of threads enqueuing "x_tensors". \n
  27. The batching will be nondeterministic if "num_batch_threads" > 1.
  28. *@li max_batch_size: The maximum batch size pulled from the queue.
  29. *@li max_enqueued_batches: The maximum number of batches pulled from the queue.
  30. *@li batch_timeout_micros: The batch processing timeout, in microseconds.
  31. *@li allowed_batch_sizes: The allowed batch size pulled from the queue.
  32. *@li grad_timeout_micros: The gradient batch processing timeout, \n
  33. in microseconds.
  34. *@li container: If non-empty, this queue is placed in the given container. \n
  35. Otherwise, a default container is used.
  36. *@li shared_name: If set, this queue will be shared under the given name \n
  37. across multiple sessions.
  38. *@li batching_queue: The queue resource container.
  39. *@par Outputs:
  40. *@li y_index: A Tensor. The index of a BatchTensor. Must be in row-major order.
  41. *@li y_id: A Tensor. The ID of a BatchTensor. Must be in row-major order.
  42. *@li y_tensors: A list or dictionary of tensors with \n
  43. the same types as "x_tensors".
  44. *@attention Constraints: \n
  45. *Batch runs on the Ascend AI CPU, which delivers poor performance. \n
  46. *@par Third-party framework compatibility
  47. *Compatible with the TensorFlow operator Batch.
  48. */
  49. REG_OP(Batch)
  50. .DYNAMIC_INPUT(x_tensors, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8, \
  51. DT_INT16, DT_UINT16, DT_UINT8, DT_INT32, DT_INT64, DT_BOOL, DT_DOUBLE}))
  52. .OUTPUT(y_index, TensorType({ DT_INT64 }))
  53. .OUTPUT(y_id, TensorType({ DT_INT64 }))
  54. .DYNAMIC_OUTPUT(y_tensors, TensorType({DT_INT8, DT_UINT8, DT_INT16, \
  55. DT_UINT16, DT_INT32, DT_INT64, DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_BOOL}))
  56. .REQUIRED_ATTR(num_batch_threads, Int)
  57. .REQUIRED_ATTR(max_batch_size, Int)
  58. .ATTR(max_enqueued_batches, Int, 10)
  59. .REQUIRED_ATTR(batch_timeout_micros, Int)
  60. .ATTR(allowed_batch_sizes, ListInt, {})
  61. .REQUIRED_ATTR(grad_timeout_micros, Int)
  62. .ATTR(container, String, "")
  63. .ATTR(shared_name, String, "")
  64. .ATTR(batching_queue, String, "")
  65. .OP_END_FACTORY_REG(Batch)
  66. /**
  67. *@brief Reverses the operation of Batch for a single output Tensor.
  68. *@par Inputs:
  69. *Input "x_tensors" is a list or a dictionary of tensors. \n
  70. * @li x_tensors: The list or dictionary of tensors to enqueue.
  71. * @li index: The matching "batch_index" obtained from Batch.
  72. * @li id: The "id" scalar emitted by Batch.
  73. *@par Attributes:
  74. *@li timeout_micros: The unbatch processing timeout, in microseconds.
  75. *@li container: If non-empty, this queue is placed in the given container. \n
  76. Otherwise, a default container is used.
  77. *@li shared_name: If set, this queue will be shared under the given name \n
  78. across multiple sessions.
  79. *@par Outputs:
  80. *y_tensor: A list or dictionary of tensors with the same types as "x_tensors".
  81. *@attention Constraints: \n
  82. *Unbatch runs on the Ascend AI CPU, which delivers poor performance. \n
  83. *@par Third-party framework compatibility
  84. *Compatible with the TensorFlow operator Unbatch.
  85. */
  86. REG_OP(Unbatch)
  87. .INPUT(x_tensor, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  88. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  89. .INPUT(index, TensorType({DT_INT64}))
  90. .INPUT(id, TensorType({DT_INT64}))
  91. .OUTPUT(y_tensor, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  92. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  93. .REQUIRED_ATTR(timeout_micros, Int)
  94. .ATTR(container, String, "")
  95. .ATTR(shared_name, String, "")
  96. .OP_END_FACTORY_REG(Unbatch)
  97. /**
  98. *@brief Acts like Batch but using the given "batch_index" index of batching \n
  99. things as they become available.
  100. *@par Inputs:
  101. *Input "x_input" is a list or a dictionary of tensors. \n
  102. * @li x_input: The input to the Unbatch operation.
  103. * @li index: The batch_index given to the Unbatch operation.
  104. * @li id: The "id" scalar emitted by Batch.
  105. * @li grad: The downstream gradient.
  106. *@par Attributes:
  107. *@li container: If non-empty, this queue is placed in the given container. \n
  108. Otherwise, a default container is used.
  109. *@li shared_name: If set, this queue will be shared under the given name \n
  110. across multiple sessions.
  111. *@par Outputs:
  112. *y_grad: The return value, either an empty tensor or the batched gradient.
  113. *@attention Constraints: \n
  114. *UnbatchGrad runs on the Ascend AI CPU, which delivers poor performance. \n
  115. *@par Third-party framework compatibility
  116. *Compatible with the TensorFlow operator UnbatchGrad.
  117. */
  118. REG_OP(UnbatchGrad)
  119. .INPUT(x_input, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  120. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  121. .INPUT(index, TensorType({DT_INT64}))
  122. .INPUT(grad, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  123. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  124. .INPUT(id, TensorType({DT_INT64}))
  125. .OUTPUT(y_grad, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  126. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  127. .ATTR(container, String, "")
  128. .ATTR(shared_name, String, "")
  129. .OP_END_FACTORY_REG(UnbatchGrad)
  130. } // namespace ge
  131. #endif // GE_OP_BATCH_OPS_H_

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