Skip to content

Commit f086ebb

Browse files
author
chengduo
authored
Merge pull request PaddlePaddle#7536 from chengduoZH/feature/refine_conv_pool_python
Exposing use_cudnn
2 parents 4b3e22b + edd2132 commit f086ebb

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed

paddle/operators/conv_op.cc

+15
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ void ConvOp::InferShape(framework::InferShapeContext* ctx) const {
7070
framework::OpKernelType ConvOp::GetExpectedKernelType(
7171
const framework::ExecutionContext& ctx) const {
7272
bool use_cudnn = ctx.Attr<bool>("use_cudnn");
73+
use_cudnn &= platform::is_gpu_place(ctx.GetPlace());
74+
#ifdef PADDLE_WITH_CUDA
75+
if (platform::is_gpu_place(ctx.GetPlace())) {
76+
auto& dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();
77+
use_cudnn &= dev_ctx.cudnn_handle() != nullptr;
78+
}
79+
#endif
7380
framework::LibraryType library_;
7481
if (use_cudnn) {
7582
library_ = framework::LibraryType::kCUDNN;
@@ -283,6 +290,14 @@ void ConvOpGrad::InferShape(framework::InferShapeContext* ctx) const {
283290
framework::OpKernelType ConvOpGrad::GetExpectedKernelType(
284291
const framework::ExecutionContext& ctx) const {
285292
bool use_cudnn = ctx.Attr<bool>("use_cudnn");
293+
use_cudnn &= platform::is_gpu_place(ctx.GetPlace());
294+
#ifdef PADDLE_WITH_CUDA
295+
if (platform::is_gpu_place(ctx.GetPlace())) {
296+
auto& dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();
297+
use_cudnn &= dev_ctx.cudnn_handle() != nullptr;
298+
}
299+
#endif
300+
286301
framework::LibraryType library_;
287302
if (use_cudnn) {
288303
library_ = framework::LibraryType::kCUDNN;

paddle/operators/conv_transpose_op.cc

+14
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ void ConvTransposeOp::InferShape(framework::InferShapeContext* ctx) const {
6161
framework::OpKernelType ConvTransposeOp::GetExpectedKernelType(
6262
const framework::ExecutionContext& ctx) const {
6363
bool use_cudnn = ctx.Attr<bool>("use_cudnn");
64+
use_cudnn &= platform::is_gpu_place(ctx.GetPlace());
65+
#ifdef PADDLE_WITH_CUDA
66+
if (platform::is_gpu_place(ctx.GetPlace())) {
67+
auto& dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();
68+
use_cudnn &= dev_ctx.cudnn_handle() != nullptr;
69+
}
70+
#endif
6471
framework::LibraryType library_;
6572
if (use_cudnn) {
6673
library_ = framework::LibraryType::kCUDNN;
@@ -263,6 +270,13 @@ void ConvTransposeOpGrad::InferShape(framework::InferShapeContext* ctx) const {
263270
framework::OpKernelType ConvTransposeOpGrad::GetExpectedKernelType(
264271
const framework::ExecutionContext& ctx) const {
265272
bool use_cudnn = ctx.Attr<bool>("use_cudnn");
273+
use_cudnn &= platform::is_gpu_place(ctx.GetPlace());
274+
#ifdef PADDLE_WITH_CUDA
275+
if (platform::is_gpu_place(ctx.GetPlace())) {
276+
auto& dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();
277+
use_cudnn &= dev_ctx.cudnn_handle() != nullptr;
278+
}
279+
#endif
266280
framework::LibraryType library_;
267281
if (use_cudnn) {
268282
library_ = framework::LibraryType::kCUDNN;

paddle/operators/pool_op.cc

+14
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ void PoolOp::InferShape(framework::InferShapeContext *ctx) const {
6464
framework::OpKernelType PoolOp::GetExpectedKernelType(
6565
const framework::ExecutionContext &ctx) const {
6666
bool use_cudnn = ctx.Attr<bool>("use_cudnn");
67+
use_cudnn &= platform::is_gpu_place(ctx.GetPlace());
68+
#ifdef PADDLE_WITH_CUDA
69+
if (platform::is_gpu_place(ctx.GetPlace())) {
70+
auto &dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();
71+
use_cudnn &= dev_ctx.cudnn_handle() != nullptr;
72+
}
73+
#endif
6774
framework::LibraryType library_;
6875
if (use_cudnn) {
6976
library_ = framework::LibraryType::kCUDNN;
@@ -88,6 +95,13 @@ void PoolOpGrad::InferShape(framework::InferShapeContext *ctx) const {
8895
framework::OpKernelType PoolOpGrad::GetExpectedKernelType(
8996
const framework::ExecutionContext &ctx) const {
9097
bool use_cudnn = ctx.Attr<bool>("use_cudnn");
98+
use_cudnn &= platform::is_gpu_place(ctx.GetPlace());
99+
#ifdef PADDLE_WITH_CUDA
100+
if (platform::is_gpu_place(ctx.GetPlace())) {
101+
auto &dev_ctx = ctx.template device_context<platform::CUDADeviceContext>();
102+
use_cudnn &= dev_ctx.cudnn_handle() != nullptr;
103+
}
104+
#endif
91105
framework::LibraryType library_;
92106
if (use_cudnn) {
93107
library_ = framework::LibraryType::kCUDNN;

python/paddle/v2/fluid/layers/nn.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ def conv2d(input,
676676
groups=None,
677677
param_attr=None,
678678
bias_attr=None,
679+
use_cudnn=True,
679680
act=None):
680681
"""
681682
**Convlution2D Layer**
@@ -739,6 +740,8 @@ def conv2d(input,
739740
connected to the second half of the input channels. Default: groups=1
740741
param_attr(ParamAttr): The parameters to the Conv2d Layer. Default: None
741742
bias_attr(ParamAttr): Bias parameter for the Conv2d layer. Default: None
743+
use_cudnn(bool): Use cudnn kernel or not, it is valid only when the cudnn
744+
library is installed. Default: True
742745
act(str): Activation type. Default: None
743746
744747
Returns:
@@ -774,6 +777,8 @@ def conv2d(input,
774777
stride = [stride, stride]
775778
if isinstance(padding, int):
776779
padding = [padding, padding]
780+
if not isinstance(use_cudnn, bool):
781+
raise ValueError("use_cudnn should be True or False")
777782

778783
input_shape = input.shape
779784
filter_shape = [num_filters, num_filter_channels] + filter_size
@@ -797,9 +802,12 @@ def _get_default_param_initializer():
797802
'Filter': filter_param,
798803
},
799804
outputs={"Output": pre_bias},
800-
attrs={'strides': stride,
801-
'paddings': padding,
802-
'groups': groups})
805+
attrs={
806+
'strides': stride,
807+
'paddings': padding,
808+
'groups': groups,
809+
'use_cudnn': use_cudnn
810+
})
803811

804812
pre_act = helper.append_bias_op(pre_bias, dim_start=1, dim_end=2)
805813

@@ -948,6 +956,7 @@ def pool2d(input,
948956
pool_stride=None,
949957
pool_padding=None,
950958
global_pooling=False,
959+
use_cudnn=True,
951960
name=None):
952961
"""
953962
This function adds the operator for pooling in 2 dimensions, using the
@@ -967,6 +976,8 @@ def pool2d(input,
967976
pool_stride = [pool_stride, pool_stride]
968977
if isinstance(pool_padding, int):
969978
pool_padding = [pool_padding, pool_padding]
979+
if not isinstance(use_cudnn, bool):
980+
raise ValueError("use_cudnn should be True or False")
970981

971982
helper = LayerHelper('pool2d', **locals())
972983
dtype = helper.input_dtype()
@@ -981,7 +992,8 @@ def pool2d(input,
981992
"ksize": pool_size,
982993
"global_pooling": global_pooling,
983994
"strides": pool_stride,
984-
"paddings": pool_padding
995+
"paddings": pool_padding,
996+
"use_cudnn": use_cudnn
985997
})
986998

987999
return pool_out
@@ -1096,6 +1108,7 @@ def conv2d_transpose(input,
10961108
stride=None,
10971109
dilation=None,
10981110
param_attr=None,
1111+
use_cudnn=True,
10991112
name=None):
11001113
"""
11011114
The transpose of conv2d layer.
@@ -1123,6 +1136,8 @@ def conv2d_transpose(input,
11231136
contain two integers, (dilation_H, dilation_W). Otherwise, the
11241137
dilation_H = dilation_W = dilation.
11251138
param_attr: Parameter Attribute.
1139+
use_cudnn(bool): Use cudnn kernel or not, it is valid only when the cudnn
1140+
library is installed. Default: True
11261141
name(str|None): A name for this layer(optional). If set None, the layer
11271142
will be named automatically.
11281143
@@ -1151,6 +1166,10 @@ def conv2d_transpose(input,
11511166
elif dilation is not None:
11521167
op_attr['dilations'] = dilation
11531168

1169+
if not isinstance(use_cudnn, bool):
1170+
raise ValueError("use_cudnn should be True or False")
1171+
op_attr['use_cudnn'] = use_cudnn
1172+
11541173
if filter_size is None:
11551174
if output_size is None:
11561175
raise ValueError("output_size must be set when filter_size is None")

python/paddle/v2/fluid/nets.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ def simple_img_conv_pool(input,
2828
pool_stride,
2929
act,
3030
param_attr=None,
31-
pool_type='max'):
31+
pool_type='max',
32+
use_cudnn=True):
3233
conv_out = layers.conv2d(
3334
input=input,
3435
num_filters=num_filters,
3536
filter_size=filter_size,
3637
param_attr=param_attr,
37-
act=act)
38+
act=act,
39+
use_cudnn=use_cudnn)
3840

3941
pool_out = layers.pool2d(
4042
input=conv_out,
4143
pool_size=pool_size,
4244
pool_type=pool_type,
43-
pool_stride=pool_stride)
45+
pool_stride=pool_stride,
46+
use_cudnn=use_cudnn)
4447
return pool_out
4548

4649

@@ -54,7 +57,8 @@ def img_conv_group(input,
5457
conv_with_batchnorm=False,
5558
conv_batchnorm_drop_rate=None,
5659
pool_stride=1,
57-
pool_type=None):
60+
pool_type=None,
61+
use_cudnn=True):
5862
"""
5963
Image Convolution Group, Used for vgg net.
6064
"""
@@ -85,7 +89,8 @@ def __extend_list__(obj):
8589
filter_size=conv_filter_size[i],
8690
padding=conv_padding[i],
8791
param_attr=param_attr[i],
88-
act=local_conv_act)
92+
act=local_conv_act,
93+
use_cudnn=use_cudnn)
8994

9095
if conv_with_batchnorm[i]:
9196
tmp = layers.batch_norm(input=tmp, act=conv_act)
@@ -97,7 +102,8 @@ def __extend_list__(obj):
97102
input=tmp,
98103
pool_size=pool_size,
99104
pool_type=pool_type,
100-
pool_stride=pool_stride)
105+
pool_stride=pool_stride,
106+
use_cudnn=use_cudnn)
101107
return pool_out
102108

103109

0 commit comments

Comments
 (0)