sparse_unary.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 UnaryOpSparseFuzzer(Fuzzer):
  11. def __init__(self, seed, dtype=torch.float32, cuda=False):
  12. super().__init__(
  13. parameters=[
  14. # Sparse dim parameter of x. (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`.
  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. [
  30. FuzzedParameter(
  31. name=f"k_any_{i}",
  32. minval=_MIN_DIM_SIZE,
  33. maxval=_MAX_DIM_SIZE,
  34. distribution="loguniform",
  35. ) for i in range(3)
  36. ],
  37. [
  38. FuzzedParameter(
  39. name=f"k_pow2_{i}",
  40. distribution={size: 1. / len(_POW_TWO_SIZES) for size in _POW_TWO_SIZES}
  41. ) for i in range(3)
  42. ],
  43. [
  44. FuzzedParameter(
  45. name=f"k{i}",
  46. distribution={
  47. ParameterAlias(f"k_any_{i}"): 0.8,
  48. ParameterAlias(f"k_pow2_{i}"): 0.2,
  49. },
  50. strict=True,
  51. ) for i in range(3)
  52. ],
  53. FuzzedParameter(
  54. name="density",
  55. distribution={0.1: 0.4, 0.05: 0.3, 0.01: 0.3},
  56. ),
  57. FuzzedParameter(
  58. name="coalesced",
  59. distribution={True: 0.5, False: 0.5},
  60. ),
  61. FuzzedParameter(name="random_value", minval=0, maxval=2 ** 32 - 1, distribution="uniform"),
  62. ],
  63. tensors=[
  64. FuzzedSparseTensor(
  65. name="x",
  66. size=("k0", "k1", "k2"),
  67. dim_parameter="dim_parameter",
  68. sparse_dim="sparse_dim",
  69. min_elements=4 * 1024,
  70. max_elements=32 * 1024 ** 2,
  71. density="density",
  72. coalesced="coalesced",
  73. dtype=dtype,
  74. cuda=cuda,
  75. ),
  76. ],
  77. seed=seed,
  78. )