Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
bfc34ac1
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
bfc34ac1
编写于
11月 23, 2018
作者:
Z
Zeng Jinle
提交者:
GitHub
11月 23, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14536 from sneaxiy/dlpack_integration
Add dlpack support
上级
00b9e9a1
488610a6
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
320 addition
and
0 deletion
+320
-0
CMakeLists.txt
CMakeLists.txt
+1
-0
cmake/external/dlpack.cmake
cmake/external/dlpack.cmake
+31
-0
paddle/fluid/framework/CMakeLists.txt
paddle/fluid/framework/CMakeLists.txt
+3
-0
paddle/fluid/framework/dlpack_tensor.cc
paddle/fluid/framework/dlpack_tensor.cc
+127
-0
paddle/fluid/framework/dlpack_tensor.h
paddle/fluid/framework/dlpack_tensor.h
+45
-0
paddle/fluid/framework/dlpack_tensor_test.cc
paddle/fluid/framework/dlpack_tensor_test.cc
+113
-0
未找到文件。
CMakeLists.txt
浏览文件 @
bfc34ac1
...
...
@@ -205,6 +205,7 @@ include(external/pybind11) # download pybind11
include
(
external/cares
)
include
(
external/cub
)
include
(
external/xxhash
)
# download xxhash
include
(
external/dlpack
)
include
(
external/snappy
)
# download snappy
include
(
external/snappystream
)
# download snappystream
...
...
cmake/external/dlpack.cmake
0 → 100644
浏览文件 @
bfc34ac1
include
(
ExternalProject
)
set
(
DLPACK_SOURCE_DIR
${
THIRD_PARTY_PATH
}
/dlpack
)
set
(
DLPACK_INCLUDE_DIR
${
DLPACK_SOURCE_DIR
}
/src/extern_dlpack/include
)
include_directories
(
${
DLPACK_INCLUDE_DIR
}
)
ExternalProject_Add
(
extern_dlpack
${
EXTERNAL_PROJECT_LOG_ARGS
}
GIT_REPOSITORY
"https://github.com/dmlc/dlpack.git"
GIT_TAG
"v0.2"
PREFIX
${
DLPACK_SOURCE_DIR
}
UPDATE_COMMAND
""
CONFIGURE_COMMAND
""
BUILD_COMMAND
""
INSTALL_COMMAND
""
TEST_COMMAND
""
)
if
(
${
CMAKE_VERSION
}
VERSION_LESS
"3.3.0"
)
set
(
dummyfile
${
CMAKE_CURRENT_BINARY_DIR
}
/dlpack_dummy.c
)
file
(
WRITE
${
dummyfile
}
"const char *dummy =
\"
${
dummyfile
}
\"
;"
)
add_library
(
dlpack STATIC
${
dummyfile
}
)
else
()
add_library
(
dlpack INTERFACE
)
endif
()
add_dependencies
(
dlpack extern_dlpack
)
LIST
(
APPEND externl_project_dependencies dlpack
)
paddle/fluid/framework/CMakeLists.txt
浏览文件 @
bfc34ac1
...
...
@@ -192,3 +192,6 @@ cc_test(tuple_test SRCS tuple_test.cc )
if
(
NOT WIN32
)
cc_test
(
rw_lock_test SRCS rw_lock_test.cc
)
endif
(
NOT WIN32
)
cc_library
(
dlpack_tensor SRCS dlpack_tensor.cc DEPS tensor dlpack
)
cc_test
(
dlpack_tensor_test SRCS dlpack_tensor_test.cc DEPS dlpack_tensor glog
)
paddle/fluid/framework/dlpack_tensor.cc
0 → 100644
浏览文件 @
bfc34ac1
// 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/dlpack_tensor.h"
namespace
paddle
{
namespace
framework
{
namespace
internal
{
template
<
typename
T
>
static
::
DLDataType
GetDLDataTypeCode
()
{
::
DLDataType
dtype
;
if
(
std
::
is_same
<
T
,
platform
::
float16
>::
value
||
std
::
is_floating_point
<
T
>::
value
)
{
dtype
.
code
=
kDLFloat
;
}
else
if
(
std
::
is_unsigned
<
T
>::
value
)
{
dtype
.
code
=
kDLUInt
;
}
else
if
(
std
::
is_integral
<
T
>::
value
)
{
dtype
.
code
=
kDLInt
;
}
else
{
PADDLE_THROW
(
"Unsupported data type %s"
,
typeid
(
T
).
name
());
}
dtype
.
bits
=
8
*
sizeof
(
T
);
dtype
.
lanes
=
1
;
return
dtype
;
}
static
DLDataType
GetDLDataTypeFromTypeIndex
(
const
std
::
type_index
&
type
)
{
#define REG_DL_DATA_TYPE(type) \
{ std::type_index(typeid(type)), GetDLDataTypeCode<type>() }
static
const
std
::
unordered_map
<
std
::
type_index
,
::
DLDataType
>
type_to_dtype_map
({
REG_DL_DATA_TYPE
(
platform
::
float16
),
// NOLINT
REG_DL_DATA_TYPE
(
float
),
// NOLINT
REG_DL_DATA_TYPE
(
double
),
// NOLINT
REG_DL_DATA_TYPE
(
int
),
// NOLINT
REG_DL_DATA_TYPE
(
int64_t
),
// NOLINT
REG_DL_DATA_TYPE
(
bool
),
// NOLINT
REG_DL_DATA_TYPE
(
size_t
),
// NOLINT
REG_DL_DATA_TYPE
(
int16_t
),
// NOLINT
REG_DL_DATA_TYPE
(
uint8_t
),
// NOLINT
REG_DL_DATA_TYPE
(
int8_t
)
// NOLINT
});
static
auto
type_to_dtype_map_end_it
=
type_to_dtype_map
.
end
();
auto
it
=
type_to_dtype_map
.
find
(
type
);
PADDLE_ENFORCE
(
it
!=
type_to_dtype_map_end_it
,
"Unsupported data type %s"
,
type
.
name
());
return
it
->
second
;
#undef REG_DL_DATA_TYPE
}
struct
DLContextVisitor
:
public
boost
::
static_visitor
<::
DLContext
>
{
inline
::
DLContext
operator
()(
const
platform
::
CPUPlace
&
place
)
const
{
DLContext
ctx
;
ctx
.
device_type
=
kDLCPU
;
ctx
.
device_id
=
0
;
return
ctx
;
}
inline
::
DLContext
operator
()(
const
platform
::
CUDAPlace
&
place
)
const
{
#ifdef PADDLE_WITH_CUDA
DLContext
ctx
;
ctx
.
device_type
=
kDLGPU
;
ctx
.
device_id
=
place
.
device
;
return
ctx
;
#else
PADDLE_THROW
(
"platform::CUDAPlace is not supported in CPU only version"
);
#endif
}
inline
::
DLContext
operator
()(
const
platform
::
CUDAPinnedPlace
&
place
)
const
{
#ifdef PADDLE_WITH_CUDA
DLContext
ctx
;
ctx
.
device_type
=
kDLCPUPinned
;
ctx
.
device_id
=
0
;
return
ctx
;
#else
PADDLE_THROW
(
"platform::CUDAPinnedPlace is not supported in CPU only version"
);
#endif
}
};
}
// namespace internal
DLPackTensor
::
DLPackTensor
(
const
Tensor
&
tensor
,
LaneType
lanes
)
{
// init data, data buffer
t_
.
data
=
const_cast
<
void
*>
(
tensor
.
data
<
void
>
());
// init ctx, DLContext type with device_type and device_id
auto
place
=
tensor
.
place
();
t_
.
ctx
=
boost
::
apply_visitor
(
internal
::
DLContextVisitor
(),
place
);
// init dtype
t_
.
dtype
=
internal
::
GetDLDataTypeFromTypeIndex
(
tensor
.
type
());
t_
.
dtype
.
lanes
=
lanes
;
// init ndim, tensor rank
auto
&
dims
=
tensor
.
dims
();
using
DimType
=
decltype
(
t_
.
ndim
);
// int
t_
.
ndim
=
static_cast
<
DimType
>
(
dims
.
size
());
// init shape, tensor dims
t_
.
shape
=
shape_
;
for
(
DimType
i
=
0
;
i
<
t_
.
ndim
;
++
i
)
{
t_
.
shape
[
i
]
=
dims
[
i
];
}
// init strides, nullptr means the tensor is compact
t_
.
strides
=
nullptr
;
// init byte_offset
t_
.
byte_offset
=
0
;
}
}
// namespace framework
}
// namespace paddle
paddle/fluid/framework/dlpack_tensor.h
0 → 100644
浏览文件 @
bfc34ac1
// 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.
#pragma once
#include <dlpack/dlpack.h>
#include "paddle/fluid/framework/tensor.h"
namespace
paddle
{
namespace
framework
{
class
DLPackTensor
{
public:
using
LaneType
=
decltype
(
::
DLTensor
::
dtype
.
lanes
);
// uint16_t
using
ShapeType
=
std
::
remove_reference
<
decltype
(
::
DLTensor
::
shape
[
0
])
>::
type
;
// int64_t
// lanes is only used in CPU to enable vectorization
explicit
DLPackTensor
(
const
Tensor
&
tensor
,
LaneType
lanes
=
1
);
inline
operator
const
::
DLTensor
&
()
const
{
return
t_
;
}
inline
operator
::
DLTensor
&
()
{
return
t_
;
}
private:
::
DLTensor
t_
;
// The shape in DLTensor is defined as int64_t*
// Add this member to make TVMTensor init without heap allocation
ShapeType
shape_
[
9
];
};
}
// namespace framework
}
// namespace paddle
paddle/fluid/framework/dlpack_tensor_test.cc
0 → 100644
浏览文件 @
bfc34ac1
// 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/dlpack_tensor.h"
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <vector>
namespace
paddle
{
namespace
framework
{
namespace
{
// NOLINT
template
<
typename
T
>
constexpr
uint8_t
GetDLDataTypeCode
()
{
return
std
::
is_same
<
platform
::
float16
,
T
>::
value
||
std
::
is_floating_point
<
T
>::
value
?
static_cast
<
uint8_t
>
(
kDLFloat
)
:
(
std
::
is_unsigned
<
T
>::
value
?
static_cast
<
uint8_t
>
(
kDLUInt
)
:
(
std
::
is_integral
<
T
>::
value
?
static_cast
<
uint8_t
>
(
kDLInt
)
:
static_cast
<
uint8_t
>
(
-
1
)));
}
}
// NOLINT
template
<
typename
T
>
void
TestMain
(
const
platform
::
Place
&
place
,
uint16_t
lanes
)
{
DDim
dims
{
4
,
5
,
6
,
7
};
Tensor
tensor
;
tensor
.
Resize
(
dims
);
void
*
p
=
tensor
.
mutable_data
<
T
>
(
place
);
DLPackTensor
dlpack_tensor
(
tensor
,
lanes
);
::
DLTensor
&
dl_tensor
=
dlpack_tensor
;
CHECK_EQ
(
p
,
dl_tensor
.
data
);
if
(
platform
::
is_cpu_place
(
place
))
{
CHECK_EQ
(
kDLCPU
,
dl_tensor
.
ctx
.
device_type
);
CHECK_EQ
(
0
,
dl_tensor
.
ctx
.
device_id
);
}
else
if
(
platform
::
is_gpu_place
(
place
))
{
CHECK_EQ
(
kDLGPU
,
dl_tensor
.
ctx
.
device_type
);
CHECK_EQ
(
boost
::
get
<
platform
::
CUDAPlace
>
(
place
).
device
,
dl_tensor
.
ctx
.
device_id
);
}
else
if
(
platform
::
is_cuda_pinned_place
(
place
))
{
CHECK_EQ
(
kDLCPUPinned
,
dl_tensor
.
ctx
.
device_type
);
CHECK_EQ
(
0
,
dl_tensor
.
ctx
.
device_id
);
}
else
{
CHECK_EQ
(
false
,
true
);
}
CHECK_EQ
(
dims
.
size
(),
dl_tensor
.
ndim
);
for
(
auto
i
=
0
;
i
<
dims
.
size
();
++
i
)
{
CHECK_EQ
(
dims
[
i
],
dl_tensor
.
shape
[
i
]);
}
CHECK_EQ
(
dl_tensor
.
strides
==
nullptr
,
true
);
CHECK_EQ
(
static_cast
<
uint64_t
>
(
0
),
dl_tensor
.
byte_offset
);
CHECK_EQ
(
lanes
,
dl_tensor
.
dtype
.
lanes
);
CHECK_EQ
(
sizeof
(
T
)
*
8
,
dl_tensor
.
dtype
.
bits
);
CHECK_EQ
(
GetDLDataTypeCode
<
T
>
(),
dl_tensor
.
dtype
.
code
);
}
template
<
typename
T
>
void
TestMainLoop
()
{
#ifdef PADDLE_WITH_CUDA
std
::
vector
<
platform
::
Place
>
places
{
platform
::
CPUPlace
(),
platform
::
CUDAPlace
(
0
),
platform
::
CUDAPinnedPlace
()};
if
(
platform
::
GetCUDADeviceCount
()
>
1
)
{
places
.
emplace_back
(
platform
::
CUDAPlace
(
1
));
}
#else
std
::
vector
<
platform
::
Place
>
places
{
platform
::
CPUPlace
()};
#endif
std
::
vector
<
uint16_t
>
lanes
{
1
,
2
};
for
(
auto
&
p
:
places
)
{
for
(
auto
&
l
:
lanes
)
{
TestMain
<
T
>
(
p
,
l
);
}
}
}
#define PADDLE_DLPACK_TEST(type) \
TEST(dlpack, test_##type) { TestMainLoop<type>(); }
using
float16
=
platform
::
float16
;
PADDLE_DLPACK_TEST
(
float16
);
PADDLE_DLPACK_TEST
(
float
);
PADDLE_DLPACK_TEST
(
double
);
PADDLE_DLPACK_TEST
(
int
);
PADDLE_DLPACK_TEST
(
int64_t
);
PADDLE_DLPACK_TEST
(
bool
);
PADDLE_DLPACK_TEST
(
size_t
);
PADDLE_DLPACK_TEST
(
int16_t
);
PADDLE_DLPACK_TEST
(
uint8_t
);
PADDLE_DLPACK_TEST
(
int8_t
);
#undef PADDLE_DLPACK_TEST
}
// namespace framework
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录