Skip to content
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

[PaddlePaddle Hackathon 4][Frontend][Paddle]add thresholded_relu/index_select/eye/linspace/take_alone_axis/dist for paddle frontend #14172

Merged
merged 8 commits into from
Mar 12, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix confilt
  • Loading branch information
XG-zheng committed Mar 10, 2023

Verified

This commit was signed with the committer’s verified signature.
gsmet Guillaume Smet
commit e3f4afa29556a6cd2cad5eb9d113e2cda0d285f5
31 changes: 31 additions & 0 deletions python/tvm/relay/frontend/paddlepaddle.py
Original file line number Diff line number Diff line change
@@ -2188,6 +2188,36 @@ def convert_thresholded_relu(g, op, block):
out = tvm.relay.where(x > threshold, x, zero)
g.add_node(op.output("Out")[0], out)

def convert_tile(g, op, block):
"""Operator converter for tile."""

x = g.get_node(op.input("X")[0])
if op.input("RepeatTimes"):
reps = g.get_node(op.input("RepeatTimes")[0])
reps, infered = try_infer_value(reps, g.get_params())
if infered:
reps = reps.tolist()
elif op.input("repeat_times_tensor"):
reps = []
for rep_value in op.input("repeat_times_tensor"):
rep_value = g.get_node(rep_value).astype("int32")
reps.append(rep_value)
reps = _op.concatenate(reps, axis=0)
reps, infered = try_infer_value(reps, g.get_params())
if infered:
reps = reps.tolist()
else:
reps = op.attr("repeat_times")
infered = True

if not infered:
msg = 'Value {} in attribute "repeat_times" of operator Tile is not "valid."'
raise tvm.error.OpAttributeInvalid(msg.format(reps))

op_func = get_relay_op(op.type)
out = op_func(x, reps=reps)
g.add_node(op.output("Out")[0], out)


def convert_topk(g, op, block):
"""Operator converter for topk."""
@@ -2412,6 +2442,7 @@ def convert_where_index(g, op, block):
"tan": convert_unary_op,
"tanh": convert_unary_op,
"thresholded_relu": convert_thresholded_relu,
"tile": convert_tile,
"top_k_v2": convert_topk,
"transpose2": convert_transpose,
"unsqueeze2": convert_unsqueeze,
187 changes: 187 additions & 0 deletions tests/python/frontend/paddlepaddle/test_forward.py
Original file line number Diff line number Diff line change
@@ -1784,6 +1784,193 @@ def where_index_1(inputs):


@tvm.testing.uses_gpu
def test_forward_stack():
class Stack1(nn.Layer):
@paddle.jit.to_static
def forward(self, input0, input1, input2):
return paddle.stack([input0, input1, input2], axis=-1)

class Stack2(nn.Layer):
@paddle.jit.to_static
def forward(self, input0, input1, input2):
return paddle.stack([input0, input1, input2], axis=1)

class Stack3(nn.Layer):
@paddle.jit.to_static
def forward(self, input0, input1, input2):
return paddle.stack([input0, input1, input2], axis=2)

input_shapes = [[2, 3], [5, 10, 11], [3, 4, 5, 6]]
for input_shape in input_shapes:
input_data_0 = paddle.randn(shape=input_shape, dtype="float32")
input_data_1 = paddle.randn(shape=input_shape, dtype="float32")
input_data_2 = paddle.randn(shape=input_shape, dtype="float32")
verify_model(Stack1(), [input_data_0, input_data_1, input_data_2])
verify_model(Stack2(), [input_data_0, input_data_1, input_data_2])
verify_model(Stack3(), [input_data_0, input_data_1, input_data_2])


