Nodes/DOGMA Nodes/DOGMA Prepare SAM Input v11.1 — VRAM Safe
ComfyUI Node

DOGMA Prepare SAM Input v11.1 — VRAM Safe

Unload the big models before SAM eats your VRAM

By axior·Created 4 months ago·Updated 3 days ago· 1
DOGMA Prepare SAM Input v11.1 — VRAM Safe
  • source_image
  • working_master_dependency
  • sam_image
  • info
max_side2560
multiple16
unload_modelstrue

This node is a traffic cop. It takes your source image, waits until a second image exists, deliberately unloads everything ComfyUI has cached in VRAM, resizes the source to something SAM can chew on, and hands that to your segmentation node.

DOGMAPrepareSAMInputV11_1 solves a problem specific to multi-stage restoration, and it's the least glamorous node in the pack for it. If your pipeline is upscale the master → run Qwen or an upscaler or a VLM → run SAM → crop → re-render, by the time SAM loads you may already have a 4B vision model and a diffusion checkpoint resident. On a 12–16GB card that's the moment you get an OOM that has nothing to do with SAM itself.

How it works

Inputs are source_image, working_master_dependency, max_side (2560), multiple (16) and unload_models (True).

The trick is working_master_dependency. Its pixels are never used - the docstring says so explicitly - but wiring your high-resolution master into it forces this node to execute after the upscale stage has finished. That's the graph equivalent of a semaphore, and it's a neat use of ComfyUI's lazy evaluation: you get ordering without a "wait for" node type existing.

With unload_models on, it calls ComfyUI's model management to unload all models, runs a garbage collect, and empties the cache. Then it takes source_image - your original, not the master - and only if its longest side exceeds max_side does it downscale, bicubically with antialiasing, rounding dimensions to a multiple of multiple and clamping so rounding can't push it past the ceiling. info reports what happened: SAM analysis image: 4096x2731 -> 2560x1712. Max side=2560. Models/cache unloaded before SAM=True. followed by the design note: SAM does NOT see the 2x working master; masks are rescaled later.

That last line is the important one. You are not segmenting the upscaled image. You're segmenting the original at a manageable resolution, and the pack's crop nodes scale those mask coordinates up to master space when they build crops.

Wiring

source_image ← your untouched original. working_master_dependency ← the 2x (or whatever) upscaled master, purely for ordering. sam_image → your SAM/grounding segmentation node. info → a text preview while you tune.

Install

ComfyUI Manager → search DOGMA Nodes, or:

cd ComfyUI/custom_nodes
git clone https://github.com/axior/ComfyUI-DOGMA-Nodes
# restart ComfyUI

Nothing to install beyond the pack itself. The README documents only the WAN VACE prep and the sampler nodes, so this one ships with no in-app description.

Gotchas

Unloading is not free. unload_models=True will evict your diffusion checkpoint too, so the next sampler run reloads several gigabytes from disk. On a fast NVMe that's a few seconds; on a spinning disk or a network mount it's painful. Turn it off if you have headroom and measure.

Re-running with a fixed seed won't skip this node. By design its dependency input means it re-executes when the master changes. If you notice the whole workflow unloading models every run, that's the graph re-evaluating downstream of the upscale, not a bug in this node.

Don't set max_side to your master resolution. The point is a smaller, faster SAM pass whose masks get rescaled later. Feeding it 4096 buys you memory pressure and a slower run for masks the crop nodes are going to resample anyway.

SAM at low resolution loses small objects. If tiny distant pedestrians vanish between this node and your crops, the downscale is usually why - raise max_side, or segment the original at native size and accept the VRAM cost.

CategoryDOGMA/Semantic Detailer

Inputs (5)

NameTypeDefaultDescription
source_imageIMAGE
working_master_dependencyIMAGE
max_sideINT25601024–4096
multipleINT168–64
unload_modelsBOOLEANtrue

Outputs (2)

NameTypeDescription
sam_imageIMAGE
infoSTRING