sparse_binary.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import numpy as np
  2. import torch
  3. from torch.utils.benchmark import Fuzzer, FuzzedParameter, ParameterAlias, FuzzedSparseTensor
  4. _MIN_DIM_SIZE = 16
  5. _MAX_DIM_SIZE = 16 * 1024 ** 2
  6. _POW_TWO_SIZES = tuple(2 ** i for i in range(
  7. int(np.log2(_MIN_DIM_SIZE)),
  8. int(np.log2(_MAX_DIM_SIZE)) + 1,
  9. ))
  10. class BinaryOpSparseFuzzer(Fuzzer):
  11. def __init__(self, seed, dtype=torch.float32, cuda=False):
  12. super().__init__(
  13. parameters=[
  14. # Dimensionality of x and y. (e.g. 1D, 2D, or 3D.)
  15. FuzzedParameter("dim_parameter", distribution={1: 0.3, 2: 0.4, 3: 0.3}, strict=True),
  16. FuzzedParameter(
  17. name="sparse_dim",
  18. distribution={1: 0.4, 2: 0.4, 3: 0.2},
  19. strict=True
  20. ),
  21. # Shapes for `x` and `y`.
  22. # It is important to test all shapes, however
  23. # powers of two are especially important and therefore
  24. # warrant special attention. This is done by generating
  25. # both a value drawn from all integers between the min and
  26. # max allowed values, and another from only the powers of two
  27. # (both distributions are loguniform) and then randomly
  28. # selecting between the two.
  29. # Moreover, `y` will occasionally have singleton
  30. # dimensions in order to test broadcasting.
  31. [
  32. FuzzedParameter(
  33. name=f"k_any_{i}",
  34. minval=_MIN_DIM_SIZE,
  35. maxval=_MAX_DIM_SIZE,
  36. distribution="loguniform",
  37. ) for i in range(3)
  38. ],
  39. [
  40. FuzzedParameter(
  41. name=f"k_pow2_{i}",
  42. distribution={size: 1. / len(_POW_TWO_SIZES) for size in _POW_TWO_SIZES}
  43. ) for i in range(3)
  44. ],
  45. [
  46. FuzzedParameter(
  47. name=f"k{i}",
  48. distribution={
  49. ParameterAlias(f"k_any_{i}"): 0.8,
  50. ParameterAlias(f"k_pow2_{i}"): 0.2,
  51. },
  52. strict=True,
  53. ) for i in range(3)
  54. ],
  55. [
  56. FuzzedParameter(
  57. name=f"y_k{i}",
  58. distribution={
  59. ParameterAlias(f"k{i}"): 1.0},
  60. strict=True,
  61. ) for i in range(3)
  62. ],
  63. FuzzedParameter(
  64. name="density",
  65. distribution={0.1: 0.4, 0.05: 0.3, 0.01: 0.3},
  66. ),
  67. FuzzedParameter(
  68. name="coalesced",
  69. distribution={True: 0.5, False: 0.5},
  70. ),
  71. # Repeatable entropy for downstream applications.
  72. FuzzedParameter(name="random_value", minval=0, maxval=2 ** 32 - 1, distribution="uniform"),
  73. ],
  74. tensors=[
  75. FuzzedSparseTensor(
  76. name="x",
  77. size=("k0", "k1", "k2"),
  78. dim_parameter="dim_parameter",
  79. sparse_dim="sparse_dim",
  80. density="density",
  81. coalesced="coalesced",
  82. min_elements=4 * 1024,
  83. max_elements=32 * 1024 ** 2,
  84. dtype=dtype,
  85. cuda=cuda,
  86. ),
  87. FuzzedSparseTensor(
  88. name="y",
  89. size=("y_k0", "y_k1", "y_k2"),
  90. dim_parameter="dim_parameter",
  91. sparse_dim="sparse_dim",
  92. density="density",
  93. coalesced="coalesced",
  94. min_elements=4 * 1024,
  95. max_elements=32 * 1024 ** 2,
  96. dtype=dtype,
  97. cuda=cuda,
  98. ),
  99. ],
  100. seed=seed,
  101. )