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.

linalg_ops.h 14 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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_LINALG_OPS_H_
  17. #define GE_OP_LINALG_OPS_H_
  18. #include "graph/operator_reg.h"
  19. #include "graph/operator.h"
  20. namespace ge {
  21. /**
  22. *@brief Computes the reverse mode backpropagated gradient of the Cholesky \n
  23. algorithm.
  24. *@par Inputs:
  25. *The input x has to be symmetric and positive definite. Inputs include: \n
  26. *@li x:A Tensor. Must be one of the following types: double, float32. Output \n
  27. of batch Cholesky algorithm x = cholesky(A). Shape is [..., M, M]. Algorithm \n
  28. depends only on lower triangular part of the innermost matrices of this tensor.
  29. *@li grad:A Tensor. Must have the same type as l. df/dx where f is some \n
  30. scalar function. Shape is [..., M, M]. Algorithm depends only on lower \n
  31. triangular part of the innermost matrices of this tensor.
  32. *@par Outputs:
  33. *y:A Tensor. Has the same type as x.
  34. *@attention Constraints: \n
  35. *The input x is a tensor of shape [..., M, M] whose inner-most 2 dimensions \n
  36. form square matrices. \n
  37. *@par Third-party framework compatibility
  38. *Compatible with tensorflow CholeskyGrad operator.
  39. */
  40. REG_OP(CholeskyGrad)
  41. .INPUT(x, TensorType({DT_FLOAT, DT_DOUBLE}))
  42. .INPUT(grad, TensorType({DT_FLOAT, DT_DOUBLE}))
  43. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  44. .OP_END_FACTORY_REG(CholeskyGrad)
  45. /**
  46. *@brief Computes the Cholesky decomposition of one or more square matrices.
  47. *@par Inputs:
  48. *The input x has to be symmetric and positive definite.Inputs include: \n
  49. *x:A Tensor. Must be one of the following types: double, float32. Shape \n
  50. is [..., M, M].
  51. *@par Outputs:
  52. *y:A Tensor. Has the same type as x.
  53. *@attention Constraints: \n
  54. *The input x is a tensor of shape [..., M, M] whose inner-most 2 dimensions \n
  55. form square matrices. \n
  56. *@par Third-party framework compatibility
  57. *Compatible with tensorflow Cholesky operator.
  58. */
  59. REG_OP(Cholesky)
  60. .INPUT(x, TensorType({DT_FLOAT, DT_DOUBLE}))
  61. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  62. .OP_END_FACTORY_REG(Cholesky)
  63. /**
  64. *@brief Computes the sign and the log of the absolute value of the determinant \n
  65. of one or more square matrices.
  66. *@par Inputs:
  67. *The input x is a tensor of shape [N, M, M] whose inner-most 2 dimensions \n
  68. form square matrices. Inputs include: \n
  69. *x:A Tensor. Must be one of the following types: double, float32. Shape is \n
  70. [..., M, M].
  71. *@par Outputs:
  72. *@li y:A Tensor. Has the same type as x.
  73. *@li sign:A Tensor. Has the same type as x.
  74. *@attention Constraints: \n
  75. *The input x is a tensor of shape [N, M, M] whose inner-most 2 dimensions \n
  76. form square matrices. \n
  77. *@par Third-party framework compatibility
  78. *Compatible with tensorflow LogMatrixDeterminant operator.
  79. */
  80. REG_OP(LogMatrixDeterminant)
  81. .INPUT(x, TensorType({DT_FLOAT, DT_DOUBLE}))
  82. .OUTPUT(sign, TensorType({DT_FLOAT, DT_DOUBLE}))
  83. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  84. .OP_END_FACTORY_REG(LogMatrixDeterminant)
  85. /**
  86. *@brief Computes the determinant of one or more square matrices.
  87. *@par Inputs:
  88. *The input x is a tensor of shape [N, M, M] whose inner-most 2 dimensions \n
  89. form square matrices. Inputs include: \n
  90. *x:A Tensor. Must be one of the following types: double, float32. Shape is \n
  91. [..., M, M].
  92. *@par Outputs:
  93. *y:A Tensor. Has the same type as x.
  94. *@attention Constraints: \n
  95. *The input x is a tensor of shape [..., M, M] whose inner-most 2 dimensions \n
  96. form square matrices. \n
  97. *@par Third-party framework compatibility
  98. *Compatible with tensorflow MatrixDeterminant operator.
  99. */
  100. REG_OP(MatrixDeterminant)
  101. .INPUT(x, TensorType({DT_FLOAT, DT_DOUBLE}))
  102. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  103. .OP_END_FACTORY_REG(MatrixDeterminant)
  104. /**
  105. *@brief Computes the inverse of one or more square invertible matrices or \n
  106. their adjoints (conjugate transposes).
  107. *@par Inputs:
  108. *The input x is a tensor of shape [..., M, M] whose inner-most 2 dimensions \n
  109. form square matrices. Inputs include: \n
  110. *x:A Tensor. Must be one of the following types: double, float. Shape is \n
  111. [..., M, M].
  112. *@par Attributes:
  113. *adjoint:An optional bool. Defaults to False.Boolean indicating whether to \n
  114. deal with matrix or its (block-wise) adjoint.
  115. *@par Outputs:
  116. *y:A Tensor. Has the same type as x.
  117. *@attention Constraints: \n
  118. *The input x is a tensor of shape [..., M, M] whose inner-most 2 dimensions \n
  119. form square matrices. \n
  120. *@par Third-party framework compatibility
  121. *Compatible with tensorflow MatrixInverse operator.
  122. */
  123. REG_OP(MatrixInverse)
  124. .INPUT(x, TensorType({DT_FLOAT, DT_DOUBLE}))
  125. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  126. .ATTR(adjoint, Bool, false)
  127. .OP_END_FACTORY_REG(MatrixInverse)
  128. /**
  129. *@brief Solves systems of linear equations.
  130. *@par Inputs:
  131. *The input rhs must have the same type as matrix. Inputs include: \n
  132. *@li matrix:A Tensor. Must be one of the following types: double, float. \n
  133. Shape is [..., M, M].
  134. *@li rhs:A Tensor. Must have the same type as matrix. Shape is [..., M, K].
  135. *@par Attributes:
  136. *adjoint:An optional bool. Defaults to False.Boolean indicating whether to \n
  137. solve with matrix or its (block-wise) adjoint.
  138. *@par Outputs:
  139. *y:A Tensor. Has the same type as matrix.
  140. *@attention Constraints: \n
  141. *The input matrix is a tensor of shape [..., M, M] whose inner-most 2 \n
  142. dimensions form square matrices. \n
  143. *@par Third-party framework compatibility
  144. *Compatible with tensorflow MatrixSolve operator.
  145. */
  146. REG_OP(MatrixSolve)
  147. .INPUT(matrix, TensorType({DT_FLOAT, DT_DOUBLE}))
  148. .INPUT(rhs, TensorType({DT_FLOAT, DT_DOUBLE}))
  149. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  150. .ATTR(adjoint, Bool, false)
  151. .OP_END_FACTORY_REG(MatrixSolve)
  152. /**
  153. *@brief Solves systems of linear equations.
  154. *@par Inputs:
  155. *The input rhs must have the same type as matrix. Inputs include: \n
  156. *@li matrix:A Tensor. Shape is [..., M, M].
  157. *@li rhs:A Tensor. Must have the same type as matrix. Shape is [..., M, K].
  158. *@li l2:0-D double Tensor. Ignored if fast=False.
  159. *@par Attributes:
  160. *fast:bool. Defaults to True.
  161. *@par Outputs:
  162. *y:Tensor of shape [..., N, K] whose inner-most 2 dimensions form M-by-K \n
  163. matrices that solve the equations matrix[..., :, :] * output[..., :, :] = \n
  164. rhs[..., :, :] in the least squares sense.
  165. *@attention Constraints: \n
  166. *The input matrix matrix is a tensor of shape [..., M, M] whose inner-most 2 \n
  167. dimensions form square matrices. \n
  168. *@par Third-party framework compatibility
  169. *Compatible with tensorflow MatrixSolveLs operator.
  170. */
  171. REG_OP(MatrixSolveLs)
  172. .INPUT(matrix, TensorType({DT_FLOAT, DT_DOUBLE}))
  173. .INPUT(rhs, TensorType({DT_FLOAT, DT_DOUBLE}))
  174. .INPUT(l2, TensorType({DT_DOUBLE}))
  175. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  176. .ATTR(fast, Bool, true)
  177. .OP_END_FACTORY_REG(MatrixSolveLs)
  178. /**
  179. *@brief Solves systems of linear equations with upper or lower triangular \n
  180. matrices by backsubstitution.
  181. *@par Inputs:
  182. *The input rhs must have the same type as matrix. Inputs include: \n
  183. *@li matrix: A Tensor. Must be one of the following types: double, float. \n
  184. Shape is [..., M, M].
  185. *@li rhs:A Tensor. Must have the same type as matrix. Shape is [..., M, K].
  186. *@par Attributes:
  187. *@li lower: An optional bool. Defaults to True. Boolean indicating whether \n
  188. the innermost matrices in matrix are lower or upper triangular.
  189. *@li An optional bool. Defaults to False. Boolean indicating whether to solve \n
  190. with matrix or its (block-wise) adjoint.
  191. *@par Outputs:
  192. *y:A Tensor. Has the same type as matrix.
  193. *@attention Constraints: \n
  194. *The input matrix is a tensor of shape [..., M, M] whose inner-most 2 \n
  195. dimensions form square matrices. \n
  196. *@par Third-party framework compatibility
  197. *Compatible with tensorflow MatrixTriangularSolve operator.
  198. */
  199. REG_OP(MatrixTriangularSolve)
  200. .INPUT(matrix, TensorType({DT_FLOAT, DT_DOUBLE}))
  201. .INPUT(rhs, TensorType({DT_FLOAT, DT_DOUBLE}))
  202. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  203. .ATTR(lower, Bool, true)
  204. .ATTR(adjoint, Bool, false)
  205. .OP_END_FACTORY_REG(MatrixTriangularSolve)
  206. /**
  207. *@brief Computes the QR decompositions of one or more matrices.
  208. *@par Inputs:
  209. *The input shape of x must be [..., M, N]. Inputs include: \n
  210. *x:A Tensor whose shape is [..., M, N]. Must be one of the following types: \n
  211. double, float.
  212. *@par Attributes:
  213. *full_matrices: An optional bool. Defaults to False. If true, compute \n
  214. full-sized q and r. If false (the default), compute only the leading P \n
  215. columns of q.
  216. *@par Outputs:
  217. *@li q: A Tensor. Has the same type as x.
  218. *@li r: A Tensor. Has the same type as x.
  219. *@attention Constraints: \n
  220. *The input matrix x is a tensor of shape [..., M, N] whose inner-most 2 \n
  221. dimensions form matrices of size [M, N]. \n
  222. *@par Third-party framework compatibility
  223. *Compatible with tensorflow Qr operator.
  224. */
  225. REG_OP(Qr)
  226. .INPUT(x, TensorType({ DT_FLOAT16, DT_FLOAT, DT_DOUBLE }))
  227. .OUTPUT(q, TensorType({ DT_FLOAT16, DT_FLOAT, DT_DOUBLE }))
  228. .OUTPUT(r, TensorType({ DT_FLOAT16, DT_FLOAT, DT_DOUBLE }))
  229. .ATTR(full_matrices, Bool, false)
  230. .OP_END_FACTORY_REG(Qr)
  231. /**
  232. *@brief Computes the eigen decomposition of a batch of self-adjoint matrices.
  233. *@par Inputs:
  234. *The input shape of x must be [..., N, N]. Inputs include: \n
  235. *x:Tensor of shape [..., N, N]. Only the lower triangular part of each inner \n
  236. inner matrix is referenced.
  237. *@par Attributes:
  238. *compute_v:bool. Defaults to True.
  239. *@par Outputs:
  240. *@li eigen_value:Eigenvalues. Shape is [..., N]. Sorted in non-decreasing order.
  241. *@li eigen_vector:Shape is [..., N, N]. The columns of the inner most matrices \n
  242. contain eigenvectors of the corresponding matrices in tensor
  243. *@attention Constraints: \n
  244. *The input x is a tensor of shape [..., N, N] whose inner-most 2 dimensions \n
  245. form square matrices. \n
  246. *@par Third-party framework compatibility
  247. *Compatible with tensorflow SelfAdjointEig operator.
  248. */
  249. REG_OP(SelfAdjointEig)
  250. .INPUT(x, TensorType({ DT_DOUBLE, DT_FLOAT }))
  251. .OUTPUT(eigen_value, TensorType({ DT_DOUBLE, DT_FLOAT }))
  252. .OUTPUT(eigen_vector, TensorType({ DT_DOUBLE, DT_FLOAT }))
  253. .ATTR(compute_v, Bool, true)
  254. .OP_END_FACTORY_REG(SelfAdjointEig)
  255. /**
  256. *@brief Computes the singular value decompositions of one or more matrices.
  257. *@par Inputs:
  258. *The input shape of x must be [..., N, N]. Inputs include: \n
  259. *x:Tensor of shape [..., M, N]. Let P be the minimum of M and N.
  260. *@par Attributes:
  261. *compute_uv:If True then left and right singular vectors will be computed and \n
  262. returned in u and v, respectively. Otherwise, only the singular values will \n
  263. be computed, which can be significantly faster.
  264. *@par Outputs:
  265. *@li sigma:Singular values. Shape is [..., P]. The values are sorted in \n
  266. reverse order of magnitude, so s[..., 0] is the largest value, s[..., 1] \n
  267. is the second largest, etc.
  268. *@li u:Left singular vectors. If full_matrices is False (default) then shape \n
  269. is [..., M, P]; if full_matrices is True then shape is [..., M, M]. Not \n
  270. returned if compute_uv is False.
  271. *@li v:Right singular vectors. If full_matrices is False (default) then shape \n
  272. is [..., N, P]. If full_matrices is True then shape is [..., N, N]. Not \n
  273. returned if compute_uv is False.
  274. *@attention Constraints: \n
  275. *The input x is a tensor of shape [..., N, N] whose inner-most 2 dimensions \n
  276. form square matrices. \n
  277. *@par Third-party framework compatibility
  278. *Compatible with tensorflow Svd operator
  279. */
  280. REG_OP(Svd)
  281. .INPUT(x, TensorType({ DT_DOUBLE, DT_FLOAT }))
  282. .OUTPUT(sigma, TensorType({ DT_DOUBLE, DT_FLOAT }))
  283. .OUTPUT(u, TensorType({ DT_DOUBLE, DT_FLOAT }))
  284. .OUTPUT(v, TensorType({ DT_DOUBLE, DT_FLOAT }))
  285. .ATTR(compute_uv, Bool, true)
  286. .ATTR(full_matrices, Bool, false)
  287. .OP_END_FACTORY_REG(Svd)
  288. /**
  289. *@brief Computes the LU decomposition of one or more square matrices.
  290. *@par Inputs:
  291. *input: A tensor of shape `[..., M, M]` whose inner-most 2 dimensions form \n
  292. matrices of size `[M, M]`.
  293. *@par Outputs:
  294. *@li lu: A tensor of shape `[..., M, M]` whose strictly lower triangular part \n
  295. denotes the lower triangular factor `L` with unit diagonal.
  296. *@li p: upper triangular part denotes the upper triangular factor `U`.Permutation \n
  297. of the rows encoded as a list of indices in `0..M-1`. Shape is `[..., M]`.
  298. *@par Third-party framework compatibility
  299. * Compatible with TensorFlow Lu operator.
  300. */
  301. REG_OP(Lu)
  302. .INPUT(input, TensorType({DT_FLOAT, DT_DOUBLE}))
  303. .OUTPUT(lu, TensorType({DT_FLOAT, DT_DOUBLE}))
  304. .OUTPUT(p, TensorType({DT_INT32, DT_INT64}))
  305. .REQUIRED_ATTR(output_idx_type, Type)
  306. .OP_END_FACTORY_REG(Lu)
  307. /**
  308. *@brief Computes the matrix square root of one or more square matrices.
  309. *@par Inputs:
  310. *input: Shape is `[..., M, M]`.
  311. *@par Outputs:
  312. y: Shape is `[..., M, M]`.
  313. *@par Third-party framework compatibility
  314. * Compatible with TensorFlow MatrixSquareRoot operator.
  315. */
  316. REG_OP(MatrixSquareRoot)
  317. .INPUT(input, TensorType({DT_FLOAT, DT_DOUBLE}))
  318. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  319. .OP_END_FACTORY_REG(MatrixSquareRoot)
  320. /**
  321. *@brief Solves tridiagonal systems of equations.
  322. *@par Inputs:
  323. *@li diagonals: Tensor of shape `[..., 3, M]` whose innermost 2 dimensions represent the \n
  324. tridiagonal matrices with three rows being the superdiagonal, diagonals, and \n
  325. subdiagonals, in order. The last element of the superdiagonal and the first \n
  326. element of the subdiagonal is ignored.
  327. *@li rhs: Tensor of shape `[..., M, K]`, representing K right-hand sides per each \n
  328. left-hand side.
  329. *@par Outputs:
  330. y: Tensor of shape `[..., M, K]` containing the solutions
  331. *@par Third-party framework compatibility
  332. * Compatible with TensorFlow TridiagonalSolve operator.
  333. */
  334. REG_OP(TridiagonalSolve)
  335. .INPUT(diagonals, TensorType({DT_FLOAT, DT_DOUBLE}))
  336. .INPUT(rhs, TensorType({DT_FLOAT, DT_DOUBLE}))
  337. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  338. .ATTR(partial_pivoting, Bool, true)
  339. .OP_END_FACTORY_REG(TridiagonalSolve)
  340. } // namespace ge
  341. #endif // GE_OP_LINALG_OPS_H_

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