diff --git a/ngraph/test/backend/subtract.in.cpp b/ngraph/test/backend/subtract.in.cpp index 396c7cc0987..15b51a758cc 100644 --- a/ngraph/test/backend/subtract.in.cpp +++ b/ngraph/test/backend/subtract.in.cpp @@ -87,3 +87,96 @@ NGRAPH_TEST(${BACKEND_NAME}, subtract_overload) handle->call_with_validate({result}, {a, b}); EXPECT_TRUE(test::all_close_f((vector{1, 2, 4, 8}), read_vector(result))); } + +namespace +{ + template + void subtract_broadcst() + { + const auto element_type = ngraph::element::from(); + const Shape shape_a{3, 2, 1}; + const Shape shape_b{1, 6}; + const Shape shape_o{3, 2, 6}; + std::vector in_a{12, 24, 36, 48, 60, 72}; + std::vector in_b{1, 2, 3, 4, 6, 1}; + // clang-format off + std::vector out{11, 10, 9, 8, 6, 11, + 23, 22, 21, 20, 18, 23, + + 35, 34, 33, 32, 30, 35, + 47, 46, 45, 44, 42, 47, + + 59, 58, 57, 56, 54, 59, + 71, 70, 69, 68, 66, 71}; + // clang-format on + + auto A = make_shared(element_type, shape_a); + auto B = make_shared(element_type, shape_b); + auto f = make_shared(make_shared(A, B), ParameterVector{A, B}); + + auto backend = runtime::Backend::create("${BACKEND_NAME}"); + + // Create some tensors for input/output + auto a = backend->create_tensor(element_type, shape_a, in_a.data()); + auto b = backend->create_tensor(element_type, shape_b, in_b.data()); + auto result = backend->create_tensor(element_type, shape_o); + + auto handle = backend->compile(f); + handle->call_with_validate({result}, {a, b}); + EXPECT_EQ(out, read_vector(result)); + } +} // namespace + +NGRAPH_TEST(${BACKEND_NAME}, subtract_int32_broadcast) +{ + subtract_broadcst(); +} + +NGRAPH_TEST(${BACKEND_NAME}, subtract_f32_broadcast) +{ + subtract_broadcst(); +} + +NGRAPH_TEST(${BACKEND_NAME}, subtract_int32_scalar) +{ + Shape shape{}; + + auto A = make_shared(element::i32, shape); + auto B = make_shared(element::i32, shape); + auto f = make_shared(make_shared(A, B), ParameterVector{A, B}); + + auto backend = runtime::Backend::create("${BACKEND_NAME}"); + + // Create some tensors for input/output + auto a = backend->create_tensor(element::i32, shape); + copy_data(a, vector{2}); + auto b = backend->create_tensor(element::i32, shape); + copy_data(b, vector{8}); + auto result = backend->create_tensor(element::i32, shape); + + auto handle = backend->compile(f); + handle->call_with_validate({result}, {a, b}); + EXPECT_EQ(vector{-6}, read_vector(result)); +} + +NGRAPH_TEST(${BACKEND_NAME}, subtract_f32_scalar) +{ + Shape shape{}; + + auto A = make_shared(element::f32, shape); + auto B = make_shared(element::f32, shape); + auto f = make_shared(make_shared(A, B), ParameterVector{A, B}); + + auto backend = runtime::Backend::create("${BACKEND_NAME}"); + + // Create some tensors for input/output + auto a = backend->create_tensor(element::f32, shape); + copy_data(a, vector{3.1}); + auto b = backend->create_tensor(element::f32, shape); + copy_data(b, vector{8}); + auto result = backend->create_tensor(element::f32, shape); + + auto handle = backend->compile(f); + handle->call_with_validate({result}, {a, b}); + EXPECT_TRUE(test::all_close_f((vector{-4.9}), read_vector(result))); +}