Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
e2341b21
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
e2341b21
编写于
9月 14, 2021
作者:
L
liu zhengxi
提交者:
GitHub
9月 14, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Transfer dynamic_decode and BeamSearchDecoder to v2 op (#35656)
上级
91cf918f
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
10 addition
and
11 deletion
+10
-11
python/paddle/fluid/layers/rnn.py
python/paddle/fluid/layers/rnn.py
+10
-11
未找到文件。
python/paddle/fluid/layers/rnn.py
浏览文件 @
e2341b21
...
@@ -958,7 +958,7 @@ class BeamSearchDecoder(Decoder):
...
@@ -958,7 +958,7 @@ class BeamSearchDecoder(Decoder):
x
=
nn
.
unsqueeze
(
x
,
[
1
])
# [batch_size, 1, ...]
x
=
nn
.
unsqueeze
(
x
,
[
1
])
# [batch_size, 1, ...]
expand_times
=
[
1
]
*
len
(
x
.
shape
)
expand_times
=
[
1
]
*
len
(
x
.
shape
)
expand_times
[
1
]
=
beam_size
expand_times
[
1
]
=
beam_size
x
=
nn
.
expand
(
x
,
expand_times
)
# [batch_size, beam_size, ...]
x
=
paddle
.
tile
(
x
,
expand_times
)
# [batch_size, beam_size, ...]
x
=
nn
.
transpose
(
x
,
list
(
range
(
2
,
len
(
x
.
shape
)))
+
x
=
nn
.
transpose
(
x
,
list
(
range
(
2
,
len
(
x
.
shape
)))
+
[
0
,
1
])
# [..., batch_size, beam_size]
[
0
,
1
])
# [..., batch_size, beam_size]
# use 0 to copy to avoid wrong shape
# use 0 to copy to avoid wrong shape
...
@@ -1024,7 +1024,7 @@ class BeamSearchDecoder(Decoder):
...
@@ -1024,7 +1024,7 @@ class BeamSearchDecoder(Decoder):
x
=
nn
.
unsqueeze
(
x
,
[
1
])
x
=
nn
.
unsqueeze
(
x
,
[
1
])
expand_times
=
[
1
]
*
len
(
x
.
shape
)
expand_times
=
[
1
]
*
len
(
x
.
shape
)
expand_times
[
1
]
=
self
.
beam_size
expand_times
[
1
]
=
self
.
beam_size
x
=
nn
.
expand
(
x
,
expand_times
)
x
=
paddle
.
tile
(
x
,
expand_times
)
return
x
return
x
def
_mask_probs
(
self
,
probs
,
finished
):
def
_mask_probs
(
self
,
probs
,
finished
):
...
@@ -1050,7 +1050,7 @@ class BeamSearchDecoder(Decoder):
...
@@ -1050,7 +1050,7 @@ class BeamSearchDecoder(Decoder):
# TODO: use where_op
# TODO: use where_op
finished
=
tensor
.
cast
(
finished
,
dtype
=
probs
.
dtype
)
finished
=
tensor
.
cast
(
finished
,
dtype
=
probs
.
dtype
)
probs
=
nn
.
elementwise_mul
(
probs
=
nn
.
elementwise_mul
(
nn
.
expand
(
nn
.
unsqueeze
(
finished
,
[
2
]),
[
1
,
1
,
self
.
vocab_size
]),
paddle
.
tile
(
nn
.
unsqueeze
(
finished
,
[
2
]),
[
1
,
1
,
self
.
vocab_size
]),
self
.
noend_mask_tensor
,
self
.
noend_mask_tensor
,
axis
=-
1
)
-
nn
.
elementwise_mul
(
axis
=-
1
)
-
nn
.
elementwise_mul
(
probs
,
(
finished
-
1
),
axis
=
0
)
probs
,
(
finished
-
1
),
axis
=
0
)
...
@@ -1080,7 +1080,7 @@ class BeamSearchDecoder(Decoder):
...
@@ -1080,7 +1080,7 @@ class BeamSearchDecoder(Decoder):
batch_size
,
batch_size
,
indices
.
dtype
)
if
batch_size
.
dtype
!=
indices
.
dtype
else
batch_size
indices
.
dtype
)
if
batch_size
.
dtype
!=
indices
.
dtype
else
batch_size
batch_size
.
stop_gradient
=
True
# TODO: remove this
batch_size
.
stop_gradient
=
True
# TODO: remove this
batch_pos
=
nn
.
expand
(
batch_pos
=
paddle
.
tile
(
nn
.
unsqueeze
(
nn
.
unsqueeze
(
tensor
.
range
(
tensor
.
range
(
0
,
batch_size
,
1
,
dtype
=
indices
.
dtype
),
[
1
]),
0
,
batch_size
,
1
,
dtype
=
indices
.
dtype
),
[
1
]),
...
@@ -1140,12 +1140,11 @@ class BeamSearchDecoder(Decoder):
...
@@ -1140,12 +1140,11 @@ class BeamSearchDecoder(Decoder):
init_cell_states
=
map_structure
(
self
.
_expand_to_beam_size
,
init_cell_states
=
map_structure
(
self
.
_expand_to_beam_size
,
initial_cell_states
)
initial_cell_states
)
# TODO: use fill_constant when support variable shape
init_inputs
=
paddle
.
full
(
init_inputs
=
nn
.
expand
(
shape
=
[
self
.
batch_size
,
self
.
beam_size
],
nn
.
unsqueeze
(
fill_value
=
self
.
start_token_tensor
,
nn
.
expand
(
self
.
start_token_tensor
,
[
self
.
batch_size
]),
[
1
]),
dtype
=
self
.
start_token_tensor
.
dtype
)
[
1
,
self
.
beam_size
])
log_probs
=
paddle
.
tile
(
log_probs
=
nn
.
expand
(
tensor
.
assign
(
tensor
.
assign
(
np
.
array
(
np
.
array
(
[[
0.
]
+
[
-
self
.
kinf
]
*
(
self
.
beam_size
-
1
)],
[[
0.
]
+
[
-
self
.
kinf
]
*
(
self
.
beam_size
-
1
)],
...
@@ -1213,7 +1212,7 @@ class BeamSearchDecoder(Decoder):
...
@@ -1213,7 +1212,7 @@ class BeamSearchDecoder(Decoder):
scores
=
log_probs
scores
=
log_probs
scores
=
nn
.
reshape
(
scores
,
[
-
1
,
self
.
beam_size
*
self
.
vocab_size
])
scores
=
nn
.
reshape
(
scores
,
[
-
1
,
self
.
beam_size
*
self
.
vocab_size
])
# TODO: add grad for topk then this beam search can be used to train
# TODO: add grad for topk then this beam search can be used to train
topk_scores
,
topk_indices
=
nn
.
topk
(
input
=
scores
,
k
=
self
.
beam_size
)
topk_scores
,
topk_indices
=
paddle
.
topk
(
x
=
scores
,
k
=
self
.
beam_size
)
beam_indices
=
nn
.
elementwise_floordiv
(
topk_indices
,
beam_indices
=
nn
.
elementwise_floordiv
(
topk_indices
,
self
.
vocab_size_tensor
)
self
.
vocab_size_tensor
)
token_indices
=
nn
.
elementwise_mod
(
topk_indices
,
self
.
vocab_size_tensor
)
token_indices
=
nn
.
elementwise_mod
(
topk_indices
,
self
.
vocab_size_tensor
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录