Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MO] relax condition for casting into FP16 #8083

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions model-optimizer/extensions/back/ChangeOutputTypeAttributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ def assert_that_is_castable_to_fp16(node: Node):
val = node.in_port(i).data.get_value()
if val is None:
return

if np.any(val > np.finfo(np.float16).max) or np.any(val < np.finfo(np.float16).min):
raise Error("Try to convert with --data_type=FP32 argument. "
"This model can not be converted to FP16 precision, since "
"'{}' node value {} exceeds FP16 allowed limits: [{}, {}]"
.format(node_name, val, np.finfo(np.float16).min, np.finfo(np.float16).max))
# some models have -Inf values but nevertheless are correctly inferred in FP16
# we should not raise an Error here but instead show a warning
log.error("value '{}' of '{}' node exceeds FP16 limits: [{}, {}]. "
pavel-esir marked this conversation as resolved.
Show resolved Hide resolved
"This may lead to incorrect results of inference or may not be a problem, "
"depending on the model.".format(
val, node_name, np.finfo(np.float16).min, np.finfo(np.float16).max), extra={'is_warning': True})
# further this input values will be rewritten since force_shape_inference=True
node.in_port(i).data.set_value(val.astype(np.float16))
pavel-esir marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,13 @@ def test_range_different_values(self):
graph, graph_ref = build_range_test_graphs(start=0, limit=50000, delta=1, dst_type_str='FP16')
self.assertRaises(Error, ChangeOutputTypeAttributes().find_and_replace_pattern, graph)

def test_range_out_of_fp16_max(self):
graph, graph_ref = build_range_test_graphs(start=0, limit=100000, delta=1, dst_type_str='FP16')
self.assertRaises(Error, ChangeOutputTypeAttributes().find_and_replace_pattern, graph)

def test_range_out_of_fp16_min(self):
graph, graph_ref = build_range_test_graphs(start=0, limit=-100000, delta=-1, dst_type_str='FP16')
self.assertRaises(Error, ChangeOutputTypeAttributes().find_and_replace_pattern, graph)

def test_cast_correct_case(self):
input_data = np.array([0, 1000, 4, 9, 0])
graph, graph_ref = build_cast_test_graphs(input_data, dst_type_str='FP16')
ChangeOutputTypeAttributes().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'res', check_op_attrs=True)
self.assertTrue(flag, resp)

def test_cast_out_of_fp16_max(self):
input_data = np.array([0, 100000, 4, 9, 0])
graph, graph_ref = build_cast_test_graphs(input_data, dst_type_str='FP16')
self.assertRaises(Error, ChangeOutputTypeAttributes().find_and_replace_pattern, graph)

def test_cast_out_of_fp16_min(self):
input_data = np.array([0, -100000, 4, 9, 0])
graph, graph_ref = build_cast_test_graphs(input_data, dst_type_str='FP16')
self.assertRaises(Error, ChangeOutputTypeAttributes().find_and_replace_pattern, graph)


def build_range_test_graphs(start=0, limit=10, delta=1, dst_type_str='FP16',
src_type_str='FP32', returns_shape_value=None):
Expand Down