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/logdet.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/qr.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/shape_util.h"
31 #include "tensorflow/compiler/xla/status_macros.h"
32 #include "tensorflow/compiler/xla/statusor.h"
33 #include "tensorflow/core/lib/core/errors.h"
34
35 namespace xla {
36
SLogDet(XlaOp a)37 SignAndLogDet SLogDet(XlaOp a) {
38 StatusOr<SignAndLogDet> result = [&]() -> StatusOr<SignAndLogDet> {
39 TF_ASSIGN_OR_RETURN(Shape a_shape, a.builder()->GetShape(a));
40 auto qr = Qr(a);
41
42 int64_t m = ShapeUtil::GetDimension(a_shape, -2);
43 int64_t n = ShapeUtil::GetDimension(a_shape, -1);
44 if (m != n) {
45 return InvalidArgument(
46 "Arguments to logdet must be (batched) square matrices, got: %s",
47 a_shape.ToString());
48 }
49 // Get the sign and logarithm of the determinant based on the values along
50 // the diagonal of R and the number of zeros in taus.
51 auto log_abs_det = Einsum(Log(Abs(qr.q_and_r)), "...aa->...");
52 auto sign_diag = Reduce(
53 Sign(Einsum(qr.q_and_r, "...aa->...a")),
54 One(a.builder(), a_shape.element_type()),
55 CreateScalarMultiplyComputation(a_shape.element_type(), a.builder()),
56 {a_shape.rank() - 2});
57 auto sliced_taus = SliceInMinorDims(qr.taus, {0}, {n - 1});
58 auto sign_taus = Reduce(
59 Select(Ne(sliced_taus, ZerosLike(sliced_taus)),
60 FullLike(sliced_taus, -1), FullLike(sliced_taus, 1)),
61 One(a.builder(), a_shape.element_type()),
62 CreateScalarMultiplyComputation(a_shape.element_type(), a.builder()),
63 {a_shape.rank() - 2});
64 return SignAndLogDet{sign_diag * sign_taus, log_abs_det};
65 }();
66 if (!result.ok()) {
67 XlaOp error = a.builder()->ReportError(result.status());
68 return SignAndLogDet{error, error};
69 }
70 return result.ValueOrDie();
71 }
72
LogDet(XlaOp a)73 XlaOp LogDet(XlaOp a) {
74 SignAndLogDet slogdet = SLogDet(a);
75 return Select(
76 Ge(slogdet.sign, ZerosLike(slogdet.sign)), slogdet.logdet,
77 FullLike(slogdet.logdet, std::numeric_limits<float>::quiet_NaN()));
78 }
79
80 } // namespace xla
81