Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
6468cdeb
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
331
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
6468cdeb
编写于
12月 26, 2019
作者:
L
Liu Yiqun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize the InferShape of reshape and reshape2.
上级
b7f6ed3d
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
33 addition
and
26 deletion
+33
-26
lite/core/tensor.h
lite/core/tensor.h
+9
-0
lite/operators/reshape_op.cc
lite/operators/reshape_op.cc
+24
-26
未找到文件。
lite/core/tensor.h
浏览文件 @
6468cdeb
...
...
@@ -102,6 +102,15 @@ class DDimLite {
DDimLite
Slice
(
int
start
,
int
end
)
const
;
bool
CheckPositive
()
const
{
for
(
size_t
i
=
0
;
i
<
size
();
++
i
)
{
if
(
data_
[
i
]
<=
0
)
{
return
false
;
}
}
return
true
;
}
DDimLite
Flatten2D
(
int
col
)
const
{
return
DDimLite
(
std
::
vector
<
value_type
>
(
{
Slice
(
0
,
col
).
production
(),
Slice
(
col
,
size
()).
production
()}));
...
...
lite/operators/reshape_op.cc
浏览文件 @
6468cdeb
...
...
@@ -27,14 +27,15 @@ bool ReshapeOp::CheckShape() const {
}
bool
ReshapeOp
::
InferShape
()
const
{
auto
shape_tensor_vct
=
param_
.
shape_tensor_vct
;
auto
&
shape_tensor_vct
=
param_
.
shape_tensor_vct
;
auto
*
shape_tensor
=
param_
.
shape_tensor
;
auto
shape_vct
=
param_
.
shape_vct
;
auto
&
shape_vct
=
param_
.
shape_vct
;
std
::
vector
<
int
>
final_shape
;
if
(
shape_tensor_vct
.
size
()
>
0
)
{
final_shape
.
resize
(
shape_tensor_vct
.
size
());
for
(
int
i
=
0
;
i
<
shape_tensor_vct
.
size
();
i
++
)
{
final_shape
.
push_back
(
shape_tensor_vct
[
i
]
->
data
<
int
>
()[
0
])
;
final_shape
[
i
]
=
shape_tensor_vct
[
i
]
->
data
<
int
>
()[
0
]
;
}
}
else
if
(
shape_tensor
!=
nullptr
)
{
auto
*
shape_tensor_data
=
shape_tensor
->
data
<
int
>
();
...
...
@@ -46,7 +47,7 @@ bool ReshapeOp::InferShape() const {
LOG
(
FATAL
)
<<
"input shape error"
;
}
auto
x_dims
=
param_
.
x
->
dims
();
auto
&
x_dims
=
param_
.
x
->
dims
();
auto
output_dims
=
ValidateShape
(
final_shape
,
x_dims
);
param_
.
output
->
Resize
(
output_dims
);
auto
out_lod
=
param_
.
output
->
mutable_lod
();
...
...
@@ -98,8 +99,9 @@ bool Reshape2Op::CheckShape() const {
bool
Reshape2Op
::
InferShape
()
const
{
ReshapeOp
::
InferShape
();
auto
x_dims
=
param_
.
x
->
dims
();
std
::
vector
<
DDim
::
value_type
>
xshape_dims
(
x_dims
.
size
()
+
1
,
0
);
auto
&
x_dims
=
param_
.
x
->
dims
();
DDim
xshape_dims
(
x_dims
.
size
()
+
1
);
xshape_dims
[
0
]
=
0
;
for
(
size_t
i
=
0
;
i
<
x_dims
.
size
();
i
++
)
{
xshape_dims
[
i
+
1
]
=
x_dims
[
i
];
}
...
...
@@ -117,19 +119,15 @@ bool Reshape2Op::AttachImpl(const cpp::OpDesc &opdesc, lite::Scope *scope) {
}
DDim
ValidateShape
(
const
std
::
vector
<
int
>
&
shape
,
const
DDim
&
input_dims
)
{
const
lite
::
DDim
::
value_type
input_size
=
input_dims
.
production
();
auto
input_shape
=
input_dims
.
Vectorize
();
bool
all_positive
=
std
::
all_of
(
input_shape
.
cbegin
(),
input_shape
.
cend
(),
[](
lite
::
DDim
::
value_type
i
)
{
return
i
>
0
;
});
// only one dimension can be set to -1, whose size will be automatically
const
DDim
::
value_type
input_size
=
input_dims
.
production
();
// Only one dimension can be set to -1, whose size will be automatically
// infered.
const
int
unk_dim_val
=
-
1
;
const
int
copy_dim_val
=
0
;
std
::
vector
<
lite
::
DDim
::
value_type
>
output_shape
(
shape
.
size
(),
0
);
lite
::
DDim
::
value_type
capacity
=
1
;
DDim
output_dims
(
shape
.
size
()
);
DDim
::
value_type
capacity
=
1
;
int
unk_dim_idx
=
-
1
;
for
(
size_t
i
=
0
;
i
<
shape
.
size
();
++
i
)
{
if
(
shape
[
i
]
==
unk_dim_val
)
{
...
...
@@ -137,7 +135,7 @@ DDim ValidateShape(const std::vector<int> &shape, const DDim &input_dims) {
<<
"Only one input dimension of Attr(shape) can be unknown."
;
unk_dim_idx
=
i
;
}
else
if
(
shape
[
i
]
==
copy_dim_val
)
{
CHECK_LT
(
static_cast
<
int
>
(
i
),
input_
shape
.
size
())
CHECK_LT
(
static_cast
<
int
>
(
i
),
input_
dims
.
size
())
<<
"The index of dimension to copy from input shape must be less "
"than the size of input shape."
;
}
else
{
...
...
@@ -145,28 +143,28 @@ DDim ValidateShape(const std::vector<int> &shape, const DDim &input_dims) {
"be negtive except one unknown dimension."
;
}
capacity
*=
(
shape
[
i
]
?
static_cast
<
lite
::
DDim
::
value_type
>
(
shape
[
i
])
:
input_shape
[
i
])
;
output_
shape
[
i
]
=
(
shape
[
i
]
?
static_cast
<
lite
::
DDim
::
value_type
>
(
shape
[
i
])
:
input_shape
[
i
])
;
DDim
::
value_type
output_dim_i
=
shape
[
i
]
?
static_cast
<
DDim
::
value_type
>
(
shape
[
i
])
:
input_dims
[
i
]
;
output_
dims
[
i
]
=
output_dim_i
;
capacity
*=
output_dim_i
;
}
if
(
unk_dim_idx
!=
-
1
)
{
if
(
all_positive
)
{
if
(
input_dims
.
CheckPositive
()
)
{
// input_size < 0 and is un-determinate in compile time, skip the check,
// for example, input_dims = [-1, 8, 1, 1], shape = [-1, 3, 8],
// capacity = -24, input_size = -8, output_
shape
[0] = 0
// capacity = -24, input_size = -8, output_
dims
[0] = 0
// the following check will fail.
output_
shape
[
unk_dim_idx
]
=
-
input_size
/
capacity
;
CHECK_EQ
(
output_
shape
[
unk_dim_idx
]
*
capacity
,
-
input_size
)
output_
dims
[
unk_dim_idx
]
=
-
input_size
/
capacity
;
CHECK_EQ
(
output_
dims
[
unk_dim_idx
]
*
capacity
,
-
input_size
)
<<
"Invalid shape is given."
;
}
else
{
output_
shape
[
unk_dim_idx
]
=
-
1
;
output_
dims
[
unk_dim_idx
]
=
-
1
;
}
}
else
{
CHECK_EQ
(
capacity
,
input_size
)
<<
"Invalid shape is given."
;
}
return
lite
::
DDim
(
output_shape
)
;
return
output_dims
;
}
}
// namespace operators
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录