From 2d9a47127d88c312dfb8c2cbfce6bad99b5f2d42 Mon Sep 17 00:00:00 2001 From: Tomasz Jankowski Date: Mon, 2 Nov 2020 10:12:39 +0100 Subject: [PATCH] refactor one-hot nGraph backend test (#2578) --- ngraph/test/backend/one_hot.in.cpp | 187 +++++++---------------------- 1 file changed, 46 insertions(+), 141 deletions(-) diff --git a/ngraph/test/backend/one_hot.in.cpp b/ngraph/test/backend/one_hot.in.cpp index 3e322b48cbf..47192df718f 100644 --- a/ngraph/test/backend/one_hot.in.cpp +++ b/ngraph/test/backend/one_hot.in.cpp @@ -23,18 +23,15 @@ #include "gtest/gtest.h" #include "ngraph/ngraph.hpp" -#include "ngraph/runtime/tensor.hpp" -#include "runtime/backend.hpp" -#include "util/all_close.hpp" -#include "util/all_close_f.hpp" -#include "util/ndarray.hpp" +#include "util/engine/test_engines.hpp" +#include "util/test_case.hpp" #include "util/test_control.hpp" -#include "util/test_tools.hpp" using namespace std; using namespace ngraph; static string s_manifest = "${MANIFEST}"; +using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME}); NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_2_in_3) { @@ -48,16 +45,10 @@ NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_2_in_3) auto r = make_shared(A, depth, on_value, off_value, axis); auto f = make_shared(r, ParameterVector{A}); - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::i32, shape_a); - copy_data(a, vector{2}); - auto result = backend->create_tensor(element::i32, shape_r); - - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - EXPECT_EQ((vector{0, 0, 1}), read_vector(result)); + auto test_case = test::TestCase(f); + test_case.add_input({2}); + test_case.add_expected_output(shape_r, {0, 0, 1}); + test_case.run(); } NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_1_in_3) @@ -72,16 +63,10 @@ NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_1_in_3) auto r = make_shared(A, depth, on_value, off_value, axis); auto f = make_shared(r, ParameterVector{A}); - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::i32, shape_a); - copy_data(a, vector{1}); - auto result = backend->create_tensor(element::i32, shape_r); - - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - EXPECT_EQ((vector{0, 1, 0}), read_vector(result)); + auto test_case = test::TestCase(f); + test_case.add_input({1}); + test_case.add_expected_output(shape_r, {0, 1, 0}); + test_case.run(); } NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_0_in_3) @@ -96,16 +81,10 @@ NGRAPH_TEST(${BACKEND_NAME}, one_hot_scalar_0_in_3) auto r = make_shared(A, depth, on_value, off_value, axis); auto f = make_shared(r, ParameterVector{A}); - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::i32, shape_a); - copy_data(a, vector{0}); - auto result = backend->create_tensor(element::i32, shape_r); - - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - EXPECT_EQ((vector{1, 0, 0}), read_vector(result)); + auto test_case = test::TestCase(f); + test_case.add_input({0}); + test_case.add_expected_output(shape_r, {1, 0, 0}); + test_case.run(); } NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_0) @@ -120,18 +99,11 @@ NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_0) auto r = make_shared(A, depth, on_value, off_value, axis); auto f = make_shared(r, ParameterVector{A}); - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::i32, shape_a); - copy_data(a, vector{2, 1, 0, 0, 2, 2, 1, 0}); - auto result = backend->create_tensor(element::i32, shape_r); - - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - EXPECT_EQ( - (vector{0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0}), - read_vector(result)); + auto test_case = test::TestCase(f); + test_case.add_input({2, 1, 0, 0, 2, 2, 1, 0}); + test_case.add_expected_output( + shape_r, {0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0}); + test_case.run(); } NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_1) @@ -146,18 +118,11 @@ NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_1) auto r = make_shared(A, depth, on_value, off_value, axis); auto f = make_shared(r, ParameterVector{A}); - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::i32, shape_a); - copy_data(a, vector{2, 1, 0, 0, 2, 2, 1, 0}); - auto result = backend->create_tensor(element::i32, shape_r); - - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - EXPECT_EQ( - (vector{0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0}), - read_vector(result)); + auto test_case = test::TestCase(f); + test_case.add_input({2, 1, 0, 0, 2, 2, 1, 0}); + test_case.add_expected_output( + shape_r, {0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0}); + test_case.run(); } NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_1_barely_oob) @@ -172,49 +137,12 @@ NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_1_barely_oob) auto r = make_shared(A, depth, on_value, off_value, axis); auto f = make_shared(r, ParameterVector{A}); - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::i32, shape_a); - copy_data(a, vector{2, 1, 0, 0, 3, 2, 1, 0}); - auto result = backend->create_tensor(element::i32, shape_r); - - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - vector rv = read_vector(result); - - EXPECT_EQ(rv[0], 0); - EXPECT_EQ(rv[1], 0); - EXPECT_EQ(rv[2], 1); - - EXPECT_EQ(rv[3], 0); - EXPECT_EQ(rv[4], 1); - EXPECT_EQ(rv[5], 0); - - EXPECT_EQ(rv[6], 1); - EXPECT_EQ(rv[7], 0); - EXPECT_EQ(rv[8], 0); - - EXPECT_EQ(rv[9], 1); - EXPECT_EQ(rv[10], 0); - EXPECT_EQ(rv[11], 0); - - // These are undefined since value is out of bounds - // EXPECT_EQ(rv[12], 0); - // EXPECT_EQ(rv[13], 0); - // EXPECT_EQ(rv[14], 0); - - EXPECT_EQ(rv[15], 0); - EXPECT_EQ(rv[16], 0); - EXPECT_EQ(rv[17], 1); - - EXPECT_EQ(rv[18], 0); - EXPECT_EQ(rv[19], 1); - EXPECT_EQ(rv[20], 0); - - EXPECT_EQ(rv[21], 1); - EXPECT_EQ(rv[22], 0); - EXPECT_EQ(rv[23], 0); + auto test_case = test::TestCase(f); + test_case.add_input({2, 1, 0, 0, 3, 2, 1, 0}); + // elements 12, 13, 14 are zeroed as out of bound + test_case.add_expected_output( + shape_r, {0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0}); + test_case.run(); } NGRAPH_TEST(${BACKEND_NAME}, one_hot_matrix_0) @@ -229,30 +157,17 @@ NGRAPH_TEST(${BACKEND_NAME}, one_hot_matrix_0) auto r = make_shared(A, depth, on_value, off_value, axis); auto f = make_shared(r, ParameterVector{A}); - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::i32, shape_a); - copy_data(a, - vector{ - 0, 1, 1, 2, 1, 0, 0, 2, 1, - }); - auto result = backend->create_tensor(element::i32, shape_r); - - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - EXPECT_EQ((vector{1, 0, 0, 0, 0, 1, 1, 0, 0, - - 0, 1, 1, 0, 1, 0, 0, 0, 1, - - 0, 0, 0, 1, 0, 0, 0, 1, 0}), - read_vector(result)); + auto test_case = test::TestCase(f); + test_case.add_input({0, 1, 1, 2, 1, 0, 0, 2, 1}); + test_case.add_expected_output( + shape_r, {1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0}); + test_case.run(); } NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_many_categories) { // Imagenet has roughly 20,000 categories - uint32_t category_count = 20000; + constexpr uint32_t category_count = 20000; Shape shape_a{6}; auto A = make_shared(element::i32, shape_a); Shape shape_r{6, category_count}; @@ -263,27 +178,17 @@ NGRAPH_TEST(${BACKEND_NAME}, one_hot_vector_many_categories) auto r = make_shared(A, depth, on_value, off_value, axis); auto f = make_shared(r, ParameterVector{A}); - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::i32, shape_a); - vector input_data{0, 11, 101, 1001, 10001, static_cast(category_count - 1)}; - copy_data(a, input_data); - auto result = backend->create_tensor(element::i32, shape_r); - - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - vector data = read_vector(result); - - vector bit_positions; - for (size_t i = 0; i < shape_size(shape_r); ++i) + vector input{0, 11, 101, 1001, 10001, static_cast(category_count - 1)}; + vector output(shape_size(shape_r), 0); + for (size_t i = 0; i < input.size(); ++i) { - if (data[i] == 1) - { - bit_positions.push_back(i % category_count); - } + output[i * category_count + input[i]] = 1; } - EXPECT_EQ(bit_positions, input_data); + + auto test_case = test::TestCase(f); + test_case.add_input(input); + test_case.add_expected_output(shape_r, output); + test_case.run(); } NGRAPH_TEST(${BACKEND_NAME}, one_hot_on_off_float)