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

[Dy2St]Fix abnormal format of message when raise KeyError in Dy2St #44996

Merged
merged 5 commits into from
Aug 9, 2022
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
9 changes: 8 additions & 1 deletion python/paddle/fluid/dygraph/dygraph_to_static/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ def __getitem__(self, key):
return self.suggestion_dict[key]


class Dy2StKeyError(Exception):
pass


class ErrorData(object):
"""
Error data attached to an exception which is raised in un-transformed code.
Expand All @@ -159,7 +163,10 @@ def __init__(self, error_type, error_value, origin_traceback,

def create_exception(self):
message = self.create_message()
new_exception = self.error_type(message)
if self.error_type is KeyError:
new_exception = Dy2StKeyError(message)
else:
new_exception = self.error_type(message)
setattr(new_exception, ERROR_DATA, self)
return new_exception

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,20 @@ def test_error(self):
for disable_new_error in [0, 1]:
self._test_raise_new_exception(disable_new_error)

@paddle.jit.to_static
def func_ker_error(x):
d = {
'x': x
}
y = d['y'] + x
return y

class TestKeyError(unittest.TestCase):
def test_key_error(self):
paddle.disable_static()
with self.assertRaises(error.Dy2StKeyError):
x = paddle.to_tensor([1])
func_ker_error(x)

if __name__ == '__main__':
unittest.main()