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

【PaddlePaddle Hackathon 4】No.63 : add embedding fp16 test #51321

Merged
merged 18 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion paddle/phi/kernels/gpu/broadcast_tensors_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ PD_REGISTER_KERNEL(broadcast_tensors_grad,
int64_t,
float,
double,
phi::dtype::float16) {}
phi::dtype::float16,
phi::dtype::bfloat16) {}
3 changes: 2 additions & 1 deletion paddle/phi/kernels/gpu/broadcast_tensors_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ PD_REGISTER_KERNEL(broadcast_tensors,
int64_t,
float,
double,
phi::dtype::float16) {}
phi::dtype::float16,
phi::dtype::bfloat16) {}
1 change: 1 addition & 0 deletions paddle/phi/kernels/gpu/lerp_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,6 @@ PD_REGISTER_KERNEL(lerp_grad,
ALL_LAYOUT,
phi::LerpGradKernel,
phi::dtype::float16,
phi::dtype::bfloat16,
float,
double) {}
1 change: 1 addition & 0 deletions paddle/phi/kernels/gpu/lerp_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ PD_REGISTER_KERNEL(lerp,
ALL_LAYOUT,
phi::LerpKernel,
phi::dtype::float16,
phi::dtype::bfloat16,
float,
double) {}
39 changes: 38 additions & 1 deletion python/paddle/fluid/tests/unittests/test_lerp_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import unittest

import numpy as np
from eager_op_test import OpTest
from eager_op_test import OpTest, convert_float_to_uint16

import paddle
from paddle.fluid import core
Expand Down Expand Up @@ -220,5 +220,42 @@ def test_x_y_broadcast_w(self):
paddle.enable_static()


@unittest.skipIf(
not core.is_compiled_with_cuda()
or not core.is_bfloat16_supported(core.CUDAPlace(0)),
"core is not complied with CUDA and not support the bfloat16",
)
class TestLerpBF16(OpTest):
Copy link
Contributor

Choose a reason for hiding this comment

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

case太少,需要跟FP16对齐数量

def setUp(self):
self.op_type = "lerp"
self.python_api = paddle.lerp
self.init_dtype()
self.init_shape()
self.__class__.op_type = self.op_type
x = np.arange(1.0, 101.0).astype(np.float32).reshape(self.shape)
y = np.full(100, 10.0).astype(np.float32).reshape(self.shape)
w = np.asarray([0.5]).astype(np.float32)
self.inputs = {
'X': convert_float_to_uint16(x),
'Y': convert_float_to_uint16(y),
'Weight': convert_float_to_uint16(w),
}
self.outputs = {'Out': convert_float_to_uint16(x + w * (y - x))}

def init_dtype(self):
self.dtype = np.uint16

def init_shape(self):
self.shape = [100]

def test_check_output(self):
place = core.CUDAPlace(0)
self.check_output_with_place(place)

def test_check_grad(self):
place = core.CUDAPlace(0)
self.check_grad_with_place(place, ['X', 'Y'], 'Out')


if __name__ == "__main__":
unittest.main()
15 changes: 13 additions & 2 deletions python/paddle/fluid/tests/unittests/test_lookup_table_v2_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ class TestLookupTableOp(OpTest):
def setUp(self):
self.op_type = "lookup_table_v2"
self.python_api = paddle.nn.functional.embedding
table = np.random.random((17, 31)).astype("float64")
self.init_dtype()

table = np.random.random((17, 32)).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.

不能因为原始测例无法通过,就改测例啊

ids = np.random.randint(0, 17, 4).astype(self.id_dtype())

self.inputs = {'W': table, 'Ids': ids}
self.outputs = {'Out': table[ids]}

def init_dtype(self):
self.dtype = "float64"

def id_dtype(self):
return "int64"

Expand Down Expand Up @@ -102,7 +108,7 @@ class TestLookupTableOpWithPadding(TestLookupTableOp):
def test_check_output(self):
ids = np.squeeze(self.inputs['Ids'])
padding_idx = np.random.choice(ids, 1)[0]
self.outputs['Out'][ids == padding_idx] = np.zeros(31)
self.outputs['Out'][ids == padding_idx] = np.zeros(32)
self.attrs = {'padding_idx': int(padding_idx)}
self.check_output()

Expand Down Expand Up @@ -297,6 +303,11 @@ def test_param_dtype():
)


class TestEmbeddingFP16OP(TestLookupTableOp):
def init_dtype(self):
self.dtype = np.float16


if __name__ == "__main__":
paddle.enable_static()
unittest.main()