Change "run_op_node" helper to use Parameter instead of Constant (#1722)
authorEwa Tusień <ewa.tusien@intel.com>
Wed, 19 Aug 2020 15:55:20 +0000 (17:55 +0200)
committerGitHub <noreply@github.com>
Wed, 19 Aug 2020 15:55:20 +0000 (17:55 +0200)
* Replace Constant with Parameter in run_op_node.

* Pass inputs to function.

* Add func to get shape.

* Make constant if input is scalar.

* Add case for list.

* Fix test.

* Split tests for run_op_node and run_op_numeric_data.

* Split more tests.

* Split more and more tests.

* Mark tests with xfail.

* Mark more tests with xfail.

* Replace scalar with parameter.

* Code formatting.

* Set empty shape for scalar.

* Remove check for list.

ngraph/python/tests/test_ngraph/test_basic.py
ngraph/python/tests/test_ngraph/test_convolution.py
ngraph/python/tests/test_ngraph/test_ops_binary.py
ngraph/python/tests/test_ngraph/test_ops_matmul.py
ngraph/python/tests/test_ngraph/test_ops_reshape.py
ngraph/python/tests/test_ngraph/test_ops_unary.py
ngraph/python/tests/test_ngraph/test_sequence_processing.py
ngraph/python/tests/test_ngraph/util.py

index e01f8fd..1755655 100644 (file)
@@ -162,9 +162,9 @@ def test_convert_to_bool(destination_type, input_data):
 @pytest.mark.parametrize(
     "destination_type, rand_range, in_dtype, expected_type",
     [
-        pytest.param(np.float32, (-8, 8), np.int32, np.float32, marks=xfail_issue_34323),
+        pytest.param(np.float32, (-8, 8), np.int32, np.float32),
         pytest.param(np.float64, (-16383, 16383), np.int64, np.float64, marks=xfail_issue_35929),
-        pytest.param("f32", (-8, 8), np.int32, np.float32, marks=xfail_issue_34323),
+        pytest.param("f32", (-8, 8), np.int32, np.float32),
         pytest.param("f64", (-16383, 16383), np.int64, np.float64, marks=xfail_issue_35929),
     ],
 )
index bfd1e9e..7fded63 100644 (file)
@@ -20,10 +20,8 @@ import ngraph as ng
 from tests.runtime import get_runtime
 from tests.test_ngraph.test_ops import convolution2d
 from tests.test_ngraph.util import run_op_node
-from tests import xfail_issue_34323
 
 
-@xfail_issue_34323
 def test_convolution_2d():
 
     # input_x should have shape N(batch) x C x H x W
@@ -214,7 +212,6 @@ def test_convolution_backprop_data():
     )
 
 
-@xfail_issue_34323
 def test_convolution_v1():
     input_tensor = np.arange(-128, 128, 1, dtype=np.float32).reshape(1, 1, 16, 16)
     filters = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
index 5269036..8494791 100644 (file)
@@ -213,7 +213,6 @@ def test_multiply():
     assert np.allclose(result, expected)
 
 
-@xfail_issue_34323
 def test_power_v1():
     A = np.arange(48, dtype=np.float32).reshape((8, 1, 6, 1))
     B = np.arange(20, dtype=np.float32).reshape((4, 1, 5))
index 9986d6e..952053a 100644 (file)
@@ -18,10 +18,8 @@ import pytest
 
 import ngraph as ng
 from tests.test_ngraph.util import run_op_node
