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

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