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.

dtr.py 4.2 kB

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

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台