Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
6514f52e
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看板
未验证
提交
6514f52e
编写于
11月 25, 2019
作者:
W
wangchaochaohu
提交者:
GitHub
11月 25, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the fill_constant op precious problem (#21322)
* fix the fill_constant op precious problem test=develop
上级
08c19c58
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
43 addition
and
10 deletion
+43
-10
paddle/fluid/operators/fill_constant_op.cc
paddle/fluid/operators/fill_constant_op.cc
+5
-1
paddle/fluid/operators/fill_constant_op.h
paddle/fluid/operators/fill_constant_op.h
+33
-9
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+5
-0
未找到文件。
paddle/fluid/operators/fill_constant_op.cc
浏览文件 @
6514f52e
...
...
@@ -90,8 +90,12 @@ class FillConstantOpMaker : public framework::OpProtoAndCheckerMaker {
"The shape of the element in vector must be [1]."
)
.
AsDuplicable
()
.
AsDispensable
();
AddAttr
<
float
>
(
"value"
,
"(float, default 0) The value to be filled"
)
AddAttr
<
float
>
(
"value"
,
"(float, default 0
.0f
) The value to be filled"
)
.
SetDefault
(
0.0
f
);
AddAttr
<
std
::
string
>
(
"str_value"
,
"(string, default empty) The str convert to value to be filled"
)
.
SetDefault
(
""
);
AddAttr
<
bool
>
(
"force_cpu"
,
"(bool, default false) Force fill output variable to cpu "
"memory. Otherwise, fill output variable to the running "
...
...
paddle/fluid/operators/fill_constant_op.h
浏览文件 @
6514f52e
...
...
@@ -14,8 +14,9 @@ limitations under the License. */
#pragma once
#include <sstream>
#include <string>
#include <vector>
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/math_function.h"
...
...
@@ -75,13 +76,28 @@ class FillConstantKernel : public framework::OpKernel<T> {
void
Compute
(
const
paddle
::
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
data_type
=
static_cast
<
framework
::
proto
::
VarType
::
Type
>
(
ctx
.
Attr
<
int
>
(
"dtype"
));
auto
value
=
ctx
.
Attr
<
float
>
(
"value"
);
auto
str_value
=
ctx
.
Attr
<
std
::
string
>
(
"str_value"
);
auto
float_value
=
ctx
.
Attr
<
float
>
(
"value"
);
auto
force_cpu
=
ctx
.
Attr
<
bool
>
(
"force_cpu"
);
framework
::
Tensor
*
tensor
=
nullptr
;
framework
::
Variable
*
out_var
=
ctx
.
OutputVar
(
"Out"
);
T
value
;
if
(
str_value
.
empty
())
{
value
=
static_cast
<
T
>
(
float_value
);
}
else
{
std
::
stringstream
convert_stream
(
str_value
);
if
(
std
::
is_same
<
int64_t
,
T
>::
value
)
{
int64_t
tmp_value
;
convert_stream
>>
tmp_value
;
value
=
static_cast
<
T
>
(
tmp_value
);
}
else
{
double
tmp_value
;
convert_stream
>>
tmp_value
;
value
=
static_cast
<
T
>
(
tmp_value
);
}
}
auto
shape
=
GetShape
(
ctx
);
if
(
out_var
->
IsType
<
framework
::
LoDTensor
>
())
{
...
...
@@ -96,15 +112,23 @@ class FillConstantKernel : public framework::OpKernel<T> {
"supports SelectedRows and LoDTensor"
);
}
if
(
force_cpu
)
{
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
&
dev_ctx
=
*
pool
.
Get
(
ctx
.
GetPlace
());
bool
cpu_place
=
force_cpu
||
ctx
.
GetPlace
()
==
platform
::
CPUPlace
();
if
(
cpu_place
)
{
tensor
->
mutable_data
(
platform
::
CPUPlace
(),
data_type
);
}
else
{
math
::
SetConstant
<
platform
::
CPUDeviceContext
,
T
>
functor
;
functor
(
reinterpret_cast
<
const
platform
::
CPUDeviceContext
&>
(
dev_ctx
),
tensor
,
static_cast
<
T
>
(
value
));
}
#ifdef PADDLE_WITH_CUDA
if
(
!
cpu_place
)
{
tensor
->
mutable_data
(
ctx
.
GetPlace
(),
data_type
);
math
::
SetConstant
<
platform
::
CUDADeviceContext
,
T
>
functor
;
functor
(
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
dev_ctx
),
tensor
,
static_cast
<
T
>
(
value
));
}
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
&
dev_ctx
=
*
pool
.
Get
(
ctx
.
GetPlace
());
math
::
set_constant
(
dev_ctx
,
tensor
,
value
);
#endif
}
};
}
// namespace operators
...
...
python/paddle/fluid/layers/tensor.py
浏览文件 @
6514f52e
...
...
@@ -552,6 +552,11 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None):
'force_cpu'
:
force_cpu
or
force_init_on_cpu
()
}
if
convert_dtype
(
dtype
)
in
[
'int64'
,
'int32'
]:
attrs
[
'str_value'
]
=
str
(
int
(
value
))
else
:
attrs
[
'str_value'
]
=
str
(
float
(
value
))
def
_contain_var
(
one_list
):
for
ele
in
one_list
:
if
isinstance
(
ele
,
Variable
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录