unary.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 UnaryOpFuzzer(Fuzzer):
  11. def __init__(self, seed, dtype=torch.float32, cuda=False):
  12. super().__init__(
  13. parameters=[
  14. # Dimensionality of x. (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`.
  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. [
  25. FuzzedParameter(
  26. name=f"k_any_{i}",
  27. minval=_MIN_DIM_SIZE,
  28. maxval=_MAX_DIM_SIZE,
  29. distribution="loguniform",
  30. ) for i in range(3)
  31. ],
  32. [
  33. FuzzedParameter(
  34. name=f"k_pow2_{i}",
  35. distribution={size: 1. / len(_POW_TWO_SIZES) for size in _POW_TWO_SIZES}
  36. ) for i in range(3)
  37. ],
  38. [
  39. FuzzedParameter(
  40. name=f"k{i}",
  41. distribution={
  42. ParameterAlias(f"k_any_{i}"): 0.8,
  43. ParameterAlias(f"k_pow2_{i}"): 0.2,
  44. },
  45. strict=True,
  46. ) for i in range(3)
  47. ],
  48. # Steps for `x`. (Benchmarks strided memory access.)
  49. [
  50. FuzzedParameter(
  51. name=f"x_step_{i}",
  52. distribution={1: 0.8, 2: 0.06, 4: 0.06, 8: 0.04, 16: 0.04},
  53. ) for i in range(3)
  54. ],
  55. # Repeatable entropy for downstream applications.
  56. FuzzedParameter(name="random_value", minval=0, maxval=2 ** 32 - 1, distribution="uniform"),
  57. ],
  58. tensors=[
  59. FuzzedTensor(
  60. name="x",
  61. size=("k0", "k1", "k2"),
  62. steps=("x_step_0", "x_step_1", "x_step_2"),
  63. probability_contiguous=0.75,
  64. min_elements=4 * 1024,
  65. max_elements=32 * 1024 ** 2,
  66. max_allocation_bytes=2 * 1024**3, # 2 GB
  67. dim_parameter="dim",
  68. dtype=dtype,
  69. cuda=cuda,
  70. ),
  71. ],
  72. seed=seed,
  73. )