You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from typing import Union
  4. from ..core import set_option as _set_option
  5. from ..core._imperative_rt.core2 import clear_candidates as _clear_candidates
  6. _eviction_threshold = 0
  7. _evictee_minimum_size = 1024 ** 2
  8. _enable_sqrt_sampling = False
  9. def _str2bytes(text: str) -> int:
  10. regex = re.compile(r"(\d+(?:\.\d+)?)\s*([kmg]?b)", re.IGNORECASE)
  11. order = ["b", "kb", "mb", "gb"]
  12. result = regex.findall(text)
  13. if len(result) != 1:
  14. raise ValueError(
  15. "Formatting of `value` only supports bytes(B), kilobyte(KB), megabyte(MB) and gigabyte(GB) units"
  16. )
  17. return int(float(result[0][0]) * 1024 ** order.index(result[0][1].lower()))
  18. @property
  19. def eviction_threshold(mod):
  20. r"""Get or set the eviction threshold in bytes. It can also be set to a string,
  21. whose formatting supports byte(B), kilobyte(KB), megabyte(MB) and
  22. gigabyte(GB) units.
  23. Note:
  24. When GPU memory usage exceeds this value, DTR will heuristically select
  25. and evict resident tensors until the amount of used memory falls below
  26. this threshold.
  27. Examples:
  28. .. code-block::
  29. import megengine as mge
  30. mge.dtr.eviction_threshold = "2GB"
  31. """
  32. return _eviction_threshold
  33. @eviction_threshold.setter
  34. def eviction_threshold(mod, value: Union[int, str]):
  35. global _eviction_threshold
  36. if isinstance(value, str):
  37. _eviction_threshold = _str2bytes(value)
  38. elif isinstance(value, int):
  39. _eviction_threshold = value
  40. else:
  41. raise TypeError("`value` should be a str or an int")
  42. _set_option("dtr_eviction_threshold", _eviction_threshold)
  43. @property
  44. def evictee_minimum_size(mod):
  45. r"""Get or set the memory threshold of tensors in bytes. It can also be set to a
  46. string, whose formatting supports byte(B), kilobyte(KB), megabyte(MB) and
  47. gigabyte(GB) units.
  48. Note:
  49. Only tensors whose size exceeds this threshold will be added to the
  50. candidate set. A tensor that is not added to the candidate set will
  51. never be evicted during its lifetime.
  52. Examples:
  53. .. code-block::
  54. import megengine as mge
  55. mge.dtr.evictee_minimum_size = "2MB"
  56. """
  57. return _evictee_minimum_size
  58. @evictee_minimum_size.setter
  59. def evictee_minimum_size(mod, value: Union[int, str]):
  60. global _evictee_minimum_size
  61. if isinstance(value, str):
  62. _evictee_minimum_size = _str2bytes(value)
  63. elif isinstance(value, int):
  64. _evictee_minimum_size = value
  65. else:
  66. raise TypeError("`value` should be a str or an int")
  67. _set_option("dtr_evictee_minimum_size", _evictee_minimum_size)
  68. @property
  69. def enable_sqrt_sampling(mod):
  70. r"""Get or set whether sqrt sampling is allowed. Sqrt sampling means that given
  71. the size of the candidate set is N, only enumerate sqrt(N) tensors. When
  72. the number of tensors is very high, enabling this optimization will speed
  73. up the training.
  74. Examples:
  75. .. code-block::
  76. import megengine as mge
  77. mge.dtr.enable_sqrt_sampling = True
  78. """
  79. return _enable_sqrt_sampling
  80. @enable_sqrt_sampling.setter
  81. def enable_sqrt_sampling(mod, value: bool):
  82. global _enable_sqrt_sampling
  83. _enable_sqrt_sampling = value
  84. _set_option("enable_dtr_sqrt_sampling", _enable_sqrt_sampling)
  85. def enable():
  86. r"""Enable to record computing path of tensors and to perform DTR policy."""
  87. _set_option("enable_dtr_auto_drop", 1)
  88. _set_option("enable_drop", 1)
  89. _set_option("record_computing_path", 1)
  90. def disable():
  91. r"""Stop recording computing path of tensors and performing DTR policy."""
  92. _set_option("enable_dtr_auto_drop", 0)
  93. _set_option("enable_drop", 0)
  94. _set_option("record_computing_path", 0)
  95. _clear_candidates()