1 /* Copyright 2018 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/qr.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/constants.h"
23 #include "tensorflow/compiler/xla/client/lib/loops.h"
24 #include "tensorflow/compiler/xla/client/lib/math.h"
25 #include "tensorflow/compiler/xla/client/lib/matrix.h"
26 #include "tensorflow/compiler/xla/client/lib/slicing.h"
27 #include "tensorflow/compiler/xla/client/xla_builder.h"
28 #include "tensorflow/compiler/xla/literal_util.h"
29 #include "tensorflow/compiler/xla/shape_util.h"
30 #include "tensorflow/compiler/xla/status_macros.h"
31 #include "tensorflow/compiler/xla/statusor.h"
32 #include "tensorflow/core/lib/core/errors.h"
33
34 namespace xla {
35
Qr(XlaOp a)36 QrDecomposition Qr(XlaOp a) {
37 auto result = [&]() -> StatusOr<QrDecomposition> {
38 XlaBuilder* builder = a.builder();
39 TF_ASSIGN_OR_RETURN(Shape a_shape, builder->GetShape(a));
40 const int num_dims = a_shape.rank();
41 if (num_dims < 2) {
42 return InvalidArgument(
43 "Arguments to QR must have rank >= 2: got shape %s",
44 a_shape.ToString());
45 }
46 const int64_t m = ShapeUtil::GetDimension(a_shape, -2);
47 const int64_t n = ShapeUtil::GetDimension(a_shape, -1);
48
49 std::vector<int64_t> taus_dims(a_shape.dimensions().begin(),
50 a_shape.dimensions().end());
51 taus_dims.pop_back();
52 taus_dims.back() = std::min(m, n);
53 auto taus_shape = ShapeUtil::MakeShape(a_shape.element_type(), taus_dims);
54
55 Shape qr_shape = ShapeUtil::MakeTupleShape({a_shape, taus_shape});
56 auto qr = CustomCall(a.builder(), "Qr", {a}, qr_shape);
57 a = GetTupleElement(qr, 0);
58 auto taus = GetTupleElement(qr, 1);
59
60 return QrDecomposition{a, taus};
61 }();
62 if (!result.ok()) {
63 XlaOp error = a.builder()->ReportError(result.status());
64 return QrDecomposition{error, error};
65 }
66 return result.ValueOrDie();
67 }
68
ProductOfElementaryHouseholderReflectors(XlaOp a,XlaOp taus)69 XlaOp ProductOfElementaryHouseholderReflectors(XlaOp a, XlaOp taus) {
70 XlaBuilder* builder = a.builder();
71 return builder->ReportErrorOrReturn([&]() -> StatusOr<XlaOp> {
72 TF_ASSIGN_OR_RETURN(Shape a_shape, builder->GetShape(a));
73 TF_ASSIGN_OR_RETURN(Shape taus_shape, builder->GetShape(taus));
74 if (a_shape.rank() < 2) {
75 return InvalidArgument(
76 "Matrix `a` must have >= 2 dimensions: got shape %s",
77 a_shape.ToString());
78 }
79 if (taus_shape.rank() + 1 != a_shape.rank()) {
80 return InvalidArgument(
81 "Matrix `taus` must have one fewer dimension than `a`: got shapes "
82 "%s and %s",
83 taus_shape.ToString(), a_shape.ToString());
84 }
85 const int64_t m = ShapeUtil::GetDimension(a_shape, -2);
86 const int64_t n = ShapeUtil::GetDimension(a_shape, -1);
87 if (m < n) {
88 return InvalidArgument(
89 "Argument to product of elementary Householder "
90 "reflectors must have m >= n, got shape %s",
91 a_shape.ToString());
92 }
93 absl::Span<const int64_t> a_batch_dims =
94 absl::MakeConstSpan(a_shape.dimensions().begin(),
95 a_shape.dimensions().begin() + a_shape.rank() - 2);
96 absl::Span<const int64_t> taus_batch_dims = absl::MakeConstSpan(
97 taus_shape.dimensions().begin(),
98 taus_shape.dimensions().begin() + taus_shape.rank() - 1);
99 const int64_t k = ShapeUtil::GetDimension(taus_shape, -1);
100 if (a_shape.element_type() != taus_shape.element_type() ||
101 a_batch_dims != taus_batch_dims || k > n) {
102 return InvalidArgument("Invalid shape for `taus`, got a=%s and taus=%s",
103 taus_shape.ToString(), a_shape.ToString());
104 }
105 return CustomCall(a.builder(), "ProductOfElementaryHouseholderReflectors",
106 {a, taus}, a_shape);
107 });
108 }
109
QrExplicit(XlaOp a,bool full_matrices,XlaOp & q,XlaOp & r)110 void QrExplicit(XlaOp a, bool full_matrices, XlaOp& q, XlaOp& r) {
111 StatusOr<Shape> a_shape_or = a.builder()->GetShape(a);
112 if (!a_shape_or.ok()) {
113 q = a.builder()->ReportError(a_shape_or.status());
114 r = q;
115 return;
116 }
117 Shape a_shape = a_shape_or.ValueOrDie();
118 const int64_t m = ShapeUtil::GetDimension(a_shape, -2);
119 const int64_t n = ShapeUtil::GetDimension(a_shape, -1);
120 const int64_t p = std::min(m, n);
121
122 auto qr = Qr(a);
123 if (full_matrices) {
124 XlaOp t;
125 if (m < n) {
126 t = SliceInMinorDims(qr.q_and_r, {0, 0}, {m, m});
127 } else {
128 t = PadInDim(qr.q_and_r, Zero(a.builder(), a_shape.element_type()),
129 a_shape.dimensions_size() - 1, /*pad_lo=*/0,
130 /*pad_hi=*/m - n);
131 }
132 q = ProductOfElementaryHouseholderReflectors(t, qr.taus);
133 r = UpperTriangle(qr.q_and_r);
134 } else {
135 XlaOp t;
136 if (m < n) {
137 t = SliceInMinorDims(qr.q_and_r, {0, 0}, {m, m});
138 } else {
139 t = qr.q_and_r;
140 }
141 q = ProductOfElementaryHouseholderReflectors(t, qr.taus);
142 q = SliceInMinorDims(q, {0, 0}, {m, p});
143 r = UpperTriangle(SliceInMinorDims(qr.q_and_r, {0, 0}, {p, n}));
144 }
145 }
146
147 } // namespace xla
148