提交 19735cd5 编写于 作者: D dangqingqing

Merge branch 'develop' of https://github.com/baidu/Paddle into config_parse_bug_fix

...@@ -7,18 +7,14 @@ ...@@ -7,18 +7,14 @@
hooks: hooks:
- id: yapf - id: yapf
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
sha: 4ef03c4223ad322c7adaa6c6c0efb26b57df3b71 sha: 7539d8bd1a00a3c1bfd34cdb606d3a6372e83469
hooks: hooks:
- id: check-added-large-files - id: check-added-large-files
- id: check-merge-conflict - id: check-merge-conflict
- id: check-symlinks - id: check-symlinks
- id: detect-private-key - id: detect-private-key
- id: end-of-file-fixer - id: end-of-file-fixer
# TODO(yuyang): trailing whitespace has some bugs on markdown - repo: https://github.com/PaddlePaddle/clang-format-pre-commit-hook.git
# files now, please not add it to pre-commit hook now sha: 28c0ea8a67a3e2dbbf4822ef44e85b63a0080a29
# - id: trailing-whitespace hooks:
# - id: clang-formater
# TODO(yuyang): debug-statements not fit for Paddle, because
# not all of our python code is runnable. Some are used for
# documenation
# - id: debug-statements
# PaddlePaddle # PaddlePaddle
[![Build Status](https://travis-ci.org/baidu/Paddle.svg?branch=master)](https://travis-ci.org/baidu/Paddle) [![Build Status](https://travis-ci.org/PaddlePaddle/Paddle.svg?branch=develop)](https://travis-ci.org/PaddlePaddle/Paddle)
[![Coverage Status](https://coveralls.io/repos/github/baidu/Paddle/badge.svg?branch=develop)](https://coveralls.io/github/baidu/Paddle?branch=develop) [![Documentation Status](https://img.shields.io/badge/docs-latest-brightgreen.svg?style=flat)](http://www.paddlepaddle.org/)
[![Join the chat at https://gitter.im/PaddlePaddle/Deep_Learning](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/PaddlePaddle/Deep_Learning?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Documentation Status](https://img.shields.io/badge/中文文档-最新-brightgreen.svg)](http://www.paddlepaddle.org/cn/index.html)
[![License](https://img.shields.io/badge/license-Apache%202.0-green.svg)](LICENSE) [![Coverage Status](https://coveralls.io/repos/github/PaddlePaddle/Paddle/badge.svg?branch=develop)](https://coveralls.io/github/PaddlePaddle/Paddle?branch=develop)
[![Release](https://img.shields.io/github/release/PaddlePaddle/Paddle.svg)](https://github.com/PaddlePaddle/Paddle/releases)
[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](LICENSE)
Welcome to the PaddlePaddle GitHub. Welcome to the PaddlePaddle GitHub.
...@@ -14,7 +17,7 @@ developed by Baidu scientists and engineers for the purpose of applying deep ...@@ -14,7 +17,7 @@ developed by Baidu scientists and engineers for the purpose of applying deep
learning to many products at Baidu. learning to many products at Baidu.
Our vision is to enable deep learning for everyone via PaddlePaddle. Our vision is to enable deep learning for everyone via PaddlePaddle.
Please refer to our [release announcement](https://github.com/baidu/Paddle/releases) to track the latest feature of PaddlePaddle. Please refer to our [release announcement](https://github.com/PaddlePaddle/Paddle/releases) to track the latest feature of PaddlePaddle.
## Features ## Features
...@@ -89,7 +92,7 @@ Both [English Docs](http://paddlepaddle.org/doc/) and [Chinese Docs](http://padd ...@@ -89,7 +92,7 @@ Both [English Docs](http://paddlepaddle.org/doc/) and [Chinese Docs](http://padd
## Ask Questions ## Ask Questions
You are welcome to submit questions and bug reports as [Github Issues](https://github.com/baidu/paddle/issues). You are welcome to submit questions and bug reports as [Github Issues](https://github.com/PaddlePaddle/Paddle/issues).
## Copyright and License ## Copyright and License
PaddlePaddle is provided under the [Apache-2.0 license](LICENSE). PaddlePaddle is provided under the [Apache-2.0 license](LICENSE).
...@@ -17,24 +17,15 @@ import os ...@@ -17,24 +17,15 @@ import os
from optparse import OptionParser from optparse import OptionParser
def extract_dict_features(pair_file, feature_file, src_dict_file, def extract_dict_features(pair_file, feature_file):
tgt_dict_file):
src_dict = set() with open(pair_file) as fin, open(feature_file, 'w') as feature_out:
tgt_dict = set()
with open(pair_file) as fin, open(feature_file, 'w') as feature_out, open(
src_dict_file, 'w') as src_dict_out, open(tgt_dict_file,
'w') as tgt_dict_out:
for line in fin: for line in fin:
sentence, labels = line.strip().split('\t') sentence, predicate, labels = line.strip().split('\t')
sentence_list = sentence.split() sentence_list = sentence.split()
labels_list = labels.split() labels_list = labels.split()
src_dict.update(sentence_list)
tgt_dict.update(labels_list)
verb_index = labels_list.index('B-V') verb_index = labels_list.index('B-V')
verb_feature = sentence_list[verb_index]
mark = [0] * len(labels_list) mark = [0] * len(labels_list)
if verb_index > 0: if verb_index > 0:
...@@ -42,47 +33,50 @@ def extract_dict_features(pair_file, feature_file, src_dict_file, ...@@ -42,47 +33,50 @@ def extract_dict_features(pair_file, feature_file, src_dict_file,
ctx_n1 = sentence_list[verb_index - 1] ctx_n1 = sentence_list[verb_index - 1]
else: else:
ctx_n1 = 'bos' ctx_n1 = 'bos'
ctx_n1_feature = ctx_n1
if verb_index > 1:
mark[verb_index - 2] = 1
ctx_n2 = sentence_list[verb_index - 2]
else:
ctx_n2 = 'bos'
mark[verb_index] = 1 mark[verb_index] = 1
ctx_0_feature = sentence_list[verb_index] ctx_0 = sentence_list[verb_index]
if verb_index < len(labels_list) - 2: if verb_index < len(labels_list) - 2:
mark[verb_index + 1] = 1 mark[verb_index + 1] = 1
ctx_p1 = sentence_list[verb_index + 1] ctx_p1 = sentence_list[verb_index + 1]
else: else:
ctx_p1 = 'eos' ctx_p1 = 'eos'
ctx_p1_feature = ctx_p1
if verb_index < len(labels_list) - 3:
mark[verb_index + 2] = 1
ctx_p2 = sentence_list[verb_index + 2]
else:
ctx_p2 = 'eos'
feature_str = sentence + '\t' \ feature_str = sentence + '\t' \
+ verb_feature + '\t' \ + predicate + '\t' \
+ ctx_n1_feature + '\t' \ + ctx_n2 + '\t' \
+ ctx_0_feature + '\t' \ + ctx_n1 + '\t' \
+ ctx_p1_feature + '\t' \ + ctx_0 + '\t' \
+ ctx_p1 + '\t' \
+ ctx_p2 + '\t' \
+ ' '.join([str(i) for i in mark]) + '\t' \ + ' '.join([str(i) for i in mark]) + '\t' \
+ labels + labels
feature_out.write(feature_str + '\n') feature_out.write(feature_str + '\n')
src_dict_out.write('<unk>\n')
src_dict_out.write('\n'.join(list(src_dict)))
tgt_dict_out.write('\n'.join(list(tgt_dict)))
if __name__ == '__main__': if __name__ == '__main__':
usage = '-p pair_file -f feature_file -s source dictionary -t target dictionary ' usage = '-p pair_file -f feature_file'
parser = OptionParser(usage) parser = OptionParser(usage)
parser.add_option('-p', dest='pair_file', help='the pair file') parser.add_option('-p', dest='pair_file', help='the pair file')
parser.add_option( parser.add_option('-f', dest='feature_file', help='the feature file')
'-f', dest='feature_file', help='the file to store feature')
parser.add_option(
'-s', dest='src_dict', help='the file to store source dictionary')
parser.add_option(
'-t', dest='tgt_dict', help='the file to store target dictionary')
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
extract_dict_features(options.pair_file, options.feature_file, extract_dict_features(options.pair_file, options.feature_file)
options.src_dict, options.tgt_dict)
...@@ -51,7 +51,7 @@ def read_sentences(words_file): ...@@ -51,7 +51,7 @@ def read_sentences(words_file):
for line in fin: for line in fin:
line = line.strip() line = line.strip()
if line == '': if line == '':
sentences.append(s.lower()) sentences.append(s)
s = '' s = ''
else: else:
s += line + ' ' s += line + ' '
...@@ -64,6 +64,11 @@ def transform_labels(sentences, labels): ...@@ -64,6 +64,11 @@ def transform_labels(sentences, labels):
if len(labels[i]) == 1: if len(labels[i]) == 1:
continue continue
else: else:
verb_list = []
for x in labels[i][0]:
if x !='-':
verb_list.append(x)
for j in xrange(1, len(labels[i])): for j in xrange(1, len(labels[i])):
label_list = labels[i][j] label_list = labels[i][j]
current_tag = 'O' current_tag = 'O'
...@@ -88,8 +93,7 @@ def transform_labels(sentences, labels): ...@@ -88,8 +93,7 @@ def transform_labels(sentences, labels):
is_in_bracket = True is_in_bracket = True
else: else:
print 'error:', ll print 'error:', ll
sen_lab_pair.append((sentences[i], verb_list[j-1], label_seq))
sen_lab_pair.append((sentences[i], label_seq))
return sen_lab_pair return sen_lab_pair
...@@ -97,9 +101,9 @@ def write_file(sen_lab_pair, output_file): ...@@ -97,9 +101,9 @@ def write_file(sen_lab_pair, output_file):
with open(output_file, 'w') as fout: with open(output_file, 'w') as fout:
for x in sen_lab_pair: for x in sen_lab_pair:
sentence = x[0] sentence = x[0]
label_seq = ' '.join(x[1]) label_seq = ' '.join(x[2])
assert len(sentence.split()) == len(x[1]) assert len(sentence.split()) == len(x[2])
fout.write(sentence + '\t' + label_seq + '\n') fout.write(sentence + '\t' + x[1]+'\t' +label_seq + '\n')
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -14,6 +14,10 @@ ...@@ -14,6 +14,10 @@
# limitations under the License. # limitations under the License.
set -e set -e
wget http://www.cs.upc.edu/~srlconll/conll05st-tests.tar.gz wget http://www.cs.upc.edu/~srlconll/conll05st-tests.tar.gz
wget https://www.googledrive.com/host/0B7Q8d52jqeI9ejh6Q1RpMTFQT1k/semantic_role_labeling/verbDict.txt --no-check-certificate
wget https://www.googledrive.com/host/0B7Q8d52jqeI9ejh6Q1RpMTFQT1k/semantic_role_labeling/targetDict.txt --no-check-certificate
wget https://www.googledrive.com/host/0B7Q8d52jqeI9ejh6Q1RpMTFQT1k/semantic_role_labeling/wordDict.txt --no-check-certificate
wget https://www.googledrive.com/host/0B7Q8d52jqeI9ejh6Q1RpMTFQT1k/semantic_role_labeling/emb --no-check-certificate
tar -xzvf conll05st-tests.tar.gz tar -xzvf conll05st-tests.tar.gz
rm conll05st-tests.tar.gz rm conll05st-tests.tar.gz
cp ./conll05st-release/test.wsj/words/test.wsj.words.gz . cp ./conll05st-release/test.wsj/words/test.wsj.words.gz .
...@@ -22,4 +26,4 @@ gunzip test.wsj.words.gz ...@@ -22,4 +26,4 @@ gunzip test.wsj.words.gz
gunzip test.wsj.props.gz gunzip test.wsj.props.gz
python extract_pairs.py -w test.wsj.words -p test.wsj.props -o test.wsj.seq_pair python extract_pairs.py -w test.wsj.words -p test.wsj.props -o test.wsj.seq_pair
python extract_dict_feature.py -p test.wsj.seq_pair -f feature -s src.dict -t tgt.dict python extract_dict_feature.py -p test.wsj.seq_pair -f feature
...@@ -17,11 +17,15 @@ from paddle.trainer.PyDataProvider2 import * ...@@ -17,11 +17,15 @@ from paddle.trainer.PyDataProvider2 import *
UNK_IDX = 0 UNK_IDX = 0
def hook(settings, word_dict, label_dict, **kwargs): def hook(settings, word_dict, label_dict, predicate_dict, **kwargs):
settings.word_dict = word_dict settings.word_dict = word_dict
settings.label_dict = label_dict settings.label_dict = label_dict
settings.predicate_dict = predicate_dict
#all inputs are integral and sequential type #all inputs are integral and sequential type
settings.slots = [ settings.slots = [
integer_value_sequence(len(word_dict)),
integer_value_sequence(len(predicate_dict)),
integer_value_sequence(len(word_dict)), integer_value_sequence(len(word_dict)),
integer_value_sequence(len(word_dict)), integer_value_sequence(len(word_dict)),
integer_value_sequence(len(word_dict)), integer_value_sequence(len(word_dict)),
...@@ -31,27 +35,33 @@ def hook(settings, word_dict, label_dict, **kwargs): ...@@ -31,27 +35,33 @@ def hook(settings, word_dict, label_dict, **kwargs):
] ]
@provider(init_hook=hook) def get_batch_size(yeild_data):
def process(obj, file_name): return len(yeild_data[0])
@provider(init_hook=hook, should_shuffle=True, calc_batch_size=get_batch_size,
can_over_batch_size=False, cache=CacheType.CACHE_PASS_IN_MEM)
def process(settings, file_name):
with open(file_name, 'r') as fdata: with open(file_name, 'r') as fdata:
for line in fdata: for line in fdata:
sentence, predicate, ctx_n1, ctx_0, ctx_p1, mark, label = \ sentence, predicate, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2, mark, label = \
line.strip().split('\t') line.strip().split('\t')
words = sentence.split() words = sentence.split()
sen_len = len(words) sen_len = len(words)
word_slot = [obj.word_dict.get(w, UNK_IDX) for w in words] word_slot = [settings.word_dict.get(w, UNK_IDX) for w in words]
predicate_slot = [obj.word_dict.get(predicate, UNK_IDX)] * sen_len predicate_slot = [settings.predicate_dict.get(predicate)] * sen_len
ctx_n1_slot = [obj.word_dict.get(ctx_n1, UNK_IDX)] * sen_len ctx_n2_slot = [settings.word_dict.get(ctx_n2, UNK_IDX)] * sen_len
ctx_0_slot = [obj.word_dict.get(ctx_0, UNK_IDX)] * sen_len ctx_n1_slot = [settings.word_dict.get(ctx_n1, UNK_IDX)] * sen_len
ctx_p1_slot = [obj.word_dict.get(ctx_p1, UNK_IDX)] * sen_len ctx_0_slot = [settings.word_dict.get(ctx_0, UNK_IDX)] * sen_len
ctx_p1_slot = [settings.word_dict.get(ctx_p1, UNK_IDX)] * sen_len
ctx_p2_slot = [settings.word_dict.get(ctx_p2, UNK_IDX)] * sen_len
marks = mark.split() marks = mark.split()
mark_slot = [int(w) for w in marks] mark_slot = [int(w) for w in marks]
label_list = label.split() label_list = label.split()
label_slot = [obj.label_dict.get(w) for w in label_list] label_slot = [settings.label_dict.get(w) for w in label_list]
yield word_slot, predicate_slot, ctx_n2_slot, ctx_n1_slot, \
yield word_slot, predicate_slot, ctx_n1_slot, \ ctx_0_slot, ctx_p1_slot, ctx_p2_slot, mark_slot, label_slot
ctx_0_slot, ctx_p1_slot, mark_slot, label_slot
...@@ -18,8 +18,9 @@ import sys ...@@ -18,8 +18,9 @@ import sys
from paddle.trainer_config_helpers import * from paddle.trainer_config_helpers import *
#file paths #file paths
word_dict_file = './data/src.dict' word_dict_file = './data/wordDict.txt'
label_dict_file = './data/tgt.dict' label_dict_file = './data/targetDict.txt'
predicate_file= './data/verbDict.txt'
train_list_file = './data/train.list' train_list_file = './data/train.list'
test_list_file = './data/test.list' test_list_file = './data/test.list'
...@@ -30,8 +31,10 @@ if not is_predict: ...@@ -30,8 +31,10 @@ if not is_predict:
#load dictionaries #load dictionaries
word_dict = dict() word_dict = dict()
label_dict = dict() label_dict = dict()
predicate_dict = dict()
with open(word_dict_file, 'r') as f_word, \ with open(word_dict_file, 'r') as f_word, \
open(label_dict_file, 'r') as f_label: open(label_dict_file, 'r') as f_label, \
open(predicate_file, 'r') as f_pre:
for i, line in enumerate(f_word): for i, line in enumerate(f_word):
w = line.strip() w = line.strip()
word_dict[w] = i word_dict[w] = i
...@@ -40,6 +43,11 @@ if not is_predict: ...@@ -40,6 +43,11 @@ if not is_predict:
w = line.strip() w = line.strip()
label_dict[w] = i label_dict[w] = i
for i, line in enumerate(f_pre):
w = line.strip()
predicate_dict[w] = i
if is_test: if is_test:
train_list_file = None train_list_file = None
...@@ -50,91 +58,157 @@ if not is_predict: ...@@ -50,91 +58,157 @@ if not is_predict:
module='dataprovider', module='dataprovider',
obj='process', obj='process',
args={'word_dict': word_dict, args={'word_dict': word_dict,
'label_dict': label_dict}) 'label_dict': label_dict,
'predicate_dict': predicate_dict })
word_dict_len = len(word_dict) word_dict_len = len(word_dict)
label_dict_len = len(label_dict) label_dict_len = len(label_dict)
pred_len = len(predicate_dict)
else: else:
word_dict_len = get_config_arg('dict_len', int) word_dict_len = get_config_arg('dict_len', int)
label_dict_len = get_config_arg('label_len', int) label_dict_len = get_config_arg('label_len', int)
pred_len = get_config_arg('pred_len', int)
############################## Hyper-parameters ##################################
mark_dict_len = 2 mark_dict_len = 2
word_dim = 32 word_dim = 32
mark_dim = 5 mark_dim = 5
hidden_dim = 128 hidden_dim = 512
depth = 8 depth = 8
emb_lr = 1e-2
fc_lr = 1e-2
lstm_lr = 2e-2
########################### Optimizer #######################################
settings( settings(
batch_size=150, batch_size=150,
learning_method=AdamOptimizer(), learning_method=MomentumOptimizer(momentum=0),
learning_rate=1e-3, learning_rate=2e-2,
regularization=L2Regularization(8e-4), regularization=L2Regularization(8e-4),
gradient_clipping_threshold=25) is_async=False,
model_average=ModelAverage(average_window=0.5,
max_average_window=10000),
)
#6 features
####################################### network ##############################
#8 features and 1 target
word = data_layer(name='word_data', size=word_dict_len) word = data_layer(name='word_data', size=word_dict_len)
predicate = data_layer(name='verb_data', size=word_dict_len) predicate = data_layer(name='verb_data', size=pred_len)
ctx_n2 = data_layer(name='ctx_n2_data', size=word_dict_len)
ctx_n1 = data_layer(name='ctx_n1_data', size=word_dict_len) ctx_n1 = data_layer(name='ctx_n1_data', size=word_dict_len)
ctx_0 = data_layer(name='ctx_0_data', size=word_dict_len) ctx_0 = data_layer(name='ctx_0_data', size=word_dict_len)
ctx_p1 = data_layer(name='ctx_p1_data', size=word_dict_len) ctx_p1 = data_layer(name='ctx_p1_data', size=word_dict_len)
ctx_p2 = data_layer(name='ctx_p2_data', size=word_dict_len)
mark = data_layer(name='mark_data', size=mark_dict_len) mark = data_layer(name='mark_data', size=mark_dict_len)
if not is_predict: if not is_predict:
target = data_layer(name='target', size=label_dict_len) target = data_layer(name='target', size=label_dict_len)
ptt = ParameterAttribute(name='src_emb', learning_rate=emb_lr)
layer_attr = ExtraLayerAttribute(drop_rate=0.5)
fc_para_attr = ParameterAttribute(learning_rate=fc_lr)
lstm_para_attr = ParameterAttribute(initial_std=0., learning_rate=lstm_lr)
para_attr = [fc_para_attr, lstm_para_attr]
word_embedding = embedding_layer(size=word_dim, input=word, param_attr=ptt) default_std=1/math.sqrt(hidden_dim)/3.0
predicate_embedding = embedding_layer(
size=word_dim, input=predicate, param_attr=ptt) emb_para = ParameterAttribute(name='emb', initial_std=0., learning_rate=0.)
ctx_n1_embedding = embedding_layer(size=word_dim, input=ctx_n1, param_attr=ptt) std_0 = ParameterAttribute(initial_std=0.)
ctx_0_embedding = embedding_layer(size=word_dim, input=ctx_0, param_attr=ptt) std_default = ParameterAttribute(initial_std=default_std)
ctx_p1_embedding = embedding_layer(size=word_dim, input=ctx_p1, param_attr=ptt)
mark_embedding = embedding_layer(size=mark_dim, input=mark) predicate_embedding = embedding_layer(size=word_dim, input=predicate, param_attr=ParameterAttribute(name='vemb',initial_std=default_std))
mark_embedding = embedding_layer(name='word_ctx-in_embedding', size=mark_dim, input=mark, param_attr=std_0)
word_input=[word, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2]
emb_layers = [embedding_layer(size=word_dim, input=x, param_attr=emb_para) for x in word_input]
emb_layers.append(predicate_embedding)
emb_layers.append(mark_embedding)
hidden_0 = mixed_layer( hidden_0 = mixed_layer(
name='hidden0',
size=hidden_dim, size=hidden_dim,
input=[ bias_attr=std_default,
full_matrix_projection(input=word_embedding), input=[ full_matrix_projection(input=emb, param_attr=std_default ) for emb in emb_layers ])
full_matrix_projection(input=predicate_embedding),
full_matrix_projection(input=ctx_n1_embedding),
full_matrix_projection(input=ctx_0_embedding),
full_matrix_projection(input=ctx_p1_embedding),
full_matrix_projection(input=mark_embedding),
])
lstm_0 = lstmemory(input=hidden_0, layer_attr=layer_attr) mix_hidden_lr = 1e-3
lstm_para_attr = ParameterAttribute(initial_std=0.0, learning_rate=1.0)
hidden_para_attr = ParameterAttribute(initial_std=default_std, learning_rate=mix_hidden_lr)
lstm_0 = lstmemory(name='lstm0',
input=hidden_0,
act=ReluActivation(),
gate_act=SigmoidActivation(),
state_act=SigmoidActivation(),
bias_attr=std_0,
param_attr=lstm_para_attr)
#stack L-LSTM and R-LSTM with direct edges #stack L-LSTM and R-LSTM with direct edges
input_tmp = [hidden_0, lstm_0] input_tmp = [hidden_0, lstm_0]
for i in range(1, depth):
fc = fc_layer(input=input_tmp, size=hidden_dim, param_attr=para_attr) for i in range(1, depth):
lstm = lstmemory( mix_hidden = mixed_layer(name='hidden'+str(i),
input=fc, size=hidden_dim,
bias_attr=std_default,
input=[full_matrix_projection(input=input_tmp[0], param_attr=hidden_para_attr),
full_matrix_projection(input=input_tmp[1], param_attr=lstm_para_attr)
]
)
lstm = lstmemory(name='lstm'+str(i),
input=mix_hidden,
act=ReluActivation(), act=ReluActivation(),
reverse=(i % 2) == 1, gate_act=SigmoidActivation(),
layer_attr=layer_attr) state_act=SigmoidActivation(),
input_tmp = [fc, lstm] reverse=((i % 2)==1),
bias_attr=std_0,
param_attr=lstm_para_attr)
prob = fc_layer( input_tmp = [mix_hidden, lstm]
input=input_tmp,
feature_out = mixed_layer(name='output',
size=label_dict_len, size=label_dict_len,
act=SoftmaxActivation(), bias_attr=std_default,
param_attr=para_attr) input=[full_matrix_projection(input=input_tmp[0], param_attr=hidden_para_attr),
full_matrix_projection(input=input_tmp[1], param_attr=lstm_para_attr)
],
)
if not is_predict: if not is_predict:
cls = classification_cost(input=prob, label=target) crf_l = crf_layer( name = 'crf',
outputs(cls) size = label_dict_len,
input = feature_out,
label = target,
param_attr=ParameterAttribute(name='crfw',initial_std=default_std, learning_rate=mix_hidden_lr)
)
crf_dec_l = crf_decoding_layer(name = 'crf_dec_l',
size = label_dict_len,
input = feature_out,
label = target,
param_attr=ParameterAttribute(name='crfw')
)
eval = sum_evaluator(input=crf_dec_l)
outputs(crf_l)
else: else:
outputs(prob) crf_dec_l = crf_decoding_layer(name = 'crf_dec_l',
size = label_dict_len,
input = feature_out,
param_attr=ParameterAttribute(name='crfw')
)
outputs(crf_dec_l)
...@@ -26,7 +26,7 @@ UNK_IDX = 0 ...@@ -26,7 +26,7 @@ UNK_IDX = 0
class Prediction(): class Prediction():
def __init__(self, train_conf, dict_file, model_dir, label_file): def __init__(self, train_conf, dict_file, model_dir, label_file, predicate_dict_file):
""" """
train_conf: trainer configure. train_conf: trainer configure.
dict_file: word dictionary file name. dict_file: word dictionary file name.
...@@ -35,26 +35,41 @@ class Prediction(): ...@@ -35,26 +35,41 @@ class Prediction():
self.dict = {} self.dict = {}
self.labels = {} self.labels = {}
self.predicate_dict={}
self.labels_reverse = {} self.labels_reverse = {}
self.load_dict_label(dict_file, label_file) self.load_dict_label(dict_file, label_file, predicate_dict_file)
len_dict = len(self.dict) len_dict = len(self.dict)
len_label = len(self.labels) len_label = len(self.labels)
len_pred = len(self.predicate_dict)
conf = parse_config(train_conf, 'dict_len=' + str(len_dict) +
',label_len=' + str(len_label) + ',is_predict=True') conf = parse_config(
train_conf,
'dict_len=' + str(len_dict) +
',label_len=' + str(len_label) +
',pred_len=' + str(len_pred) +
',is_predict=True')
self.network = swig_paddle.GradientMachine.createFromConfigProto( self.network = swig_paddle.GradientMachine.createFromConfigProto(
conf.model_config) conf.model_config)
self.network.loadParameters(model_dir) self.network.loadParameters(model_dir)
slots = [ slots = [
integer_value_sequence(len_dict),
integer_value_sequence(len_pred),
integer_value_sequence(len_dict),
integer_value_sequence(len_dict),
integer_value_sequence(len_dict),
integer_value_sequence(len_dict),
integer_value_sequence(len_dict),
integer_value_sequence(2)
]
integer_value_sequence(len_dict), integer_value_sequence(len_dict), integer_value_sequence(len_dict), integer_value_sequence(len_dict),
integer_value_sequence(len_dict), integer_value_sequence(len_dict), integer_value_sequence(len_dict), integer_value_sequence(len_dict),
integer_value_sequence(len_dict), integer_value_sequence(2) integer_value_sequence(len_dict), integer_value_sequence(2)
] ]
self.converter = DataProviderConverter(slots) self.converter = DataProviderConverter(slots)
def load_dict_label(self, dict_file, label_file): def load_dict_label(self, dict_file, label_file, predicate_dict_file):
""" """
Load dictionary from self.dict_file. Load dictionary from self.dict_file.
""" """
...@@ -65,39 +80,42 @@ class Prediction(): ...@@ -65,39 +80,42 @@ class Prediction():
self.labels[line.strip()] = line_count self.labels[line.strip()] = line_count
self.labels_reverse[line_count] = line.strip() self.labels_reverse[line_count] = line.strip()
for line_count, line in enumerate(open(predicate_dict_file, 'r')):
self.predicate_dict[line.strip()] = line_count
def get_data(self, data_file): def get_data(self, data_file):
""" """
Get input data of paddle format. Get input data of paddle format.
""" """
with open(data_file, 'r') as fdata: with open(data_file, 'r') as fdata:
for line in fdata: for line in fdata:
sentence, predicate, ctx_n1, ctx_0, ctx_p1, mark, label = line.strip( sentence, predicate, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2, mark, label = line.strip(
).split('\t') ).split('\t')
words = sentence.split() words = sentence.split()
sen_len = len(words) sen_len = len(words)
word_slot = [self.dict.get(w, UNK_IDX) for w in words] word_slot = [self.dict.get(w, UNK_IDX) for w in words]
predicate_slot = [self.dict.get(predicate, UNK_IDX)] * sen_len predicate_slot = [self.predicate_dict.get(predicate, UNK_IDX)] * sen_len
ctx_n2_slot = [self.dict.get(ctx_n2, UNK_IDX)] * sen_len
ctx_n1_slot = [self.dict.get(ctx_n1, UNK_IDX)] * sen_len ctx_n1_slot = [self.dict.get(ctx_n1, UNK_IDX)] * sen_len
ctx_0_slot = [self.dict.get(ctx_0, UNK_IDX)] * sen_len ctx_0_slot = [self.dict.get(ctx_0, UNK_IDX)] * sen_len
ctx_p1_slot = [self.dict.get(ctx_p1, UNK_IDX)] * sen_len ctx_p1_slot = [self.dict.get(ctx_p1, UNK_IDX)] * sen_len
ctx_p2_slot = [self.dict.get(ctx_p2, UNK_IDX)] * sen_len
marks = mark.split() marks = mark.split()
mark_slot = [int(w) for w in marks] mark_slot = [int(w) for w in marks]
yield word_slot, predicate_slot, ctx_n1_slot, \ yield word_slot, predicate_slot, ctx_n2_slot, ctx_n1_slot, \
ctx_0_slot, ctx_p1_slot, mark_slot ctx_0_slot, ctx_p1_slot, ctx_p2_slot, mark_slot
def predict(self, data_file): def predict(self, data_file, output_file):
""" """
data_file: file name of input data. data_file: file name of input data.
""" """
input = self.converter(self.get_data(data_file)) input = self.converter(self.get_data(data_file))
output = self.network.forwardTest(input) output = self.network.forwardTest(input)
prob = output[0]["value"] lab = output[0]["id"].tolist()
lab = list(np.argsort(-prob)[:, 0])
with open(data_file, 'r') as fin, open('predict.res', 'w') as fout: with open(data_file, 'r') as fin, open(output_file, 'w') as fout:
index = 0 index = 0
for line in fin: for line in fin:
sen = line.split('\t')[0] sen = line.split('\t')[0]
...@@ -110,7 +128,7 @@ class Prediction(): ...@@ -110,7 +128,7 @@ class Prediction():
def option_parser(): def option_parser():
usage = ("python predict.py -c config -w model_dir " usage = ("python predict.py -c config -w model_dir "
"-d word dictionary -l label_file -i input_file") "-d word dictionary -l label_file -i input_file -p pred_dict_file")
parser = OptionParser(usage="usage: %s [options]" % usage) parser = OptionParser(usage="usage: %s [options]" % usage)
parser.add_option( parser.add_option(
"-c", "-c",
...@@ -131,6 +149,13 @@ def option_parser(): ...@@ -131,6 +149,13 @@ def option_parser():
dest="label_file", dest="label_file",
default=None, default=None,
help="label file") help="label file")
parser.add_option(
"-p",
"--predict_dict_file",
action="store",
dest="predict_dict_file",
default=None,
help="predict_dict_file")
parser.add_option( parser.add_option(
"-i", "-i",
"--data", "--data",
...@@ -144,6 +169,14 @@ def option_parser(): ...@@ -144,6 +169,14 @@ def option_parser():
dest="model_path", dest="model_path",
default=None, default=None,
help="model path") help="model path")
parser.add_option(
"-o",
"--output_file",
action="store",
dest="output_file",
default=None,
help="output file")
return parser.parse_args() return parser.parse_args()
...@@ -154,10 +187,12 @@ def main(): ...@@ -154,10 +187,12 @@ def main():
dict_file = options.dict_file dict_file = options.dict_file
model_path = options.model_path model_path = options.model_path
label_file = options.label_file label_file = options.label_file
predict_dict_file = options.predict_dict_file
output_file = options.output_file
swig_paddle.initPaddle("--use_gpu=0") swig_paddle.initPaddle("--use_gpu=0")
predict = Prediction(train_conf, dict_file, model_path, label_file) predict = Prediction(train_conf, dict_file, model_path, label_file, predict_dict_file)
predict.predict(data_file) predict.predict(data_file,output_file)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -26,15 +26,18 @@ LOG=`get_best_pass $log` ...@@ -26,15 +26,18 @@ LOG=`get_best_pass $log`
LOG=(${LOG}) LOG=(${LOG})
best_model_path="output/pass-${LOG[1]}" best_model_path="output/pass-${LOG[1]}"
config_file=db_lstm.py config_file=db_lstm.py
dict_file=./data/src.dict dict_file=./data/wordDict.txt
label_file=./data/tgt.dict label_file=./data/targetDict.txt
predicate_dict_file=./data/verbDict.txt
input_file=./data/feature input_file=./data/feature
output_file=predict.res
python predict.py \ python predict.py \
-c $config_file \ -c $config_file \
-w $best_model_path \ -w $best_model_path \
-l $label_file \ -l $label_file \
-p $predicate_dict_file \
-d $dict_file \ -d $dict_file \
-i $input_file -i $input_file \
-o $output_file
...@@ -36,4 +36,5 @@ paddle train \ ...@@ -36,4 +36,5 @@ paddle train \
--job=test \ --job=test \
--use_gpu=false \ --use_gpu=false \
--config_args=is_test=1 \ --config_args=is_test=1 \
--test_all_data_in_one_period=1 \
2>&1 | tee 'test.log' 2>&1 | tee 'test.log'
...@@ -16,11 +16,14 @@ ...@@ -16,11 +16,14 @@
set -e set -e
paddle train \ paddle train \
--config=./db_lstm.py \ --config=./db_lstm.py \
--use_gpu=0 \
--log_period=5000 \
--trainer_count=1 \
--show_parameter_stats_period=5000 \
--save_dir=./output \ --save_dir=./output \
--trainer_count=4 \ --num_passes=10000 \
--log_period=10 \ --average_test_period=10000000 \
--num_passes=500 \ --init_model_path=./data \
--use_gpu=false \ --load_missing_parameter_strategy=rand \
--show_parameter_stats_period=10 \
--test_all_data_in_one_period=1 \ --test_all_data_in_one_period=1 \
2>&1 | tee 'train.log' 2>&1 | tee 'train.log'
...@@ -30,8 +30,6 @@ Several new files appear in the `data `directory as follows. ...@@ -30,8 +30,6 @@ Several new files appear in the `data `directory as follows.
conll05st-release:the test data set of CoNll-2005 shared task conll05st-release:the test data set of CoNll-2005 shared task
test.wsj.words:the Wall Street Journal data sentences test.wsj.words:the Wall Street Journal data sentences
test.wsj.props: the propositional arguments test.wsj.props: the propositional arguments
src.dict:the dictionary of words in sentences
tgt.dict:the labels dictionary
feature: the extracted features from data set feature: the extracted features from data set
``` ```
...@@ -67,6 +65,8 @@ def hook(settings, word_dict, label_dict, **kwargs): ...@@ -67,6 +65,8 @@ def hook(settings, word_dict, label_dict, **kwargs):
settings.label_dict = label_dict settings.label_dict = label_dict
#all inputs are integral and sequential type #all inputs are integral and sequential type
settings.slots = [ settings.slots = [
integer_value_sequence(len(word_dict)),
integer_value_sequence(len(predicate_dict)),
integer_value_sequence(len(word_dict)), integer_value_sequence(len(word_dict)),
integer_value_sequence(len(word_dict)), integer_value_sequence(len(word_dict)),
integer_value_sequence(len(word_dict)), integer_value_sequence(len(word_dict)),
...@@ -77,34 +77,39 @@ def hook(settings, word_dict, label_dict, **kwargs): ...@@ -77,34 +77,39 @@ def hook(settings, word_dict, label_dict, **kwargs):
``` ```
The corresponding data iterator is as following: The corresponding data iterator is as following:
``` ```
@provider(use_seq=True, init_hook=hook) @provider(init_hook=hook, should_shuffle=True, calc_batch_size=get_batch_size,
def process(obj, file_name): can_over_batch_size=False, cache=CacheType.CACHE_PASS_IN_MEM)
def process(settings, file_name):
with open(file_name, 'r') as fdata: with open(file_name, 'r') as fdata:
for line in fdata: for line in fdata:
sentence, predicate, ctx_n1, ctx_0, ctx_p1, mark, label = line.strip().split('\t') sentence, predicate, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2, mark, label = \
line.strip().split('\t')
words = sentence.split() words = sentence.split()
sen_len = len(words) sen_len = len(words)
word_slot = [obj.word_dict.get(w, UNK_IDX) for w in words] word_slot = [settings.word_dict.get(w, UNK_IDX) for w in words]
predicate_slot = [obj.word_dict.get(predicate, UNK_IDX)] * sen_len predicate_slot = [settings.predicate_dict.get(predicate)] * sen_len
ctx_n1_slot = [obj.word_dict.get(ctx_n1, UNK_IDX) ] * sen_len ctx_n2_slot = [settings.word_dict.get(ctx_n2, UNK_IDX)] * sen_len
ctx_0_slot = [obj.word_dict.get(ctx_0, UNK_IDX) ] * sen_len ctx_n1_slot = [settings.word_dict.get(ctx_n1, UNK_IDX)] * sen_len
ctx_p1_slot = [obj.word_dict.get(ctx_p1, UNK_IDX) ] * sen_len ctx_0_slot = [settings.word_dict.get(ctx_0, UNK_IDX)] * sen_len
ctx_p1_slot = [settings.word_dict.get(ctx_p1, UNK_IDX)] * sen_len
ctx_p2_slot = [settings.word_dict.get(ctx_p2, UNK_IDX)] * sen_len
marks = mark.split() marks = mark.split()
mark_slot = [int(w) for w in marks] mark_slot = [int(w) for w in marks]
label_list = label.split() label_list = label.split()
label_slot = [obj.label_dict.get(w) for w in label_list] label_slot = [settings.label_dict.get(w) for w in label_list]
yield word_slot, predicate_slot, ctx_n2_slot, ctx_n1_slot, \
yield word_slot, predicate_slot, ctx_n1_slot, ctx_0_slot, ctx_p1_slot, mark_slot, label_slot ctx_0_slot, ctx_p1_slot, ctx_p2_slot, mark_slot, label_slot
``` ```
The `process`function yield 7 lists which are six features and labels. The `process`function yield 9 lists which are 8 features and label.
### Neural Network Config ### Neural Network Config
`db_lstm.py` is the neural network config file to load the dictionaries and define the data provider module and network architecture during the training procedure. `db_lstm.py` is the neural network config file to load the dictionaries and define the data provider module and network architecture during the training procedure.
Seven `data_layer` load instances from data provider. Six features are transformed into embedddings respectively, and mixed by `mixed_layer` . Deep bidirectional LSTM layers extract features for the softmax layer. The objective function is cross entropy of labels. Nine `data_layer` load instances from data provider. Eight features are transformed into embedddings respectively, and mixed by `mixed_layer` . Deep bidirectional LSTM layers extract features for the softmax layer. The objective function is cross entropy of labels.
### Run Training ### Run Training
The script for training is `train.sh`, user just need to execute: The script for training is `train.sh`, user just need to execute:
...@@ -115,27 +120,36 @@ The content in `train.sh`: ...@@ -115,27 +120,36 @@ The content in `train.sh`:
``` ```
paddle train \ paddle train \
--config=./db_lstm.py \ --config=./db_lstm.py \
--use_gpu=0 \
--log_period=5000 \
--trainer_count=1 \
--show_parameter_stats_period=5000 \
--save_dir=./output \ --save_dir=./output \
--trainer_count=4 \ --num_passes=10000 \
--log_period=10 \ --average_test_period=10000000 \
--num_passes=500 \ --init_model_path=./data \
--use_gpu=false \ --load_missing_parameter_strategy=rand \
--show_parameter_stats_period=10 \
--test_all_data_in_one_period=1 \ --test_all_data_in_one_period=1 \
2>&1 | tee 'train.log' 2>&1 | tee 'train.log'
``` ```
- \--config=./db_lstm.py : network config file. - \--config=./db_lstm.py : network config file.
- \--save_di=./output: output path to save models. - \--use_gpu=false: use CPU to train, set true, if you install GPU version of PaddlePaddle and want to use GPU to train, until now crf_layer do not support GPU
- \--trainer_count=4 : set thread number (or GPU count). - \--log_period=500: print log every 20 batches.
- \--log_period=10 : print log every 20 batches. - \--trainer_count=1: set thread number (or GPU count).
- \--num_passes=500: set pass number, one pass in PaddlePaddle means training all samples in dataset one time. - \--show_parameter_stats_period=5000: show parameter statistic every 100 batches.
- \--use_gpu=false: use CPU to train, set true, if you install GPU version of PaddlePaddle and want to use GPU to train. - \--save_dir=./output: output path to save models.
- \--show_parameter_stats_period=10: show parameter statistic every 100 batches. - \--num_passes=10000: set pass number, one pass in PaddlePaddle means training all samples in dataset one time.
- \--test_all_data_in_one_period=1: test all data in every testing. - \--average_test_period=10000000: do test on average parameter every average_test_period batches
- \--init_model_path=./data: parameter initialization path
- \--load_missing_parameter_strategy=rand: random initialization unexisted parameters
After training, the models will be saved in directory `output`. - \--test_all_data_in_one_period=1: test all data in one period
After training, the models will be saved in directory `output`. Our training curve is as following:
<center>
![pic](./curve.jpg)
</center>
### Run testing ### Run testing
The script for testing is `test.sh`, user just need to execute: The script for testing is `test.sh`, user just need to execute:
...@@ -155,6 +169,7 @@ paddle train \ ...@@ -155,6 +169,7 @@ paddle train \
- \--model_list=$model_list.list: model list file - \--model_list=$model_list.list: model list file
- \--job=test: indicate the test job - \--job=test: indicate the test job
- \--config_args=is_test=1: flag to indicate test - \--config_args=is_test=1: flag to indicate test
- \--test_all_data_in_one_period=1: test all data in 1 period
### Run prediction ### Run prediction
...@@ -166,11 +181,13 @@ The script for prediction is `predict.sh`, user just need to execute: ...@@ -166,11 +181,13 @@ The script for prediction is `predict.sh`, user just need to execute:
In `predict.sh`, user should offer the network config file, model path, label file, word dictionary file, feature file In `predict.sh`, user should offer the network config file, model path, label file, word dictionary file, feature file
``` ```
python predict.py python predict.py
-c $config_file -c $config_file \
-w $model_path -w $best_model_path \
-l $label_file -l $label_file \
-d $dict_file -p $predicate_dict_file \
-i $input_file -d $dict_file \
-i $input_file \
-o $output_file
``` ```
`predict.py` is the main executable python script, which includes functions: load model, load data, data prediction. The network model will output the probability distribution of labels. In the demo, we take the label with maximum probability as result. User can also implement the beam search or viterbi decoding upon the probability distribution matrix. `predict.py` is the main executable python script, which includes functions: load model, load data, data prediction. The network model will output the probability distribution of labels. In the demo, we take the label with maximum probability as result. User can also implement the beam search or viterbi decoding upon the probability distribution matrix.
......
...@@ -81,5 +81,8 @@ else() ...@@ -81,5 +81,8 @@ else()
add_library(paddle_cuda ${CUDA_SOURCES}) add_library(paddle_cuda ${CUDA_SOURCES})
endif() endif()
add_style_check_target(paddle_cuda ${CUDA_SOURCES}) add_style_check_target(paddle_cuda
add_style_check_target(paddle_cuda ${CUDA_HEADERS}) ${CUDA_SOURCES}
${CUDA_HEADERS}
${CUDA_DSO_SOURCES}
${CUDA_CXX_WITH_GPU_SOURCES})
...@@ -104,7 +104,7 @@ CUBLAS_BLAS_ROUTINE_EACH(DYNAMIC_LOAD_CUBLAS_V2_WRAP) ...@@ -104,7 +104,7 @@ CUBLAS_BLAS_ROUTINE_EACH(DYNAMIC_LOAD_CUBLAS_V2_WRAP)
#endif #endif
const char* hl_cublas_get_error_string(cublasStatus_t status) { const char* hl_cublas_get_error_string(cublasStatus_t status) {
switch(status) { switch (status) {
case CUBLAS_STATUS_NOT_INITIALIZED: case CUBLAS_STATUS_NOT_INITIALIZED:
return "[cublas status]: not initialized"; return "[cublas status]: not initialized";
case CUBLAS_STATUS_ALLOC_FAILED: case CUBLAS_STATUS_ALLOC_FAILED:
...@@ -181,7 +181,7 @@ void hl_matrix_inverse(real *A_d, real *C_d, int dimN, int lda, int ldc) { ...@@ -181,7 +181,7 @@ void hl_matrix_inverse(real *A_d, real *C_d, int dimN, int lda, int ldc) {
real **inout_d = (real **)hl_malloc_device(sizeof(real *)); real **inout_d = (real **)hl_malloc_device(sizeof(real *));
hl_memcpy(inout_d, inout_h, sizeof(real *)); hl_memcpy(inout_d, inout_h, sizeof(real *));
int *pivot_d = (int *)hl_malloc_device(dimN*sizeof(int)); int *pivot_d = (int *)hl_malloc_device(dimN * sizeof(int));
int *info_d = (int *)t_resource.gpu_mem; int *info_d = (int *)t_resource.gpu_mem;
/* Note: cublasSgetrfBatched is used to calculate a number of /* Note: cublasSgetrfBatched is used to calculate a number of
...@@ -189,8 +189,7 @@ void hl_matrix_inverse(real *A_d, real *C_d, int dimN, int lda, int ldc) { ...@@ -189,8 +189,7 @@ void hl_matrix_inverse(real *A_d, real *C_d, int dimN, int lda, int ldc) {
the API for better performance. the API for better performance.
*/ */
CHECK_CUBLAS(CUBLAS_GETRF(t_resource.handle, CHECK_CUBLAS(CUBLAS_GETRF(t_resource.handle,
dimN, inout_d, lda, pivot_d, dimN, inout_d, lda, pivot_d, info_d, 1));
info_d, 1));
int info_h; int info_h;
hl_memcpy(&info_h, info_d, sizeof(int)); hl_memcpy(&info_h, info_d, sizeof(int));
......
...@@ -159,13 +159,11 @@ CUDNN_DNN_ROUTINE_EACH_R5(DYNAMIC_LOAD_CUDNN_WRAP) ...@@ -159,13 +159,11 @@ CUDNN_DNN_ROUTINE_EACH_R5(DYNAMIC_LOAD_CUDNN_WRAP)
bool g_is_libcudnn_init = false; bool g_is_libcudnn_init = false;
int g_cudnn_lib_version = 0; int g_cudnn_lib_version = 0;
void hl_cudnn_desc_init(cudnnTensorDescriptor_t* cudnn_desc) void hl_cudnn_desc_init(cudnnTensorDescriptor_t* cudnn_desc) {
{
CHECK_CUDNN(dynload::cudnnCreateTensorDescriptor(cudnn_desc)); CHECK_CUDNN(dynload::cudnnCreateTensorDescriptor(cudnn_desc));
} }
void hl_cudnn_init(cudnnHandle_t *cudnn_handle, cudaStream_t stream) void hl_cudnn_init(cudnnHandle_t *cudnn_handle, cudaStream_t stream) {
{
size_t cudnn_dso_ver = dynload::cudnnGetVersion(); size_t cudnn_dso_ver = dynload::cudnnGetVersion();
size_t cudnn_dso_major = cudnn_dso_ver / 1000; size_t cudnn_dso_major = cudnn_dso_ver / 1000;
size_t cudnn_cuh_major = CUDNN_VERSION / 1000; size_t cudnn_cuh_major = CUDNN_VERSION / 1000;
...@@ -212,13 +210,18 @@ void hl_conv_workspace(hl_tensor_descriptor input, ...@@ -212,13 +210,18 @@ void hl_conv_workspace(hl_tensor_descriptor input,
CHECK_NOTNULL(conv); CHECK_NOTNULL(conv);
// Specify workspace limit directly // Specify workspace limit directly
size_t memoryLimitBytes = (1LL << 20) * FLAGS_cudnn_conv_workspace_limit_in_mb; size_t memoryLimitBytes =
(1LL << 20) * FLAGS_cudnn_conv_workspace_limit_in_mb;
// cudnn convolution forward configuration // cudnn convolution forward configuration
cudnnTensorDescriptor_t fwd_src_desc = GET_TENSOR_DESCRIPTOR(input); cudnnTensorDescriptor_t fwd_src_desc =
cudnnTensorDescriptor_t fwd_dest_desc = GET_TENSOR_DESCRIPTOR(output); GET_TENSOR_DESCRIPTOR(input);
cudnnFilterDescriptor_t fwd_filter_desc = GET_FILTER_DESCRIPTOR(filter); cudnnTensorDescriptor_t fwd_dest_desc =
cudnnConvolutionDescriptor_t fwd_conv_desc = GET_CONVOLUTION_DESCRIPTOR(conv); GET_TENSOR_DESCRIPTOR(output);
cudnnFilterDescriptor_t fwd_filter_desc =
GET_FILTER_DESCRIPTOR(filter);
cudnnConvolutionDescriptor_t fwd_conv_desc =
GET_CONVOLUTION_DESCRIPTOR(conv);
CHECK_CUDNN(dynload::cudnnGetConvolutionForwardAlgorithm( CHECK_CUDNN(dynload::cudnnGetConvolutionForwardAlgorithm(
t_resource.cudnn_handle, t_resource.cudnn_handle,
...@@ -302,8 +305,7 @@ void hl_create_tensor_descriptor(hl_tensor_descriptor* image_desc, ...@@ -302,8 +305,7 @@ void hl_create_tensor_descriptor(hl_tensor_descriptor* image_desc,
int batch_size, int batch_size,
int feature_maps, int feature_maps,
int height, int height,
int width) int width) {
{
CHECK_NOTNULL(image_desc); CHECK_NOTNULL(image_desc);
cudnn_tensor_descriptor hl_desc = cudnn_tensor_descriptor hl_desc =
...@@ -359,8 +361,7 @@ void hl_tensor_reshape(hl_tensor_descriptor image_desc, ...@@ -359,8 +361,7 @@ void hl_tensor_reshape(hl_tensor_descriptor image_desc,
int batch_size, int batch_size,
int feature_maps, int feature_maps,
int height, int height,
int width) int width) {
{
const int stride_w = 1; const int stride_w = 1;
const int stride_h = width * stride_w; const int stride_h = width * stride_w;
const int stride_c = height * stride_h; const int stride_c = height * stride_h;
...@@ -384,8 +385,7 @@ void hl_tensor_reshape(hl_tensor_descriptor image_desc, ...@@ -384,8 +385,7 @@ void hl_tensor_reshape(hl_tensor_descriptor image_desc,
int nStride, int nStride,
int cStride, int cStride,
int hStride, int hStride,
int wStride) int wStride) {
{
CHECK_NOTNULL(image_desc); CHECK_NOTNULL(image_desc);
cudnn_tensor_descriptor hl_desc = (cudnn_tensor_descriptor)image_desc; cudnn_tensor_descriptor hl_desc = (cudnn_tensor_descriptor)image_desc;
...@@ -408,8 +408,7 @@ void hl_tensor_reshape(hl_tensor_descriptor image_desc, ...@@ -408,8 +408,7 @@ void hl_tensor_reshape(hl_tensor_descriptor image_desc,
hl_desc->width = width; hl_desc->width = width;
} }
void hl_destroy_tensor_descriptor(hl_tensor_descriptor image_desc) void hl_destroy_tensor_descriptor(hl_tensor_descriptor image_desc) {
{
CHECK_NOTNULL(image_desc); CHECK_NOTNULL(image_desc);
cudnn_tensor_descriptor hl_desc = (cudnn_tensor_descriptor)image_desc; cudnn_tensor_descriptor hl_desc = (cudnn_tensor_descriptor)image_desc;
...@@ -430,11 +429,9 @@ void hl_create_pooling_descriptor(hl_pooling_descriptor* pooling_desc, ...@@ -430,11 +429,9 @@ void hl_create_pooling_descriptor(hl_pooling_descriptor* pooling_desc,
int height_padding, int height_padding,
int width_padding, int width_padding,
int stride_height, int stride_height,
int stride_width) int stride_width) {
{
cudnnPoolingMode_t cudnn_mode; cudnnPoolingMode_t cudnn_mode;
switch (mode) switch (mode) {
{
case HL_POOLING_MAX: case HL_POOLING_MAX:
cudnn_mode = CUDNN_POOLING_MAX; cudnn_mode = CUDNN_POOLING_MAX;
break; break;
...@@ -478,13 +475,13 @@ void hl_create_pooling_descriptor(hl_pooling_descriptor* pooling_desc, ...@@ -478,13 +475,13 @@ void hl_create_pooling_descriptor(hl_pooling_descriptor* pooling_desc,
*pooling_desc = (hl_pooling_descriptor)hl_pooling_desc; *pooling_desc = (hl_pooling_descriptor)hl_pooling_desc;
} }
void hl_destroy_pooling_descriptor(hl_pooling_descriptor pooling_desc) void hl_destroy_pooling_descriptor(hl_pooling_descriptor pooling_desc) {
{
CHECK_NOTNULL(pooling_desc); CHECK_NOTNULL(pooling_desc);
cudnn_pooling_descriptor hl_pooling = (cudnn_pooling_descriptor)pooling_desc; cudnn_pooling_descriptor hl_pooling =
CHECK_NOTNULL(hl_pooling->desc); (cudnn_pooling_descriptor)pooling_desc;
CHECK_NOTNULL(hl_pooling->desc);
CHECK_CUDNN(dynload::cudnnDestroyPoolingDescriptor(hl_pooling->desc)); CHECK_CUDNN(dynload::cudnnDestroyPoolingDescriptor(hl_pooling->desc));
hl_pooling->desc = NULL; hl_pooling->desc = NULL;
...@@ -496,8 +493,7 @@ void hl_pooling_forward(hl_tensor_descriptor input, ...@@ -496,8 +493,7 @@ void hl_pooling_forward(hl_tensor_descriptor input,
real* input_image, real* input_image,
hl_tensor_descriptor output, hl_tensor_descriptor output,
real* output_image, real* output_image,
hl_pooling_descriptor pooling) hl_pooling_descriptor pooling) {
{
cudnnPoolingDescriptor_t pooling_desc; cudnnPoolingDescriptor_t pooling_desc;
cudnnTensorDescriptor_t input_desc; cudnnTensorDescriptor_t input_desc;
cudnnTensorDescriptor_t output_desc; cudnnTensorDescriptor_t output_desc;
...@@ -531,8 +527,7 @@ void hl_pooling_backward(hl_tensor_descriptor input, ...@@ -531,8 +527,7 @@ void hl_pooling_backward(hl_tensor_descriptor input,
hl_tensor_descriptor output, hl_tensor_descriptor output,
real* output_image, real* output_image,
real* output_image_grad, real* output_image_grad,
hl_pooling_descriptor pooling) hl_pooling_descriptor pooling) {
{
cudnnPoolingDescriptor_t pooling_desc; cudnnPoolingDescriptor_t pooling_desc;
cudnnTensorDescriptor_t input_desc; cudnnTensorDescriptor_t input_desc;
cudnnTensorDescriptor_t output_desc; cudnnTensorDescriptor_t output_desc;
...@@ -571,8 +566,7 @@ void hl_create_filter_descriptor(hl_filter_descriptor* filter, ...@@ -571,8 +566,7 @@ void hl_create_filter_descriptor(hl_filter_descriptor* filter,
int input_feature_maps, int input_feature_maps,
int output_feature_maps, int output_feature_maps,
int height, int height,
int width) int width) {
{
CHECK_NOTNULL(filter); CHECK_NOTNULL(filter);
cudnn_filter_descriptor hl_filter = cudnn_filter_descriptor hl_filter =
...@@ -607,8 +601,7 @@ void hl_create_filter_descriptor(hl_filter_descriptor* filter, ...@@ -607,8 +601,7 @@ void hl_create_filter_descriptor(hl_filter_descriptor* filter,
} }
void hl_destroy_filter_descriptor(hl_filter_descriptor filter) void hl_destroy_filter_descriptor(hl_filter_descriptor filter) {
{
CHECK_NOTNULL(filter); CHECK_NOTNULL(filter);
cudnn_filter_descriptor hl_filter = (cudnn_filter_descriptor)filter; cudnn_filter_descriptor hl_filter = (cudnn_filter_descriptor)filter;
...@@ -627,14 +620,13 @@ void hl_create_convolution_descriptor(hl_convolution_descriptor* conv, ...@@ -627,14 +620,13 @@ void hl_create_convolution_descriptor(hl_convolution_descriptor* conv,
int padding_height, int padding_height,
int padding_width, int padding_width,
int stride_height, int stride_height,
int stride_width) int stride_width) {
{
CHECK_NOTNULL(conv); CHECK_NOTNULL(conv);
cudnn_convolution_descriptor hl_conv = cudnn_convolution_descriptor hl_conv = (cudnn_convolution_descriptor)
(cudnn_convolution_descriptor)malloc(sizeof(_cudnn_convolution_descriptor)); malloc(sizeof(_cudnn_convolution_descriptor));
CHECK_NOTNULL(hl_conv);
CHECK_NOTNULL(hl_conv);
CHECK_CUDNN(dynload::cudnnCreateConvolutionDescriptor(&hl_conv->desc)); CHECK_CUDNN(dynload::cudnnCreateConvolutionDescriptor(&hl_conv->desc));
cudnnConvolutionMode_t mode = CUDNN_CROSS_CORRELATION; cudnnConvolutionMode_t mode = CUDNN_CROSS_CORRELATION;
...@@ -667,8 +659,7 @@ void hl_reset_convolution_descriptor(hl_convolution_descriptor conv, ...@@ -667,8 +659,7 @@ void hl_reset_convolution_descriptor(hl_convolution_descriptor conv,
int padding_height, int padding_height,
int padding_width, int padding_width,
int stride_height, int stride_height,
int stride_width) int stride_width) {
{
CHECK_NOTNULL(conv); CHECK_NOTNULL(conv);
CHECK_NOTNULL(image); CHECK_NOTNULL(image);
CHECK_NOTNULL(filter); CHECK_NOTNULL(filter);
...@@ -697,8 +688,7 @@ void hl_reset_convolution_descriptor(hl_convolution_descriptor conv, ...@@ -697,8 +688,7 @@ void hl_reset_convolution_descriptor(hl_convolution_descriptor conv,
hl_conv->mode = mode; hl_conv->mode = mode;
} }
void hl_destroy_convolution_descriptor(hl_convolution_descriptor conv) void hl_destroy_convolution_descriptor(hl_convolution_descriptor conv) {
{
CHECK_NOTNULL(conv); CHECK_NOTNULL(conv);
cudnn_convolution_descriptor hl_conv = (cudnn_convolution_descriptor)conv; cudnn_convolution_descriptor hl_conv = (cudnn_convolution_descriptor)conv;
...@@ -753,8 +743,7 @@ void hl_convolution_forward(hl_tensor_descriptor input, ...@@ -753,8 +743,7 @@ void hl_convolution_forward(hl_tensor_descriptor input,
void hl_convolution_forward_add_bias(hl_tensor_descriptor bias, void hl_convolution_forward_add_bias(hl_tensor_descriptor bias,
real* bias_data, real* bias_data,
hl_tensor_descriptor output, hl_tensor_descriptor output,
real* output_data) real* output_data) {
{
CHECK_NOTNULL(bias); CHECK_NOTNULL(bias);
CHECK_NOTNULL(output); CHECK_NOTNULL(output);
CHECK_NOTNULL(bias_data); CHECK_NOTNULL(bias_data);
...@@ -782,8 +771,7 @@ void hl_convolution_forward_add_bias(hl_tensor_descriptor bias, ...@@ -782,8 +771,7 @@ void hl_convolution_forward_add_bias(hl_tensor_descriptor bias,
void hl_convolution_backward_bias(hl_tensor_descriptor bias, void hl_convolution_backward_bias(hl_tensor_descriptor bias,
real* bias_grad_data, real* bias_grad_data,
hl_tensor_descriptor output, hl_tensor_descriptor output,
real* output_grad_data) real* output_grad_data) {
{
CHECK_NOTNULL(bias); CHECK_NOTNULL(bias);
CHECK_NOTNULL(output); CHECK_NOTNULL(output);
CHECK_NOTNULL(bias_grad_data); CHECK_NOTNULL(bias_grad_data);
...@@ -814,7 +802,6 @@ void hl_convolution_backward_filter(hl_tensor_descriptor input, ...@@ -814,7 +802,6 @@ void hl_convolution_backward_filter(hl_tensor_descriptor input,
void* gpuWorkSpace, void* gpuWorkSpace,
size_t sizeInBytes, size_t sizeInBytes,
int convBwdFilterAlgo) { int convBwdFilterAlgo) {
CHECK_NOTNULL(input); CHECK_NOTNULL(input);
CHECK_NOTNULL(output); CHECK_NOTNULL(output);
CHECK_NOTNULL(filter); CHECK_NOTNULL(filter);
...@@ -889,8 +876,7 @@ void hl_convolution_backward_data(hl_tensor_descriptor input, ...@@ -889,8 +876,7 @@ void hl_convolution_backward_data(hl_tensor_descriptor input,
void hl_softmax_forward(real *input, void hl_softmax_forward(real *input,
real *output, real *output,
int height, int height,
int width) int width) {
{
#ifndef PADDLE_TYPE_DOUBLE #ifndef PADDLE_TYPE_DOUBLE
cudnnDataType_t data_type = CUDNN_DATA_FLOAT; cudnnDataType_t data_type = CUDNN_DATA_FLOAT;
#else #else
...@@ -923,8 +909,7 @@ void hl_softmax_forward(real *input, ...@@ -923,8 +909,7 @@ void hl_softmax_forward(real *input,
void hl_softmax_backward(real *output_value, void hl_softmax_backward(real *output_value,
real *output_grad, real *output_grad,
int height, int height,
int width) int width) {
{
#ifndef PADDLE_TYPE_DOUBLE #ifndef PADDLE_TYPE_DOUBLE
cudnnDataType_t data_type = CUDNN_DATA_FLOAT; cudnnDataType_t data_type = CUDNN_DATA_FLOAT;
#else #else
......
...@@ -203,7 +203,7 @@ inline pid_t gettid() { ...@@ -203,7 +203,7 @@ inline pid_t gettid() {
#endif #endif
pid_t tid = syscall(__NR_gettid); pid_t tid = syscall(__NR_gettid);
#endif #endif
CHECK_NE(tid, -1); CHECK_NE((int)tid, -1);
return tid; return tid;
} }
...@@ -355,7 +355,8 @@ void* hl_malloc_host(size_t size) { ...@@ -355,7 +355,8 @@ void* hl_malloc_host(size_t size) {
void *dest_h; void *dest_h;
CHECK(size) << __func__ << ": the size for device memory is 0, please check."; CHECK(size) << __func__ << ": the size for device memory is 0, please check.";
CHECK_CUDA(dynload::cudaHostAlloc((void**)&dest_h, size, cudaHostAllocDefault)); CHECK_CUDA(dynload::cudaHostAlloc(
(void**)&dest_h, size, cudaHostAllocDefault));
return dest_h; return dest_h;
} }
...@@ -364,7 +365,7 @@ void hl_free_mem_host(void *dest_h) { ...@@ -364,7 +365,7 @@ void hl_free_mem_host(void *dest_h) {
CHECK_NOTNULL(dest_h); CHECK_NOTNULL(dest_h);
cudaError_t err = dynload::cudaFreeHost(dest_h); cudaError_t err = dynload::cudaFreeHost(dest_h);
CHECK (cudaSuccess == err || cudaErrorCudartUnloading == err) CHECK(cudaSuccess == err || cudaErrorCudartUnloading == err)
<< hl_get_device_error_string(); << hl_get_device_error_string();
} }
...@@ -502,7 +503,8 @@ int hl_get_cuda_version() { ...@@ -502,7 +503,8 @@ int hl_get_cuda_version() {
return g_cuda_lib_version; return g_cuda_lib_version;
} }
void hl_create_thread_resources(int device, thread_device_resources device_res) { void hl_create_thread_resources(int device,
thread_device_resources device_res) {
CHECK_CUDA(dynload::cudaSetDevice(device)); CHECK_CUDA(dynload::cudaSetDevice(device));
/* create thread stream */ /* create thread stream */
......
...@@ -78,48 +78,38 @@ __host__ cudaError_t CUDARTAPI cudaLaunchKernel(const void *func, ...@@ -78,48 +78,38 @@ __host__ cudaError_t CUDARTAPI cudaLaunchKernel(const void *func,
dim3 blockDim, dim3 blockDim,
void **args, void **args,
size_t sharedMem, size_t sharedMem,
cudaStream_t stream) cudaStream_t stream) {
{ return dynload::cudaLaunchKernel(func, gridDim, blockDim,
return dynload::cudaLaunchKernel(func, gridDim, blockDim, args, sharedMem, stream); args, sharedMem, stream);
} }
#endif /* CUDART_VERSION >= 7000 */ #endif /* CUDART_VERSION >= 7000 */
__host__ cudaError_t CUDARTAPI cudaLaunch(const void *func) __host__ cudaError_t CUDARTAPI cudaLaunch(const void *func) {
{
return dynload::cudaLaunch(func); return dynload::cudaLaunch(func);
} }
__host__ cudaError_t CUDARTAPI cudaSetupArgument(const void *arg, __host__ cudaError_t CUDARTAPI cudaSetupArgument(const void *arg,
size_t size, size_t size,
size_t offset) size_t offset) {
{
return dynload::cudaSetupArgument(arg, size, offset); return dynload::cudaSetupArgument(arg, size, offset);
} }
__host__ cudaError_t CUDARTAPI cudaConfigureCall(dim3 gridDim, __host__ cudaError_t CUDARTAPI cudaConfigureCall(dim3 gridDim,
dim3 blockDim, dim3 blockDim,
size_t sharedMem, size_t sharedMem,
cudaStream_t stream) cudaStream_t stream) {
{
return dynload::cudaConfigureCall(gridDim, blockDim, return dynload::cudaConfigureCall(gridDim, blockDim,
sharedMem, stream); sharedMem, stream);
} }
extern "C" { extern "C" {
void** CUDARTAPI __cudaRegisterFatBinary( void** CUDARTAPI __cudaRegisterFatBinary(void *fatCubin) {
void *fatCubin
)
{
return dynload::__cudaRegisterFatBinary(fatCubin); return dynload::__cudaRegisterFatBinary(fatCubin);
} }
void CUDARTAPI __cudaUnregisterFatBinary( void CUDARTAPI __cudaUnregisterFatBinary(void **fatCubinHandle) {
void **fatCubinHandle
)
{
return dynload::__cudaUnregisterFatBinary(fatCubinHandle); return dynload::__cudaUnregisterFatBinary(fatCubinHandle);
} }
......
...@@ -12,27 +12,28 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ...@@ -12,27 +12,28 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "hl_dso_loader.h" #include "hl_dso_loader.h"
#include "paddle/utils/Logging.h"
#include "paddle/utils/CommandLineParser.h" #include "paddle/utils/CommandLineParser.h"
#include "paddle/utils/Logging.h"
P_DEFINE_string(cudnn_dir, "", P_DEFINE_string(cudnn_dir,
"",
"Specify path for loading libcudnn.so. For instance, " "Specify path for loading libcudnn.so. For instance, "
"/usr/local/cudnn/lib64. If empty [default], dlopen will search " "/usr/local/cudnn/lib. If empty [default], dlopen "
"cudnn from LD_LIBRARY_PATH"); "will search cudnn from LD_LIBRARY_PATH");
P_DEFINE_string(cuda_dir, "", P_DEFINE_string(cuda_dir,
"",
"Specify path for loading cuda library, such as libcublas, " "Specify path for loading cuda library, such as libcublas, "
"libcurand. For instance, /usr/local/cuda/lib64. " "libcurand. For instance, /usr/local/cuda/lib64. (Note: "
"(Note: libcudart can not be specified by cuda_dir, since some " "libcudart can not be specified by cuda_dir, since some "
"build-in function in cudart already ran before main entry). " "build-in function in cudart already ran before main entry). "
"If empty [default], dlopen will search cuda from LD_LIBRARY_PATH"); "If default, dlopen will search cuda from LD_LIBRARY_PATH");
static inline std::string join(const std::string& part1, const std::string& part2) { static inline std::string join(const std::string& part1,
const std::string& part2) {
// directory separator // directory separator
const char sep = '/'; const char sep = '/';
if (!part2.empty() && part2.front() == sep) { if (!part2.empty() && part2.front() == sep) {
return part2; return part2;
} }
...@@ -46,34 +47,37 @@ static inline std::string join(const std::string& part1, const std::string& part ...@@ -46,34 +47,37 @@ static inline std::string join(const std::string& part1, const std::string& part
return ret; return ret;
} }
static inline void GetDsoHandleFromDefaultPath( static inline void GetDsoHandleFromDefaultPath(std::string& dso_path,
std::string& dso_path, void** dso_handle, int dynload_flags) { void** dso_handle,
int dynload_flags) {
VLOG(3) << "Try to find cuda library: " << dso_path VLOG(3) << "Try to find cuda library: " << dso_path
<< " from default system path."; << " from default system path.";
// default search from LD_LIBRARY_PATH/DYLD_LIBRARY_PATH // default search from LD_LIBRARY_PATH/DYLD_LIBRARY_PATH
*dso_handle = dlopen(dso_path.c_str(), dynload_flags); *dso_handle = dlopen(dso_path.c_str(), dynload_flags);
// DYLD_LIBRARY_PATH is disabled after Mac OS 10.11 to // DYLD_LIBRARY_PATH is disabled after Mac OS 10.11 to
// bring System Integrity Projection (SIP), if dso_handle // bring System Integrity Projection (SIP), if dso_handle
// is null, search from default package path in Mac OS. // is null, search from default package path in Mac OS.
#if defined(__APPLE__) || defined(__OSX__) #if defined(__APPLE__) || defined(__OSX__)
if (nullptr == *dso_handle) { if (nullptr == *dso_handle) {
dso_path = join("/usr/local/cuda/lib/", dso_path); dso_path = join("/usr/local/cuda/lib/", dso_path);
*dso_handle = dlopen(dso_path.c_str(), dynload_flags); *dso_handle = dlopen(dso_path.c_str(), dynload_flags);
if (nullptr == *dso_handle) { if (nullptr == *dso_handle) {
if (dso_path == "libcudnn.dylib") { if (dso_path == "libcudnn.dylib") {
LOG(FATAL) << "Note: [Recommend] copy cudnn into /usr/local/cuda/ \n" LOG(FATAL)
<< "For instance, sudo tar -xzf cudnn-7.5-osx-x64-v5.0-ga.tgz -C " << "Note: [Recommend] copy cudnn into /usr/local/cuda/ \n" // NOLINT
<< "/usr/local \n sudo chmod a+r /usr/local/cuda/include/cudnn.h " << "For instance, sudo tar -xzf "
"cudnn-7.5-osx-x64-v5.0-ga.tgz -C " // NOLINT
<< "/usr/local \n sudo chmod a+r "
"/usr/local/cuda/include/cudnn.h " // NOLINT
<< "/usr/local/cuda/lib/libcudnn*"; << "/usr/local/cuda/lib/libcudnn*";
} }
} }
} }
#endif #endif
} }
static inline void GetDsoHandleFromSearchPath( static inline void GetDsoHandleFromSearchPath(const std::string& search_root,
const std::string& search_root,
const std::string& dso_name, const std::string& dso_name,
void** dso_handle) { void** dso_handle) {
int dynload_flags = RTLD_LAZY | RTLD_LOCAL; int dynload_flags = RTLD_LAZY | RTLD_LOCAL;
...@@ -87,28 +91,40 @@ static inline void GetDsoHandleFromSearchPath( ...@@ -87,28 +91,40 @@ static inline void GetDsoHandleFromSearchPath(
dlPath = join(search_root, dso_name); dlPath = join(search_root, dso_name);
*dso_handle = dlopen(dlPath.c_str(), dynload_flags); *dso_handle = dlopen(dlPath.c_str(), dynload_flags);
// if not found, search from default path // if not found, search from default path
if (nullptr == dso_handle) { if (nullptr == *dso_handle) {
LOG(WARNING) << "Failed to find cuda library: " << dlPath; LOG(WARNING) << "Failed to find cuda library: " << dlPath;
dlPath = dso_name; dlPath = dso_name;
GetDsoHandleFromDefaultPath(dlPath, dso_handle, dynload_flags); GetDsoHandleFromDefaultPath(dlPath, dso_handle, dynload_flags);
} }
} }
CHECK(nullptr != *dso_handle) CHECK(nullptr != *dso_handle) << "Failed to find cuda library: " << dlPath
<< "Failed to find cuda library: " << dlPath << std::endl << std::endl
<< "Please specify its path correctly using one of the following ideas: \n" << "Please specify its path correctly using "
"one of the following ways: \n" // NOLINT
<< "Idea 1. set cuda and cudnn lib path at runtime. "
<< "http://www.paddlepaddle.org/doc/ui/cmd_argument/argument_outline.html \n" << "Method 1. set cuda and cudnn lib path at "
<< "For instance, issue command: paddle train --use_gpu=1 " "runtime. "
<< "--cuda_dir=/usr/local/cudnn/lib --cudnn_dir=/usr/local/cudnn/lib ...\n" << "http://www.paddlepaddle.org/doc/ui/"
"cmd_argument/"
<< "Idea 2. set environment variable LD_LIBRARY_PATH on Linux or " "argument_outline.html \n" // NOLINT
<< "For instance, issue command: paddle train "
"--use_gpu=1 "
<< "--cuda_dir=/usr/local/cuda/lib64 "
"--cudnn_dir=/usr/local/cudnn/lib "
"...\n" // NOLINT
<< "Method 2. set environment variable "
"LD_LIBRARY_PATH on Linux or "
<< "DYLD_LIBRARY_PATH on Mac OS. \n" << "DYLD_LIBRARY_PATH on Mac OS. \n"
<< "For instance, issue command: export LD_LIBRARY_PATH=... \n" << "For instance, issue command: export "
"LD_LIBRARY_PATH=... \n"
<< "Note: After Mac OS 10.11, using the DYLD_LIBRARY_PATH is impossible "
<< "unless System Integrity Protection (SIP) is disabled. However, @Idea 1" << "Note: After Mac OS 10.11, using the "
"DYLD_LIBRARY_PATH is impossible "
<< "unless System Integrity Protection (SIP) "
"is disabled. However, "
"method 1 " // NOLINT
<< "always work well."; << "always work well.";
} }
......
...@@ -240,7 +240,7 @@ public: ...@@ -240,7 +240,7 @@ public:
seqClassficationError_ = 0; seqClassficationError_ = 0;
} }
virtual void printStats(std::ostream& os) { virtual void printStats(std::ostream& os) const {
os << config_.name() << "=" os << config_.name() << "="
<< (numSequences_ ? totalScore_ / numSequences_ : 0); << (numSequences_ ? totalScore_ / numSequences_ : 0);
os << " deletions error" os << " deletions error"
......
...@@ -114,7 +114,7 @@ public: ...@@ -114,7 +114,7 @@ public:
numCorrect_ = 0; numCorrect_ = 0;
} }
virtual void printStats(std::ostream& os) { virtual void printStats(std::ostream& os) const {
double precision = (double)numCorrect_ / numOutputSegments_; double precision = (double)numCorrect_ / numOutputSegments_;
double recall = (double)numCorrect_ / numLabelSegments_; double recall = (double)numCorrect_ / numLabelSegments_;
double f1 = double f1 =
......
...@@ -315,7 +315,7 @@ public: ...@@ -315,7 +315,7 @@ public:
return 0; return 0;
} }
virtual void printStats(std::ostream& os) { virtual void printStats(std::ostream& os) const {
CHECK(colIdx_ + (int32_t)colNum_ >= 0 && colIdx_ - (int32_t)colNum_ < 0) CHECK(colIdx_ + (int32_t)colNum_ >= 0 && colIdx_ - (int32_t)colNum_ < 0)
<< "column index [" << colIdx_ << "] out of range [-" << colNum_ << ", " << "column index [" << colIdx_ << "] out of range [-" << colNum_ << ", "
<< colNum_ << ")"; << colNum_ << ")";
...@@ -421,7 +421,7 @@ void AucEvaluator::distributeEval(ParameterClient2* client) { ...@@ -421,7 +421,7 @@ void AucEvaluator::distributeEval(ParameterClient2* client) {
client->reduce(statNeg_, statNeg_, kBinNum_ + 1, FLAGS_trainer_id, 0); client->reduce(statNeg_, statNeg_, kBinNum_ + 1, FLAGS_trainer_id, 0);
} }
double AucEvaluator::calcAuc() { double AucEvaluator::calcAuc() const {
double totPos = 0.0; double totPos = 0.0;
double totNeg = 0.0; double totNeg = 0.0;
double totPosPrev = 0.0; double totPosPrev = 0.0;
...@@ -584,7 +584,7 @@ real PrecisionRecallEvaluator::evalImp(std::vector<Argument>& arguments) { ...@@ -584,7 +584,7 @@ real PrecisionRecallEvaluator::evalImp(std::vector<Argument>& arguments) {
return 0; return 0;
} }
void PrecisionRecallEvaluator::printStats(std::ostream& os) { void PrecisionRecallEvaluator::printStats(std::ostream& os) const {
int label = config_.positive_label(); int label = config_.positive_label();
if (label != -1) { if (label != -1) {
CHECK(label >= 0 && label < (int)statsInfo_.size()) CHECK(label >= 0 && label < (int)statsInfo_.size())
......
...@@ -99,19 +99,19 @@ public: ...@@ -99,19 +99,19 @@ public:
* @brief print the statistics of evaluate result * @brief print the statistics of evaluate result
* @note finish() should be called before printStats * @note finish() should be called before printStats
*/ */
virtual void printStats(std::ostream& os) { virtual void printStats(std::ostream& os) const {
os << config_.name() << "=" os << config_.name() << "="
<< (numSamples_ ? totalScore_ / numSamples_ : 0); << (numSamples_ ? totalScore_ / numSamples_ : 0);
} }
friend std::ostream& operator<<(std::ostream& os, friend std::ostream& operator<<(std::ostream& os,
Evaluator& evaluator) { const Evaluator& evaluator) {
evaluator.printStats(os); evaluator.printStats(os);
return os; return os;
} }
friend std::ostream&& operator<<(std::ostream&& os, // NOLINT friend std::ostream&& operator<<(std::ostream&& os, // NOLINT
Evaluator& evaluator) { const Evaluator& evaluator) {
evaluator.printStats(os); evaluator.printStats(os);
return std::move(os); return std::move(os);
} }
...@@ -135,7 +135,7 @@ public: ...@@ -135,7 +135,7 @@ public:
return -1; return -1;
} }
virtual void finish() {} virtual void finish() {}
virtual void printStats(std::ostream&) {} virtual void printStats(std::ostream&) const {}
}; };
/** /**
* @brief evaluate AUC using colIdx-th column as prediction. * @brief evaluate AUC using colIdx-th column as prediction.
...@@ -165,7 +165,7 @@ public: ...@@ -165,7 +165,7 @@ public:
virtual real evalImp(std::vector<Argument>& arguments); virtual real evalImp(std::vector<Argument>& arguments);
virtual void printStats(std::ostream& os) { virtual void printStats(std::ostream& os) const {
os << config_.name() << "=" << calcAuc(); os << config_.name() << "=" << calcAuc();
} }
...@@ -189,7 +189,7 @@ private: ...@@ -189,7 +189,7 @@ private:
return (X1 > X2 ? (X1 - X2) : (X2 - X1)) * (Y1 + Y2) / 2.0; return (X1 > X2 ? (X1 - X2) : (X2 - X1)) * (Y1 + Y2) / 2.0;
} }
double calcAuc(); double calcAuc() const;
}; };
/** /**
...@@ -244,7 +244,7 @@ public: ...@@ -244,7 +244,7 @@ public:
virtual real evalImp(std::vector<Argument>& arguments); virtual real evalImp(std::vector<Argument>& arguments);
virtual void printStats(std::ostream& os); virtual void printStats(std::ostream& os) const;
virtual void distributeEval(ParameterClient2* client); virtual void distributeEval(ParameterClient2* client);
...@@ -339,7 +339,7 @@ public: ...@@ -339,7 +339,7 @@ public:
virtual void finish() { calc(predictArray_); } virtual void finish() { calc(predictArray_); }
virtual void printStats(std::ostream& os) { virtual void printStats(std::ostream& os) const {
os << " pos/neg" os << " pos/neg"
<< "=" << pairArray_[0] / ((pairArray_[1] <= 0) ? 1.0 : pairArray_[1]); << "=" << pairArray_[0] / ((pairArray_[1] <= 0) ? 1.0 : pairArray_[1]);
} }
......
...@@ -154,7 +154,7 @@ public: ...@@ -154,7 +154,7 @@ public:
return -1; return -1;
} }
virtual void printStats(std::ostream& os) { virtual void printStats(std::ostream& os) const {
for (auto& evaluator : evaluators_) { for (auto& evaluator : evaluators_) {
evaluator->printStats(os); evaluator->printStats(os);
os << ' '; os << ' ';
......
...@@ -325,7 +325,7 @@ public: ...@@ -325,7 +325,7 @@ public:
(void)arguments; (void)arguments;
return -1; return -1;
} }
virtual void printStats(std::ostream& os) { virtual void printStats(std::ostream& os) const {
for (auto& evaluator : evaluators_) { for (auto& evaluator : evaluators_) {
evaluator->printStats(os); evaluator->printStats(os);
os << ' '; os << ' ';
......
...@@ -1449,8 +1449,8 @@ template<> ...@@ -1449,8 +1449,8 @@ template<>
template <class Agg> template <class Agg>
int BaseMatrixT<real>::applyRow(Agg agg, BaseMatrixT& b) { int BaseMatrixT<real>::applyRow(Agg agg, BaseMatrixT& b) {
MatrixOffset offset(0, 0, 0, 0, 0, 0); MatrixOffset offset(0, 0, 0, 0, 0, 0);
int numRows = b.height_; size_t numRows = b.height_;
int numCols = b.width_; size_t numCols = b.width_;
CHECK_EQ(height_, numRows); CHECK_EQ(height_, numRows);
CHECK_EQ(width_, 1UL); CHECK_EQ(width_, 1UL);
aggregate(agg, base::unary::identity(), base::binary::second(), b, numRows, aggregate(agg, base::unary::identity(), base::binary::second(), b, numRows,
...@@ -1463,8 +1463,8 @@ template<> ...@@ -1463,8 +1463,8 @@ template<>
template <class Agg, class Saver> template <class Agg, class Saver>
int BaseMatrixT<real>::applyRow(Agg agg, Saver sv, BaseMatrixT& b) { int BaseMatrixT<real>::applyRow(Agg agg, Saver sv, BaseMatrixT& b) {
MatrixOffset offset(0, 0, 0, 0, 0, 0); MatrixOffset offset(0, 0, 0, 0, 0, 0);
int numRows = b.height_; size_t numRows = b.height_;
int numCols = b.width_; size_t numCols = b.width_;
CHECK_EQ(height_, numRows); CHECK_EQ(height_, numRows);
CHECK_EQ(width_, 1UL); CHECK_EQ(width_, 1UL);
aggregate(agg, base::unary::identity(), sv, b, numRows, numCols, offset, aggregate(agg, base::unary::identity(), sv, b, numRows, numCols, offset,
...@@ -1493,8 +1493,8 @@ template <class Agg, class Op, class Saver> ...@@ -1493,8 +1493,8 @@ template <class Agg, class Op, class Saver>
int BaseMatrixT<real>::applyRow(Agg agg, Op op, Saver sv, int BaseMatrixT<real>::applyRow(Agg agg, Op op, Saver sv,
BaseMatrixT& b, BaseMatrixT& c) { BaseMatrixT& b, BaseMatrixT& c) {
MatrixOffset offset(0, 0, 0, 0, 0, 0); MatrixOffset offset(0, 0, 0, 0, 0, 0);
int numRows = b.height_; size_t numRows = b.height_;
int numCols = b.width_; size_t numCols = b.width_;
CHECK_EQ(height_, numRows); CHECK_EQ(height_, numRows);
CHECK_EQ(width_, 1UL); CHECK_EQ(width_, 1UL);
CHECK_EQ(c.height_, numRows); CHECK_EQ(c.height_, numRows);
...@@ -1524,8 +1524,8 @@ template<> ...@@ -1524,8 +1524,8 @@ template<>
template <class Agg> template <class Agg>
int BaseMatrixT<real>::applyCol(Agg agg, BaseMatrixT& b) { int BaseMatrixT<real>::applyCol(Agg agg, BaseMatrixT& b) {
MatrixOffset offset(0, 0, 0, 0, 0, 0); MatrixOffset offset(0, 0, 0, 0, 0, 0);
int numRows = b.height_; size_t numRows = b.height_;
int numCols = b.width_; size_t numCols = b.width_;
CHECK_EQ(width_, numCols); CHECK_EQ(width_, numCols);
CHECK_EQ(height_, 1UL); CHECK_EQ(height_, 1UL);
aggregate(agg, base::unary::identity(), base::binary::second(), b, numRows, aggregate(agg, base::unary::identity(), base::binary::second(), b, numRows,
...@@ -1538,8 +1538,8 @@ template<> ...@@ -1538,8 +1538,8 @@ template<>
template <class Agg, class Saver> template <class Agg, class Saver>
int BaseMatrixT<real>::applyCol(Agg agg, Saver sv, BaseMatrixT& b) { int BaseMatrixT<real>::applyCol(Agg agg, Saver sv, BaseMatrixT& b) {
MatrixOffset offset(0, 0, 0, 0, 0, 0); MatrixOffset offset(0, 0, 0, 0, 0, 0);
int numRows = b.height_; size_t numRows = b.height_;
int numCols = b.width_; size_t numCols = b.width_;
CHECK_EQ(width_, numCols); CHECK_EQ(width_, numCols);
CHECK_EQ(height_, 1UL); CHECK_EQ(height_, 1UL);
aggregate(agg, base::unary::identity(), sv, b, numRows, numCols, offset, aggregate(agg, base::unary::identity(), sv, b, numRows, numCols, offset,
......
...@@ -82,8 +82,8 @@ MatrixPtr VectorT<real>::toOneHotSparseMatrix(size_t idRange, bool useGpu) { ...@@ -82,8 +82,8 @@ MatrixPtr VectorT<real>::toOneHotSparseMatrix(size_t idRange, bool useGpu) {
template <> template <>
MatrixPtr VectorT<int>::toOneHotSparseMatrix(size_t idRange, bool useGpu) { MatrixPtr VectorT<int>::toOneHotSparseMatrix(size_t idRange, bool useGpu) {
int height = getSize(); size_t height = getSize();
int width = idRange; size_t width = idRange;
MatrixPtr mat = Matrix::createSparseMatrix( MatrixPtr mat = Matrix::createSparseMatrix(
height, idRange, height, NO_VALUE, SPARSE_CSR, false, useGpu); height, idRange, height, NO_VALUE, SPARSE_CSR, false, useGpu);
...@@ -91,7 +91,7 @@ MatrixPtr VectorT<int>::toOneHotSparseMatrix(size_t idRange, bool useGpu) { ...@@ -91,7 +91,7 @@ MatrixPtr VectorT<int>::toOneHotSparseMatrix(size_t idRange, bool useGpu) {
cpuIds.copyFrom(*this); cpuIds.copyFrom(*this);
int *idData = cpuIds.getData(); int *idData = cpuIds.getData();
for (int i = 0; i < height; i ++) { for (decltype(height) i = 0; i < height; i ++) {
const unsigned int id = idData[i]; const unsigned int id = idData[i];
CHECK_LT(id, width); CHECK_LT(id, width);
mat->setRow(i, 1, &id, nullptr); mat->setRow(i, 1, &id, nullptr);
......
...@@ -1469,7 +1469,6 @@ void ParameterServer2::waitPassFinish(const WaitPassFinishRequest& request, ...@@ -1469,7 +1469,6 @@ void ParameterServer2::waitPassFinish(const WaitPassFinishRequest& request,
void ParameterServer2::synchronize(const SynchronizeRequest& request, void ParameterServer2::synchronize(const SynchronizeRequest& request,
ProtoResponseCallback callback) { ProtoResponseCallback callback) {
CHECK_LT(request.sync_object_id(), SyncObject_ARRAYSIZE);
synchronizeBarriers_[request.sync_object_id()]->wait(); synchronizeBarriers_[request.sync_object_id()]->wait();
dataSize_ = 0; dataSize_ = 0;
callback(SynchronizeResponse()); callback(SynchronizeResponse());
...@@ -1477,7 +1476,6 @@ void ParameterServer2::synchronize(const SynchronizeRequest& request, ...@@ -1477,7 +1476,6 @@ void ParameterServer2::synchronize(const SynchronizeRequest& request,
void ParameterServer2::asyncFinishPass(const SynchronizeRequest& request, void ParameterServer2::asyncFinishPass(const SynchronizeRequest& request,
ProtoResponseCallback callback) { ProtoResponseCallback callback) {
CHECK_LT(request.sync_object_id(), SyncObject_ARRAYSIZE);
synchronizeBarriers_[request.sync_object_id()]->wait(); synchronizeBarriers_[request.sync_object_id()]->wait();
callback(SynchronizeResponse()); callback(SynchronizeResponse());
......
...@@ -29,10 +29,10 @@ P_DEFINE_bool(log_barrier_show_log, false, // for performance tuning insight ...@@ -29,10 +29,10 @@ P_DEFINE_bool(log_barrier_show_log, false, // for performance tuning insight
namespace paddle { namespace paddle {
std::ostream &operator<<(std::ostream &output, BarrierStatBase &stat) { std::ostream &operator<<(std::ostream &output,
const BarrierStatBase &stat) {
if (FLAGS_log_barrier_abstract) { if (FLAGS_log_barrier_abstract) {
std::lock_guard<std::mutex> guard( std::lock_guard<std::mutex> guard(stat.lock_);
const_cast<BarrierStatBase &>(stat).lock_);
stat.showAbstract(output); stat.showAbstract(output);
} }
return output; return output;
...@@ -136,7 +136,7 @@ void BarrierEndStat::reset(bool clearRawData) { ...@@ -136,7 +136,7 @@ void BarrierEndStat::reset(bool clearRawData) {
totAbstract_.minDelta = UINT64_MAX; totAbstract_.minDelta = UINT64_MAX;
} }
void BarrierEndStat::showAbstract(std::ostream &output) { void BarrierEndStat::showAbstract(std::ostream &output) const {
// do not support the case "<=2 pserver" // do not support the case "<=2 pserver"
if (numConnThreads_ <= 2 || !totSamples_) { if (numConnThreads_ <= 2 || !totSamples_) {
return; return;
...@@ -272,7 +272,7 @@ void BarrierDeltaStat::reset(bool clearRawData) { ...@@ -272,7 +272,7 @@ void BarrierDeltaStat::reset(bool clearRawData) {
totAbstract_.minDelta = UINT64_MAX; totAbstract_.minDelta = UINT64_MAX;
} }
void BarrierDeltaStat::showAbstract(std::ostream &output) { void BarrierDeltaStat::showAbstract(std::ostream &output) const {
// do not support the case "<=2 pserver" // do not support the case "<=2 pserver"
if (numConnThreads_ <= 2 || !totSamples_) { if (numConnThreads_ <= 2 || !totSamples_) {
return; return;
......
...@@ -218,11 +218,12 @@ public: ...@@ -218,11 +218,12 @@ public:
} }
protected: protected:
virtual void showAbstract(std::ostream &output) {} virtual void showAbstract(std::ostream &output) const {}
friend std::ostream &operator<<(std::ostream &output, BarrierStatBase &stat); friend std::ostream &operator<<(std::ostream &output,
const BarrierStatBase &stat);
protected: protected:
std::mutex lock_; mutable std::mutex lock_;
std::mutex abstractLock_; // see note on updaterStat std::mutex abstractLock_; // see note on updaterStat
// each freqency for each barrier trainer // each freqency for each barrier trainer
std::vector<struct Abstract> abstract_; std::vector<struct Abstract> abstract_;
...@@ -262,7 +263,7 @@ protected: ...@@ -262,7 +263,7 @@ protected:
* log_barrier_abstract, log_barrier_lowest_nodes, log_barrier_threshold * log_barrier_abstract, log_barrier_lowest_nodes, log_barrier_threshold
* control details. * control details.
*/ */
virtual void showAbstract(std::ostream &output); virtual void showAbstract(std::ostream &output) const;
private: private:
std::unique_ptr<TimeVectorEnd> timeVector_; std::unique_ptr<TimeVectorEnd> timeVector_;
...@@ -286,7 +287,7 @@ public: ...@@ -286,7 +287,7 @@ public:
virtual bool checkPassBarrier() { return timeVector_->empty(); } virtual bool checkPassBarrier() { return timeVector_->empty(); }
protected: protected:
virtual void showAbstract(std::ostream &outPut); virtual void showAbstract(std::ostream &outPut) const;
private: private:
// store delta time in uint64_t, eg BP time of all trainers // store delta time in uint64_t, eg BP time of all trainers
......
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.
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. */
#pragma once
#define ATTR_NORETURN __attribute__((noreturn))
...@@ -134,7 +134,7 @@ static void initializeLogFds(char* argv0) { ...@@ -134,7 +134,7 @@ static void initializeLogFds(char* argv0) {
gLogInited = true; gLogInited = true;
} }
static void (*gFailureFunctionPtr)() __attribute__((noreturn)) = abort; static void (*gFailureFunctionPtr)() ATTR_NORETURN = abort;
LogMessage::LogMessage(const char* fname, int line, int severity) LogMessage::LogMessage(const char* fname, int line, int severity)
: fname_(fname), line_(line), severity_(severity) {} : fname_(fname), line_(line), severity_(severity) {}
...@@ -171,7 +171,7 @@ void setMinLogLevel(int level) { ...@@ -171,7 +171,7 @@ void setMinLogLevel(int level) {
paddle::internal::gMinLogLevel = level; paddle::internal::gMinLogLevel = level;
} }
void installFailureFunction(void (*callback)()) { void installFailureFunction(void (*callback)() ATTR_NORETURN) {
paddle::internal::gFailureFunctionPtr = callback; paddle::internal::gFailureFunctionPtr = callback;
} }
......
...@@ -23,6 +23,7 @@ limitations under the License. */ ...@@ -23,6 +23,7 @@ limitations under the License. */
#include <string> #include <string>
#ifndef PADDLE_USE_GLOG #ifndef PADDLE_USE_GLOG
#include "CompilerMacros.h"
//! TODO(yuyang18): Move this utility macro into some global header. //! TODO(yuyang18): Move this utility macro into some global header.
#define PP_CAT(a, b) PP_CAT_I(a, b) #define PP_CAT(a, b) PP_CAT_I(a, b)
...@@ -168,7 +169,7 @@ void setMinLogLevel(int level); ...@@ -168,7 +169,7 @@ void setMinLogLevel(int level);
* @brief Install Log(Fatal) failure function. Default is abort(); * @brief Install Log(Fatal) failure function. Default is abort();
* @param callback: The failure function. * @param callback: The failure function.
*/ */
void installFailureFunction(void (*callback)()); void installFailureFunction(void (*callback)() ATTR_NORETURN);
/** /**
* @brief installFailureWriter * @brief installFailureWriter
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册