Models / flux_embedding_node.py
Dervlex's picture
Upload 3 files
7f2508e verified
Raw History Blame Contribute Delete
2.49 kB
import torch
from diffusers import DiffusionPipeline, FluxTransformer2DModel
from torchao.quantization import quantize_, int8_weight_only
from sd_embed.embedding_funcs import get_weighted_text_embeddings_flux1
class WeightedFluxEmbeddings:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model_path": ("STRING", {"default":"black-forest-labs/FLUX.1-dev"}),
"prompt": ("STRING", {"multiline":True}),
"quantize": ("BOOLEAN", {"default": True}),
}
}
RETURN_TYPES = ("CONDITIONING",) # Note: Flux only returns positive conditioning
FUNCTION = "run"
CATEGORY = "embedding/FLUX"
_PIPELINE_CACHE = {}
def run(self, model_path, prompt, quantize):
# load or re-use the FLUX pipeline
pipe = self._PIPELINE_CACHE.get(model_path)
if pipe is None:
if quantize:
transformer = FluxTransformer2DModel.from_pretrained(
model_path,
subfolder="transformer",
torch_dtype=torch.bfloat16
)
quantize_(transformer, int8_weight_only())
pipe = DiffusionPipeline.from_pretrained(
model_path,
transformer=transformer,
torch_dtype=torch.bfloat16
)
pipe.enable_model_cpu_offload()
else:
pipe = DiffusionPipeline.from_pretrained(
model_path,
torch_dtype=torch.bfloat16
)
pipe.to("cuda")
self._PIPELINE_CACHE[model_path] = pipe
# get the embeddings (all torch.Tensor)
prompt_embeds, pooled_prompt_embeds = get_weighted_text_embeddings_flux1(
pipe=pipe, prompt=prompt
)
# Keep as tensors, just move to CPU if needed
prompt_embeds_cpu = prompt_embeds.detach().cpu()
pooled_prompt_embeds_cpu = pooled_prompt_embeds.detach().cpu()
# Format for ComfyUI - Note that FLUX only has positive embeddings
positive = [[prompt_embeds_cpu, {"pooled_output": pooled_prompt_embeds_cpu}]]
return (positive,)
NODE_CLASS_MAPPINGS = {
"WeightedFluxEmbeddings": WeightedFluxEmbeddings
}
NODE_DISPLAY_NAME_MAPPINGS = {
"WeightedFluxEmbeddings": "Weighted FLUX Embeddings"
}