Skip to content

v4: Restore the PyTorch builds that were installed before a nodepack - #3618

Open
ltdrdata wants to merge 1 commit into
manager-v4from
fix/v4-torch-restore
Open

ltdrdata wants to merge 1 commit into
manager-v4from
fix/v4-torch-restore

Conversation

@ltdrdata

@ltdrdata ltdrdata commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Intent and scope

When a nodepack changed torch or torchvision, PIPFixer.fix_broken() runs the PyTorch restore, torch_rollback(). It chose what to install from the torch version from before. For a torch without a local label it sent pip install --force with the package names alone, which installs the newest releases and which uv refuses. For a torch with a label it installed the versions of a table that ends at 2.7.0, and for a version that the table lacks, a nightly build. A nodepack that removed torch, or removed torchvision and left torch as it was, ended the restore in a KeyError. After this change the restore installs exactly the builds that were installed before the nodepack, or it installs no other build in their place and logs the failure.

  • Applies to: torch_rollback() and PIPFixer.fix_broken() in comfyui_manager/common/manager_util.py, which comfyui_manager/prestartup_script.py, the manager_core.py of both servers and cm_cli/__main__.py call. pip and uv; uv is the default on this line.
  • Accepted:
    • The restore runs when torch or torchvision is not the build it was before the nodepack, a removed one included. A change of torchaudio alone starts none, as before.
    • Each of torch, torchvision and torchaudio that changed is installed again as <name>===<build>, local label included. Packages that the nodepack left alone are not named.
    • A command names the PyTorch index of its build with --extra-index-url: whl/<label> for a release with a label, whl/nightly/<label> with --pre for a development build, no index for a release without a label.
    • torch is installed first, with its dependencies. The CUDA libraries that a torch of another CUDA version overwrote are installed again.
    • torchvision and torchaudio follow without their dependencies, so that one that did not fit torch before the nodepack is put back as it was and does not move torch.
    • When the nodepack changed torch and added a torchaudio to an install without one, one last command asks pip or uv for a torchaudio no newer than the restored torch, torchaudio<=2.14.0 beside torch 2.14.0+cu130. It names the PyTorch index of that torch, when it has one, with --index-url and is sent once.
    • With uv, the form of a command is settled with --dry-run before it is sent: as it is, then from uv's cache alone (--offline), then from the cache without the PyTorch index. The first form that resolves is sent, once. When none resolves, the command is sent as it is, once.
    • The restore sends no install command after one that failed. A failed command ends it with Failed to restore PyTorch and the command, and no other build is installed in place of the one from before.
  • Not covered:
    • What the cache can give. With uv, when the command as it is does not resolve, the restore puts back what uv resolves from its cache alone, which is the builds and dependencies whose index pages the cache has. download.pytorch.org sends the pages of its nightly indexes and of cuda-bindings, cuda-pathfinder and cuda-toolkit with Cache-Control: no-store, so uv keeps none of them. Outside what the restore guarantees are therefore, while the PyTorch index is not reached: a development build that was installed from there, a torch for CUDA 13 from there whose cuda-bindings, cuda-pathfinder or cuda-toolkit the nodepack changed, a torch that pip installed, a cache that was emptied or that another version of uv wrote, and an install whose only reachable index is a mirror and whose cache cannot give the build. In that last case a command without the PyTorch index installs the build from the mirror; the restore does not send it. The restore then logs a warning and the error, and changes nothing more.
    • pip. Every failed pip command ends the restore: pip has no mode that uses its cache alone. pip goes on past an index that it cannot reach, so with a mirror as the only reachable index its commands complete.
    • Partial restore. The restore is several commands. What the commands before a failed one restored stays, such as a restored torch beside a torchaudio that the nodepack added.
    • A failure while a command installs (a file that cannot be removed, a write that fails, the process ended) can leave torch not importable, with the error logged. With uv that includes the --offline command, in a configuration in which the one command of the base failed at the index and changed nothing.
    • A torchaudio for another CUDA version. pip and uv keep the torchaudio that the nodepack added when it satisfies that last command. One that was built for another CUDA version then stays and does not import beside the restored torch: 2.11.0+cu126 beside 2.14.0+cu130, and 2.11.0 from PyPI beside 2.14.0+cpu, with pip and with uv. The base leaves the same torchaudio. The restore does not replace it by its local label. That was built and measured: where an index of the user is read before or beside the --index-url of the command (UV_DEFAULT_INDEX, UV_EXTRA_INDEX_URL, PIP_EXTRA_INDEX_URL), it replaced a torchaudio that worked by one that does not import, in 6 configurations. By the same order of indexes, a torchaudio that the last command does install can come from an index of the user.
    • Put back, not repaired. A torchvision or torchaudio that did not load beside its torch before the nodepack is put back in that build, also when the nodepack had replaced it by one that loads. A dependency of torchvision that the nodepack removed, such as pillow, is not brought back; the base brought it back by resolving all three packages.
    • Dependencies from the cache. A dependency of torch can come back from the cache in another version than before (cuda-bindings 13.5.0 where 13.4.4 was).
    • Time. With uv each command is resolved twice, once for the dry run: 2 to 3 seconds more on a CUDA install. When the PyTorch index does not answer, a dry run waits about 50 seconds and prints nothing; when no form resolves, the command as it is waits as long again.
    • cm-cli shows no line at info level, as before. A restore from the cache shows uv's install output and no line of Manager. A restore that failed shows the warning and the errors.
    • An install that was already broken. When an earlier interrupted uninstall left a stale .dist-info, pip answers Requirement already satisfied and installs nothing, with no error. The base does the same.
    • Two nodepacks in one command. cm-cli install of two nodepacks gives the restore after the second one the package list from before the first restore as the state of before, because get_installed_packages() returns its cached list. That restore installs the first nodepack's torch again, and the last check of cm-cli puts the build of before back. The base reads the same cached list. Not changed here.
    • main gets the same change in Restore the PyTorch builds that were installed before a nodepack #3617.

