Nodes/ComfyUI-lhyNodes/Set CUDA Device
ComfyUI Node

Set CUDA Device

Set CUDA_VISIBLE_DEVICES mid-run, without restarting ComfyUI

By lihaoyun6·Created 12 months ago·Updated 13 days ago· 20
Set CUDA Device
  • any
  • any
  • original
device

CudaDevicePatcher (displayed as Set CUDA Device) does one thing: it changes the CUDA_VISIBLE_DEVICES environment variable while your workflow is running, instead of at ComfyUI startup. The description from the pack is exact: "Modify the value of the environment variable CUDA_VISIBLE_DEVICES during runtime."

Why would you want that? Multi-GPU machines. Maybe you're running two ComfyUI instances and one of them keeps grabbing both cards, or you have a model that's happier offloaded to the second GPU, or you're testing which card is causing OOM. Normally CUDA_VISIBLE_DEVICES has to be set before the process starts. This node lets a workflow decide it mid-graph.

How it works, and the honest caveat

The implementation is three lines: save the current value, write the new one into os.environ, and print what it did. The any input is a pass-through - required, because a node needs an incoming wire to be reachable in the graph. That's actually the design: feed it the output of whatever runs before you want the switch to happen, and the environment gets patched at exactly that point in execution order. It returns the value it received plus the original environment string, in case you want to restore it later.

Now the part that matters: CUDA_VISIBLE_DEVICES only takes effect when CUDA first initializes. ComfyUI already has CUDA up by the time any workflow runs, so this node cannot redirect tensors that already exist on GPU 0. Where it does work is anything that spawns or initializes CUDA after the node executes - subprocesses, nodes that fork off their own workers, or a library call that hasn't touched CUDA yet. If your mental model is "swap the GPU for the rest of the graph," this won't do that. If your model is "patch the environment before a downstream component reads it," this is exactly right.

Inputs and outputs

  • any (*) - a pass-through that also gates execution order. Any upstream value works.
  • device (STRING) - the new value, e.g. "1" or "0,1" or "" to clear it.

Two outputs: any (the input, unchanged) and original (STRING - the previous value, if you want to restore it).

Installing it

Part of ComfyUI-lhyNodes:

  • ComfyUI Manager → search lhyNodes → Install, restart.
  • Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/lihaoyun6/ComfyUI-lhyNodes.git
python -m pip install -r ComfyUI-lhyNodes/requirements.txt

Restart ComfyUI.

Where people get burned

The one real misunderstanding is expecting it to hot-swap already-running GPU work - it can't, and no node can. If you need to pin ComfyUI itself to one card, set CUDA_VISIBLE_DEVICES in your launch script/environment before starting, which is the standard fix. The other footgun: because it's an output node, it sits at the end of a run and ComfyUI may execute it after the sampling is already done; put the thing you want switched before it, and wire this node's output downstream so nothing it "controls" runs before the patch. Used for its actual purpose - nudging an environment variable at a specific point in a run - it's a clean, tiny tool.

CategorylhyNodes/Utils

Inputs (2)

NameTypeDefaultDescription
any*
deviceSTRING

Outputs (2)

NameTypeDescription
any*
originalSTRING