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

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

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