binary.py 4.0 KB

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