-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
❌ The PR is not created using PR's template. You can refer to this Demo. |
1a7352f
to
14382b7
Compare
self.outputs = {'Out': output, 'Indices': indices} | ||
self.init_numeric_grads() | ||
|
||
def init_numeric_grads(self): |
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.
这个函数是否可以提取出来,减少重复代码
self.python_api = paddle.mode | ||
self.dtype = np.float16 | ||
self.input_data = ( | ||
np.random.rand(2, 64, 1).astype(np.float16).astype(np.float32) |
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.
fp16应该不用这样转,初始化为fp16时,内部参考结果计算时会转回fp32的
14382b7
to
ab60ca0
Compare
@@ -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) |
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.
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) |
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.
这个也是同样的问题,fp64的测例无需修改为user_defined_grads,然后补充fp16和bf16的测例,使用user_defined_grads
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.
这里反向不是精度的问题 而是数值微分计算方式会有有问题,比如,[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],不符
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.
了解,但是仍有两个问题:
- 对于double类型使用float32表示是否不合适
- 仍未补充fp16和bf16的TestModeOpLastdim测例
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. |
e937e26
to
d9ae910
Compare
@ZzSean done~有空辛苦再review下 |
@unittest.skipIf( | ||
not core.is_compiled_with_cuda(), "core is not compiled with CUDA" | ||
) | ||
class TestModeFP16Op(OpTest): |
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.
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) |
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.
这里可以直接使用check_output,可以直接继承TestModeOp,复用其中的函数
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.
这里还没改,可以直接继承,不用再写test_check_output和test_check_grad
self.input_data.shape, | ||
self.outputs['Indices'], | ||
self.axis, | ||
np.float32, |
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.
减少这个传参,改为在内部判断
def init_args(self): | ||
self.axis = -1 | ||
|
||
def setUp(self): |
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.
同样的,lastdim与原case也只有shape和axis不同,也可以将shape的设置写在init_args中,或者新写一个函数,不用重复再写setUp中的代码
d9ae910
to
2079c32
Compare
|
||
class TestModeOpLastdim(TestModeOp): | ||
def init_args(self): | ||
self.axis = -1 |
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.
我之前的意思是说把shape的设置也提出来,但是这里继承了TestModeOp后,测试的shape仍是(2, 64, 1),并不是(2, 1, 1, 2, 30)了
ab409ce
to
63e709f
Compare
63e709f
to
39b8756
Compare
@ZzSean done~,辛苦有空再review下 |
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.
LGTM
PR types
Others
PR changes
APIs
Description
为mode算子添加fp16,bf16支持并添加单测
改动说明: