/* Copyright 2017 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 #include #include "tensorflow/lite/kernels/test_util.h" #include "tensorflow/lite/schema/schema_generated.h" #include "tensorflow/lite/string_type.h" namespace tflite { namespace { using ::testing::ElementsAreArray; class TileOpBaseModel : public SingleOpModel { public: template void SetInput(std::initializer_list data) { PopulateTensor(input_, data); } template void SetMultipliers(std::initializer_list data) { PopulateTensor(multipliers_, data); } template std::vector GetOutput() { return ExtractVector(output_); } std::vector GetOutputShape() { return GetTensorShape(output_); } protected: int input_; int multipliers_; int output_; }; template class TileOpConstModel : public TileOpBaseModel { public: TileOpConstModel(std::initializer_list input_shape, std::initializer_list input_data, TensorType input_type, TensorType multiply_type, std::initializer_list multipliers_data, bool allocate_and_delegate = true) { SetupInput(input_shape, input_data, input_type, std::is_same()); multipliers_ = AddConstInput(multiply_type, multipliers_data, {static_cast(multipliers_data.size())}); output_ = AddOutput(input_type); SetBuiltinOp(BuiltinOperator_TILE, BuiltinOptions_TileOptions, 0); BuildInterpreter({input_shape, {static_cast(input_shape.size())}}, /*num_threads=*/-1, /*allow_fp32_relax_to_fp16=*/false, /*apply_delegate=*/true, allocate_and_delegate); if (allocate_and_delegate) { PopulateInput(input_data, std::is_same()); } } private: template void SetupInput(std::initializer_list input_shape, std::initializer_list input_data, TensorType input_type, std::false_type) { input_ = AddConstInput(input_type, input_data, input_shape); } template void SetupInput(std::initializer_list input_shape, std::initializer_list input_data, TensorType input_type, std::true_type) { input_ = AddInput(input_type); } template void PopulateInput(std::initializer_list input_data, std::false_type) {} template void PopulateInput(std::initializer_list input_data, std::true_type) { SetInput(input_data); } }; class TileOpDynamicModel : public TileOpBaseModel { public: TileOpDynamicModel(std::initializer_list input_shape, TensorType input_type, TensorType multiply_type) { input_ = AddInput(input_type); multipliers_ = AddInput(multiply_type); output_ = AddOutput(input_type); SetBuiltinOp(BuiltinOperator_TILE, BuiltinOptions_TileOptions, 0); BuildInterpreter({input_shape, {static_cast(input_shape.size())}}); } }; enum class TestType { kConst = 0, kDynamic = 1, }; template void Check(std::initializer_list input_shape, std::initializer_list input_data, std::initializer_list multipliers_data, std::initializer_list exp_output_shape, std::initializer_list exp_output_data, TensorType input_type, TensorType multiply_type, TestType test_type) { switch (test_type) { case TestType::kConst: { if (SingleOpModel::GetForceUseNnapi() && !std::is_same::value) { // NNAPI does not support graphs with all constant inputs. return; } TileOpConstModel m( input_shape, input_data, input_type, multiply_type, multipliers_data); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray(exp_output_shape)); EXPECT_THAT(m.template GetOutput(), ElementsAreArray(exp_output_data)); return; } case TestType::kDynamic: { TileOpDynamicModel m(input_shape, input_type, multiply_type); m.SetInput(input_data); m.SetMultipliers(multipliers_data); ASSERT_EQ(m.Invoke(), kTfLiteOk); EXPECT_THAT(m.GetOutputShape(), ElementsAreArray(exp_output_shape)); EXPECT_THAT(m.template GetOutput(), ElementsAreArray(exp_output_data)); return; } } } class TileTest : public ::testing::TestWithParam {}; TEST_P(TileTest, Float32Vector) { Check(/*input_shape=*/{3}, /*input_data=*/{1.0, 2.0, 3.0}, /*multipliers_data=*/{2}, /*exp_output_shape=*/{6}, /*exp_output_data=*/{1.0, 2.0, 3.0, 1.0, 2.0, 3.0}, /*input_type=*/TensorType_FLOAT32, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, Float32Matrix) { Check( /*input_shape=*/{2, 3}, /*input_data=*/{11.f, 12.f, 13.f, 21.f, 22.f, 23.f}, /*multipliers_data=*/{2, 1}, /*exp_output_shape=*/{4, 3}, /*exp_output_data=*/ {11.f, 12.f, 13.f, 21.f, 22.f, 23.f, 11.f, 12.f, 13.f, 21.f, 22.f, 23.f}, /*input_type=*/TensorType_FLOAT32, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, Float32HighDimension) { Check( /*input_shape=*/{1, 2, 3}, /*input_data=*/{11.f, 12.f, 13.f, 21.f, 22.f, 23.f}, /*multipliers_data=*/{2, 3, 1}, /*exp_output_shape=*/{2, 6, 3}, /*exp_output_data=*/{11.f, 12.f, 13.f, 21.f, 22.f, 23.f, 11.f, 12.f, 13.f, 21.f, 22.f, 23.f, 11.f, 12.f, 13.f, 21.f, 22.f, 23.f, 11.f, 12.f, 13.f, 21.f, 22.f, 23.f, 11.f, 12.f, 13.f, 21.f, 22.f, 23.f, 11.f, 12.f, 13.f, 21.f, 22.f, 23.f}, /*input_type=*/TensorType_FLOAT32, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, Uint8Matrix) { Check( /*input_shape=*/{2, 3}, /*input_data=*/{11, 12, 13, 21, 22, 23}, /*multipliers_data=*/{2, 1}, /*exp_output_shape=*/{4, 3}, /*exp_output_data=*/{11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23}, /*input_type=*/TensorType_UINT8, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, Int32Matrix) { Check( /*input_shape=*/{2, 3}, /*input_data=*/{11, 12, 13, 21, 22, 23}, /*multipliers_data=*/{2, 1}, /*exp_output_shape=*/{4, 3}, /*exp_output_data=*/{11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23}, /*input_type=*/TensorType_INT32, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, BooleanMatrix) { Check( /*input_shape=*/{2, 3}, /*input_data=*/{true, false, false, true, true, false}, /*multipliers_data=*/{2, 1}, /*exp_output_shape=*/{4, 3}, /*exp_output_data=*/ {true, false, false, true, true, false, true, false, false, true, true, false}, /*input_type=*/TensorType_BOOL, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, Int64Matrix) { Check( /*input_shape=*/{2, 3}, /*input_data=*/{11, 12, 13, 21, 22, 23}, /*multipliers_data=*/{2, 1}, /*exp_output_shape=*/{4, 3}, /*exp_output_data=*/{11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23}, /*input_type=*/TensorType_INT64, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, Int64Matrix64Multipliers) { Check( /*input_shape=*/{2, 3}, /*input_data=*/{11, 12, 13, 21, 22, 23}, /*multipliers_data=*/{2, 1}, /*exp_output_shape=*/{4, 3}, /*exp_output_data=*/{11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23}, /*input_type=*/TensorType_INT64, /*multiply_type=*/TensorType_INT64, GetParam()); } TEST_P(TileTest, Int8Matrix) { if (SingleOpModel::GetForceUseNnapi()) { return; } Check( /*input_shape=*/{2, 3}, /*input_data=*/{11, 12, 13, 21, 22, 23}, /*multipliers_data=*/{2, 1}, /*exp_output_shape=*/{4, 3}, /*exp_output_data=*/{11, 12, 13, 21, 22, 23, 11, 12, 13, 21, 22, 23}, /*input_type=*/TensorType_INT8, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, StringMatrix) { Check( /*input_shape=*/{2, 3}, /*input_data=*/{"AA", "AB", "AC", "BA", "BB", "BC"}, /*multipliers_data=*/{1, 2}, /*exp_output_shape=*/{2, 6}, /*exp_output_data=*/ {"AA", "AB", "AC", "AA", "AB", "AC", "BA", "BB", "BC", "BA", "BB", "BC"}, /*input_type=*/TensorType_STRING, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, StringMatrix64Multipliers) { Check( /*input_shape=*/{2, 3}, /*input_data=*/{"AA", "AB", "AC", "BA", "BB", "BC"}, /*multipliers_data=*/{2, 1}, /*exp_output_shape=*/{4, 3}, /*exp_output_data=*/ {"AA", "AB", "AC", "BA", "BB", "BC", "AA", "AB", "AC", "BA", "BB", "BC"}, /*input_type=*/TensorType_STRING, /*multiply_type=*/TensorType_INT64, GetParam()); } TEST_P(TileTest, StringMatrix2) { Check( /*input_shape=*/{3, 2, 1}, /*input_data=*/{"AA", "AB", "AC", "BA", "BB", "BC"}, /*multipliers_data=*/{2, 2, 2}, /*exp_output_shape=*/{6, 4, 2}, /*exp_output_data=*/ {"AA", "AA", "AB", "AB", "AA", "AA", "AB", "AB", "AC", "AC", "BA", "BA", "AC", "AC", "BA", "BA", "BB", "BB", "BC", "BC", "BB", "BB", "BC", "BC", "AA", "AA", "AB", "AB", "AA", "AA", "AB", "AB", "AC", "AC", "BA", "BA", "AC", "AC", "BA", "BA", "BB", "BB", "BC", "BC", "BB", "BB", "BC", "BC"}, /*input_type=*/TensorType_STRING, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, StringMatrixEmptyInputElements) { Check( /*input_shape=*/{0, 1, 1}, /*input_data=*/{}, /*multipliers_data=*/{2, 2, 2}, /*exp_output_shape=*/{0, 2, 2}, /*exp_output_data=*/ {}, /*input_type=*/TensorType_STRING, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST_P(TileTest, Int32EmptyInput) { Check(/*input_shape=*/{2, 1, 3}, /*input_data=*/{11, 12, 13, 21, 22, 23}, /*multipliers_data=*/{2, 0, 2}, /*exp_output_shape=*/{4, 0, 6}, /*exp_output_data=*/{}, /*input_type=*/TensorType_INT32, /*multiply_type=*/TensorType_INT32, GetParam()); } TEST(TileDynamicTest, NegativeMultipliersString) { TileOpDynamicModel m({2, 1, 1}, TensorType_STRING, TensorType_INT32); m.SetInput({"A", "B"}); m.SetMultipliers({2, -100, -10}); EXPECT_NE(m.Invoke(), kTfLiteOk); } template void CheckInvalidMultipliers( TensorType multiply_type, TestType test_type, std::initializer_list multipliers_data) { switch (test_type) { case TestType::kConst: { TileOpConstModel m( {2, 3}, {11, 12, 13, 21, 22, 23}, TensorType_INT32, multiply_type, multipliers_data, /*allocate_and_delegate=*/false); EXPECT_NE(m.AllocateTensors(), kTfLiteOk); return; } case TestType::kDynamic: { TileOpDynamicModel m({2, 3}, TensorType_INT32, multiply_type); m.SetInput({11, 12, 13, 21, 22, 23}); m.SetMultipliers(multipliers_data); EXPECT_NE(m.Invoke(), kTfLiteOk); return; } } } TEST_P(TileTest, MultiplierOverflowInt32) { CheckInvalidMultipliers(TensorType_INT32, GetParam(), {1073741824, 1}); } TEST_P(TileTest, MultiplierNegativeInt32) { CheckInvalidMultipliers(TensorType_INT32, GetParam(), {-1, 1}); } TEST_P(TileTest, MultiplierNegativeInt64) { CheckInvalidMultipliers(TensorType_INT64, GetParam(), {-1LL, 1LL}); } INSTANTIATE_TEST_SUITE_P(TileTest, TileTest, ::testing::Values(TestType::kConst, TestType::kDynamic)); } // namespace } // namespace tflite