ComfyUI Node Runs on cloud

LoraExtractKJ

Pulling a LoRA out of a finetuned checkpoint

By kijai·Created 3 years ago·Updated about 16 hours ago· 2,930
LoraExtractKJ
  • finetuned
  • original
    filename_prefixloras/ComfyUI_extracted_lora
    rank64
    lora_type
    algorithmsvd_lowrank
    lowrank_iters7
    output_dtypefp16
    bias_difftrue
    adaptive_param0.15
    clamp_quantilefalse

    Full finetunes are heavy to ship - gigabytes where a LoRA is megabytes. LoraExtractKJ is how you go from "I have a full finetuned model" to "I have a LoRA that reproduces most of what changed," by computing the difference between your finetune and its original base and factoring that difference down into a low-rank LoRA file. This is a real, established technique - LoRA extraction from a finetune's diff against its base is the community's standard middle ground between full-finetune quality and LoRA-sized distribution - not something niche to this pack.

    It's also not hypothetical for this exact author: kijai has used this same extraction approach to produce distilled Wan LoRAs like CausVid from full finetuned checkpoints, describing the process in his own words as carrying the distillation over "pretty well" when used at a modest strength. This node is that workflow, exposed as a ComfyUI node instead of a standalone script.

    Why you'd want this

    If you've got a full finetuned or merged checkpoint and don't want to distribute gigabytes of weights - or want a LoRA you can stack at partial strength alongside others, which a full checkpoint swap can't do - extraction gets you there. It's lossy: a low-rank approximation of the diff isn't identical to the diff itself. But for most style and concept finetunes, a well-chosen rank captures the useful part of the change while staying small.

    How it works

    You give it two versions of the same architecture: original (the unmodified base) and finetuned (the version you want to extract a LoRA from). The node computes the weight-level difference between them and factorizes that difference using a low-rank decomposition - SVD by default - capped at whatever rank you set, then saves the result as a LoRA file. Everything else in the node is tuning knobs for that decomposition.

    The inputs and outputs that matter

    • original / finetuned - both typed MODEL,CLIP, meaning you can extract from either the diffusion model or the CLIP/text encoder (or both, run separately).
    • rank (default 64, range 1–4096) - the target LoRA rank. Higher captures more of the original difference but produces a larger file; this is the main quality/size trade-off to tune.
    • algorithm (default svd_lowrank) - the decomposition method used to factor the diff.
    • lowrank_iters (default 7, range 1–100) - iteration count for the low-rank algorithm; more iterations can improve the approximation at the cost of extraction time.
    • adaptive_param (default 0.15) - a tuning parameter for the extraction algorithm; leave at default unless you have a specific reason to adjust it.
    • clamp_quantile (boolean, default false) - clamps outlier values during extraction, a common option in LoRA-extraction tooling generally, useful if a few extreme weight deltas are otherwise dominating the result.
    • bias_diff (boolean, default true) - whether bias terms are included in the extraction, not just weight matrices.
    • output_dtype (default fp16) - the precision the extracted LoRA is saved at.
    • filename_prefix (default loras/ComfyUI_extracted_lora) - where the output file is written, relative to your output directory.

    There are no data outputs - this is a save node (is_output_node: true), the result is written straight to disk as a LoRA file.

    How to install it

    • ComfyUI Manager - search KJNodes for ComfyUI, install, restart.
    • Manual - cd ComfyUI/custom_nodes && git clone https://github.com/kijai/ComfyUI-KJNodes, then pip install -r ComfyUI-KJNodes/requirements.txt (portable: python_embeded\python.exe -m pip install -r ComfyUI\custom_nodes\ComfyUI-KJNodes\requirements.txt), then restart.

    No extra model downloads, but be aware this node needs both your full original and finetuned checkpoints loaded simultaneously to compute the diff - that's meaningfully more VRAM/RAM than running either model alone, especially at higher rank and lowrank_iters where the decomposition itself takes real compute and time.

    Common issues & troubleshooting

    Extraction is slow, or seems to hang. SVD-based decomposition at a high rank or high lowrank_iters is genuinely compute-heavy, especially across a full model's weights rather than a small LoRA-sized delta. This isn't necessarily broken - check whether it's still making progress before assuming it's stuck.

    Extracted LoRA doesn't reproduce the finetune's effect well. Try raising rank - a low rank forces a more aggressive approximation of the diff, and for finetunes with broad, distributed changes (rather than a narrow, concentrated style shift), a low rank simply can't capture enough of it. clamp_quantile is worth toggling too if you suspect a handful of outlier weights are skewing the result.

    Out of memory during extraction. Both original and finetuned need to be resident at once for the diff to be computed - if you're already close to your VRAM ceiling running either model individually, extraction may not fit. There's no partial/streaming option exposed here; it's both models, fully loaded, at the same time.

    CategoryKJNodes/lora

    Inputs (11)

    NameTypeDefaultDescription
    finetunedMODEL,CLIPThe finetuned model or clip to extract LoRA from.
    originalMODEL,CLIPThe original base model or clip to diff against.
    filename_prefixSTRINGloras/ComfyUI_extracted_lora
    rankINT641–4096The rank to use for standard LoRA, or maximum rank limit for adaptive methods.
    lora_typeCOMBO6 options: standard, full, adaptive_ratio, adaptive_quantile, adaptive_energy, adaptive_fro
    algorithmCOMBOsvd_lowrankSVD algorithm to use, svd_lowrank is faster but less accurate.
    lowrank_itersINT71–100The number of subspace iterations for lowrank SVD algorithm.
    output_dtypeCOMBOfp163 options: fp16, bf16, fp32
    bias_diffBOOLEANtrue
    adaptive_paramFLOAT0.150–1For ratio mode, this is the ratio of the maximum singular value. For quantile mode, this is the quantile of the singular values. For fro mode, this is the Frobenius norm retention ratio.
    clamp_quantileBOOLEANfalse

    Outputs (0)

    No outputs