Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
276c159d
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
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看板
未验证
提交
276c159d
编写于
7月 12, 2023
作者:
H
HongyuJia
提交者:
GitHub
7月 12, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[0D-Tensor] CINN supports broadcast_to, fix infershape (#55321)
上级
885d1aec
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
66 addition
and
5 deletion
+66
-5
paddle/cinn/frontend/net_builder.cc
paddle/cinn/frontend/net_builder.cc
+5
-3
paddle/cinn/hlir/op/broadcast.cc
paddle/cinn/hlir/op/broadcast.cc
+7
-2
test/cinn/ops/test_zero_dim_tensor.py
test/cinn/ops/test_zero_dim_tensor.py
+54
-0
未找到文件。
paddle/cinn/frontend/net_builder.cc
浏览文件 @
276c159d
...
...
@@ -322,10 +322,12 @@ Variable NetBuilder::Concat(const std::vector<Variable>& input_vars, int axis) {
Variable
NetBuilder
::
BroadcastTo
(
const
Variable
&
operand
,
const
std
::
vector
<
int
>&
out_shape
)
{
auto
x_shape_size
=
operand
->
shape
.
size
();
if
(
x_shape_size
==
0
)
{
VLOG
(
4
)
<<
"0D-Tensor "
<<
operand
->
id
<<
" broadcast to shape ("
<<
cinn
::
utils
::
Join
(
out_shape
,
","
)
<<
")"
;
return
BroadcastTo
(
operand
,
out_shape
,
{
0
});
}
auto
y_shape_size
=
out_shape
.
size
();
CHECK_GT
(
x_shape_size
,
0
)
<<
"Cannot broadcast a empty operand "
<<
operand
->
id
<<
" to "
<<
cinn
::
utils
::
Join
(
out_shape
,
","
);
CHECK_LE
(
x_shape_size
,
y_shape_size
)
<<
"The broadcast_p's input shape dimension should less than the "
"output's, "
...
...
paddle/cinn/hlir/op/broadcast.cc
浏览文件 @
276c159d
...
...
@@ -236,8 +236,13 @@ std::vector<shape_t> InferShapeForBroadcastTo(
VLOG
(
3
)
<<
"broadcast input shape: "
<<
utils
::
Join
(
inputs_shape
[
0
],
", "
);
VLOG
(
3
)
<<
"broadcast out shape: "
<<
utils
::
Join
(
out_shape
,
", "
);
VLOG
(
3
)
<<
"broadcast_axes shape: "
<<
utils
::
Join
(
broadcast_axes
,
", "
);
CHECK_EQ
(
inputs_shape
[
0
].
size
(),
broadcast_axes
.
size
())
<<
"broadcast_axes's size should be same with the input shape's size"
;
if
(
inputs_shape
[
0
].
empty
())
{
CHECK
(
broadcast_axes
.
size
()
==
1
&&
broadcast_axes
[
0
]
==
0
)
<<
"broadcast_axes's size should be {1} when the input is 0D-Tensor"
;
}
else
{
CHECK_EQ
(
inputs_shape
[
0
].
size
(),
broadcast_axes
.
size
())
<<
"broadcast_axes's size should be same with the input shape's size"
;
}
CHECK_GE
(
out_shape
.
size
(),
broadcast_axes
.
size
())
<<
"broadcast_axes's size should be no more than out_shape's size"
;
...
...
test/cinn/ops/test_zero_dim_tensor.py
浏览文件 @
276c159d
...
...
@@ -630,6 +630,60 @@ class TestScaleOp(OpTest):
self
.
check_outputs_and_grads
()
@
OpTestTool
.
skip_if
(
not
is_compiled_with_cuda
(),
"x86 test will be skipped due to timeout."
)
class
TestBroadcastToOp1D
(
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
.
broadcast_shape
=
[
1
]
def
build_paddle_program
(
self
,
target
):
x
=
paddle
.
to_tensor
(
self
.
inputs
[
"x"
],
stop_gradient
=
False
)
out
=
paddle
.
broadcast_to
(
x
,
shape
=
self
.
broadcast_shape
)
self
.
paddle_outputs
=
[
out
]
def
build_cinn_program
(
self
,
target
):
builder
=
NetBuilder
(
"broadcast_to_op"
)
x
=
builder
.
create_input
(
cinn_dtype_convert
(
self
.
dtype
),
self
.
inputs
[
"x"
].
shape
,
"x"
)
out
=
builder
.
broadcast_to
(
x
,
self
.
broadcast_shape
)
prog
=
builder
.
build
()
res
=
self
.
get_cinn_output
(
prog
,
target
,
[
x
],
[
self
.
inputs
[
"x"
]],
[
out
])
self
.
cinn_outputs
=
res
self
.
assertEqual
(
list
(
res
[
0
].
shape
),
list
(
self
.
broadcast_shape
))
def
test_check_results
(
self
):
self
.
check_outputs_and_grads
()
class
TestBroadcastToOp2D
(
TestBroadcastToOp1D
):
def
init_input
(
self
):
self
.
inputs
=
{
"x"
:
np
.
random
.
randint
(
-
10
,
10
,
[]).
astype
(
self
.
dtype
),
}
self
.
broadcast_shape
=
[
1
,
1
]
class
TestBroadcastToOp3D
(
TestBroadcastToOp1D
):
def
init_input
(
self
):
self
.
inputs
=
{
"x"
:
np
.
random
.
randint
(
-
10
,
10
,
[]).
astype
(
self
.
dtype
),
}
self
.
broadcast_shape
=
[
3
,
3
,
3
]
@
OpTestTool
.
skip_if
(
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录