@tvm.testing.uses_gpu
def test_forward_unstack():
class UnStack1(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
return paddle.unstack(inputs, axis=-1)

class UnStack2(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
return paddle.unstack(inputs, axis=1)

class UnStack3(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
return paddle.unstack(inputs, axis=0)

input_shapes = [[2, 3], [5, 10, 11], [3, 4, 5, 6], [1, 3, 4, 1, 1]]
for input_shape in input_shapes:
input_data = paddle.randn(shape=input_shape, dtype="float32")
verify_model(UnStack1(), input_data)
verify_model(UnStack2(), input_data)
verify_model(UnStack3(), input_data)


@tvm.testing.uses_gpu
def test_forward_silu():
class Silu(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
return nn.functional.silu(inputs)

input_shapes = [[10], [2, 3], [5, 10, 11], [3, 4, 5, 6]]
for input_shape in input_shapes:
input_data = paddle.randn(shape=input_shape, dtype="float32")
verify_model(Silu(), input_data=input_data)


@tvm.testing.uses_gpu
def test_forward_softshrink():
@paddle.jit.to_static
def Softshrink1(input):
return nn.functional.softshrink(input, threshold=0.0)

@paddle.jit.to_static
def Softshrink2(input):
return nn.functional.softshrink(input, threshold=0.5)

@paddle.jit.to_static
def Softshrink3(input):
return nn.functional.softshrink(input, threshold=1.0)

x = paddle.to_tensor([-0.9, -0.2, 0.1, 0.8])
verify_model(Softshrink2, x)

input_shapes = [[10], [2, 3], [5, 10, 11], [3, 4, 5, 6]]
for input_shape in input_shapes:
input_data = paddle.randn(shape=input_shape, dtype="float32")
verify_model(Softshrink1, input_data=input_data)
verify_model(Softshrink2, input_data=input_data)
verify_model(Softshrink3, input_data=input_data)


@tvm.testing.uses_gpu
def test_forward_where():
@paddle.jit.to_static
def where1(x, y):
return paddle.where(x > 1, x, y)

@paddle.jit.to_static
def where2(x, y):
return paddle.where(x > y, x, y)

x = paddle.to_tensor([0.9383, 0.1983, 3.2, 1.2])
y = paddle.to_tensor([1.0, 1.0, 1.0, 1.0])
verify_model(where1, [x, y])

input_shapes = [[10], [2, 3], [5, 10, 11], [3, 4, 5, 6]]
for input_shape in input_shapes:
x = paddle.randn(shape=input_shape, dtype="float32")
y = paddle.randn(shape=input_shape, dtype="float32")
verify_model(where1, [x, y])
verify_model(where2, [x, y])


@tvm.testing.uses_gpu
def test_forward_tile():
class Tile1(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
return paddle.tile(inputs, repeat_times=[10])

class Tile2(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
return paddle.tile(inputs, repeat_times=[2, 3])

class Tile3(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
return paddle.tile(inputs, repeat_times=[1, 2, 3])

class Tile4(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
return paddle.tile(inputs, repeat_times=[2, 3, 4, 1, 5])

class Tile5(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
reps = paddle.to_tensor([3, 2])
reps = paddle.cast(reps, "int32")
return paddle.tile(inputs, repeat_times=reps)

class Tile6(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
rep_0 = paddle.to_tensor([3])
rep_1 = paddle.to_tensor([2])
rep_0 = paddle.cast(rep_0, "int32")
rep_1 = paddle.cast(rep_1, "int32")
return paddle.tile(inputs, repeat_times=[rep_0, rep_1])

input_shapes = [
[10],
[2, 3],
[3, 4, 5],
[5, 3, 1, 4],
[1, 3, 1, 6, 7],
]
for input_shape in input_shapes:
input_data = paddle.randn(shape=input_shape, dtype="float32")
verify_model(Tile1(), input_data=input_data)
verify_model(Tile2(), input_data=input_data)
verify_model(Tile3(), input_data=input_data)
verify_model(Tile4(), input_data=input_data)
verify_model(Tile5(), input_data=input_data)
verify_model(Tile6(), input_data=input_data)


@tvm.testing.uses_gpu
def test_forward_mish():
class Mish(nn.Layer):
@paddle.jit.to_static
def forward(self, inputs):
return nn.functional.mish(inputs)

input_shapes = [[10], [2, 3], [5, 10, 11], [3, 4, 5, 6]]
if paddle.version.full_version >= "2.4.2":
for input_shape in input_shapes:
input_data = paddle.randn(shape=input_shape, dtype="float32")
verify_model(Mish(), input_data=input_data)
input_data += 20.0
verify_model(Mish(), input_data=input_data)

input_data = paddle.to_tensor([-5.0, 0.0, 5.0, 23.1, 20.0])
verify_model(Mish(), input_data=input_data)


@tvm.testing.uses_gpu
<<<<<<< HEAD
def test_forward_thresholded_relu():
class ThresholdedRelu(nn.Layer):
@paddle.jit.to_static
You are viewing a condensed version of this merge commit. You can view the full changes here.