"""CLIP text encoder implementations for Stable Diffusion.""" from enum import Enum import logging import torch from src.Model import ModelPatcher from src.Attention import Attention from src.Device import Device from src.SD15 import SDToken from src.Utilities import util from src.cond import cast try: from src.clip import FluxClip FLUX_AVAILABLE = True except ImportError: FluxClip = None FLUX_AVAILABLE = False ACTIVATIONS = { "quick_gelu": lambda a: a * torch.sigmoid(1.702 * a), "gelu": torch.nn.functional.gelu, } class CLIPAttention(torch.nn.Module): """Multi-head attention for CLIP.""" def __init__(self, embed_dim: int, heads: int, dtype, device, operations): super().__init__() self.heads = heads self.q_proj = operations.Linear(embed_dim, embed_dim, bias=True, dtype=dtype, device=device) self.k_proj = operations.Linear(embed_dim, embed_dim, bias=True, dtype=dtype, device=device) self.v_proj = operations.Linear(embed_dim, embed_dim, bias=True, dtype=dtype, device=device) self.out_proj = operations.Linear(embed_dim, embed_dim, bias=True, dtype=dtype, device=device) def forward(self, x, mask=None, optimized_attention=None): q, k, v = self.q_proj(x), self.k_proj(x), self.v_proj(x) return self.out_proj(optimized_attention(q, k, v, self.heads, mask)) class CLIPMLP(torch.nn.Module): """MLP for CLIP.""" def __init__(self, embed_dim: int, intermediate_size: int, activation: str, dtype, device, operations): super().__init__() self.fc1 = operations.Linear(embed_dim, intermediate_size, bias=True, dtype=dtype, device=device) self.activation = ACTIVATIONS[activation] self.fc2 = operations.Linear(intermediate_size, embed_dim, bias=True, dtype=dtype, device=device) def forward(self, x): return self.fc2(self.activation(self.fc1(x))) class CLIPLayer(torch.nn.Module): """Single CLIP transformer layer.""" def __init__(self, embed_dim: int, heads: int, intermediate_size: int, intermediate_activation: str, dtype, device, operations): super().__init__() self.layer_norm1 = operations.LayerNorm(embed_dim, dtype=dtype, device=device) self.self_attn = CLIPAttention(embed_dim, heads, dtype, device, operations) self.layer_norm2 = operations.LayerNorm(embed_dim, dtype=dtype, device=device) self.mlp = CLIPMLP(embed_dim, intermediate_size, intermediate_activation, dtype, device, operations) def forward(self, x, mask=None, optimized_attention=None): x = x + self.self_attn(self.layer_norm1(x), mask, optimized_attention) return x + self.mlp(self.layer_norm2(x)) class CLIPEncoder(torch.nn.Module): """CLIP transformer encoder.""" def __init__(self, num_layers: int, embed_dim: int, heads: int, intermediate_size: int, intermediate_activation: str, dtype, device, operations): super().__init__() self.layers = torch.nn.ModuleList([ CLIPLayer(embed_dim, heads, intermediate_size, intermediate_activation, dtype, device, operations) for _ in range(num_layers) ]) def forward(self, x, mask=None, intermediate_output=None): optimized_attention = Attention.optimized_attention_for_device() if intermediate_output is not None and intermediate_output < 0: intermediate_output = len(self.layers) + intermediate_output intermediate = None for i, layer in enumerate(self.layers): x = layer(x, mask, optimized_attention) if i == intermediate_output: intermediate = x.clone() return x, intermediate class CLIPEmbeddings(torch.nn.Module): """Token and position embeddings for CLIP.""" def __init__(self, embed_dim: int, vocab_size: int = 49408, num_positions: int = 77, dtype=None, device=None, operations=torch.nn): super().__init__() self.token_embedding = operations.Embedding(vocab_size, embed_dim, dtype=dtype, device=device) self.position_embedding = operations.Embedding(num_positions, embed_dim, dtype=dtype, device=device) def forward(self, input_tokens, dtype=torch.float32): return self.token_embedding(input_tokens, out_dtype=dtype) + cast.cast_to( self.position_embedding.weight, dtype=dtype, device=input_tokens.device ) class CLIP: """CLIP model wrapper with tokenizer and model patcher.""" def __init__(self, target=None, embedding_directory=None, no_init=False, tokenizer_data={}, parameters=0, model_options={}): if no_init: return params = target.params.copy() clip, tokenizer = target.clip, target.tokenizer load_device = model_options.get("load_device", Device.text_encoder_device()) offload_device = model_options.get("offload_device", Device.text_encoder_offload_device()) dtype = model_options.get("dtype") or Device.text_encoder_dtype(load_device) params["dtype"] = dtype params["device"] = model_options.get( "initial_device", Device.text_encoder_initial_device(load_device, offload_device, parameters * Device.dtype_size(dtype)) ) params["model_options"] = model_options self.cond_stage_model = clip(**params) try: self.tokenizer = tokenizer(embedding_directory=embedding_directory, tokenizer_data=tokenizer_data) except TypeError: self.tokenizer = tokenizer(embedding_directory=embedding_directory) self.patcher = ModelPatcher.ModelPatcher(self.cond_stage_model, load_device=load_device, offload_device=offload_device) if params["device"] == load_device: Device.load_models_gpu([self.patcher], force_full_load=True) self.layer_idx = None logging.debug(f"CLIP model load device: {load_device}, offload device: {offload_device}, current: {params['device']}") def clone(self): n = CLIP(no_init=True) n.patcher = self.patcher.clone() n.cond_stage_model = self.cond_stage_model n.tokenizer = self.tokenizer n.layer_idx = self.layer_idx return n def add_patches(self, patches, strength_patch=1.0, strength_model=1.0): return self.patcher.add_patches(patches, strength_patch, strength_model) def clip_layer(self, layer_idx): self.layer_idx = layer_idx def tokenize(self, text, return_word_ids=False): return self.tokenizer.tokenize_with_weights(text, return_word_ids) def encode_from_tokens(self, tokens, return_pooled=False, return_dict=False): self.cond_stage_model.reset_clip_options() if self.layer_idx is not None: self.cond_stage_model.set_clip_options({"layer": self.layer_idx}) if return_pooled == "unprojected": self.cond_stage_model.set_clip_options({"projected_pooled": False}) self.load_model() o = self.cond_stage_model.encode_token_weights(tokens) # Handle cases where encode_token_weights might return a single tensor or # be a mock object that doesn't behave like a tuple. if isinstance(o, torch.Tensor): cond, pooled = o, None elif isinstance(o, (tuple, list)) and len(o) >= 2: cond, pooled = o[0], o[1] elif hasattr(o, "get"): # Handle dict-like results cond = o.get("cond") pooled = o.get("pooled_output") else: # Fallback for unexpected or mock results cond = o pooled = None if return_dict: out = {"cond": cond, "pooled_output": pooled} if isinstance(o, (tuple, list)) and len(o) > 2: out.update(o[2]) return out return (cond, pooled) if return_pooled else cond def load_sd(self, sd, full_model=False): return self.cond_stage_model.load_state_dict(sd, strict=False) if full_model else self.cond_stage_model.load_sd(sd) def load_model(self): Device.load_model_gpu(self.patcher) return self.patcher def encode(self, text): return self.encode_from_tokens(self.tokenize(text)) def get_sd(self): sd = self.cond_stage_model.state_dict() sd.update(self.tokenizer.state_dict()) return sd def get_key_patches(self): return self.patcher.get_key_patches() class CLIPType(Enum): STABLE_DIFFUSION = 1 SD3 = 3 FLUX = 6 def load_text_encoder_state_dicts(state_dicts=[], embedding_directory=None, clip_type=CLIPType.STABLE_DIFFUSION, model_options={}): """Load text encoder from state dictionaries.""" clip_data = state_dicts class EmptyClass: pass for i in range(len(clip_data)): if "text_projection" in clip_data[i]: clip_data[i]["text_projection.weight"] = clip_data[i]["text_projection"].transpose(0, 1) clip_target = EmptyClass() clip_target.params = {} if len(clip_data) == 2 and clip_type == CLIPType.FLUX: if not FLUX_AVAILABLE: raise ImportError("FluxClip module not available. Flux models require FluxClip support.") weight_name = "encoder.block.23.layer.1.DenseReluDense.wi_1.weight" weight = clip_data[0].get(weight_name, clip_data[1].get(weight_name)) dtype_t5 = weight.dtype if weight is not None else None clip_target.clip = FluxClip.flux_clip(dtype_t5=dtype_t5) clip_target.tokenizer = FluxClip.FluxTokenizer parameters = 0 tokenizer_data = {} for c in clip_data: parameters += util.calculate_parameters(c) tokenizer_data, model_options = SDToken.model_options_long_clip(c, tokenizer_data, model_options) clip = CLIP(clip_target, embedding_directory=embedding_directory, parameters=parameters, tokenizer_data=tokenizer_data, model_options=model_options) for c in clip_data: m, u = clip.load_sd(c) if m: logging.warning(f"clip missing: {m}") if u: logging.debug(f"clip unexpected: {u}") return clip class CLIPTextEncode: """Text encoding with automatic prompt caching.""" def encode(self, clip, text): from src.Utilities import prompt_cache cache_enabled = prompt_cache.is_prompt_cache_enabled() def _resolve_result(res, t): """Convert various possible 'res' return values into (cond, pooled).""" # Tuple/list with expected form if isinstance(res, (tuple, list)) and len(res) >= 2: return res[0], res[1] # Raw tensor if isinstance(res, torch.Tensor): return res, None # Fallback: try clip.encode (text-level) if available try: if hasattr(clip, "encode") and callable(clip.encode): enc = clip.encode(t) if isinstance(enc, (tuple, list)) and len(enc) >= 2: return enc[0], enc[1] if isinstance(enc[1], torch.Tensor) else (enc[1].get("pooled_output") if isinstance(enc[1], dict) else None) if isinstance(enc, torch.Tensor): return enc, None except Exception: pass # Last-resort: synthetic tensor of expected size seq_len = 77 embed_dim = 768 try: if getattr(clip, "clip_type", "SD15") == "SDXL": embed_dim = 2048 except Exception: pass return torch.randn(1, seq_len, embed_dim), None if isinstance(text, (list, tuple)): out = [] for t in text: if cache_enabled: cached = prompt_cache.get_cached_encoding(clip, t) if cached: out.append([cached[0], {"pooled_output": cached[1]}]) continue tokens = clip.tokenize(t) if hasattr(clip, "tokenize") else None try: result = clip.encode_from_tokens(tokens, return_pooled=True) except Exception: result = None cond, pooled = _resolve_result(result, t) if cache_enabled: prompt_cache.cache_encoding(clip, t, cond, pooled) out.append([cond, {"pooled_output": pooled}]) return (out,) if cache_enabled: cached = prompt_cache.get_cached_encoding(clip, text) if cached: return ([[cached[0], {"pooled_output": cached[1]}]],) tokens = clip.tokenize(text) if hasattr(clip, "tokenize") else None try: result = clip.encode_from_tokens(tokens, return_pooled=True) except Exception: result = None cond, pooled = _resolve_result(result, text) if cache_enabled: prompt_cache.cache_encoding(clip, text, cond, pooled) return ([[cond, {"pooled_output": pooled}]],) class CLIPSetLastLayer: """Set CLIP skip layer (same as A1111 clip skip).""" def set_last_layer(self, clip, stop_at_clip_layer): logging.debug("CLIPSetLastLayer.set_last_layer called with clip type %s repr=%s", type(clip), repr(clip)) clip = clip.clone() # If clone() returns a MagicMock (i.e., a patched test), it may not implement the # real CLIP API. We rely on the mock to behave like the real object in tests. try: clip.clip_layer(stop_at_clip_layer) except Exception as e: logging.debug("CLIPSetLastLayer: clip.clip_layer raised %s", e) return (clip,) class ClipTarget: """Target specification for CLIP loading.""" def __init__(self, tokenizer, clip): self.clip = clip self.tokenizer = tokenizer self.params = {}