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】instance_norm fp16 and bf16 support. #52241

Merged
merged 24 commits into from
Apr 10, 2023

Conversation

qizhaoaoe
Copy link
Contributor

PR types

Others

PR changes

OPs

Describe

  1. add fp16 and bf16 dtype in instance_norm.
  2. add relative op tests.

@paddle-bot
Copy link

paddle-bot bot commented Mar 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.

const T *x,
const BatchNormParamType<T> *variance,
const BatchNormParamType<AccT> *variance,
Copy link
Contributor

Choose a reason for hiding this comment

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

可以看下BatchNormParamType的定义,这里不需要再使用AccT,直接用T就可以,BatchNormParamType(T==fp16orbf16)就是float了

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.

self.bias = np.random.random([self.shape[1]]).astype(np.float32)

def set_err_thre(self):
self.atol = 1e-3
Copy link
Contributor

Choose a reason for hiding this comment

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

fp32的误差阈值就需要这么大嘛

Copy link
Contributor Author

Choose a reason for hiding this comment

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

试了1e-4不通过,就还是保持1e-3吧?

self.max_relative_error = 1e-2
self.inputs = {
'X': convert_float_to_uint16(self.value),
'Scale': convert_float_to_uint16(self.scale),
Copy link
Contributor

Choose a reason for hiding this comment

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

bf16这里的scale和bias应该也不需要convert吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.

return x_norm, mean.reshape(N * C), std.reshape(N * C)

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.

这里需要判断下是否编译的是GPU版本,或者直接调用check_output,然后下面bf16的重写一下

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it.


def set_err_thre(self):
self.atol = 0.03125
self.max_relative_error = 8e-3
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.

是的

self.init_dtype()
self.init_shape()
self.init_value()
self.atol = 1e-2
Copy link
Contributor

Choose a reason for hiding this comment

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

bf16默认值为1e-2,无需设置

@@ -22,6 +22,11 @@
#include "paddle/phi/kernels/funcs/norm_utils.h"
#include "paddle/phi/kernels/gpu/instance_norm_utils.h"

#include "paddle/phi/common/bfloat16.h"
#include "paddle/phi/common/data_type.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

删除无用头文件,data_type.h应该包含了bfloat16和float16,device_context.h也需要确认下是否需要

Copy link
Contributor Author

Choose a reason for hiding this comment

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

删除了引用 data_type.h and device_context.h.

@@ -22,8 +22,13 @@
#include "paddle/phi/kernels/funcs/norm_utils.h"
#include "paddle/phi/kernels/gpu/instance_norm_utils.h"

#include "paddle/phi/common/bfloat16.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

删除无用头文件


def init_value(self):
np.random.seed(0)
self.value = np.random.random(self.shape).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.

这里对于bf16应该是初始化为fp32,然后convert

self.python_api = instance_norm_warpper
self.eps = 1e-5
self.data_format = "NCHW"
self.init_dtype()
Copy link
Contributor

Choose a reason for hiding this comment

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

bf16的单测并没有对init_dtype重写,所以当前还是dtype还是float32

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

Copy link
Contributor

@luotao1 luotao1 left a comment

Choose a reason for hiding this comment

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

LGTM

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

@ZzSean ZzSean merged commit 7c98abd into PaddlePaddle:develop Apr 10, 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