Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
afeixing77
Paddle
提交
6bd41ecc
P
Paddle
项目概览
afeixing77
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6bd41ecc
编写于
2月 05, 2023
作者:
D
DesmonDay
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix dlpack
上级
5cedad40
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
59 addition
and
15 deletion
+59
-15
paddle/fluid/framework/dlpack_tensor.cc
paddle/fluid/framework/dlpack_tensor.cc
+55
-9
paddle/fluid/framework/dlpack_tensor.h
paddle/fluid/framework/dlpack_tensor.h
+3
-2
paddle/fluid/pybind/tensor.cc
paddle/fluid/pybind/tensor.cc
+1
-4
未找到文件。
paddle/fluid/framework/dlpack_tensor.cc
浏览文件 @
6bd41ecc
...
...
@@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/fluid/framework/dlpack_tensor.h"
#include "pybind11/pybind11.h"
#include "paddle/fluid/framework/convert_utils.h"
#include "paddle/fluid/framework/data_type.h"
...
...
@@ -135,9 +134,60 @@ struct DLDeviceVisitor
};
}
// namespace internal
DLPackTensor
::
DLPackTensor
(
phi
::
DenseTensor
&
tensor
,
LaneType
lanes
)
{
struct
PaddleDLMTensor
{
phi
::
DenseTensor
handle
;
DLManagedTensor
tensor
;
};
void
deleter
(
DLManagedTensor
*
arg
)
{
delete
[]
arg
->
dl_tensor
.
shape
;
delete
[]
arg
->
dl_tensor
.
strides
;
delete
static_cast
<
PaddleDLMTensor
*>
(
arg
->
manager_ctx
);
}
DLManagedTensor
*
toDLPack
(
const
phi
::
DenseTensor
&
src
)
{
PaddleDLMTensor
*
pdDLMTensor
(
new
PaddleDLMTensor
);
pdDLMTensor
->
handle
=
const_cast
<
phi
::
DenseTensor
&>
(
src
);
pdDLMTensor
->
tensor
.
manager_ctx
=
pdDLMTensor
;
pdDLMTensor
->
tensor
.
deleter
=
&
deleter
;
pdDLMTensor
->
tensor
.
dl_tensor
.
data
=
const_cast
<
void
*>
(
src
.
data
());
// init ndim
using
DimType
=
decltype
(
pdDLMTensor
->
tensor
.
dl_tensor
.
ndim
);
// int
pdDLMTensor
->
tensor
.
dl_tensor
.
ndim
=
static_cast
<
DimType
>
(
src
.
dims
().
size
());
DimType
ndim
=
pdDLMTensor
->
tensor
.
dl_tensor
.
ndim
;
// init shape
auto
shape
=
new
int64_t
[
ndim
];
for
(
DimType
i
=
0
;
i
<
ndim
;
++
i
)
{
shape
[
i
]
=
src
.
dims
()[
i
];
}
pdDLMTensor
->
tensor
.
dl_tensor
.
shape
=
shape
;
// init stride
auto
strides
=
new
int64_t
[
ndim
];
for
(
DimType
i
=
0
;
i
<
ndim
;
++
i
)
{
strides
[
i
]
=
1
;
}
for
(
DimType
i
=
ndim
-
2
;
i
>=
0
;
--
i
)
{
strides
[
i
]
=
shape
[
i
+
1
]
*
strides
[
i
+
1
];
}
pdDLMTensor
->
tensor
.
dl_tensor
.
strides
=
strides
;
// init device, DLDevice type with device_type and device_id
auto
place
=
src
.
place
();
pdDLMTensor
->
tensor
.
dl_tensor
.
device
=
paddle
::
platform
::
VisitPlace
(
place
,
internal
::
DLDeviceVisitor
());
pdDLMTensor
->
tensor
.
dl_tensor
.
dtype
=
internal
::
GetDLDataTypeFromTypeIndex
(
framework
::
TransToProtoVarType
(
src
.
dtype
()));
pdDLMTensor
->
tensor
.
dl_tensor
.
byte_offset
=
0
;
return
&
(
pdDLMTensor
->
tensor
);
}
DLPackTensor
::
DLPackTensor
(
const
phi
::
DenseTensor
&
tensor
,
LaneType
lanes
)
{
// init data, data buffer
dt_
=
&
tensor
;
t_
.
data
=
const_cast
<
void
*>
(
tensor
.
data
());
// init device, DLDevice type with device_type and device_id
...
...
@@ -188,19 +238,15 @@ DLPackTensor::DLPackTensor(phi::DenseTensor &tensor, LaneType lanes) {
auto
tensor
=
new
DLManagedTensor
;
tensor
->
dl_tensor
=
t_
;
tensor
->
manager_ctx
=
dt_
;
tensor
->
deleter
=
[](
DLManagedTensor
*
arg
)
{
phi
::
DenseTensor
*
tensor_ptr
=
reinterpret_cast
<
phi
::
DenseTensor
*>
(
arg
->
manager_ctx
);
pybind11
::
handle
tensor_handle
=
pybind11
::
cast
(
tensor_ptr
);
tensor_handle
.
dec_ref
();
delete
[]
arg
->
dl_tensor
.
shape
;
delete
[]
arg
->
dl_tensor
.
strides
;
delete
arg
;
};
tensor
->
manager_ctx
=
nullptr
;
return
tensor
;
}
...
...
paddle/fluid/framework/dlpack_tensor.h
浏览文件 @
6bd41ecc
...
...
@@ -28,7 +28,7 @@ class DLPackTensor {
std
::
remove_reference
<
decltype
(
::
DLTensor
::
shape
[
0
])
>::
type
;
// int64_t
// lanes is only used in CPU to enable vectorization
explicit
DLPackTensor
(
phi
::
DenseTensor
&
tensor
,
LaneType
lanes
=
1
);
explicit
DLPackTensor
(
const
phi
::
DenseTensor
&
tensor
,
LaneType
lanes
=
1
);
inline
operator
const
::
DLTensor
&
()
const
{
return
t_
;
}
...
...
@@ -42,8 +42,9 @@ class DLPackTensor {
// The shape in DLTensor is defined as int64_t*
// Add this member to make TVMTensor init without heap allocation
ShapeType
shape_
[
DDim
::
kMaxRank
];
phi
::
DenseTensor
*
dt_
;
};
DLManagedTensor
*
toDLPack
(
const
phi
::
DenseTensor
&
src
);
}
// namespace framework
}
// namespace paddle
paddle/fluid/pybind/tensor.cc
浏览文件 @
6bd41ecc
...
...
@@ -473,10 +473,7 @@ void BindTensor(pybind11::module &m) { // NOLINT
)DOC"
)
.
def
(
"_to_dlpack"
,
[](
phi
::
DenseTensor
&
self
)
{
pybind11
::
handle
tensor_handle
=
pybind11
::
cast
(
&
self
);
tensor_handle
.
inc_ref
();
DLPackTensor
dlpack_tensor
(
self
,
1
);
DLManagedTensor
*
dmt
=
dlpack_tensor
.
ToDLManagedTensor
();
DLManagedTensor
*
dmt
=
framework
::
toDLPack
(
self
);
auto
capsule
=
pybind11
::
capsule
(
static_cast
<
void
*>
(
dmt
),
"dltensor"
,
[](
PyObject
*
ptr
)
{
if
(
!
PyCapsule_IsValid
(
ptr
,
"dltensor"
))
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录