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

[AMP OP&Test] Fix scale kernel and perfect unit test #50998

Merged
merged 8 commits into from
Mar 6, 2023

Conversation

201716010711
Copy link
Contributor

@201716010711 201716010711 commented Feb 28, 2023

PR types

Others

PR changes

Others

Describe

修复scale fp16和bf16的精度损失问题,精简scale fp16 单测

@paddle-bot
Copy link

paddle-bot bot commented Feb 28, 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.

class TestScaleFp16Op1(TestScaleOp):
def init_dtype_type(self):
self.dtype = np.float16
self.inputs = {'X': np.random.random((10, 20, 20)).astype(self.dtype)}
Copy link
Contributor

Choose a reason for hiding this comment

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

建议用uniform的形式替换random生成

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

def test_check_output(self):
place = core.CUDAPlace(0)
if core.is_float16_supported(place):
self.check_output_with_place(place, atol=0.001, check_eager=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的atol可以不加了,里面已经默认成了1e-3

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

def init_dtype_type(self):
self.dtype = np.float16
self.inputs = {
'X': np.random.uniform(-10, 10, (10, 20, 20)).astype(self.dtype)
Copy link
Contributor

Choose a reason for hiding this comment

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

random里面的给的参数是shape,范围是0-1的,uniform这个建议采用0,1为范围,shape与原测试保持一致

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -178,6 +178,22 @@ def test_check_grad(self):
self.check_grad(['X'], 'Out', numeric_grad_delta=0.8, check_eager=True)


class TestScaleFp16Op1(TestScaleOp):
Copy link
Contributor

Choose a reason for hiding this comment

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

这里上面就有FP16的测试,直接改那个就可以

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -41,6 +41,28 @@ struct ScaleFunctor {
}
};

struct ScaleFunctorFp16 {
Copy link
Contributor

Choose a reason for hiding this comment

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

可以重写下ScaleFunctor,增加一个模版参数,表示scale和bias的数据类型,如下
template <typename DataT, typename ParamT>
struct ScaleFunctor {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@ZzSean
Copy link
Contributor

ZzSean commented Mar 2, 2023

改下PR Title和PR描述

@@ -149,15 +149,12 @@ def init_dtype_type(self):

def test_check_output(self):
place = core.CUDAPlace(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

你这个不能都删掉,至少得运行一下self.check_output

@201716010711 201716010711 changed the title fix scale fp16 test Fix scale kernel and perfect unit test Mar 2, 2023
@@ -149,15 +149,12 @@ def init_dtype_type(self):

def test_check_output(self):
place = core.CUDAPlace(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

这行删

place, ["X"], "Out", max_relative_error=0.05, check_eager=True
)
self.check_grad_with_place(
place, ["X"], "Out", max_relative_error=0.001, check_eager=True
Copy link
Contributor

Choose a reason for hiding this comment

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

这里调的函数也不对啊,是self.check_grad

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改,位置在#51083

Copy link
Contributor

@ZzSean ZzSean left a comment

Choose a reason for hiding this comment

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

LGTM

@ZzSean ZzSean changed the title Fix scale kernel and perfect unit test [AMP OP&Test] Fix scale kernel and perfect unit test Mar 6, 2023
@ZzSean ZzSean merged commit 02f6674 into PaddlePaddle:develop Mar 6, 2023
Xreki added a commit to Xreki/Paddle that referenced this pull request Apr 9, 2023
aoyulong pushed a commit that referenced this pull request Apr 11, 2023
* Fix scale kernel for low precision, cherry pick #50998.

* Fix the FP16 precision problem of add_n. (#50129)

* Change squared_l2_norm to reuse ReduceKernel, and register fp16 and bf16 kernel, which is cherry pick #48315.

* Cherry-pick the fix of MPTypeTrait in KP, which is implemented in #50993.

* Cherry-pick the multi-precision support of AdamW for bf16, #48041.

* Fix compiling error.

* Cherry-pick the fix of CubTensorReduceImpl for bfloat16 in #50993.

* Fix unittest.

---------

Co-authored-by: liuruyan <44316842+liuruyan@users.noreply.github.com>
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