Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
aeb74af5
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看板
提交
aeb74af5
编写于
11月 26, 2018
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
allow operator to run imperatively
上级
b1f6fda5
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
107 addition
and
52 deletion
+107
-52
paddle/fluid/framework/feed_fetch_method.cc
paddle/fluid/framework/feed_fetch_method.cc
+1
-9
paddle/fluid/framework/ir/graph.cc
paddle/fluid/framework/ir/graph.cc
+2
-3
paddle/fluid/imperative/tracer.h
paddle/fluid/imperative/tracer.h
+42
-1
paddle/fluid/pybind/imperative.cc
paddle/fluid/pybind/imperative.cc
+15
-1
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+1
-0
python/paddle/fluid/imperative/layers.py
python/paddle/fluid/imperative/layers.py
+36
-22
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+1
-0
python/paddle/fluid/tests/unittests/test_imperative.py
python/paddle/fluid/tests/unittests/test_imperative.py
+9
-16
未找到文件。
paddle/fluid/framework/feed_fetch_method.cc
浏览文件 @
aeb74af5
...
...
@@ -58,15 +58,7 @@ LoDTensor& GetFetchVariable(const Scope& scope, const std::string& var_name,
LoDTensor
&
GetVariableTensor
(
const
Scope
&
scope
,
const
std
::
string
&
var_name
)
{
Variable
*
var
=
scope
.
FindVar
(
var_name
);
PADDLE_ENFORCE
(
var
,
"%s no in scope"
,
var_name
);
// TODO(panyx0718): hack, remove it once we run oprerator.
LoDTensor
*
tensor
=
var
->
GetMutable
<
LoDTensor
>
();
int
numel
=
10
;
float
*
data
=
tensor
->
mutable_data
<
float
>
(
framework
::
make_ddim
({
numel
}),
platform
::
CPUPlace
(),
sizeof
(
float
)
*
numel
);
for
(
size_t
i
=
0
;
i
<
numel
;
++
i
)
data
[
i
]
=
1
;
PADDLE_ENFORCE
(
var
->
IsType
<
LoDTensor
>
(),
"Variable is not LoDTensor"
);
PADDLE_ENFORCE
(
var
->
IsType
<
LoDTensor
>
(),
"Only support lod tensor now."
);
return
*
var
->
GetMutable
<
LoDTensor
>
();
}
...
...
paddle/fluid/framework/ir/graph.cc
浏览文件 @
aeb74af5
...
...
@@ -38,9 +38,8 @@ void CheckProgram(const ProgramDesc &program) {
switch
(
role_id
)
{
case
_INT
(
OpRole
::
kForward
):
if
(
visit
.
find
(
_INT
(
OpRole
::
kBackward
))
!=
visit
.
end
())
{
LOG
(
ERROR
)
<<
"Cannot add backward operator before forward operator %s."
<<
op
->
Type
();
LOG
(
ERROR
)
<<
"Cannot add backward operator before forward operator "
<<
op
->
Type
();
}
break
;
case
_INT
(
OpRole
::
kBackward
):
...
...
paddle/fluid/imperative/tracer.h
浏览文件 @
aeb74af5
...
...
@@ -14,8 +14,12 @@
#pragma once
#include <string>
#include <vector>
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/imperative/engine.h"
namespace
paddle
{
...
...
@@ -26,10 +30,47 @@ class Tracer {
Tracer
()
{}
void
Trace
(
framework
::
OpDesc
*
op_desc
)
{
LOG
(
ERROR
)
<<
"tracing "
<<
op_desc
->
Type
();
LOG
(
ERROR
)
<<
"tracer tracing "
<<
op_desc
->
Type
();
op_desc
->
InferShape
(
*
block_
);
op_desc
->
InferVarType
(
block_
);
std
::
unique_ptr
<
framework
::
OperatorBase
>
op
=
framework
::
OpRegistry
::
CreateOp
(
*
op_desc
);
for
(
const
std
::
string
&
vname
:
op_desc
->
InputArgumentNames
())
{
framework
::
Variable
*
var
=
scope_
->
Var
(
vname
);
if
(
!
var
->
IsInitialized
())
{
framework
::
VarDesc
*
var_desc
=
block_
->
FindVar
(
vname
);
if
(
var_desc
->
GetType
()
==
framework
::
proto
::
VarType
::
LOD_TENSOR
)
{
var
->
GetMutable
<
framework
::
LoDTensor
>
();
}
else
{
LOG
(
ERROR
)
<<
"tracer doesn't support yet"
;
}
}
}
for
(
const
std
::
string
&
vname
:
op_desc
->
OutputArgumentNames
())
{
framework
::
Variable
*
var
=
scope_
->
Var
(
vname
);
if
(
!
var
->
IsInitialized
())
{
framework
::
VarDesc
*
var_desc
=
block_
->
FindVar
(
vname
);
if
(
var_desc
->
GetType
()
==
framework
::
proto
::
VarType
::
LOD_TENSOR
)
{
var
->
GetMutable
<
framework
::
LoDTensor
>
();
}
else
{
LOG
(
ERROR
)
<<
"tracer doesn't support yet"
;
}
}
}
op
->
Run
(
*
scope_
,
platform
::
CPUPlace
());
}
void
SetScope
(
framework
::
Scope
*
scope
)
{
scope_
=
scope
;
}
void
SetBlock
(
framework
::
BlockDesc
*
block
)
{
block_
=
block
;
}
framework
::
Scope
*
Scope
()
const
{
return
scope_
;
}
framework
::
BlockDesc
*
Block
()
const
{
return
block_
;
}
private:
framework
::
BlockDesc
*
block_
;
framework
::
Scope
*
scope_
;
std
::
vector
<
Runnable
*>
runnables_
;
};
...
...
paddle/fluid/pybind/imperative.cc
浏览文件 @
aeb74af5
...
...
@@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/pybind/imperative.h"
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/imperative/tracer.h"
namespace
paddle
{
...
...
@@ -22,7 +24,19 @@ namespace pybind {
void
BindTracer
(
pybind11
::
module
*
m
)
{
pybind11
::
class_
<
imperative
::
Tracer
>
(
*
m
,
"Tracer"
,
""
)
.
def
(
pybind11
::
init
<>
())
.
def
(
"trace"
,
&
imperative
::
Tracer
::
Trace
);
.
def
(
"trace"
,
&
imperative
::
Tracer
::
Trace
)
.
def_property
(
"scope"
,
[](
const
imperative
::
Tracer
&
self
)
{
return
self
.
Scope
();
},
[](
imperative
::
Tracer
&
self
,
framework
::
Scope
*
scope
)
{
self
.
SetScope
(
scope
);
},
R"DOC()DOC"
)
.
def_property
(
"block"
,
[](
const
imperative
::
Tracer
&
self
)
{
return
self
.
Block
();
},
[](
imperative
::
Tracer
&
self
,
framework
::
BlockDesc
*
block
)
{
self
.
SetBlock
(
block
);
},
R"DOC()DOC"
);
}
}
// namespace pybind
...
...
paddle/fluid/pybind/pybind.cc
浏览文件 @
aeb74af5
...
...
@@ -159,6 +159,7 @@ PYBIND11_MODULE(core, m) {
self
.
mutable_data
<
float
>
(
place
);
})
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
float
>
)
.
def
(
"set_float"
,
PyCPUTensorSetFromArray
<
float
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
int
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
double
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
int64_t
>
)
...
...
python/paddle/fluid/imperative/layers.py
浏览文件 @
aeb74af5
...
...
@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
contextlib
import
sys
import
numpy
as
np
...
...
@@ -21,33 +22,46 @@ from paddle.fluid import framework
__all__
=
[
'PyLayer'
]
@
contextlib
.
contextmanager
def
trace_scope
(
scope
,
block
):
tmp_scope
=
framework
.
_imperative_tracer
().
scope
tmp_block
=
framework
.
_imperative_tracer
().
block
framework
.
_imperative_tracer
().
scope
=
scope
framework
.
_imperative_tracer
().
block
=
block
yield
framework
.
_imperative_tracer
().
scope
=
tmp_scope
framework
.
_imperative_tracer
().
block
=
tmp_block
class
PyLayer
(
core
.
Layer
):
def
__init__
(
self
):
self
.
_scope
=
core
.
Scope
()
self
.
_block
=
framework
.
default_main_program
().
current_block
()
def
__call__
(
self
,
inputs
):
if
not
isinstance
(
inputs
,
list
)
and
not
isinstance
(
inputs
,
tuple
):
inputs
=
[
inputs
]
var_inputs
=
[]
for
x
in
inputs
:
if
isinstance
(
x
,
np
.
ndarray
):
tensor
=
core
.
LoDTensor
()
tensor
.
set
(
x
,
core
.
CPUPlace
())
x
=
framework
.
Variable
(
framework
.
default_main_program
().
current_block
(),
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
name
=
None
,
shape
=
x
.
shape
,
dtype
=
x
.
dtype
)
elif
not
isinstance
(
x
,
framework
.
Variable
):
raise
ValueError
(
"not var or ndarray %s"
%
type
(
x
))
self
.
_scope
.
var
(
x
.
name
)
var_inputs
.
append
(
x
)
outputs
=
self
.
forward
(
var_inputs
)
for
out
in
outputs
:
self
.
_scope
.
var
(
out
.
name
)
return
outputs
with
trace_scope
(
self
.
_scope
,
self
.
_block
.
desc
):
if
not
isinstance
(
inputs
,
list
)
and
not
isinstance
(
inputs
,
tuple
):
inputs
=
[
inputs
]
var_inputs
=
[]
for
x
in
inputs
:
if
isinstance
(
x
,
np
.
ndarray
):
py_var
=
framework
.
Variable
(
self
.
_block
,
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
name
=
None
,
shape
=
x
.
shape
,
dtype
=
x
.
dtype
)
var
=
self
.
_scope
.
var
(
py_var
.
name
)
tensor
=
var
.
get_tensor
()
tensor
.
set_float
(
x
,
core
.
CPUPlace
())
var_inputs
.
append
(
py_var
)
elif
isinstance
(
x
,
framework
.
Variable
):
var_inputs
.
append
(
x
)
else
:
raise
ValueError
(
"not var or ndarray %s"
%
type
(
x
))
outputs
=
self
.
forward
(
var_inputs
)
return
outputs
def
forward
(
self
,
inputs
):
print
(
"at python."
)
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
aeb74af5
...
...
@@ -17,6 +17,7 @@ All layers just related to the neural network.
from
__future__
import
print_function
import
sys
import
numpy
as
np
import
os
from
..layer_helper
import
LayerHelper
...
...
python/paddle/fluid/tests/unittests/test_imperative.py
浏览文件 @
aeb74af5
...
...
@@ -31,25 +31,18 @@ class MyLayer(fluid.imperative.PyLayer):
class
TestImperative
(
unittest
.
TestCase
):
def
test_layer
(
self
):
cl
=
core
.
Layer
()
cl
.
forward
([])
l
=
fluid
.
imperative
.
PyLayer
()
l
.
forward
([])
def
test_imperative_trace
(
self
):
with
fluid
.
imperative
.
guard
():
self
.
assertTrue
(
fluid
.
imperative
.
enabled
())
x
=
fluid
.
layers
.
data
(
name
=
'abc'
,
shape
=
[
3
,
4
],
dtype
=
'float32'
)
for
_
in
xrange
(
2
):
x
=
fluid
.
layers
.
relu
(
x
)
x
=
fluid
.
layers
.
elementwise_mul
(
x
,
x
)
self
.
assertIsNotNone
(
x
)
cl
=
core
.
Layer
()
cl
.
forward
([])
l
=
fluid
.
imperative
.
PyLayer
()
l
.
forward
([])
def
test_layer_in_out
(
self
):
l
=
MyLayer
()
x
=
l
(
np
.
ones
([
1
],
np
.
float32
))[
0
]
self
.
assertIsNotNone
(
x
)
sys
.
stderr
.
write
(
"%s output: %s
\n
"
%
(
x
,
x
.
numpy
(
scope
=
l
.
_scope
)))
with
fluid
.
imperative
.
guard
():
l
=
MyLayer
()
x
=
l
(
np
.
array
([
1.0
,
2.0
,
-
1.0
],
dtype
=
np
.
float32
))[
0
]
self
.
assertIsNotNone
(
x
)
sys
.
stderr
.
write
(
"%s output: %s
\n
"
%
(
x
,
x
.
numpy
(
scope
=
l
.
_scope
)))
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录