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

【Hackathon No61】add bf16 for mode #53195

Merged
merged 12 commits into from
May 16, 2023

Conversation

Difers
Copy link
Contributor

@Difers Difers commented Apr 21, 2023

PR types

Others

PR changes

APIs

Description

为mode算子添加fp16,bf16支持并添加单测
改动说明:

  1. 原先单测中out计算函数_mode1D不正确,导致众数计算结果都会说序列第一个,但由于原先input中重复值概率很小,因此未检测到。
  2. 对于众数bf16类型的计算结果测试,需要对输入对齐到精度损失后的input,例如input为[1.231,1.242,1.243],dtype=float32,计算众数,结果可以为1.231,index=0,而对bf16测试时,对input进行fp32 to uint16转换后,再转回,input精度损失后假设为[1.23,1.24,1.24],众数计算结果应为1.24,index=1,不应与原本fp32的结果进行对照检测,因此对input 进行了fp32 to uint16, uint16 to fp32的精度损失转换。
  3. 众数的反向计算采用默认数值微分的方式会有误,故采用了user_defined_grads

@paddle-bot
Copy link

paddle-bot bot commented Apr 21, 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.

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Apr 21, 2023
@paddle-bot
Copy link

paddle-bot bot commented Apr 21, 2023

❌ The PR is not created using PR's template. You can refer to this Demo.
Please use PR's template, it helps save our maintainers' time so that more developers get helped.

@Difers Difers force-pushed the add_fp_bf_seg_mode branch 5 times, most recently from 1a7352f to 14382b7 Compare April 24, 2023 02:24
self.outputs = {'Out': output, 'Indices': indices}
self.init_numeric_grads()

def init_numeric_grads(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

这个函数是否可以提取出来,减少重复代码

self.python_api = paddle.mode
self.dtype = np.float16
self.input_data = (
np.random.rand(2, 64, 1).astype(np.float16).astype(np.float32)
Copy link
Contributor

Choose a reason for hiding this comment

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

fp16应该不用这样转,初始化为fp16时,内部参考结果计算时会转回fp32的

@Difers Difers force-pushed the add_fp_bf_seg_mode branch from 14382b7 to ab60ca0 Compare April 26, 2023 06:12
@@ -56,6 +61,24 @@ def cal_mode(a, axis, keepdim=False):
return modes, indexes


def init_numeric_grads(input_shape, out_indices, axis):
grad = np.zeros(input_shape).astype(np.float32)
Copy link
Contributor

Choose a reason for hiding this comment

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

TestModeOp是float64的测试,但是这里cast为float32,属于高精度转低精度,不太合适,所以fp64这个单测是不是不用修改为user_defind_grads?

self.init_last_dim_numeric_grads()

def init_last_dim_numeric_grads(self):
self.grad = np.zeros(self.input_data.shape).astype(np.float32)
Copy link
Contributor

Choose a reason for hiding this comment

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

这个也是同样的问题,fp64的测例无需修改为user_defined_grads,然后补充fp16和bf16的测例,使用user_defined_grads

Copy link
Contributor Author

@Difers Difers Apr 28, 2023

Choose a reason for hiding this comment

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

这里反向不是精度的问题 而是数值微分计算方式会有有问题,比如,[1,2,2],众数是2,反向结果应该是[0,1,1],而数值微分计算是:
(ypos,yneg是对应index的x加或减了delta后计算众数的结果)
index0: ypos=2,yneg=2,grad0=0;
index1:ypos=f([1,2+0.005,2])=1,yneg同理=1,grad1=0;
index2同理可得grad2=0,
导致反向结果为[0,0,0],不符

Copy link
Contributor

Choose a reason for hiding this comment

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

了解,但是仍有两个问题:

  • 对于double类型使用float32表示是否不合适
  • 仍未补充fp16和bf16的TestModeOpLastdim测例

@paddle-ci-bot
Copy link

paddle-ci-bot bot commented May 4, 2023

Sorry to inform you that ab60ca0's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

@Difers Difers force-pushed the add_fp_bf_seg_mode branch 2 times, most recently from e937e26 to d9ae910 Compare May 5, 2023 14:55
@Difers
Copy link
Contributor Author

Difers commented May 6, 2023

@ZzSean done~有空辛苦再review下

@unittest.skipIf(
not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
class TestModeFP16Op(OpTest):
Copy link
Contributor

Choose a reason for hiding this comment

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

fp16的单测与原单测只有数据类型不同,和init_numeric_grads传的数据类型不同,能否把公共部分提出来,精简下代码,当前有很多重复代码

self.attrs = {'axis': self.axis}
output, indices = cal_mode(self.input_data, axis=self.axis)
self.outputs = {'Out': output, 'Indices': indices}

def test_check_output(self):
paddle.enable_static()
self.check_output()
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.

这里可以直接使用check_output,可以直接继承TestModeOp,复用其中的函数

Copy link
Contributor

Choose a reason for hiding this comment

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

这里还没改,可以直接继承,不用再写test_check_output和test_check_grad

self.input_data.shape,
self.outputs['Indices'],
self.axis,
np.float32,
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_args(self):
self.axis = -1

def setUp(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

同样的,lastdim与原case也只有shape和axis不同,也可以将shape的设置写在init_args中,或者新写一个函数,不用重复再写setUp中的代码

@Difers Difers force-pushed the add_fp_bf_seg_mode branch from d9ae910 to 2079c32 Compare May 11, 2023 05:23

class TestModeOpLastdim(TestModeOp):
def init_args(self):
self.axis = -1
Copy link
Contributor

Choose a reason for hiding this comment

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

我之前的意思是说把shape的设置也提出来,但是这里继承了TestModeOp后,测试的shape仍是(2, 64, 1),并不是(2, 1, 1, 2, 30)了

@Difers Difers force-pushed the add_fp_bf_seg_mode branch 2 times, most recently from ab409ce to 63e709f Compare May 12, 2023 06:10
@Difers Difers force-pushed the add_fp_bf_seg_mode branch from 63e709f to 39b8756 Compare May 12, 2023 07:18
@Difers
Copy link
Contributor Author

Difers commented May 12, 2023

@ZzSean done~,辛苦有空再review下

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 merged commit 640cff0 into PaddlePaddle:develop May 16, 2023
@luotao1 luotao1 changed the title 【Hackathon No57】add bf16 for mode 【Hackathon No61】add bf16 for mode May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants