Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
dfff52ea
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
dfff52ea
编写于
7月 09, 2021
作者:
L
Leo Chen
提交者:
GitHub
7月 09, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine varbase init function (#34052)
* remove check on kwargs * refine code, reuse commom function
上级
1412d3bc
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
38 addition
and
29 deletion
+38
-29
paddle/fluid/pybind/imperative.cc
paddle/fluid/pybind/imperative.cc
+38
-29
未找到文件。
paddle/fluid/pybind/imperative.cc
浏览文件 @
dfff52ea
...
...
@@ -146,21 +146,33 @@ static const platform::Place PyObjectToPlace(const py::object &place_obj) {
}
}
static
void
InitTensorForVarBase
(
imperative
::
VarBase
*
self
,
const
py
::
array
&
array
,
const
platform
::
Place
place
,
bool
persistable
=
false
,
bool
zero_copy
=
false
,
std
::
string
name
=
""
,
int
stop_gradient
=
-
1
)
{
if
(
name
==
""
)
{
name
=
imperative
::
GetCurrentTracer
()
->
GenerateUniqueName
(
"generated_tensor"
);
}
VLOG
(
5
)
<<
"Init Tensor as: / name: "
<<
name
<<
" / persistable: "
<<
persistable
<<
" / zero_copy: "
<<
zero_copy
// only initialize varbase, but not its tensor.
static
void
InitVarBaseOnly
(
imperative
::
VarBase
*
self
,
const
std
::
string
&
name
,
bool
persistable
=
false
,
int
stop_gradient
=
-
1
)
{
auto
name_
=
name
==
""
?
imperative
::
GetCurrentTracer
()
->
GenerateUniqueName
(
"generated_tensor"
)
:
name
;
VLOG
(
5
)
<<
"Init Tensor as: / name: "
<<
name_
<<
" / persistable: "
<<
persistable
<<
" / stop_gradient: "
<<
stop_gradient
;
new
(
self
)
imperative
::
VarBase
(
name
);
new
(
self
)
imperative
::
VarBase
(
name_
);
if
(
stop_gradient
!=
-
1
)
{
self
->
SetOverridedStopGradient
(
stop_gradient
);
}
self
->
SetPersistable
(
persistable
);
self
->
SetType
(
framework
::
proto
::
VarType
::
LOD_TENSOR
);
}
// initialize varbase and its tensor.
static
void
InitVarBaseAndTensor
(
imperative
::
VarBase
*
self
,
const
py
::
array
&
array
,
const
platform
::
Place
&
place
,
const
std
::
string
&
name
,
bool
persistable
=
false
,
bool
zero_copy
=
false
,
int
stop_gradient
=
-
1
)
{
InitVarBaseOnly
(
self
,
name
,
persistable
,
stop_gradient
);
auto
*
tensor
=
self
->
MutableVar
()
->
GetMutable
<
framework
::
LoDTensor
>
();
VLOG
(
4
)
<<
"zero_copy: "
<<
zero_copy
;
if
(
platform
::
is_cpu_place
(
place
))
{
SetTensorFromPyArray
<
platform
::
CPUPlace
>
(
tensor
,
array
,
BOOST_GET_CONST
(
platform
::
CPUPlace
,
place
),
zero_copy
);
...
...
@@ -182,26 +194,15 @@ static void InitTensorForVarBase(imperative::VarBase *self,
"Place should be one of "
"CPUPlace/XPUPlace/CUDAPlace/CUDAPinnedPlace/NPUPlace"
));
}
if
(
stop_gradient
!=
-
1
)
{
self
->
SetOverridedStopGradient
(
stop_gradient
);
}
self
->
SetPersistable
(
persistable
);
self
->
SetType
(
framework
::
proto
::
VarType
::
LOD_TENSOR
);
self
->
SetDataType
(
tensor
->
type
());
}
static
void
InitVarBaseFromNumpyWithKwargs
(
imperative
::
VarBase
*
self
,
const
py
::
kwargs
&
kwargs
)
{
VLOG
(
4
)
<<
"Init VarBase from kwargs: "
;
PADDLE_ENFORCE_EQ
(
kwargs
.
contains
(
"value"
),
true
,
platform
::
errors
::
NotFound
(
"The kwargs used to create Varbase misses argument: value"
));
auto
persistable
=
kwargs
.
contains
(
"persistable"
)
?
kwargs
[
"persistable"
].
cast
<
bool
>
()
:
false
;
auto
array
=
kwargs
.
contains
(
"value"
)
?
kwargs
[
"value"
].
cast
<
py
::
array
>
()
:
py
::
array
();
auto
zero_copy
=
kwargs
.
contains
(
"zero_copy"
)
?
kwargs
[
"zero_copy"
].
cast
<
bool
>
()
:
false
;
auto
name
=
kwargs
.
contains
(
"name"
)
?
kwargs
[
"name"
].
cast
<
std
::
string
>
()
:
""
;
...
...
@@ -209,10 +210,18 @@ static void InitVarBaseFromNumpyWithKwargs(imperative::VarBase *self,
?
kwargs
[
"stop_gradient"
].
cast
<
int
>
()
:
-
1
;
auto
default_place
=
imperative
::
GetCurrentTracer
()
->
ExpectedPlace
();
if
(
kwargs
.
contains
(
"value"
))
{
auto
array
=
kwargs
[
"value"
].
cast
<
py
::
array
>
();
// place is only used when array is given, otherwise, it is meaningless and
// ignored
auto
place
=
kwargs
.
contains
(
"place"
)
?
PyObjectToPlace
(
kwargs
[
"place"
])
:
default_place
;
InitTensorForVarBase
(
self
,
array
,
place
,
persistable
,
zero_copy
,
name
,
InitVarBaseAndTensor
(
self
,
array
,
place
,
name
,
persistable
,
zero_copy
,
stop_gradient
);
}
else
{
InitVarBaseOnly
(
self
,
name
,
persistable
,
stop_gradient
);
}
}
template
<
typename
P
>
...
...
@@ -247,7 +256,7 @@ static void InitVarBaseFromNumpyWithArgDefault(imperative::VarBase *self,
const
py
::
array
&
array
)
{
auto
place
=
imperative
::
GetCurrentTracer
()
->
ExpectedPlace
();
VLOG
(
4
)
<<
"Init VarBase from numpy at "
<<
place
;
Init
TensorForVarBase
(
self
,
array
,
place
);
Init
VarBaseAndTensor
(
self
,
array
,
place
,
""
);
}
static
void
InitVarBaseFromTensorWithArgDefault
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录