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 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
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
50 changes: 50 additions & 0 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,56 @@ def generate_code(self, kwargs):
return super().generate_code(kwargs)


class TensorStrideMatcher(BaseMatcher):
def generate_code(self, kwargs):
if 'dim' not in kwargs or kwargs['dim'] == 'None':
API_TEMPLATE = textwrap.dedent(
"""
{}.get_strides()
"""
)
code = API_TEMPLATE.format(self.paddleClass)
else:
API_TEMPLATE = textwrap.dedent(
"""
{}.get_strides()[{}]
"""
)
code = API_TEMPLATE.format(self.paddleClass, kwargs['dim'])
return code


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 = "{}.size * {}.element_size()".format(
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 TRFMPreTrainedTokenizerMatcher(BaseMatcher):
def generate_aux_code(self):
CODE_TEMPLATE = textwrap.dedent(
Expand Down
11 changes: 11 additions & 0 deletions paconvert/attribute_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,24 @@
"Matcher": "Attribute2Func",
"paddle_api": "paddle.Tensor.is_sparse"
},
"torch.Tensor.is_sparse_csr": {
"Matcher": "Attribute2Func",
"paddle_api": "paddle.Tensor.is_sparse_csr"
},
"torch.Tensor.itemsize": {
"Matcher": "Attribute2Func",
"paddle_api": "paddle.Tensor.element_size"
},
"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