helpers.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """ Layer/Module Helpers
  2. Hacked together by / Copyright 2020 Ross Wightman
  3. """
  4. from itertools import repeat
  5. import collections.abc
  6. # From PyTorch internals
  7. def _ntuple(n):
  8. """Return a function that converts input to an n-tuple.
  9. Scalar values are repeated n times, while iterables are converted to tuples.
  10. Strings are treated as scalars to avoid character-level splitting.
  11. Args:
  12. n: Target tuple length.
  13. Returns:
  14. Function that converts input to n-tuple.
  15. """
  16. def parse(x):
  17. if isinstance(x, collections.abc.Iterable) and not isinstance(x, str):
  18. return tuple(x)
  19. return tuple(repeat(x, n))
  20. return parse
  21. to_1tuple = _ntuple(1)
  22. to_2tuple = _ntuple(2)
  23. to_3tuple = _ntuple(3)
  24. to_4tuple = _ntuple(4)
  25. to_ntuple = _ntuple
  26. def make_divisible(v, divisor=8, min_value=None, round_limit=.9):
  27. """Adjust value to be divisible by a divisor, typically for channel counts.
  28. Rounds to the nearest multiple of divisor while ensuring the result doesn't
  29. fall below min_value or decrease by more than (1 - round_limit).
  30. Args:
  31. v: Value to adjust.
  32. divisor: Target divisor.
  33. min_value: Minimum acceptable value.
  34. round_limit: Prevent decrease beyond this fraction of original value.
  35. Returns:
  36. Adjusted value divisible by divisor.
  37. """
  38. min_value = min_value or divisor
  39. new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
  40. # Make sure that round down does not go down by more than 10%.
  41. if new_v < round_limit * v:
  42. new_v += divisor
  43. return new_v
  44. def extend_tuple(x, n):
  45. """Pad a tuple to length n by repeating the last value.
  46. If input is shorter than n, extends by repeating the last element.
  47. If input is longer than n, truncates to n.
  48. Args:
  49. x: Input value, tuple, or list.
  50. n: Target length.
  51. Returns:
  52. Tuple of length n.
  53. """
  54. if not isinstance(x, (tuple, list)):
  55. x = (x,)
  56. else:
  57. x = tuple(x)
  58. pad_n = n - len(x)
  59. if pad_n <= 0:
  60. return x[:n]
  61. return x + (x[-1],) * pad_n