Spaces:
Runtime error
Runtime error
Update generator.py
Browse files- generator.py +18 -8
generator.py
CHANGED
|
@@ -763,14 +763,16 @@ class RetroArtConverter:
|
|
| 763 |
pipe_kwargs["generator"] = generator
|
| 764 |
|
| 765 |
# --- START FIX: Correct Compel batching and slicing ---
|
|
|
|
| 766 |
if self.use_compel and self.compel is not None:
|
| 767 |
try:
|
| 768 |
print("Encoding prompts with Compel...")
|
| 769 |
|
| 770 |
-
# Pass both prompts as a list to be batched
|
| 771 |
conditioning_batch, pooled_batch = self.compel([prompt, negative_prompt])
|
| 772 |
-
|
| 773 |
# Unpack the batch results using slicing
|
|
|
|
| 774 |
pipe_kwargs["prompt_embeds"] = conditioning_batch[0:1]
|
| 775 |
pipe_kwargs["pooled_prompt_embeds"] = pooled_batch[0:1]
|
| 776 |
pipe_kwargs["negative_prompt_embeds"] = conditioning_batch[1:2]
|
|
@@ -829,28 +831,36 @@ class RetroArtConverter:
|
|
| 829 |
|
| 830 |
print(f" - Face embedding: {face_emb_tensor.shape} -> {face_proj_embeds.shape}, Scale: {boosted_scale:.2f}")
|
| 831 |
|
|
|
|
|
|
|
| 832 |
if 'prompt_embeds' in pipe_kwargs:
|
| 833 |
original_embeds = pipe_kwargs['prompt_embeds']
|
| 834 |
|
| 835 |
-
if original_embeds.shape[0] > 1:
|
| 836 |
-
|
| 837 |
|
| 838 |
combined_embeds = torch.cat([original_embeds, face_proj_embeds], dim=1)
|
| 839 |
pipe_kwargs['prompt_embeds'] = combined_embeds
|
| 840 |
-
|
| 841 |
# CRITICAL: Pad negative_prompt_embeds by the same amount
|
| 842 |
if 'negative_prompt_embeds' in pipe_kwargs:
|
| 843 |
negative_embeds = pipe_kwargs['negative_prompt_embeds']
|
| 844 |
neg_padding = torch.zeros(
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
|
|
|
|
|
|
| 848 |
device=negative_embeds.device,
|
| 849 |
dtype=negative_embeds.dtype
|
| 850 |
)
|
| 851 |
pipe_kwargs['negative_prompt_embeds'] = torch.cat([negative_embeds, neg_padding], dim=1)
|
|
|
|
|
|
|
|
|
|
| 852 |
else:
|
| 853 |
print(f" [WARNING] Can't concatenate - no prompt_embeds (use Compel)")
|
|
|
|
| 854 |
|
| 855 |
elif has_detected_faces:
|
| 856 |
print(" Face detected but IP-Adapter/embeddings unavailable, using keypoints only")
|
|
|
|
| 763 |
pipe_kwargs["generator"] = generator
|
| 764 |
|
| 765 |
# --- START FIX: Correct Compel batching and slicing ---
|
| 766 |
+
# This fixes the "93 vs 77" error
|
| 767 |
if self.use_compel and self.compel is not None:
|
| 768 |
try:
|
| 769 |
print("Encoding prompts with Compel...")
|
| 770 |
|
| 771 |
+
# Pass both prompts as a list to be batched
|
| 772 |
conditioning_batch, pooled_batch = self.compel([prompt, negative_prompt])
|
| 773 |
+
|
| 774 |
# Unpack the batch results using slicing
|
| 775 |
+
# [0:1] and [1:2] keeps the batch dimension, which is required
|
| 776 |
pipe_kwargs["prompt_embeds"] = conditioning_batch[0:1]
|
| 777 |
pipe_kwargs["pooled_prompt_embeds"] = pooled_batch[0:1]
|
| 778 |
pipe_kwargs["negative_prompt_embeds"] = conditioning_batch[1:2]
|
|
|
|
| 831 |
|
| 832 |
print(f" - Face embedding: {face_emb_tensor.shape} -> {face_proj_embeds.shape}, Scale: {boosted_scale:.2f}")
|
| 833 |
|
| 834 |
+
# --- START FIX: Pad negative embeds to match face embeds ---
|
| 835 |
+
# This fixes the "109 vs 93" error
|
| 836 |
if 'prompt_embeds' in pipe_kwargs:
|
| 837 |
original_embeds = pipe_kwargs['prompt_embeds']
|
| 838 |
|
| 839 |
+
# Note: The old CFG check (if original_embeds.shape[0] > 1:) is removed
|
| 840 |
+
# as our Compel logic already provides separate cond/uncond embeds.
|
| 841 |
|
| 842 |
combined_embeds = torch.cat([original_embeds, face_proj_embeds], dim=1)
|
| 843 |
pipe_kwargs['prompt_embeds'] = combined_embeds
|
| 844 |
+
|
| 845 |
# CRITICAL: Pad negative_prompt_embeds by the same amount
|
| 846 |
if 'negative_prompt_embeds' in pipe_kwargs:
|
| 847 |
negative_embeds = pipe_kwargs['negative_prompt_embeds']
|
| 848 |
neg_padding = torch.zeros(
|
| 849 |
+
(
|
| 850 |
+
negative_embeds.shape[0], # 1
|
| 851 |
+
face_proj_embeds.shape[1], # 16
|
| 852 |
+
negative_embeds.shape[2], # 2048
|
| 853 |
+
),
|
| 854 |
device=negative_embeds.device,
|
| 855 |
dtype=negative_embeds.dtype
|
| 856 |
)
|
| 857 |
pipe_kwargs['negative_prompt_embeds'] = torch.cat([negative_embeds, neg_padding], dim=1)
|
| 858 |
+
print(f" [OK] Negative prompt padded to match: {pipe_kwargs['negative_prompt_embeds'].shape}")
|
| 859 |
+
|
| 860 |
+
print(f" [OK] Face embeddings concatenated successfully! Prompt: {combined_embeds.shape}")
|
| 861 |
else:
|
| 862 |
print(f" [WARNING] Can't concatenate - no prompt_embeds (use Compel)")
|
| 863 |
+
# --- END FIX ---
|
| 864 |
|
| 865 |
elif has_detected_faces:
|
| 866 |
print(" Face detected but IP-Adapter/embeddings unavailable, using keypoints only")
|