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

Print the forward's stack when backward op has nan/inf and FLAGS_check_nan_inf_level = 0 #52639

Merged
merged 9 commits into from
Apr 18, 2023

Conversation

AnnaTrainingG
Copy link
Contributor

@AnnaTrainingG AnnaTrainingG commented Apr 7, 2023

PR types

Others

PR changes

Others

Describe

新增nan_inf检查工具反向op调用栈打印的支持,效果如下:
image

@paddle-bot
Copy link

paddle-bot bot commented Apr 7, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Copy link
Contributor

@Xreki Xreki left a comment

Choose a reason for hiding this comment

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

需要添加单测

egr::Controller::Instance().GetOpPythonStackStr() + forward_trace;
try {
PADDLE_ENFORCE(
false,
Copy link
Contributor

Choose a reason for hiding this comment

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

这个PADDLE_ENFORCE有什么用呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

之前是通过catch的形式获取当前函数的名称和行数现在不需要了 我删除一下

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

// Check NaN and Inf if needed
if (FLAGS_check_nan_inf) {
egr::CheckTensorHasNanOrInf("conv2d", api_result);
std::string filename = __FILE__;
Copy link
Contributor

Choose a reason for hiding this comment

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

每个manual实现的API都需要添加,重复代码太多了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

@@ -74,6 +74,12 @@ class Controller {

void EnableLayoutAutoTune() { tracer_->EnableLayoutAutoTune(); }

void SetOpPythonStackStr(std::string stack_str) {
Copy link
Contributor

Choose a reason for hiding this comment

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

SetOpPythonStackStr -> SetPythonStack

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

std::string filename = __FILE__;
std::string line= std::to_string(__LINE__);
std::string function_name = __FUNCTION__;
pybind11::gil_scoped_acquire gil;
Copy link
Contributor

Choose a reason for hiding this comment

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

获取python代码调用栈的pybind实现,还是建议放在pybind正式代码里面,尽量不要全部都用代码生成的方式。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

backward_trace_ = backward_trace;
}

std::string GetForwardTrace() { return backward_trace_; }
Copy link
Contributor

Choose a reason for hiding this comment

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

backward_trace_ -> forward_trace_

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

@AnnaTrainingG AnnaTrainingG changed the title trace back Print the forward's stack when backward op has nan/inf and FLAGS_check_nan_inf_level = 0 Apr 12, 2023
Copy link
Contributor

@zhangting2020 zhangting2020 left a comment

Choose a reason for hiding this comment

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

LGTM

CHECK_NAN_AND_INF_TEMPLATE_BACKWARD = """
if (FLAGS_check_nan_inf) {{
try{{
egr::CheckTensorHasNanOrInf("{}", {});
Copy link
Contributor

Choose a reason for hiding this comment

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

forward_trace可以直接传入CheckTensorHasNanOrInf,在CheckTensorHasNanOrInf里面打印?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

那样就需要修改CheckTensorHasNanOrInf了,代码改的比较多了

Copy link
Contributor

@Xreki Xreki Apr 18, 2023

Choose a reason for hiding this comment

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

CheckTensorHasNanOrInf是合理的啊,因为打印调用栈本身就是CheckTensorHasNanOrInf功能的一部分,后面静态图也能服用部分逻辑,可以后续PR再考虑。

std::cout<<forward_trace<<std::endl;
std::rethrow_exception(std::current_exception());
}}
}}
Copy link
Contributor

Choose a reason for hiding this comment

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

注意字符串模板中的代码格式,另外为啥要用两个花括号?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这是自动代码生成需要用两层花括号

@@ -121,7 +121,10 @@ def FindParsingFunctionFromAttributeType(atype):
NOAMP_DYGRAPH_FUNCTION_TEMPLATE = "decltype({}({})) out = {}({});"


FUNCTION_SET_DEVICE_TEMPLATE = """{} if (paddle::platform::is_gpu_place(place)) {{
FUNCTION_SET_DEVICE_TEMPLATE = """{}
LOG(INFO)<<"this is SetPythonStack";
Copy link
Contributor

Choose a reason for hiding this comment

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

这个模板是用于哪里的?LOG(INFO)会一直输出吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

是的 这个我下个PR再修改

FUNCTION_SET_DEVICE_TEMPLATE = """{} if (paddle::platform::is_gpu_place(place)) {{
FUNCTION_SET_DEVICE_TEMPLATE = """{}
LOG(INFO)<<"this is SetPythonStack";
SetPythonStack();
Copy link
Contributor

Choose a reason for hiding this comment

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

这里需要加if (FLAGS_check_nan_inf)判断来限制一下吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

不需要,这个调用里面我加了判断

@@ -141,6 +148,25 @@ def _check_num_nan_inf(use_cuda):
if paddle.fluid.core.is_compiled_with_cuda():
_check_num_nan_inf(use_cuda=True)

def test_check_stack(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

这个测试其实没有必要再费劲加到这个单测里面了。之所以构造这么一个复杂的测试方式,是因为之前CUDA抛出的异常捕获不到。调用栈报错,应该可以直接用try ... except捕获到。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

不行呀,前向的调用栈是print出来的,无法try... except捕获

Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

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

2comments

@@ -1047,11 +1069,15 @@ def GenerateNodeCreationCodes(self, for_backward=False):

node_event_name = forward_api_name + " node_creation"
node_creation_event_str = f"{indent}paddle::platform::RecordEvent node_creation_record_event(\"{node_event_name}\", paddle::platform::TracerEventType::OperatorInner, 1);\n"
set_forward_trace = (
Copy link
Contributor

Choose a reason for hiding this comment

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

能否设置FLAGS_check_nan_inf为false时不设置?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

可以的,后续和上面的临时变量问题统一修改

@@ -484,7 +488,25 @@ class {} : public egr::GradNodeBase {{
}}
}}"""

CHECK_NAN_AND_INF_TEMPLATE = """ if (FLAGS_check_nan_inf) {{ egr::CheckTensorHasNanOrInf("{}", {}); }}
CHECK_NAN_AND_INF_TEMPLATE_FORWARD = """
std::string forward_trace ="";
Copy link
Contributor

Choose a reason for hiding this comment

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

是否可以不使用临时变量减少下平凡的构造和析构开销

Copy link
Contributor Author

Choose a reason for hiding this comment

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

可以的 我下个PR统一修改

Copy link
Contributor

@JiabinYang JiabinYang left a comment

Choose a reason for hiding this comment

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

LGTM, fix problem in next pr

@AnnaTrainingG AnnaTrainingG merged commit 660f781 into PaddlePaddle:develop Apr 18, 2023
@Xreki Xreki mentioned this pull request Apr 19, 2023
jjyaoao pushed a commit to jjyaoao/Paddle that referenced this pull request Apr 19, 2023
AnnaTrainingG added a commit to AnnaTrainingG/Paddle that referenced this pull request Apr 23, 2023
lijialin03 pushed a commit to lijialin03/Paddle that referenced this pull request Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants