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

修改转换规则 Nanquantile. #327

Closed
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
2 changes: 1 addition & 1 deletion paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,7 @@
]
},
"torch.Tensor.nanquantile": {
"Matcher": "GenericMatcher",
"Matcher": "NanquantileMatcher",
"paddle_api": "paddle.Tensor.nanquantile",
"min_input_args": 1,
"args_list": [
Expand Down
17 changes: 17 additions & 0 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4078,3 +4078,20 @@ def generate_code(self, kwargs):
return ast.parse(
"paddle.utils.cpp_extension.setup({})".format(self.kwargs_to_str(kwargs))
)


class NanquantileMatcher(BaseMatcher):
def generate_code(self, kwargs):
new_kwargs = {}
kwargs_change = self.api_mapping["kwargs_change"]
for k in list(kwargs.keys()):
if k in kwargs_change:
new_kwargs[kwargs_change[k]] = kwargs[k]
else:
new_kwargs[k] = kwargs[k]
if "q" in k:
if "tensor" in kwargs[k] and "[" in kwargs[k]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个目前没有很好的方式来转写,因为如果传入的q是一个变量,则无法知道是torch.tensor还是list类型

Copy link
Contributor Author

Choose a reason for hiding this comment

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

是因为转换成了字符串处理吗?我是参考了这个 Matcher 实现的,请问这个变量是元组的话能否转写呢?那这个函数就只修改文档转写然后这里先搁置吗?

new_kwargs[k] = "{}.tolist()".format(kwargs[k])

code = "{}({})".format(self.get_paddle_api(), self.kwargs_to_str(new_kwargs))
return code
11 changes: 11 additions & 0 deletions tests/test_Tensor_nanquantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,14 @@ def test_case_7():
unsupport=True,
reason="Paddle not support this parameter",
)


def test_multi_q():
pytorch_code = textwrap.dedent(
"""
import torch
x = torch.tensor([[float('nan'), 1.02, 2.21, 3.333,30, float('nan')]], dtype=torch.float64)
result = x.nanquantile(q=torch.tensor([0.3, 0.4], dtype=torch.float64), dim=1, keepdim=True)
"""
)
obj.run(pytorch_code, ["result"])