forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_embedding.py
1260 lines (1084 loc) · 57.2 KB
/
test_embedding.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
# Owner(s): ["module: nn"]
import unittest
import random
import itertools
from itertools import product
import torch
from torch.testing._internal.common_utils import run_tests, set_default_dtype, skipIfTorchDynamo, \
instantiate_parametrized_tests, parametrize as parametrize_test, _assertGradAndGradgradChecks, IS_JETSON
from torch.testing._internal.common_cuda import TEST_CUDA
from torch.testing._internal.common_nn import NNTestCase
from torch.testing._internal.common_device_type import onlyNativeDeviceTypes, dtypes, \
instantiate_device_type_tests, dtypesIfCUDA, onlyCUDA, \
TEST_WITH_ROCM, skipCUDAIf, skipMeta
import torch.nn.functional as F
import torch.nn as nn
from torch.testing._internal.common_utils import dtype2prec_DONTUSE
class TestEmbeddingNN(NNTestCase):
_do_cuda_memory_leak_check = True
_do_cuda_non_default_stream = True
@unittest.skipIf(not TEST_CUDA, "CUDA unavailable")
def test_embedding_max_norm_unsorted_repeating_indices(self):
def create_embedding(device):
# Seed RNG so we get the same Embedding each time
torch.manual_seed(0)
return torch.nn.Embedding(
num_embeddings=20,
embedding_dim=64,
max_norm=1.0).to(device)
ix = torch.arange(2, device='cpu', dtype=torch.long).repeat(2000)
out_cpu = create_embedding('cpu')(ix)
ix = ix.to('cuda')
out = create_embedding('cuda')(ix)
self.assertEqual(out.cpu(), out_cpu)
def test_embedding_sparse_basic(self):
embedding = nn.Embedding(10, 20, sparse=True)
input = torch.tensor([[0, 2, 4, 5], [4, 3, 0, 9]], dtype=torch.long)
embedding(input).sum().backward()
self.assertTrue(embedding.weight.grad.is_sparse)
self.assertEqual(embedding.weight.grad.shape, embedding.weight.shape)
def test_embedding_sparse_empty_tensor(self):
embedding = nn.Embedding(0, 0, sparse=True)
input = torch.tensor([], dtype=torch.int64)
embedding(input).sum().backward()
self.assertTrue(embedding.weight.grad.is_sparse)
self.assertEqual(embedding.weight.grad.shape, embedding.weight.shape)
embedding = nn.Embedding(10, 0, sparse=True)
input = torch.LongTensor([[0, 2, 4, 5], [4, 3, 0, 9]])
embedding(input).sum().backward()
self.assertTrue(embedding.weight.grad.is_sparse)
self.assertEqual(embedding.weight.grad.shape, embedding.weight.shape)
def test_move_sparse_half_embedding(self):
embedding = nn.Embedding(10, 3, sparse=True)
self.assertEqual(embedding.weight.device.type, 'cpu')
self.assertEqual(embedding.weight.dtype, torch.get_default_dtype())
embedding.to(torch.float16)
self.assertEqual(embedding.weight.dtype, torch.float16)
self.assertEqual(embedding.embedding_dim, 3)
self.assertEqual(embedding.num_embeddings, 10)
if torch.cuda.is_available():
embedding.to('cuda')
self.assertEqual(embedding.weight.device.type, 'cuda')
embedding.to('cpu')
self.assertEqual(embedding.weight.device.type, 'cpu')
def test_embedding_max_norm(self):
embedding = nn.Embedding(22, 5, max_norm=1.0)
input = torch.tensor([2, 8, 8, 6], dtype=torch.long)
output = embedding(input)
self.assertEqual(output[1], output[2])
self.assertTrue(output.data.norm(p=2, dim=1).le(1).all())
@parametrize_test("dtype", (torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64, torch.float, torch.double))
def test_embedding_from_pretrained(self, dtype):
a = torch.tensor([[1., 2., 3.], [4., 5., 6.]], dtype=dtype)
embedding = nn.Embedding.from_pretrained(a)
self.assertEqual(a, embedding.weight.data)
input = torch.LongTensor([0, 1])
output = embedding(input)
self.assertEqual(a, output)
def test_embedding_bag_from_pretrained(self):
a = torch.tensor([[1., 2., 3.], [4., 5., 6.]])
embedding = nn.EmbeddingBag.from_pretrained(a)
self.assertEqual(a, embedding.weight)
input = torch.tensor([0, 1], dtype=torch.long)
output = embedding(input, torch.arange(input.size(0)))
self.assertEqual(a, output)
def test_embedding_from_pretrained_padding_idx(self):
padding_idx = 2
padding_vec = torch.ones(3) * 7
embeddings = torch.rand(4, 3, requires_grad=True)
with torch.no_grad():
embeddings[padding_idx] = padding_vec
embedding_nn = nn.Embedding.from_pretrained(embeddings, padding_idx=padding_idx)
self.assertEqual(embedding_nn.weight[padding_idx], padding_vec)
def test_embedding_bag_from_pretrained_padding_idx(self):
padding_idx = 2
embeddings = torch.rand(4, 3, requires_grad=True)
embedding_nn = nn.EmbeddingBag.from_pretrained(embeddings, padding_idx=padding_idx)
self.assertEqual(embedding_nn.weight, embeddings)
def test_embedding_from_pretrained_options(self):
with set_default_dtype(torch.double):
a = torch.tensor([[1., 2., 3.], [4., 5., 6.]])
opts = {
"max_norm": 2.,
"norm_type": .5,
"scale_grad_by_freq": False,
"sparse": True
}
embedding = nn.Embedding.from_pretrained(a, **opts)
input = torch.LongTensor([0, 1])
output = embedding(input)
# test output and that weight matrix was renormalized
self.assertEqual(a, output)
self.assertTrue(a.ne(torch.arange(1, 7, dtype=a.dtype).view(2, 3)).all())
self.assertTrue(output.data.norm(p=opts["norm_type"], dim=1).le(opts["max_norm"]).all())
def test_embedding_functional(self):
a = torch.tensor([
[1, 3, 2],
[0, 2, 1]
], dtype=torch.long)
embeddings = torch.rand(4, 3, requires_grad=True)
embed_old = torch.nn.Embedding(4, 3)
embed_old.weight.data = embeddings.data
res_old = embed_old(a)
res_F = F.embedding(a, embeddings)
self.assertEqual(res_old, res_F)
embed_old = torch.nn.Embedding(4, 3)
embed_old = embed_old.from_pretrained(embeddings, padding_idx=2)
res_old = embed_old(a)
res_F = F.embedding(a, embeddings, padding_idx=2)
self.assertEqual(res_old, res_F)
def test_embedding_bag_functional(self):
a = torch.tensor([
[1, 3, 2],
[0, 2, 1]
], dtype=torch.long)
embeddings = torch.rand(4, 3, requires_grad=True)
embed_old = torch.nn.EmbeddingBag(4, 3)
embed_old.weight = torch.nn.Parameter(embeddings)
res_old = embed_old(a)
res_F = F.embedding_bag(a, embeddings)
self.assertEqual(res_old, res_F)
embed_old = torch.nn.EmbeddingBag(4, 3)
embed_old = embed_old.from_pretrained(embeddings, padding_idx=2)
res_old = embed_old(a)
res_F = F.embedding_bag(a, embeddings, padding_idx=2)
self.assertEqual(res_old, res_F)
# Make sure that error is thrown if padding_idx is out of bounds
def test_embedding_bag_padding_idx_error(self):
a = torch.tensor([
[1, 3, 2],
[0, 2, 1]
], dtype=torch.long)
num_embeddings = 4
num_features = 3
embeddings = torch.rand(num_embeddings, num_features, requires_grad=True)
functional_err_msg = r'padding_idx must be within the number of embeddings'
module_err_msg = r'padding_idx must be within num_embeddings'
for padding_idx in range(-(num_embeddings + 2), (num_embeddings + 2)):
if (padding_idx < -num_embeddings) or (padding_idx >= num_embeddings):
with self.assertRaisesRegex(RuntimeError, functional_err_msg):
F.embedding_bag(a, embeddings, padding_idx=padding_idx)
with self.assertRaisesRegex(AssertionError, module_err_msg):
torch.nn.EmbeddingBag(num_embeddings, num_features, padding_idx=padding_idx)
else:
F.embedding_bag(a, embeddings, padding_idx=padding_idx)
torch.nn.EmbeddingBag(num_embeddings, num_features, padding_idx=padding_idx)
def test_embeddingbag_from_pretrained(self):
a = torch.tensor([[1., 2., 3.], [4., 5., 6.]])
embeddingbag = nn.EmbeddingBag.from_pretrained(a)
self.assertEqual(a, embeddingbag.weight.data)
input = torch.LongTensor([[0, 1]])
output = embeddingbag(input)
self.assertEqual(a.mean(0, keepdim=True), output)
def test_embeddingbag_from_pretrained_options(self):
with set_default_dtype(torch.double):
a = torch.tensor([[1., 2., 3.], [4., 5., 6.]])
opts = {
"max_norm": 2.,
"norm_type": .5,
"scale_grad_by_freq": False,
"mode": "max",
"sparse": False
}
embeddingbag = nn.EmbeddingBag.from_pretrained(a, **opts)
input = torch.LongTensor([[0, 1]])
output = embeddingbag(input)
self.assertEqual(a.max(0, keepdim=True)[0], output)
self.assertTrue(a.ne(torch.arange(1, 7, dtype=a.dtype).view(2, 3)).all())
self.assertTrue(a.norm(p=opts["norm_type"], dim=1).le(opts["max_norm"]).all())
def test_embeddingbag_include_last_offset(self):
# Test case from https://github.com/pytorch/pytorch/issues/89677
embeddingbag = nn.EmbeddingBag(100, 3, include_last_offset=True, padding_idx=61)
input = torch.tensor([0, 1, 2, 3])
out = embeddingbag(input, torch.tensor([0, 3, 3]))
out2 = embeddingbag(input, torch.tensor([0, 3, 4]))
weight = embeddingbag.weight
row0 = weight[0:3].mean(0)
row1 = weight[3]
ref_out = torch.stack([row0, row1])
self.assertEqual(ref_out, out)
self.assertEqual(ref_out, out2)
class TestEmbeddingNNDeviceType(NNTestCase):
def test_embedding_dense_grad(self, device):
with set_default_dtype(torch.double):
embd = nn.Embedding(20, 20).to(device)
weight = embd.weight
def fn_wrapper(device):
def fn(weight):
inp = torch.tensor([[0, 1, 1, 2], [3, 5, 7, 11]], dtype=torch.long).to(device)
return torch.nn.functional.embedding(inp, weight)
return fn
fn = fn_wrapper(device)
_assertGradAndGradgradChecks(self, fn, (weight, ))
def test_embedding_scalar_weight_error(self, device):
indices = torch.rand(2, 2, device=device).long()
weights = [
torch.tensor(1.0, device=device),
torch.tensor(1.0, device=device).reshape(1, 1, 1),
]
for weight in weights:
with self.assertRaisesRegex(RuntimeError, "'weight' must be 2-D"):
torch.nn.functional.embedding(indices, weight)
@dtypesIfCUDA(torch.float16, torch.float64)
@dtypes(torch.float64)
def test_embedding_backward(self, device, dtype):
embedding = nn.Embedding(10, 3, sparse=True)
tensor = torch.tensor([[7, 1, 3]])
ones = torch.tensor(1., dtype=dtype).expand(3, 3)
tensorTwice = tensor.repeat(1, 2)
onesTwice = torch.cat((ones, ones))
embedding = embedding.to(dtype=dtype).to(device)
tensor = tensor.to(device)
ones = ones.to(device)
tensorTwice = tensorTwice.to(device)
onesTwice = onesTwice.to(device)
embedding.zero_grad()
embedding(tensor[0]).sum().backward()
self.assertEqual(embedding.weight.grad._indices(), tensor)
self.assertEqual(embedding.weight.grad._values(), ones)
embedding.zero_grad()
embedding(tensor[0]).sum().backward()
embedding(tensor[0]).sum().backward()
self.assertEqual(embedding.weight.grad._indices(), tensorTwice)
self.assertEqual(embedding.weight.grad._values(), onesTwice)
embedding.zero_grad()
embedding(tensor[0]).sum().backward()
tensor[0, 0] = 8
embedding(tensor[0]).sum().backward()
tensorTwice[0, 3] = 8
self.assertEqual(embedding.weight.grad._indices(), tensorTwice)
self.assertEqual(embedding.weight.grad._values(), onesTwice)
@dtypesIfCUDA(*((torch.float, torch.double, torch.bfloat16, torch.half)
if TEST_WITH_ROCM else (torch.float, torch.double, torch.half)))
@dtypes(torch.float32)
def test_embedding_max_norm_backward(self, device, dtype):
# can't use gradcheck since in place renorm makes analytical gradients different from produced ones
weight = torch.randn((4, 4), device=device, dtype=dtype) * 2
weight.requires_grad_()
inp_list = [0, 1, 2, 2]
inp = torch.tensor(inp_list, device=device)
out = nn.functional.embedding(inp, weight, max_norm=1.).sum()
out.backward()
expected_grad = torch.tensor([[1., 1., 2., 0.]], device=device, dtype=dtype).transpose(0, 1).expand(4, 4)
self.assertEqual(weight.grad, expected_grad)
@dtypesIfCUDA(*((torch.float, torch.double, torch.bfloat16, torch.half)
if TEST_WITH_ROCM else (torch.float, torch.double, torch.half)))
@dtypes(torch.float32)
def test_embedding_max_norm_fwd_AD(self, device, dtype):
if torch.device(device).type == 'xla':
self.skipTest("forward AD doesn't work on xla")
# can't use gradcheck since in place renorm makes analytical gradients different from produced ones
weight = torch.randn((4, 4), device=device, dtype=dtype) * 2
tangent = torch.ones((4, 4), device=device, dtype=dtype)
inp = torch.tensor([[0, 1], [2, 2]], device=device)
with torch.autograd.forward_ad.dual_level():
dual_weight = torch.autograd.forward_ad.make_dual(weight, tangent)
out = nn.functional.embedding(inp, dual_weight, max_norm=1.)
jvp = torch.autograd.forward_ad.unpack_dual(out).tangent
expected_grad = torch.ones((2, 2, 4), device=device, dtype=dtype)
self.assertEqual(jvp, expected_grad)
@dtypesIfCUDA(*((torch.float, torch.double, torch.bfloat16, torch.half)
if TEST_WITH_ROCM else (torch.float, torch.double, torch.half)))
@dtypes(torch.float32)
def test_embedding_padding_idx(self, device, dtype):
embedding = nn.Embedding(10, 20, padding_idx=0).to(device, dtype)
input = torch.tensor([[0, 2, 4, 5], [4, 3, 0, 9]], dtype=torch.long).to(device)
output = embedding(input)
self.assertEqual(output[0][0].sum(), 0)
self.assertEqual(output[1][2].sum(), 0)
embedding = nn.Embedding(10, 20, padding_idx=0, sparse=True).to(device, dtype)
input = torch.tensor([[0, 2, 4, 5], [4, 3, 0, 9]], dtype=torch.long).to(device)
output = embedding(input)
self.assertEqual(output[0][0].sum(), 0)
self.assertEqual(output[1][2].sum(), 0)
# negative indexing check for padding_idx
# padding_idx=-2, num_embeddings=10 ==> index 8 padded
embedding = nn.Embedding(10, 20, padding_idx=-2).to(device, dtype)
input = torch.tensor([[0, 2, 8, 5], [4, 8, 0, 9]], dtype=torch.long).to(device)
output = embedding(input)
self.assertEqual(output[0][2].sum(), 0)
self.assertEqual(output[1][1].sum(), 0)
embedding = nn.Embedding(10, 20, padding_idx=-2, sparse=True).to(device, dtype)
input = torch.tensor([[0, 2, 8, 5], [4, 8, 0, 9]], dtype=torch.long).to(device)
output = embedding(input)
self.assertEqual(output[0][2].sum(), 0)
self.assertEqual(output[1][1].sum(), 0)
# change padding vector
padding_vector = torch.ones(20, dtype=dtype, device=device)
embedding = nn.Embedding(10, 20, padding_idx=2, sparse=True).to(device, dtype)
with torch.no_grad():
embedding.weight[2] = padding_vector
input = torch.tensor([0, 2], dtype=torch.long).to(device)
output = embedding(input)
self.assertEqual(output[1], padding_vector)
# out of bounds check for padding_idx
self.assertRaises(AssertionError, nn.Embedding, num_embeddings=10, embedding_dim=20, padding_idx=25)
self.assertRaises(AssertionError, nn.Embedding, num_embeddings=10, embedding_dim=20, padding_idx=-25)
padding_idx = 0
embedding = nn.Embedding(5, 2, padding_idx=padding_idx).to(device, dtype)
for n in (1, 2, 1000): # Need large N to trigger all the methods we have implemented
for other_indices in ([], [1, 3], [2]):
indices = torch.tensor(other_indices + [padding_idx] * n, dtype=torch.long).to(device)
pre = embedding.weight[padding_idx].clone()
embedding(indices).sum().backward()
after = (embedding.weight + embedding.weight.grad)[padding_idx]
embedding.zero_grad()
self.assertEqual(after, pre)
# test double backward
emb_sum = embedding(indices).sum()
emb_grad = torch.autograd.grad(outputs=emb_sum, inputs=list(embedding.parameters()), retain_graph=True)
scalar = emb_grad[0].sum() + emb_sum
scalar.backward()
after = (embedding.weight + embedding.weight.grad)[padding_idx]
embedding.zero_grad()
self.assertEqual(after, pre)
# Check correctness of torch.nn.functional.embedding_bag forward and
# backward functions with padding_idx, given a 1D input separated into bags
# with an offset array. Compare against an equivalent 2D input that uses
# padding indices to fill in the gaps indicated by the offset array
@skipIfTorchDynamo("see https://github.com/pytorch/pytorch/pull/95621")
@onlyNativeDeviceTypes
@dtypes(torch.float32, torch.float64)
@dtypesIfCUDA(torch.half, torch.bfloat16)
def test_embedding_bag_1D_padding_idx(self, device, dtype):
num_features = 3
max_indices_per_bag = 10
num_bags = 10
num_words = 100
def gen_1D_indices_offsets(include_last_offset, allpad):
indices = []
offsets = []
cur_offset = 0
# Make one bag full and one bag empty, for extra coverage
empty_bag = random.randint(0, num_bags - 1)
full_bag = empty_bag
while full_bag == empty_bag:
full_bag = random.randint(0, num_bags - 1)
for bag in range(num_bags):
offsets.append(cur_offset)
if bag == full_bag:
bag_size = max_indices_per_bag
elif bag == empty_bag:
bag_size = 0
else:
bag_size = random.randint(1, max_indices_per_bag - 1)
indices += [1 if allpad else random.randint(0, num_words - 1) for _ in range(bag_size)]
cur_offset += bag_size
# embedding_bag requires first entry of offsets to be 0
assert offsets[0] == 0
indices = torch.tensor(indices, device=device)
if include_last_offset:
offsets.append(indices.size(0))
offsets = torch.tensor(offsets, device=device)
return indices, offsets
# Convert a 1-D indices-offsets representation into 2-D. Fill any empty
# indices with padding_idx
def gen_2D_indices_from_1D(indices_1D, offsets, include_last_offset, padding_idx):
assert offsets[0] == 0
if include_last_offset:
offsets = offsets[:-1]
indices_2D = torch.empty(num_bags, max_indices_per_bag, device=device, dtype=torch.long)
for bag in range(num_bags):
# Determine the start and end position of the bag within indices_1D
start = offsets[bag]
end = len(indices_1D) if bag + 1 == num_bags else offsets[bag + 1]
end = min(len(indices_1D), end)
# Pull out the bag's indices from indices_1D, and fill any
# remaining space with padding indices
indices_in_bag = []
for item_pos in range(0, max_indices_per_bag):
if (start + item_pos) < end:
indices_in_bag.append(indices_1D[start + item_pos])
else:
indices_in_bag.append(padding_idx)
indices_2D[bag] = torch.tensor(indices_in_bag, device=device)
return indices_2D
test_cases = product(['max', 'mean', 'sum'], [False, True], [False, True], [False, True])
for mode, sparse, include_last_offset, allpad in test_cases:
# Max sparse and bfloat16 are not supported
if mode == 'max':
if sparse or (dtype == torch.bfloat16):
continue
indices_1D, offsets = gen_1D_indices_offsets(include_last_offset, allpad)
for padding_idx_1D in list(set(indices_1D.tolist())) + [None]:
msg = (
f"mode: '{mode}', sparse: {sparse}, include_last_offset: {include_last_offset}, "
f"padding_idx_1D: {padding_idx_1D}")
# If 1D input does not use a padding index, we still need one for the 2D input,
# so we can add one dummy word to the weights to act as the padded word
padding_idx_2D = padding_idx_1D if padding_idx_1D is not None else num_words
num_words_with_padding = num_words if padding_idx_1D is not None else num_words + 1
indices_2D = gen_2D_indices_from_1D(
indices_1D,
offsets,
include_last_offset,
padding_idx_2D)
weights = torch.randn(
num_words_with_padding,
num_features,
dtype=dtype,
device=device,
requires_grad=True)
weights_check = weights.clone().detach().requires_grad_(True)
bag = torch.nn.functional.embedding_bag(
indices_1D,
weights,
offsets,
padding_idx=padding_idx_1D,
mode=mode,
sparse=sparse,
include_last_offset=include_last_offset)
bag_check = torch.nn.functional.embedding_bag(
indices_2D,
weights_check,
padding_idx=padding_idx_2D,
mode=mode,
sparse=sparse)
self.assertEqual(bag, bag_check, msg=msg)
bag.sum().backward()
bag_check.sum().backward()
# Sometimes, half dtype gradients mismatch by a greater amount
# than other dtypes
if dtype in [torch.half, torch.bfloat16]:
atol = 0.01
rtol = 0.01
else:
atol = None
rtol = None
self.assertEqual(weights.grad, weights_check.grad, msg=msg, atol=atol, rtol=rtol)
# Check correctness of torch.nn.functional.embedding_bag forward and
# backward functions with padding_idx, given a 2D indices input. Compare
# against torch.nn.functional.embedding followed by a reduction.
@onlyNativeDeviceTypes
@dtypes(torch.float32, torch.float64)
@dtypesIfCUDA(torch.half, torch.bfloat16)
def test_embedding_bag_2D_padding_idx(self, device, dtype):
# Use a Python implementation of embedding_bag with padding_idx support
# to check torch.nn.functional.embedding_bag correctness
def embedding_bag_check(indices, weights, mode, sparse, padding_idx):
assert padding_idx is not None
embedding = torch.nn.functional.embedding(
indices,
weights,
padding_idx=padding_idx,
sparse=sparse)
reduction_dim = indices.dim() - 1
if mode == 'sum' or mode == 'mean':
# We must avoid including elements at padding_idx in the
# sum/mean, so multiply those elements by 0, and multiply
# all other elements by 1
per_sample_weights = indices.ne(padding_idx).to(dtype).unsqueeze(-1)
res = embedding.mul(per_sample_weights).sum(dim=reduction_dim)
if mode == 'mean':
weights_sum = per_sample_weights.sum(dim=reduction_dim)
res = res.div(weights_sum)
elif mode == 'max':
# We must avoid allowing elements at padding_idx to be chosen
# as the max, so set those elements to negative infinity
res = embedding.masked_fill(
indices.unsqueeze(-1) == padding_idx, -float('inf')
).amax(dim=reduction_dim)
else:
raise RuntimeError(f"mode '{mode}' is not available")
# If a row is all padding, set its corresponding result row to 0.
# This is needed because the above mean and max mode
# implementations set these elements to nan and -inf, respectively
if mode in ['mean', 'max']:
res = res.masked_fill(
indices.eq(padding_idx).all(dim=-1).unsqueeze(-1),
0)
return res
num_features = 3
num_words = 10
indices_dim1 = 10
for mode, sparse, allpad, indices_dim0 in product(['max', 'mean', 'sum'], [False, True], [False, True], [1, 10]):
# Max sparse and bfloat16 are not supported
if mode == 'max':
if sparse or (dtype == torch.bfloat16):
continue
if allpad:
indices = torch.empty(indices_dim0, indices_dim1, dtype=torch.long, device=device).fill_(1)
else:
indices = torch.randint(0, num_words, (indices_dim0, indices_dim1), device=device)
if indices_dim0 > 1:
# Fill one row with duplicate index so we can test with a fully
# padded row
duplicate_row = random.randint(0, indices_dim0 - 1)
indices[duplicate_row] = indices[duplicate_row][0]
for padding_idx in list(set(indices.flatten(0, -1).tolist())):
weights = torch.randn(num_words, num_features, dtype=dtype, device=device, requires_grad=True)
weights_check = weights.clone().detach().requires_grad_(True)
msg = (
f"mode: '{mode}', sparse: {sparse}, padding_idx: {padding_idx}, "
f"allpad: {allpad}, indices.size(): {indices.size()}")
# Check forward with a Python implementation of padding_idx embedding_bag
bag_check = embedding_bag_check(
indices,
weights_check,
mode,
sparse,
padding_idx)
bag = torch.nn.functional.embedding_bag(
indices,
weights,
padding_idx=padding_idx,
mode=mode,
sparse=sparse)
self.assertEqual(bag, bag_check, msg=msg)
bag_check.sum().backward()
grad_check = weights_check.grad
bag.sum().backward()
grad = weights.grad
# Sometimes, half dtype gradients mismatch by a greater amount
# than other dtypes
if dtype in [torch.half, torch.bfloat16]:
atol = 0.01
rtol = 0.01
else:
atol = None
rtol = None
self.assertEqual(grad, grad_check, msg=msg, atol=atol, rtol=rtol)
@onlyCUDA
@dtypes(*((torch.float, torch.double, torch.bfloat16, torch.half)
if TEST_WITH_ROCM else (torch.float, torch.double, torch.half)))
def test_embedding_max_norm_device(self, device, dtype):
embedding = nn.Embedding(22, 5, max_norm=1.0).to(device, dtype=dtype)
# nn.Embedding only takes LongTensor as input
input = torch.tensor([2, 8, 8, 6], device=device, dtype=torch.long)
output = embedding(input)
self.assertEqual(output[1], output[2])
self.assertTrue(output.data.norm(p=2, dim=1).le(1).all())
@dtypes(*itertools.product((torch.int, torch.long), (torch.int, torch.long)))
def test_embedding_bag_empty_input(self, device, dtypes):
m = 4
n = 3
x = torch.tensor([], device=device, dtype=dtypes[0])
for sparse in [True, False]:
Embed = torch.nn.EmbeddingBag(m, n, sparse=sparse)
Embed.to(device)
output = Embed(input=x, offsets=torch.tensor([0], device=device, dtype=dtypes[1]))
self.assertEqual(output, torch.zeros_like(output))
output = Embed(input=x, offsets=torch.tensor([0, 0], device=device, dtype=dtypes[1]))
self.assertEqual(output, torch.zeros_like(output))
@skipCUDAIf(True, "no out-of-bounds check on CUDA for perf.")
@dtypes(*itertools.product((torch.float, torch.double), (torch.int, torch.long)))
@parametrize_test("padding_idx", [None, 0])
@parametrize_test("mode", ["sum", "mean", "max"])
def test_embedding_bag_out_of_bounds_idx(self, device, dtypes, padding_idx, mode):
padding_idx = 0
w_dtype, idx_dtype = dtypes
# negative out-of-bound
idx1 = torch.tensor([[-1, 1]], device=device, dtype=idx_dtype)
# positive out-of-bound
idx2 = torch.tensor([[11, 8]], device=device, dtype=idx_dtype)
weight = torch.randn(10, 2, device=device, dtype=w_dtype)
if mode == 'sum':
# Only `sum` supports per_sample_weight
per_sample_weights = (None, torch.randn_like(idx1, device=device, dtype=w_dtype))
else:
per_sample_weights = (None,)
for p_s_weights, idx in itertools.product(per_sample_weights, (idx1, idx2)):
msg = "Expected idx >= 0 && idx < num_embeddings"
with self.assertRaisesRegex(RuntimeError, msg):
torch.nn.functional.embedding_bag(idx, weight,
per_sample_weights=p_s_weights, padding_idx=padding_idx,
mode=mode)
def test_embedding_bag_dimension_errors(self, device):
funcs = (
lambda x, y, z: torch.nn.functional.embedding_bag(y, x, z),
torch.embedding_bag,
torch._embedding_bag,
torch._embedding_bag_forward_only
)
for i, f in enumerate(funcs):
err_type = ValueError if i == 0 else RuntimeError
weight = torch.full((2, 6,), 0, dtype=torch.float64, device=device)
indices = torch.full((2, 0, 0, 6, 6,), 2, dtype=torch.int64, device=device)
offsets = torch.full((2, 0, 0, 6, 6), 0, dtype=torch.int64, device=device)
if i == 0:
error_msg = 'input has to be 1D or 2D Tensor'
else:
error_msg = 'input has to be a 1D or 2D Tensor'
with self.assertRaisesRegex(err_type, error_msg):
f(weight, indices, offsets)
weight = torch.full((2, 2), 0, dtype=torch.float64, device=device)
indices = torch.full((2,), 1, dtype=torch.int64, device=device)
with self.assertRaisesRegex(err_type, 'offsets has to be a 1D Tensor'):
f(weight, indices, offsets)
weight = torch.full((2, 2, 2), 0, dtype=torch.float64, device=device)
indices = torch.full((2,), 2, dtype=torch.int64, device=device)
offsets = torch.full((2,), 0, dtype=torch.int64, device=device)
with self.assertRaisesRegex(err_type, 'weight has to be a 2D Tensor'):
f(weight, indices, offsets)
@dtypes(*itertools.product((torch.int, torch.long), (torch.int, torch.long)))
def test_EmbeddingBag_per_sample_weights_failures(self, device, dtypes):
# Failure 1: mismatched embeddings / per_sample_weights dtype
es = nn.EmbeddingBag(5, 2, mode='sum').to(dtype=torch.float, device=device)
input = torch.tensor([3, 1, 1, 1, 4, 0], dtype=dtypes[0], device=device)
offsets = torch.tensor([0, 0, 3, 3, 6], dtype=dtypes[1], device=device)
per_sample_weights = torch.randn_like(input, dtype=torch.double, device=device)
if device == 'cpu':
with self.assertRaisesRegex(RuntimeError, 'have the same type as'):
es(input, offsets, per_sample_weights)
else:
with self.assertRaisesRegex(RuntimeError, 'expected scalar type'):
es(input, offsets, per_sample_weights)
# Failure 2.1: input/per_sample_weights have different sizes (1d input)
input = torch.tensor([3, 1, 1, 1, 4, 0], dtype=dtypes[0], device=device)
offsets = torch.tensor([0, 0, 3, 3, 6], dtype=dtypes[1], device=device)
per_sample_weights = torch.randn(5, dtype=torch.float, device=device)
with self.assertRaisesRegex(ValueError, 'same shape as the input'):
es(input, offsets, per_sample_weights)
# Failure 2.2: input/per_sample_weights have different sizes (2d input)
input = torch.randint(5, (7, 3), dtype=dtypes[0], device=device)
offsets = None
per_sample_weights = torch.randn(7 * 3, dtype=torch.float, device=device)
with self.assertRaisesRegex(ValueError, 'same shape as the input'):
es(input, offsets, per_sample_weights)
# Failure 3: Unsupported per_sample_weights and mode=('max', 'mean')
for unsupported_mode in ('max', 'mean'):
es = nn.EmbeddingBag(5, 2, mode=unsupported_mode).to(
dtype=torch.float, device=device)
input = torch.randint(5, (7, 3), dtype=dtypes[0], device=device)
offsets = None
per_sample_weights = torch.randn(7, 3, dtype=torch.float, device=device)
with self.assertRaisesRegex(NotImplementedError,
"only supported for mode='sum'"):
es(input, offsets, per_sample_weights)
def _embedding_bag_reference_impl(self, input, weight, offsets=None, mode='sum',
per_sample_weights=None, include_last_offset=False):
assert mode == 'sum' or per_sample_weights is None
assert offsets is not None
if per_sample_weights is None:
per_sample_weights = torch.ones(input.size()).to(
dtype=weight.dtype, device=weight.device
)
assert input.numel() == per_sample_weights.numel()
bags = []
long_input = input.to(torch.long)
embeddings = weight.index_select(0, long_input) * per_sample_weights.unsqueeze(1)
if include_last_offset:
for index in range(len(offsets) - 1):
offset = offsets[index]
next_offset = offsets[index + 1]
length = next_offset - offset
if length == 0:
bags.append(
torch.tensor([0] * weight.size(1)).to(
dtype=embeddings.dtype, device=embeddings.device
)
)
else:
if mode == 'sum':
bags.append(embeddings.narrow(0, offset, length).sum(0))
elif mode == 'mean':
bags.append(embeddings.narrow(0, offset, length).sum(0).div(length))
else:
assert mode == 'max'
bags.append(embeddings.narrow(0, offset, length).max(0)[0])
else:
for index, offset in enumerate(offsets):
if index + 1 < len(offsets):
next_offset = offsets[index + 1]
else:
next_offset = len(long_input)
length = next_offset - offset
if length == 0:
bags.append(
torch.tensor([0] * weight.size(1)).to(
dtype=embeddings.dtype, device=embeddings.device
)
)
else:
if mode == 'sum':
bags.append(embeddings.narrow(0, offset, length).sum(0))
elif mode == 'mean':
bags.append(embeddings.narrow(0, offset, length).sum(0).div(length))
else:
assert mode == 'max'
bags.append(embeddings.narrow(0, offset, length).max(0)[0])
return torch.stack(bags)
@skipMeta
@dtypes(*itertools.product((torch.int, torch.long), (torch.int, torch.long),
(torch.half, torch.bfloat16, torch.float, torch.double)))
@dtypesIfCUDA(*itertools.product((torch.int, torch.long), (torch.int, torch.long),
(torch.float, torch.double, torch.half)))
def test_EmbeddingBag_empty_per_sample_weights_and_offsets(self, device, dtypes):
# Test empty input and per sample weight, and backward pass. There was a CUDA
# invalid configuration bug (more context in #46572)
def test_per_sample_weights(mode, trainable_scale):
es = nn.EmbeddingBag(5, 2, mode=mode).to(dtype=dtypes[2], device=device)
es.weight.data.copy_(
torch.arange(1, 11, device=device).view_as(es.weight).to(dtypes[2]))
input = torch.tensor([], device=device, dtype=dtypes[0])
offsets = torch.tensor([0, 0, 0, 0, 0], device=device, dtype=dtypes[1])
per_sample_weights = torch.randn_like(input, dtype=dtypes[2]) \
.requires_grad_(trainable_scale)
ref_per_sample_weights = \
per_sample_weights.detach().requires_grad_(trainable_scale)
reference_weights = es.weight.detach().requires_grad_()
expected = self._embedding_bag_reference_impl(
input, reference_weights, offsets, mode, ref_per_sample_weights)
result = es(input, offsets, per_sample_weights)
self.assertEqual(result, expected, atol=dtype2prec_DONTUSE[dtypes[2]], rtol=0)
grad = torch.randn_like(expected)
result.backward(grad)
# the reference impl doesn't have grad fn for empty input; but the grad should
# simply be a zero tensor
ref_weights_grad = torch.zeros_like(es.weight)
self.assertEqual(es.weight.grad, ref_weights_grad,
atol=dtype2prec_DONTUSE[dtypes[2]], rtol=0)
if trainable_scale:
ref_per_sample_weights_grad = torch.empty_like(per_sample_weights)
self.assertEqual(per_sample_weights.grad, ref_per_sample_weights_grad,
atol=dtype2prec_DONTUSE[dtypes[2]], rtol=0)
modes = ('sum',)
trainable_scale = (True, False)
for mode, trainable in itertools.product(modes, trainable_scale):
test_per_sample_weights(mode, trainable)
@skipMeta
@dtypes(*itertools.product((torch.int, torch.long), (torch.int, torch.long),
(torch.float, torch.double, torch.half, torch.bfloat16)))
@dtypesIfCUDA(*itertools.product((torch.int, torch.long), (torch.int, torch.long),
(torch.float, torch.double, torch.half)))
def test_EmbeddingBag_per_sample_weights_and_offsets(self, device, dtypes):
def test_per_sample_weights(mode, trainable_scale):
es = nn.EmbeddingBag(5, 2, mode=mode).to(dtype=dtypes[2], device=device)
es.weight.data.copy_(
torch.arange(1, 11, device=device).view_as(es.weight).to(dtypes[2]))
input = torch.tensor([3, 1, 1, 1, 4, 0], device=device, dtype=dtypes[0])
offsets = torch.tensor([0, 0, 3, 3, 6], device=device, dtype=dtypes[1])
per_sample_weights = torch.randn_like(input, dtype=dtypes[2]) \
.requires_grad_(trainable_scale)
ref_per_sample_weights = \
per_sample_weights.detach().requires_grad_(trainable_scale)
reference_weights = es.weight.detach().requires_grad_()
expected = self._embedding_bag_reference_impl(
input, reference_weights, offsets, mode, ref_per_sample_weights)
result = es(input, offsets, per_sample_weights)
self.assertEqual(result, expected, atol=dtype2prec_DONTUSE[dtypes[2]], rtol=0)
grad = torch.randn_like(expected).to(dtype=dtypes[2], device=device)
result.backward(grad)
expected.backward(grad)
self.assertEqual(es.weight.grad, reference_weights.grad,
atol=dtype2prec_DONTUSE[dtypes[2]], rtol=0)
if trainable_scale:
self.assertEqual(per_sample_weights.grad, ref_per_sample_weights.grad,
atol=dtype2prec_DONTUSE[dtypes[2]], rtol=0)
modes = ('sum',)
trainable_scale = (True, False)
for mode, trainable in itertools.product(modes, trainable_scale):
test_per_sample_weights(mode, trainable)
@skipMeta
@dtypes(*itertools.product((torch.int, torch.long), (torch.int, torch.long),
(torch.float, torch.double, torch.half, torch.bfloat16)))
@dtypesIfCUDA(*itertools.product((torch.int, torch.long), (torch.int, torch.long),
(torch.float, torch.double, torch.half)))
def test_EmbeddingBag_per_sample_weights_and_new_offsets(self, device, dtypes):
def test_per_sample_weights_new_offsets(mode, trainable_scale, include_last_offset, has_weight=True):
es = nn.EmbeddingBag(5, 2, mode=mode, include_last_offset=include_last_offset).to(dtype=dtypes[2], device=device)
es.weight.data.copy_(
torch.arange(1, 11, device=device).view_as(es.weight).to(dtypes[2]))
input = torch.tensor([3, 1, 1, 1, 4, 0], device=device, dtype=dtypes[0])
offsets = torch.tensor([0, 0, 3, 3, 6], device=device, dtype=dtypes[1])
if include_last_offset:
offsets = torch.cat((offsets, torch.tensor([input.size(0)], device=device, dtype=dtypes[1])), 0)
if has_weight:
per_sample_weights = torch.randn_like(input, device=device, dtype=dtypes[2]) \
.requires_grad_(trainable_scale)
ref_per_sample_weights = \
per_sample_weights.detach().requires_grad_(trainable_scale)
else:
per_sample_weights = None
ref_per_sample_weights = None
reference_weights = es.weight.detach().requires_grad_()
expected = self._embedding_bag_reference_impl(
input, reference_weights, offsets, mode, ref_per_sample_weights, include_last_offset)
result = es(input, offsets, per_sample_weights)
self.assertEqual(result, expected, atol=dtype2prec_DONTUSE[dtypes[2]], rtol=0)
grad = torch.randn_like(expected)
result.backward(grad)
expected.backward(grad)
self.assertEqual(es.weight.grad, reference_weights.grad,
atol=dtype2prec_DONTUSE[dtypes[2]], rtol=0)
if has_weight and trainable_scale:
self.assertEqual(per_sample_weights.grad, ref_per_sample_weights.grad,
atol=dtype2prec_DONTUSE[dtypes[2]], rtol=0)
trainable_scale = (True, False)
include_last_offset = (True, False)
modes = (('sum', False), ('sum', True), ('max', False), ('mean', False))
for (mode, has_weight), trainable, include_last_offset in itertools.product(
modes, trainable_scale, include_last_offset
):
test_per_sample_weights_new_offsets(
mode, trainable, include_last_offset, has_weight
)
def _test_EmbeddingBag_vs_Embedding(self, N, D, B, L, max_norm=None,
mode='mean',
device='cpu',
wdtype=torch.float,
dtype=torch.long,
test_per_sample_weights=False,
trainable_per_sample_weights=False,
sparse=False,
test_backward=True,
backward_prec=None):
es = nn.EmbeddingBag(N, D, mode=mode, sparse=sparse, max_norm=max_norm).to(device, wdtype)
e = nn.Embedding(N, D, max_norm=max_norm).to(device, wdtype)
e.weight.data.copy_(es.weight)
input = torch.randint(N, (B, L), device=device, dtype=dtype)
offsets = torch.arange(0, B, device=device, dtype=dtype).mul_(L)
grad_output = torch.rand(B, D, device=device, dtype=wdtype)
if test_per_sample_weights:
# To prevent large gradients, weights should sum to 1 for each bag
per_sample_weights = \
torch.randn(B, L, device=device, dtype=wdtype).softmax(dim=-1)
per_sample_weights_reference = \
per_sample_weights.clone().requires_grad_(trainable_per_sample_weights)
per_sample_weights.requires_grad_(trainable_per_sample_weights)
output = es(input.view(-1), offsets, per_sample_weights.view(-1))
else:
output = es(input.view(-1), offsets)
per_sample_weights = None
per_sample_weights_reference = None
if mode == 'sum':
if test_per_sample_weights:
ref_output = (e(input) * per_sample_weights_reference.unsqueeze(-1)).sum(1)
else:
ref_output = e(input).sum(1)
elif mode == 'mean':
assert not test_per_sample_weights
ref_output = e(input).mean(1)
elif mode == 'max':
assert not test_per_sample_weights
ref_output = e(input).max(1)[0]
self.assertEqual(output, ref_output, atol=dtype2prec_DONTUSE[wdtype], rtol=0)
if not test_backward:
return
output.backward(grad_output)