Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
241712a4
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看板
未验证
提交
241712a4
编写于
1月 05, 2021
作者:
L
liu zhengxi
提交者:
GitHub
1月 05, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add static predict (#5147)
* add static predict * rm position_encoding_init * fix static predict
上级
9ddc050e
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
126 addition
and
4 deletion
+126
-4
PaddleNLP/benchmark/transformer/static/predict.py
PaddleNLP/benchmark/transformer/static/predict.py
+121
-0
PaddleNLP/benchmark/transformer/static/train.py
PaddleNLP/benchmark/transformer/static/train.py
+1
-1
PaddleNLP/examples/machine_translation/transformer/train.py
PaddleNLP/examples/machine_translation/transformer/train.py
+1
-1
PaddleNLP/paddlenlp/transformers/transformer/modeling.py
PaddleNLP/paddlenlp/transformers/transformer/modeling.py
+3
-2
未找到文件。
PaddleNLP/benchmark/transformer/static/predict.py
0 → 100644
浏览文件 @
241712a4
import
os
import
time
import
sys
import
argparse
import
logging
import
numpy
as
np
import
yaml
from
attrdict
import
AttrDict
from
pprint
import
pprint
import
paddle
from
paddlenlp.transformers
import
InferTransformerModel
sys
.
path
.
append
(
"../"
)
import
reader
FORMAT
=
'%(asctime)s-%(levelname)s: %(message)s'
logging
.
basicConfig
(
level
=
logging
.
INFO
,
format
=
FORMAT
)
logger
=
logging
.
getLogger
(
__name__
)
def
parse_args
():
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--config"
,
default
=
"../configs/transformer.big.yaml"
,
type
=
str
,
help
=
"Path of the config file. "
)
args
=
parser
.
parse_args
()
return
args
def
post_process_seq
(
seq
,
bos_idx
,
eos_idx
,
output_bos
=
False
,
output_eos
=
False
):
"""
Post-process the decoded sequence.
"""
eos_pos
=
len
(
seq
)
-
1
for
i
,
idx
in
enumerate
(
seq
):
if
idx
==
eos_idx
:
eos_pos
=
i
break
seq
=
[
idx
for
idx
in
seq
[:
eos_pos
+
1
]
if
(
output_bos
or
idx
!=
bos_idx
)
and
(
output_eos
or
idx
!=
eos_idx
)
]
return
seq
def
do_train
(
args
):
paddle
.
enable_static
()
if
args
.
use_gpu
:
place
=
paddle
.
set_device
(
"gpu:0"
)
else
:
place
=
paddle
.
set_device
(
"cpu"
)
# Define data loader
test_loader
,
to_tokens
=
reader
.
create_infer_loader
(
args
)
test_program
=
paddle
.
static
.
Program
()
startup_program
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
test_program
,
startup_program
):
src_word
=
paddle
.
static
.
data
(
name
=
"src_word"
,
shape
=
[
None
,
None
],
dtype
=
"int64"
)
# Define model
transformer
=
InferTransformerModel
(
src_vocab_size
=
args
.
src_vocab_size
,
trg_vocab_size
=
args
.
trg_vocab_size
,
max_length
=
args
.
max_length
+
1
,
n_layer
=
args
.
n_layer
,
n_head
=
args
.
n_head
,
d_model
=
args
.
d_model
,
d_inner_hid
=
args
.
d_inner_hid
,
dropout
=
args
.
dropout
,
weight_sharing
=
args
.
weight_sharing
,
bos_id
=
args
.
bos_idx
,
eos_id
=
args
.
eos_idx
,
beam_size
=
args
.
beam_size
,
max_out_len
=
args
.
max_out_len
)
finished_seq
=
transformer
(
src_word
=
src_word
)
test_program
=
test_program
.
clone
(
for_test
=
True
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_program
)
assert
(
args
.
init_from_params
),
"must set init_from_params to load parameters"
paddle
.
static
.
load
(
test_program
,
os
.
path
.
join
(
args
.
init_from_params
,
"transformer"
),
exe
)
print
(
"finish initing model from params from %s"
%
(
args
.
init_from_params
))
f
=
open
(
args
.
output_file
,
"w"
)
for
data
in
test_loader
:
finished_sequence
,
=
exe
.
run
(
test_program
,
feed
=
{
'src_word'
:
data
[
0
]},
fetch_list
=
finished_seq
.
name
)
finished_sequence
=
finished_sequence
.
transpose
([
0
,
2
,
1
])
for
ins
in
finished_sequence
:
for
beam_idx
,
beam
in
enumerate
(
ins
):
if
beam_idx
>=
args
.
n_best
:
break
id_list
=
post_process_seq
(
beam
,
args
.
bos_idx
,
args
.
eos_idx
)
word_list
=
to_tokens
(
id_list
)
sequence
=
" "
.
join
(
word_list
)
+
"
\n
"
f
.
write
(
sequence
)
paddle
.
disable_static
()
if
__name__
==
"__main__"
:
ARGS
=
parse_args
()
yaml_file
=
ARGS
.
config
with
open
(
yaml_file
,
'rt'
)
as
f
:
args
=
AttrDict
(
yaml
.
safe_load
(
f
))
pprint
(
args
)
do_train
(
args
)
PaddleNLP/benchmark/transformer/static/train.py
浏览文件 @
241712a4
...
...
@@ -12,7 +12,7 @@ from pprint import pprint
import
paddle
import
paddle.distributed
as
dist
from
paddlenlp.transformers
import
TransformerModel
,
CrossEntropyCriterion
,
position_encoding_init
from
paddlenlp.transformers
import
TransformerModel
,
CrossEntropyCriterion
sys
.
path
.
append
(
"../"
)
import
reader
...
...
PaddleNLP/examples/machine_translation/transformer/train.py
浏览文件 @
241712a4
...
...
@@ -11,7 +11,7 @@ import paddle
import
paddle.distributed
as
dist
import
reader
from
paddlenlp.transformers
import
TransformerModel
,
CrossEntropyCriterion
,
position_encoding_init
from
paddlenlp.transformers
import
TransformerModel
,
CrossEntropyCriterion
from
paddlenlp.utils.log
import
logger
...
...
PaddleNLP/paddlenlp/transformers/transformer/modeling.py
浏览文件 @
241712a4
...
...
@@ -167,7 +167,7 @@ class TransformerBeamSearchDecoder(nn.decode.BeamSearchDecoder):
return
c
def
_split_batch_beams_with_var_dim
(
self
,
c
):
var_dim_size
=
c
.
shape
[
self
.
var_dim_in_state
]
var_dim_size
=
paddle
.
shape
(
c
)
[
self
.
var_dim_in_state
]
c
=
paddle
.
reshape
(
c
,
[
-
1
,
self
.
beam_size
]
+
[
int
(
size
)
...
...
@@ -374,6 +374,7 @@ class InferTransformerModel(TransformerModel):
max_step_num
=
self
.
max_out_len
,
memory
=
enc_output
,
trg_src_attn_bias
=
trg_src_attn_bias
,
static_cache
=
static_cache
)
static_cache
=
static_cache
,
is_test
=
True
)
return
rs
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录