Skip to content

[Issue 101][eval-and-fix] clear_vae_memory: dispatch via soft_empty_cache#47

Merged
pollockjj merged 3 commits into
issue_101from
issue_193
May 5, 2026
Merged

[Issue 101][eval-and-fix] clear_vae_memory: dispatch via soft_empty_cache#47
pollockjj merged 3 commits into
issue_101from
issue_193

Conversation

@pollockjj
Copy link
Copy Markdown
Owner

Summary

  • One-line production change at comfy_extras/nodes_seedvr.py:84: torch.cuda.empty_cache()comfy.model_management.soft_empty_cache(). The canonical helper at comfy/model_management.py:1780 short-circuits via cpu_mode() and dispatches per-backend (MPS / XPU / NPU / MLU / CUDA), so it is the only correct call shape on non-CUDA hosts and on managed-device hosts where comfy.cli_args.args.cpu is True.
  • New CPU-only regression test at tests-unit/comfy_test/test_seedvr_clear_vae_memory_soft_empty_cache.py that flips args.cpu = True before importing any comfy.ldm.* / comfy_extras.* symbol, then asserts clear_vae_memory(stub) invokes soft_empty_cache exactly once and torch.cuda.empty_cache zero times.
  • Tracking issue: pollockjj/mydevelopment#193 (parent umbrella Add SeedVR2 support #101). Source finding: CodeRabbit duplicate/nitpick review on feat: Add support For SeedVR2 (CORE-6) Comfy-Org/ComfyUI#11294.

Scope

  • comfy_extras/nodes_seedvr.py — 1 line replaced (line 84). The for module in vae_model.modules(): if hasattr(module, "memory"): module.memory = None loop and gc.collect() are unchanged — both are CUDA-agnostic and out of scope.
  • tests-unit/comfy_test/test_seedvr_clear_vae_memory_soft_empty_cache.py — new file (64 lines).
  • git diff --name-only origin/issue_101..issue_193 lists exactly two paths.
  • No new imports: comfy.model_management is already imported at comfy_extras/nodes_seedvr.py:8.

Test plan

  • Pre-fix probe (slice 3 test against origin/issue_101 comfy_extras/nodes_seedvr.py): pytest exit 1, AssertionError: torch.cuda.empty_cache was called 1 times; expected 0.
  • Post-fix probe (slice 3 test against issue_193 HEAD): pytest exit 0, 1 passed in 1.88s.
  • AC-A: line 84 reads comfy.model_management.soft_empty_cache(); literal torch.cuda.empty_cache() no longer appears in clear_vae_memory.
  • AC-B: regression test asserts call counts when args.cpu = True.
  • AC-E: diff lists exactly the two in-scope paths (comfy_extras/nodes_seedvr.py + new test module).
  • AC-F: _cli_args.cpu = True (line 31) precedes comfy.model_management (line 33) and comfy_extras.nodes_seedvr (line 34) imports.

pollockjj added 3 commits May 5, 2026 15:09
… cache-clear dispatch (Comfy-Org#193)

Adds tests-unit/comfy_test/test_seedvr_clear_vae_memory_soft_empty_cache.py:
asserts comfy_extras.nodes_seedvr.clear_vae_memory dispatches its
cache clear via comfy.model_management.soft_empty_cache (the canonical
per-backend helper at comfy/model_management.py:1780) rather than
calling torch.cuda.empty_cache() directly.

Test forces comfy.cli_args.args.cpu = True before importing
comfy.model_management / comfy_extras.nodes_seedvr so the path under
test is soft_empty_cache's CPU-mode short-circuit at
comfy/model_management.py:1781 — deterministic regardless of host
CUDA availability. Stub is a bare torch.nn.Module so .modules()
iteration is well-defined.

Pre-fix probe (this commit, against issue_101 HEAD without fix
applied): pytest exits 1 with AssertionError "torch.cuda.empty_cache
was called 1 times; expected 0".

Production line-84 fix and post-fix probe land in Slice 2.
…ear via soft_empty_cache (Comfy-Org#193)

Replace the unguarded torch.cuda.empty_cache() call at
comfy_extras/nodes_seedvr.py:84 with comfy.model_management.soft_empty_cache().
The canonical helper at comfy/model_management.py:1780 short-circuits via
cpu_mode() and dispatches per backend (MPS / XPU / NPU / MLU / CUDA), so
it is the only correct call shape on non-CUDA backends and on managed-
device hosts where comfy.cli_args.args.cpu is True.

The comfy.model_management module is already imported at line 8; no new
import is required. The for-loop that nulls module.memory and the
gc.collect() call are unchanged — both are CUDA-agnostic and out of
scope.

Slice 2 post-fix probe (this commit) runs
tests-unit/comfy_test/test_seedvr_clear_vae_memory_soft_empty_cache.py
to PASS; Slice 1 had it failing against pre-fix HEAD with
"torch.cuda.empty_cache was called 1 times; expected 0".
…ar_vae_memory_uses_soft_empty_cache (Comfy-Org#193)

Rename the regression test function from
test_clear_vae_memory_dispatches_via_soft_empty_cache to
test_clear_vae_memory_uses_soft_empty_cache to match the slice
contract test ID. Test body, fixtures, imports, and module-level
args.cpu flip are unchanged.
Copilot AI review requested due to automatic review settings May 5, 2026 21:22
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes SeedVR2 VAE memory clearing to use ComfyUI’s cross-backend cache clearing helper instead of calling CUDA directly, and adds a regression test to prevent reintroducing the CUDA-only call shape.

Changes:

  • Replace torch.cuda.empty_cache() with comfy.model_management.soft_empty_cache() in clear_vae_memory.
  • Add a CPU-mode regression test asserting clear_vae_memory dispatches via soft_empty_cache and does not directly call torch.cuda.empty_cache.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
comfy_extras/nodes_seedvr.py Switches VAE cache clearing to model_management.soft_empty_cache() for backend-aware behavior.
tests-unit/comfy_test/test_seedvr_clear_vae_memory_soft_empty_cache.py Adds a regression test guarding against direct torch.cuda.empty_cache() calls from clear_vae_memory.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +29 to +34
from comfy.cli_args import args as _cli_args

_cli_args.cpu = True

import comfy.model_management # noqa: E402
import comfy_extras.nodes_seedvr as nodes_seedvr # noqa: E402
Comment on lines +53 to +57
f"times; expected 0. clear_vae_memory must dispatch via "
f"comfy.model_management.soft_empty_cache, which short-circuits in "
f"CPU mode (cpu_mode() check at comfy/model_management.py:1781). "
f"The unguarded torch.cuda.empty_cache() call at "
f"comfy_extras/nodes_seedvr.py:84 is the regression this test locks."
@pollockjj pollockjj merged commit 91c9fe3 into issue_101 May 5, 2026
10 checks passed
@pollockjj pollockjj deleted the issue_193 branch May 5, 2026 22:14
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.

2 participants