Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f86d35a2
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
f86d35a2
编写于
4月 16, 2018
作者:
T
typhoonzero
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add sharable tensor
上级
494c262a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
60 addition
and
0 deletion
+60
-0
paddle/fluid/framework/tensor.h
paddle/fluid/framework/tensor.h
+29
-0
paddle/fluid/framework/tensor_impl.h
paddle/fluid/framework/tensor_impl.h
+31
-0
未找到文件。
paddle/fluid/framework/tensor.h
浏览文件 @
f86d35a2
...
...
@@ -98,6 +98,9 @@ class Tensor {
/*! The internal of two tensors share the same memory block. */
inline
Tensor
&
ShareDataWith
(
const
Tensor
&
src
);
/*! Share part of the memory of the two tensors */
inline
Tensor
&
ShareDataWith
(
Tensor
*
src
,
size_t
offset
);
/**
* @brief Return a sub-tensor of the given tensor.
*
...
...
@@ -176,6 +179,32 @@ class Tensor {
std
::
type_index
type_
;
};
template
<
typename
Place
>
struct
SharedPlaceholderImpl
:
public
Placeholder
{
SharedPlaceholderImpl
(
Place
place
,
uint8_t
*
data
,
size_t
size
,
std
::
type_index
type
)
:
ptr_
(
data
),
place_
(
place
),
size_
(
size
),
type_
(
type
)
{}
virtual
size_t
size
()
const
{
return
size_
;
}
virtual
platform
::
Place
place
()
const
{
return
place_
;
}
virtual
void
*
ptr
()
const
{
return
static_cast
<
void
*>
(
ptr_
);
}
virtual
std
::
type_index
type
()
const
{
return
type_
;
}
virtual
void
set_type
(
std
::
type_index
type
)
{
type_
=
type
;
}
virtual
void
set_place
(
platform
::
Place
place
)
{
place_
=
place
;
}
/*! the pointer of memory block. */
uint8_t
*
ptr_
;
/*! the place of memory block. */
platform
::
Place
place_
;
/*! the size of memory block. */
size_t
size_
;
/* the current type of memory */
std
::
type_index
type_
;
};
/*! holds the memory block if allocated. */
std
::
shared_ptr
<
Placeholder
>
holder_
;
...
...
paddle/fluid/framework/tensor_impl.h
浏览文件 @
f86d35a2
...
...
@@ -162,6 +162,37 @@ inline Tensor& Tensor::ShareDataWith(const Tensor& src) {
return
*
this
;
}
inline
Tensor
&
Tensor
::
ShareDataWith
(
Tensor
*
src
,
size_t
offset
)
{
// NOTE: data size is determined by current tensor shape and data type
src
->
check_memory_size
();
PADDLE_ENFORCE_EQ
(
src
->
type
(),
this
->
type
(),
"tensor data type must be the same when sharing data"
);
auto
place
=
src
->
place
();
auto
type
=
src
->
type
();
size_t
size
=
src
->
numel
()
*
SizeOfType
(
src
->
type
());
auto
*
ref
=
static_cast
<
uint8_t
*>
(
src
->
mutable_data
(
place
))
+
offset
;
if
(
platform
::
is_cpu_place
(
place
))
{
holder_
.
reset
(
new
SharedPlaceholderImpl
<
platform
::
CPUPlace
>
(
boost
::
get
<
platform
::
CPUPlace
>
(
place
),
ref
,
size
,
type
));
}
else
if
(
platform
::
is_gpu_place
(
place
)
||
platform
::
is_cuda_pinned_place
(
place
))
{
#ifndef PADDLE_WITH_CUDA
PADDLE_THROW
(
"CUDAPlace or CUDAPinnedPlace is not supported in CPU-only mode."
);
}
#else
if
(
platform
::
is_gpu_place
(
place
))
{
holder_
.
reset
(
new
SharedPlaceholderImpl
<
platform
::
CUDAPlace
>
(
boost
::
get
<
platform
::
CUDAPlace
>
(
place
),
ref
,
size
,
type
));
}
else
if
(
platform
::
is_cuda_pinned_place
(
place
))
{
holder_
.
reset
(
new
SharedPlaceholderImpl
<
platform
::
CUDAPinnedPlace
>
(
boost
::
get
<
platform
::
CUDAPinnedPlace
>
(
place
),
ref
,
size
,
type
));
}
}
#endif
return
*
this
;
}
inline
Tensor
Tensor
::
Slice
(
int
begin_idx
,
int
end_idx
)
const
{
check_memory_size
();
PADDLE_ENFORCE_GE
(
begin_idx
,
0
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录