forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Han Zhao
authored
Jul 23, 2021
1 parent
53c3b20
commit 38b7778
Showing
3 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
python/paddle/fluid/tests/unittests/ipu/ipu_graph_with_variable.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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 paddle | ||
import numpy as np | ||
|
||
# 飞桨2.X默认模式为动态图,需要开启静态图模式 | ||
paddle.enable_static() | ||
|
||
# 编译期:调用飞桨的API编写Python程序,如下述代码中定义了一个含conv2d的网络,并使用Adam优化器优化参数。 | ||
x = paddle.static.data(name='x', shape=[3, 4], dtype='float32') | ||
y = paddle.static.nn.fc(x=x, size=2, name='y') | ||
|
||
# 运行期:先运行一次startup program初始化网络参数,然后调用飞桨的Executor和CompiledProgram API运行网络。 | ||
place = paddle.IPUPlace() # 使用何种设备运行网络,IPUPlace表示使用IPU运行 | ||
executor = paddle.static.Executor(place) # 创建执行器 | ||
compiled_program = paddle.static.CompiledProgram( | ||
paddle.static.default_startup_program()) | ||
#executor.run(paddle.static.default_startup_program()) # 运行startup program进行参数初始化 | ||
executor.run(compiled_program) # 运行startup program进行参数初始化 | ||
|
||
prog = paddle.static.default_startup_program() | ||
print("default_startup_program:") | ||
print(prog._to_readable_code()) | ||
|
||
print("---------------------------------") | ||
|
||
prog = paddle.static.default_main_program() | ||
print("default_main_program:") | ||
print(prog._to_readable_code()) | ||
|
||
# 再使用CompiledProgram编译网络,准备执行。 | ||
compiled_program = paddle.static.CompiledProgram( | ||
paddle.static.default_main_program()) | ||
|
||
result = executor.run(compiled_program, | ||
feed={'x': np.random.random([3, 4]).astype('float32')}, | ||
fetch_list=[y]) | ||
print("result = {}".format(result)) | ||
|
||
# 关闭静态图模式 | ||
paddle.disable_static() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# 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 numpy as np | ||
import paddle | ||
import paddle.fluid.core as core | ||
from paddle.static import Program | ||
|
||
paddle.enable_static() | ||
|
||
a = paddle.static.data(name='a', shape=[1], dtype='float32') | ||
b = paddle.static.data(name='b', shape=[1], dtype='float32') | ||
c = a + b | ||
|
||
place = paddle.CPUPlace() | ||
executor = paddle.static.Executor(place) | ||
|
||
print("------------------------") | ||
print("default_startup_program:") | ||
startup_prog = paddle.static.default_startup_program() | ||
print(startup_prog._to_readable_code()) | ||
executor.run(startup_prog) | ||
|
||
print("------------------------") | ||
print("default_main_program:") | ||
main_prog = paddle.static.default_main_program() | ||
print(main_prog._to_readable_code()) | ||
|
||
graph = core.Graph(main_prog.desc) | ||
|
||
print(graph) | ||
|
||
#graph_viz_pass = core.get_pass("graph_viz_pass") | ||
#graph_viz_path = "./test_viz_pass" | ||
#graph_viz_pass.set('graph_viz_path', graph_viz_path) | ||
#graph = graph_viz_pass.apply(graph) | ||
|
||
feed_list = ['a', 'b'] | ||
fetch_list = ['tmp_0'] | ||
|
||
ipu_graph_builder_pass = core.get_pass("ipu_graph_builder_pass") | ||
ipu_graph_builder_pass.set("feed_list", feed_list) | ||
ipu_graph_builder_pass.set("fetch_list", fetch_list) | ||
ipu_graph_builder_pass.apply(graph) | ||
|
||
ipu_runtime_replacer_pass = core.get_pass("ipu_runtime_replacer_pass") | ||
ipu_runtime_replacer_pass.set("feed_list", feed_list) | ||
ipu_runtime_replacer_pass.set("fetch_list", fetch_list) | ||
ipu_runtime_replacer_pass.apply(graph) | ||
|
||
convert_pass = core.get_pass('graph_to_program_pass') | ||
desc = core.ProgramDesc() | ||
convert_pass.set_not_owned('program', desc) | ||
convert_pass.apply(graph) | ||
program = Program._construct_from_desc(desc) | ||
print("Program to run:") | ||
print(program._to_readable_code()) | ||
|
||
result = executor.run(program, | ||
feed={ | ||
'a': np.array( | ||
[1], dtype=np.float32), | ||
'b': np.array( | ||
[1], dtype=np.float32) | ||
}, | ||
fetch_list=[c]) | ||
print("result = {}".format(result)) |
55 changes: 55 additions & 0 deletions
55
python/paddle/fluid/tests/unittests/ipu/ipu_training_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# 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 paddle | ||
import numpy as np | ||
|
||
# 飞桨2.X默认模式为动态图,需要开启静态图模式 | ||
paddle.enable_static() | ||
|
||
# 编译期:调用飞桨的API编写Python程序,如下述代码中定义了一个含conv2d的网络,并使用Adam优化器优化参数。 | ||
image = paddle.static.data( | ||
name='image', shape=[None, 3, 224, 224], dtype='float32') | ||
conv_result = paddle.static.nn.conv2d(image, num_filters=64, filter_size=3) | ||
loss = paddle.mean(conv_result) | ||
adam = paddle.optimizer.Adam(learning_rate=1e-3) | ||
adam.minimize(loss) | ||
|
||
# 运行期:先运行一次startup program初始化网络参数,然后调用飞桨的Executor和CompiledProgram API运行网络。 | ||
place = paddle.IPUPlace() # 使用何种设备运行网络,IPUPlace表示使用IPU运行 | ||
executor = paddle.static.Executor(place) # 创建执行器 | ||
print("---------- startup_program --------------") | ||
prog = paddle.static.default_startup_program() | ||
print(prog._to_readable_code()) | ||
executor.run( | ||
paddle.static.default_startup_program()) # 运行startup program进行参数初始化 | ||
print("---------- main_program --------------") | ||
prog = paddle.static.default_main_program() | ||
print(prog._to_readable_code()) | ||
|
||
# 再使用CompiledProgram编译网络,准备执行。 | ||
compiled_program = paddle.static.CompiledProgram( | ||
paddle.static.default_main_program()) | ||
|
||
BATCH_NUM = 2 | ||
BATCH_SIZE = 32 | ||
|
||
for batch_id in range(BATCH_NUM): | ||
input_image = np.random.random([BATCH_SIZE, 3, 224, 224]).astype('float32') | ||
loss_numpy, = executor.run( | ||
compiled_program, feed={'image': input_image}, fetch_list=[loss]) | ||
print("Batch {}, loss = {}".format(batch_id, loss_numpy)) | ||
|
||
# 关闭静态图模式 | ||
paddle.disable_static() |