forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_legacy_nn.py
1319 lines (1187 loc) · 51.1 KB
/
test_legacy_nn.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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
import random
import unittest
import collections
from copy import deepcopy
import torch
import torch.legacy.nn as nn
from common import to_gpu, freeze_rng_state, run_tests
from common_nn import NNTestCase, ModuleTest, CriterionTest, iter_tensors, \
module_tests, criterion_tests, PRECISION
from torch.autograd.gradcheck import get_numerical_jacobian
from torch.autograd import Variable
class OldModuleTest(ModuleTest):
def __init__(self, *args, **kwargs):
super(OldModuleTest, self).__init__(*args, **kwargs)
self.check_inplace = kwargs.get('check_inplace', False)
# Never check gradgrad for legacy NN
self.check_gradgrad = False
def _do_test(self, test_case, module, input):
# TODO: check update parameters
# TODO: test IO
module.training()
with torch.no_grad():
test_case.check_jacobian(module, input, self.jacobian_input)
module.evaluate()
with torch.no_grad():
test_case.check_jacobian(module, input, self.jacobian_input)
# Test .type()
module.float().double().forward(input)
# Test .clearState()
module.clearState()
# test if module can be printed
module.__repr__()
if self.check_inplace:
input2 = deepcopy(input)
module_ip = self.constructor(*self.constructor_args, inplace=True)
with freeze_rng_state():
output = module.forward(input)
test_case.assertEqual(input, input2)
with freeze_rng_state():
output2 = module_ip.forward(input2)
if not torch.equal(output, input):
test_case.assertNotEqual(input, input2)
test_case.assertEqual(output, input2)
# TODO: hessian tests
tests = [
OldModuleTest(nn.Add,
constructor_args=(torch.Size([5, 4]),),
input_size=(3, 5, 4),
desc='3D'),
OldModuleTest(nn.Add,
constructor_args=(1, True),
input_size=(3, 1, 4),
desc='scalar'),
OldModuleTest(nn.AddConstant,
constructor_args=(3.5,),
input_size=(3, 5, 4),
reference_fn=lambda i, _: i + 3.5,
check_inplace=True),
OldModuleTest(nn.BatchNormalization,
constructor_args=(10,),
input_size=(4, 10),
desc='affine'),
OldModuleTest(nn.BatchNormalization,
constructor_args=(10, 1e-3, 0.3, False),
input_size=(4, 10),
desc='not_affine'),
OldModuleTest(nn.SpatialBatchNormalization,
constructor_args=(3,),
input_size=(2, 3, 6, 6)),
OldModuleTest(nn.SpatialBatchNormalization,
constructor_args=(3, 1e-3, 0.8),
input_size=(2, 3, 6, 6),
desc='momentum'),
OldModuleTest(nn.SpatialBatchNormalization,
constructor_args=(3, 1e-3, 0.8, False),
input_size=(2, 3, 6, 6),
desc='no_affine'),
OldModuleTest(nn.VolumetricBatchNormalization,
constructor_args=(3,),
input_size=(2, 3, 4, 4, 4)),
OldModuleTest(nn.VolumetricBatchNormalization,
constructor_args=(3, 1e-3, 0.7),
input_size=(2, 3, 4, 4, 4),
desc='momentum'),
OldModuleTest(nn.VolumetricBatchNormalization,
constructor_args=(3, 1e-3, 0.7, False),
input_size=(2, 3, 4, 4, 4),
desc='no_affine'),
OldModuleTest(nn.CMul,
constructor_args=(5, 6),
input_size=(10, 5, 6),
desc='3D'),
OldModuleTest(nn.CMul,
constructor_args=(50, 4),
input_size=(1, 50, 4),
desc='3D_single_example'),
OldModuleTest(nn.CMul,
constructor_args=(1, 5),
input_fn=lambda: torch.randn(10, 3, 5)[:, 1],
desc='3D_noncontiguous'),
OldModuleTest(nn.Exp,
input_size=(2, 3, 4),
reference_fn=lambda i, _: i.exp()),
OldModuleTest(nn.Log,
input_fn=lambda: torch.rand(2, 3, 2) + 0.1,
reference_fn=lambda i, _: i.log()),
OldModuleTest(nn.Clamp,
constructor_args=(-2., 5.),
input_fn=lambda: torch.randn(3, 2, 50) * 6,
reference_fn=lambda i, _: i.clamp(-2, 5)),
OldModuleTest(nn.Abs,
input_size=(3, 20, 5),
reference_fn=lambda i, _: i.abs()),
OldModuleTest(nn.Bilinear,
constructor_args=(2, 3, 10),
input_size=[(4, 2), (4, 3)]),
OldModuleTest(nn.Bilinear,
constructor_args=(5, 4, 2),
input_size=[(2, 5), (2, 4)],
desc='small_output'),
OldModuleTest(nn.Euclidean,
constructor_args=(5, 7),
input_size=(10, 5)),
OldModuleTest(nn.WeightedEuclidean,
constructor_args=(5, 7),
input_size=(10, 5)),
OldModuleTest(nn.Cosine,
constructor_args=(5, 7),
input_size=(10, 5)),
OldModuleTest(nn.CAddTable,
input_size=[(5, 7), (5, 7)]),
OldModuleTest(nn.CSubTable,
input_size=[(5, 7), (5, 7)]),
OldModuleTest(nn.CDivTable,
input_fn=lambda: [torch.randn(1, 7), torch.rand(1, 7) + 0.1]),
OldModuleTest(nn.CMulTable,
input_size=[(5, 7), (5, 7)]),
OldModuleTest(nn.Square,
input_size=(10, 2, 4),
reference_fn=lambda i, _: i.mul(i)),
OldModuleTest(nn.Sqrt,
input_fn=lambda: torch.rand(10, 2, 4) + 0.01,
reference_fn=lambda i, _: i.sqrt()),
OldModuleTest(nn.Squeeze,
input_size=(2, 1, 1, 4, 5),
reference_fn=lambda i, _: i.squeeze()),
OldModuleTest(nn.Squeeze,
constructor_args=(1,),
input_size=(2, 1, 1, 4, 5),
reference_fn=lambda i, _: i.squeeze(1),
desc='dim'),
OldModuleTest(nn.Unsqueeze,
constructor_args=(1,),
input_size=(2, 4, 5),
reference_fn=lambda i, _: i.view(2, 1, 4, 5)),
OldModuleTest(nn.Unsqueeze,
constructor_args=(0,),
input_size=(2, 4, 5),
reference_fn=lambda i, _: i.view(1, 2, 4, 5),
desc='fist_dim'),
OldModuleTest(nn.Unsqueeze,
constructor_args=(3,),
input_size=(2, 4, 5),
reference_fn=lambda i, _: i.view(2, 4, 5, 1),
desc='last_dim'),
OldModuleTest(nn.View,
constructor_args=(-1, 2, 20),
input_size=(2, 2, 4, 5),
reference_fn=lambda i, _: i.view(-1, 2, 20),
desc='infer_batch'),
OldModuleTest(nn.View,
constructor_args=(2, 2, 2, 5),
input_size=(2, 4, 5),
reference_fn=lambda i, _: i.view(2, 2, 2, 5),
desc='split_dim'),
OldModuleTest(nn.View,
constructor_args=(2, -1, 2, 5),
input_size=(2, 4, 5),
reference_fn=lambda i, _: i.view(2, -1, 2, 5),
desc='infer_middle'),
OldModuleTest(nn.Sum,
constructor_args=(1,),
input_size=(2, 4, 5),
reference_fn=lambda i, _: i.sum(1, keepdim=False)),
OldModuleTest(nn.Sum,
constructor_args=(1, True),
input_size=(2, 4, 5),
reference_fn=lambda i, _: i.sum(1, keepdim=False).div(i.size(1)),
desc='sizeAverage'),
OldModuleTest(nn.Mean,
constructor_args=(1,),
input_size=(2, 4, 5),
reference_fn=lambda i, _: torch.mean(i, 1, keepdim=False)),
OldModuleTest(lambda: nn.Sequential().add(nn.GradientReversal()).add(nn.GradientReversal()),
input_size=(4, 3, 2, 2),
fullname='GradientReversal'),
OldModuleTest(nn.Identity,
input_size=(4, 3, 2, 4),
reference_fn=lambda i, _: i),
OldModuleTest(nn.DotProduct,
input_size=[(10, 4), (10, 4)],
reference_fn=lambda i, _: torch.Tensor(list(
a.dot(b) for a, b in zip(i[0], i[1])))
),
OldModuleTest(nn.CosineDistance,
input_size=[(10, 4), (10, 4)],
reference_fn=lambda i, _: torch.Tensor(list(
a.dot(b) / (a.norm(2) * b.norm(2)) for a, b in zip(i[0], i[1])))
),
OldModuleTest(nn.JoinTable,
constructor_args=(0,),
input_size=[(10, 4), (10, 4)],
reference_fn=lambda i, _: torch.cat(i, 0),
desc='first_dim'),
OldModuleTest(nn.JoinTable,
constructor_args=(2,),
input_size=[(2, 4, 2), (2, 4, 2)],
reference_fn=lambda i, _: torch.cat(i, 2),
desc='positive_dim_index'),
OldModuleTest(nn.JoinTable,
constructor_args=(-1,),
input_size=[(2, 4, 2, 4), (2, 4, 2, 4)],
reference_fn=lambda i, _: torch.cat(i, 3),
desc='negative_dim_index'),
OldModuleTest(nn.MM,
input_size=[(4, 5, 3), (4, 3, 2)],
reference_fn=lambda i, _: torch.bmm(*i)),
OldModuleTest(nn.MV,
input_size=[(4, 5, 3), (4, 3)],
reference_fn=lambda i, _: torch.bmm(i[0], i[1].view(i[1].size(0), i[1].size(1), 1)).squeeze()),
OldModuleTest(nn.Max,
input_size=(4, 5, 3),
reference_fn=lambda i, _: torch.max(i, 0, False)[0]),
OldModuleTest(nn.Max,
constructor_args=(1,),
input_size=(4, 5, 3),
reference_fn=lambda i, _: torch.max(i, 1, False)[0],
desc='with_dimension'),
OldModuleTest(nn.Min,
input_size=(4, 5, 3),
reference_fn=lambda i, _: torch.min(i, 0, False)[0]),
OldModuleTest(nn.Min,
constructor_args=(1,),
input_size=(4, 5, 3),
reference_fn=lambda i, _: torch.min(i, 1, False)[0],
desc='with_dimension'),
OldModuleTest(nn.MixtureTable,
input_size=[(5, 3), (5, 3, 6)]),
OldModuleTest(nn.LookupTable,
constructor_args=(4, 3),
input_fn=lambda: torch.randperm(2).repeat(1, 2),
jacobian_input=False),
OldModuleTest(nn.Mul,
input_size=(2, 3, 4, 2),
reference_fn=lambda i, p: i * p[0][0]),
OldModuleTest(nn.MulConstant,
constructor_args=(4,),
input_size=(2, 3, 4, 2),
reference_fn=lambda i, _: i * 4,
check_inplace=True),
OldModuleTest(nn.Narrow,
constructor_args=(0, 0),
input_size=(2, 3, 4, 2),
reference_fn=lambda i, _: i.narrow(0, 0, 1)),
OldModuleTest(nn.Narrow,
constructor_args=(1, 1, 2),
input_size=(2, 3, 4, 2),
reference_fn=lambda i, _: i.narrow(1, 1, 2),
desc='length'),
OldModuleTest(nn.Transpose,
constructor_args=((1, 2), (1, 3)),
input_size=(2, 3, 4, 5),
reference_fn=lambda i, _: i.transpose(1, 2).transpose(1, 3)),
OldModuleTest(nn.Transpose,
constructor_args=((1, 2),),
input_size=(2, 3, 4, 5),
reference_fn=lambda i, _: i.transpose(1, 2),
desc='single_arg'),
# TODO: this seems to be very slow
OldModuleTest(nn.Replicate,
constructor_args=(2, 1),
input_size=(10, 3, 4, 5),
reference_fn=lambda i, _: i.view(10, 1, 3, 4, 5).expand(10, 2, 3, 4, 5)),
OldModuleTest(nn.Padding,
constructor_args=(0, 2, -10),
input_size=(2, 3, 4, 5)),
OldModuleTest(nn.Padding,
constructor_args=(0, 2, -10, 1),
input_size=(2, 3, 4, 5),
desc='index'),
OldModuleTest(nn.Padding,
constructor_args=(0, -2, -10, 1),
input_size=(2, 3, 4, 5),
desc='negative_pad'),
OldModuleTest(nn.PartialLinear,
constructor_args=(5, 6),
input_size=(4, 5)),
OldModuleTest(lambda: nn.PartialLinear(5, 6).setPartition(torch.Tensor((2, 4))),
input_size=(4, 5),
fullname='PartialLinear_setPartition'),
OldModuleTest(nn.Power,
constructor_args=(2,),
input_size=(2, 3, 4, 5)),
OldModuleTest(nn.Power,
constructor_args=(1.5,),
input_fn=lambda: torch.rand(3, 4, 5),
desc='fractional'),
OldModuleTest(nn.Reshape,
constructor_args=(4, 5),
input_size=(3, 4 * 5),
desc='add_dim'),
OldModuleTest(nn.Reshape,
constructor_args=(4 * 5,),
input_size=(3, 4, 5),
desc='squash_dim'),
OldModuleTest(nn.Select,
constructor_args=(1, 2),
input_size=(3, 4, 5),
reference_fn=lambda i, _: i.select(1, 2)),
OldModuleTest(nn.SelectTable,
constructor_args=(1,),
input_size=[(1,), (2,), (3,), (4,)],
reference_fn=lambda i, _: i[1]),
OldModuleTest(nn.SpatialAveragePooling,
constructor_args=(2, 2),
input_size=(2, 3, 6, 6)),
OldModuleTest(nn.SpatialAveragePooling,
constructor_args=(2, 2, 2, 2),
input_size=(2, 3, 6, 6),
desc='stride'),
OldModuleTest(nn.SpatialAveragePooling,
constructor_args=(2, 2, 2, 2, 1, 1),
input_size=(2, 3, 6, 6),
desc='stride_pad'),
OldModuleTest(nn.SpatialAdaptiveMaxPooling,
constructor_args=(4, 4),
input_size=(2, 3, 8, 8),
reference_fn=lambda i, _: nn.SpatialMaxPooling(2, 2).forward(i)),
OldModuleTest(nn.SpatialAdaptiveMaxPooling,
constructor_args=(4, 4),
input_size=(2, 3, 7, 11),
desc='irregular'),
OldModuleTest(nn.SpatialConvolution,
constructor_args=(3, 4, 3, 3),
input_size=(2, 3, 6, 6)),
OldModuleTest(nn.SpatialConvolution,
constructor_args=(3, 4, 3, 3, 2, 2),
input_size=(2, 3, 6, 6),
desc='strided'),
OldModuleTest(nn.SpatialConvolution,
constructor_args=(3, 4, 3, 3, 2, 2, 1, 1),
input_size=(2, 3, 6, 6),
desc='padding'),
OldModuleTest(nn.SpatialConvolutionLocal,
constructor_args=(3, 2, 4, 4, 2, 2),
input_size=(1, 3, 4, 4)),
OldModuleTest(nn.SpatialConvolutionLocal,
constructor_args=(3, 2, 6, 6, 2, 2, 2, 2),
input_size=(2, 3, 6, 6),
desc='stride'),
OldModuleTest(nn.SpatialConvolutionLocal,
constructor_args=(3, 2, 6, 6, 2, 2, 2, 2, 1, 1),
input_size=(2, 3, 6, 6),
desc='stride_pad'),
OldModuleTest(nn.SpatialDivisiveNormalization,
constructor_args=(3,),
input_size=(2, 3, 8, 8)),
OldModuleTest(nn.SpatialContrastiveNormalization,
constructor_args=(3,),
input_size=(2, 3, 8, 8)),
OldModuleTest(nn.SpatialDilatedConvolution,
constructor_args=(3, 2, 3, 3, 2, 2, 1, 1, 2, 2),
input_size=(2, 3, 8, 8)),
OldModuleTest(nn.SpatialDilatedConvolution,
constructor_args=(3, 2, 3, 3, 2, 2, 1, 1, 2, 2),
input_size=(2, 3, 8, 8),
desc='stride_pad'),
OldModuleTest(nn.SpatialMaxPooling,
constructor_args=(3, 3, 2, 2, 1, 1),
input_size=(1, 3, 7, 7)),
OldModuleTest(nn.SpatialReflectionPadding,
constructor_args=(1, 2, 3, 4),
input_size=(2, 3, 8, 8)),
OldModuleTest(nn.SpatialReplicationPadding,
constructor_args=(1, 2, 3, 4),
input_size=(2, 3, 4, 4)),
OldModuleTest(nn.SpatialZeroPadding,
constructor_args=(1, 2, 3, 4),
input_size=(2, 3, 4, 4)),
OldModuleTest(nn.SpatialConvolutionMap,
constructor_args=(nn.SpatialConvolutionMap.maps.oneToOne(3), 3, 3),
input_size=(3, 5, 5),
desc='oneToOne'),
OldModuleTest(nn.SpatialConvolutionMap,
constructor_args=(nn.SpatialConvolutionMap.maps.oneToOne(3), 3, 3, 2, 2),
input_size=(3, 5, 5),
desc='oneToOne_stride'),
OldModuleTest(nn.SpatialConvolutionMap,
constructor_args=(nn.SpatialConvolutionMap.maps.full(3, 4), 3, 3),
input_size=(3, 5, 5),
desc='full'),
OldModuleTest(nn.SpatialFullConvolutionMap,
constructor_args=(nn.SpatialConvolutionMap.maps.oneToOne(3), 3, 3),
input_size=(3, 5, 5),
desc='oneToOne'),
OldModuleTest(nn.SpatialFullConvolutionMap,
constructor_args=(nn.SpatialConvolutionMap.maps.oneToOne(3), 3, 3, 2, 2),
input_size=(3, 5, 5),
desc='oneToOne_stride'),
OldModuleTest(nn.SpatialFullConvolutionMap,
constructor_args=(nn.SpatialConvolutionMap.maps.full(3, 4), 3, 3),
input_size=(3, 5, 5),
desc='full'),
# TODO: test CUDA
OldModuleTest(lambda: nn.SpatialFractionalMaxPooling(2, 2, 0.5, 0.5).fixPoolingRegions(),
input_size=(1, 3, 5, 5),
fullname='SpatialFractionalMaxPooling_ratio',
test_cuda=False),
OldModuleTest(lambda: nn.SpatialFractionalMaxPooling(2, 2, 4, 4).fixPoolingRegions(),
input_size=(1, 3, 7, 7),
fullname='SpatialFractionalMaxPooling_size',
test_cuda=False),
OldModuleTest(nn.SpatialFullConvolution,
constructor_args=(3, 4, 3, 3, 2, 2, 1, 1, 1, 1),
input_size=(1, 3, 7, 7)),
OldModuleTest(nn.SpatialLPPooling,
constructor_args=(3, 2, 2, 2, 2, 2),
input_size=(1, 3, 7, 7)),
OldModuleTest(nn.SpatialSubSampling,
constructor_args=(3, 3, 3, 2, 2),
input_size=(1, 3, 7, 7)),
OldModuleTest(nn.SpatialSubtractiveNormalization,
constructor_args=(3,),
input_size=(1, 3, 7, 7)),
OldModuleTest(nn.SpatialSubtractiveNormalization,
constructor_args=(3, torch.rand(3)),
input_size=(1, 3, 7, 7),
desc='kernel'),
OldModuleTest(nn.SpatialUpSamplingNearest,
constructor_args=(2,),
input_size=(1, 3, 4, 4)),
OldModuleTest(nn.TemporalConvolution,
constructor_args=(4, 5, 3),
input_size=(2, 10, 4)),
OldModuleTest(nn.TemporalConvolution,
constructor_args=(4, 5, 3, 2),
input_size=(2, 10, 4),
desc='stride'),
# TODO: this runs in non-batch mode only
OldModuleTest(nn.TemporalSubSampling,
constructor_args=(4, 3),
input_size=(10, 4)),
OldModuleTest(nn.TemporalSubSampling,
constructor_args=(4, 3, 2),
input_size=(10, 4),
desc='stride'),
OldModuleTest(nn.VolumetricAveragePooling,
constructor_args=(2, 2, 2),
input_size=(2, 3, 4, 4, 4)),
OldModuleTest(nn.VolumetricAveragePooling,
constructor_args=(2, 2, 2, 2, 2, 2),
input_size=(2, 3, 5, 5, 5),
desc='stride'),
OldModuleTest(nn.VolumetricAveragePooling,
constructor_args=(2, 2, 2, 2, 2, 2, 1, 1, 1),
input_size=(2, 3, 5, 5, 5),
desc='stride_pad'),
OldModuleTest(nn.VolumetricConvolution,
constructor_args=(3, 4, 2, 2, 2),
input_size=(2, 3, 3, 3, 3)),
OldModuleTest(nn.VolumetricConvolution,
constructor_args=(3, 4, 2, 2, 2, 2, 2, 2),
input_size=(2, 3, 5, 5, 5),
desc='stride'),
OldModuleTest(nn.VolumetricConvolution,
constructor_args=(3, 4, 2, 2, 2, 2, 2, 2, 1, 1, 1),
input_size=(2, 3, 5, 5, 5),
desc='stride_padding'),
OldModuleTest(nn.VolumetricFullConvolution,
constructor_args=(2, 3, 2, 2, 2),
input_size=(1, 2, 4, 4, 4)),
OldModuleTest(nn.VolumetricMaxPooling,
constructor_args=(2, 2, 2),
input_fn=lambda: (torch.randn(2, 3, 5, 5, 5) * 1000)),
OldModuleTest(nn.VolumetricMaxPooling,
constructor_args=(2, 2, 2, 2, 2, 2),
input_fn=lambda: (torch.randn(2, 3, 5, 5, 5) * 1000),
desc='stride'),
OldModuleTest(nn.VolumetricMaxPooling,
constructor_args=(2, 2, 2, 2, 2, 2, 1, 1, 1),
input_fn=lambda: (torch.randn(2, 3, 5, 5, 5) * 1000),
desc='stride_padding'),
OldModuleTest(nn.VolumetricReplicationPadding,
constructor_args=(1, 2, 3, 4, 5, 6),
input_size=(2, 3, 5, 5, 5)),
CriterionTest(nn.L1Cost,
input_size=(2, 3, 4, 5),
target=None),
CriterionTest(nn.L1HingeEmbeddingCriterion,
input_size=[(2, 3, 4, 5), (2, 3, 4, 5)],
target=1),
CriterionTest(nn.L1HingeEmbeddingCriterion,
constructor_args=(2,),
input_size=[(2, 3, 4, 5), (2, 3, 4, 5)],
target=1,
desc='margin'),
CriterionTest(nn.WeightedMSECriterion,
constructor_args_fn=lambda: (torch.rand(3, 4, 5),),
input_size=(2, 3, 4, 5),
target_size=(2, 3, 4, 5)),
CriterionTest(nn.MarginCriterion,
input_size=(5, 10),
target_fn=lambda: torch.randn(5, 10).sign()),
CriterionTest(nn.ClassSimplexCriterion,
constructor_args=(30,),
input_fn=lambda: torch.randn(5, 30).mul(10).renorm(2, 0, 1),
target_fn=lambda: torch.rand(5).mul(30).floor().long(),
desc='margin'),
]
# TODO: FlattenTable gradient
# TODO: NarrowTable gradient
# TODO: CriterionTable
# TODO: MultiCriterion
# TODO: SplitTable
for p in (1, 2, 1.5):
tests.append(
OldModuleTest(nn.Normalize,
constructor_args=(p,),
input_size=(4, 5),
# Eh, we need to use p as a default, so it's passed by value
reference_fn=lambda i, _, p=p: i.div(i.norm(p, 1, True).expand_as(i)),
desc=str(p)),
)
for p in range(1, 4 + 1):
tests.append(
OldModuleTest(nn.PairwiseDistance,
constructor_args=(p,),
input_size=[(4, 10), (4, 10)],
desc=str(p))
)
def build_spatial_unpooling_net():
pool = nn.SpatialMaxPooling(2, 2, 2, 2)
unpool = nn.SpatialMaxUnpooling(pool)
return nn.Sequential().add(pool).add(unpool)
tests.append(
OldModuleTest(build_spatial_unpooling_net,
input_size=(1, 3, 10, 10),
desc='SpatialMaxUnpooling')
)
def build_volumetric_unpooling_net():
pool = nn.VolumetricMaxPooling(2, 2, 2, 2)
unpool = nn.VolumetricMaxUnpooling(pool)
return nn.Sequential().add(pool).add(unpool)
tests.append(
OldModuleTest(build_volumetric_unpooling_net,
input_size=(1, 3, 10, 10),
desc='VolumetricMaxUnpooling')
)
def prepare_tests():
def add_test(test):
test_name = test.get_name()
cuda_test_name = test_name + '_cuda'
if hasattr(TestNN, test_name):
raise RuntimeError('Found two tests with the same name: ' + test_name)
if hasattr(TestNN, cuda_test_name):
raise RuntimeError('Found two tests with the same name: ' + cuda_test_name)
setattr(TestNN, test_name, lambda self, test=test: test(self))
setattr(TestNN, cuda_test_name, lambda self, test=test: test.test_cuda(self))
name_remap = {
'Conv2d': 'SpatialConvolution',
'MaxPool2d': 'SpatialMaxPooling',
'AvgPool2d': 'SpatialAveragePooling',
'Softmax': 'SoftMax',
'Softmax2d': 'SpatialSoftMax',
'LogSoftmax': 'LogSoftMax',
'BatchNorm1d': 'BatchNormalization',
'BatchNorm2d': 'SpatialBatchNormalization',
'BatchNorm3d': 'VolumetricBatchNormalization',
'Hardtanh': 'HardTanh',
'Hardshrink': 'HardShrink',
'Softplus': 'SoftPlus',
'Softshrink': 'SoftShrink',
'Softsign': 'SoftSign',
'Softmin': 'SoftMin',
'Tanhshrink': 'TanhShrink',
'CrossMapLRN2d': 'SpatialCrossMapLRN',
'L1Loss': 'AbsCriterion',
'NLLLoss': 'ClassNLLCriterion',
'NLLLoss2d': 'SpatialClassNLLCriterion',
'KLDivLoss': 'DistKLDivCriterion',
}
for test in tests:
add_test(test)
for test_params in module_tests:
test_params = deepcopy(test_params)
name = test_params.pop('module_name')
name = name_remap.get(name, name)
test_params['constructor'] = getattr(nn, name)
test = OldModuleTest(**test_params)
add_test(test)
for test_params in criterion_tests:
test_params = deepcopy(test_params)
name = test_params.pop('module_name')
name = name_remap.get(name, name.replace('Loss', 'Criterion'))
# nn.NLLLoss2d is deprecated, but there is a NLLLoss test for 2d
if name == 'ClassNLLCriterion' and 'desc' in test_params.keys() and '2d' in test_params['desc']:
name = 'SpatialClassNLLCriterion'
test_params['constructor'] = getattr(nn, name)
# If legacy constructor args are specified, use them instead
legacy_args = test_params.pop('legacy_constructor_args', None)
if legacy_args is not None:
test_params['constructor_args'] = legacy_args
test = CriterionTest(**test_params)
add_test(test)
def require_grad(input):
if isinstance(input, torch.Tensor):
input = input.detach()
input.requires_grad = True
return input
elif isinstance(input, collections.Iterable):
return type(input)(require_grad(e) for e in input)
return input
class TestNN(NNTestCase):
_do_cuda_memory_leak_check = True
def _numerical_jacobian(self, module, input, jacobian_input=True, jacobian_parameters=True):
def fw(input):
out = self._forward(module, input)
if isinstance(out, Variable):
return out.data
return out
res = tuple()
if jacobian_input:
input = require_grad(input)
res += get_numerical_jacobian(fw, input, eps=1e-6),
if jacobian_parameters:
params, _ = self._get_parameters(module)
jacobians = []
for p in params:
p = p.detach()
p.requires_grad = True
jacobians.append(get_numerical_jacobian(fw, input, p, eps=1e-6))
res += torch.cat(jacobians, 0),
return res
def _forward(self, module, input):
with freeze_rng_state():
with torch.no_grad():
return module.forward(input)
def _backward(self, module, input, output, grad_output, create_graph=False):
if isinstance(input, Variable):
input = input.data
return module.backward(input, grad_output)
def _forward_criterion(self, criterion, input, target):
with torch.no_grad():
return criterion.forward(input, target)
def _backward_criterion(self, criterion, input, target, gradOutput=None):
# Ignore gradOutput. It's used for non-legacy tests.
with torch.no_grad():
return criterion.backward(input, target)
def _zero_grad_parameters(self, module):
return module.zeroGradParameters()
def _get_parameters(self, module):
return module.parameters() or ([], [])
def test_Dropout(self):
p = 0.2
input = torch.Tensor(1000).fill_(1 - p)
module = nn.Dropout(p)
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
module = nn.Dropout(p, True)
output = module.forward(input.clone())
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input.clone(), input.clone())
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_SpatialDropout(self):
p = 0.2
b = random.randint(1, 5)
w = random.randint(1, 5)
h = random.randint(1, 5)
nfeats = 1000
input = torch.Tensor(b, nfeats, w, h).fill_(1)
module = nn.SpatialDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_VolumetricDropout(self):
p = 0.2
bsz = random.randint(1, 5)
t = random.randint(1, 5)
w = random.randint(1, 5)
h = random.randint(1, 5)
nfeats = 1000
input = torch.Tensor(bsz, nfeats, t, w, h).fill_(1)
module = nn.VolumetricDropout(p)
module.training()
output = module.forward(input)
self.assertLess(abs(output.mean() - (1 - p)), 0.05)
gradInput = module.backward(input, input)
self.assertLess(abs(gradInput.mean() - (1 - p)), 0.05)
# Check that these don't raise errors
module.__repr__()
str(module)
def test_ReLU_reference(self):
input = torch.randn(10, 20)
module = nn.ReLU()
output = module.forward(input)
self.assertTrue(output[input.ge(0)].eq(input[input.gt(0)]).all())
self.assertTrue(output[input.lt(0)].eq(0).all())
def test_ReLU6_reference(self):
input = torch.randn(10, 20).mul(10)
module = nn.ReLU6()
output = module.forward(input)
self.assertTrue(output[input.ge(6)].eq(6).all())
self.assertTrue(output[input.lt(0)].eq(0).all())
def test_Copy(self):
input = torch.randn(3, 4).double()
c = nn.Copy(torch.DoubleTensor, torch.FloatTensor)
output = c.forward(input)
self.assertIsInstance(output, torch.FloatTensor)
self.assertEqual(output, input.float(), 1e-6)
gradInput = c.backward(input, output.fill_(1))
self.assertIsInstance(gradInput, torch.DoubleTensor)
self.assertEqual(gradInput, output.double(), 1e-6)
c.dontCast = True
c.double()
self.assertIsInstance(output, torch.FloatTensor)
# Check that these don't raise errors
c.__repr__()
str(c)
def test_FlattenTable(self):
input = [
torch.rand(1),
[
torch.rand(2),
[
torch.rand(3)
],
],
torch.rand(4)
]
gradOutput = [
torch.rand(1),
torch.rand(2),
torch.rand(3),
torch.rand(4)
]
m = nn.FlattenTable()
output = m.forward(input)
self.assertEqual(len(output), 4)
self.assertEqual(output[0], input[0])
self.assertEqual(output[1], input[1][0])
self.assertEqual(output[2], input[1][1][0])
self.assertEqual(output[3], input[2])
gradInput = m.backward(input, gradOutput)
self.assertEqual(gradOutput[0], gradInput[0])
self.assertEqual(gradOutput[1], gradInput[1][0])
self.assertEqual(gradOutput[2], gradInput[1][1][0])
self.assertEqual(gradOutput[3], gradInput[2])
# Check that these don't raise errors
m.__repr__()
str(m)
# More uglyness: FlattenTable doesn't rebuild the table every updateOutput
# call, so we need to make sure that modifications to the input are
# detected correctly (and that the table is correctly rebuilt.
# CASE 1: Nothing changes so the output table shouldn't be redefined
old_input_map = m.input_map
old_output = m.output
m.forward(input)
self.assertEqual(old_input_map, m.input_map)
self.assertEqual(old_output, m.output)
# CASE 2: An element is added to the input table
old_input_map = m.input_map
old_output = m.output
input[1].append(torch.rand(5))
m.forward(input)
self.assertNotEqual(old_input_map, m.input_map)
self.assertNotEqual(old_output, m.output)
# CASE 3: An element is removed from the input table
old_input_map = m.input_map
old_output = m.output
input.pop()
m.forward(input)
self.assertNotEqual(old_input_map, m.input_map)
self.assertNotEqual(old_output, m.output)
def test_Concat(self):
input = torch.randn(4, 2)
num_modules = random.randint(2, 5)
linears = [nn.Linear(2, 5) for i in range(num_modules)]
m = nn.Concat(0)
for l in linears:
m.add(l)
l.zeroGradParameters()
l.weight.fill_(1)
l.bias.fill_(0)
# Check that these don't raise errors
m.__repr__()
str(m)
output = m.forward(input)
output2 = input.sum(1, True).expand(4, 5).repeat(num_modules, 1)
self.assertEqual(output2, output)
gradInput = m.backward(input, torch.ones(output2.size()))
gradInput2 = torch.ones(4, 2).fill_(num_modules * 5)
self.assertEqual(gradInput, gradInput2)
gradWeight = input.sum(0, keepdim=True).expand(5, 2)
for l in linears:
self.assertEqual(gradWeight, l.gradWeight)
def test_Parallel(self):
input = torch.randn(3, 4, 5)
m = nn.Parallel(0, 2)
m.add(nn.View(4, 5, 1))
m.add(nn.View(4, 5, 1))
m.add(nn.View(4, 5, 1))
# Check that these don't raise errors
m.__repr__()
str(m)
output = m.forward(input)
output2 = input.transpose(0, 2).transpose(0, 1)
self.assertEqual(output2, output)
gradInput = m.backward(input, output2)
self.assertEqual(gradInput, input)
def test_ParallelTable(self):
input = torch.randn(3, 4, 5)
p = nn.ParallelTable()
p.add(nn.View(4, 5, 1))
p.add(nn.View(4, 5, 1))
p.add(nn.View(4, 5, 1))
m = nn.Sequential()
m.add(nn.SplitTable(0))
m.add(p)
m.add(nn.JoinTable(2))
# Check that these don't raise errors
p.__repr__()
str(p)
output = m.forward(input)
output2 = input.transpose(0, 2).transpose(0, 1)
self.assertEqual(output2, output)
gradInput = m.backward(input, output2)
self.assertEqual(gradInput, input)
def test_ConcatTable(self):
input = [
torch.randn(3, 4).float(), torch.randn(3, 4).float(), [torch.randn(3, 4).float()]
]
_gradOutput = [
torch.randn(3, 3, 4).float(), torch.randn(3, 3, 4).float(), torch.randn(3, 3, 4).float()
]
gradOutput = [
[_gradOutput[0][0], _gradOutput[1][0], [_gradOutput[2][0]]],
[_gradOutput[0][1], _gradOutput[1][1], [_gradOutput[2][1]]],
[_gradOutput[0][2], _gradOutput[1][2], [_gradOutput[2][2]]]
]
module = nn.ConcatTable()
module.add(nn.Identity())
module.add(nn.Identity())
module.add(nn.Identity())
module.float()
# Check that these don't raise errors
module.__repr__()
str(module)
output = module.forward(input)
output2 = [input, input, input]
self.assertEqual(output2, output)
gradInput = module.backward(input, gradOutput)
gradInput2 = [_gradOutput[0].sum(0, keepdim=False), _gradOutput[1].sum(
0, keepdim=False), [_gradOutput[2].sum(0, keepdim=False)]]
self.assertTrue(isinstance(gradInput, list))
self.assertFalse(isinstance(gradInput[0], list))
self.assertFalse(isinstance(gradInput[1], list))
self.assertTrue(isinstance(gradInput[2], list))
self.assertEqual(len(gradInput), 3)
self.assertEqual(len(gradInput[2]), 1)
for t1, t2 in zip(iter_tensors(gradInput), iter_tensors(gradInput2)):
self.assertEqual(t1, t2)
# test outputs for variable length inputs
test = nn.ConcatTable()
test.add(nn.Identity())
test.add(nn.Identity())
x = [torch.randn(5), torch.randn(5)]
y = [torch.randn(5)]
o1 = len(test.forward(x))
go1 = len(test.backward(x, [x, x]))
o2 = len(test.forward(y))
go2 = len(test.backward(y, [y, y]))
self.assertEqual(o1, 2)
self.assertEqual(go1, 2)
self.assertEqual(o2, 2)
self.assertEqual(go2, 1)
def test_DepthConcat(self):
outputSize = [5, 6, 7, 8]
input = torch.randn(2, 3, 12, 12)
gradOutput = torch.randn(2, sum(outputSize), 12, 12)
concat = nn.DepthConcat(1)
concat.add(nn.SpatialConvolution(3, outputSize[0], 1, 1, 1, 1)) # > 2, 5, 12, 12
concat.add(nn.SpatialConvolution(3, outputSize[1], 3, 3, 1, 1)) # > 2, 6, 10, 10
concat.add(nn.SpatialConvolution(3, outputSize[2], 4, 4, 1, 1)) # > 2, 7, 9, 9
concat.add(nn.SpatialConvolution(3, outputSize[3], 5, 5, 1, 1)) # > 2, 8, 8, 8
concat.zeroGradParameters()
# forward/backward
outputConcat = concat.forward(input)
gradInputConcat = concat.backward(input, gradOutput)
# the spatial dims are the largest, the nFilters is the sum
output = torch.Tensor(2, sum(outputSize), 12, 12).zero_() # zero for padding
narrows = ((slice(None), slice(0, 5), slice(None), slice(None)),
(slice(None), slice(5, 11), slice(1, 11), slice(1, 11)),
(slice(None), slice(11, 18), slice(1, 10), slice(1, 10)),
(slice(None), slice(18, 26), slice(2, 10), slice(2, 10)))
gradInput = input.clone().zero_()
for i in range(4):
conv = concat.get(i)
gradWeight = conv.gradWeight.clone()
conv.zeroGradParameters()
output[narrows[i]].copy_(conv.forward(input))