Skip to content

Commit

Permalink
Merge pull request apache#46 from wjj19950828/paddle_frontend
Browse files Browse the repository at this point in the history
Fixed Dynamic bugs
  • Loading branch information
jiangjiajun authored Sep 23, 2021
2 parents 9ea0e53 + 81a83f4 commit bb76e0a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 39 deletions.
53 changes: 20 additions & 33 deletions python/tvm/relay/frontend/paddlepaddle.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ def convert_crop(g, op, block):
"""Operator converter for crop."""

x = g.get_node(op.input("X")[0])
dims = len(infer_shape(x))
input_shape = op.input("Shape")
input_offsets = op.input("Offsets")
if input_shape:
Expand All @@ -616,13 +617,15 @@ def convert_crop(g, op, block):
else:
offsets = op.attr("offsets")

crop_len = len(shape)
slice_start = [0] * crop_len
slice_end = shape
for i in range(crop_len):
slice_start[i] += offsets[i]
slice_end[i] += offsets[i]
out = _op.strided_slice(x, slice_start, slice_end)
if not isinstance(shape, _expr.Expr):
shape = _op.const(shape, "int32")
if not isinstance(offsets, _expr.Expr):
offsets = _op.const(offsets, "int32")
slice_start = offsets
slice_end = _op.add(shape, offsets)
strides = _op.const([1] * dims, dtype="int32")

out = _op.strided_slice(x, slice_start, slice_end, strides)
g.add_node(op.output("Out")[0], out)


Expand Down Expand Up @@ -739,28 +742,13 @@ def convert_expand(g, op, block):
"""Operator converter for expand."""

x = g.get_node(op.input("X")[0])
input_shape = list(infer_shape(x))

ndims = len(input_shape)
if op.input("Shape"):
sizes = g.get_node(op.input("Shape")[0])
sizes = _infer_value(sizes, g.get_params())
else:
sizes = op.attr("shape")

out = x
out_dims = len(sizes)
if ndims < out_dims:
num_newaxis = out_dims - ndims
out = _op.expand_dims(out, axis=0, num_newaxis=num_newaxis)
input_shape = [1] * num_newaxis + input_shape

for i in range(out_dims):
if sizes[i] != -1 and input_shape[i] == 1:
if not isinstance(sizes[i], int):
sizes[i] = int(infer_value(sizes[i], {}).numpy())
out = _op.repeat(out, sizes[i], axis=i)

out = _op.broadcast_to(x, sizes)
g.add_node(op.output("Out")[0], out)


Expand Down Expand Up @@ -1784,16 +1772,15 @@ def make_init_param_inputs(g, node, layer):
axes=[0],
)
return init_h, init_c
else:
all_init_h = node.input("PreState")[0]
bidirect_len = 2 if node.attr("is_bidirec") else 1
init_h = _op.strided_slice(
g.get_node(all_init_h),
[layer * bidirect_len],
[layer * bidirect_len + bidirect_len],
axes=[0],
)
return init_h
all_init_h = node.input("PreState")[0]
bidirect_len = 2 if node.attr("is_bidirec") else 1
init_h = _op.strided_slice(
g.get_node(all_init_h),
[layer * bidirect_len],
[layer * bidirect_len + bidirect_len],
axes=[0],
)
return init_h

hidden_size = op.attr("hidden_size")
num_layers = op.attr("num_layers")
Expand Down
11 changes: 5 additions & 6 deletions tests/python/frontend/paddlepaddle/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,18 +485,17 @@ def crop3(inputs):
return paddle.crop(inputs, shape=[3, 3], offsets=offsets)

@paddle.jit.to_static
def crop4(inputs):
shape = paddle.to_tensor(np.array([3, 3]).astype("int32"))
offsets = paddle.to_tensor(np.array([1, 1]).astype("int32"))
def crop4(inputs, shape, offsets):
return paddle.crop(inputs, shape=shape, offsets=offsets)

input_shape = [10, 10]
input_data = paddle.rand(input_shape, dtype="float32")
verify_model(crop1, input_data=[input_data])
shape = paddle.to_tensor(np.array([3, 3], "int32"))
# verify_model(crop2, [input_data, shape], input_shape=[[-1, -1], [2]])
verify_model(crop2, [input_data, shape], input_shape=[[-1, -1], [2]])
verify_model(crop3, input_data=[input_data])
verify_model(crop4, input_data=[input_data])
offsets = paddle.to_tensor(np.array([1, 1]).astype("int32"))
verify_model(crop4, input_data=[input_data, shape, offsets], input_shape=[[-1, -1], [2], [2]])


@tvm.testing.uses_gpu
Expand Down Expand Up @@ -691,7 +690,7 @@ def expand2(inputs, shape):
x_data = paddle.rand(x_shape, dtype="float32")
verify_model(expand1, input_data=[x_data])
shape = paddle.to_tensor(np.array([2, 3]).astype("int32"))
# verify_model(expand2, [x_data, shape], input_shape=[[3], [2]])
verify_model(expand2, [x_data, shape], input_shape=[[3], [2]])


@tvm.testing.uses_gpu
Expand Down

0 comments on commit bb76e0a

Please sign in to comment.