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

[AMP OP&Test] Norm bf16 #51083

Merged
merged 24 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
764bccb
fix scale fp16 test
201716010711 Feb 28, 2023
b70aa99
fix scale fp16 test1
201716010711 Feb 28, 2023
a8e3250
fix scale fp16 test2
201716010711 Feb 28, 2023
164c867
fix scale fp16 test3
201716010711 Feb 28, 2023
12362e3
add norm op test and support bf16 norm
201716010711 Mar 1, 2023
986fcf3
add norm op test and support bf16 norm2
201716010711 Mar 1, 2023
46f7674
add norm op test and support bf16 norm3
201716010711 Mar 2, 2023
6d37195
add norm op test and support bf16 norm4
201716010711 Mar 2, 2023
7cce1b6
add norm op test and support bf16 norm5
201716010711 Mar 3, 2023
ba9b68d
add norm op test and support bf16 norm6
201716010711 Mar 3, 2023
858ef6e
add norm op test and support bf16 norm7
201716010711 Mar 4, 2023
6af3425
add norm op test and support bf16 norm8
201716010711 Mar 6, 2023
468b463
add norm op test and support bf16 norm9
201716010711 Mar 6, 2023
edbac22
add norm op test and support bf16 norm10
201716010711 Mar 7, 2023
b14ba7c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
201716010711 Mar 7, 2023
60c8df9
add norm op test and support bf16 norm11
201716010711 Mar 7, 2023
e1cb93f
add norm op test and support bf16 norm12
201716010711 Mar 7, 2023
ec4bef8
add norm op test and support bf16 norm13
201716010711 Mar 8, 2023
95074f7
add norm op test and support bf16 norm14
201716010711 Mar 8, 2023
febee3f
add norm op test and support bf16 norm15
201716010711 Mar 13, 2023
17a384b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
201716010711 Mar 15, 2023
d4fa88f
add norm op test and support bf16 norm over
201716010711 Mar 17, 2023
ed405f7
fix norm pull
201716010711 Mar 17, 2023
57e09a1
add norm op test and support bf16 norm over1
201716010711 Mar 20, 2023
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/norm_grad_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,5 @@ PD_REGISTER_KERNEL(norm_grad,
phi::NormGradKernel,
float,
double,
phi::dtype::float16) {}
phi::dtype::float16,
phi::dtype::bfloat16) {}
10 changes: 5 additions & 5 deletions paddle/phi/kernels/gpu/norm_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ __global__ void Normalize(const T* x,
const int pre,
const int axis_n, // dim in axis
const int post,
const T eps,
const float eps,
T* y,
T* out_norm) {
using MT = typename phi::dtype::MPTypeTrait<T>::Type;
Expand Down Expand Up @@ -86,7 +86,6 @@ void NormKernel(const Context& ctx,

auto xdim = in_x->dims();
if (axis < 0) axis = xdim.size() + axis;
T eps = static_cast<T>(epsilon);

DenseTensor* out_norm;
DenseTensor out_norm_tmp;
Expand Down Expand Up @@ -117,8 +116,8 @@ void NormKernel(const Context& ctx,
int max_threads = ctx.GetMaxPhysicalThreadCount();
const int max_blocks = std::max(max_threads / block, 1);
int grid = std::min(max_blocks, pre * post);
Normalize<T, block>
<<<grid, block, 0, ctx.stream()>>>(x_ptr, pre, n, post, eps, y, norm_ptr);
Normalize<T, block><<<grid, block, 0, ctx.stream()>>>(
x_ptr, pre, n, post, epsilon, y, norm_ptr);
}

} // namespace phi
Expand All @@ -129,4 +128,5 @@ PD_REGISTER_KERNEL(norm,
phi::NormKernel,
float,
double,
phi::dtype::float16) {}
phi::dtype::float16,
phi::dtype::bfloat16) {}
33 changes: 33 additions & 0 deletions python/paddle/fluid/tests/unittests/test_norm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

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

import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core


def l2_norm(x, axis, epsilon):
Expand Down Expand Up @@ -157,6 +159,37 @@ def init_test_case(self):
self.epsilon = 1e-8


@unittest.skipIf(
not core.is_compiled_with_cuda(),
"core is not compiled with CUDA and not support the bfloat16",
Copy link
Contributor

Choose a reason for hiding this comment

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

"and not support the bfloat16" 这句可以去掉

)
class TestNormBF16Op(OpTest):
def setUp(self):
self.op_type = "norm"
self.python_api = norm_wrapper
self.init_test_case()
self.dtype = "float32"
x = np.random.random(self.shape).astype(self.dtype)
y, norm = l2_norm(x, self.axis, self.epsilon)
self.inputs = {'X': convert_float_to_uint16(x)}
self.attrs = {'epsilon': self.epsilon, 'axis': self.axis}
self.outputs = {'Out': convert_float_to_uint16(y), 'Norm': norm}
self.python_out_sig = ['Out']

def test_check_output(self):
self.check_output_with_place(core.CUDAPlace(0), atol=1e-1)

def test_check_grad(self):
self.check_grad_with_place(
core.CUDAPlace(0), ['X'], 'Out', max_relative_error=1e-2
)

def init_test_case(self):
self.shape = [2, 3, 4, 5]
self.axis = 1
self.epsilon = 1e-8


class API_NormTest(unittest.TestCase):
def test_errors(self):
with fluid.program_guard(fluid.Program()):
Expand Down
6 changes: 2 additions & 4 deletions python/paddle/fluid/tests/unittests/test_scale_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,10 @@ def init_dtype_type(self):
self.dtype = np.float16

def test_check_output(self):
place = core.CUDAPlace(0)
self.check_output_with_place(place, check_eager=True)
self.check_output(check_eager=True)

def test_check_grad(self):
place = core.CUDAPlace(0)
self.check_grad_with_place(place, ["X"], "Out", check_eager=True)
self.check_grad(["X"], "Out", check_eager=True)


class TestScaleBF16Op(OpTest):
Expand Down