提交 bac4164d 编写于 作者: M mamingjie-China

modify

上级 2304e07f
......@@ -168,7 +168,11 @@ class TFOpMapper(OpMapper):
x_input = y
y_input = x
x_shape = y.out_shapes[0]
if len(x_shape) == 0:
x_shape = [1]
y_shape = x.out_shapes[0]
if len(y_shape) == 0:
y_shape = [1]
else:
if len(x_shape) == 1 and len(y_shape) == 4 and x_shape[
0] == y_shape[-1] and y_shape.count(-1) < 1:
......
......@@ -121,6 +121,25 @@ class TFOpMapperNHWC(OpMapper):
pd_param_name = list(param.values())[0]
tf_param = node.get_attr(tf_param_name)
attr[pd_param_name] = tf_param
if len(input.out_shapes[0]) == 4 and op_info[0] != 'shape':
attr1 = {"perm": [0, 3, 1, 2]}
node.fluid_code.add_layer('transpose',
inputs=input,
output=node,
param_attr=attr1)
input = node
node.fluid_code.add_layer(op_info[0],
inputs=input,
output=node,
param_attr=attr)
input = node
attr2 = {"perm": [0, 2, 3, 1]}
node.fluid_code.add_layer('transpose',
inputs=input,
output=node,
param_attr=attr2)
else:
node.fluid_code.add_layer(op_info[0],
inputs=input,
output=node,
......@@ -149,7 +168,11 @@ class TFOpMapperNHWC(OpMapper):
x_input = y
y_input = x
x_shape = y.out_shapes[0]
if len(x_shape) == 0:
x_shape = [1]
y_shape = x.out_shapes[0]
if len(y_shape) == 0:
y_shape = [1]
else:
raise Exception("Unexpected situation happend")
......@@ -193,6 +216,25 @@ class TFOpMapperNHWC(OpMapper):
output="y_tmp",
param_attr=attr)
y_input = "y_tmp"
if len(x_shape) == 4 and len(y_shape) == 4:
node.fluid_code.add_layer("transpose",
inputs=x_input,
output=x_input,
param_attr={'perm': [0, 3, 1, 2]})
node.fluid_code.add_layer("transpose",
inputs=y_input,
output=y_input,
param_attr={'perm': [0, 3, 1, 2]})
inputs = {"x": x_input, "y": y_input}
node.fluid_code.add_layer(op_type,
inputs=inputs,
output=node,
param_attr=None)
node.fluid_code.add_layer("transpose",
inputs=node,
output=node,
param_attr={'perm': [0, 2, 3, 1]})
else:
inputs = {"x": x_input, "y": y_input}
node.fluid_code.add_layer(op_type,
inputs=inputs,
......@@ -978,9 +1020,7 @@ class TFOpMapperNHWC(OpMapper):
if pad_mode == "SAME":
if node.tf_data_format == "NHWC":
print(out_shape)
out_shape = [out_shape[i] for i in [0, 3, 1, 2]]
print(out_shape)
for i in range(4):
if out_shape[i] < 0:
out_shape[i] = 999999
......
......@@ -232,84 +232,35 @@ class TFOptimizer(object):
'act']
node.fluid_code.clear()
self.graph.remove_node(node.layer_name)
self.graph.identity_map[node.layer_name] = input.layer_name
def remove_transpose(self):
graph_copy = cp.deepcopy(self.graph)
nhwc_insensitive_ops = [
'Relu', 'Relu6', 'Abs', 'Sigmoid', 'Exp', 'Rsqrt', 'swish_f32',
'LeakyRelu', 'Cast'
'LeakyRelu', 'Cast', 'Tanh'
]
elementwise_ops = [
'Sub', 'Add', 'RealDiv', 'Maximum', 'Mul', 'FloorDiv',
'GreaterEqual'
]
for node_name in self.graph.topo_sort:
node = graph_copy.get_node(node_name)
if node is None:
continue
if node.layer_type in nhwc_insensitive_ops:
graph_copy.remove_node(node_name)
optimize_ops = [
'Conv2D', 'MaxPool', 'FusedBatchNorm', 'DepthwiseConv2dNative',
'AvgPool', 'Pad', 'Conv2DBackpropInput', 'ResizeNearestNeighbor',
'ResizeBilinear', "Placeholder"
]
can_be_optimized_ops = [
'Conv2D', 'MaxPool', 'FusedBatchNorm', 'DepthwiseConv2dNative',
'AvgPool', 'Pad', 'Conv2DBackpropInput', 'ResizeNearestNeighbor',
'ResizeBilinear', "Placeholder", 'Relu', 'Relu6', 'Abs', 'Sigmoid',
'Exp', 'Rsqrt', 'swish_f32', 'LeakyRelu', 'Cast', 'Tanh'
]
for node_name in self.graph.topo_sort:
node = graph_copy.get_node(node_name)
if node is None:
continue
if node.layer_type in elementwise_ops:
is_nhwc = True
for in_name in node.inputs:
in_node = graph_copy.get_node(in_name)
if hasattr(in_node, "is_nhwc"):
if not in_node.is_nhwc:
is_nhwc = False
else:
if len(in_node.fluid_code.layers) < 2:
is_nhwc = False
continue
if in_node.fluid_code.layers[
-1].op != "transpose" or in_node.fluid_code.layers[
-1].param_attr["perm"] != [0, 2, 3, 1]:
is_nhwc = False
continue
node.is_nhwc = is_nhwc
for i in range(len(self.graph.topo_sort)):
node_name = self.graph.topo_sort[-1 * i - 1]
node = graph_copy.get_node(node_name)
if node is None:
continue
if node.layer_type in elementwise_ops:
can_be_removed = True
if len(node.fluid_code.layers) > 1:
can_be_removed = False
if not node.is_nhwc:
can_be_removed = False
for out_name in node.outputs:
out_node = graph_copy.get_node(out_name)
if hasattr(out_node, "is_nhwc"):
if not out_node.is_nhwc:
can_be_removed = False
else:
if len(out_node.fluid_code.layers) < 2:
can_be_removed = False
break
if out_node.fluid_code.layers[
0].op != "transpose" or out_node.fluid_code.layers[
0].param_attr["perm"] != [0, 3, 1, 2]:
can_be_removed = False
break
node.can_be_removed = can_be_removed
for node_name in self.graph.topo_sort:
node = graph_copy.get_node(node_name)
if node is None:
continue
if node.layer_type in optimize_ops:
if node.layer_type in can_be_optimized_ops:
if node.fluid_code.layers[
-1].op != "transpose" or node.fluid_code.layers[
-1].param_attr["perm"] != [0, 2, 3, 1]:
......@@ -327,6 +278,9 @@ class TFOptimizer(object):
0].param_attr["perm"] != [0, 3, 1, 2]:
can_be_removed = False
break
elif out_node.layer_type in elementwise_ops:
can_be_removed = False
break
if can_be_removed and len(node.fluid_code.layers) > 1:
true_node = self.graph.get_node(node_name)
if true_node.layer_type == "Placeholder":
......@@ -346,8 +300,6 @@ class TFOptimizer(object):
del true_node.fluid_code.layers[-1]
for out_name in output_names:
out_node = self.graph.get_node(out_name)
if out_node.layer_type in elementwise_ops:
continue
out_node.fluid_code.layers[
1].inputs = out_node.fluid_code.layers[0].inputs
del out_node.fluid_code.layers[0]
......@@ -357,43 +309,178 @@ class TFOptimizer(object):
if node is None:
continue
if node.layer_type in elementwise_ops:
if not node.can_be_removed:
can_be_removed = True
if node.fluid_code.layers[
-1].op != "transpose" or node.fluid_code.layers[
-1].param_attr["perm"] != [0, 2, 3, 1]:
continue
can_be_removed = True
output_names = node.outputs
for out_name in output_names:
out_node = graph_copy.get_node(out_name)
if len(out_node.fluid_code.layers) < 3:
can_be_removed = False
break
if hasattr(out_node, "can_be_removed"):
if not out_node.can_be_removed:
can_be_removed = False
break
if out_node.layer_type in can_be_optimized_ops:
if out_node.fluid_code.layers[
0].op != "transpose" or out_node.fluid_code.layers[
0].param_attr["perm"] != [0, 3, 1, 2]:
can_be_removed = False
break
elif out_node.layer_type in elementwise_ops:
if out_node.fluid_code.layers[
0].op != "transpose" and out_node.fluid_code.layers[
1].op != "transpose":
can_be_removed = False
break
if out_node.fluid_code.layers[0].op == "transpose":
if out_node.fluid_code.layers[0].param_attr[
"perm"] != [0, 3, 1, 2]:
can_be_removed = False
break
if out_node.fluid_code.layers[1].op == "transpose":
if out_node.fluid_code.layers[1].param_attr[
"perm"] != [0, 3, 1, 2]:
can_be_removed = False
break
if can_be_removed and len(node.fluid_code.layers) > 1:
true_node = self.graph.get_node(node_name)
for i, in_name in enumerate(node.inputs):
in_node = graph_copy.get_node(in_name)
if hasattr(in_node, "is_nhwc") and in_node.is_nhwc:
if i == 0:
l = Layer()
l.op = "transpose"
l.inputs = true_node.fluid_code.layers[
0].inputs["x"]
l.param_attr = {"perm": [0, 2, 3, 1]}
l.output = "nhwc_" + l.inputs.layer_name
true_node.fluid_code.layers[0].inputs[
"x"] = l.output
true_node.fluid_code.layers.insert(0, l)
elif i == 1:
l = Layer()
l.op = "transpose"
l.inputs = true_node.fluid_code.layers[
0].inputs["y"]
l.param_attr = {"perm": [0, 2, 3, 1]}
l.output = "nhwc_" + l.inputs.layer_name
true_node.fluid_code.layers[0].inputs[
"y"] = l.output
true_node.fluid_code.layers.insert(0, l)
true_node.fluid_code.layers[
-2].output = true_node.fluid_code.layers[-1].output
del true_node.fluid_code.layers[-1]
for out_name in output_names:
out_node = self.graph.get_node(out_name)
if out_node.layer_type in can_be_optimized_ops:
out_node.fluid_code.layers[
1].inputs = out_node.fluid_code.layers[0].inputs
del out_node.fluid_code.layers[0]
elif out_node.layer_type in elementwise_ops:
if out_node.inputs[0] in node.layer_name:
if out_node.fluid_code.layers[
1].op == 'transpose':
out_node.fluid_code.layers[2].inputs[
'x'] = out_node.fluid_code.layers[
0].inputs
del out_node.fluid_code.layers[0]
else:
raise Exception("Unexpected situation happend")
continue
out_node.fluid_code.layers[1].inputs[
'x'] = out_node.fluid_code.layers[
0].inputs
del out_node.fluid_code.layers[0]
elif out_node.inputs[1] in node.layer_name:
if out_node.fluid_code.layers[
1].op == 'transpose':
out_node.fluid_code.layers[2].inputs[
'y'] = out_node.fluid_code.layers[
1].inputs
del out_node.fluid_code.layers[1]
else:
for out_name in node.outputs:
out_node.fluid_code.layers[1].inputs[
'y'] = out_node.fluid_code.layers[
0].inputs
del out_node.fluid_code.layers[0]
graph_copy = cp.deepcopy(self.graph)
for node_name in self.graph.topo_sort:
node = graph_copy.get_node(node_name)
if node is None or len(node.fluid_code.layers) < 2:
continue
if node.layer_type in can_be_optimized_ops and node.layer_type != "Placeholder":
if node.fluid_code.layers[
-1].op != "transpose" or node.fluid_code.layers[
-1].param_attr["perm"] != [0, 2, 3, 1]:
continue
can_be_removed = True
output_names = node.outputs
for out_name in output_names:
out_node = graph_copy.get_node(out_name)
if hasattr(out_node, "can_be_removed"):
if not out_node.can_be_removed:
can_be_removed = False
break
if len(out_node.fluid_code.layers) < 2:
can_be_removed = False
break
if out_node.layer_type in can_be_optimized_ops:
if out_node.fluid_code.layers[
0].op != "transpose" or out_node.fluid_code.layers[
0].param_attr["perm"] != [0, 3, 1, 2]:
can_be_removed = False
break
elif out_node.layer_type in elementwise_ops:
if out_node.fluid_code.layers[
0].op != "transpose" and out_node.fluid_code.layers[
1].op != "transpose":
can_be_removed = False
break
if out_node.fluid_code.layers[
0].op == "expand" or out_node.fluid_code.layers[
1].op == "expand":
can_be_removed = False
break
if out_node.fluid_code.layers[0].op == "transpose":
if out_node.fluid_code.layers[0].param_attr[
"perm"] != [0, 3, 1, 2]:
can_be_removed = False
break
if out_node.fluid_code.layers[1].op == "transpose":
if out_node.fluid_code.layers[1].param_attr[
"perm"] != [0, 3, 1, 2]:
can_be_removed = False
break
elif out_node.layer_type not in elementwise_ops and out_node.layer_type not in can_be_optimized_ops:
can_be_removed = False
break
if can_be_removed:
true_node = self.graph.get_node(node_name)
if len(true_node.fluid_code.layers) < 2:
continue
true_node.fluid_code.layers[
-2].output = true_node.fluid_code.layers[-1].output
del true_node.fluid_code.layers[-1]
for out_name in output_names:
out_node = self.graph.get_node(out_name)
if out_node.layer_type not in elementwise_ops:
assert out_node.fluid_code.layers[
0].op == "transpose", "unexpected situation happend"
if out_node.layer_type in can_be_optimized_ops:
out_node.fluid_code.layers[
1].inputs = out_node.fluid_code.layers[0].inputs
del out_node.fluid_code.layers[0]
elif out_node.layer_type in elementwise_ops:
if out_node.inputs[0] in node.layer_name:
if out_node.fluid_code.layers[
1].op == 'transpose':
if out_node.fluid_code.layers[
2].op == 'transpose':
out_node.fluid_code.layers[3].inputs[
'x'] = out_node.fluid_code.layers[
0].inputs
else:
out_node.fluid_code.layers[2].inputs[
'x'] = out_node.fluid_code.layers[
0].inputs
del out_node.fluid_code.layers[0]
else:
out_node.fluid_code.layers[1].inputs[
'x'] = out_node.fluid_code.layers[
0].inputs
del out_node.fluid_code.layers[0]
elif out_node.inputs[1] in node.layer_name:
if out_node.fluid_code.layers[
1].op == 'transpose':
out_node.fluid_code.layers[2].inputs[
'y'] = out_node.fluid_code.layers[
1].inputs
del out_node.fluid_code.layers[1]
else:
out_node.fluid_code.layers[1].inputs[
'y'] = out_node.fluid_code.layers[
0].inputs
del out_node.fluid_code.layers[0]
def make_nchw_input_output(self):
for i, name in enumerate(self.graph.input_nodes):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册