Files
M1kep-KepPromptLang/lib/actions/diff.py
T
Claude 6fcd3b8a86 Port to current ComfyUI CLIP API and refactor
Rebase the DSL text encoder on comfy.sd1_clip.SDClipModel and
comfy.clip_model.CLIPTextModel_, replacing the old HuggingFace
transformers CLIPTextModel/CLIPTextTransformer/CLIPTextEmbeddings
subclasses (which are no longer how ComfyUI implements CLIP).

Structure:
- Merge lib/action/ into lib/actions/; drop lib/fun_clip_stuff.py
  and the bundled clip_config*.json (comfy ships its own)
- Convert all custom_nodes.KepPromptLang.* absolute imports to
  relative imports so installs via Manager work regardless of
  install directory name

Encoder:
- PromptLangSDClipModel overrides encode_token_weights to walk
  the segment/action tree, assemble [B, seq, hidden] embeds, and
  call self.transformer(None, mask, embeds=..., num_tokens=...)
- posScale / postPos are supported without patching the transformer
  by pre-baking the delta (modified - default) into embeds, so the
  transformer's inline add yields the modified position embedding
- Drop the unused empty-baseline batch that was prepended and then
  sliced off; halves the per-encode forward pass for single prompts

Actions:
- Fix copy-pasted broken __repr__ / depth_repr across sum/diff/avg/
  slerp that referenced fields that didn't exist
- Fix NameError in AverageAction._validate_args (start_arg_token_length)
- Fix class-level mutable state in RandAction
- Unify _parse_scalar / _parse_scalar_weight / _parse_int into a
  single parse_numeric_arg helper in action_utils.py
- Share add_with_broadcast between SumAction and DiffAction
- Convert PostModifiers from TypedDict to dataclass (attribute
  access catches typos that .get() on string keys hides)

Drop the two _exp-pooler / _exp-pooledAvg actions: they recursively
invoked the HF CLIPTextTransformer and would need a rework to fit
the current CLIPTextModel_ interface. They were experimental and
not documented as stable.

BuildGif node:
- Collapse the 10-positional-arg _save_* helpers onto a small
  _SaveContext dataclass
- Stop mutating the input arg semantics (split_every_val was
  reassigned to len(images) when -1)

Tests:
- Add pytest suite covering the parser (grammar, nesting, errors)
  and every action (embedding math, shape, modifier payloads)
- conftest stubs ComfyUI at collection time so tests don't need a
  real ComfyUI install

Drop the broken test_files/ scripts (CI helpers, not a test suite)
and regenerate the README from tools/build_docs.py.
2026-04-12 05:05:58 +00:00

40 lines
1.2 KiB
Python

from typing import List
from torch import Tensor
from torch.nn import Embedding
from .action_utils import add_with_broadcast, concat_embeddings
from .base import MultiArgAction
from .types import SegOrAction
class DiffAction(MultiArgAction):
grammar = 'diff(" arg ("|" arg)* ")"'
display_name = "Difference"
action_name = "diff"
description = (
"Subtracts the segments in the order they are given. "
"The first segment is subtracted from the second, then the third from the result, and so on."
)
usage_examples = [
"diff(The cat is|The dog is)",
"diff(Cat|Dog)",
"sum(diff(king|man)|woman)",
]
def __init__(self, args: List[List[SegOrAction]]) -> None:
super().__init__(args)
self.base_arg = args[0]
self.additional_args = args[1:]
def token_length(self) -> int:
return sum(s.token_length() for s in self.base_arg)
def get_result(self, embedding_module: Embedding) -> Tensor:
result = concat_embeddings(self.base_arg, embedding_module)
for arg in self.additional_args:
arg_embedding = concat_embeddings(arg, embedding_module)
result = add_with_broadcast(result, arg_embedding, op="sub")
return result