Skip to content

Commit

Permalink
转换规则 No.252/256/267 (#148)
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
co63oc authored Jul 5, 2023
1 parent 9d691cc commit b11376a
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 19 deletions.
31 changes: 28 additions & 3 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -3699,12 +3699,15 @@
}
},
"torch.exp": {
"Matcher": "ExpMatcher",
"Matcher": "GenericMatcher",
"paddle_api": "paddle.exp",
"args_list": [
"input",
"out"
]
],
"kwargs_change": {
"input": "x"
}
},
"torch.exp2": {
"Matcher": "Exp2Matcher",
Expand All @@ -3714,7 +3717,7 @@
]
},
"torch.expm1": {
"Matcher": "ExpMatcher",
"Matcher": "GenericMatcher",
"paddle_api": "paddle.expm1",
"args_list": [
"input",
Expand Down Expand Up @@ -4746,6 +4749,17 @@
"input": "x"
}
},
"torch.linalg.multi_dot": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.linalg.multi_dot",
"args_list": [
"tensors",
"out"
],
"kwargs_change": {
"tensors": "x"
}
},
"torch.linalg.norm": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.linalg.norm",
Expand Down Expand Up @@ -8660,6 +8674,17 @@
"input": "x"
}
},
"torch.special.expm1": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.expm1",
"args_list": [
"input",
"out"
],
"kwargs_change": {
"input": "x"
}
},
"torch.special.log1p": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.log1p",
Expand Down
16 changes: 0 additions & 16 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2419,22 +2419,6 @@ def generate_code(self, kwargs):
return code


class ExpMatcher(BaseMatcher):
def generate_code(self, kwargs):
if "input" in kwargs:
kwargs["x"] = "(" + kwargs.pop("input").strip("\n") + ").astype('float32')"

if "out" in kwargs and kwargs["out"] is not None:
out_v = kwargs.pop("out").strip("\n")
code = "paddle.assign({}({}), output={})".format(
self.get_paddle_api(), self.kwargs_to_str(kwargs), out_v
)
else:
code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(kwargs))

return code


class TensorSVDMatcher(BaseMatcher):
def generate_code(self, kwargs):

Expand Down
72 changes: 72 additions & 0 deletions tests/test_dsplit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import textwrap

from apibase import APIBase

obj = APIBase("torch.dsplit")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.arange(16.0).reshape(2, 2, 4)
result = torch.dsplit(a, 2)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.arange(16.0).reshape(2, 2, 4)
result = torch.dsplit(a, [2,2])
if len(result) > 2:
result = (result[0], result[2])
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.arange(12).reshape(3, 2, 2)
result = torch.dsplit(a, indices=[1,1])
if len(result) > 2:
result = (result[0], result[2])
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
54 changes: 54 additions & 0 deletions tests/test_linalg_multi_dot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import textwrap

from apibase import APIBase

obj = APIBase("torch.linalg.multi_dot")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
input = [torch.tensor([1, 2], dtype=torch.float32), torch.tensor([2, 3], dtype=torch.float32)]
result = torch.linalg.multi_dot(input)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
input = [torch.tensor([1, 2], dtype=torch.float32), torch.tensor([2, 3], dtype=torch.float32)]
result = torch.linalg.multi_dot(tensors=input)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
input = [torch.tensor([1, 2], dtype=torch.float32), torch.tensor([2, 3], dtype=torch.float32)]
out = torch.tensor([])
result = torch.linalg.multi_dot(input, out=out)
"""
)
obj.run(pytorch_code, ["result"])
64 changes: 64 additions & 0 deletions tests/test_special_expm1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import textwrap

from apibase import APIBase

obj = APIBase("torch.special.expm1")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.special.expm1(torch.tensor([0., -2., 3.]))
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.tensor([-1., -2., 3.])
result = torch.special.expm1(a)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
a = [-1, -2, 3]
out = torch.tensor(a, dtype=torch.float32)
result = torch.special.expm1(torch.tensor(a), out=out)
"""
)
obj.run(pytorch_code, ["out"])


def test_case_4():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.tensor([-1, -2, 3])
result = torch.special.expm1(input=a)
"""
)
obj.run(pytorch_code, ["result"])

0 comments on commit b11376a

Please sign in to comment.