Home | History | Annotate | Download | only in tests
      1 /* Copyright 2017 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 <memory>
     17 #include <vector>
     18 
     19 #include "tensorflow/compiler/xla/client/computation_builder.h"
     20 #include "tensorflow/compiler/xla/client/global_data.h"
     21 #include "tensorflow/compiler/xla/client/local_client.h"
     22 #include "tensorflow/compiler/xla/shape_util.h"
     23 #include "tensorflow/compiler/xla/status_macros.h"
     24 #include "tensorflow/compiler/xla/statusor.h"
     25 #include "tensorflow/compiler/xla/test_helpers.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 #include "tensorflow/compiler/xla/tests/test_utils.h"
     30 #include "tensorflow/compiler/xla/xla_data.pb.h"
     31 #include "tensorflow/core/platform/test.h"
     32 #include "tensorflow/core/platform/types.h"
     33 
     34 namespace xla {
     35 namespace {
     36 
     37 class ClientTest : public ClientLibraryTestBase {};
     38 
     39 XLA_TEST_F(ClientTest, ExecuteWithLayout) {
     40   ComputationBuilder b(client_, TestName());
     41 
     42   std::vector<std::vector<int64>> layouts = {{0, 1}, {1, 0}};
     43   for (const std::vector<int64>& execute_layout : layouts) {
     44     for (const std::vector<int64>& transfer_layout : layouts) {
     45       b.Add(b.ConstantR2<int32>({{1, 2}, {3, 4}}),
     46             b.ConstantR2<int32>({{10, 20}, {30, 40}}));
     47       TF_ASSERT_OK_AND_ASSIGN(auto computation, b.Build());
     48 
     49       ExecutionOptions execution_options = execution_options_;
     50       *execution_options.mutable_shape_with_output_layout() =
     51           ShapeUtil::MakeShapeWithLayout(S32, /*dimensions=*/{2, 2},
     52                                          execute_layout);
     53       TF_ASSERT_OK_AND_ASSIGN(
     54           std::unique_ptr<GlobalData> data,
     55           client_->Execute(computation, {}, &execution_options));
     56 
     57       std::unique_ptr<Literal> expected_literal =
     58           Literal::CreateR2WithLayout<int32>(
     59               {{11, 22}, {33, 44}}, LayoutUtil::MakeLayout(transfer_layout));
     60 
     61       TF_ASSERT_OK_AND_ASSIGN(
     62           auto computed, client_->Transfer(*data, &expected_literal->shape()));
     63 
     64       LiteralTestUtil::AssertEqualShapesAndLayouts(expected_literal->shape(),
     65                                                    computed->shape());
     66       LiteralTestUtil::ExpectEqual(*expected_literal, *computed);
     67     }
     68   }
     69 }
     70 
     71 XLA_TEST_F(ClientTest, ExecuteWithTupleLayout) {
     72   ComputationBuilder b(client_, TestName());
     73 
     74   b.Tuple({b.ConstantR2<int32>({{1, 2}, {3, 4}}),
     75            b.ConstantR2<int32>({{10, 20}, {30, 40}})});
     76 
     77   TF_ASSERT_OK_AND_ASSIGN(auto computation, b.Build());
     78 
     79   ExecutionOptions execution_options = execution_options_;
     80   // Create a result shape with one element column major and the other row
     81   // major.
     82   *execution_options.mutable_shape_with_output_layout() =
     83       ShapeUtil::MakeTupleShape(
     84           {ShapeUtil::MakeShapeWithLayout(S32, /*dimensions=*/{2, 2},
     85                                           /*minor_to_major=*/{0, 1}),
     86            ShapeUtil::MakeShapeWithLayout(S32, /*dimensions=*/{2, 2},
     87                                           /*minor_to_major=*/{1, 0})});
     88 
     89   TF_ASSERT_OK_AND_ASSIGN(
     90       auto result,
     91       client_->ExecuteAndTransfer(computation, {}, &execution_options));
     92   LiteralTestUtil::ExpectR2Equal<int32>({{1, 2}, {3, 4}},
     93                                         LiteralView::Create(*result, {0}));
     94   LiteralTestUtil::ExpectR2Equal<int32>({{10, 20}, {30, 40}},
     95                                         LiteralView::Create(*result, {1}));
     96 
     97   EXPECT_TRUE(ShapeUtil::IsTuple(result->shape()));
     98   EXPECT_EQ(2, ShapeUtil::TupleElementCount(result->shape()));
     99 
    100   EXPECT_TRUE(ShapeUtil::Equal(
    101       ShapeUtil::GetTupleElementShape(result->shape(), 0),
    102       ShapeUtil::MakeShapeWithLayout(S32, /*dimensions=*/{2, 2},
    103                                      /*minor_to_major=*/{0, 1})));
    104   EXPECT_TRUE(ShapeUtil::Equal(
    105       ShapeUtil::GetTupleElementShape(result->shape(), 1),
    106       ShapeUtil::MakeShapeWithLayout(S32, /*dimensions=*/{2, 2},
    107                                      /*minor_to_major=*/{1, 0})));
    108 }
    109 
    110 XLA_TEST_F(ClientTest,
    111         DISABLED_ON_CPU_PARALLEL(DISABLED_ON_GPU(ExecuteParallel))) {
    112   Computation add_with_one_arg, mul_with_two_args, dot_with_one_arg;
    113   Shape shape = ShapeUtil::MakeShape(S32, {2, 2});
    114 
    115   TF_ASSERT_OK_AND_ASSIGN(
    116       std::unique_ptr<GlobalData> const_arg,
    117       client_->TransferToServer(*Literal::CreateR2<int32>({{5, 6}, {7, 8}})));
    118 
    119   ComputationBuilder b(client_, TestName() + ".add");
    120   b.Add(b.Parameter(0, shape, "param_0"),
    121         b.ConstantR2<int32>({{1, 2}, {3, 4}}));
    122   TF_ASSERT_OK_AND_ASSIGN(add_with_one_arg, b.Build());
    123 
    124   // We can't really test parallel execution on CPU since all of the cores in a
    125   // CPU are presented as a single device.  So for now we test "parallel"
    126   // execution on a single device.
    127   std::vector<Client::ComputationInstance> computation_instances;
    128   TF_ASSERT_OK_AND_ASSIGN(std::vector<xla::DeviceHandle> devices,
    129                           client_->GetDeviceHandles(1));
    130   ASSERT_EQ(devices.size(), 1);
    131 
    132   ExecutionOptions options = execution_options_;
    133   *options.add_device_handles() = devices[0];
    134   computation_instances.push_back(Client::ComputationInstance(
    135       add_with_one_arg, {const_arg.get()}, options, nullptr));
    136 
    137   TF_ASSERT_OK_AND_ASSIGN(auto results,
    138                           client_->ExecuteParallel(computation_instances));
    139   auto expected_result = Literal::CreateR2<int32>({{6, 8}, {10, 12}});
    140 
    141   TF_ASSERT_OK_AND_ASSIGN(
    142       auto result_literal,
    143       client_->Transfer(*results[0], &expected_result->shape()));
    144 
    145   LiteralTestUtil::ExpectEqual(*expected_result, *result_literal);
    146 }
    147 
    148 }  // namespace
    149 }  // namespace xla
    150