Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wmsofts
Paddle
提交
3db6facb
P
Paddle
项目概览
wmsofts
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
3db6facb
编写于
12月 14, 2021
作者:
J
jianghaicheng
提交者:
GitHub
12月 14, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ipu_commit_tests p6 (#38090)
上级
1ca791c7
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
942 addition
and
0 deletion
+942
-0
python/paddle/fluid/tests/unittests/ipu/test_ipu_place.py
python/paddle/fluid/tests/unittests/ipu/test_ipu_place.py
+51
-0
python/paddle/fluid/tests/unittests/ipu/test_ipu_shard.py
python/paddle/fluid/tests/unittests/ipu/test_ipu_shard.py
+70
-0
python/paddle/fluid/tests/unittests/ipu/test_ipu_strategy.py
python/paddle/fluid/tests/unittests/ipu/test_ipu_strategy.py
+57
-0
python/paddle/fluid/tests/unittests/ipu/test_layernorm_op_ipu.py
...paddle/fluid/tests/unittests/ipu/test_layernorm_op_ipu.py
+199
-0
python/paddle/fluid/tests/unittests/ipu/test_log_softmax_op_ipu.py
...ddle/fluid/tests/unittests/ipu/test_log_softmax_op_ipu.py
+110
-0
python/paddle/fluid/tests/unittests/ipu/test_lookuptable_op_ipu.py
...ddle/fluid/tests/unittests/ipu/test_lookuptable_op_ipu.py
+137
-0
python/paddle/fluid/tests/unittests/ipu/test_lr_sheduelr.py
python/paddle/fluid/tests/unittests/ipu/test_lr_sheduelr.py
+101
-0
python/paddle/fluid/tests/unittests/ipu/test_matmul_op_ipu.py
...on/paddle/fluid/tests/unittests/ipu/test_matmul_op_ipu.py
+217
-0
未找到文件。
python/paddle/fluid/tests/unittests/ipu/test_ipu_place.py
0 → 100644
浏览文件 @
3db6facb
# Copyright (c) 2021 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.
from
__future__
import
print_function
import
numpy
as
np
import
unittest
import
sys
sys
.
path
.
append
(
".."
)
import
paddle
import
paddle.fluid
as
fluid
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
"core is not compiled with IPU"
)
class
TestIpuPlace
(
unittest
.
TestCase
):
def
test_ipu_place
(
self
):
num_devices
=
fluid
.
core
.
get_ipu_device_count
()
self
.
assertGreater
(
num_devices
,
0
)
for
i
in
range
(
num_devices
):
place
=
paddle
.
IPUPlace
()
p
=
fluid
.
core
.
Place
()
p
.
set_place
(
place
)
self
.
assertTrue
(
p
.
is_ipu_place
())
def
test_ipu_set_device
(
self
):
num_devices
=
fluid
.
core
.
get_ipu_device_count
()
self
.
assertGreater
(
num_devices
,
0
)
for
i
in
range
(
num_devices
):
paddle
.
set_device
(
'ipu'
)
device
=
paddle
.
get_device
()
self
.
assertTrue
(
device
==
"ipus:{{0-{}}}"
.
format
(
num_devices
-
1
))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ipu/test_ipu_shard.py
0 → 100644
浏览文件 @
3db6facb
# Copyright (c) 2021 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.
from
__future__
import
print_function
import
numpy
as
np
import
unittest
import
sys
import
paddle
import
paddle.fluid
as
fluid
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
"core is not compiled with IPU"
)
class
TestIpuShard
(
unittest
.
TestCase
):
def
_test
(
self
):
# build graph
a
=
paddle
.
static
.
data
(
name
=
'data'
,
shape
=
[
None
,
1
],
dtype
=
'int32'
)
b
=
a
+
2
# scale : scale * x + bias, ipu_index : no
with
paddle
.
fluid
.
ipu_shard
(
ipu_index
=
1
):
c
=
b
+
1
# scale, ipu_index : 1
with
paddle
.
fluid
.
ipu_shard
(
ipu_index
=
2
):
d
=
c
*
2
# scale, ipu_index : 2
with
paddle
.
fluid
.
ipu_shard
(
ipu_index
=
3
):
e
=
d
+
3
# scale, ipu_index : 3
with
paddle
.
fluid
.
ipu_shard
(
ipu_index
=
1
):
e
=
e
+
3
# scale, ipu_index : 1
with
paddle
.
fluid
.
ipu_shard
(
ipu_index
=
2
):
e
=
e
+
3
# scale, ipu_index : 2
with
paddle
.
fluid
.
ipu_shard
(
ipu_index
=
1
):
f
=
paddle
.
tensor
.
pow
(
e
,
2.0
)
# pow, ipu_index : 1
with
paddle
.
fluid
.
ipu_shard
(
ipu_index
=
2
):
g
=
f
-
1
# scale, ipu_index : 2
h
=
g
+
1
# scale, ipu_index : no
ipu_index_list
=
[]
main_prog
=
paddle
.
static
.
default_main_program
()
for
op
in
main_prog
.
global_block
().
ops
:
if
op
.
desc
.
has_attr
(
"ipu_index"
):
ipu_index_list
.
append
(
op
.
desc
.
attr
(
"ipu_index"
))
return
ipu_index_list
def
test_ipu_shard
(
self
):
ipu_index_list
=
self
.
_test
()
expected_ipu_index_list
=
[
1
,
2
,
3
,
1
,
2
,
1
,
2
]
self
.
assertTrue
(
np
.
allclose
(
ipu_index_list
,
expected_ipu_index_list
,
atol
=
0
))
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ipu/test_ipu_strategy.py
0 → 100644
浏览文件 @
3db6facb
# Copyright (c) 2021 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.
from
__future__
import
print_function
import
numpy
as
np
import
unittest
import
sys
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
paddle
.
enable_static
()
SEED
=
2021
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
"core is not compiled with IPU"
)
class
TestConvNet
(
unittest
.
TestCase
):
def
test_training
(
self
):
ipu_strategy
=
compiler
.
get_ipu_strategy
()
assert
ipu_strategy
.
num_ipus
==
1
,
"Default num_ipus must be 1"
assert
ipu_strategy
.
is_training
==
True
,
"Default is_training is True"
assert
ipu_strategy
.
enable_pipelining
==
False
,
\
"Default enable_pipelining is False"
assert
ipu_strategy
.
enable_manual_shard
==
False
,
\
"Default enable_manual_shard is False"
ipu_strategy
.
num_ipus
=
2
assert
ipu_strategy
.
num_ipus
==
2
,
"Set num_ipus Failed"
ipu_strategy
.
is_training
=
False
assert
ipu_strategy
.
is_training
==
False
,
"Set is_training Failed"
ipu_strategy
.
enable_pipelining
=
True
assert
ipu_strategy
.
enable_pipelining
==
True
,
\
"Set enable_pipelining Failed"
ipu_strategy
.
enable_manual_shard
=
True
assert
ipu_strategy
.
enable_manual_shard
==
True
,
\
"Set enable_manual_shard Failed"
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ipu/test_layernorm_op_ipu.py
0 → 100644
浏览文件 @
3db6facb
# Copyright (c) 2021 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.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
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
(),
"core is not compiled with IPU"
)
class
TestBase
(
IPUOpTest
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_feed_attr
()
self
.
set_attrs
()
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
10
,
10
]).
astype
(
'float32'
),
}
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_dtype
=
[
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
self
.
attrs
=
{
"scale"
:
True
,
"shift"
:
True
,
"begin_norm_axis"
:
1
,
"epsilon"
:
1e-05
,
}
def
_test_base
(
self
,
run_ipu
=
True
):
scope
=
fluid
.
core
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
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
=
self
.
feed_dtype
[
0
])
if
self
.
is_training
:
ch
=
self
.
feed_shape
[
0
][
1
]
conv1
=
paddle
.
static
.
nn
.
conv2d
(
x
,
num_filters
=
ch
,
filter_size
=
3
,
bias_attr
=
False
)
scale
=
paddle
.
ParamAttr
(
trainable
=
True
)
bias
=
paddle
.
ParamAttr
(
trainable
=
True
)
out
=
paddle
.
fluid
.
layers
.
nn
.
layer_norm
(
conv1
,
param_attr
=
scale
,
bias_attr
=
bias
,
**
self
.
attrs
)
else
:
# scale = True
# bias = True
scale
=
self
.
attrs
[
'scale'
]
bias
=
self
.
attrs
[
'shift'
]
out
=
paddle
.
fluid
.
layers
.
nn
.
layer_norm
(
x
,
param_attr
=
scale
,
bias_attr
=
bias
,
**
self
.
attrs
)
if
self
.
is_training
:
loss
=
paddle
.
mean
(
out
)
adam
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
1e-2
)
adam
.
minimize
(
loss
)
fetch_list
=
[
loss
.
name
]
else
:
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
=
compiler
.
get_ipu_strategy
()
ipu_strategy
.
is_training
=
self
.
is_training
program
=
compiler
.
IPUCompiledProgram
(
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
program
=
main_prog
if
self
.
is_training
:
result
=
[]
for
_
in
range
(
self
.
epoch
):
loss_res
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
result
.
append
(
loss_res
[
0
])
return
np
.
array
(
result
)
else
:
result
=
exe
.
run
(
program
,
feed
=
self
.
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
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shape
)
@
unittest
.
skip
(
'raise error'
)
class
TestCase1
(
TestBase
):
def
set_attrs
(
self
):
self
.
attrs
=
{
"scale"
:
False
,
"shift"
:
True
,
"begin_norm_axis"
:
1
,
"epsilon"
:
1e-05
,
}
@
unittest
.
skip
(
'raise error'
)
class
TestCase2
(
TestBase
):
def
set_attrs
(
self
):
self
.
attrs
=
{
"scale"
:
True
,
"shift"
:
False
,
"begin_norm_axis"
:
1
,
"epsilon"
:
1e-05
,
}
class
TestCase3
(
TestBase
):
def
set_attrs
(
self
):
self
.
attrs
=
{
"scale"
:
True
,
"shift"
:
True
,
"begin_norm_axis"
:
2
,
"epsilon"
:
1e-05
,
}
class
TestTrainCase1
(
TestBase
):
def
set_atol
(
self
):
self
.
atol
=
1e-3
def
set_training
(
self
):
self
.
is_training
=
True
self
.
epoch
=
10
class
TestTrainCase2
(
TestBase
):
def
set_atol
(
self
):
self
.
atol
=
1e-3
def
set_attrs
(
self
):
self
.
attrs
=
{
"scale"
:
True
,
"shift"
:
True
,
"begin_norm_axis"
:
2
,
"epsilon"
:
1e-05
,
}
def
set_training
(
self
):
self
.
is_training
=
True
self
.
epoch
=
10
# not support `layer_norm(x, param_attr=False, bias_attr=False, **self.attrs)`
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ipu/test_log_softmax_op_ipu.py
0 → 100644
浏览文件 @
3db6facb
# Copyright (c) 2021 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.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
from
paddle.fluid.tests.unittests.ipu.op_test_ipu
import
(
IPUOpTest
,
np_dtype_to_fluid_str
)
import
paddle.nn.functional
as
F
paddle
.
enable_static
()
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
"core is not compiled with IPU"
)
class
TestBase
(
IPUOpTest
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_feed_attr
()
self
.
set_attrs
()
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
1
,
3
,
10
,
10
]).
astype
(
'float32'
)
}
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_dtype
=
[
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
self
.
attrs
=
{
"axis"
:
-
1
}
def
_test_base
(
self
,
run_ipu
=
True
):
scope
=
fluid
.
core
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
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
=
self
.
feed_dtype
[
0
])
out
=
F
.
log_softmax
(
x
,
**
self
.
attrs
)
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
=
compiler
.
get_ipu_strategy
()
ipu_strategy
.
is_training
=
self
.
is_training
program
=
compiler
.
IPUCompiledProgram
(
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
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
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shape
)
class
TestCase1
(
TestBase
):
def
set_attrs
(
self
):
self
.
attrs
=
{
"axis"
:
1
}
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ipu/test_lookuptable_op_ipu.py
0 → 100644
浏览文件 @
3db6facb
# Copyright (c) 2021 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.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
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
(),
"core is not compiled with IPU"
)
class
TestBase
(
IPUOpTest
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_training
()
self
.
set_attrs
()
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_dtype
=
[
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
self
.
attrs
=
{
"size"
:
[
128
,
16
],
"is_sparse"
:
False
,
"is_distributed"
:
False
,
"padding_idx"
:
-
1
,
"dtype"
:
'float32'
}
def
_test_base
(
self
,
run_ipu
=
True
):
scope
=
fluid
.
core
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
SEED
if
run_ipu
:
self
.
feed
=
{
"x"
:
np
.
array
(
[[[
1
],
[
3
]],
[[
2
],
[
4
]],
[[
4
],
[
127
]]]).
astype
(
np
.
int32
)
}
else
:
self
.
feed
=
{
"x"
:
np
.
array
(
[[[
1
],
[
3
]],
[[
2
],
[
4
]],
[[
4
],
[
127
]]]).
astype
(
np
.
int64
)
}
self
.
set_feed_attr
()
with
fluid
.
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
=
self
.
feed_dtype
[
0
])
out
=
paddle
.
fluid
.
layers
.
embedding
(
x
,
**
self
.
attrs
)
if
self
.
is_training
:
loss
=
paddle
.
mean
(
out
)
adam
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
1e-2
)
adam
.
minimize
(
loss
)
fetch_list
=
[
loss
.
name
]
else
:
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
=
compiler
.
get_ipu_strategy
()
ipu_strategy
.
is_training
=
self
.
is_training
program
=
compiler
.
IPUCompiledProgram
(
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
program
=
main_prog
if
self
.
is_training
:
result
=
[]
for
_
in
range
(
self
.
epoch
):
loss_res
=
exe
.
run
(
program
,
feed
=
self
.
feed
,
fetch_list
=
fetch_list
)
result
.
append
(
loss_res
[
0
])
return
np
.
array
(
result
)
else
:
result
=
exe
.
run
(
program
,
feed
=
self
.
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
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shape
)
class
TestTrainCase1
(
TestBase
):
def
set_training
(
self
):
self
.
is_training
=
True
self
.
epoch
=
10
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ipu/test_lr_sheduelr.py
0 → 100644
浏览文件 @
3db6facb
# Copyright (c) 2021 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.
from
__future__
import
print_function
import
numpy
as
np
import
unittest
import
sys
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
from
paddle.optimizer.lr
import
LRScheduler
paddle
.
enable_static
()
SEED
=
2021
class
LR_New
(
LRScheduler
):
def
__init__
(
self
,
learning_rate
=
1.0
,
last_epoch
=-
1
,
verbose
=
False
):
super
(
LR_New
,
self
).
__init__
(
learning_rate
,
last_epoch
,
verbose
)
def
get_lr
(
self
):
self
.
base_lr
=
self
.
base_lr
+
1
self
.
last_epoch
=
self
.
last_epoch
+
1
return
self
.
base_lr
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_ipu
(),
"core is not compiled with IPU"
)
class
TestConvNet
(
unittest
.
TestCase
):
def
_test
(
self
,
run_ipu
=
True
):
scope
=
fluid
.
core
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
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
.
program_guard
(
main_prog
,
startup_prog
):
image
=
paddle
.
static
.
data
(
name
=
'image'
,
shape
=
[
1
,
3
,
10
,
10
],
dtype
=
'float32'
)
conv1
=
paddle
.
static
.
nn
.
conv2d
(
image
,
num_filters
=
3
,
filter_size
=
3
,
bias_attr
=
False
)
loss
=
paddle
.
mean
(
conv1
)
sgd
=
paddle
.
optimizer
.
SGD
(
learning_rate
=
LR_New
())
sgd
.
minimize
(
loss
)
if
run_ipu
:
place
=
paddle
.
IPUPlace
()
else
:
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
if
run_ipu
:
feed_list
=
[
image
.
name
]
fetch_list
=
[
loss
.
name
]
ipu_strategy
=
compiler
.
get_ipu_strategy
()
ipu_strategy
.
is_training
=
True
program
=
compiler
.
IPUCompiledProgram
(
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
program
=
main_prog
result
=
[]
for
epoch
in
range
(
100
):
if
hasattr
(
program
,
"lr_sheduler"
):
program
.
lr_sheduler
.
step
()
loss_res
=
exe
.
run
(
program
,
feed
=
{
image
.
name
:
np_image
},
fetch_list
=
[
loss
])
result
.
append
(
loss_res
)
return
np
.
array
(
result
)
def
test_training
(
self
):
# cpu and ipu dimenstion mismatch, cpu:(100, 1, 1), ipu:(100, 1)
ipu_loss
=
self
.
_test
(
True
).
flatten
()
cpu_loss
=
self
.
_test
(
False
).
flatten
()
self
.
assertTrue
(
np
.
allclose
(
ipu_loss
,
cpu_loss
,
atol
=
1e-4
))
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/ipu/test_matmul_op_ipu.py
0 → 100644
浏览文件 @
3db6facb
# Copyright (c) 2021 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.fluid
as
fluid
import
paddle.fluid.compiler
as
compiler
import
paddle.optimizer
import
paddle.static
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
(),
"core is not compiled with IPU"
)
class
TestBase
(
IPUOpTest
):
def
setUp
(
self
):
self
.
set_atol
()
self
.
set_training
()
self
.
set_feed
()
self
.
set_feed_attr
()
self
.
set_attrs
()
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
2
,
3
]).
astype
(
'float32'
),
"y"
:
np
.
random
.
uniform
(
size
=
[
3
,
2
]).
astype
(
'float32'
),
}
def
set_feed_attr
(
self
):
self
.
feed_shape
=
[
x
.
shape
for
x
in
self
.
feed
.
values
()]
self
.
feed_list
=
list
(
self
.
feed
.
keys
())
self
.
feed_dtype
=
[
np_dtype_to_fluid_str
(
x
.
dtype
)
for
x
in
self
.
feed
.
values
()
]
def
set_attrs
(
self
):
self
.
attrs
=
{
"transpose_x"
:
False
,
"transpose_y"
:
False
,
"alpha"
:
1.0
,
}
def
_test_base
(
self
,
run_ipu
=
True
):
scope
=
fluid
.
core
.
Scope
()
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
SEED
=
self
.
SEED
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
SEED
with
fluid
.
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
=
self
.
feed_dtype
[
0
])
y
=
paddle
.
static
.
data
(
name
=
self
.
feed_list
[
1
],
shape
=
self
.
feed_shape
[
1
],
dtype
=
self
.
feed_dtype
[
1
])
out
=
paddle
.
fluid
.
layers
.
matmul
(
x
,
y
,
**
self
.
attrs
)
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
=
compiler
.
get_ipu_strategy
()
ipu_strategy
.
is_training
=
self
.
is_training
program
=
compiler
.
IPUCompiledProgram
(
main_prog
,
ipu_strategy
=
ipu_strategy
).
compile
(
feed_list
,
fetch_list
)
else
:
program
=
main_prog
result
=
exe
.
run
(
program
,
feed
=
self
.
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
.
flatten
(),
atol
=
self
.
atol
))
self
.
assertTrue
(
res0
.
shape
==
res1
.
shape
)
class
TestCase1
(
TestBase
):
def
set_attrs
(
self
):
self
.
attrs
=
{
"transpose_x"
:
True
,
"transpose_y"
:
True
,
"alpha"
:
1.0
,
}
class
TestCase2
(
TestBase
):
def
set_attrs
(
self
):
self
.
attrs
=
{
"transpose_x"
:
True
,
"transpose_y"
:
True
,
"alpha"
:
3.14
,
}
class
TestCase3
(
TestBase
):
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
5
,
4
,
2
,
3
]).
astype
(
'float32'
),
"y"
:
np
.
random
.
uniform
(
size
=
[
5
,
4
,
3
,
2
]).
astype
(
'float32'
),
}
class
TestCase4
(
TestBase
):
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
4
,
2
,
3
]).
astype
(
'float32'
),
"y"
:
np
.
random
.
uniform
(
size
=
[
4
,
3
,
2
]).
astype
(
'float32'
),
}
class
TestCase5
(
TestBase
):
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
4
,
2
,
3
]).
astype
(
'float32'
),
"y"
:
np
.
random
.
uniform
(
size
=
[
3
,
2
]).
astype
(
'float32'
),
}
class
TestCase6
(
TestBase
):
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
3
]).
astype
(
'float32'
),
"y"
:
np
.
random
.
uniform
(
size
=
[
3
]).
astype
(
'float32'
),
}
@
unittest
.
skip
(
"not supported"
)
class
TestCase6_2
(
TestCase6
):
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
3
]).
astype
(
'float32'
),
"y"
:
np
.
random
.
uniform
(
size
=
[
3
]).
astype
(
'float32'
),
}
def
set_attrs
(
self
):
self
.
attrs
=
{
"transpose_x"
:
True
,
"transpose_y"
:
True
,
"alpha"
:
1.0
,
}
class
TestCase7
(
TestBase
):
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
3
,
1
]).
astype
(
'float32'
),
"y"
:
np
.
random
.
uniform
(
size
=
[
1
,
2
]).
astype
(
'float32'
),
}
@
unittest
.
skip
(
"not supported"
)
class
TestCase7_2
(
TestBase
):
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
3
]).
astype
(
'float32'
),
"y"
:
np
.
random
.
uniform
(
size
=
[
2
]).
astype
(
'float32'
),
}
# equal to
# self.feed = {
# "x": np.random.uniform(size=[3, 1]).astype('float32'),
# "y": np.random.uniform(size=[1, 2]).astype('float32'),
# }
def
set_attrs
(
self
):
self
.
attrs
=
{
"transpose_x"
:
True
,
"transpose_y"
:
True
,
"alpha"
:
1.0
,
}
@
unittest
.
skip
(
"dim > 4 is not supported"
)
class
TestCase8
(
TestBase
):
def
set_feed
(
self
):
self
.
feed
=
{
"x"
:
np
.
random
.
uniform
(
size
=
[
6
,
5
,
4
,
2
,
3
]).
astype
(
'float32'
),
"y"
:
np
.
random
.
uniform
(
size
=
[
6
,
5
,
4
,
3
,
2
]).
astype
(
'float32'
),
}
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录