_utils_internal.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import os
  2. import sys
  3. import tempfile
  4. # this arbitrary-looking assortment of functionality is provided here
  5. # to have a central place for overrideable behavior. The motivating
  6. # use is the FB build environment, where this source file is replaced
  7. # by an equivalent.
  8. if sys.executable == "torch_deploy":
  9. # __file__ is meaningless in the context of frozen torch used in torch deploy.
  10. # setting empty torch_parent should allow below functions to operate without crashing,
  11. # but it's unclear if there is a valid use case for them in the context of deploy.
  12. torch_parent = ""
  13. else:
  14. if os.path.basename(os.path.dirname(__file__)) == "shared":
  15. torch_parent = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
  16. else:
  17. torch_parent = os.path.dirname(os.path.dirname(__file__))
  18. def get_file_path(*path_components: str) -> str:
  19. return os.path.join(torch_parent, *path_components)
  20. def get_file_path_2(*path_components: str) -> str:
  21. return os.path.join(*path_components)
  22. def get_writable_path(path: str) -> str:
  23. if os.access(path, os.W_OK):
  24. return path
  25. return tempfile.mkdtemp(suffix=os.path.basename(path))
  26. def prepare_multiprocessing_environment(path: str) -> None:
  27. pass
  28. def resolve_library_path(path: str) -> str:
  29. return os.path.realpath(path)
  30. TEST_MASTER_ADDR = "127.0.0.1"
  31. TEST_MASTER_PORT = 29500
  32. # USE_GLOBAL_DEPS controls whether __init__.py tries to load
  33. # libtorch_global_deps, see Note [Global dependencies]
  34. USE_GLOBAL_DEPS = True
  35. # USE_RTLD_GLOBAL_WITH_LIBTORCH controls whether __init__.py tries to load
  36. # _C.so with RTLD_GLOBAL during the call to dlopen.
  37. USE_RTLD_GLOBAL_WITH_LIBTORCH = False