From b8fc850b4722f8a5f207a5df455d2496e1b87f04 Mon Sep 17 00:00:00 2001 From: Benjamin Berman Date: Thu, 8 Feb 2024 08:36:05 -0800 Subject: [PATCH] Correctly preserves your installed torch when installed like pip install --no-build-isolation git+https://github.com/hiddenswitch/ComfyUI.git --- README.md | 6 +++++ comfy/configs/__init__.py | 0 comfy/ldm/models/diffusion/__init__.py | 0 requirements.txt | 1 - setup.py | 37 +++++++++++++++++++------- 5 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 comfy/configs/__init__.py create mode 100644 comfy/ldm/models/diffusion/__init__.py diff --git a/README.md b/README.md index c79792eca..aca4e4c1d 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,12 @@ On macOS, install exactly Python 3.11 using `brew`, which you can download from ```shell pip install git+https://github.com/hiddenswitch/ComfyUI.git ``` + **Advanced**: If you are running in Google Collab or another environment which has already installed `torch` for you, disable build isolation, and the package will recognize your currently installed torch. + ```shell + # You will need wheel, which isn't included in Python 3.11 or later + pip install wheel + pip install --no-build-isolation git+https://github.com/hiddenswitch/ComfyUI.git + ``` 4. To run the web server: ```shell comfyui diff --git a/comfy/configs/__init__.py b/comfy/configs/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/comfy/ldm/models/diffusion/__init__.py b/comfy/ldm/models/diffusion/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/requirements.txt b/requirements.txt index ccd3abd49..b6a4eb26f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,6 @@ torchaudio torchvision torchdiffeq>=0.2.3 torchsde>=0.2.6 -torchvision einops>=0.6.0 open-clip-torch>=2.16.0 transformers>=4.29.1 diff --git a/setup.py b/setup.py index 334c0ad30..00b439c69 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ import os.path import platform import subprocess import sys -from typing import List +from typing import List, Literal, Union, Optional from pip._internal.index.collector import LinkCollector from pip._internal.index.package_finder import PackageFinder @@ -44,11 +44,21 @@ cpu_torch_index = ("https://download.pytorch.org/whl/cpu", "https://download.pyt # xformers not required for new torch """ -Indicates if we're installing an editable (develop) mode package +Indicates if this is installing an editable (develop) mode package """ is_editable = '--editable' in sys.argv or '-e' in sys.argv or ( 'python' in sys.argv and 'setup.py' in sys.argv and 'develop' in sys.argv) +# If we're installing with no build isolation, we can check if torch is already installed in the environment, and if so, +# go ahead and use the version that is already installed. +is_build_isolated_and_torch_version: Optional[str] +try: + import torch + print(f"comfyui setup.py: torch version was {torch.__version__} and built without build isolation, using this torch instead of upgrading", file=sys.stderr) + is_build_isolated_and_torch_version = torch.__version__ +except Exception as e: + print(f"comfyui setup.py: torch could not be imported because running with build isolation or not installed ({e}), installing torch for your platform", file=sys.stderr) + is_build_isolated_and_torch_version = None def _is_nvidia() -> bool: system = platform.system().lower() @@ -102,6 +112,15 @@ def _is_linux_arm64(): def dependencies() -> List[str]: _dependencies = open(os.path.join(os.path.dirname(__file__), "requirements.txt")).readlines() + # torch is already installed, and we could have only known this if the user specifically requested a + # no-build-isolation build, so the user knows what is going on + if is_build_isolated_and_torch_version is not None: + for i, dep in enumerate(_dependencies): + stripped = dep.strip() + if stripped == "torch": + _dependencies[i] = f"{stripped}=={is_build_isolated_and_torch_version}" + break + return _dependencies _alternative_indices = [amd_torch_index, nvidia_torch_index] session = PipSession() @@ -120,14 +139,14 @@ def dependencies() -> List[str]: if sys.version_info >= (3, 12): # use the nightlies - index_urls = [nightly for (_, nightly) in index_urls] - _alternative_indices = [nightly for (_, nightly) in _alternative_indices] + index_urls_selected = [nightly for (_, nightly) in index_urls] + _alternative_indices_selected = [nightly for (_, nightly) in _alternative_indices] else: - index_urls = [stable for (stable, _) in index_urls] - _alternative_indices = [stable for (stable, _) in _alternative_indices] + index_urls_selected = [stable for (stable, _) in index_urls] + _alternative_indices_selected = [stable for (stable, _) in _alternative_indices] try: # pip 23 - finder = PackageFinder.create(LinkCollector(session, SearchScope([], index_urls, no_index=False)), + finder = PackageFinder.create(LinkCollector(session, SearchScope([], index_urls_selected, no_index=False)), SelectionPreferences(allow_yanked=False, prefer_binary=False, allow_all_prereleases=True)) except: @@ -138,12 +157,12 @@ def dependencies() -> List[str]: allow_all_prereleases=True) , use_deprecated_html5lib=False) except: - raise Exception("upgrade pip with\npip install -U pip") + raise Exception("upgrade pip with\npython -m pip install -U pip") for i, package in enumerate(_dependencies[:]): requirement = InstallRequirement(Requirement(package), comes_from=f"{package_name}=={version}") candidate = finder.find_best_candidate(requirement.name, requirement.specifier) if candidate.best_candidate is not None: - if any([url in candidate.best_candidate.link.url for url in _alternative_indices]): + if any([url in candidate.best_candidate.link.url for url in _alternative_indices_selected]): _dependencies[i] = f"{requirement.name} @ {candidate.best_candidate.link.url}" return _dependencies