• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/tridiagonal.h"
17 
18 #include "tensorflow/compiler/xla/array2d.h"
19 #include "tensorflow/compiler/xla/client/lib/constants.h"
20 #include "tensorflow/compiler/xla/client/lib/slicing.h"
21 #include "tensorflow/compiler/xla/client/xla_builder.h"
22 #include "tensorflow/compiler/xla/error_spec.h"
23 #include "tensorflow/compiler/xla/literal.h"
24 #include "tensorflow/compiler/xla/shape_util.h"
25 #include "tensorflow/compiler/xla/test.h"
26 #include "tensorflow/compiler/xla/tests/client_library_test_base.h"
27 #include "tensorflow/compiler/xla/tests/literal_test_util.h"
28 #include "tensorflow/compiler/xla/tests/test_macros.h"
29 
30 namespace xla {
31 namespace tridiagonal {
32 namespace {
33 
34 class TridiagonalTest
35     : public ClientLibraryTestBase,
36       public ::testing::WithParamInterface<std::tuple<int, int, int>> {};
37 
XLA_TEST_P(TridiagonalTest,Solves)38 XLA_TEST_P(TridiagonalTest, Solves) {
39   const auto& spec = GetParam();
40   xla::XlaBuilder builder(TestName());
41 
42   // TODO(belletti): parametrize num_rhs.
43   const int64 batch_size = std::get<0>(spec);
44   const int64 num_eqs = std::get<1>(spec);
45   const int64 num_rhs = std::get<2>(spec);
46 
47   Array3D<float> lower_diagonal(batch_size, 1, num_eqs);
48   Array3D<float> main_diagonal(batch_size, 1, num_eqs);
49   Array3D<float> upper_diagonal(batch_size, 1, num_eqs);
50   Array3D<float> rhs(batch_size, num_rhs, num_eqs);
51 
52   lower_diagonal.FillRandom(1.0, /*mean=*/0.0, /*seed=*/0);
53   main_diagonal.FillRandom(0.05, /*mean=*/1.0,
54                            /*seed=*/batch_size * num_eqs);
55   upper_diagonal.FillRandom(1.0, /*mean=*/0.0,
56                             /*seed=*/2 * batch_size * num_eqs);
57   rhs.FillRandom(1.0, /*mean=*/0.0, /*seed=*/3 * batch_size * num_eqs);
58 
59   XlaOp lower_diagonal_xla;
60   XlaOp main_diagonal_xla;
61   XlaOp upper_diagonal_xla;
62   XlaOp rhs_xla;
63 
64   auto lower_diagonal_data = CreateR3Parameter<float>(
65       lower_diagonal, 0, "lower_diagonal", &builder, &lower_diagonal_xla);
66   auto main_diagonal_data = CreateR3Parameter<float>(
67       main_diagonal, 1, "main_diagonal", &builder, &main_diagonal_xla);
68   auto upper_diagonal_data = CreateR3Parameter<float>(
69       upper_diagonal, 2, "upper_diagonal", &builder, &upper_diagonal_xla);
70   auto rhs_data = CreateR3Parameter<float>(rhs, 3, "rhs", &builder, &rhs_xla);
71 
72   TF_ASSERT_OK_AND_ASSIGN(XlaOp x,
73                           ThomasSolver(lower_diagonal_xla, main_diagonal_xla,
74                                        upper_diagonal_xla, rhs_xla));
75 
76   auto Coefficient = [](auto operand, auto i) {
77     return SliceInMinorDims(operand, /*start=*/{i}, /*end=*/{i + 1});
78   };
79 
80   std::vector<XlaOp> relative_errors(num_eqs);
81 
82   for (int64 i = 0; i < num_eqs; i++) {
83     auto a_i = Coefficient(lower_diagonal_xla, i);
84     auto b_i = Coefficient(main_diagonal_xla, i);
85     auto c_i = Coefficient(upper_diagonal_xla, i);
86     auto d_i = Coefficient(rhs_xla, i);
87 
88     if (i == 0) {
89       relative_errors[i] =
90           (b_i * Coefficient(x, i) + c_i * Coefficient(x, i + 1) - d_i) / d_i;
91     } else if (i == num_eqs - 1) {
92       relative_errors[i] =
93           (a_i * Coefficient(x, i - 1) + b_i * Coefficient(x, i) - d_i) / d_i;
94     } else {
95       relative_errors[i] =
96           (a_i * Coefficient(x, i - 1) + b_i * Coefficient(x, i) +
97            c_i * Coefficient(x, i + 1) - d_i) /
98           d_i;
99     }
100   }
101   Abs(ConcatInDim(&builder, relative_errors, 2));
102 
103   TF_ASSERT_OK_AND_ASSIGN(
104       auto result,
105       ComputeAndTransfer(&builder,
106                          {lower_diagonal_data.get(), main_diagonal_data.get(),
107                           upper_diagonal_data.get(), rhs_data.get()}));
108 
109   auto result_data = result.data<float>({});
110   for (auto result_component : result_data) {
111     EXPECT_TRUE(result_component < 5e-3);
112   }
113 }
114 
115 INSTANTIATE_TEST_CASE_P(TridiagonalTestInstantiation, TridiagonalTest,
116                         ::testing::Combine(::testing::Values(1, 12),
117                                            ::testing::Values(4, 8),
118                                            ::testing::Values(1, 12)));
119 
120 }  // namespace
121 }  // namespace tridiagonal
122 }  // namespace xla
123