binary.py 4.0 KB

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