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.1 kB

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

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