jit.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """ JIT scripting/tracing utils
  2. Hacked together by / Copyright 2020 Ross Wightman
  3. """
  4. import os
  5. import torch
  6. def set_jit_legacy():
  7. """ Set JIT executor to legacy w/ support for op fusion
  8. This is hopefully a temporary need in 1.5/1.5.1/1.6 to restore performance due to changes
  9. in the JIT executor. These API are not supported so could change.
  10. """
  11. #
  12. assert hasattr(torch._C, '_jit_set_profiling_executor'), "Old JIT behavior doesn't exist!"
  13. torch._C._jit_set_profiling_executor(False)
  14. torch._C._jit_set_profiling_mode(False)
  15. torch._C._jit_override_can_fuse_on_gpu(True)
  16. #torch._C._jit_set_texpr_fuser_enabled(True)
  17. def set_jit_fuser(fuser):
  18. if fuser == "te":
  19. # default fuser should be == 'te'
  20. torch._C._jit_set_profiling_executor(True)
  21. torch._C._jit_set_profiling_mode(True)
  22. torch._C._jit_override_can_fuse_on_cpu(False)
  23. torch._C._jit_override_can_fuse_on_gpu(True)
  24. torch._C._jit_set_texpr_fuser_enabled(True)
  25. try:
  26. torch._C._jit_set_nvfuser_enabled(False)
  27. except Exception:
  28. pass
  29. elif fuser == "old" or fuser == "legacy":
  30. torch._C._jit_set_profiling_executor(False)
  31. torch._C._jit_set_profiling_mode(False)
  32. torch._C._jit_override_can_fuse_on_gpu(True)
  33. torch._C._jit_set_texpr_fuser_enabled(False)
  34. try:
  35. torch._C._jit_set_nvfuser_enabled(False)
  36. except Exception:
  37. pass
  38. elif fuser == "nvfuser" or fuser == "nvf":
  39. os.environ['PYTORCH_NVFUSER_DISABLE_FALLBACK'] = '1'
  40. #os.environ['PYTORCH_NVFUSER_DISABLE_FMA'] = '1'
  41. #os.environ['PYTORCH_NVFUSER_JIT_OPT_LEVEL'] = '0'
  42. torch._C._jit_set_texpr_fuser_enabled(False)
  43. torch._C._jit_set_profiling_executor(True)
  44. torch._C._jit_set_profiling_mode(True)
  45. torch._C._jit_can_fuse_on_cpu()
  46. torch._C._jit_can_fuse_on_gpu()
  47. torch._C._jit_override_can_fuse_on_cpu(False)
  48. torch._C._jit_override_can_fuse_on_gpu(False)
  49. torch._C._jit_set_nvfuser_guard_mode(True)
  50. torch._C._jit_set_nvfuser_enabled(True)
  51. else:
  52. assert False, f"Invalid jit fuser ({fuser})"