setup.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/sam2"
  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. "notebooks": [
  30. "matplotlib>=3.9.1",
  31. "jupyter>=1.0.0",
  32. "opencv-python>=4.7.0",
  33. "eva-decord>=0.6.1",
  34. ],
  35. "interactive-demo": [
  36. "Flask>=3.0.3",
  37. "Flask-Cors>=5.0.0",
  38. "av>=13.0.0",
  39. "dataclasses-json>=0.6.7",
  40. "eva-decord>=0.6.1",
  41. "gunicorn>=23.0.0",
  42. "imagesize>=1.4.1",
  43. "pycocotools>=2.0.8",
  44. "strawberry-graphql>=0.243.0",
  45. ],
  46. "dev": [
  47. "black==24.2.0",
  48. "usort==1.0.2",
  49. "ufmt==2.0.0b2",
  50. "fvcore>=0.1.5.post20221221",
  51. "pandas>=2.2.2",
  52. "scikit-image>=0.24.0",
  53. "tensorboard>=2.17.0",
  54. "pycocotools>=2.0.8",
  55. "tensordict>=0.5.0",
  56. "opencv-python>=4.7.0",
  57. "submitit>=1.5.1",
  58. ],
  59. }
  60. # By default, we also build the SAM 2 CUDA extension.
  61. # You may turn off CUDA build with `export SAM2_BUILD_CUDA=0`.
  62. BUILD_CUDA = os.getenv("SAM2_BUILD_CUDA", "1") == "1"
  63. # By default, we allow SAM 2 installation to proceed even with build errors.
  64. # You may force stopping on errors with `export SAM2_BUILD_ALLOW_ERRORS=0`.
  65. BUILD_ALLOW_ERRORS = os.getenv("SAM2_BUILD_ALLOW_ERRORS", "1") == "1"
  66. # Catch and skip errors during extension building and print a warning message
  67. # (note that this message only shows up under verbose build mode
  68. # "pip install -v -e ." or "python setup.py build_ext -v")
  69. CUDA_ERROR_MSG = (
  70. "{}\n\n"
  71. "Failed to build the SAM 2 CUDA extension due to the error above. "
  72. "You can still use SAM 2 and it's OK to ignore the error above, although some "
  73. "post-processing functionality may be limited (which doesn't affect the results in most cases; "
  74. "(see https://github.com/facebookresearch/sam2/blob/main/INSTALL.md).\n"
  75. )
  76. def get_extensions():
  77. if not BUILD_CUDA:
  78. return []
  79. try:
  80. from torch.utils.cpp_extension import CUDAExtension
  81. srcs = ["sam2/csrc/connected_components.cu"]
  82. compile_args = {
  83. "cxx": [],
  84. "nvcc": [
  85. "-DCUDA_HAS_FP16=1",
  86. "-D__CUDA_NO_HALF_OPERATORS__",
  87. "-D__CUDA_NO_HALF_CONVERSIONS__",
  88. "-D__CUDA_NO_HALF2_OPERATORS__",
  89. ],
  90. }
  91. ext_modules = [CUDAExtension("sam2._C", srcs, extra_compile_args=compile_args)]
  92. except Exception as e:
  93. if BUILD_ALLOW_ERRORS:
  94. print(CUDA_ERROR_MSG.format(e))
  95. ext_modules = []
  96. else:
  97. raise e
  98. return ext_modules
  99. try:
  100. from torch.utils.cpp_extension import BuildExtension
  101. class BuildExtensionIgnoreErrors(BuildExtension):
  102. def finalize_options(self):
  103. try:
  104. super().finalize_options()
  105. except Exception as e:
  106. print(CUDA_ERROR_MSG.format(e))
  107. self.extensions = []
  108. def build_extensions(self):
  109. try:
  110. super().build_extensions()
  111. except Exception as e:
  112. print(CUDA_ERROR_MSG.format(e))
  113. self.extensions = []
  114. def get_ext_filename(self, ext_name):
  115. try:
  116. return super().get_ext_filename(ext_name)
  117. except Exception as e:
  118. print(CUDA_ERROR_MSG.format(e))
  119. self.extensions = []
  120. return "_C.so"
  121. cmdclass = {
  122. "build_ext": (
  123. BuildExtensionIgnoreErrors.with_options(no_python_abi_suffix=True)
  124. if BUILD_ALLOW_ERRORS
  125. else BuildExtension.with_options(no_python_abi_suffix=True)
  126. )
  127. }
  128. except Exception as e:
  129. cmdclass = {}
  130. if BUILD_ALLOW_ERRORS:
  131. print(CUDA_ERROR_MSG.format(e))
  132. else:
  133. raise e
  134. # Setup configuration
  135. setup(
  136. name=NAME,
  137. version=VERSION,
  138. description=DESCRIPTION,
  139. long_description=LONG_DESCRIPTION,
  140. long_description_content_type="text/markdown",
  141. url=URL,
  142. author=AUTHOR,
  143. author_email=AUTHOR_EMAIL,
  144. license=LICENSE,
  145. packages=find_packages(exclude="notebooks"),
  146. include_package_data=True,
  147. install_requires=REQUIRED_PACKAGES,
  148. extras_require=EXTRA_PACKAGES,
  149. python_requires=">=3.10.0",
  150. ext_modules=get_extensions(),
  151. cmdclass=cmdclass,
  152. )