Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8965cee8
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看板
未验证
提交
8965cee8
编写于
8月 28, 2018
作者:
Y
Yu Yang
提交者:
GitHub
8月 28, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish PrintOp (#12895)
* Polish PrintOp * Polish PrintOp * Polish PrintOp * Refine test_print_op
上级
9be39bb4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
37 addition
and
79 deletion
+37
-79
paddle/fluid/framework/var_type.h
paddle/fluid/framework/var_type.h
+1
-1
paddle/fluid/operators/print_op.cc
paddle/fluid/operators/print_op.cc
+33
-71
python/paddle/fluid/layers/control_flow.py
python/paddle/fluid/layers/control_flow.py
+1
-4
python/paddle/fluid/tests/unittests/test_print_op.py
python/paddle/fluid/tests/unittests/test_print_op.py
+2
-3
未找到文件。
paddle/fluid/framework/var_type.h
浏览文件 @
8965cee8
...
@@ -26,7 +26,7 @@ namespace paddle {
...
@@ -26,7 +26,7 @@ namespace paddle {
namespace
framework
{
namespace
framework
{
template
<
typename
T
>
template
<
typename
T
>
bool
IsType
(
const
std
::
type_index
&
type_index
)
{
inline
bool
IsType
(
const
std
::
type_index
&
type_index
)
{
return
type_index
==
std
::
type_index
(
typeid
(
T
));
return
type_index
==
std
::
type_index
(
typeid
(
T
));
}
}
...
...
paddle/fluid/operators/print_op.cc
浏览文件 @
8965cee8
...
@@ -13,14 +13,12 @@
...
@@ -13,14 +13,12 @@
limitations under the License. */
limitations under the License. */
#include <algorithm>
#include <algorithm>
#include <ctime>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/var_type.h"
#include "paddle/fluid/framework/var_type.h"
#include "paddle/fluid/framework/variable.h"
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
using
framework
::
GradVarName
;
#define CLOG std::cout
#define CLOG std::cout
...
@@ -35,7 +33,7 @@ struct Formater {
...
@@ -35,7 +33,7 @@ struct Formater {
std
::
type_index
dtype
{
typeid
(
const
char
)};
std
::
type_index
dtype
{
typeid
(
const
char
)};
framework
::
LoD
lod
;
framework
::
LoD
lod
;
int
summarize
;
int
summarize
;
void
*
data
{
nullptr
};
void
*
data
{
nullptr
};
void
operator
()(
size_t
size
)
{
void
operator
()(
size_t
size
)
{
PrintMessage
();
PrintMessage
();
...
@@ -101,7 +99,7 @@ struct Formater {
...
@@ -101,7 +99,7 @@ struct Formater {
template
<
typename
T
>
template
<
typename
T
>
void
Display
(
size_t
size
)
{
void
Display
(
size_t
size
)
{
auto
*
d
=
reinterpret_cast
<
T
*>
(
data
);
auto
*
d
=
reinterpret_cast
<
T
*>
(
data
);
CLOG
<<
"
\t
data: "
;
CLOG
<<
"
\t
data: "
;
if
(
summarize
!=
-
1
)
{
if
(
summarize
!=
-
1
)
{
summarize
=
std
::
min
(
size
,
(
size_t
)
summarize
);
summarize
=
std
::
min
(
size
,
(
size_t
)
summarize
);
...
@@ -120,51 +118,36 @@ struct Formater {
...
@@ -120,51 +118,36 @@ struct Formater {
// TODO(ChunweiYan) there should be some other printers for TensorArray
// TODO(ChunweiYan) there should be some other printers for TensorArray
class
TensorPrintOp
:
public
framework
::
OperatorBase
{
class
TensorPrintOp
:
public
framework
::
OperatorBase
{
public:
public:
TensorPrintOp
(
const
std
::
string
&
type
,
TensorPrintOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
const
framework
::
AttributeMap
&
attrs
)
:
OperatorBase
(
type
,
inputs
,
outputs
,
attrs
)
{}
:
OperatorBase
(
type
,
inputs
,
outputs
,
attrs
)
{}
TensorPrintOp
(
const
TensorPrintOp
&
o
)
TensorPrintOp
(
const
TensorPrintOp
&
o
)
:
framework
::
OperatorBase
(
:
framework
::
OperatorBase
(
static_cast
<
const
framework
::
OperatorBase
&>
(
o
))
{
static_cast
<
const
framework
::
OperatorBase
&>
(
o
))
{
PADDLE_THROW
(
"Not implemented."
);
PADDLE_THROW
(
"Not implemented."
);
}
}
private:
private:
void
RunImpl
(
const
framework
::
Scope
&
scope
,
void
RunImpl
(
const
framework
::
Scope
&
scope
,
const
platform
::
Place
&
place
)
const
override
{
const
platform
::
Place
&
place
)
const
override
{
const
framework
::
Variable
*
in_var_ptr
=
nullptr
;
const
framework
::
Variable
*
in_var_ptr
=
nullptr
;
std
::
string
phase
(
kForward
);
std
::
string
printed_var_name
=
""
;
std
::
string
printed_var_name
=
""
;
auto
&
inputs
=
Inputs
();
in_var_ptr
=
scope
.
FindVar
(
Input
(
"In"
));
if
(
inputs
.
find
(
"In"
)
!=
inputs
.
end
()
&&
!
Inputs
(
"In"
).
empty
())
{
printed_var_name
=
Inputs
(
"In"
).
front
();
in_var_ptr
=
scope
.
FindVar
(
Input
(
"In"
));
printed_var_name
=
Inputs
(
"In"
).
front
();
}
else
if
(
inputs
.
find
(
"In@GRAD"
)
!=
inputs
.
end
()
&&
!
Inputs
(
"In@GRAD"
).
empty
())
{
in_var_ptr
=
scope
.
FindVar
(
Input
(
"In@GRAD"
));
printed_var_name
=
Inputs
(
"In@GRAD"
).
front
();
phase
=
std
::
string
(
kBackward
);
}
else
{
PADDLE_THROW
(
"Unknown phase, should be forward or backward."
);
}
PADDLE_ENFORCE_NOT_NULL
(
in_var_ptr
);
PADDLE_ENFORCE_NOT_NULL
(
in_var_ptr
);
auto
&
in_tensor
=
in_var_ptr
->
Get
<
framework
::
LoDTensor
>
();
auto
&
in_tensor
=
in_var_ptr
->
Get
<
framework
::
LoDTensor
>
();
auto
*
out_var_ptr
=
scope
.
FindVar
(
Output
(
"Out"
));
auto
&
out_tensor
=
*
out_var_ptr
->
GetMutable
<
framework
::
LoDTensor
>
();
// Just copy data from input tensor to output tensor
// output tensor share same memory with input tensor
out_tensor
.
ShareDataWith
(
in_tensor
);
out_tensor
.
set_lod
(
in_tensor
.
lod
());
std
::
string
print_phase
=
Attr
<
std
::
string
>
(
"print_phase"
);
std
::
string
print_phase
=
Attr
<
std
::
string
>
(
"print_phase"
);
if
(
print_phase
!=
phase
&&
print_phase
!=
std
::
string
(
kBoth
))
{
bool
is_forward
=
Attr
<
bool
>
(
"is_forward"
);
if
((
is_forward
&&
print_phase
==
kBackward
)
||
(
!
is_forward
&&
print_phase
==
kForward
))
{
return
;
return
;
}
}
...
@@ -192,7 +175,7 @@ class TensorPrintOp : public framework::OperatorBase {
...
@@ -192,7 +175,7 @@ class TensorPrintOp : public framework::OperatorBase {
formater
.
dtype
=
printed_tensor
.
type
();
formater
.
dtype
=
printed_tensor
.
type
();
}
}
if
(
Attr
<
bool
>
(
"print_tensor_shape"
))
{
if
(
Attr
<
bool
>
(
"print_tensor_shape"
))
{
auto
&
dims
=
printed_tensor
.
dims
();
auto
&
dims
=
printed_tensor
.
dims
();
formater
.
dims
.
resize
(
dims
.
size
());
formater
.
dims
.
resize
(
dims
.
size
());
for
(
int
i
=
0
;
i
<
dims
.
size
();
++
i
)
formater
.
dims
[
i
]
=
dims
[
i
];
for
(
int
i
=
0
;
i
<
dims
.
size
();
++
i
)
formater
.
dims
[
i
]
=
dims
[
i
];
}
}
...
@@ -200,7 +183,7 @@ class TensorPrintOp : public framework::OperatorBase {
...
@@ -200,7 +183,7 @@ class TensorPrintOp : public framework::OperatorBase {
formater
.
lod
=
printed_tensor
.
lod
();
formater
.
lod
=
printed_tensor
.
lod
();
}
}
formater
.
summarize
=
Attr
<
int
>
(
"summarize"
);
formater
.
summarize
=
Attr
<
int
>
(
"summarize"
);
formater
.
data
=
reinterpret_cast
<
void
*>
(
printed_tensor
.
data
<
void
>
());
formater
.
data
=
reinterpret_cast
<
void
*>
(
printed_tensor
.
data
<
void
>
());
formater
(
printed_tensor
.
numel
());
formater
(
printed_tensor
.
numel
());
}
}
...
@@ -219,14 +202,14 @@ class PrintOpProtoAndCheckMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -219,14 +202,14 @@ class PrintOpProtoAndCheckMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
bool
>
(
"print_tensor_type"
,
"Whether to print the tensor's dtype."
);
AddAttr
<
bool
>
(
"print_tensor_type"
,
"Whether to print the tensor's dtype."
);
AddAttr
<
bool
>
(
"print_tensor_shape"
,
"Whether to print the tensor's shape."
);
AddAttr
<
bool
>
(
"print_tensor_shape"
,
"Whether to print the tensor's shape."
);
AddAttr
<
bool
>
(
"print_tensor_lod"
,
"Whether to print the tensor's lod."
);
AddAttr
<
bool
>
(
"print_tensor_lod"
,
"Whether to print the tensor's lod."
);
AddAttr
<
std
::
string
>
(
AddAttr
<
std
::
string
>
(
"print_phase"
,
"print_phase"
,
"(string, default 'FORWARD') Which phase to display "
"(string, default 'BOTH') Which phase to display
including 'FORWARD' "
"
including 'FORWARD' "
"'BACKWARD' and 'BOTH'."
)
"'BACKWARD' and 'BOTH'."
)
.
SetDefault
(
std
::
string
(
kBoth
))
.
SetDefault
(
std
::
string
(
kBoth
))
.
InEnum
({
std
::
string
(
kForward
),
std
::
string
(
kBackward
),
.
InEnum
({
std
::
string
(
kForward
),
std
::
string
(
kBackward
),
std
::
string
(
kBoth
)});
std
::
string
(
kBoth
)});
Add
Output
(
"Out"
,
"Output tensor with same data as input tensor."
);
Add
Attr
<
bool
>
(
"is_forward"
,
"Whether is forward or not"
).
SetDefault
(
true
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
Creates a print op that will print when a tensor is accessed.
Creates a print op that will print when a tensor is accessed.
...
@@ -238,40 +221,21 @@ tensor `t`.)DOC");
...
@@ -238,40 +221,21 @@ tensor `t`.)DOC");
class
InferShapeForward
:
public
framework
::
InferShapeBase
{
class
InferShapeForward
:
public
framework
::
InferShapeBase
{
public:
public:
void
operator
()(
framework
::
InferShapeContext
*
context
)
const
override
{
void
operator
()(
framework
::
InferShapeContext
*
context
)
const
override
{
PADDLE_ENFORCE
(
context
->
HasInput
(
"In"
),
"Input(In) should not be null."
);
PADDLE_ENFORCE
(
context
->
HasInput
(
"In"
),
"Input(In) should not be null."
);
context
->
ShareLoD
(
"In"
,
/*->*/
"Out"
);
context
->
SetOutputDim
(
"Out"
,
context
->
GetInputDim
(
"In"
));
}
};
class
InferShapeBackward
:
public
framework
::
InferShapeBase
{
public:
void
operator
()(
framework
::
InferShapeContext
*
context
)
const
override
{
PADDLE_ENFORCE
(
context
->
HasInput
(
"In@GRAD"
),
"Input(In@GRAD) should not be null."
);
context
->
ShareLoD
(
"In@GRAD"
,
/*->*/
"Out"
);
context
->
SetOutputDim
(
"Out"
,
context
->
GetInputDim
(
"In@GRAD"
));
}
}
};
};
class
InferVarType
:
public
framework
::
VarTypeInference
{
class
PrintOpGradientMaker
:
public
framework
::
SingleGradOpDescMaker
{
public:
void
operator
()(
const
framework
::
OpDesc
&
op_desc
,
framework
::
BlockDesc
*
block
)
const
override
{}
};
class
PrintOpProtoAndCheckGradOpMaker
:
public
framework
::
SingleGradOpDescMaker
{
public:
public:
using
framework
::
SingleGradOpDescMaker
::
SingleGradOpDescMaker
;
using
framework
::
SingleGradOpDescMaker
::
SingleGradOpDescMaker
;
std
::
unique_ptr
<
framework
::
OpDesc
>
Apply
()
const
override
{
std
::
unique_ptr
<
framework
::
OpDesc
>
Apply
()
const
override
{
auto
*
op_desc_ptr
=
new
framework
::
OpDesc
();
auto
*
op_desc_ptr
=
new
framework
::
OpDesc
();
op_desc_ptr
->
SetType
(
"print_grad"
);
op_desc_ptr
->
SetType
(
"print"
);
op_desc_ptr
->
SetInput
(
"In@GRAD"
,
OutputGrad
(
"Out"
));
op_desc_ptr
->
SetInput
(
"In"
,
InputGrad
(
"In"
));
op_desc_ptr
->
SetOutput
(
"Out"
,
InputGrad
(
"In"
));
op_desc_ptr
->
SetAttrMap
(
Attrs
());
op_desc_ptr
->
SetAttrMap
(
Attrs
());
op_desc_ptr
->
SetAttr
(
"is_forward"
,
false
);
return
std
::
unique_ptr
<
framework
::
OpDesc
>
(
op_desc_ptr
);
return
std
::
unique_ptr
<
framework
::
OpDesc
>
(
op_desc_ptr
);
}
}
};
};
...
@@ -282,6 +246,4 @@ class PrintOpProtoAndCheckGradOpMaker
...
@@ -282,6 +246,4 @@ class PrintOpProtoAndCheckGradOpMaker
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
print
,
ops
::
TensorPrintOp
,
ops
::
PrintOpProtoAndCheckMaker
,
REGISTER_OPERATOR
(
print
,
ops
::
TensorPrintOp
,
ops
::
PrintOpProtoAndCheckMaker
,
ops
::
PrintOpProtoAndCheckGradOpMaker
,
ops
::
InferShapeForward
,
ops
::
PrintOpGradientMaker
,
ops
::
InferShapeForward
);
ops
::
InferVarType
);
REGISTER_OPERATOR
(
print_grad
,
ops
::
TensorPrintOp
,
ops
::
InferShapeBackward
);
python/paddle/fluid/layers/control_flow.py
浏览文件 @
8965cee8
...
@@ -189,7 +189,6 @@ def Print(input,
...
@@ -189,7 +189,6 @@ def Print(input,
message="The content of some_layer: ")
message="The content of some_layer: ")
'''
'''
helper
=
LayerHelper
(
'print'
,
**
locals
())
helper
=
LayerHelper
(
'print'
,
**
locals
())
out
=
helper
.
create_tmp_variable
(
dtype
=
helper
.
input_dtype
())
helper
.
append_op
(
helper
.
append_op
(
type
=
'print'
,
type
=
'print'
,
inputs
=
{
'In'
:
input
},
inputs
=
{
'In'
:
input
},
...
@@ -202,9 +201,7 @@ def Print(input,
...
@@ -202,9 +201,7 @@ def Print(input,
'print_tensor_shape'
:
print_tensor_shape
,
'print_tensor_shape'
:
print_tensor_shape
,
'print_tensor_lod'
:
print_tensor_lod
,
'print_tensor_lod'
:
print_tensor_lod
,
'print_phase'
:
print_phase
.
upper
()
'print_phase'
:
print_phase
.
upper
()
},
})
outputs
=
{
'Out'
:
out
})
return
out
class
BlockGuard
(
object
):
class
BlockGuard
(
object
):
...
...
python/paddle/fluid/tests/unittests/test_print_op.py
浏览文件 @
8965cee8
...
@@ -35,9 +35,8 @@ class TestPrintOpCPU(unittest.TestCase):
...
@@ -35,9 +35,8 @@ class TestPrintOpCPU(unittest.TestCase):
def
build_network
(
self
,
only_forward
,
**
kargs
):
def
build_network
(
self
,
only_forward
,
**
kargs
):
x
=
layers
.
data
(
'x'
,
shape
=
[
3
],
dtype
=
'float32'
,
lod_level
=
1
)
x
=
layers
.
data
(
'x'
,
shape
=
[
3
],
dtype
=
'float32'
,
lod_level
=
1
)
x
.
stop_gradient
=
False
x
.
stop_gradient
=
False
printed
=
layers
.
Print
(
input
=
x
,
**
kargs
)
layers
.
Print
(
input
=
x
,
**
kargs
)
if
only_forward
:
return
printed
loss
=
layers
.
mean
(
x
)
loss
=
layers
.
mean
(
printed
)
append_backward
(
loss
=
loss
)
append_backward
(
loss
=
loss
)
return
loss
return
loss
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录