Lora Math
Do actual math on LoRA tensors — not just strengths
- V
- F
- Expression
- stack
- LORA_MODEL
- STACK
Most "LoRA math" in ComfyUI is a strength slider. This node is the other end of the spectrum: it runs More Math's expression language directly on the raw LoRA tensors - the lora_up.weight, lora_down.weight, alpha, and diff dicts - and hands you back a modified LoRA. Same idea as the pack's Image/Latent/Float math nodes, but pointed at the weights themselves instead of pixels or numbers.
The LORA_MODEL type it works on is the raw LoRA tensor dict from ComfyUI's newer experimental training pipeline - the thing LoRATrainer emits. Feed the output into the experimental Load LoRA Model node (applies it to a model) or Save LoRA Weights (writes it back to disk). That's the practical angle: this is where you merge, blend, or surgically edit LoRAs at the tensor level. V0 + V1 merges two LoRAs. V0 * F0 scales one. Something like if (K == "lora_up.weight") V0 else 0 zeroes out one component. It's a scalpel, not a hammer.
How it works
There's no base model here. A LORA_MODEL is just a dict of tensors, and unlike the Model/Clip/Vae math nodes there's no patched-versus-base distinction - the tensors already are the LoRA's deltas, so there's no V0_d variable. The node collects the union of tensor keys across all your input LoRAs, then evaluates your expression once per tensor, with:
V0,V1, ... - the matching tensor from each input (zero-filled when that input doesn't have that key)K/key- the current tensor's name, e.g.lora_up.weightL/layer,LC/layer_count- current index and total layer countF0..Fn- your float inputs- plus the full More Math function library:
lerp,clamp,smoothstep,where,concat,sigm, on and on.
Because the expression runs per tensor, one expression can do different things per layer. That's how you'd build a per-layer strength ramp or fade in only the up-projection. If you want to see the machinery, it's the calculate_lora_dict_autogrow function in loraDictCommon.py.
The inputs that matter
You'll realistically set three things:
- V - autogrow
LORA_MODELinputs. Plug in one or more LoRAs; the expression sees them asV0,V1, ... - F - autogrow float inputs (
F0, ...) for strengths, scales, offsets. - Expression (default
V0) - the math expression, evaluated per tensor.
Then there's use_compute_device ("Move tensors to GPU", default on): it temporarily copies the LoRA tensors to the compute device for the math and moves them back afterwards, per the tooltip. Leave it on unless you're bumping VRAM - with it off, all the tensor math grinds on the CPU. The optional stack input/output passes the pack's shared stack state between nodes.
Outputs
- LORA_MODEL - the modified tensor dict, same keys as the input. Wire it into Load LoRA Model, Save LoRA Weights, or another More Math node.
- STACK - the shared stack, if you're chaining math nodes.
Installing
Standard custom node stuff. In ComfyUI Manager search "More math", or:
cd ComfyUI/custom_nodes
git clone https://github.com/mcDandy/more_math
cd more_math
pip install -r requirements.txt # antlr4-python3-runtime, torch
Restart ComfyUI. No model downloads and no heavy dependencies - beyond torch the only real requirement is the ANTLR runtime that powers the expression parser. The author (Daniel Martinek, publishing as mkdaniel) also lists it on the Comfy registry as "More Math". The pack is GPL v3.
Where people get burned
- You can't feed it a normal LoRA file directly. The core LoraLoader outputs MODEL/CLIP, not
LORA_MODEL. You need something that emits that type - ComfyUI's experimental LoRATrainer, or another More Math Lora node. This node lives in the training/injection pipeline, not the "pick a .safetensors and go" flow, which trips people up more than anything else. - Missing keys come back as zeros. Do
V0 + V1and where one LoRA lacks a key the other has, that input gets a zero-filled tensor in its place. Great for merging, surprising if you expected an error. - Expressions are lazy. Inputs that aren't referenced in the expression aren't evaluated at all, so an unused
V2never gets touched. - It's a math node, not a wizard. If a plain strength slider does the job, it does the job. Reach for Lora Math when you're doing real surgery - merging, per-layer scaling, or post-processing trained weights before you save them.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| V | COMFY_AUTOGROW_V3 | — | |
| F | COMFY_AUTOGROW_V3 | — | |
| Expression | STRING,SYNTAX_TREE | V0 | Expression to apply on LoRA tensors |
| use_compute_device | BOOLEAN | true | Temporarily copies LoRA tensors to the compute device for math and moves the result back afterwards. |
| stackopt | STACK | Access stack between nodes |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| LORA_MODEL | LORA_MODEL | — |
| STACK | STACK | — |