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

Fill constant bs like and shape bf16 #51396

Merged
Show file tree
Hide file tree
Changes from all 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/full_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ PD_REGISTER_KERNEL(full_batch_size_like,
int,
int64_t,
bool,
phi::dtype::float16) {
phi::dtype::float16,
phi::dtype::bfloat16) {
kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
#endif
3 changes: 2 additions & 1 deletion paddle/phi/kernels/shape_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ PD_REGISTER_KERNEL(shape,
double,
phi::dtype::complex<float>,
phi::dtype::complex<double>,
phi::dtype::float16) {
phi::dtype::float16,
phi::dtype::bfloat16) {
kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
kernel->OutputAt(0).SetBackend(phi::Backend::CPU);
kernel->OutputAt(0).SetDataType(phi::DataType::INT32);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
import unittest

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

import paddle
import paddle.fluid.core as core
from paddle.fluid.framework import convert_np_dtype_to_dtype_

paddle.enable_static()
Expand Down Expand Up @@ -70,5 +71,46 @@ def test_check_output(self):
self.check_output()


@unittest.skipIf(
not core.is_compiled_with_cuda() or not core.supports_bfloat16(),
"core is not compiled with CUDA or place do not support bfloat16",
)
class TestFillConstatnBatchSizeLikeBf16(OpTest):
# test bf16
def setUp(self):
self.op_type = "fill_constant_batch_size_like"
self.python_api = fill_constant_batch_size_like
self.init_data()

input = np.zeros(self.shape).astype("float32")
input_bf16 = convert_float_to_uint16(input)
out = np.full_like(input, self.value, np.float32)
out_bf16 = convert_float_to_uint16(out)

self.inputs = {'Input': input_bf16}
self.outputs = {'Out': out_bf16}
self.attrs = {
'shape': self.shape,
'dtype': convert_np_dtype_to_dtype_(self.dtype),
'value': self.value,
'input_dim_idx': self.input_dim_idx,
'output_dim_idx': self.output_dim_idx,
'force_cpu': self.force_cpu,
}

def init_data(self):
self.shape = [10, 10]
self.dtype = np.uint16
self.value = 100
self.input_dim_idx = 0
self.output_dim_idx = 0
self.force_cpu = False

def test_check_output(self):
place = core.CUDAPlace(0)
lizhenyun01 marked this conversation as resolved.
Show resolved Hide resolved
self.check_output_with_place(place)


if __name__ == "__main__":
paddle.enable_static()
unittest.main()
26 changes: 25 additions & 1 deletion python/paddle/fluid/tests/unittests/test_shape_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 op_test import OpTest
from op_test import OpTest, convert_float_to_uint16

import paddle
from paddle.fluid import core
Expand Down Expand Up @@ -85,5 +85,29 @@ def test_check_output(self):
self.check_with_place(place)


@unittest.skipIf(
not core.is_compiled_with_cuda() or not core.supports_bfloat16(),
"core is not compiled with CUDA or place do not support bfloat16",
)
class TestShapeOpBf16(OpTest):
def setUp(self):
self.op_type = "shape"
self.dtype = 'bfloat16'
self.python_api = paddle.shape
self.config()
self.shape = [2, 3]
input = np.zeros(self.shape)
input = convert_float_to_uint16(input.astype('float32'))
self.inputs = {'Input': input}
self.outputs = {'Out': np.array(self.shape)}

def config(self):
self.shape = [2, 3]

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


if __name__ == '__main__':
unittest.main()