2024-08-19 18:41:28 +00:00
|
|
|
from typing import Literal
|
2024-08-20 17:05:31 +00:00
|
|
|
|
|
|
|
import torch
|
2024-08-16 21:04:48 +00:00
|
|
|
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5Tokenizer
|
2024-08-12 18:23:02 +00:00
|
|
|
|
2024-08-22 15:29:59 +00:00
|
|
|
from invokeai.app.invocations.baseinvocation import BaseInvocation, Classification, invocation
|
2024-08-15 14:27:42 +00:00
|
|
|
from invokeai.app.invocations.fields import FieldDescriptions, Input, InputField
|
2024-08-12 22:01:42 +00:00
|
|
|
from invokeai.app.invocations.model import CLIPField, T5EncoderField
|
2024-08-23 17:50:01 +00:00
|
|
|
from invokeai.app.invocations.primitives import FluxConditioningOutput
|
2024-08-12 18:23:02 +00:00
|
|
|
from invokeai.app.services.shared.invocation_context import InvocationContext
|
2024-08-16 21:04:48 +00:00
|
|
|
from invokeai.backend.flux.modules.conditioner import HFEncoder
|
2024-08-19 14:14:58 +00:00
|
|
|
from invokeai.backend.stable_diffusion.diffusion.conditioning_data import ConditioningFieldData, FLUXConditioningInfo
|
2024-08-12 18:23:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@invocation(
|
|
|
|
"flux_text_encoder",
|
|
|
|
title="FLUX Text Encoding",
|
2024-08-21 13:45:22 +00:00
|
|
|
tags=["prompt", "conditioning", "flux"],
|
|
|
|
category="conditioning",
|
2024-08-12 18:23:02 +00:00
|
|
|
version="1.0.0",
|
2024-08-22 15:29:59 +00:00
|
|
|
classification=Classification.Prototype,
|
2024-08-12 18:23:02 +00:00
|
|
|
)
|
|
|
|
class FluxTextEncoderInvocation(BaseInvocation):
|
2024-08-20 19:31:22 +00:00
|
|
|
"""Encodes and preps a prompt for a flux image."""
|
2024-08-21 13:27:54 +00:00
|
|
|
|
2024-08-12 22:01:42 +00:00
|
|
|
clip: CLIPField = InputField(
|
|
|
|
title="CLIP",
|
|
|
|
description=FieldDescriptions.clip,
|
|
|
|
input=Input.Connection,
|
|
|
|
)
|
2024-08-19 18:41:28 +00:00
|
|
|
t5_encoder: T5EncoderField = InputField(
|
2024-08-15 14:49:14 +00:00
|
|
|
title="T5Encoder",
|
2024-08-21 13:27:54 +00:00
|
|
|
description=FieldDescriptions.t5_encoder,
|
2024-08-12 22:01:42 +00:00
|
|
|
input=Input.Connection,
|
2024-08-12 18:23:02 +00:00
|
|
|
)
|
2024-08-21 13:45:22 +00:00
|
|
|
t5_max_seq_len: Literal[256, 512] = InputField(
|
|
|
|
description="Max sequence length for the T5 encoder. Expected to be 256 for FLUX schnell models and 512 for FLUX dev models."
|
|
|
|
)
|
2024-08-22 16:18:33 +00:00
|
|
|
prompt: str = InputField(description="Text prompt to encode.")
|
2024-08-12 18:23:02 +00:00
|
|
|
|
|
|
|
@torch.no_grad()
|
2024-08-23 17:50:01 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> FluxConditioningOutput:
|
2024-08-12 22:01:42 +00:00
|
|
|
t5_embeddings, clip_embeddings = self._encode_prompt(context)
|
2024-08-12 18:23:02 +00:00
|
|
|
conditioning_data = ConditioningFieldData(
|
|
|
|
conditionings=[FLUXConditioningInfo(clip_embeds=clip_embeddings, t5_embeds=t5_embeddings)]
|
|
|
|
)
|
|
|
|
|
|
|
|
conditioning_name = context.conditioning.save(conditioning_data)
|
2024-08-23 17:50:01 +00:00
|
|
|
return FluxConditioningOutput.build(conditioning_name)
|
2024-08-15 14:27:42 +00:00
|
|
|
|
2024-08-12 22:01:42 +00:00
|
|
|
def _encode_prompt(self, context: InvocationContext) -> tuple[torch.Tensor, torch.Tensor]:
|
|
|
|
# Load CLIP.
|
|
|
|
clip_tokenizer_info = context.models.load(self.clip.tokenizer)
|
|
|
|
clip_text_encoder_info = context.models.load(self.clip.text_encoder)
|
|
|
|
|
|
|
|
# Load T5.
|
2024-08-19 18:41:28 +00:00
|
|
|
t5_tokenizer_info = context.models.load(self.t5_encoder.tokenizer)
|
|
|
|
t5_text_encoder_info = context.models.load(self.t5_encoder.text_encoder)
|
2024-08-12 18:23:02 +00:00
|
|
|
|
2024-08-23 17:28:05 +00:00
|
|
|
prompt = [self.prompt]
|
|
|
|
|
2024-08-12 18:23:02 +00:00
|
|
|
with (
|
2024-08-12 22:01:42 +00:00
|
|
|
t5_text_encoder_info as t5_text_encoder,
|
|
|
|
t5_tokenizer_info as t5_tokenizer,
|
2024-08-12 18:23:02 +00:00
|
|
|
):
|
|
|
|
assert isinstance(t5_text_encoder, T5EncoderModel)
|
2024-08-16 21:04:48 +00:00
|
|
|
assert isinstance(t5_tokenizer, T5Tokenizer)
|
|
|
|
|
2024-08-21 13:45:22 +00:00
|
|
|
t5_encoder = HFEncoder(t5_text_encoder, t5_tokenizer, False, self.t5_max_seq_len)
|
2024-08-12 22:01:42 +00:00
|
|
|
|
2024-08-16 21:04:48 +00:00
|
|
|
prompt_embeds = t5_encoder(prompt)
|
2024-08-12 18:23:02 +00:00
|
|
|
|
2024-08-23 17:28:05 +00:00
|
|
|
with (
|
|
|
|
clip_text_encoder_info as clip_text_encoder,
|
|
|
|
clip_tokenizer_info as clip_tokenizer,
|
|
|
|
):
|
|
|
|
assert isinstance(clip_text_encoder, CLIPTextModel)
|
|
|
|
assert isinstance(clip_tokenizer, CLIPTokenizer)
|
|
|
|
|
|
|
|
clip_encoder = HFEncoder(clip_text_encoder, clip_tokenizer, True, 77)
|
|
|
|
|
2024-08-16 21:04:48 +00:00
|
|
|
pooled_prompt_embeds = clip_encoder(prompt)
|
2024-08-12 18:23:02 +00:00
|
|
|
|
|
|
|
assert isinstance(prompt_embeds, torch.Tensor)
|
|
|
|
assert isinstance(pooled_prompt_embeds, torch.Tensor)
|
|
|
|
return prompt_embeds, pooled_prompt_embeds
|