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

[XPU] prepare ut for bf16 #59389

Merged
merged 7 commits into from
Nov 29, 2023
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
5 changes: 4 additions & 1 deletion paddle/phi/backends/xpu/xpu2_op_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@ XPUOpMap& get_kl2_ops() {
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"mean_grad",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"mean", XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"mean",
XPUKernelSet({phi::DataType::FLOAT32,
phi::DataType::FLOAT16,
phi::DataType::BFLOAT16})},
{"merged_adam", XPUKernelSet({phi::DataType::FLOAT32})},
{"merged_momentum",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
Expand Down
5 changes: 4 additions & 1 deletion paddle/phi/backends/xpu/xpu3_op_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,10 @@ XPUOpMap& get_kl3_ops() {
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"mean_grad",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"mean", XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"mean",
XPUKernelSet({phi::DataType::FLOAT32,
phi::DataType::FLOAT16,
phi::DataType::BFLOAT16})},
{"merged_momentum",
XPUKernelSet({phi::DataType::FLOAT32, phi::DataType::FLOAT16})},
{"mish_grad", XPUKernelSet({phi::DataType::FLOAT32})},
Expand Down
8 changes: 8 additions & 0 deletions paddle/phi/kernels/funcs/tensor_formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ std::string TensorFormatter::Format(const phi::DenseTensor& print_tensor,
FormatData<int64_t>(print_tensor, log_stream);
} else if (dtype == phi::DataType::BOOL) {
FormatData<bool>(print_tensor, log_stream);
} else if (dtype == phi::DataType::FLOAT16) {
FormatData<phi::dtype::float16>(print_tensor, log_stream);
} else if (dtype == phi::DataType::BFLOAT16) {
FormatData<phi::dtype::bfloat16>(print_tensor, log_stream);
} else {
log_stream << " - data: unprintable type: " << dtype << std::endl;
}
Expand Down Expand Up @@ -153,6 +157,10 @@ template void TensorFormatter::FormatData<int>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<int64_t>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<phi::dtype::float16>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);
template void TensorFormatter::FormatData<phi::dtype::bfloat16>(
const phi::DenseTensor& print_tensor, std::stringstream& log_stream);

} // namespace funcs
} // namespace paddle
39 changes: 35 additions & 4 deletions test/legacy_test/test_print_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import unittest

import numpy as np
from op_test import convert_float_to_uint16
from simple_nets import init_data, simple_fc_net

import paddle
Expand All @@ -30,14 +31,17 @@

class TestPrintOpCPU(unittest.TestCase):
def setUp(self):
self.dtype = 'float32'
self.place = paddle.CPUPlace()
self.x_tensor = base.core.LoDTensor()
tensor_np = np.random.random(size=(2, 3)).astype('float32')
tensor_np = np.random.random(size=(2, 3)).astype(self.dtype)
self.x_tensor.set(tensor_np, self.place)
self.x_tensor.set_recursive_sequence_lengths([[1, 1]])

def build_network(self, only_forward, **kargs):
x = paddle.static.data('x', shape=[-1, 3], dtype='float32', lod_level=1)
x = paddle.static.data(
'x', shape=[-1, 3], dtype=self.dtype, lod_level=1
)
x.stop_gradient = False
paddle.static.Print(input=x, **kargs)
loss = paddle.mean(x)
Expand Down Expand Up @@ -77,7 +81,7 @@ def test_all_parameters(self):
prog = paddle.static.Program()
with paddle.static.program_guard(prog, paddle.static.Program()):
x = paddle.static.data(
'x', shape=[-1, 3], dtype='float32', lod_level=1
'x', shape=[-1, 3], dtype=self.dtype, lod_level=1
)
x.stop_gradient = False

Expand Down Expand Up @@ -136,9 +140,36 @@ def test_errors(self):
)
class TestPrintOpGPU(TestPrintOpCPU):
def setUp(self):
self.dtype = 'float32'
self.place = paddle.CUDAPlace(0)
self.x_tensor = base.core.LoDTensor()
tensor_np = np.random.random(size=(2, 3)).astype('float32')
tensor_np = np.random.random(size=(2, 3)).astype(self.dtype)
self.x_tensor.set(tensor_np, self.place)
self.x_tensor.set_recursive_sequence_lengths([[1, 1]])


@unittest.skipIf(
not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
class TestPrintOpGPUFP16(TestPrintOpCPU):
def setUp(self):
self.dtype = 'float16'
self.place = paddle.CUDAPlace(0)
self.x_tensor = base.core.LoDTensor()
tensor_np = np.random.random(size=(2, 3)).astype(self.dtype)
self.x_tensor.set(tensor_np, self.place)
self.x_tensor.set_recursive_sequence_lengths([[1, 1]])


@unittest.skipIf(
not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
class TestPrintOpGPUBFP16(TestPrintOpCPU):
def setUp(self):
self.dtype = 'bfloat16'
self.place = paddle.CUDAPlace(0)
self.x_tensor = base.core.LoDTensor()
tensor_np = convert_float_to_uint16(np.random.random(size=(2, 3)))
self.x_tensor.set(tensor_np, self.place)
self.x_tensor.set_recursive_sequence_lengths([[1, 1]])

Expand Down
4 changes: 2 additions & 2 deletions test/xpu/op_test_xpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def check_grad_with_place(
if not core.is_float16_supported(place):
return

if self.dtype == np.float16:
max_relative_error = 1.0
if self.dtype == np.float16 or self.dtype == np.uint16:
max_relative_error = 0.1
return super().check_grad_with_place(
place,
inputs_to_check,
Expand Down
2 changes: 1 addition & 1 deletion test/xpu/test_adamw_op_xpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def setUp(self):
# Test AdamW Op with supplied attributes
self.op_type = "adamw"
self.init_shape()
self.dtype = self.in_type_str
self.dtype = self.in_type
param = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
grad = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
moment1 = np.random.uniform(-1, 1, self.shape).astype("float32")
Expand Down