Skip to content

Commit

Permalink
revertback:- freeze_placeholder_test.py
Browse files Browse the repository at this point in the history
  • Loading branch information
PRATHAM-SPS committed Oct 9, 2023
1 parent 62cc056 commit b07a587
Showing 1 changed file with 148 additions and 151 deletions.
299 changes: 148 additions & 151 deletions tools/mo/unit_tests/mo/utils/freeze_placeholder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import numpy as np
import onnx
from generator import generator, generate
from onnx.helper import make_graph, make_model, make_tensor_value_info

from openvino.frontend import (
Expand Down Expand Up @@ -51,7 +52,7 @@ def base_args_config(use_legacy_fe: bool = None, use_new_fe: bool = None):

try:
import openvino_telemetry as tm

from openvino_telemetry.backend import backend_ga4
except ImportError:
import openvino.tools.mo.utils.telemetry_stub as tm

Expand All @@ -60,6 +61,7 @@ def get_test_default_frontends():
return {"onnx": "new", "tf": "legacy"}


@generator
class TestMoFreezePlaceholder(unittest.TestCase):
def setUp(self):
tm.Telemetry.__init__ = Mock(return_value=None)
Expand Down Expand Up @@ -122,185 +124,180 @@ def tearDown(self):
for name in self.models.keys():
os.remove(name)

def test_freeze_placeholder_with_value_onnx_fe(self):
test_cases = [
@generate(
*[
(
"in1[1 4]{f32}->[1.0 2.0 3.0 4.0],in2[1 4]{f32}->[1.0 2.0 3.0 4.0]",
True,
{},
np.array([2.0, 4.0, 6.0, 8.0]),
np.float32,
"in1[1 4]{f32}->[1.0 2.0 3.0 4.0],in2[1 4]{f32}->[1.0 2.0 3.0 4.0]",
True,
{},
np.array([2.0, 4.0, 6.0, 8.0]),
np.float32,
),
(
"in2{f32}->[0.0 0.0 0.0 0.0]",
True,
{"in1": np.array([[1.0, 2.0], [3.0, 4.0]])},
np.array([[1.0, 2.0], [3.0, 4.0]]),
np.float32,
"in2{f32}->[0.0 0.0 0.0 0.0]",
True,
{"in1": np.array([[1.0, 2.0], [3.0, 4.0]])},
np.array([[1.0, 2.0], [3.0, 4.0]]),
np.float32,
),
(
"in2{f32}->[1.0 15.0 15.5 1.0]",
True,
{"in1": np.array([[2.0, 4.0], [12.0, 8.0]])},
np.array([[3.0, 19.0], [27.5, 9.0]]),
np.float32,
"in2{f32}->[1.0 15.0 15.5 1.0]",
True,
{"in1": np.array([[2.0, 4.0], [12.0, 8.0]])},
np.array([[3.0, 19.0], [27.5, 9.0]]),
np.float32,
),
(
"in1[1 4]{i32}->[1 2 3 4],in2[1 4]{i32}->[1 2 3 4]",
True,
{},
np.array([2.0, 4.0, 6.0, 8.0]),
np.int32,
"in1[1 4]{i32}->[1 2 3 4],in2[1 4]{i32}->[1 2 3 4]",
True,
{},
np.array([2.0, 4.0, 6.0, 8.0]),
np.int32,
),
]

],
)
def test_freeze_placeholder_with_value_onnx_fe(self, input_freezing_value, use_new_fe, inputs, expected,
dtype=None):
with patch("openvino.tools.mo.convert_impl.get_default_frontends") as default_fe:
default_fe.return_value = get_test_default_frontends()

for idx, (input_freezing_value, use_new_fe, inputs, expected, dtype) in enumerate(test_cases):
with self.subTest(test_case_idx=idx):
args = base_args_config(use_new_fe=use_new_fe)
args.input_model = "test_model.onnx"
args.input = input_freezing_value

_, model = prepare_ir(args)

ie = Core()
exec_net = ie.compile_model(model, "CPU")
req = exec_net.create_infer_request()
results = req.infer(inputs)
values = list(results.values())[0]
if dtype is not None:
self.assertEqual(values.dtype, dtype)
self.assertTrue(np.allclose(values, expected))

def test_freeze_placeholder_with_value_mul(self):
test_cases = [
args = base_args_config(use_new_fe=use_new_fe)
args.input_model = "test_model.onnx"
args.input = input_freezing_value

_, model = prepare_ir(args)

ie = Core()
exec_net = ie.compile_model(model, "CPU")
req = exec_net.create_infer_request()
results = req.infer(inputs)
values = list(results.values())[0]
if dtype is not None:
assert values.dtype == dtype
assert np.allclose(values, expected)

@generate(
*[
(
"in1{f32}->[1.0 15.0 1.0]",
True,
{"in2": np.array([2])},
np.array([2.0, 30.0, 2.0]),
np.float32,
"in1{f32}->[1.0 15.0 1.0]",
True,
{"in2": np.array([2])},
np.array([2.0, 30.0, 2.0]),
np.float32,
),
(
"in1{f32}->[7.0 11.0 -1.0],in2{f32}->3.0",
True,
{},
np.array([21.0, 33.0, -3.0]),
np.float32,
"in1{f32}->[7.0 11.0 -1.0],in2{f32}->3.0",
True,
{},
np.array([21.0, 33.0, -3.0]),
np.float32,
),
(
None,
True,
{
"in1": np.array([2.0, 2.0, 2.0]).reshape(1, 1, 3),
"in2": np.array([-1.0]),
},
np.array([-2.0, -2.0, -2.0]),
np.float32,
None,
True,
{
"in1": np.array([2.0, 2.0, 2.0]).reshape(1, 1, 3),
"in2": np.array([-1.0]),
},
np.array([-2.0, -2.0, -2.0]),
np.float32,
),
(
"in1[3 1]{f32}->[7.0 11.0 -1.0],in2{f32}->3.0",
True,
{},
np.array([21.0, 33.0, -3.0]).reshape(3, 1),
np.float32,
"in1[3 1]{f32}->[7.0 11.0 -1.0],in2{f32}->3.0",
True,
{},
np.array([21.0, 33.0, -3.0]).reshape(3, 1),
np.float32,
),
(
"in1[3 1]{f16}->[7.0 11.0 -1.0],in2{f16}->3.0",
True,
{},
np.array([21.0, 33.0, -3.0]).reshape(3, 1),
np.float16,
"in1[3 1]{f16}->[7.0 11.0 -1.0],in2{f16}->3.0",
True,
{},
np.array([21.0, 33.0, -3.0]).reshape(3, 1),
np.float16,
),
(
"in1[3 1]{i32}->[7 11 -1],in2{i32}->3.0",
True,
{},
np.array([21, 33, -3]).reshape(3, 1),
np.int32,
"in1[3 1]{i32}->[7 11 -1],in2{i32}->3.0",
True,
{},
np.array([21, 33, -3]).reshape(3, 1),
np.int32,
),
]

],
)
def test_freeze_placeholder_with_value_mul(self, input_freezing_value, use_new_fe, inputs, expected, dtype=None):
with patch("openvino.tools.mo.convert_impl.get_default_frontends") as default_fe:
default_fe.return_value = get_test_default_frontends()

for idx, (input_freezing_value, use_new_fe, inputs, expected, dtype) in enumerate(test_cases):
with self.subTest(test_case_idx=idx):
args = base_args_config(use_new_fe=use_new_fe)
args.input_model = "test_model_2.onnx"
args.input = input_freezing_value

_, model = prepare_ir(args)

ie = Core()
exec_net = ie.compile_model(model, "CPU")
req = exec_net.create_infer_request()
results = req.infer(inputs)
values = list(results.values())[0]
if dtype is not None:
self.assertEqual(values.dtype, dtype)
self.assertTrue(np.allclose(values, expected))

def test_value_without_type(self):
test_cases = [
args = base_args_config(use_new_fe=use_new_fe)
args.input_model = "test_model_2.onnx"
args.input = input_freezing_value

_, model = prepare_ir(args)

ie = Core()
exec_net = ie.compile_model(model, "CPU")
req = exec_net.create_infer_request()
results = req.infer(inputs)
values = list(results.values())[0]
if dtype is not None:
assert values.dtype == dtype
assert np.allclose(values, expected)

@generate(
*[
(
"in1->[1.0 15.0 1.0]",
True,
{"in2": np.array([2])},
np.array([2.0, 30.0, 2.0]),
np.float32,
"in1->[1.0 15.0 1.0]",
True,
{"in2": np.array([2])},
np.array([2.0, 30.0, 2.0]),
np.float32,
),
]

],
)
def test_value_without_type(self, input_freezing_value, use_new_fe, inputs, expected,
dtype=None):
with patch("openvino.tools.mo.convert_impl.get_default_frontends") as default_fe:
default_fe.return_value = get_test_default_frontends()

for idx, (input_freezing_value, use_new_fe, inputs, expected, dtype) in enumerate(test_cases):
with self.subTest(test_case_idx=idx):
args = base_args_config(use_new_fe=use_new_fe)
args.input_model = "test_model_2.onnx"
args.input = input_freezing_value

_, model = prepare_ir(args)

ie = Core()
exec_net = ie.compile_model(model, "CPU")
req = exec_net.create_infer_request()
results = req.infer(inputs)
values = list(results.values())[0]
if dtype is not None:
self.assertEqual(values.dtype, dtype)
self.assertTrue(np.allclose(values, expected))

def test_value_without_type_int32(self):
test_cases = [
args = base_args_config(use_new_fe=use_new_fe)
args.input_model = "test_model_2.onnx"
args.input = input_freezing_value

_, model = prepare_ir(args)

ie = Core()
exec_net = ie.compile_model(model, "CPU")
req = exec_net.create_infer_request()
results = req.infer(inputs)
values = list(results.values())[0]
if dtype is not None:
assert values.dtype == dtype
assert np.allclose(values, expected)

@generate(
*[
(
"in2->[3 2 5]",
True,
{"in1": np.array([[2, 1, 3], [1, 5, 6]], dtype=np.int32)},
np.array([[6, 2, 15], [3, 10, 30]], dtype=np.int32),
np.int32,
"in2->[3 2 5]",
True,
{"in1": np.array([[2, 1, 3], [1, 5, 6]], dtype=np.int32)},
np.array([[6, 2, 15], [3, 10, 30]], dtype=np.int32),
np.int32,
),
]

],
)
def test_value_without_type_int32(self, input_freezing_value, use_new_fe, inputs, expected,
dtype=None):
with patch("openvino.tools.mo.convert_impl.get_default_frontends") as default_fe:
default_fe.return_value = get_test_default_frontends()

for idx, (input_freezing_value, use_new_fe, inputs, expected, dtype) in enumerate(test_cases):
with self.subTest(test_case_idx=idx):
args = base_args_config(use_new_fe=use_new_fe)
args.input_model = "test_model_int.onnx"
args.input = input_freezing_value

_, model = prepare_ir(args)

ie = Core()
exec_net = ie.compile_model(model, "CPU")
req = exec_net.create_infer_request()
results = req.infer(inputs)
values = list(results.values())[0]
if dtype is not None:
self.assertEqual(values.dtype, dtype)
self.assertTrue(np.allclose(values, expected))
args = base_args_config(use_new_fe=use_new_fe)
args.input_model = "test_model_int.onnx"
args.input = input_freezing_value

_, model = prepare_ir(args)

ie = Core()
exec_net = ie.compile_model(model, "CPU")
req = exec_net.create_infer_request()
results = req.infer(inputs)
values = list(results.values())[0]
if dtype is not None:
assert values.dtype == dtype
assert np.allclose(values, expected)

0 comments on commit b07a587

Please sign in to comment.