From 090c974e4bb66cb8b9d2d5b96ba0b6746f1ad8ab Mon Sep 17 00:00:00 2001 From: wangyang59 Date: Fri, 24 Feb 2017 10:37:13 -0800 Subject: [PATCH] completed implementation of cudnn_convt convTransProjection and convTransOperator --- python/paddle/trainer/config_parser.py | 27 +++- .../paddle/trainer_config_helpers/layers.py | 7 +- .../protostr/img_trans_layers.protostr | 4 + .../configs/protostr/projections.protostr | 132 ++++++++++++++++-- 4 files changed, 148 insertions(+), 22 deletions(-) diff --git a/python/paddle/trainer/config_parser.py b/python/paddle/trainer/config_parser.py index 41873b63763..e9f5d53678c 100644 --- a/python/paddle/trainer/config_parser.py +++ b/python/paddle/trainer/config_parser.py @@ -726,7 +726,7 @@ class ConvProjection(ConvBaseProjection): **xargs): super(ConvProjection, self).__init__(input_layer_name, **xargs) - parse_conv(conv_conf, input_layer_name, self.proj_conf.conv_conf, + parse_conv(conv_conf, self.input_layer_name, self.proj_conf.conv_conf, num_filters) self.proj_conf.output_size = self.proj_conf.conv_conf.output_x * \ self.proj_conf.conv_conf.output_y * \ @@ -746,7 +746,7 @@ class ConvTransProjection(ConvBaseProjection): parse_conv( conv_conf, - input_layer_name, + self.input_layer_name, self.proj_conf.conv_conf, num_filters, trans=True) @@ -1834,8 +1834,17 @@ class ConvTransLayerBase(LayerBase): use_gpu = int(g_command_config_args.get("use_gpu", 0)) parallel_nn = int(g_command_config_args.get("parallel_nn", 0)) - # cudnn_convt has not been implemented so use exconvt only - self.layer_type = "exconvt" + # Automatically select cudnn_type for GPU and exconvt for CPU + # if set type=exconvt, but still reserve the way user specify + # exconvt or cudnn_convt manually. + if self.layer_type == "cudnn_convt": + config_assert(use_gpu, "cudnn_convt only support GPU") + + if (use_gpu == 1 and self.layer_type != "exconvt" and + (parallel_nn == 0 or self.config.device > -1)): + self.layer_type = "cudnn_convt" + else: + self.layer_type = "exconvt" # need to specify layer in config self.config.type = self.layer_type @@ -1852,10 +1861,9 @@ class ConvTransLayerBase(LayerBase): trans=True) conv_conf = self.config.inputs[input_index].conv_conf psize = self.calc_parameter_size(conv_conf) - print("output size for %s is %d " % (name, conv_conf.output_x)) self.create_input_parameter(input_index, psize) - self.set_layer_size( - (conv_conf.img_size**2) * self.config.num_filters) + self.set_cnn_layer(name, conv_conf.img_size_y, conv_conf.img_size, + self.config.num_filters) psize = self.config.size if shared_biases: @@ -1872,6 +1880,11 @@ class ConvTransLayer(ConvTransLayerBase): layer_type = 'exconvt' +@config_layer('cudnn_convt') +class ConvTransLayer(ConvTransLayerBase): + layer_type = 'cudnn_convt' + + @config_layer('norm') class NormLayer(LayerBase): def __init__(self, name, inputs, **xargs): diff --git a/python/paddle/trainer_config_helpers/layers.py b/python/paddle/trainer_config_helpers/layers.py index 9066778c60b..6b0a8d56cdf 100755 --- a/python/paddle/trainer_config_helpers/layers.py +++ b/python/paddle/trainer_config_helpers/layers.py @@ -2046,8 +2046,9 @@ def img_conv_layer(input, :param trans: true if it is a convTransLayer, false if it is a convLayer :type trans: bool :param layer_type: specify the layer_type, default is None. If trans=True, - layer_type has to be "exconvt", otherwise layer_type - has to be either "exconv" or "cudnn_conv" + layer_type has to be "exconvt" or "cudnn_convt", + otherwise layer_type has to be either "exconv" or + "cudnn_conv" :type layer_type: String :return: LayerOutput object. :rtype: LayerOutput @@ -2087,7 +2088,7 @@ def img_conv_layer(input, if layer_type: if trans: - assert layer_type in ["exconvt"] + assert layer_type in ["exconvt", "cudnn_convt"] else: assert layer_type in ["exconv", "cudnn_conv"] lt = layer_type diff --git a/python/paddle/trainer_config_helpers/tests/configs/protostr/img_trans_layers.protostr b/python/paddle/trainer_config_helpers/tests/configs/protostr/img_trans_layers.protostr index 6934fd0da62..2818389b16c 100644 --- a/python/paddle/trainer_config_helpers/tests/configs/protostr/img_trans_layers.protostr +++ b/python/paddle/trainer_config_helpers/tests/configs/protostr/img_trans_layers.protostr @@ -33,6 +33,8 @@ layers { bias_parameter_name: "___conv_0__.wbias" num_filters: 64 shared_biases: true + height: 256 + width: 256 } layers { name: "__batch_norm_0__" @@ -58,6 +60,8 @@ layers { } bias_parameter_name: "___batch_norm_0__.wbias" moving_average_fraction: 0.9 + height: 256 + width: 256 } layers { name: "__crmnorm_0__" diff --git a/python/paddle/trainer_config_helpers/tests/configs/protostr/projections.protostr b/python/paddle/trainer_config_helpers/tests/configs/protostr/projections.protostr index 2943ab130bd..fed37900436 100644 --- a/python/paddle/trainer_config_helpers/tests/configs/protostr/projections.protostr +++ b/python/paddle/trainer_config_helpers/tests/configs/protostr/projections.protostr @@ -154,13 +154,38 @@ layers { inputs { input_layer_name: "img" } + inputs { + input_layer_name: "img" + proj_conf { + type: "conv" + name: "___mixed_6__.w1" + input_size: 1024 + output_size: 57600 + conv_conf { + filter_size: 3 + channels: 1 + stride: 1 + padding: 0 + groups: 1 + filter_channels: 1 + output_x: 30 + img_size: 32 + caffe_mode: true + filter_size_y: 3 + padding_y: 0 + stride_y: 1 + output_y: 30 + img_size_y: 32 + } + } + } inputs { input_layer_name: "filter" } operator_confs { type: "conv" input_indices: 0 - input_indices: 1 + input_indices: 2 input_sizes: 1024 input_sizes: 576 output_size: 57600 @@ -186,38 +211,110 @@ layers { layers { name: "__mixed_7__" type: "mixed" + size: 254016 + active_type: "" + inputs { + input_layer_name: "img" + } + inputs { + input_layer_name: "img" + proj_conf { + type: "convt" + name: "___mixed_7__.w1" + input_size: 1024 + output_size: 254016 + conv_conf { + filter_size: 3 + channels: 1 + stride: 2 + padding: 1 + groups: 1 + filter_channels: 64 + output_x: 32 + img_size: 63 + caffe_mode: true + filter_size_y: 3 + padding_y: 1 + stride_y: 2 + output_y: 32 + img_size_y: 63 + } + } + } + inputs { + input_layer_name: "filter" + } + operator_confs { + type: "convt" + input_indices: 0 + input_indices: 2 + input_sizes: 1024 + input_sizes: 576 + output_size: 254016 + conv_conf { + filter_size: 3 + channels: 1 + stride: 2 + padding: 1 + groups: 1 + filter_channels: 64 + output_x: 32 + img_size: 63 + caffe_mode: true + filter_size_y: 3 + padding_y: 1 + stride_y: 2 + output_y: 32 + img_size_y: 63 + } + num_filters: 64 + } +} +layers { + name: "__mixed_8__" + type: "mixed" size: 100 active_type: "" inputs { input_layer_name: "__mixed_4__" - input_parameter_name: "___mixed_7__.w0" + input_parameter_name: "___mixed_8__.w0" proj_conf { type: "fc" - name: "___mixed_7__.w0" + name: "___mixed_8__.w0" input_size: 300 output_size: 100 } } inputs { input_layer_name: "__mixed_5__" - input_parameter_name: "___mixed_7__.w1" + input_parameter_name: "___mixed_8__.w1" proj_conf { type: "trans_fc" - name: "___mixed_7__.w1" + name: "___mixed_8__.w1" input_size: 100 output_size: 100 } } inputs { input_layer_name: "__mixed_6__" - input_parameter_name: "___mixed_7__.w2" + input_parameter_name: "___mixed_8__.w2" proj_conf { type: "fc" - name: "___mixed_7__.w2" + name: "___mixed_8__.w2" input_size: 57600 output_size: 100 } } + inputs { + input_layer_name: "__mixed_7__" + input_parameter_name: "___mixed_8__.w3" + proj_conf { + type: "fc" + name: "___mixed_8__.w3" + input_size: 254016 + output_size: 100 + } + } drop_rate: 0.5 } parameters { @@ -281,7 +378,7 @@ parameters { initial_smart: true } parameters { - name: "___mixed_7__.w0" + name: "___mixed_8__.w0" size: 30000 initial_mean: 0.0 initial_std: 0.057735026919 @@ -291,7 +388,7 @@ parameters { initial_smart: true } parameters { - name: "___mixed_7__.w1" + name: "___mixed_8__.w1" size: 10000 initial_mean: 0.0 initial_std: 0.1 @@ -301,7 +398,7 @@ parameters { initial_smart: true } parameters { - name: "___mixed_7__.w2" + name: "___mixed_8__.w2" size: 5760000 initial_mean: 0.0 initial_std: 0.00416666666667 @@ -310,10 +407,20 @@ parameters { initial_strategy: 0 initial_smart: true } +parameters { + name: "___mixed_8__.w3" + size: 25401600 + initial_mean: 0.0 + initial_std: 0.00198412698413 + dims: 254016 + dims: 100 + initial_strategy: 0 + initial_smart: true +} input_layer_names: "test" input_layer_names: "img" input_layer_names: "filter" -output_layer_names: "__mixed_7__" +output_layer_names: "__mixed_8__" sub_models { name: "root" layer_names: "test" @@ -328,10 +435,11 @@ sub_models { layer_names: "filter" layer_names: "__mixed_6__" layer_names: "__mixed_7__" + layer_names: "__mixed_8__" input_layer_names: "test" input_layer_names: "img" input_layer_names: "filter" - output_layer_names: "__mixed_7__" + output_layer_names: "__mixed_8__" is_recurrent_layer_group: false } -- GitLab