Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
300b687a
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
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看板
未验证
提交
300b687a
编写于
3月 14, 2023
作者:
Q
qizhaoaoe
提交者:
GitHub
3月 14, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
implement expand as using tile (#51577)
上级
7a3d05d9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
44 addition
and
2 deletion
+44
-2
python/paddle/fluid/tests/unittests/test_expand_as_v2_op.py
python/paddle/fluid/tests/unittests/test_expand_as_v2_op.py
+7
-2
python/paddle/incubate/autograd/composite_rules.py
python/paddle/incubate/autograd/composite_rules.py
+37
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_expand_as_v2_op.py
浏览文件 @
300b687a
...
...
@@ -24,6 +24,7 @@ import paddle.fluid as fluid
class
TestExpandAsBasic
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"expand_as_v2"
self
.
prim_op_type
=
"comp"
self
.
python_api
=
paddle
.
expand_as
x
=
np
.
random
.
rand
(
100
).
astype
(
"float64"
)
target_tensor
=
np
.
random
.
rand
(
2
,
100
).
astype
(
"float64"
)
...
...
@@ -34,15 +35,16 @@ class TestExpandAsBasic(OpTest):
self
.
outputs
=
{
'Out'
:
output
}
def
test_check_output
(
self
):
self
.
check_output
(
check_eager
=
True
)
self
.
check_output
(
check_eager
=
True
,
check_prim
=
True
)
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
,
check_eager
=
True
)
self
.
check_grad
([
'X'
],
'Out'
,
check_eager
=
True
,
check_prim
=
True
)
class
TestExpandAsOpRank2
(
TestExpandAsBasic
):
def
setUp
(
self
):
self
.
op_type
=
"expand_as_v2"
self
.
prim_op_type
=
"comp"
self
.
python_api
=
paddle
.
expand_as
x
=
np
.
random
.
rand
(
10
,
12
).
astype
(
"float64"
)
target_tensor
=
np
.
random
.
rand
(
10
,
12
).
astype
(
"float64"
)
...
...
@@ -56,6 +58,7 @@ class TestExpandAsOpRank2(TestExpandAsBasic):
class
TestExpandAsOpRank3
(
TestExpandAsBasic
):
def
setUp
(
self
):
self
.
op_type
=
"expand_as_v2"
self
.
prim_op_type
=
"comp"
self
.
python_api
=
paddle
.
expand_as
x
=
np
.
random
.
rand
(
2
,
3
,
20
).
astype
(
"float64"
)
target_tensor
=
np
.
random
.
rand
(
2
,
3
,
20
).
astype
(
"float64"
)
...
...
@@ -69,6 +72,7 @@ class TestExpandAsOpRank3(TestExpandAsBasic):
class
TestExpandAsOpRank4
(
TestExpandAsBasic
):
def
setUp
(
self
):
self
.
op_type
=
"expand_as_v2"
self
.
prim_op_type
=
"comp"
self
.
python_api
=
paddle
.
expand_as
x
=
np
.
random
.
rand
(
1
,
1
,
7
,
16
).
astype
(
"float64"
)
target_tensor
=
np
.
random
.
rand
(
4
,
6
,
7
,
16
).
astype
(
"float64"
)
...
...
@@ -84,6 +88,7 @@ class TestExpandAsOpRank5(TestExpandAsBasic):
def
setUp
(
self
):
self
.
op_type
=
"expand_as_v2"
self
.
prim_op_type
=
"comp"
self
.
python_api
=
paddle
.
expand_as
x
=
np
.
random
.
rand
(
1
,
1
,
7
,
16
).
astype
(
"int64"
)
target_tensor
=
np
.
random
.
rand
(
4
,
6
,
7
,
16
).
astype
(
"float64"
)
...
...
python/paddle/incubate/autograd/composite_rules.py
浏览文件 @
300b687a
...
...
@@ -225,6 +225,43 @@ def expand_v2_composite(x, shape):
return
tile
(
x
,
repeat_times
=
repeat_times
)
@
REGISTER_COMPOSITE
(
'expand_as_v2'
)
def
expand_as_v2_composite
(
x
,
y
,
target_shape
):
"""
define composite rule of op expnad_as_v2, expand_as_v2->expand_as
repeat_times = target_shape / x.shape
out = tile(x, repeat_times = repeat_times)
"""
shape_in
=
x
.
shape
if
y
is
not
None
:
target_shape
=
y
.
shape
assert
target_shape
is
not
None
dim_out
=
len
(
target_shape
)
dim_in
=
len
(
shape_in
)
assert
dim_in
<=
dim_out
and
dim_out
>=
0
repeat_times
=
[]
for
i
in
range
(
dim_out
):
offset
=
dim_out
-
i
dim
=
dim_in
-
offset
size_in
=
shape_in
[
dim
]
if
dim
>=
0
else
1
size_out
=
target_shape
[
i
]
if
size_out
==
-
1
:
assert
dim
>=
0
repeat
=
1
else
:
assert
size_out
%
size_in
==
0
repeat
=
int
(
size_out
/
size_in
)
repeat_times
.
append
(
repeat
)
if
dim_in
<
dim_out
:
shape_in_expand
=
[]
for
i
in
range
(
dim_out
-
dim_in
):
shape_in_expand
.
append
(
1
)
shape_in_expand
.
extend
(
shape_in
)
x_reshape
=
reshape
(
x
,
shape_in_expand
)
return
tile
(
x_reshape
,
repeat_times
=
repeat_times
)
return
tile
(
x
,
repeat_times
=
repeat_times
)
@
REGISTER_COMPOSITE
(
'stack'
)
def
stack_composite
(
x
,
axis
):
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录