From 6000f7e97a6a0f20fbda5ecb6b8bce91ef8980d6 Mon Sep 17 00:00:00 2001 From: shenghong96 Date: Thu, 29 Apr 2021 15:30:45 +0800 Subject: [PATCH] optimize add, sub, mul, div mapper --- .../mapper/impl/ops/add_mapper.py | 15 +++++++++++---- .../mapper/impl/ops/div_mapper.py | 15 +++++++++++---- .../mapper/impl/ops/mul_mapper.py | 15 +++++++++++---- .../mapper/impl/ops/sub_mapper.py | 15 +++++++++++---- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/add_mapper.py b/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/add_mapper.py index 409bc52c..4d47ba7f 100644 --- a/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/add_mapper.py +++ b/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/add_mapper.py @@ -51,6 +51,15 @@ class AddMapper(ONNXToMindSporeMapper): if not op: raise ValueError("Can not get MindSpore operation name.") if not weights: + variable_slot = "var_0" + construct_template = \ + f"opt_{{{variable_slot}}} = {op}()({{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}})" + template = { + variable_slot: { + TemplateKeywords.INIT.value: [], + TemplateKeywords.CONSTRUCT.value: [construct_template] + } + } return template, exchange_msg, outputs_list, outputs_mapping tensor = AddMapper._find_val_by_index(0, weights) @@ -58,7 +67,6 @@ class AddMapper(ONNXToMindSporeMapper): bias_location = AddMapper._find_location_by_index(0, weights) variable_slot = "var_0" - init_template = f"self.{{{variable_slot}}} = {op}({', '.join(['%s={%s}' % (p, p) for p in args])})" inputs_in_construct = [f"{{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}"] if bias_location != -1: inputs_in_construct.insert(bias_location, f"self.{{{variable_slot}}}_bias") @@ -72,11 +80,10 @@ class AddMapper(ONNXToMindSporeMapper): args["bias_value"] = tensor.tolist() init_tensor = f"self.{{{variable_slot}}}_bias = {{bias_value}}" - construct_template = f"opt_{{{variable_slot}}} = self.{{{variable_slot}}}" \ - f"({', '.join(inputs_in_construct)})" + construct_template = f"opt_{{{variable_slot}}} = {' + '.join(inputs_in_construct)}" template = { variable_slot: { - TemplateKeywords.INIT.value: [init_template, init_tensor], + TemplateKeywords.INIT.value: [init_tensor], TemplateKeywords.CONSTRUCT.value: [construct_template] } } diff --git a/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/div_mapper.py b/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/div_mapper.py index 7ec9423f..94748425 100644 --- a/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/div_mapper.py +++ b/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/div_mapper.py @@ -49,6 +49,15 @@ class DivMapper(ONNXToMindSporeMapper): weights = kwargs.get("weights") trainable_params = kwargs.get("trainable_params", dict()) if not weights: + variable_slot = "var_0" + construct_template = \ + f"opt_{{{variable_slot}}} = {op}()({{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}) " + template = { + variable_slot: { + TemplateKeywords.INIT.value: [], + TemplateKeywords.CONSTRUCT.value: [construct_template] + } + } return template, exchange_msg, outputs_list, outputs_mapping tensor = DivMapper._find_val_by_index(0, weights) @@ -57,7 +66,6 @@ class DivMapper(ONNXToMindSporeMapper): w_location = DivMapper._find_location_by_index(0, weights) variable_slot = "var_0" - init_template = f"self.{{{variable_slot}}} = {op}()" inputs_in_construct = [f"{{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}"] if w_location != -1: inputs_in_construct.insert(w_location, f"self.{{{variable_slot}}}_w") @@ -72,9 +80,8 @@ class DivMapper(ONNXToMindSporeMapper): args["w_value"] = tensor.tolist() init_tensor = f"self.{{{variable_slot}}}_w = {{w_value}}" - construct_template = f"opt_{{{variable_slot}}} = self.{{{variable_slot}}}" \ - f"({', '.join(inputs_in_construct)})" - template = reset_init_or_construct(template, variable_slot, [init_template, init_tensor], + construct_template = f"opt_{{{variable_slot}}} = {' / '.join(inputs_in_construct)}" + template = reset_init_or_construct(template, variable_slot, [init_tensor], TemplateKeywords.INIT.value) template = reset_init_or_construct(template, variable_slot, [construct_template], TemplateKeywords.CONSTRUCT.value) diff --git a/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/mul_mapper.py b/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/mul_mapper.py index 34af9bff..692fc382 100644 --- a/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/mul_mapper.py +++ b/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/mul_mapper.py @@ -50,6 +50,15 @@ class MulMapper(ONNXToMindSporeMapper): weights = kwargs.get("weights") trainable_params = kwargs.get('trainable_params', dict()) if not weights: + variable_slot = "var_0" + construct_template = \ + f"opt_{{{variable_slot}}} = {op}()({{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}})" + template = { + variable_slot: { + TemplateKeywords.INIT.value: [], + TemplateKeywords.CONSTRUCT.value: [construct_template] + } + } return template, exchange_msg, outputs_list, outputs_mapping tensor = MulMapper._find_val_by_index(0, weights) @@ -57,7 +66,6 @@ class MulMapper(ONNXToMindSporeMapper): w_location = MulMapper._find_location_by_index(0, weights) variable_slot = "var_0" - init_template = f"self.{{{variable_slot}}} = {op}()" inputs_in_construct = [f"{{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}"] if w_location != -1: inputs_in_construct.insert(w_location, f"self.{{{variable_slot}}}_w") @@ -70,9 +78,8 @@ class MulMapper(ONNXToMindSporeMapper): args["w_value"] = tensor.tolist() init_tensor = f"self.{{{variable_slot}}}_w = {{w_value}}" - construct_template = f"opt_{{{variable_slot}}} = self.{{{variable_slot}}}" \ - f"({', '.join(inputs_in_construct)})" - template = reset_init_or_construct(template, variable_slot, [init_template, init_tensor], + construct_template = f"opt_{{{variable_slot}}} = {' * '.join(inputs_in_construct)}" + template = reset_init_or_construct(template, variable_slot, [init_tensor], TemplateKeywords.INIT.value) template = reset_init_or_construct(template, variable_slot, [construct_template], TemplateKeywords.CONSTRUCT.value) diff --git a/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/sub_mapper.py b/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/sub_mapper.py index 37c1825f..2f21530e 100644 --- a/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/sub_mapper.py +++ b/mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/sub_mapper.py @@ -50,6 +50,15 @@ class SubMapper(ONNXToMindSporeMapper): if not op: raise ValueError("Can not get MindSpore operation name.") if not weights: + variable_slot = "var_0" + construct_template = \ + f"opt_{{{variable_slot}}} = {op}()({{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}})" + template = { + variable_slot: { + TemplateKeywords.INIT.value: [], + TemplateKeywords.CONSTRUCT.value: [construct_template] + } + } return template, exchange_msg, outputs_list, outputs_mapping tensor = SubMapper._find_val_by_index(0, weights) @@ -58,7 +67,6 @@ class SubMapper(ONNXToMindSporeMapper): bias_location = SubMapper._find_location_by_index(0, weights) variable_slot = "var_0" - init_template = f"self.{{{variable_slot}}} = {op}()" inputs_in_construct = [f"{{{ExchangeMessageKeywords.VariableScope.value.INPUTS.value}}}"] if bias_location != -1: inputs_in_construct.insert(bias_location, f"self.{{{variable_slot}}}_bias") @@ -73,11 +81,10 @@ class SubMapper(ONNXToMindSporeMapper): args["bias_value"] = tensor.tolist() init_tensor = f"self.{{{variable_slot}}}_bias = {{bias_value}}" - construct_template = f"opt_{{{variable_slot}}} = self.{{{variable_slot}}}" \ - f"({', '.join(inputs_in_construct)})" + construct_template = f"opt_{{{variable_slot}}} = {' - '.join(inputs_in_construct)}" template = { variable_slot: { - TemplateKeywords.INIT.value: [init_template, init_tensor], + TemplateKeywords.INIT.value: [init_tensor], TemplateKeywords.CONSTRUCT.value: [construct_template] } }