Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
278e368a
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
278e368a
编写于
10月 25, 2018
作者:
G
Guo Sheng
提交者:
GitHub
10月 25, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1354 from guoshengCS/opt-transformer-matmul
Optimize Transformer performance
上级
9fd693ba
4a20e672
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
38 addition
and
14 deletion
+38
-14
fluid/neural_machine_translation/transformer/model.py
fluid/neural_machine_translation/transformer/model.py
+8
-5
fluid/neural_machine_translation/transformer/profile.py
fluid/neural_machine_translation/transformer/profile.py
+24
-8
fluid/neural_machine_translation/transformer/reader.py
fluid/neural_machine_translation/transformer/reader.py
+6
-1
未找到文件。
fluid/neural_machine_translation/transformer/model.py
浏览文件 @
278e368a
...
@@ -80,7 +80,7 @@ def multi_head_attention(queries,
...
@@ -80,7 +80,7 @@ def multi_head_attention(queries,
# The value 0 in shape attr means copying the corresponding dimension
# The value 0 in shape attr means copying the corresponding dimension
# size of the input as the output dimension size.
# size of the input as the output dimension size.
reshaped
=
layers
.
reshape
(
reshaped
=
layers
.
reshape
(
x
=
x
,
shape
=
[
0
,
0
,
n_head
,
hidden_size
//
n_head
])
x
=
x
,
shape
=
[
0
,
0
,
n_head
,
hidden_size
//
n_head
]
,
inplace
=
True
)
# permuate the dimensions into:
# permuate the dimensions into:
# [batch_size, n_head, max_sequence_len, hidden_size_per_head]
# [batch_size, n_head, max_sequence_len, hidden_size_per_head]
...
@@ -99,7 +99,9 @@ def multi_head_attention(queries,
...
@@ -99,7 +99,9 @@ def multi_head_attention(queries,
# The value 0 in shape attr means copying the corresponding dimension
# The value 0 in shape attr means copying the corresponding dimension
# size of the input as the output dimension size.
# size of the input as the output dimension size.
return
layers
.
reshape
(
return
layers
.
reshape
(
x
=
trans_x
,
shape
=
[
0
,
0
,
trans_x
.
shape
[
2
]
*
trans_x
.
shape
[
3
]])
x
=
trans_x
,
shape
=
[
0
,
0
,
trans_x
.
shape
[
2
]
*
trans_x
.
shape
[
3
]],
inplace
=
True
)
def
scaled_dot_product_attention
(
q
,
k
,
v
,
attn_bias
,
d_key
,
dropout_rate
):
def
scaled_dot_product_attention
(
q
,
k
,
v
,
attn_bias
,
d_key
,
dropout_rate
):
"""
"""
...
@@ -523,8 +525,7 @@ def transformer(src_vocab_size,
...
@@ -523,8 +525,7 @@ def transformer(src_vocab_size,
epsilon
=
label_smooth_eps
)
epsilon
=
label_smooth_eps
)
cost
=
layers
.
softmax_with_cross_entropy
(
cost
=
layers
.
softmax_with_cross_entropy
(
logits
=
layers
.
reshape
(
logits
=
predict
,
predict
,
shape
=
[
-
1
,
trg_vocab_size
]),
label
=
label
,
label
=
label
,
soft_label
=
True
if
label_smooth_eps
else
False
)
soft_label
=
True
if
label_smooth_eps
else
False
)
weighted_cost
=
cost
*
weights
weighted_cost
=
cost
*
weights
...
@@ -637,6 +638,9 @@ def wrap_decoder(trg_vocab_size,
...
@@ -637,6 +638,9 @@ def wrap_decoder(trg_vocab_size,
preprocess_cmd
,
preprocess_cmd
,
postprocess_cmd
,
postprocess_cmd
,
caches
=
caches
)
caches
=
caches
)
# Reshape to 2D tensor to use GEMM instead of BatchedGEMM
dec_output
=
layers
.
reshape
(
dec_output
,
shape
=
[
-
1
,
dec_output
.
shape
[
-
1
]],
inplace
=
True
)
if
weight_sharing
:
if
weight_sharing
:
predict
=
layers
.
matmul
(
predict
=
layers
.
matmul
(
x
=
dec_output
,
x
=
dec_output
,
...
@@ -751,7 +755,6 @@ def fast_decode(
...
@@ -751,7 +755,6 @@ def fast_decode(
dec_inputs
=
(
pre_ids
,
pre_pos
,
None
,
pre_src_attn_bias
),
dec_inputs
=
(
pre_ids
,
pre_pos
,
None
,
pre_src_attn_bias
),
enc_output
=
pre_enc_output
,
enc_output
=
pre_enc_output
,
caches
=
pre_caches
)
caches
=
pre_caches
)
logits
=
layers
.
reshape
(
logits
,
(
-
1
,
trg_vocab_size
))
topk_scores
,
topk_indices
=
layers
.
topk
(
topk_scores
,
topk_indices
=
layers
.
topk
(
input
=
layers
.
softmax
(
logits
),
k
=
beam_size
)
input
=
layers
.
softmax
(
logits
),
k
=
beam_size
)
...
...
fluid/neural_machine_translation/transformer/profile.py
浏览文件 @
278e368a
import
argparse
import
argparse
import
ast
import
ast
import
contextlib
import
multiprocessing
import
multiprocessing
import
os
import
os
import
six
import
six
...
@@ -79,8 +80,7 @@ def parse_args():
...
@@ -79,8 +80,7 @@ def parse_args():
type
=
lambda
x
:
str
(
x
.
encode
().
decode
(
"unicode-escape"
)),
type
=
lambda
x
:
str
(
x
.
encode
().
decode
(
"unicode-escape"
)),
default
=
" "
,
default
=
" "
,
help
=
"The delimiter used to split tokens in source or target sentences. "
help
=
"The delimiter used to split tokens in source or target sentences. "
"For EN-DE BPE data we provided, use spaces as token delimiter. "
"For EN-DE BPE data we provided, use spaces as token delimiter."
)
"For EN-FR wordpiece data we provided, use '
\x01
' as token delimiter."
)
parser
.
add_argument
(
parser
.
add_argument
(
"--use_mem_opt"
,
"--use_mem_opt"
,
type
=
ast
.
literal_eval
,
type
=
ast
.
literal_eval
,
...
@@ -98,9 +98,14 @@ def parse_args():
...
@@ -98,9 +98,14 @@ def parse_args():
help
=
"The iteration number to run in profiling."
)
help
=
"The iteration number to run in profiling."
)
parser
.
add_argument
(
parser
.
add_argument
(
"--use_parallel_exe"
,
"--use_parallel_exe"
,
type
=
boo
l
,
type
=
ast
.
literal_eva
l
,
default
=
False
,
default
=
False
,
help
=
"The flag indicating whether to use ParallelExecutor."
)
help
=
"The flag indicating whether to use ParallelExecutor."
)
parser
.
add_argument
(
"--profile_ops"
,
type
=
ast
.
literal_eval
,
default
=
True
,
help
=
"The flag indicating whether to profile operators."
)
parser
.
add_argument
(
parser
.
add_argument
(
'opts'
,
'opts'
,
help
=
'See config.py for all options'
,
help
=
'See config.py for all options'
,
...
@@ -125,6 +130,8 @@ def parse_args():
...
@@ -125,6 +130,8 @@ def parse_args():
def
main
(
args
):
def
main
(
args
):
train_prog
=
fluid
.
Program
()
train_prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
train_prog
.
random_seed
=
1000
startup_prog
.
random_seed
=
1000
with
fluid
.
program_guard
(
train_prog
,
startup_prog
):
with
fluid
.
program_guard
(
train_prog
,
startup_prog
):
with
fluid
.
unique_name
.
guard
():
with
fluid
.
unique_name
.
guard
():
sum_cost
,
avg_cost
,
predict
,
token_num
,
pyreader
=
transformer
(
sum_cost
,
avg_cost
,
predict
,
token_num
,
pyreader
=
transformer
(
...
@@ -243,24 +250,33 @@ def main(args):
...
@@ -243,24 +250,33 @@ def main(args):
if
args
.
use_py_reader
:
if
args
.
use_py_reader
:
pyreader
.
reset
()
pyreader
.
reset
()
pyreader
.
start
()
pyreader
.
start
()
break
return
reader_time
,
run_time
return
reader_time
,
run_time
@
contextlib
.
contextmanager
def
profile_context
(
profile
=
True
):
if
profile
:
with
profiler
.
profiler
(
'All'
,
'total'
,
'/tmp/profile_file'
):
yield
else
:
yield
# start-up
# start-up
init_flag
=
True
init_flag
=
True
run
(
1
)
run
(
5
)
init_flag
=
False
init_flag
=
False
# profiling
# profiling
start
=
time
.
time
()
start
=
time
.
time
()
# currently only support profiling on one device
# currently only support profiling on one device
with
profile
r
.
profiler
(
'All'
,
'total'
,
'/tmp/profile_file'
):
with
profile
_context
(
args
.
profile_ops
):
reader_time
,
run_time
=
run
(
args
.
iter_num
)
reader_time
,
run_time
=
run
(
args
.
iter_num
)
end
=
time
.
time
()
end
=
time
.
time
()
total_time
=
end
-
start
total_time
=
end
-
start
print
(
"Total time: {0}, reader time: {1} s, run time: {2} s"
.
format
(
print
(
total_time
,
np
.
sum
(
reader_time
),
np
.
sum
(
run_time
)))
"Total time: {0}, reader time: {1} s, run time: {2} s, step number: {3}"
.
format
(
total_time
,
np
.
sum
(
reader_time
),
np
.
sum
(
run_time
),
args
.
iter_num
))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
fluid/neural_machine_translation/transformer/reader.py
浏览文件 @
278e368a
...
@@ -297,9 +297,14 @@ class DataReader(object):
...
@@ -297,9 +297,14 @@ class DataReader(object):
infos
=
self
.
_sample_infos
infos
=
self
.
_sample_infos
if
self
.
_sort_type
==
SortType
.
POOL
:
if
self
.
_sort_type
==
SortType
.
POOL
:
reverse
=
True
for
i
in
range
(
0
,
len
(
infos
),
self
.
_pool_size
):
for
i
in
range
(
0
,
len
(
infos
),
self
.
_pool_size
):
# to avoid placing short next to long sentences
reverse
=
not
reverse
infos
[
i
:
i
+
self
.
_pool_size
]
=
sorted
(
infos
[
i
:
i
+
self
.
_pool_size
]
=
sorted
(
infos
[
i
:
i
+
self
.
_pool_size
],
key
=
lambda
x
:
x
.
max_len
)
infos
[
i
:
i
+
self
.
_pool_size
],
key
=
lambda
x
:
x
.
max_len
,
reverse
=
reverse
)
# concat batch
# concat batch
batches
=
[]
batches
=
[]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录