forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_netbuilder.py
More file actions
executable file
·123 lines (106 loc) · 4.19 KB
/
Copy pathtest_netbuilder.py
File metadata and controls
executable file
·123 lines (106 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
# Copyright (c) 2021 CINN 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 sys
import unittest
import numpy as np
import paddle
from paddle import static
from paddle.cinn.common import DefaultHostTarget, DefaultNVGPUTarget, Float
from paddle.cinn.frontend import NetBuilder
enable_gpu = sys.argv.pop()
class TestNetBuilder(unittest.TestCase):
def setUp(self):
if enable_gpu == "ON":
self.target = DefaultNVGPUTarget()
else:
self.target = DefaultHostTarget()
def paddle_verify_basic(self, result):
paddle.enable_static()
a = static.data(name='A', shape=[1, 24, 56, 56], dtype='float32')
b = static.data(name='B', shape=[1, 24, 56, 56], dtype='float32')
c = paddle.add(a, b)
d = paddle.nn.initializer.NumpyArrayInitializer(
np.array(result[2]).reshape((144, 24, 1, 1)).astype('float32')
)
res = paddle.nn.Conv2D(
in_channels=24,
out_channels=144,
kernel_size=1,
stride=1,
dilation=1,
padding=0,
weight_attr=d,
)(c)
exe = static.Executor(paddle.CPUPlace())
exe.run(static.default_startup_program())
x = np.array(result[0]).reshape((1, 24, 56, 56)).astype("float32")
y = np.array(result[1]).reshape((1, 24, 56, 56)).astype("float32")
output = exe.run(feed={"A": x, "B": y}, fetch_list=[res])
output = np.array(output).reshape(-1)
print("result in paddle_verify: \n")
for i in range(0, output.shape[0]):
if np.abs(output[i] - result[len(result) - 1][i]) > 1e-4:
print(
"Error! ",
i,
"-th data has diff with target data:\n",
output[i],
" vs: ",
result[len(result) - 1][i],
". Diff is: ",
output[i] - result[len(result) - 1][i],
)
np.testing.assert_allclose(result[len(result) - 1], output, atol=1e-4)
def test_basic(self):
builder = NetBuilder("test_basic")
a = builder.create_input(Float(32), (1, 24, 56, 56), "A")
b = builder.create_input(Float(32), (1, 24, 56, 56), "B")
c = builder.add(a, b)
d = builder.create_input(Float(32), (144, 24, 1, 1), "D")
e = builder.conv2d(c, d)
prog = builder.build()
self.assertEqual(prog.size(), 2)
# print program
for i in range(prog.size()):
print(prog[i])
tensor_data = [
np.random.random([1, 24, 56, 56]).astype("float32"),
np.random.random([1, 24, 56, 56]).astype("float32"),
np.random.random([144, 24, 1, 1]).astype("float32"),
]
result = prog.build_and_get_output(
self.target, [a, b, d], tensor_data, [e]
)
result = result[0].numpy(self.target).reshape(-1)
tensor_data.append(result)
self.paddle_verify_basic(tensor_data)
class TestNetBuilderOp(unittest.TestCase):
def setUp(self):
if enable_gpu == "ON":
self.target = DefaultNVGPUTarget()
else:
self.target = DefaultHostTarget()
def test_basic(self):
builder = NetBuilder("testmul")
a = builder.create_input(Float(32), (4, 4), "A")
tensor_data = [np.random.random([4, 4]).astype("float32")]
print(tensor_data[0])
b = builder.add(a, a)
prog = builder.build()
result = prog.build_and_get_output(self.target, [a], tensor_data, [b])
res = result[0].numpy(self.target)
print(res)
if __name__ == "__main__":
unittest.main()