paddle v2 个性化推荐代码速度问题
背景
视频内容的个性化推荐
实现方式
仿照paddle的个性化推荐例子(http://book.paddlepaddle.org/05.recommender_system/index.cn.html),回归预测每个用户对video的阅读时长
模型代码如下:
def cluster_data_reader(file_dir, node_id):
def data_reader():
files = os.listdir(file_dir)
t1 = time.time()
print "\n*****begin to reader*****"
for fi in files:
with open(file_dir + '/' + fi, "r") as f:
for line in f:
user_id, time_period, user_location, app_id, content_id, cate_id, title, brief, quality_level, check_in_period, read_time = line.strip().split('\t')
user_id_code = feature_dict['user_id'].gen(user_id)
user_location_code = [feature_dict['user_location'].gen(ul) for ul in user_location.split('|')]
content_id_code = feature_dict['content_id'].gen(int(content_id))
cate_id_code = feature_dict['cate_id'].gen(cate_id)
title_code = [feature_dict['word'].gen(w.lower()) for w in title.split('^')]
brief_code = [feature_dict['word'].gen(w.lower()) for w in brief.split('^')]
check_in_period_code = feature_dict['check_in_period'].gen(int(check_in_period))
record = [user_id_code, int(time_period), user_location_code, content_id_code, cate_id_code, title_code, brief_code, check_in_period_code]
yield record + [[float(read_time)]]
print "cost time of reader is %s" % (time.time()-t1)
print "*****end reader*****"
return data_reader
def get_usr_combined_features():
print "user_id size_%d" % feature_dict['user_id'].size()
print "user_location_%d" % feature_dict['user_location'].size()
uid = paddle.layer.data(
name='user_id',
type=paddle.data_type.integer_value(
feature_dict['user_id'].size()))
usr_emb = paddle.layer.embedding(input=uid, size=16)
usr_fc = paddle.layer.fc(input=usr_emb, size=16)
time_period = paddle.layer.data(
name='time_period',
type=paddle.data_type.integer_value(24))
time_period_emb = paddle.layer.embedding(input=time_period, size=16)
time_period_fc = paddle.layer.fc(input=time_period_emb, size=16)
usr_location = paddle.layer.data(
name='user_location',
type=paddle.data_type.sparse_binary_vector(
feature_dict['user_location'].size()))
usr_location_fc = paddle.layer.fc(input=usr_location, size=32)
usr_combined_features = paddle.layer.fc(
input=[usr_fc, time_period_fc, usr_location_fc],
size=200,
act=paddle.activation.Tanh())
return usr_combined_features
def get_content_combined_features():
content_word_dict = feature_dict['word'].dic
print "content_id size_%d" % feature_dict['content_id'].size()
print "cate_id size_%d" % feature_dict['cate_id'].size()
print "content_word_dict length_%d" % len(content_word_dict)
print "check_in_period size_%d" % feature_dict['check_in_period'].size()
content_id = paddle.layer.data(
name='content_id',
type=paddle.data_type.integer_value(
feature_dict['content_id'].size()))
content_emb = paddle.layer.embedding(input=content_id, size=32)
content_fc = paddle.layer.fc(input=content_emb, size=32)
content_categories = paddle.layer.data(
name='cate_id',
type=paddle.data_type.integer_value(
feature_dict['cate_id'].size()))
content_categories_emb = paddle.layer.embedding(input=content_categories, size=16)
content_categories_fc = paddle.layer.fc(input=content_categories_emb, size=16)
content_title_id = paddle.layer.data(
name='title',
type=paddle.data_type.integer_value_sequence(len(content_word_dict)))
content_title_emb = paddle.layer.embedding(input=content_title_id, size=128)
content_title_conv = paddle.networks.sequence_conv_pool(
input=content_title_emb, hidden_size=128, context_len=2)
content_brief_id = paddle.layer.data(
name='brief',
type=paddle.data_type.integer_value_sequence(len(content_word_dict)))
content_brief_emb = paddle.layer.embedding(input=content_brief_id, size=128)
content_brief_conv = paddle.networks.sequence_conv_pool(
input=content_brief_emb, hidden_size=128, context_len=2)
check_in_period = paddle.layer.data(
name='check_in_period',
type=paddle.data_type.integer_value(
feature_dict['check_in_period'].size()))
check_in_period_emb = paddle.layer.embedding(input=check_in_period, size=32)
check_in_period_fc = paddle.layer.fc(input=check_in_period_emb, size=32)
content_combined_features = paddle.layer.fc(
input=[content_fc, content_categories_fc, content_title_conv, content_brief_conv, check_in_period_fc],
size=200,
act=paddle.activation.Tanh())
return content_combined_features
usr_combined_features = get_usr_combined_features()
content_combined_features = get_content_combined_features()
inference = paddle.layer.cos_sim(
a=usr_combined_features, b=content_combined_features, size=1)
cost = paddle.layer.square_error_cost(
input=inference,
label=paddle.layer.data(
name='read_time', type=paddle.data_type.dense_vector(1)))
def train_model(num_pass):
print("Begin to train model...")
train_reader = paddle.batch(
paddle.reader.shuffle(
cluster_data_reader(cluster_train_dir, node_id), buf_size=8192),
batch_size=100)
test_reader = paddle.batch(
paddle.reader.shuffle(
cluster_data_reader(cluster_test_dir, node_id), buf_size=8192),
batch_size=100)
parameters = paddle.parameters.create(cost)
trainer = paddle.trainer.SGD(
cost=cost,
parameters=parameters,
update_equation=paddle.optimizer.Adam(learning_rate=1e-3))
global t
t = time.time()
def event_handler(event):
if isinstance(event, paddle.event.EndPass):
if not os.path.exists("model_params"):
os.makedirs("model_params")
with gzip.open("model_params/video_recomm_pass_%03d.tar.gz" % event.pass_id, 'w') as f:
parameters.to_tar(f)
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 1 == 0:
global t
print "Pass %d Batch %d Cost %.5f Time %s" % (
event.pass_id, event.batch_id, event.cost, time.time()-t)
t = time.time()
trainer.train(
reader=train_reader,
event_handler=event_handler,
feeding=feeding,
num_passes=num_pass)
print("Training finished!")
耗时数据
测试数据:500条,batch_size=100,pass_num=20,将每个batch的耗时打印出来,具体如下: begin to reader cost time of reader is 0.0190041065216 end reader Pass 0 Batch 0 Cost 0.00802 Time 0.0562610626221 Pass 0 Batch 1 Cost 0.01444 Time 2.72469496727 Pass 0 Batch 2 Cost 0.00655 Time 2.34903788567 Pass 0 Batch 3 Cost 0.00880 Time 2.33675003052 Pass 0 Batch 4 Cost 0.00874 Time 2.34233188629
begin to reader cost time of reader is 0.0191640853882 end reader Pass 1 Batch 0 Cost 0.00708 Time 70.6922171116 Pass 1 Batch 1 Cost 0.00345 Time 2.46361804008 Pass 1 Batch 2 Cost 0.00581 Time 2.33501601219 Pass 1 Batch 3 Cost 0.00240 Time 2.33918118477 Pass 1 Batch 4 Cost 0.00394 Time 2.33651804924
begin to reader cost time of reader is 0.0201029777527 end reader Pass 2 Batch 0 Cost 0.00428 Time 64.1795651913 Pass 2 Batch 1 Cost 0.00132 Time 2.34728193283 Pass 2 Batch 2 Cost 0.00151 Time 2.3494591713 Pass 2 Batch 3 Cost 0.00295 Time 2.35163593292 Pass 2 Batch 4 Cost 0.00090 Time 2.34288620949
begin to reader cost time of reader is 0.203039884567 end reader Pass 3 Batch 0 Cost 0.00317 Time 66.1391232014 Pass 3 Batch 1 Cost 0.00065 Time 2.39867901802 Pass 3 Batch 2 Cost 0.00048 Time 2.33365297318 Pass 3 Batch 3 Cost 0.00172 Time 2.33354210854 Pass 3 Batch 4 Cost 0.00055 Time 2.33912920952
问题
1、为什么第一个pass的第一个batch耗时很短(0.056s),后面每个pass的第一个batch耗时都非常长(60+s),核心问题点在哪里呢?是哪个地方的代码写的不好呢?
根据代码:
def event_handler(event):
if isinstance(event, paddle.event.EndPass):
if not os.path.exists("model_params"):
os.makedirs("model_params")
with gzip.open("model_params/video_recomm_pass_%03d.tar.gz" % event.pass_id, 'w') as f:
parameters.to_tar(f)
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 1 == 0:
global t
print "Pass %d Batch %d Cost %.5f Time %s" % (
event.pass_id, event.batch_id, event.cost, time.time()-t)
t = time.time()
在EndPass的时候执行了保存模型的操作,由于变量t`是global,所以这部分的耗时也算在下一个pass的第一个batch了。
1、去掉保存模型的那段代码后,下一个pass的第一个batch的耗时就回归正常了,具体如下: begin to reader cost time of reader is 0.0198950767517 end reader Pass 0 Batch 0 Cost 0.01086 Time 0.057541847229 Pass 0 Batch 1 Cost 0.00953 Time 2.41039896011 Pass 0 Batch 2 Cost 0.01217 Time 2.34353995323 Pass 0 Batch 3 Cost 0.00774 Time 2.38308596611 Pass 0 Batch 4 Cost 0.00611 Time 2.3384039402
begin to reader cost time of reader is 0.0186748504639 end reader Pass 1 Batch 0 Cost 0.00618 Time 2.35229802132 Pass 1 Batch 1 Cost 0.00679 Time 2.33350300789 Pass 1 Batch 2 Cost 0.00230 Time 2.33129096031 Pass 1 Batch 3 Cost 0.00520 Time 2.33390402794 Pass 1 Batch 4 Cost 0.00134 Time 2.33259606361
begin to reader cost time of reader is 0.0195112228394 end reader Pass 2 Batch 0 Cost 0.00215 Time 2.39848899841 Pass 2 Batch 1 Cost 0.00342 Time 2.3564760685 Pass 2 Batch 2 Cost 0.00073 Time 2.33124685287 Pass 2 Batch 3 Cost 0.00245 Time 2.33508682251 Pass 2 Batch 4 Cost 0.00166 Time 2.33647203445
2、为了确认是否是fc层的size大导致参数过多,训练时间长,又试验了一下除content_combined_features和usr_combined_features外的fc的size都改为8,每个batch的耗时并未发生太大变化,具体如下: begin to reader cost time of reader is 0.0192558765411 end reader Pass 0 Batch 0 Cost 0.00543 Time 0.0559267997742 Pass 0 Batch 1 Cost 0.01636 Time 2.67969298363 Pass 0 Batch 2 Cost 0.00543 Time 2.34373402596 Pass 0 Batch 3 Cost 0.00873 Time 2.33688998222 Pass 0 Batch 4 Cost 0.00673 Time 2.33708095551
begin to reader cost time of reader is 0.017924785614 end reader Pass 1 Batch 0 Cost 0.01189 Time 2.35544705391 Pass 1 Batch 1 Cost 0.00244 Time 2.33957314491 Pass 1 Batch 2 Cost 0.00303 Time 2.34591603279 Pass 1 Batch 3 Cost 0.00432 Time 2.33616900444 Pass 1 Batch 4 Cost 0.00455 Time 2.34570884705
begin to reader cost time of reader is 0.0185658931732 end reader Pass 2 Batch 0 Cost 0.00258 Time 2.36736178398 Pass 2 Batch 1 Cost 0.00170 Time 2.34268593788 Pass 2 Batch 2 Cost 0.00232 Time 2.33838582039 Pass 2 Batch 3 Cost 0.00072 Time 2.33503699303 Pass 2 Batch 4 Cost 0.00729 Time 2.33629798889
3、在2的基础上进一步去掉content_title_conv、content_brief_conv两个卷积,第一个pass的第一个batch耗时降低了一半多时间,剩下的batch耗时有所降低,但还是在2s的水平,具体如下: begin to reader cost time of reader is 0.0187819004059 end reader Pass 0 Batch 0 Cost 0.00844 Time 0.0241801738739 Pass 0 Batch 1 Cost 0.01429 Time 2.10377597809 Pass 0 Batch 2 Cost 0.00453 Time 2.1256840229 Pass 0 Batch 3 Cost 0.00530 Time 2.10151100159 Pass 0 Batch 4 Cost 0.00477 Time 2.10498213768
begin to reader cost time of reader is 0.0180230140686 end reader Pass 1 Batch 0 Cost 0.00308 Time 2.12215590477 Pass 1 Batch 1 Cost 0.00428 Time 2.10180091858 Pass 1 Batch 2 Cost 0.00959 Time 2.12116479874 Pass 1 Batch 3 Cost 0.00300 Time 2.10274815559 Pass 1 Batch 4 Cost 0.00463 Time 2.10080599785
begin to reader cost time of reader is 0.0180270671844 end reader Pass 2 Batch 0 Cost 0.00516 Time 2.11908507347 Pass 2 Batch 1 Cost 0.00224 Time 2.10361218452 Pass 2 Batch 2 Cost 0.00295 Time 2.12860393524 Pass 2 Batch 3 Cost 0.00652 Time 2.10283899307 Pass 2 Batch 4 Cost 0.00282 Time 2.10277104378
问题
第一个pass的第一个batch的耗时为啥比其它batch要短那么多呢?模型耗时的关键点在哪里,如何提速?
通用的方法可以参考: https://github.com/PaddlePaddle/Paddle/blob/develop/doc/faq/local/index_cn.rst#id7
另外reader 可以试试增加cache是否有效果。
第一个pass的第一个batch的耗时为啥比其它batch要短,没看出来是什么原因。
另外,我想问下,最终目标是什么呢,如果是需要提升大量数据的总训练时间,可以通过增加并行度,比如每个节点增加trainer_count,增加节点个数,这样训练每GB数据的总时间仍然是可以缩短的。提升训练速度的目标我理解无非是能在同样的时间,训练更多的数据达到更好的效果。
你说的这些通用的方法v2版本不是不支持吗? 最终的目标就是在相同的时间内训练更多的数据,目前一个batch(100条数据)需要2s的速度,还是不太可接受,因为我们线上是亿级别的数据,例如:2亿条数据,照这个速度训练一个pass需要1112个小时,就算有50个节点的mpi集群跑,需要22个小时左右,假如模型50个pass可以收敛,最终需要1100个小时才能完成模型的训练。所以看看哪些地方可以有优化的地方,刚刚又试了一下只用user_id,content_id这俩特征,不保存模型的情况下,运行耗时结果如下: begin to reader cost time of reader is 0.0189650058746 end reader Pass 0 Batch 0 Cost 0.01777 Time 0.33441400528 Pass 0 Batch 1 Cost 0.02581 Time 0.275286197662 Pass 0 Batch 2 Cost 0.07598 Time 0.232975006104 Pass 0 Batch 3 Cost 0.01390 Time 0.233116149902 Pass 0 Batch 4 Cost 0.04789 Time 0.232594966888
begin to reader cost time of reader is 0.0181798934937 end reader Pass 1 Batch 0 Cost 0.03391 Time 0.252593994141 Pass 1 Batch 1 Cost 0.02259 Time 0.235366106033 Pass 1 Batch 2 Cost 0.02738 Time 0.232609987259 Pass 1 Batch 3 Cost 0.01866 Time 0.233798027039 Pass 1 Batch 4 Cost 0.01431 Time 0.233622074127
begin to reader cost time of reader is 0.0182199478149 end reader Pass 2 Batch 0 Cost 0.00865 Time 0.252803087234 Pass 2 Batch 1 Cost 0.00533 Time 0.23236823082 Pass 2 Batch 2 Cost 0.00909 Time 0.232687950134 Pass 2 Batch 3 Cost 0.00961 Time 0.255095005035 Pass 2 Batch 4 Cost 0.00625 Time 0.232652902603
这次每个pass的每个batch的耗时就差不多了,都是0.2+秒的样子,于是猜测加入更多特征后,获取特征数据的过程慢,然后就在get_usr_combined_features() 、get_content_combined_features()打印了每一个feature获取的耗时,但是这部分日志只在train model前有一次输出,后续的每个batch训练都没有,请问一下模型训练的时候,具体是怎么调用这个get_usr_combined_features() 、get_content_combined_features()的呢?不是每来一条数据都去调用一下get_usr_combined_features() 和get_content_combined_features()得到相应的向量么?速度提不上去跟这个有关吗?
我理解你在去掉一些feature的时候,reader本身还是要逐个buffer的读取全部的训练数据到内存,即使没有feed给模型训练,reader消耗的时间也不会差别太大。是否可以试试: http://doc.paddlepaddle.org/doc_cn/api/v2/data.html#reader buffered这个decorator,看下是否能有明显的效果。也可以试试增加batch_size。
我理解batchsize=100训练2s主要应该都在计算上了,但去掉feature势必会影响模型的效果,最终线上的训练任务肯定不可能只用user_id,content_id。
get_usr_combined_features()等函数,是模型的配置,这个只会执行一次,模型配置好之后,具体的计算都是在C++内执行的。速度和这个没有关系。
另外一个提速的思路:可以考虑增量训练模型,比如拿1个月的数据,训练一个基础的模型,然后用每天的数据更新这个基础模型,这样每天训练的数据量小。基础的模型训练花费时间长也可以接受了。
可以增量更新模型吗?求方法和例子
训练增量模型的时候parameters从基础模型加载就行,参考infer加载模型的代码。
@typhoonzero 增量方式支持词典表的增加吗,比如新id来了。
感觉应该有一些方法可以做到,比如,base模型预留足够多的<unk>,增量模型逐步使用预留的id增量训练,下一个base模型,再用最新的词典训练并进一步扩展预留id,依次类推。不确定这样是否对效果有什么影响。
也是个办法,可以试试看