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 "tensorflow/compiler/xla/array2d.h"
19 #include "tensorflow/compiler/xla/array3d.h"
20 #include "tensorflow/compiler/xla/client/lib/matrix.h"
21 #include "tensorflow/compiler/xla/client/xla_builder.h"
22 #include "tensorflow/compiler/xla/literal.h"
23 #include "tensorflow/compiler/xla/statusor.h"
24 #include "tensorflow/compiler/xla/test.h"
25 #include "tensorflow/compiler/xla/tests/client_library_test_base.h"
26 #include "tensorflow/compiler/xla/tests/literal_test_util.h"
27 #include "tensorflow/compiler/xla/tests/test_macros.h"
28 #include "tensorflow/compiler/xla/xla_data.pb.h"
29 #include "tensorflow/core/lib/core/status_test_util.h"
30
31 namespace {
32
33 using QrTest = xla::ClientLibraryTestBase;
34
XLA_TEST_F(QrTest,Simple)35 XLA_TEST_F(QrTest, Simple) {
36 xla::XlaBuilder builder(TestName());
37
38 xla::Array2D<float> a_vals({
39 {4, 6, 8, 10},
40 {6, 45, 54, 63},
41 {8, 54, 146, 166},
42 {10, 63, 166, 310},
43 });
44
45 xla::XlaOp a;
46 auto a_data = CreateR2Parameter<float>(a_vals, 0, "a", &builder, &a);
47 TF_ASSERT_OK_AND_ASSIGN(
48 auto result,
49 xla::QRDecomposition(a, /*full_matrices=*/true, /*block_size=*/2));
50
51 // Verifies that the decomposition composes back to the original matrix.
52 //
53 // This isn't a terribly demanding test, (e.g., we should verify that Q is
54 // orthonormal and R is upper-triangular) but it's awkward to write such tests
55 // without more linear algebra libraries. It's easier to test the numerics
56 // from Python, anyway, where we have access to numpy and scipy.
57 xla::BatchDot(result.q, result.r, xla::PrecisionConfig::HIGHEST);
58
59 ComputeAndCompareR2<float>(&builder, a_vals, {a_data.get()},
60 xla::ErrorSpec(1e-4, 1e-4));
61 }
62
XLA_TEST_F(QrTest,SimpleBatched)63 XLA_TEST_F(QrTest, SimpleBatched) {
64 xla::XlaBuilder builder(TestName());
65
66 xla::Array3D<float> a_vals({
67 {
68 {4, 6, 8, 10},
69 {6, 45, 54, 63},
70 {8, 54, 146, 166},
71 {10, 63, 166, 310},
72 },
73 {
74 {16, 24, 8, 12},
75 {24, 61, 82, 48},
76 {8, 82, 456, 106},
77 {12, 48, 106, 62},
78 },
79 });
80
81 xla::XlaOp a;
82 auto a_data = CreateR3Parameter<float>(a_vals, 0, "a", &builder, &a);
83 TF_ASSERT_OK_AND_ASSIGN(
84 auto result,
85 xla::QRDecomposition(a, /*full_matrices=*/true, /*block_size=*/2));
86
87 xla::BatchDot(result.q, result.r, xla::PrecisionConfig::HIGHEST);
88
89 ComputeAndCompareR3<float>(&builder, a_vals, {a_data.get()},
90 xla::ErrorSpec(1e-4, 1e-4));
91 }
92
93 } // namespace
94