Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
1f3ff41c
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看板
未验证
提交
1f3ff41c
编写于
10月 26, 2022
作者:
S
Siming Dai
提交者:
GitHub
10月 26, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix dlpack deletion (#47310)
* fix dlpack deletion * add unittest * fix unittest
上级
d8314ff5
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
54 addition
and
2 deletion
+54
-2
paddle/fluid/framework/tensor_util.cc
paddle/fluid/framework/tensor_util.cc
+42
-0
paddle/fluid/framework/tensor_util.h
paddle/fluid/framework/tensor_util.h
+2
-0
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+2
-2
python/paddle/tests/test_dlpack.py
python/paddle/tests/test_dlpack.py
+8
-0
未找到文件。
paddle/fluid/framework/tensor_util.cc
浏览文件 @
1f3ff41c
...
@@ -1108,6 +1108,48 @@ void TensorFromDLPack(const ::DLTensor& dl_tensor, phi::DenseTensor* dst) {
...
@@ -1108,6 +1108,48 @@ void TensorFromDLPack(const ::DLTensor& dl_tensor, phi::DenseTensor* dst) {
#endif
#endif
}
}
void
TensorFromDLPack
(
const
DLManagedTensor
*
src
,
phi
::
DenseTensor
*
dst
)
{
std
::
vector
<
int64_t
>
vec
;
std
::
copy
(
src
->
dl_tensor
.
shape
,
src
->
dl_tensor
.
shape
+
src
->
dl_tensor
.
ndim
,
std
::
back_inserter
(
vec
));
framework
::
DDim
vddim
=
phi
::
make_ddim
(
vec
);
dst
->
Resize
(
vddim
);
::
DLDataType
type
=
src
->
dl_tensor
.
dtype
;
auto
src_ptr
=
static_cast
<
const
void
*>
(
src
->
dl_tensor
.
data
);
auto
size
=
phi
::
product
(
vddim
)
*
type
.
bits
/
8
;
if
(
src
->
dl_tensor
.
device
.
device_type
==
kDLCPU
)
{
platform
::
CPUPlace
dst_place
=
platform
::
CPUPlace
();
platform
::
CPUPlace
src_place
=
platform
::
CPUPlace
();
void
*
dst_ptr
=
GetDstPtrByDLDataType
(
type
,
dst
,
dst_place
);
memory
::
Copy
(
dst_place
,
dst_ptr
,
src_place
,
src_ptr
,
size
);
}
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
if
(
src
->
dl_tensor
.
device
.
device_type
==
kDLGPU
)
{
platform
::
CUDAPlace
dst_place
=
platform
::
CUDAPlace
(
src
->
dl_tensor
.
device
.
device_id
);
platform
::
CUDAPlace
src_place
=
platform
::
CUDAPlace
(
src
->
dl_tensor
.
device
.
device_id
);
void
*
dst_ptr
=
GetDstPtrByDLDataType
(
type
,
dst
,
dst_place
);
auto
*
ctx
=
platform
::
DeviceContextPool
::
Instance
().
GetByPlace
(
dst_place
);
// Fix copy by share allocation.
memory
::
Copy
(
dst_place
,
dst_ptr
,
src_place
,
src_ptr
,
size
,
reinterpret_cast
<
const
phi
::
GPUContext
&>
(
*
ctx
).
stream
());
}
#endif
src
->
deleter
(
const_cast
<
DLManagedTensor
*>
(
src
));
#ifdef PADDLE_WITH_XPU
PADDLE_THROW
(
platform
::
errors
::
Unimplemented
(
"XPUPlace is not supported"
));
#endif
}
template
<
typename
T
>
template
<
typename
T
>
std
::
string
format_tensor
(
const
phi
::
DenseTensor
&
tensor
)
{
std
::
string
format_tensor
(
const
phi
::
DenseTensor
&
tensor
)
{
// TODO(zhiqiu): use the print option to format tensor.
// TODO(zhiqiu): use the print option to format tensor.
...
...
paddle/fluid/framework/tensor_util.h
浏览文件 @
1f3ff41c
...
@@ -112,7 +112,9 @@ template <typename T>
...
@@ -112,7 +112,9 @@ template <typename T>
void
TesnorToVector
(
const
phi
::
DenseTensor
&
src
,
std
::
vector
<
T
>*
dst
);
void
TesnorToVector
(
const
phi
::
DenseTensor
&
src
,
std
::
vector
<
T
>*
dst
);
// convert dlpack's DLTensor to tensor
// convert dlpack's DLTensor to tensor
void
TensorFromDLPack
(
const
::
DLTensor
&
dl_tensor
,
phi
::
DenseTensor
*
dst
);
void
TensorFromDLPack
(
const
::
DLTensor
&
dl_tensor
,
phi
::
DenseTensor
*
dst
);
void
TensorFromDLPack
(
const
DLManagedTensor
*
src
,
phi
::
DenseTensor
*
dst
);
//
//
// The implementation of template functions.
// The implementation of template functions.
...
...
paddle/fluid/pybind/pybind.cc
浏览文件 @
1f3ff41c
...
@@ -703,11 +703,11 @@ PYBIND11_MODULE(libpaddle, m) {
...
@@ -703,11 +703,11 @@ PYBIND11_MODULE(libpaddle, m) {
phi
::
DenseTensor
tensor
;
phi
::
DenseTensor
tensor
;
if
(
dl
.
device
.
device_type
==
kDLCPU
)
{
if
(
dl
.
device
.
device_type
==
kDLCPU
)
{
paddle
::
framework
::
TensorFromDLPack
(
d
l
,
&
tensor
);
paddle
::
framework
::
TensorFromDLPack
(
d
mt
,
&
tensor
);
}
}
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
if
(
dl
.
device
.
device_type
==
kDLGPU
)
{
if
(
dl
.
device
.
device_type
==
kDLGPU
)
{
paddle
::
framework
::
TensorFromDLPack
(
d
l
,
&
tensor
);
paddle
::
framework
::
TensorFromDLPack
(
d
mt
,
&
tensor
);
}
}
#endif
#endif
return
tensor
;
return
tensor
;
...
...
python/paddle/tests/test_dlpack.py
浏览文件 @
1f3ff41c
...
@@ -123,6 +123,14 @@ class TestDLPack(unittest.TestCase):
...
@@ -123,6 +123,14 @@ class TestDLPack(unittest.TestCase):
self
.
func_test_dlpack_dtype_conversion
()
self
.
func_test_dlpack_dtype_conversion
()
self
.
func_test_dlpack_dtype_conversion
()
self
.
func_test_dlpack_dtype_conversion
()
def
test_dlpack_deletion
(
self
):
# See Paddle issue 47171
if
paddle
.
is_compiled_with_cuda
():
for
i
in
range
(
80
):
a
=
paddle
.
rand
(
shape
=
[
1024
*
128
,
1024
],
dtype
=
"float32"
)
dlpack
=
paddle
.
utils
.
dlpack
.
to_dlpack
(
a
)
b
=
paddle
.
utils
.
dlpack
.
from_dlpack
(
dlpack
)
class
TestRaiseError
(
unittest
.
TestCase
):
class
TestRaiseError
(
unittest
.
TestCase
):
def
func_test_from_dlpack_raise_type_error
(
self
):
def
func_test_from_dlpack_raise_type_error
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录