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 <vector>
     17 
     18 #include "tensorflow/compiler/xla/client/computation_builder.h"
     19 #include "tensorflow/compiler/xla/client/local_client.h"
     20 #include "tensorflow/compiler/xla/tests/client_library_test_base.h"
     21 #include "tensorflow/compiler/xla/tests/literal_test_util.h"
     22 #include "tensorflow/compiler/xla/tests/test_macros.h"
     23 #include "tensorflow/core/platform/test.h"
     24 
     25 namespace xla {
     26 namespace {
     27 
     28 class AxpySimpleTest : public ClientLibraryTestBase {};
     29 
     30 TEST_F(AxpySimpleTest, AxTenValues) {
     31   ComputationBuilder builder(client_, "ax_10");
     32   auto alpha = builder.ConstantR0<float>(3.1415926535);
     33   auto x = builder.ConstantR1<float>(
     34       {-1.0, 1.0, 2.0, -2.0, -3.0, 3.0, 4.0, -4.0, -5.0, 5.0});
     35   auto ax = builder.Mul(alpha, x);
     36 
     37   std::vector<float> expected = {
     38       -3.14159265, 3.14159265,  6.28318531,   -6.28318531,  -9.42477796,
     39       9.42477796,  12.56637061, -12.56637061, -15.70796327, 15.70796327};
     40   ComputeAndCompareR1<float>(&builder, expected, {}, ErrorSpec(0.0001));
     41 }
     42 
     43 XLA_TEST_F(AxpySimpleTest, AxpyZeroValues) {
     44   ComputationBuilder builder(client_, "axpy_10");
     45   auto alpha = builder.ConstantR0<float>(3.1415926535);
     46   auto x = builder.ConstantR1<float>({});
     47   auto y = builder.ConstantR1<float>({});
     48   auto ax = builder.Mul(alpha, x);
     49   auto axpy = builder.Add(ax, y);
     50 
     51   std::vector<float> expected = {};
     52   ComputeAndCompareR1<float>(&builder, expected, {}, ErrorSpec(0.0001));
     53 }
     54 
     55 TEST_F(AxpySimpleTest, AxpyTenValues) {
     56   ComputationBuilder builder(client_, "axpy_10");
     57   auto alpha = builder.ConstantR0<float>(3.1415926535);
     58   auto x = builder.ConstantR1<float>(
     59       {-1.0, 1.0, 2.0, -2.0, -3.0, 3.0, 4.0, -4.0, -5.0, 5.0});
     60   auto y = builder.ConstantR1<float>(
     61       {5.0, -5.0, -4.0, 4.0, 3.0, -3.0, -2.0, 2.0, 1.0, -1.0});
     62   auto ax = builder.Mul(alpha, x);
     63   auto axpy = builder.Add(ax, y);
     64 
     65   TF_ASSERT_OK_AND_ASSIGN(ProgramShape shape, builder.GetProgramShape());
     66 
     67   EXPECT_EQ("() -> f32[10]", ShapeUtil::HumanString(shape));
     68 
     69   std::vector<float> expected = {
     70       1.85840735, -1.85840735, 2.28318531,   -2.28318531,  -6.42477796,
     71       6.42477796, 10.56637061, -10.56637061, -14.70796327, 14.70796327};
     72   ComputeAndCompareR1<float>(&builder, expected, {}, ErrorSpec(0.0001));
     73 }
     74 
     75 }  // namespace
     76 }  // namespace xla
     77