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.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. */
  47. REG_OP(Batch)
  48. .DYNAMIC_INPUT(x_tensors, TensorType({DT_FLOAT16, DT_FLOAT, DT_INT8, \
  49. DT_INT16, DT_UINT16, DT_UINT8, DT_INT32, DT_INT64, DT_BOOL, DT_DOUBLE}))
  50. .OUTPUT(y_index, TensorType({ DT_INT64 }))
  51. .OUTPUT(y_id, TensorType({ DT_INT64 }))
  52. .DYNAMIC_OUTPUT(y_tensors, TensorType({DT_INT8, DT_UINT8, DT_INT16, \
  53. DT_UINT16, DT_INT32, DT_INT64, DT_FLOAT, DT_FLOAT16, DT_DOUBLE, DT_BOOL}))
  54. .REQUIRED_ATTR(num_batch_threads, Int)
  55. .REQUIRED_ATTR(max_batch_size, Int)
  56. .ATTR(max_enqueued_batches, Int, 10)
  57. .REQUIRED_ATTR(batch_timeout_micros, Int)
  58. .ATTR(allowed_batch_sizes, ListInt, {})
  59. .REQUIRED_ATTR(grad_timeout_micros, Int)
  60. .ATTR(container, String, "")
  61. .ATTR(shared_name, String, "")
  62. .ATTR(batching_queue, String, "")
  63. .OP_END_FACTORY_REG(Batch)
  64. /**
  65. *@brief Reverses the operation of Batch for a single output Tensor.
  66. *@par Inputs:
  67. *Input "x_tensors" is a list or a dictionary of tensors. \n
  68. * @li x_tensors: The list or dictionary of tensors to enqueue.
  69. * @li index: The matching "batch_index" obtained from Batch.
  70. * @li id: The "id" scalar emitted by Batch.
  71. *@par Attributes:
  72. *@li timeout_micros: The unbatch processing timeout, in microseconds.
  73. *@li container: If non-empty, this queue is placed in the given container. \n
  74. Otherwise, a default container is used.
  75. *@li shared_name: If set, this queue will be shared under the given name \n
  76. across multiple sessions.
  77. *@par Outputs:
  78. *y_tensor: A list or dictionary of tensors with the same types as "x_tensors".
  79. *@attention Constraints: \n
  80. *Unbatch runs on the Ascend AI CPU, which delivers poor performance. \n
  81. */
  82. REG_OP(Unbatch)
  83. .INPUT(x_tensor, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  84. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  85. .INPUT(index, TensorType({DT_INT64}))
  86. .INPUT(id, TensorType({DT_INT64}))
  87. .OUTPUT(y_tensor, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  88. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  89. .REQUIRED_ATTR(timeout_micros, Int)
  90. .ATTR(container, String, "")
  91. .ATTR(shared_name, String, "")
  92. .OP_END_FACTORY_REG(Unbatch)
  93. /**
  94. *@brief Acts like Batch but using the given "batch_index" index of batching \n
  95. things as they become available.
  96. *@par Inputs:
  97. *Input "x_input" is a list or a dictionary of tensors. \n
  98. * @li x_input: The input to the Unbatch operation.
  99. * @li index: The batch_index given to the Unbatch operation.
  100. * @li id: The "id" scalar emitted by Batch.
  101. * @li grad: The downstream gradient.
  102. *@par Attributes:
  103. *@li container: If non-empty, this queue is placed in the given container. \n
  104. Otherwise, a default container is used.
  105. *@li shared_name: If set, this queue will be shared under the given name \n
  106. across multiple sessions.
  107. *@par Outputs:
  108. *y_grad: The return value, either an empty tensor or the batched gradient.
  109. *@attention Constraints: \n
  110. *UnbatchGrad runs on the Ascend AI CPU, which delivers poor performance. \n
  111. */
  112. REG_OP(UnbatchGrad)
  113. .INPUT(x_input, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  114. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  115. .INPUT(index, TensorType({DT_INT64}))
  116. .INPUT(grad, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  117. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  118. .INPUT(id, TensorType({DT_INT64}))
  119. .OUTPUT(y_grad, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  120. DT_INT32, DT_INT64, DT_BOOL, DT_FLOAT, DT_DOUBLE}))
  121. .ATTR(container, String, "")
  122. .ATTR(shared_name, String, "")
  123. .OP_END_FACTORY_REG(UnbatchGrad)
  124. } // namespace ge
  125. #endif // GE_OP_BATCH_OPS_H_

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