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 <utility>
     18 
     19 #include "tensorflow/compiler/xla/client/computation.h"
     20 #include "tensorflow/compiler/xla/client/computation_builder.h"
     21 #include "tensorflow/compiler/xla/literal_util.h"
     22 #include "tensorflow/compiler/xla/shape_util.h"
     23 #include "tensorflow/compiler/xla/test_helpers.h"
     24 #include "tensorflow/compiler/xla/tests/client_library_test_base.h"
     25 #include "tensorflow/compiler/xla/tests/literal_test_util.h"
     26 #include "tensorflow/compiler/xla/tests/test_macros.h"
     27 #include "tensorflow/compiler/xla/xla_data.pb.h"
     28 #include "tensorflow/core/platform/test.h"
     29 
     30 namespace xla {
     31 namespace {
     32 
     33 class CallOpTest : public ClientLibraryTestBase {
     34  protected:
     35   Computation CreateR0F32IdentityComputation() {
     36     ComputationBuilder builder(client_, "Identity");
     37     builder.Parameter(0, r0f32_, "x");
     38     auto build_status = builder.Build();
     39     EXPECT_IS_OK(build_status.status());
     40     return build_status.ConsumeValueOrDie();
     41   }
     42 
     43   Computation CreateR1S0F32AdditionComputation() {
     44     ComputationBuilder builder(client_, "Addition");
     45     auto x = builder.Parameter(0, r1s0f32_, "x");
     46     auto y = builder.Parameter(1, r1s0f32_, "y");
     47     builder.Add(x, y);
     48     auto build_status = builder.Build();
     49     EXPECT_IS_OK(build_status.status());
     50     return build_status.ConsumeValueOrDie();
     51   }
     52 
     53   Computation CreateR1S2F32AdditionComputation() {
     54     ComputationBuilder builder(client_, "Addition");
     55     auto x = builder.Parameter(0, r1s2f32_, "x");
     56     auto y = builder.Parameter(1, r1s2f32_, "y");
     57     builder.Add(x, y);
     58     auto build_status = builder.Build();
     59     EXPECT_IS_OK(build_status.status());
     60     return build_status.ConsumeValueOrDie();
     61   }
     62 
     63   Computation CreateR0F32TupleComputation() {
     64     ComputationBuilder builder(client_, "Tuple");
     65     builder.Tuple({builder.Parameter(0, r0f32_, "x")});
     66     auto build_status = builder.Build();
     67     EXPECT_IS_OK(build_status.status());
     68     return build_status.ConsumeValueOrDie();
     69   }
     70 
     71   Shape r0f32_ = ShapeUtil::MakeShape(F32, {});
     72   Shape r1s0f32_ = ShapeUtil::MakeShape(F32, {0});
     73   Shape r1s2f32_ = ShapeUtil::MakeShape(F32, {2});
     74 };
     75 
     76 XLA_TEST_F(CallOpTest, CallR0F32IdentityScalar) {
     77   ComputationBuilder builder(client_, TestName());
     78   Computation callee = CreateR0F32IdentityComputation();
     79   auto constant = builder.ConstantLiteral(*Literal::CreateR0<float>(42.0));
     80   builder.Call(callee, {constant});
     81 
     82   ComputeAndCompareR0<float>(&builder, 42.0, {}, ErrorSpec(0.01f));
     83 }
     84 
     85 XLA_TEST_F(CallOpTest, CallR1S0F32AddArray) {
     86   ComputationBuilder builder(client_, TestName());
     87   Computation callee = CreateR1S0F32AdditionComputation();
     88   auto x = builder.ConstantLiteral(*Literal::CreateR1<float>({}));
     89   auto y = builder.ConstantLiteral(*Literal::CreateR1<float>({}));
     90   builder.Call(callee, {x, y});
     91 
     92   ComputeAndCompareR1<float>(&builder, {}, {}, ErrorSpec(0.01f));
     93 }
     94 
     95 XLA_TEST_F(CallOpTest, CallR1S2F32AddArray) {
     96   ComputationBuilder builder(client_, TestName());
     97   Computation callee = CreateR1S2F32AdditionComputation();
     98   auto x = builder.ConstantLiteral(*Literal::CreateR1<float>({1.0f, 2.0f}));
     99   auto y = builder.ConstantLiteral(*Literal::CreateR1<float>({2.0f, 3.0f}));
    100   builder.Call(callee, {x, y});
    101 
    102   ComputeAndCompareR1<float>(&builder, {3.0f, 5.0f}, {}, ErrorSpec(0.01f));
    103 }
    104 
    105 XLA_TEST_F(CallOpTest, CallTreeTwoDeepBranchFactorThree) {
    106   ComputationBuilder builder(client_, "inner");
    107   {
    108     auto x = builder.Parameter(0, r0f32_, "x");
    109     builder.Add(x, builder.ConstantR0<float>(1.0));
    110   }
    111   TF_ASSERT_OK_AND_ASSIGN(Computation inner, builder.Build());
    112 
    113   ComputationBuilder builder2(client_, "outer");
    114   {
    115     auto x = builder2.Parameter(0, r0f32_, "x");
    116     x = builder2.Call(inner, {x});
    117     x = builder2.Call(inner, {x});
    118     x = builder2.Call(inner, {x});
    119   }
    120   TF_ASSERT_OK_AND_ASSIGN(Computation outer, builder2.Build());
    121 
    122   ComputationBuilder builder3(client_, "outermost");
    123   {
    124     auto x = builder3.Parameter(0, r0f32_, "x");
    125     x = builder3.Call(outer, {x});
    126     x = builder3.Call(outer, {x});
    127     x = builder3.Call(outer, {x});
    128   }
    129 
    130   TF_ASSERT_OK_AND_ASSIGN(
    131       std::unique_ptr<GlobalData> start,
    132       client_->TransferToServer(*Literal::CreateR0<float>(1.0f)));
    133   ComputeAndCompareR0<float>(&builder3, 10.0f, {start.get()}, ErrorSpec(0.0f));
    134 }
    135 
    136 XLA_TEST_F(CallOpTest, CallR0F32Tuple) {
    137   ComputationBuilder builder(client_, TestName());
    138   Computation callee = CreateR0F32TupleComputation();
    139   auto elem = Literal::CreateR0<float>(42.0);
    140   auto tuple = Literal::MakeTuple({elem.get()});
    141   builder.Call(callee, {builder.ConstantLiteral(*elem)});
    142 
    143   ComputeAndCompareTuple(&builder, *tuple, {}, ErrorSpec(0.01f));
    144 }
    145 
    146 }  // namespace
    147 }  // namespace xla
    148