Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ce4a26dd
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看板
提交
ce4a26dd
编写于
12月 19, 2018
作者:
S
sneaxiy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
clean code
try to fix mac compile bug? test=develop
上级
74a8e6b0
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
77 addition
and
66 deletion
+77
-66
paddle/fluid/framework/CMakeLists.txt
paddle/fluid/framework/CMakeLists.txt
+1
-4
paddle/fluid/framework/var_type_traits.cc
paddle/fluid/framework/var_type_traits.cc
+51
-2
paddle/fluid/framework/var_type_traits.h
paddle/fluid/framework/var_type_traits.h
+3
-52
paddle/fluid/framework/var_type_traits_test.cc
paddle/fluid/framework/var_type_traits_test.cc
+22
-8
未找到文件。
paddle/fluid/framework/CMakeLists.txt
浏览文件 @
ce4a26dd
...
...
@@ -83,10 +83,7 @@ cc_test(threadpool_test SRCS threadpool_test.cc DEPS threadpool)
cc_library
(
var_type_traits SRCS var_type_traits DEPS lod_tensor selected_rows framework_proto
)
if
(
WITH_GPU
)
target_link_libraries
(
var_type_traits cudnn
)
if
(
NOT WIN32
)
target_link_libraries
(
var_type_traits nccl
)
endif
()
target_link_libraries
(
var_type_traits dynload_cuda
)
endif
()
cc_test
(
var_type_traits_test SRCS var_type_traits_test.cc DEPS var_type_traits
)
...
...
paddle/fluid/framework/var_type_traits.cc
浏览文件 @
ce4a26dd
...
...
@@ -17,9 +17,58 @@
namespace
paddle
{
namespace
framework
{
const
char
*
ToTypeName
(
int
var_id
)
{
return
ToTypeIndex
(
var_id
).
name
();
}
// Besides registering variable type id, it is helpful to register a
// var_id -> std::type_index map (for example, get type names according to id)
namespace
detail
{
const
std
::
type_index
&
ToTypeIndex
(
int
var_id
)
{
template
<
int
kStart
,
int
kEnd
,
bool
kStop
>
struct
VarIdToTypeIndexMapInitializerImpl
{
static
void
Init
(
std
::
unordered_map
<
int
,
std
::
type_index
>
*
m
)
{
using
Type
=
typename
std
::
tuple_element
<
kStart
,
VarTypeRegistry
::
ArgTuple
>::
type
;
constexpr
int
kId
=
VarTypeTrait
<
Type
>::
kId
;
if
(
!
std
::
is_same
<
Type
,
void
>::
value
)
{
m
->
emplace
(
kId
,
std
::
type_index
(
typeid
(
Type
)));
}
VarIdToTypeIndexMapInitializerImpl
<
kStart
+
1
,
kEnd
,
kStart
+
1
==
kEnd
>::
Init
(
m
);
}
};
template
<
int
kStart
,
int
kEnd
>
struct
VarIdToTypeIndexMapInitializerImpl
<
kStart
,
kEnd
,
true
>
{
static
void
Init
(
std
::
unordered_map
<
int
,
std
::
type_index
>
*
m
)
{}
};
// VarIdToTypeIndexMapInitializer is designed to initialize var_id ->
// std::type_index map
using
VarIdToTypeIndexMapInitializer
=
VarIdToTypeIndexMapInitializerImpl
<
0
,
VarTypeRegistry
::
kRegisteredTypeNum
,
VarTypeRegistry
::
kRegisteredTypeNum
==
0
>
;
struct
VarIdToTypeIndexMapHolder
{
public:
static
const
std
::
type_index
&
ToTypeIndex
(
int
var_id
)
{
static
const
VarIdToTypeIndexMapHolder
instance
;
auto
it
=
instance
.
var_type_map_
.
find
(
var_id
);
PADDLE_ENFORCE
(
it
!=
instance
.
var_type_map_
.
end
(),
"VarId %d is not registered."
,
var_id
);
return
it
->
second
;
}
private:
VarIdToTypeIndexMapHolder
()
{
VarIdToTypeIndexMapInitializer
::
Init
(
&
var_type_map_
);
}
std
::
unordered_map
<
int
,
std
::
type_index
>
var_type_map_
;
};
}
// namespace detail
const
char
*
ToTypeName
(
int
var_id
)
{
return
ToTypeIndex
(
var_id
).
name
();
}
const
std
::
type_index
&
ToTypeIndex
(
int
var_id
)
{
return
detail
::
VarIdToTypeIndexMapHolder
::
ToTypeIndex
(
var_id
);
}
...
...
paddle/fluid/framework/var_type_traits.h
浏览文件 @
ce4a26dd
...
...
@@ -40,6 +40,9 @@
namespace
paddle
{
namespace
framework
{
const
char
*
ToTypeName
(
int
var_id
);
const
std
::
type_index
&
ToTypeIndex
(
int
var_id
);
namespace
detail
{
template
<
bool
kStop
,
int
kStart
,
int
kEnd
,
typename
T1
,
typename
T2
,
...
...
@@ -145,58 +148,6 @@ REG_PROTO_VAR_TYPE_TRAIT(ReaderHolder, proto::VarType::READER);
/** End of variable type registration */
// Besides register variable id, it is helpful to register a
// var_id -> std::type_index (for example, get var names according to id)
namespace
detail
{
template
<
int
kStart
,
int
kEnd
,
bool
kStop
>
struct
VarIdToTypeIndexMapInitializerImpl
{
static
void
Init
(
std
::
unordered_map
<
int
,
std
::
type_index
>
*
m
)
{
using
Type
=
typename
std
::
tuple_element
<
kStart
,
VarTypeRegistry
::
ArgTuple
>::
type
;
constexpr
int
kId
=
VarTypeTrait
<
Type
>::
kId
;
if
(
!
std
::
is_same
<
Type
,
void
>::
value
)
{
m
->
emplace
(
kId
,
std
::
type_index
(
typeid
(
Type
)));
}
VarIdToTypeIndexMapInitializerImpl
<
kStart
+
1
,
kEnd
,
kStart
+
1
==
kEnd
>::
Init
(
m
);
}
};
template
<
int
kStart
,
int
kEnd
>
struct
VarIdToTypeIndexMapInitializerImpl
<
kStart
,
kEnd
,
true
>
{
static
void
Init
(
std
::
unordered_map
<
int
,
std
::
type_index
>
*
m
)
{}
};
// VarIdToTypeIndexMapInitializer is designed to initialize var_id ->
// std::type_index map
using
VarIdToTypeIndexMapInitializer
=
VarIdToTypeIndexMapInitializerImpl
<
0
,
VarTypeRegistry
::
kRegisteredTypeNum
,
VarTypeRegistry
::
kRegisteredTypeNum
==
0
>
;
struct
VarIdToTypeIndexMapHolder
{
public:
static
const
std
::
type_index
&
ToTypeIndex
(
int
var_id
)
{
static
const
VarIdToTypeIndexMapHolder
instance
;
auto
it
=
instance
.
var_type_map_
.
find
(
var_id
);
PADDLE_ENFORCE
(
it
!=
instance
.
var_type_map_
.
end
(),
"VarId %d is not registered."
,
var_id
);
return
it
->
second
;
}
private:
VarIdToTypeIndexMapHolder
()
{
VarIdToTypeIndexMapInitializer
::
Init
(
&
var_type_map_
);
}
std
::
unordered_map
<
int
,
std
::
type_index
>
var_type_map_
;
};
}
// namespace detail
const
char
*
ToTypeName
(
int
var_id
);
const
std
::
type_index
&
ToTypeIndex
(
int
var_id
);
template
<
typename
T
>
inline
constexpr
bool
IsRegisteredVarType
()
{
return
VarTypeRegistry
::
IsRegistered
<
T
>
();
...
...
paddle/fluid/framework/var_type_traits_test.cc
浏览文件 @
ce4a26dd
...
...
@@ -15,32 +15,46 @@
#include "paddle/fluid/framework/var_type_traits.h"
#include <gtest/gtest.h>
#include <cstdint>
#include <unordered_set>
namespace
paddle
{
namespace
framework
{
template
<
int
kPos
,
int
kEnd
,
bool
kStop
>
struct
TypeIndexChecker
{
static
void
Check
()
{
template
<
typename
SetType1
,
typename
SetType2
>
static
void
Check
(
SetType1
*
var_id_set
,
SetType2
*
type_index_set
)
{
using
Type
=
typename
std
::
tuple_element
<
kPos
,
VarTypeRegistry
::
ArgTuple
>::
type
;
static_assert
(
std
::
is_same
<
typename
VarTypeTrait
<
Type
>::
Type
,
Type
>::
value
,
"Type must be the same"
);
constexpr
auto
kId
=
VarTypeTrait
<
Type
>::
kId
;
if
(
!
std
::
is_same
<
Type
,
void
>::
value
)
{
EXPECT_TRUE
(
ToTypeIndex
(
VarTypeTrait
<
Type
>::
kId
)
==
typeid
(
Type
));
EXPECT_TRUE
(
std
::
string
(
ToTypeName
(
VarTypeTrait
<
Type
>::
kId
))
==
typeid
(
Type
).
name
());
std
::
type_index
actual_type
(
typeid
(
Type
));
EXPECT_EQ
(
std
::
string
(
ToTypeName
(
kId
)),
std
::
string
(
actual_type
.
name
()));
EXPECT_EQ
(
ToTypeIndex
(
kId
),
actual_type
);
EXPECT_TRUE
(
var_id_set
->
count
(
kId
)
==
0
);
// NOLINT
EXPECT_TRUE
(
type_index_set
->
count
(
actual_type
)
==
0
);
// NOLINT
var_id_set
->
insert
(
kId
);
type_index_set
->
insert
(
std
::
type_index
(
typeid
(
Type
)));
}
TypeIndexChecker
<
kPos
+
1
,
kEnd
,
kPos
+
1
==
kEnd
>::
Check
();
TypeIndexChecker
<
kPos
+
1
,
kEnd
,
kPos
+
1
==
kEnd
>::
Check
(
var_id_set
,
type_index_set
);
}
};
template
<
int
kPos
,
int
kEnd
>
struct
TypeIndexChecker
<
kPos
,
kEnd
,
true
>
{
static
void
Check
()
{}
template
<
typename
SetType1
,
typename
SetType2
>
static
void
Check
(
SetType1
*
,
SetType2
*
)
{}
};
TEST
(
var_type_traits
,
check_
type_index
)
{
TEST
(
var_type_traits
,
check_
no_duplicate_registry
)
{
constexpr
size_t
kRegisteredNum
=
VarTypeRegistry
::
kRegisteredTypeNum
;
TypeIndexChecker
<
0
,
kRegisteredNum
,
kRegisteredNum
==
0
>::
Check
();
std
::
unordered_set
<
int
>
var_id_set
;
std
::
unordered_set
<
std
::
type_index
>
type_index_set
;
TypeIndexChecker
<
0
,
kRegisteredNum
,
kRegisteredNum
==
0
>::
Check
(
&
var_id_set
,
&
type_index_set
);
}
template
<
typename
T
>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录