* limitations under the License.
*/
-#include "kernels/Equal.h"
#include "kernels/TestUtils.h"
-#include "luci_interpreter/TestMemoryManager.h"
+#include "luci_interpreter/test_models/equal/FloatEqualKernel.h"
+#include "luci_interpreter/test_models/equal/IntEqualKernel.h"
+
+#include "loader/ModuleLoader.h"
namespace luci_interpreter
{
-namespace kernels
-{
namespace
{
class EqualTest : public ::testing::Test
{
-protected:
- void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
-
- std::unique_ptr<IMemoryManager> _memory_manager;
+ // Do nothing
};
-TEST_F(EqualTest, FloatSimple)
-{
- std::vector<float> x_data{
- 0.5, 0.7, 0.9, // Row 1
- 1, 0, -1, // Row 2
- };
-
- std::vector<float> y_data{
- 0.9, 0.7, 0.5, // Row 1
- -1, 0, 1, // Row 2
- };
-
- std::vector<bool> ref_output_data{
- false, true, false, // Row 1
- false, true, false, // Row 2
- };
-
- Tensor x_tensor = makeInputTensor<DataType::FLOAT32>({2, 3}, x_data, _memory_manager.get());
- Tensor y_tensor = makeInputTensor<DataType::FLOAT32>({2, 3}, y_data, _memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- kernel.configure();
- _memory_manager->allocate_memory(output_tensor);
- kernel.execute();
-
- EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({2, 3}));
-}
-
-TEST_F(EqualTest, FloatBroardcast)
+template <typename T, typename U>
+std::vector<U> checkEqualKernel(test_kernel::TestDataBase<T, U> *test_data_base)
{
- std::vector<float> x_data{
- 0.5, 0.7, 0.9, // Row 1
- 1, 0, -1, // Row 2
- -1, 0, 1, // Row 3
- 0.9, 0.7, 0.5, // Row 4
- };
-
- std::vector<float> y_data{
- 0.9, 0.7, 0.5, // Row 1
- };
-
- std::vector<bool> ref_output_data{
- false, true, false, // Row 1
- false, false, false, // Row 2
- false, false, false, // Row 3
- true, true, true, // Row 4
- };
-
- Tensor x_tensor = makeInputTensor<DataType::FLOAT32>({4, 3}, x_data, _memory_manager.get());
- Tensor y_tensor = makeInputTensor<DataType::FLOAT32>({1, 3}, y_data, _memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- kernel.configure();
- _memory_manager->allocate_memory(output_tensor);
- kernel.execute();
-
- EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({4, 3}));
-}
-
-template <loco::DataType DType>
-void checkIntegerSimple(luci_interpreter::IMemoryManager *memory_manager)
-{
- using dtype = typename loco::DataTypeImpl<DType>::Type;
- dtype min_value = std::numeric_limits<dtype>::min();
- dtype max_value = std::numeric_limits<dtype>::max();
- std::vector<dtype> x_data{min_value, 2, max_value};
-
- std::vector<dtype> y_data{min_value, -2, max_value};
-
- std::vector<bool> ref_output_data{true, false, true};
-
- Tensor x_tensor = makeInputTensor<DType>({3}, x_data, memory_manager);
- Tensor y_tensor = makeInputTensor<DType>({3}, y_data, memory_manager);
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- kernel.configure();
- memory_manager->allocate_memory(output_tensor);
- kernel.execute();
+ MemoryManager memory_manager{};
+ RuntimeModule runtime_module{};
+ bool dealloc_input = true;
- EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({3}));
-}
+ // Load model with single op
+ auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
+ ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
-template <loco::DataType DType>
-void checkIntegerBroadcast(luci_interpreter::IMemoryManager *memory_manager)
-{
- using dtype = typename loco::DataTypeImpl<DType>::Type;
- dtype min_value = std::numeric_limits<dtype>::min();
- dtype max_value = std::numeric_limits<dtype>::max();
- std::vector<dtype> x_data{
- min_value, 2, 3, // Row 1
- 4, 5, max_value, // Row 2
- -1, -2, -3, // Row 3
- min_value, -2, max_value, // Row 4
- };
+ auto *main_runtime_graph = runtime_module.getMainGraph();
+ assert(main_runtime_graph->getNumOfInputTensors() == 2);
- std::vector<dtype> y_data{
- min_value, -2, max_value, // Row 1
- };
+ // set left input data
+ {
+ auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
+ std::copy(test_data_base->get_input_data_by_index(0).begin(),
+ test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
+ }
- std::vector<bool> ref_output_data{
- true, false, false, // Row 1
- false, false, true, // Row 2
- false, true, false, // Row 3
- true, true, true, // Row 4
- };
+ // set right input data
+ {
+ auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(1));
+ std::copy(test_data_base->get_input_data_by_index(1).begin(),
+ test_data_base->get_input_data_by_index(1).end(), input_tensor_data);
+ }
- Tensor x_tensor = makeInputTensor<DType>({4, 3}, x_data, memory_manager);
- Tensor y_tensor = makeInputTensor<DType>({3}, y_data, memory_manager);
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
+ runtime_module.execute();
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- kernel.configure();
- memory_manager->allocate_memory(output_tensor);
- kernel.execute();
+ assert(main_runtime_graph->getNumOfOutputTensors() == 1);
- EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({4, 3}));
+ U *output_data = reinterpret_cast<U *>(main_runtime_graph->getOutputDataByIndex(0));
+ const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(U));
+ std::vector<U> output_data_vector(output_data, output_data + num_elements);
+ return output_data_vector;
}
-TEST_F(EqualTest, Int32)
+TEST_F(EqualTest, FloatNoBroadcast_P)
{
- checkIntegerSimple<loco::DataType::S32>(_memory_manager.get());
- checkIntegerBroadcast<loco::DataType::S32>(_memory_manager.get());
- SUCCEED();
+ const bool is_with_broadcast = false;
+ test_kernel::TestDataFloatEqual test_data_kernel(is_with_broadcast, false);
+ std::vector<bool> output_data_vector = checkEqualKernel<float, bool>(&test_data_kernel);
+ EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
}
-TEST_F(EqualTest, Int64)
+TEST_F(EqualTest, FloatWithBroadcast_P)
{
- checkIntegerSimple<loco::DataType::S64>(_memory_manager.get());
- checkIntegerBroadcast<loco::DataType::S64>(_memory_manager.get());
- SUCCEED();
+ const bool is_with_broadcast = true;
+ test_kernel::TestDataFloatEqual test_data_kernel(is_with_broadcast, false);
+ std::vector<bool> output_data_vector = checkEqualKernel<float, bool>(&test_data_kernel);
+ EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
}
-// Choose min / max in such a way that there are exactly 256 units to avoid rounding errors.
-const float F_MIN = -128.0 / 128.0;
-const float F_MAX = 127.0 / 128.0;
-
-TEST_F(EqualTest, Uint8Quantized)
+TEST_F(EqualTest, FloatNoBroadcast_NEG)
{
- std::vector<float> x_data{
- 0.5, 0.5, 0.7, 0.9, // Row 1
- 1, 0, 0.05, -1, // Row 2
- };
-
- std::vector<float> y_data{
- 0.9, 0.5, 0.55, 0.5, // Row 1
- -1, 0, 0.05, 1, // Row 2
- };
-
- std::vector<bool> ref_output_data{
- false, true, false, false, // Row 1
- false, true, true, false, // Row 2
- };
-
- std::pair<float, int32_t> x_quant_param = quantizationParams<uint8_t>(F_MIN, F_MAX);
- Tensor x_tensor = makeInputTensor<DataType::U8>(
- {1, 2, 4, 1}, x_quant_param.first, x_quant_param.second, x_data, _memory_manager.get());
-
- std::pair<float, int32_t> y_quant_param = quantizationParams<uint8_t>(F_MIN * 2, F_MAX * 2);
- Tensor y_tensor = makeInputTensor<DataType::U8>(
- {1, 2, 4, 1}, y_quant_param.first, y_quant_param.second, y_data, _memory_manager.get());
-
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- kernel.configure();
- _memory_manager->allocate_memory(output_tensor);
- kernel.execute();
-
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 2, 4, 1}));
- EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
+ const bool is_with_broadcast = false;
+ test_kernel::TestDataFloatEqual test_data_kernel(is_with_broadcast, true);
+ EXPECT_DEATH(checkEqualKernel(&test_data_kernel), "");
}
-TEST_F(EqualTest, Uint8QuantizedBroadcast)
+TEST_F(EqualTest, FloatWithBroadcast_NEG)
{
- std::vector<float> x_data{
- 0.4, -0.8, 0.7, 0.3, // Row 1
- -0.5, 0.1, 0, 0.5, // Row 2
- 1, 0, 0.05, -1, // Row 3
- -1, 0.05, 0, 1, // Row 4
- };
-
- std::vector<float> y_data{
- -1, 0.05, 0, 1, // Row 1
- };
-
- std::vector<bool> ref_output_data{
- false, false, false, false, // Row 1
- false, false, true, false, // Row 2
- false, false, false, false, // Row 3
- true, true, true, true, // Row 4
- };
-
- std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(F_MIN, F_MAX);
- Tensor x_tensor = makeInputTensor<DataType::U8>(
- {1, 4, 4, 1}, quant_param.first, quant_param.second, x_data, _memory_manager.get());
- Tensor y_tensor = makeInputTensor<DataType::U8>(
- {1, 1, 4, 1}, quant_param.first, quant_param.second, y_data, _memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- kernel.configure();
- _memory_manager->allocate_memory(output_tensor);
- kernel.execute();
-
- EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 4, 4, 1}));
- EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
+ const bool is_with_broadcast = true;
+ test_kernel::TestDataFloatEqual test_data_kernel(is_with_broadcast, true);
+ EXPECT_DEATH(checkEqualKernel(&test_data_kernel), "");
}
-TEST_F(EqualTest, Input_Type_Mismatch_NEG)
+TEST_F(EqualTest, IntWithBroadcast_P)
{
- Tensor x_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.f}, _memory_manager.get());
- Tensor y_tensor = makeInputTensor<DataType::U8>({1}, {1}, _memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- EXPECT_ANY_THROW(kernel.configure());
+ const bool is_with_broadcast = true;
+ test_kernel::TestDataIntEqual test_data_kernel(is_with_broadcast, false);
+ std::vector<bool> output_data_vector = checkEqualKernel<int32_t, bool>(&test_data_kernel);
+ EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
}
-TEST_F(EqualTest, Input_Output_Type_NEG)
+TEST_F(EqualTest, IntNoBroadcast_P)
{
- Tensor x_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.f}, _memory_manager.get());
- Tensor y_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.f}, _memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- EXPECT_ANY_THROW(kernel.configure());
+ const bool is_with_broadcast = false;
+ test_kernel::TestDataIntEqual test_data_kernel(is_with_broadcast, false);
+ std::vector<bool> output_data_vector = checkEqualKernel<int32_t, bool>(&test_data_kernel);
+ EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
}
-TEST_F(EqualTest, Float_Broadcast_NEG)
+TEST_F(EqualTest, IntWithBroadcast_NEG)
{
- Tensor x_tensor = makeInputTensor<DataType::FLOAT32>({2}, {1.f, 2.f}, _memory_manager.get());
- Tensor y_tensor = makeInputTensor<DataType::FLOAT32>({3}, {1.f, 2.f, 3.f}, _memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- ASSERT_ANY_THROW(kernel.configure());
+ const bool is_with_broadcast = true;
+ test_kernel::TestDataIntEqual test_data_kernel(is_with_broadcast, true);
+ EXPECT_DEATH(checkEqualKernel(&test_data_kernel), "");
}
-TEST_F(EqualTest, Int32_Broadcast_NEG)
+TEST_F(EqualTest, IntNoBroadcast_NEG)
{
- Tensor x_tensor = makeInputTensor<DataType::S32>({2}, {1, 2}, _memory_manager.get());
- Tensor y_tensor = makeInputTensor<DataType::S32>({3}, {1, 2, 3}, _memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- ASSERT_ANY_THROW(kernel.configure());
-}
-
-TEST_F(EqualTest, Int64_Broadcast_NEG)
-{
- Tensor x_tensor = makeInputTensor<DataType::S64>({2}, {1, 2}, _memory_manager.get());
- Tensor y_tensor = makeInputTensor<DataType::S64>({3}, {1, 2, 3}, _memory_manager.get());
- Tensor output_tensor = makeOutputTensor(DataType::BOOL);
-
- Equal kernel(&x_tensor, &y_tensor, &output_tensor);
- ASSERT_ANY_THROW(kernel.configure());
+ const bool is_with_broadcast = false;
+ test_kernel::TestDataIntEqual test_data_kernel(is_with_broadcast, true);
+ EXPECT_DEATH(checkEqualKernel(&test_data_kernel), "");
}
} // namespace
-} // namespace kernels
} // namespace luci_interpreter