Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f736f151
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看板
未验证
提交
f736f151
编写于
7月 17, 2023
作者:
H
HongyuJia
提交者:
GitHub
7月 17, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[0D-Tensor] CINN supports unsqueeze, delete hack in Paddle's pass (#55336)
上级
14551c85
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
56 addition
and
28 deletion
+56
-28
paddle/cinn/hlir/op/elementwise.cc
paddle/cinn/hlir/op/elementwise.cc
+2
-2
paddle/fluid/framework/paddle2cinn/cinn_zero_tensor_trick_pass.cc
...luid/framework/paddle2cinn/cinn_zero_tensor_trick_pass.cc
+0
-26
test/cinn/ops/test_zero_dim_tensor.py
test/cinn/ops/test_zero_dim_tensor.py
+54
-0
未找到文件。
paddle/cinn/hlir/op/elementwise.cc
浏览文件 @
f736f151
...
@@ -755,8 +755,8 @@ std::shared_ptr<OpStrategy> StrategyForExpandDims(
...
@@ -755,8 +755,8 @@ std::shared_ptr<OpStrategy> StrategyForExpandDims(
std
::
vector
<
std
::
vector
<
int
>>
InferShapeForExpandDims
(
std
::
vector
<
std
::
vector
<
int
>>
InferShapeForExpandDims
(
const
std
::
vector
<
std
::
vector
<
int
>>
&
inputs_shape
,
const
std
::
vector
<
std
::
vector
<
int
>>
&
inputs_shape
,
const
framework
::
AttrMapType
&
attrs
)
{
const
framework
::
AttrMapType
&
attrs
)
{
CHECK
(
!
inputs_shape
.
empty
()
&&
!
inputs_shape
[
0
].
empty
()
)
CHECK
(
!
inputs_shape
.
empty
())
<<
"
The input's shape size is 0! Please check again
."
;
<<
"
At least 1 input tensor for expand_dims operator
."
;
CHECK_EQ
(
inputs_shape
.
size
(),
1U
);
CHECK_EQ
(
inputs_shape
.
size
(),
1U
);
const
std
::
vector
<
int
>
&
axes
=
const
std
::
vector
<
int
>
&
axes
=
...
...
paddle/fluid/framework/paddle2cinn/cinn_zero_tensor_trick_pass.cc
浏览文件 @
f736f151
...
@@ -32,32 +32,6 @@ void CinnZeroTensorTrickPass::ApplyImpl(ir::Graph* graph) const {
...
@@ -32,32 +32,6 @@ void CinnZeroTensorTrickPass::ApplyImpl(ir::Graph* graph) const {
"assign_value"
,
"assign_value"
,
"gaussian_random"
,
"gaussian_random"
,
"set_value"
};
"set_value"
};
// NOTE: Hack squeeze2 0D-Tensor input
// If squeeze2 inputs 0D-Tensor and axes, The 0D-Tensor's shape will convert
// to 1D-Tensor, which could lead error. We hack squeeze2's axes attribute to
// resolve this. Change 0D-Tensor input to 1D-Tensor input and then make
// axes->axes[: -1]
for
(
const
ir
::
Node
*
n
:
graph
->
Nodes
())
{
if
(
n
->
IsOp
()
&&
n
->
Op
()
->
Type
()
==
"unsqueeze2"
)
{
if
(
n
->
Op
()
->
HasAttr
(
"axes"
))
{
auto
axes
=
PADDLE_GET_CONST
(
std
::
vector
<
int32_t
>
,
n
->
Op
()
->
GetAttr
(
"axes"
));
for
(
const
ir
::
Node
*
var
:
n
->
inputs
)
{
if
(
var
->
Var
()
&&
var
->
Var
()
->
GetType
()
==
proto
::
VarType
::
LOD_TENSOR
)
{
std
::
vector
<
int64_t
>
shape
=
var
->
Var
()
->
GetShape
();
if
(
shape
.
empty
())
{
axes
.
pop_back
();
n
->
Op
()
->
SetAttr
(
"axes"
,
axes
);
VLOG
(
4
)
<<
"unsqueeze2 axes dims is full, fix dim -> dim[:-1] to "
"avoid 0D-Tensor input error"
;
}
}
}
}
}
}
// CINN ops in this white list support 0D-Tensor, wait-list = {"remainder"}
// CINN ops in this white list support 0D-Tensor, wait-list = {"remainder"}
const
std
::
unordered_set
<
std
::
string
>
white_op_list
{
"elementwise_add"
,
const
std
::
unordered_set
<
std
::
string
>
white_op_list
{
"elementwise_add"
,
"elementwise_sub"
,
"elementwise_sub"
,
...
...
test/cinn/ops/test_zero_dim_tensor.py
浏览文件 @
f736f151
...
@@ -668,6 +668,60 @@ class TestTransposeOp(OpTest):
...
@@ -668,6 +668,60 @@ class TestTransposeOp(OpTest):
self
.
check_outputs_and_grads
()
self
.
check_outputs_and_grads
()
@
OpTestTool
.
skip_if
(
not
is_compiled_with_cuda
(),
"x86 test will be skipped due to timeout."
)
class
TestExpandDimsOp
(
OpTest
):
def
setUp
(
self
):
np
.
random
.
seed
(
2023
)
self
.
dtype
=
"float32"
self
.
init_input
()
def
init_input
(
self
):
self
.
inputs
=
{
"x"
:
np
.
random
.
randint
(
-
10
,
10
,
[]).
astype
(
self
.
dtype
),
}
self
.
unsqueeze_dim
=
[
0
]
self
.
target_shape
=
(
1
,)
def
build_paddle_program
(
self
,
target
):
x
=
paddle
.
to_tensor
(
self
.
inputs
[
"x"
],
stop_gradient
=
False
)
out
=
paddle
.
unsqueeze
(
x
,
self
.
unsqueeze_dim
)
self
.
paddle_outputs
=
[
out
]
def
build_cinn_program
(
self
,
target
):
builder
=
NetBuilder
(
"unsqueeze_op"
)
x
=
builder
.
create_input
(
cinn_dtype_convert
(
self
.
dtype
),
self
.
inputs
[
"x"
].
shape
,
"x"
)
out
=
builder
.
expand_dims
(
x
,
self
.
unsqueeze_dim
)
prog
=
builder
.
build
()
res
=
self
.
get_cinn_output
(
prog
,
target
,
[
x
],
[
self
.
inputs
[
"x"
]],
[
out
])
self
.
cinn_outputs
=
res
self
.
assertEqual
(
res
[
0
].
shape
,
self
.
target_shape
)
def
test_check_results
(
self
):
self
.
check_outputs_and_grads
()
@
OpTestTool
.
skip_if
(
not
is_compiled_with_cuda
(),
"x86 test will be skipped due to timeout."
)
class
TestExpandDimsOp2D
(
TestExpandDimsOp
):
def
init_input
(
self
):
self
.
inputs
=
{
"x"
:
np
.
random
.
randint
(
-
10
,
10
,
[]).
astype
(
self
.
dtype
),
}
self
.
unsqueeze_dim
=
[
0
,
1
]
self
.
target_shape
=
(
1
,
1
,
)
@
OpTestTool
.
skip_if
(
@
OpTestTool
.
skip_if
(
not
is_compiled_with_cuda
(),
"x86 test will be skipped due to timeout."
not
is_compiled_with_cuda
(),
"x86 test will be skipped due to timeout."
)
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录