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.

rnn.h 12 kB

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

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