ComfyUI-latent-eval
ZEval is a Python library and ComfyUI custom node that allows you to perform mathematical operations and transformations on latent tensors using a domain-specific language (DSL).
ZEval - Latent Space Expression Evaluator
ZEval is a Python library and ComfyUI custom node that allows you to perform mathematical operations and transformations on latent tensors using a domain-specific language (DSL). It enables complex manipulations of diffusion model latents through simple expression syntax.
Features
- Expression-based Operations: Define latent transformations using intuitive mathematical expressions
- ComfyUI Integration: Includes a custom node for seamless integration into ComfyUI workflows
- Multiple Latent Support: Work with multiple input latents simultaneously
- Rich Operation Set: Supports arithmetic operations, channel mapping, clamping, activation functions, and more
- Dynamic Resolution Matching: Automatically interpolates multiple latents to match the resolution of the first
Installation
To install ZEval, ensure you have Python and PyTorch installed, then install the dependencies listed in requirements.txt.
Usage
Basic Usage
The main interface is through the parse() function which converts expressions into executable operations:
from zeval import parse, LatentMap
import torch
# Create some example latents
l1 = torch.randn(1, 4, 64, 64)
l2 = torch.randn(1, 4, 64, 64)
# Create a latent map
lm = LatentMap.create([l1, l2])
# Parse and execute an expression
ast = parse("l1 + l2 * 0.5")
result = ast(lm)
ComfyUI Node
In ComfyUI, the ZEvalNode accepts multiple latent inputs and an expression string. The latents are automatically named l1, l2, etc., corresponding to latent_1, latent_2, etc.
Operations
ZEval supports the following operations:
- Arithmetic:
+,-,*,/ - Assignment:
name = expression; - Functions:
noise()- Generate random noisechanmap(expr, ch1, ch2, ...)- Map channels (use-for no connection)clamp(expr, min, max)- Clamp values between min and maxroll(expr, shifts, dim)- Roll tensor along dimensionshuffle(expr)- Shuffle channels randomlyflip(expr, dim)- Flip tensor along dimensionsigmoid(expr)- Apply sigmoid activationrelu(expr)- Apply ReLU activationzoom(expr, scale)- Resize latent by scale factorresize(height, width)- Resize latent to specific dimensions
Examples
Here are some practical examples of expressions you can use:
Simple Arithmetic
l1 + l2 * 0.3
Blends the first latent with 30% of the second latent.
Noise Addition
l1 + noise() * 0.1
Adds 10% noise to the first latent.
Channel Mapping
chanmap(l1, 0, 1, 2, -)
Maps the first three channels of l1 and sets the fourth channel to zero.
Clamping Values
clamp(l1, -1.0, 1.0)
Clamps all values in the latent between -1.0 and 1.0.
Complex Expression
a = l1 * 0.7;
b = l2 * 0.3;
clamp(a + b, -2.0, 2.0);
sigmoid(l1)
Assigns weighted values to variables, adds them together, clamps the result, and applies sigmoid.
Using Activation Functions
relu(l1) + sigmoid(l2)
Applies ReLU to the first latent and sigmoid to the second, then adds them.
License
This project is licensed under the terms found in the LICENSE file.