Skip to content
Node packVerifiedExternal
Open source

ComfyUI-Apt_UnitPromptWeight

A powerful ComfyUI node for precise control of prompt weights

Maintainer@cardenluoUnratedgrade

Package profile

Status
Verified
Publisher
@cardenluo
Source
GitHub
Stars
8

README

中文 | English

Unit Prompt Weight

A powerful ComfyUI node for precise control of prompt weights, supporting multiple processing modes and visual weight allocation.

Core Working Principle

Weight Parsing Algorithm

The Unit Prompt Weight node employs an advanced Semantic Weight Dynamic Allocation Algorithm, which achieves precise control of prompt weights through a multi-stage processing flow:

  1. Regular Expression Pattern Matching: Uses an optimized regular expression \[([^\]]+)@([0-9.]+)\] for semantic-weight pair identification, which can accurately capture nested structures and edge cases.

  2. Text Segmentation Processing: Adopts Adaptive Segment Segmentation strategy, which can intelligently identify structured information in prompts. The algorithm first identifies core semantic units with weight markers (such as [[email protected]]) through regular expressions, then processes unmarked parts as context units with default weight (1.0). This method ensures accurate capture of user semantic intent even in complex prompt structures, while preserving natural language coherence. The segmentation process also includes text cleaning steps to automatically remove excess punctuation and whitespace characters, optimizing subsequent encoding efficiency.

  3. Weight Vector Normalization: Executes L1-Norm Normalization, a key technique for maintaining numerical stability in high-dimensional feature spaces. The algorithm first calculates the absolute sum of all weight values, then divides each weight by this sum to ensure the normalized weight vector satisfies the unit L1 norm constraint. This normalization method is particularly suitable for prompt weight adjustment scenarios as it maintains the relative proportional relationships of weights while preventing numerical overflow or gradient vanishing problems. When all weights are detected to be equal, the algorithm automatically switches to uniform distribution mode, simplifying the calculation process.

  4. Independent Feature Encoding: Performs CLIP Context-Aware Encoding on each semantic segment, which fully leverages the powerful semantic understanding capabilities of the CLIP model. Each segment is individually input into CLIP's text encoder to generate 768-dimensional high-dimensional feature representations. The encoding process preserves the complete contextual information of each semantic unit, ensuring that combined concepts like "girl with umbrella" are correctly understood as a whole rather than a simple bag-of-words model. For encoding failures, the algorithm implements graceful degradation processing to ensure the robustness of the entire process.

  5. Weighted Feature Fusion: Applies Linear Weighted Fusion strategy, an advanced technique for achieving precise semantic weight control in feature space. The algorithm first performs alignment processing on feature vectors of different lengths (especially in qwen-image mode), then executes convex combination operations based on normalized weight vectors. This fusion method ensures that the final conditional embedding can accurately reflect the user's weight intentions while maintaining the geometric properties of the feature space. The fusion process also implements balance control between the overall prompt and individual weighted segments, providing additional adjustment freedom through the main_prompt_ratio parameter.

Feature Fusion Mechanism

The node implements two key feature fusion mechanisms:

  1. Individual Feature Fusion: Fuses features of each segment with different weights according to weight proportions
  2. Overall and Individual Balance: Controls the balance between the overall prompt and individual weighted segments through the main_prompt_ratio parameter

Features

  • **P

Nodes in this pack

1 node
  • Unit_PromptWeight

Sources

3 sources
GitHubSourceVersion 28f68cb78b96d61e28ffa66ad9ea7958636d4433
github.com/cardenluo/ComfyUI-Apt_UnitPromptWeight

Verified Oct 10

Source excerpts

1 excerpt

Source context: Repo cardenluo/ComfyUI-Apt_UnitPromptWeight