black-forest-labs / flux

Official inference repo for FLUX.1 models
Apache License 2.0
15.34k stars 1.1k forks source link

Fix Logical and Runtime Errors #108

Closed DrHazemAli closed 2 months ago

DrHazemAli commented 2 months ago

Issue Addressed:

The prepare function previously faced challenges with mismatched numbers of prompts and images. This issue was particularly evident when multiple prompts were associated with a single image, leading to inconsistent batch sizes and potential runtime errors. The original function lacked sufficient mechanisms for aligning image and text data properly, which could result in misaligned processing inputs.

Implemented Fixes:

1. Automatic Image Replication:

Enhanced the function to automatically replicate images when multiple prompts are provided with fewer images. This ensures that each prompt is paired with an image, preventing mismatches and potential processing errors. Error Handling for Mismatched Inputs: Introduced error checks that trigger if the number of prompts and images do not align when multiple images are provided. This preemptive check prevents confusing errors deeper in the processing pipeline.

2. Text Processing:

Strengthened text handling by ensuring text embeddings are consistently replicated across the calculated batch size. This change guarantees that each image in a batch is paired with corresponding text data, which is crucial for models leveraging both visual and textual inputs.

3. Tensor Consistency:

Improved the handling of tensor device and data type settings, ensuring all tensors involved in the computation are consistently configured. This uniformity is key for avoiding device-related errors and optimizing computational efficiency.

These enhancements make the function more reliable and user-friendly, streamlining the handling of batch sizes and prompt associations, and reducing the risk of runtime errors in your data processing workflow.

timudk commented 2 months ago

@DrHazemAli could you give a command to replicate the issue? i am not entirely sure what you mean with "when multiple prompts were associated with a single image"

DrHazemAli commented 2 months ago

Sure, The below code can demonstrate the mismatch handling where one image is expected to correspond to multiple text prompts:

# Assuming HFEmbedder and other necessary components are properly implemented and imported
t5_embedder = HFEmbedder()  # Hypothetical initializer for text embedding
clip_embedder = HFEmbedder()  # Hypothetical initializer for clip embedding

single_image = torch.randn(1, 3, 256, 256)  
multiple_prompts = ["A sunny day in the park", "A snowy mountain", "A bustling city night"]
prepared_data = prepare(t5_embedder, clip_embedder, single_image, multiple_prompts)
print(prepared_data)

This code will trigger the original prepare function with one image and several prompts. Depending on the initial implementation, you may see that the function doesn’t handle the replication of the single image across multiple prompts correctly, which would typically lead to a runtime error or sometimes incorrect data processing.

timudk commented 2 months ago

@DrHazemAli this snippet is never used though?

this codebase is only meant to support standard single prompt sampling via the cli, streamlit demo, and gradio demo.

DrHazemAli commented 2 months ago

I do understand that, But as of my vision this project will grow, and it may need such a thing as the development keeps up. So using such a way of handling will be great for the future as the project grows with development.

timudk commented 2 months ago

But as of my vision this project will grow, and it may need such a thing as the development keeps up.

we can reopen this once necessary, but i will close it for now