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

【Hackathon 7th No.38】为 Paddle 代码转换工具新增 API 转换规则(第5组) #486

Closed
wants to merge 1 commit into from
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
409 changes: 205 additions & 204 deletions paconvert/api_alias_mapping.json

Large diffs are not rendered by default.

33,107 changes: 16,631 additions & 16,476 deletions paconvert/api_mapping.json

Large diffs are not rendered by default.

257 changes: 255 additions & 2 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,14 @@ def generate_code(self, kwargs):
return GenericMatcher.generate_code(self, kwargs)


class ScatterReduceMatcher(BaseMatcher):
def generate_code(self, kwargs):
reduce_mapping = {'"""sum"""': '"add"', '"""prod"""': '"multiply"'}
if "reduce" in kwargs and kwargs["reduce"] in reduce_mapping:
kwargs["reduce"] = reduce_mapping[kwargs["reduce"]]
return GenericMatcher.generate_code(self, kwargs)


class SparseSoftmaxMatcher(BaseMatcher):
def generate_code(self, kwargs):
code = ""
Expand Down Expand Up @@ -3138,6 +3146,16 @@ def generate_code(self, kwargs):
return code


class CartesianProdMatcher(BaseMatcher):
def get_paddle_nodes(self, args, kwargs):
new_args = self.parse_args(args)
code = "paddle.cartesian_prod([ {}".format(new_args[0])
for arg in new_args[1:]:
code = code + ", {}".format(arg)
code = code + "])"
return ast.parse(code).body


class Chain_MatmulMatcher(BaseMatcher):
def get_paddle_nodes(self, args, kwargs):
if len(args) == 1 and isinstance(args[0], ast.Starred):
Expand Down Expand Up @@ -4274,11 +4292,246 @@ def generate_code(self, kwargs):
return "paddle_aux._CONVERT_SYMEIG({})".format(self.kwargs_to_str(kwargs))


class CanCastMatcher(BaseMatcher):
def generate_aux_code(self):
CODE_TEMPLATE = textwrap.dedent(
"""
def can_cast(from_, to):
can_cast_dict = {
paddle.bfloat16: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: False,
paddle.int8: False,
paddle.int16: False,
paddle.int32: False,
paddle.int64: False,
paddle.bool: False
},
paddle.float16: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: False,
paddle.int8: False,
paddle.int16: False,
paddle.int32: False,
paddle.int64: False,
paddle.bool: False,
},
paddle.float32: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: False,
paddle.int8: False,
paddle.int16: False,
paddle.int32: False,
paddle.int64: False,
paddle.bool: False,
},
paddle.float64: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: False,
paddle.int8: False,
paddle.int16: False,
paddle.int32: False,
paddle.int64: False,
paddle.bool: False,
},
paddle.complex64: {
paddle.bfloat16: False,
paddle.float16: False,
paddle.float32: False,
paddle.float64: False,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: False,
paddle.int8: False,
paddle.int16: False,
paddle.int32: False,
paddle.int64: False,
paddle.bool: False,
},
paddle.complex128: {
paddle.bfloat16: False,
paddle.float16: False,
paddle.float32: False,
paddle.float64: False,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: False,
paddle.int8: False,
paddle.int16: False,
paddle.int32: False,
paddle.int64: False,
paddle.bool: False,
},
paddle.uint8: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: True,
paddle.int8: True,
paddle.int16: True,
paddle.int32: True,
paddle.int64: True,
paddle.bool: False,
},
paddle.int8: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: True,
paddle.int8: True,
paddle.int16: True,
paddle.int32: True,
paddle.int64: True,
paddle.bool: False,
},
paddle.int16: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: True,
paddle.int8: True,
paddle.int16: True,
paddle.int32: True,
paddle.int64: True,
paddle.bool: False,
},
paddle.int32: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: True,
paddle.int8: True,
paddle.int16: True,
paddle.int32: True,
paddle.int64: True,
paddle.bool: False,
},
paddle.int64: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: True,
paddle.int8: True,
paddle.int16: True,
paddle.int32: True,
paddle.int64: True,
paddle.bool: False,
},
paddle.bool: {
paddle.bfloat16: True,
paddle.float16: True,
paddle.float32: True,
paddle.float64: True,
paddle.complex64: True,
paddle.complex128: True,
paddle.uint8: True,
paddle.int8: True,
paddle.int16: True,
paddle.int32: True,
paddle.int64: True,
paddle.bool: True,
}
}
return can_cast_dict[from_][to]
setattr(paddle, 'can_cast', can_cast)
"""
)
return CODE_TEMPLATE

def generate_code(self, kwargs):
self.write_aux_code()
_from_dtype = kwargs["from_"][3:-3]
_to_dtype = kwargs["to"][3:-3]
code = "paddle_aux.can_cast(paddle.{}, paddle.{})".format(
_from_dtype, _to_dtype
)
return code


class FloatPowerMatcher(BaseMatcher):
def generate_code(self, kwargs):
return "{}.cast(paddle.float64).pow({})".format(
self.paddleClass, kwargs["exponent"]
if "input" not in kwargs:
return "{}.cast(paddle.float64).pow({}.cast(paddle.float64) if isinstance({}, paddle.Tensor) else {})".format(
self.paddleClass,
kwargs["exponent"],
kwargs["exponent"],
kwargs["exponent"],
)
else:
if "out" not in kwargs:
return "paddle.pow({}.cast(paddle.float64), {}.cast(paddle.float64) if isinstance({}, paddle.Tensor) else {})".format(
kwargs["input"],
kwargs["exponent"],
kwargs["exponent"],
kwargs["exponent"],
)
else:
return "paddle.assign(paddle.pow({}.cast(paddle.float64), {}.cast(paddle.float64) if isinstance({}, paddle.Tensor) else {}), {})".format(
kwargs["input"],
kwargs["exponent"],
kwargs["exponent"],
kwargs["exponent"],
kwargs["out"],
)


class PositiveMatcher(BaseMatcher):
def generate_code(self, kwargs):
API_TEMPLATE = textwrap.dedent(
"""
def positive({}):
if {}.dtype != paddle.bool:
return {}
else:
raise RuntimeError("boolean tensors is not supported.")

positive({})
"""
)
if "input" not in kwargs:
code = API_TEMPLATE.format(
self.paddleClass, self.paddleClass, self.paddleClass, self.paddleClass
)
else:
code = API_TEMPLATE.format(
kwargs["input"], kwargs["input"], kwargs["input"], kwargs["input"]
)
return code


class FloatPowerInplaceMatcher(BaseMatcher):
Expand Down
51 changes: 51 additions & 0 deletions tests/test_Tensor_isneginf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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.isneginf")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]).isneginf()
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
input = torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')])
result = input.isneginf()
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
input = torch.tensor([1, 6.9, 2])
result = input.isneginf()
"""
)
obj.run(pytorch_code, ["result"])
51 changes: 51 additions & 0 deletions tests/test_Tensor_isposinf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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.isposinf")


def test_case_1():
pytorch_code = textwrap.dedent(
"""
import torch
result = torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]).isposinf()
"""
)
obj.run(pytorch_code, ["result"])


def test_case_2():
pytorch_code = textwrap.dedent(
"""
import torch
input = torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')])
result = input.isposinf()
"""
)
obj.run(pytorch_code, ["result"])


def test_case_3():
pytorch_code = textwrap.dedent(
"""
import torch
input = torch.tensor([1, 6.9, 2])
result = input.isposinf()
"""
)
obj.run(pytorch_code, ["result"])
Loading