提交 bfbc25bd 编写于 作者: K Kexin Zhao

add fp16 pool2d support

上级 02b3cfb1
...@@ -78,7 +78,8 @@ class PoolCUDNNOpKernel : public framework::OpKernel<T> { ...@@ -78,7 +78,8 @@ class PoolCUDNNOpKernel : public framework::OpKernel<T> {
// ------------------- cudnn pool algorithm --------------------- // ------------------- cudnn pool algorithm ---------------------
auto handle = ctx.cuda_device_context().cudnn_handle(); auto handle = ctx.cuda_device_context().cudnn_handle();
T alpha = 1.0f, beta = 0.0f; typename platform::CudnnDataType<T>::ScalingParamType alpha = 1.0f,
beta = 0.0f;
PADDLE_ENFORCE(platform::dynload::cudnnPoolingForward( PADDLE_ENFORCE(platform::dynload::cudnnPoolingForward(
handle, cudnn_pool_desc, &alpha, cudnn_input_desc, input_data, &beta, handle, cudnn_pool_desc, &alpha, cudnn_input_desc, input_data, &beta,
...@@ -144,7 +145,8 @@ class PoolCUDNNGradOpKernel : public framework::OpKernel<T> { ...@@ -144,7 +145,8 @@ class PoolCUDNNGradOpKernel : public framework::OpKernel<T> {
// ------------------- cudnn pool algorithm --------------------- // ------------------- cudnn pool algorithm ---------------------
auto handle = ctx.cuda_device_context().cudnn_handle(); auto handle = ctx.cuda_device_context().cudnn_handle();
T alpha = 1.0f, beta = 0.0f; typename platform::CudnnDataType<T>::ScalingParamType alpha = 1.0f,
beta = 0.0f;
if (input_grad) { if (input_grad) {
T *input_grad_data = input_grad->mutable_data<T>(ctx.GetPlace()); T *input_grad_data = input_grad->mutable_data<T>(ctx.GetPlace());
...@@ -162,17 +164,19 @@ class PoolCUDNNGradOpKernel : public framework::OpKernel<T> { ...@@ -162,17 +164,19 @@ class PoolCUDNNGradOpKernel : public framework::OpKernel<T> {
} // namespace paddle } // namespace paddle
namespace ops = paddle::operators; namespace ops = paddle::operators;
namespace plat = paddle::platform;
REGISTER_OP_KERNEL(pool2d, CUDNN, ::paddle::platform::CUDAPlace, REGISTER_OP_KERNEL(pool2d, CUDNN, plat::CUDAPlace,
ops::PoolCUDNNOpKernel<float>, ops::PoolCUDNNOpKernel<float>,
ops::PoolCUDNNOpKernel<double>); ops::PoolCUDNNOpKernel<double>,
REGISTER_OP_KERNEL(pool2d_grad, CUDNN, ::paddle::platform::CUDAPlace, ops::PoolCUDNNOpKernel<plat::float16>);
REGISTER_OP_KERNEL(pool2d_grad, CUDNN, plat::CUDAPlace,
ops::PoolCUDNNGradOpKernel<float>, ops::PoolCUDNNGradOpKernel<float>,
ops::PoolCUDNNGradOpKernel<double>); ops::PoolCUDNNGradOpKernel<double>);
REGISTER_OP_KERNEL(pool3d, CUDNN, ::paddle::platform::CUDAPlace, REGISTER_OP_KERNEL(pool3d, CUDNN, plat::CUDAPlace,
ops::PoolCUDNNOpKernel<float>, ops::PoolCUDNNOpKernel<float>,
ops::PoolCUDNNOpKernel<double>); ops::PoolCUDNNOpKernel<double>);
REGISTER_OP_KERNEL(pool3d_grad, CUDNN, ::paddle::platform::CUDAPlace, REGISTER_OP_KERNEL(pool3d_grad, CUDNN, plat::CUDAPlace,
ops::PoolCUDNNGradOpKernel<float>, ops::PoolCUDNNGradOpKernel<float>,
ops::PoolCUDNNGradOpKernel<double>); ops::PoolCUDNNGradOpKernel<double>);
...@@ -124,11 +124,15 @@ framework::OpKernelType PoolOpGrad::GetExpectedKernelType( ...@@ -124,11 +124,15 @@ framework::OpKernelType PoolOpGrad::GetExpectedKernelType(
} }
#endif #endif
auto input_data_type = framework::ToDataType(ctx.Input<Tensor>("X")->type());
if (input_data_type == framework::proto::VarType::FP16) {
PADDLE_ENFORCE_EQ(library_, framework::LibraryType::kCUDNN,
"float16 can only be used when CUDNN is used");
}
std::string data_format = ctx.Attr<std::string>("data_format"); std::string data_format = ctx.Attr<std::string>("data_format");
framework::DataLayout layout_ = framework::StringToDataLayout(data_format); framework::DataLayout layout_ = framework::StringToDataLayout(data_format);
return framework::OpKernelType( return framework::OpKernelType(input_data_type, ctx.GetPlace(), layout_,
framework::ToDataType(ctx.Input<Tensor>("X")->type()), ctx.GetPlace(), library_);
layout_, library_);
} }
Pool2dOpMaker::Pool2dOpMaker(OpProto *proto, OpAttrChecker *op_checker) Pool2dOpMaker::Pool2dOpMaker(OpProto *proto, OpAttrChecker *op_checker)
......
...@@ -483,9 +483,9 @@ class OpTest(unittest.TestCase): ...@@ -483,9 +483,9 @@ class OpTest(unittest.TestCase):
input: input numpy array input: input numpy array
Returns: Returns:
input: if the dtype of input is np.float16, its dtype will be input: The dtype of input will be changed to np.uint16 if
changed to np.uint16 so that the internal memory will be it is originally np.float16, such that the internal memory
reinterpreted input as of dtype np.uint16. of input will be reinterpreted as of dtype np.uint16.
""" """
if input.dtype == np.float16: if input.dtype == np.float16:
input.dtype = np.uint16 input.dtype = np.uint16
......
...@@ -65,10 +65,10 @@ class TestConv2dOp(OpTest): ...@@ -65,10 +65,10 @@ class TestConv2dOp(OpTest):
def setUp(self): def setUp(self):
self.use_cudnn = False self.use_cudnn = False
self.use_mkldnn = False self.use_mkldnn = False
self.dtype = np.float32
self.init_op_type() self.init_op_type()
self.init_group() self.init_group()
self.init_dilation() self.init_dilation()
self.init_data_type()
self.init_test_case() self.init_test_case()
conv2d_param = { conv2d_param = {
...@@ -159,9 +159,6 @@ class TestConv2dOp(OpTest): ...@@ -159,9 +159,6 @@ class TestConv2dOp(OpTest):
f_c = self.input_size[1] / self.groups f_c = self.input_size[1] / self.groups
self.filter_size = [6, f_c, 3, 3] self.filter_size = [6, f_c, 3, 3]
def init_data_type(self):
self.dtype = np.float32
def init_dilation(self): def init_dilation(self):
self.dilations = [1, 1] self.dilations = [1, 1]
...@@ -246,8 +243,10 @@ class TestCUDNN(TestConv2dOp): ...@@ -246,8 +243,10 @@ class TestCUDNN(TestConv2dOp):
self.op_type = "conv2d" self.op_type = "conv2d"
class TestFP16CUDNN(TestCUDNN): class TestFP16CUDNN(TestConv2dOp):
def init_data_type(self): def init_op_type(self):
self.use_cudnn = True
self.op_type = "conv2d"
self.dtype = np.float16 self.dtype = np.float16
def test_check_output(self): def test_check_output(self):
...@@ -263,8 +262,10 @@ class TestCUDNNWithPad(TestWithPad): ...@@ -263,8 +262,10 @@ class TestCUDNNWithPad(TestWithPad):
self.op_type = "conv2d" self.op_type = "conv2d"
class TestFP16CUDNNWithPad(TestCUDNNWithPad): class TestFP16CUDNNWithPad(TestWithPad):
def init_data_type(self): def init_op_type(self):
self.use_cudnn = True
self.op_type = "conv2d"
self.dtype = np.float16 self.dtype = np.float16
def test_check_output(self): def test_check_output(self):
...@@ -280,8 +281,10 @@ class TestCUDNNWithStride(TestWithStride): ...@@ -280,8 +281,10 @@ class TestCUDNNWithStride(TestWithStride):
self.op_type = "conv2d" self.op_type = "conv2d"
class TestFP16CUDNNWithStride(TestCUDNNWithStride): class TestFP16CUDNNWithStride(TestWithStride):
def init_data_type(self): def init_op_type(self):
self.use_cudnn = True
self.op_type = "conv2d"
self.dtype = np.float16 self.dtype = np.float16
def test_check_output(self): def test_check_output(self):
...@@ -297,8 +300,10 @@ class TestCUDNNWithGroup(TestWithGroup): ...@@ -297,8 +300,10 @@ class TestCUDNNWithGroup(TestWithGroup):
self.op_type = "conv2d" self.op_type = "conv2d"
class TestFP16CUDNNWithGroup(TestCUDNNWithGroup): class TestFP16CUDNNWithGroup(TestWithGroup):
def init_data_type(self): def init_op_type(self):
self.use_cudnn = True
self.op_type = "conv2d"
self.dtype = np.float16 self.dtype = np.float16
def test_check_output(self): def test_check_output(self):
...@@ -314,8 +319,10 @@ class TestCUDNNWith1x1(TestWith1x1): ...@@ -314,8 +319,10 @@ class TestCUDNNWith1x1(TestWith1x1):
self.op_type = "conv2d" self.op_type = "conv2d"
class TestFP16CUDNNWith1x1(TestCUDNNWith1x1): class TestFP16CUDNNWith1x1(TestWith1x1):
def init_data_type(self): def init_op_type(self):
self.use_cudnn = True
self.op_type = "conv2d"
self.dtype = np.float16 self.dtype = np.float16
def test_check_output(self): def test_check_output(self):
...@@ -331,8 +338,10 @@ class TestCUDNNWithInput1x1Filter1x1(TestWithInput1x1Filter1x1): ...@@ -331,8 +338,10 @@ class TestCUDNNWithInput1x1Filter1x1(TestWithInput1x1Filter1x1):
self.op_type = "conv2d" self.op_type = "conv2d"
class TestFP16CUDNNWithInput1x1Filter1x1(TestCUDNNWithInput1x1Filter1x1): class TestFP16CUDNNWithInput1x1Filter1x1(TestWithInput1x1Filter1x1):
def init_data_type(self): def init_op_type(self):
self.use_cudnn = True
self.op_type = "conv2d"
self.dtype = np.float16 self.dtype = np.float16
def test_check_output(self): def test_check_output(self):
......
...@@ -80,6 +80,7 @@ class TestPool2d_Op(OpTest): ...@@ -80,6 +80,7 @@ class TestPool2d_Op(OpTest):
def setUp(self): def setUp(self):
self.use_cudnn = False self.use_cudnn = False
self.use_mkldnn = False self.use_mkldnn = False
self.dtype = np.float32
self.init_test_case() self.init_test_case()
self.init_global_pool() self.init_global_pool()
self.init_op_type() self.init_op_type()
...@@ -87,11 +88,11 @@ class TestPool2d_Op(OpTest): ...@@ -87,11 +88,11 @@ class TestPool2d_Op(OpTest):
self.init_ceil_mode() self.init_ceil_mode()
if self.global_pool: if self.global_pool:
self.paddings = [0 for _ in range(len(self.paddings))] self.paddings = [0 for _ in range(len(self.paddings))]
input = np.random.random(self.shape).astype("float32") input = np.random.random(self.shape).astype(self.dtype)
output = self.pool2D_forward_naive(input, self.ksize, self.strides, output = self.pool2D_forward_naive(input, self.ksize, self.strides,
self.paddings, self.global_pool, self.paddings, self.global_pool,
self.ceil_mode).astype("float32") self.ceil_mode).astype(self.dtype)
self.inputs = {'X': input} self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)}
self.attrs = { self.attrs = {
'strides': self.strides, 'strides': self.strides,
...@@ -105,7 +106,7 @@ class TestPool2d_Op(OpTest): ...@@ -105,7 +106,7 @@ class TestPool2d_Op(OpTest):
'data_format': 'AnyLayout' # TODO(dzhwinter) : should be fix latter 'data_format': 'AnyLayout' # TODO(dzhwinter) : should be fix latter
} }
self.outputs = {'Out': output.astype('float32')} self.outputs = {'Out': output}
def test_check_output(self): def test_check_output(self):
if self.use_cudnn: if self.use_cudnn:
...@@ -115,6 +116,8 @@ class TestPool2d_Op(OpTest): ...@@ -115,6 +116,8 @@ class TestPool2d_Op(OpTest):
self.check_output() self.check_output()
def test_check_grad(self): def test_check_grad(self):
if self.dtype == np.float16:
return
if self.use_cudnn and self.pool_type != "max": if self.use_cudnn and self.pool_type != "max":
place = core.CUDAPlace(0) place = core.CUDAPlace(0)
self.check_grad_with_place( self.check_grad_with_place(
...@@ -212,36 +215,114 @@ class TestCUDNNCase1(TestPool2d_Op): ...@@ -212,36 +215,114 @@ class TestCUDNNCase1(TestPool2d_Op):
self.op_type = "pool2d" self.op_type = "pool2d"
class TestFP16CUDNNCase1(TestPool2d_Op):
def init_op_type(self):
self.use_cudnn = True
self.op_type = "pool2d"
self.dtype = np.float16
def test_check_output(self):
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
if core.is_float16_supported(place):
self.check_output_with_place(place, atol=1e-3)
class TestCUDNNCase2(TestCase1): class TestCUDNNCase2(TestCase1):
def init_op_type(self): def init_op_type(self):
self.use_cudnn = True self.use_cudnn = True
self.op_type = "pool2d" self.op_type = "pool2d"
class TestFP16CUDNNCase2(TestCase1):
def init_op_type(self):
self.use_cudnn = True
self.op_type = "pool2d"
self.dtype = np.float16
def test_check_output(self):
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
if core.is_float16_supported(place):
self.check_output_with_place(place, atol=1e-3)
class TestCUDNNCase3(TestCase2): class TestCUDNNCase3(TestCase2):
def init_op_type(self): def init_op_type(self):
self.use_cudnn = True self.use_cudnn = True
self.op_type = "pool2d" self.op_type = "pool2d"
class TestFP16CUDNNCase3(TestCase2):
def init_op_type(self):
self.use_cudnn = True
self.op_type = "pool2d"
self.dtype = np.float16
def test_check_output(self):
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
if core.is_float16_supported(place):
self.check_output_with_place(place, atol=1e-3)
class TestCUDNNCase4(TestCase3): class TestCUDNNCase4(TestCase3):
def init_op_type(self): def init_op_type(self):
self.use_cudnn = True self.use_cudnn = True
self.op_type = "pool2d" self.op_type = "pool2d"
class TestFP16CUDNNCase4(TestCase3):
def init_op_type(self):
self.use_cudnn = True
self.op_type = "pool2d"
self.dtype = np.float16
def test_check_output(self):
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
if core.is_float16_supported(place):
self.check_output_with_place(place, atol=1e-3)
class TestCUDNNCase5(TestCase4): class TestCUDNNCase5(TestCase4):
def init_op_type(self): def init_op_type(self):
self.use_cudnn = True self.use_cudnn = True
self.op_type = "pool2d" self.op_type = "pool2d"
class TestFP16CUDNNCase5(TestCase4):
def init_op_type(self):
self.use_cudnn = True
self.op_type = "pool2d"
self.dtype = np.float16
def test_check_output(self):
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
if core.is_float16_supported(place):
self.check_output_with_place(place, atol=1e-3)
class TestCUDNNCase6(TestCase5): class TestCUDNNCase6(TestCase5):
def init_op_type(self): def init_op_type(self):
self.use_cudnn = True self.use_cudnn = True
self.op_type = "pool2d" self.op_type = "pool2d"
class TestFP16CUDNNCase6(TestCase5):
def init_op_type(self):
self.use_cudnn = True
self.op_type = "pool2d"
self.dtype = np.float16
def test_check_output(self):
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
if core.is_float16_supported(place):
self.check_output_with_place(place, atol=1e-3)
class TestCeilModeCase1(TestCUDNNCase1): class TestCeilModeCase1(TestCUDNNCase1):
def init_ceil_mode(self): def init_ceil_mode(self):
self.ceil_mode = True self.ceil_mode = True
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册