-
Notifications
You must be signed in to change notification settings - Fork 5.7k
math API support int tensor autocast to float32 易用性提升 #69252
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,48 @@ | |
"remainder_": ["x", "y"], | ||
} | ||
|
||
# ops support casting int tensor into float32 to do forward calculation | ||
type_autocast_op_list = { | ||
"acos": ["x"], | ||
"acosh": ["x"], | ||
"asin": ["x"], | ||
"asinh": ["x"], | ||
"atan": ["x"], | ||
"atanh": ["x"], | ||
"ceil": ["x"], | ||
"cos": ["x"], | ||
"cosh": ["x"], | ||
"digamma": ["x"], | ||
"erf": ["x"], | ||
"erfinv": ["x"], | ||
"floor": ["x"], | ||
"i0": ["x"], | ||
"i0e": ["x"], | ||
"i1": ["x"], | ||
"i1e": ["x"], | ||
"lgamma": ["x"], | ||
"logcumsumexp": ["x"], | ||
"logit": ["x"], | ||
"logsumexp": ["x"], | ||
"polygamma": ["x"], | ||
"reciprocal": ["x"], | ||
"rsqrt": ["x"], | ||
"sigmoid": ["x"], | ||
"sin": ["x"], | ||
"sinh": ["x"], | ||
"sqrt": ["x"], | ||
"stanh": ["x"], | ||
"tan": ["x"], | ||
"tanh": ["x"], | ||
} | ||
|
||
# ops support casting int tensor into float32 to do forward calculation, | ||
# and it is valid to cast float32 gradient back to int tensor. | ||
type_autocast_valid_grad_op_list = { | ||
"ceil", | ||
"floor", | ||
} | ||
|
||
# dict of special api that forward api's output will affect backward api's output | ||
# backward api's output usually affected by backward api's input | ||
|
||
|
@@ -327,6 +369,8 @@ class {} : public egr::GradNodeBase {{ | |
// AMP Logic | ||
{} | ||
// Type promotion Logic | ||
{} | ||
// Type autocast Logic | ||
{} | ||
// Layout autotune | ||
{} | ||
|
@@ -404,6 +448,8 @@ class {} : public egr::GradNodeBase {{ | |
// AMP Logic | ||
{} | ||
// Type promotion Logic | ||
{} | ||
// Type autocast Logic | ||
{} | ||
// Layout autotune | ||
{} | ||
|
@@ -618,6 +664,15 @@ class {} : public egr::GradNodeBase {{ | |
}} | ||
""" | ||
|
||
TYPE_AUTOCAST_LOGIC_TEMPLATE = """ | ||
if (phi::NeedTypeAutoCast({op_func_name}, {x}.dtype())) {{ | ||
VLOG(5) << "math operation got integer input data type, run type autocast."; | ||
LOG_FIRST_N(WARNING, 1) << "math operation got integer input data type, run type autocast, this may cause data type been changed."; | ||
{op_name} | ||
auto new_{x} = egr::PromoteCast("{x}", {x}, phi::DataType::FLOAT32, {trace_backward}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{return_value} | ||
}} | ||
""" | ||
|
||
LAYOUT_LOGIC_TEMPLATE = """ | ||
if (egr::Controller::Instance().UseLayoutAutoTune()) {{ | ||
|
@@ -1563,6 +1618,7 @@ def GenerateForwardDefinitionAndDeclaration(self, is_inplaced): | |
|
||
amp_inputs_call_list = ["" for i in range(num_inputs)] | ||
type_promote_inputs_call_list = ["" for i in range(num_inputs)] | ||
type_autocast_inputs_call_list = ["" for i in range(num_inputs)] | ||
amp_tensors_vector_list = [] | ||
amp_tensors_vector_optional_list = [] | ||
amp_autocast_list = [] | ||
|
@@ -1591,6 +1647,11 @@ def GenerateForwardDefinitionAndDeclaration(self, is_inplaced): | |
type_promote_inputs_call_list[pos] = f"new_{name}" | ||
else: | ||
type_promote_inputs_call_list[pos] = f"{name}" | ||
if forward_api_name in type_autocast_op_list: | ||
if name in type_autocast_op_list[forward_api_name]: | ||
type_autocast_inputs_call_list[pos] = f"new_{name}" | ||
else: | ||
type_autocast_inputs_call_list[pos] = f"{name}" | ||
if IsPlainTensorType(ttype): | ||
if is_optional: | ||
if ( | ||
|
@@ -1682,6 +1743,7 @@ def GenerateForwardDefinitionAndDeclaration(self, is_inplaced): | |
inputs_call_list[pos] = name | ||
amp_inputs_call_list[pos] = name | ||
type_promote_inputs_call_list[pos] = name | ||
type_autocast_inputs_call_list[pos] = name | ||
if default_val is not None: | ||
inputs_args_declaration_list[pos] = ( | ||
f"{atype} {name} = {default_val}" | ||
|
@@ -1971,6 +2033,31 @@ def GenerateForwardDefinitionAndDeclaration(self, is_inplaced): | |
) | ||
else: | ||
type_promotion_logic_str = f'\n VLOG(5) << " No Type Promotion for {forward_ad_function_name} api. "; ' | ||
|
||
# Forward type autocast logic | ||
if forward_api_name in type_autocast_op_list: | ||
# only support one inputs | ||
op_func_name = f'"{forward_api_name}"' | ||
x = type_autocast_op_list[forward_api_name][0] | ||
type_autocast_inputs_call_args_str = ", ".join( | ||
type_autocast_inputs_call_list | ||
) | ||
trace_backward = ( | ||
forward_api_name in type_autocast_valid_grad_op_list | ||
) and (not self.is_forward_only) | ||
trace_backward = str(trace_backward).lower() | ||
type_autocast_call_list = f"return {forward_ad_function_name}({type_autocast_inputs_call_args_str});" | ||
|
||
type_autocast_logic_str = TYPE_AUTOCAST_LOGIC_TEMPLATE.format( | ||
op_func_name=op_func_name, | ||
x=x, | ||
op_name=kernel_trans2_op_name_str, | ||
trace_backward=trace_backward, | ||
return_value=type_autocast_call_list, | ||
) | ||
else: | ||
type_autocast_logic_str = f'\n VLOG(5) << " No Type Autocast for {forward_ad_function_name} api. "; ' | ||
|
||
# Forward layout autotune | ||
layout_autotune_list_str = " ".join( | ||
layout_autotune_list | ||
|
@@ -2020,6 +2107,7 @@ def GenerateForwardDefinitionAndDeclaration(self, is_inplaced): | |
dygraph_event_str, | ||
amp_logic_str, | ||
type_promotion_logic_str, | ||
type_autocast_logic_str, | ||
layout_logic_str, | ||
forward_api_name, | ||
before_log_str, | ||
|
@@ -2044,6 +2132,7 @@ def GenerateForwardDefinitionAndDeclaration(self, is_inplaced): | |
dygraph_event_str, | ||
amp_logic_str, | ||
type_promotion_logic_str, | ||
type_autocast_logic_str, | ||
layout_logic_str, | ||
inputs_autograd_meta_str, | ||
forward_api_name, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
只有int情况下会被cast,还是所有情况下会被cast,不能引入不兼容的性能下降问题,只能对于之前不支持会报错的情况去cast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
只有在指定的op列表里且输入是int的时候会cast,应该不会不兼容。CE-Framework 的报错可能需要修改 PaddleTest 中的一些单测,因为有一些单测里面包含int输入会报错的case。
在 aistudio 上跑的结果

在 PaddleTest 中注释掉 paddle.sin 的单测:PaddlePaddle/PaddleTest#2992