__init__.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import ctypes
  2. import importlib.util
  3. import logging
  4. import os
  5. import shutil
  6. os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
  7. # https://github.com/pytorch/pytorch/issues/27971#issuecomment-1768868068
  8. os.environ["ONEDNN_PRIMITIVE_CACHE_CAPACITY"] = "1"
  9. os.environ["LRU_CACHE_CAPACITY"] = "1"
  10. # prevent CPU memory leak when run model on GPU
  11. # https://github.com/pytorch/pytorch/issues/98688#issuecomment-1869288431
  12. # https://github.com/pytorch/pytorch/issues/108334#issuecomment-1752763633
  13. os.environ["TORCH_CUDNN_V8_API_LRU_CACHE_LIMIT"] = "1"
  14. import warnings
  15. warnings.simplefilter("ignore", UserWarning)
  16. def fix_window_pytorch():
  17. # copy from: https://github.com/comfyanonymous/ComfyUI/blob/5cbaa9e07c97296b536f240688f5a19300ecf30d/fix_torch.py#L4
  18. import platform
  19. try:
  20. if platform.system() != "Windows":
  21. return
  22. torch_spec = importlib.util.find_spec("torch")
  23. for folder in torch_spec.submodule_search_locations:
  24. lib_folder = os.path.join(folder, "lib")
  25. test_file = os.path.join(lib_folder, "fbgemm.dll")
  26. dest = os.path.join(lib_folder, "libomp140.x86_64.dll")
  27. if os.path.exists(dest):
  28. break
  29. with open(test_file, "rb") as f:
  30. contents = f.read()
  31. if b"libomp140.x86_64.dll" not in contents:
  32. break
  33. try:
  34. mydll = ctypes.cdll.LoadLibrary(test_file)
  35. except FileNotFoundError:
  36. logging.warning("Detected pytorch version with libomp issue, patching.")
  37. shutil.copyfile(os.path.join(lib_folder, "libiomp5md.dll"), dest)
  38. except:
  39. pass
  40. def entry_point():
  41. # To make os.environ["XDG_CACHE_HOME"] = args.model_cache_dir works for diffusers
  42. # https://github.com/huggingface/diffusers/blob/be99201a567c1ccd841dc16fb24e88f7f239c187/src/diffusers/utils/constants.py#L18
  43. from sorawm.iopaint.cli import typer_app
  44. fix_window_pytorch()
  45. typer_app()