Skip to content

Commit

Permalink
generalize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-esir committed Feb 7, 2024
1 parent 5bdff7f commit 4aae998
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 19 deletions.
25 changes: 18 additions & 7 deletions tests/layer_tests/common/utils/common_utils.py
Expand Up @@ -48,15 +48,26 @@ def generate_ir(coverage=False, **kwargs):


def generate_ir_python_api(coverage=False, **kwargs):
from openvino.runtime import serialize
from openvino.tools.mo import convert_model

out_dir = kwargs['output_dir'] + os.sep + kwargs['model_name'] + ".xml"

# TODO: Remove usage of legacy params from layer tests and switch to convert_model from tools.ovc
ov_model = convert_model(**kwargs)
serialize(ov_model, out_dir)

if getattr(kwargs, 'use_legacy_frontend', False):
from openvino.runtime import serialize
from openvino.tools.mo import convert_model
# TODO: Remove usage of legacy params from layer tests and switch to convert_model from tools.ovc
ov_model = convert_model(**kwargs)
serialize(ov_model, out_dir)
else:
from openvino.runtime import save_model
from openvino.tools.ovc import convert_model

compress_to_fp16 = getattr(kwargs, 'compress_to_fp16', True)
import inspect
for key in list(kwargs.keys()):
if key not in inspect.signature(convert_model).parameters.keys():
kwargs.pop(key)

ov_model = convert_model(**kwargs)
save_model(ov_model, out_dir, compress_to_fp16)
return 0, ""


Expand Down
54 changes: 54 additions & 0 deletions tests/layer_tests/tensorflow_tests/test_tf_Floor.py
@@ -0,0 +1,54 @@
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import numpy as np
import pytest

from common.tf_layer_test_class import CommonTFLayerTest
from common.utils.tf_utils import permute_nchw_to_nhwc


class TestFloor(CommonTFLayerTest):
def create_add_placeholder_const_net(self, x_shape, dtype, ir_version, use_new_frontend):
import tensorflow as tf

tf.compat.v1.reset_default_graph()

# Create the graph and model
with tf.compat.v1.Session() as sess:
x = tf.compat.v1.placeholder(dtype, x_shape, 'Input')
res = tf.raw_ops.Floor(x=x)

tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def

ref_net = None

return tf_net, ref_net

def _prepare_input(self, inputs_dict):
for input in inputs_dict.keys():
inputs_dict[input] = np.array([0.1, 0.2, 0.5, 0.55, 0.9, -0.1, -0.6, -0.9]).astype(np.float32)
return inputs_dict

# TODO: implement tests for 2 Consts + Add

test_data_1D = [
dict(x_shape=[8], dtype=np.float32),
# dict(x_shape=[], dtype=np.int32),
# dict(x_shape=[2], dtype=np.int64),
# dict(x_shape=[2, 4, 5], dtype=np.int32),
# dict(x_shape=[], dtype=np.float32),
# dict(x_shape=[2], dtype=np.float64),
# dict(x_shape=[2, 4, 5], dtype=np.float32),
]

@pytest.mark.parametrize("params", test_data_1D)
@pytest.mark.nightly
@pytest.mark.precommit_tf_fe
def test_add_placeholder_const_1D(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend):
self._test(*self.create_add_placeholder_const_net(**params, ir_version=ir_version,
use_new_frontend=use_new_frontend),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_new_frontend=use_new_frontend)
80 changes: 68 additions & 12 deletions tests/layer_tests/tensorflow_tests/test_tf_FloorDiv.py
Expand Up @@ -7,17 +7,18 @@
from common.tf_layer_test_class import CommonTFLayerTest


class TestFloorDiv(CommonTFLayerTest):
def create_add_placeholder_const_net(self, x_shape, dtype, ir_version, use_new_frontend):
class TestFloorDivRandomInputs(CommonTFLayerTest):
def create_add_placeholder_const_net(self, x_shape, dtype, ir_version, use_new_frontend, **kwargs):
import tensorflow as tf

tf.compat.v1.reset_default_graph()

# Create the graph and model
with tf.compat.v1.Session() as sess:
x = tf.compat.v1.placeholder(dtype, x_shape, 'Input')
constant_value = np.array(-256).astype(dtype)
constant_value = np.array(10).astype(dtype)
y = tf.constant(constant_value)
x = tf.raw_ops.Abs(x=x)
res = tf.raw_ops.FloorDiv(x=x, y=y)

