Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
923f2458
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看板
未验证
提交
923f2458
编写于
1月 10, 2023
作者:
X
xysheng-baidu
提交者:
GitHub
1月 10, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Zero_Dim][unittest] add repeat_interleave unittest for zero_dim (#49596)
上级
35fa30d0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
69 addition
and
0 deletion
+69
-0
python/paddle/fluid/tests/unittests/test_repeat_interleave_op.py
...paddle/fluid/tests/unittests/test_repeat_interleave_op.py
+23
-0
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
+46
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_repeat_interleave_op.py
浏览文件 @
923f2458
...
@@ -104,6 +104,7 @@ class TestRepeatInterleaveOp2(OpTest):
...
@@ -104,6 +104,7 @@ class TestRepeatInterleaveOp2(OpTest):
class
TestIndexSelectAPI
(
unittest
.
TestCase
):
class
TestIndexSelectAPI
(
unittest
.
TestCase
):
def
input_data
(
self
):
def
input_data
(
self
):
self
.
data_zero_dim_x
=
np
.
array
(
0.5
)
self
.
data_x
=
np
.
array
(
self
.
data_x
=
np
.
array
(
[
[
[
1.0
,
2.0
,
3.0
,
4.0
],
[
1.0
,
2.0
,
3.0
,
4.0
],
...
@@ -170,6 +171,19 @@ class TestIndexSelectAPI(unittest.TestCase):
...
@@ -170,6 +171,19 @@ class TestIndexSelectAPI(unittest.TestCase):
expect_out
=
np
.
repeat
(
self
.
data_x
,
repeats
,
axis
=
0
)
expect_out
=
np
.
repeat
(
self
.
data_x
,
repeats
,
axis
=
0
)
np
.
testing
.
assert_allclose
(
expect_out
,
np
.
array
(
res
),
rtol
=
1e-05
)
np
.
testing
.
assert_allclose
(
expect_out
,
np
.
array
(
res
),
rtol
=
1e-05
)
# case 3 zero_dim:
with
program_guard
(
Program
(),
Program
()):
x
=
fluid
.
layers
.
data
(
name
=
'x'
,
shape
=
[])
z
=
paddle
.
repeat_interleave
(
x
,
repeats
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
(
res
,)
=
exe
.
run
(
feed
=
{
'x'
:
self
.
data_zero_dim_x
},
fetch_list
=
[
z
.
name
],
return_numpy
=
False
,
)
expect_out
=
np
.
repeat
(
self
.
data_zero_dim_x
,
repeats
)
np
.
testing
.
assert_allclose
(
expect_out
,
np
.
array
(
res
),
rtol
=
1e-05
)
def
test_dygraph_api
(
self
):
def
test_dygraph_api
(
self
):
self
.
input_data
()
self
.
input_data
()
# case axis none
# case axis none
...
@@ -220,6 +234,15 @@ class TestIndexSelectAPI(unittest.TestCase):
...
@@ -220,6 +234,15 @@ class TestIndexSelectAPI(unittest.TestCase):
expect_out
=
np
.
repeat
(
self
.
data_x
,
index
,
axis
=
0
)
expect_out
=
np
.
repeat
(
self
.
data_x
,
index
,
axis
=
0
)
np
.
testing
.
assert_allclose
(
expect_out
,
np_z
,
rtol
=
1e-05
)
np
.
testing
.
assert_allclose
(
expect_out
,
np_z
,
rtol
=
1e-05
)
# case 3 zero_dim:
with
fluid
.
dygraph
.
guard
():
x
=
fluid
.
dygraph
.
to_variable
(
self
.
data_zero_dim_x
)
index
=
2
z
=
paddle
.
repeat_interleave
(
x
,
index
,
None
)
np_z
=
z
.
numpy
()
expect_out
=
np
.
repeat
(
self
.
data_zero_dim_x
,
index
,
axis
=
None
)
np
.
testing
.
assert_allclose
(
expect_out
,
np_z
,
rtol
=
1e-05
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
浏览文件 @
923f2458
...
@@ -966,6 +966,34 @@ class TestSundryAPI(unittest.TestCase):
...
@@ -966,6 +966,34 @@ class TestSundryAPI(unittest.TestCase):
self
.
assertEqual
(
x1
.
grad
.
numpy
(),
0
)
self
.
assertEqual
(
x1
.
grad
.
numpy
(),
0
)
self
.
assertEqual
(
x2
.
grad
.
numpy
(),
0
)
self
.
assertEqual
(
x2
.
grad
.
numpy
(),
0
)
def
test_repeat_interleave
(
self
):
places
=
[
'cpu'
]
if
paddle
.
is_compiled_with_cuda
():
places
.
append
(
'gpu'
)
for
place
in
places
:
paddle
.
set_device
(
place
)
x
=
paddle
.
randn
(())
x
.
stop_gradient
=
False
out
=
paddle
.
repeat_interleave
(
x
,
2
,
None
)
out
.
backward
()
# check shape of output
self
.
assertEqual
(
out
.
shape
,
[
2
])
# check grad shape
self
.
assertEqual
(
x
.
grad
.
shape
,
[])
repeats
=
paddle
.
to_tensor
([
3
],
dtype
=
'int32'
)
out
=
paddle
.
repeat_interleave
(
x
,
repeats
,
None
)
# check shape of output with 1D repeats
self
.
assertEqual
(
out
.
shape
,
[
3
])
# check grad shape with 1D repeats
self
.
assertEqual
(
x
.
grad
.
shape
,
[])
class
TestSundryAPIStatic
(
unittest
.
TestCase
):
class
TestSundryAPIStatic
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -1380,6 +1408,24 @@ class TestSundryAPIStatic(unittest.TestCase):
...
@@ -1380,6 +1408,24 @@ class TestSundryAPIStatic(unittest.TestCase):
self
.
assertEqual
(
res
[
0
].
shape
,
())
self
.
assertEqual
(
res
[
0
].
shape
,
())
self
.
assertEqual
(
res
[
1
].
shape
,
())
self
.
assertEqual
(
res
[
1
].
shape
,
())
@
prog_scope
()
def
test_repeat_interleave
(
self
):
x
=
paddle
.
full
([],
1.0
,
'float32'
)
out
=
paddle
.
repeat_interleave
(
x
,
2
,
None
)
paddle
.
static
.
append_backward
(
out
)
prog
=
paddle
.
static
.
default_main_program
()
res
=
self
.
exe
.
run
(
prog
,
fetch_list
=
[
out
])
self
.
assertEqual
(
res
[
0
].
shape
,
(
2
,))
repeats
=
paddle
.
to_tensor
([
3
],
dtype
=
'int32'
)
out
=
paddle
.
repeat_interleave
(
x
,
repeats
,
None
)
paddle
.
static
.
append_backward
(
out
)
prog
=
paddle
.
static
.
default_main_program
()
res
=
self
.
exe
.
run
(
prog
,
fetch_list
=
[
out
])
self
.
assertEqual
(
res
[
0
].
shape
,
(
3
,))
# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.
# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.
class
TestNoBackwardAPI
(
unittest
.
TestCase
):
class
TestNoBackwardAPI
(
unittest
.
TestCase
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录