Skip to content

Commit

Permalink
Installable
Browse files Browse the repository at this point in the history
  • Loading branch information
doctorpangloss committed Jun 11, 2024
1 parent 9d38c33 commit ceaa75d
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 123 deletions.
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Installable fork of ComfyUI ELLA nodes. This uses the built-in T5 features of upstream.

# ComfyUI-ELLA

<div align="center">
Expand Down
File renamed without changes.
File renamed without changes.
116 changes: 46 additions & 70 deletions ella.py → comfyui_ella/ella.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import logging
import os
from typing import Dict
from typing import Dict, Optional

import folder_paths
import torch
from comfy import model_management, samplers
from comfy.conds import CONDCrossAttn

from comfy import samplers
from comfy.cmd.folder_paths import add_model_folder_path
from comfy.conds import CONDCrossAttn
from comfy.model_downloader import KNOWN_HUGGINGFACE_MODEL_REPOS, get_or_download, get_filename_list_with_downloadable, \
add_known_models, KNOWN_CHECKPOINTS
from comfy.model_downloader_types import HuggingFile, CivitFile
from comfy_extras.nodes.nodes_language import TransformersLoader
from .model import ELLA, T5TextEmbedder

ELLA_TYPE = "ELLA"
Expand All @@ -17,17 +20,18 @@
APPLY_MODE_ELLA_AND_CLIP = "ELLA + CLIP"

# set the models directory
if "ella" not in folder_paths.folder_names_and_paths:
current_paths = [os.path.join(folder_paths.models_dir, "ella")]
else:
current_paths, _ = folder_paths.folder_names_and_paths["ella"]
folder_paths.folder_names_and_paths["ella"] = (current_paths, folder_paths.supported_pt_extensions)
FOLDER_NAME = "ella"
KNOWN_HUGGINGFACE_MODEL_REPOS.add("google/flan-t5-xl")
add_model_folder_path(FOLDER_NAME)

if "ella_encoder" not in folder_paths.folder_names_and_paths:
current_paths = [os.path.join(folder_paths.models_dir, "ella_encoder")]
else:
current_paths, _ = folder_paths.folder_names_and_paths["ella_encoder"]
folder_paths.folder_names_and_paths["ella_encoder"] = (current_paths, folder_paths.supported_pt_extensions)
KNOWN_ELLA_CHECKPOINTS = [
HuggingFile("QQGYLab/ELLA", "ella-sd1.5-tsc-t5xl.safetensors")
]

add_known_models("checkpoints", KNOWN_CHECKPOINTS,
# from example workflow
CivitFile(84476, 156202, "awpainting_v12.safetensors")
)


def ella_encode(ella: ELLA, timesteps: torch.Tensor, embeds: dict):
Expand All @@ -49,13 +53,13 @@ def ella_encode(ella: ELLA, timesteps: torch.Tensor, embeds: dict):

class EllaProxyUNet:
def __init__(
self,
ella: ELLA,
model_sampling,
positive,
negative,
mode=APPLY_MODE_ELLA_ONLY,
**kwargs,
self,
ella: ELLA,
model_sampling,
positive,
negative,
mode=APPLY_MODE_ELLA_ONLY,
**kwargs,
) -> None:
self.ella = ella
self.model_sampling = model_sampling
Expand Down Expand Up @@ -143,14 +147,14 @@ def INPUT_TYPES(cls):
CATEGORY = "ella/apply"

def apply(
self,
model,
ella,
positive,
negative,
sigmas=None,
mode=APPLY_MODE_ELLA_AND_CLIP,
**kwargs,
self,
model,
ella,
positive,
negative,
sigmas=None,
mode=APPLY_MODE_ELLA_AND_CLIP,
**kwargs,
):
model_clone = model.clone()
model_sampling = model_clone.get_model_object("model_sampling")
Expand Down Expand Up @@ -266,7 +270,7 @@ def INPUT_TYPES(cls):
},
"optional": {
"clip": ("CLIP", {"default": None}),
"text_clip": ("STRING", {"default":"", "multiline": True, "dynamicPrompts": True}),
"text_clip": ("STRING", {"default": "", "multiline": True, "dynamicPrompts": True}),
},
}

