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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 as _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. 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 mod._eviction_threshold
  41. @eviction_threshold.setter
  42. def eviction_threshold(mod, value: Union[int, str]):
  43. if isinstance(value, str):
  44. mod._eviction_threshold = mod._str2bytes(value)
  45. elif isinstance(value, int):
  46. mod._eviction_threshold = value
  47. else:
  48. raise TypeError("`value` should be a str or an int")
  49. _set_option("dtr_eviction_threshold", mod._eviction_threshold)
  50. @mproperty
  51. def evictee_minimum_size(mod):
  52. r"""
  53. Get or set the memory threshold of tensors in bytes. It can also be set to a
  54. string, whose formatting supports byte(B), kilobyte(KB), megabyte(MB) and
  55. gigabyte(GB) units.
  56. .. note::
  57. Only tensors whose size exceeds this threshold will be added to the
  58. candidate set. A tensor that is not added to the candidate set will
  59. never be evicted during its lifetime.
  60. Examples:
  61. .. code-block::
  62. import megengine as mge
  63. mge.dtr.evictee_minimum_size = "2MB"
  64. """
  65. return mod._evictee_minimum_size
  66. @evictee_minimum_size.setter
  67. def evictee_minimum_size(mod, value: Union[int, str]):
  68. if isinstance(value, str):
  69. mod._evictee_minimum_size = mod._str2bytes(value)
  70. elif isinstance(value, int):
  71. mod._evictee_minimum_size = value
  72. else:
  73. raise TypeError("`value` should be a str or an int")
  74. _set_option("dtr_evictee_minimum_size", mod._evictee_minimum_size)
  75. def enable():
  76. r"""
  77. Enable to record computing path of tensors and to perform DTR policy.
  78. """
  79. _set_defrag(True)
  80. _set_option("enable_dtr_auto_drop", 1)
  81. _set_option("enable_drop", 1)
  82. _set_option("buffer_length", 0)
  83. _set_option("record_computing_path", 1)
  84. def disable():
  85. r"""
  86. Stop recording computing path of tensors and performing DTR policy.
  87. """
  88. _set_option("enable_dtr_auto_drop", 0)
  89. _set_option("enable_drop", 0)
  90. _set_option("record_computing_path", 0)

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