refactor one-hot nGraph backend test (#2578)

This commit is contained in:
Tomasz Jankowski 2020-11-02 10:12:39 +01:00 committed by GitHub
parent 1b94682f86
commit 2d9a47127d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<op::v1::OneHot>(A, depth, on_value, off_value, axis);
auto f = make_shared<Function>(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<int32_t>{2});
auto result = backend->create_tensor(element::i32, shape_r);
auto handle = backend->compile(f);
handle->call_with_validate({result}, {a});
EXPECT_EQ((vector<int32_t>{0, 0, 1}), read_vector<int32_t>(result));
auto test_case = test::TestCase<TestEngine>(f);
test_case.add_input<int32_t>({2});
test_case.add_expected_output<int32_t>(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<op::v1::OneHot>(A, depth, on_value, off_value, axis);
auto f = make_shared<Function>(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<int32_t>{1});
auto result = backend->create_tensor(element::i32, shape_r);
auto handle = backend->compile(f);
handle->call_with_validate({result}, {a});
EXPECT_EQ((vector<int32_t>{0, 1, 0}), read_vector<int32_t>(result));
auto test_case = test::TestCase<TestEngine>(f);
test_case.add_input<int32_t>({1});
test_case.add_expected_output<int32_t>(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<op::v1::OneHot>(A, depth, on_value, off_value, axis);
auto f = make_shared<Function>(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<int32_t>{0});
auto result = backend->create_tensor(element::i32, shape_r);
auto handle = backend->compile(f);
handle->call_with_validate({result}, {a});
EXPECT_EQ((vector<int32_t>{1, 0, 0}), read_vector<int32_t>(result));
auto test_case = test::TestCase<TestEngine>(f);
test_case.add_input<int32_t>({0});
test_case.add_expected_output<int32_t>(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<op::v1::OneHot>(A, depth, on_value, off_value, axis);
auto f = make_shared<Function>(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<int32_t>{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<int32_t>{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<int32_t>(result));
auto test_case = test::TestCase<TestEngine>(f);
test_case.add_input<int32_t>({2, 1, 0, 0, 2, 2, 1, 0});
test_case.add_expected_output<int32_t>(
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<op::v1::OneHot>(A, depth, on_value, off_value, axis);
auto f = make_shared<Function>(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<int32_t>{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<int32_t>{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<int32_t>(result));
auto test_case = test::TestCase<TestEngine>(f);
test_case.add_input<int32_t>({2, 1, 0, 0, 2, 2, 1, 0});
test_case.add_expected_output<int32_t>(
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<op::v1::OneHot>(A, depth, on_value, off_value, axis);
auto f = make_shared<Function>(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<int32_t>{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<int32_t> rv = read_vector<int32_t>(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<TestEngine>(f);
test_case.add_input<int32_t>({2, 1, 0, 0, 3, 2, 1, 0});
// elements 12, 13, 14 are zeroed as out of bound
test_case.add_expected_output<int32_t>(
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<op::v1::OneHot>(A, depth, on_value, off_value, axis);
auto f = make_shared<Function>(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<int32_t>{
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<int32_t>{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<int32_t>(result));
auto test_case = test::TestCase<TestEngine>(f);
test_case.add_input<int32_t>({0, 1, 1, 2, 1, 0, 0, 2, 1});
test_case.add_expected_output<int32_t>(
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<op::Parameter>(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<op::v1::OneHot>(A, depth, on_value, off_value, axis);
auto f = make_shared<Function>(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<int32_t> input_data{0, 11, 101, 1001, 10001, static_cast<int32_t>(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<int32_t> data = read_vector<int32_t>(result);
vector<int32_t> bit_positions;
for (size_t i = 0; i < shape_size(shape_r); ++i)
vector<int32_t> input{0, 11, 101, 1001, 10001, static_cast<int32_t>(category_count - 1)};
vector<int32_t> 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<TestEngine>(f);
test_case.add_input<int32_t>(input);
test_case.add_expected_output<int32_t>(shape_r, output);
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, one_hot_on_off_float)