Changes

  • The restore installs the builds from before. torch_rollback() gets what was installed before and after the nodepack in place of the previous torch version, and installs each changed package as === its build from the index of that build. The version table is removed. Module: comfyui_manager/common/manager_util.py.
  • A removed torch or torchvision counts as changed. fix_broken() read new_pip_versions['torch'], which raised KeyError for a removed package. Module: comfyui_manager/common/manager_util.py.
  • With uv, a dry run settles the form of a command, and the command is sent once. uv does not put back what it removed when a command fails while it installs, and an install command sent after it can lay the new build over what is left and report it as installed. So nothing is sent again after a failure; which form to send is asked first with --dry-run, which installs nothing and whose output is not shown. Module: comfyui_manager/common/manager_util.py.
  • tests/test_torch_rollback.py: 143 cases, 16 before. They run fix_broken() and check the dry runs and the install commands it sends, in order, and the lines it logs.
  • CHANGELOG.md: one bullet under Unreleased, Fixed.

Verification

Real ComfyUI. A disposable ComfyUI instance, ComfyUI master started with --cpu --enable-manager, each commit installed as the Manager package of a Python 3.13.12 venv that has uv 0.13.10 and no pip. Start states of the venv, each put back before a journey: A1 torch 2.11.0+cpu and torchvision 0.26.0+cpu; B6 A1 with torchaudio 2.10.0+cpu; A1m A1 installed through a mirror set in UV_EXTRA_INDEX_URL. The nodepacks are local git repositories whose install script changes PyTorch. Each journey ran through cm-cli and through the server (the install queued, then the restart that carries it out): 31 journeys.

Real CUDA installs. The fix_broken() of each tree, driven with real pip 26.3 and uv 0.13.9 on copies of kept venvs (Python 3.12.3), among them C0, torch 2.14.0+cu130 and torchvision 0.29.0+cu130: 98 runs, 55 of the head. After each run: pip freeze, an import of torch, torchvision.ops.nms, torchaudio.functional.lowpass_biquad, and the hash of every recorded file of torch and its requirements. No GPU computation.

Script. The fix_broken() of the base and head trees of both lines with subprocess replaced, nothing installed: 200,688 pairs of an input and a plan of which forms resolve and which install command fails.

