Nodes/More Math/Selective guider math
ComfyUI Node

Selective guider math

Hook into attention heads mid-diffusion and rewrite the tensor

By mcDandy·Created about a year ago·Updated 3 days ago· 5
Selective guider math
  • V
  • F
  • Expression
  • stack
  • GUIDER
  • STACK
layer_x0
hook_targetall
remember_stackfalse
debug_hooksfalse

This is the node in the More Math pack that makes everyone's eyes go wide - and it's the hardest one to recommend to a beginner. Selective guider math attaches hooks inside the model's actual layers during guided diffusion: attention blocks, DiT blocks, UNet stage patches, even the model's entry and exit edges. Your expression runs on the tensor flowing through that layer, with the layer's identity handed to you as variables. It's attention hacking with math.

What it does

Pick a hook_target - attn1, attn2, double_block_attn, single_block_attn, dit_block, unet_block, model_begin, model_end, or all - and a layer_x index, and the node runs your Expression whenever that layer fires. The expression gets real introspection: inp/sample is the tensor, q, k, v and heads in attention hooks, layer_key (like output.6.attn2.0), block_name, layer_id (aliased layer and i), hook_kind, transformer_index, has_qkv. There are guard variables so a model-agnostic expression can stay sane: is_attn1, is_attn2, is_dit, is_unet_block. And when the guider carries original_conds, hooks fire separately for positive and negative sides, exposed as cond_side (positive/negative), is_positive, is_negative.

The default expression is deliberately a debug one - print([layer, layer_key, hook_kind, cond_side, activations_shape]); inp - and debug_hooks logs hook activity. The author built the layer filtering to be runtime-adjustable in the UI, which is the one thing that keeps this usable.

Inputs and outputs

V is a single GUIDER, F the autogrow floats, Expression, layer_x (int, default 0), hook_target (combo, default all), remember_stack, debug_hooks, and the optional stack. Outputs are GUIDER and STACK.

Installing it

Identical to the rest of the pack:

cd ComfyUI/custom_nodes
git clone https://github.com/mcDandy/more_math
cd more_math
pip install -r requirements.txt

Restart ComfyUI, or install "More math" from ComfyUI Manager. Dependency is antlr4-python3-runtime plus torch. No models.

Honest warnings

Read the README's "Practical notes" before you start, because the gotchas are documented and they're real. attn2 is only a hook label - it doesn't guarantee real cross-attention, so use is_cross_attention/attention_relation if that matters. On SD1.x the same layer_id hits repeatedly (one UNet block has several transformer sub-blocks), so use transformer_index to target exactly one. Timestep-embedding hooking needs layer_x=0 and a filter on block_name=="time_emb". And the author deliberately restores base conditions before reattaching hooks to avoid hook accumulation across reruns - which tells you that's a failure mode people hit.

This is advanced tooling in a brand-new, solo-maintained pack, and its results can be spectacular or absolute noise. Start from the debug expression, read the variable table, and change one thing at a time. If you just want per-step guidance without the layer surgery, the plain Guider math node is the gentler sibling.

CategoryMore math

Inputs (8)

NameTypeDefaultDescription
VGUIDER
FCOMFY_AUTOGROW_V3
ExpressionSTRING,SYNTAX_TREEprint([layer, layer_key, hook_kind, cond_side, activations_shape]); inp
layer_xINT0-999–999
hook_targetCOMBOall9 options: all, dit_block, unet_block, attn1, attn2, double_block_attn, +3
remember_stackBOOLEANfalse
debug_hooksBOOLEANfalse
stackoptSTACKAccess stack between nodes

Outputs (2)

NameTypeDescription
GUIDERGUIDER
STACKSTACK