1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/compiler/xla/client/lib/self_adjoint_eig.h"
17
18 #include <memory>
19 #include <vector>
20
21 #include "tensorflow/compiler/xla/client/lib/arithmetic.h"
22 #include "tensorflow/compiler/xla/client/lib/comparators.h"
23 #include "tensorflow/compiler/xla/client/lib/constants.h"
24 #include "tensorflow/compiler/xla/client/lib/loops.h"
25 #include "tensorflow/compiler/xla/client/lib/math.h"
26 #include "tensorflow/compiler/xla/client/lib/matrix.h"
27 #include "tensorflow/compiler/xla/client/lib/slicing.h"
28 #include "tensorflow/compiler/xla/client/xla_builder.h"
29 #include "tensorflow/compiler/xla/literal_util.h"
30 #include "tensorflow/compiler/xla/primitive_util.h"
31 #include "tensorflow/compiler/xla/shape_util.h"
32 #include "tensorflow/compiler/xla/status_macros.h"
33 #include "tensorflow/compiler/xla/statusor.h"
34 #include "tensorflow/compiler/xla/util.h"
35 #include "tensorflow/core/lib/core/errors.h"
36
37 namespace xla {
38
SelfAdjointEig(XlaOp a,bool lower,int64_t max_iter,float tol,bool sort_eigenvalues)39 SelfAdjointEigResult SelfAdjointEig(XlaOp a, bool lower, int64_t max_iter,
40 float tol, bool sort_eigenvalues) {
41 XlaBuilder* builder = a.builder();
42 XlaOp result = builder->ReportErrorOrReturn([&]() -> StatusOr<XlaOp> {
43 TF_ASSIGN_OR_RETURN(Shape a_shape, builder->GetShape(a));
44 const int64_t num_dims = a_shape.rank();
45 if (num_dims < 2) {
46 return InvalidArgument(
47 "Arguments to Eigen decomposition must have rank >= 2: got shape %s.",
48 a_shape.ToString());
49 }
50 PrimitiveType type = a_shape.element_type();
51 if (!primitive_util::IsFloatingPointType(type) &&
52 !primitive_util::IsComplexType(type)) {
53 return InvalidArgument(
54 "Type of the input matrix must be floating point "
55 "or complex: got %s.",
56 a_shape.ToString());
57 }
58
59 const int64_t m = ShapeUtil::GetDimension(a_shape, -2);
60 const int64_t n = ShapeUtil::GetDimension(a_shape, -1);
61
62 if (m != n) {
63 return InvalidArgument(
64 "Arguments to symmetric eigendecomposition must be square matrices: "
65 "got shape (%d, %d).",
66 m, n);
67 }
68
69 const int num_batch_dims = a_shape.dimensions().size() - 2;
70 const std::vector<int64_t> batch_dims(
71 a_shape.dimensions().begin(),
72 a_shape.dimensions().begin() + num_batch_dims);
73
74 PrimitiveType eigvals_type =
75 primitive_util::IsComplexType(type)
76 ? primitive_util::ComplexComponentType(type)
77 : type;
78 std::vector<int64_t> eigvals_dims = batch_dims;
79 eigvals_dims.push_back(m);
80 Shape eigh_shape = ShapeUtil::MakeTupleShape(
81 {a_shape, ShapeUtil::MakeShape(eigvals_type, eigvals_dims)});
82 // TODO(phawkins): upgrade Eigh decomposition to a first-class HLO operator.
83 std::string opaque =
84 absl::StrFormat("%d,%d,%d,%f", lower, sort_eigenvalues, max_iter, tol);
85 return CustomCall(a.builder(), "Eigh", {a}, eigh_shape, opaque);
86 });
87 return SelfAdjointEigResult{GetTupleElement(result, 0),
88 GetTupleElement(result, 1)};
89 }
90
91 } // namespace xla
92