ComfyUI Node

Regex Weights

Per-key merge weights by pattern — block weights, but with regex instead of architecture

By ljleb·Created 3 years ago·Updated about a month ago· 101
Regex Weights
    • recipe
    • recipe_txt
    regex_weights
    default0.00
    model_configsdxl-sgm
    regex_modesimple
    match_full_keysfalse

    Mecha Regex Weights is the "I want a different strength for these weights" node. Where Blocks Mecha Hyper gives you one weight per architecture block, this gives you weights per key pattern: write rules like pattern: weight, one per line, and every model key matching a pattern gets that weight when the merge runs. It's the general case of block weighting - instead of being stuck with the architecture's notion of "block," you can weight by anything you can express as a pattern. Merge attention blocks differently from the rest, boost the mid layer, treat all time_embed keys one way - that's the kind of control that used to mean writing a custom script.

    Inputs

    • regex_weights - the rules, one per line, pattern: weight. The author's tooltip: <pattern>: <weight>. What counts as a pattern depends on regex_mode:
      • simple (default) - the only special character is *, meaning "any number of characters." a*n matches alliteration. \* matches a literal *, \\ a literal backslash. Perfect for people who flinch at regex.
      • regex - full Python regex. .* matches any number of characters. For real pattern power.
    • default - the weight for any key matching nothing (a FLOAT, default 0.0). This is your "everything else" value, so zeroing it out zeroes out unmatched keys - usually you want it set to the same strength as the rest of the blend.
    • model_config - which key layout the patterns run against (17 configs, default sdxl-sgm). The patterns match keys in this layout.
    • match_full_keys - false (default): a pattern matches if it covers part of a key. true: the pattern must cover the whole key start to finish. false is the forgiving mode - you'll notice if a partial match catches more than you meant.
    • regex_mode - simple or regex, as above.

    Outputs: recipe (the param MECHA_RECIPE) and recipe_txt (the STRING form of it - handy for seeing exactly what the patterns expanded to).

    How it works

    Patterns are matched against each key in the model config, in order; first match wins, and matched keys carry that weight into the merge's param slot (typically alpha). Because the result is a param recipe, it plugs into the same sockets as a Float - but with per-key variation instead of one flat number.

    Install & gotchas

    ComfyUI Manager → search mechaMecha Merge Node Pack, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/ljleb/comfy-mecha.git
    pip install -r comfy-mecha/requirements.txt
    

    Restart; only dependency is sd-mecha==1.1.7.

    Where people get burned: the default of 0.0 is a trap - if you write patterns for a handful of keys and leave default at zero, every other key gets strength 0 and the merge collapses to almost nothing. Set default to your base strength first, then layer patterns over it. Second: patterns are matched against the keys as they appear in the chosen model_config - a pattern that worked for SDXL won't necessarily match the same semantic layer in a kohya-layout config, where keys are renamed. And in simple mode, remember only * is special; if you write what looks like regex and it silently matches everything, that's the mode doing its job. Use the recipe_txt output to verify what actually matched before running the full merge.

    Categorymecha

    Inputs (5)

    NameTypeDefaultDescription
    regex_weightsSTRINGone regex per line: <pattern>: <weight> <pattern>: <weight>
    defaultFLOAT0.00-18446744073709550000–18446744073709550000
    model_configCOMBOsdxl-sgm17 options: singleton-mecha, sdxl-supermerger_blocks, sdxl-sgm, sdxl-kohya_lycoris_lora, sdxl-kohya_kohya_lora, sdxl-kohya_but_diffusers_lycoris_lora, +11
    regex_modeCOMBOsimple- simple: the only special character is *, which means to match any number of characters. For example, "a*n" will match "alliteration". \* matches * literally and \\ matches \ literally - regex: full python regex mode. for example, ".*" matches any number of characters
    match_full_keysBOOLEANfalse- True: a pattern needs to cover a key from start to end to match it - False: a pattern only needs cover a part of a key to match it

    Outputs (2)

    NameTypeDescription
    recipeMECHA_RECIPE
    recipe_txtSTRING