enable --compress_to_fp16=True by default in MO (#15488)

* enable --compress_to_fp16 by default in MO

* corrected docs, added warning if user did't specify --compress_to_fp16 explicitly

* fix failing MO unit-tests

* do not wipe out data_type if user defined it explicitly by cli argument

* updated warning message and docs

* corrected phrasing

* corrected phrasing in FP16_Compression.md

* set compress_to_fp16=False for convert tests

* leftover: set compress_to_fp16=False for convert tests

* minor correction

* print info message in main.py, som minor changes

* typos fix

* fix losing information whether arguments set by user or got from defaults

* returned back default values instead of None

* more selective correcting of test_mo_convert_pytorch.py; added test for cases when compression is enabled/disabled or left by default

* fix test_mo_convert_pytorch.py
This commit is contained in:
Pavel Esir authored and GitHub committed 2023-02-21 13:07:43 +01:00
1 parent 94b64fed79
commit 15973fd2da
7 files changed
+154 -21

No files matched your search

+7 -5
View File
@@ -214,12 +214,14 @@ def arguments_post_parsing(argv: argparse.Namespace):
log.error(e)
raise_ie_not_found()
if ('data_type' in argv and argv.data_type in ['FP16', 'half']) or \
('compress_to_fp16' in argv and argv.compress_to_fp16 is True):
argv.data_type = 'FP32'
argv.compress_fp16 = True
else:
# Turn off compression only if it's disabled explicitly by --compress_to_fp16=False or --data_type=FP32.
# By default, in all other cases compression is enabled
if ('data_type' in argv and argv.data_type in ['FP32', 'float']) or \
('compress_to_fp16' in argv and argv.compress_to_fp16 is False):
argv.compress_fp16 = False
else:
argv.compress_fp16 = True
argv.data_type = 'FP32' # if compression was enabled will be restored back to 'FP16' after apply_offline_transformations
# This is just to check that transform key is valid and transformations are available
check_available_transforms(parse_transform(argv.transform))
+5 -2
View File
@@ -10,10 +10,9 @@ try:
import openvino_telemetry as tm
except ImportError:
import openvino.tools.mo.utils.telemetry_stub as tm
from openvino.tools.mo.utils.get_ov_update_message import get_compression_message
from openvino.tools.mo.convert_impl import _convert
from openvino.tools.mo.pipeline.common import get_ir_version
from openvino.tools.mo.utils.cli_parser import get_model_name_from_args
from openvino.tools.mo.utils.logger import init_logger
from openvino.tools.mo.utils.error import Error, FrameworkError
import traceback
@@ -38,6 +37,10 @@ def main(cli_parser: argparse.ArgumentParser, framework=None):
try:
ngraph_function, argv = _convert(cli_parser, framework, {})
is_tf, _, _, _, _ = deduce_legacy_frontend_by_namespace(argv)
if ('compress_to_fp16' in argv and argv.compress_to_fp16) \
or ('data_type' in argv and argv.data_type in ['half', 'FP16']):
print(get_compression_message())
ov_update_message = get_ov_update_message()
ov_api20_message = get_ov_api20_message()
if ov_update_message is not None:
@@ -559,7 +559,9 @@ mo_convert_params = {
' used to fuse dimensions, for example "[n,c,...]->[n*c,...]".', '', '', layout_param_to_str),
'compress_to_fp16': ParamDescription(
'If the original model has FP32 weights or biases, they are compressed to FP16. '
'All intermediate data is kept in original precision.', '', '', None),
'All intermediate data is kept in original precision. Option can be specified alone as "--compress_to_fp16", '
'or explicit True/False values can be set, for example: "--compress_to_fp16=False", or "--compress_to_fp16=True"',
'', '', None),
'transform': ParamDescription(
'Apply additional transformations. {}' +
'"--transform transformation_name1[args],transformation_name2..." ' +
@@ -1017,14 +1019,14 @@ def get_common_cli_parser(parser: argparse.ArgumentParser = None):
'FP32 model weights and biases are compressed to FP16. '
'All intermediate data is kept in original precision.',
choices=["FP16", "FP32", "half", "float"],
default='float',
default='FP16',
action=DeprecatedOptionCommon)
common_group.add_argument('--compress_to_fp16',
help=mo_convert_params_common['compress_to_fp16'].description,
type=check_bool,
nargs="?",
const=True,
default=False)
default=True)
common_group.add_argument('--transform',
help=mo_convert_params_common['transform'].description.format(
mo_convert_params_common['transform'].possible_types_command_line),
@@ -32,3 +32,12 @@ def get_tf_fe_message():
'Find more information about new TensorFlow Frontend at {}'.format(link)
return message
def get_compression_message():
link = "https://docs.openvino.ai/latest/openvino_docs_MO_DG_FP16_Compression.html"
message = '[ INFO ] Generated IR will be compressed to FP16. ' \
'If you get lower accuracy, please consider disabling compression explicitly ' \
'by adding argument --compress_to_fp16=False.\n' \
'Find more information about compression to FP16 at {}'.format(link)
return message
@@ -9,14 +9,15 @@ from contextlib import redirect_stdout
from unittest.mock import patch
from openvino.tools.mo.main import main
from openvino.tools.mo.utils.get_ov_update_message import get_tf_fe_message
from openvino.tools.mo.utils.get_ov_update_message import get_tf_fe_message, get_compression_message
def arg_parse_helper(input_model,
use_legacy_frontend,
use_new_frontend,
input_model_is_text,
framework):
framework,
compress_to_fp16=False):
path = os.path.dirname(__file__)
input_model = os.path.join(path, "test_models", input_model)
@@ -48,6 +49,7 @@ def arg_parse_helper(input_model,
freeze_placeholder_with_value=None,
data_type=None,
tensorflow_custom_operations_config_update=None,
compress_to_fp16=compress_to_fp16,
)
@@ -63,3 +65,18 @@ class TestInfoMessagesTFFE(unittest.TestCase):
std_out = f.getvalue()
tf_fe_message_found = get_tf_fe_message() in std_out
assert tf_fe_message_found
class TestInfoMessagesCompressFP16(unittest.TestCase):
@patch('argparse.ArgumentParser.parse_args',
return_value=arg_parse_helper(input_model="model_int32.pbtxt",
use_legacy_frontend=False, use_new_frontend=True,
compress_to_fp16=True,
framework=None, input_model_is_text=True))
def test_compress_to_fp16(self, mock_argparse):
f = io.StringIO()
with redirect_stdout(f):
main(argparse.ArgumentParser())
std_out = f.getvalue()
fp16_compression_message_found = get_compression_message() in std_out
assert fp16_compression_message_found