tf.compat.v1.global_variables_initializer()
Expand All @@ -27,29 +28,84 @@ def create_add_placeholder_const_net(self, x_shape, dtype, ir_version, use_new_f

return tf_net, ref_net

def _prepare_input(self, inputs_dict):
for input in inputs_dict.keys():
inputs_dict[input] = np.random.randint(-100000, 100000, inputs_dict[input]).astype(np.float32)
return inputs_dict

# TODO: implement tests for 2 Consts + Add

test_data_1D = [
random_data_inputs = [
dict(x_shape=[], dtype=np.int32),
dict(x_shape=[2], dtype=np.int64),
dict(x_shape=[2, 4, 5], dtype=np.int32),
dict(x_shape=[2, 1000], dtype=np.int32),
dict(x_shape=[], dtype=np.float32),
dict(x_shape=[2], dtype=np.float64),
dict(x_shape=[2, 4, 5], dtype=np.float32),
]

@pytest.mark.parametrize("params", test_data_1D)

@pytest.mark.parametrize("params", random_data_inputs)
@pytest.mark.nightly
@pytest.mark.precommit_tf_fe
def test_add_placeholder_const_1D(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend):
self._test(*self.create_add_placeholder_const_net(**params, ir_version=ir_version,
use_new_frontend=use_new_frontend),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_new_frontend=use_new_frontend)
use_new_frontend=use_new_frontend)

class TestFloorDiv(CommonTFLayerTest):
def create_flordiv_tf_net(self, min, max, step, y, dtype, ir_version, use_new_frontend, **kwargs):
import tensorflow as tf

x = np.arange(min, max, step, dtype=dtype)
x_shape = x.reshape(kwargs['x_shape']).shape if 'x_shape' in kwargs else x.shape

tf.compat.v1.reset_default_graph()

with tf.compat.v1.Session() as sess:
x = tf.compat.v1.placeholder(dtype, x_shape, 'Input')
y = tf.constant(np.array(y).astype(dtype))
res = tf.raw_ops.FloorDiv(x=x, y=y)

tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def

ref_net = None

return tf_net, ref_net

def _prepare_input(self, inputs_dict, kwargs):
for input in inputs_dict.keys():
inputs_dict[input] = np.arange(kwargs['min'], kwargs['max'], kwargs['step'], dtype=kwargs['dtype'])
if 'x_shape' in kwargs:
inputs_dict[input] = inputs_dict[input].reshape(kwargs['x_shape'])
return inputs_dict

test_inputs = [
# test for integers
dict(min=-20, max=20, step=1, y=[10],dtype=np.int32),
dict(min=-10000, max=10000, step=100, y=[10000], dtype=np.int32),
dict(min=-10000, max=10000, step=100, y=[-10000], dtype=np.int32),
dict(min=-1e5, max=1e5, step=100, y=[1e5], dtype=np.int32),
dict(min=-1e8, max=1e8, step=1e4, y=[1e5], dtype=np.int32),
dict(min=-1e8, max=1e8, step=1e4, y=[-1e5], dtype=np.int32),

# test for multidimensinal input
dict(min=-10000, max=10000, step=10, y=[10000], x_shape=[20, -1], dtype=np.int32),
dict(min=-10000, max=10000, step=10, y=[10000], x_shape=[2, 5, -1], dtype=np.int32),
dict(min=-10000, max=10000, step=1, y=[10000], x_shape=[2, 5, 10, -1], dtype=np.int32),

# test for floats
dict(min=-20, max=20, step=1, y=[10],dtype=np.float32),
dict(min=-10000, max=10000, step=100, y=[10000], dtype=np.float32),
dict(min=-10, max=10, step=10, y=[100], dtype=np.float64),
dict(min=-1e5, max=1e5, step=100, y=[1e5], dtype=np.float32),
dict(min=-1e8, max=1e8, step=1e4, y=[1e5], dtype=np.float32),
dict(min=-1e8, max=1e8, step=1e4, y=[-1e5], dtype=np.float32),
]
@pytest.mark.parametrize("params", test_inputs)
@pytest.mark.nightly
@pytest.mark.precommit_tf_fe
def test_floordiv(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend):
self._test(*self.create_flordiv_tf_net(**params, ir_version=ir_version,
use_new_frontend=use_new_frontend),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_new_frontend=use_new_frontend, kwargs_to_prepare_input=params)

0 comments on commit 4aae998

Please sign in to comment.