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

Fix lu_factor_ex #268

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
2 changes: 1 addition & 1 deletion paconvert/api_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -5816,7 +5816,7 @@
}
},
"torch.linalg.lu_factor_ex": {
"Matcher": "TripleAssignMatcher",
"Matcher": "LinalgLufactorexMatcher",
"paddle_api": "paddle.linalg.lu",
"args_list": [
"A",
Expand Down
41 changes: 41 additions & 0 deletions paconvert/api_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3492,6 +3492,47 @@ def generate_code(self, kwargs):
return code.strip("\n")


class LinalgLufactorexMatcher(BaseMatcher):
def generate_code(self, kwargs):
kwargs = self.set_paddle_default_kwargs(kwargs)
kwargs_change = {}
if "kwargs_change" in self.api_mapping:
kwargs_change = self.api_mapping["kwargs_change"]

for k in kwargs_change:
if k in kwargs:
if kwargs_change[k]:
kwargs[kwargs_change[k]] = kwargs.pop(k)
else:
kwargs.pop(k)

if "out" in kwargs:
out_v = kwargs.pop("out")
API_TEMPLATE = textwrap.dedent(
"""
out1, out2, out3 = {}({})
out3 = paddle.to_tensor(out3.item(), dtype='int32')
paddle.assign(out1, {}[0]), paddle.assign(out2, {}[1]), paddle.assign(out3, {}[2])
"""
)
code = API_TEMPLATE.format(
self.get_paddle_api(), self.kwargs_to_str(kwargs), out_v, out_v, out_v
)
return code.strip("\n")
else:
API_TEMPLATE = textwrap.dedent(
"""
out1, out2, out3 = {}({})
out3 = paddle.to_tensor(out3.item(), dtype='int32')
Copy link
Collaborator

@zhwesky2010 zhwesky2010 Sep 5, 2023

Choose a reason for hiding this comment

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

这个功能 只是做一个cast为int32的类型变化吗

Copy link
Contributor Author

Choose a reason for hiding this comment

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

图片
shape会转换

(out1, out2, out3)
"""
)
code = API_TEMPLATE.format(
self.get_paddle_api(), self.kwargs_to_str(kwargs)
)
return code.strip("\n")


class RoundMatcher(BaseMatcher):
def generate_code(self, kwargs):
if "input" not in kwargs:
Expand Down
5 changes: 0 additions & 5 deletions tests/test_linalg_lu_factor_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_case_1():
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
LU, pivots, info = torch.linalg.lu_factor_ex(x)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])
Expand All @@ -37,7 +36,6 @@ def test_case_2():
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
LU, pivots, info = torch.linalg.lu_factor_ex(A=x)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个只有一个shape的差异吗,先设置 check_shape=False 来测试吧,这个问题我们会在框架上修复,shape=[]的0D更合理

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

@zhwesky2010 zhwesky2010 Sep 6, 2023

Choose a reason for hiding this comment

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

好吧,那暂时就先这样转吧,文档中说明下就可以,等修复0D这个问题后再删掉 info.item() 这行就可以

Expand All @@ -49,7 +47,6 @@ def test_case_3():
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
LU, pivots, info = torch.linalg.lu_factor_ex(pivot=True, A=x)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])
Expand All @@ -62,7 +59,6 @@ def test_case_4():
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
out = (torch.tensor([], dtype=torch.float64), torch.tensor([], dtype=torch.int), torch.tensor([], dtype=torch.int))
LU, pivots, info = torch.linalg.lu_factor_ex(x, pivot=True, check_errors=False, out=out)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])
Expand All @@ -75,7 +71,6 @@ def test_case_5():
x = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=torch.float64)
out = (torch.tensor([], dtype=torch.float64), torch.tensor([], dtype=torch.int), torch.tensor([], dtype=torch.int))
LU, pivots, info = torch.linalg.lu_factor_ex(A=x, pivot=True, check_errors=True, out=out)
info = info.item()
"""
)
obj.run(pytorch_code, ["LU", "pivots", "info"])