[PyOV] Make graph tests hardware agnostic - part 4 (#14705)

This commit is contained in:
Przemyslaw Wysocki 2022-12-21 14:44:20 +01:00 committed by GitHub
parent 9ba9687301
commit cc221679e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 537 additions and 1377 deletions

View File

@ -186,7 +186,7 @@ def acosh(node: NodeInput, name: Optional[str] = None) -> Node:
:param name: Optional new name for output node.
:return: New node with arccosh operation applied on it.
"""
return _get_node_factory_opset4().create("Acosh", [node])
return _get_node_factory_opset4().create("Acosh", as_nodes(node))
@nameable_op
@ -197,7 +197,7 @@ def asinh(node: NodeInput, name: Optional[str] = None) -> Node:
:param name: Optional new name for output node.
:return: New node with arcsinh operation applied on it.
"""
return _get_node_factory_opset4().create("Asinh", [node])
return _get_node_factory_opset4().create("Asinh", as_nodes(node))
@nameable_op
@ -208,7 +208,7 @@ def atanh(node: NodeInput, name: Optional[str] = None) -> Node:
:param name: Optional new name for output node.
:return: New node with arctanh operation applied on it.
"""
return _get_node_factory_opset4().create("Atanh", [node])
return _get_node_factory_opset4().create("Atanh", as_nodes(node))
@nameable_op

View File

@ -44,8 +44,6 @@ xfail_issue_33581 = xfail_test(reason="RuntimeError: nGraph does not support the
"GatherElements")
xfail_issue_35923 = xfail_test(reason="RuntimeError: PReLU without weights is not supported")
xfail_issue_35927 = xfail_test(reason="RuntimeError: B has zero dimension that is not allowable")
xfail_issue_36486 = xfail_test(reason="RuntimeError: HardSigmoid operation should be converted "
"to HardSigmoid_IE")
xfail_issue_38084 = xfail_test(reason="RuntimeError: AssertionFailed: layer->get_output_partial_shape(i)."
"is_static() nGraph <value> operation with name: <value> cannot be "
"converted to <value> layer with name: <value> because output "

View File

@ -6,9 +6,8 @@
import numpy as np
import ngraph as ng
from ngraph.impl import AxisSet, Function, Shape, Type
from ngraph.impl import AxisSet, Shape, Type
from ngraph.impl.op import Constant, Parameter
from tests_compatibility.runtime import get_runtime
def binary_op(op_str, a, b):
@ -81,47 +80,42 @@ def binary_op_ref(op_str, a, b):
return np.power(a, b)
def binary_op_exec(op_str):
def binary_op_exec(op_str, expected_ov_str=None):
if not expected_ov_str:
expected_ov_str = op_str
element_type = Type.f32
shape = Shape([2, 2])
A = Parameter(element_type, shape)
B = Parameter(element_type, shape)
parameter_list = [A, B]
function = Function([binary_op(op_str, A, B)], parameter_list, "test")
a_arr = np.array([[1, 6], [7, 4]], dtype=np.float32)
b_arr = np.array([[5, 2], [3, 8]], dtype=np.float32)
node = binary_op(op_str, A, B)
runtime = get_runtime()
computation = runtime.computation(function, A, B)
result = computation(a_arr, b_arr)[0]
expected = binary_op_ref(op_str, a_arr, b_arr)
assert np.allclose(result, expected)
assert node.get_type_name() == expected_ov_str
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [2, 2]
assert node.get_output_element_type(0) == Type.f32
def binary_op_comparison(op_str):
def binary_op_comparison(op_str, expected_ov_str=None):
if not expected_ov_str:
expected_ov_str = op_str
element_type = Type.f32
shape = Shape([2, 2])
A = Parameter(element_type, shape)
B = Parameter(element_type, shape)
parameter_list = [A, B]
function = Function([binary_op(op_str, A, B)], parameter_list, "test")
a_arr = np.array([[1, 5], [3, 2]], dtype=np.float32)
b_arr = np.array([[2, 4], [3, 1]], dtype=np.float32)
runtime = get_runtime()
computation = runtime.computation(function, A, B)
result = computation(a_arr, b_arr)[0]
node = binary_op(op_str, A, B)
expected = binary_op_ref(op_str, a_arr, b_arr)
assert np.allclose(result, expected)
assert node.get_type_name() == expected_ov_str
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [2, 2]
assert node.get_output_element_type(0) == Type.boolean
def test_add():
binary_op_exec("+")
binary_op_exec("+", "Add")
def test_add_op():
@ -129,27 +123,27 @@ def test_add_op():
def test_sub():
binary_op_exec("-")
binary_op_exec("-", "Subtract")
def test_sub_op():
binary_op_exec("Sub")
binary_op_exec("Sub", "Subtract")
def test_mul():
binary_op_exec("*")
binary_op_exec("*", "Multiply")
def test_mul_op():
binary_op_exec("Mul")
binary_op_exec("Mul", "Multiply")
def test_div():
binary_op_exec("/")
binary_op_exec("/", "Divide")
def test_div_op():
binary_op_exec("Div")
binary_op_exec("Div", "Divide")
def test_maximum():
@ -169,7 +163,7 @@ def test_greater():
def test_greater_eq():
binary_op_comparison("GreaterEq")
binary_op_comparison("GreaterEq", "GreaterEqual")
def test_less():
@ -177,7 +171,7 @@ def test_less():
def test_less_eq():
binary_op_comparison("LessEq")
binary_op_comparison("LessEq", "LessEqual")
def test_not_equal():
@ -191,23 +185,12 @@ def test_add_with_mul():
A = Parameter(element_type, shape)
B = Parameter(element_type, shape)
C = Parameter(element_type, shape)
parameter_list = [A, B, C]
function = Function([ng.multiply(ng.add(A, B), C)], parameter_list, "test")
node = ng.multiply(ng.add(A, B), C)
runtime = get_runtime()
computation = runtime.computation(function, A, B, C)
result = computation(
np.array([1, 2, 3, 4], dtype=np.float32),
np.array([5, 6, 7, 8], dtype=np.float32),
np.array([9, 10, 11, 12], dtype=np.float32),
)[0]
a_arr = np.array([1, 2, 3, 4], dtype=np.float32)
b_arr = np.array([5, 6, 7, 8], dtype=np.float32)
c_arr = np.array([9, 10, 11, 12], dtype=np.float32)
result_arr_ref = (a_arr + b_arr) * c_arr
assert np.allclose(result, result_arr_ref)
assert node.get_type_name() == "Multiply"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [4]
assert node.get_output_element_type(0) == Type.f32
def unary_op(op_str, a):
@ -298,22 +281,21 @@ def unary_op_ref(op_str, a):
return np.tanh(a)
def unary_op_exec(op_str, input_list):
def unary_op_exec(op_str, input_list, expected_ov_str=None):
"""
input_list needs to have deep length of 4
"""
if not expected_ov_str:
expected_ov_str = op_str
element_type = Type.f32
shape = Shape(np.array(input_list).shape)
A = Parameter(element_type, shape)
parameter_list = [A]
function = Function([unary_op(op_str, A)], parameter_list, "test")
node = unary_op(op_str, A)
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(np.array(input_list, dtype=np.float32))[0]
expected = unary_op_ref(op_str, np.array(input_list, dtype=np.float32))
assert np.allclose(result, expected)
assert node.get_type_name() == expected_ov_str
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == list(shape)
assert node.get_output_element_type(0) == Type.f32
def test_abs():
@ -385,19 +367,19 @@ def test_floor():
def test_log():
input_list = [1, 2, 3, 4]
op_str = "log"
unary_op_exec(op_str, input_list)
unary_op_exec(op_str, input_list, "Log")
def test_exp():
input_list = [-1, 0, 1, 2]
op_str = "exp"
unary_op_exec(op_str, input_list)
unary_op_exec(op_str, input_list, "Exp")
def test_negative():
input_list = [-1, 0, 1, 2]
op_str = "negative"
unary_op_exec(op_str, input_list)
unary_op_exec(op_str, input_list, "Negative")
def test_sign():
@ -441,67 +423,42 @@ def test_reshape():
element_type = Type.f32
shape = Shape([2, 3])
A = Parameter(element_type, shape)
parameter_list = [A]
function = Function([ng.reshape(A, Shape([3, 2]), special_zero=False)], parameter_list, "test")
node = ng.reshape(A, Shape([3, 2]), special_zero=False)
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(np.array(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), dtype=np.float32))[0]
expected = np.reshape(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32), (3, 2))
assert np.allclose(result, expected)
assert node.get_type_name() == "Reshape"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [3, 2]
assert node.get_output_element_type(0) == element_type
def test_broadcast():
element_type = Type.f32
A = Parameter(element_type, Shape([3]))
parameter_list = [A]
function = Function([ng.broadcast(A, [3, 3])], parameter_list, "test")
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(np.array([1, 2, 3], dtype=np.float32))[0]
a_arr = np.array([[0], [0], [0]], dtype=np.float32)
b_arr = np.array([[1, 2, 3]], dtype=np.float32)
expected = np.add(a_arr, b_arr)
assert np.allclose(result, expected)
node = ng.broadcast(A, [3, 3])
assert node.get_type_name() == "Broadcast"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [3, 3]
assert node.get_output_element_type(0) == element_type
def test_constant():
element_type = Type.f32
parameter_list = []
function = Function([Constant(element_type, Shape([3, 3]), list(range(9)))], parameter_list, "test")
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation()[0]
expected = np.arange(9).reshape(3, 3)
assert np.allclose(result, expected)
node = Constant(element_type, Shape([3, 3]), list(range(9)))
assert node.get_type_name() == "Constant"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [3, 3]
assert node.get_output_element_type(0) == element_type
def test_concat():
element_type = Type.f32
A = Parameter(element_type, Shape([1, 2]))
B = Parameter(element_type, Shape([1, 2]))
C = Parameter(element_type, Shape([1, 2]))
parameter_list = [A, B, C]
axis = 0
function = Function([ng.concat([A, B, C], axis)], parameter_list, "test")
a_arr = np.array([[1, 2]], dtype=np.float32)
b_arr = np.array([[5, 6]], dtype=np.float32)
c_arr = np.array([[7, 8]], dtype=np.float32)
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(a_arr, b_arr, c_arr)[0]
expected = np.concatenate((a_arr, b_arr, c_arr), axis)
assert np.allclose(result, expected)
node = Constant(element_type, Shape([3, 3]), list(range(9)))
assert node.get_type_name() == "Constant"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [3, 3]
assert node.get_output_element_type(0) == element_type
def test_axisset():
@ -525,31 +482,18 @@ def test_select():
A = Parameter(Type.boolean, Shape([1, 2]))
B = Parameter(element_type, Shape([1, 2]))
C = Parameter(element_type, Shape([1, 2]))
parameter_list = [A, B, C]
function = Function([ng.select(A, B, C)], parameter_list, "test")
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(
np.array([[True, False]], dtype=bool),
np.array([[5, 6]], dtype=np.float32),
np.array([[7, 8]], dtype=np.float32),
)[0]
expected = np.array([[5, 8]])
assert np.allclose(result, expected)
node = ng.select(A, B, C)
assert node.get_type_name() == "Select"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [1, 2]
assert node.get_output_element_type(0) == element_type
def test_max_pool():
# test 1d
def test_max_pool_1d():
element_type = Type.f32
shape = Shape([1, 1, 10])
A = Parameter(element_type, shape)
parameter_list = [A]
input_arr = np.arange(10, dtype=np.float32).reshape([1, 1, 10])
window_shape = [3]
A = Parameter(element_type, shape)
strides = [1] * len(window_shape)
dilations = [1] * len(window_shape)
@ -570,19 +514,26 @@ def test_max_pool():
auto_pad,
idx_elem_type,
)
function = Function([model], parameter_list, "test")
assert model.get_type_name() == "MaxPool"
assert model.get_output_size() == 2
assert list(model.get_output_shape(0)) == [1, 1, 8]
assert list(model.get_output_shape(1)) == [1, 1, 8]
assert model.get_output_element_type(0) == element_type
assert model.get_output_element_type(1) == Type.i32
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(input_arr)[0]
expected = (np.arange(8) + 2).reshape(1, 1, 8)
assert np.allclose(result, expected)
# test 1d with strides
def test_max_pool_1d_with_strides():
element_type = Type.f32
shape = Shape([1, 1, 10])
A = Parameter(element_type, shape)
window_shape = [3]
strides = [2]
pads_begin = [0] * len(window_shape)
dilations = [1] * len(window_shape)
pads_end = [0] * len(window_shape)
rounding_type = "floor"
auto_pad = "explicit"
idx_elem_type = "i32"
model = ng.max_pool(
A,
@ -595,16 +546,15 @@ def test_max_pool():
auto_pad,
idx_elem_type,
)
function = Function([model], parameter_list, "test")
assert model.get_type_name() == "MaxPool"
assert model.get_output_size() == 2
assert list(model.get_output_shape(0)) == [1, 1, 4]
assert list(model.get_output_shape(1)) == [1, 1, 4]
assert model.get_output_element_type(0) == element_type
assert model.get_output_element_type(1) == Type.i32
size = 4
computation = runtime.computation(function, *parameter_list)
result = computation(input_arr)[0]
expected = ((np.arange(size) + 1) * 2).reshape(1, 1, size)
assert np.allclose(result, expected)
# test 2d
def test_max_pool_2d():
element_type = Type.f32
shape = Shape([1, 1, 10, 10])
A = Parameter(element_type, shape)
@ -612,6 +562,9 @@ def test_max_pool():
input_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
window_shape = [3, 3]
rounding_type = "floor"
auto_pad = "explicit"
idx_elem_type = "i32"
strides = [1, 1]
dilations = [1, 1]
@ -629,19 +582,26 @@ def test_max_pool():
auto_pad,
idx_elem_type,
)
function = Function([model], parameter_list, "test")
assert model.get_type_name() == "MaxPool"
assert model.get_output_size() == 2
assert list(model.get_output_shape(0)) == [1, 1, 8, 8]
assert list(model.get_output_shape(1)) == [1, 1, 8, 8]
assert model.get_output_element_type(0) == element_type
assert model.get_output_element_type(1) == Type.i32
computation = runtime.computation(function, *parameter_list)
result = computation(input_arr)[0]
expected = ((np.arange(100).reshape(10, 10))[2:, 2:]).reshape(1, 1, 8, 8)
assert np.allclose(result, expected)
# test 2d with strides
def test_max_pool_2d_with_strides():
element_type = Type.f32
shape = Shape([1, 1, 10, 10])
A = Parameter(element_type, shape)
strides = [2, 2]
dilations = [1, 1]
pads_begin = [0, 0]
pads_end = [0, 0]
window_shape = [3, 3]
rounding_type = "floor"
auto_pad = "explicit"
idx_elem_type = "i32"
model = ng.max_pool(
A,
@ -654,13 +614,12 @@ def test_max_pool():
auto_pad,
idx_elem_type,
)
function = Function([model], parameter_list, "test")
computation = runtime.computation(function, *parameter_list)
result = computation(input_arr)[0]
size = 4
expected = ((np.arange(100).reshape(10, 10))[2::2, 2::2]).reshape(1, 1, size, size)
assert np.allclose(result, expected)
assert model.get_type_name() == "MaxPool"
assert model.get_output_size() == 2
assert list(model.get_output_shape(0)) == [1, 1, 4, 4]
assert list(model.get_output_shape(1)) == [1, 1, 4, 4]
assert model.get_output_element_type(0) == element_type
assert model.get_output_element_type(1) == Type.i32
def convolution2d(
@ -716,9 +675,6 @@ def test_convolution_simple():
filter_shape = Shape([1, 1, 3, 3])
data = Parameter(element_type, image_shape)
filters = Parameter(element_type, filter_shape)
parameter_list = [data, filters]
image_arr = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16)
filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
filter_arr[0][0][0][0] = -1
filter_arr[0][0][1][1] = -1
@ -732,14 +688,10 @@ def test_convolution_simple():
dilations = [1, 1]
model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations)
function = Function([model], parameter_list, "test")
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(image_arr, filter_arr)[0]
expected = convolution2d(image_arr[0][0], filter_arr[0][0]).reshape(1, 1, 14, 14)
assert np.allclose(result, expected)
assert model.get_type_name() == "Convolution"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [1, 1, 14, 14]
assert model.get_output_element_type(0) == element_type
def test_convolution_with_strides():
@ -749,9 +701,6 @@ def test_convolution_with_strides():
filter_shape = Shape([1, 1, 3, 3])
data = Parameter(element_type, image_shape)
filters = Parameter(element_type, filter_shape)
parameter_list = [data, filters]
image_arr = np.arange(100, dtype=np.float32).reshape([1, 1, 10, 10])
filter_arr = np.zeros(9, dtype=np.float32).reshape([1, 1, 3, 3])
filter_arr[0][0][1][1] = 1
strides = [2, 2]
@ -760,14 +709,10 @@ def test_convolution_with_strides():
dilations = [1, 1]
model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations)
function = Function([model], parameter_list, "test")
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(image_arr, filter_arr)[0]
expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides).reshape(1, 1, 4, 4)
assert np.allclose(result, expected)
assert model.get_type_name() == "Convolution"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [1, 1, 4, 4]
assert model.get_output_element_type(0) == element_type
def test_convolution_with_filter_dilation():
@ -777,24 +722,16 @@ def test_convolution_with_filter_dilation():
filter_shape = Shape([1, 1, 3, 3])
data = Parameter(element_type, image_shape)
filters = Parameter(element_type, filter_shape)
parameter_list = [data, filters]
image_arr = np.arange(100, dtype=np.float32).reshape([1, 1, 10, 10])
filter_arr = np.ones(9, dtype=np.float32).reshape([1, 1, 3, 3])
strides = [1, 1]
pads_begin = [0, 0]
pads_end = [0, 0]
dilations = [2, 2]
model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations)
function = Function([model], parameter_list, "test")
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(image_arr, filter_arr)[0]
expected = convolution2d(image_arr[0][0], filter_arr[0][0], strides, dilations).reshape([1, 1, 6, 6])
assert np.allclose(result, expected)
assert model.get_type_name() == "Convolution"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [1, 1, 6, 6]
assert model.get_output_element_type(0) == element_type
def test_convolution_with_padding():
@ -804,9 +741,6 @@ def test_convolution_with_padding():
filter_shape = Shape([1, 1, 3, 3])
data = Parameter(element_type, image_shape)
filters = Parameter(element_type, filter_shape)
parameter_list = [data, filters]
image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
filter_arr = np.zeros(9, dtype=np.float32).reshape(1, 1, 3, 3)
filter_arr[0][0][1][1] = 1
strides = [1, 1]
@ -815,16 +749,10 @@ def test_convolution_with_padding():
pads_end = [0, 0]
model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations)
function = Function([model], parameter_list, "test")
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(image_arr, filter_arr)[0]
expected = convolution2d(
image_arr[0][0], filter_arr[0][0], strides, dilations, pads_begin, pads_end
).reshape([1, 1, 6, 6])
assert np.allclose(result, expected)
assert model.get_type_name() == "Convolution"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [1, 1, 6, 6]
assert model.get_output_element_type(0) == element_type
def test_convolution_with_non_zero_padding():
@ -833,9 +761,6 @@ def test_convolution_with_non_zero_padding():
filter_shape = Shape([1, 1, 3, 3])
data = Parameter(element_type, image_shape)
filters = Parameter(element_type, filter_shape)
parameter_list = [data, filters]
image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1
filter_arr[0][0][1][1] = 1
strides = [1, 1]
@ -844,13 +769,7 @@ def test_convolution_with_non_zero_padding():
pads_end = [1, 2]
model = ng.convolution(data, filters, strides, pads_begin, pads_end, dilations)
function = Function([model], parameter_list, "test")
runtime = get_runtime()
computation = runtime.computation(function, *parameter_list)
result = computation(image_arr, filter_arr)[0]
expected = convolution2d(
image_arr[0][0], filter_arr[0][0], strides, dilations, pads_begin, pads_end
).reshape([1, 1, 9, 9])
assert np.allclose(result, expected)
assert model.get_type_name() == "Convolution"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [1, 1, 9, 9]
assert model.get_output_element_type(0) == element_type

View File

@ -74,7 +74,7 @@ def test_binary_op(ng_api_helper, expected_type):
"ng_api_helper",
[ng.logical_and, ng.logical_or, ng.logical_xor],
)
def test_binary_logical_op(ng_api_helper):
def test_binary_logical_op_parameter_inputs(ng_api_helper):
shape = [2, 2]
parameter_a = ng.parameter(shape, name="A", dtype=bool)
parameter_b = ng.parameter(shape, name="B", dtype=bool)
@ -89,8 +89,8 @@ def test_binary_logical_op(ng_api_helper):
"ng_api_helper",
[ng.logical_and, ng.logical_or, ng.logical_xor],
)
def test_binary_logical_op_with_scalar(ng_api_helper):
value_b = np.array([[False, True], [False, True]], dtype=bool)
def test_binary_logical_numpy_input(ng_api_helper):
value_b = np.array([[False, True], [False, True]], dtype=np.bool)
shape = [2, 2]
parameter_a = ng.parameter(shape, name="A", dtype=bool)
@ -98,6 +98,7 @@ def test_binary_logical_op_with_scalar(ng_api_helper):
model = ng_api_helper(parameter_a, value_b)
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [2, 2]
assert model.get_output_element_type(0) == Type.boolean
@pytest.mark.parametrize(
@ -143,7 +144,7 @@ def test_binary_operators(operator, expected_type):
],
)
def test_binary_operators_with_scalar(operator, expected_type):
value_b = np.array([[5, 6], [7, 8]], dtype=np.float32)
value_b = np.array(3, dtype=np.float32)
shape = [2, 2]
parameter_a = ng.parameter(shape, name="A", dtype=np.float32)

View File

@ -2,52 +2,37 @@
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import pytest
import ngraph as ng
from tests_compatibility.runtime import get_runtime
from tests_compatibility import xfail_issue_36486
from ngraph.impl import Type
def test_elu_operator_with_scalar_and_array():
runtime = get_runtime()
data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32)
data_value = ng.parameter((2, 2), name="data_value", dtype=np.float32)
alpha_value = np.float32(3)
model = ng.elu(data_value, alpha_value)
computation = runtime.computation(model)
result = computation()
expected = np.array([[-2.9797862, 1.0], [-2.5939941, 3.0]], dtype=np.float32)
assert np.allclose(result, expected)
assert model.get_type_name() == "Elu"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [2, 2]
assert model.get_output_element_type(0) == Type.f32
def test_elu_operator_with_scalar():
runtime = get_runtime()
data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32)
parameter_data = ng.parameter([2, 2], name="Data", dtype=np.float32)
alpha_value = np.float32(3)
data_shape = [2, 2]
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.elu(parameter_data, alpha_value)
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.array([[-2.9797862, 1.0], [-2.5939941, 3.0]], dtype=np.float32)
assert np.allclose(result, expected)
assert model.get_type_name() == "Elu"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [2, 2]
assert model.get_output_element_type(0) == Type.f32
def test_fake_quantize():
runtime = get_runtime()
data_value = np.arange(24.0, dtype=np.float32).reshape(1, 2, 3, 4)
input_low_value = np.float32(0)
input_high_value = np.float32(23)
output_low_value = np.float32(2)
output_high_value = np.float32(16)
levels = np.float32(4)
data_shape = [1, 2, 3, 4]
@ -66,74 +51,29 @@ def test_fake_quantize():
parameter_output_high,
levels,
)
computation = runtime.computation(
model,
parameter_data,
parameter_input_low,
parameter_input_high,
parameter_output_low,
parameter_output_high,
)
result = computation(data_value, input_low_value, input_high_value, output_low_value, output_high_value)
expected = np.array(
[
[
[
[
[2.0, 2.0, 2.0, 2.0],
[6.6666669, 6.6666669, 6.6666669, 6.6666669],
[6.6666669, 6.6666669, 6.6666669, 6.6666669],
],
[
[11.33333301, 11.33333301, 11.33333301, 11.33333301],
[11.33333301, 11.33333301, 11.33333301, 11.33333301],
[16.0, 16.0, 16.0, 16.0],
],
]
]
],
dtype=np.float32,
)
assert np.allclose(result, expected)
assert model.get_type_name() == "FakeQuantize"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [1, 2, 3, 4]
assert model.get_output_element_type(0) == Type.f32
def test_depth_to_space():
runtime = get_runtime()
data_value = np.array(
[
[
[[0, 1, 2], [3, 4, 5]],
[[6, 7, 8], [9, 10, 11]],
[[12, 13, 14], [15, 16, 17]],
[[18, 19, 20], [21, 22, 23]],
]
],
dtype=np.float32,
)
data_shape = [1, 4, 2, 3]
mode = "blocks_first"
block_size = np.float32(2)
data_shape = [1, 4, 2, 3]
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.depth_to_space(parameter_data, mode, block_size)
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.array(
[[[[0, 6, 1, 7, 2, 8], [12, 18, 13, 19, 14, 20], [3, 9, 4, 10, 5, 11], [15, 21, 16, 22, 17, 23]]]],
dtype=np.float32,
)
assert np.allclose(result, expected)
assert model.get_type_name() == "DepthToSpace"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [1, 1, 4, 6]
assert model.get_output_element_type(0) == Type.f32
def test_space_to_batch():
runtime = get_runtime()
data_value = np.array([[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]], dtype=np.float32)
data_shape = [1, 2, 2, 3]
data_shape = data_value.shape
block_shape = np.array([1, 2, 3, 2], dtype=np.int64)
@ -143,50 +83,14 @@ def test_space_to_batch():
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.space_to_batch(parameter_data, block_shape, pads_begin, pads_end)
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.array(
[
[[[0, 0]]],
[[[0, 0]]],
[[[0, 2]]],
[[[1, 0]]],
[[[3, 5]]],
[[[4, 0]]],
[[[0, 0]]],
[[[0, 0]]],
[[[6, 8]]],
[[[7, 0]]],
[[[9, 11]]],
[[[10, 0]]],
],
dtype=np.float32,
)
assert np.allclose(result, expected)
assert model.get_type_name() == "SpaceToBatch"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [12, 1, 1, 2]
assert model.get_output_element_type(0) == Type.f32
def test_batch_to_space():
runtime = get_runtime()
data = np.array(
[
[[[0, 0]]],
[[[0, 0]]],
[[[0, 2]]],
[[[1, 0]]],
[[[3, 5]]],
[[[4, 0]]],
[[[0, 0]]],
[[[0, 0]]],
[[[6, 8]]],
[[[7, 0]]],
[[[9, 11]]],
[[[10, 0]]],
],
dtype=np.float32,
)
data_shape = data.shape
data_shape = [12, 1, 1, 2]
block_shape = np.array([1, 2, 3, 2], dtype=np.int64)
crops_begin = np.array([0, 0, 1, 0], dtype=np.int64)
@ -195,142 +99,89 @@ def test_batch_to_space():
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.batch_to_space(parameter_data, block_shape, crops_begin, crops_end)
computation = runtime.computation(model, parameter_data)
result = computation(data)
expected = np.array([[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]], dtype=np.float32)
assert np.allclose(result, expected)
assert model.get_type_name() == "BatchToSpace"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [1, 2, 2, 3]
assert model.get_output_element_type(0) == Type.f32
def test_clamp_operator():
runtime = get_runtime()
data_shape = [2, 2]
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
min_value = np.float32(3)
max_value = np.float32(12)
model = ng.clamp(parameter_data, min_value, max_value)
computation = runtime.computation(model, parameter_data)
data_value = np.array([[-5, 9], [45, 3]], dtype=np.float32)
result = computation(data_value)
expected = np.clip(data_value, min_value, max_value)
assert np.allclose(result, expected)
assert model.get_type_name() == "Clamp"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [2, 2]
assert model.get_output_element_type(0) == Type.f32
def test_clamp_operator_with_array():
runtime = get_runtime()
data_value = np.array([[-5, 9], [45, 3]], dtype=np.float32)
min_value = np.float32(3)
max_value = np.float32(12)
model = ng.clamp(data_value, min_value, max_value)
computation = runtime.computation(model)
result = computation()
expected = np.clip(data_value, min_value, max_value)
assert np.allclose(result, expected)
assert model.get_type_name() == "Clamp"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [2, 2]
assert model.get_output_element_type(0) == Type.f32
def test_squeeze_operator():
runtime = get_runtime()
data_shape = [1, 2, 1, 3, 1, 1]
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
data_value = np.arange(6.0, dtype=np.float32).reshape([1, 2, 1, 3, 1, 1])
axes = [2, 4]
model = ng.squeeze(parameter_data, axes)
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.arange(6.0, dtype=np.float32).reshape([1, 2, 3, 1])
assert np.allclose(result, expected)
assert model.get_type_name() == "Squeeze"
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == [1, 2, 3, 1]
assert model.get_output_element_type(0) == Type.f32
def test_squared_difference_operator():
runtime = get_runtime()
x1_shape = [1, 2, 3, 4]
x2_shape = [2, 3, 4]
parameter_x1 = ng.parameter(x1_shape, name="x1", dtype=np.float32)
parameter_x2 = ng.parameter(x2_shape, name="x2", dtype=np.float32)
x1_value = np.arange(24.0, dtype=np.float32).reshape(x1_shape)
x2_value = np.arange(start=4.0, stop=28.0, step=1.0, dtype=np.float32).reshape(x2_shape)
model = ng.squared_difference(parameter_x1, parameter_x2)
computation = runtime.computation(model, parameter_x1, parameter_x2)
result = computation(x1_value, x2_value)
expected = np.square(np.subtract(x1_value, x2_value))
assert np.allclose(result, expected)
assert model.get_type_name() == "SquaredDifference"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [1, 2, 3, 4]
def test_shuffle_channels_operator():
runtime = get_runtime()
data_shape = [1, 15, 2, 2]
axis = 1
groups = 5
parameter = ng.parameter(data_shape, name="Data", dtype=np.float32)
data_value = np.arange(60.0, dtype=np.float32).reshape(data_shape)
model = ng.shuffle_channels(parameter, axis, groups)
computation = runtime.computation(model, parameter)
result = computation(data_value)
expected = np.array(
[
[
[[0.0, 1.0], [2.0, 3.0]],
[[12.0, 13.0], [14.0, 15.0]],
[[24.0, 25.0], [26.0, 27.0]],
[[36.0, 37.0], [38.0, 39.0]],
[[48.0, 49.0], [50.0, 51.0]],
[[4.0, 5.0], [6.0, 7.0]],
[[16.0, 17.0], [18.0, 19.0]],
[[28.0, 29.0], [30.0, 31.0]],
[[40.0, 41.0], [42.0, 43.0]],
[[52.0, 53.0], [54.0, 55.0]],
[[8.0, 9.0], [10.0, 11.0]],
[[20.0, 21.0], [22.0, 23.0]],
[[32.0, 33.0], [34.0, 35.0]],
[[44.0, 45.0], [46.0, 47.0]],
[[56.0, 57.0], [58.0, 59.0]],
]
],
dtype=np.float32,
)
assert np.allclose(result, expected)
assert model.get_type_name() == "ShuffleChannels"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [1, 15, 2, 2]
def test_unsqueeze():
runtime = get_runtime()
data_shape = [3, 4, 5]
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
data_value = np.arange(60.0, dtype=np.float32).reshape(3, 4, 5)
axes = [0, 4]
model = ng.unsqueeze(parameter_data, axes)
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.arange(60.0, dtype=np.float32).reshape([1, 3, 4, 5, 1])
assert np.allclose(result, expected)
assert model.get_type_name() == "Unsqueeze"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [1, 3, 4, 5, 1]
def test_grn_operator():
runtime = get_runtime()
data_value = np.arange(start=1.0, stop=25.0, dtype=np.float32).reshape([1, 2, 3, 4])
bias = np.float32(1e-6)
data_shape = [1, 2, 3, 4]
@ -338,202 +189,82 @@ def test_grn_operator():
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.grn(parameter_data, bias)
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.array(
[
[
[
[0.0766965, 0.14142136, 0.19611613, 0.24253564],
[0.28216633, 0.31622776, 0.34570536, 0.37139067],
[0.39391932, 0.41380295, 0.4314555, 0.4472136],
],
[
[0.9970545, 0.98994946, 0.9805807, 0.97014254],
[0.9593655, 0.9486833, 0.9383431, 0.9284767],
[0.91914505, 0.9103665, 0.9021342, 0.8944272],
],
]
],
dtype=np.float32,
)
assert np.allclose(result, expected)
assert model.get_type_name() == "GRN"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == data_shape
def test_prelu_operator():
runtime = get_runtime()
data_shape = [1, 2, 3, 4]
slope_shape = [2, 3, 1]
data_value = np.arange(start=1.0, stop=25.0, dtype=np.float32).reshape(data_shape)
slope_value = np.arange(start=-10.0, stop=-4.0, dtype=np.float32).reshape(slope_shape)
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
parameter_slope = ng.parameter(slope_shape, name="Slope", dtype=np.float32)
model = ng.prelu(parameter_data, parameter_slope)
computation = runtime.computation(model, parameter_data, parameter_slope)
result = computation(data_value, slope_value)
expected = np.clip(data_value, 0, np.inf) + np.clip(data_value, -np.inf, 0) * slope_value
assert np.allclose(result, expected)
assert model.get_type_name() == "PRelu"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [1, 2, 3, 4]
def test_selu_operator():
runtime = get_runtime()
data_shape = [4, 2, 3, 1]
data = np.arange(start=1.0, stop=25.0, dtype=np.float32).reshape(data_shape)
alpha = np.array(1.6733, dtype=np.float32)
lambda_value = np.array(1.0507, dtype=np.float32)
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.selu(parameter_data, alpha, lambda_value)
computation = runtime.computation(model, parameter_data)
result = computation(data)
expected = lambda_value * ((data > 0) * data + (data <= 0) * (alpha * np.exp(data) - alpha))
assert np.allclose(result, expected)
assert model.get_type_name() == "Selu"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [4, 2, 3, 1]
@xfail_issue_36486
def test_hard_sigmoid_operator():
runtime = get_runtime()
data_shape = [3]
alpha_value = np.float32(0.5)
beta_value = np.float32(0.6)
data_value = np.array([-1, 0, 1], dtype=np.float32)
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
parameter_alpha = ng.parameter([], name="Alpha", dtype=np.float32)
parameter_beta = ng.parameter([], name="Beta", dtype=np.float32)
model = ng.hard_sigmoid(parameter_data, parameter_alpha, parameter_beta)
computation = runtime.computation(model, parameter_data, parameter_alpha, parameter_beta)
result = computation(data_value, alpha_value, beta_value)
expected = [0.1, 0.6, 1.0]
assert np.allclose(result, expected)
assert model.get_type_name() == "HardSigmoid"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [3]
def test_mvn_operator():
runtime = get_runtime()
data_shape = [3, 3, 3, 1]
axes = [0, 2, 3]
normalize_variance = True
eps = np.float32(1e-9)
eps_mode = "outside_sqrt"
data_value = np.array(
[
[
[[0.8439683], [0.5665144], [0.05836735]],
[[0.02916367], [0.12964272], [0.5060197]],
[[0.79538304], [0.9411346], [0.9546573]],
],
[
[[0.17730942], [0.46192095], [0.26480448]],
[[0.6746842], [0.01665257], [0.62473077]],
[[0.9240844], [0.9722341], [0.11965699]],
],
[
[[0.41356155], [0.9129373], [0.59330076]],
[[0.81929934], [0.7862604], [0.11799799]],
[[0.69248444], [0.54119414], [0.07513223]],
],
],
dtype=np.float32,
)
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.mvn(parameter_data, axes, normalize_variance, eps, eps_mode)
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.array(
[
[
[[1.3546423], [0.33053496], [-1.5450814]],
[[-1.2106764], [-0.8925952], [0.29888135]],
[[0.38083088], [0.81808794], [0.85865635]],
],
[
[[-1.1060555], [-0.05552877], [-0.78310335]],
[[0.83281356], [-1.250282], [0.67467856]],
[[0.7669372], [0.9113869], [-1.6463585]],
],
[
[[-0.23402764], [1.6092131], [0.42940593]],
[[1.2906139], [1.1860244], [-0.92945826]],
[[0.0721334], [-0.38174], [-1.7799333]],
],
],
dtype=np.float32,
)
assert np.allclose(result, expected)
assert model.get_type_name() == "MVN"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == data_shape
def test_space_to_depth_operator():
runtime = get_runtime()
data_shape = [1, 2, 4, 4]
data_value = np.arange(start=0, stop=32, step=1.0, dtype=np.float32).reshape(data_shape)
mode = "blocks_first"
block_size = 2
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.space_to_depth(parameter_data, mode, block_size)
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.array(
[
0,
2,
8,
10,
16,
18,
24,
26,
1,
3,
9,
11,
17,
19,
25,
27,
4,
6,
12,
14,
20,
22,
28,
30,
5,
7,
13,
15,
21,
23,
29,
31,
],
dtype=np.float32,
).reshape(1, 8, 2, 2)
assert np.allclose(result, expected)
assert model.get_type_name() == "SpaceToDepth"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [1, 8, 2, 2]
batch_size = 2
input_size = 3
@ -551,41 +282,6 @@ def test_space_to_depth_operator():
parameter_R = ng.parameter(R_shape, name="R", dtype=np.float32)
parameter_B = ng.parameter(B_shape, name="B", dtype=np.float32)
X_value = np.array(
[0.3432185, 0.612268, 0.20272376, 0.9513413, 0.30585995, 0.7265472], dtype=np.float32
).reshape(X_shape)
H_t_value = np.array(
[0.12444675, 0.52055854, 0.46489045, 0.4983964, 0.7730452, 0.28439692], dtype=np.float32
).reshape(H_t_shape)
W_value = np.array(
[
0.41930267,
0.7872176,
0.89940447,
0.23659843,
0.24676207,
0.17101714,
0.3147149,
0.6555601,
0.4559603,
],
dtype=np.float32,
).reshape(W_shape)
R_value = np.array(
[
0.8374871,
0.86660194,
0.82114047,
0.71549815,
0.18775631,
0.3182116,
0.25392973,
0.38301638,
0.85531586,
],
dtype=np.float32,
).reshape(R_shape)
B_value = np.array([1.0289404, 1.6362579, 0.4370661], dtype=np.float32).reshape(B_shape)
activations = ["sigmoid"]
activation_alpha = []
activation_beta = []
@ -603,47 +299,32 @@ def test_space_to_depth_operator():
activation_beta,
clip,
)
computation = runtime.computation(
model, parameter_X, parameter_H_t, parameter_W, parameter_R, parameter_B
)
result = computation(X_value, H_t_value, W_value, R_value, B_value)
expected = np.array(
[0.94126844, 0.9036043, 0.841243, 0.9468489, 0.934215, 0.873708], dtype=np.float32
).reshape(batch_size, hidden_size)
assert np.allclose(result, expected)
assert model.get_type_name() == "RNNCell"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [batch_size, hidden_size]
def test_group_convolution_operator():
runtime = get_runtime()
data_shape = [1, 4, 2, 2]
filters_shape = [2, 1, 2, 1, 1]
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
parameter_filters = ng.parameter(filters_shape, name="Filters", dtype=np.float32)
data_value = np.arange(start=1.0, stop=17.0, dtype=np.float32).reshape(data_shape)
filters_value = np.arange(start=1.0, stop=5.0, dtype=np.float32).reshape(filters_shape)
strides = [1, 1]
dilations = [1, 1]
pads_begin = [0, 0]
pads_end = [0, 0]
model = ng.group_convolution(parameter_data, parameter_filters, strides, pads_begin, pads_end, dilations)
computation = runtime.computation(model, parameter_data, parameter_filters)
result = computation(data_value, filters_value)
expected = np.array([11, 14, 17, 20, 79, 86, 93, 100], dtype=np.float32).reshape(1, 2, 2, 2)
assert np.allclose(result, expected)
assert model.get_type_name() == "GroupConvolution"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [1, 2, 2, 2]
@pytest.mark.xfail(reason="Computation mismatch")
def test_group_convolution_backprop_data():
runtime = get_runtime()
data_shape = [1, 1, 3, 3]
filters_shape = [1, 1, 1, 3, 3]
strides = [2, 2]
@ -657,87 +338,13 @@ def test_group_convolution_backprop_data():
data_node, filters_node, strides, None, pads_begin, pads_end, output_padding=output_padding
)
data_value = np.array(
[
0.16857791,
-0.15161794,
0.08540368,
0.1820628,
-0.21746576,
0.08245695,
0.1431433,
-0.43156421,
0.30591947,
],
dtype=np.float32,
).reshape(data_shape)
filters_value = np.array(
[
-0.06230065,
0.37932432,
-0.25388849,
0.33878803,
0.43709868,
-0.22477469,
0.04118127,
-0.44696793,
0.06373066,
],
dtype=np.float32,
).reshape(filters_shape)
computation = runtime.computation(model, data_node, filters_node)
result = computation(data_value, filters_value)
expected = np.array(
[
0.07368518,
-0.08925839,
-0.06627201,
0.06301362,
0.03732984,
-0.01919658,
-0.00628807,
-0.02817563,
-0.01472169,
0.04392925,
-0.00689478,
-0.01549204,
0.07957941,
-0.11459791,
-0.09505399,
0.07681622,
0.03604182,
-0.01853423,
-0.0270785,
-0.00680824,
-0.06650258,
0.08004665,
0.07918708,
0.0724144,
0.06256775,
-0.17838378,
-0.18863615,
0.20064656,
0.133717,
-0.06876295,
-0.06398046,
-0.00864975,
0.19289537,
-0.01490572,
-0.13673618,
0.01949645,
],
dtype=np.float32,
).reshape(1, 1, 6, 6)
assert np.allclose(result, expected)
assert model.get_type_name() == "GroupConvolutionBackpropData"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [1, 1, 6, 6]
def test_group_convolution_backprop_data_output_shape():
runtime = get_runtime()
data_shape = [1, 1, 1, 10]
filters_shape = [1, 1, 1, 1, 5]
strides = [1, 1]
@ -749,18 +356,7 @@ def test_group_convolution_backprop_data_output_shape():
model = ng.group_convolution_backprop_data(
data_node, filters_node, strides, output_shape_node, auto_pad="same_upper"
)
data_value = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=np.float32).reshape(
data_shape
)
filters_value = np.array([1.0, 2.0, 3.0, 2.0, 1.0], dtype=np.float32).reshape(filters_shape)
computation = runtime.computation(model, data_node, filters_node)
result = computation(data_value, filters_value)
expected = np.array(
[0.0, 1.0, 4.0, 10.0, 18.0, 27.0, 36.0, 45.0, 54.0, 63.0, 62.0, 50.0, 26.0, 9.0], dtype=np.float32,
).reshape(1, 1, 1, 14)
assert np.allclose(result, expected)
assert model.get_type_name() == "GroupConvolutionBackpropData"
assert model.get_output_size() == 1
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [1, 1, 1, 14]

View File

@ -5,36 +5,33 @@ import numpy as np
import pytest
import ngraph as ng
from tests_compatibility.test_ngraph.util import run_op_node
from ngraph.impl import Type
@pytest.mark.parametrize(
"shape_a, shape_b, transpose_a, transpose_b",
("shape_a", "shape_b", "transpose_a", "transpose_b", "expected_shape"),
[
# matrix, vector
([2, 4], [4], False, False),
([4], [4, 2], False, False),
([2, 4], [4], False, False, [2]),
([4], [4, 2], False, False, [2]),
# matrix, matrix
([2, 4], [4, 2], False, False),
([2, 4], [4, 2], False, False, [2, 2]),
# tensor, vector
([2, 4, 5], [5], False, False),
([2, 4, 5], [5], False, False, [2, 4]),
# # tensor, matrix
([2, 4, 5], [5, 4], False, False),
([2, 4, 5], [5, 4], False, False, [2, 4, 4]),
# # tensor, tensor
([2, 2, 4], [2, 4, 2], False, False),
([2, 2, 4], [2, 4, 2], False, False, [2, 2, 2]),
],
)
def test_matmul(shape_a, shape_b, transpose_a, transpose_b):
def test_matmul(shape_a, shape_b, transpose_a, transpose_b, expected_shape):
np.random.seed(133391)
left_input = -100.0 + np.random.rand(*shape_a).astype(np.float32) * 200.0
right_input = -100.0 + np.random.rand(*shape_b).astype(np.float32) * 200.0
left_input = np.random.rand(*shape_a).astype(np.float32)
right_input = np.random.rand(*shape_b).astype(np.float32)
result = run_op_node([left_input, right_input], ng.matmul, transpose_a, transpose_b)
node = ng.matmul(left_input, right_input, transpose_a, transpose_b)
if transpose_a:
left_input = np.transpose(left_input)
if transpose_b:
right_input = np.transpose(right_input)
expected = np.matmul(left_input, right_input)
assert np.allclose(result, expected)
assert node.get_output_size() == 1
assert node.get_type_name() == "MatMul"
assert list(node.get_output_shape(0)) == expected_shape
assert node.get_output_element_type(0) == Type.f32

View File

@ -4,33 +4,34 @@
import numpy as np
import ngraph as ng
from tests_compatibility.runtime import get_runtime
from ngraph.impl import Type
def test_split():
runtime = get_runtime()
input_tensor = ng.constant(np.array([0, 1, 2, 3, 4, 5], dtype=np.int32))
axis = ng.constant(0, dtype=np.int64)
splits = 3
split_node = ng.split(input_tensor, axis, splits)
computation = runtime.computation(split_node)
split_results = computation()
expected_results = np.array([[0, 1], [2, 3], [4, 5]], dtype=np.int32)
assert np.allclose(split_results, expected_results)
assert split_node.get_type_name() == "Split"
assert split_node.get_output_size() == 3
assert list(split_node.get_output_shape(0)) == [2]
assert list(split_node.get_output_shape(1)) == [2]
assert list(split_node.get_output_shape(2)) == [2]
assert split_node.get_output_element_type(0) == Type.i32
assert split_node.get_output_element_type(1) == Type.i32
assert split_node.get_output_element_type(2) == Type.i32
def test_variadic_split():
runtime = get_runtime()
input_tensor = ng.constant(np.array([[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]], dtype=np.int32))
axis = ng.constant(1, dtype=np.int64)
splits = ng.constant(np.array([2, 4], dtype=np.int64))
v_split_node = ng.variadic_split(input_tensor, axis, splits)
computation = runtime.computation(v_split_node)
results = computation()
split0 = np.array([[0, 1], [6, 7]], dtype=np.int32)
split1 = np.array([[2, 3, 4, 5], [8, 9, 10, 11]], dtype=np.int32)
assert np.allclose(results[0], split0)
assert np.allclose(results[1], split1)
assert v_split_node.get_type_name() == "VariadicSplit"
assert v_split_node.get_output_size() == 2
assert list(v_split_node.get_output_shape(0)) == [2, 2]
assert list(v_split_node.get_output_shape(1)) == [2, 4]
assert v_split_node.get_output_element_type(0) == Type.i32
assert v_split_node.get_output_element_type(1) == Type.i32

View File

@ -1,36 +1,37 @@
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import ngraph as ng
import numpy as np
import pytest
from tests_compatibility.runtime import get_runtime
from tests_compatibility.test_ngraph.util import run_op_node, run_op_numeric_data
import ngraph as ng
from ngraph.impl import Type
from ngraph.utils.types import get_element_type
def test_concat():
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
axis = 0
expected = np.concatenate((a, b), axis=0)
runtime = get_runtime()
parameter_a = ng.parameter(list(a.shape), name="A", dtype=np.float32)
parameter_b = ng.parameter(list(b.shape), name="B", dtype=np.float32)
node = ng.concat([parameter_a, parameter_b], axis)
computation = runtime.computation(node, parameter_a, parameter_b)
result = computation(a, b)
assert np.allclose(result, expected)
assert node.get_type_name() == "Concat"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [3, 2]
assert node.get_output_element_type(0) == Type.f32
@pytest.mark.parametrize(
"val_type, value", [(bool, False), (bool, np.empty((2, 2), dtype=bool))]
("val_type", "value", "output_shape"), [(bool, False, []), (bool, np.empty((2, 2), dtype=bool), [2, 2])]
)
def test_constant_from_bool(val_type, value):
expected = np.array(value, dtype=val_type)
result = run_op_numeric_data(value, ng.constant, val_type)
assert np.allclose(result, expected)
def test_constant_from_bool(val_type, value, output_shape):
node = ng.constant(value, val_type)
assert node.get_type_name() == "Constant"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == Type.boolean
assert list(node.get_output_shape(0)) == output_shape
@pytest.mark.parametrize(
@ -49,9 +50,11 @@ def test_constant_from_bool(val_type, value):
],
)
def test_constant_from_scalar(val_type, value):
expected = np.array(value, dtype=val_type)
result = run_op_numeric_data(value, ng.constant, val_type)
assert np.allclose(result, expected)
node = ng.constant(value, val_type)
assert node.get_type_name() == "Constant"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == get_element_type(val_type)
assert list(node.get_output_shape(0)) == []
@pytest.mark.parametrize(
@ -64,8 +67,11 @@ def test_constant_from_scalar(val_type, value):
def test_constant_from_float_array(val_type):
np.random.seed(133391)
input_data = np.array(-1 + np.random.rand(2, 3, 4) * 2, dtype=val_type)
result = run_op_numeric_data(input_data, ng.constant, val_type)
assert np.allclose(result, input_data)
node = ng.constant(input_data, val_type)
assert node.get_type_name() == "Constant"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == get_element_type(val_type)
assert list(node.get_output_shape(0)) == [2, 3, 4]
@pytest.mark.parametrize(
@ -86,8 +92,11 @@ def test_constant_from_integer_array(val_type, range_start, range_end):
input_data = np.array(
np.random.randint(range_start, range_end, size=(2, 2)), dtype=val_type
)
result = run_op_numeric_data(input_data, ng.constant, val_type)
assert np.allclose(result, input_data)
node = ng.constant(input_data, val_type)
assert node.get_type_name() == "Constant"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == get_element_type(val_type)
assert list(node.get_output_shape(0)) == [2, 2]
def test_broadcast_numpy():
@ -126,27 +135,25 @@ def test_transpose():
)
input_order = np.array([0, 2, 3, 1], dtype=np.int32)
result = run_op_node([input_tensor], ng.transpose, input_order)
expected = np.transpose(input_tensor, input_order)
assert np.allclose(result, expected)
node = ng.transpose(input_tensor, input_order)
assert node.get_type_name() == "Transpose"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == Type.i32
assert list(node.get_output_shape(0)) == [3, 224, 224, 3]
def test_tile():
input_tensor = np.arange(6, dtype=np.int32).reshape((2, 1, 3))
repeats = np.array([2, 1], dtype=np.int32)
result = run_op_node([input_tensor], ng.tile, repeats)
node = ng.tile(input_tensor, repeats)
expected = np.array([0, 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5]).reshape((2, 2, 3))
assert np.allclose(result, expected)
assert node.get_type_name() == "Tile"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == Type.i32
assert list(node.get_output_shape(0)) == [2, 2, 3]
@pytest.mark.xfail(
reason="RuntimeError: Check 'shape_size(get_input_shape(0)) == shape_size(output_shape)'"
)
def test_strided_slice():
input_tensor = np.arange(2 * 3 * 4, dtype=np.float32).reshape((2, 3, 4))
begin = np.array([1, 0], dtype=np.int32)
@ -158,9 +165,8 @@ def test_strided_slice():
shrink_axis_mask = np.array([1, 0, 0], dtype=np.int32)
ellipsis_mask = np.array([0, 0, 0], dtype=np.int32)
result = run_op_node(
[input_tensor],
ng.strided_slice,
node = ng.strided_slice(
input_tensor,
begin,
end,
strides,
@ -171,11 +177,10 @@ def test_strided_slice():
ellipsis_mask,
)
expected = np.array(
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], dtype=np.float32
).reshape((1, 3, 4))
assert np.allclose(result, expected)
assert node.get_type_name() == "StridedSlice"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == Type.f32
assert list(node.get_output_shape(0)) == [1, 3, 4]
def test_reshape_v1():
@ -183,16 +188,18 @@ def test_reshape_v1():
shape = np.array([0, -1, 4], dtype=np.int32)
special_zero = True
expected_shape = np.array([2, 150, 4])
expected = np.reshape(A, expected_shape)
result = run_op_node([A], ng.reshape, shape, special_zero)
assert np.allclose(result, expected)
node = ng.reshape(A, shape, special_zero)
assert node.get_type_name() == "Reshape"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == Type.f32
assert list(node.get_output_shape(0)) == [2, 150, 4]
def test_shape_of():
input_tensor = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32)
result = run_op_node([input_tensor], ng.shape_of)
assert np.allclose(result, [3, 3])
node = ng.shape_of(input_tensor)
assert node.get_type_name() == "ShapeOf"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == Type.i64
assert list(node.get_output_shape(0)) == [2]

View File

@ -6,129 +6,118 @@ import pytest
import ngraph as ng
from ngraph.impl import Shape, Type
from tests_compatibility.runtime import get_runtime
from tests_compatibility.test_ngraph.util import run_op_node
R_TOLERANCE = 1e-6 # global relative tolerance
@pytest.mark.parametrize(
"graph_api_fn, numpy_fn, range_start, range_end",
("graph_api_fn", "type_name"),
[
(ng.absolute, np.abs, -1, 1),
(ng.abs, np.abs, -1, 1),
(ng.acos, np.arccos, -1, 1),
(ng.acosh, np.arccosh, 1, 2),
(ng.asin, np.arcsin, -1, 1),
(ng.asinh, np.arcsinh, -1, 1),
(ng.atan, np.arctan, -100.0, 100.0),
(ng.atanh, np.arctanh, 0.0, 1.0),
(ng.ceiling, np.ceil, -100.0, 100.0),
(ng.ceil, np.ceil, -100.0, 100.0),
(ng.cos, np.cos, -100.0, 100.0),
(ng.cosh, np.cosh, -100.0, 100.0),
(ng.exp, np.exp, -100.0, 100.0),
(ng.floor, np.floor, -100.0, 100.0),
(ng.log, np.log, 0, 100.0),
(ng.relu, lambda x: np.maximum(0, x), -100.0, 100.0),
(ng.sign, np.sign, -100.0, 100.0),
(ng.sin, np.sin, -100.0, 100.0),
(ng.sinh, np.sinh, -100.0, 100.0),
(ng.sqrt, np.sqrt, 0.0, 100.0),
(ng.tan, np.tan, -1.0, 1.0),
(ng.tanh, np.tanh, -100.0, 100.0),
(ng.absolute, "Abs"),
(ng.abs, "Abs"),
(ng.acos, "Acos"),
(ng.acosh, "Acosh"),
(ng.asin, "Asin"),
(ng.asinh, "Asinh"),
(ng.atan, "Atan"),
(ng.atanh, "Atanh"),
(ng.ceiling, "Ceiling"),
(ng.ceil, "Ceiling"),
(ng.cos, "Cos"),
(ng.cosh, "Cosh"),
(ng.exp, "Exp"),
(ng.floor, "Floor"),
(ng.log, "Log"),
(ng.relu, "Relu"),
(ng.sign, "Sign"),
(ng.sin, "Sin"),
(ng.sinh, "Sinh"),
(ng.sqrt, "Sqrt"),
(ng.tan, "Tan"),
(ng.tanh, "Tanh"),
],
)
def test_unary_op_array(graph_api_fn, numpy_fn, range_start, range_end):
def test_unary_op_array(graph_api_fn, type_name):
np.random.seed(133391)
input_data = (range_start + np.random.rand(2, 3, 4) * (range_end - range_start)).astype(np.float32)
expected = numpy_fn(input_data)
result = run_op_node([input_data], graph_api_fn)
assert np.allclose(result, expected, rtol=0.001)
input_data = np.random.rand(2, 3, 4).astype(np.float32)
node = graph_api_fn(input_data)
assert node.get_output_size() == 1
assert node.get_type_name() == type_name
assert node.get_output_element_type(0) == Type.f32
assert list(node.get_output_shape(0)) == [2, 3, 4]
@pytest.mark.parametrize(
"graph_api_fn, numpy_fn, input_data",
("graph_api_fn", "input_data"),
[
pytest.param(ng.absolute, np.abs, np.float32(-3)),
pytest.param(ng.abs, np.abs, np.float32(-3)),
pytest.param(ng.acos, np.arccos, np.float32(-0.5)),
pytest.param(ng.asin, np.arcsin, np.float32(-0.5)),
pytest.param(ng.atan, np.arctan, np.float32(-0.5)),
pytest.param(ng.ceiling, np.ceil, np.float32(1.5)),
pytest.param(ng.ceil, np.ceil, np.float32(1.5)),
pytest.param(ng.cos, np.cos, np.float32(np.pi / 4.0)),
pytest.param(ng.cosh, np.cosh, np.float32(np.pi / 4.0)),
pytest.param(ng.exp, np.exp, np.float32(1.5)),
pytest.param(ng.floor, np.floor, np.float32(1.5)),
pytest.param(ng.log, np.log, np.float32(1.5)),
pytest.param(ng.relu, lambda x: np.maximum(0, x), np.float32(-0.125)),
pytest.param(ng.sign, np.sign, np.float32(0.0)),
pytest.param(ng.sin, np.sin, np.float32(np.pi / 4.0)),
pytest.param(ng.sinh, np.sinh, np.float32(0.0)),
pytest.param(ng.sqrt, np.sqrt, np.float32(3.5)),
pytest.param(ng.tan, np.tan, np.float32(np.pi / 4.0)),
pytest.param(ng.tanh, np.tanh, np.float32(0.1234)),
pytest.param(ng.absolute, np.float32(-3)),
pytest.param(ng.abs, np.float32(-3)),
pytest.param(ng.acos, np.float32(-0.5)),
pytest.param(ng.asin, np.float32(-0.5)),
pytest.param(ng.atan, np.float32(-0.5)),
pytest.param(ng.ceiling, np.float32(1.5)),
pytest.param(ng.ceil, np.float32(1.5)),
pytest.param(ng.cos, np.float32(np.pi / 4.0)),
pytest.param(ng.cosh, np.float32(np.pi / 4.0)),
pytest.param(ng.exp, np.float32(1.5)),
pytest.param(ng.floor, np.float32(1.5)),
pytest.param(ng.log, np.float32(1.5)),
pytest.param(ng.relu, np.float32(-0.125)),
pytest.param(ng.sign, np.float32(0.0)),
pytest.param(ng.sin, np.float32(np.pi / 4.0)),
pytest.param(ng.sinh, np.float32(0.0)),
pytest.param(ng.sqrt, np.float32(3.5)),
pytest.param(ng.tan, np.float32(np.pi / 4.0)),
pytest.param(ng.tanh, np.float32(0.1234)),
],
)
def test_unary_op_scalar(graph_api_fn, numpy_fn, input_data):
expected = numpy_fn(input_data)
def test_unary_op_scalar(graph_api_fn, input_data):
node = graph_api_fn(input_data)
result = run_op_node([input_data], graph_api_fn)
assert np.allclose(result, expected)
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == Type.f32
assert list(node.get_output_shape(0)) == []
@pytest.mark.parametrize(
"input_data", [(np.array([True, False, True, False])), (np.array([True])), (np.array([False]))]
)
def test_logical_not(input_data):
expected = np.logical_not(input_data)
result = run_op_node([input_data], ng.logical_not)
assert np.allclose(result, expected)
node = ng.logical_not(input_data)
assert node.get_output_size() == 1
assert node.get_type_name() == "LogicalNot"
assert node.get_output_element_type(0) == Type.boolean
assert list(node.get_output_shape(0)) == list(input_data.shape)
def test_sigmoid():
input_data = np.array([-3.14, -1.0, 0.0, 2.71001, 1000.0], dtype=np.float32)
result = run_op_node([input_data], ng.sigmoid)
node = ng.sigmoid(input_data)
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
expected = np.array(list(map(sigmoid, input_data)))
assert np.allclose(result, expected)
assert node.get_output_size() == 1
assert node.get_type_name() == "Sigmoid"
assert node.get_output_element_type(0) == Type.f32
assert list(node.get_output_shape(0)) == [5]
def test_softmax_positive_axis():
def test_softmax():
axis = 1
input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
result = run_op_node([input_tensor], ng.softmax, axis)
expected = [[0.09003056, 0.24472842, 0.6652409], [0.09003056, 0.24472842, 0.6652409]]
assert np.allclose(result, expected)
def test_softmax_negative_axis():
axis = -1
input_tensor = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
result = run_op_node([input_tensor], ng.softmax, axis)
expected = [[0.09003056, 0.24472842, 0.6652409], [0.09003056, 0.24472842, 0.6652409]]
assert np.allclose(result, expected)
node = ng.softmax(input_tensor, axis)
assert node.get_output_size() == 1
assert node.get_type_name() == "Softmax"
assert node.get_output_element_type(0) == Type.f32
assert list(node.get_output_shape(0)) == [2, 3]
def test_erf():
input_tensor = np.array([-1.0, 0.0, 1.0, 2.5, 3.14, 4.0], dtype=np.float32)
expected = [-0.842701, 0.0, 0.842701, 0.999593, 0.999991, 1.0]
result = run_op_node([input_tensor], ng.erf)
assert np.allclose(result, expected)
node = ng.erf(input_tensor)
assert node.get_output_size() == 1
assert node.get_type_name() == "Erf"
assert node.get_output_element_type(0) == Type.f32
assert list(node.get_output_shape(0)) == [6]
def test_hswish():
@ -152,29 +141,6 @@ def test_round_even():
assert list(node.get_output_shape(0)) == [3, 10]
assert node.get_output_element_type(0) == Type.f32
input_tensor = np.array([-2.5, -1.5, -0.5, 0.5, 0.9, 1.5, 2.3, 2.5, 3.5], dtype=np.float32)
expected = [-2.0, -2.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 4.0]
result = run_op_node([input_tensor], ng.round, "HALF_TO_EVEN")
assert np.allclose(result, expected)
def test_round_away():
float_dtype = np.float32
data = ng.parameter(Shape([3, 10]), dtype=float_dtype, name="data")
node = ng.round(data, "HALF_AWAY_FROM_ZERO")
assert node.get_type_name() == "Round"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [3, 10]
assert node.get_output_element_type(0) == Type.f32
input_tensor = np.array([-2.5, -1.5, -0.5, 0.5, 0.9, 1.5, 2.3, 2.5, 3.5], dtype=np.float32)
expected = [-3.0, -2.0, -1.0, 1.0, 1.0, 2.0, 2.0, 3.0, 4.0]
result = run_op_node([input_tensor], ng.round, "HALF_AWAY_FROM_ZERO")
assert np.allclose(result, expected)
def test_hsigmoid():
float_dtype = np.float32
@ -188,92 +154,42 @@ def test_hsigmoid():
def test_gelu_operator_with_parameters():
runtime = get_runtime()
data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32)
data_shape = [2, 2]
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.gelu(parameter_data, "erf")
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.array([[-1.6391277e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32)
assert np.allclose(result, expected, 1e-6, 1e-6)
assert model.get_output_size() == 1
assert model.get_type_name() == "Gelu"
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [2, 2]
def test_gelu_operator_with_array():
runtime = get_runtime()
data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32)
model = ng.gelu(data_value, "erf")
computation = runtime.computation(model)
result = computation()
expected = np.array([[-1.6391277e-06, 8.4134471e-01], [-4.5500278e-02, 2.9959502]], dtype=np.float32)
assert np.allclose(result, expected, 1e-6, 1e-6)
assert model.get_output_size() == 1
assert model.get_type_name() == "Gelu"
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [2, 2]
def test_gelu_tanh_operator_with_parameters():
runtime = get_runtime()
data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32)
data_shape = [2, 2]
parameter_data = ng.parameter(data_shape, name="Data", dtype=np.float32)
model = ng.gelu(parameter_data, "tanh")
computation = runtime.computation(model, parameter_data)
result = computation(data_value)
expected = np.array([[0.0, 0.841192], [-0.04540223, 2.9963627]], dtype=np.float32)
assert np.allclose(result, expected, 1e-6, 1e-6)
assert model.get_output_size() == 1
assert model.get_type_name() == "Gelu"
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [2, 2]
def test_gelu_tanh_operator_with_array():
runtime = get_runtime()
data_value = np.array([[-5, 1], [-2, 3]], dtype=np.float32)
model = ng.gelu(data_value, "tanh")
computation = runtime.computation(model)
result = computation()
expected = np.array([[0.0, 0.841192], [-0.04540223, 2.9963627]], dtype=np.float32)
assert np.allclose(result, expected, 1e-6, 1e-6)
type_tolerance = [
(np.float64, 1e-6),
(np.float32, 1e-6),
(np.float16, 1e-3),
]
@pytest.mark.parametrize("type_tolerance", type_tolerance)
def test_softsign_with_parameters(type_tolerance):
dtype, atol = type_tolerance
data = np.random.uniform(-1.0, 1.0, (32, 5)).astype(dtype)
expected = np.divide(data, np.abs(data) + 1)
runtime = get_runtime()
param = ng.parameter(data.shape, dtype, name="Data")
result = runtime.computation(ng.softsign(param, "SoftSign"), param)(data)
assert np.allclose(result, expected, R_TOLERANCE, atol)
@pytest.mark.parametrize("type_tolerance", type_tolerance)
def test_softsign_with_array(type_tolerance):
dtype, atol = type_tolerance
data = np.random.uniform(-1.0, 1.0, (32, 5)).astype(dtype)
expected = np.divide(data, np.abs(data) + 1)
runtime = get_runtime()
result = runtime.computation(ng.softsign(data, "SoftSign"))()
assert np.allclose(result, expected, R_TOLERANCE, atol)
assert model.get_output_size() == 1
assert model.get_type_name() == "Gelu"
assert model.get_output_element_type(0) == Type.f32
assert list(model.get_output_shape(0)) == [2, 2]

View File

@ -5,7 +5,7 @@ import numpy as np
import pytest
import ngraph as ng
from tests_compatibility.runtime import get_runtime
from ngraph.impl import Type
@pytest.fixture
@ -14,7 +14,6 @@ def _ndarray_1x1x4x4():
def test_avg_pool_2d(_ndarray_1x1x4x4):
runtime = get_runtime()
input_data = _ndarray_1x1x4x4
param = ng.parameter(input_data.shape, name="A", dtype=np.float32)
@ -24,41 +23,15 @@ def test_avg_pool_2d(_ndarray_1x1x4x4):
pads_end = [0] * spatial_dim_count
strides = [2, 2]
exclude_pad = True
expected = [[[[13.5, 15.5], [21.5, 23.5]]]]
avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad)
computation = runtime.computation(avg_pool_node, param)
result = computation(input_data)
assert np.allclose(result, expected)
expected = [[[[13.5, 14.5, 15.5], [17.5, 18.5, 19.5], [21.5, 22.5, 23.5]]]]
strides = [1, 1]
avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad)
computation = runtime.computation(avg_pool_node, param)
result = computation(input_data)
assert np.allclose(result, expected)
pads_begin = [1, 1]
pads_end = [1, 1]
strides = [2, 2]
exclude_pad = True
expected = [[[[11.0, 12.5, 14.0], [17.0, 18.5, 20.0], [23.0, 24.5, 26.0]]]]
avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad)
computation = runtime.computation(avg_pool_node, param)
result = computation(input_data)
assert np.allclose(result, expected)
exclude_pad = False
expected = [[[[2.75, 6.25, 3.5], [8.5, 18.5, 10.0], [5.75, 12.25, 6.5]]]]
avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad)
computation = runtime.computation(avg_pool_node, param)
result = computation(input_data)
assert np.allclose(result, expected)
assert avg_pool_node.get_type_name() == "AvgPool"
assert avg_pool_node.get_output_size() == 1
assert list(avg_pool_node.get_output_shape(0)) == [1, 1, 2, 2]
assert avg_pool_node.get_output_element_type(0) == Type.f32
def test_avg_pooling_3d(_ndarray_1x1x4x4):
rt = get_runtime()
data = _ndarray_1x1x4x4
data = np.broadcast_to(data, (1, 1, 4, 4, 4))
param = ng.parameter(list(data.shape))
@ -70,19 +43,13 @@ def test_avg_pooling_3d(_ndarray_1x1x4x4):
exclude_pad = True
avgpool = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad)
comp = rt.computation(avgpool, param)
result = comp(data)
result_ref = [[[[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5, 23.5]]]]]
assert np.allclose(result, result_ref)
assert avgpool.get_type_name() == "AvgPool"
assert avgpool.get_output_size() == 1
assert list(avgpool.get_output_shape(0)) == [1, 1, 2, 2, 2]
assert avgpool.get_output_element_type(0) == Type.f32
def test_max_pool_basic():
rt = get_runtime()
# array([[[[ 0.5, 1.5, 2.5, 3.5],
# [ 4.5, 5.5, 6.5, 7.5],
# [ 8.5, 9.5, 10.5, 11.5],
# [12.5, 13.5, 14.5, 15.5]]]], dtype=float32)
data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4))
strides = [1, 1]
dilations = [1, 1]
@ -105,24 +72,15 @@ def test_max_pool_basic():
auto_pad,
index_et,
)
comp = rt.computation(maxpool_node, data_node)
result = comp(data)
expected = np.array(
[[[[5.5, 6.5, 7.5], [9.5, 10.5, 11.5], [13.5, 14.5, 15.5]]]], dtype=np.float32
)
expected_idx = np.array([[[[5, 6, 7], [9, 10, 11], [13, 14, 15]]]], dtype=np.int32)
assert np.allclose(result[0], expected)
assert np.allclose(result[1], expected_idx)
assert maxpool_node.get_type_name() == "MaxPool"
assert maxpool_node.get_output_size() == 2
assert list(maxpool_node.get_output_shape(0)) == [1, 1, 3, 3]
assert list(maxpool_node.get_output_shape(1)) == [1, 1, 3, 3]
assert maxpool_node.get_output_element_type(0) == Type.f32
assert maxpool_node.get_output_element_type(1) == Type.i32
def test_max_pool_strides():
rt = get_runtime()
# array([[[[ 0.5, 1.5, 2.5, 3.5],
# [ 4.5, 5.5, 6.5, 7.5],
# [ 8.5, 9.5, 10.5, 11.5],
# [12.5, 13.5, 14.5, 15.5]]]], dtype=float32)
data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4))
strides = [2, 1]
dilations = [1, 1]
@ -145,22 +103,15 @@ def test_max_pool_strides():
auto_pad,
index_et,
)
comp = rt.computation(maxpool_node, data_node)
result = comp(data)
expected = np.array([[[[5.5, 6.5, 7.5], [13.5, 14.5, 15.5]]]], dtype=np.float32)
expected_idx = np.array([[[[5, 6, 7], [13, 14, 15]]]], dtype=np.int32)
assert np.allclose(result[0], expected)
assert np.allclose(result[1], expected_idx)
assert maxpool_node.get_type_name() == "MaxPool"
assert maxpool_node.get_output_size() == 2
assert list(maxpool_node.get_output_shape(0)) == [1, 1, 2, 3]
assert list(maxpool_node.get_output_shape(1)) == [1, 1, 2, 3]
assert maxpool_node.get_output_element_type(0) == Type.f32
assert maxpool_node.get_output_element_type(1) == Type.i32
def test_max_pool_kernel_shape1x1():
rt = get_runtime()
# array([[[[ 0.5, 1.5, 2.5, 3.5],
# [ 4.5, 5.5, 6.5, 7.5],
# [ 8.5, 9.5, 10.5, 11.5],
# [12.5, 13.5, 14.5, 15.5]]]], dtype=float32)
data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4))
strides = [1, 1]
dilations = [1, 1]
@ -183,20 +134,15 @@ def test_max_pool_kernel_shape1x1():
auto_pad,
index_et,
)
comp = rt.computation(maxpool_node, data_node)
result = comp(data)
assert np.allclose(result[0], data)
assert np.allclose(result[1], np.arange(0, 16, dtype=np.int32).reshape((1, 1, 4, 4)))
assert maxpool_node.get_type_name() == "MaxPool"
assert maxpool_node.get_output_size() == 2
assert list(maxpool_node.get_output_shape(0)) == [1, 1, 4, 4]
assert list(maxpool_node.get_output_shape(1)) == [1, 1, 4, 4]
assert maxpool_node.get_output_element_type(0) == Type.f32
assert maxpool_node.get_output_element_type(1) == Type.i32
def test_max_pool_kernel_shape3x3():
rt = get_runtime()
# array([[[[ 0.5, 1.5, 2.5, 3.5],
# [ 4.5, 5.5, 6.5, 7.5],
# [ 8.5, 9.5, 10.5, 11.5],
# [12.5, 13.5, 14.5, 15.5]]]], dtype=float32)
data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4))
strides = [1, 1]
dilations = [1, 1]
@ -219,31 +165,20 @@ def test_max_pool_kernel_shape3x3():
auto_pad,
index_et,
)
comp = rt.computation(maxpool_node, data_node)
result = comp(data)
expected = np.array([[[[10.5, 11.5], [14.5, 15.5]]]], dtype=np.float32)
assert np.allclose(result[0], expected)
assert maxpool_node.get_type_name() == "MaxPool"
assert maxpool_node.get_output_size() == 2
assert list(maxpool_node.get_output_shape(0)) == [1, 1, 2, 2]
assert list(maxpool_node.get_output_shape(1)) == [1, 1, 2, 2]
assert maxpool_node.get_output_element_type(0) == Type.f32
assert maxpool_node.get_output_element_type(1) == Type.i32
def test_max_pool_non_zero_pads():
rt = get_runtime()
# array([[[[ 0.5, 1.5, 2.5, 3.5],
# [ 4.5, 5.5, 6.5, 7.5],
# [ 8.5, 9.5, 10.5, 11.5],
# [12.5, 13.5, 14.5, 15.5]]]], dtype=float32)
data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4))
strides = [1, 1]
dilations = [1, 1]
pads_begin = [1, 1]
pads_end = [1, 1]
# 0 0 , 0 , 0 , 0, 0
# 0 [ 0.5, 1.5, 2.5, 3.5], 0,
# 0 [ 4.5, 5.5, 6.5, 7.5], 0,
# 0 [ 8.5, 9.5, 10.5, 11.5], 0,
# 0 [12.5, 13.5, 14.5, 15.5], 0
# 0 0 , 0 , 0 , 0, 0
kernel_shape = [2, 2]
rounding_type = "floor"
auto_pad = None
@ -261,58 +196,20 @@ def test_max_pool_non_zero_pads():
auto_pad,
index_et,
)
comp = rt.computation(maxpool_node, data_node)
result = comp(data)
expected = np.array(
[
[
[
[0.5, 1.5, 2.5, 3.5, 3.5],
[4.5, 5.5, 6.5, 7.5, 7.5],
[8.5, 9.5, 10.5, 11.5, 11.5],
[12.5, 13.5, 14.5, 15.5, 15.5],
[12.5, 13.5, 14.5, 15.5, 15.5],
]
]
],
dtype=np.float32,
)
expected_idx = np.array(
[
[
[
[0, 1, 2, 3, 3],
[4, 5, 6, 7, 7],
[8, 9, 10, 11, 11],
[12, 13, 14, 15, 15],
[12, 13, 14, 15, 15],
]
]
],
dtype=np.int32,
)
assert np.allclose(result[0], expected)
assert np.allclose(result[1], expected_idx)
assert maxpool_node.get_type_name() == "MaxPool"
assert maxpool_node.get_output_size() == 2
assert list(maxpool_node.get_output_shape(0)) == [1, 1, 5, 5]
assert list(maxpool_node.get_output_shape(1)) == [1, 1, 5, 5]
assert maxpool_node.get_output_element_type(0) == Type.f32
assert maxpool_node.get_output_element_type(1) == Type.i32
def test_max_pool_same_upper_auto_pads():
rt = get_runtime()
# array([[[[ 0.5, 1.5, 2.5, 3.5],
# [ 4.5, 5.5, 6.5, 7.5],
# [ 8.5, 9.5, 10.5, 11.5],
# [12.5, 13.5, 14.5, 15.5]]]], dtype=float32)
data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4))
strides = [1, 1]
dilations = [1, 1]
pads_begin = [0, 0]
pads_end = [0, 0]
# [ 0.5, 1.5, 2.5, 3.5], 0,
# [ 4.5, 5.5, 6.5, 7.5], 0,
# [ 8.5, 9.5, 10.5, 11.5], 0,
# [12.5, 13.5, 14.5, 15.5], 0
# 0 , 0 , 0 , 0, 0
kernel_shape = [2, 2]
auto_pad = "same_upper"
rounding_type = "floor"
@ -330,56 +227,20 @@ def test_max_pool_same_upper_auto_pads():
auto_pad,
index_et,
)
comp = rt.computation(maxpool_node, data_node)
result = comp(data)
expected = np.array(
[
[
[
[5.5, 6.5, 7.5, 7.5],
[9.5, 10.5, 11.5, 11.5],
[13.5, 14.5, 15.5, 15.5],
[13.5, 14.5, 15.5, 15.5],
]
]
],
dtype=np.float32,
)
expected_idx = np.array(
[
[
[
[5, 6, 7, 7],
[9, 10, 11, 11],
[13, 14, 15, 15],
[13, 14, 15, 15],
]
]
],
dtype=np.int32,
)
assert np.allclose(result[0], expected)
assert np.allclose(result[1], expected_idx)
assert maxpool_node.get_type_name() == "MaxPool"
assert maxpool_node.get_output_size() == 2
assert list(maxpool_node.get_output_shape(0)) == [1, 1, 4, 4]
assert list(maxpool_node.get_output_shape(1)) == [1, 1, 4, 4]
assert maxpool_node.get_output_element_type(0) == Type.f32
assert maxpool_node.get_output_element_type(1) == Type.i32
def test_max_pool_same_lower_auto_pads():
rt = get_runtime()
# array([[[[ 0.5, 1.5, 2.5, 3.5],
# [ 4.5, 5.5, 6.5, 7.5],
# [ 8.5, 9.5, 10.5, 11.5],
# [12.5, 13.5, 14.5, 15.5]]]], dtype=float32)
data = np.arange(0.5, 16, dtype=np.float32).reshape((1, 1, 4, 4))
strides = [1, 1]
dilations = [1, 1]
pads_begin = [0, 0]
pads_end = [0, 0]
# 0 0 , 0 , 0 , 0,
# 0 [ 0.5, 1.5, 2.5, 3.5],
# 0 [ 4.5, 5.5, 6.5, 7.5],
# 0 [ 8.5, 9.5, 10.5, 11.5],
# 0 [12.5, 13.5, 14.5, 15.5],
kernel_shape = [2, 2]
auto_pad = "same_lower"
rounding_type = "floor"
@ -397,34 +258,9 @@ def test_max_pool_same_lower_auto_pads():
auto_pad,
index_et,
)
comp = rt.computation(maxpool_node, data_node)
result = comp(data)
expected = np.array(
[
[
[
[0.5, 1.5, 2.5, 3.5],
[4.5, 5.5, 6.5, 7.5],
[8.5, 9.5, 10.5, 11.5],
[12.5, 13.5, 14.5, 15.5],
]
]
],
dtype=np.float32,
)
expected_idx = np.array(
[
[
[
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15],
]
]
],
dtype=np.int32,
)
assert np.allclose(result[0], expected)
assert np.allclose(result[1], expected_idx)
assert maxpool_node.get_type_name() == "MaxPool"
assert maxpool_node.get_output_size() == 2
assert list(maxpool_node.get_output_shape(0)) == [1, 1, 4, 4]
assert list(maxpool_node.get_output_shape(1)) == [1, 1, 4, 4]
assert maxpool_node.get_output_element_type(0) == Type.f32
assert maxpool_node.get_output_element_type(1) == Type.i32

View File

@ -2,12 +2,12 @@
# SPDX-License-Identifier: Apache-2.0
import ngraph as ng
from ngraph.impl import Type
import numpy as np
from tests_compatibility.runtime import get_runtime
def test_random_uniform():
runtime = get_runtime()
input_tensor = ng.constant(np.array([2, 4, 3], dtype=np.int32))
min_val = ng.constant(np.array([-2.7], dtype=np.float32))
max_val = ng.constant(np.array([3.5], dtype=np.float32))
@ -15,16 +15,10 @@ def test_random_uniform():
random_uniform_node = ng.random_uniform(input_tensor, min_val, max_val,
output_type="f32", global_seed=7461,
op_seed=1546)
computation = runtime.computation(random_uniform_node)
random_uniform_results = computation()
expected_results = np.array([[[2.8450181, -2.3457108, 2.2134445],
[-1.0436587, 0.79548645, 1.3023183],
[0.34447956, -2.0267959, 1.3989122],
[0.9607613, 1.5363653, 3.117298]],
[[1.570041, 2.2782724, 2.3193843],
[3.3393657, 0.63299894, 0.41231918],
[3.1739233, 0.03919673, -0.2136085],
[-1.4519991, -2.277353, 2.630727]]], dtype=np.float32)
assert np.allclose(random_uniform_results, expected_results)
random_uniform_node = ng.random_uniform(input_tensor, min_val, max_val,
output_type="f32", global_seed=7461,
op_seed=1546)
assert random_uniform_node.get_output_size() == 1
assert random_uniform_node.get_type_name() == "RandomUniform"
assert random_uniform_node.get_output_element_type(0) == Type.f32
assert list(random_uniform_node.get_output_shape(0)) == [2, 4, 3]

View File

@ -3,60 +3,59 @@
import numpy as np
import pytest
from _pyngraph import PartialShape, Dimension
import ngraph as ng
from ngraph.utils.types import make_constant_node
from tests_compatibility.runtime import get_runtime
from tests_compatibility.test_ngraph.util import run_op_node
from ngraph.impl import Type
@pytest.mark.parametrize(
"ng_api_helper, numpy_function, reduction_axes",
("ng_api_helper", "reduction_axes", "expected_shape"),
[
(ng.reduce_max, np.max, np.array([0, 1, 2, 3])),
(ng.reduce_min, np.min, np.array([0, 1, 2, 3])),
(ng.reduce_sum, np.sum, np.array([0, 1, 2, 3])),
(ng.reduce_prod, np.prod, np.array([0, 1, 2, 3])),
(ng.reduce_max, np.max, np.array([0])),
(ng.reduce_min, np.min, np.array([0])),
(ng.reduce_sum, np.sum, np.array([0])),
(ng.reduce_prod, np.prod, np.array([0])),
(ng.reduce_max, np.max, np.array([0, 2])),
(ng.reduce_min, np.min, np.array([0, 2])),
(ng.reduce_sum, np.sum, np.array([0, 2])),
(ng.reduce_prod, np.prod, np.array([0, 2])),
(ng.reduce_max, np.array([0, 1, 2, 3]), []),
(ng.reduce_min, np.array([0, 1, 2, 3]), []),
(ng.reduce_sum, np.array([0, 1, 2, 3]), []),
(ng.reduce_prod, np.array([0, 1, 2, 3]), []),
(ng.reduce_max, np.array([0]), [4, 3, 2]),
(ng.reduce_min, np.array([0]), [4, 3, 2]),
(ng.reduce_sum, np.array([0]), [4, 3, 2]),
(ng.reduce_prod, np.array([0]), [4, 3, 2]),
(ng.reduce_max, np.array([0, 2]), [4, 2]),
(ng.reduce_min, np.array([0, 2]), [4, 2]),
(ng.reduce_sum, np.array([0, 2]), [4, 2]),
(ng.reduce_prod, np.array([0, 2]), [4, 2]),
],
)
def test_reduction_ops(ng_api_helper, numpy_function, reduction_axes):
def test_reduction_ops(ng_api_helper, reduction_axes, expected_shape):
shape = [2, 4, 3, 2]
np.random.seed(133391)
input_data = np.random.randn(*shape).astype(np.float32)
expected = numpy_function(input_data, axis=tuple(reduction_axes))
result = run_op_node([input_data], ng_api_helper, reduction_axes)
assert np.allclose(result, expected)
node = ng_api_helper(input_data, reduction_axes)
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == expected_shape
assert node.get_output_element_type(0) == Type.f32
@pytest.mark.parametrize(
"ng_api_helper, numpy_function, reduction_axes",
("ng_api_helper", "reduction_axes", "expected_shape"),
[
(ng.reduce_logical_and, np.logical_and.reduce, np.array([0])),
(ng.reduce_logical_or, np.logical_or.reduce, np.array([0])),
(ng.reduce_logical_and, np.logical_and.reduce, np.array([0, 2])),
(ng.reduce_logical_or, np.logical_or.reduce, np.array([0, 2])),
(ng.reduce_logical_and, np.logical_and.reduce, np.array([0, 1, 2, 3])),
(ng.reduce_logical_or, np.logical_or.reduce, np.array([0, 1, 2, 3])),
(ng.reduce_logical_and, np.array([0]), [4, 3, 2]),
(ng.reduce_logical_or, np.array([0]), [4, 3, 2]),
(ng.reduce_logical_and, np.array([0, 2]), [4, 2]),
(ng.reduce_logical_or, np.array([0, 2]), [4, 2]),
(ng.reduce_logical_and, np.array([0, 1, 2, 3]), []),
(ng.reduce_logical_or, np.array([0, 1, 2, 3]), []),
],
)
def test_reduction_logical_ops(ng_api_helper, numpy_function, reduction_axes):
def test_reduction_logical_ops(ng_api_helper, reduction_axes, expected_shape):
shape = [2, 4, 3, 2]
np.random.seed(133391)
input_data = np.random.randn(*shape).astype(bool)
expected = numpy_function(input_data, axis=tuple(reduction_axes))
result = run_op_node([input_data], ng_api_helper, reduction_axes)
assert np.allclose(result, expected)
node = ng_api_helper(input_data, reduction_axes)
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == expected_shape
assert node.get_output_element_type(0) == Type.boolean
def test_topk():
@ -69,24 +68,27 @@ def test_topk():
assert node.get_output_size() == 2
assert list(node.get_output_shape(0)) == [6, 3, 10, 24]
assert list(node.get_output_shape(1)) == [6, 3, 10, 24]
assert node.get_output_element_type(0) == Type.f32
assert node.get_output_element_type(1) == Type.i32
@pytest.mark.parametrize(
"ng_api_helper, numpy_function, reduction_axes",
("ng_api_helper", "reduction_axes", "expected_shape"),
[
(ng.reduce_mean, np.mean, np.array([0, 1, 2, 3])),
(ng.reduce_mean, np.mean, np.array([0])),
(ng.reduce_mean, np.mean, np.array([0, 2])),
(ng.reduce_mean, np.array([0, 1, 2, 3]), []),
(ng.reduce_mean, np.array([0]), [4, 3, 2]),
(ng.reduce_mean, np.array([0, 2]), [4, 2]),
],
)
def test_reduce_mean_op(ng_api_helper, numpy_function, reduction_axes):
def test_reduce_mean_op(ng_api_helper, reduction_axes, expected_shape):
shape = [2, 4, 3, 2]
np.random.seed(133391)
input_data = np.random.randn(*shape).astype(np.float32)
expected = numpy_function(input_data, axis=tuple(reduction_axes))
result = run_op_node([input_data], ng_api_helper, reduction_axes)
assert np.allclose(result, expected)
node = ng_api_helper(input_data, reduction_axes)
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == expected_shape
assert node.get_output_element_type(0) == Type.f32
def test_non_zero():
@ -99,6 +101,7 @@ def test_non_zero():
assert node.get_type_name() == "NonZero"
assert node.get_output_size() == 1
assert node.get_output_element_type(0) == Type.i64
def test_roi_align():
@ -131,6 +134,7 @@ def test_roi_align():
assert node.get_type_name() == "ROIAlign"
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == expected_shape
assert node.get_output_element_type(0) == Type.f32
@pytest.mark.parametrize(
@ -140,16 +144,11 @@ def test_roi_align():
def test_cum_sum(input_shape, cumsum_axis, reverse):
input_data = np.arange(np.prod(input_shape)).reshape(input_shape)
if reverse:
expected = np.cumsum(input_data[::-1], axis=cumsum_axis)[::-1]
else:
expected = np.cumsum(input_data, axis=cumsum_axis)
runtime = get_runtime()
node = ng.cum_sum(input_data, cumsum_axis, reverse=reverse)
computation = runtime.computation(node)
result = computation()
assert np.allclose(result, expected)
assert node.get_output_size() == 1
assert node.get_type_name() == "CumSum"
assert list(node.get_output_shape(0)) == input_shape
assert node.get_output_element_type(0) == Type.i64
def test_normalize_l2():
@ -160,38 +159,8 @@ def test_normalize_l2():
eps = 1e-6
eps_mode = "add"
runtime = get_runtime()
node = ng.normalize_l2(input_data, axes, eps, eps_mode)
computation = runtime.computation(node)
result = computation()
expected = np.array(
[
0.01428571,
0.02857143,
0.04285714,
0.05714286,
0.07142857,
0.08571429,
0.1,
0.11428571,
0.12857144,
0.14285715,
0.15714286,
0.17142858,
0.18571429,
0.2,
0.21428572,
0.22857143,
0.24285714,
0.25714287,
0.27142859,
0.2857143,
0.30000001,
0.31428573,
0.32857144,
0.34285715,
]
).reshape(input_shape)
assert np.allclose(result, expected)
assert node.get_output_size() == 1
assert node.get_type_name() == "NormalizeL2"
assert list(node.get_output_shape(0)) == input_shape
assert node.get_output_element_type(0) == Type.f32

View File

@ -2,20 +2,19 @@
# SPDX-License-Identifier: Apache-2.0
import ngraph as ng
from ngraph.impl import Type
import numpy as np
from tests_compatibility.runtime import get_runtime
def test_roll():
runtime = get_runtime()
input = np.reshape(np.arange(10), (2, 5))
input_tensor = ng.constant(input)
input_shift = ng.constant(np.array([-10, 7], dtype=np.int32))
input_axes = ng.constant(np.array([-1, 0], dtype=np.int32))
roll_node = ng.roll(input_tensor, input_shift, input_axes)
computation = runtime.computation(roll_node)
roll_results = computation()
expected_results = np.roll(input, shift=(-10, 7), axis=(-1, 0))
assert np.allclose(roll_results, expected_results)
assert roll_node.get_output_size() == 1
assert roll_node.get_type_name() == "Roll"
assert list(roll_node.get_output_shape(0)) == [2, 5]
assert roll_node.get_output_element_type(0) == Type.i64

View File

@ -4,17 +4,13 @@
import numpy as np
import ngraph as ng
from tests_compatibility.runtime import get_runtime
from tests_compatibility.test_ngraph.util import run_op_node
from ngraph.impl import Type
def test_onehot():
runtime = get_runtime()
param = ng.parameter([3], dtype=np.int32)
model = ng.one_hot(param, 3, 1, 0, 0)
computation = runtime.computation(model, param)
expected = np.eye(3)[np.array([1, 0, 2])]
input_data = np.array([1, 0, 2], dtype=np.int32)
result = computation(input_data)
assert np.allclose(result, expected)
assert model.get_output_size() == 1
assert model.get_type_name() == "OneHot"
assert list(model.get_output_shape(0)) == [3, 3]
assert model.get_output_element_type(0) == Type.i64

View File

@ -1,75 +1,6 @@
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from typing import Any, Callable, List, Union
import numpy as np
import ngraph as ng
from ngraph.utils.types import NumericData
from tests_compatibility.runtime import get_runtime
from string import ascii_uppercase
def _get_numpy_dtype(scalar):
return np.array([scalar]).dtype
def run_op_node(input_data, op_fun, *args):
# type: (Union[NumericData, List[NumericData]], Callable, *Any) -> List[NumericData]
"""Run computation on node performing `op_fun`.
`op_fun` has to accept a node as an argument.
This function converts passed raw input data to nGraph Constant Node and that form is passed
to `op_fun`.
:param input_data: The input data for performed computation.
:param op_fun: The function handler for operation we want to carry out.
:param args: The arguments passed to operation we want to carry out.
:return: The result from computations.
"""
runtime = get_runtime()
comp_args = []
op_fun_args = []
comp_inputs = []
for idx, data in enumerate(input_data):
node = None
if np.isscalar(data):
node = ng.parameter([], name=ascii_uppercase[idx], dtype=_get_numpy_dtype(data))
else:
node = ng.parameter(data.shape, name=ascii_uppercase[idx], dtype=data.dtype)
op_fun_args.append(node)
comp_args.append(node)
comp_inputs.append(data)
op_fun_args.extend(args)
node = op_fun(*op_fun_args)
computation = runtime.computation(node, *comp_args)
return computation(*comp_inputs)
def run_op_numeric_data(input_data, op_fun, *args):
# type: (NumericData, Callable, *Any) -> List[NumericData]
"""Run computation on node performing `op_fun`.
`op_fun` has to accept a scalar or an array.
This function passess input data AS IS. This mean that in case they're a scalar (integral,
or floating point value) or a NumPy's ndarray object they will be automatically converted
to nGraph's Constant Nodes.
:param input_data: The input data for performed computation.
:param op_fun: The function handler for operation we want to carry out.
:param args: The arguments passed to operation we want to carry out.
:return: The result from computations.
"""
runtime = get_runtime()
node = op_fun(input_data, *args)
computation = runtime.computation(node)
return computation()
def count_ops_of_type(func, op_type):
count = 0