Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
91f63cd4
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
91f63cd4
编写于
7月 29, 2018
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix split_ids_op and add unit test
上级
02c31458
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
68 addition
and
4 deletion
+68
-4
paddle/fluid/operators/split_ids_op.h
paddle/fluid/operators/split_ids_op.h
+8
-4
python/paddle/fluid/tests/unittests/test_split_ids_op.py
python/paddle/fluid/tests/unittests/test_split_ids_op.py
+60
-0
未找到文件。
paddle/fluid/operators/split_ids_op.h
浏览文件 @
91f63cd4
...
@@ -14,6 +14,7 @@ limitations under the License. */
...
@@ -14,6 +14,7 @@ limitations under the License. */
#pragma once
#pragma once
#include <map>
#include <vector>
#include <vector>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"
...
@@ -68,9 +69,11 @@ class SplitIdsOpKernel : public framework::OpKernel<T> {
...
@@ -68,9 +69,11 @@ class SplitIdsOpKernel : public framework::OpKernel<T> {
auto
outs
=
ctx
.
MultiOutput
<
framework
::
SelectedRows
>
(
"Out"
);
auto
outs
=
ctx
.
MultiOutput
<
framework
::
SelectedRows
>
(
"Out"
);
const
size_t
shard_num
=
outs
.
size
();
const
size_t
shard_num
=
outs
.
size
();
// get rows for outputs
// get rows for outputs
for
(
auto
&
id
:
ids_rows
)
{
std
::
map
<
int64_t
,
size_t
>
id_to_index
;
size_t
shard_id
=
static_cast
<
size_t
>
(
id
)
%
shard_num
;
for
(
size_t
i
=
0
;
i
<
ids_rows
.
size
();
++
i
)
{
outs
[
shard_id
]
->
mutable_rows
()
->
push_back
(
id
);
id_to_index
[
ids_rows
[
i
]]
=
i
;
size_t
shard_id
=
static_cast
<
size_t
>
(
ids_rows
[
i
])
%
shard_num
;
outs
[
shard_id
]
->
mutable_rows
()
->
push_back
(
ids_rows
[
i
]);
}
}
int64_t
row_width
=
ids_dims
[
1
];
int64_t
row_width
=
ids_dims
[
1
];
...
@@ -80,7 +83,8 @@ class SplitIdsOpKernel : public framework::OpKernel<T> {
...
@@ -80,7 +83,8 @@ class SplitIdsOpKernel : public framework::OpKernel<T> {
{
static_cast
<
int64_t
>
(
out
->
rows
().
size
()),
row_width
});
{
static_cast
<
int64_t
>
(
out
->
rows
().
size
()),
row_width
});
T
*
output
=
out
->
mutable_value
()
->
mutable_data
<
T
>
(
ddim
,
place
);
T
*
output
=
out
->
mutable_value
()
->
mutable_data
<
T
>
(
ddim
,
place
);
for
(
int64_t
i
=
0
;
i
<
ddim
[
0
];
++
i
)
{
for
(
int64_t
i
=
0
;
i
<
ddim
[
0
];
++
i
)
{
memcpy
(
output
+
i
*
row_width
,
ids
+
out
->
rows
()[
i
]
*
row_width
,
memcpy
(
output
+
i
*
row_width
,
ids
+
id_to_index
[
out
->
rows
()[
i
]]
*
row_width
,
row_width
*
sizeof
(
T
));
row_width
*
sizeof
(
T
));
}
}
}
}
...
...
python/paddle/fluid/tests/unittests/test_split_ids_op.py
浏览文件 @
91f63cd4
...
@@ -15,6 +15,8 @@
...
@@ -15,6 +15,8 @@
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
from
op_test
import
OpTest
from
op_test
import
OpTest
import
paddle.fluid.core
as
core
from
paddle.fluid.op
import
Operator
class
TestSplitIdsOp
(
OpTest
):
class
TestSplitIdsOp
(
OpTest
):
...
@@ -31,5 +33,63 @@ class TestSplitIdsOp(OpTest):
...
@@ -31,5 +33,63 @@ class TestSplitIdsOp(OpTest):
self
.
check_output
()
self
.
check_output
()
class
TestSpliteIds
(
unittest
.
TestCase
):
def
get_places
(
self
):
places
=
[
core
.
CPUPlace
()]
return
places
def
test_check_output
(
self
):
for
place
in
self
.
get_places
():
self
.
check_with_place
(
place
)
def
check_with_place
(
self
,
place
):
scope
=
core
.
Scope
()
rows
=
[
0
,
5
,
7
,
4
,
9
]
height
=
20
row_numel
=
2
# initialize input variable X
x
=
scope
.
var
(
'X'
).
get_selected_rows
()
x
.
set_rows
(
rows
)
x
.
set_height
(
height
)
np_array
=
np
.
ones
((
len
(
rows
),
row_numel
)).
astype
(
"float32"
)
for
i
in
range
(
len
(
rows
)):
for
j
in
range
(
row_numel
):
np_array
[
i
,
j
]
=
rows
[
i
]
+
j
x_tensor
=
x
.
get_tensor
()
x_tensor
.
set
(
np_array
,
place
)
outs_name
=
[
"out%d"
%
i
for
i
in
xrange
(
3
)]
outs
=
[
scope
.
var
(
var_name
).
get_selected_rows
()
for
var_name
in
outs_name
]
# expected output selected rows
expected_out0_rows
=
[
0
,
9
]
expected_out1_rows
=
[
7
,
4
]
expected_out2_rows
=
[
5
]
op
=
Operator
(
"split_ids"
,
Ids
=
"X"
,
Out
=
outs_name
)
op
.
run
(
scope
,
place
)
self
.
assertEqual
(
outs
[
0
].
rows
(),
expected_out0_rows
)
self
.
assertEqual
(
outs
[
1
].
rows
(),
expected_out1_rows
)
self
.
assertEqual
(
outs
[
2
].
rows
(),
expected_out2_rows
)
self
.
assertAlmostEqual
(
0.0
,
np
.
array
(
outs
[
0
].
get_tensor
())[
0
,
0
])
self
.
assertAlmostEqual
(
1.0
,
np
.
array
(
outs
[
0
].
get_tensor
())[
0
,
1
])
self
.
assertAlmostEqual
(
9.0
,
np
.
array
(
outs
[
0
].
get_tensor
())[
1
,
0
])
self
.
assertAlmostEqual
(
10.0
,
np
.
array
(
outs
[
0
].
get_tensor
())[
1
,
1
])
self
.
assertAlmostEqual
(
7.0
,
np
.
array
(
outs
[
1
].
get_tensor
())[
0
,
0
])
self
.
assertAlmostEqual
(
8.0
,
np
.
array
(
outs
[
1
].
get_tensor
())[
0
,
1
])
self
.
assertAlmostEqual
(
4.0
,
np
.
array
(
outs
[
1
].
get_tensor
())[
1
,
0
])
self
.
assertAlmostEqual
(
5.0
,
np
.
array
(
outs
[
1
].
get_tensor
())[
1
,
1
])
self
.
assertAlmostEqual
(
5.0
,
np
.
array
(
outs
[
2
].
get_tensor
())[
0
,
0
])
self
.
assertAlmostEqual
(
6.0
,
np
.
array
(
outs
[
2
].
get_tensor
())[
0
,
1
])
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录