-   Notifications  You must be signed in to change notification settings 
- Fork 5.9k
enable VGG with MKLDNN layers #4310
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
   Merged  
     Merged  
 Changes from all commits
 Commits 
  Show all changes 
  6 commits   Select commit Hold shift + click to select a range 
 ed27a3b  add some log info 
  tensor-tang 0f7c407  add vgg and script for mkldnn benchmark 
  tensor-tang f96d31d  only link iomp when with MKLDNN and MKLML 
  tensor-tang d72f636  add KMP setting and default test vgg19 
  tensor-tang bea39f6  add compare simple net of mkldnn and cpu 
  tensor-tang 86a9434  rename script and modify comments 
  tensor-tang File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
There are no files selected for viewing
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| set -e | ||
|  | ||
| unset OMP_NUM_THREADS MKL_NUM_THREADS | ||
| export OMP_DYNAMIC="FALSE" | ||
| export KMP_AFFINITY="granularity=fine,compact,0,0" | ||
|  | ||
| function train() { | ||
| topology=$1 | ||
| bs=$2 | ||
| use_mkldnn=$3 | ||
| if [ $3 == "True" ]; then | ||
| use_mkldnn=$3 | ||
| thread=1 | ||
| log="logs/${topology}-mkldnn-${bs}.log" | ||
| elif [ $3 == "False" ]; then | ||
| use_mkldnn=$3 | ||
| thread=`nproc` | ||
| log="logs/${topology}-${thread}mklml-${bs}.log" | ||
| else | ||
| echo "Wrong input $3, use True or False." | ||
| fi | ||
| args="batch_size=${bs}" | ||
| config="${topology}.py" | ||
| paddle train --job=time \ | ||
| --config=$config \ | ||
| --use_mkldnn=$use_mkldnn \ | ||
| --use_gpu=False \ | ||
| --trainer_count=$thread \ | ||
| --log_period=10 \ | ||
| --test_period=100 \ | ||
| --config_args=$args \ | ||
| 2>&1 | tee ${log} | ||
| } | ||
|  | ||
| if [ ! -d "train.list" ]; then | ||
| echo " " > train.list | ||
| fi | ||
| if [ ! -d "logs" ]; then | ||
| mkdir logs | ||
| fi | ||
|  | ||
| #========= mkldnn =========# | ||
| # vgg | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 43行也是多余的。 | ||
| train vgg 64 True | ||
| train vgg 128 True | ||
| train vgg 256 True | ||
|  | ||
| #========== mklml ===========# | ||
| train vgg 64 False | ||
| train vgg 128 False | ||
| train vgg 256 False | ||
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #!/usr/bin/env python | ||
| from paddle.trainer_config_helpers import * | ||
|  | ||
| height = 224 | ||
| width = 224 | ||
| num_class = 1000 | ||
| batch_size = get_config_arg('batch_size', int, 64) | ||
| layer_num = get_config_arg('layer_num', int, 19) | ||
|  | ||
| args = {'height': height, 'width': width, 'color': True, 'num_class': num_class} | ||
| define_py_data_sources2( | ||
| "train.list", None, module="provider", obj="process", args=args) | ||
|  | ||
| settings( | ||
| batch_size=batch_size, | ||
| learning_rate=0.01 / batch_size, | ||
| learning_method=MomentumOptimizer(0.9), | ||
| regularization=L2Regularization(0.0005 * batch_size)) | ||
|  | ||
| img = data_layer(name='image', size=height * width * 3) | ||
|  | ||
|  | ||
| def vgg_network(vgg_num=3): | ||
| tmp = img_conv_group( | ||
| input=img, | ||
| num_channels=3, | ||
| conv_padding=1, | ||
| conv_num_filter=[64, 64], | ||
| conv_filter_size=3, | ||
| conv_act=ReluActivation(), | ||
| pool_size=2, | ||
| pool_stride=2, | ||
| pool_type=MaxPooling()) | ||
|  | ||
| tmp = img_conv_group( | ||
| input=tmp, | ||
| conv_num_filter=[128, 128], | ||
| conv_padding=1, | ||
| conv_filter_size=3, | ||
| conv_act=ReluActivation(), | ||
| pool_stride=2, | ||
| pool_type=MaxPooling(), | ||
| pool_size=2) | ||
|  | ||
| channels = [] | ||
| for i in range(vgg_num): | ||
| channels.append(256) | ||
| tmp = img_conv_group( | ||
| input=tmp, | ||
| conv_num_filter=channels, | ||
| conv_padding=1, | ||
| conv_filter_size=3, | ||
| conv_act=ReluActivation(), | ||
| pool_stride=2, | ||
| pool_type=MaxPooling(), | ||
| pool_size=2) | ||
| channels = [] | ||
| for i in range(vgg_num): | ||
| channels.append(512) | ||
| tmp = img_conv_group( | ||
| input=tmp, | ||
| conv_num_filter=channels, | ||
| conv_padding=1, | ||
| conv_filter_size=3, | ||
| conv_act=ReluActivation(), | ||
| pool_stride=2, | ||
| pool_type=MaxPooling(), | ||
| pool_size=2) | ||
| tmp = img_conv_group( | ||
| input=tmp, | ||
| conv_num_filter=channels, | ||
| conv_padding=1, | ||
| conv_filter_size=3, | ||
| conv_act=ReluActivation(), | ||
| pool_stride=2, | ||
| pool_type=MaxPooling(), | ||
| pool_size=2) | ||
|  | ||
| tmp = fc_layer( | ||
| input=tmp, | ||
| size=4096, | ||
| act=ReluActivation(), | ||
| layer_attr=ExtraAttr(drop_rate=0.5)) | ||
|  | ||
| tmp = fc_layer( | ||
| input=tmp, | ||
| size=4096, | ||
| act=ReluActivation(), | ||
| layer_attr=ExtraAttr(drop_rate=0.5)) | ||
|  | ||
| return fc_layer(input=tmp, size=num_class, act=SoftmaxActivation()) | ||
|  | ||
|  | ||
| if layer_num == 16: | ||
| vgg = vgg_network(3) | ||
| elif layer_num == 19: | ||
| vgg = vgg_network(4) | ||
| else: | ||
| print("Wrong layer number.") | ||
|  | ||
| lab = data_layer('label', num_class) | ||
| loss = cross_entropy(input=vgg, label=lab) | ||
| outputs(loss) | 
   This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
        This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -37,6 +37,19 @@ add_test(NAME test_CompareTwoNets | |
| --config_file_a=trainer/tests/sample_trainer_config_qb_rnn.conf --config_file_b=trainer/tests/sample_trainer_config_rnn.conf | ||
| WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle/) | ||
|  | ||
| ################ test_CompareMKLDNNandCPU ###################### | ||
| if(WITH_MKLDNN) | ||
| add_unittest_without_exec(test_CompareMKLDNNandCPU | ||
| test_CompareTwoNets.cpp) | ||
| add_test(NAME test_CompareMKLDNNandCPU | ||
| COMMAND ${PADDLE_SOURCE_DIR}/paddle/.set_python_path.sh -d ${PADDLE_SOURCE_DIR}/python/ | ||
| ${CMAKE_CURRENT_BINARY_DIR}/test_CompareMKLDNNandCPU | ||
| --config_file_a=trainer/tests/sample_trainer_config_simple_net.conf --use_mkldnn_a=True | ||
| --config_file_b=trainer/tests/sample_trainer_config_simple_net.conf --use_mkldnn_b=False | ||
| --use_gpu=False | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 49行的 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是因为目前使用对比的时候是不希望使用gpu的。 | ||
| WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}/paddle/) | ||
| endif() | ||
|  | ||
| ############### test_CompareTwoOpts ################### | ||
| add_unittest_without_exec(test_CompareTwoOpts | ||
| test_CompareTwoOpts.cpp) | ||
|  | ||
   63 changes: 63 additions & 0 deletions  63   paddle/trainer/tests/sample_trainer_config_simple_net.conf          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright (c) 2017 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. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|  | ||
