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 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 mprop import mproperty
  12. from .core._imperative_rt.core2 import set_option
  13. from .core._imperative_rt.utils import _set_defrag
  14. _eviction_threshold = 0
  15. _evictee_minimum_size = 1024 ** 2
  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. @mproperty
  26. def eviction_threshold(mod):
  27. r"""
  28. Returns the eviction threshold in bytes.
  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. """
  34. return mod._eviction_threshold
  35. @eviction_threshold.setter
  36. def eviction_threshold(mod, value: Union[int, str]):
  37. r"""
  38. Change the eviction threshold. If `value` is an int, it represents the
  39. number of bytes. If `value` is a string, its formatting supports bytes(B),
  40. kilobyte(KB), megabyte(MB) and gigabyte(GB) units.
  41. Examples:
  42. .. code-block::
  43. import megengine as mge
  44. mge.dtr.eviction_threshold = 2 * 1024 ** 3
  45. mge.dtr.eviction_threshold = "2GB"
  46. mge.dtr.eviction_threshold = "2048MB"
  47. """
  48. if isinstance(value, str):
  49. mod._eviction_threshold = mod.str2bytes(value)
  50. elif isinstance(value, int):
  51. mod._eviction_threshold = value
  52. else:
  53. raise TypeError("`value` should be a str or an int")
  54. set_option("dtr_eviction_threshold", mod._eviction_threshold)
  55. @mproperty
  56. def evictee_minimum_size(mod):
  57. r"""
  58. Returns the memory threshold of tensors in bytes.
  59. .. note::
  60. Only tensors whose size exceeds this threshold will be added to the
  61. candidate set. A tensor that is not added to the candidate set will
  62. never be evicted during its lifetime.
  63. """
  64. return mod._evictee_minimum_size
  65. @evictee_minimum_size.setter
  66. def evictee_minimum_size(mod, value: Union[int, str]):
  67. r"""
  68. Change the memory threshold of tensors. If `value` is an int, it represents
  69. the number of bytes. If `value` is a string, its formatting supports bytes(B),
  70. kilobyte(KB), megabyte(MB) and gigabyte(GB) units.
  71. Examples:
  72. .. code-block::
  73. import megengine as mge
  74. mge.dtr.evictee_minimum_size = 2 * 1024 ** 2
  75. mge.dtr.evictee_minimum_size = "2MB"
  76. mge.dtr.evictee_minimum_size = "2048KB"
  77. """
  78. if isinstance(value, str):
  79. mod._evictee_minimum_size = mod.str2bytes(value)
  80. elif isinstance(value, int):
  81. mod._evictee_minimum_size = value
  82. else:
  83. raise TypeError("`value` should be a str or an int")
  84. set_option("dtr_evictee_minimum_size", mod._evictee_minimum_size)
  85. def enable():
  86. r"""
  87. Enable to record computing path of tensors and to perform DTR policy.
  88. """
  89. _set_defrag(True)
  90. set_option("enable_dtr_auto_drop", 1)
  91. set_option("enable_drop", 1)
  92. set_option("buffer_length", 0)
  93. set_option("record_computing_path", 1)
  94. def disable():
  95. r"""
  96. Stop recording computing path of tensors and performing DTR policy.
  97. """
  98. set_option("enable_dtr_auto_drop", 0)
  99. set_option("enable_drop", 0)
  100. set_option("record_computing_path", 0)

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