未验证 提交 139a30ec 编写于 作者: Z Zhangjingyu06 提交者: GitHub

modify unit test in bn, stack and split. *test=kunlun (#40880)

上级 04087012
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
......@@ -38,25 +38,15 @@ class BatchNormXPUKernel : public framework::OpKernel<T> {
bool global_stats = test_mode || use_global_stats;
const auto &data_layout_str = ctx.Attr<std::string>("data_layout");
const auto data_layout = framework::StringToDataLayout(data_layout_str);
PADDLE_ENFORCE_EQ(data_layout_str == "NCHW" || data_layout_str == "NHWC",
true,
platform::errors::InvalidArgument(
"The 'data_layout' attribute must be NCHW or NHWC. "
"But recevived 'data_layout' is [%s].",
data_layout_str));
const auto *x = ctx.Input<Tensor>("X");
const auto &x_dims = x->dims();
PADDLE_ENFORCE_EQ(
x_dims.size() >= 2 && x_dims.size() <= 5, true,
platform::errors::InvalidArgument(
"The size of input's dimensions should be between 2 and 5"
"But received: the size of input's dimensions is [%d]",
x_dims.size()));
int N, C, H, W, D;
ExtractNCWHD(x_dims, data_layout, &N, &C, &H, &W, &D);
int temp = x_dims[3];
temp = (x_dims.size() != 4) ? 1 : temp;
bool is_nchw = (data_layout == DataLayout::kNCHW);
const int N = x_dims[0];
const int C = is_nchw ? x_dims[1] : temp;
const int H = is_nchw ? x_dims[2] : x_dims[1];
const int W = is_nchw ? temp : x_dims[2];
const auto *scale = ctx.Input<Tensor>("Scale");
const auto *bias = ctx.Input<Tensor>("Bias");
const auto *x_data = x->data<T>();
......@@ -77,7 +67,6 @@ class BatchNormXPUKernel : public framework::OpKernel<T> {
saved_variance->mutable_data<float>(ctx.GetPlace());
auto &dev_ctx = ctx.template device_context<DeviceContext>();
bool is_nchw = data_layout_str == "NCHW";
if (!global_stats) {
auto *mean_out_data = mean_out->data<float>();
......@@ -94,15 +83,27 @@ class BatchNormXPUKernel : public framework::OpKernel<T> {
&mom_cpu);
momentum = mom_tensor->data<float>()[0];
}
if (C == 1) {
int r = xpu::batch_norm<T>(dev_ctx.x_context(), x_data, y_data, N, 1, H,
W, epsilon, momentum, scale_data, bias_data,
saved_mean_data, saved_variance_data,
mean_out_data, variance_out_data, true);
PADDLE_ENFORCE_EQ(
r, xpu::Error_t::SUCCESS,
platform::errors::External(
"The batch_norm XPU API return wrong value[%d %s]", r,
XPUAPIErrorMsg[r]));
} else {
int r = xpu::batch_norm<T>(dev_ctx.x_context(), x_data, y_data, N, C, H,
W, epsilon, momentum, scale_data, bias_data,
saved_mean_data, saved_variance_data,
mean_out_data, variance_out_data, is_nchw);
PADDLE_ENFORCE_EQ(r, xpu::Error_t::SUCCESS,
PADDLE_ENFORCE_EQ(
r, xpu::Error_t::SUCCESS,
platform::errors::External(
"The batch_norm XPU API return wrong value[%d %s]",
r, XPUAPIErrorMsg[r]));
"The batch_norm XPU API return wrong value[%d %s]", r,
XPUAPIErrorMsg[r]));
}
} else {
const auto *mean = ctx.Input<Tensor>("Mean");
const auto *variance = ctx.Input<Tensor>("Variance");
......@@ -110,7 +111,7 @@ class BatchNormXPUKernel : public framework::OpKernel<T> {
const auto *variance_data = variance->data<float>();
int r = xpu::batch_norm_infer(dev_ctx.x_context(), x_data, y_data, N, C,
H, W, epsilon, scale_data, bias_data,
mean_data, variance_data, is_nchw);
mean_data, variance_data, true);
PADDLE_ENFORCE_EQ(
r, xpu::Error_t::SUCCESS,
platform::errors::External(
......@@ -171,13 +172,6 @@ class BatchNormGradXPUKernel : public framework::OpKernel<T> {
const float epsilon = ctx.Attr<float>("epsilon");
const auto data_layout = framework::StringToDataLayout(data_layout_str);
PADDLE_ENFORCE_EQ(data_layout_str == "NCHW" || data_layout_str == "NHWC",
true,
platform::errors::InvalidArgument(
"The 'data_layout' attribute must be NCHW or NHWC. "
"But recevived 'data_layout' is [%s].",
data_layout_str));
auto *d_x = ctx.Output<Tensor>(framework::GradVarName("X"));
auto *d_scale = ctx.Output<Tensor>(framework::GradVarName("Scale"));
auto *d_bias = ctx.Output<Tensor>(framework::GradVarName("Bias"));
......@@ -210,15 +204,13 @@ class BatchNormGradXPUKernel : public framework::OpKernel<T> {
}
const auto &x_dims = x->dims();
PADDLE_ENFORCE_EQ(
x_dims.size() >= 2 && x_dims.size() <= 5, true,
platform::errors::InvalidArgument(
"The size of input's dimensions should be between 2 and 5"
"But received: the size of input's dimensions is [%d]",
x_dims.size()));
int N, C, H, W, D;
ExtractNCWHD(x_dims, data_layout, &N, &C, &H, &W, &D);
int temp = x_dims[3];
temp = (x_dims.size() != 4) ? 1 : temp;
bool is_nchw = (data_layout == DataLayout::kNCHW);
const int N = x_dims[0];
const int C = is_nchw ? x_dims[1] : temp;
const int H = is_nchw ? x_dims[2] : x_dims[1];
const int W = is_nchw ? temp : x_dims[2];
const auto *x_data = x->data<T>();
const auto *d_y_data = d_y->data<T>();
......@@ -243,45 +235,42 @@ class BatchNormGradXPUKernel : public framework::OpKernel<T> {
"the size of scale's dimensions is [%d], the dimensions of scale "
"is [%s].",
scale->dims().size(), scale->dims()));
PADDLE_ENFORCE_EQ(
scale->dims()[0], C,
platform::errors::InvalidArgument(
"The first dimension of scale must equal to Channels[%d]. But "
"received: the first dimension of scale is [%d]",
C, scale->dims()[0]));
auto &dev_ctx = ctx.template device_context<DeviceContext>();
xpu::ctx_guard RAII_GUARD(dev_ctx.x_context());
const auto *batch_mean = ctx.Input<Tensor>("SavedMean");
const auto *batch_inv_std = ctx.Input<Tensor>("SavedVariance");
const auto *global_mean = ctx.Input<Tensor>("Mean");
const auto *global_var = ctx.Input<Tensor>("Variance");
const T *mean_data = nullptr;
const T *inv_var_data = nullptr;
// TODO(guozibin): hadle the situation case of N * H * W = 1
if (is_inplace) {
float *global_inv_std_data;
if (use_global_stats) {
global_inv_std_data =
RAII_GUARD.alloc_l3_or_gm<float>(global_var->numel());
if (!use_global_stats) {
const auto *saved_mean = ctx.Input<Tensor>("SavedMean");
// SavedVariance have been reverted in forward operator
const auto *saved_inv_variance = ctx.Input<Tensor>("SavedVariance");
mean_data = saved_mean->data<float>();
inv_var_data = saved_inv_variance->data<float>();
} else {
const auto *running_mean = ctx.Input<Tensor>("Mean");
const auto *running_variance = ctx.Input<Tensor>("Variance");
mean_data = running_mean->data<float>();
inv_var_data = running_variance->data<float>();
float *running_inv_var_data =
RAII_GUARD.alloc_l3_or_gm<float>(running_variance->numel());
float *epsilon_data = RAII_GUARD.alloc_l3_or_gm<float>(1);
int r1 =
calculate_inv_var(dev_ctx.x_context(), global_var->data<float>(),
epsilon, C, epsilon_data, global_inv_std_data);
int r1 = calculate_inv_var(dev_ctx.x_context(), inv_var_data, epsilon, C,
epsilon_data, running_inv_var_data);
PADDLE_ENFORCE_EQ(r1, XPU_SUCCESS, platform::errors::External(
"XPU API(batch_norm_grad "
"calculate_inv_var function) "
"return wrong value[%d %s]",
r1, XPUAPIErrorMsg[r1]));
inv_var_data = running_inv_var_data;
}
if (is_inplace) {
auto px = *x;
auto *inv_std_data =
use_global_stats ? global_inv_std_data : batch_inv_std->data<float>();
auto mean_data = use_global_stats ? global_mean->data<float>()
: batch_mean->data<float>();
int r2 = calculate_inv_BN_Y(
dev_ctx.x_context(), px.mutable_data<T>(ctx.GetPlace()),
scale->data<float>(), bias->data<float>(), mean_data, inv_std_data, N,
scale->data<float>(), bias->data<float>(), mean_data, inv_var_data, N,
C, H * W, x->data<T>());
PADDLE_ENFORCE_EQ(r2, XPU_SUCCESS, platform::errors::External(
"XPU API(batch_norm_grad "
......@@ -289,15 +278,6 @@ class BatchNormGradXPUKernel : public framework::OpKernel<T> {
"return wrong value[%d %s]",
r2, XPUAPIErrorMsg[r2]));
}
int r3;
bool is_nchw = data_layout_str == "NCHW";
if (use_global_stats) {
r3 = xpu::batch_norm_grad<T>(
dev_ctx.x_context(), x_data, d_y_data, d_x_data, N, C, H, W,
scale_data, nullptr, nullptr, d_scale_data, d_bias_data, is_nchw,
global_mean->data<float>(), global_var->data<float>(), epsilon);
} else {
if (!d_x) {
d_x_data = RAII_GUARD.alloc_l3_or_gm<T>(x->numel());
}
......@@ -307,11 +287,10 @@ class BatchNormGradXPUKernel : public framework::OpKernel<T> {
if (!d_bias_data) {
d_bias_data = RAII_GUARD.alloc_l3_or_gm<float>(C);
}
r3 = xpu::batch_norm_grad<T>(
dev_ctx.x_context(), x_data, d_y_data, d_x_data, N, C, H, W,
scale_data, batch_mean->data<float>(), batch_inv_std->data<float>(),
d_scale_data, d_bias_data, is_nchw);
}
int r3 = xpu::batch_norm_grad<T>(
dev_ctx.x_context(), x_data, d_y_data, d_x_data, N, C, H, W, scale_data,
mean_data, inv_var_data, d_scale_data, d_bias_data, is_nchw);
PADDLE_ENFORCE_EQ(r3, XPU_SUCCESS, platform::errors::External(
"XPU API(batch_norm_grad) return "
"wrong value[%d %s]",
......
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -26,6 +26,10 @@ import paddle.fluid as fluid
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.fluid import compiler, Program, program_guard
from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
paddle.enable_static()
def ref_batch_norm_infer(x, scale, bias, mean, variance, momentum, epsilon,
......@@ -121,17 +125,23 @@ def ref_batch_norm_train(x, y_grad, scale, bias, mean, variance, momentum,
return y, mean_out, variance_out, saved_mean, saved_inv_std, x_grad, scale_grad, bias_grad
@unittest.skipIf(not paddle.is_compiled_with_xpu(),
class XPUTestBatchNormOp(XPUOpTestWrapper):
def __init__(self):
self.op_name = 'batch_norm'
self.use_dynamic_create_class = False
@unittest.skipIf(not paddle.is_compiled_with_xpu(),
"core is not compiled with XPU")
class TestXPUBatchNormOp(unittest.TestCase):
class TestBatchNormOp(unittest.TestCase):
def setUp(self):
self.place = paddle.XPUPlace(0)
self.op_type = "batch_norm"
self.dtype = np.float32
self.shape = [2, 3, 4, 5]
self.data_layout = "NCHW"
self.epsilon = 1e-05
self.momentum = 0.9
self.init_dtype()
self.set_xpu()
self.set_attrs()
if self.data_layout == "NHWC":
......@@ -156,6 +166,14 @@ class TestXPUBatchNormOp(unittest.TestCase):
def set_attrs(self):
pass
def init_dtype(self):
self.dtype = self.in_type
def set_xpu(self):
self.__class__.use_xpu = True
self.__class__.op_type = self.in_type
self.place = paddle.XPUPlace(0)
def test_infer(self):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
......@@ -266,8 +284,7 @@ class TestXPUBatchNormOp(unittest.TestCase):
np.allclose(
outputs[name], outs[id], atol=1e-4), True)
class TestXPUBatchNormOpUseGlobalStats(unittest.TestCase):
class TestBatchNormOpUseGlobalStats(unittest.TestCase):
def setUp(self):
self.places = [paddle.XPUPlace(0)]
self.init_test()
......@@ -296,38 +313,24 @@ class TestXPUBatchNormOpUseGlobalStats(unittest.TestCase):
net2.training = False
y1 = net1(x)
y2 = net2(x)
self.assertEqual(
np.allclose(
y1.numpy(), y2.numpy(), atol=1e-4), True)
class TestXPUBatchNormUseGlobalStatsCase1(TestXPUBatchNormOpUseGlobalStats):
### test mode
def init_test(self):
self.use_global_stats = False
self.trainable_statistics = True
self.assertEqual(np.allclose(y1.numpy(), y2.numpy()), True)
class TestXPUBatchNormUseGlobalStatsCase2(TestXPUBatchNormOpUseGlobalStats):
### train mode
def init_test(self):
self.use_global_stats = False
self.trainable_statistics = False
class TestXPUBatchNormUseGlobalStatsCase3(TestXPUBatchNormOpUseGlobalStats):
class TestBatchNormOpUseGlobalStats1(TestBatchNormOpUseGlobalStats):
### test mode
def init_test(self):
self.use_global_stats = True
self.trainable_statistics = True
class TestXPUBatchNormUseGlobalStatsCase4(TestXPUBatchNormOpUseGlobalStats):
class TestBatchNormUseGlobalStats2(TestBatchNormOpUseGlobalStats):
### train mode
def init_test(self):
self.use_global_stats = True
self.trainable_statistics = False
if __name__ == "__main__":
support_types = get_xpu_op_support_types('batch_norm')
for stype in support_types:
create_test_class(globals(), XPUTestBatchNormOp, stype)
if __name__ == '__main__':
unittest.main()
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -23,23 +23,25 @@ from op_test_xpu import XPUOpTest
import paddle
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
paddle.enable_static()
# test with attr(num)
class TestSplitOp(XPUOpTest):
def initDefaultParameters(self):
self.dtype = 'float32'
self.x = np.random.random((4, 5, 6)).astype(self.dtype)
self.axis = 2
self.sections = []
self.num = 3
self.indices_or_sections = 3
class XPUTestSplitOp(XPUOpTestWrapper):
def __init__(self):
self.op_name = 'split'
self.use_dynamic_create_class = False
# test with attr(num)
class TestSplitOp(XPUOpTest):
def setUp(self):
self.init_dtype()
self.__class__.use_xpu = True
self.__class__.op_type = 'split'
self.use_xpu = True
self.use_mkldnn = False
self.initDefaultParameters()
self.initParameters()
self.inputs = {'X': self.x}
self.attrs = {
'axis': self.axis,
......@@ -51,77 +53,41 @@ class TestSplitOp(XPUOpTest):
self.outputs = {'Out': [('out%d' % i, out[i]) \
for i in range(len(out))]}
def test_check_output(self):
if paddle.is_compiled_with_xpu():
paddle.enable_static()
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
def init_dtype(self):
self.dtype = self.in_type
def initParameters(self):
self.x = np.random.random((4, 5, 6)).astype(self.dtype)
self.axis = 2
self.sections = []
self.num = 3
self.indices_or_sections = 3
def test_check_output(self):
self.check_output_with_place(paddle.XPUPlace(0))
# unknown sections
class TestSplitOp_2(XPUOpTest):
def initDefaultParameters(self):
self.dtype = 'float32'
# unknown sections
class TestSplitOp1(TestSplitOp):
def initParameters(self):
self.x = np.random.random((4, 5, 6)).astype(self.dtype)
self.axis = 2
self.sections = [2, 1, -1]
self.num = 0
self.indices_or_sections = [2, 3]
def setUp(self):
self.__class__.op_type = 'split'
self.use_xpu = True
self.use_mkldnn = False
self.initDefaultParameters()
self.inputs = {'X': self.x}
self.attrs = {
'axis': self.axis,
'sections': self.sections,
'num': self.num
}
out = np.split(self.x, self.indices_or_sections, self.axis)
self.outputs = {'Out': [('out%d' % i, out[i]) \
for i in range(len(out))]}
def test_check_output(self):
if paddle.is_compiled_with_xpu():
paddle.enable_static()
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
# test with int32
class TestSplitOp_5(XPUOpTest):
def initDefaultParameters(self):
self.dtype = 'int32'
self.x = np.random.random((4, 5, 6)).astype(self.dtype)
# test with int32
class TestSplitOp2(TestSplitOp):
def initParameters(self):
self.x = np.random.random((4, 5, 6)).astype(np.int32)
self.axis = 2
self.sections = []
self.num = 3
self.indices_or_sections = 3
def setUp(self):
self.__class__.op_type = 'split'
self.use_xpu = True
self.use_mkldnn = False
self.initDefaultParameters()
self.inputs = {'X': self.x}
self.attrs = {
'axis': self.axis,
'sections': self.sections,
'num': self.num
}
out = np.split(self.x, self.indices_or_sections, self.axis)
self.outputs = {'Out': [('out%d' % i, out[i]) \
for i in range(len(out))]}
def test_check_output(self):
if paddle.is_compiled_with_xpu():
paddle.enable_static()
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
support_types = get_xpu_op_support_types('split')
for stype in support_types:
create_test_class(globals(), XPUTestSplitOp, stype)
if __name__ == '__main__':
unittest.main()
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -23,29 +23,30 @@ from op_test_xpu import XPUOpTest
import paddle
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
paddle.enable_static()
class XPUTestStackOp(XPUOpTestWrapper):
def __init__(self):
self.op_name = 'stack'
self.use_dynamic_create_class = False
@skip_check_grad_ci(reason="There is no grad kernel for stack_xpu op.")
class TestStackOpBase(XPUOpTest):
@skip_check_grad_ci(reason="There is no grad kernel for stack_xpu op.")
class TestStackOp(XPUOpTest):
def initDefaultParameters(self):
self.num_inputs = 4
self.input_dim = (5, 6, 7)
self.axis = 0
self.dtype = 'float32'
def initParameters(self):
pass
def get_x_names(self):
x_names = []
for i in range(self.num_inputs):
x_names.append('x{}'.format(i))
return x_names
self.dtype = np.float32
def setUp(self):
self.initDefaultParameters()
self.initParameters()
self.op_type = 'stack'
self.__class__.use_xpu = True
self.__class__.op_type = 'stack'
self.x = []
for i in range(self.num_inputs):
self.x.append(
......@@ -60,79 +61,82 @@ class TestStackOpBase(XPUOpTest):
self.outputs = {'Y': np.stack(self.x, axis=self.axis)}
self.attrs = {'axis': self.axis}
def init_dtype(self):
self.dtype = self.in_type
def initParameters(self):
pass
def get_x_names(self):
x_names = []
for i in range(self.num_inputs):
x_names.append('x{}'.format(i))
return x_names
def test_check_output(self):
if paddle.is_compiled_with_xpu():
paddle.enable_static()
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
self.check_output_with_place(paddle.XPUPlace(0))
def test_check_grad(self):
if self.dtype == 'int64' or self.dtype == 'int32':
if self.dtype == np.int32 or self.dtype == np.int64:
pass
else:
if paddle.is_compiled_with_xpu():
paddle.enable_static()
place = paddle.XPUPlace(0)
self.check_grad_with_place(place, self.get_x_names(), 'Y')
self.check_grad_with_place(
paddle.XPUPlace(0), self.get_x_names(), 'Y')
class TestStackOp1(TestStackOpBase):
class TestStackOp1(TestStackOp):
def initParameters(self):
self.num_inputs = 16
class TestStackOp2(TestStackOpBase):
class TestStackOp2(TestStackOp):
def initParameters(self):
self.num_inputs = 30
class TestStackOp3(TestStackOpBase):
class TestStackOp3(TestStackOp):
def initParameters(self):
self.axis = -1
def test_check_grad(self):
pass
class TestStackOp4(TestStackOpBase):
class TestStackOp4(TestStackOp):
def initParameters(self):
self.axis = -4
def test_check_grad(self):
pass
class TestStackOp5(TestStackOpBase):
class TestStackOp5(TestStackOp):
def initParameters(self):
self.axis = 1
class TestStackOp6(TestStackOpBase):
class TestStackOp6(TestStackOp):
def initParameters(self):
self.axis = 3
class TestStackOpint64(TestStackOpBase):
def initDefaultParameters(self):
class TestStackOp7(TestStackOp):
def initParameters(self):
self.num_inputs = 4
self.input_dim = (5, 6, 7)
self.axis = 0
self.dtype = 'int64'
def initParameters(self):
self.num_inputs = 16
self.dtype = np.int64
def test_check_grad(self):
pass
class TestStackOpint(TestStackOpBase):
def initDefaultParameters(self):
class TestStackOp8(TestStackOp):
def initParameters(self):
self.num_inputs = 4
self.input_dim = (5, 6, 7)
self.axis = 0
self.dtype = 'int32'
self.dtype = np.int32
def test_check_grad(self):
pass
def initParameters(self):
self.num_inputs = 16
support_types = get_xpu_op_support_types('stack')
for stype in support_types:
create_test_class(globals(), XPUTestStackOp, stype)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册