Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2018 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 <gtest/gtest.h>
     16 #include "tensorflow/lite/interpreter.h"
     17 #include "tensorflow/lite/kernels/register.h"
     18 #include "tensorflow/lite/kernels/test_util.h"
     19 #include "tensorflow/lite/model.h"
     20 
     21 namespace tflite {
     22 namespace {
     23 
     24 using ::testing::ElementsAreArray;
     25 
     26 class BaseSquaredDifferenceOpModel : public SingleOpModel {
     27  public:
     28   BaseSquaredDifferenceOpModel(const TensorData& input1,
     29                                const TensorData& input2,
     30                                const TensorData& output) {
     31     input1_ = AddInput(input1);
     32     input2_ = AddInput(input2);
     33     output_ = AddOutput(output);
     34     SetBuiltinOp(BuiltinOperator_SQUARED_DIFFERENCE,
     35                  BuiltinOptions_SquaredDifferenceOptions,
     36                  CreateSquaredDifferenceOptions(builder_).Union());
     37     BuildInterpreter({GetShape(input1_), GetShape(input2_)});
     38   }
     39 
     40   int input1() { return input1_; }
     41   int input2() { return input2_; }
     42 
     43  protected:
     44   int input1_;
     45   int input2_;
     46   int output_;
     47 };
     48 
     49 class FloatSquaredDifferenceOpModel : public BaseSquaredDifferenceOpModel {
     50  public:
     51   using BaseSquaredDifferenceOpModel::BaseSquaredDifferenceOpModel;
     52 
     53   std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
     54 };
     55 
     56 class IntegerSquaredDifferenceOpModel : public BaseSquaredDifferenceOpModel {
     57  public:
     58   using BaseSquaredDifferenceOpModel::BaseSquaredDifferenceOpModel;
     59 
     60   std::vector<int32_t> GetOutput() { return ExtractVector<int32_t>(output_); }
     61 };
     62 
     63 TEST(FloatSquaredDifferenceOpTest, FloatType_SameShape) {
     64   FloatSquaredDifferenceOpModel m({TensorType_FLOAT32, {1, 2, 2, 1}},
     65                                   {TensorType_FLOAT32, {1, 2, 2, 1}},
     66                                   {TensorType_FLOAT32, {}});
     67   m.PopulateTensor<float>(m.input1(), {-0.2, 0.2, -1.2, 0.8});
     68   m.PopulateTensor<float>(m.input2(), {0.5, 0.2, -1.5, 0.5});
     69   m.Invoke();
     70   EXPECT_THAT(m.GetOutput(),
     71               ElementsAreArray(ArrayFloatNear({0.49, 0.0, 0.09, 0.09})));
     72 }
     73 
     74 TEST(FloatSquaredDifferenceOpTest, FloatType_VariousInputShapes) {
     75   std::vector<std::vector<int>> test_shapes = {
     76       {6}, {2, 3}, {2, 1, 3}, {1, 3, 1, 2}};
     77   for (int i = 0; i < test_shapes.size(); ++i) {
     78     FloatSquaredDifferenceOpModel m({TensorType_FLOAT32, test_shapes[i]},
     79                                     {TensorType_FLOAT32, test_shapes[i]},
     80                                     {TensorType_FLOAT32, {}});
     81     m.PopulateTensor<float>(m.input1(), {-2.0, 0.2, 0.3, 0.8, 1.1, -2.0});
     82     m.PopulateTensor<float>(m.input2(), {1.0, 0.2, 0.6, 0.4, -1.0, -0.0});
     83     m.Invoke();
     84     EXPECT_THAT(
     85         m.GetOutput(),
     86         ElementsAreArray(ArrayFloatNear({9.0, 0.0, 0.09, 0.16, 4.41, 4.0})))
     87         << "With shape number " << i;
     88   }
     89 }
     90 
     91 TEST(FloatSquaredDifferenceOpTest, FloatType_WithBroadcast) {
     92   std::vector<std::vector<int>> test_shapes = {
     93       {6}, {2, 3}, {2, 1, 3}, {1, 3, 1, 2}};
     94   for (int i = 0; i < test_shapes.size(); ++i) {
     95     FloatSquaredDifferenceOpModel m(
     96         {TensorType_FLOAT32, test_shapes[i]},
     97         {TensorType_FLOAT32, {}},  // always a scalar
     98         {TensorType_FLOAT32, {}});
     99     m.PopulateTensor<float>(m.input1(), {-0.2, 0.2, 0.5, 0.8, 0.11, 1.1});
    100     m.PopulateTensor<float>(m.input2(), {0.1});
    101     m.Invoke();
    102     EXPECT_THAT(
    103         m.GetOutput(),
    104         ElementsAreArray(ArrayFloatNear({0.09, 0.01, 0.16, 0.49, 0.0001, 1.0})))
    105         << "With shape number " << i;
    106   }
    107 }
    108 
    109 TEST(IntegerSquaredDifferenceOpTest, IntegerType_SameShape) {
    110   IntegerSquaredDifferenceOpModel m({TensorType_INT32, {1, 2, 2, 1}},
    111                                     {TensorType_INT32, {1, 2, 2, 1}},
    112                                     {TensorType_INT32, {}});
    113   m.PopulateTensor<int32_t>(m.input1(), {-2, 2, -15, 8});
    114   m.PopulateTensor<int32_t>(m.input2(), {5, -2, -3, 5});
    115   m.Invoke();
    116   EXPECT_THAT(m.GetOutput(), ElementsAreArray({49, 16, 144, 9}));
    117 }
    118 
    119 TEST(IntegerSquaredDifferenceOpTest, IntegerType_VariousInputShapes) {
    120   std::vector<std::vector<int>> test_shapes = {
    121       {6}, {2, 3}, {2, 1, 3}, {1, 3, 1, 2}};
    122   for (int i = 0; i < test_shapes.size(); ++i) {
    123     IntegerSquaredDifferenceOpModel m({TensorType_INT32, test_shapes[i]},
    124                                       {TensorType_INT32, test_shapes[i]},
    125                                       {TensorType_INT32, {}});
    126     m.PopulateTensor<int32_t>(m.input1(), {-20, 2, 3, 8, 11, -20});
    127     m.PopulateTensor<int32_t>(m.input2(), {1, 2, 6, 5, -5, -20});
    128     m.Invoke();
    129     EXPECT_THAT(m.GetOutput(), ElementsAreArray({441, 0, 9, 9, 256, 0}))
    130         << "With shape number " << i;
    131   }
    132 }
    133 
    134 TEST(IntegerSquaredDifferenceOpTest, IntegerType_WithBroadcast) {
    135   std::vector<std::vector<int>> test_shapes = {
    136       {6}, {2, 3}, {2, 1, 3}, {1, 3, 1, 2}};
    137   for (int i = 0; i < test_shapes.size(); ++i) {
    138     IntegerSquaredDifferenceOpModel m(
    139         {TensorType_INT32, test_shapes[i]},
    140         {TensorType_INT32, {}},  // always a scalar
    141         {TensorType_INT32, {}});
    142     m.PopulateTensor<int32_t>(m.input1(), {-20, 10, 7, 3, 1, 13});
    143     m.PopulateTensor<int32_t>(m.input2(), {3});
    144     m.Invoke();
    145     EXPECT_THAT(m.GetOutput(), ElementsAreArray({529, 49, 16, 0, 4, 100}))
    146         << "With shape number " << i;
    147   }
    148 }
    149 
    150 }  // namespace
    151 }  // namespace tflite
    152 
    153 int main(int argc, char** argv) {
    154   ::tflite::LogToStderr();
    155   ::testing::InitGoogleTest(&argc, argv);
    156   return RUN_ALL_TESTS();
    157 }
    158