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.

profiler.py 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import importlib
  2. import sys
  3. from functools import wraps
  4. from typing import Any, Callable, Dict, Tuple, Type
  5. def reraise(tp, value, tb):
  6. try:
  7. if value is None:
  8. value = tp()
  9. if value.__traceback__ is not tb:
  10. raise value.with_traceback(tb)
  11. raise value
  12. finally:
  13. value = None
  14. tb = None
  15. class Profiler:
  16. def __init__(self) -> None:
  17. import cProfile
  18. self.pr = cProfile.Profile()
  19. def __enter__(self):
  20. self.pr.enable()
  21. def __exit__(self, tp, exc, tb):
  22. self.pr.disable()
  23. if tp is not None:
  24. reraise(tp, exc, tb)
  25. import pstats
  26. ps = pstats.Stats(self.pr, stream=sys.stderr).sort_stats('tottime')
  27. ps.print_stats(20)
  28. def wrapper(tp: Type[Profiler]) -> Callable[[], Callable[..., Any]]:
  29. def _inner(func: Callable[..., Any]) -> Callable[..., Any]:
  30. @wraps(func)
  31. def executor(*args: Tuple[Any, ...], **kwargs: Dict[str, Any]) -> Any:
  32. with tp():
  33. return func(*args, **kwargs)
  34. return executor
  35. return _inner
  36. PIPELINE_BASE_MODULE = 'modelscope.pipelines.base'
  37. PIPELINE_BASE_CLASS = 'Pipeline'
  38. def enable():
  39. base = importlib.import_module(PIPELINE_BASE_MODULE)
  40. Pipeline = getattr(base, PIPELINE_BASE_CLASS)
  41. Pipeline.__call__ = wrapper(Profiler)(Pipeline.__call__)