Browse Source

optimize add, sub, mul, div mapper

pull/1323/head
shenghong96 5 years ago
parent
commit
6000f7e97a
4 changed files with 44 additions and 16 deletions
  1. +11
    -4
      mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/add_mapper.py
  2. +11
    -4
      mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/div_mapper.py
  3. +11
    -4
      mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/mul_mapper.py
  4. +11
    -4
      mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/sub_mapper.py

+ 11
- 4
mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/add_mapper.py View File

@@ -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]
}
}


+ 11
- 4
mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/div_mapper.py View File

@@ -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)


+ 11
- 4
mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/mul_mapper.py View File

@@ -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)


+ 11
- 4
mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/sub_mapper.py View File

@@ -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]
}
}


Loading…
Cancel
Save