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.42】NO.42 为 Paddle 代码转换工具新增 API 转换规则(第 9 组) #490

Merged
merged 17 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
106 changes: 100 additions & 6 deletions paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,28 @@
"other": "y"
}
},
"torch.Tensor.bitwise_left_shift": {},
"torch.Tensor.bitwise_left_shift_": {},
"torch.Tensor.bitwise_left_shift": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.bitwise_left_shift",
"min_input_args": 1,
"args_list": [
"other"
],
"kwargs_change": {
"other": "y"
}
},
"torch.Tensor.bitwise_left_shift_": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.bitwise_left_shift_",
"min_input_args": 1,
"args_list": [
"other"
],
"kwargs_change": {
"other": "y"
}
},
"torch.Tensor.bitwise_not": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.bitwise_not",
Expand Down Expand Up @@ -780,8 +800,28 @@
"other": "y"
}
},
"torch.Tensor.bitwise_right_shift": {},
"torch.Tensor.bitwise_right_shift_": {},
"torch.Tensor.bitwise_right_shift": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.bitwise_right_shift",
"min_input_args": 1,
"args_list": [
"other"
],
"kwargs_change": {
"other": "y"
}
},
"torch.Tensor.bitwise_right_shift_": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.bitwise_right_shift_",
"min_input_args": 1,
"args_list": [
"other"
],
"kwargs_change": {
"other": "y"
}
},
"torch.Tensor.bitwise_xor": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.bitwise_xor",
Expand Down Expand Up @@ -970,6 +1010,10 @@
"paddle_api": "paddle.Tensor.coalesce",
"min_input_args": 0
},
"torch.Tensor.col_indices": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.cols"
},
"torch.Tensor.conj": {
"Matcher": "UnchangeMatcher",
"min_input_args": 0
Expand Down Expand Up @@ -1096,6 +1140,10 @@
"dim": "axis"
}
},
"torch.Tensor.crow_indices": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.crows"
},
"torch.Tensor.cuda": {
"Matcher": "TensorCudaMatcher",
"paddle_api": "paddle.Tensor.cuda",
Expand Down Expand Up @@ -1182,7 +1230,10 @@
},
"min_input_args": 1
},
"torch.Tensor.data_ptr": {},
"torch.Tensor.data_ptr": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.data_ptr"
},
"torch.Tensor.deg2rad": {
"Matcher": "UnchangeMatcher",
"min_input_args": 0
Expand Down Expand Up @@ -1291,6 +1342,9 @@
"paddle_api": "paddle.Tensor.dim",
"min_input_args": 0
},
"torch.Tensor.dim_order": {
"Matcher": "DimOrderMatcher"
},
"torch.Tensor.dist": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.Tensor.dist",
Expand Down Expand Up @@ -3609,7 +3663,14 @@
"torch.Tensor.storage_type": {
"min_input_args": 0
},
"torch.Tensor.stride": {},
"torch.Tensor.stride": {
"Matcher": "TensorStrideMatcher",
"paddle_api": "paddle.Tensor.get_strides",
"min_input_args": 0,
"args_list": [
"dim"
]
},
"torch.Tensor.subtract": {
"Matcher": "TensorSubtractMatcher",
"paddle_api": "paddle.Tensor.subtract",
Expand Down Expand Up @@ -3798,6 +3859,9 @@
"sparse_dim"
]
},
"torch.Tensor.to_sparse_coo": {
"Matcher": "TensorToSparseCooMatcher"
},
"torch.Tensor.tolist": {
"Matcher": "UnchangeMatcher",
"min_input_args": 0
Expand Down Expand Up @@ -4953,6 +5017,21 @@
"other": "y"
}
},
"torch.bitwise_left_shift": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.bitwise_left_shift",
"min_input_args": 2,
"args_list": [
"input",
"other",
"*",
"out"
],
"kwargs_change": {
"input": "x",
"other": "y"
}
},
"torch.bitwise_not": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.bitwise_not",
Expand Down Expand Up @@ -4981,6 +5060,21 @@
"other": "y"
}
},
"torch.bitwise_right_shift": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.bitwise_right_shift",
"min_input_args": 2,
"args_list": [
"input",
"other",
"*",
"out"
],
"kwargs_change": {
"input": "x",
"other": "y"
}
},
"torch.bitwise_xor": {
"Matcher": "GenericMatcher",
"paddle_api": "paddle.bitwise_xor",
Expand Down
66 changes: 66 additions & 0 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,72 @@ def generate_code(self, kwargs):
return super().generate_code(kwargs)


class IsSparseCsrMatcher(BaseMatcher):
Copy link
Collaborator

Choose a reason for hiding this comment

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

用 Attribute2Func 这个Matcher就行

def get_paddle_class_attribute_nodes(self, node):
self.parse_func(node)
code = "{}.is_sparse_csr()".format(self.paddleClass)
return ast.parse(code).body


class TensorStrideMatcher(BaseMatcher):
def get_paddle_nodes(self, args, kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个应该不用重写更底层的get_paddle_nodes,不涉及到可变参数这些,重写generate_code就行

kwargs = self.parse_kwargs(kwargs)
args = self.parse_args(args)
if "dim" in kwargs.keys() and kwargs["dim"] != "None":
code = "{}.get_strides()[{}]".format(self.paddleClass, kwargs["dim"])
elif len(args) > 0 and args[0] != "None":
code = "{}.get_strides()[{}]".format(self.paddleClass, args[0])
else:
code = "{}.get_strides()".format(self.paddleClass)

return ast.parse(code).body


class TensorToSparseCooMatcher(BaseMatcher):
def generate_code(self, kwargs):
API_TEMPLATE = textwrap.dedent(
"""
{}.to_sparse_coo(len({}.shape))
"""
)
code = API_TEMPLATE.format(self.paddleClass, self.paddleClass)
return code


class TensorNbytesMatcher(BaseMatcher):
def get_paddle_class_attribute_nodes(self, node):
self.parse_func(node)
code = "int(paddle.numel({}) * {}.element_size())".format(
Copy link
Collaborator

Choose a reason for hiding this comment

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

按文档改组合方式

self.paddleClass, self.paddleClass
)
return ast.parse(code).body


class DimOrderMatcher(BaseMatcher):
def generate_code(self, kwargs):
API_TEMPLATE = textwrap.dedent(
"""
tuple([i for i in range(len({}.shape))])
"""
)
code = API_TEMPLATE.format(self.paddleClass)
return code


class TensorItemsizeMatcher(BaseMatcher):
Copy link
Collaborator

Choose a reason for hiding this comment

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

这么一个简单的示例,无需重写Matcher吧,用GenericMatcher就行

def get_paddle_class_attribute_nodes(self, node):
self.parse_func(node)
paddle_class = self.paddleClass

API_TEMPLATE = textwrap.dedent(
"""
{}.element_size()
"""
)
code = API_TEMPLATE.format(paddle_class)
return ast.parse(code).body


class TRFMPreTrainedTokenizerMatcher(BaseMatcher):
def generate_aux_code(self):
CODE_TEMPLATE = textwrap.dedent(
Expand Down
9 changes: 9 additions & 0 deletions paconvert/attribute_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@
"Matcher": "Attribute2Func",
"paddle_api": "paddle.Tensor.is_sparse"
},
"torch.Tensor.is_sparse_csr": {
"Matcher": "IsSparseCsrMatcher"
},
"torch.Tensor.itemsize": {
"Matcher": "TensorItemsizeMatcher"
},
"torch.Tensor.mH": {
"Matcher": "TensorMhMatcher"
},
"torch.Tensor.mT": {
"Matcher": "TensorMtMatcher"
},
"torch.Tensor.names": {},
"torch.Tensor.nbytes": {
"Matcher": "TensorNbytesMatcher"
},
"torch.Tensor.ndim": {
"Matcher": "UnchangeMatcher"
},
Expand Down
21 changes: 3 additions & 18 deletions tests/test_Tensor_bitwise_left_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ def test_case_1():
result = input.bitwise_left_shift(other)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
obj.run(pytorch_code, ["result"])


def test_case_2():
Expand All @@ -46,12 +41,7 @@ def test_case_2():
result = input.bitwise_left_shift(other=other)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
obj.run(pytorch_code, ["result"])


def test_case_3():
Expand All @@ -63,9 +53,4 @@ def test_case_3():
result = input.bitwise_left_shift(other=other)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
obj.run(pytorch_code, ["result"])
21 changes: 3 additions & 18 deletions tests/test_Tensor_bitwise_left_shift_.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ def test_case_1():
result = input.bitwise_left_shift_(other)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
obj.run(pytorch_code, ["result"])


def test_case_2():
Expand All @@ -46,12 +41,7 @@ def test_case_2():
result = input.bitwise_left_shift_(other=other)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
obj.run(pytorch_code, ["result"])


def test_case_3():
Expand All @@ -63,9 +53,4 @@ def test_case_3():
result = input.bitwise_left_shift_(other=other)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
obj.run(pytorch_code, ["result"])
21 changes: 3 additions & 18 deletions tests/test_Tensor_bitwise_right_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ def test_case_1():
result = input.bitwise_right_shift(other)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
obj.run(pytorch_code, ["result"])


def test_case_2():
Expand All @@ -46,12 +41,7 @@ def test_case_2():
result = input.bitwise_right_shift(other=other)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
obj.run(pytorch_code, ["result"])


def test_case_3():
Expand All @@ -63,9 +53,4 @@ def test_case_3():
result = input.bitwise_right_shift(other=other)
"""
)
obj.run(
pytorch_code,
["result"],
unsupport=True,
reason="paddle does not support this function temporarily",
)
obj.run(pytorch_code, ["result"])
Loading