Skip to content

ComfyUI Dynamic VRAM compatibility#572

Draft
Pizzawookiee wants to merge 3 commits into
numz:mainfrom
Pizzawookiee:main
Draft

ComfyUI Dynamic VRAM compatibility#572
Pizzawookiee wants to merge 3 commits into
numz:mainfrom
Pizzawookiee:main

Conversation

@Pizzawookiee
Copy link
Copy Markdown

Patches the model with ComfyUI's operations layers (i.e. replacing nn.Linear with operations.Linear) before loading the weights. Also uses ComfyUI's safetensors loader. In theory this should be all that is necessary to implement ComfyUI's recent Dynamic VRAM features.

@thehhmdb
Copy link
Copy Markdown
Contributor

I had a quick test and it to errors with:

[ERROR] Error during processing: module 'comfy' has no attribute 'ops'

@Pizzawookiee
Copy link
Copy Markdown
Author

Is it failing at this line specifically?

src/common/config.py, line 219 --> default_ops = comfy.ops.manual_cast

I am on comfy stable release 0.18.1

@thehhmdb
Copy link
Copy Markdown
Contributor

Yes, here are more log entries:

[20:26:15.005] ❌ [ERROR] Error during processing: module 'comfy' has no attribute 'ops'
Traceback (most recent call last):
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\inference_cli.py", line 1643, in main
    frames = process_single_file(file_path, args, device_list, output_path,
                                format_auto_detected=format_auto_detected,
                                runner_cache=runner_cache)
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\inference_cli.py", line 537, in process_single_file
    for result in _stream_video_chunks(
                  ~~~~~~~~~~~~~~~~~~~~^
        cap=cap,
        ^^^^^^^^
    ...<9 lines>...
        cleanup_timer_name="chunk_cleanup"
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ):
    ^
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\inference_cli.py", line 696, in _stream_video_chunks
    result = _process_frames_core(
        frames_tensor=frames.to(torch.float16),
    ...<3 lines>...
        runner_cache=runner_cache
    )
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\inference_cli.py", line 916, in _process_frames_core
    runner, cache_context = prepare_runner(
                            ~~~~~~~~~~~~~~^
        dit_model=args.dit_model,
        ^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<22 lines>...
        torch_compile_args_vae=torch_compile_args_vae
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\src\core\generation_utils.py", line 491, in prepare_runner
    runner, cache_context = configure_runner(
                            ~~~~~~~~~~~~~~~~^
        dit_model=dit_model,
        ^^^^^^^^^^^^^^^^^^^^
    ...<18 lines>...
        torch_compile_args_vae=torch_compile_args_vae
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\src\core\model_configuration.py", line 829, in configure_runner
    _setup_models(
    ~~~~~~~~~~~~~^
        runner, cache_context, dit_model, vae_model,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        base_cache_dir, block_swap_config, debug
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\src\core\model_configuration.py", line 949, in _setup_models
    dit_created = _setup_dit_model(runner, cache_context, dit_model, base_cache_dir,
                                   block_swap_config, debug)
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\src\core\model_configuration.py", line 1055, in _setup_dit_model
    runner = prepare_model_structure(runner, "dit", dit_checkpoint_path,
                                    runner.config, debug, block_swap_config)
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\src\core\model_loader.py", line 453, in prepare_model_structure
    model = create_object(model_config)
  File "Y:\ComfyUI\custom_nodes\ComfyUI-SeedVR2_VideoUpscaler\src\common\config.py", line 219, in create_object
    default_ops = comfy.ops.manual_cast
                  ^^^^^^^^^
AttributeError: module 'comfy' has no attribute 'ops'

I have upgraded Comfyui

@Pizzawookiee
Copy link
Copy Markdown
Author

Pizzawookiee commented Apr 13, 2026

Just to clarify, are you using the standalone CLI? And if that is true, are you able to test the node itself within ComfyUI?

@thehhmdb
Copy link
Copy Markdown
Contributor

Yes, I was using the CLI.

I have also tested the node within confyui, and Phase 2 DIT upscaling gives an OOM error (Error in Phase 2 (Upscaling): Allocation on device) without block swap, despite lots of RAM being available.

@Pizzawookiee Pizzawookiee marked this pull request as draft April 13, 2026 20:44
@Pizzawookiee
Copy link
Copy Markdown
Author

Okay then I'll mark this as draft while I look at these issues.

@wywywywy
Copy link
Copy Markdown

wywywywy commented May 21, 2026

Just tested this MR with the latest Comfy using 7b Q4 on a 5090 batch size 41 on a 2.7MP video, no crash at all. Upscaling processed successfully.

I tested with both blocks_to_swap=36 and blocks_to_swap=0, both worked. Not sure if this parameter actually does anything any more.

EDIT: Btw this is through the UI not CLI

@Pizzawookiee Pizzawookiee marked this pull request as ready for review May 21, 2026 15:54
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8a2308f770

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/core/model_loader.py
debug.log("Using CPU intermediate loading for MPS compatibility",
category="info", indent_level=1)
state = load_safetensors_file(checkpoint_path, device="cpu")
state = comfy.utils.load_torch_file(checkpoint_path, device=device_str)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Load MPS fallback tensors on CPU

In the MPS allocator-error fallback path, the retry still calls comfy.utils.load_torch_file(..., device=device_str), where device_str is still "mps". That re-runs the same failing load path instead of doing the intended CPU intermediate load, so users on MPS who hit allocation/watermark errors will continue to fail rather than recovering.

Useful? React with 👍 / 👎.

Comment thread src/common/config.py Outdated

# 2b. Handle InflatedCausalConv3d
elif isinstance (child, InflatedCausalConv3d) and hasattr(target_ops, "Conv3d"):
child.__bases__ = (target_ops.Conv3d,)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Convert InflatedCausalConv3d by replacing module instance

Setting child.__bases__ = (target_ops.Conv3d,) mutates an instance attribute, not the class inheritance used for dispatch, so this does not actually convert InflatedCausalConv3d layers to Comfy ops. As a result, models that rely on InflatedCausalConv3d keep the original layer type and miss the dynamic-VRAM operation patching this commit intends to add.

Useful? React with 👍 / 👎.

@Pizzawookiee
Copy link
Copy Markdown
Author

Just tested this MR with the latest Comfy using 7b Q4 on a 5090 batch size 41 on a 2.7MP video, no crash at all. Upscaling processed successfully.

I tested with both blocks_to_swap=36 and blocks_to_swap=0, both worked. Not sure if this parameter actually does anything any more.

EDIT: Btw this is through the UI not CLI

To clarify, with the base repository (without this pull request) were you able to run the model without crashes at both blocks to swap = 0 and blocks to swap = 36?

@wywywywy
Copy link
Copy Markdown

To clarify, with the base repository (without this pull request) were you able to run the model without crashes at both blocks to swap = 0 and blocks to swap = 36?

On the official main branch yes I can run blocks to swap = 36 no problems as well. I haven't tested 0 in a long time so I'm not sure.

@wywywywy
Copy link
Copy Markdown

Let me know if there's anything you want me to test

@Pizzawookiee Pizzawookiee marked this pull request as draft May 21, 2026 20:56
@Pizzawookiee
Copy link
Copy Markdown
Author

Let me know if there's anything you want me to test

In theory, I could use some testing with block swap turned completely off. That said, on my device which is less powerful, disabling block swap while using my pull request currently still results in OOM so I don't consider this PR up to par yet.

@wywywywy
Copy link
Copy Markdown

Just realised that swap_io_components needs to be off as well.

So I pulled the latest commit and tested it again. Still works with batch size of 41.

When I use batch size 81, the VRAM goes to 100%, but I guess at that point it's all taken up by the latents not the layers, so it won't be a good test for offloading.

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.

3 participants