Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
aeb74af5
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看板
提交
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,
...
@@ -58,15 +58,7 @@ LoDTensor& GetFetchVariable(const Scope& scope, const std::string& var_name,
LoDTensor
&
GetVariableTensor
(
const
Scope
&
scope
,
const
std
::
string
&
var_name
)
{
LoDTensor
&
GetVariableTensor
(
const
Scope
&
scope
,
const
std
::
string
&
var_name
)
{
Variable
*
var
=
scope
.
FindVar
(
var_name
);
Variable
*
var
=
scope
.
FindVar
(
var_name
);
PADDLE_ENFORCE
(
var
,
"%s no in scope"
,
var_name
);
PADDLE_ENFORCE
(
var
,
"%s no in scope"
,
var_name
);
// TODO(panyx0718): hack, remove it once we run oprerator.
PADDLE_ENFORCE
(
var
->
IsType
<
LoDTensor
>
(),
"Only support lod tensor now."
);
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"
);
return
*
var
->
GetMutable
<
LoDTensor
>
();
return
*
var
->
GetMutable
<
LoDTensor
>
();
}
}
...
...
paddle/fluid/framework/ir/graph.cc
浏览文件 @
aeb74af5
...
@@ -38,8 +38,7 @@ void CheckProgram(const ProgramDesc &program) {
...
@@ -38,8 +38,7 @@ void CheckProgram(const ProgramDesc &program) {
switch
(
role_id
)
{
switch
(
role_id
)
{
case
_INT
(
OpRole
::
kForward
):
case
_INT
(
OpRole
::
kForward
):
if
(
visit
.
find
(
_INT
(
OpRole
::
kBackward
))
!=
visit
.
end
())
{
if
(
visit
.
find
(
_INT
(
OpRole
::
kBackward
))
!=
visit
.
end
())
{
LOG
(
ERROR
)
LOG
(
ERROR
)
<<
"Cannot add backward operator before forward operator "
<<
"Cannot add backward operator before forward operator %s."
<<
op
->
Type
();
<<
op
->
Type
();
}
}
break
;
break
;
...
...
paddle/fluid/imperative/tracer.h
浏览文件 @
aeb74af5
...
@@ -14,8 +14,12 @@
...
@@ -14,8 +14,12 @@
#pragma once
#pragma once
#include <string>
#include <vector>
#include <vector>
#include "paddle/fluid/framework/op_desc.h"
#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"
#include "paddle/fluid/imperative/engine.h"
namespace
paddle
{
namespace
paddle
{
...
@@ -26,10 +30,47 @@ class Tracer {
...
@@ -26,10 +30,47 @@ class Tracer {
Tracer
()
{}
Tracer
()
{}
void
Trace
(
framework
::
OpDesc
*
op_desc
)
{
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:
private:
framework
::
BlockDesc
*
block_
;
framework
::
Scope
*
scope_
;
std
::
vector
<
Runnable
*>
runnables_
;
std
::
vector
<
Runnable
*>
runnables_
;
};
};
...
...
paddle/fluid/pybind/imperative.cc
浏览文件 @
aeb74af5
...
@@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
...
@@ -13,6 +13,8 @@ See the License for the specific language governing permissions and
limitations under the License. */
limitations under the License. */
#include "paddle/fluid/pybind/imperative.h"
#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"
#include "paddle/fluid/imperative/tracer.h"
namespace
paddle
{
namespace
paddle
{
...
@@ -22,7 +24,19 @@ namespace pybind {
...
@@ -22,7 +24,19 @@ namespace pybind {
void
BindTracer
(
pybind11
::
module
*
m
)
{
void
BindTracer
(
pybind11
::
module
*
m
)
{
pybind11
::
class_
<
imperative
::
Tracer
>
(
*
m
,
"Tracer"
,
""
)
pybind11
::
class_
<
imperative
::
Tracer
>
(
*
m
,
"Tracer"
,
""
)
.
def
(
pybind11
::
init
<>
())
.
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
}
// namespace pybind
...
...
paddle/fluid/pybind/pybind.cc
浏览文件 @
aeb74af5
...
@@ -159,6 +159,7 @@ PYBIND11_MODULE(core, m) {
...
@@ -159,6 +159,7 @@ PYBIND11_MODULE(core, m) {
self
.
mutable_data
<
float
>
(
place
);
self
.
mutable_data
<
float
>
(
place
);
})
})
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
float
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
float
>
)
.
def
(
"set_float"
,
PyCPUTensorSetFromArray
<
float
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
int
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
int
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
double
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
double
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
int64_t
>
)
.
def
(
"set"
,
PyCPUTensorSetFromArray
<
int64_t
>
)
...
...
python/paddle/fluid/imperative/layers.py
浏览文件 @
aeb74af5
...
@@ -12,6 +12,7 @@
...
@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
import
contextlib
import
sys
import
sys
import
numpy
as
np
import
numpy
as
np
...
@@ -21,32 +22,45 @@ from paddle.fluid import framework
...
@@ -21,32 +22,45 @@ from paddle.fluid import framework
__all__
=
[
'PyLayer'
]
__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
):
class
PyLayer
(
core
.
Layer
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
_scope
=
core
.
Scope
()
self
.
_scope
=
core
.
Scope
()
self
.
_block
=
framework
.
default_main_program
().
current_block
()
def
__call__
(
self
,
inputs
):
def
__call__
(
self
,
inputs
):
with
trace_scope
(
self
.
_scope
,
self
.
_block
.
desc
):
if
not
isinstance
(
inputs
,
list
)
and
not
isinstance
(
inputs
,
tuple
):
if
not
isinstance
(
inputs
,
list
)
and
not
isinstance
(
inputs
,
tuple
):
inputs
=
[
inputs
]
inputs
=
[
inputs
]
var_inputs
=
[]
var_inputs
=
[]
for
x
in
inputs
:
for
x
in
inputs
:
if
isinstance
(
x
,
np
.
ndarray
):
if
isinstance
(
x
,
np
.
ndarray
):
tensor
=
core
.
LoDTensor
()
py_var
=
framework
.
Variable
(
tensor
.
set
(
x
,
core
.
CPUPlace
())
self
.
_block
,
x
=
framework
.
Variable
(
framework
.
default_main_program
().
current_block
(),
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
type
=
core
.
VarDesc
.
VarType
.
LOD_TENSOR
,
name
=
None
,
name
=
None
,
shape
=
x
.
shape
,
shape
=
x
.
shape
,
dtype
=
x
.
dtype
)
dtype
=
x
.
dtype
)
elif
not
isinstance
(
x
,
framework
.
Variable
):
var
=
self
.
_scope
.
var
(
py_var
.
name
)
raise
ValueError
(
"not var or ndarray %s"
%
type
(
x
))
tensor
=
var
.
get_tensor
()
self
.
_scope
.
var
(
x
.
name
)
tensor
.
set_float
(
x
,
core
.
CPUPlace
())
var_inputs
.
append
(
py_var
)
elif
isinstance
(
x
,
framework
.
Variable
):
var_inputs
.
append
(
x
)
var_inputs
.
append
(
x
)
else
:
raise
ValueError
(
"not var or ndarray %s"
%
type
(
x
))
outputs
=
self
.
forward
(
var_inputs
)
outputs
=
self
.
forward
(
var_inputs
)
for
out
in
outputs
:
self
.
_scope
.
var
(
out
.
name
)
return
outputs
return
outputs
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
aeb74af5
...
@@ -17,6 +17,7 @@ All layers just related to the neural network.
...
@@ -17,6 +17,7 @@ All layers just related to the neural network.
from
__future__
import
print_function
from
__future__
import
print_function
import
sys
import
numpy
as
np
import
numpy
as
np
import
os
import
os
from
..layer_helper
import
LayerHelper
from
..layer_helper
import
LayerHelper
...
...
python/paddle/fluid/tests/unittests/test_imperative.py
浏览文件 @
aeb74af5
...
@@ -31,23 +31,16 @@ class MyLayer(fluid.imperative.PyLayer):
...
@@ -31,23 +31,16 @@ class MyLayer(fluid.imperative.PyLayer):
class
TestImperative
(
unittest
.
TestCase
):
class
TestImperative
(
unittest
.
TestCase
):
def
test_layer
(
self
):
def
test_layer
(
self
):
with
fluid
.
imperative
.
guard
():
cl
=
core
.
Layer
()
cl
=
core
.
Layer
()
cl
.
forward
([])
cl
.
forward
([])
l
=
fluid
.
imperative
.
PyLayer
()
l
=
fluid
.
imperative
.
PyLayer
()
l
.
forward
([])
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
)
def
test_layer_in_out
(
self
):
def
test_layer_in_out
(
self
):
with
fluid
.
imperative
.
guard
():
l
=
MyLayer
()
l
=
MyLayer
()
x
=
l
(
np
.
ones
([
1
],
np
.
float32
))[
0
]
x
=
l
(
np
.
array
([
1.0
,
2.0
,
-
1.0
],
dtype
=
np
.
float32
))[
0
]
self
.
assertIsNotNone
(
x
)
self
.
assertIsNotNone
(
x
)
sys
.
stderr
.
write
(
"%s output: %s
\n
"
%
(
x
,
x
.
numpy
(
scope
=
l
.
_scope
)))
sys
.
stderr
.
write
(
"%s output: %s
\n
"
%
(
x
,
x
.
numpy
(
scope
=
l
.
_scope
)))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录