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 17 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /*!
  17. * \file rnn.h
  18. * \brief
  19. */
  20. #ifndef GE_OP_RNN_H
  21. #define GE_OP_RNN_H
  22. #include "graph/operator_reg.h"
  23. namespace ge {
  24. /**
  25. *@brief: Basic LSTM Cell forward calculation.
  26. *@par Inputs:
  27. *five inputs: \n
  28. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  29. *@li h:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  30. *@li c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  31. *@li w:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  32. *@li b:A 1D Tensor. Must be one of the following types: float16. The format must be ND.
  33. *@par Attributes:
  34. *@li keep_prob:An integer identifying the keep prob in the op. Default to 1.
  35. *@li forget_bias:An integer identifying the forget bias in the op. Default to 1.
  36. *@li state_is_tuple:An bool identifying if the hidden state and cell state is tuple. Default to true.
  37. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported.
  38. *@par Outputs:
  39. *seven outputs: \n
  40. *@li mask:A 1D Tensor. Must be one of the following types: uint8.
  41. *@li ct:A 4D Tensor. Must be one of the following types: float16, float32.
  42. *@li ht:A 4D Tensor. Must be one of the following types: float16.
  43. *@li it:A 4D Tensor. Must be one of the following types: float16, float32.
  44. *@li jt:A 4D Tensor. Must be one of the following types: float16, float32.
  45. *@li ft:A 4D Tensor. Must be one of the following types: float16, float32.
  46. *@li ot:A 4D Tensor. Must be one of the following types: float16, float32.
  47. *@li tanhct:A 4D Tensor. Must be one of the following types: float16, float32.
  48. */
  49. REG_OP(BasicLSTMCell)
  50. .INPUT(x, TensorType({DT_FLOAT16}))
  51. .INPUT(h, TensorType({DT_FLOAT16}))
  52. .INPUT(c, TensorType({DT_FLOAT16, DT_FLOAT}))
  53. .INPUT(w, TensorType({DT_FLOAT16}))
  54. .INPUT(b, TensorType({DT_FLOAT16, DT_FLOAT}))
  55. .OPTIONAL_INPUT(mask, TensorType({DT_UINT8}))
  56. .OUTPUT(ct, TensorType({DT_FLOAT16, DT_FLOAT}))
  57. .OUTPUT(ht, TensorType({DT_FLOAT16}))
  58. .OUTPUT(it, TensorType({DT_FLOAT16, DT_FLOAT}))
  59. .OUTPUT(jt, TensorType({DT_FLOAT16, DT_FLOAT}))
  60. .OUTPUT(ft, TensorType({DT_FLOAT16, DT_FLOAT}))
  61. .OUTPUT(ot, TensorType({DT_FLOAT16, DT_FLOAT}))
  62. .OUTPUT(tanhct, TensorType({DT_FLOAT16, DT_FLOAT}))
  63. .ATTR(keep_prob, Float, 1.0)
  64. .ATTR(forget_bias, Float, 1.0)
  65. .ATTR(state_is_tuple, Bool, true)
  66. .ATTR(activation, String, "tanh")
  67. .OP_END_FACTORY_REG(BasicLSTMCell)
  68. /**
  69. *@brief: Dynamic LSTM forward calculation.
  70. *@par Inputs:
  71. *@li x:A 4D Tensor. Must be the type float32. The format must be FRACTAL_NZ.
  72. *@li w:A 4D Tensor. Must be the type float32. The format must be FRACTAL_Z.
  73. *@li b:A 1D Tensor. Must be the type float32. The format must be ND.
  74. *@par Outputs:
  75. *output_h:A Tensor of output. Must be the type float32. The format must be FRACTAL_Z.
  76. */
  77. REG_OP(DynamicLSTM)
  78. .INPUT(x, TensorType({DT_FLOAT32}))
  79. .INPUT(w, TensorType({DT_FLOAT32}))
  80. .INPUT(b, TensorType({DT_FLOAT32}))
  81. .OUTPUT(output_h, TensorType({DT_FLOAT32}))
  82. .OP_END_FACTORY_REG(DynamicLSTM)
  83. /**
  84. *@brief: DynamicRNN calculation.
  85. *@par Inputs:
  86. *ten inputs: \n
  87. *@li x:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  88. *@li w:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_ZN_LSTM.
  89. *@li b:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  90. *@li seq_length:A 1D Tensor. Must be one of the following types: int32. The format must be ND.
  91. *@li init_h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  92. *@li init_c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  93. *@li wci:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_ZN_LSTM.
  94. *@li wcf:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_ZN_LSTM.
  95. *@li wco:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_ZN_LSTM.
  96. *@li mask:A 1D Tensor. Must be one of the following types: uint8. The format must be ND.
  97. *@par Attributes:
  98. *@li cell_type:An string identifying the cell type in the op. Default to "LSTM". Only LSTM is currently supported.
  99. *@li direction:An string identifying the direction in the op. Default to "UNIDIRECTIONAL". Only UNIDIRECTIONAL is currently supported.
  100. *@li cell_depth:An integer identifying the cell depth in the op. Default to 1.
  101. *@li use_peephole:An bool identifying if use peephole in the op. Default to false.
  102. *@li keep_prob:An float identifying the keep prob in the op. Default to 1.
  103. *@li cell_clip:An float identifying the cell clip in the op. Default to -1.
  104. *@li num_proj:An integer identifying the num projection in the op. Default to 0.
  105. *@li time_major:An bool identifying the time major in the op. Default to false.
  106. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported.
  107. *@li forget_bias:An float identifying the forget bias in the op. Default to 0.
  108. *@li is_training:An bool identifying is training in the op. Default to true.
  109. *@par Outputs:
  110. *eight outputs: \n
  111. *@li y:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  112. *@li output_h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  113. *@li output_c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  114. *@li i:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  115. *@li j:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  116. *@li f:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  117. *@li o:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  118. *@li tanhct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  119. */
  120. REG_OP(DynamicRNN)
  121. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
  122. .INPUT(w, TensorType({DT_FLOAT16, DT_FLOAT}))
  123. .INPUT(b, TensorType({DT_FLOAT16, DT_FLOAT}))
  124. .OPTIONAL_INPUT(seq_length, TensorType({DT_UINT32}))
  125. .OPTIONAL_INPUT(init_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  126. .OPTIONAL_INPUT(init_c, TensorType({DT_FLOAT16, DT_FLOAT}))
  127. .OPTIONAL_INPUT(wci, TensorType({DT_FLOAT16, DT_FLOAT}))
  128. .OPTIONAL_INPUT(wcf, TensorType({DT_FLOAT16, DT_FLOAT}))
  129. .OPTIONAL_INPUT(wco, TensorType({DT_FLOAT16, DT_FLOAT}))
  130. .OPTIONAL_INPUT(mask, TensorType({DT_UINT8}))
  131. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  132. .OUTPUT(output_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  133. .OUTPUT(output_c, TensorType({DT_FLOAT16, DT_FLOAT}))
  134. .OUTPUT(i, TensorType({DT_FLOAT16, DT_FLOAT}))
  135. .OUTPUT(j, TensorType({DT_FLOAT16, DT_FLOAT}))
  136. .OUTPUT(f, TensorType({DT_FLOAT16, DT_FLOAT}))
  137. .OUTPUT(o, TensorType({DT_FLOAT16, DT_FLOAT}))
  138. .OUTPUT(tanhc, TensorType({DT_FLOAT16, DT_FLOAT}))
  139. .ATTR(cell_type, String, "LSTM")
  140. .ATTR(direction, String, "UNIDIRECTIONAL")
  141. .ATTR(cell_depth, Int, 1)
  142. .ATTR(use_peephole, Bool, false)
  143. .ATTR(keep_prob, Float, 1.0)
  144. .ATTR(cell_clip, Float, -1.0)
  145. .ATTR(num_proj, Int, 0)
  146. .ATTR(time_major, Bool, false)
  147. .ATTR(forget_bias, Float, 0.0)
  148. .ATTR(is_training, Bool, true)
  149. .OP_END_FACTORY_REG(DynamicRNN)
  150. /**
  151. *@brief: Basic LSTM Cell backward calculation.Calculate the gradient of input and hidden state.
  152. *@par Inputs:
  153. *three inputs: \n
  154. *@li dgate:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  155. *@li w:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  156. *@li dropout_mask:A 1D Tensor. Must be one of the following types: uint8. The format must be ND.
  157. *@par Attributes:
  158. *keep_prob:An integer identifying the keep prob in the op. Default to 1.
  159. *@par Outputs:
  160. *two outputs: \n
  161. *@li dxt:A 4D Tensor. Must be one of the following types: float16, float32.
  162. *@li dht:A 4D Tensor. Must be one of the following types: float16, float32.
  163. */
  164. REG_OP(BasicLSTMCellInputGrad)
  165. .INPUT(dgate, TensorType({DT_FLOAT16}))
  166. .INPUT(w, TensorType({DT_FLOAT16}))
  167. .OPTIONAL_INPUT(dropout_mask, TensorType({DT_UINT8}))
  168. .OUTPUT(dxt, TensorType({DT_FLOAT16, DT_FLOAT32}))
  169. .OUTPUT(dht, TensorType({DT_FLOAT16, DT_FLOAT32}))
  170. .ATTR(keep_prob, Float, 1.0)
  171. .OP_END_FACTORY_REG(BasicLSTMCellInputGrad)
  172. /**
  173. *@brief: Basic LSTM Cell backward calculation.Calculate the gradient of weight and bias.
  174. *@par Inputs:
  175. *three inputs: \n
  176. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  177. *@li h:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  178. *@li dgate:A 4D Tensor. Must be one of the following types: uint8. The format must be FRACTAL_NZ.
  179. *@par Outputs:
  180. *two outputs: \n
  181. *@li dw:A 4D Tensor. Must be one of the following types: float16.
  182. *@li db:A 4D Tensor. Must be one of the following types: float16, float32.
  183. */
  184. REG_OP(BasicLSTMCellWeightGrad)
  185. .INPUT(x, TensorType({DT_FLOAT16}))
  186. .INPUT(h, TensorType({DT_FLOAT16}))
  187. .INPUT(dgate, TensorType({DT_FLOAT16}))
  188. .OUTPUT(dw, TensorType({DT_FLOAT16}))
  189. .OUTPUT(db, TensorType({DT_FLOAT16, DT_FLOAT32}))
  190. .OP_END_FACTORY_REG(BasicLSTMCellWeightGrad)
  191. /**
  192. *@brief: Basic LSTM Cell backward calculation.Calculate the gradient of gates and cell state.
  193. *@par Inputs:
  194. *eight inputs: \n
  195. *@li c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  196. *@li dht:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  197. *@li dct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  198. *@li it:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  199. *@li jt:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  200. *@li ft:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  201. *@li ot:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  202. *@li tanhct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  203. *@par Attributes:
  204. *@li forget_bias:An integer identifying the forget bias in the op. Default to 1.
  205. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported.
  206. *@par Outputs:
  207. *two outputs: \n
  208. *@li dgate:A 4D Tensor. Must be one of the following types: float16.
  209. *@li dct_1:A 4D Tensor. Must be one of the following types: float16, float32.
  210. */
  211. REG_OP(BasicLSTMCellCStateGrad)
  212. .INPUT(c, TensorType({DT_FLOAT16, DT_FLOAT}))
  213. .INPUT(dht, TensorType({DT_FLOAT16, DT_FLOAT}))
  214. .INPUT(dct, TensorType({DT_FLOAT16, DT_FLOAT}))
  215. .INPUT(it, TensorType({DT_FLOAT16, DT_FLOAT}))
  216. .INPUT(jt, TensorType({DT_FLOAT16, DT_FLOAT}))
  217. .INPUT(ft, TensorType({DT_FLOAT16, DT_FLOAT}))
  218. .INPUT(ot, TensorType({DT_FLOAT16, DT_FLOAT}))
  219. .INPUT(tanhct, TensorType({DT_FLOAT16, DT_FLOAT}))
  220. .OUTPUT(dgate, TensorType({DT_FLOAT16}))
  221. .OUTPUT(dct_1, TensorType({DT_FLOAT16, DT_FLOAT}))
  222. .ATTR(forget_bias, Float, 1.0)
  223. .ATTR(activation, String, "tanh")
  224. .OP_END_FACTORY_REG(BasicLSTMCellCStateGrad)
  225. /**
  226. *@brief: RNN operator.
  227. *@par Inputs:
  228. *eight inputs: \n
  229. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  230. *@li cont:A 1D Tensor. Must be one of the following types: float16. The format must be ND.
  231. *@li x_static:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  232. *@li h_0:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  233. *@li w_xh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  234. *@li w_sh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  235. *@li w_hh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  236. *@li w_ho:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  237. *@li bias_h:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  238. *@li bias_o:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  239. *@par Attributes:
  240. *@li expose_hidden:An bool identifying if expose the hidden state of last time step. Default to false.
  241. *@li num_output:An integer identifying the number of output features. Default to 0.
  242. *@par Outputs:
  243. *two outputs: \n
  244. *@li o:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  245. *@li h_t:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  246. */
  247. REG_OP(RNN)
  248. .INPUT(x, TensorType({DT_FLOAT16}))
  249. .INPUT(cont, TensorType({DT_FLOAT16}))
  250. .OPTIONAL_INPUT(x_static, TensorType({DT_FLOAT16}))
  251. .OPTIONAL_INPUT(h_0, TensorType({DT_FLOAT16, DT_FLOAT}))
  252. .INPUT(w_xh, TensorType({DT_FLOAT16}))
  253. .INPUT(bias_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  254. .OPTIONAL_INPUT(w_sh, TensorType({DT_FLOAT16}))
  255. .INPUT(w_hh, TensorType({DT_FLOAT16}))
  256. .INPUT(w_ho, TensorType({DT_FLOAT16}))
  257. .INPUT(bias_o, TensorType({DT_FLOAT16, DT_FLOAT}))
  258. .OUTPUT(o, TensorType({DT_FLOAT16, DT_FLOAT}))
  259. .OUTPUT(h_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  260. .ATTR(num_output, Int, 0)
  261. .ATTR(expose_hidden, Bool, false)
  262. .OP_END_FACTORY_REG(RNN)
  263. /**
  264. *@brief: BasicRNNCell operator.
  265. *@par Inputs:
  266. *eight inputs: \n
  267. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  268. *@li cont:A 1D Tensor. Must be one of the following types: float16. The format must be ND.
  269. *@li w_xh_x_static:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  270. *@li h_0:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  271. *@li w_xh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  272. *@li w_hh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  273. *@li w_ho:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  274. *@li bias_h:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  275. *@li bias_o:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  276. *@par Attributes:
  277. *@li expose_hidden:An bool identifying if expose the hidden state of last time step. Default to false.
  278. *@li num_output:An integer identifying the number of output features. Default to 0.
  279. *@par Outputs:
  280. *two outputs: \n
  281. *@li o_t:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  282. *@li h_t:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  283. */
  284. REG_OP(BasicRNNCell)
  285. .INPUT(x, TensorType({DT_FLOAT16}))
  286. .OPTIONAL_INPUT(cont, TensorType({DT_FLOAT16}))
  287. .OPTIONAL_INPUT(w_xh_x_static, TensorType({DT_FLOAT16, DT_FLOAT}))
  288. .OPTIONAL_INPUT(h_0, TensorType({DT_FLOAT16, DT_FLOAT}))
  289. .INPUT(w_xh, TensorType({DT_FLOAT16}))
  290. .INPUT(bias_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  291. .OPTIONAL_INPUT(w_hh, TensorType({DT_FLOAT16}))
  292. .INPUT(w_ho, TensorType({DT_FLOAT16}))
  293. .INPUT(bias_o, TensorType({DT_FLOAT16, DT_FLOAT}))
  294. .OUTPUT(o_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  295. .OUTPUT(h_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  296. .ATTR(expose_hidden, Bool, false)
  297. .ATTR(num_output, Int, 0)
  298. .OP_END_FACTORY_REG(BasicRNNCell)
  299. } // namespace ge
  300. #endif // GE_OP_RNN_H

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