Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ca327508
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
提交
ca327508
编写于
4月 17, 2018
作者:
Y
Yancey1989
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
ed6241cd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
47 addition
and
17 deletion
+47
-17
paddle/fluid/framework/selected_rows.cc
paddle/fluid/framework/selected_rows.cc
+27
-10
paddle/fluid/framework/selected_rows.h
paddle/fluid/framework/selected_rows.h
+3
-2
paddle/fluid/framework/selected_rows_test.cc
paddle/fluid/framework/selected_rows_test.cc
+14
-4
paddle/fluid/operators/sgd_op.h
paddle/fluid/operators/sgd_op.h
+3
-1
未找到文件。
paddle/fluid/framework/selected_rows.cc
浏览文件 @
ca327508
...
...
@@ -38,10 +38,10 @@ struct ReAllocateVisitor {
framework
::
DDim
dims_
;
};
struct
Tensor
Sliced
CopyVisitor
{
Tensor
Sliced
CopyVisitor
(
const
platform
::
Place
&
place
,
framework
::
Tensor
*
dst
,
int64_t
dst_offset
,
const
framework
::
Tensor
src
,
int64_t
src_offset
,
int64_t
size
)
struct
TensorCopyVisitor
{
TensorCopyVisitor
(
const
platform
::
Place
&
place
,
framework
::
Tensor
*
dst
,
int64_t
dst_offset
,
const
framework
::
Tensor
src
,
int64_t
src_offset
,
int64_t
size
)
:
place_
(
place
),
dst_
(
dst
),
dst_offset_
(
dst_offset
),
...
...
@@ -121,10 +121,27 @@ bool SelectedRows::HasKey(int64_t key) const {
:
true
;
}
Tensor
SelectedRows
::
Get
(
int64_t
key
)
const
{
bool
SelectedRows
::
Get
(
int64_t
key
,
framework
::
Tensor
*
value
,
int64_t
row
)
const
{
int64_t
index
=
Index
(
key
);
PADDLE_ENFORCE_GE
(
index
,
0
,
"The key should be exists in the Table."
);
return
value_
->
Slice
(
index
,
index
+
1
);
PADDLE_ENFORCE
(
value
->
IsInitialized
(),
"The value tensor should be initialized."
);
int64_t
value_width
=
value
->
numel
()
/
value
->
dims
()[
0
];
PADDLE_ENFORCE_EQ
(
value_width
,
value_
->
numel
()
/
value_
->
dims
()[
0
],
"output tensor should have the same shape with table "
"execpt the dims[0]."
);
// TODO(Yancey1989): support other place
platform
::
CPUPlace
cpu
;
framework
::
VisitDataType
(
framework
::
ToDataType
(
value_
->
type
()),
TensorCopyVisitor
(
cpu
,
value
,
row
*
value_width
,
*
value_
.
get
(),
index
*
value_width
,
value_width
));
return
true
;
}
bool
SelectedRows
::
Set
(
int64_t
key
,
const
framework
::
Tensor
&
value
)
{
...
...
@@ -143,7 +160,7 @@ bool SelectedRows::Set(int64_t key, const framework::Tensor& value) {
rows_
.
push_back
(
key
);
index
=
rows_
.
size
()
-
1
;
is_new_key
=
true
;
// whether need to resize the
valu
e
// whether need to resize the
tabl
e
if
(
static_cast
<
int64_t
>
(
rows_
.
size
())
>
value_
->
dims
()[
0
])
{
auto
dims
=
value_
->
dims
();
dims
[
0
]
=
(
dims
[
0
]
+
1
)
<<
1
;
...
...
@@ -154,9 +171,9 @@ bool SelectedRows::Set(int64_t key, const framework::Tensor& value) {
framework
::
VisitDataType
(
framework
::
ToDataType
(
value
.
type
()),
Tensor
Sliced
CopyVisitor
(
cpu
,
value_
.
get
(),
index
*
value_
->
numel
()
/
value_
->
dims
()[
0
]
,
value
,
static_cast
<
int64_t
>
(
0
),
value
.
numel
()));
TensorCopyVisitor
(
cpu
,
value_
.
get
(),
index
*
value_
->
numel
()
/
value_
->
dims
()[
0
],
value
,
static_cast
<
int64_t
>
(
0
),
value
.
numel
()));
return
is_new_key
;
}
...
...
paddle/fluid/framework/selected_rows.h
浏览文件 @
ca327508
...
...
@@ -62,9 +62,10 @@ class SelectedRows {
* @brief Get a value by the specified key, if the
* key does not exists, this function would throw an exception.
*
* @return
a sliced tensor
* @return
true if the Get operation successed.
*/
Tensor
Get
(
int64_t
key
)
const
;
bool
Get
(
int64_t
key
,
framework
::
Tensor
*
tensor
,
int64_t
row
=
0
)
const
;
/*
* @brief Set a key-value pair into the table.
...
...
paddle/fluid/framework/selected_rows_test.cc
浏览文件 @
ca327508
...
...
@@ -62,6 +62,10 @@ TEST_F(SelectedRowsTester, SerializeAndDeseralize) {
TEST_F
(
SelectedRowsTester
,
Table
)
{
platform
::
CPUPlace
cpu
;
SelectedRows
table
;
// initialize a sparse table
table
.
mutable_value
()
->
Resize
(
framework
::
make_ddim
({
1
,
100
}));
table
.
mutable_value
()
->
mutable_data
<
float
>
(
cpu
);
table
.
mutable_rows
()
->
push_back
(
1
);
int64_t
key
=
10000
;
framework
::
Tensor
value
;
...
...
@@ -69,15 +73,21 @@ TEST_F(SelectedRowsTester, Table) {
auto
ptr
=
value
.
mutable_data
<
float
>
(
cpu
);
ptr
[
0
]
=
static_cast
<
float
>
(
10
);
ASSERT_EQ
(
table
.
rows
().
size
(),
static_cast
<
size_t
>
(
0
));
ASSERT_EQ
(
table
.
rows
().
size
(),
static_cast
<
size_t
>
(
1
));
ASSERT_EQ
(
table
.
HasKey
(
key
),
false
);
table
.
Set
(
key
,
value
);
ASSERT_EQ
(
table
.
rows
().
size
(),
static_cast
<
size_t
>
(
1
));
ASSERT_EQ
(
table
.
rows
().
size
(),
static_cast
<
size_t
>
(
2
));
ASSERT_EQ
(
table
.
HasKey
(
key
),
true
);
ASSERT_EQ
(
table
.
value
().
dims
()[
0
],
static_cast
<
int64_t
>
(
2
));
ASSERT_EQ
(
table
.
Get
(
key
).
data
<
float
>
()[
0
],
static_cast
<
float
>
(
10
));
// check re-allocate
ASSERT_EQ
(
table
.
value
().
dims
()[
0
],
static_cast
<
int64_t
>
(
4
));
framework
::
Tensor
get_value
;
get_value
.
mutable_data
<
float
>
(
framework
::
make_ddim
({
20
,
100
}),
cpu
);
table
.
Get
(
key
,
&
get_value
,
10
);
ASSERT_EQ
(
get_value
.
data
<
float
>
()[
10
*
100
],
static_cast
<
float
>
(
10
));
}
}
// namespace framework
...
...
paddle/fluid/operators/sgd_op.h
浏览文件 @
ca327508
...
...
@@ -107,7 +107,9 @@ class SGDOpKernel : public framework::OpKernel<T> {
for
(
size_t
i
=
0
;
i
<
grad
.
rows
().
size
();
i
++
)
{
PADDLE_ENFORCE
(
grad
.
rows
()[
i
]
<
grad
.
height
(),
"Input rows index should less than height"
);
int64_t
id_index
=
param
.
index
(
grad
.
rows
()[
i
]);
int64_t
id_index
=
param
.
Index
(
grad
.
rows
()[
i
]);
PADDLE_ENFORCE_GE
(
id_index
,
static_cast
<
int64_t
>
(
0
),
"id should be in the table"
);
for
(
size_t
j
=
0
;
j
<
grad_row_width
;
j
++
)
{
out_data
[
id_index
*
grad_row_width
+
j
]
-=
lr
[
0
]
*
grad_data
[
i
*
grad_row_width
+
j
];
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录