Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
6ff3596e
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
6ff3596e
编写于
12月 24, 2021
作者:
C
Chen Weihang
提交者:
GitHub
12月 24, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add is dense tensor method (#38424)
上级
6554cc10
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
23 addition
and
13 deletion
+23
-13
paddle/pten/api/include/tensor.h
paddle/pten/api/include/tensor.h
+8
-0
paddle/pten/api/lib/tensor.cc
paddle/pten/api/lib/tensor.cc
+8
-13
paddle/pten/tests/api/test_pten_tensor.cc
paddle/pten/tests/api/test_pten_tensor.cc
+7
-0
未找到文件。
paddle/pten/api/include/tensor.h
浏览文件 @
6ff3596e
...
@@ -204,6 +204,14 @@ class PADDLE_API Tensor final {
...
@@ -204,6 +204,14 @@ class PADDLE_API Tensor final {
*/
*/
DataLayout
layout
()
const
;
DataLayout
layout
()
const
;
/**
* @brief Determine whether tensor is DenseTensor
*
* @return true
* @return false
*/
bool
is_dense_tensor
()
const
;
/* Part 3: Device and Backend methods */
/* Part 3: Device and Backend methods */
/**
/**
...
...
paddle/pten/api/lib/tensor.cc
浏览文件 @
6ff3596e
...
@@ -58,15 +58,6 @@ limitations under the License. */
...
@@ -58,15 +58,6 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
namespace
experimental
{
namespace
experimental
{
namespace
detail
{
inline
bool
IsDenseTensor
(
const
std
::
shared_ptr
<
pten
::
TensorBase
>
&
tensor_impl
)
{
return
tensor_impl
->
type_info
().
name
()
==
"DenseTensor"
;
}
}
// namespace detail
// declare cast api
// declare cast api
Tensor
cast
(
const
Tensor
&
x
,
DataType
out_dtype
);
Tensor
cast
(
const
Tensor
&
x
,
DataType
out_dtype
);
...
@@ -118,7 +109,7 @@ void Tensor::reshape(const std::vector<int64_t> &shape) {
...
@@ -118,7 +109,7 @@ void Tensor::reshape(const std::vector<int64_t> &shape) {
"reason: `reshape` means changing the tensor shape without "
"reason: `reshape` means changing the tensor shape without "
"touching underlying data, this requires the total size of "
"touching underlying data, this requires the total size of "
"the tensor to remain constant."
;
"the tensor to remain constant."
;
if
(
detail
::
IsDenseTensor
(
impl_
))
{
if
(
is_dense_tensor
(
))
{
std
::
dynamic_pointer_cast
<
pten
::
DenseTensor
>
(
impl_
)
->
set_meta
(
std
::
dynamic_pointer_cast
<
pten
::
DenseTensor
>
(
impl_
)
->
set_meta
(
pten
::
DenseTensorMeta
(
dtype
(),
framework
::
make_ddim
(
shape
)));
pten
::
DenseTensorMeta
(
dtype
(),
framework
::
make_ddim
(
shape
)));
}
else
{
}
else
{
...
@@ -133,6 +124,10 @@ DataType Tensor::type() const { return impl_->dtype(); }
...
@@ -133,6 +124,10 @@ DataType Tensor::type() const { return impl_->dtype(); }
DataLayout
Tensor
::
layout
()
const
{
return
impl_
->
layout
();
}
DataLayout
Tensor
::
layout
()
const
{
return
impl_
->
layout
();
}
bool
Tensor
::
is_dense_tensor
()
const
{
return
pten
::
DenseTensor
::
classof
(
impl_
.
get
());
}
/* Part 3: Device and Backend methods */
/* Part 3: Device and Backend methods */
PlaceType
Tensor
::
place
()
const
{
PlaceType
Tensor
::
place
()
const
{
...
@@ -153,7 +148,7 @@ bool Tensor::is_cuda() const {
...
@@ -153,7 +148,7 @@ bool Tensor::is_cuda() const {
template
<
typename
T
>
template
<
typename
T
>
T
*
Tensor
::
mutable_data
()
{
T
*
Tensor
::
mutable_data
()
{
if
(
detail
::
IsDenseTensor
(
impl_
))
{
if
(
is_dense_tensor
(
))
{
return
std
::
dynamic_pointer_cast
<
pten
::
DenseTensor
>
(
impl_
)
return
std
::
dynamic_pointer_cast
<
pten
::
DenseTensor
>
(
impl_
)
->
mutable_data
<
T
>
();
->
mutable_data
<
T
>
();
}
}
...
@@ -209,7 +204,7 @@ Tensor::mutable_data<paddle::platform::float16>(const PlaceType &place);
...
@@ -209,7 +204,7 @@ Tensor::mutable_data<paddle::platform::float16>(const PlaceType &place);
template
<
typename
T
>
template
<
typename
T
>
const
T
*
Tensor
::
data
()
const
{
const
T
*
Tensor
::
data
()
const
{
if
(
detail
::
IsDenseTensor
(
impl_
))
{
if
(
is_dense_tensor
(
))
{
return
std
::
dynamic_pointer_cast
<
pten
::
DenseTensor
>
(
impl_
)
->
data
<
T
>
();
return
std
::
dynamic_pointer_cast
<
pten
::
DenseTensor
>
(
impl_
)
->
data
<
T
>
();
}
}
return
nullptr
;
return
nullptr
;
...
@@ -259,7 +254,7 @@ Tensor::data<paddle::platform::float16>();
...
@@ -259,7 +254,7 @@ Tensor::data<paddle::platform::float16>();
// TODO(chenweihang): replace slice impl by API
// TODO(chenweihang): replace slice impl by API
Tensor
Tensor
::
slice
(
const
int64_t
begin_idx
,
const
int64_t
end_idx
)
const
{
Tensor
Tensor
::
slice
(
const
int64_t
begin_idx
,
const
int64_t
end_idx
)
const
{
if
(
detail
::
IsDenseTensor
(
impl_
))
{
if
(
is_dense_tensor
(
))
{
return
Tensor
(
std
::
make_shared
<
pten
::
DenseTensor
>
(
return
Tensor
(
std
::
make_shared
<
pten
::
DenseTensor
>
(
std
::
move
(
pten
::
CompatibleDenseTensorUtils
::
Slice
(
std
::
move
(
pten
::
CompatibleDenseTensorUtils
::
Slice
(
std
::
dynamic_pointer_cast
<
pten
::
DenseTensor
>
(
impl_
).
get
(),
std
::
dynamic_pointer_cast
<
pten
::
DenseTensor
>
(
impl_
).
get
(),
...
...
paddle/pten/tests/api/test_pten_tensor.cc
浏览文件 @
6ff3596e
...
@@ -205,6 +205,11 @@ void TestInitilized() {
...
@@ -205,6 +205,11 @@ void TestInitilized() {
}
}
}
}
void
TestJudgeTensorType
()
{
experimental
::
Tensor
test_tensor
(
paddle
::
PlaceType
::
kCPU
,
{
1
,
1
});
CHECK
(
test_tensor
.
is_dense_tensor
()
==
true
);
}
TEST
(
PtenTensor
,
All
)
{
TEST
(
PtenTensor
,
All
)
{
VLOG
(
2
)
<<
"TestCopy"
;
VLOG
(
2
)
<<
"TestCopy"
;
GroupTestCopy
();
GroupTestCopy
();
...
@@ -220,6 +225,8 @@ TEST(PtenTensor, All) {
...
@@ -220,6 +225,8 @@ TEST(PtenTensor, All) {
GroupTestCast
();
GroupTestCast
();
VLOG
(
2
)
<<
"TestInitilized"
;
VLOG
(
2
)
<<
"TestInitilized"
;
TestInitilized
();
TestInitilized
();
VLOG
(
2
)
<<
"TestJudgeTensorType"
;
TestJudgeTensorType
();
}
}
}
// namespace tests
}
// namespace tests
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录