-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_jagged_tensor.py
More file actions
2824 lines (2446 loc) · 117 KB
/
test_jagged_tensor.py
File metadata and controls
2824 lines (2446 loc) · 117 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
# Copyright Contributors to the OpenVDB Project
# SPDX-License-Identifier: Apache-2.0
#
import itertools
import tempfile
import unittest
from typing import List
import numpy as np
import torch
from fvdb.types import (
ListOfListsOfTensors,
ListOfTensors,
LShapeRank1,
LShapeRank2,
LShapeSpec,
RShapeSpec,
is_ListOfListsOfTensors,
is_ListOfTensors,
is_LShapeRank1,
is_LShapeRank2,
is_LShapeSpec,
is_RShapeSpec,
)
from fvdb.utils.tests import get_fvdb_test_data_path, probabilistic_test
from parameterized import parameterized
import fvdb
def _scatter_reduce_ref(src, index, dim_size, reduce):
idx = index.view(-1, *([1] * (src.dim() - 1))).expand_as(src) if src.dim() > 1 else index
if reduce == "sum":
out = torch.zeros(dim_size, *src.shape[1:], dtype=src.dtype, device=src.device)
out.scatter_reduce_(0, idx, src, reduce="sum", include_self=True)
elif reduce == "amin":
out = torch.full((dim_size, *src.shape[1:]), float("inf"), dtype=src.dtype, device=src.device)
out.scatter_reduce_(0, idx, src, reduce="amin", include_self=False)
elif reduce == "amax":
out = torch.full((dim_size, *src.shape[1:]), float("-inf"), dtype=src.dtype, device=src.device)
out.scatter_reduce_(0, idx, src, reduce="amax", include_self=False)
return out
all_device_dtype_combos = [
["cuda", torch.float16],
["cuda", torch.bfloat16],
["cpu", torch.bfloat16],
["cpu", torch.float32],
["cuda", torch.float32],
["cpu", torch.float64],
["cuda", torch.float64],
]
NVOX = 10_000
class TestJaggedTensor(unittest.TestCase):
def setUp(self):
torch.random.manual_seed(2024)
np.random.seed(2024)
def mklol(
self,
num_outer,
num_inner_min,
num_inner_max,
device,
dtype,
last_dims=(3, 4),
base_num=1000,
vary_num=10,
empty_prob=0.0,
):
pts_list = []
for _ in range(num_outer):
pts_list_i = []
while len(pts_list_i) == 0:
size = base_num + (np.random.randint(vary_num) if vary_num > 0 else 0)
if np.random.rand() < empty_prob:
size = 0
pts_list_i = [
torch.rand(size, *last_dims, device=device, dtype=dtype)
for _ in range(np.random.randint(num_inner_min, num_inner_max))
]
pts_list.append(pts_list_i)
ret = fvdb.JaggedTensor(pts_list), pts_list
self.assertTrue(ret[0].eshape == [s for s in ret[0].jdata.shape[1:]])
return ret
def mklol_like(self, lol, vary_dim_1=False, vary_dim_2=False):
res = []
shape_1 = lol[0][0].shape[1] + np.random.randint(0, 5) if vary_dim_2 else lol[0][0].shape[1]
for loli in lol:
res_i = []
for lolij in loli:
shape_0 = lolij.shape[0] + np.random.randint(0, 5) if vary_dim_1 else lolij.shape[0]
res_i.append(torch.rand(shape_0, shape_1, device=lolij.device, dtype=lolij.dtype))
res.append(res_i)
return fvdb.JaggedTensor(res), res
def check_lshape(self, jt: fvdb.JaggedTensor, lt: ListOfTensors | ListOfListsOfTensors):
self.assertEqual(len(jt), len(lt))
if jt.ldim == 1:
assert is_ListOfTensors(lt)
for i in range(len(jt)):
self.assertEqual(jt.lshape[i], lt[i].shape[0])
elif jt.ldim == 2:
assert is_ListOfListsOfTensors(lt)
for i, jti in enumerate(jt):
self.assertEqual(len(jti), len(lt[i]))
assert is_LShapeRank2(jt.lshape)
assert is_LShapeRank1(jti.lshape)
for j in range(len(jti)):
assert isinstance(jt.lshape[i], list)
self.assertEqual(jt.lshape[i][j], lt[i][j].shape[0])
else:
assert False, "jagged tensor ldim should be 1 or 2"
@parameterized.expand(all_device_dtype_combos)
def test_jsqueeze_noop(self, device, dtype):
tensor_list = [torch.rand(100 + np.random.randint(10), 3, device=device, dtype=dtype) for _ in range(7)]
jt = fvdb.JaggedTensor(tensor_list)
jt_squeezed = jt.jsqueeze()
self.assertEqual(jt_squeezed.lshape, jt.lshape)
self.assertEqual(jt_squeezed.jdata.shape, jt.jdata.shape)
self.assertTrue(torch.equal(jt_squeezed.jdata, jt.jdata))
self.assertTrue(torch.equal(jt_squeezed.joffsets, jt.joffsets))
self.assertTrue(torch.equal(jt_squeezed.jidx, jt.jidx))
self.assertEqual(jt_squeezed.joffsets.shape, jt.joffsets.shape)
self.assertEqual(jt_squeezed.jidx.shape, jt.jidx.shape)
self.assertEqual(jt_squeezed.device, jt.device)
self.assertEqual(jt_squeezed.dtype, jt.dtype)
self.assertEqual(jt_squeezed.ldim, jt.ldim)
self.assertEqual(jt_squeezed.eshape, jt.eshape)
@parameterized.expand(all_device_dtype_combos)
def test_jsqueeze_noop_list_of_lists(self, device, dtype):
tensor_list = [torch.rand(100 + np.random.randint(10), 3, device=device, dtype=dtype) for _ in range(7)]
jt = fvdb.JaggedTensor([tensor_list, tensor_list])
jt_squeezed = jt.jsqueeze()
self.assertEqual(jt_squeezed.lshape, jt.lshape)
self.assertEqual(jt_squeezed.jdata.shape, jt.jdata.shape)
self.assertTrue(torch.equal(jt_squeezed.jdata, jt.jdata))
self.assertTrue(torch.equal(jt_squeezed.joffsets, jt.joffsets))
self.assertTrue(torch.equal(jt_squeezed.jidx, jt.jidx))
self.assertEqual(jt_squeezed.joffsets.shape, jt.joffsets.shape)
self.assertEqual(jt_squeezed.jidx.shape, jt.jidx.shape)
self.assertEqual(jt_squeezed.device, jt.device)
self.assertEqual(jt_squeezed.dtype, jt.dtype)
self.assertEqual(jt_squeezed.ldim, jt.ldim)
self.assertEqual(jt_squeezed.eshape, jt.eshape)
@parameterized.expand(all_device_dtype_combos)
def test_jsqueeze_empty_list(self, device, dtype):
tensor_list = [torch.rand(0, 1, 3, device=device, dtype=dtype) for _ in range(7)]
jt = fvdb.JaggedTensor(tensor_list)
jt_squeezed = jt.jsqueeze()
self.assertEqual(jt_squeezed.lshape, jt.lshape)
self.assertNotEqual(jt_squeezed.jdata.shape, jt.jdata.shape)
self.assertTrue(torch.equal(jt_squeezed.jdata.unsqueeze(1), jt.jdata))
self.assertTrue(torch.equal(jt_squeezed.jdata, jt.jdata.squeeze()))
self.assertTrue(torch.equal(jt_squeezed.joffsets, jt.joffsets))
self.assertTrue(torch.equal(jt_squeezed.jidx, jt.jidx))
self.assertEqual(jt_squeezed.joffsets.shape, jt.joffsets.shape)
self.assertEqual(jt_squeezed.jidx.shape, jt.jidx.shape)
self.assertEqual(jt_squeezed.device, jt.device)
self.assertEqual(jt_squeezed.dtype, jt.dtype)
self.assertEqual(jt_squeezed.ldim, jt.ldim)
self.assertNotEqual(jt_squeezed.eshape, jt.eshape)
@parameterized.expand(all_device_dtype_combos)
def test_jsqueeze_empty_list_of_lists(self, device, dtype):
tensor_list = [torch.rand(0, 1, 3, device=device, dtype=dtype) for _ in range(7)]
jt = fvdb.JaggedTensor([tensor_list, tensor_list])
jt_squeezed = jt.jsqueeze()
self.assertEqual(jt_squeezed.lshape, jt.lshape)
self.assertNotEqual(jt_squeezed.jdata.shape, jt.jdata.shape)
self.assertTrue(torch.equal(jt_squeezed.jdata.unsqueeze(1), jt.jdata))
self.assertTrue(torch.equal(jt_squeezed.jdata, jt.jdata.squeeze()))
self.assertTrue(torch.equal(jt_squeezed.joffsets, jt.joffsets))
self.assertTrue(torch.equal(jt_squeezed.jidx, jt.jidx))
self.assertEqual(jt_squeezed.joffsets.shape, jt.joffsets.shape)
self.assertEqual(jt_squeezed.jidx.shape, jt.jidx.shape)
self.assertEqual(jt_squeezed.device, jt.device)
self.assertEqual(jt_squeezed.dtype, jt.dtype)
self.assertEqual(jt_squeezed.ldim, jt.ldim)
self.assertNotEqual(jt_squeezed.eshape, jt.eshape)
@parameterized.expand(all_device_dtype_combos)
def test_jsqueeze_simple(self, device, dtype):
tensor_list = [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(7)]
jt = fvdb.JaggedTensor(tensor_list)
jt_squeezed = jt.jsqueeze()
self.assertEqual(jt_squeezed.lshape, jt.lshape)
self.assertNotEqual(jt_squeezed.jdata.shape, jt.jdata.shape)
self.assertTrue(torch.equal(jt_squeezed.jdata.unsqueeze(1), jt.jdata))
self.assertTrue(torch.equal(jt_squeezed.jdata, jt.jdata.squeeze()))
self.assertTrue(torch.equal(jt_squeezed.joffsets, jt.joffsets))
self.assertTrue(torch.equal(jt_squeezed.jidx, jt.jidx))
self.assertEqual(jt_squeezed.joffsets.shape, jt.joffsets.shape)
self.assertEqual(jt_squeezed.jidx.shape, jt.jidx.shape)
self.assertEqual(jt_squeezed.device, jt.device)
self.assertEqual(jt_squeezed.dtype, jt.dtype)
self.assertEqual(jt_squeezed.ldim, jt.ldim)
self.assertNotEqual(jt_squeezed.eshape, jt.eshape)
@parameterized.expand(all_device_dtype_combos)
def test_jsqueeze_simple_list_of_lists(self, device, dtype):
tensor_list = [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(7)]
jt = fvdb.JaggedTensor([tensor_list, tensor_list])
jt_squeezed = jt.jsqueeze()
self.assertEqual(jt_squeezed.lshape, jt.lshape)
self.assertNotEqual(jt_squeezed.jdata.shape, jt.jdata.shape)
self.assertTrue(torch.equal(jt_squeezed.jdata.unsqueeze(1), jt.jdata))
self.assertTrue(torch.equal(jt_squeezed.jdata, jt.jdata.squeeze()))
self.assertTrue(torch.equal(jt_squeezed.joffsets, jt.joffsets))
self.assertTrue(torch.equal(jt_squeezed.jidx, jt.jidx))
self.assertEqual(jt_squeezed.joffsets.shape, jt.joffsets.shape)
self.assertEqual(jt_squeezed.jidx.shape, jt.jidx.shape)
self.assertEqual(jt_squeezed.device, jt.device)
self.assertEqual(jt_squeezed.dtype, jt.dtype)
self.assertEqual(jt_squeezed.ldim, jt.ldim)
self.assertNotEqual(jt_squeezed.eshape, jt.eshape)
@parameterized.expand(all_device_dtype_combos)
def test_jsqueeze_empty_tensors(self, device, dtype):
tensor_list = [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(3)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list += [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(3)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list += [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(3)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list = [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(3)]
jt = fvdb.JaggedTensor(tensor_list)
jt_squeezed = jt.jsqueeze()
self.assertEqual(jt_squeezed.lshape, jt.lshape)
self.assertNotEqual(jt_squeezed.jdata.shape, jt.jdata.shape)
self.assertTrue(torch.equal(jt_squeezed.jdata.unsqueeze(1), jt.jdata))
self.assertTrue(torch.equal(jt_squeezed.jdata, jt.jdata.squeeze()))
self.assertTrue(torch.equal(jt_squeezed.joffsets, jt.joffsets))
self.assertTrue(torch.equal(jt_squeezed.jidx, jt.jidx))
self.assertEqual(jt_squeezed.joffsets.shape, jt.joffsets.shape)
self.assertEqual(jt_squeezed.jidx.shape, jt.jidx.shape)
self.assertEqual(jt_squeezed.device, jt.device)
self.assertEqual(jt_squeezed.dtype, jt.dtype)
self.assertEqual(jt_squeezed.ldim, jt.ldim)
self.assertNotEqual(jt_squeezed.eshape, jt.eshape)
@parameterized.expand(all_device_dtype_combos)
def test_jsqueeze_empty_tensors_list_of_lists(self, device, dtype):
tensor_list = [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(3)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list += [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(3)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list += [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(3)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list += [torch.empty(0, 1, 3, device=device, dtype=dtype) for _ in range(4)]
tensor_list = [torch.rand(100 + np.random.randint(10), 1, 3, device=device, dtype=dtype) for _ in range(3)]
jt = fvdb.JaggedTensor([tensor_list, tensor_list])
jt_squeezed = jt.jsqueeze()
self.assertEqual(jt_squeezed.lshape, jt.lshape)
self.assertNotEqual(jt_squeezed.jdata.shape, jt.jdata.shape)
self.assertTrue(torch.equal(jt_squeezed.jdata.unsqueeze(1), jt.jdata))
self.assertTrue(torch.equal(jt_squeezed.jdata, jt.jdata.squeeze()))
self.assertTrue(torch.equal(jt_squeezed.joffsets, jt.joffsets))
self.assertTrue(torch.equal(jt_squeezed.jidx, jt.jidx))
self.assertEqual(jt_squeezed.joffsets.shape, jt.joffsets.shape)
self.assertEqual(jt_squeezed.jidx.shape, jt.jidx.shape)
self.assertEqual(jt_squeezed.device, jt.device)
self.assertEqual(jt_squeezed.dtype, jt.dtype)
self.assertEqual(jt_squeezed.ldim, jt.ldim)
self.assertNotEqual(jt_squeezed.eshape, jt.eshape)
@parameterized.expand(all_device_dtype_combos)
def test_jcat_along_dim_0_with_one_tensor(self, device, dtype):
batch_size = 1
# Make a point cloud with a random number of points
def get_pc(num_pc_list: list):
pc_list = []
for num_pc in num_pc_list:
pc_list.append(torch.rand((num_pc, 3)).to(device))
return pc_list
num_pc_list = torch.randint(low=50, high=1000, size=(batch_size,), device=device).cpu().tolist()
pc1_tensor_list = get_pc(num_pc_list)
pc2_tensor_list = get_pc(num_pc_list)
pc1_jagged = fvdb.JaggedTensor(pc1_tensor_list)
pc2_jagged = fvdb.JaggedTensor(pc2_tensor_list)
cat_dim = 0
concat_tensor_list = [
torch.cat([pc1_tensor_list[i], pc2_tensor_list[i]], dim=cat_dim) for i in range(batch_size)
]
jagged_from_concat_list = fvdb.JaggedTensor(concat_tensor_list)
jcat_result = fvdb.jcat([pc1_jagged, pc2_jagged], dim=cat_dim)
self.assertTrue(torch.equal(jagged_from_concat_list.jdata, jcat_result.jdata))
@parameterized.expand(all_device_dtype_combos)
def test_pickle(self, device, dtype):
jt, _ = self.mklol(7, 4, 8, device, dtype)
with tempfile.NamedTemporaryFile() as tmp:
torch.save(jt, tmp.name)
jt2 = fvdb.JaggedTensor(impl=torch.load(tmp.name, weights_only=False))
self.assertTrue(torch.all(jt.jdata == jt2.jdata))
self.assertTrue(torch.all(jt.joffsets == jt2.joffsets))
self.assertTrue(torch.all(jt.jidx == jt2.jidx))
self.assertTrue(jt.device == jt2.device)
self.assertTrue(jt.dtype == jt2.dtype)
self.assertEqual(jt.lshape, jt2.lshape)
jt = fvdb.JaggedTensor([torch.randn(100 + np.random.randint(10), 3, 2).to(device).to(dtype) for _ in range(10)])
with tempfile.NamedTemporaryFile() as tmp:
torch.save(jt, tmp.name)
jt2 = fvdb.JaggedTensor(impl=torch.load(tmp.name, weights_only=False))
self.assertTrue(torch.all(jt.jdata == jt2.jdata))
self.assertTrue(torch.all(jt.joffsets == jt2.joffsets))
self.assertTrue(torch.all(jt.jidx == jt2.jidx))
self.assertTrue(jt.device == jt2.device)
self.assertTrue(jt.dtype == jt2.dtype)
self.assertEqual(jt.lshape, jt2.lshape)
jt = fvdb.JaggedTensor([torch.rand(1024, 9, 9, 9)])
with tempfile.NamedTemporaryFile() as tmp:
torch.save(jt, tmp.name)
jt2 = fvdb.JaggedTensor(impl=torch.load(tmp.name, weights_only=False))
self.assertTrue(torch.all(jt.jdata == jt2.jdata))
self.assertTrue(torch.all(jt.joffsets == jt2.joffsets))
self.assertTrue(torch.all(jt.jidx == jt2.jidx))
self.assertTrue(jt.device == jt2.device)
self.assertTrue(jt.dtype == jt2.dtype)
self.assertEqual(jt.lshape, jt2.lshape)
@parameterized.expand(all_device_dtype_combos)
def test_jflatten_list_of_lists(self, device, dtype):
jt1, l1 = self.mklol(7, 4, 8, device, dtype)
jt2, l2 = self.mklol(3, 7, 11, device, dtype)
self.check_lshape(jt1, l1)
self.check_lshape(jt2, l2)
jt3 = jt1.jflatten(dim=0)
lshape1 = jt1.lshape
assert is_LShapeRank2(lshape1)
lshape3 = jt3.lshape
assert is_LShapeRank1(lshape3)
count = 0
for i, inner1 in enumerate(jt1):
for j, inner2 in enumerate(inner1):
self.assertTrue(torch.all(jt3[count].jdata == inner2.jdata))
self.assertEqual(lshape1[i][j], lshape3[count])
count += 1
jt3 = jt1.jflatten(dim=-2)
lshape1 = jt1.lshape
assert is_LShapeRank2(lshape1)
lshape3 = jt3.lshape
assert is_LShapeRank1(lshape3)
count = 0
for i, inner1 in enumerate(jt1):
for j, inner2 in enumerate(inner1):
self.assertTrue(torch.all(jt3[count].jdata == inner2.jdata))
self.assertEqual(lshape1[i][j], lshape3[count])
count += 1
jt4 = jt2.jflatten(dim=1)
lshape2 = jt2.lshape
assert is_LShapeRank2(lshape2)
lshape4 = jt4.lshape
assert is_LShapeRank1(lshape4)
for i, inner1 in enumerate(jt2):
unbound = inner1.unbind()
assert is_ListOfTensors(unbound)
data1 = torch.cat(tuple(unbound), dim=0)
data2 = jt4[i].jdata
self.assertTrue(torch.all(data1 == data2))
self.assertEqual(lshape4[i], np.sum(lshape2[i]))
jt4 = jt2.jflatten(dim=-1)
lshape2 = jt2.lshape
lshape4 = jt4.lshape
for i, inner1 in enumerate(jt2):
unbound = inner1.unbind()
assert is_ListOfTensors(unbound)
data1 = torch.cat(unbound, dim=0)
data2 = jt4[i].jdata
self.assertTrue(torch.all(data1 == data2))
self.assertEqual(lshape4[i], np.sum(lshape2[i]))
with self.assertRaises(IndexError):
jt4 = jt2.jflatten(dim=2)
with self.assertRaises(IndexError):
jt4 = jt2.jflatten(dim=-3)
@parameterized.expand(all_device_dtype_combos)
def test_jflatten_list(self, device, dtype):
jt1 = fvdb.JaggedTensor.from_rand([100, 200, 300, 400, 500, 600, 700, 800], [2, 3, 4])
jt3 = jt1.jflatten(dim=0)
self.assertEqual(len(jt3.lshape), 1)
self.assertEqual(jt3.lshape[0], np.sum(jt1.lshape))
unbound = jt3.unbind()
assert is_ListOfTensors(unbound)
self.assertTrue(torch.all(unbound[0] == jt1.jdata))
jt3 = jt1.jflatten(dim=-1)
self.assertEqual(len(jt3.lshape), 1)
self.assertEqual(jt3.lshape[0], np.sum(jt1.lshape))
unbound = jt3.unbind()
assert is_ListOfTensors(unbound)
self.assertTrue(torch.all(unbound[0] == jt1.jdata))
with self.assertRaises(IndexError):
jt3 = jt1.jflatten(dim=1)
with self.assertRaises(IndexError):
jt3 = jt1.jflatten(dim=-2)
with self.assertRaises(IndexError):
jt3 = jt1.jflatten(dim=2)
@parameterized.expand(all_device_dtype_combos)
def test_concatenation(self, device, dtype):
jt1, l1 = self.mklol(
7,
2,
5,
device,
dtype,
last_dims=(3,),
base_num=1_000_000 if device == "cuda" else 1000,
vary_num=100,
empty_prob=0.0,
)
jt2, _ = self.mklol(
3,
3,
5,
device,
dtype,
last_dims=(3,),
base_num=1_000_000 if device == "cuda" else 1000,
vary_num=100,
empty_prob=0.0,
)
jt3, l3 = self.mklol_like(l1, vary_dim_1=True, vary_dim_2=False)
jt4, l4 = self.mklol_like(l1, vary_dim_1=False, vary_dim_2=True)
self.check_lshape(jt1, l1)
self.check_lshape(jt3, l3)
self.check_lshape(jt4, l4)
with self.assertRaises(Exception):
jtcat = fvdb.jcat([jt1, jt2], dim=0)
with self.assertRaises(Exception):
jtcat = fvdb.jcat([], dim=0)
for dim in [-1, 0, 1]:
jtcat = fvdb.jcat([jt1, jt1], dim=dim)
lcatted = []
for i, jtcati in enumerate(jtcat):
lcatted.append([])
for j, jtcatij in enumerate(jtcati):
cat_ij = torch.cat([l1[i][j], l1[i][j]], dim=dim) # meow
lcatted[-1].append(cat_ij)
self.assertTrue(torch.all(jtcatij.jdata == cat_ij))
jt_to_cat = jt3 if dim == 0 else jt4
jtcat = fvdb.jcat([jt1, jt1, jt_to_cat, jt1, jt_to_cat, jt1], dim=dim)
lcatted = []
for i, jtcati in enumerate(jtcat):
lcatted.append([])
for j, jtcatij in enumerate(jtcati):
t_test = (l3 if dim == 0 else l4)[i][j]
t1ij = l1[i][j]
cat_ij = torch.cat([t1ij, t1ij, t_test, t1ij, t_test, t1ij], dim=dim) # meow
lcatted[-1].append(cat_ij)
self.assertTrue(torch.all(jtcatij.jdata == cat_ij))
jtcat = fvdb.jcat([jt1, jt3 if dim == 0 else jt4, jt1], dim=dim)
lcatted = []
for i, jtcati in enumerate(jtcat):
lcatted.append([])
for j, jtcatij in enumerate(jtcati):
cat_ij = torch.cat([l1[i][j], (l3 if dim == 0 else l4)[i][j], l1[i][j]], dim=dim)
lcatted[-1].append(cat_ij)
self.assertTrue(torch.all(jtcatij.jdata == cat_ij))
self.check_lshape(jtcat, lcatted)
jtcat = fvdb.jcat([jt1, jt1], dim=1)
lcatted = []
for i, jtcati in enumerate(jtcat):
lcatted.append([])
for j, jtcatij in enumerate(jtcati):
cat_ij = torch.cat([l1[i][j], l1[i][j]], dim=1)
lcatted[-1].append(cat_ij)
self.assertTrue(torch.all(jtcatij.jdata == cat_ij))
jtcat = fvdb.jcat([jt1, jt4, jt1], dim=1)
lcatted = []
for i, jtcati in enumerate(jtcat):
lcatted.append([])
for j, jtcatij in enumerate(jtcati):
cat_ij = torch.cat([l1[i][j], l4[i][j], l1[i][j]], dim=1)
lcatted[-1].append(cat_ij)
self.assertTrue(torch.all(jtcatij.jdata == cat_ij))
with self.assertRaises(Exception):
jtcat = fvdb.jcat([jt1, jt1], dim=-2)
with self.assertRaises(Exception):
jtcat = fvdb.jcat([jt1, jt1], dim=2)
with self.assertRaises(IndexError):
jtcat = fvdb.jcat([jt1, jt1], dim=-3)
with self.assertRaises(IndexError):
jtcat = fvdb.jcat([jt1, jt1], dim=3)
@parameterized.expand(all_device_dtype_combos)
def test_jagged_concatenation(self, device, dtype):
jt1, list1 = self.mklol(7, 4, 8, device, dtype)
jt2, list2 = self.mklol(3, 7, 11, device, dtype)
self.check_lshape(jt1, list1)
self.check_lshape(jt2, list2)
jt3 = fvdb.jcat([jt1, jt2], dim=None)
list3 = list1 + list2
self.check_lshape(jt3, list3)
for i, jt3i in enumerate(jt3):
for j, jt3ij in enumerate(jt3i):
self.assertTrue(torch.all(jt3ij.jdata == list3[i][j]))
multi = [self.mklol(np.random.randint(3, 7), 4, 8, device, dtype) for _ in range(10)]
multi_jt = [a[0] for a in multi]
multi_list = [a[1] for a in multi]
ll = []
for l in multi_list:
ll += l
jtl = fvdb.jcat(multi_jt, dim=None)
self.check_lshape(jtl, ll)
for i, jtli in enumerate(jtl):
for j, jtlij in enumerate(jtli):
self.assertTrue(torch.all(jtlij.jdata == ll[i][j]))
# Nesting dimension mismatch
jt4 = fvdb.JaggedTensor([torch.randn(np.random.randint(4, 100), 4, device=device, dtype=dtype)] * 7)
with self.assertRaises(Exception):
_ = fvdb.jcat([jt1, jt4], dim=None)
# Device dimension mismatch
other_device = "cpu" if device == "cuda" else "cuda"
jt4 = jt1.to(other_device)
with self.assertRaises(Exception):
_ = fvdb.jcat([jt1, jt4], dim=None)
# Dtype dimension mismatch
other_dtype = torch.float32 if dtype != torch.float32 else torch.float64
jt4 = jt1.to(other_dtype)
with self.assertRaises(Exception):
_ = fvdb.jcat([jt1, jt4], dim=None)
# Empty list
with self.assertRaises(Exception):
_ = fvdb.jcat([], dim=None)
@parameterized.expand(
[[*l1, *l2] for l1, l2 in itertools.product(all_device_dtype_combos, all_device_dtype_combos)]
)
def test_jagged_like(self, from_device, from_dtype, to_device, to_dtype):
num_grids = np.random.randint(1, 128)
nvox_per_grid = NVOX if from_device == "cuda" else 100
nrand = 10_000 if from_device == "cuda" else 100
pts_list = [
torch.rand(nvox_per_grid + np.random.randint(nrand), 3, device=from_device, dtype=from_dtype)
for _ in range(num_grids)
]
randpts = fvdb.JaggedTensor(pts_list)
featdata = torch.randn(randpts.jdata.shape[0], 32, dtype=to_dtype, device=to_device)
randfeats = randpts.jagged_like(featdata)
self.check_lshape(randpts, pts_list)
self.check_lshape(randfeats, pts_list)
self.assertEqual(randfeats.jdata.shape[0], randpts.jdata.shape[0])
self.assertEqual(randfeats.jdata.shape[0], randpts.jdata.shape[0])
self.assertEqual(randfeats.device, randpts.device) # jagged_like ignore device
self.assertEqual(randpts.dtype, from_dtype)
self.assertEqual(randfeats.dtype, to_dtype)
@parameterized.expand(all_device_dtype_combos)
def test_rmask(self, device, dtype):
num_grids = np.random.randint(1, 128)
nvox_per_grid = NVOX if device == "cuda" else 100
nrand = 10_000 if device == "cuda" else 100
pts_list = [
torch.rand(nvox_per_grid + np.random.randint(nrand), 3, device=device, dtype=dtype)
for _ in range(num_grids)
]
randpts = fvdb.JaggedTensor(pts_list)
self.check_lshape(randpts, pts_list)
mask = torch.rand(randpts.jdata.shape[0], device=device) < 0.5
masked_randpts = randpts.rmask(mask)
masked_list = []
for i, pts in enumerate(pts_list):
maski = mask[randpts.joffsets[i] : randpts.joffsets[i + 1]]
masked_list.append(pts[maski])
self.assertTrue(torch.all(masked_randpts[i].jdata == masked_list[-1]))
self.check_lshape(masked_randpts, masked_list)
self.assertEqual(masked_randpts.jdata.shape[0], mask.sum().item())
@parameterized.expand(all_device_dtype_combos)
def test_jagged_tensor_one_element(self, device, dtype):
if dtype == torch.bfloat16:
self.skipTest("GridBatch.from_points does not support bfloat16")
# Make sure we can pass in JaggedTensors with a single thing explicitly
pts_list = []
while len(pts_list) == 0:
pts_list = [torch.rand(1000 + np.random.randint(10), 3, device=device, dtype=dtype) for _ in range(4)]
randpts = fvdb.JaggedTensor(pts_list)
self.check_lshape(randpts, pts_list)
gridbatch = fvdb.GridBatch.from_points(randpts, voxel_sizes=0.1)
grid = gridbatch[0]
data_path = get_fvdb_test_data_path()
ray_o_path = data_path / "jagged_tensor" / "ray_orig.pt"
ray_d_path = data_path / "jagged_tensor" / "ray_dir.pt"
ray_o = torch.load(ray_o_path, weights_only=True).to(device=device, dtype=dtype)
ray_d = torch.load(ray_d_path, weights_only=True).to(device=device, dtype=dtype)
ray_orig = fvdb.JaggedTensor([ray_o])
ray_dir = fvdb.JaggedTensor([ray_d])
self.check_lshape(ray_orig, [ray_o])
self.check_lshape(ray_dir, [ray_d])
grid.voxels_along_rays(ray_orig, ray_dir, 1)
@parameterized.expand(all_device_dtype_combos)
def test_indexing(self, device, dtype):
if dtype == torch.bfloat16:
self.skipTest("GridBatch.from_points does not support bfloat16")
pts_list: List[torch.Tensor] = []
ijk_list: List[torch.Tensor] = []
while len(pts_list) == 0:
for _ in range(17):
pts = torch.rand(1000 + np.random.randint(10), 3, device=device, dtype=dtype) * 10.0
ijk = fvdb.GridBatch.from_points(fvdb.JaggedTensor([pts]), voxel_sizes=0.5).ijk.jdata
ijk_list.append(ijk)
pts_list.append(pts)
randpts = fvdb.JaggedTensor(pts_list)
gridbatch = fvdb.GridBatch.from_points(randpts, voxel_sizes=0.5)
idx = np.random.randint(len(gridbatch))
self.assertTrue(torch.equal(gridbatch[idx].ijk.jdata, gridbatch.ijk[idx].jdata))
self.check_lshape(gridbatch[idx].ijk, [ijk_list[idx]])
self.check_lshape(gridbatch.ijk[idx], [ijk_list[idx]])
self.assertTrue(torch.equal(gridbatch[-4:-2].ijk.jdata, gridbatch.ijk[-4:-2].jdata))
self.check_lshape(gridbatch[-4:-2].ijk, ijk_list[-4:-2])
self.check_lshape(gridbatch.ijk[-4:-2], ijk_list[-4:-2])
self.assertTrue(torch.equal(gridbatch[4:-3].ijk.jdata, gridbatch.ijk[4:-3].jdata))
self.check_lshape(gridbatch[4:-3].ijk, ijk_list[4:-3])
self.check_lshape(gridbatch.ijk[4:-3], ijk_list[4:-3])
self.assertTrue(torch.equal(gridbatch[-13:8].ijk.jdata, gridbatch.ijk[-13:8].jdata))
self.check_lshape(gridbatch[-13:8].ijk, ijk_list[-13:8])
self.check_lshape(gridbatch.ijk[-13:8], ijk_list[-13:8])
self.assertTrue(torch.equal(gridbatch[-13:8:1].ijk.jdata, gridbatch.ijk[-13:8:1].jdata))
self.check_lshape(gridbatch[-13:8:1].ijk, ijk_list[-13:8:1])
self.check_lshape(gridbatch.ijk[-13:8:1], ijk_list[-13:8:1])
self.assertTrue(torch.equal(gridbatch[9:8:1].ijk.jdata, gridbatch.ijk[9:8:1].jdata))
# An empty grid returns an ijk JaggedTensor with one thing in it so we can't quite compare!
# self.check_lshape(gridbatch[9:8:1].ijk, ijk_list[9:8:1])
self.check_lshape(gridbatch.ijk[9:8:1], ijk_list[9:8:1])
self.assertTrue(torch.equal(gridbatch[9:8:2].ijk.jdata, gridbatch.ijk[9:8:1].jdata))
# An empty grid returns an ijk JaggedTensor with one thing in it so we can't quite compare!
# self.check_lshape(gridbatch[9:8:1].ijk, ijk_list[9:8:1])
self.check_lshape(gridbatch.ijk[9:8:2], ijk_list[9:8:2])
self.assertTrue(torch.equal(gridbatch[-13:8:2].ijk.jdata, gridbatch.ijk[-13:8:2].jdata))
self.check_lshape(gridbatch[-13:8:2].ijk, ijk_list[-13:8:2])
self.check_lshape(gridbatch.ijk[-13:8:2], ijk_list[-13:8:2])
self.assertTrue(torch.equal(gridbatch[4:17:3].ijk.jdata, gridbatch.ijk[4:17:3].jdata))
self.check_lshape(gridbatch[4:17:3].ijk, ijk_list[4:17:3])
self.check_lshape(gridbatch.ijk[4:17:3], ijk_list[4:17:3])
self.assertTrue(torch.equal(gridbatch[4:15:4].ijk.jdata, gridbatch.ijk[4:15:4].jdata))
self.check_lshape(gridbatch[4:15:4].ijk, ijk_list[4:15:4])
self.check_lshape(gridbatch.ijk[4:15:4], ijk_list[4:15:4])
self.assertTrue(torch.equal(gridbatch.ijk.jdata, gridbatch.ijk[...].jdata))
self.check_lshape(gridbatch.ijk, ijk_list)
self.check_lshape(gridbatch.ijk[...], ijk_list)
self.assertTrue(torch.equal(gridbatch[-900:800].ijk.jdata, gridbatch.ijk[-900:800].jdata))
self.check_lshape(gridbatch[-900:800].ijk, ijk_list[-900:800])
self.check_lshape(gridbatch.ijk[-900:800], ijk_list[-900:800])
self.assertTrue(torch.equal(gridbatch[::].ijk.jdata, gridbatch.ijk[::].jdata))
self.check_lshape(gridbatch[::].ijk, ijk_list[::])
self.check_lshape(gridbatch.ijk[::], ijk_list[::])
with self.assertRaises(Exception):
print(gridbatch.ijk[9:8:0])
with self.assertRaises(Exception):
print(gridbatch.ijk[9:8:-1])
with self.assertRaises(Exception):
print(gridbatch.ijk[None])
with self.assertRaises(Exception):
print(gridbatch.ijk[9:8:-1])
with self.assertRaises(Exception):
print(gridbatch.ijk[::-1])
with self.assertRaises(Exception):
print(gridbatch.ijk[::-3])
@parameterized.expand(all_device_dtype_combos)
def test_arithmetic_operators(self, device, dtype):
pts_list = []
while len(pts_list) == 0:
pts_list = [torch.rand(1000 + np.random.randint(10), 3, device=device, dtype=dtype) for _ in range(17)]
randpts = fvdb.JaggedTensor(pts_list)
randpts_b = fvdb.JaggedTensor([torch.rand_like(x) + 1e-5 for x in pts_list])
pts_list_2 = [pts_list[i] for i in torch.randperm(len(pts_list)).tolist()]
randpts_c = fvdb.JaggedTensor(pts_list_2)
self.check_lshape(randpts, pts_list)
self.check_lshape(randpts_b, pts_list)
self.check_lshape(randpts_c, pts_list_2)
# ------------
# Neg
# ------------
res = -randpts
self.assertTrue(torch.allclose(res.jdata, -randpts.jdata))
self.check_lshape(res, pts_list)
# ------------
# Add
# ------------
res = randpts + 2
self.assertTrue(torch.allclose(res.jdata, randpts.jdata + 2))
self.check_lshape(res, pts_list)
res = randpts + 3.14
self.assertTrue(torch.allclose(res.jdata, randpts.jdata + 3.14))
self.check_lshape(res, pts_list)
res = randpts + randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata + randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b + randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata + randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b + randpts_c
fvdb.config.pedantic_error_checking = False
# ------------
# Subtract
# ------------
res = randpts - 3
self.assertTrue(torch.allclose(res.jdata, randpts.jdata - 3))
self.check_lshape(res, pts_list)
res = randpts - randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata - randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b - randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata - randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b - randpts_c
fvdb.config.pedantic_error_checking = False
# ------------
# Multiply
# ------------
res = randpts * 4
self.assertTrue(torch.allclose(res.jdata, randpts.jdata * 4))
self.check_lshape(res, pts_list)
res = randpts * randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata * randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b * randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata * randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b * randpts_c
fvdb.config.pedantic_error_checking = False
# ------------
# Divide
# ------------
res = randpts / randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata / randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b / randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata / randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b / randpts_c
fvdb.config.pedantic_error_checking = False
res = randpts / 5
self.assertTrue(torch.allclose(res.jdata, randpts.jdata / 5))
self.check_lshape(res, pts_list)
# ------------
# Pow
# ------------
res = randpts**randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata**randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b**randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata**randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b**randpts_c
fvdb.config.pedantic_error_checking = False
res = randpts**5
self.assertTrue(torch.allclose(res.jdata, randpts.jdata**5))
self.check_lshape(res, pts_list)
# ------------
# Floor divide
# ------------
res = randpts // randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata // randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b // randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata // randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b // randpts_c
fvdb.config.pedantic_error_checking = False
res = randpts // 6
self.assertTrue(torch.allclose(res.jdata, randpts.jdata // 6))
self.check_lshape(res, pts_list)
# ------------
# Modulo
# ------------
res = randpts % randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata % randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b % randpts_c
if dtype not in (
torch.float16,
torch.bfloat16,
): # Not stable in reduced precision, but not important for this test
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata % randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b % randpts_c
fvdb.config.pedantic_error_checking = False
res = randpts % 5
self.assertTrue(torch.allclose(res.jdata, randpts.jdata % 5))
self.check_lshape(res, pts_list)
# ------------
# Greater than
# ------------
res = randpts > randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata > randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b > randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata > randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b > randpts_c
fvdb.config.pedantic_error_checking = False
res = randpts > 2
self.assertTrue(torch.allclose(res.jdata, randpts.jdata > 2))
self.check_lshape(res, pts_list)
res = randpts > 3.14
self.assertTrue(torch.allclose(res.jdata, randpts.jdata > 3.14))
self.check_lshape(res, pts_list)
# ----------------
# Greater or equal
# ----------------
res = randpts >= randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata >= randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b >= randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata >= randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b >= randpts_c
fvdb.config.pedantic_error_checking = False
res = randpts >= 2
self.assertTrue(torch.allclose(res.jdata, randpts.jdata >= 2))
self.check_lshape(res, pts_list)
res = randpts >= 3.14
self.assertTrue(torch.allclose(res.jdata, randpts.jdata >= 3.14))
self.check_lshape(res, pts_list)
# ------------
# Less than
# ------------
res = randpts < randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata < randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b < randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata < randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b < randpts_c
fvdb.config.pedantic_error_checking = False
res = randpts < 2
self.assertTrue(torch.allclose(res.jdata, randpts.jdata < 2))
self.check_lshape(res, pts_list)
res = randpts < 3.14
self.assertTrue(torch.allclose(res.jdata, randpts.jdata < 3.14))
self.check_lshape(res, pts_list)
# ------------------
# Less than or equal
# ------------------
res = randpts <= randpts_b
self.assertTrue(torch.allclose(res.jdata, randpts.jdata <= randpts_b.jdata))
self.check_lshape(res, pts_list)
res2 = randpts_b <= randpts_c
self.assertTrue(torch.allclose(res2.jdata, randpts_b.jdata <= randpts_c.jdata))
self.check_lshape(res2, pts_list)
fvdb.config.pedantic_error_checking = True
with self.assertRaises(Exception):
res = randpts_b <= randpts_c
fvdb.config.pedantic_error_checking = False
res = randpts <= 2