Intent Path and step Before · a1b026474 After · 09a29ba04
A removed torchvision is put back real ComfyUI, A1: a nodepack whose install script removes torchvision; cm-cli and server Red: Failed to restore PyTorch from a KeyError. torchvision stays removed, and ComfyUI does not come up after the restart Green: uv pip install --no-deps torchvision===0.26.0+cpu --extra-index-url https://download.pytorch.org/whl/cpu, after the info line restore PyTorch: torchvision 0.26.0+cpu. The builds of before, ComfyUI comes up
The build of before, from the index of that build real ComfyUI, A1: a nodepack that installs torch 2.14.0+cpu; cm-cli and server Red: --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cpu. The nodepack's torch 2.14.0+cpu stays, beside a nightly torchvision 0.30.0.dev Green: one dry run, then torch===2.11.0+cpu --extra-index-url https://download.pytorch.org/whl/cpu. The builds of before
torch first, then torchvision without its dependencies real ComfyUI, A1: a nodepack that installs torch 2.14.0+cpu and torchvision 0.29.0+cpu; cm-cli and server Red: the nightly command. The nodepack's builds stay Green: the torch command, then --no-deps torchvision===0.26.0+cpu --extra-index-url …, each after its dry run. The builds of before
The CUDA libraries of the restored torch real CUDA install, C0: the nodepack pins torch 2.10.0 (CUDA 12.8), which overwrites files of four CUDA 13 libraries; pip and uv Red: a nightly pair is installed, and cuSPARSELt is left with the files of the other CUDA version Green: the torch command, then --no-deps --force-reinstall of cuDNN, cuSPARSELt, NCCL and NVSHMEM. The builds of before, torch imports, 0 recorded files with another hash
A torchaudio is put back without moving torch real ComfyUI, B6: a nodepack that installs torch 2.14.0+cpu and another torchaudio; cm-cli and server Red: the nightly command. Ends with torch 2.14.0+cpu, torchvision 0.30.0.dev and torchaudio 2.11.0+cpu Green: the torch command, then --no-deps torchaudio===2.10.0+cpu --extra-index-url …. The builds of before, torch not moved, all three work
A torchaudio that the nodepack added and that requires another torch is replaced real ComfyUI, A1: a nodepack that adds torchaudio 2.10.0+cpu with its torch; cm-cli and server Red: a nightly set of three Green: the torch command, then torch===2.11.0+cpu torchaudio<=2.11.0 --index-url https://download.pytorch.org/whl/cpu with no dry run. torchaudio 2.11.0+cpu, all three work
The network is cut, the cache has the build real ComfyUI, A1: a nodepack that installs torch 2.14.0+cpu, then no network; the cache of the instance; cm-cli and server Red: the nightly command exits 2. The nodepack's torch stays, and torchvision.ops.nms fails Green: the dry run of the command fails, the dry run with --offline resolves, the --offline command is sent once. The builds of before. The server log has the two info lines and nothing at warning level; the screen of cm-cli shows uv's install output and no error:
The same, the cache was filled through UV_EXTRA_INDEX_URL real ComfyUI, A1m, no network; cm-cli and server not run the first two dry runs fail, the third resolves, torch===2.11.0+cpu --offline is sent once. The builds of before
The same, an empty cache real ComfyUI, A1, no network; cm-cli and server the nightly command exits 2, the error, the nodepack's state the same end. Three dry runs fail and the command is sent once: uv's error: Request failed, the warning no dry run of the command resolved, …, then Failed to restore PyTorch with the command
Nothing is sent after a failed install command script; real CUDA installs with uv stopped while it removes or writes torch, in the first form and in the --offline form — 0 install commands after a failed one in 200,688 pairs, in the 55 real CUDA runs and in the 31 journeys. Each stopped run ends with the error
Both lines send the same commands script: this head against the head of the main counterpart, and base against base 0 of 200,688 pairs differ 0 of 200,688 pairs differ

Over the 55 real CUDA runs of the head: 67 dry runs, none changed a file of site-packages. In the 28 runs in which every install command completed, nothing is logged at warning level. Each of the 27 runs that a failed command ended has Failed to restore PyTorch with the command.

Results

  • Head 09a29ba04 on manager-v4 a1b026474. The real runs used e00ee78c4, which differs from the head in CHANGELOG.md and the commit message alone. Linux x86_64; Python 3.12.3 for the unit suite.
  • Unit suite (tests without tests/e2e and tests/playwright): 4243 passed, 17 skipped. On a1b026474: 4116 passed, 17 skipped. The 127 more are the test file: 143 cases where the base has 16.
  • tests/test_torch_rollback.py with the product code of a1b026474: 139 of 143 fail. The 4 that pass are the cases about an install without torchaudio, which the base already does.
  • Each part of the product change taken out alone: 88 variants, each fails at least one test. Two independent review passes wrote their own variants (52 and 51), and each found one that no test fails: except subprocess.CalledProcessError around a dry run widened to except Exception.
  • Other tests that run fix_broken() are unchanged and pass. ruff check . passes.
  • Not run: an install of a registry nodepack through the server queue, a server start that reads the registry (the server journeys used network_mode = offline), a ComfyUI instance whose venv has pip, the legacy UI, GPU computation, Windows, macOS. uv versions before 0.13.9 were measured on the main counterpart.
