diff --git a/more_math/AudioMathNode.py b/more_math/AudioMathNode.py index 85835fe..ef3d1ba 100644 --- a/more_math/AudioMathNode.py +++ b/more_math/AudioMathNode.py @@ -3,7 +3,7 @@ from antlr4 import CommonTokenStream, InputStream from .Parser.MathExprParser import MathExprParser from .Parser.MathExprLexer import MathExprLexer from .Parser.TensorEvalVisitor import TensorEvalVisitor -from .helper_functions import getIndexTensorAlongDim +from .helper_functions import getIndexTensorAlongDim, comonLazy from comfy_api.latest import io @@ -33,20 +33,22 @@ class AudioMathNode(io.ComfyNode): display_name="Audio math", inputs=[ io.Audio.Input(id="a", tooltip="Input audio tensor"), - io.Audio.Input(id="b", optional=True, tooltip="Second input audio tensor"), - io.Audio.Input(id="c", optional=True, tooltip="Third input audio tensor"), - io.Audio.Input(id="d", optional=True, tooltip="Fourth input audio tensor"), - io.Float.Input(id="w", default=0.0, optional=True, force_input=True), - io.Float.Input(id="x", default=0.0, optional=True, force_input=True), - io.Float.Input(id="y", default=0.0, optional=True, force_input=True), - io.Float.Input(id="z", default=0.0, optional=True, force_input=True), + io.Audio.Input(id="b", optional=True,lazy=True, tooltip="Second input audio tensor"), + io.Audio.Input(id="c", optional=True,lazy=True, tooltip="Third input audio tensor"), + io.Audio.Input(id="d", optional=True,lazy=True, tooltip="Fourth input audio tensor"), + io.Float.Input(id="w", default=0.0, optional=True,lazy=True, force_input=True), + io.Float.Input(id="x", default=0.0, optional=True,lazy=True, force_input=True), + io.Float.Input(id="y", default=0.0, optional=True,lazy=True, force_input=True), + io.Float.Input(id="z", default=0.0, optional=True,lazy=True, force_input=True), io.String.Input(id="AudioExpr", default="a*(1-w)+b*w", tooltip="Expression to apply on input audio tensors"), ], outputs=[ io.Audio.Output(), ], ) - + @classmethod + def check_lazy_status(cls, AudioExpr, a, b=[], c=[], d=[],w=0,x=0,y=0,z=0): + return comonLazy(AudioExpr, a, b, c, d) @classmethod def execute(cls, a, AudioExpr, b=None, c=None, d=None, w=0.0, x=0.0, y=0.0, z=0.0): diff --git a/more_math/ClipMathNode.py b/more_math/ClipMathNode.py index e14db7f..a5bde56 100644 --- a/more_math/ClipMathNode.py +++ b/more_math/ClipMathNode.py @@ -1,6 +1,7 @@ from comfy_api.latest import io from .modelLikeCommon import calculate_patches from inspect import cleandoc +from .helper_functions import comonLazy class CLIPMathNode(io.ComfyNode): """ @@ -14,13 +15,13 @@ class CLIPMathNode(io.ComfyNode): category="More math", inputs=[ io.Clip.Input(id="a", tooltip="Main CLIP (base)"), - io.Clip.Input(id="b", optional=True, tooltip="Optional 2nd CLIP"), - io.Clip.Input(id="c", optional=True, tooltip="Optional 3rd CLIP"), - io.Clip.Input(id="d", optional=True, tooltip="Optional 4th CLIP"), - io.Float.Input(id="w", default=0.0, optional=True, force_input=True), - io.Float.Input(id="x", default=0.0, optional=True, force_input=True), - io.Float.Input(id="y", default=0.0, optional=True, force_input=True), - io.Float.Input(id="z", default=0.0, optional=True, force_input=True), + io.Clip.Input(id="b", optional=True,lazy=True, tooltip="Optional 2nd CLIP"), + io.Clip.Input(id="c", optional=True,lazy=True, tooltip="Optional 3rd CLIP"), + io.Clip.Input(id="d", optional=True,lazy=True, tooltip="Optional 4th CLIP"), + io.Float.Input(id="w", default=0.0, optional=True,lazy=True, force_input=True), + io.Float.Input(id="x", default=0.0, optional=True,lazy=True, force_input=True), + io.Float.Input(id="y", default=0.0, optional=True,lazy=True, force_input=True), + io.Float.Input(id="z", default=0.0, optional=True,lazy=True, force_input=True), io.String.Input(id="Model", default="a*(1-w)+b*w", tooltip="Expression to apply on weights"), ], outputs=[ @@ -29,6 +30,9 @@ class CLIPMathNode(io.ComfyNode): ) tooltip = cleandoc(__doc__) + @classmethod + def check_lazy_status(cls, Model, a, b=[], c=[], d=[],w=0,x=0,y=0,z=0): + return comonLazy(Model, a, b, c, d) @classmethod def execute(cls, Model, a, b=None, c=None, d=None, w=0.0, x=0.0, y=0.0, z=0.0) -> io.NodeOutput: diff --git a/more_math/ConditioningMathNode.py b/more_math/ConditioningMathNode.py index d93b8b6..973b73e 100644 --- a/more_math/ConditioningMathNode.py +++ b/more_math/ConditioningMathNode.py @@ -5,6 +5,7 @@ import torch from .Parser.MathExprParser import MathExprParser from .Parser.MathExprLexer import MathExprLexer from .Parser.TensorEvalVisitor import TensorEvalVisitor +from .helper_functions import ThrowingErrorListener, comonLazy from comfy_api.latest import io @@ -36,13 +37,13 @@ class ConditioningMathNode(io.ComfyNode): category="More math", inputs=[ io.Conditioning.Input(id="a"), - io.Conditioning.Input(id="b", optional=True), - io.Conditioning.Input(id="c", optional=True), - io.Conditioning.Input(id="d", optional=True), - io.Float.Input(id="w", default=0.0,optional=True, force_input=True), - io.Float.Input(id="x", default=0.0,optional=True, force_input=True), - io.Float.Input(id="y", default=0.0,optional=True, force_input=True), - io.Float.Input(id="z", default=0.0,optional=True, force_input=True), + io.Conditioning.Input(id="b", optional=True,lazy=True), + io.Conditioning.Input(id="c", optional=True,lazy=True), + io.Conditioning.Input(id="d", optional=True,lazy=True), + io.Float.Input(id="w", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="x", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="y", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="z", default=0.0,optional=True,lazy=True, force_input=True), io.String.Input(id="Tensor", default="a*(1-w)+b*w", tooltip="Describes composition of the image."), io.String.Input(id="pooled_output", default="a*(1-w)+b*w", tooltip="Composition of the image condensed into one vector"), ], @@ -55,7 +56,9 @@ class ConditioningMathNode(io.ComfyNode): #OUTPUT_NODE = False #OUTPUT_TOOLTIPS = ("",) # Tooltips for the output node - + @classmethod + def check_lazy_status(cls, Tensor,pooled_output, a, b=[], c=[], d=[],w=0,x=0,y=0,z=0): + return list(set(comonLazy(Tensor, a, b, c, d)).union(comonLazy(pooled_output, a, b, c, d))) @classmethod def execute(cls, Tensor,pooled_output, a, b=None, c=None, d=None,w=0.0,x=0.0,y=0.0,z=0.0): if b is None: @@ -87,6 +90,7 @@ class ConditioningMathNode(io.ComfyNode): lexer = MathExprLexer(input_stream) stream = CommonTokenStream(lexer) parser = MathExprParser(stream) + parser.addErrorListener(ThrowingErrorListener()) tree = parser.expr() visitor = TensorEvalVisitor(variables,ta.shape) result1 = visitor.visit(tree) diff --git a/more_math/FloatMathNode.py b/more_math/FloatMathNode.py index 2a04da9..4ee1e5f 100644 --- a/more_math/FloatMathNode.py +++ b/more_math/FloatMathNode.py @@ -2,12 +2,11 @@ from inspect import cleandoc from antlr4 import CommonTokenStream, InputStream -from .helper_functions import ThrowingErrorListener +from .helper_functions import ThrowingErrorListener, comonLazy from .Parser.MathExprParser import MathExprParser from .Parser.MathExprLexer import MathExprLexer from .Parser.FloatEvalVisitor import FloatEvalVisitor - from comfy_api.latest import io class FloatMathNode(io.ComfyNode): @@ -27,7 +26,9 @@ class FloatMathNode(io.ComfyNode): """ def __init__(self): pass - + @classmethod + def check_lazy_status(cls, Model, a, b=[], c=[], d=[],w=0,x=0,y=0,z=0): + return comonLazy(Model, a, b, c, d) @classmethod def define_schema(cls) -> io.Schema: """ @@ -38,13 +39,13 @@ class FloatMathNode(io.ComfyNode): display_name="Float math", inputs=[ io.Float.Input(id="a", force_input=True), - io.Float.Input(id="b", default=0.0,optional=True, force_input=True), - io.Float.Input(id="c", default=0.0,optional=True, force_input=True), - io.Float.Input(id="d", default=0.0,optional=True, force_input=True), - io.Float.Input(id="w", default=0.0,optional=True, force_input=True), - io.Float.Input(id="x", default=0.0,optional=True, force_input=True), - io.Float.Input(id="y", default=0.0,optional=True, force_input=True), - io.Float.Input(id="z", default=0.0,optional=True, force_input=True), + io.Float.Input(id="b", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="c", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="d", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="w", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="x", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="y", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="z", default=0.0,optional=True,lazy=True, force_input=True), io.String.Input(id="FloatFunc", default="a*(1-w)+b*w", tooltip="Expression to use on inputs"), ], outputs=[ @@ -58,10 +59,11 @@ class FloatMathNode(io.ComfyNode): #OUTPUT_NODE = False #OUTPUT_TOOLTIPS = ("",) # Tooltips for the output node @classmethod + def check_lazy_status(cls, FloatFunc, a, b=[], c=[], d=[],w=0,x=0,y=0,z=0): + return comonLazy(FloatFunc, a, b, c, d) + @classmethod def execute(cls, FloatFunc, a, b=0.0, c=0.0, d=0.0, w=0.0, x=0.0, y=0.0, z=0.0): - - variables = {'a': a, 'b': b, 'c': c, 'd': d, 'w': w, 'x': x, 'y': y, 'z': z} input_stream = InputStream(FloatFunc) lexer = MathExprLexer(input_stream) diff --git a/more_math/ImageMathNode.py b/more_math/ImageMathNode.py index 7efd1f8..53e79e0 100644 --- a/more_math/ImageMathNode.py +++ b/more_math/ImageMathNode.py @@ -2,7 +2,7 @@ from antlr4 import CommonTokenStream from antlr4.atn.LexerActionExecutor import InputStream import torch -from .helper_functions import ThrowingErrorListener, getIndexTensorAlongDim +from .helper_functions import ThrowingErrorListener, getIndexTensorAlongDim, comonLazy from .Parser.MathExprParser import MathExprParser from .Parser.MathExprLexer import MathExprLexer @@ -38,20 +38,22 @@ class ImageMathNode(io.ComfyNode): display_name="Image math", inputs=[ io.Image.Input(id="a"), - io.Image.Input(id="b", optional=True), - io.Image.Input(id="c", optional=True), - io.Image.Input(id="d", optional=True), - io.Float.Input(id="w", default=0.0,optional=True, force_input=True), - io.Float.Input(id="x", default=0.0,optional=True, force_input=True), - io.Float.Input(id="y", default=0.0,optional=True, force_input=True), - io.Float.Input(id="z", default=0.0,optional=True, force_input=True), + io.Image.Input(id="b", optional=True,lazy=True), + io.Image.Input(id="c", optional=True,lazy=True), + io.Image.Input(id="d", optional=True,lazy=True), + io.Float.Input(id="w", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="x", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="y", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="z", default=0.0,optional=True,lazy=True, force_input=True), io.String.Input(id="Image", default="a*(1-w)+b*w", tooltip="Expression to apply on input images"), ], outputs=[ io.Image.Output(), ], ) - + @classmethod + def check_lazy_status(cls, Image, a, b=[], c=[], d=[],w=0,x=0,y=0,z=0): + return comonLazy(Image, a, b, c, d) @classmethod def execute(scls, Image, a, b=None, c=None, d=None, w=0.0, x=0.0, y=0.0, z=0.0): b = torch.zeros_like(a) if b is None else b diff --git a/more_math/LatentMathNode.py b/more_math/LatentMathNode.py index 588a688..e714149 100644 --- a/more_math/LatentMathNode.py +++ b/more_math/LatentMathNode.py @@ -5,7 +5,7 @@ from comfy_api.latest import io from antlr4 import CommonTokenStream, InputStream import torch -from .helper_functions import ThrowingErrorListener, getIndexTensorAlongDim +from .helper_functions import ThrowingErrorListener, getIndexTensorAlongDim,comonLazy from .Parser.MathExprParser import MathExprParser from .Parser.MathExprLexer import MathExprLexer @@ -50,10 +50,10 @@ class LatentMathNode(io.ComfyNode): io.Latent.Input(id="b", optional=True, lazy=True), io.Latent.Input(id="c", optional=True, lazy=True), io.Latent.Input(id="d", optional=True, lazy=True), - io.Float.Input(id="w", default=0.0,optional=True, force_input=True), - io.Float.Input(id="x", default=0.0,optional=True, force_input=True), - io.Float.Input(id="y", default=0.0,optional=True, force_input=True), - io.Float.Input(id="z", default=0.0,optional=True, force_input=True), + io.Float.Input(id="w", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="x", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="y", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="z", default=0.0,optional=True,lazy=True, force_input=True), io.String.Input(id="Latent", default="a*(1-w)+b*w", tooltip="Expression to apply on input latents"), ], outputs=[ @@ -66,7 +66,9 @@ class LatentMathNode(io.ComfyNode): #OUTPUT_NODE = False #OUTPUT_TOOLTIPS = ("",) # Tooltips for the output node - + @classmethod + def check_lazy_status(cls, Latent, a, b=[], c=[], d=[],w=0,x=0,y=0,z=0): + return comonLazy(Latent, a, b, c, d) @classmethod def execute(cls, Latent, a, b=None, c=None, d=None, w=0.0, x=0.0, y=0.0, z=0.0) -> io.NodeOutput: # Extract raw sample tensors (may be Tensor or NestedTensor) diff --git a/more_math/ModelMathNode.py b/more_math/ModelMathNode.py index 40e45a2..6260680 100644 --- a/more_math/ModelMathNode.py +++ b/more_math/ModelMathNode.py @@ -1,11 +1,8 @@ from inspect import cleandoc from comfy_api.latest import io -from antlr4 import CommonTokenStream, InputStream -from .Parser.MathExprLexer import MathExprLexer from .helper_functions import comonLazy from .modelLikeCommon import calculate_patches -import copy class ModelMathNode(io.ComfyNode): diff --git a/more_math/NoiseMathNode.py b/more_math/NoiseMathNode.py index 833c0ae..841b36d 100644 --- a/more_math/NoiseMathNode.py +++ b/more_math/NoiseMathNode.py @@ -3,7 +3,7 @@ from inspect import cleandoc from antlr4 import CommonTokenStream, InputStream import torch -from .helper_functions import ThrowingErrorListener, getIndexTensorAlongDim +from .helper_functions import ThrowingErrorListener, getIndexTensorAlongDim,comonLazy from .Parser.MathExprParser import MathExprParser from .Parser.MathExprLexer import MathExprLexer @@ -43,13 +43,13 @@ class NoiseMathNode(io.ComfyNode): display_name="Noise math", inputs=[ io.Noise.Input(id="a"), - io.Noise.Input(id="b", optional=True), - io.Noise.Input(id="c", optional=True), - io.Noise.Input(id="d", optional=True), - io.Float.Input(id="w", default=0.0,optional=True, force_input=True), - io.Float.Input(id="x", default=0.0,optional=True, force_input=True), - io.Float.Input(id="y", default=0.0,optional=True, force_input=True), - io.Float.Input(id="z", default=0.0,optional=True, force_input=True), + io.Noise.Input(id="b", optional=True,lazy=True), + io.Noise.Input(id="c", optional=True,lazy=True), + io.Noise.Input(id="d", optional=True,lazy=True), + io.Float.Input(id="w", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="x", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="y", default=0.0,optional=True,lazy=True, force_input=True), + io.Float.Input(id="z", default=0.0,optional=True,lazy=True, force_input=True), io.String.Input(id="Noise", default="a*(1-w)+b*w", tooltip="Expression to apply on input noise generators"), ], outputs=[ @@ -62,7 +62,9 @@ class NoiseMathNode(io.ComfyNode): #OUTPUT_TOOLTIPS = ("",) # Tooltips for the output node CATEGORY = "More math" - + @classmethod + def check_lazy_status(cls, Noise, a, b=[], c=[], d=[],w=0,x=0,y=0,z=0): + return comonLazy(Noise, a, b, c, d) @classmethod def execute(cls, Noise, a, b=None, c=None, d=None, w=0.0, x=0.0, y=0.0, z=0.0): return (NoiseExecutor(a, b, c, d, w, x, y, z, Noise),) diff --git a/more_math/VaeMathNode.py b/more_math/VaeMathNode.py index babf8a3..450439e 100644 --- a/more_math/VaeMathNode.py +++ b/more_math/VaeMathNode.py @@ -2,6 +2,7 @@ from inspect import cleandoc from comfy_api.latest import io import copy from .modelLikeCommon import calculate_patches +from .helper_functions import comonLazy class VAEMathNode(io.ComfyNode): """ @@ -15,13 +16,13 @@ class VAEMathNode(io.ComfyNode): category="More math", inputs=[ io.Vae.Input(id="a", tooltip="Main VAE (base)"), - io.Vae.Input(id="b", optional=True, tooltip="Optional 2nd VAE"), - io.Vae.Input(id="c", optional=True, tooltip="Optional 3rd VAE"), - io.Vae.Input(id="d", optional=True, tooltip="Optional 4th VAE"), - io.Float.Input(id="w", default=0.0, optional=True, force_input=True), - io.Float.Input(id="x", default=0.0, optional=True, force_input=True), - io.Float.Input(id="y", default=0.0, optional=True, force_input=True), - io.Float.Input(id="z", default=0.0, optional=True, force_input=True), + io.Vae.Input(id="b", optional=True,lazy=True, tooltip="Optional 2nd VAE"), + io.Vae.Input(id="c", optional=True,lazy=True, tooltip="Optional 3rd VAE"), + io.Vae.Input(id="d", optional=True,lazy=True, tooltip="Optional 4th VAE"), + io.Float.Input(id="w", default=0.0,lazy=True, optional=True, force_input=True), + io.Float.Input(id="x", default=0.0,lazy=True, optional=True, force_input=True), + io.Float.Input(id="y", default=0.0,lazy=True, optional=True, force_input=True), + io.Float.Input(id="z", default=0.0,lazy=True, optional=True, force_input=True), io.String.Input(id="Model", default="a*(1-w)+b*w", tooltip="Expression to apply on weights"), ], outputs=[ @@ -30,7 +31,9 @@ class VAEMathNode(io.ComfyNode): ) tooltip = cleandoc(__doc__) - + @classmethod + def check_lazy_status(cls, Model, a, b=[], c=[], d=[],w=0,x=0,y=0,z=0): + return comonLazy(Model, a, b, c, d) @classmethod def execute(cls, Model, a, b=None, c=None, d=None, w=0.0, x=0.0, y=0.0, z=0.0) -> io.NodeOutput: patcher_a = a.patcher diff --git a/more_math/helper_functions.py b/more_math/helper_functions.py index c1cf2e2..16b1fb8 100644 --- a/more_math/helper_functions.py +++ b/more_math/helper_functions.py @@ -1,5 +1,8 @@ from antlr4.error.ErrorListener import ErrorListener from antlr4 import InputStream +from .Parser.MathExprLexer import MathExprLexer +from .Parser.MathExprParser import MathExprParser +from antlr4 import CommonTokenStream import torch @@ -31,7 +34,7 @@ def freq_to_time(element: torch.Tensor) -> torch.Tensor: class ThrowingErrorListener(ErrorListener): def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e): - raise ValueError(f"Syntax error in AudioExpr at line {line}, col {column}: {msg}") + raise ValueError(f"Syntax error in expression at line {line}, col {column}: {msg}") def comonLazy(expr, a, b=None, c=None, d=None, w=0.0, x=0.0, y=0.0, z=0.0):