Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
d9e3c4ff
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d9e3c4ff
编写于
9月 30, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add GradOpDescMaker to OpInfo and complete OperatorRegistrar method
上级
f76b38c2
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
148 addition
and
32 deletion
+148
-32
paddle/framework/CMakeLists.txt
paddle/framework/CMakeLists.txt
+1
-1
paddle/framework/details/op_registry.h
paddle/framework/details/op_registry.h
+105
-0
paddle/framework/op_info.h
paddle/framework/op_info.h
+10
-3
paddle/framework/op_registry.h
paddle/framework/op_registry.h
+21
-28
paddle/framework/op_registry_test.cc
paddle/framework/op_registry_test.cc
+11
-0
未找到文件。
paddle/framework/CMakeLists.txt
浏览文件 @
d9e3c4ff
...
...
@@ -22,7 +22,7 @@ cc_library(attribute SRCS attribute.cc DEPS framework_proto)
cc_library
(
proto_desc SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc DEPS attribute
)
cc_library
(
op_proto_maker SRCS op_proto_maker.cc DEPS framework_proto attribute
)
cc_test
(
op_proto_maker_test SRCS op_proto_maker_test.cc DEPS op_proto_maker
)
cc_library
(
op_info SRCS op_info.cc DEPS attribute framework_proto
)
cc_library
(
op_info SRCS op_info.cc DEPS attribute framework_proto
proto_desc
)
cc_library
(
operator SRCS operator.cc DEPS op_info device_context tensor scope
)
cc_test
(
operator_test SRCS operator_test.cc DEPS operator op_registry
)
...
...
paddle/framework/details/op_registry.h
0 → 100644
浏览文件 @
d9e3c4ff
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/op_info.h"
#include "paddle/framework/op_proto_maker.h"
#include "paddle/framework/operator.h"
namespace
paddle
{
namespace
framework
{
namespace
details
{
enum
OpInfoFillType
{
kOperator
=
0
,
kOpProtoAndCheckerMaker
=
1
,
kGradOpDescMaker
=
2
};
template
<
typename
T
>
struct
OpInfoFillTypeID
{
static
constexpr
OpInfoFillType
ID
()
{
return
std
::
is_base_of
<
OperatorBase
,
T
>::
value
?
kOperator
:
(
std
::
is_base_of
<
OpProtoAndCheckerMaker
,
T
>::
value
?
kOpProtoAndCheckerMaker
:
(
std
::
is_base_of
<
GradOpDescMakerBase
,
T
>::
value
?
kGradOpDescMaker
:
static_cast
<
OpInfoFillType
>
(
-
1
)));
}
};
template
<
typename
T
,
OpInfoFillType
=
OpInfoFillTypeID
<
T
>
::
ID
()
>
struct
OpInfoFiller
;
template
<
size_t
I
,
bool
at_end
,
typename
...
ARGS
>
class
OperatorRegistrarRecursive
;
template
<
size_t
I
,
typename
...
ARGS
>
class
OperatorRegistrarRecursive
<
I
,
false
,
ARGS
...
>
{
public:
using
T
=
typename
std
::
tuple_element
<
I
,
std
::
tuple
<
ARGS
...
>>::
type
;
OperatorRegistrarRecursive
(
const
char
*
op_type
,
OpInfo
*
info
)
{
OpInfoFiller
<
T
>
fill
;
fill
(
op_type
,
info
);
constexpr
auto
size
=
sizeof
...(
ARGS
);
OperatorRegistrarRecursive
<
I
+
1
,
I
+
1
==
size
,
ARGS
...
>
reg
(
op_type
,
info
);
(
void
)(
reg
);
}
};
template
<
size_t
I
,
typename
...
ARGS
>
class
OperatorRegistrarRecursive
<
I
,
true
,
ARGS
...
>
{
public:
OperatorRegistrarRecursive
(
const
char
*
op_type
,
OpInfo
*
info
)
{}
};
template
<
typename
T
>
struct
OpInfoFiller
<
T
,
kOperator
>
{
void
operator
()(
const
char
*
op_type
,
OpInfo
*
info
)
const
{
info
->
creator_
=
[](
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
)
{
return
new
T
(
type
,
inputs
,
outputs
,
attrs
);
};
}
};
template
<
typename
T
>
struct
OpInfoFiller
<
T
,
kOpProtoAndCheckerMaker
>
{
void
operator
()(
const
char
*
op_type
,
OpInfo
*
info
)
const
{
info
->
proto_
=
new
OpProto
;
info
->
checker_
=
new
OpAttrChecker
();
auto
maker
=
T
(
info
->
proto_
,
info
->
checker_
);
maker
.
Validate
();
info
->
proto_
->
set_type
(
op_type
);
PADDLE_ENFORCE
(
info
->
proto_
->
IsInitialized
(),
"Fail to initialize %s's OpProto, because %s is not initialized"
,
op_type
,
info
->
proto_
->
InitializationErrorString
());
}
};
template
<
typename
T
>
struct
OpInfoFiller
<
T
,
kGradOpDescMaker
>
{
void
operator
()(
const
char
*
op_type
,
OpInfo
*
info
)
const
{
info
->
grad_op_maker_
=
new
T
();
}
};
}
// namespace details
}
// namespace framework
}
// namespace paddle
paddle/framework/op_info.h
浏览文件 @
d9e3c4ff
...
...
@@ -17,8 +17,8 @@
#include <map>
#include <string>
#include <unordered_map>
#include "paddle/framework/attribute.h"
#include "paddle/framework/op_desc.h"
namespace
paddle
{
namespace
framework
{
...
...
@@ -29,11 +29,18 @@ using OpCreator = std::function<OperatorBase*(
const
std
::
string
&
/*type*/
,
const
VariableNameMap
&
/*inputs*/
,
const
VariableNameMap
&
/*outputs*/
,
const
AttributeMap
&
/*attrs*/
)
>
;
class
GradOpDescMakerBase
{
public:
virtual
~
GradOpDescMakerBase
()
=
default
;
virtual
std
::
vector
<
OpDescBind
>
operator
()(
const
OpDescBind
&
)
const
=
0
;
};
struct
OpInfo
{
OpCreator
creator_
;
std
::
string
grad_op_type_
;
OpProto
*
proto_
;
OpAttrChecker
*
checker_
;
GradOpDescMakerBase
*
grad_op_maker_
{
nullptr
};
OpProto
*
proto_
{
nullptr
};
OpAttrChecker
*
checker_
{
nullptr
};
bool
HasOpProtoAndChecker
()
const
{
return
proto_
!=
nullptr
&&
checker_
!=
nullptr
;
...
...
paddle/framework/op_registry.h
浏览文件 @
d9e3c4ff
...
...
@@ -21,49 +21,42 @@ limitations under the License. */
#include <unordered_map>
#include <unordered_set>
#include "paddle/framework/attribute.h"
#include "paddle/framework/details/op_registry.h"
#include "paddle/framework/framework.pb.h"
#include "paddle/framework/grad_op_builder.h"
#include "paddle/framework/op_info.h"
#include "paddle/framework/op_proto_maker.h"
#include "paddle/framework/operator.h"
#include "paddle/framework/scope.h"
namespace
paddle
{
namespace
framework
{
template
<
typename
...
ARGS
>
struct
OperatorRegistrar
{
explicit
OperatorRegistrar
(
const
char
*
op_type
)
:
op_type
(
op_type
)
{
PADDLE_ENFORCE
(
!
OpInfoMap
::
Instance
().
Has
(
op_type
),
"'%s' is registered more than once."
,
op_type
);
static_assert
(
sizeof
...(
ARGS
)
!=
0
,
"OperatorRegistrar should be invoked at least by OpClass"
);
details
::
OperatorRegistrarRecursive
<
0
,
false
,
ARGS
...
>
(
op_type
,
&
info
);
}
~
OperatorRegistrar
()
{
OpInfoMap
::
Instance
().
Insert
(
op_type
,
info
);
}
const
char
*
op_type
;
OpInfo
info
;
};
class
OpRegistry
{
public:
template
<
typename
OpType
,
typename
ProtoMakerType
,
typename
GradOpType
>
static
void
RegisterOp
(
const
std
::
string
&
op_type
,
const
std
::
string
&
grad_op_type
)
{
PADDLE_ENFORCE
(
!
OpInfoMap
::
Instance
().
Has
(
op_type
),
"'%s' is registered more than once."
,
op_type
);
OpInfo
op_info
;
op_info
.
creator_
=
[](
const
std
::
string
&
type
,
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
)
{
return
new
OpType
(
type
,
inputs
,
outputs
,
attrs
);
};
op_info
.
grad_op_type_
=
grad_op_type
;
if
(
std
::
type_index
(
typeid
(
ProtoMakerType
))
!=
std
::
type_index
(
typeid
(
NOPMaker
)))
{
op_info
.
proto_
=
new
OpProto
;
op_info
.
checker_
=
new
OpAttrChecker
;
auto
maker
=
ProtoMakerType
(
op_info
.
proto_
,
op_info
.
checker_
);
maker
.
Validate
();
op_info
.
proto_
->
set_type
(
op_type
);
PADDLE_ENFORCE
(
op_info
.
proto_
->
IsInitialized
(),
"Fail to initialize %s's OpProto, because %s is not initialized"
,
op_type
,
op_info
.
proto_
->
InitializationErrorString
());
}
else
{
op_info
.
proto_
=
nullptr
;
op_info
.
checker_
=
nullptr
;
}
OpInfoMap
::
Instance
().
Insert
(
op_type
,
op_info
);
OperatorRegistrar
<
OpType
,
ProtoMakerType
>
reg
(
op_type
.
c_str
());
reg
.
info
.
grad_op_type_
=
grad_op_type
;
// register gradient op
if
(
!
grad_op_type
.
empty
())
{
RegisterOp
<
GradOpType
,
NOPMaker
,
NOP
>
(
grad_op_type
,
""
);
OperatorRegistrar
<
GradOpType
>
grad_reg
(
grad_op_type
.
c_str
()
);
}
}
...
...
paddle/framework/op_registry_test.cc
浏览文件 @
d9e3c4ff
...
...
@@ -173,3 +173,14 @@ TEST(OpRegistry, CustomChecker) {
int
test_attr
=
op
->
Attr
<
int
>
(
"test_attr"
);
ASSERT_EQ
(
test_attr
,
4
);
}
class
CosineOpComplete
:
public
paddle
::
framework
::
CosineOp
{
public:
DEFINE_OP_CONSTRUCTOR
(
CosineOpComplete
,
paddle
::
framework
::
CosineOp
);
DEFINE_OP_CLONE_METHOD
(
CosineOpComplete
);
};
TEST
(
OperatorRegistrar
,
Test
)
{
using
namespace
paddle
::
framework
;
OperatorRegistrar
<
CosineOpComplete
,
CosineOpProtoAndCheckerMaker
>
reg
(
"cos"
);
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录