/* Copyright 2018 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/types/half.h" namespace tflite { namespace { using ::testing::ElementsAreArray; template class UnpackOpModel : public SingleOpModel { public: UnpackOpModel(const TensorData& input, int axis) { if (axis < 0) { axis += input.shape.size(); } const int num_outputs = input.shape[axis]; input_ = AddInput(input); for (int i = 0; i < num_outputs; ++i) { outputs_.push_back(AddOutput(input.type)); } SetBuiltinOp(BuiltinOperator_UNPACK, BuiltinOptions_UnpackOptions, CreateUnpackOptions(builder_, num_outputs, axis).Union()); BuildInterpreter({GetShape(input_)}); } // In this test, we represent all input and output test data as integers, and // convert it to the intended type here. void SetInput(std::vector data) { std::vector typed_data(data.begin(), data.end()); PopulateTensor(input_, typed_data); } std::vector> GetOutputDatas() { std::vector> output_datas; for (const int output : outputs_) { std::vector typed_output_data = ExtractVector(output); output_datas.emplace_back(typed_output_data.begin(), typed_output_data.end()); } return output_datas; } std::vector> GetOutputShapes() { std::vector> output_shapes; for (const int output : outputs_) { output_shapes.push_back(GetTensorShape(output)); } return output_shapes; } private: int input_; std::vector outputs_; }; template void Check(int axis, const std::initializer_list& input_shape, const std::initializer_list& input_data, const std::vector>& exp_output_shape, const std::vector>& exp_output_data, const TensorType& type = TensorType_FLOAT32) { UnpackOpModel m({type, input_shape}, axis); m.SetInput(input_data); ASSERT_EQ(m.Invoke(), kTfLiteOk); // Check outputs shapes. EXPECT_THAT(m.GetOutputShapes(), ElementsAreArray(exp_output_shape)); // Check outputs values. EXPECT_THAT(m.GetOutputDatas(), ElementsAreArray(exp_output_data)); } template struct UnpackOpTest : public ::testing::Test { using TypeToTest = InputType; TensorType TENSOR_TYPE = GetTensorType(); }; using TestTypes = testing::Types; TYPED_TEST_CASE(UnpackOpTest, TestTypes); TYPED_TEST(UnpackOpTest, ThreeOutputs) { Check( /*axis=*/0, /*input_shape=*/{3, 2}, /*input_data=*/{1, 2, 3, 4, 5, 6}, /*exp_output_shape=*/{{2}, {2}, {2}}, /*exp_output_data=*/{{1, 2}, {3, 4}, {5, 6}}, TestFixture::TENSOR_TYPE); } TYPED_TEST(UnpackOpTest, ThreeOutputsAxisOne) { Check( /*axis=*/1, /*input_shape=*/{3, 2}, /*input_data=*/{1, 2, 3, 4, 5, 6}, /*exp_output_shape=*/{{3}, {3}}, /*exp_output_data=*/{{1, 3, 5}, {2, 4, 6}}, TestFixture::TENSOR_TYPE); } TYPED_TEST(UnpackOpTest, ThreeOutputsNegativeAxisOne) { Check( /*axis=*/-1, /*input_shape=*/{3, 2}, /*input_data=*/{1, 2, 3, 4, 5, 6}, /*exp_output_shape=*/{{3}, {3}}, /*exp_output_data=*/{{1, 3, 5}, {2, 4, 6}}, TestFixture::TENSOR_TYPE); } TYPED_TEST(UnpackOpTest, OneOutput) { Check( /*axis=*/0, /*input_shape=*/{1, 6}, /*input_data=*/{1, 2, 3, 4, 5, 6}, /*exp_output_shape=*/{{6}}, /*exp_output_data=*/{{1, 2, 3, 4, 5, 6}}, TestFixture::TENSOR_TYPE); } TYPED_TEST(UnpackOpTest, ThreeDimensionsOutputs) { Check( /*axis=*/2, /*input_shape=*/{2, 2, 2}, /*input_data=*/{1, 2, 3, 4, 5, 6, 7, 8}, /*exp_output_shape=*/{{2, 2}, {2, 2}}, /*exp_output_data=*/{{1, 3, 5, 7}, {2, 4, 6, 8}}, TestFixture::TENSOR_TYPE); } TYPED_TEST(UnpackOpTest, FiveDimensionsOutputs) { Check( /*axis=*/2, /*input_shape=*/{2, 2, 2, 2, 1}, /*input_data=*/{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, /*exp_output_shape=*/{{2, 2, 2, 1}, {2, 2, 2, 1}}, /*exp_output_data=*/ {{1, 2, 5, 6, 9, 10, 13, 14}, {3, 4, 7, 8, 11, 12, 15, 16}}, /*type=*/TestFixture::TENSOR_TYPE); } TYPED_TEST(UnpackOpTest, VectorToScalar) { Check( /*axis=*/0, /*input_shape=*/{5}, /*input_data=*/{1, 2, 3, 4, 5}, /*exp_output_shape=*/{{}, {}, {}, {}, {}}, /*exp_output_data=*/{{1}, {2}, {3}, {4}, {5}}, TestFixture::TENSOR_TYPE); } // bool tests. TEST(UnpackOpTestBool, BoolThreeOutputs) { Check( /*axis=*/0, /*input_shape=*/{3, 2}, /*input_data=*/{true, false, true, false, true, false}, /*exp_output_shape=*/{{2}, {2}, {2}}, /*exp_output_data=*/{{true, false}, {true, false}, {true, false}}, /*type=*/TensorType_BOOL); } TEST(UnpackOpTestBool, BoolThreeOutputsAxisOne) { Check( /*axis=*/1, /*input_shape=*/{3, 2}, /*input_data=*/{true, false, true, false, true, false}, /*exp_output_shape=*/{{3}, {3}}, /*exp_output_data=*/{{true, true, true}, {false, false, false}}, /*type=*/TensorType_BOOL); } TEST(UnpackOpTestBool, BoolThreeOutputsNegativeAxisOne) { Check( /*axis=*/-1, /*input_shape=*/{3, 2}, /*input_data=*/{true, false, true, false, true, false}, /*exp_output_shape=*/{{3}, {3}}, /*exp_output_data=*/{{true, true, true}, {false, false, false}}, /*type=*/TensorType_BOOL); } TEST(UnpackOpTestBool, BoolThreeOutputsNegativeAxisTwo) { Check( /*axis=*/-2, /*input_shape=*/{3, 2}, /*input_data=*/{true, false, true, false, true, false}, /*exp_output_shape=*/{{2}, {2}, {2}}, /*exp_output_data=*/{{true, false}, {true, false}, {true, false}}, /*type=*/TensorType_BOOL); } TEST(UnpackOpTestBool, BoolOneOutput) { Check( /*axis=*/0, /*input_shape=*/{1, 6}, /*input_data=*/{true, false, true, false, true, false}, /*exp_output_shape=*/{{6}}, /*exp_output_data=*/{{true, false, true, false, true, false}}, /*type=*/TensorType_BOOL); } TEST(UnpackOpTestBool, BoolThreeDimensionsOutputs) { Check( /*axis=*/2, /*input_shape=*/{2, 2, 2}, /*input_data=*/{true, false, true, false, true, false, true, false}, /*exp_output_shape=*/{{2, 2}, {2, 2}}, /*exp_output_data=*/ {{true, true, true, true}, {false, false, false, false}}, /*type=*/TensorType_BOOL); } TEST(UnpackOpTest, BoolFiveDimensionsOutputs) { Check( /*axis=*/2, /*input_shape=*/{2, 2, 2, 2, 1}, /*input_data=*/ {true, false, true, false, true, false, true, false, true, true, true, true, true, true, true, true}, /*exp_output_shape=*/{{2, 2, 2, 1}, {2, 2, 2, 1}}, /*exp_output_data=*/ {{true, false, true, false, true, true, true, true}, {true, false, true, false, true, true, true, true}}, /*type=*/TensorType_BOOL); } TEST(UnpackOpTestBool, BoolVectorToScalar) { Check(/*axis=*/0, /*input_shape=*/{5}, /*input_data=*/{true, false, true, false, true}, /*exp_output_shape=*/{{}, {}, {}, {}, {}}, /*exp_output_data=*/{{true}, {false}, {true}, {false}, {true}}, /*type=*/TensorType_BOOL); } } // namespace } // namespace tflite