Expand Down Expand Up @@ -307,12 +311,13 @@ def concat(self, conditioning_to, conditioning_from):

for i in range(len(conditioning_to)):
t1 = conditioning_to[i][0]
tw = torch.cat((t1, cond_from),1)
tw = torch.cat((t1, cond_from), 1)
n = [tw, conditioning_to[i][1].copy()]
out.append(n)

return out


"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Loaders
Expand All @@ -325,7 +330,7 @@ class ELLALoader:
def INPUT_TYPES(cls):
return {
"required": {
"name": (folder_paths.get_filename_list("ella"),),
"name": (get_filename_list_with_downloadable(FOLDER_NAME, KNOWN_ELLA_CHECKPOINTS),),
},
}

Expand All @@ -334,52 +339,23 @@ def INPUT_TYPES(cls):
CATEGORY = "ella/loaders"

def load(self, name: str, **kwargs):
ella_file = folder_paths.get_full_path("ella", name)
ella_file = get_or_download("ella", name, KNOWN_ELLA_CHECKPOINTS)
if not ella_file:
raise ValueError("ELLA ckpt not found")
ella = ELLA(ella_file)
return ({"model": ella, "file": ella_file},)


class T5TextEncoderLoader:
@classmethod
def INPUT_TYPES(cls):
paths = []
for search_path in folder_paths.get_folder_paths("ella_encoder"):
if os.path.exists(search_path):
for root, _, files in os.walk(search_path, followlinks=True):
if "config.json" in files:
paths.append(os.path.relpath(root, start=search_path))
return {
"required": {
"name": (paths,),
"max_length": ("INT", {"default": 0, "min": 0, "max": 128, "step": 16}),
"dtype": (["auto", "FP32", "FP16"],),
}
}

class T5TextEncoderLoader(TransformersLoader):
RETURN_TYPES = ("T5_TEXT_ENCODER",)
FUNCTION = "load"
CATEGORY = "ella/loaders"

def load(self, name: str, max_length: int = 0, dtype="auto", **kwargs):
t5_file = folder_paths.get_full_path("ella_encoder", name)
# "flexible_token_length" trick: Set `max_length=None` eliminating any text token padding or truncation.
# Help improve the quality of generated images corresponding to short captions.
for search_path in folder_paths.get_folder_paths("ella_encoder"):
if os.path.exists(search_path):
path = os.path.join(search_path, name)
if os.path.exists(path):
t5_file = path
break
if dtype == "auto":
dtype = model_management.text_encoder_dtype(model_management.text_encoder_device())
elif dtype == "FP16":
dtype = torch.float16
else:
dtype = torch.float32
t5_encoder = T5TextEmbedder(t5_file, max_length=max_length or None, dtype=dtype) # type: ignore
return ({"model": t5_encoder, "file": t5_file},)
def load(self, ckpt_name: str, subfolder: Optional[str] = None, *args, **kwargs):
t5_model, = super().execute(ckpt_name, subfolder, args, kwargs)

t5_encoder = T5TextEmbedder(t5_model, max_length=None) # type: ignore
return ({"model": t5_encoder, "file": None},)


"""
Expand Down Expand Up @@ -486,7 +462,7 @@ def set_timesteps(self, model, ella, scheduler, steps, denoise, sigmas=None):
if denoise <= 0.0:
return (torch.FloatTensor([]),)
total_steps = int(steps / denoise)
sigmas = samplers.calculate_sigmas(model_sampling, scheduler, total_steps).cpu()[-(steps + 1) :]
sigmas = samplers.calculate_sigmas(model_sampling, scheduler, total_steps).cpu()[-(steps + 1):]
timesteps = model_sampling.timestep(sigmas)
return ({**ella, "timesteps": timesteps},)

Expand Down
Loading

0 comments on commit ceaa75d

Please sign in to comment.