From b15a70caf7f0bfeaa2027f3783ff975e6927ad07 Mon Sep 17 00:00:00 2001 From: Vladimir Gavrilov Date: Sat, 27 Feb 2021 08:49:47 +0300 Subject: [PATCH] Corrections in MO to fix performance drop in Interpolate-4 when mode=linear instead of linear_onnx (#4488) * Written the transformation that replaces 'linear' mode of Interpolate-4 by 'linear_onnx' mode, when the input tensor rank is equal to 4. * Fixed BOM-file. * Fixed copyright year. * Added message into assert. * Small fix. --- model-optimizer/automation/package_BOM.txt | 1 + .../back/LinearToLinearONNXReplacer.py | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 model-optimizer/extensions/back/LinearToLinearONNXReplacer.py diff --git a/model-optimizer/automation/package_BOM.txt b/model-optimizer/automation/package_BOM.txt index 971c53397da..4ab9ebec530 100644 --- a/model-optimizer/automation/package_BOM.txt +++ b/model-optimizer/automation/package_BOM.txt @@ -30,6 +30,7 @@ extensions/back/InterpolateReshape.py extensions/back/kaldi_remove_memory_output.py extensions/back/LayoutChangeForGatherND.py extensions/back/LeakyReLUMutation.py +extensions/back/LinearToLinearONNXReplacer.py extensions/back/LRNToNorm.py extensions/back/MatMulNormalizer.py extensions/back/MaxPool.py diff --git a/model-optimizer/extensions/back/LinearToLinearONNXReplacer.py b/model-optimizer/extensions/back/LinearToLinearONNXReplacer.py new file mode 100644 index 00000000000..7d3ff7d5b3f --- /dev/null +++ b/model-optimizer/extensions/back/LinearToLinearONNXReplacer.py @@ -0,0 +1,41 @@ +""" + Copyright (C) 2018-2021 Intel Corporation + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +""" + +from extensions.back.InterpolateReshape import InterpolateConcat, InterpolateReshapeWA +from mo.back.replacement import BackReplacementPattern +from mo.graph.graph import Graph + + +class LinearToLinearONNXReplacer(BackReplacementPattern): + """ + If we don't use this transformation, then we have a performance drop, because CPU and GPU have no optimized + version of the 'linear' mode of the operation Interpolate. + TODO: delete this transformation, when CPU and GPU will have optimized version of the 'linear' mode. + """ + enabled = True + + def run_after(self): + return [InterpolateConcat, InterpolateReshapeWA] + + def find_and_replace_pattern(self, graph: Graph): + for interpolate_node in graph.get_op_nodes(type='Interpolate', version='opset4', mode='linear'): + input_shape = interpolate_node.in_port(0).data.get_shape() + interpolate_name = interpolate_node.soft_get('name', interpolate_node.id) + assert input_shape is not None, \ + 'Shape of interpolated data for node {} must not be None'.format(interpolate_name) + input_rank = len(input_shape) + if input_rank == 4: + interpolate_node['mode'] = 'linear_onnx'