Commands
git diff a1b026474...09a29ba04
python -m pytest tests --ignore=tests/e2e --ignore=tests/playwright -q -p no:cacheprovider
python -m pytest tests/test_torch_rollback.py -q -p no:cacheprovider
ruff check .
# real ComfyUI, one instance per commit; the nodepack is a local git repository
cm-cli install <url of the nodepack>
# through the server: the install queued, then POST /v2/manager/reboot

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 0446647d-d505-41d3-91ff-bff6877eb77f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

When a nodepack changed torch or torchvision, torch_rollback() chose
what to install from the torch version from before. Without a local
label it sent `pip install --force` with the package names, which
installs the newest releases and which uv refuses. With a label it sent
the versions of a table that ends at 2.7.0, or the newest nightly build
for a version that the table lacks. A nodepack that removed torch, or
removed torchvision and left torch as it was, ended the restore in a
KeyError.

- Give torch_rollback() what was installed before and after the
  nodepack, and count a removed torch or torchvision as changed
- Install each of torch, torchvision and torchaudio that is not the
  build it was before as `<name>===<build>`, torch first with its
  dependencies, then the other two with `--no-deps`. Name the index
  https://download.pytorch.org/whl/<label> for a release with a local
  label, and the nightly index with `--pre` for a development build
- Install again the requirements of the restored torch whose files a
  distribution of the nodepack overwrote, such as the CUDA libraries of
  a torch of another CUDA version
- When the nodepack changed torch and added a torchaudio to an install
  without one, ask last for one no newer than the restored torch:
  `torch===<build> torchaudio<=<public version of that torch>`, with
  torch's index, if it has one, as `--index-url`, sent once. pip and uv
  keep a torchaudio that satisfies it, so a build for another CUDA
  version stays and can fail to import
- With uv, ask with `--dry-run` which form of a command that puts back a
  build resolves: as it is, with `--offline`, or with `--offline` and
  without the PyTorch index. Send the first that does once, or the
  command as it is when none does
- Remove the version table
- Extend tests/test_torch_rollback.py: the tests run fix_broken() with
  pip and with uv, and check the commands it sends and the lines it logs
- Add a CHANGELOG entry

An install command that fails ends the restore with "Failed to restore
PyTorch" and that command. uv does not put back what it removed when a
command fails while it installs, so the restore sends no command after
it, and no other build is installed in place of the one from before.
What the restore cannot put back is listed in #3618.

The same change for main is #3617.
ltdrdata added a commit that referenced this pull request Nov 28, 2026
When a nodepack changed torch or torchvision, torch_rollback() chose
what to install from the torch version from before. Without a local
label it sent `pip install --force` with the package names, which
installs the newest releases and which uv refuses. With a label it sent
the versions of a table that ends at 2.7.0, or the newest nightly build
for a version that the table lacks. A nodepack that removed torch, or
removed torchvision and left torch as it was, ended the restore in a
KeyError.

- Give torch_rollback() what was installed before and after the
  nodepack, and count a removed torch or torchvision as changed
- Install each of torch, torchvision and torchaudio that is not the
  build it was before as `<name>===<build>`, torch first with its
  dependencies, then the other two with `--no-deps`. Name the index
  https://download.pytorch.org/whl/<label> for a release with a local
  label, and the nightly index with `--pre` for a development build
- Install again the requirements of the restored torch whose files a
  distribution of the nodepack overwrote, such as the CUDA libraries of
  a torch of another CUDA version
- When the nodepack changed torch and added a torchaudio to an install
  without one, ask last for one no newer than the restored torch:
  `torch===<build> torchaudio<=<public version of that torch>`, with
  torch's index, if it has one, as `--index-url`, sent once. pip and uv
  keep a torchaudio that satisfies it, so a build for another CUDA
  version stays and can fail to import
- With uv, ask with `--dry-run` which form of a command that puts back a
  build resolves: as it is, with `--offline`, or with `--offline` and
  without the PyTorch index. Send the first that does once, or the
  command as it is when none does
- Remove the version table
- Extend tests/test_torch_rollback.py: the tests run fix_broken() with
  pip and with uv, and check the commands it sends and the lines it logs
- Add a CHANGELOG entry

An install command that fails ends the restore with "Failed to restore
PyTorch" and that command. uv does not put back what it removed when a
command fails while it installs, so the restore sends no command after
it, and no other build is installed in place of the one from before.
What the restore cannot put back is listed in #3617.

The same change for manager-v4 is #3618.
@ltdrdata
ltdrdata force-pushed the fix/v4-torch-restore branch from 8b1348b to 09a29ba Compare November 28, 2026 10:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant