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.

basic_lstm_cell.h 7.2 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_BASIC_LSTM_CELL_H
  17. #define GE_OP_BASIC_LSTM_CELL_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. } // namespace ge
  140. #endif // GE_OP_BASIC_LSTM_CELL_H

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