setup.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. def get_extensions():
  40. if not BUILD_CUDA:
  41. return []
  42. srcs = ["sam2/csrc/connected_components.cu"]
  43. compile_args = {
  44. "cxx": [],
  45. "nvcc": [
  46. "-DCUDA_HAS_FP16=1",
  47. "-D__CUDA_NO_HALF_OPERATORS__",
  48. "-D__CUDA_NO_HALF_CONVERSIONS__",
  49. "-D__CUDA_NO_HALF2_OPERATORS__",
  50. ],
  51. }
  52. ext_modules = [CUDAExtension("sam2._C", srcs, extra_compile_args=compile_args)]
  53. return ext_modules
  54. class BuildExtensionIgnoreErrors(BuildExtension):
  55. # Catch and skip errors during extension building and print a warning message
  56. # (note that this message only shows up under verbose build mode
  57. # "pip install -v -e ." or "python setup.py build_ext -v")
  58. ERROR_MSG = (
  59. "{}\n\n"
  60. "Failed to build the SAM 2 CUDA extension due to the error above. "
  61. "You can still use SAM 2, but some post-processing functionality may be limited "
  62. "(see https://github.com/facebookresearch/segment-anything-2/blob/main/INSTALL.md).\n"
  63. )
  64. def finalize_options(self):
  65. try:
  66. super().finalize_options()
  67. except Exception as e:
  68. print(self.ERROR_MSG.format(e))
  69. self.extensions = []
  70. def build_extensions(self):
  71. try:
  72. super().build_extensions()
  73. except Exception as e:
  74. print(self.ERROR_MSG.format(e))
  75. self.extensions = []
  76. def get_ext_filename(self, ext_name):
  77. try:
  78. return super().get_ext_filename(ext_name)
  79. except Exception as e:
  80. print(self.ERROR_MSG.format(e))
  81. self.extensions = []
  82. return "_C.so"
  83. # Setup configuration
  84. setup(
  85. name=NAME,
  86. version=VERSION,
  87. description=DESCRIPTION,
  88. long_description=LONG_DESCRIPTION,
  89. long_description_content_type="text/markdown",
  90. url=URL,
  91. author=AUTHOR,
  92. author_email=AUTHOR_EMAIL,
  93. license=LICENSE,
  94. packages=find_packages(exclude="notebooks"),
  95. install_requires=REQUIRED_PACKAGES,
  96. extras_require=EXTRA_PACKAGES,
  97. python_requires=">=3.10.0",
  98. ext_modules=get_extensions(),
  99. cmdclass={
  100. "build_ext": (
  101. BuildExtensionIgnoreErrors.with_options(no_python_abi_suffix=True)
  102. if BUILD_ALLOW_ERRORS
  103. else BuildExtension.with_options(no_python_abi_suffix=True)
  104. ),
  105. },
  106. )