Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
11d4d39c
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看板
提交
11d4d39c
编写于
1月 08, 2019
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
forward working
test=develop
上级
b6291333
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
35 addition
and
16 deletion
+35
-16
paddle/fluid/imperative/layer.h
paddle/fluid/imperative/layer.h
+12
-9
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+3
-2
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+4
-1
python/paddle/fluid/imperative/layers.py
python/paddle/fluid/imperative/layers.py
+14
-2
python/paddle/fluid/tests/unittests/test_imperative.py
python/paddle/fluid/tests/unittests/test_imperative.py
+2
-2
未找到文件。
paddle/fluid/imperative/layer.h
浏览文件 @
11d4d39c
...
...
@@ -161,13 +161,14 @@ class Layer {
static
void
CallPythonFunc
(
py
::
object
*
callable
,
const
std
::
vector
<
framework
::
LoDTensor
>&
ins
,
std
::
vector
<
framework
::
LoDTensor
*>*
outs
)
{
std
::
vector
<
VarBase
*>*
outs
)
{
py
::
gil_scoped_acquire
guard
;
py
::
tuple
in_args
(
ins
.
size
());
for
(
size_t
i
=
0
;
i
<
ins
.
size
();
++
i
)
{
in_args
[
i
]
=
ins
[
i
].
IsInitialized
()
?
py
::
cast
(
ins
[
i
])
:
py
::
cast
(
nullptr
);
}
// TODO(panyx0718): Who owns the returned LoDTensor.
auto
ret
=
(
*
callable
)(
in_args
);
auto
ret_tuple
=
py
::
cast
<
py
::
tuple
>
(
ret
);
size_t
ret_num
=
py
::
len
(
ret_tuple
);
...
...
@@ -176,7 +177,11 @@ static void CallPythonFunc(py::object* callable,
auto
*
py_out_tensor
=
py
::
cast
<
framework
::
LoDTensor
*>
(
ret_tuple
[
i
]);
PADDLE_ENFORCE_NOT_NULL
(
py_out_tensor
,
"Output tensor %d should not be nullptr"
,
i
);
outs
->
push_back
(
py_out_tensor
);
VarBase
*
var
=
new
VarBase
();
auto
*
tensor
=
var
->
var_
->
GetMutable
<
framework
::
LoDTensor
>
();
tensor
->
ShareDataWith
(
*
py_out_tensor
);
tensor
->
set_lod
(
py_out_tensor
->
lod
());
outs
->
push_back
(
var
);
}
catch
(
py
::
cast_error
&
)
{
PADDLE_THROW
(
"The %d-th output must be LoDTensor"
,
i
);
}
...
...
@@ -187,18 +192,16 @@ class PyLayer {
public:
virtual
~
PyLayer
()
{}
static
std
::
vector
<
VarBase
>
Apply
(
py
::
object
*
callable
,
const
std
::
vector
<
VarBase
>&
inputs
)
{
std
::
vector
<
VarBase
>
outputs
;
static
std
::
vector
<
VarBase
*>
Apply
(
py
::
object
*
callable
,
const
std
::
vector
<
VarBase
>&
inputs
)
{
std
::
vector
<
framework
::
LoDTensor
>
tensor_inputs
;
std
::
vector
<
framework
::
LoDTensor
*>
tensor_outputs
;
std
::
vector
<
VarBase
*>
ret
;
for
(
const
VarBase
&
in
:
inputs
)
{
tensor_inputs
.
push_back
(
in
.
var_
->
Get
<
framework
::
LoDTensor
>
());
}
CallPythonFunc
(
callable
,
tensor_inputs
,
&
tensor_outputs
);
return
outputs
;
CallPythonFunc
(
callable
,
tensor_inputs
,
&
ret
);
return
ret
;
}
};
...
...
paddle/fluid/pybind/pybind.cc
浏览文件 @
11d4d39c
...
...
@@ -182,9 +182,10 @@ PYBIND11_MODULE(core, m) {
.
def_static
(
"apply"
,
[](
py
::
object
*
callable
,
const
std
::
vector
<
imperative
::
VarBase
>
&
inputs
)
->
std
::
vector
<
imperative
::
VarBase
>
{
->
std
::
vector
<
imperative
::
VarBase
*
>
{
return
imperative
::
PyLayer
::
Apply
(
callable
,
inputs
);
});
},
py
::
return_value_policy
::
take_ownership
);
BindTracer
(
&
m
);
...
...
python/paddle/fluid/framework.py
浏览文件 @
11d4d39c
...
...
@@ -372,7 +372,10 @@ class Variable(object):
self
.
stop_gradient
=
stop_gradient
self
.
is_data
=
is_data
if
_in_imperative_mode
():
self
.
_ivar
=
core
.
VarBase
()
if
'ivar'
in
kwargs
:
self
.
_ivar
=
kwargs
[
'ivar'
]
else
:
self
.
_ivar
=
core
.
VarBase
()
self
.
_ivar
.
desc
=
self
.
desc
self
.
_ivar
.
stop_gradient
=
stop_gradient
...
...
python/paddle/fluid/imperative/layers.py
浏览文件 @
11d4d39c
...
...
@@ -67,5 +67,17 @@ class PyLayer(core.PyLayer):
def
__call__
(
cls
,
inputs
):
inputs
=
map
(
base
.
to_variable
,
inputs
)
inputs
=
[
x
.
_ivar
for
x
in
inputs
]
sys
.
stderr
.
write
(
'%s
\n
'
%
inputs
)
return
core
.
PyLayer
.
apply
(
cls
.
forward
,
inputs
)
ivars
=
core
.
PyLayer
.
apply
(
cls
.
forward
,
inputs
)
ret
=
[]
for
ivar
in
ivars
:
tensor
=
ivar
.
value
.
get_tensor
()
block
=
framework
.
default_main_program
().
current_block
()
py_var
=
framework
.
Variable
(
block
,
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
name
=
None
,
shape
=
tensor
.
shape
(),
dtype
=
tensor
.
_dtype
(),
ivar
=
ivar
)
ret
.
append
(
py_var
)
return
ret
python/paddle/fluid/tests/unittests/test_imperative.py
浏览文件 @
11d4d39c
...
...
@@ -81,8 +81,8 @@ class TestImperative(unittest.TestCase):
def
test_pylayer
(
self
):
with
fluid
.
imperative
.
guard
():
my_py_layer
=
MyPyLayer
()
out
=
my_py_layer
([
np
.
ones
([
2
,
2
],
np
.
float32
)])
sys
.
stderr
.
write
(
'%s
\n
'
%
np
.
array
(
out
))
out
s
=
my_py_layer
([
np
.
ones
([
2
,
2
],
np
.
float32
)])
sys
.
stderr
.
write
(
'%s
\n
'
%
outs
[
0
].
_numpy
(
))
# out.backward()
def
test_layer_in_out
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录