IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

61 linhas
2.0KB

  1. from icosagon.fastconv import _sparse_diag_cat, \
  2. _cat
  3. from icosagon.data import _equal
  4. import torch
  5. def test_sparse_diag_cat_01():
  6. matrices = [ torch.rand(5, 10).round() for _ in range(7) ]
  7. ground_truth = torch.zeros(35, 70)
  8. ground_truth[0:5, 0:10] = matrices[0]
  9. ground_truth[5:10, 10:20] = matrices[1]
  10. ground_truth[10:15, 20:30] = matrices[2]
  11. ground_truth[15:20, 30:40] = matrices[3]
  12. ground_truth[20:25, 40:50] = matrices[4]
  13. ground_truth[25:30, 50:60] = matrices[5]
  14. ground_truth[30:35, 60:70] = matrices[6]
  15. res = _sparse_diag_cat([ m.to_sparse() for m in matrices ])
  16. res = res.to_dense()
  17. assert torch.all(res == ground_truth)
  18. def test_sparse_diag_cat_02():
  19. x = [ torch.rand(5, 10).round() for _ in range(7) ]
  20. a = [ m.to_sparse() for m in x ]
  21. a = _sparse_diag_cat(a)
  22. b = torch.rand(70, 64)
  23. res = torch.sparse.mm(a, b)
  24. ground_truth = torch.zeros(35, 64)
  25. ground_truth[0:5, :] = torch.mm(x[0], b[0:10])
  26. ground_truth[5:10, :] = torch.mm(x[1], b[10:20])
  27. ground_truth[10:15, :] = torch.mm(x[2], b[20:30])
  28. ground_truth[15:20, :] = torch.mm(x[3], b[30:40])
  29. ground_truth[20:25, :] = torch.mm(x[4], b[40:50])
  30. ground_truth[25:30, :] = torch.mm(x[5], b[50:60])
  31. ground_truth[30:35, :] = torch.mm(x[6], b[60:70])
  32. assert torch.all(res == ground_truth)
  33. def test_cat_01():
  34. matrices = [ torch.rand(5, 10) for _ in range(7) ]
  35. res = _cat(matrices)
  36. assert res.shape == (35, 10)
  37. assert not res.is_sparse
  38. ground_truth = torch.zeros(35, 10)
  39. for i in range(7):
  40. ground_truth[i*5:(i+1)*5, :] = matrices[i]
  41. assert torch.all(res == ground_truth)
  42. def test_cat_02():
  43. matrices = [ torch.rand(5, 10) for _ in range(7) ]
  44. ground_truth = torch.zeros(35, 10)
  45. for i in range(7):
  46. ground_truth[i*5:(i+1)*5, :] = matrices[i]
  47. res = _cat([ m.to_sparse() for m in matrices ])
  48. assert res.shape == (35, 10)
  49. assert res.is_sparse
  50. assert torch.all(res.to_dense() == ground_truth)