Skip to content

Commit 2c25994

Browse files
authored
[Ospp] VisualDL Support Paddle PIR (#1279)
* VisualDL Adaptation for Paddle PIR * [Doc] Add document for VisualDL Adaptation for Paddle PIR * Dynamic graph changemodel interfaces support *.json file * [Doc] Add document for VisualDL Adaptation for Paddle PIR * [Doc] Add document for VisualDL Adaptation for Paddle PIR * Update Paddle PIR Visualization.md * Fix code style * Fix paddle dependency
1 parent c988ab5 commit 2c25994

File tree

16 files changed

+843
-52
lines changed

16 files changed

+843
-52
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright (c) 2024 VisualDL Authors. All Rights Reserve.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# =======================================================================
15+
import paddle
16+
from visualdl import LogWriter
17+
"""
18+
pseudocode:
19+
for i in range(1, 10):
20+
a = 2 * i
21+
if i < 5:
22+
if i >= 3:
23+
return a + a
24+
else:
25+
return a - a
26+
else:
27+
if i < 8:
28+
return a * a
29+
else:
30+
return a / a
31+
"""
32+
paddle.enable_static()
33+
34+
35+
def less_than_branch(i, a):
36+
return paddle.static.nn.cond(
37+
i >= 3.0,
38+
lambda: paddle.add(a, a),
39+
lambda: paddle.subtract(a, a),
40+
)
41+
42+
43+
def greater_equal_branch(i, a):
44+
return paddle.static.nn.cond(
45+
i < 8.0,
46+
lambda: paddle.multiply(a, a),
47+
lambda: paddle.divide(a, a),
48+
)
49+
50+
51+
main_program = paddle.static.Program()
52+
startup_program = paddle.static.Program()
53+
with paddle.static.program_guard(main_program, startup_program):
54+
i = paddle.static.data(name="i", shape=[1], dtype='float32')
55+
i.stop_gradient = False
56+
a = 2.0 * i
57+
out = paddle.static.nn.cond(
58+
i < 5.0,
59+
lambda: less_than_branch(i, a),
60+
lambda: greater_equal_branch(i, a),
61+
)
62+
mean = paddle.mean(out)
63+
64+
with LogWriter(logdir="./log/cond_inside_cond_test/") as writer:
65+
writer.add_graph(
66+
model=main_program,
67+
input_spec=[paddle.static.InputSpec([1], dtype='float32')],
68+
verbose=True,
69+
is_pir=True)

demo/components/cond_test.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright (c) 2024 VisualDL Authors. All Rights Reserve.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# =======================================================================
15+
import paddle
16+
from visualdl import LogWriter
17+
18+
paddle.enable_static()
19+
"""
20+
pseudocode:
21+
for i in range(1, 10):
22+
a = 2 * i
23+
if i < 5:
24+
return a + a
25+
else:
26+
return a - a
27+
"""
28+
29+
30+
class ConditionalLayer(paddle.nn.Layer):
31+
def __init__(self):
32+
super(ConditionalLayer, self).__init__()
33+
34+
def forward(self, i):
35+
a = 2.0 * i
36+
out = paddle.static.nn.cond(
37+
i < 5.0,
38+
lambda: paddle.add(a, a),
39+
lambda: paddle.subtract(a, a),
40+
)
41+
return out
42+
43+
44+
main_program = paddle.static.Program()
45+
startup_program = paddle.static.Program()
46+
with paddle.static.program_guard(main_program, startup_program):
47+
i = paddle.static.data(name="i", shape=[1], dtype='float32')
48+
i.stop_gradient = False
49+
a = 2.0 * i
50+
out = paddle.static.nn.cond(
51+
i < 5.0,
52+
lambda: paddle.add(a, a),
53+
lambda: paddle.subtract(a, a),
54+
)
55+
mean = paddle.mean(out)
56+
57+
with LogWriter(logdir="./log/cond_test/") as writer:
58+
writer.add_graph(
59+
model=main_program,
60+
input_spec=[paddle.static.InputSpec([1], 'float32')],
61+
verbose=True,
62+
is_pir=True)

demo/components/pir_graph_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright (c) 2024 VisualDL Authors. All Rights Reserve.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# =======================================================================
15+
import paddle
16+
import paddle.nn.functional as F
17+
from paddle import nn
18+
from visualdl import LogWriter
19+
20+
21+
class MyNet(nn.Layer):
22+
def __init__(self):
23+
super(MyNet, self).__init__()
24+
self.conv1 = nn.Conv2D(
25+
in_channels=1, out_channels=20, kernel_size=5, stride=1, padding=2)
26+
self.max_pool1 = nn.MaxPool2D(kernel_size=2, stride=2)
27+
self.conv2 = nn.Conv2D(
28+
in_channels=20,
29+
out_channels=20,
30+
kernel_size=5,
31+
stride=1,
32+
padding=2)
33+
self.max_pool2 = nn.MaxPool2D(kernel_size=2, stride=2)
34+
self.fc = nn.Linear(in_features=980, out_features=10)
35+
36+
def forward(self, inputs):
37+
x = self.conv1(inputs)
38+
x = F.relu(x)
39+
x = self.max_pool1(x)
40+
x = self.conv2(x)
41+
x = F.relu(x)
42+
x = self.max_pool2(x)
43+
x = paddle.reshape(x, [x.shape[0], -1])
44+
x = self.fc(x)
45+
return x
46+
47+
48+
net = MyNet()
49+
with LogWriter(logdir="./log/pir_graph_test/") as writer:
50+
writer.add_graph(
51+
model=net,
52+
input_spec=[paddle.static.InputSpec([-1, 1, 28, 28], 'float32')],
53+
verbose=True,
54+
is_pir=True)
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
# Copyright (c) 2024 VisualDL Authors. All Rights Reserve.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# =======================================================================
115
import paddle
2-
from paddle import ir
3-
416
from visualdl import LogWriter
517

618
paddle.enable_static()
@@ -18,11 +30,9 @@
1830
batch_norm = paddle.nn.BatchNorm(32, act='relu', data_layout='NHWC')
1931
out = batch_norm(conv2d(tanh_out))
2032

21-
newir_program = ir.translate_to_new_ir(main_program.desc)
22-
2333
with LogWriter(logdir="./log/program_test/") as writer:
2434
writer.add_graph(
25-
model=newir_program,
35+
model=main_program,
2636
input_spec=[paddle.static.InputSpec([-1, 1, 28, 28], 'float32')],
2737
verbose=True,
2838
is_pir=True)

demo/components/while_test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (c) 2024 VisualDL Authors. All Rights Reserve.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# =======================================================================
15+
import paddle
16+
from visualdl import LogWriter
17+
18+
paddle.enable_static()
19+
main_program = paddle.static.Program()
20+
startup_program = paddle.static.Program()
21+
with paddle.static.program_guard(main_program, startup_program):
22+
linear = paddle.nn.Linear(16, 10)
23+
24+
def cond(i, loop_len, x, result):
25+
return i < loop_len
26+
27+
def body(i, loop_len, x, result):
28+
result = linear(x)
29+
paddle.increment(i)
30+
return [i, loop_len, x, result]
31+
32+
x = paddle.static.data(name='x', shape=[32, 16], dtype='float32')
33+
i = paddle.zeros(shape=[1], dtype='int64')
34+
loop_len = paddle.ones(shape=[1], dtype='int64')
35+
result = paddle.zeros(
36+
shape=x.shape[:-1] + linear.weight.shape[-1:], dtype="float32"
37+
)
38+
result.stop_gradient = False
39+
_, _, _, results = paddle.static.nn.while_loop(
40+
cond, body, [i, loop_len, x, result]
41+
)
42+
loss = paddle.mean(results)
43+
44+
with LogWriter(logdir="./log/while_test/") as writer:
45+
writer.add_graph(
46+
model=main_program,
47+
input_spec=[paddle.static.InputSpec([1], 'float32')],
48+
verbose=True,
49+
is_pir=True)

0 commit comments

Comments
 (0)