Skip to content

fix bug of clip and prelu #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion x2paddle/decoder/onnx_decoder.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ def __init__(self, layer, layer_name=None):
self.dtype = None
self.which_child = {}

def get_input_index(self, input_name):
"""
get the index of input_name in layer.input
-1 means input_name is not in the input
"""
index = -1
for i in range(len(self.layer.input)):
if input_name == self.layer.input[i]:
index = i
break
return index

def get_attr_map(self):
"""
convert ONNX node attributes to dict
Expand Down Expand Up @@ -294,7 +306,6 @@ def build(self):
for layer_name, node in self.node_map.items():
if isinstance(node, ONNXGraphNode):
self.build_connection(layer_name, node)

#generate topo
super(ONNXGraph, self).build()

Expand Down
54 changes: 41 additions & 13 deletions x2paddle/op_mapper/onnx2paddle/opset9/opset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,17 @@ def ConstantOfShape(self, node):
outputs=[node.name],
**layer_attrs)

@print_mapping_info
def GatherND(self, node):
print(len(node.inputs), node.inputs)
val_x = self.graph.get_input_node(node, idx=0, copy=True)
val_y = self.graph.get_input_node(node, idx=1, copy=True)
self.paddle_graph.add_layer(
"paddle.gather_nd",
inputs={"x": val_x.name,
"index": val_y.name},
outputs=[node.name])

@print_mapping_info
def Clip(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True)
Expand All @@ -1165,23 +1176,40 @@ def Clip(self, node):
outputs=[node.name],
**layer_attrs)
else:
min_ipt = self.graph.get_input_node(node, idx=1, copy=True)
max_ipt = self.graph.get_input_node(node, idx=2, copy=True)
min_value = _const_weight_or_none(min_ipt)
max_value = _const_weight_or_none(max_ipt)
if max_value.shape == (1, ):
max_value = max_value[0]
if min_value.shape == (1, ):
min_value = min_value[0]
if max_value is not None and min_value is not None:
layer_attrs = {'max': max_value, 'min': min_value}
if len(node.inputs) == 2:
val_ipt = self.graph.get_input_node(node, idx=1, copy=True)

index = node.get_input_index(val_ipt.name)

val_value = _const_weight_or_none(val_ipt)
if val_value.shape == (1, ):
val_value = val_value[0]

if index == 1:
layer_attrs = {'min': val_value}

if index == 2:
layer_attrs = {'max': val_value}

self.paddle_graph.add_layer(
'paddle.clip',
inputs={"x": val_x.name},
outputs=[node.name],
**layer_attrs)
else:
raise Exception("max_value or min_value can't be None")
if len(node.inputs) == 3:
min_ipt = self.graph.get_input_node(node, idx=1, copy=True)
max_ipt = self.graph.get_input_node(node, idx=2, copy=True)
self.paddle_graph.add_layer(
'paddle.clip',
inputs={
"x": val_x.name,
"min": min_ipt.name,
"max": max_ipt.name
},
outputs=[node.name])
else:
raise Exception("max_value or min_value can't be None")

@print_mapping_info
def Split(self, node):
Expand Down Expand Up @@ -1549,9 +1577,9 @@ def PRelu(self, node):
num_parameters = val_x.out_shapes[0][1]
else:
num_parameters = 1
slope_data = self.weights[val_slope.name]
_rename_or_remove_weight(self.weights, val_slope.name)
self.weights[op_name + '._weight'] = np.reshape(
self.weights[val_slope.name], [1])
self.weights[op_name + '._weight'] = np.reshape(slope_data, [1])
self.paddle_graph.add_layer(
"paddle.nn.PReLU",
inputs={"x": val_x.name},
Expand Down