| from paddle.trainer_config_helpers import * | ||
|  | ||
| ################################### Data Configuration ################################### | ||
| TrainData(ProtoData(files = "trainer/tests/mnist.list")) | ||
| ################################### Algorithm Configuration ################################### | ||
| settings(batch_size = 1000, | ||
| learning_method = MomentumOptimizer(momentum=0.5, sparse=False)) | ||
| ################################### Network Configuration ################################### | ||
| data = data_layer(name ="input", size=784) | ||
|  | ||
| tmp = img_conv_layer(input=data, | ||
| num_channels=1, | ||
| filter_size=3, | ||
| num_filters=32, | ||
| padding=1, | ||
| shared_biases=True, | ||
| act=ReluActivation()) | ||
|  | ||
| tmp = img_pool_layer(input=tmp, | ||
| pool_size=3, | ||
| stride=2, | ||
| padding=1, | ||
| pool_type=AvgPooling()) | ||
|  | ||
| tmp = img_conv_layer(input=tmp, | ||
| filter_size=3, | ||
| num_filters=64, | ||
| padding=1, | ||
| shared_biases=True, | ||
| act=ReluActivation()) | ||
|  | ||
| tmp = img_pool_layer(input=tmp, | ||
| pool_size=3, | ||
| stride=2, | ||
| padding=1, | ||
| pool_type=MaxPooling()) | ||
|  | ||
| tmp = fc_layer(input=tmp, size=64, | ||
| bias_attr=True, | ||
| act=ReluActivation()) | ||
|  | ||
| output = fc_layer(input=tmp, size=10, | ||
| bias_attr=True, | ||
| act=SoftmaxActivation()) | ||
|  | ||
| lbl = data_layer(name ="label", size=10) | ||
|  | ||
| cost = classification_cost(input=output, label=lbl) | ||
| outputs(cost) | 
  Oops, something went wrong.  
  Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
12行和16行是多余的。