未验证 提交 ca33e9fa 编写于 作者: J Jason 提交者: GitHub

Merge pull request #278 from Channingss/develop

add OP:Greater, ReduceMax
...@@ -54,6 +54,11 @@ default_op_mapping = { ...@@ -54,6 +54,11 @@ default_op_mapping = {
dict(axes='dim', keepdims='keep_dim'), dict(axes='dim', keepdims='keep_dim'),
dict(keep_dim=1) dict(keep_dim=1)
], ],
'ReduceMax': [
'reduce_max', ['X'], ['Out'],
dict(axes='dim', keepdims='keep_dim'),
dict(keep_dim=1)
],
#active function #active function
'Relu': ['relu', ['X'], ['Out']], 'Relu': ['relu', ['X'], ['Out']],
'LeakyRelu': ['leaky_relu', ['X'], ['Out'], 'LeakyRelu': ['leaky_relu', ['X'], ['Out'],
...@@ -83,6 +88,6 @@ default_op_mapping = { ...@@ -83,6 +88,6 @@ default_op_mapping = {
} }
default_ioa_constraint = { default_ioa_constraint = {
'Gather': 'Gather': [(lambda i, o, a: a.get('axis', 0) == 0,
[(lambda i, o, a: a.get('axis', 0) == 0, 'only axis = 0 is supported')], 'only axis = 0 is supported')],
} }
...@@ -246,10 +246,8 @@ class ONNXOpMapper(OpMapper): ...@@ -246,10 +246,8 @@ class ONNXOpMapper(OpMapper):
assert len(val_inps) == 1, 'directly_map error with multi inputs' assert len(val_inps) == 1, 'directly_map error with multi inputs'
if fluid_op not in ['shape']: if fluid_op not in ['shape']:
attr['name'] = string(node.layer_name) attr['name'] = string(node.layer_name)
node.fluid_code.add_layer(fluid_op, node.fluid_code.add_layer(
inputs=val_inps[0], fluid_op, inputs=val_inps[0], output=val_outs[0], param_attr=attr)
output=val_outs[0],
param_attr=attr)
def deal_custom_layer(self, node): def deal_custom_layer(self, node):
op = node.layer_type op = node.layer_type
...@@ -258,7 +256,8 @@ class ONNXOpMapper(OpMapper): ...@@ -258,7 +256,8 @@ class ONNXOpMapper(OpMapper):
params = get_params(node.layer, node.layer_type) params = get_params(node.layer, node.layer_type)
arg_names, kwargs = set_args(func, params) arg_names, kwargs = set_args(func, params)
kwargs['name'] = string(node.layer_name) kwargs['name'] = string(node.layer_name)
node.fluid_code.add_layer(func.__code__.co_name, node.fluid_code.add_layer(
func.__code__.co_name,
inputs=node.inputs, inputs=node.inputs,
output=node, output=node,
param_attr=kwargs, param_attr=kwargs,
...@@ -299,21 +298,18 @@ class ONNXOpMapper(OpMapper): ...@@ -299,21 +298,18 @@ class ONNXOpMapper(OpMapper):
'shape': val_y_reshaped, 'shape': val_y_reshaped,
'name': string(var_y_reshaped) 'name': string(var_y_reshaped)
} }
node.fluid_code.add_layer('reshape', node.fluid_code.add_layer(
'reshape',
inputs=val_y, inputs=val_y,
output=var_y_reshaped, output=var_y_reshaped,
param_attr=attr_reshaped) param_attr=attr_reshaped)
inputs = {'x': val_x, 'y': var_y_reshaped} inputs = {'x': val_x, 'y': var_y_reshaped}
node.fluid_code.add_layer(op_type, node.fluid_code.add_layer(
inputs=inputs, op_type, inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
else: else:
inputs = {'x': val_x, 'y': val_y} inputs = {'x': val_x, 'y': val_y}
node.fluid_code.add_layer(op_type, node.fluid_code.add_layer(
inputs=inputs, op_type, inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def place_holder(self, node): def place_holder(self, node):
self.input_shapes.append(node.out_shapes[0]) self.input_shapes.append(node.out_shapes[0])
...@@ -331,10 +327,8 @@ class ONNXOpMapper(OpMapper): ...@@ -331,10 +327,8 @@ class ONNXOpMapper(OpMapper):
"append_batch_size": 'False' "append_batch_size": 'False'
} }
node.fluid_code.add_layer("data", node.fluid_code.add_layer(
inputs=None, "data", inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
def create_parameter(self, node, parameter=None): def create_parameter(self, node, parameter=None):
if parameter is not None: if parameter is not None:
...@@ -351,10 +345,8 @@ class ONNXOpMapper(OpMapper): ...@@ -351,10 +345,8 @@ class ONNXOpMapper(OpMapper):
'attr': string(node.layer_name), 'attr': string(node.layer_name),
'default_initializer': 'Constant(0.0)' 'default_initializer': 'Constant(0.0)'
} }
node.fluid_code.add_layer("create_parameter", node.fluid_code.add_layer(
inputs=None, "create_parameter", inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
def _pad_if_asymmetric(self, node, pads, val_name): # pads: SSEE def _pad_if_asymmetric(self, node, pads, val_name): # pads: SSEE
assert len(pads) & 1 == 0 assert len(pads) & 1 == 0
...@@ -418,10 +410,8 @@ class ONNXOpMapper(OpMapper): ...@@ -418,10 +410,8 @@ class ONNXOpMapper(OpMapper):
else: else:
attr['out_shape'] = out_shape attr['out_shape'] = out_shape
node.fluid_code.add_layer(fluid_op, node.fluid_code.add_layer(
inputs=val_x, fluid_op, inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def RoiAlign(self, node): def RoiAlign(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -437,7 +427,8 @@ class ONNXOpMapper(OpMapper): ...@@ -437,7 +427,8 @@ class ONNXOpMapper(OpMapper):
'spatial_scale': spatial_scale, 'spatial_scale': spatial_scale,
'sampling_ratio': sampling_ratio, 'sampling_ratio': sampling_ratio,
} }
node.fluid_code.add_layer('roi_align', node.fluid_code.add_layer(
'roi_align',
inputs={ inputs={
'input': val_x, 'input': val_x,
'rois': val_rois 'rois': val_rois
...@@ -456,7 +447,8 @@ class ONNXOpMapper(OpMapper): ...@@ -456,7 +447,8 @@ class ONNXOpMapper(OpMapper):
'pooled_width': pooled_width, 'pooled_width': pooled_width,
'spatial_scale': spatial_scale, 'spatial_scale': spatial_scale,
} }
node.fluid_code.add_layer('roi_pool', node.fluid_code.add_layer(
'roi_pool',
inputs={ inputs={
'input': val_x, 'input': val_x,
'rois': val_rois 'rois': val_rois
...@@ -499,13 +491,12 @@ class ONNXOpMapper(OpMapper): ...@@ -499,13 +491,12 @@ class ONNXOpMapper(OpMapper):
attr['paddings'] = paddings attr['paddings'] = paddings
if op_independent: if op_independent:
attr['name'] = string(node.layer_name) attr['name'] = string(node.layer_name)
node.fluid_code.add_layer(fluid_op, node.fluid_code.add_layer(
inputs=val_x, fluid_op, inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
else: else:
attr['name'] = string(node.layer_name + '_paded') attr['name'] = string(node.layer_name + '_paded')
node.fluid_code.add_layer(fluid_op, node.fluid_code.add_layer(
fluid_op,
inputs=val_x, inputs=val_x,
output=node.layer_name + '_paded', output=node.layer_name + '_paded',
param_attr=attr) param_attr=attr)
...@@ -515,16 +506,12 @@ class ONNXOpMapper(OpMapper): ...@@ -515,16 +506,12 @@ class ONNXOpMapper(OpMapper):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
axes = node.get_attr('axes') axes = node.get_attr('axes')
if len(val_x.out_shapes[0]) == 0: if len(val_x.out_shapes[0]) == 0:
node.fluid_code.add_layer('assign', node.fluid_code.add_layer(
inputs=val_x, 'assign', inputs=val_x, output=node, param_attr=None)
output=node,
param_attr=None)
else: else:
attr = {'axes': axes, 'name': string(node.layer_name)} attr = {'axes': axes, 'name': string(node.layer_name)}
node.fluid_code.add_layer('unsqueeze', node.fluid_code.add_layer(
inputs=val_x, 'unsqueeze', inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Shrink(self, node): def Shrink(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -532,10 +519,20 @@ class ONNXOpMapper(OpMapper): ...@@ -532,10 +519,20 @@ class ONNXOpMapper(OpMapper):
lambd = node.get_attr('lambd') lambd = node.get_attr('lambd')
assert bias == 0.0, 'not support bias!=0' assert bias == 0.0, 'not support bias!=0'
attr = {'threshold': lambd, 'name': node.layer_name} attr = {'threshold': lambd, 'name': node.layer_name}
node.fluid_code.add_layer('hard_shrink', node.fluid_code.add_layer(
inputs=val_x, 'hard_shrink', inputs=val_x, output=node, param_attr=attr)
def Greater(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True)
val_y = self.graph.get_input_node(node, idx=1, copy=True)
node.fluid_code.add_layer(
'greater_than',
inputs={
'x': val_x,
'y': val_y
},
output=node, output=node,
param_attr=attr) param_attr=None)
def Constant(self, node): def Constant(self, node):
val_output = self.graph.get_node(node.layer.output[0], copy=True) val_output = self.graph.get_node(node.layer.output[0], copy=True)
...@@ -565,10 +562,8 @@ class ONNXOpMapper(OpMapper): ...@@ -565,10 +562,8 @@ class ONNXOpMapper(OpMapper):
if dtype.name == 'int64': if dtype.name == 'int64':
dtype = 'int32' dtype = 'int32'
attr = {'shape': shape, 'dtype': string(dtype), 'value': value} attr = {'shape': shape, 'dtype': string(dtype), 'value': value}
node.fluid_code.add_layer('fill_constant', node.fluid_code.add_layer(
inputs=None, 'fill_constant', inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
else: else:
value = np.reshape(value, shape) value = np.reshape(value, shape)
self.weights[node.layer_name] = value self.weights[node.layer_name] = value
...@@ -579,10 +574,8 @@ class ONNXOpMapper(OpMapper): ...@@ -579,10 +574,8 @@ class ONNXOpMapper(OpMapper):
'attr': string(node.layer_name), 'attr': string(node.layer_name),
'default_initializer': 'Constant(0.0)' 'default_initializer': 'Constant(0.0)'
} }
node.fluid_code.add_layer("create_parameter", node.fluid_code.add_layer(
inputs=None, "create_parameter", inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Resize(self, node): def Resize(self, node):
self._interpolate(node) self._interpolate(node)
...@@ -603,13 +596,12 @@ class ONNXOpMapper(OpMapper): ...@@ -603,13 +596,12 @@ class ONNXOpMapper(OpMapper):
name_ones = node.layer_name + '_ones' name_ones = node.layer_name + '_ones'
attr_ones = {'shape': out_shape, 'dtype': string(val_x_dtype)} attr_ones = {'shape': out_shape, 'dtype': string(val_x_dtype)}
node.fluid_code.add_layer('ones', node.fluid_code.add_layer(
inputs=None, 'ones', inputs=None, output=name_ones, param_attr=attr_ones)
output=name_ones,
param_attr=attr_ones)
inputs = {'x': name_ones, 'y': val_x} inputs = {'x': name_ones, 'y': val_x}
attr = {'name': string(node.layer_name)} attr = {'name': string(node.layer_name)}
node.fluid_code.add_layer('elementwise_mul', node.fluid_code.add_layer(
'elementwise_mul',
inputs=inputs, inputs=inputs,
output=node.layer_name, output=node.layer_name,
param_attr=attr) param_attr=attr)
...@@ -622,7 +614,8 @@ class ONNXOpMapper(OpMapper): ...@@ -622,7 +614,8 @@ class ONNXOpMapper(OpMapper):
assert len( assert len(
indices_shape) <= 2, "Gather op don't support dim of indice >2 " indices_shape) <= 2, "Gather op don't support dim of indice >2 "
if axis == 0 and len(indices_shape) <= 1: if axis == 0 and len(indices_shape) <= 1:
node.fluid_code.add_layer('gather', node.fluid_code.add_layer(
'gather',
inputs={ inputs={
'input': val_x, 'input': val_x,
'index': indices 'index': indices
...@@ -634,25 +627,26 @@ class ONNXOpMapper(OpMapper): ...@@ -634,25 +627,26 @@ class ONNXOpMapper(OpMapper):
perm = [axis] + perm[:axis] + perm[axis + 1:] perm = [axis] + perm[:axis] + perm[axis + 1:]
attr_trans = {'perm': perm} attr_trans = {'perm': perm}
name_trans = val_x.layer_name + '_trans' name_trans = val_x.layer_name + '_trans'
node.fluid_code.add_layer('transpose', node.fluid_code.add_layer(
'transpose',
inputs=val_x, inputs=val_x,
output=name_trans, output=name_trans,
param_attr=attr_trans) param_attr=attr_trans)
node.fluid_code.add_layer('gather', node.fluid_code.add_layer(
'gather',
inputs={ inputs={
'input': name_trans, 'input': name_trans,
'index': indices 'index': indices
}, },
output=node, output=node,
param_attr=None) param_attr=None)
node.fluid_code.add_layer('transpose', node.fluid_code.add_layer(
inputs=node, 'transpose', inputs=node, output=node, param_attr=attr_trans)
output=node,
param_attr=attr_trans)
elif len(indices_shape) > 1: elif len(indices_shape) > 1:
from functools import reduce from functools import reduce
reshape_shape = reduce(lambda x, y: x * y, indices_shape) reshape_shape = reduce(lambda x, y: x * y, indices_shape)
node.fluid_code.add_layer('reshape', node.fluid_code.add_layer(
'reshape',
inputs=indices, inputs=indices,
output=indices, output=indices,
param_attr={'shape': [ param_attr={'shape': [
...@@ -663,28 +657,29 @@ class ONNXOpMapper(OpMapper): ...@@ -663,28 +657,29 @@ class ONNXOpMapper(OpMapper):
perm = [axis] + perm[:axis] + perm[axis + 1:] perm = [axis] + perm[:axis] + perm[axis + 1:]
attr_trans = {'perm': perm} attr_trans = {'perm': perm}
name_trans = val_x.layer_name + '_trans' name_trans = val_x.layer_name + '_trans'
node.fluid_code.add_layer('transpose', node.fluid_code.add_layer(
'transpose',
inputs=val_x, inputs=val_x,
output=name_trans, output=name_trans,
param_attr=attr_trans) param_attr=attr_trans)
node.fluid_code.add_layer('gather', node.fluid_code.add_layer(
'gather',
inputs={ inputs={
'input': name_trans, 'input': name_trans,
'index': indices 'index': indices
}, },
output=node, output=node,
param_attr=None) param_attr=None)
node.fluid_code.add_layer('transpose', node.fluid_code.add_layer(
inputs=node, 'transpose', inputs=node, output=node, param_attr=attr_trans)
output=node,
param_attr=attr_trans)
val_x_shape = val_x.out_shapes[0] val_x_shape = val_x.out_shapes[0]
reshaped_shape = [] reshaped_shape = []
for i in perm: for i in perm:
reshaped_shape.append(indices_shape[i]) reshaped_shape.append(indices_shape[i])
for i in val_x_shape[:axis] + val_x_shape[axis + 1:]: for i in val_x_shape[:axis] + val_x_shape[axis + 1:]:
reshaped_shape.append(i) reshaped_shape.append(i)
node.fluid_code.add_layer('reshape', node.fluid_code.add_layer(
'reshape',
inputs=node, inputs=node,
output=node, output=node,
param_attr={'shape': reshaped_shape}) param_attr={'shape': reshaped_shape})
...@@ -725,10 +720,8 @@ class ONNXOpMapper(OpMapper): ...@@ -725,10 +720,8 @@ class ONNXOpMapper(OpMapper):
if value > shape[axes[idx]]: if value > shape[axes[idx]]:
ends[idx] = shape[axes[idx]] ends[idx] = shape[axes[idx]]
attr = {"axes": axes, "starts": starts, "ends": ends} attr = {"axes": axes, "starts": starts, "ends": ends}
node.fluid_code.add_layer('slice', node.fluid_code.add_layer(
inputs=val_x, 'slice', inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def ConstantOfShape(self, node): def ConstantOfShape(self, node):
val_shape = self.graph.get_input_node(node, idx=0, copy=True) val_shape = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -751,10 +744,8 @@ class ONNXOpMapper(OpMapper): ...@@ -751,10 +744,8 @@ class ONNXOpMapper(OpMapper):
if dtype.name == 'int64': if dtype.name == 'int64':
dtype = 'int32' dtype = 'int32'
attr = {'shape': shape, 'dtype': string(dtype), 'value': value} attr = {'shape': shape, 'dtype': string(dtype), 'value': value}
node.fluid_code.add_layer('fill_constant', node.fluid_code.add_layer(
inputs=None, 'fill_constant', inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Split(self, node): def Split(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -769,10 +760,8 @@ class ONNXOpMapper(OpMapper): ...@@ -769,10 +760,8 @@ class ONNXOpMapper(OpMapper):
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer('split', node.fluid_code.add_layer(
inputs=val_x, 'split', inputs=val_x, output=val_y, param_attr=attr)
output=val_y,
param_attr=attr)
def Reshape(self, node): def Reshape(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -789,7 +778,8 @@ class ONNXOpMapper(OpMapper): ...@@ -789,7 +778,8 @@ class ONNXOpMapper(OpMapper):
shape, _, _ = self.get_dynamic_shape(val_shape.layer_name) shape, _, _ = self.get_dynamic_shape(val_shape.layer_name)
if val_shape.dtype == 'int64': if val_shape.dtype == 'int64':
val_shape_cast = val_shape.layer_name + '_cast' val_shape_cast = val_shape.layer_name + '_cast'
node.fluid_code.add_layer('cast', node.fluid_code.add_layer(
'cast',
inputs=val_shape, inputs=val_shape,
output=val_shape_cast, output=val_shape_cast,
param_attr={'dtype': string('int32')}) param_attr={'dtype': string('int32')})
...@@ -810,10 +800,8 @@ class ONNXOpMapper(OpMapper): ...@@ -810,10 +800,8 @@ class ONNXOpMapper(OpMapper):
val_x.layer_name, val_reshaped.layer_name) val_x.layer_name, val_reshaped.layer_name)
attr['shape'] = shape attr['shape'] = shape
node.fluid_code.add_layer('reshape', node.fluid_code.add_layer(
inputs=val_x, 'reshape', inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Cast(self, node): def Cast(self, node):
val_input = self.graph.get_input_node(node, idx=0, copy=True) val_input = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -827,10 +815,8 @@ class ONNXOpMapper(OpMapper): ...@@ -827,10 +815,8 @@ class ONNXOpMapper(OpMapper):
if output_dtype: if output_dtype:
assert dtype == output_dtype, 'dtype of to unmatches output' assert dtype == output_dtype, 'dtype of to unmatches output'
attr = {'dtype': string(dtype)} attr = {'dtype': string(dtype)}
node.fluid_code.add_layer('cast', node.fluid_code.add_layer(
inputs=val_input, 'cast', inputs=val_input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def AveragePool(self, node): def AveragePool(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -865,10 +851,8 @@ class ONNXOpMapper(OpMapper): ...@@ -865,10 +851,8 @@ class ONNXOpMapper(OpMapper):
"name": string(node.layer_name) "name": string(node.layer_name)
} }
node.fluid_code.add_layer(fluid_op, node.fluid_code.add_layer(
inputs=val_x, fluid_op, inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Concat(self, node): def Concat(self, node):
inputs = [] inputs = []
...@@ -880,19 +864,15 @@ class ONNXOpMapper(OpMapper): ...@@ -880,19 +864,15 @@ class ONNXOpMapper(OpMapper):
inputs.append(ipt.layer_name) inputs.append(ipt.layer_name)
axis = node.get_attr('axis') axis = node.get_attr('axis')
attr = {'axis': axis} attr = {'axis': axis}
node.fluid_code.add_layer('concat', node.fluid_code.add_layer(
inputs=inputs, 'concat', inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Flatten(self, node): def Flatten(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
axis = node.get_attr('axis', 1) axis = node.get_attr('axis', 1)
attr = {"axis": str(axis), "name": string(node.layer_name)} attr = {"axis": str(axis), "name": string(node.layer_name)}
node.fluid_code.add_layer('flatten', node.fluid_code.add_layer(
inputs=val_x, 'flatten', inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Gemm(self, node): def Gemm(self, node):
val_a = self.graph.get_input_node(node, idx=0, copy=True) val_a = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -911,7 +891,8 @@ class ONNXOpMapper(OpMapper): ...@@ -911,7 +891,8 @@ class ONNXOpMapper(OpMapper):
"alpha": alpha, "alpha": alpha,
"name": string(val_mm) "name": string(val_mm)
} }
node.fluid_code.add_layer('matmul', node.fluid_code.add_layer(
'matmul',
inputs=matmul_inputs, inputs=matmul_inputs,
output=val_mm, output=val_mm,
param_attr=attr_matmul) param_attr=attr_matmul)
...@@ -920,21 +901,24 @@ class ONNXOpMapper(OpMapper): ...@@ -920,21 +901,24 @@ class ONNXOpMapper(OpMapper):
if beta == 1.: if beta == 1.:
add_inputs = {"x": val_mm, "y": val_c} add_inputs = {"x": val_mm, "y": val_c}
attr = {"name": string(node.layer_name)} attr = {"name": string(node.layer_name)}
node.fluid_code.add_layer("elementwise_add", node.fluid_code.add_layer(
"elementwise_add",
inputs=add_inputs, inputs=add_inputs,
output=node, output=node,
param_attr=attr) param_attr=attr)
else: else:
var_beta = node.layer_name + '_beta' var_beta = node.layer_name + '_beta'
matmul_beta_inputs = {"x": val_c, "y": var_beta} matmul_beta_inputs = {"x": val_c, "y": var_beta}
node.fluid_code.add_layer("Constant", node.fluid_code.add_layer(
"Constant",
inputs=matmul_beta_inputs, inputs=matmul_beta_inputs,
output=var_beta, output=var_beta,
param_attr={'value': beta}) param_attr={'value': beta})
add_inputs = {"x": val_mm, "y": var_beta} add_inputs = {"x": val_mm, "y": var_beta}
attr = {"name": string(node.layer_name)} attr = {"name": string(node.layer_name)}
node.fluid_code.add_layer("elementwise_add", node.fluid_code.add_layer(
"elementwise_add",
inputs=add_inputs, inputs=add_inputs,
output=node, output=node,
param_attr=attr) param_attr=attr)
...@@ -953,19 +937,16 @@ class ONNXOpMapper(OpMapper): ...@@ -953,19 +937,16 @@ class ONNXOpMapper(OpMapper):
"x": node.layer_name, "x": node.layer_name,
"y": y, "y": y,
} }
node.fluid_code.add_layer("elementwise_add", node.fluid_code.add_layer(
inputs=inputs, "elementwise_add", inputs=inputs, output=node)
output=node)
def MatMul(self, node): def MatMul(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
val_y = self.graph.get_input_node(node, idx=1, copy=True) val_y = self.graph.get_input_node(node, idx=1, copy=True)
inputs = {"x": val_x, "y": val_y} inputs = {"x": val_x, "y": val_y}
attr = {"name": string(node.layer_name)} attr = {"name": string(node.layer_name)}
node.fluid_code.add_layer("matmul", node.fluid_code.add_layer(
inputs=inputs, "matmul", inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def BatchNormalization(self, node): def BatchNormalization(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -996,27 +977,21 @@ class ONNXOpMapper(OpMapper): ...@@ -996,27 +977,21 @@ class ONNXOpMapper(OpMapper):
"use_global_stats": spatial, "use_global_stats": spatial,
"name": string(node.layer_name) "name": string(node.layer_name)
} }
node.fluid_code.add_layer("batch_norm", node.fluid_code.add_layer(
inputs=val_x, "batch_norm", inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Transpose(self, node): def Transpose(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
perm = node.get_attr('perm') perm = node.get_attr('perm')
attr = {'perm': perm, "name": string(node.layer_name)} attr = {'perm': perm, "name": string(node.layer_name)}
node.fluid_code.add_layer("transpose", node.fluid_code.add_layer(
inputs=val_x, "transpose", inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Relu(self, node): def Relu(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
attr = {"name": string(node.layer_name)} attr = {"name": string(node.layer_name)}
node.fluid_code.add_layer("relu", node.fluid_code.add_layer(
inputs=val_x, "relu", inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def PRelu(self, node): def PRelu(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -1032,24 +1007,21 @@ class ONNXOpMapper(OpMapper): ...@@ -1032,24 +1007,21 @@ class ONNXOpMapper(OpMapper):
"param_attr": string(val_slope.layer_name), "param_attr": string(val_slope.layer_name),
'mode': string(mode) 'mode': string(mode)
} }
node.fluid_code.add_layer("prelu", node.fluid_code.add_layer(
inputs=val_x, "prelu", inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Squeeze(self, node): def Squeeze(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
axes = node.get_attr('axes') axes = node.get_attr('axes')
attr = {'axes': axes, "name": string(node.layer_name)} attr = {'axes': axes, "name": string(node.layer_name)}
node.fluid_code.add_layer("squeeze", node.fluid_code.add_layer(
inputs=val_x, "squeeze", inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Equal(self, node): def Equal(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
val_y = self.graph.get_input_node(node, idx=1, copy=True) val_y = self.graph.get_input_node(node, idx=1, copy=True)
node.fluid_code.add_layer("equal", node.fluid_code.add_layer(
"equal",
inputs={ inputs={
'x': val_x, 'x': val_x,
'y': val_y 'y': val_y
...@@ -1063,22 +1035,26 @@ class ONNXOpMapper(OpMapper): ...@@ -1063,22 +1035,26 @@ class ONNXOpMapper(OpMapper):
val_y = self.graph.get_input_node(node, idx=2, copy=True) val_y = self.graph.get_input_node(node, idx=2, copy=True)
not_condition = condition.layer_name + '_not' not_condition = condition.layer_name + '_not'
node.fluid_code.add_layer("logical_not", node.fluid_code.add_layer(
"logical_not",
inputs=condition, inputs=condition,
output=not_condition, output=not_condition,
param_attr=None) param_attr=None)
cast_not_condition = not_condition + '_cast' cast_not_condition = not_condition + '_cast'
node.fluid_code.add_layer("cast", node.fluid_code.add_layer(
"cast",
inputs=not_condition, inputs=not_condition,
output=cast_not_condition, output=cast_not_condition,
param_attr={'dtype': string(val_x.dtype)}) param_attr={'dtype': string(val_x.dtype)})
cast_condition = condition.layer_name + '_cast' cast_condition = condition.layer_name + '_cast'
node.fluid_code.add_layer("cast", node.fluid_code.add_layer(
"cast",
inputs=condition, inputs=condition,
output=cast_condition, output=cast_condition,
param_attr={'dtype': string(val_x.dtype)}) param_attr={'dtype': string(val_x.dtype)})
mul_val_x = val_x.layer_name + '_mul' mul_val_x = val_x.layer_name + '_mul'
node.fluid_code.add_layer("elementwise_mul", node.fluid_code.add_layer(
"elementwise_mul",
inputs={ inputs={
'x': val_x, 'x': val_x,
'y': cast_condition 'y': cast_condition
...@@ -1087,7 +1063,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1087,7 +1063,8 @@ class ONNXOpMapper(OpMapper):
param_attr=None) param_attr=None)
mul_val_y = val_y.layer_name + '_mul' mul_val_y = val_y.layer_name + '_mul'
node.fluid_code.add_layer("elementwise_mul", node.fluid_code.add_layer(
"elementwise_mul",
inputs={ inputs={
'x': val_y, 'x': val_y,
'y': cast_not_condition 'y': cast_not_condition
...@@ -1095,7 +1072,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1095,7 +1072,8 @@ class ONNXOpMapper(OpMapper):
output=mul_val_y, output=mul_val_y,
param_attr=None) param_attr=None)
node.fluid_code.add_layer("elementwise_add", node.fluid_code.add_layer(
"elementwise_add",
inputs={ inputs={
'x': mul_val_x, 'x': mul_val_x,
'y': mul_val_y 'y': mul_val_y
...@@ -1106,9 +1084,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1106,9 +1084,8 @@ class ONNXOpMapper(OpMapper):
def NonZero(self, node): def NonZero(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
where_name = node.layer_name + '_where' where_name = node.layer_name + '_where'
node.fluid_code.add_layer("where", node.fluid_code.add_layer(
inputs=val_x.layer_name + '!=0', "where", inputs=val_x.layer_name + '!=0', output=where_name)
output=where_name)
dims = len(val_x.out_shapes[0]) dims = len(val_x.out_shapes[0])
elements_count_val_x = reduce(lambda x, y: x * y, val_x.out_shapes[0]) elements_count_val_x = reduce(lambda x, y: x * y, val_x.out_shapes[0])
flatten_names = [] flatten_names = []
...@@ -1121,18 +1098,15 @@ class ONNXOpMapper(OpMapper): ...@@ -1121,18 +1098,15 @@ class ONNXOpMapper(OpMapper):
'starts': [0, dim], 'starts': [0, dim],
'ends': [elements_count_val_x, dim + 1] 'ends': [elements_count_val_x, dim + 1]
} }
node.fluid_code.add_layer("slice", node.fluid_code.add_layer(
inputs=where_name, "slice", inputs=where_name, output=slice_name, param_attr=attr)
output=slice_name, node.fluid_code.add_layer(
param_attr=attr) "flatten",
node.fluid_code.add_layer("flatten",
inputs=slice_name, inputs=slice_name,
output=flatten_name, output=flatten_name,
param_attr={'axis': 0}) param_attr={'axis': 0})
node.fluid_code.add_layer("concat", node.fluid_code.add_layer(
inputs=flatten_names, "concat", inputs=flatten_names, output=node, param_attr={'axis': 0})
output=node,
param_attr={'axis': 0})
def Identity(self, node): def Identity(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -1151,10 +1125,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1151,10 +1125,8 @@ class ONNXOpMapper(OpMapper):
'expand_times': repeats, 'expand_times': repeats,
"name": string(node.layer_name), "name": string(node.layer_name),
} }
node.fluid_code.add_layer("expand", node.fluid_code.add_layer(
inputs=val_x, "expand", inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def MaxPool(self, node): def MaxPool(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -1191,10 +1163,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1191,10 +1163,8 @@ class ONNXOpMapper(OpMapper):
"name": string(node.layer_name), "name": string(node.layer_name),
"exclusive": False "exclusive": False
} }
node.fluid_code.add_layer(fluid_op, node.fluid_code.add_layer(
inputs=val_x, fluid_op, inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def _global_pool(self, node): def _global_pool(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -1220,10 +1190,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1220,10 +1190,8 @@ class ONNXOpMapper(OpMapper):
"global_pooling": True, "global_pooling": True,
"name": string(node.layer_name) "name": string(node.layer_name)
} }
node.fluid_code.add_layer(fluid_op, node.fluid_code.add_layer(
inputs=val_x, fluid_op, inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def GlobalMaxPool(self, node): def GlobalMaxPool(self, node):
self._global_pool(node) self._global_pool(node)
...@@ -1279,10 +1247,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1279,10 +1247,8 @@ class ONNXOpMapper(OpMapper):
attr["bias_attr"] = string(val_b.layer_name) attr["bias_attr"] = string(val_b.layer_name)
else: else:
attr["bias_attr"] = False attr["bias_attr"] = False
node.fluid_code.add_layer(fluid_op, node.fluid_code.add_layer(
inputs=val_x, fluid_op, inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def ConvTranspose(self, node): def ConvTranspose(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -1332,10 +1298,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1332,10 +1298,8 @@ class ONNXOpMapper(OpMapper):
'bias_attr': None if val_b is None else string(val_b.layer_name), 'bias_attr': None if val_b is None else string(val_b.layer_name),
'name': string(node.layer_name), 'name': string(node.layer_name),
} }
node.fluid_code.add_layer(fluid_op, node.fluid_code.add_layer(
inputs=val_x, fluid_op, inputs=val_x, output=node, param_attr=attr)
output=node,
param_attr=attr)
def GRU(self, node): def GRU(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -1352,15 +1316,13 @@ class ONNXOpMapper(OpMapper): ...@@ -1352,15 +1316,13 @@ class ONNXOpMapper(OpMapper):
else: else:
miss_arg_num += 1 miss_arg_num += 1
if num_ipt > 4 and node.layer.input[4] != '': if num_ipt > 4 and node.layer.input[4] != '':
val_len = self.graph.get_input_node(node, val_len = self.graph.get_input_node(
idx=4 - miss_arg_num, node, idx=4 - miss_arg_num, copy=True)
copy=True)
else: else:
miss_arg_num += 1 miss_arg_num += 1
if num_ipt > 5 and node.layer.input[5] != '': if num_ipt > 5 and node.layer.input[5] != '':
val_xh = self.graph.get_input_node(node, val_xh = self.graph.get_input_node(
idx=5 - miss_arg_num, node, idx=5 - miss_arg_num, copy=True)
copy=True)
data, dtype, shape = self.get_dynamic_shape(val_x.layer_name) data, dtype, shape = self.get_dynamic_shape(val_x.layer_name)
...@@ -1401,7 +1363,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1401,7 +1363,8 @@ class ONNXOpMapper(OpMapper):
is_reverse = direction == 'reverse' is_reverse = direction == 'reverse'
var_x0 = node.layer_name + '_x0' var_x0 = node.layer_name + '_x0'
node.fluid_code.add_layer('squeeze', node.fluid_code.add_layer(
'squeeze',
inputs=val_x, inputs=val_x,
output=var_x0, output=var_x0,
param_attr={ param_attr={
...@@ -1410,7 +1373,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1410,7 +1373,8 @@ class ONNXOpMapper(OpMapper):
}) })
var_w0 = node.layer_name + '_w0' var_w0 = node.layer_name + '_w0'
node.fluid_code.add_layer('squeeze', node.fluid_code.add_layer(
'squeeze',
inputs=val_w, inputs=val_w,
output=var_w0, output=var_w0,
param_attr={ param_attr={
...@@ -1420,7 +1384,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1420,7 +1384,8 @@ class ONNXOpMapper(OpMapper):
var_fc = node.layer_name + '_fc' var_fc = node.layer_name + '_fc'
var_mm = (node.layer_name + '_mm') if val_b else var_fc var_mm = (node.layer_name + '_mm') if val_b else var_fc
node.fluid_code.add_layer('matmul', node.fluid_code.add_layer(
'matmul',
inputs={ inputs={
'x': var_x0, 'x': var_x0,
'y': var_w0 'y': var_w0
...@@ -1433,7 +1398,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1433,7 +1398,8 @@ class ONNXOpMapper(OpMapper):
}) })
var_r0 = node.layer_name + '_r0' var_r0 = node.layer_name + '_r0'
node.fluid_code.add_layer('squeeze', node.fluid_code.add_layer(
'squeeze',
inputs=val_r, inputs=val_r,
output=var_r0, output=var_r0,
param_attr={ param_attr={
...@@ -1443,7 +1409,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1443,7 +1409,8 @@ class ONNXOpMapper(OpMapper):
var_r0t = node.layer_name + '_r0t' var_r0t = node.layer_name + '_r0t'
node.fluid_code.add_layer('transpose', node.fluid_code.add_layer(
'transpose',
inputs=var_r0, inputs=var_r0,
output=var_r0t, output=var_r0t,
param_attr={ param_attr={
...@@ -1453,19 +1420,18 @@ class ONNXOpMapper(OpMapper): ...@@ -1453,19 +1420,18 @@ class ONNXOpMapper(OpMapper):
if val_b: if val_b:
var_bi = node.layer_name + '_bi' var_bi = node.layer_name + '_bi'
var_bh = node.layer_name + '_bh' var_bh = node.layer_name + '_bh'
node.fluid_code.add_layer('split', node.fluid_code.add_layer(
'split',
inputs=val_b, inputs=val_b,
output=var_bi + ',' + var_bh, output=var_bi + ',' + var_bh,
param_attr={ param_attr={
'axis': 'axis': 1,
1, 'split': [hidden_size * 3, hidden_size * 3],
'split': 'name': string(node.layer_name + '.b/split')
[hidden_size * 3, hidden_size * 3],
'name':
string(node.layer_name + '.b/split')
}) })
var_bi0 = node.layer_name + '_bi0' var_bi0 = node.layer_name + '_bi0'
node.fluid_code.add_layer('squeeze', node.fluid_code.add_layer(
'squeeze',
inputs=var_bi, inputs=var_bi,
output=var_bi0, output=var_bi0,
param_attr={ param_attr={
...@@ -1473,19 +1439,19 @@ class ONNXOpMapper(OpMapper): ...@@ -1473,19 +1439,19 @@ class ONNXOpMapper(OpMapper):
'name': string(var_bi0) 'name': string(var_bi0)
}) })
node.fluid_code.add_layer('elmentwise_add', node.fluid_code.add_layer(
'elmentwise_add',
inputs=[var_mm, var_bi0], inputs=[var_mm, var_bi0],
output=var_fc, output=var_fc,
param_attr={ param_attr={
'axes': 'axes': 1,
1, 'name': string(node.layer_name + '.i/bias')
'name':
string(node.layer_name + '.i/bias')
}) })
if val_xh: if val_xh:
var_xh0 = node.layer_name + '_xh0' var_xh0 = node.layer_name + '_xh0'
node.fluid_code.add_layer('squeeze', node.fluid_code.add_layer(
'squeeze',
inputs=val_xh, inputs=val_xh,
output=var_xh0, output=var_xh0,
param_attr={ param_attr={
...@@ -1503,7 +1469,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1503,7 +1469,8 @@ class ONNXOpMapper(OpMapper):
'param_attr': string(var_r0t), 'param_attr': string(var_r0t),
'bias_attr': string(var_bh) if val_b else False, 'bias_attr': string(var_bh) if val_b else False,
} }
node.fluid_code.add_layer('dynamic_gru', node.fluid_code.add_layer(
'dynamic_gru',
inputs=var_fc + ',' + str(hidden_size), inputs=var_fc + ',' + str(hidden_size),
output=var_y00, output=var_y00,
param_attr=attr) param_attr=attr)
...@@ -1511,7 +1478,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1511,7 +1478,8 @@ class ONNXOpMapper(OpMapper):
num_opt = len(node.layer.output) num_opt = len(node.layer.output)
if num_opt > 0 and node.layer.output[0] != '': if num_opt > 0 and node.layer.output[0] != '':
node.fluid_code.add_layer('unsqueeze', node.fluid_code.add_layer(
'unsqueeze',
inputs=var_y00, inputs=var_y00,
output=node.layer.output[0], output=node.layer.output[0],
param_attr={ param_attr={
...@@ -1519,7 +1487,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1519,7 +1487,8 @@ class ONNXOpMapper(OpMapper):
'name': string(node.layer.output[0]) 'name': string(node.layer.output[0])
}) })
if num_opt > 1 and node.layer.output[1] != '': if num_opt > 1 and node.layer.output[1] != '':
node.fluid_code.add_layer('unsqueeze', node.fluid_code.add_layer(
'unsqueeze',
inputs=var_y00, inputs=var_y00,
output=node.layer.output[1], output=node.layer.output[1],
param_attr={ param_attr={
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册