Nodes/cgem156-ComfyUI๐ŸŒ/LoRA SVD Rank ๐ŸŒ
ComfyUI Node

LoRA SVD Rank ๐ŸŒ

Shrink a LoRA's rank without hand-picking a number

By laksjdjfยทCreated 2 years agoยทUpdated about a month agoยท 93
LoRA SVD Rank ๐ŸŒ
  • lora
  • STRING
โ—„threshold1.000โ–บ
โ—„deviceโ–พโ–บ

A LoRA's rank (network_dim, in the vocabulary every trainer borrowed from Kohya's sd-scripts) sets how much capacity its low-rank matrices have. Bigger rank means a bigger file and more capacity to capture detail, but a lot of that capacity often goes unused - plenty of trained LoRAs could be meaningfully smaller without losing much. This node figures out how much smaller, automatically, using the same SVD-based resizing idea Kohya's resize_lora.py popularized.

The mechanism

A LoRA's weight update is a low-rank matrix, and singular value decomposition breaks any matrix down into its component "directions" ranked by how much of the update each one actually accounts for. Most of a trained LoRA's expressive power usually concentrates in a handful of the largest singular values - the rest contribute increasingly little. This node runs that decomposition and keeps only enough singular values to capture a target fraction of the original weight's total energy, dropping the rest. The practical effect is a smaller-rank LoRA that approximates the original closely, without you having to guess a target rank by trial and error the way manual resizing usually works.

Inputs and outputs

Required:

  • lora - the LoRA to resize.
  • threshold - 0-1, default 1. This is the fraction of the original weight's energy you want preserved; at the default of 1 nothing gets dropped, so you'll want to lower it to actually shrink anything. Lower thresholds mean more aggressive rank reduction and a smaller resulting file, at increasing risk of losing fidelity to the original LoRA's effect.
  • device - a combo choosing where the SVD computation runs (CPU vs GPU). SVD on large matrices can be slow on CPU, so if you've got the VRAM to spare, running it on GPU will usually be noticeably faster.

Output is a single STRING - most likely a summary report of what rank each layer ended up at after the reduction, rather than the resized LoRA itself. That's worth knowing before you go looking for a LoRA-type output that isn't there.

Installing it

ComfyUI Manager, search "cgem156-ComfyUI". Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/laksjdjf/cgem156-ComfyUI

Restart ComfyUI. No separate model download - this is pure linear algebra on the LoRA's own weight matrices.

A note on that STRING output

The pack's README flags that some features assume you also have pythongosssss's ComfyUI-Custom-Scripts installed, specifically for its ShowText node - this is exactly the kind of node that comment is about. Since the output here is a text report rather than something visual, wiring it into ShowText (or any node that displays a string) is the practical way to actually see what threshold value produced what result, rather than working blind.

Common issues

Leaving threshold at the default of 1 and wondering why nothing changed is the first trap - you have to lower it deliberately to get any reduction at all. Push it too low, though, and you're discarding singular values that were doing real work; if a resized LoRA suddenly feels weaker or less accurate than the original at the same weight, that's the tradeoff this node makes explicit - try backing the threshold up toward 1 rather than assuming something's broken. And if the operation is unexpectedly slow, check device - CPU-side SVD on a large LoRA's matrices is the more likely culprit than the algorithm itself being inefficient.

Categorycgem156 ๐ŸŒ/lora_merger

Inputs (3)

NameTypeDefaultDescription
loraLoRAโ€”
thresholdFLOAT1.0000โ€“1โ€”
deviceCOMBO2 options: cuda, cpu

Outputs (1)

NameTypeDescription
STRINGSTRINGโ€”