setup.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # All rights reserved.
  3. # This source code is licensed under the license found in the
  4. # LICENSE file in the root directory of this source tree.
  5. import os
  6. from setuptools import find_packages, setup
  7. # Package metadata
  8. NAME = "SAM 2"
  9. VERSION = "1.0"
  10. DESCRIPTION = "SAM 2: Segment Anything in Images and Videos"
  11. URL = "https://github.com/facebookresearch/segment-anything-2"
  12. AUTHOR = "Meta AI"
  13. AUTHOR_EMAIL = "segment-anything@meta.com"
  14. LICENSE = "Apache 2.0"
  15. # Read the contents of README file
  16. with open("README.md", "r", encoding="utf-8") as f:
  17. LONG_DESCRIPTION = f.read()
  18. # Required dependencies
  19. REQUIRED_PACKAGES = [
  20. "torch>=2.3.1",
  21. "torchvision>=0.18.1",
  22. "numpy>=1.24.4",
  23. "tqdm>=4.66.1",
  24. "hydra-core>=1.3.2",
  25. "iopath>=0.1.10",
  26. "pillow>=9.4.0",
  27. ]
  28. EXTRA_PACKAGES = {
  29. "demo": ["matplotlib>=3.9.1", "jupyter>=1.0.0", "opencv-python>=4.7.0"],
  30. "dev": ["black==24.2.0", "usort==1.0.2", "ufmt==2.0.0b2"],
  31. }
  32. # By default, we also build the SAM 2 CUDA extension.
  33. # You may turn off CUDA build with `export SAM2_BUILD_CUDA=0`.
  34. BUILD_CUDA = os.getenv("SAM2_BUILD_CUDA", "1") == "1"
  35. # By default, we allow SAM 2 installation to proceed even with build errors.
  36. # You may force stopping on errors with `export SAM2_BUILD_ALLOW_ERRORS=0`.
  37. BUILD_ALLOW_ERRORS = os.getenv("SAM2_BUILD_ALLOW_ERRORS", "1") == "1"
  38. # Catch and skip errors during extension building and print a warning message
  39. # (note that this message only shows up under verbose build mode
  40. # "pip install -v -e ." or "python setup.py build_ext -v")
  41. CUDA_ERROR_MSG = (
  42. "{}\n\n"
  43. "Failed to build the SAM 2 CUDA extension due to the error above. "
  44. "You can still use SAM 2 and it's OK to ignore the error above, although some "
  45. "post-processing functionality may be limited (which doesn't affect the results in most cases; "
  46. "(see https://github.com/facebookresearch/segment-anything-2/blob/main/INSTALL.md).\n"
  47. )
  48. def get_extensions():
  49. if not BUILD_CUDA:
  50. return []
  51. try:
  52. from torch.utils.cpp_extension import CUDAExtension
  53. srcs = ["sam2/csrc/connected_components.cu"]
  54. compile_args = {
  55. "cxx": [],
  56. "nvcc": [
  57. "-DCUDA_HAS_FP16=1",
  58. "-D__CUDA_NO_HALF_OPERATORS__",
  59. "-D__CUDA_NO_HALF_CONVERSIONS__",
  60. "-D__CUDA_NO_HALF2_OPERATORS__",
  61. ],
  62. }
  63. ext_modules = [CUDAExtension("sam2._C", srcs, extra_compile_args=compile_args)]
  64. except Exception as e:
  65. if BUILD_ALLOW_ERRORS:
  66. print(CUDA_ERROR_MSG.format(e))
  67. ext_modules = []
  68. else:
  69. raise e
  70. return ext_modules
  71. try:
  72. from torch.utils.cpp_extension import BuildExtension
  73. class BuildExtensionIgnoreErrors(BuildExtension):
  74. def finalize_options(self):
  75. try:
  76. super().finalize_options()
  77. except Exception as e:
  78. print(CUDA_ERROR_MSG.format(e))
  79. self.extensions = []
  80. def build_extensions(self):
  81. try:
  82. super().build_extensions()
  83. except Exception as e:
  84. print(CUDA_ERROR_MSG.format(e))
  85. self.extensions = []
  86. def get_ext_filename(self, ext_name):
  87. try:
  88. return super().get_ext_filename(ext_name)
  89. except Exception as e:
  90. print(CUDA_ERROR_MSG.format(e))
  91. self.extensions = []
  92. return "_C.so"
  93. cmdclass = {
  94. "build_ext": (
  95. BuildExtensionIgnoreErrors.with_options(no_python_abi_suffix=True)
  96. if BUILD_ALLOW_ERRORS
  97. else BuildExtension.with_options(no_python_abi_suffix=True)
  98. )
  99. }
  100. except Exception as e:
  101. cmdclass = {}
  102. if BUILD_ALLOW_ERRORS:
  103. print(CUDA_ERROR_MSG.format(e))
  104. else:
  105. raise e
  106. # Setup configuration
  107. setup(
  108. name=NAME,
  109. version=VERSION,
  110. description=DESCRIPTION,
  111. long_description=LONG_DESCRIPTION,
  112. long_description_content_type="text/markdown",
  113. url=URL,
  114. author=AUTHOR,
  115. author_email=AUTHOR_EMAIL,
  116. license=LICENSE,
  117. packages=find_packages(exclude="notebooks"),
  118. package_data={"": ["*.yaml"]}, # SAM 2 configuration files
  119. include_package_data=True,
  120. install_requires=REQUIRED_PACKAGES,
  121. extras_require=EXTRA_PACKAGES,
  122. python_requires=">=3.10.0",
  123. ext_modules=get_extensions(),
  124. cmdclass=cmdclass,
  125. )