"static_shape" parameter fix. (#18843)

* Fixed static_shape param.

* Removed wrong change.

* Fixed unit test.

* Corrected mistakes in tests, switched on smart_reshape.

* Removed not used method.

* Corrected test.
This commit is contained in:
Anastasiia Pnevskaia 2023-08-02 11:03:38 +02:00 committed by GitHub
parent 099aaf5228
commit 0769af959e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 95 additions and 36 deletions

View File

@ -377,8 +377,8 @@ def create_tf_stateful_partioned_call_net(temp_dir):
param1 = ov.opset8.parameter(data_shape, dtype=np.float32)
param2 = ov.opset8.parameter(filters_shape, dtype=np.float32)
transpose2 = ov.opset8.transpose(param2, np.array([3, 2, 0, 1], dtype=np.int64))
conv = ov.opset11.convolution(param1, transpose2, strides, pads_begin, pads_end, dilations, auto_pad="same_upper")
reshape = ov.opset8.reshape(param2, np.array([1, 1, 3, 3], dtype=np.int64), True)
conv = ov.opset11.convolution(param1, reshape, strides, pads_begin, pads_end, dilations, auto_pad="same_upper")
parameter_list = [param1, param2]
model_ref = Model([conv], parameter_list, "test")
@ -639,6 +639,85 @@ def create_keras_layer_with_string_tensor(tmp_dir):
return model, model_ref, {}
def shape_of_const_fold_test(temp_dir):
import tensorflow as tf
tf.keras.backend.clear_session()
tf.compat.v1.reset_default_graph()
# TF model
x1 = tf.keras.Input(shape=[4, 10, 10], name="Input", batch_size=1)
shape = tf.shape(x1)
rank = tf.cast(tf.shape(shape), dtype=tf.float32)
reshape = tf.reshape(x1, shape)
mul = rank * reshape
keras_net = tf.keras.Model(inputs=[x1], outputs=[mul])
# Ref model
param1 = ov.opset8.parameter(PartialShape([1, 4, 10, 10]))
mul_const = ov.opset8.constant([[[[4]]]], dtype=np.float16)
cast = ov.opset8.convert(mul_const, np.float32)
mul = ov.opset8.multiply(cast, param1)
parameter_list = [param1]
model_ref = Model([mul], parameter_list, "test")
tf.keras.backend.clear_session()
return keras_net, model_ref, {}
def static_shape_true(temp_dir):
import tensorflow as tf
tf.keras.backend.clear_session()
tf.compat.v1.reset_default_graph()
# TF model
x1 = tf.keras.Input(shape=[4, 10, 10], name="Input", batch_size=1)
shape = tf.shape(x1)
rank = tf.cast(tf.shape(shape), dtype=tf.float32)
reshape = tf.reshape(x1, shape)
mul = rank * reshape
keras_net = tf.keras.Model(inputs=[x1], outputs=[mul])
# Ref model
param1 = ov.opset8.parameter(PartialShape([1, 4, 10, 10]))
mul_const = ov.opset8.constant([[[[4]]]], dtype=np.float32)
mul = ov.opset8.multiply(mul_const, param1)
parameter_list = [param1]
model_ref = Model([mul], parameter_list, "test")
tf.keras.backend.clear_session()
return keras_net, model_ref, {'use_convert_model_from_mo': True, 'static_shape': True}
def static_shape_false(temp_dir):
import tensorflow as tf
tf.keras.backend.clear_session()
tf.compat.v1.reset_default_graph()
# TF model
x1 = tf.keras.Input(shape=[4, 10, 10], name="Input", batch_size=1)
shape = tf.shape(x1)
rank = tf.cast(tf.shape(shape), dtype=tf.float32)
reshape = tf.reshape(x1, shape)
mul = rank * reshape
keras_net = tf.keras.Model(inputs=[x1], outputs=[mul])
# Ref model
param1 = ov.opset8.parameter(PartialShape([1, 4, 10, 10]))
shape_const = ov.opset8.constant([1, 4, 10, 10], dtype=np.int32)
reshape = ov.opset8.reshape(param1, shape_const, False)
mul_const = ov.opset8.constant([[[[4]]]], dtype=np.float32)
mul = ov.opset8.multiply(mul_const, reshape)
parameter_list = [param1]
model_ref = Model([mul], parameter_list, "test")
tf.keras.backend.clear_session()
return keras_net, model_ref, {'use_convert_model_from_mo': True, 'static_shape': False}
class TestMoConvertTF(CommonMOConvertTest):
test_data = [
@ -663,6 +742,9 @@ class TestMoConvertTF(CommonMOConvertTest):
create_keras_layer_with_tf_function_call_no_signature,
create_keras_layer_with_tf_function_call_no_signature_single_input,
create_keras_layer_with_string_tensor,
shape_of_const_fold_test,
static_shape_true,
static_shape_false,
# TF1
create_tf_graph,

View File

@ -113,7 +113,8 @@ def apply_offline_transformations(func: Model, argv: argparse.Namespace):
# Apply preprocessing (mean/scale/reverse_channels/convert_layout/etc)
apply_preprocessing(ov_function=func, argv=argv)
apply_moc_transformations(func)
from openvino._offline_transformations import apply_moc_transformations as moc_transformations # pylint: disable=import-error,no-name-in-module
moc_transformations(func, cf=argv.static_shape, smart_reshape=True)
params_with_custom_types = create_params_with_custom_types(argv.packed_user_shapes)
apply_moc_legacy_transformations(func, params_with_custom_types)

View File

@ -14,10 +14,12 @@ def moc_emit_ir(ngraph_function: Model, argv: argparse.Namespace):
apply_preprocessing(ov_function=ngraph_function, argv=argv)
# Apply transformations
from openvino.tools.mo.back.offline_transformations import apply_user_transformations, apply_moc_transformations, \
from openvino.tools.mo.back.offline_transformations import apply_user_transformations, \
apply_moc_legacy_transformations, apply_fused_names_cleanup
apply_moc_transformations(ngraph_function)
from openvino._offline_transformations import apply_moc_transformations # pylint: disable=import-error,no-name-in-module
apply_moc_transformations(ngraph_function, cf=argv.static_shape, smart_reshape=True)
from openvino._offline_transformations import compress_quantize_weights_transformation # pylint: disable=no-name-in-module,import-error
compress_quantize_weights_transformation(ngraph_function)

View File

@ -52,7 +52,8 @@ def arg_parse_helper(input_model,
data_type=None,
tensorflow_custom_operations_config_update=None,
compress_to_fp16=compress_to_fp16,
extensions=None
extensions=None,
static_shape=False
)

View File

@ -8,16 +8,14 @@ from openvino.tools.ovc.moc_frontend.preprocessing import apply_preprocessing
def moc_emit_ir(ngraph_function: Model, argv: argparse.Namespace):
from openvino._offline_transformations import compress_quantize_weights_transformation, apply_moc_transformations # pylint: disable=no-name-in-module,import-error
from openvino.tools.ovc.moc_frontend.offline_transformations import apply_moc_legacy_transformations, apply_fused_names_cleanup
# Apply preprocessing (mean/scale/reverse_channels/convert_layout/etc)
apply_preprocessing(ov_function=ngraph_function, argv=argv)
# Apply transformations
from openvino.tools.ovc.moc_frontend.offline_transformations import apply_user_transformations, apply_moc_transformations, \
apply_moc_legacy_transformations, apply_fused_names_cleanup
apply_moc_transformations(ngraph_function)
from openvino._offline_transformations import compress_quantize_weights_transformation # pylint: disable=no-name-in-module,import-error
apply_moc_transformations(ngraph_function, cf=True, smart_reshape=True)
compress_quantize_weights_transformation(ngraph_function)
if argv.framework == "onnx": # TODO: Consider removing

View File

@ -87,11 +87,6 @@ def apply_user_transformations(func: object, transforms: list):
available_transformations[name](func, **args)
def apply_moc_transformations(func: object):
from openvino._offline_transformations import apply_moc_transformations # pylint: disable=import-error,no-name-in-module
apply_moc_transformations(func, cf=False, smart_reshape=True)
def apply_moc_legacy_transformations(func: object, params_with_custom_types: List[str]):
from openvino._offline_transformations import apply_moc_legacy_transformations # pylint: disable=import-error,no-name-in-module
apply_moc_legacy_transformations(func, params_with_custom_types)
@ -104,23 +99,3 @@ def compress_model(func: object):
def apply_fused_names_cleanup(func: object):
from openvino._offline_transformations import apply_fused_names_cleanup # pylint: disable=import-error,no-name-in-module
apply_fused_names_cleanup(func)
def apply_offline_transformations(func: Model, argv: argparse.Namespace):
from openvino.tools.ovc.moc_frontend.preprocessing import apply_preprocessing # pylint: disable=no-name-in-module,import-error
# Apply preprocessing (mean/scale/reverse_channels/convert_layout/etc)
apply_preprocessing(ov_function=func, argv=argv)
apply_moc_transformations(func)
params_with_custom_types = create_params_with_custom_types(argv.packed_user_shapes)
apply_moc_legacy_transformations(func, params_with_custom_types)
if "compress_to_fp16" in argv and argv.compress_to_fp16:
compress_model(func)
apply_fused_names_cleanup(func)
return func