Skip to content
Closed
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
93 changes: 89 additions & 4 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -2309,7 +2309,25 @@
]
},
"torch.Tensor.qscheme": {},
"torch.Tensor.quantile": {},
"torch.Tensor.quantile": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.quantile",
"min_input_args": 1,
"args_list": [
"q",
"dim",
"keepdim",
"*",
"interpolation"
],
"kwargs_change": {
"dim": "axis",
"input": "x"
},
"unsupport_args": [
"interpolation"
]
},
"torch.Tensor.rad2deg": {
"Matcher": "UnchangeMatcher"
},
Expand Down Expand Up @@ -2712,7 +2730,22 @@
"correction": "unbiased"
}
},
"torch.Tensor.stft": {},
"torch.Tensor.stft": {
"Matcher": "StftMatcher",
"paddle_api": "paddle.signal.stft",
"min_input_args": 1,
"args_list": [
"n_fft",
"hop_length",
"win_length",
"window",
"center",
"normalized",
"onesided",
"length",
"return_complex"
]
},
"torch.Tensor.storage": {},
"torch.Tensor.storage_offset": {},
"torch.Tensor.storage_type": {},
Expand Down Expand Up @@ -2820,7 +2853,11 @@
"paddle_api": "paddle.Tensor.tanh_"
},
"torch.Tensor.tensor_split": {},
"torch.Tensor.tile": {},
"torch.Tensor.tile": {
"Matcher": "TensorTileMatcher",
"min_input_args": 1,
"paddle_api": "paddle.Tensor.tile"
},
"torch.Tensor.to": {
"Matcher": "TensorToMatcher"
},
Expand All @@ -2829,7 +2866,14 @@
"paddle_api": "paddle.Tensor.to_dense"
},
"torch.Tensor.to_mkldnn": {},
"torch.Tensor.to_sparse": {},
"torch.Tensor.to_sparse": {
"Matcher": "GenericMatcher",
"min_input_args": 0,
"paddle_api": "paddle.Tensor.to_sparse_coo",
"args_list": [
"sparse_dim"
]
},
"torch.Tensor.tolist": {
"Matcher": "UnchangeMatcher"
},
Expand Down Expand Up @@ -7190,6 +7234,27 @@
"dim"
]
},
"torch.nanquantile": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.nanquantile",
"min_input_args": 2,
"args_list": [
"input",
"q",
"dim",
"keepdim",
"*",
"interpolation",
"out"
],
"kwargs_change": {
"input": "x",
"dim": "axis"
},
"unsupport_args": [
"interpolation "
]
},
"torch.nansum": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.nansum",
Expand Down Expand Up @@ -10988,6 +11053,7 @@
"torch.quantile": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.quantile",
"min_input_args": 2,
"args_list": [
"input",
"q",
Expand Down Expand Up @@ -12011,6 +12077,25 @@
"correction": "unbiased"
}
},
"torch.stft": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.signal.stft",
"args_list": [
"input",
"n_fft",
"hop_length",
"win_length",
"window",
"center",
"normalized",
"onesided",
"length",
"return_complex"
],
"kwargs_change": {
"input": "x"
}
},
"torch.subtract": {
"Matcher": "Num2TensorBinaryWithAlphaMatcher",
"paddle_api": "paddle.subtract",
Expand Down
35 changes: 35 additions & 0 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,41 @@ def get_paddle_nodes(self, args, kwargs):
return ast.parse(code).body


class TensorTileMatcher(BaseMatcher):
def get_paddle_class_nodes(self, func, args, kwargs):
self.parse_func(func)

if len(args) == 1 and isinstance(args[0], (ast.List, ast.Tuple)):
repeat_times_list = self.parse_args(args)[0]
else: # len(args) >= 1
repeat_times_list = self.parse_args(args)

kwargs = self.parse_kwargs(kwargs)
if kwargs is None:
return None

if "reps" in kwargs:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一个支持 可变参数的写法,例如:

reps=2, 3, 4
x.tile(*reps)

infoflow 2023-09-26 11-54-06

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里我目前测试,可变参数的写法应该是已经支持的,参考的TensorPermuteMatcher
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里我目前测试,可变参数的写法应该是已经支持的,参考的TensorPermuteMatcher image

参考最新的TensorPermuteMatcher,已经重构到更好写法了。
另外本地测试不具有说明性,必须要在单测中添加相应测试,test_Tensor_tile目前的测试是很不全的

kwargs = {"repeat_times": kwargs.pop("reps"), **kwargs}
else:
kwargs = {"repeat_times": str(repeat_times_list).replace("'", ""), **kwargs}

code = "{}.tile({})".format(self.paddleClass, self.kwargs_to_str(kwargs))
return ast.parse(code).body


class StftMatcher(BaseMatcher):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这两个合并到一块吧,像这样加个判断:

infoflow 2023-09-26 11-57-44

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已添加

def generate_code(self, kwargs):
if "input" not in kwargs:
kwargs["x"] = self.paddleClass
if ("return_complex" in kwargs) and (kwargs.pop("return_complex") == "(False)"):
code = "paddle.as_real(paddle.signal.stft({}))".format(
self.kwargs_to_str(kwargs)
)
else:
code = "paddle.signal.stft({})".format(self.kwargs_to_str(kwargs))
return code


class DeviceMatcher(BaseMatcher):
def generate_code(self, kwargs):
if len(kwargs) == 1:
Expand Down
69 changes: 69 additions & 0 deletions tests/test_Tensor_quantile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.Tensor.quantile")


def test_case_1():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytorch_code = textwrap.dedent(
"""
import torch
result = torch.tensor([[ 0.0795, -1.2117, 0.9765], [ 1.1707, 0.6706, 0.4884]],dtype=torch.float64).quantile(0.6)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.tensor([[ 0.0795, -1.2117, 0.9765], [ 1.1707, 0.6706, 0.4884]],dtype=torch.float64).quantile(0.6, dim=None)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.tensor([[ 0.0795, -1.2117, 0.9765], [ 1.1707, 0.6706, 0.4884]],dtype=torch.float64).quantile(0.6, dim=0, keepdim=False)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_4():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.tensor([[ 0.0795, -1.2117, 0.9765], [ 1.1707, 0.6706, 0.4884]],dtype=torch.float64).quantile(0.6, dim=1, keepdim=False)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_5():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.tensor([[ 0.0795, -1.2117, 0.9765], [ 1.1707, 0.6706, 0.4884]],dtype=torch.float64).quantile(0.6, dim=1, keepdim=True)
"""
)
obj.run(pytorch_code, ["result"])
69 changes: 69 additions & 0 deletions tests/test_Tensor_stft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 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.Tensor.stft")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[ (5.975718021392822+0j) ,
(5.975718021392822+0j) ,
(5.341437339782715+0j) ,
(5.404394626617432+0j) ,
(5.404394626617432+0j) ],
[ (0.0629572868347168+0j) ,
0.0629572868347168j ,
(-0.0629572868347168-0.6342806816101074j),
(0.6342806816101074+0j) ,
0.6342806816101074j ],
[(-0.4979677200317383+0j) ,
(0.4979677200317383+0j) ,
(0.13631296157836914+0j) ,
(-0.19927024841308594+0j) ,
(0.19927024841308594+0j) ]])
result = x.stft(n_fft=4, onesided=False, return_complex=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

"""
)
obj.run(pytorch_code, ["result"], rtol=1e-1, atol=1e-04)


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[ (5.975718021392822+0j) ,
(5.975718021392822+0j) ,
(5.341437339782715+0j) ,
(5.404394626617432+0j) ,
(5.404394626617432+0j) ],
[ (0.0629572868347168+0j) ,
0.0629572868347168j ,
(-0.0629572868347168-0.6342806816101074j),
(0.6342806816101074+0j) ,
0.6342806816101074j ],
[(-0.4979677200317383+0j) ,
(0.4979677200317383+0j) ,
(0.13631296157836914+0j) ,
(-0.19927024841308594+0j) ,
(0.19927024841308594+0j) ]])
result = x.stft(n_fft=4, onesided=False, return_complex=False)
"""
)
obj.run(pytorch_code, ["result"], rtol=1e-1, atol=1e-04)
16 changes: 14 additions & 2 deletions tests/test_Tensor_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ def test_case_1():
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="Paddle not support this api convert now",
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参考最新的test_Tensor_permute测试,包含十几种用例



def test_case_2():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,测试内容不全

参考test_Tensor_reshape的测试

pytorch_code = textwrap.dedent(
"""
import torch
a = torch.Tensor([[1.,2.], [3.,4.]])
result = a.tile(1, 2)
"""
)
obj.run(
pytorch_code,
["result"],
)
49 changes: 49 additions & 0 deletions tests/test_Tensor_to_sparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.Tensor.to_sparse")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.Tensor([[1.,2.], [3.,4.]])
b = a.to_sparse(1)
result = b.to_dense()
"""
)
obj.run(
pytorch_code,
["result"],
)


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.Tensor([[1.,2.], [3.,4.]])
b = a.to_sparse(sparse_dim = 1)
result = b.to_dense()
"""
)
obj.run(
pytorch_code,
["result"],
)
Loading