Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
061044a0
P
Paddle
项目概览
PaddlePaddle
/
Paddle
接近 2 年 前同步成功
通知
2322
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看板
未验证
提交
061044a0
编写于
3月 08, 2022
作者:
A
Allen Guo
提交者:
GitHub
3月 08, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[IPU] update ipu unittests p4 (#40073)
* update ipu UTs part4 * rename uts * sync api changes * update uts for new api
上级
a279a4f8
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
782 addition
and
490 deletion
+782
-490
python/paddle/fluid/tests/unittests/ipu/test_set_batch_size_ipu.py
...ddle/fluid/tests/unittests/ipu/test_set_batch_size_ipu.py
+55
-41
python/paddle/fluid/tests/unittests/ipu/test_slice_op_ipu.py
python/paddle/fluid/tests/unittests/ipu/test_slice_op_ipu.py
+68
-54
python/paddle/fluid/tests/unittests/ipu/test_softmax_op_ipu.py
...n/paddle/fluid/tests/unittests/ipu/test_softmax_op_ipu.py
+45
-42
python/paddle/fluid/tests/unittests/ipu/test_split_op_ipu.py
python/paddle/fluid/tests/unittests/ipu/test_split_op_ipu.py
+113
-0
python/paddle/fluid/tests/unittests/ipu/test_squeeze_op_ipu.py
...n/paddle/fluid/tests/unittests/ipu/test_squeeze_op_ipu.py
+47
-44
python/paddle/fluid/tests/unittests/ipu/test_stack_op_ipu.py
python/paddle/fluid/tests/unittests/ipu/test_stack_op_ipu.py
+56
-46
python/paddle/fluid/tests/unittests/ipu/test_sum_op_ipu.py
python/paddle/fluid/tests/unittests/ipu/test_sum_op_ipu.py
+80
-63
python/paddle/fluid/tests/unittests/ipu/test_topk_op_ipu.py
python/paddle/fluid/tests/unittests/ipu/test_topk_op_ipu.py
+83
-88
python/paddle/fluid/tests/unittests/ipu/test_transpose_op_ipu.py
...paddle/fluid/tests/unittests/ipu/test_transpose_op_ipu.py
+50
-48
python/paddle/fluid/tests/unittests/ipu/test_unsqueeze_op_ipu.py
...paddle/fluid/tests/unittests/ipu/test_unsqueeze_op_ipu.py
+45
-41
python/paddle/fluid/tests/unittests/ipu/test_varname_inplace_ipu.py
...dle/fluid/tests/unittests/ipu/test_varname_inplace_ipu.py
+14
-23
python/paddle/fluid/tests/unittests/ipu/test_weight_sharing_ipu.py
...ddle/fluid/tests/unittests/ipu/test_weight_sharing_ipu.py
+126
-0
未找到文件。
python/paddle/fluid/tests/unittests/ipu/test_set_batch_size_ipu.py
浏览文件 @
061044a0
...
@@ -16,13 +16,8 @@ import unittest
...
@@ -16,13 +16,8 @@ import unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
...
@@ -31,36 +26,46 @@ class TestBase(IPUOpTest):
...
@@ -31,36 +26,46 @@ class TestBase(IPUOpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_data_feed
()
self
.
set_attrs
()
self
.
set_feed_attr
()
self
.
set_op_attrs
()
def
set_feed
(
self
):
self
.
feed_shape
=
[]
@
property
self
.
feed_shape
.
append
([
-
1
,
3
,
128
,
128
])
def
fp16_enabled
(
self
):
return
True
self
.
feed
=
{}
self
.
feed
[
"in_0"
]
=
np
.
random
.
uniform
(
def
set_atol
(
self
):
size
=
[
2
,
3
,
128
,
128
]).
astype
(
np
.
float32
)
self
.
atol
=
3e-6
self
.
rtol
=
1e-5
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
atol_fp16
=
1e-2
self
.
rtol_fp16
=
1e-3
def
set_attrs
(
self
):
def
set_data_feed
(
self
):
data
=
np
.
random
.
uniform
(
size
=
[
2
,
3
,
128
,
128
])
self
.
feed_fp32
=
{
"in_0"
:
data
.
astype
(
np
.
float32
)}
self
.
feed_fp16
=
{
"in_0"
:
data
.
astype
(
np
.
float16
)}
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
def
set_op_attrs
(
self
):
self
.
attrs
=
{}
self
.
attrs
=
{}
def
_test_base
(
self
,
run_ipu
=
Tru
e
):
def
_test_base
(
self
,
exec_mod
e
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
'float32'
)
dtype
=
'float32'
)
conv1
=
paddle
.
static
.
nn
.
conv2d
(
conv1
=
paddle
.
static
.
nn
.
conv2d
(
x
,
num_filters
=
3
,
filter_size
=
3
,
bias_attr
=
False
)
x
,
num_filters
=
3
,
filter_size
=
3
,
bias_attr
=
False
)
conv2
=
paddle
.
static
.
nn
.
conv2d
(
conv2
=
paddle
.
static
.
nn
.
conv2d
(
...
@@ -70,36 +75,45 @@ class TestBase(IPUOpTest):
...
@@ -70,36 +75,45 @@ class TestBase(IPUOpTest):
conv4
=
paddle
.
static
.
nn
.
conv2d
(
conv4
=
paddle
.
static
.
nn
.
conv2d
(
conv3
,
num_filters
=
3
,
filter_size
=
3
,
bias_attr
=
False
)
conv3
,
num_filters
=
3
,
filter_size
=
3
,
bias_attr
=
False
)
fetch_list
=
[
conv4
.
name
]
fetch_list
=
[
conv4
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
batch_size
=
2
,
is_training
=
self
.
is_training
)
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
program
=
compiler
.
IPUCompiledProgram
(
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
# set batch size
ipu_strategy
.
micro_batch_size
=
2
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
feed
=
self
.
feed_fp32
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
feed
=
self
.
feed_fp16
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
return
result
[
0
]
def
test_base
(
self
):
def
test
(
self
):
res0
=
self
.
_test_base
(
True
)
output_dict
=
{}
res1
=
self
.
_test_base
(
False
)
for
mode
in
ExecutionMode
:
if
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
:
break
output_dict
[
mode
]
=
self
.
_test_base
(
mode
).
flatten
()
self
.
assertTrue
(
self
.
check
(
output_dict
)
np
.
allclose
(
res0
.
flatten
(),
res1
.
flatten
(),
atol
=
self
.
atol
))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
python/paddle/fluid/tests/unittests/ipu/test_slice_op_ipu.py
浏览文件 @
061044a0
...
@@ -16,14 +16,8 @@ import unittest
...
@@ -16,14 +16,8 @@ import unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
(
IPUOpTest
,
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
np_dtype_to_fluid_str
)
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
...
@@ -32,78 +26,88 @@ class TestBase(IPUOpTest):
...
@@ -32,78 +26,88 @@ class TestBase(IPUOpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_
data_
feed
()
self
.
set_feed_attr
()
self
.
set_feed_attr
()
self
.
set_attrs
()
self
.
set_op_attrs
()
@
property
def
fp16_enabled
(
self
):
return
True
def
set_feed
(
self
):
def
set_data_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
4
,
5
,
6
]).
astype
(
'float32'
),
}
data
=
np
.
random
.
uniform
(
size
=
[
4
,
5
,
6
])
self
.
feed_fp32
=
{
"in_0"
:
data
.
astype
(
np
.
float32
)}
self
.
feed_fp16
=
{
"in_0"
:
data
.
astype
(
np
.
float16
)}
def
set_feed_attr
(
self
):
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
self
.
feed_dtype
=
[
self
.
feed_dtype
=
[
x
.
dtype
for
x
in
self
.
feed_fp32
.
values
()]
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
self
.
attrs
=
{
"axes"
:
[
0
,
1
,
2
],
"axes"
:
[
0
,
1
,
2
],
"starts"
:
[
-
3
,
0
,
2
],
"starts"
:
[
-
3
,
0
,
2
],
"ends"
:
[
3
,
2
,
4
],
"ends"
:
[
3
,
2
,
4
],
}
}
def
_test_base
(
self
,
run_ipu
=
Tru
e
):
def
_test_base
(
self
,
exec_mod
e
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
self
.
feed_dtype
[
0
])
dtype
=
'float32'
)
out
=
paddle
.
fluid
.
layers
.
slice
(
x
,
**
self
.
attrs
)
out
=
paddle
.
fluid
.
layers
.
slice
(
x
,
**
self
.
attrs
)
fetch_list
=
[
out
.
name
]
fetch_list
=
[
out
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
feed
=
self
.
feed_fp32
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
feed
=
self
.
feed_fp16
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
return
result
[
0
]
def
test_base
(
self
):
def
test_base
(
self
):
res0
=
self
.
_test_base
(
False
)
output_dict
=
{}
res1
=
self
.
_test_base
(
True
)
for
mode
in
ExecutionMode
:
if
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
:
self
.
assertTrue
(
break
np
.
allclose
(
output_dict
[
mode
]
=
self
.
_test_base
(
mode
)
res0
.
flatten
(),
res1
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shap
e
)
self
.
check
(
output_dict
,
check_shape
=
Tru
e
)
class
TestCase1
(
TestBase
):
class
TestCase1
(
TestBase
):
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
self
.
attrs
=
{
"axes"
:
[
0
,
1
],
"axes"
:
[
0
,
1
],
"starts"
:
[
0
,
0
],
"starts"
:
[
0
,
0
],
...
@@ -113,38 +117,45 @@ class TestCase1(TestBase):
...
@@ -113,38 +117,45 @@ class TestCase1(TestBase):
@
unittest
.
skip
(
'dynamic graph is not support on IPU'
)
@
unittest
.
skip
(
'dynamic graph is not support on IPU'
)
class
TestCase2
(
TestBase
):
class
TestCase2
(
TestBase
):
def
set_feed
(
self
):
def
set_data_feed
(
self
):
self
.
feed
=
{
x
=
np
.
random
.
uniform
(
size
=
[
4
,
5
,
6
])
"x"
:
np
.
random
.
uniform
(
size
=
[
4
,
5
,
6
]).
astype
(
'float32'
),
s
=
np
.
array
([
0
,
0
,
2
])
"starts"
:
np
.
array
([
0
,
0
,
2
]).
astype
(
'int32'
),
e
=
np
.
array
([
3
,
2
,
4
])
"ends"
:
np
.
array
([
3
,
2
,
4
]).
astype
(
'int32'
),
self
.
feed_fp32
=
{
"x"
:
x
.
astype
(
np
.
float32
),
"starts"
:
s
.
astype
(
np
.
int32
),
"ends"
:
e
.
astype
(
np
.
int32
)
}
self
.
feed_fp16
=
{
"x"
:
x
.
astype
(
np
.
float16
),
"starts"
:
s
.
astype
(
np
.
int32
),
"ends"
:
e
.
astype
(
np
.
int32
)
}
}
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axes"
:
[
0
,
1
,
2
]}
self
.
attrs
=
{
"axes"
:
[
0
,
1
,
2
]}
def
_test_base
(
self
,
run_ipu
=
True
):
def
_test_base
(
self
,
run_ipu
=
True
):
scope
=
fluid
.
core
.
Scope
()
scope
=
fluid
.
core
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
self
.
feed_dtype
[
0
]
)
dtype
=
'float32'
)
starts
=
paddle
.
static
.
data
(
starts
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
1
],
name
=
self
.
feed_list
[
1
],
shape
=
self
.
feed_shape
[
1
],
shape
=
self
.
feed_shape
[
1
],
dtype
=
self
.
feed_dtype
[
1
]
)
dtype
=
'int32'
)
ends
=
paddle
.
static
.
data
(
ends
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
2
],
name
=
self
.
feed_list
[
2
],
shape
=
self
.
feed_shape
[
2
],
shape
=
self
.
feed_shape
[
2
],
dtype
=
self
.
feed_dtype
[
2
]
)
dtype
=
'int32'
)
out
=
paddle
.
fluid
.
layers
.
slice
(
out
=
paddle
.
fluid
.
layers
.
slice
(
x
,
starts
=
starts
,
ends
=
ends
,
**
self
.
attrs
)
x
,
starts
=
starts
,
ends
=
ends
,
**
self
.
attrs
)
...
@@ -160,8 +171,8 @@ class TestCase2(TestBase):
...
@@ -160,8 +171,8 @@ class TestCase2(TestBase):
if
run_ipu
:
if
run_ipu
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphC
onfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_c
onfig
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPU
CompiledProgram
(
program
=
paddle
.
static
.
Ipu
CompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
...
@@ -170,6 +181,9 @@ class TestCase2(TestBase):
...
@@ -170,6 +181,9 @@ class TestCase2(TestBase):
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
return
result
[
0
]
def
test_base
(
self
):
pass
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
python/paddle/fluid/tests/unittests/ipu/test_softmax_op_ipu.py
浏览文件 @
061044a0
...
@@ -13,16 +13,11 @@
...
@@ -13,16 +13,11 @@
# limitations under the License.
# limitations under the License.
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
(
IPUOpTest
,
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
np_dtype_to_fluid_str
)
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
...
@@ -31,76 +26,84 @@ class TestBase(IPUOpTest):
...
@@ -31,76 +26,84 @@ class TestBase(IPUOpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_
data_
feed
()
self
.
set_feed_attr
()
self
.
set_feed_attr
()
self
.
set_attrs
()
self
.
set_op_attrs
()
@
property
def
fp16_enabled
(
self
):
return
True
def
set_feed
(
self
):
def
set_
data_
feed
(
self
):
self
.
feed
=
{
data
=
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
20
])
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
]).
astype
(
'float32'
),
self
.
feed_fp32
=
{
"in_0"
:
data
.
astype
(
np
.
float32
)}
}
self
.
feed_fp16
=
{
"in_0"
:
data
.
astype
(
np
.
float16
)
}
def
set_feed_attr
(
self
):
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
self
.
feed_dtype
=
[
self
.
feed_dtype
=
[
x
.
dtype
for
x
in
self
.
feed_fp32
.
values
()]
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axis"
:
-
1
}
self
.
attrs
=
{
"axis"
:
-
1
}
def
_test_base
(
self
,
run_ipu
=
Tru
e
):
def
_test_base
(
self
,
exec_mod
e
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
self
.
feed_dtype
[
0
])
dtype
=
'float32'
)
out
=
paddle
.
fluid
.
layers
.
softmax
(
x
,
**
self
.
attrs
)
out
=
paddle
.
fluid
.
layers
.
softmax
(
x
,
**
self
.
attrs
)
fetch_list
=
[
out
.
name
]
fetch_list
=
[
out
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
feed
=
self
.
feed_fp32
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
feed
=
self
.
feed_fp16
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
return
result
[
0
]
def
test_base
(
self
):
def
test_base
(
self
):
res0
=
self
.
_test_base
(
False
)
output_dict
=
{}
res1
=
self
.
_test_base
(
True
)
for
mode
in
ExecutionMode
:
if
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
:
self
.
assertTrue
(
break
np
.
allclose
(
output_dict
[
mode
]
=
self
.
_test_base
(
mode
).
flatten
()
res0
.
flatten
(),
res1
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shape
)
self
.
check
(
output_dict
)
class
TestCase1
(
TestBase
):
class
TestCase1
(
TestBase
):
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axis"
:
2
}
self
.
attrs
=
{
"axis"
:
2
}
...
...
python/paddle/fluid/tests/unittests/ipu/test_s
gd_optimizer
.py
→
python/paddle/fluid/tests/unittests/ipu/test_s
plit_op_ipu
.py
浏览文件 @
061044a0
# Copyright (c) 202
1
PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 202
2
PaddlePaddle Authors. All Rights Reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# you may not use this file except in compliance with the License.
...
@@ -12,76 +12,101 @@
...
@@ -12,76 +12,101 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
from
__future__
import
print_function
import
unittest
import
numpy
as
np
import
numpy
as
np
import
unittest
import
sys
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.static
import
paddle.fluid.compiler
as
compiler
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
paddle
.
enable_static
()
SEED
=
2021
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
"core is not compiled with IPU"
)
"core is not compiled with IPU"
)
class
TestSGD
(
unittest
.
TestCase
):
class
TestBase
(
IPUOpTest
):
def
_test_sgd
(
self
,
run_ipu
=
True
):
def
setUp
(
self
):
scope
=
fluid
.
core
.
Scope
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_data_feed
()
self
.
set_feed_attr
()
self
.
set_op_attrs
()
@
property
def
fp16_enabled
(
self
):
return
True
def
set_data_feed
(
self
):
data1
=
np
.
random
.
uniform
(
size
=
[
1
,
3
,
10
,
10
])
self
.
feed_fp32
=
{
'x'
:
data1
.
astype
(
np
.
float32
)}
self
.
feed_fp16
=
{
'x'
:
data1
.
astype
(
np
.
float16
)}
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
def
set_op_attrs
(
self
):
self
.
attrs
=
{
"num_or_sections"
:
[
1
,
1
,
1
],
"axis"
:
1
}
def
_test_base
(
self
,
exec_mode
):
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
main_prog
.
random_seed
=
SEED
main_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
np
.
random
.
seed
(
SEED
)
np_image
=
np
.
random
.
rand
(
1
,
3
,
10
,
10
).
astype
(
np
.
float32
)
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
image
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
'image'
,
shape
=
[
1
,
3
,
10
,
10
],
dtype
=
'float32'
)
name
=
self
.
feed_list
[
0
],
conv1
=
paddle
.
static
.
nn
.
conv2d
(
shape
=
self
.
feed_shape
[
0
],
image
,
num_filters
=
3
,
filter_size
=
3
,
bias_attr
=
False
)
dtype
=
'float32'
)
loss
=
paddle
.
mean
(
conv1
)
sgd
=
paddle
.
optimizer
.
SGD
(
learning_rate
=
1e-1
)
out
=
paddle
.
split
(
x
,
**
self
.
attrs
)
sgd
.
minimize
(
loss
)
if
run_ipu
:
fetch_list
=
[
fetch
.
name
for
fetch
in
out
]
place
=
paddle
.
IPUPlace
()
else
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
[
image
.
name
]
feed_list
=
self
.
feed_list
fetch_list
=
[
loss
.
name
]
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
True
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
fetch_list
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
result
=
[]
feed
=
self
.
feed_fp32
for
epoch
in
range
(
100
):
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
loss_res
=
exe
.
run
(
program
,
feed
=
self
.
feed_fp16
feed
=
{
"image"
:
np_image
},
fetch_list
=
[
loss
])
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
result
.
append
(
loss_res
)
return
result
[
0
]
def
test_base
(
self
):
output_dict
=
{}
for
mode
in
ExecutionMode
:
if
(
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
)
or
mode
==
ExecutionMode
.
IPU_POPART_FP16
:
break
output_dict
[
mode
]
=
self
.
_test_base
(
mode
).
flatten
()
return
np
.
array
(
resul
t
)
self
.
check
(
output_dic
t
)
def
test_sgd
(
self
):
# cpu and ipu dimenstion mismatch, cpu:(100, 1, 1), ipu:(100, 1)
ipu_loss
=
self
.
_test_sgd
(
True
).
flatten
()
cpu_loss
=
self
.
_test_sgd
(
False
).
flatten
()
self
.
assertTrue
(
np
.
allclose
(
ipu_loss
,
cpu_loss
,
atol
=
1e-4
))
class
TestCase1
(
TestBase
):
def
set_op_attrs
(
self
):
self
.
attrs
=
{
"num_or_sections"
:
[
2
,
8
],
"axis"
:
2
}
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
python/paddle/fluid/tests/unittests/ipu/test_squeeze_op_ipu.py
浏览文件 @
061044a0
...
@@ -13,16 +13,11 @@
...
@@ -13,16 +13,11 @@
# limitations under the License.
# limitations under the License.
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
(
IPUOpTest
,
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
np_dtype_to_fluid_str
)
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
...
@@ -31,81 +26,89 @@ class TestBase(IPUOpTest):
...
@@ -31,81 +26,89 @@ class TestBase(IPUOpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_
data_
feed
()
self
.
set_feed_attr
()
self
.
set_feed_attr
()
self
.
set_attrs
()
self
.
set_op_attrs
()
@
property
def
fp16_enabled
(
self
):
return
True
def
set_feed
(
self
):
def
set_
data_
feed
(
self
):
self
.
feed
=
{
data
=
np
.
random
.
uniform
(
size
=
[
1
,
3
,
1
,
5
])
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
1
,
5
]).
astype
(
'float32'
),
self
.
feed_fp32
=
{
"in_0"
:
data
.
astype
(
np
.
float32
)}
}
self
.
feed_fp16
=
{
"in_0"
:
data
.
astype
(
np
.
float16
)
}
def
set_feed_attr
(
self
):
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
self
.
feed_dtype
=
[
self
.
feed_dtype
=
[
x
.
dtype
for
x
in
self
.
feed_fp32
.
values
()]
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axes"
:
[
0
]}
self
.
attrs
=
{
"axes"
:
[
0
]}
def
_test_base
(
self
,
run_ipu
=
Tru
e
):
def
_test_base
(
self
,
exec_mod
e
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
self
.
feed_dtype
[
0
])
dtype
=
'float32'
)
out
=
paddle
.
fluid
.
layers
.
squeeze
(
x
,
**
self
.
attrs
)
out
=
paddle
.
fluid
.
layers
.
squeeze
(
x
,
**
self
.
attrs
)
fetch_list
=
[
out
.
name
]
fetch_list
=
[
out
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
i
i
pu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
feed
=
self
.
feed_fp32
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
feed
=
self
.
feed_fp16
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
return
result
[
0
]
def
test_base
(
self
):
def
test_base
(
self
):
res0
=
self
.
_test_base
(
False
)
output_dict
=
{}
res1
=
self
.
_test_base
(
True
)
for
mode
in
ExecutionMode
:
if
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
:
self
.
assertTrue
(
break
np
.
allclose
(
output_dict
[
mode
]
=
self
.
_test_base
(
mode
)
res0
.
flatten
(),
res1
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shap
e
)
self
.
check
(
output_dict
,
check_shape
=
Tru
e
)
class
TestCase1
(
TestBase
):
class
TestCase1
(
TestBase
):
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axes"
:
[]}
self
.
attrs
=
{
"axes"
:
[]}
class
TestCase2
(
TestBase
):
class
TestCase2
(
TestBase
):
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axes"
:
[
-
2
]}
self
.
attrs
=
{
"axes"
:
[
-
2
]}
...
...
python/paddle/fluid/tests/unittests/ipu/test_stack_op_ipu.py
浏览文件 @
061044a0
...
@@ -16,14 +16,8 @@ import unittest
...
@@ -16,14 +16,8 @@ import unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
(
IPUOpTest
,
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
np_dtype_to_fluid_str
)
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
...
@@ -32,86 +26,102 @@ class TestBase(IPUOpTest):
...
@@ -32,86 +26,102 @@ class TestBase(IPUOpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_
data_
feed
()
self
.
set_feed_attr
()
self
.
set_feed_attr
()
self
.
set_attrs
()
self
.
set_op_attrs
()
def
set_feed
(
self
):
@
property
self
.
feed
=
{
def
fp16_enabled
(
self
):
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
2
]).
astype
(
'float32'
),
return
True
"y"
:
np
.
random
.
uniform
(
size
=
[
1
,
2
]).
astype
(
'float32'
),
"z"
:
np
.
random
.
uniform
(
size
=
[
1
,
2
]).
astype
(
'float32'
),
def
set_data_feed
(
self
):
x
=
np
.
random
.
uniform
(
size
=
[
1
,
2
])
y
=
np
.
random
.
uniform
(
size
=
[
1
,
2
])
z
=
np
.
random
.
uniform
(
size
=
[
1
,
2
])
self
.
feed_fp32
=
{
"x"
:
x
.
astype
(
np
.
float32
),
"y"
:
y
.
astype
(
np
.
float32
),
"z"
:
z
.
astype
(
np
.
float32
)
}
self
.
feed_fp16
=
{
"x"
:
x
.
astype
(
np
.
float16
),
"y"
:
y
.
astype
(
np
.
float16
),
"z"
:
z
.
astype
(
np
.
float16
)
}
}
def
set_feed_attr
(
self
):
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
self
.
feed_dtype
=
[
self
.
feed_dtype
=
[
x
.
dtype
for
x
in
self
.
feed_fp32
.
values
()]
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axis"
:
0
}
self
.
attrs
=
{
"axis"
:
0
}
def
_test_base
(
self
,
run_ipu
=
Tru
e
):
def
_test_base
(
self
,
exec_mod
e
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
self
.
feed_dtype
[
0
]
)
dtype
=
'float32'
)
y
=
paddle
.
static
.
data
(
y
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
1
],
name
=
self
.
feed_list
[
1
],
shape
=
self
.
feed_shape
[
1
],
shape
=
self
.
feed_shape
[
1
],
dtype
=
self
.
feed_dtype
[
1
]
)
dtype
=
'float32'
)
z
=
paddle
.
static
.
data
(
z
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
2
],
name
=
self
.
feed_list
[
2
],
shape
=
self
.
feed_shape
[
2
],
shape
=
self
.
feed_shape
[
2
],
dtype
=
self
.
feed_dtype
[
2
])
dtype
=
'float32'
)
out
=
paddle
.
fluid
.
layers
.
stack
([
x
,
y
,
z
],
**
self
.
attrs
)
out
=
paddle
.
fluid
.
layers
.
stack
([
x
,
y
,
z
],
**
self
.
attrs
)
fetch_list
=
[
out
.
name
]
fetch_list
=
[
out
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
feed
=
self
.
feed_fp32
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
feed
=
self
.
feed_fp16
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
return
result
[
0
]
def
test_base
(
self
):
def
test_base
(
self
):
res0
=
self
.
_test_base
(
False
)
output_dict
=
{}
res1
=
self
.
_test_base
(
True
)
for
mode
in
ExecutionMode
:
if
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
:
self
.
assertTrue
(
break
np
.
allclose
(
output_dict
[
mode
]
=
self
.
_test_base
(
mode
)
res0
.
flatten
(),
res1
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shap
e
)
self
.
check
(
output_dict
,
check_shape
=
Tru
e
)
class
TestCase1
(
TestBase
):
class
TestCase1
(
TestBase
):
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axis"
:
-
2
}
self
.
attrs
=
{
"axis"
:
-
2
}
...
...
python/paddle/fluid/tests/unittests/ipu/test_sum_op_ipu.py
浏览文件 @
061044a0
...
@@ -16,14 +16,8 @@ import unittest
...
@@ -16,14 +16,8 @@ import unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
(
IPUOpTest
,
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
np_dtype_to_fluid_str
)
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
...
@@ -32,131 +26,154 @@ class TestBase(IPUOpTest):
...
@@ -32,131 +26,154 @@ class TestBase(IPUOpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_
data_
feed
()
self
.
set_feed_attr
()
self
.
set_feed_attr
()
self
.
set_attrs
()
self
.
set_
op_
attrs
()
def
set_feed
(
self
):
@
property
self
.
feed
=
{
def
fp16_enabled
(
self
):
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
]).
astype
(
'float32'
),
return
True
"y"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
]).
astype
(
'float32'
),
}
def
set_data_feed
(
self
):
x
=
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
])
y
=
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
])
self
.
feed_fp32
=
{
"x"
:
x
.
astype
(
np
.
float32
),
"y"
:
y
.
astype
(
np
.
float32
)}
self
.
feed_fp16
=
{
"x"
:
x
.
astype
(
np
.
float16
),
"y"
:
y
.
astype
(
np
.
float16
)}
def
set_feed_attr
(
self
):
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
self
.
feed_dtype
=
[
self
.
feed_dtype
=
[
x
.
dtype
for
x
in
self
.
feed_fp32
.
values
()]
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{}
self
.
attrs
=
{}
def
_test_base
(
self
,
run_ipu
=
Tru
e
):
def
_test_base
(
self
,
exec_mod
e
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
self
.
feed_dtype
[
0
]
)
dtype
=
'float32'
)
y
=
paddle
.
static
.
data
(
y
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
1
],
name
=
self
.
feed_list
[
1
],
shape
=
self
.
feed_shape
[
1
],
shape
=
self
.
feed_shape
[
1
],
dtype
=
self
.
feed_dtype
[
1
])
dtype
=
'float32'
)
out
=
paddle
.
fluid
.
layers
.
sum
([
x
,
y
],
**
self
.
attrs
)
out
=
paddle
.
fluid
.
layers
.
sum
([
x
,
y
],
**
self
.
attrs
)
fetch_list
=
[
out
.
name
]
fetch_list
=
[
out
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
feed
=
self
.
feed_fp32
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
feed
=
self
.
feed_fp16
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
return
result
[
0
]
def
test_base
(
self
):
def
test_base
(
self
):
res0
=
self
.
_test_base
(
False
)
output_dict
=
{}
res1
=
self
.
_test_base
(
True
)
for
mode
in
ExecutionMode
:
if
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
:
self
.
assertTrue
(
break
np
.
allclose
(
output_dict
[
mode
]
=
self
.
_test_base
(
mode
)
res0
.
flatten
(),
res1
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shap
e
)
self
.
check
(
output_dict
,
check_shape
=
Tru
e
)
@
unittest
.
skip
(
''
)
@
unittest
.
skip
(
''
)
class
TestCase1
(
TestBase
):
class
TestCase1
(
TestBase
):
def
set_feed
(
self
):
def
set_feed
(
self
):
self
.
feed
=
{
x
=
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
])
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
]).
astype
(
'float32'
),
y
=
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
])
"y"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
]).
astype
(
'float32'
),
z
=
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
])
"z"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
2
,
2
]).
astype
(
'float32'
),
self
.
feed_fp32
=
{
"x"
:
x
.
astype
(
np
.
float32
),
"y"
:
y
.
astype
(
np
.
float32
),
"z"
:
y
.
astype
(
np
.
float32
)
}
self
.
feed_fp16
=
{
"x"
:
x
.
astype
(
np
.
float16
),
"y"
:
y
.
astype
(
np
.
float16
),
"z"
:
y
.
astype
(
np
.
float16
)
}
}
def
_test_base
(
self
,
run_ipu
=
Tru
e
):
def
_test_base
(
self
,
exec_mod
e
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
self
.
feed_dtype
[
0
]
)
dtype
=
'float32'
)
y
=
paddle
.
static
.
data
(
y
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
1
],
name
=
self
.
feed_list
[
1
],
shape
=
self
.
feed_shape
[
1
],
shape
=
self
.
feed_shape
[
1
],
dtype
=
self
.
feed_dtype
[
1
]
)
dtype
=
'float32'
)
z
=
paddle
.
static
.
data
(
z
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
2
],
name
=
self
.
feed_list
[
2
],
shape
=
self
.
feed_shape
[
2
],
shape
=
self
.
feed_shape
[
2
],
dtype
=
self
.
feed_dtype
[
2
])
dtype
=
'float32'
)
out
=
paddle
.
fluid
.
layers
.
sum
([
x
,
y
,
z
],
**
self
.
attrs
)
out
=
paddle
.
fluid
.
layers
.
sum
([
x
,
y
,
z
],
**
self
.
attrs
)
fetch_list
=
[
out
.
name
]
fetch_list
=
[
out
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
iipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
iipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
feed
=
self
.
feed_fp32
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
feed
=
self
.
feed_fp16
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
return
result
[
0
]
...
...
python/paddle/fluid/tests/unittests/ipu/test_topk_op_ipu.py
浏览文件 @
061044a0
...
@@ -16,130 +16,125 @@ import unittest
...
@@ -16,130 +16,125 @@ import unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
"core is not compiled with IPU"
)
"core is not compiled with IPU"
)
class
TestTopKOp
(
IPUOpTest
):
class
TestTopKOp
(
IPUOpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_ops
()
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
k
=
3
self
.
set_data_feed
()
self
.
use_K_as_const_variable
=
False
self
.
set_feed_attr
()
self
.
set_test_op
()
self
.
set_feed
()
self
.
set_op_attrs
()
self
.
set_attrs
()
@
property
def
set_ops
(
self
):
def
fp16_enabled
(
self
):
self
.
ops
=
[
return
True
paddle
.
fluid
.
layers
.
topk
,
paddle
.
topk
# use top_k_v2 implementation
def
set_test_op
(
self
):
]
self
.
op
=
paddle
.
fluid
.
layers
.
topk
def
set_feed
(
self
):
def
set_data_feed
(
self
):
self
.
feed_shape
=
[]
data
=
np
.
random
.
uniform
(
size
=
[
3
,
5
])
self
.
feed_shape
.
append
([
3
,
5
])
self
.
feed_fp32
=
{
"in_0"
:
data
.
astype
(
np
.
float32
)}
self
.
feed_fp16
=
{
"in_0"
:
data
.
astype
(
np
.
float16
)}
self
.
feed
=
{}
self
.
feed_list
=
[]
def
set_feed_attr
(
self
):
self
.
feed
[
"in_0"
]
=
np
.
random
.
uniform
(
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
size
=
self
.
feed_shape
[
0
]).
astype
(
np
.
float32
)
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
self
.
feed_list
.
append
(
"in_0"
)
if
self
.
use_K_as_const_variable
:
def
set_op_attrs
(
self
):
# self.feed["in_1"] = np.array([self.k]).astype("int32")
self
.
use_k_as_const_variable
=
False
# self.feed_list.append("in_1")
self
.
attrs
=
{}
pass
if
not
self
.
use_k_as_const_variable
:
print
(
"[TestTopKop] feed data:
\n
%s"
%
self
.
feed
[
"in_0"
])
self
.
attrs
[
"k"
]
=
3
def
set_attrs
(
self
):
def
_test_base
(
self
,
exec_mode
):
self
.
attrs
=
{
scope
=
paddle
.
static
.
Scope
()
# "axis": -1,
# "sorted": True
}
if
not
self
.
use_K_as_const_variable
:
self
.
attrs
[
"k"
]
=
self
.
k
def
_test_base
(
self
,
run_ipu
=
True
,
op
=
None
,
data_feed
=
None
):
assert
(
op
is
not
None
)
assert
(
data_feed
is
not
None
)
scope
=
fluid
.
core
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
'float32'
)
dtype
=
'float32'
)
if
not
self
.
use_K_as_const_variable
:
topk_values
,
topk_indices
=
op
(
x
,
**
self
.
attrs
)
if
not
self
.
use_k_as_const_variable
:
topk_values
,
topk_indices
=
self
.
op
(
x
,
**
self
.
attrs
)
else
:
else
:
# !important, popart cannot accept non const tensor
# !important, popart cannot accept non const tensor
# K_t = paddle.static.data(name="in_1", shape=[1], dtype='int32')
K_t
=
paddle
.
fluid
.
layers
.
fill_constant
(
K_t
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int32'
,
value
=
self
.
k
,
name
=
"in_2"
)
shape
=
[
1
],
dtype
=
'int32'
,
value
=
self
.
k
,
name
=
"in_2"
)
topk_values
,
topk_indices
=
op
(
x
,
K_t
,
**
self
.
attrs
)
topk_values
,
topk_indices
=
self
.
op
(
x
,
K_t
,
**
self
.
attrs
)
fetch_list
=
[
topk_values
.
name
,
topk_indices
.
name
]
fetch_list
=
[
topk_values
.
name
,
topk_indices
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
print
(
"Running inference ..."
)
feed
=
self
.
feed_fp32
result
=
exe
.
run
(
program
,
feed
=
data_feed
,
fetch_list
=
fetch_list
)
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
print
(
"Complete running infrence."
)
feed
=
self
.
feed_fp16
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
return
result
return
result
def
test_base
(
self
):
def
test_base
(
self
):
for
op
in
self
.
ops
:
value_dict
=
{}
res0_topk_values
,
res0_topk_indices
=
self
.
_test_base
(
index_dict
=
{}
True
,
op
=
op
,
data_feed
=
self
.
feed
)
for
mode
in
ExecutionMode
:
res1_topk_values
,
res1_topk_indices
=
self
.
_test_base
(
if
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
:
False
,
op
=
paddle
.
fluid
.
layers
.
topk
,
data_feed
=
self
.
feed
)
break
value
,
index
=
self
.
_test_base
(
mode
)
print
(
"[TestTopKop] IPU res0 values:
\n
%s
\n
"
%
res0_topk_values
)
value_dict
[
mode
]
=
value
print
(
"[TestTopKop] CPU res1 values:
\n
%s
\n
"
%
res1_topk_values
)
index_dict
[
mode
]
=
index
view_type
=
np
.
uint32
print
(
"[TestTopKop] IPU res0 indices:
\n
%s
\n
"
%
self
.
check
(
value_dict
)
res0_topk_indices
.
astype
(
view_type
))
self
.
check
(
index_dict
)
print
(
"[TestTopKop] CPU res1 indices:
\n
%s
\n
"
%
res1_topk_indices
)
self
.
assertTrue
(
class
TestCase2
(
TestTopKOp
):
np
.
allclose
(
def
set_test_op
(
self
):
res0_topk_values
.
flatten
(),
self
.
op
=
paddle
.
topk
res1_topk_values
.
flatten
(),
atol
=
self
.
atol
))
@
unittest
.
skip
(
"Trying to get data as int64 but it is of type int32"
)
self
.
assertTrue
(
class
TestCase3
(
TestTopKOp
):
np
.
allclose
(
def
set_op_attrs
(
self
):
res0_topk_indices
.
astype
(
view_type
).
flatten
(),
self
.
use_k_as_const_variable
=
True
res1_topk_indices
.
flatten
(),
self
.
attrs
=
{}
atol
=
self
.
atol
))
self
.
k
=
2
@
unittest
.
skip
(
"Trying to get data as int64 but it is of type int32"
)
class
TestCase4
(
TestCase3
):
def
set_test_op
(
self
):
self
.
op
=
paddle
.
topk
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
python/paddle/fluid/tests/unittests/ipu/test_transpose_op_ipu.py
浏览文件 @
061044a0
...
@@ -16,14 +16,8 @@ import unittest
...
@@ -16,14 +16,8 @@ import unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
(
IPUOpTest
,
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
np_dtype_to_fluid_str
)
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
...
@@ -32,86 +26,94 @@ class TestBase(IPUOpTest):
...
@@ -32,86 +26,94 @@ class TestBase(IPUOpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_
data_
feed
()
self
.
set_feed_attr
()
self
.
set_feed_attr
()
self
.
set_attrs
()
self
.
set_op_attrs
()
@
property
def
fp16_enabled
(
self
):
return
True
def
set_feed
(
self
):
def
set_
data_
feed
(
self
):
self
.
feed
=
{
data
=
np
.
random
.
uniform
(
size
=
[
1
,
3
,
10
,
10
])
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
10
,
10
]).
astype
(
'float32'
),
self
.
feed_fp32
=
{
"x"
:
data
.
astype
(
np
.
float32
)}
}
self
.
feed_fp16
=
{
"x"
:
data
.
astype
(
np
.
float16
)
}
def
set_feed_attr
(
self
):
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
self
.
feed_dtype
=
[
self
.
feed_dtype
=
[
x
.
dtype
for
x
in
self
.
feed_fp32
.
values
()]
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"perm"
:
[
0
,
2
,
3
,
1
]}
self
.
attrs
=
{
"perm"
:
[
0
,
2
,
3
,
1
]}
def
_test_base
(
self
,
run_ipu
=
Tru
e
):
def
_test_base
(
self
,
exec_mod
e
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
self
.
feed_dtype
[
0
])
dtype
=
'float32'
)
out
=
paddle
.
fluid
.
layers
.
transpose
(
x
,
**
self
.
attrs
)
out
=
paddle
.
fluid
.
layers
.
transpose
(
x
,
**
self
.
attrs
)
fetch_list
=
[
out
.
name
]
fetch_list
=
[
out
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
feed
=
self
.
feed_fp32
return
result
[
0
]
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
feed
=
self
.
feed_fp16
def
test_base
(
self
):
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
res0
=
self
.
_test_base
(
False
)
return
result
[
0
]
res1
=
self
.
_test_base
(
True
)
self
.
assertTrue
(
def
test
(
self
):
np
.
allclose
(
output_dict
=
{}
res0
.
flatten
(),
res1
.
flatten
(),
atol
=
self
.
atol
))
for
mode
in
ExecutionMode
:
if
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
:
break
output_dict
[
mode
]
=
self
.
_test_base
(
mode
).
flatten
()
self
.
assertTrue
(
res0
.
shape
==
res1
.
shap
e
)
self
.
check
(
output_dict
,
check_shape
=
Tru
e
)
class
TestCase1
(
TestBase
):
class
TestCase1
(
TestBase
):
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"perm"
:
[
0
,
1
,
2
,
3
]}
self
.
attrs
=
{
"perm"
:
[
0
,
1
,
2
,
3
]}
class
TestCase2
(
TestBase
):
class
TestCase2
(
TestBase
):
def
set_feed
(
self
):
def
set_
data_
feed
(
self
):
self
.
feed
=
{
data
=
np
.
random
.
uniform
(
size
=
[
1
,
2
,
3
,
4
,
5
])
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
2
,
3
,
4
,
5
]).
astype
(
'float32'
),
self
.
feed_fp32
=
{
"x"
:
data
.
astype
(
np
.
float32
)}
}
self
.
feed_fp16
=
{
"x"
:
data
.
astype
(
np
.
float16
)
}
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"perm"
:
[
4
,
0
,
2
,
3
,
1
]}
self
.
attrs
=
{
"perm"
:
[
4
,
0
,
2
,
3
,
1
]}
...
...
python/paddle/fluid/tests/unittests/ipu/test_unsqueeze_op_ipu.py
浏览文件 @
061044a0
...
@@ -16,14 +16,8 @@ import unittest
...
@@ -16,14 +16,8 @@ import unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
(
IPUOpTest
,
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
,
ExecutionMode
np_dtype_to_fluid_str
)
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
...
@@ -32,79 +26,89 @@ class TestBase(IPUOpTest):
...
@@ -32,79 +26,89 @@ class TestBase(IPUOpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_
data_
feed
()
self
.
set_feed_attr
()
self
.
set_feed_attr
()
self
.
set_attrs
()
self
.
set_op_attrs
()
@
property
def
fp16_enabled
(
self
):
return
True
def
set_feed
(
self
):
def
set_data_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
2
,
3
]).
astype
(
'float32'
)}
data
=
np
.
random
.
uniform
(
size
=
[
1
,
2
,
3
])
self
.
feed_fp32
=
{
"x"
:
data
.
astype
(
np
.
float32
)}
self
.
feed_fp16
=
{
"x"
:
data
.
astype
(
np
.
float16
)}
def
set_feed_attr
(
self
):
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_fp32
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_list
=
list
(
self
.
feed_fp32
.
keys
())
self
.
feed_dtype
=
[
self
.
feed_dtype
=
[
x
.
dtype
for
x
in
self
.
feed_fp32
.
values
()]
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axes"
:
0
}
self
.
attrs
=
{
"axes"
:
0
}
def
_test_base
(
self
,
run_ipu
=
Tru
e
):
def
_test_base
(
self
,
exec_mod
e
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
self
.
feed_dtype
[
0
])
dtype
=
'float32'
)
out
=
paddle
.
fluid
.
layers
.
unsqueeze
(
x
,
**
self
.
attrs
)
out
=
paddle
.
fluid
.
layers
.
unsqueeze
(
x
,
**
self
.
attrs
)
fetch_list
=
[
out
.
name
]
fetch_list
=
[
out
.
name
]
if
run_ipu
:
if
exec_mode
==
ExecutionMode
.
CPU_FP32
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
else
:
place
=
paddle
.
IPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
if
exec_mode
!=
ExecutionMode
.
CPU_FP32
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphConfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_config
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPUCompiledProgram
(
if
exec_mode
==
ExecutionMode
.
IPU_POPART_FP16
:
ipu_strategy
.
set_precision_config
(
enable_fp16
=
True
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
program
=
main_prog
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
feed
=
self
.
feed_fp32
if
exec_mode
>
ExecutionMode
.
IPU_FP32
:
feed
=
self
.
feed_fp16
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
return
result
[
0
]
def
test_base
(
self
):
def
test_base
(
self
):
res0
=
self
.
_test_base
(
False
)
output_dict
=
{}
res1
=
self
.
_test_base
(
True
)
for
mode
in
ExecutionMode
:
if
mode
>
ExecutionMode
.
IPU_FP32
and
not
self
.
fp16_enabled
:
self
.
assertTrue
(
break
np
.
allclose
(
output_dict
[
mode
]
=
self
.
_test_base
(
mode
).
flatten
()
res0
.
flatten
(),
res1
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shap
e
)
self
.
check
(
output_dict
,
check_shape
=
Tru
e
)
class
TestCase1
(
TestBase
):
class
TestCase1
(
TestBase
):
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axes"
:
-
1
}
self
.
attrs
=
{
"axes"
:
-
1
}
class
TestCase2
(
TestBase
):
class
TestCase2
(
TestBase
):
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
"axes"
:
[
1
,
2
]}
self
.
attrs
=
{
"axes"
:
[
1
,
2
]}
...
...
python/paddle/fluid/tests/unittests/ipu/test_varname_inplace.py
→
python/paddle/fluid/tests/unittests/ipu/test_varname_inplace
_ipu
.py
浏览文件 @
061044a0
...
@@ -16,15 +16,8 @@ import unittest
...
@@ -16,15 +16,8 @@ import unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
from
paddle.fluid.executor
import
global_scope
import
paddle.optimizer
import
paddle.static
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
(
IPUOpTest
,
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
np_dtype_to_fluid_str
)
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
...
@@ -33,11 +26,11 @@ class TestBase(IPUOpTest):
...
@@ -33,11 +26,11 @@ class TestBase(IPUOpTest):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_atol
()
self
.
set_training
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_
data_
feed
()
self
.
set_feed_attr
()
self
.
set_feed_attr
()
self
.
set_attrs
()
self
.
set_
op_
attrs
()
def
set_feed
(
self
):
def
set_
data_
feed
(
self
):
self
.
feed
=
{
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
10
,
10
]).
astype
(
'float32'
),
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
10
,
10
]).
astype
(
'float32'
),
}
}
...
@@ -45,25 +38,22 @@ class TestBase(IPUOpTest):
...
@@ -45,25 +38,22 @@ class TestBase(IPUOpTest):
def
set_feed_attr
(
self
):
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_dtype
=
[
self
.
feed_dtype
=
[
x
.
dtype
for
x
in
self
.
feed
.
values
()]
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
def
set_
op_
attrs
(
self
):
self
.
attrs
=
{
self
.
attrs
=
{
"shape"
:
[
30
,
10
],
"shape"
:
[
30
,
10
],
"inplace"
:
True
,
"inplace"
:
True
,
}
}
def
_test_base
(
self
,
run_ipu
=
True
):
def
_test_base
(
self
,
run_ipu
=
True
):
scope
=
fluid
.
core
.
Scope
()
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
scope_guard
(
scope
):
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
name
=
self
.
feed_list
[
0
],
...
@@ -76,12 +66,13 @@ class TestBase(IPUOpTest):
...
@@ -76,12 +66,13 @@ class TestBase(IPUOpTest):
scale2
=
paddle
.
fluid
.
layers
.
scale
(
scale1
,
scale
=
1.3
,
bias
=
0.5
)
scale2
=
paddle
.
fluid
.
layers
.
scale
(
scale1
,
scale
=
1.3
,
bias
=
0.5
)
scale3
=
paddle
.
fluid
.
layers
.
scale
(
scale2
,
scale
=
2
,
bias
=
0.7
)
scale3
=
paddle
.
fluid
.
layers
.
scale
(
scale2
,
scale
=
2
,
bias
=
0.7
)
fetch_list
=
[
scale3
.
name
]
fetch_list
=
[
scale3
.
name
]
if
run_ipu
:
if
run_ipu
:
place
=
paddle
.
IPUPlace
()
place
=
paddle
.
IPUPlace
()
else
:
else
:
place
=
paddle
.
CPUPlace
()
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
exe
.
run
(
startup_prog
)
scale1_out
=
main_prog
.
global_block
().
ops
[
4
].
output
(
"Out"
)[
0
]
scale1_out
=
main_prog
.
global_block
().
ops
[
4
].
output
(
"Out"
)[
0
]
...
@@ -92,8 +83,8 @@ class TestBase(IPUOpTest):
...
@@ -92,8 +83,8 @@ class TestBase(IPUOpTest):
if
run_ipu
:
if
run_ipu
:
feed_list
=
self
.
feed_list
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
SetGraphC
onfig
(
is_training
=
self
.
is_training
)
ipu_strategy
.
set_graph_c
onfig
(
is_training
=
self
.
is_training
)
program
=
compiler
.
IPU
CompiledProgram
(
program
=
paddle
.
static
.
Ipu
CompiledProgram
(
main_prog
,
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
else
:
...
...
python/paddle/fluid/tests/unittests/ipu/test_weight_sharing_ipu.py
0 → 100644
浏览文件 @
061044a0
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
unittest
import
numpy
as
np
import
paddle
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
IPUOpTest
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
"core is not compiled with IPU"
)
class
TestWeightSharing
(
IPUOpTest
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_training
()
self
.
set_data_feed
()
self
.
set_feed_attr
()
self
.
set_op_attrs
()
def
set_atol
(
self
):
self
.
atol
=
1e-6
self
.
rtol
=
1e-5
self
.
atol_fp16
=
1e-2
self
.
rtol_fp16
=
1e-3
def
set_data_feed
(
self
):
x
=
np
.
random
.
randint
(
0
,
768
,
size
=
(
128
,
1
)).
astype
(
np
.
int32
)
self
.
feed_cpu
=
{
"x"
:
x
.
astype
(
np
.
int64
)}
self
.
feed_ipu
=
{
"x"
:
np
.
tile
(
x
.
astype
(
np
.
int64
)[
np
.
newaxis
,
:],
[
3
,
1
,
1
])
}
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed_cpu
.
values
()]
self
.
feed_list
=
list
(
self
.
feed_cpu
.
keys
())
def
set_op_attrs
(
self
):
self
.
attrs
=
{}
def
_test_base
(
self
,
run_ipu
=
True
):
scope
=
paddle
.
static
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
main_prog
.
random_seed
=
self
.
SEED
startup_prog
.
random_seed
=
self
.
SEED
with
paddle
.
static
.
scope_guard
(
scope
):
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
x
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
0
],
shape
=
self
.
feed_shape
[
0
],
dtype
=
'int64'
)
with
paddle
.
static
.
ipu_shard_guard
(
index
=
0
,
stage
=
0
):
y
=
paddle
.
fluid
.
layers
.
embedding
(
input
=
x
,
size
=
[
768
,
768
],
dtype
=
'float32'
,
param_attr
=
paddle
.
fluid
.
ParamAttr
(
name
=
'word_embedding'
),
is_sparse
=
False
)
with
paddle
.
static
.
ipu_shard_guard
(
index
=
1
,
stage
=
1
):
z
=
paddle
.
fluid
.
layers
.
fc
(
input
=
y
,
size
=
768
,
param_attr
=
paddle
.
fluid
.
ParamAttr
(
name
=
"fc"
))
with
paddle
.
static
.
ipu_shard_guard
(
index
=
0
,
stage
=
2
):
out
=
paddle
.
fluid
.
layers
.
matmul
(
x
=
z
,
y
=
main_prog
.
global_block
().
var
(
'word_embedding'
),
transpose_y
=
True
)
fetch_list
=
[
out
.
name
]
if
run_ipu
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
feed_list
=
self
.
feed_list
ipu_strategy
=
paddle
.
static
.
IpuStrategy
()
ipu_strategy
.
set_graph_config
(
num_ipus
=
2
,
is_training
=
self
.
is_training
,
enable_manual_shard
=
True
)
ipu_strategy
.
set_pipelining_config
(
enable_pipelining
=
True
,
batches_per_step
=
3
)
program
=
paddle
.
static
.
IpuCompiledProgram
(
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
program
=
main_prog
feed
=
self
.
feed_ipu
if
run_ipu
else
self
.
feed_cpu
result
=
exe
.
run
(
program
,
feed
=
feed
,
fetch_list
=
fetch_list
)
return
result
[
0
]
def
test_base
(
self
):
res0
=
self
.
_test_base
(
False
)
res1
=
self
.
_test_base
(
True
)
self
.
assertTrue
(
np
.
allclose
(
res0
.
flatten
(),
res1
[
0
].
flatten
(),
atol
=
self
.
atol
))
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录