/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #include #include #include #include #include #include "flatbuffers/flatbuffers.h" // from @flatbuffers #include "tensorflow/lite/core/c/c_api_types.h" #include "tensorflow/lite/kernels/test_util.h" #include "tensorflow/lite/schema/schema_generated.h" namespace tflite { namespace { using ::testing::ElementsAreArray; using ::testing::Test; class BaseWhereOpModel : public SingleOpModel { public: BaseWhereOpModel(const TensorData& input, const TensorData& output) { input_ = AddInput(input); output_ = AddOutput(output); SetBuiltinOp(BuiltinOperator_WHERE, BuiltinOptions_WhereOptions, CreateWhereOptions(builder_).Union()); BuildInterpreter({GetShape(input_)}); } int input() { return input_; } protected: int input_; int output_; }; class IntegerWhereOpModel : public BaseWhereOpModel { public: using BaseWhereOpModel::BaseWhereOpModel; std::vector GetOutput() { return ExtractVector(output_); } }; template class ConstInputWhereOpModel : public SingleOpModel { public: ConstInputWhereOpModel(T1 constant_values, const TensorData& output) { input_ = AddConstInput(GetTensorType(), {constant_values}, {}); output_ = AddOutput(output); SetBuiltinOp(BuiltinOperator_WHERE, BuiltinOptions_WhereOptions, CreateWhereOptions(builder_).Union()); BuildInterpreter({{}}); } int input() { return input_; } std::vector GetOutput() { return ExtractVector(output_); } protected: int input_; int output_; }; // Utils which returns TensorType from primitive type. // Currently Where op supports only float, bool. template TensorType GetTfLiteType(); template <> TensorType GetTfLiteType() { return TensorType_BOOL; } template <> TensorType GetTfLiteType() { return TensorType_FLOAT32; } template <> TensorType GetTfLiteType() { return TensorType_INT8; } template <> TensorType GetTfLiteType() { return TensorType_UINT8; } template <> TensorType GetTfLiteType() { return TensorType_INT32; } template <> TensorType GetTfLiteType() { return TensorType_UINT32; } template <> TensorType GetTfLiteType() { return TensorType_INT64; } // Helper function which creates std::vector from boolean type array 'data' // but with different type. The returned value will be in type 'T' and // matches the true/false criteria of where op. template std::vector GetCompatibleData(const std::initializer_list& data) { std::vector result; for (auto item : data) if (item) result.push_back(T(1)); else result.push_back(T(0)); return result; } // Typed test so we can run the same set of tests with different data types. template class WhereOpTest : public Test { public: using List = std::list; static T shared_; T value_; }; using MyTypes = ::testing::Types; TYPED_TEST_SUITE(WhereOpTest, MyTypes); TYPED_TEST(WhereOpTest, ScalarValueFail) { ConstInputWhereOpModel m(false, {TensorType_INT64, {}}); EXPECT_EQ(m.Invoke(), kTfLiteError); } TYPED_TEST(WhereOpTest, SelectFromVectorNoResult) { IntegerWhereOpModel m({GetTfLiteType(), {3}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({false, false, false})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutput().size(), 0); } TYPED_TEST(WhereOpTest, SelectFromVector) { IntegerWhereOpModel m({GetTfLiteType(), {3}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({true, false, true})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutput(), ElementsAreArray({0, 2})); } TYPED_TEST(WhereOpTest, SelectFromMatrixNoResult) { IntegerWhereOpModel m({GetTfLiteType(), {3, 3}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({false, false, false, // false, false, false, // false, false, false})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_EQ(m.GetOutput().size(), 0); } TYPED_TEST(WhereOpTest, SelectFromMatrix1) { IntegerWhereOpModel m({GetTfLiteType(), {3, 1}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({true, false, true})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutput(), ElementsAreArray({0, 0, // 2, 0})); } TYPED_TEST(WhereOpTest, SelectFromMatrix2) { IntegerWhereOpModel m({GetTfLiteType(), {3, 3}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({true, true, false, // true, false, false, // true, false, true})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutput(), ElementsAreArray({0, 0, // 0, 1, // 1, 0, // 2, 0, // 2, 2})); } TYPED_TEST(WhereOpTest, SelectFromMatrix3) { IntegerWhereOpModel m({GetTfLiteType(), {3, 5}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({true, false, false, true, true, // false, true, true, false, false, // true, false, true, false, false})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutput(), ElementsAreArray({0, 0, // 0, 3, // 0, 4, // 1, 1, // 1, 2, // 2, 0, // 2, 2})); } TYPED_TEST(WhereOpTest, SelectFromRank3TensorNoResult) { IntegerWhereOpModel m({GetTfLiteType(), {2, 2, 2}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({false, false, false, false, // false, false, false, false})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_EQ(m.GetOutput().size(), 0); } TYPED_TEST(WhereOpTest, SelectFromRank3Tensor1) { IntegerWhereOpModel m({GetTfLiteType(), {2, 1, 3}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({true, false, true, // false, false, true})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutput(), ElementsAreArray({0, 0, 0, // 0, 0, 2, // 1, 0, 2})); } TYPED_TEST(WhereOpTest, SelectFromRank3Tensor2) { IntegerWhereOpModel m({GetTfLiteType(), {2, 2, 2}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({true, true, false, true, // false, false, true, true})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutput(), ElementsAreArray({0, 0, 0, // 0, 0, 1, // 0, 1, 1, // 1, 1, 0, // 1, 1, 1})); } TYPED_TEST(WhereOpTest, SelectFromRank3Tensor3) { IntegerWhereOpModel m({GetTfLiteType(), {2, 3, 2}}, {TensorType_INT64, {}}); m.PopulateTensor( m.input(), GetCompatibleData({true, true, false, true, false, false, // false, false, true, false, true, true})); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutput(), ElementsAreArray({0, 0, 0, // 0, 0, 1, // 0, 1, 1, // 1, 1, 0, // 1, 2, 0, // 1, 2, 1})); } } // namespace } // namespace tflite