Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
711d86bb
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看板
提交
711d86bb
编写于
5月 10, 2018
作者:
Y
yuyang18
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish data_type.h
上级
ba57348f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
88 addition
and
66 deletion
+88
-66
paddle/fluid/framework/CMakeLists.txt
paddle/fluid/framework/CMakeLists.txt
+3
-3
paddle/fluid/framework/data_type.cc
paddle/fluid/framework/data_type.cc
+82
-0
paddle/fluid/framework/data_type.h
paddle/fluid/framework/data_type.h
+3
-63
未找到文件。
paddle/fluid/framework/CMakeLists.txt
浏览文件 @
711d86bb
...
...
@@ -5,11 +5,11 @@ proto_library(framework_proto SRCS framework.proto)
cc_library
(
ddim SRCS ddim.cc DEPS eigen3 boost
)
cc_test
(
ddim_test SRCS ddim_test.cc DEPS ddim
)
nv_test
(
dim_test SRCS dim_test.cu DEPS ddim
)
cc_library
(
data_type SRCS data_type.cc DEPS framework_proto ddim
)
if
(
WITH_GPU
)
nv_library
(
tensor SRCS tensor.cc tensor_util.cu DEPS
ddim place memory device_context framework_proto
)
nv_library
(
tensor SRCS tensor.cc tensor_util.cu DEPS
place memory device_context data_type
)
else
()
cc_library
(
tensor SRCS tensor.cc tensor_util.cc DEPS
ddim place memory device_context framework_proto
)
cc_library
(
tensor SRCS tensor.cc tensor_util.cc DEPS
place memory device_context data_type
)
endif
()
cc_test
(
tensor_test SRCS tensor_test.cc DEPS tensor
)
...
...
paddle/fluid/framework/data_type.cc
0 → 100644
浏览文件 @
711d86bb
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
#include "paddle/fluid/framework/data_type.h"
namespace
paddle
{
namespace
framework
{
struct
DataTypeMap
{
std
::
unordered_map
<
std
::
type_index
,
proto
::
VarType
::
Type
>
cpp_to_proto_
;
std
::
unordered_map
<
proto
::
VarType
::
Type
,
std
::
type_index
>
proto_to_cpp_
;
std
::
unordered_map
<
proto
::
VarType
::
Type
,
std
::
string
>
proto_to_str_
;
};
static
DataTypeMap
g_data_type_map_
;
template
<
typename
T
>
static
inline
void
RegisterType
(
proto
::
VarType
::
Type
proto_type
,
const
std
::
string
&
name
)
{
g_data_type_map_
.
proto_to_cpp_
.
emplace
(
proto_type
,
typeid
(
T
));
g_data_type_map_
.
cpp_to_proto_
.
emplace
(
typeid
(
T
),
proto_type
);
g_data_type_map_
.
proto_to_str_
.
emplace
(
proto_type
,
name
);
}
static
int
RegisterAllTypes
()
{
#define RegType(cc_type, proto_type) RegisterType<cc_type>(proto_type, #cc_type)
RegType
(
platform
::
float16
,
proto
::
VarType
::
FP16
);
RegType
(
float
,
proto
::
VarType
::
FP32
);
RegType
(
double
,
proto
::
VarType
::
FP64
);
RegType
(
int
,
proto
::
VarType
::
INT32
);
RegType
(
int64_t
,
proto
::
VarType
::
INT64
);
RegType
(
bool
,
proto
::
VarType
::
BOOL
);
#undef RegType
return
0
;
}
static
std
::
once_flag
register_once_flag_
;
proto
::
VarType
::
Type
ToDataType
(
std
::
type_index
type
)
{
std
::
call_once
(
register_once_flag_
,
RegisterAllTypes
);
auto
it
=
g_data_type_map_
.
cpp_to_proto_
.
find
(
type
);
if
(
it
!=
g_data_type_map_
.
cpp_to_proto_
.
end
())
{
return
it
->
second
;
}
PADDLE_THROW
(
"Not support %s as tensor type"
,
type
.
name
());
}
std
::
type_index
ToTypeIndex
(
proto
::
VarType
::
Type
type
)
{
std
::
call_once
(
register_once_flag_
,
RegisterAllTypes
);
auto
it
=
g_data_type_map_
.
proto_to_cpp_
.
find
(
type
);
if
(
it
!=
g_data_type_map_
.
proto_to_cpp_
.
end
())
{
return
it
->
second
;
}
PADDLE_THROW
(
"Not support proto::VarType::Type(%d) as tensor type"
,
static_cast
<
int
>
(
type
));
}
std
::
string
DataTypeToString
(
const
proto
::
VarType
::
Type
type
)
{
std
::
call_once
(
register_once_flag_
,
RegisterAllTypes
);
auto
it
=
g_data_type_map_
.
proto_to_str_
.
find
(
type
);
if
(
it
!=
g_data_type_map_
.
proto_to_str_
.
end
())
{
return
it
->
second
;
}
PADDLE_THROW
(
"Not support proto::VarType::Type(%d) as tensor type"
,
static_cast
<
int
>
(
type
));
}
}
// namespace framework
}
// namespace paddle
paddle/fluid/framework/data_type.h
浏览文件 @
711d86bb
...
...
@@ -22,47 +22,8 @@ limitations under the License. */
namespace
paddle
{
namespace
framework
{
inline
proto
::
VarType
::
Type
ToDataType
(
std
::
type_index
type
)
{
if
(
typeid
(
platform
::
float16
).
hash_code
()
==
type
.
hash_code
())
{
return
proto
::
VarType
::
FP16
;
}
else
if
(
typeid
(
const
float
).
hash_code
()
==
type
.
hash_code
())
{
// CPPLint complains Using C-style cast. Use static_cast<float>() instead
// One fix to this is to replace float with const float because
// typeid(T) == typeid(const T)
// http://en.cppreference.com/w/cpp/language/typeid
return
proto
::
VarType
::
FP32
;
}
else
if
(
typeid
(
const
double
).
hash_code
()
==
type
.
hash_code
())
{
return
proto
::
VarType
::
FP64
;
}
else
if
(
typeid
(
const
int
).
hash_code
()
==
type
.
hash_code
())
{
return
proto
::
VarType
::
INT32
;
}
else
if
(
typeid
(
const
int64_t
).
hash_code
()
==
type
.
hash_code
())
{
return
proto
::
VarType
::
INT64
;
}
else
if
(
typeid
(
const
bool
).
hash_code
()
==
type
.
hash_code
())
{
return
proto
::
VarType
::
BOOL
;
}
else
{
PADDLE_THROW
(
"Not supported"
);
}
}
inline
std
::
type_index
ToTypeIndex
(
proto
::
VarType
::
Type
type
)
{
switch
(
type
)
{
case
proto
::
VarType
::
FP16
:
return
typeid
(
platform
::
float16
);
case
proto
::
VarType
::
FP32
:
return
typeid
(
float
);
case
proto
::
VarType
::
FP64
:
return
typeid
(
double
);
case
proto
::
VarType
::
INT32
:
return
typeid
(
int
);
case
proto
::
VarType
::
INT64
:
return
typeid
(
int64_t
);
case
proto
::
VarType
::
BOOL
:
return
typeid
(
bool
);
default:
PADDLE_THROW
(
"Not support type %d"
,
type
);
}
}
extern
proto
::
VarType
::
Type
ToDataType
(
std
::
type_index
type
);
extern
std
::
type_index
ToTypeIndex
(
proto
::
VarType
::
Type
type
);
template
<
typename
Visitor
>
inline
void
VisitDataType
(
proto
::
VarType
::
Type
type
,
Visitor
visitor
)
{
switch
(
type
)
{
...
...
@@ -89,32 +50,11 @@ inline void VisitDataType(proto::VarType::Type type, Visitor visitor) {
}
}
inline
std
::
string
DataTypeToString
(
const
proto
::
VarType
::
Type
type
)
{
switch
(
type
)
{
case
proto
::
VarType
::
FP16
:
return
"float16"
;
case
proto
::
VarType
::
FP32
:
return
"float32"
;
case
proto
::
VarType
::
FP64
:
return
"float64"
;
case
proto
::
VarType
::
INT16
:
return
"int16"
;
case
proto
::
VarType
::
INT32
:
return
"int32"
;
case
proto
::
VarType
::
INT64
:
return
"int64"
;
case
proto
::
VarType
::
BOOL
:
return
"bool"
;
default:
PADDLE_THROW
(
"Not support type %d"
,
type
);
}
}
extern
std
::
string
DataTypeToString
(
const
proto
::
VarType
::
Type
type
);
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
proto
::
VarType
::
Type
&
type
)
{
out
<<
DataTypeToString
(
type
);
return
out
;
}
}
// namespace framework
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录