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

转换规则 No.43/97/98/100 #263

Merged
merged 2 commits into from
Sep 4, 2023
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
36 changes: 33 additions & 3 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,16 @@
]
},
"torch.Tensor.resize_": {},
"torch.Tensor.resize_as_": {},
"torch.Tensor.resize_as_": {
"Matcher": "TensorResize_as_Matcher",
"args_list": [
"the_template",
"memory_format"
],
"kwargs_change": {
"memory_format": ""
}
},
"torch.Tensor.resolve_conj": {},
"torch.Tensor.resolve_neg": {},
"torch.Tensor.retain_grad": {
Expand Down Expand Up @@ -2027,7 +2036,12 @@
"out"
]
},
"torch.Tensor.round_": {},
"torch.Tensor.round_": {
"Matcher": "TensorRound_Matcher",
"args_list": [
"decimals"
]
},
"torch.Tensor.rsqrt": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.rsqrt"
Expand Down Expand Up @@ -2066,7 +2080,23 @@
"src": "values"
}
},
"torch.Tensor.scatter_add": {},
"torch.Tensor.scatter_add": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.put_along_axis",
"args_list": [
"dim",
"index",
"src"
],
"kwargs_change": {
"dim": "axis",
"index": "indices",
"src": "values"
},
"paddle_default_kwargs": {
"reduce": "'add'"
}
},
"torch.Tensor.scatter_add_": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.put_along_axis_",
Expand Down
29 changes: 29 additions & 0 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3081,6 +3081,18 @@ def generate_code(self, kwargs):
return code


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

API_TEMPLATE = textwrap.dedent(
"""
{}.reshape_({}.shape)
"""
)
code = API_TEMPLATE.format(self.paddleClass, kwargs["the_template"])
return code


class SelectMatcher(BaseMatcher):
def generate_code(self, kwargs):
if "input" not in kwargs:
Expand Down Expand Up @@ -3688,6 +3700,23 @@ def get_paddle_class_nodes(self, func, args, kwargs):
return "unchange"


class TensorRound_Matcher(BaseMatcher):
def generate_code(self, kwargs):
kwargs["input"] = self.paddleClass

if "decimals" in kwargs:
API_TEMPLATE = textwrap.dedent(
"""
paddle.assign(({} * (10**{})).round_() / (10**{}), {})
"""
)
return API_TEMPLATE.format(
kwargs["input"], kwargs["decimals"], kwargs["decimals"], kwargs["input"]
)
else:
return "{}.round_()".format(kwargs["input"])


class NonzeroMatcher(BaseMatcher):
def generate_code(self, kwargs):
if "as_tuple" in kwargs and kwargs["as_tuple"] != "(False)":
Expand Down
79 changes: 79 additions & 0 deletions tests/test_Tensor_resize_as_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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.resize_as_")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.ones([15])
b = torch.zeros([3, 5])
result = a.resize_as_(b)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.ones([15])
b = torch.zeros([3, 5])
result = a.resize_as_(the_template=b)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.ones([15])
b = torch.zeros([3, 5])
result = a.resize_as_(memory_format=torch.contiguous_format, the_template=b)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_4():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.ones([15])
b = torch.zeros([3, 5])
result = a.resize_as_(b+1, memory_format=torch.contiguous_format)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_5():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.ones([15])
b = torch.zeros([3, 5])
result = a.resize_as_(the_template=b+1, memory_format=torch.contiguous_format)
"""
)
obj.run(pytorch_code, ["result"])
73 changes: 73 additions & 0 deletions tests/test_Tensor_round_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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.round_")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.tensor([[ 0.9254, -0.6213]])
result = a.round_()
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.tensor([[ 102003.9254, -12021.6213]])
result = a.round_(decimals=1)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.tensor([[ 102003.9254, -12021.6213]])
result = a.round_(decimals=-1)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_4():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.tensor([[ 102003.9254, -12021.6213]])
result = a.round_(decimals=3)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_5():
pytorch_code = textwrap.dedent(
"""
import torch
a = torch.tensor([[ 102003.9254, -12021.6213]])
result = a.round_(decimals=-3)
"""
)
obj.run(pytorch_code, ["result"])
80 changes: 80 additions & 0 deletions tests/test_Tensor_scatter_add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# 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.scatter_add")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
src = torch.ones((1, 5))
index = torch.tensor([[0, 1, 2, 0, 0]])
result = torch.zeros(3, 5, dtype=src.dtype).scatter_add(0, index, src)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
src = torch.ones((2, 5))
index = torch.tensor([[0, 1, 2, 0, 0], [0, 1, 2, 2, 2]])
result = torch.zeros(3, 5, dtype=src.dtype).scatter_add(dim=0, index=index, src=src)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
src = torch.ones((2, 5))
index = torch.tensor([[0, 1, 2, 0, 0], [0, 1, 2, 2, 2]])
result = torch.zeros(3, 5, dtype=src.dtype).scatter_add(dim=0, src=src, index=index)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_4():
pytorch_code = textwrap.dedent(
"""
import torch
src = torch.ones((1, 5))
index = torch.tensor([[0, 1, 2, 0, 0]])
result = torch.zeros(3, 5, dtype=src.dtype).scatter_add(0, index, src)
"""
)
obj.run(pytorch_code, ["result"])


def test_case_5():
pytorch_code = textwrap.dedent(
"""
import torch
src = torch.ones((2, 5))
index = torch.tensor([[0, 1, 2, 0, 0], [0, 1, 2, 2, 2]])
result = torch.zeros(2, 5, dtype=src.dtype).scatter_add(dim=1, index=index, src=src)
"""
)
obj.run(pytorch_code, ["result"])
Loading