-from tests import xfail_issue_34323
 
 
-@xfail_issue_34323
 @pytest.mark.parametrize(
     "shape_a, shape_b, transpose_a, transpose_b",
     [
index a620b67..b1c77dc 100644 (file)
@@ -19,7 +19,7 @@ import pytest
 import ngraph as ng
 from tests.runtime import get_runtime
 from tests.test_ngraph.util import run_op_node, run_op_numeric_data
-from tests import xfail_issue_34323, xfail_issue_35929
+from tests import xfail_issue_34323, xfail_issue_35929, xfail_issue_35926
 
 
 def test_concat():
@@ -123,7 +123,7 @@ def test_broadcast_bidirectional():
     assert node.get_output_size() == 1
 
 
-@xfail_issue_34323
+@xfail_issue_35926
 def test_gather():
     input_data = np.array([1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32).reshape((3, 3))
     input_indices = np.array([0, 2], np.int64).reshape(1, 2)
@@ -134,6 +134,15 @@ def test_gather():
     result = run_op_node([input_data, input_indices, input_axes], ng.gather)
     assert np.allclose(result, expected)
 
+
+@xfail_issue_34323
+def test_gather_using_constants():
+    input_data = np.array([1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2], np.float32).reshape((3, 3))
+    input_indices = np.array([0, 2], np.int64).reshape(1, 2)
+    input_axes = np.array([1], np.int64)
+
+    expected = np.array([1.0, 1.2, 2.0, 2.2, 3.0, 3.2], dtype=np.float32).reshape((3, 1, 2))
+
     result = run_op_numeric_data(input_data, ng.gather, input_indices, input_axes)
     assert np.allclose(result, expected)
 
index 92c3415..f6c03fc 100644 (file)
@@ -19,7 +19,7 @@ import pytest
 import ngraph as ng
 from ngraph.impl import Shape, Type
 from tests.test_ngraph.util import run_op_node, run_op_numeric_data
-from tests import xfail_issue_35929, xfail_issue_34323
+from tests import xfail_issue_35929, xfail_issue_34323, xfail_issue_36483
 
 
 @xfail_issue_35929
@@ -58,10 +58,73 @@ def test_unary_op_array(ng_api_fn, numpy_fn, range_start, range_end):
     result = run_op_node([input_data], ng_api_fn)
     assert np.allclose(result, expected, rtol=0.001)
 
+
+@xfail_issue_35929
+@pytest.mark.parametrize(
+    "ng_api_fn, numpy_fn, range_start, range_end",
+    [
+        (ng.absolute, np.abs, -1, 1),
+        (ng.abs, np.abs, -1, 1),
+        (ng.acos, np.arccos, -1, 1),
+        (ng.asin, np.arcsin, -1, 1),
+        (ng.atan, np.arctan, -100.0, 100.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),
+    ],
+)
+def test_unary_op_array_using_constants(ng_api_fn, numpy_fn, range_start, range_end):
+    np.random.seed(133391)
+    input_data = range_start + np.random.rand(2, 3, 4) * (range_end - range_start)
+    expected = numpy_fn(input_data)
+
     result = run_op_numeric_data(input_data, ng_api_fn)
     assert np.allclose(result, expected, rtol=0.001)
 
 
+@pytest.mark.skip(reason="Segmentation fault")
+@pytest.mark.parametrize(
+    "ng_api_fn, numpy_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), marks=xfail_issue_36483),
+        pytest.param(ng.ceil, np.ceil, np.float32(1.5), marks=xfail_issue_36483),
+        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)),
+    ],
+)
+def test_unary_op_scalar(ng_api_fn, numpy_fn, input_data):
+    expected = numpy_fn(input_data)
+
+    result = run_op_node([input_data], ng_api_fn)
+    assert np.allclose(result, expected)
+
+
 @xfail_issue_34323
 @pytest.mark.parametrize(
     "ng_api_fn, numpy_fn, input_data",
@@ -90,31 +153,34 @@ def test_unary_op_array(ng_api_fn, numpy_fn, range_start, range_end):
         (ng.tanh, np.tanh, np.float32(0.1234)),
     ],
 )
-def test_unary_op_scalar(ng_api_fn, numpy_fn, input_data):
+def test_unary_op_scalar_using_constants(ng_api_fn, numpy_fn, input_data):
     expected = numpy_fn(input_data)
 
-    result = run_op_node([input_data], ng_api_fn)
-    assert np.allclose(result, expected)
-
     result = run_op_numeric_data(input_data, ng_api_fn)
     assert np.allclose(result, expected)
 
 
-@xfail_issue_34323
 @pytest.mark.parametrize(
-    "input_data", [(np.array([True, False, True, False])), (np.array(True)), (np.array(False))]
+    "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)
+
+
+@xfail_issue_34323
+@pytest.mark.parametrize(
+    "input_data", [(np.array([True, False, True, False])), (np.array([True])), (np.array([False]))]
+)
+def test_logical_not_using_constants(input_data):
+    expected = np.logical_not(input_data)
+
     result = run_op_numeric_data(input_data, ng.logical_not)
     assert np.allclose(result, expected)
 
 
-@xfail_issue_34323
 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)
@@ -139,7 +205,6 @@ def test_softmax():
     assert np.allclose(result, expected)
 
 
-@xfail_issue_34323
 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]
@@ -147,6 +212,12 @@ def test_erf():
     result = run_op_node([input_tensor], ng.erf)
     assert np.allclose(result, expected)
 
+
+@xfail_issue_34323
+def test_erf_using_constants():
+    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_numeric_data(input_tensor, ng.erf)
     assert np.allclose(result, expected)
 
index 6c6078a..19ba7b9 100644 (file)
 # limitations under the License.
 # ******************************************************************************
 import numpy as np
+import pytest
 
 import ngraph as ng
 from tests.runtime import get_runtime
 from tests.test_ngraph.util import run_op_node
-from tests import xfail_issue_34323
+from tests import xfail_issue_36478
 
 
 def test_onehot():
@@ -33,7 +34,7 @@ def test_onehot():
     assert np.allclose(result, expected)
 
 
-@xfail_issue_34323
+@xfail_issue_36478
 def test_one_hot():
     data = np.array([0, 1, 2], dtype=np.int32)
     depth = 2
@@ -46,7 +47,7 @@ def test_one_hot():
     assert np.allclose(result, excepted)
 
 
-@xfail_issue_34323
+@pytest.mark.skip(reason="Segmentation fault")
 def test_range():
     start = 5
     stop = 35
index 611e143..54ccfc0 100644 (file)
@@ -21,6 +21,7 @@ import numpy as np
 import ngraph as ng
 from ngraph.utils.types import NumericData
 from tests.runtime import get_runtime
+from string import ascii_uppercase
 
 
 def _get_numpy_dtype(scalar):
@@ -45,8 +46,17 @@ def run_op_node(input_data, op_fun, *args):
     comp_args = []
     op_fun_args = []
     comp_inputs = []
-    for data in input_data:
-        op_fun_args.append(ng.constant(data, _get_numpy_dtype(data)))
+
+    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)