forked from Charles-hit/PaddleApiTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.py
663 lines (630 loc) · 24 KB
/
api_test.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
import gc
import random
import unittest
from typing import Sequence
import numpy as np
import torch
from utils import (
TOLERANCE,
convert_dtype_to_torch_type,
np_assert_accuracy,
np_assert_staility,
)
import paddle
from paddle.utils import map_structure
def _as_list(x):
if x is None:
return []
return list(x) if isinstance(x, Sequence) else [x]
def flatten(nest_list: Sequence):
out = []
for item in nest_list:
if isinstance(item, Sequence):
tmp_list = flatten(item)
out += tmp_list
else:
out.append(item)
return out
class ApiTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
np.random.seed(2023)
random.seed(2023)
paddle.seed(2023)
torch.manual_seed(2023)
torch.cuda.manual_seed_all(2023)
@classmethod
def tearDownClass(cls):
pass
def check_inputs_and_out_grads(self):
if not hasattr(self, "inputs"):
raise TypeError(
"Please set self.inputs to a list of numpy arrays in setUp function."
)
if not isinstance(self.inputs, list):
raise TypeError(
"Please must set self.inputs to a list of numpy arrays in setUp function."
)
if hasattr(self, "out_grads") and not isinstance(self.out_grads, list):
raise TypeError(
"You must set self.out_grads to a list of numpy arrays in setUp function."
)
def check_dtype(self):
if not hasattr(self, "dtype"):
raise TypeError("You must set self.dtype in setUp function.")
expected_dtype = ["float32", "float16", "bfloat16"]
if self.dtype not in expected_dtype:
raise TypeError(
f"The data type must be {expected_dtype}, but received {self.dtype}."
)
def gen_eager_data(self, np_xs, dtype):
eager_xs = []
if isinstance(np_xs, Sequence):
for np_x in np_xs:
if isinstance(np_x, Sequence):
eager_xs.append(self.gen_eager_data(np_x, dtype))
else:
if dtype == "bfloat16" and np_x.dtype == np.float32:
x = paddle.to_tensor(
np_x,
dtype="float32",
place="gpu",
stop_gradient=False,
)
x = paddle.cast(x, dtype="uint16")
else:
x = paddle.to_tensor(
np_x,
dtype=np_x.dtype,
place="gpu",
stop_gradient=False,
)
eager_xs.append(x)
else:
if dtype == "bfloat16" and np_xs.dtype == np.float32:
x = paddle.to_tensor(
np_xs, dtype="float32", place="gpu", stop_gradient=False
)
x = paddle.cast(x, dtype=dtype)
else:
x = paddle.to_tensor(
np_xs, dtype=np_xs.dtype, place="gpu", stop_gradient=False
)
eager_xs.append(x)
return eager_xs
def gen_torch_data(self, np_xs, dtype):
eager_xs = []
if isinstance(np_xs, Sequence):
for np_x in np_xs:
if isinstance(np_x, Sequence):
eager_xs.append(self.gen_eager_data(np_x, dtype))
else:
if dtype == "bfloat16" and np_x.dtype == np.float32:
x = torch.tensor(
np_x,
device='cuda',
dtype=torch.float32,
requires_grad=True,
)
x = x.to(dtype=torch.bfloat16)
else:
x = torch.tensor(
np_x,
device='cuda',
dtype=convert_dtype_to_torch_type(np_x.dtype),
requires_grad=True,
)
eager_xs.append(x)
else:
if dtype == "bfloat16" and np_xs.dtype == np.float32:
x = torch.tensor(
np_xs,
device='cuda',
dtype=torch.float32,
requires_grad=True,
)
x = x.to(dtype=torch.bfloat16)
else:
x = torch.tensor(
np_xs,
device='cuda',
dtype=convert_dtype_to_torch_type(np_xs.dtype),
requires_grad=True,
)
eager_xs.append(x)
return eager_xs
def gen_static_data_and_feed(self, np_xs, dtype, base_name):
feed = {}
static_xs = []
if isinstance(np_xs, Sequence):
for i, x in enumerate(np_xs):
if isinstance(x, Sequence):
xs_sub, feed_sub = self.gen_static_data_and_feed(
x, f"{base_name}_{i}"
)
static_xs.append(xs_sub)
feed.update(feed_sub)
else:
if dtype == "bfloat16" and x.dtype == np.float32:
data = paddle.static.data(
f"{base_name}_{i}", x.shape, "float32"
)
data = paddle.cast(data, dtype="uint16")
else:
data = paddle.static.data(
f"{base_name}_{i}", x.shape, x.dtype
)
data.stop_gradient = False
static_xs.append(data)
feed.update({f"{base_name}_{i}": x})
else:
if dtype == "bfloat16" and x.dtype == np.float32:
data = paddle.static.data(
f"{base_name}_{i}", np_xs.shape, "float32"
)
data = paddle.cast(data, dtype="uint16")
else:
data = paddle.static.data(
f"{base_name}_{i}", np_xs.shape, np_xs.dtype
)
data.stop_gradient = False
static_xs.append(data)
feed.update({f"{base_name}_{i}": np_xs})
return static_xs, feed
def check_custom_config(self):
self.check_inputs_and_out_grads()
self.check_dtype()
def get_default_threshold(self):
return TOLERANCE[self.dtype]
def cal_torch_res(self, inputs, out_grads=None):
raise NotImplementedError(
"You must implement cal_torch_res function in your test case."
)
def cal_paddle_res(self, inputs, out_grads=None):
raise NotImplementedError(
"You must implement cal_paddle_res function in your test case."
)
def check_eager_res(self, atol=None, rtol=None):
self.check_custom_config()
default_threshold_mp = self.get_default_threshold()
atol = atol if atol else default_threshold_mp["atol"]
rtol = rtol if rtol else default_threshold_mp["rtol"]
torch_inputs = self.gen_torch_data(self.inputs, dtype=self.dtype)
torch_douts = (
self.gen_torch_data(self.out_grads, dtype=self.dtype)
if hasattr(self, "out_grads")
else None
)
torch_outputs, torch_gradouts = self.cal_torch_res(
torch_inputs, torch_douts
)
if torch_gradouts is None:
torch_gradouts = []
torch_outputs, torch_gradouts = flatten(
_as_list(torch_outputs)
), flatten(_as_list(torch_gradouts))
if self.dtype == "bfloat16":
torch_outputs = (
map_structure(lambda x: x.to(torch.float32), torch_outputs)
if len(torch_outputs) > 0
else torch_outputs
)
torch_gradouts = (
map_structure(lambda x: x.to(torch.float32), torch_gradouts)
if len(torch_gradouts) > 0
else torch_gradouts
)
torch_outputs_np = map_structure(
lambda x: x.cpu().detach().numpy(),
torch_outputs,
)
torch_gradouts_np = map_structure(
lambda x: x.cpu().detach().numpy(),
torch_gradouts,
)
del torch_inputs
del torch_douts
del torch_outputs
del torch_gradouts
gc.collect()
torch.cuda.empty_cache()
pd_inputs = self.gen_eager_data(self.inputs, dtype=self.dtype)
pd_douts = (
self.gen_eager_data(self.out_grads, dtype=self.dtype)
if hasattr(self, "out_grads")
else None
)
pd_outputs, pd_gradouts = self.cal_paddle_res(pd_inputs, pd_douts)
if pd_douts is None:
pd_gradouts = []
pd_outputs, pd_gradouts = flatten(_as_list(pd_outputs)), flatten(
_as_list(pd_gradouts)
)
if self.dtype == "bfloat16":
pd_outputs = (
map_structure(lambda x: paddle.cast(x, "float32"), pd_outputs)
if len(pd_outputs) > 0
else pd_outputs
)
pd_gradouts = (
map_structure(lambda x: paddle.cast(x, "float32"), pd_gradouts)
if len(pd_gradouts) > 0
else pd_gradouts
)
pd_outputs_np = map_structure(
lambda x: x.numpy(),
pd_outputs,
)
pd_gradouts_np = map_structure(
lambda x: x.numpy(),
pd_gradouts,
)
del pd_inputs
del pd_douts
del pd_outputs
del pd_gradouts
gc.collect()
paddle.device.cuda.empty_cache()
np.testing.assert_equal(
len(pd_outputs_np),
len(torch_outputs_np),
err_msg=(
'Mismatch between paddle and torch forward output tensor nums.'
'paddle output tensor num: {}, torch output tensor num: {}.\n'.format(str(len(pd_outputs_np)), str(len(torch_outputs_np)))
),
)
for idx in range(len(torch_outputs_np)):
np_assert_accuracy(
pd_outputs_np[idx],
torch_outputs_np[idx],
atol,
rtol,
self.dtype,
version_a="paddle",
version_b="torch",
eager_or_static_mode="eager",
fwd_or_bkd="forward",
api="",
)
np.testing.assert_equal(
len(pd_gradouts_np),
len(torch_gradouts_np),
err_msg=(
'Mismatch between paddle and torch grad output tensor nums in eager mode.'
'paddle grad output tensor num: {}, torch grad output tensor num: {}.\n'.format(str(len(pd_gradouts_np)), str(len(torch_gradouts_np)))
),
)
for idx in range(len(torch_gradouts_np)):
np_assert_accuracy(
pd_gradouts_np[idx],
torch_gradouts_np[idx],
atol,
rtol,
self.dtype,
version_a="paddle",
version_b="torch",
eager_or_static_mode="eager",
fwd_or_bkd="grad",
api="",
)
def check_static_res(self, atol=None, rtol=None):
self.check_custom_config()
default_threshold_mp = self.get_default_threshold()
atol = atol if atol else default_threshold_mp["atol"]
rtol = rtol if rtol else default_threshold_mp["rtol"]
torch_inputs = self.gen_torch_data(self.inputs, dtype=self.dtype)
torch_douts = (
self.gen_torch_data(self.out_grads, dtype=self.dtype)
if hasattr(self, "out_grads")
else None
)
torch_outputs, torch_gradouts = self.cal_torch_res(
torch_inputs, torch_douts
)
if torch_gradouts is None:
torch_gradouts = []
torch_outputs, torch_gradouts = flatten(
_as_list(torch_outputs)
), flatten(_as_list(torch_gradouts))
if self.dtype == "bfloat16":
torch_outputs = (
map_structure(lambda x: x.to(torch.float32), torch_outputs)
if len(torch_outputs) > 0
else torch_outputs
)
torch_gradouts = (
map_structure(lambda x: x.to(torch.float32), torch_gradouts)
if len(torch_gradouts) > 0
else torch_gradouts
)
torch_outputs_np = map_structure(
lambda x: x.cpu().detach().numpy(),
torch_outputs,
)
torch_gradouts_np = map_structure(
lambda x: x.cpu().detach().numpy(),
torch_gradouts,
)
del torch_inputs
del torch_douts
del torch_outputs
del torch_gradouts
gc.collect()
torch.cuda.empty_cache()
with paddle.fluid.framework._dygraph_guard(None):
mp, sp = paddle.static.Program(), paddle.static.Program()
with paddle.static.program_guard(mp, sp):
static_inputs, inputs_feed = self.gen_static_data_and_feed(
self.inputs, dtype=self.dtype, base_name="x"
)
(static_douts, douts_feed) = (
self.gen_static_data_and_feed(
self.out_grads, dtype=self.dtype, base_name="dout"
)
if hasattr(self, "out_grads")
else (None, {})
)
pd_outputs, pd_gradouts = self.cal_paddle_res(
static_inputs, static_douts
)
if pd_gradouts is None:
pd_gradouts = []
pd_outputs, pd_gradouts = flatten(
_as_list(pd_outputs)
), flatten(_as_list(pd_gradouts))
if self.dtype == "bfloat16":
pd_outputs = (
map_structure(
lambda x: paddle.cast(x, "float32"), pd_outputs
)
if len(pd_outputs) > 0
else pd_outputs
)
pd_gradouts = (
map_structure(
lambda x: paddle.cast(x, "float32"), pd_gradouts
)
if len(pd_gradouts) > 0
else pd_gradouts
)
feed = {**inputs_feed, **douts_feed}
exe = paddle.static.Executor(place=paddle.CUDAPlace(0))
exe.run(sp)
out = exe.run(
mp,
feed=feed,
fetch_list=pd_outputs + pd_gradouts,
)
if len(pd_gradouts) > 0:
out_static, out_grads_static = (
out[0 : len(pd_outputs)],
out[len(pd_outputs) :],
)
else:
out_static, out_grads_static = out, []
np.testing.assert_equal(
len(out_static),
len(torch_outputs_np),
err_msg=(
'Mismatch between paddle static and torch forward output tensor nums.'
'paddle static mode output tensor num: {}, torch output tensor num: {}.\n'.format(str(len(out_static)), str(len(torch_outputs_np)))
),
)
for idx in range(len(torch_outputs_np)):
np_assert_accuracy(
out_static[idx],
torch_outputs_np[idx],
atol,
rtol,
self.dtype,
version_a="paddle",
version_b="torch",
eager_or_static_mode="static",
fwd_or_bkd="forward",
api="",
)
np.testing.assert_equal(
len(out_grads_static),
len(torch_gradouts_np),
err_msg=(
'Mismatch between paddle and torch grad output tensor nums in staitc mode.'
'paddle grad output tensor num: {}, torch grad output tensor num: {}.\n'.format(str(len(out_grads_static)), str(len(torch_gradouts_np)))
),
)
for idx in range(len(torch_gradouts_np)):
np_assert_accuracy(
out_grads_static[idx],
torch_gradouts_np[idx],
atol,
rtol,
self.dtype,
version_a="paddle",
version_b="torch",
eager_or_static_mode="static",
fwd_or_bkd="grad",
api="",
)
def check_eager_stability(self, frequency=5):
self.check_custom_config()
x_eager = self.gen_eager_data(self.inputs, dtype=self.dtype)
dout_eager = (
self.gen_eager_data(self.out_grads, dtype=self.dtype)
if hasattr(self, "out_grads")
else None
)
out_eager_baseline, out_grads_eager_baseline = self.cal_paddle_res(
x_eager, dout_eager
)
if out_grads_eager_baseline is None:
out_grads_eager_baseline = []
out_eager_baseline, out_grads_eager_baseline = flatten(
_as_list(out_eager_baseline)
), flatten(_as_list(out_grads_eager_baseline))
if self.dtype == "bfloat16":
out_eager_baseline = (
map_structure(
lambda x: paddle.cast(x, "float32"), out_eager_baseline
)
if len(out_eager_baseline) > 0
else out_eager_baseline
)
out_grads_eager_baseline = (
map_structure(
lambda x: paddle.cast(x, "float32"),
out_grads_eager_baseline,
)
if len(out_grads_eager_baseline) > 0
else out_grads_eager_baseline
)
out_eager_baseline_np = map_structure(
lambda x: x.numpy(),
out_eager_baseline,
)
out_grads_eager_baseline_np = map_structure(
lambda x: x.numpy(),
out_grads_eager_baseline,
)
del out_eager_baseline
del out_grads_eager_baseline
gc.collect()
paddle.device.cuda.empty_cache()
for i in range(frequency):
out_eager, out_grads_eager = self.cal_paddle_res(
x_eager, dout_eager
)
if out_grads_eager is None:
out_grads_eager = []
out_eager, out_grads_eager = flatten(_as_list(out_eager)), flatten(
_as_list(out_grads_eager)
)
if self.dtype == "bfloat16":
out_eager = (
map_structure(
lambda x: paddle.cast(x, "float32"), out_eager
)
if len(out_eager) > 0
else out_eager
)
out_grads_eager = (
map_structure(
lambda x: paddle.cast(x, "float32"), out_grads_eager
)
if len(out_grads_eager) > 0
else out_grads_eager
)
out_eager_np = map_structure(
lambda x: x.numpy(),
out_eager,
)
out_grads_eager_np = map_structure(
lambda x: x.numpy(),
out_grads_eager,
)
for idx in range(len(out_eager_baseline_np)):
np_assert_staility(
out_eager_np[idx],
out_eager_baseline_np[idx],
self.dtype,
version="paddle_develop",
eager_or_static_mode="eager",
fwd_or_bkd="forward",
api="",
)
for idx in range(len(out_grads_eager_baseline_np)):
np_assert_staility(
out_grads_eager_np[idx],
out_grads_eager_baseline_np[idx],
self.dtype,
version="paddle_develop",
eager_or_static_mode="eager",
fwd_or_bkd="backward",
api="",
)
def check_static_stability(self, frequency=5):
self.check_custom_config()
with paddle.fluid.framework._dygraph_guard(None):
mp, sp = paddle.static.Program(), paddle.static.Program()
with paddle.static.program_guard(mp, sp):
static_inputs, inputs_feed = self.gen_static_data_and_feed(
self.inputs, dtype=self.dtype, base_name="x"
)
static_douts, douts_feed = (
self.gen_static_data_and_feed(
self.out_grads, dtype=self.dtype, base_name="dout"
)
if hasattr(self, "out_grads")
else (None, {})
)
(pd_outputs, pd_gradouts) = self.cal_paddle_res(
static_inputs, static_douts
)
if pd_gradouts is None:
pd_gradouts = []
pd_outputs, pd_gradouts = flatten(
_as_list(pd_outputs)
), flatten(_as_list(pd_gradouts))
if self.dtype == "bfloat16":
pd_outputs = (
map_structure(
lambda x: paddle.cast(x, "float32"), pd_outputs
)
if len(pd_outputs) > 0
else pd_outputs
)
pd_gradouts = (
map_structure(
lambda x: paddle.cast(x, "float32"), pd_gradouts
)
if len(pd_gradouts) > 0
else pd_gradouts
)
feed = {**inputs_feed, **douts_feed}
exe = paddle.static.Executor(place=paddle.CUDAPlace(0))
exe.run(sp)
out = exe.run(
mp,
feed=feed,
fetch_list=pd_outputs + pd_gradouts,
)
if len(pd_gradouts) > 0:
out_static_baseline, out_grads_static_baseline = (
out[0 : len(pd_outputs)],
out[len(pd_outputs) :],
)
else:
out_static_baseline, out_grads_static_baseline = out, []
for i in range(frequency):
out = exe.run(
mp,
feed=feed,
fetch_list=pd_outputs + pd_gradouts,
)
if len(pd_gradouts) > 0:
out_static, out_grads_static = (
out[0 : len(pd_outputs)],
out[len(pd_outputs) :],
)
else:
out_static, out_grads_static = out, []
# test develop static forward stability
for idx in range(len(out_static_baseline)):
np_assert_staility(
out_static[idx],
out_static_baseline[idx],
self.dtype,
version="paddle_develop",
eager_or_static_mode="static",
fwd_or_bkd="forward",
api="",
)
# test develop static backward stability
for idx in range(len(out_grads_static_baseline)):
np_assert_staility(
out_grads_static[idx],
out_grads_static_baseline[idx],
self.dtype,
version="paddle_develop",
eager_or_static_mode="static",
fwd_or_bkd="backward",
api="",
)