Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
311b3b44
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
311b3b44
编写于
12月 09, 2020
作者:
S
ShenLiang
提交者:
GitHub
12月 09, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix the bug where embedding can‘t be processed correctly in reducer (#29485)
* fix the bug of reducer in embedding * add comment
上级
560b4323
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
85 addition
and
13 deletion
+85
-13
python/paddle/fluid/dygraph/parallel.py
python/paddle/fluid/dygraph/parallel.py
+7
-3
python/paddle/fluid/tests/unittests/parallel_dygraph_sparse_embedding_fp64.py
...tests/unittests/parallel_dygraph_sparse_embedding_fp64.py
+78
-10
未找到文件。
python/paddle/fluid/dygraph/parallel.py
浏览文件 @
311b3b44
...
...
@@ -24,8 +24,8 @@ from paddle.fluid.dygraph import layers
from
paddle.fluid.dygraph
import
parallel_helper
from
paddle.fluid.dygraph
import
to_variable
,
no_grad
from
paddle.utils
import
deprecated
from
paddle.fluid.dygraph
import
nn
import
warnings
import
paddle
__all__
=
[
"prepare_context"
,
"ParallelEnv"
,
"DataParallel"
]
...
...
@@ -419,9 +419,13 @@ class DataParallel(layers.Layer):
# NOTE(shenliang03): Here we can only use the attributes to judge whether
# parameter is sparse(or SelectedRows). The reason is that the sparse message
# can't be obtained when bp hasn't happened yet. So if layer supports sparse parameter,
# we should add the layer here like "
n
n.Embedding".
# we should add the layer here like "
paddle.nn.layer.commo
n.Embedding".
def
check_layer_sparse
(
sublayer
):
if
isinstance
(
sublayer
,
nn
.
Embedding
):
if
isinstance
(
sublayer
,
paddle
.
nn
.
layer
.
common
.
Embedding
):
return
sublayer
.
_sparse
# NOTE(shenliang03):This is for compatibility. If paddle.fluid.dygraph.Embedding
# is removed in the future, the judgment will also be removed here.
if
isinstance
(
sublayer
,
paddle
.
fluid
.
dygraph
.
Embedding
):
return
sublayer
.
_is_sparse
return
False
...
...
python/paddle/fluid/tests/unittests/parallel_dygraph_sparse_embedding_fp64.py
浏览文件 @
311b3b44
...
...
@@ -15,14 +15,60 @@
from
__future__
import
print_function
import
numpy
as
np
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.dygraph.nn
import
Embedding
from
paddle.fluid.dygraph.base
import
to_variable
from
test_dist_base
import
runtime_main
,
TestParallelDyGraphRunnerBase
from
parallel_dygraph_sparse_embedding
import
SimpleNet
,
fake_sample_reader
,
TestSparseEmbedding
from
paddle.nn
import
Layer
,
Embedding
paddle
.
set_default_dtype
(
"float64"
)
class
SimpleNet
(
Layer
):
def
__init__
(
self
,
hidden_size
,
vocab_size
,
num_steps
=
20
,
init_scale
=
0.1
,
is_sparse
=
False
,
dtype
=
"float64"
):
super
(
SimpleNet
,
self
).
__init__
()
self
.
hidden_size
=
hidden_size
self
.
vocab_size
=
vocab_size
self
.
init_scale
=
init_scale
self
.
num_steps
=
num_steps
self
.
embedding
=
Embedding
(
self
.
vocab_size
,
self
.
hidden_size
,
sparse
=
True
,
weight_attr
=
paddle
.
ParamAttr
(
name
=
'embedding_param'
,
initializer
=
paddle
.
nn
.
initializer
.
Uniform
(
low
=-
init_scale
,
high
=
init_scale
)))
self
.
softmax_weight
=
self
.
create_parameter
(
attr
=
paddle
.
ParamAttr
(),
shape
=
[
self
.
hidden_size
,
self
.
vocab_size
],
dtype
=
dtype
,
default_initializer
=
paddle
.
nn
.
initializer
.
Uniform
(
low
=-
self
.
init_scale
,
high
=
self
.
init_scale
))
self
.
softmax_bias
=
self
.
create_parameter
(
attr
=
paddle
.
ParamAttr
(),
shape
=
[
self
.
vocab_size
],
dtype
=
dtype
,
default_initializer
=
paddle
.
nn
.
initializer
.
Uniform
(
low
=-
self
.
init_scale
,
high
=
self
.
init_scale
))
def
forward
(
self
,
input
,
label
):
x_emb
=
self
.
embedding
(
input
)
fc
=
paddle
.
matmul
(
x_emb
,
self
.
softmax_weight
)
fc
=
paddle
.
add
(
fc
,
self
.
softmax_bias
)
projection
=
paddle
.
reshape
(
fc
,
shape
=
[
-
1
,
self
.
vocab_size
])
loss
=
paddle
.
nn
.
functional
.
softmax_with_cross_entropy
(
logits
=
projection
,
label
=
label
,
soft_label
=
False
)
loss
=
paddle
.
reshape
(
loss
,
shape
=
[
-
1
,
self
.
num_steps
])
loss
=
paddle
.
mean
(
loss
,
axis
=
[
0
])
loss
=
paddle
.
sum
(
loss
)
return
loss
# global configs
batch_size
=
4
...
...
@@ -33,24 +79,46 @@ num_steps = 3
init_scale
=
0.1
class
TestSparseEmbeddingFP64
(
TestSparseEmbedding
):
def
fake_sample_reader
():
def
__reader__
():
for
i
in
range
(
batch_num
):
x_data
=
np
.
arange
(
num_steps
).
astype
(
'int64'
)
y_data
=
np
.
arange
(
1
,
1
+
num_steps
).
astype
(
'int64'
)
yield
x_data
,
y_data
return
__reader__
class
TestSparseEmbeddingFP64
(
TestParallelDyGraphRunnerBase
):
def
get_model
(
self
):
model
=
SimpleNet
(
hidden_size
=
hidden_size
,
vocab_size
=
vocab_size
,
num_steps
=
num_steps
,
init_scale
=
init_scale
,
is_sparse
=
True
,
dtype
=
"float64"
)
is_sparse
=
True
)
train_reader
=
paddle
.
batch
(
fake_sample_reader
(),
batch_size
=
batch_size
,
drop_last
=
True
)
optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.001
,
parameter_list
=
model
.
parameters
())
optimizer
=
paddle
.
optimizer
.
SGD
(
learning_rate
=
0.001
,
parameters
=
model
.
parameters
())
return
model
,
train_reader
,
optimizer
def
run_one_loop
(
self
,
model
,
optimizer
,
batch
):
x_data
=
np
.
array
([
x
[
0
].
reshape
(
3
)
for
x
in
batch
]).
astype
(
'int64'
)
y_data
=
np
.
array
([
x
[
1
].
reshape
(
3
)
for
x
in
batch
]).
astype
(
'int64'
)
x_data
=
x_data
.
reshape
((
-
1
,
num_steps
,
1
))
y_data
=
y_data
.
reshape
((
-
1
,
1
))
x
=
paddle
.
to_tensor
(
x_data
)
y
=
paddle
.
to_tensor
(
y_data
)
dy_loss
=
model
(
x
,
y
)
return
dy_loss
if
__name__
==
"__main__"
:
runtime_main
(
TestSparseEmbeddingFP64
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录