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 32 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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:
  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 . \n
  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 . \n
  38. *@par Outputs:
  39. *seven outputs:
  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 . \n
  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 . \n
  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: DynamicRNNGrad 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_NZ.
  89. *@li b:A 1D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  90. *@li y:A 1D Tensor. Must be one of the following types: int32. The format must be FRACTAL_NZ.
  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 h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  94. *@li c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  95. *@li dy:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  96. *@li dh:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  97. *@li dc:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  98. *@li i:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  99. *@li j:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  100. *@li f:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  101. *@li o:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  102. *@li tanhct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  103. *@li seq_length:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  104. *@li mask:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  105. *@li wci:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  106. *@li wcf:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  107. *@li wco:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  108. *@par Attributes:
  109. *@li cell_type:An string identifying the cell type in the op. Default to "LSTM". Only LSTM is currently supported.
  110. *@li direction:An string identifying the direction in the op. Default to "UNIDIRECTIONAL". Only UNIDIRECTIONAL is currently supported.
  111. *@li cell_depth:An integer identifying the cell depth in the op. Default to 1.
  112. *@li use_peephole:An bool identifying if use peephole in the op. Default to false.
  113. *@li keep_prob:An float identifying the keep prob in the op. Default to 1.
  114. *@li cell_clip:An float identifying the cell clip in the op. Default to -1.
  115. *@li num_proj:An integer identifying the num projection in the op. Default to 0.
  116. *@li time_major:An bool identifying the time major in the op. Default to false.
  117. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported.
  118. *@li forget_bias:An float identifying the forget bias in the op. Default to 0.
  119. *@li is_training:An bool identifying is training in the op. Default to true.
  120. *@par Outputs:
  121. *eight outputs: \n
  122. *@li dw:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  123. *@li db:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  124. *@li dx:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  125. *@li dh_prev:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  126. *@li dc_prev:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  127. */
  128. REG_OP(DynamicRNNGrad)
  129. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
  130. .INPUT(w, TensorType({DT_FLOAT16, DT_FLOAT}))
  131. .INPUT(b, TensorType({DT_FLOAT16, DT_FLOAT}))
  132. .INPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  133. .INPUT(init_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  134. .INPUT(init_c, TensorType({DT_FLOAT16, DT_FLOAT}))
  135. .INPUT(h, TensorType({DT_FLOAT16, DT_FLOAT}))
  136. .INPUT(c, TensorType({DT_FLOAT16, DT_FLOAT}))
  137. .INPUT(dy, TensorType({DT_FLOAT16, DT_FLOAT}))
  138. .INPUT(dh, TensorType({DT_FLOAT16, DT_FLOAT}))
  139. .INPUT(dc, TensorType({DT_FLOAT16, DT_FLOAT}))
  140. .INPUT(i, TensorType({DT_FLOAT16, DT_FLOAT}))
  141. .INPUT(j, TensorType({DT_FLOAT16, DT_FLOAT}))
  142. .INPUT(f, TensorType({DT_FLOAT16, DT_FLOAT}))
  143. .INPUT(o, TensorType({DT_FLOAT16, DT_FLOAT}))
  144. .OPTIONAL_INPUT(tanhct, TensorType({DT_FLOAT16, DT_FLOAT}))
  145. .OPTIONAL_INPUT(seq_length, TensorType({DT_INT32}))
  146. .OPTIONAL_INPUT(mask, TensorType({DT_UINT8}))
  147. .OPTIONAL_INPUT(wci, TensorType({DT_FLOAT16, DT_FLOAT}))
  148. .OPTIONAL_INPUT(wcf, TensorType({DT_FLOAT16, DT_FLOAT}))
  149. .OPTIONAL_INPUT(wco, TensorType({DT_FLOAT16, DT_FLOAT}))
  150. .OUTPUT(dw, TensorType({DT_FLOAT16, DT_FLOAT}))
  151. .OUTPUT(db, TensorType({DT_FLOAT16, DT_FLOAT}))
  152. .OUTPUT(dx, TensorType({DT_FLOAT16, DT_FLOAT}))
  153. .OUTPUT(dh_prev, TensorType({DT_FLOAT16, DT_FLOAT}))
  154. .OUTPUT(dc_prev, TensorType({DT_FLOAT16, DT_FLOAT}))
  155. .DYNAMIC_OUTPUT(dwci, TensorType({DT_FLOAT16, DT_FLOAT}))
  156. .DYNAMIC_OUTPUT(dwcf, TensorType({DT_FLOAT16, DT_FLOAT}))
  157. .DYNAMIC_OUTPUT(dwco, TensorType({DT_FLOAT16, DT_FLOAT}))
  158. .ATTR(cell_type, String, "LSTM")
  159. .ATTR(direction, String, "UNIDIRECTIONAL")
  160. .ATTR(cell_depth, Int, 0)
  161. .ATTR(use_peephole, Bool, false)
  162. .ATTR(keep_prob, Float, -1.0)
  163. .ATTR(cell_clip, Float, -1.0)
  164. .ATTR(num_proj, Int, 0)
  165. .ATTR(time_major, Bool, true)
  166. .ATTR(forget_bias, Float, 0.0)
  167. .OP_END_FACTORY_REG(DynamicRNNGrad)
  168. /**
  169. *@brief: DynamicRNN calculation.
  170. *@par Inputs:
  171. *ten inputs:
  172. *@li x:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  173. *@li w:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_ZN_LSTM.
  174. *@li b:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  175. *@li seq_length:A 1D Tensor. Must be one of the following types: int32. The format must be ND.
  176. *@li init_h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  177. *@li init_c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  178. *@li wci:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_ZN_LSTM.
  179. *@li wcf:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_ZN_LSTM.
  180. *@li wco:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_ZN_LSTM.
  181. *@li mask:A 1D Tensor. Must be one of the following types: uint8. The format must be ND . \n
  182. *@par Attributes:
  183. *@li cell_type:An string identifying the cell type in the op. Default to "LSTM". Only LSTM is currently supported.
  184. *@li direction:An string identifying the direction in the op. Default to "UNIDIRECTIONAL". Only UNIDIRECTIONAL is currently supported.
  185. *@li cell_depth:An integer identifying the cell depth in the op. Default to 1.
  186. *@li use_peephole:An bool identifying if use peephole in the op. Default to false.
  187. *@li keep_prob:An float identifying the keep prob in the op. Default to 1.
  188. *@li cell_clip:An float identifying the cell clip in the op. Default to -1.
  189. *@li num_proj:An integer identifying the num projection in the op. Default to 0.
  190. *@li time_major:An bool identifying the time major in the op. Default to true.
  191. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported.
  192. *@li forget_bias:An float identifying the forget bias in the op. Default to 0.
  193. *@li is_training:An bool identifying is training in the op. Default to true . \n
  194. *@par Outputs:
  195. *eight outputs:
  196. *@li y:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  197. *@li output_h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  198. *@li output_c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  199. *@li i:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  200. *@li j:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  201. *@li f:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  202. *@li o:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  203. *@li tanhct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  204. */
  205. REG_OP(DynamicRNN)
  206. .INPUT(x, TensorType({DT_FLOAT16, DT_FLOAT}))
  207. .INPUT(w, TensorType({DT_FLOAT16, DT_FLOAT}))
  208. .INPUT(b, TensorType({DT_FLOAT16, DT_FLOAT}))
  209. .OPTIONAL_INPUT(seq_length, TensorType({DT_INT32}))
  210. .OPTIONAL_INPUT(init_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  211. .OPTIONAL_INPUT(init_c, TensorType({DT_FLOAT16, DT_FLOAT}))
  212. .OPTIONAL_INPUT(wci, TensorType({DT_FLOAT16, DT_FLOAT}))
  213. .OPTIONAL_INPUT(wcf, TensorType({DT_FLOAT16, DT_FLOAT}))
  214. .OPTIONAL_INPUT(wco, TensorType({DT_FLOAT16, DT_FLOAT}))
  215. .OPTIONAL_INPUT(mask, TensorType({DT_UINT8}))
  216. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  217. .OUTPUT(output_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  218. .OUTPUT(output_c, TensorType({DT_FLOAT16, DT_FLOAT}))
  219. .OUTPUT(i, TensorType({DT_FLOAT16, DT_FLOAT}))
  220. .OUTPUT(j, TensorType({DT_FLOAT16, DT_FLOAT}))
  221. .OUTPUT(f, TensorType({DT_FLOAT16, DT_FLOAT}))
  222. .OUTPUT(o, TensorType({DT_FLOAT16, DT_FLOAT}))
  223. .OUTPUT(tanhc, TensorType({DT_FLOAT16, DT_FLOAT}))
  224. .ATTR(cell_type, String, "LSTM")
  225. .ATTR(direction, String, "UNIDIRECTIONAL")
  226. .ATTR(cell_depth, Int, 1)
  227. .ATTR(use_peephole, Bool, false)
  228. .ATTR(keep_prob, Float, 1.0)
  229. .ATTR(cell_clip, Float, -1.0)
  230. .ATTR(num_proj, Int, 0)
  231. .ATTR(time_major, Bool, true)
  232. .ATTR(activation, String, "tanh")
  233. .ATTR(forget_bias, Float, 0.0)
  234. .ATTR(is_training, Bool, true)
  235. .OP_END_FACTORY_REG(DynamicRNN)
  236. /**
  237. *@brief: LSTMInputGrad calculation.
  238. *@par Inputs:
  239. *ten inputs: \n
  240. *@li w:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  241. *@li init_c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  242. *@li h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  243. *@li c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  244. *@li dy:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  245. *@li dh:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  246. *@li dc:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  247. *@li i:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  248. *@li j:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  249. *@li f:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  250. *@li o:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  251. *@li tanhct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  252. *@par Outputs:
  253. *eight outputs: \n
  254. *@li dx:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  255. *@li dh_prev:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  256. *@li dc_prev:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  257. */
  258. REG_OP(LSTMInputGrad)
  259. .INPUT(w, TensorType({DT_FLOAT16, DT_FLOAT}))
  260. .INPUT(init_c, TensorType({DT_FLOAT16, DT_FLOAT}))
  261. .INPUT(c, TensorType({DT_FLOAT16, DT_FLOAT}))
  262. .INPUT(dy, TensorType({DT_FLOAT16, DT_FLOAT}))
  263. .INPUT(dh, TensorType({DT_FLOAT16, DT_FLOAT}))
  264. .INPUT(dc, TensorType({DT_FLOAT16, DT_FLOAT}))
  265. .INPUT(i, TensorType({DT_FLOAT16, DT_FLOAT}))
  266. .INPUT(j, TensorType({DT_FLOAT16, DT_FLOAT}))
  267. .INPUT(f, TensorType({DT_FLOAT16, DT_FLOAT}))
  268. .INPUT(o, TensorType({DT_FLOAT16, DT_FLOAT}))
  269. .OPTIONAL_INPUT(tanhct, TensorType({DT_FLOAT16, DT_FLOAT}))
  270. .OUTPUT(dx, TensorType({DT_FLOAT16, DT_FLOAT}))
  271. .OUTPUT(dh_prev, TensorType({DT_FLOAT16, DT_FLOAT}))
  272. .OUTPUT(dc_prev, TensorType({DT_FLOAT16, DT_FLOAT}))
  273. .OUTPUT(dgate, TensorType({DT_FLOAT16}))
  274. .OP_END_FACTORY_REG(LSTMInputGrad)
  275. /**
  276. *@brief: Basic LSTM Cell backward calculation.Calculate the gradient of input and hidden state.
  277. *@par Inputs:
  278. *three inputs:
  279. *@li dgate:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  280. *@li w:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  281. *@li dropout_mask:A 1D Tensor. Must be one of the following types: uint8. The format must be ND . \n
  282. *@par Attributes:
  283. *keep_prob:An integer identifying the keep prob in the op. Default to 1 . \n
  284. *@par Outputs:
  285. *two outputs:
  286. *@li dxt:A 4D Tensor. Must be one of the following types: float16, float32.
  287. *@li dht:A 4D Tensor. Must be one of the following types: float16, float32.
  288. */
  289. REG_OP(BasicLSTMCellInputGrad)
  290. .INPUT(dgate, TensorType({DT_FLOAT16}))
  291. .INPUT(w, TensorType({DT_FLOAT16}))
  292. .OPTIONAL_INPUT(dropout_mask, TensorType({DT_UINT8}))
  293. .OUTPUT(dxt, TensorType({DT_FLOAT16, DT_FLOAT32}))
  294. .OUTPUT(dht, TensorType({DT_FLOAT16, DT_FLOAT32}))
  295. .ATTR(keep_prob, Float, 1.0)
  296. .OP_END_FACTORY_REG(BasicLSTMCellInputGrad)
  297. /**
  298. *@brief: Basic LSTM Cell backward calculation.Calculate the gradient of weight and bias.
  299. *@par Inputs:
  300. *three inputs:
  301. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  302. *@li h:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  303. *@li dgate:A 4D Tensor. Must be one of the following types: uint8. The format must be FRACTAL_NZ . \n
  304. *@par Outputs:
  305. *two outputs:
  306. *@li dw:A 4D Tensor. Must be one of the following types: float16.
  307. *@li db:A 4D Tensor. Must be one of the following types: float16, float32.
  308. */
  309. REG_OP(BasicLSTMCellWeightGrad)
  310. .INPUT(x, TensorType({DT_FLOAT16}))
  311. .INPUT(h, TensorType({DT_FLOAT16}))
  312. .INPUT(dgate, TensorType({DT_FLOAT16}))
  313. .OUTPUT(dw, TensorType({DT_FLOAT16}))
  314. .OUTPUT(db, TensorType({DT_FLOAT16, DT_FLOAT32}))
  315. .OP_END_FACTORY_REG(BasicLSTMCellWeightGrad)
  316. /**
  317. *@brief: Basic LSTM Cell backward calculation.Calculate the gradient of gates and cell state.
  318. *@par Inputs:
  319. *eight inputs:
  320. *@li c:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  321. *@li dht:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  322. *@li dct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  323. *@li it:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  324. *@li jt:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  325. *@li ft:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  326. *@li ot:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  327. *@li tanhct:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ . \n
  328. *@par Attributes:
  329. *@li forget_bias:An integer identifying the forget bias in the op. Default to 1.
  330. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported . \n
  331. *@par Outputs:
  332. *two outputs:
  333. *@li dgate:A 4D Tensor. Must be one of the following types: float16.
  334. *@li dct_1:A 4D Tensor. Must be one of the following types: float16, float32.
  335. */
  336. REG_OP(BasicLSTMCellCStateGrad)
  337. .INPUT(c, TensorType({DT_FLOAT16, DT_FLOAT}))
  338. .INPUT(dht, TensorType({DT_FLOAT16, DT_FLOAT}))
  339. .INPUT(dct, TensorType({DT_FLOAT16, DT_FLOAT}))
  340. .INPUT(it, TensorType({DT_FLOAT16, DT_FLOAT}))
  341. .INPUT(jt, TensorType({DT_FLOAT16, DT_FLOAT}))
  342. .INPUT(ft, TensorType({DT_FLOAT16, DT_FLOAT}))
  343. .INPUT(ot, TensorType({DT_FLOAT16, DT_FLOAT}))
  344. .INPUT(tanhct, TensorType({DT_FLOAT16, DT_FLOAT}))
  345. .OUTPUT(dgate, TensorType({DT_FLOAT16}))
  346. .OUTPUT(dct_1, TensorType({DT_FLOAT16, DT_FLOAT}))
  347. .ATTR(forget_bias, Float, 1.0)
  348. .ATTR(activation, String, "tanh")
  349. .OP_END_FACTORY_REG(BasicLSTMCellCStateGrad)
  350. /**
  351. *@brief: RNN operator.
  352. *@par Inputs:
  353. *eight inputs:
  354. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  355. *@li cont:A 1D Tensor. Must be one of the following types: float16. The format must be ND.
  356. *@li x_static:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  357. *@li h_0:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  358. *@li w_xh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  359. *@li w_sh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  360. *@li w_hh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  361. *@li w_ho:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  362. *@li bias_h:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  363. *@li bias_o:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND . \n
  364. *@par Attributes:
  365. *@li expose_hidden:An bool identifying if expose the hidden state of last time step. Default to false.
  366. *@li num_output:An integer identifying the number of output features. Default to 0 . \n
  367. *@par Outputs:
  368. *two outputs:
  369. *@li o:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  370. *@li h_t:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  371. *@par Restrictions:
  372. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  373. */
  374. REG_OP(RNN)
  375. .INPUT(x, TensorType({DT_FLOAT16}))
  376. .INPUT(cont, TensorType({DT_FLOAT16}))
  377. .OPTIONAL_INPUT(x_static, TensorType({DT_FLOAT16}))
  378. .OPTIONAL_INPUT(h_0, TensorType({DT_FLOAT16, DT_FLOAT}))
  379. .INPUT(w_xh, TensorType({DT_FLOAT16}))
  380. .INPUT(bias_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  381. .OPTIONAL_INPUT(w_sh, TensorType({DT_FLOAT16}))
  382. .INPUT(w_hh, TensorType({DT_FLOAT16}))
  383. .INPUT(w_ho, TensorType({DT_FLOAT16}))
  384. .INPUT(bias_o, TensorType({DT_FLOAT16, DT_FLOAT}))
  385. .OUTPUT(o, TensorType({DT_FLOAT16, DT_FLOAT}))
  386. .OUTPUT(h_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  387. .ATTR(num_output, Int, 0)
  388. .ATTR(expose_hidden, Bool, false)
  389. .OP_END_FACTORY_REG(RNN)
  390. /**
  391. *@brief: BasicRNNCell operator.
  392. *@par Inputs:
  393. *eight inputs:
  394. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  395. *@li cont:A 1D Tensor. Must be one of the following types: float16. The format must be ND.
  396. *@li w_xh_x_static:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  397. *@li h_0:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  398. *@li w_xh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  399. *@li w_hh:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  400. *@li w_ho:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_Z.
  401. *@li bias_h:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  402. *@li bias_o:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND . \n
  403. *@par Attributes:
  404. *@li expose_hidden:An bool identifying if expose the hidden state of last time step. Default to false.
  405. *@li num_output:An integer identifying the number of output features. Default to 0 . \n
  406. *@par Outputs:
  407. *two outputs:
  408. *@li o_t:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  409. *@li h_t:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  410. */
  411. REG_OP(BasicRNNCell)
  412. .INPUT(x, TensorType({DT_FLOAT16}))
  413. .OPTIONAL_INPUT(cont, TensorType({DT_FLOAT16}))
  414. .OPTIONAL_INPUT(w_xh_x_static, TensorType({DT_FLOAT16, DT_FLOAT}))
  415. .OPTIONAL_INPUT(h_0, TensorType({DT_FLOAT16, DT_FLOAT}))
  416. .INPUT(w_xh, TensorType({DT_FLOAT16}))
  417. .INPUT(bias_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  418. .OPTIONAL_INPUT(w_hh, TensorType({DT_FLOAT16}))
  419. .INPUT(w_ho, TensorType({DT_FLOAT16}))
  420. .INPUT(bias_o, TensorType({DT_FLOAT16, DT_FLOAT}))
  421. .OUTPUT(o_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  422. .OUTPUT(h_t, TensorType({DT_FLOAT16, DT_FLOAT}))
  423. .ATTR(expose_hidden, Bool, false)
  424. .ATTR(num_output, Int, 0)
  425. .OP_END_FACTORY_REG(BasicRNNCell)
  426. /**
  427. *@brief: DynamicGRU calculation.
  428. *@par Inputs:
  429. *seven inputs: \n
  430. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  431. *@li w:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_ZN_LSTM.
  432. *@li b:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  433. *@li cw:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_ZN_LSTM.
  434. *@li cb:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  435. *@li seq_length:A 1D Tensor. Must be one of the following types: int32. The format must be ND.
  436. *@li init_h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  437. *@par Attributes:
  438. *@li direction:An string identifying the direction in the op. Default to "UNIDIRECTIONAL". Only UNIDIRECTIONAL is currently supported.
  439. *@li cell_depth:An integer identifying the cell depth in the op. Default to 1.
  440. *@li keep_prob:An float identifying the keep prob in the op. Default to 1.
  441. *@li cell_clip:An float identifying the cell clip in the op. Default to -1.
  442. *@li num_proj:An integer identifying the num projection in the op. Default to 0.
  443. *@li time_major:An bool identifying the time major in the op. Default to true.
  444. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported.
  445. *@li is_training:An bool identifying is training in the op. Default to true.
  446. *@par Outputs:
  447. *five outputs: \n
  448. *@li y:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  449. *@li output_h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  450. *@li r:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  451. *@li i:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  452. *@li n:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  453. *@par Restrictions:
  454. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  455. */
  456. REG_OP(DynamicGRU)
  457. .INPUT(x, TensorType({DT_FLOAT16}))
  458. .INPUT(w, TensorType({DT_FLOAT16}))
  459. .INPUT(b, TensorType({DT_FLOAT16, DT_FLOAT}))
  460. .INPUT(cw, TensorType({DT_FLOAT16}))
  461. .INPUT(cb, TensorType({DT_FLOAT16, DT_FLOAT}))
  462. .OPTIONAL_INPUT(seq_length, TensorType({DT_UINT32}))
  463. .OPTIONAL_INPUT(init_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  464. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  465. .OUTPUT(output_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  466. .OUTPUT(r, TensorType({DT_FLOAT16, DT_FLOAT}))
  467. .OUTPUT(i, TensorType({DT_FLOAT16, DT_FLOAT}))
  468. .OUTPUT(n, TensorType({DT_FLOAT16, DT_FLOAT}))
  469. .ATTR(direction, String, "UNIDIRECTIONAL")
  470. .ATTR(cell_depth, Int, 1)
  471. .ATTR(keep_prob, Float, 1.0)
  472. .ATTR(cell_clip, Float, -1.0)
  473. .ATTR(num_proj, Int, 0)
  474. .ATTR(time_major, Bool, true)
  475. .ATTR(activation, String, "tanh")
  476. .ATTR(is_training, Bool, true)
  477. .OP_END_FACTORY_REG(DynamicGRU)
  478. /**
  479. *@brief: DynamicGRUV2 calculation.
  480. *@par Inputs:
  481. *seven inputs: \n
  482. *@li x:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_NZ.
  483. *@li weight_input:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_ZN_LSTM.
  484. *@li weight_hidden:A 4D Tensor. Must be one of the following types: float16. The format must be FRACTAL_ZN_LSTM.
  485. *@li bias_input:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  486. *@li bias_hidden:A 1D Tensor. Must be one of the following types: float16, float32. The format must be ND.
  487. *@li seq_length:A 1D Tensor. Must be one of the following types: int32. The format must be ND.
  488. *@li init_h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  489. *@par Attributes:
  490. *@li direction:An string identifying the direction in the op. Default to "UNIDIRECTIONAL". Only UNIDIRECTIONAL is currently supported.
  491. *@li cell_depth:An integer identifying the cell depth in the op. Default to 1.
  492. *@li keep_prob:An float identifying the keep prob in the op. Default to 1.
  493. *@li cell_clip:An float identifying the cell clip in the op. Default to -1.
  494. *@li num_proj:An integer identifying the num projection in the op. Default to 0.
  495. *@li time_major:An bool identifying the time major in the op. Default to true.
  496. *@li activation:An string identifying the type of activation function in the op. Default to "tanh". Only tanh is currently supported.
  497. *@li gate_order:An string identifying the gate order in weight and bias. Default to "zrh". "rzh" is another option.
  498. *@li reset_after:An bool identifying whether to apply reset gate after matrix multiplication. Default to true.
  499. *@li is_training:An bool identifying is training in the op. Default to true.
  500. *@par Outputs:
  501. *six outputs: \n
  502. *@li y:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  503. *@li output_h:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  504. *@li update:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  505. *@li reset:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  506. *@li new:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  507. *@li hidden_new:A 4D Tensor. Must be one of the following types: float16, float32. The format must be FRACTAL_NZ.
  508. *@par Restrictions:
  509. *Warning: THIS FUNCTION IS EXPERIMENTAL. Please do not use.
  510. */
  511. REG_OP(DynamicGRUV2)
  512. .INPUT(x, TensorType({DT_FLOAT16}))
  513. .INPUT(weight_input, TensorType({DT_FLOAT16}))
  514. .INPUT(weight_hidden, TensorType({DT_FLOAT16}))
  515. .OPTIONAL_INPUT(bias_input, TensorType({DT_FLOAT16, DT_FLOAT}))
  516. .OPTIONAL_INPUT(bias_hidden, TensorType({DT_FLOAT16, DT_FLOAT}))
  517. .OPTIONAL_INPUT(seq_length, TensorType({DT_UINT32}))
  518. .OPTIONAL_INPUT(init_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  519. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  520. .OUTPUT(output_h, TensorType({DT_FLOAT16, DT_FLOAT}))
  521. .OUTPUT(update, TensorType({DT_FLOAT16, DT_FLOAT}))
  522. .OUTPUT(reset, TensorType({DT_FLOAT16, DT_FLOAT}))
  523. .OUTPUT(new, TensorType({DT_FLOAT16, DT_FLOAT}))
  524. .OUTPUT(hidden_new, TensorType({DT_FLOAT16, DT_FLOAT}))
  525. .ATTR(direction, String, "UNIDIRECTIONAL")
  526. .ATTR(cell_depth, Int, 1)
  527. .ATTR(keep_prob, Float, 1.0)
  528. .ATTR(cell_clip, Float, -1.0)
  529. .ATTR(num_proj, Int, 0)
  530. .ATTR(time_major, Bool, true)
  531. .ATTR(activation, String, "tanh")
  532. .ATTR(gate_order, String, "zrh")
  533. .ATTR(reset_after, Bool, true)
  534. .ATTR(is_training, Bool, true)
  535. .OP_END_FACTORY_REG(DynamicGRUV2)
  536. } // namespace ge
  537. #endif // GE_OP_RNN_H

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