1515type_triplets = [cpu_triplet ]
1616if torch .cuda .is_available ():
1717 cuda_triplet = (
18- torch .cuda .LongTensor ,
18+ torch .cuda .IntTensor ,
1919 torch .cuda .DoubleTensor ,
2020 torch .cuda .sparse .DoubleTensor )
2121 type_triplets .append (cuda_triplet )
@@ -312,27 +312,33 @@ def test_shape(di, dj, dk):
312312 test_shape (3000 , 64 , 300 )
313313
314314 def _test_spadd_shape (self , shape_i , shape_v = None ):
315- shape = shape_i + (shape_v or [])
316- x , _ , _ = self ._gen_sparse (len (shape_i ), 10 , shape )
317- y = torch .randn (* shape )
318- r = random .random ()
315+ for is_cuda in [False , True ]:
316+ shape = shape_i + (shape_v or [])
317+ x , _ , _ = self ._gen_sparse (len (shape_i ), 10 , shape , is_cuda )
318+ y = torch .randn (* shape )
319+ if is_cuda :
320+ y = y .cuda ()
321+ r = random .random ()
319322
320- expected = y + r * x .to_dense ()
321- res = torch .add (y , r , x )
323+ expected = y + r * x .to_dense ()
324+ res = torch .add (y , r , x )
322325
323- self .assertEqual (res , expected )
326+ self .assertEqual (res , expected )
324327
325- # Non contiguous dense tensor
326- s = list (shape )
327- s [0 ] = shape [- 1 ]
328- s [- 1 ] = shape [0 ]
329- y = torch .randn (* s ).transpose_ (0 , len (s ) - 1 )
330- r = random .random ()
328+ # Non contiguous dense tensor
329+ s = list (shape )
330+ s [0 ] = shape [- 1 ]
331+ s [- 1 ] = shape [0 ]
332+ y = torch .randn (* s )
333+ if is_cuda :
334+ y = y .cuda ()
335+ y .transpose_ (0 , len (s ) - 1 )
336+ r = random .random ()
331337
332- expected = y + r * x .to_dense ()
333- res = torch .add (y , r , x )
338+ expected = y + r * x .to_dense ()
339+ res = torch .add (y , r , x )
334340
335- self .assertEqual (res , expected )
341+ self .assertEqual (res , expected )
336342
337343 def test_spadd (self ):
338344 self ._test_spadd_shape ([5 , 6 ])
@@ -347,49 +353,50 @@ def test_spadd_hybrid(self):
347353 self ._test_spadd_shape ([5 , 5 , 5 , 5 , 5 , 5 ], [2 ])
348354
349355 def _test_basic_ops_shape (self , shape_i , shape_v = None ):
350- shape = shape_i + (shape_v or [])
351- x1 , _ , _ = self ._gen_sparse (len (shape_i ), 9 , shape )
352- x2 , _ , _ = self ._gen_sparse (len (shape_i ), 12 , shape )
353-
354- y1 = x1 + x2
355- y2 = x1 .clone ()
356- y2 .add_ (x2 )
357- expected = x1 .to_dense () + x2 .to_dense ()
358- self .assertEqual (y1 .to_dense (), expected )
359- self .assertEqual (y2 .to_dense (), expected )
360-
361- y1 = x1 - x2
362- y2 = x1 .clone ()
363- y2 .sub_ (x2 )
364- expected = x1 .to_dense () - x2 .to_dense ()
365- self .assertEqual (y1 .to_dense (), expected )
366- self .assertEqual (y2 .to_dense (), expected )
367-
368- y1 = x1 * x2
369- y2 = x1 .clone ()
370- y2 .mul_ (x2 )
371- expected = x1 .to_dense () * x2 .to_dense ()
372- self .assertEqual (y1 .to_dense (), expected )
373- self .assertEqual (y2 .to_dense (), expected )
374-
375- y1 = x1 * 37.5
376- y2 = x1 .clone ()
377- y2 .mul_ (37.5 )
378- expected = x1 .to_dense () * 37.5
379- self .assertEqual (y1 .to_dense (), expected )
380- self .assertEqual (y2 .to_dense (), expected )
381-
382- y1 = x1 / 37.5
383- y2 = x1 .clone ()
384- y2 .div_ (37.5 )
385- expected = x1 .to_dense () / 37.5
386- self .assertEqual (y1 .to_dense (), expected )
387- self .assertEqual (y2 .to_dense (), expected )
388-
389- y = x1 .clone ()
390- y .zero_ ()
391- expected = torch .zeros (x1 .size ())
392- self .assertEqual (y .to_dense (), expected )
356+ for is_cuda in [False , True ]:
357+ shape = shape_i + (shape_v or [])
358+ x1 , _ , _ = self ._gen_sparse (len (shape_i ), 9 , shape , is_cuda )
359+ x2 , _ , _ = self ._gen_sparse (len (shape_i ), 12 , shape , is_cuda )
360+
361+ y1 = x1 + x2
362+ y2 = x1 .clone ()
363+ y2 .add_ (x2 )
364+ expected = x1 .to_dense () + x2 .to_dense ()
365+ self .assertEqual (y1 .to_dense (), expected )
366+ self .assertEqual (y2 .to_dense (), expected )
367+
368+ y1 = x1 - x2
369+ y2 = x1 .clone ()
370+ y2 .sub_ (x2 )
371+ expected = x1 .to_dense () - x2 .to_dense ()
372+ self .assertEqual (y1 .to_dense (), expected )
373+ self .assertEqual (y2 .to_dense (), expected )
374+
375+ y1 = x1 * x2
376+ y2 = x1 .clone ()
377+ y2 .mul_ (x2 )
378+ expected = x1 .to_dense () * x2 .to_dense ()
379+ self .assertEqual (y1 .to_dense (), expected )
380+ self .assertEqual (y2 .to_dense (), expected )
381+
382+ y1 = x1 * 37.5
383+ y2 = x1 .clone ()
384+ y2 .mul_ (37.5 )
385+ expected = x1 .to_dense () * 37.5
386+ self .assertEqual (y1 .to_dense (), expected )
387+ self .assertEqual (y2 .to_dense (), expected )
388+
389+ y1 = x1 / 37.5
390+ y2 = x1 .clone ()
391+ y2 .div_ (37.5 )
392+ expected = x1 .to_dense () / 37.5
393+ self .assertEqual (y1 .to_dense (), expected )
394+ self .assertEqual (y2 .to_dense (), expected )
395+
396+ y = x1 .clone ()
397+ y .zero_ ()
398+ expected = torch .zeros (x1 .size ())
399+ self .assertEqual (y .to_dense (), expected )
393400
394401 def test_basic_ops (self ):
395402 self ._test_basic_ops_shape ([5 , 6 ])
@@ -403,6 +410,59 @@ def test_basic_ops_hybrid(self):
403410 self ._test_basic_ops_shape ([50 , 30 , 20 ], [2 ])
404411 self ._test_basic_ops_shape ([5 , 5 , 5 , 5 , 5 , 5 ], [2 ])
405412
413+ def _test_sparse_mask_shape (self , shape_i , shape_v = None ):
414+ for is_cuda in [False , True ]:
415+ shape = shape_i + (shape_v or [])
416+ x1 , _ , _ = self ._gen_sparse (len (shape_i ), 9 , shape , is_cuda )
417+ x2 , _ , _ = self ._gen_sparse (len (shape_i ), 12 , shape , is_cuda )
418+
419+ y1 = x1 + x2
420+ y2 = x1 .clone ()
421+ y2 .add_ (x2 )
422+ expected = x1 .to_dense () + x2 .to_dense ()
423+ self .assertEqual (y1 .to_dense (), expected )
424+ self .assertEqual (y2 .to_dense (), expected )
425+
426+ def test_sparse_mask (self ):
427+ for IndexTensor , ValueTensor , SparseTensor in type_triplets :
428+ i = IndexTensor ([
429+ [1 , 3 , 3 , 0 , 4 ],
430+ [2 , 1 , 1 , 2 , 3 ],
431+ ])
432+ v = ValueTensor ([1 , 2 , 3 , 4 , 5 ])
433+ x = SparseTensor (i , v , torch .Size ([5 , 4 ]))
434+ dense = ValueTensor ([
435+ [1 , 2 , 3 , 4 ],
436+ [5 , 6 , 7 , 8 ],
437+ [9 , 10 , 11 , 12 ],
438+ [13 , 14 , 15 , 16 ],
439+ [17 , 18 , 19 , 20 ],
440+ ])
441+ exp_v = ValueTensor ([7 , 14 , 14 , 3 , 20 ])
442+ expected = SparseTensor (i , exp_v , torch .Size ([5 , 4 ]))
443+ res = dense .sparse_mask (x )
444+ self .assertEqual (res , expected )
445+
446+ def test_sparse_mask_hybrid (self ):
447+ for IndexTensor , ValueTensor , SparseTensor in type_triplets :
448+ i = IndexTensor ([
449+ [1 , 3 , 3 , 0 , 4 ],
450+ [2 , 1 , 1 , 2 , 3 ],
451+ ])
452+ v = ValueTensor ([[1 , 2 ], [2 , 3 ], [3 , 4 ], [4 , 5 ], [5 , 6 ]])
453+ x = SparseTensor (i , v , torch .Size ([5 , 4 , 2 ]))
454+ dense = ValueTensor ([
455+ [[1 , 3 ], [2 , 2 ], [3 , 3 ], [4 , 2 ]],
456+ [[5 , 7 ], [6 , 7 ], [7 , 9 ], [8 , 9 ]],
457+ [[9 , 2 ], [10 , 4 ], [11 , 1 ], [12 , 3 ]],
458+ [[13 , 5 ], [14 , 1 ], [15 , 1 ], [16 , 6 ]],
459+ [[17 , 7 ], [18 , 2 ], [19 , 7 ], [20 , 1 ]],
460+ ])
461+ exp_v = ValueTensor ([[7 , 9 ], [14 , 1 ], [14 , 1 ], [3 , 3 ], [20 , 1 ]])
462+ expected = SparseTensor (i , exp_v , torch .Size ([5 , 4 , 2 ]))
463+ res = dense .sparse_mask (x )
464+ self .assertEqual (res , expected )
465+
406466
407467if __name__ == '__main__' :
408468 run_tests ()
0 commit comments