Change ATTN() to COUPLE() and remove need for AND
This commit is contained in:
+14
-13
@@ -8,7 +8,7 @@ The implementation is based on the one by [pamparamm](https://github.com/pampara
|
||||
|
||||
By default, the implementation produces slightly different results from Pamparamm's implementation because ComfyUI will only run the hook for conds that have it attached and can't batch negative conditionings.
|
||||
|
||||
As a consequence of this, however, you can also use `ATTN()` in your negative prompt, and it will work correctly.
|
||||
As a consequence of this, however, you can also use `COUPLE` in your negative prompt, and it will work correctly.
|
||||
|
||||
To enable batching negative prompts, run your positive and negative prompt through the `PPCAttentionCoupleBatchNegative` node. This will make the outputs identical to pamparamm's implementation and will also improve performance. It will fall back to the default behaviour in cases where batching can't be done, so it should always be safe to use.
|
||||
|
||||
@@ -17,23 +17,24 @@ To enable batching negative prompts, run your positive and negative prompt throu
|
||||
|
||||
See also the main syntax documentation for `MASK` etc.
|
||||
|
||||
### ATTN: Trigger Attention Couple
|
||||
### COUPLE: Trigger Attention Couple
|
||||
|
||||
Use `ATTN()` to mark a prompt to be used with Attention Couple. `ATTN()` needs to be combined with either `MASK()` or `IMASK()` to work correctly.
|
||||
You can use `COUPLE` to attach attention-coupled prompts to a base prompt:
|
||||
|
||||
If no mask is specified, an implicit `MASK()` is assumed.
|
||||
`base_prompt COUPLE MASK(0 0.5) coupled prompt 1 with mask COUPLE IMASK(0) coupled prompt 2 with custom mask`
|
||||
|
||||
For attention masking to take effect, you need at least two prompt segments with the `ATTN()` marker (separated with `AND`). A single prompt with `ATTN()` will simply ignore the marker.
|
||||
as a shortcut, `COUPLE(maskparams)` is expanded to `COUPLE MASK(maskparams)`, so the above prompt can also be written as:
|
||||
|
||||
For the first prompt (and the first prompt only) you can also use `FILL()` to automatically mask all parts not masked by other prompt segments.
|
||||
`base_prompt COUPLE(0 0.5) coupled prompt 1 with mask COUPLE IMASK(0) coupled prompt 2 with custom mask`
|
||||
|
||||
Behaviour:
|
||||
- If no mask is specified, an implicit `MASK()` is assumed.
|
||||
|
||||
- For the base prompt, you can also use `FILL()` to automatically mask all parts not masked by coupled prompts
|
||||
|
||||
- If the base prompt has weight set to zero (ie. ´:0` at the end), then the first coupled prompt with non-zero weight becomes the base prompt.
|
||||
|
||||
For example:
|
||||
```
|
||||
dog FILL() ATTN() AND cat MASK(0.5 1) ATTN()
|
||||
dog FILL() COUPLE(0.5 1) cat
|
||||
```
|
||||
|
||||
If typing `ATTN() MASK()` feels bothersome, try the following macro:
|
||||
```
|
||||
DEF(AM=ATTN() MASK($1))
|
||||
```
|
||||
and then use it like `MASK`: `AM(0 1, 0.5 1)`
|
||||
|
||||
+14
-10
@@ -103,7 +103,7 @@ This syntax is also available in outside scheduled with the `PCTextEncode` node,
|
||||
|
||||
`AND` can be used to create "prompt segments". By default, it works as if you had combined the different prompts with `ConditioningCombine`.
|
||||
|
||||
It is also used with regional prompting to separate different prompts; see `MASK` and `ATTN` below.
|
||||
It is also used with regional prompting, see `MASK` and `COUPLE` below.
|
||||
|
||||
Prompts can have a weight at the end:
|
||||
```
|
||||
@@ -123,14 +123,18 @@ Note that the `:` needs to be escaped with a `\` or it will be interpreted as sc
|
||||
Prompt operators are processed in the following order, meaning that all features "below" another can be affected by the feature above it. That is, `BREAK` can go inside a `TE()` call, but not `AND` or `CAT`.
|
||||
|
||||
- DEF macros are expanded
|
||||
- Scheduling is expanded
|
||||
- Prompts are split by AND
|
||||
- Most functions (like STYLE, MASK) and cutoffs are evaluated
|
||||
- prompts are split by AVG()
|
||||
- prompts are split by CAT
|
||||
- the TE() function is evaluated to set per-encoder prompts
|
||||
- BREAK is evaluated
|
||||
- Everything else
|
||||
- Scheduling is expanded, and for each scheduled prompt:
|
||||
- The prompt is split by AND, and for each:
|
||||
- Prompts are split by COUPLE. and for each:
|
||||
- Most functions (like MASK) and cutoffs are evaluated
|
||||
- prompts are split by `AVG()` or CAT
|
||||
- the TE() function is evaluated to set per-encoder prompts
|
||||
- BREAK is evaluated
|
||||
- Everything else
|
||||
- Prompts are combined with `ConditioningAverage` (for `AVG`) or `ConditioningConcat` (for `CAT`)
|
||||
- If coupled prompts exist, the base cond is set up for attention coupling and returned
|
||||
- Prompts split with `AND` are combined with `ConditioningCombine`
|
||||
- Each scheduled prompt is restricted to its effective range with `ConditioningSetTimestepRange`
|
||||
|
||||
## Functions
|
||||
|
||||
@@ -387,7 +391,7 @@ a "" b "$2"
|
||||
a "A" b "$2"
|
||||
```
|
||||
|
||||
## ATTN: Attention couple
|
||||
## COUPLE: Attention couple
|
||||
|
||||
See [here](doc/attention_couple.md)
|
||||
|
||||
|
||||
+66
-64
@@ -503,15 +503,9 @@ def apply_noise(cond, weight, gen):
|
||||
return cond * (1 - weight) + n * weight
|
||||
|
||||
|
||||
def encode_prompt(clip, text, start_pct, end_pct, defaults, masks):
|
||||
# First style modifier applies to ANDed prompts too unless overridden
|
||||
style, normalization, text = get_style(text)
|
||||
text, mask_size = get_mask_size(text, defaults)
|
||||
|
||||
prompts = [p.strip() for p in re.split(r"\bAND\b", text)]
|
||||
|
||||
p, sdxl_opts = get_sdxl(prompts[0], defaults)
|
||||
prompts[0] = p
|
||||
def process_settings(prompt, defaults, masks, mask_size, sdxl_opts):
|
||||
if "ATTN()" in prompt:
|
||||
raise ValueError("ATTN() no longer works and has been replaced by COUPLE()")
|
||||
|
||||
def weight(t):
|
||||
opts = {}
|
||||
@@ -526,55 +520,46 @@ def encode_prompt(clip, text, start_pct, end_pct, defaults, masks):
|
||||
|
||||
return w, opts, t
|
||||
|
||||
settings = {"prompt": prompt}
|
||||
|
||||
if "FILL()" in prompt:
|
||||
prompt = prompt.replace("FILL()", "")
|
||||
settings["x-promptcontrol.fill"] = True
|
||||
prompt, mask, mask_weight = get_mask(prompt, mask_size, masks)
|
||||
prompt, noise_w, generator = get_noise(prompt)
|
||||
prompt, area = get_area(prompt)
|
||||
prompt, local_sdxl_opts = get_sdxl(prompt, defaults)
|
||||
# Get weight last so other syntax doesn't interfere with it
|
||||
w, opts, prompt = weight(prompt)
|
||||
settings["strength"] = w
|
||||
settings.update(sdxl_opts)
|
||||
settings.update(local_sdxl_opts)
|
||||
if area:
|
||||
settings["area"] = area[0]
|
||||
settings["strength"] = area[1]
|
||||
settings["set_area_to_bounds"] = False
|
||||
if mask is not None:
|
||||
settings["mask"] = mask
|
||||
settings["mask_strength"] = mask_weight
|
||||
|
||||
if not w:
|
||||
settings = None
|
||||
return prompt, settings
|
||||
|
||||
|
||||
def encode_prompt(clip, text, start_pct, end_pct, defaults, masks):
|
||||
# First style modifier applies to ANDed prompts too unless overridden
|
||||
style, normalization, text = get_style(text)
|
||||
text, mask_size = get_mask_size(text, defaults)
|
||||
|
||||
prompts = [p.strip() for p in re.split(r"\bAND\b", text)]
|
||||
|
||||
p, sdxl_opts = get_sdxl(prompts[0], defaults)
|
||||
prompts[0] = p
|
||||
|
||||
conds = []
|
||||
# TODO: is this still needed?
|
||||
# scale = sum(abs(weight(p)[0]) for p in prompts if not ("AREA(" in p or "MASK(" in p))
|
||||
attnmasked_prompts = []
|
||||
fill = False
|
||||
for prompt in prompts:
|
||||
attn_couple = False
|
||||
prompt_has_fill = False
|
||||
if "ATTN()" in prompt:
|
||||
prompt = prompt.replace("ATTN()", "")
|
||||
attn_couple = True
|
||||
if "FILL()" in prompt:
|
||||
prompt = prompt.replace("FILL()", "")
|
||||
prompt_has_fill = True
|
||||
prompt, mask, mask_weight = get_mask(prompt, mask_size, masks)
|
||||
text, noise_w, generator = get_noise(text)
|
||||
prompt, area = get_area(prompt)
|
||||
prompt, local_sdxl_opts = get_sdxl(prompt, defaults)
|
||||
# Get weight last so other syntax doesn't interfere with it
|
||||
w, opts, prompt = weight(prompt)
|
||||
if not w:
|
||||
continue
|
||||
settings = {"prompt": prompt}
|
||||
settings["strength"] = w
|
||||
settings.update(sdxl_opts)
|
||||
settings.update(local_sdxl_opts)
|
||||
if area:
|
||||
settings["area"] = area[0]
|
||||
settings["strength"] = area[1]
|
||||
settings["set_area_to_bounds"] = False
|
||||
if mask is not None:
|
||||
settings["mask"] = mask
|
||||
settings["mask_strength"] = mask_weight
|
||||
|
||||
settings["start_percent"] = start_pct
|
||||
settings["end_percent"] = end_pct
|
||||
|
||||
x = encode_prompt_segment(clip, prompt, settings, style, normalization)
|
||||
if attn_couple:
|
||||
if prompt_has_fill:
|
||||
if attnmasked_prompts:
|
||||
log.warning("FILL() can only be used for the first prompt, ignoring")
|
||||
elif mask is not None:
|
||||
log.warning("MASK() and FILL() can't be used together, ignoring FILL()")
|
||||
else:
|
||||
fill = True
|
||||
attnmasked_prompts.extend(x)
|
||||
else:
|
||||
conds.extend(x)
|
||||
|
||||
def ensure_mask(c):
|
||||
if "mask" not in c[1]:
|
||||
@@ -583,20 +568,37 @@ def encode_prompt(clip, text, start_pct, end_pct, defaults, masks):
|
||||
c[1]["mask_strength"] = 1.0
|
||||
return c
|
||||
|
||||
if attnmasked_prompts:
|
||||
base_cond = attnmasked_prompts[0]
|
||||
if not fill:
|
||||
ensure_mask(base_cond)
|
||||
# else, set_cond_attnmask will have the base mask fill any unspecified areas
|
||||
def couple_mask(args):
|
||||
if args is None:
|
||||
return ""
|
||||
return f"MASK({args})"
|
||||
|
||||
for prompt in prompts:
|
||||
base_prompt, attn_couple_prompts = split_by_function(prompt, "COUPLE", defaults=None)
|
||||
|
||||
prompts = [base_prompt] + [couple_mask(p["args"]) + p["text"] for p in attn_couple_prompts]
|
||||
encoded = []
|
||||
for p in prompts:
|
||||
p, settings = process_settings(p, defaults, masks, mask_size, sdxl_opts)
|
||||
if settings is None: # weight = 0
|
||||
continue
|
||||
settings["start_percent"] = start_pct
|
||||
settings["end_percent"] = end_pct
|
||||
x = encode_prompt_segment(clip, p, settings, style, normalization)
|
||||
encoded.extend(x)
|
||||
|
||||
base_cond, *attention_couple = encoded
|
||||
base_cond = [base_cond]
|
||||
if len(attnmasked_prompts) > 1:
|
||||
if attention_couple:
|
||||
fill = base_cond[0][1].get("x-promptcontrol.fill")
|
||||
if not fill:
|
||||
ensure_mask(base_cond)
|
||||
# else, set_cond_attnmask will have the base mask fill any unspecified areas
|
||||
base_cond = set_cond_attnmask(
|
||||
base_cond,
|
||||
[ensure_mask(c) for c in attnmasked_prompts[1:]],
|
||||
[ensure_mask(c) for c in attention_couple],
|
||||
fill=fill,
|
||||
)
|
||||
else:
|
||||
log.warning("You must specify at least two prompt segments with ATTN() for attention couple to work")
|
||||
conds.extend(base_cond)
|
||||
|
||||
return conds
|
||||
|
||||
Reference in New Issue
Block a user