__init__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import sys
  2. import torch
  3. from contextlib import contextmanager
  4. from torch.backends import ContextProp, PropModule, __allow_nonbracketed_mutation
  5. def is_available():
  6. r"""Returns whether PyTorch is built with MKL-DNN support."""
  7. return torch._C.has_mkldnn
  8. VERBOSE_OFF = 0
  9. VERBOSE_ON = 1
  10. VERBOSE_ON_CREATION = 2
  11. class verbose:
  12. """
  13. On-demand oneDNN (former MKL-DNN) verbosing functionality
  14. To make it easier to debug performance issues, oneDNN can dump verbose
  15. messages containing information like kernel size, input data size and
  16. execution duration while executing the kernel. The verbosing functionality
  17. can be invoked via an environment variable named `DNNL_VERBOSE`. However,
  18. this methodology dumps messages in all steps. Those are a large amount of
  19. verbose messages. Moreover, for investigating the performance issues,
  20. generally taking verbose messages for one single iteration is enough.
  21. This on-demand verbosing functionality makes it possible to control scope
  22. for verbose message dumping. In the following example, verbose messages
  23. will be dumped out for the second inference only.
  24. .. highlight:: python
  25. .. code-block:: python
  26. import torch
  27. model(data)
  28. with torch.backends.mkldnn.verbose(torch.backends.mkldnn.VERBOSE_ON):
  29. model(data)
  30. Args:
  31. level: Verbose level
  32. - ``VERBOSE_OFF``: Disable verbosing
  33. - ``VERBOSE_ON``: Enable verbosing
  34. - ``VERBOSE_ON_CREATION``: Enable verbosing, including oneDNN kernel creation
  35. """
  36. def __init__(self, level):
  37. self.level = level
  38. def __enter__(self):
  39. if self.level == VERBOSE_OFF:
  40. return
  41. st = torch._C._verbose.mkldnn_set_verbose(self.level)
  42. assert st, "Failed to set MKLDNN into verbose mode. Please consider to disable this verbose scope."
  43. return self
  44. def __exit__(self, exc_type, exc_val, exc_tb):
  45. torch._C._verbose.mkldnn_set_verbose(VERBOSE_OFF)
  46. return False
  47. def set_flags(_enabled):
  48. orig_flags = (torch._C._get_mkldnn_enabled(),)
  49. torch._C._set_mkldnn_enabled(_enabled)
  50. return orig_flags
  51. @contextmanager
  52. def flags(enabled=False):
  53. with __allow_nonbracketed_mutation():
  54. orig_flags = set_flags(enabled)
  55. try:
  56. yield
  57. finally:
  58. with __allow_nonbracketed_mutation():
  59. set_flags(orig_flags[0])
  60. class MkldnnModule(PropModule):
  61. def __init__(self, m, name):
  62. super().__init__(m, name)
  63. enabled = ContextProp(torch._C._get_mkldnn_enabled, torch._C._set_mkldnn_enabled)
  64. # Cool stuff from torch/backends/cudnn/__init__.py and
  65. # https://stackoverflow.com/questions/2447353/getattr-on-a-module/7668273#7668273
  66. sys.modules[__name__] = MkldnnModule(sys.modules[__name__], __name__)