Home | History | Annotate | Download | only in internal
      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 #include "tensorflow/contrib/lite/kernels/internal/tensor_utils.h"
     16 #include <gmock/gmock.h>
     17 #include "tensorflow/contrib/lite/builtin_op_data.h"
     18 #include "tensorflow/contrib/lite/kernels/test_util.h"
     19 
     20 namespace tflite {
     21 namespace tensor_utils {
     22 
     23 TEST(uKernels, ClipTest) {
     24   constexpr int kVectorSize = 10;
     25   constexpr float kAbsLimit = 2.0;
     26   static float input[kVectorSize] = {0.0,  -0.5, 1.0,  -1.5, 2.0,
     27                                      -2.5, 3.0,  -3.5, 4.0,  -4.5};
     28   std::vector<float> output(kVectorSize);
     29   ClipVector(input, kVectorSize, kAbsLimit, output.data());
     30   EXPECT_THAT(output,
     31               ElementsAreArray(ArrayFloatNear(
     32                   {0.0, -0.5, 1.0, -1.5, 2.0, -2.0, 2.0, -2.0, 2.0, -2.0})));
     33 }
     34 
     35 TEST(uKernels, MatrixBatchVectorMultiplyAccumulateTest) {
     36   constexpr int kRow = 3;
     37   constexpr int kCol = 4;
     38   constexpr int kBatch = 2;
     39   static float matrix[kRow * kCol] = {1.0,  2.0,  3.0,  4.0,   //
     40                                       -1.0, -2.0, -3.0, -4.0,  //
     41                                       1.0,  -2.0, 3.0,  -4.0};
     42   static float vector[kCol * kBatch] = {1.0, -1.0, 1.0, -1.0,  //
     43                                         2.0, -2.0, 2.0, -2.0};
     44   std::vector<float> output(kRow * kBatch);
     45   std::fill(output.begin(), output.end(), 3.0);
     46   MatrixBatchVectorMultiplyAccumulate(matrix, kRow, kCol, vector, kBatch,
     47                                       output.data(), /*result_stride=*/1);
     48   EXPECT_THAT(output, ElementsAreArray(ArrayFloatNear({1., 5., 13.,  //
     49                                                        -1., 7., 23.})));
     50 
     51   std::vector<float> output_with_stride2(kRow * kBatch * 2);
     52   std::fill(output_with_stride2.begin(), output_with_stride2.end(), 3.0);
     53   MatrixBatchVectorMultiplyAccumulate(matrix, kRow, kCol, vector, kBatch,
     54                                       output_with_stride2.data(),
     55                                       /*result_stride=*/2);
     56   EXPECT_THAT(output_with_stride2,
     57               ElementsAreArray(ArrayFloatNear({1., 3., 5., 3., 13., 3.,  //
     58                                                -1., 3., 7., 3., 23., 3.})));
     59 }
     60 
     61 TEST(uKernels, VectorVectorCwiseProductTest) {
     62   constexpr int kVectorSize = 10;
     63   static float input1[kVectorSize] = {0.0,  -0.5, 1.0,  -1.5, 2.0,
     64                                       -2.5, 3.0,  -3.5, 4.0,  -4.5};
     65   static float input2[kVectorSize] = {0.1,  -0.1, 0.1,  -0.1, 0.1,
     66                                       -0.1, 0.1,  -0.1, 0.1,  -0.1};
     67   std::vector<float> output(kVectorSize);
     68   VectorVectorCwiseProduct(input1, input2, kVectorSize, output.data());
     69   EXPECT_THAT(output,
     70               ElementsAreArray(ArrayFloatNear(
     71                   {0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45})));
     72 }
     73 
     74 TEST(uKernels, VectorVectorCwiseProductAccumulateTest) {
     75   constexpr int kVectorSize = 10;
     76   static float input1[kVectorSize] = {0.0,  -0.5, 1.0,  -1.5, 2.0,
     77                                       -2.5, 3.0,  -3.5, 4.0,  -4.5};
     78   static float input2[kVectorSize] = {0.1,  -0.1, 0.1,  -0.1, 0.1,
     79                                       -0.1, 0.1,  -0.1, 0.1,  -0.1};
     80   std::vector<float> output(kVectorSize);
     81   std::fill(output.begin(), output.end(), 1.0);
     82   VectorVectorCwiseProductAccumulate(input1, input2, kVectorSize,
     83                                      output.data());
     84   EXPECT_THAT(output,
     85               ElementsAreArray(ArrayFloatNear(
     86                   {1.0, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45})));
     87 }
     88 
     89 TEST(uKernels, VectorBatchVectorAssignTest) {
     90   constexpr int kVectorSize = 5;
     91   constexpr int kBatchSize = 3;
     92   static float input[kVectorSize] = {0.0, -0.5, 1.0, -1.5, 2.0};
     93   std::vector<float> output(kVectorSize * kBatchSize);
     94   VectorBatchVectorAssign(input, kVectorSize, kBatchSize, output.data());
     95   EXPECT_THAT(output, ElementsAreArray(ArrayFloatNear(
     96                           {0.0, -0.5, 1.0, -1.5, 2.0, 0.0, -0.5, 1.0, -1.5, 2.0,
     97                            0.0, -0.5, 1.0, -1.5, 2.0})));
     98 }
     99 
    100 TEST(uKernels, ApplySigmoidToVectorTest) {
    101   constexpr int kVectorSize = 5;
    102   static float input[kVectorSize] = {0.0, -0.5, 1.0, -1.5, 2.0};
    103   std::vector<float> output(kVectorSize);
    104   ApplySigmoidToVector(input, kVectorSize, output.data());
    105   EXPECT_THAT(output, ElementsAreArray(ArrayFloatNear(
    106                           {0.5, 0.377541, 0.731059, 0.182426, 0.880797})));
    107 }
    108 
    109 TEST(uKernels, ApplyActivationToVectorTest) {
    110   constexpr int kVectorSize = 5;
    111   static float input[kVectorSize] = {0.0, -0.5, 1.0, -1.5, 2.0};
    112   std::vector<float> output(kVectorSize);
    113   ApplyActivationToVector(input, kVectorSize, kTfLiteActRelu, output.data());
    114   EXPECT_THAT(output,
    115               ElementsAreArray(ArrayFloatNear({0.0, 0.0, 1.0, 0.0, 2.0})));
    116 
    117   ApplyActivationToVector(input, kVectorSize, kTfLiteActTanh, output.data());
    118   EXPECT_THAT(output, ElementsAreArray(ArrayFloatNear(
    119                           {0.0, -0.462117, 0.761594, -0.905148, 0.964028})));
    120 }
    121 
    122 TEST(uKernels, CopyVectorTest) {
    123   constexpr int kVectorSize = 5;
    124   static float input[kVectorSize] = {0.0, -0.5, 1.0, -1.5, 2.0};
    125   std::vector<float> output(kVectorSize);
    126   CopyVector(input, kVectorSize, output.data());
    127   EXPECT_THAT(output,
    128               ElementsAreArray(ArrayFloatNear({0.0, -0.5, 1.0, -1.5, 2.0})));
    129 }
    130 
    131 TEST(uKernels, Sub1VectorTest) {
    132   constexpr int kVectorSize = 5;
    133   static float input[kVectorSize] = {0.0, -0.5, 1.0, -1.5, 2.0};
    134   std::vector<float> output(kVectorSize);
    135   Sub1Vector(input, kVectorSize, output.data());
    136   EXPECT_THAT(output,
    137               ElementsAreArray(ArrayFloatNear({1.0, 1.5, 0.0, 2.5, -1.0})));
    138 }
    139 
    140 TEST(uKernels, ZeroVectorTest) {
    141   constexpr int kVectorSize = 5;
    142   std::vector<float> output(kVectorSize);
    143   ZeroVector(output.data(), kVectorSize);
    144   EXPECT_THAT(output,
    145               ElementsAreArray(ArrayFloatNear({0.0, 0.0, 0.0, 0.0, 0.0})));
    146 }
    147 
    148 TEST(uKernels, BatchVectorBatchVectorDotProductTest) {
    149   constexpr int kVectorSize = 5;
    150   constexpr int kBatch = 2;
    151   static float input1[kVectorSize * kBatch] = {0.0,  -0.5, 1.0,  -1.5, 2.0,
    152                                                -2.5, 3.0,  -3.5, 4.0,  -4.5};
    153   static float input2[kVectorSize * kBatch] = {0.1,  -0.1, 0.1,  -0.1, 0.1,
    154                                                -0.1, 0.1,  -0.1, 0.1,  -0.1};
    155   std::vector<float> output(kBatch);
    156   BatchVectorBatchVectorDotProduct(input1, input2, kVectorSize, kBatch,
    157                                    output.data(), /*result_stride=*/1);
    158   EXPECT_THAT(output, ElementsAreArray(ArrayFloatNear({0.5, 1.75})));
    159 }
    160 
    161 TEST(uKernels, VectorShiftLeftTest) {
    162   constexpr int kVectorSize = 5;
    163   static float input[kVectorSize] = {0.0, -0.5, 1.0, -1.5, 2.0};
    164   std::vector<float> result(kVectorSize);
    165   VectorShiftLeft(input, kVectorSize, 3.0);
    166   result.assign(input, input + kVectorSize);
    167   EXPECT_THAT(result,
    168               ElementsAreArray(ArrayFloatNear({-0.5, 1.0, -1.5, 2.0, 3.0})));
    169 }
    170 
    171 TEST(uKernels, ReductionSumVectorTest) {
    172   constexpr int kInputVectorSize = 10;
    173   constexpr int kOutputVectorSize1 = 5;
    174   constexpr int kReductionSize1 = 2;
    175   static float input[kInputVectorSize] = {0.0, -0.5, 1.0, -1.5, 2.0,
    176                                           0.0, -0.5, 1.0, 1.0,  2.0};
    177   std::vector<float> result1(kOutputVectorSize1);
    178   ReductionSumVector(input, result1.data(), kOutputVectorSize1,
    179                      kReductionSize1);
    180   EXPECT_THAT(result1,
    181               ElementsAreArray(ArrayFloatNear({-0.5, -0.5, 2.0, 0.5, 3.0})));
    182 
    183   constexpr int kOutputVectorSize2 = 2;
    184   constexpr int kReductionSize2 = 5;
    185   std::vector<float> result2(kOutputVectorSize2);
    186   ReductionSumVector(input, result2.data(), kOutputVectorSize2,
    187                      kReductionSize2);
    188   EXPECT_THAT(result2, ElementsAreArray(ArrayFloatNear({1.0, 3.5})));
    189 }
    190 
    191 }  // namespace tensor_utils
    192 }  // namespace tflite
    193