Skip to content

Commit 01f1746

Browse files
author
0x45f
committed
fix review
1 parent 079112a commit 01f1746

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

python/paddle/fluid/dygraph/dygraph_to_static/error.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ def formated_message(self):
143143
return msg + '\n'.join(self.source_code) + '\n'
144144

145145

146+
class SuggestionDict(object):
147+
def __init__(self):
148+
# {(keywords): (suggestions)}
149+
self.suggestion_dict = {
150+
('is not initialized.', 'Hint:', 'IsInitialized'):
151+
("Please ensure all your sublayers are inheritted from nn.Layer.",
152+
"Please ensure there is no tensor created explicitly depended on external data, we suggest to register it as buffer tensor. See https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/04_dygraph_to_static/export_model/principle_cn.html#parameters-buffers for details"
153+
)
154+
}
155+
156+
def keys(self):
157+
return self.suggestion_dict.keys()
158+
159+
def __getitem__(self, key):
160+
return self.suggestion_dict[key]
161+
162+
146163
class ErrorData(object):
147164
"""
148165
Error data attached to an exception which is raised in un-transformed code.
@@ -155,6 +172,7 @@ def __init__(self, error_type, error_value, origin_traceback,
155172
self.origin_traceback = origin_traceback
156173
self.origin_info_map = origin_info_map
157174
self.in_runtime = False
175+
self.suggestion_dict = SuggestionDict()
158176

159177
def create_exception(self):
160178
message = self.create_message()
@@ -216,27 +234,20 @@ def create_message(self):
216234
return '\n'.join(message_lines)
217235

218236
def _create_revise_suggestion(self, bottom_error_message):
219-
# {(keywords): [suggestions]}
220-
suggestion_dict = {
221-
('is not initialized.', 'Hint:', 'IsInitialized'): [
222-
"Ensure all sublayers inherit nn.Layer.",
223-
"Ensure layer's inputs should be buffer Variables. For more information, please refer to: https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/04_dygraph_to_static/export_model/principle_cn.html#parameters-buffers"
224-
]
225-
}
226237
revise_suggestions = [
227-
' ' * BLANK_COUNT_BEFORE_FILE_STR + 'Revise suggestion: '
238+
'', ' ' * BLANK_COUNT_BEFORE_FILE_STR + 'Revise suggestion: '
228239
]
229-
for keywords in suggestion_dict.keys():
240+
for keywords in self.suggestion_dict.keys():
230241
contain_keywords = [
231242
True for i in keywords if i in ''.join(bottom_error_message)
232243
]
233244
if len(contain_keywords) == len(
234245
keywords): # all keywords should be in bottom_error_message
235-
for suggestion in suggestion_dict[keywords]:
246+
for suggestion in self.suggestion_dict[keywords]:
236247
suggestion_msg = ' ' * BLANK_COUNT_BEFORE_FILE_STR * 2 + '{}. {}'.format(
237-
str(len(revise_suggestions)), suggestion)
248+
str(len(revise_suggestions) - 1), suggestion)
238249
revise_suggestions.append(suggestion_msg)
239-
return revise_suggestions if len(revise_suggestions) > 1 else []
250+
return revise_suggestions if len(revise_suggestions) > 2 else []
240251

241252
def _simplify_error_value(self):
242253
"""

python/paddle/fluid/dygraph/dygraph_to_static/partial_program.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,10 @@ def _check_params_all_inited(self, main_program):
476476
raise ValueError(
477477
"\n\tWe don't support to define layer with parameters in the function decorated by `@to_static`."
478478
"\n\tBut we found parameter(%s) was created in the decorated function."
479+
"\n"
479480
"\n\tRevise suggestion: "
480-
"\n\t\t1. Ensure all sublayers inherit nn.Layer"
481-
"\n\t\t2. Should use nn.ParameterList and nn.LayerList as container instead of using a native Python container such as List"
481+
"\n\t\t1. Please ensure all your sublayers are inheritted from nn.Layer."
482+
"\n\t\t2. Please use nn.ParameterList and nn.LayerList as container instead of using a native Python container such as List"
482483
% name)
483484

484485
def _valid_vars(self, vars):

python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ def set_message(self):
431431
'<--- HERE',
432432
'return out',
433433
'Revise suggestion:',
434-
'Ensure all sublayers inherit nn.Layer.',
435-
'Ensure layer\'s inputs should be buffer Variables. For more information, please refer to:'
434+
'Please ensure all your sublayers are inheritted from nn.Layer.',
435+
'Please ensure there is no tensor created explicitly depended on external data, we suggest to register it as buffer tensor. See'
436436
]
437437

438438
def set_func_call(self):

0 commit comments

Comments
 (0)