From bf2a41b2cac294940ed3f90bbef8c3b83c83c05a Mon Sep 17 00:00:00 2001 From: Junst Date: Wed, 24 Sep 2025 16:49:29 +0900 Subject: [PATCH] Fix context and x tensor dimension mismatch - Fix 'Sizes of tensors must match except in dimension 1' error - Add context spatial dimension resizing to match x tensor dimensions - Use F.interpolate to resize context to match latent space dimensions - Ensure context and x tensors have compatible spatial dimensions - Improve tensor shape compatibility for Flux model forward pass --- concept_attention.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/concept_attention.py b/concept_attention.py index bffb9c7..06880f1 100644 --- a/concept_attention.py +++ b/concept_attention.py @@ -67,7 +67,7 @@ class ConceptAttention: latent_height = max(height // 8, 32) latent_width = max(width // 8, 32) - # Create dummy inputs for Flux model + # Create dummy inputs for Flux model - match context dimensions x = torch.randn(batch_size, 4, latent_height, latent_width, device=self.device) timestep_tensor = torch.tensor([timestep], device=self.device) @@ -89,6 +89,16 @@ class ConceptAttention: else: context = concept_embeddings.unsqueeze(0) # Add batch dimension if needed + # Ensure context spatial dimensions match x dimensions + if context.shape[2] != latent_height or context.shape[3] != latent_width: + # Resize context to match x dimensions + context = F.interpolate( + context.view(context.shape[0], context.shape[1], context.shape[2], context.shape[3]), + size=(latent_height, latent_width), + mode='bilinear', + align_corners=False + ) + # Create guidance vector y = torch.zeros(batch_size, 512, device=self.device)