/
test_graph_send_uv_op.py
261 lines (215 loc) · 10.1 KB
/
test_graph_send_uv_op.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# 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.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.framework import _test_eager_guard
from op_test import OpTest
def compute_graph_send_uv(inputs, attributes):
x = inputs['x']
y = inputs['y']
src_index = inputs['src_index']
dst_index = inputs['dst_index']
compute_type = attributes['compute_type']
gather_x = x[src_index]
gather_y = y[dst_index]
# Calculate forward output.
if compute_type == "ADD":
results = gather_x + gather_y
elif compute_type == "MUL":
results = gather_x * gather_y
return results
def graph_send_uv_wrapper(x, y, src_index, dst_index, compute_type="add"):
return paddle.geometric.send_uv(x, y, src_index, dst_index,
compute_type.lower())
class TestGraphSendUVOp(OpTest):
def setUp(self):
paddle.enable_static()
self.python_api = graph_send_uv_wrapper
self.python_out_sig = ['out']
self.op_type = "graph_send_uv"
self.set_config()
self.inputs = {
'x': self.x,
'y': self.y,
'src_index': self.src_index,
'dst_index': self.dst_index
}
self.attrs = {'compute_type': self.compute_type}
out = compute_graph_send_uv(self.inputs, self.attrs)
self.outputs = {'out': out}
def test_check_output(self):
self.check_output(check_eager=True)
def test_check_grad(self):
self.check_grad(['x', 'y'], 'out', check_eager=True)
def set_config(self):
self.x = np.random.random((10, 20)).astype("float64")
self.y = np.random.random((10, 20)).astype("float64")
index = np.random.randint(0, 10, (15, 2)).astype(np.int64)
self.src_index = index[:, 0]
self.dst_index = index[:, 1]
self.compute_type = 'ADD'
class TestCase1(TestGraphSendUVOp):
def set_config(self):
self.x = np.random.random((10, 20)).astype("float64")
self.y = np.random.random((10, 20)).astype("float64")
index = np.random.randint(0, 10, (15, 2)).astype(np.int64)
self.src_index = index[:, 0]
self.dst_index = index[:, 1]
self.compute_type = 'MUL'
class TestCase2(TestGraphSendUVOp):
def set_config(self):
self.x = np.random.random((100, 1)).astype("float64")
self.y = np.random.random((100, 20)).astype("float64")
index = np.random.randint(0, 100, (15, 2)).astype(np.int64)
self.src_index = index[:, 0]
self.dst_index = index[:, 1]
self.compute_type = 'ADD'
class TestCase3(TestGraphSendUVOp):
def set_config(self):
self.x = np.random.random((100, 20)).astype("float64")
self.y = np.random.random((100, 1)).astype("float64")
index = np.random.randint(0, 100, (15, 2)).astype(np.int64)
self.src_index = index[:, 0]
self.dst_index = index[:, 1]
self.compute_type = 'ADD'
class TestCase4(TestGraphSendUVOp):
def set_config(self):
self.x = np.random.random((100, 1)).astype("float64")
self.y = np.random.random((100, 20)).astype("float64")
index = np.random.randint(0, 100, (15, 2)).astype(np.int64)
self.src_index = index[:, 0]
self.dst_index = index[:, 1]
self.compute_type = 'MUL'
class TestCase5(TestGraphSendUVOp):
def set_config(self):
self.x = np.random.random((100, 20)).astype("float64")
self.y = np.random.random((100, 1)).astype("float64")
index = np.random.randint(0, 100, (15, 2)).astype(np.int64)
self.src_index = index[:, 0]
self.dst_index = index[:, 1]
self.compute_type = 'MUL'
class TestCase6(TestGraphSendUVOp):
def set_config(self):
self.x = np.random.random((10, 10, 1)).astype("float64")
self.y = np.random.random((10, 10, 10))
index = np.random.randint(0, 10, (15, 2)).astype(np.int64)
self.src_index = index[:, 0]
self.dst_index = index[:, 1]
self.compute_type = 'ADD'
class TestCase7(TestGraphSendUVOp):
def set_config(self):
self.x = np.random.random((10, 10, 1)).astype("float64")
self.y = np.random.random((10, 10, 10))
index = np.random.randint(0, 10, (15, 2)).astype(np.int64)
self.src_index = index[:, 0]
self.dst_index = index[:, 1]
self.compute_type = 'MUL'
class API_GeometricSendUVTest(unittest.TestCase):
def test_compute_all_dygraph(self):
paddle.disable_static()
x = paddle.to_tensor([[0, 2, 3], [1, 4, 5], [2, 6, 7]], dtype="float32")
y = paddle.to_tensor([[1, 1, 2], [2, 3, 4], [4, 5, 6]], dtype="float32")
src_index = paddle.to_tensor(np.array([0, 1, 2, 0]), dtype="int32")
dst_index = paddle.to_tensor(np.array([1, 2, 1, 0]), dtype="int32")
res_add = paddle.geometric.send_uv(x,
y,
src_index,
dst_index,
compute_type="add")
res_sub = paddle.geometric.send_uv(x,
y,
src_index,
dst_index,
compute_type="sub")
res_mul = paddle.geometric.send_uv(x,
y,
src_index,
dst_index,
compute_type="mul")
res_div = paddle.geometric.send_uv(x,
y,
src_index,
dst_index,
compute_type="div")
res = [res_add, res_sub, res_mul, res_div]
np_add = np.array([[2, 5, 7], [5, 9, 11], [4, 9, 11], [1, 3, 5]],
dtype="float32")
np_sub = np.array([[-2, -1, -1], [-3, -1, -1], [0, 3, 3], [-1, 1, 1]],
dtype="float32")
np_mul = np.array([[0, 6, 12], [4, 20, 30], [4, 18, 28], [0, 2, 6]],
dtype="float32")
np_div = np.array(
[[0, 2 / 3, 0.75], [0.25, 0.8, 5 / 6], [1, 2, 7 / 4], [0, 2, 1.5]],
dtype="float32")
for np_res, paddle_res in zip([np_add, np_sub, np_mul, np_div], res):
self.assertTrue(
np.allclose(np_res, paddle_res, atol=1e-6), "two value is\
{}\n{}, check diff!".format(np_res, paddle_res))
def test_compute_all_static(self):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data(name="x", shape=[3, 3], dtype="float32")
y = paddle.static.data(name="y", shape=[3, 3], dtype="float32")
src_index = paddle.static.data(name="src", shape=[4], dtype="int32")
dst_index = paddle.static.data(name="dst", shape=[4], dtype="int32")
res_add = paddle.geometric.send_uv(x,
y,
src_index,
dst_index,
compute_type="add")
res_sub = paddle.geometric.send_uv(x,
y,
src_index,
dst_index,
compute_type="sub")
res_mul = paddle.geometric.send_uv(x,
y,
src_index,
dst_index,
compute_type="mul")
res_div = paddle.geometric.send_uv(x,
y,
src_index,
dst_index,
compute_type="div")
exe = paddle.static.Executor(paddle.CPUPlace())
data1 = np.array([[0, 2, 3], [1, 4, 5], [2, 6, 7]], dtype="float32")
data2 = np.array([[1, 1, 2], [2, 3, 4], [4, 5, 6]], dtype="float32")
data3 = np.array([0, 1, 2, 0], dtype="int32")
data4 = np.array([1, 2, 1, 0], dtype="int32")
np_add = np.array([[2, 5, 7], [5, 9, 11], [4, 9, 11], [1, 3, 5]],
dtype="float32")
np_sub = np.array(
[[-2, -1, -1], [-3, -1, -1], [0, 3, 3], [-1, 1, 1]],
dtype="float32")
np_mul = np.array([[0, 6, 12], [4, 20, 30], [4, 18, 28], [0, 2, 6]],
dtype="float32")
np_div = np.array([[0, 2 / 3, 0.75], [0.25, 0.8, 5 / 6],
[1, 2, 7 / 4], [0, 2, 1.5]],
dtype="float32")
ret = exe.run(feed={
'x': data1,
'y': data2,
'src': data3,
'dst': data4,
},
fetch_list=[res_add, res_sub, res_mul, res_div])
for np_res, paddle_res in zip([np_add, np_sub, np_mul, np_div],
ret):
self.assertTrue(
np.allclose(np_res, paddle_res, atol=1e-6), "two value is\
{}\n{}, check diff!".format(np_res, paddle_res))