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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 set_option as _set_option
  12. from ..core._imperative_rt.utils import _set_defrag
  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"""
  28. Get or set the eviction threshold in bytes. It can also be set to a string,
  29. whose formatting supports byte(B), kilobyte(KB), megabyte(MB) and
  30. gigabyte(GB) units.
  31. .. note::
  32. When GPU memory usage exceeds this value, DTR will heuristically select
  33. and evict resident tensors until the amount of used memory falls below
  34. this threshold.
  35. Examples:
  36. .. code-block::
  37. import megengine as mge
  38. mge.dtr.eviction_threshold = "2GB"
  39. """
  40. return _eviction_threshold
  41. @eviction_threshold.setter
  42. def eviction_threshold(mod, value: Union[int, str]):
  43. global _eviction_threshold
  44. if isinstance(value, str):
  45. _eviction_threshold = _str2bytes(value)
  46. elif isinstance(value, int):
  47. _eviction_threshold = value
  48. else:
  49. raise TypeError("`value` should be a str or an int")
  50. _set_option("dtr_eviction_threshold", _eviction_threshold)
  51. @property
  52. def evictee_minimum_size(mod):
  53. r"""
  54. Get or set the memory threshold of tensors in bytes. It can also be set to a
  55. string, whose formatting supports byte(B), kilobyte(KB), megabyte(MB) and
  56. gigabyte(GB) units.
  57. .. note::
  58. Only tensors whose size exceeds this threshold will be added to the
  59. candidate set. A tensor that is not added to the candidate set will
  60. never be evicted during its lifetime.
  61. Examples:
  62. .. code-block::
  63. import megengine as mge
  64. mge.dtr.evictee_minimum_size = "2MB"
  65. """
  66. return _evictee_minimum_size
  67. @evictee_minimum_size.setter
  68. def evictee_minimum_size(mod, value: Union[int, str]):
  69. global _evictee_minimum_size
  70. if isinstance(value, str):
  71. _evictee_minimum_size = _str2bytes(value)
  72. elif isinstance(value, int):
  73. _evictee_minimum_size = value
  74. else:
  75. raise TypeError("`value` should be a str or an int")
  76. _set_option("dtr_evictee_minimum_size", _evictee_minimum_size)
  77. @property
  78. def enable_sqrt_sampling(mod):
  79. r"""
  80. Get or set whether sqrt sampling is allowed. Sqrt sampling means that given
  81. the size of the candidate set is N, only enumerate sqrt(N) tensors. When
  82. the number of tensors is very high, enabling this optimization will speed
  83. up the training.
  84. Examples:
  85. .. code-block::
  86. import megengine as mge
  87. mge.dtr.enable_sqrt_sampling = True
  88. """
  89. return _enable_sqrt_sampling
  90. @enable_sqrt_sampling.setter
  91. def enable_sqrt_sampling(mod, value: bool):
  92. global _enable_sqrt_sampling
  93. _enable_sqrt_sampling = value
  94. _set_option("enable_dtr_sqrt_sampling", _enable_sqrt_sampling)
  95. def enable():
  96. r"""
  97. Enable to record computing path of tensors and to perform DTR policy.
  98. """
  99. _set_defrag(True)
  100. _set_option("enable_dtr_auto_drop", 1)
  101. _set_option("enable_drop", 1)
  102. _set_option("buffer_length", 0)
  103. _set_option("record_computing_path", 1)
  104. def disable():
  105. r"""
  106. Stop recording computing path of tensors and performing DTR policy.
  107. """
  108. _set_defrag(False)
  109. _set_option("enable_dtr_auto_drop", 0)
  110. _set_option("enable_drop", 0)
  111. _set_option("record_computing_path", 0)

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