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] Range output_type correction for FP16 #6590

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions model-optimizer/automation/package_BOM.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extensions/back/AvgPool.py
extensions/back/blob_normalizer.py
extensions/back/CellNormalizer.py
extensions/back/ChangeCastOutputType.py
extensions/back/ChangeRangeOutputType.py
extensions/back/ClampNormalizer.py
extensions/back/compress_quantized_weights.py
extensions/back/ConvolutionNormalizer.py
Expand Down
73 changes: 73 additions & 0 deletions model-optimizer/extensions/back/ChangeRangeOutputType.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import logging as log

import numpy as np

from mo.back.replacement import BackReplacementPattern
from mo.graph.graph import Graph, Node
from mo.middle.passes.convert_data_type import data_type_str_to_np
from mo.utils.error import Error


class ChangeRangeOutputType(BackReplacementPattern):
"""
Change Range 'output_type' from fp64 to fp32 since not all plugins support fp64 data type.
pavel-esir marked this conversation as resolved.
Show resolved Hide resolved
And from fp32 to fp16 when generating IR for fp16. Before changing precision to FP16 ensures that it's possible
"""
enabled = True
force_shape_inference = True

def run_after(self):
from extensions.back.MarkNodesWithShapeValues import MarkNodesWithShapeValues
return [MarkNodesWithShapeValues]

def run_before(self):
return []

def find_and_replace_pattern(self, graph: Graph):
ir_data_type = data_type_str_to_np(graph.graph['cmd_params'].data_type)

for node in graph.get_op_nodes(op='Range'):
node_name = node.soft_get('name', node.id)
assert node.has_valid('output_type')

final_type = None
if node.output_type == np.float64:
final_type = np.float32
if node.output_type in [np.float32, np.float64] and ir_data_type == np.float16:
final_type = np.float16

if final_type == np.float16:
assert_that_is_castable_to_fp16(node)

if final_type is not None:
node.output_type = final_type
log.warning('Change output_type from {} to {} for Range node {}'.format(
node.output_type, final_type, node_name))


def assert_that_is_castable_to_fp16(node: Node):
node_name = node.soft_get('name', node.id)
start, limit, delta = [node.in_port(i).data.get_value() for i in range(3)]
for val in [start, limit, delta]:
if val > np.finfo(np.float16).max or 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 "
"Range node '{}' input value {} exceeds FP16 allowed limits: [{}, {}]"
.format(node_name, val, np.finfo(np.float16).min, np.finfo(np.float16).max))

start, limit, delta = [node.in_port(i).data.get_value().astype(np.float16) for i in range(3)]
casted_output = np.arange(start, limit, delta, dtype=node['output_type'])
original_output = node.out_port(0).data.get_value()
if len(original_output) != len(casted_output):
raise Error("Try to convert with --data_type=FP32 argument."
"This model can not be converted to FP16 precision, since "
"after changing Range node '{}' dtype to FP16 output shape {} differs from original {}."
.format(node_name, len(casted_output), len(original_output)))

diff_count = np.count_nonzero(np.subtract(original_output, casted_output) > 1.e-4)
if diff_count > 0:
log.warning("{} elements of {} of Range node '{}' output differ from the original values while "
"converting network to FP16 precision".format(diff_count, len(original_output), node_name))
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
'Tile': [1], # repeats input
'TopK': [1], # K input
'Pad': [1, 2], # pads_begin, pads_end
'Range': [0, 1, 2], # start, stop, step inputs
'OneHot': [1], # depth input
}

Expand Down
2 changes: 0 additions & 2 deletions model-optimizer/extensions/ops/range.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import logging as log

import numpy as np

from mo.graph.graph import Node, Graph
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import unittest
from copy import deepcopy

import numpy as np

from extensions.back.ChangeRangeOutputType import ChangeRangeOutputType
from extensions.ops.range import Range
from mo.front.common.partial_infer.utils import float32_array
from mo.middle.passes.convert_data_type import convert_blobs, data_type_str_to_np
from mo.middle.passes.infer import partial_infer
from mo.utils.ir_engine.compare_graphs import compare_graphs
from unit_tests.utils.graph import build_graph, result, regular_op_with_empty_data, connect
from unit_tests.utils.graph import valued_const_with_data


class ChangeRangeOutputTypeTests(unittest.TestCase):

def test_correct_case(self):
graph, graph_ref = build_test_graphs(start=0, limit=10, delta=1, dst_type_str='FP16')
ChangeRangeOutputType().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'res', check_op_attrs=True)
self.assertTrue(flag, resp)

# starting from ~1000 FP16 absolute difference between neighbor values is more than 1
# fails because of shape inconsistency
def test_range_different_values(self):
graph, graph_ref = build_test_graphs(start=0, limit=50000, delta=1, dst_type_str='FP16')
self.assertRaises(Exception, ChangeRangeOutputType().find_and_replace_pattern, graph)

def test_range_out_of_fp16_max(self):
graph, graph_ref = build_test_graphs(start=0, limit=100000, delta=1, dst_type_str='FP16')
self.assertRaises(Exception, ChangeRangeOutputType().find_and_replace_pattern, graph)

def test_range_out_of_fp16_min(self):
graph, graph_ref = build_test_graphs(start=0, limit=-100000, delta=-1, dst_type_str='FP16')
self.assertRaises(Exception, ChangeRangeOutputType().find_and_replace_pattern, graph)


def build_test_graphs(start=0, limit=10, delta=1, dst_type_str='FP16'):
nodes = {
**valued_const_with_data('start', float32_array(start)),
**valued_const_with_data('limit', float32_array(limit)),
**valued_const_with_data('delta', float32_array(delta)),
**regular_op_with_empty_data('range', {'type': 'Range', 'op': 'Range',
'output_type': np.float32,
'infer': Range.infer}),
**result('res'),
}

nodes_ref = deepcopy(nodes)
nodes_ref.update({
**regular_op_with_empty_data('range', {'type': 'Range', 'op': 'Range',
'output_type': data_type_str_to_np(dst_type_str),
'infer': Range.infer}),
})

edges = [
*connect('start', '0:range'),
*connect('limit', '1:range'),
*connect('delta', '2:range'),
*connect('range', 'res'),
]
graph = build_graph(nodes, edges)
graph_ref = build_graph(nodes_ref, edges)

graph = partial_infer(graph)

graph.graph['cmd_params'].data_type = dst_type_str
convert_blobs(graph, dst_type_str)
return graph, graph_ref