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.

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_RNN_H
  17. #define GE_OP_RNN_H
  18. #include "graph/operator_reg.h"
  19. namespace ge {
  20. /**
  21. *@brief: Basic LSTM Cell forward calculation.
  22. *@par Inputs:
  23. *five inputs: \n
  24. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  25. *@li h:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  26. *@li c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  27. *@li w:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  28. *@li b:A 1D Tensor. Must be one of the following types: float16. The format must be ND.
  29. *@par Attributes:
  30. *@li keep_prob:An integer identifying the keep prob in the op. Default to 1.
  31. *@li forget_bias:An integer identifying the forget bias in the op. Default to 1.
  32. *@li state_is_tuple:An bool identifying if the hidden state and cell state is tuple. Default to true.
  33. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported.
  34. *@par Outputs:
  35. *seven outputs: \n
  36. *@li mask:A 1D Tensor. Must be one of the following types: uint8.
  37. *@li ct:A 4D Tensor. Must be one of the following types: float16, float32.
  38. *@li ht:A 4D Tensor. Must be one of the following types: float16.
  39. *@li it:A 4D Tensor. Must be one of the following types: float16, float32.
  40. *@li jt:A 4D Tensor. Must be one of the following types: float16, float32.
  41. *@li ft:A 4D Tensor. Must be one of the following types: float16, float32.
  42. *@li ot:A 4D Tensor. Must be one of the following types: float16, float32.
  43. *@li tanhct:A 4D Tensor. Must be one of the following types: float16, float32.
  44. */
  45. REG_OP(BasicLSTMCell)
  46. .INPUT(x, TensorType({DT_FLOAT16}))
  47. .INPUT(h, TensorType({DT_FLOAT16}))
  48. .INPUT(c, TensorType({DT_FLOAT16, DT_FLOAT}))
  49. .INPUT(w, TensorType({DT_FLOAT16}))
  50. .INPUT(b, TensorType({DT_FLOAT16, DT_FLOAT}))
  51. .OPTIONAL_INPUT(mask, TensorType({DT_UINT8}))
  52. .OUTPUT(ct, TensorType({DT_FLOAT16, DT_FLOAT}))
  53. .OUTPUT(ht, TensorType({DT_FLOAT16}))
  54. .OUTPUT(it, TensorType({DT_FLOAT16, DT_FLOAT}))
  55. .OUTPUT(jt, TensorType({DT_FLOAT16, DT_FLOAT}))
  56. .OUTPUT(ft, TensorType({DT_FLOAT16, DT_FLOAT}))
  57. .OUTPUT(ot, TensorType({DT_FLOAT16, DT_FLOAT}))
  58. .OUTPUT(tanhct, TensorType({DT_FLOAT16, DT_FLOAT}))
  59. .ATTR(keep_prob, Float, 1.0)
  60. .ATTR(forget_bias, Float, 1.0)
  61. .ATTR(state_is_tuple, Bool, true)
  62. .ATTR(activation, String, "tanh")
  63. .OP_END_FACTORY_REG(BasicLSTMCell)
  64. /**
  65. *@brief: Dynamic LSTM forward calculation.
  66. *@par Inputs:
  67. *@li x:A 4D Tensor. Must be the type float32. The format must be FRACTAL_NZ.
  68. *@li w:A 4D Tensor. Must be the type float32. The format must be FRACTAL_Z.
  69. *@li b:A 1D Tensor. Must be the type float32. The format must be ND.
  70. *@par Outputs:
  71. *output_h:A Tensor of output. Must be the type float32. The format must be FRACTAL_Z.
  72. */
  73. REG_OP(DynamicLSTM)
  74. .INPUT(x, TensorType({DT_FLOAT32}))
  75. .INPUT(w, TensorType({DT_FLOAT32}))
  76. .INPUT(b, TensorType({DT_FLOAT32}))
  77. .OUTPUT(output_h, TensorType({DT_FLOAT32}))
  78. .OP_END_FACTORY_REG(DynamicLSTM)
  79. /**
  80. *@brief: Basic LSTM Cell backward calculation.Calculate the gradient of input and hidden state.
  81. *@par Inputs:
  82. *three inputs: \n
  83. *@li dgate:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  84. *@li w:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  85. *@li dropout_mask:A 1D Tensor. Must be one of the following types: uint8. The format must be ND.
  86. *@par Attributes:
  87. *keep_prob:An integer identifying the keep prob in the op. Default to 1.
  88. *@par Outputs:
  89. *two outputs: \n
  90. *@li dxt:A 4D Tensor. Must be one of the following types: float16, float32.
  91. *@li dht:A 4D Tensor. Must be one of the following types: float16, float32.
  92. */
  93. REG_OP(BasicLSTMCellInputGrad)
  94. .INPUT(dgate, TensorType({DT_FLOAT16}))
  95. .INPUT(w, TensorType({DT_FLOAT16}))
  96. .OPTIONAL_INPUT(dropout_mask, TensorType({DT_UINT8}))
  97. .OUTPUT(dxt, TensorType({DT_FLOAT16, DT_FLOAT32}))
  98. .OUTPUT(dht, TensorType({DT_FLOAT16, DT_FLOAT32}))
  99. .ATTR(keep_prob, Float, 1.0)
  100. .OP_END_FACTORY_REG(BasicLSTMCellInputGrad)
  101. /**
  102. *@brief: Basic LSTM Cell backward calculation.Calculate the gradient of weight and bias.
  103. *@par Inputs:
  104. *three inputs: \n
  105. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  106. *@li h:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  107. *@li dgate:A 4D Tensor. Must be one of the following types: uint8. The format must be FRACTAL_NZ.
  108. *@par Outputs:
  109. *two outputs: \n
  110. *@li dw:A 4D Tensor. Must be one of the following types: float16.
  111. *@li db:A 4D Tensor. Must be one of the following types: float16, float32.
  112. */
  113. REG_OP(BasicLSTMCellWeightGrad)
  114. .INPUT(x, TensorType({DT_FLOAT16}))
  115. .INPUT(h, TensorType({DT_FLOAT16}))
  116. .INPUT(dgate, TensorType({DT_FLOAT16}))
  117. .OUTPUT(dw, TensorType({DT_FLOAT16}))
  118. .OUTPUT(db, TensorType({DT_FLOAT16, DT_FLOAT32}))
  119. .OP_END_FACTORY_REG(BasicLSTMCellWeightGrad)
  120. /**
  121. *@brief: Basic LSTM Cell backward calculation.Calculate the gradient of gates and cell state.
  122. *@par Inputs:
  123. *eight inputs: \n
  124. *@li c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  125. *@li dht:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  126. *@li dct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  127. *@li it:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  128. *@li jt:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  129. *@li ft:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  130. *@li ot:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  131. *@li tanhct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  132. *@par Attributes:
  133. *@li forget_bias:An integer identifying the forget bias in the op. Default to 1.
  134. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported.
  135. *@par Outputs:
  136. *two outputs: \n
  137. *@li dgate:A 4D Tensor. Must be one of the following types: float16.
  138. *@li dct_1:A 4D Tensor. Must be one of the following types: float16, float32.
  139. */
  140. REG_OP(BasicLSTMCellCStateGrad)
  141. .INPUT(c, TensorType({DT_FLOAT16, DT_FLOAT}))
  142. .INPUT(dht, TensorType({DT_FLOAT16, DT_FLOAT}))
  143. .INPUT(dct, TensorType({DT_FLOAT16, DT_FLOAT}))
  144. .INPUT(it, TensorType({DT_FLOAT16, DT_FLOAT}))
  145. .INPUT(jt, TensorType({DT_FLOAT16, DT_FLOAT}))
  146. .INPUT(ft, TensorType({DT_FLOAT16, DT_FLOAT}))
  147. .INPUT(ot, TensorType({DT_FLOAT16, DT_FLOAT}))
  148. .INPUT(tanhct, TensorType({DT_FLOAT16, DT_FLOAT}))
  149. .OUTPUT(dgate, TensorType({DT_FLOAT16}))
  150. .OUTPUT(dct_1, TensorType({DT_FLOAT16, DT_FLOAT}))
  151. .ATTR(forget_bias, Float, 1.0)
  152. .ATTR(activation, String, "tanh")
  153. .OP_END_FACTORY_REG(BasicLSTMCellCStateGrad)
  154. /**
  155. *@brief: RNN operator.
  156. *@par Inputs:
  157. *eight inputs: \n
  158. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  159. *@li cont:A 1D Tensor. Must be one of the following types: float16. The format must be ND.
  160. *@li x_static:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  161. *@li h_0:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  162. *@li w_xh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  163. *@li w_sh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  164. *@li w_hh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  165. *@li w_ho:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  166. *@li bias_h:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  167. *@li bias_o:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  168. *@par Attributes:
  169. *@li expose_hidden:An bool identifying if expose the hidden state of last time step. Default to false.
  170. *@li num_output:An integer identifying the number of output features. Default to 0.
  171. *@par Outputs:
  172. *two outputs: \n
  173. *@li o:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  174. *@li h_t:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  175. */
  176. REG_OP(RNN)
  177. .INPUT(x, TensorType({DT_FLOAT16}))
  178. .INPUT(cont, TensorType({DT_FLOAT16}))
  179. .OPTIONAL_INPUT(x_static, TensorType({DT_FLOAT16}))
  180. .OPTIONAL_INPUT(h_0, TensorType({DT_FLOAT16, DT_FLOAT}))
  181. .INPUT(w_xh, TensorType({DT_FLOAT16}))
  182. .INPUT(bias_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  183. .OPTIONAL_INPUT(w_sh, TensorType({DT_FLOAT16}))
  184. .INPUT(w_hh, TensorType({DT_FLOAT16}))
  185. .INPUT(w_ho, TensorType({DT_FLOAT16}))
  186. .INPUT(bias_o, TensorType({DT_FLOAT16, DT_FLOAT}))
  187. .OUTPUT(o, TensorType({DT_FLOAT16, DT_FLOAT}))
  188. .OUTPUT(h_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  189. .ATTR(num_output, Int, 0)
  190. .ATTR(expose_hidden, Bool, false)
  191. .OP_END_FACTORY_REG(RNN)
  192. /**
  193. *@brief: BasicRNNCell operator.
  194. *@par Inputs:
  195. *eight inputs: \n
  196. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  197. *@li cont:A 1D Tensor. Must be one of the following types: float16. The format must be ND.
  198. *@li w_xh_x_static:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  199. *@li h_0:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  200. *@li w_xh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  201. *@li w_hh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  202. *@li w_ho:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  203. *@li bias_h:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  204. *@li bias_o:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  205. *@par Attributes:
  206. *@li expose_hidden:An bool identifying if expose the hidden state of last time step. Default to false.
  207. *@li num_output:An integer identifying the number of output features. Default to 0.
  208. *@par Outputs:
  209. *two outputs: \n
  210. *@li o_t:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  211. *@li h_t:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  212. */
  213. REG_OP(BasicRNNCell)
  214. .INPUT(x, TensorType({DT_FLOAT16}))
  215. .OPTIONAL_INPUT(cont, TensorType({DT_FLOAT16}))
  216. .OPTIONAL_INPUT(w_xh_x_static, TensorType({DT_FLOAT16, DT_FLOAT}))
  217. .OPTIONAL_INPUT(h_0, TensorType({DT_FLOAT16, DT_FLOAT}))
  218. .INPUT(w_xh, TensorType({DT_FLOAT16}))
  219. .INPUT(bias_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  220. .OPTIONAL_INPUT(w_hh, TensorType({DT_FLOAT16}))
  221. .INPUT(w_ho, TensorType({DT_FLOAT16}))
  222. .INPUT(bias_o, TensorType({DT_FLOAT16, DT_FLOAT}))
  223. .OUTPUT(o_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  224. .OUTPUT(h_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  225. .ATTR(expose_hidden, Bool, false)
  226. .ATTR(num_output, Int, 0)
  227. .OP_END_FACTORY_REG(BasicRNNCell)
  228. } // namespace ge
  229. #endif // GE_OP_RNN_H

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