setup.py 4.0 KB

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