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.

utils.py 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import os
  2. import sys
  3. import __main__
  4. from functools import wraps
  5. import inspect
  6. from inspect import ismethod
  7. import functools
  8. from copy import deepcopy
  9. from io import StringIO
  10. import time
  11. import numpy as np
  12. from fastNLP.envs.env import FASTNLP_GLOBAL_RANK
  13. from fastNLP.core.drivers.utils import distributed_open_proc
  14. from fastNLP.core.log import logger
  15. def get_class_that_defined_method(meth):
  16. if isinstance(meth, functools.partial):
  17. return get_class_that_defined_method(meth.func)
  18. if inspect.ismethod(meth) or (inspect.isbuiltin(meth) and getattr(meth, '__self__', None) is not None and getattr(meth.__self__, '__class__', None)):
  19. for cls in inspect.getmro(meth.__self__.__class__):
  20. if meth.__name__ in cls.__dict__:
  21. return cls
  22. meth = getattr(meth, '__func__', meth) # fallback to __qualname__ parsing
  23. if inspect.isfunction(meth):
  24. cls = getattr(inspect.getmodule(meth),
  25. meth.__qualname__.split('.<locals>', 1)[0].rsplit('.', 1)[0],
  26. None)
  27. if isinstance(cls, type):
  28. return cls
  29. return getattr(meth, '__objclass__', None) # handle special descriptor objects
  30. def recover_logger(fn):
  31. @wraps(fn)
  32. def wrapper(*args, **kwargs):
  33. # 保存logger的状态
  34. handlers = [handler for handler in logger.handlers]
  35. level = logger.level
  36. res = fn(*args, **kwargs)
  37. logger.handlers = handlers
  38. logger.setLevel(level)
  39. return res
  40. return wrapper
  41. def magic_argv_env_context(fn):
  42. @wraps(fn)
  43. def wrapper(*args, **kwargs):
  44. command = deepcopy(sys.argv)
  45. env = deepcopy(os.environ.copy())
  46. used_args = []
  47. for each_arg in sys.argv[1:]:
  48. if "test" not in each_arg:
  49. used_args.append(each_arg)
  50. pytest_current_test = os.environ.get('PYTEST_CURRENT_TEST')
  51. try:
  52. l_index = pytest_current_test.index("[")
  53. r_index = pytest_current_test.index("]")
  54. subtest = pytest_current_test[l_index: r_index + 1]
  55. except:
  56. subtest = ""
  57. if not ismethod(fn) and get_class_that_defined_method(fn) is None:
  58. sys.argv = [sys.argv[0], f"{os.path.abspath(sys.modules[fn.__module__].__file__)}::{fn.__name__}{subtest}"] + used_args
  59. else:
  60. sys.argv = [sys.argv[0], f"{os.path.abspath(sys.modules[fn.__module__].__file__)}::{get_class_that_defined_method(fn).__name__}::{fn.__name__}{subtest}"] + used_args
  61. res = fn(*args, **kwargs)
  62. sys.argv = deepcopy(command)
  63. os.environ = env
  64. return res
  65. return wrapper
  66. class Capturing(list):
  67. # 用来捕获当前环境中的stdout和stderr,会将其中stderr的输出拼接在stdout的输出后面
  68. """
  69. 使用例子
  70. with Capturing() as output:
  71. do_something
  72. assert 'xxx' in output[0]
  73. """
  74. def __init__(self, no_del=False):
  75. # 如果no_del为True,则不会删除_stringio,和_stringioerr
  76. super().__init__()
  77. self.no_del = no_del
  78. def __enter__(self):
  79. self._stdout = sys.stdout
  80. self._stderr = sys.stderr
  81. sys.stdout = self._stringio = StringIO()
  82. sys.stderr = self._stringioerr = StringIO()
  83. return self
  84. def __exit__(self, *args):
  85. self.append(self._stringio.getvalue() + self._stringioerr.getvalue())
  86. if not self.no_del:
  87. del self._stringio, self._stringioerr # free up some memory
  88. sys.stdout = self._stdout
  89. sys.stderr = self._stderr
  90. def re_run_current_cmd_for_torch(num_procs, output_from_new_proc='ignore'):
  91. # Script called as `python a/b/c.py`
  92. if int(os.environ.get('LOCAL_RANK', '0')) == 0:
  93. if __main__.__spec__ is None: # pragma: no-cover
  94. # pull out the commands used to run the script and resolve the abs file path
  95. command = sys.argv
  96. command[0] = os.path.abspath(command[0])
  97. # use the same python interpreter and actually running
  98. command = [sys.executable] + command
  99. # Script called as `python -m a.b.c`
  100. else:
  101. command = [sys.executable, "-m", __main__.__spec__._name] + sys.argv[1:]
  102. for rank in range(1, num_procs+1):
  103. env_copy = os.environ.copy()
  104. env_copy["LOCAL_RANK"] = f"{rank}"
  105. env_copy['WOLRD_SIZE'] = f'{num_procs+1}'
  106. env_copy['RANK'] = f'{rank}'
  107. # 如果是多机,一定需要用户自己拉起,因此我们自己使用 open_subprocesses 开启的进程的 FASTNLP_GLOBAL_RANK 一定是 LOCAL_RANK;
  108. env_copy[FASTNLP_GLOBAL_RANK] = str(rank)
  109. proc = distributed_open_proc(output_from_new_proc, command, env_copy, None)
  110. delay = np.random.uniform(1, 5, 1)[0]
  111. time.sleep(delay)