### Install Diffusers Dependencies Source: https://huggingface.co/Qwen/Qwen-Image-Edit/blob/418bcab18188842e18720b4092510b5d1abc63a8/README.md Install the necessary libraries to run the Qwen-Image-Edit model. ```bash pip install -U diffusers transformers accelerate ``` ```bash pip install -U diffusers ``` -------------------------------- ### Install and Use Qwen-Image-Edit with Diffusers Source: https://huggingface.co/Qwen/Qwen-Image-Edit/discussions/1/files Install the necessary dependencies and initialize the pipeline to perform image editing tasks. ```bash pip install -U diffusers transformers accelerate ``` ```python import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image-Edit", dtype=torch.bfloat16, device_map="cuda") prompt = "Turn this cat into a dog" input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") image = pipe(image=input_image, prompt=prompt).images[0] ``` -------------------------------- ### Configure Model and Generation Parameters Source: https://huggingface.co/Qwen/Qwen-Image/discussions/32/files Examples of setting up the pipeline, defining prompt magic, and managing aspect ratios. ```python pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = { "en": "Ultra HD, 4K, cinematic composition." # for english prompt, "zh": "超清,4K,电影级构图" # for chinese prompt, } # Generate image prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition''' negative_prompt = " " # using an empty string if you do not have specific concept to remove # Generate with different aspect ratios aspect_ratios = { "16:9": (1664, 928), "9:16": (928, 1664), "4:3": (1472, 1140), "3:4": (1140, 1472) } width, height = aspect_ratios["16:9"] ``` -------------------------------- ### Initialize Model Pipeline Source: https://huggingface.co/Qwen/Qwen-Image/blob/0770fddc587fa1795e0a9eb01dd40218fcbdd524/README.md Basic setup for importing the pipeline and defining the model name. ```python from diffusers import DiffusionPipeline import torch model_name = "Qwen/Qwen-Image" ``` -------------------------------- ### Initialize and Run Qwen-Image Pipeline Source: https://huggingface.co/Qwen/Qwen-Image/blame/80a84a33c628044c1a720c5b22b318cd25d9c20d/transformer/diffusion_pytorch_model.safetensors.index.json This snippet demonstrates how to install the necessary dependencies, initialize the DiffusionPipeline with the Qwen-Image model, and generate an image from a text prompt. ```APIDOC ## Initialize and Run Qwen-Image Pipeline ### Description This operation initializes the Qwen-Image model using the Hugging Face Diffusers library and generates an image based on a provided text prompt. ### Installation ```bash pip install -U diffusers transformers accelerate ``` ### Usage Example ```python import torch from diffusers import DiffusionPipeline # Initialize the pipeline # Use device_map="cuda" for GPU or "mps" for Apple Silicon pipe = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda") # Define prompt and generate image prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] ``` ``` -------------------------------- ### Install Diffusers Dependencies Source: https://huggingface.co/Qwen/Qwen-Image-Edit/blame/main/processor/tokenizer.json Install the necessary libraries to run the model. ```bash pip install -U diffusers transformers accelerate ``` -------------------------------- ### Full Quantized Pipeline Setup Script Source: https://huggingface.co/Qwen/Qwen-Image-Edit/discussions/6 Complete script demonstrating the configuration of BitsAndBytes quantization for both the transformer and text encoder components. ```python import torch from transformers import BitsAndBytesConfig as TransformersBitsAndBytesConfig from transformers import Qwen2_5_VLForConditionalGeneration from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig from diffusers import QwenImageEditPipeline, QwenImageTransformer2DModel from PIL import Image import os model_id = "Qwen/Qwen-Image-Edit" torch_dtype = torch.bfloat16 device = "cuda" cache_dir = "../scratch/qwen_image_edit/" quantization_config = DiffusersBitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, llm_int8_skip_modules=["transformer_blocks.0.img_mod"], ) transformer = QwenImageTransformer2DModel.from_pretrained( model_id, subfolder="transformer", quantization_config=quantization_config, torch_dtype=torch_dtype, cache_dir=cache_dir, ) transformer = transformer.to("cpu") quantization_config = TransformersBitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, ) text_encoder = Qwen2_5_VLForConditionalGeneration.from_pretrained( model_id, subfolder="text_encoder", quantization_config=quantization_config, torch_dtype=torch_dtype, cache_dir=cache_dir, ) text_encoder = text_encoder.to("cpu") pipe = QwenImageEditPipeline.from_pretrained( model_id, transformer=transformer, text_encoder=text_encoder, torch_dtype=torch_dtype, cache_dir=cache_dir, ) # optionally load LoRA weights to speed up inference pipe.load_lora_weights( "lightx2v/Qwen-Image-Lightning", weight_name="Qwen-Image-Lightning-8steps-V1.1.safetensors", cache_dir=cache_dir ) # pipe.load_lora_weights( # "lightx2v/Qwen-Image-Lightning", # weight_name="Qwen-Image-Lightning-4steps-V1.0-bf16.safetensors", # cache_dir=cache_dir, # ) pipe.enable_model_cpu_offload() if __name__ == "__main__": generator = torch.Generator(device="cpu").manual_seed(42) # use test folder use_test = True if use_test: file_name = "test_image_input" input_image = Image.open(f"./test/images/{file_name}.jpg").convert('RGB') prompt = open(f"./test/prompts/{file_name}.txt", 'r').read() output_dir = f"./test/outputs" os.makedirs(output_dir, exist_ok=True) # change steps to 8 or 4 if you used the lighting loras torch.cuda.empty_cache() with torch.inference_mode(): image = pipe(input_image, prompt, num_inference_steps=8).images[0] image.save(f"{output_dir}/{file_name}.jpg") ``` -------------------------------- ### Install Diffusers and Run Inference Source: https://huggingface.co/Qwen/Qwen-Image/blob/689a9491249248ccfd20f73bc2e9aa68d6a43d40/text_encoder/config.json Commands to install the necessary dependencies and a Python script to initialize the pipeline and generate an image. ```bash pip install -U diffusers transformers accelerate ``` ```python import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] ``` -------------------------------- ### Install dependencies for local inference Source: https://huggingface.co/Qwen/Qwen-Image-Edit/colab Install the necessary libraries to run the model locally. ```bash !pip install -U diffusers ``` ```bash pip install -U diffusers transformers accelerate ``` -------------------------------- ### Install TorchAO dependency Source: https://huggingface.co/Qwen/Qwen-Image/blob/93ea1cbcbb4cfed308d0a6d7449e9b6cb2c56d57/README.md Required installation command for the TorchAO library before running the model. ```bash pip install -U torchao ``` -------------------------------- ### Using Qwen-Image with Diffusers Source: https://huggingface.co/Qwen/Qwen-Image/blame/4516c4d3058302ff35cd86c62ffa645d039fefad/LICENSE This snippet demonstrates how to install the necessary dependencies and initialize the Qwen-Image pipeline to generate images from text prompts. ```APIDOC ## Using Qwen-Image with Diffusers ### Description Initialize the Qwen-Image model using the Hugging Face `diffusers` library to perform text-to-image generation. ### Installation ```bash pip install -U diffusers transformers accelerate ``` ### Usage Example ```python import torch from diffusers import DiffusionPipeline # Initialize the pipeline # Note: Use "mps" for Apple devices if applicable pipe = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda") # Generate an image prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] ``` ``` -------------------------------- ### Install Diffusers Library Source: https://huggingface.co/Qwen/Qwen-Image-Edit/blame/main/README.md Install the latest version of the diffusers library from the official GitHub repository. ```bash pip install git+https://github.com/huggingface/diffusers ``` -------------------------------- ### Advanced Generation with Aspect Ratios Source: https://huggingface.co/Qwen/Qwen-Image/blob/2c88b9782e37fe87f99db2b4d28a2e36cdf972d1/README.md?code=true Detailed example showing how to configure device settings, handle prompts, and specify custom aspect ratios for image generation. ```python from diffusers import DiffusionPipeline import torch model_name = "Qwen/Qwen-Image" # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = [ "en": "Ultra HD, 4K, cinematic composition." # for english prompt, "zh": "超清,4K,电影级构图" # for chinese prompt, ] # Generate image prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition''' negative_prompt = " " # Generate with different aspect ratios aspect_ratios = { "1:1": (1328, 1328), "16:9": (1664, 928), "9:16": (928, 1664), "4:3": (1472, 1140), "3:4": (1140, 1472) } width, height = aspect_ratios["16:9"] image = pipe( prompt=prompt + positive_magic["en"], negative_prompt=negative_prompt, width=width, height=height, num_inference_steps=50, true_cfg_scale=4.0, ``` -------------------------------- ### Using Qwen-Image-Edit with Diffusers Source: https://huggingface.co/Qwen/Qwen-Image-Edit/discussions/48 This snippet demonstrates how to install the necessary dependencies, load the model pipeline, and perform an image-to-image transformation. ```APIDOC ## Using Qwen-Image-Edit with Diffusers ### Description This code demonstrates how to load the Qwen-Image-Edit model using the `DiffusionPipeline` and perform an image-to-image edit based on a text prompt. ### Installation ```bash pip install -U diffusers transformers accelerate ``` ### Usage Example ```python import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # Initialize the pipeline # Note: switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image-Edit", dtype=torch.bfloat16, device_map="cuda") # Define inputs prompt = "Turn this cat into a dog" input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") # Run inference image = pipe(image=input_image, prompt=prompt).images[0] ``` ``` -------------------------------- ### Install Diffusers Library Source: https://huggingface.co/Qwen/Qwen-Image/colab Install the diffusers library to enable model pipeline functionality. ```bash !pip install -U diffusers ``` -------------------------------- ### Advanced Inference with Aspect Ratios Source: https://huggingface.co/Qwen/Qwen-Image/blob/80a84a33c628044c1a720c5b22b318cd25d9c20d/README.md?code=true A comprehensive example demonstrating how to configure device settings, handle prompts, and generate images with specific aspect ratios. ```python from diffusers import DiffusionPipeline import torch model_name = "Qwen/Qwen-Image" # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = { "en": "Ultra HD, 4K, cinematic composition." # for english prompt, "zh": "超清,4K,电影级构图" # for chinese prompt, } # Generate image prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition''' negative_prompt = " " # using an empty string if you do not have specific concept to remove # Generate with different aspect ratios aspect_ratios = { "1:1": (1328, 1328), "16:9": (1664, 928), "9:16": (928, 1664), "4:3": (1472, 1140), "3:4": (1140, 1472), "3:2": (1584, 1056), "2:3": (1056, 1584), } width, height = aspect_ratios["16:9"] image = pipe( prompt=prompt + positive_magic["en"], negative_prompt=negative_prompt, width=width, ``` -------------------------------- ### Troubleshoot OOM with Device Mapping Source: https://huggingface.co/Qwen/Qwen-Image-Edit/discussions/18 Example of a configuration that triggers a ValueError due to conflicting device placement strategies. ```python pipeline = QwenImageEditPipeline.from_pretrained( "/home/tmp/Qwen-Image-Edit", device_map="balanced") print("pipeline loaded") pipeline.to(torch.bfloat16) pipeline.to("cuda") pipeline.set_progress_bar_config(disable=None) ``` -------------------------------- ### Run Qwen-Image with torchao on 24GB VRAM Source: https://huggingface.co/Qwen/Qwen-Image/blame/93ea1cbcbb4cfed308d0a6d7449e9b6cb2c56d57/README.md Uses torchao for int8 quantization to fit the model on 24GB GPUs. Requires the torchao library to be installed. ```python # make sure torchao is installed: `pip install -U torchao import torch from diffusers import AutoModel, DiffusionPipeline, TorchAoConfig model_id = "Qwen/Qwen-Image" torch_dtype = torch.bfloat16 device = "cuda" quantization_config = TorchAoConfig("int8wo") transformer = AutoModel.from_pretrained( model_id, subfolder="transformer", quantization_config=quantization_config, torch_dtype=torch_dtype, ) pipe = DiffusionPipeline.from_pretrained(model_id, transformer=transformer, torch_dtype=torch_dtype) pipe.enable_model_cpu_offload() prompt = "a woman and a man sitting at a cafe, the woman has red hair and she's wearing purple sweater with a black scarf and a white hat, the man is sitting on the other side of the table and he's wearing a white shirt with a purple scarf and red hat, both of them are sipping their coffee while in the table there's some cake slices on their respective plates, each with forks and knives at each side." negative_prompt = "" generator = torch.Generator(device="cuda").manual_seed(42) image = pipe( prompt=prompt, negative_prompt=negative_prompt, width=1664, height=928, num_inference_steps=25, true_cfg_scale=4.0, generator=generator, ).images[0] image.save("qwen_torchao.png") ``` -------------------------------- ### Table Code Examples Source: https://huggingface.co/Qwen/Qwen-Image/blame/b0d9cbe58a813c316e7107e15310a4347b223020/transformer/diffusion_pytorch_model.safetensors.index.json Examples extracted from the table section. ```text 1 ``` ```text 2 ``` ```text 3 ``` ```text 4 ``` ```text 5 ``` ```text 6 ``` ```text 7 ``` ```text 8 ``` -------------------------------- ### Generate images with Qwen-Image Source: https://huggingface.co/Qwen/Qwen-Image/blob/0770fddc587fa1795e0a9eb01dd40218fcbdd524/README.md?code=true Example of generating an image using specific inference parameters and saving the result. ```python height=height, num_inference_steps=50, true_cfg_scale=4.0, generator=torch.Generator(device="cuda").manual_seed(42) ).images[0] image.save("example.png") ``` -------------------------------- ### Model metadata examples Source: https://huggingface.co/Qwen/Qwen-Image/blame/09c99e2b1b5dd47e0d71f7ce80eeb3057dcc7634/README.md Miscellaneous code snippets found in the repository metadata. ```text a3751cc ``` ```text 1 ``` ```text 2 ``` ```text 3 ``` ```text 4 ``` ```yaml --- license: apache-2.0 --- ``` -------------------------------- ### Define local model path Source: https://huggingface.co/Qwen/Qwen-Image/discussions/27 Example of setting the model identifier to a local directory path. ```python model_id = "Qwen/Qwen-Image" ``` ```python model_id = "./models/qwen-image" ``` -------------------------------- ### Attempt Multi-Image Input Configuration Source: https://huggingface.co/Qwen/Qwen-Image/discussions/69 Example of an unsuccessful attempt to pass a list of images directly into the pipeline input dictionary. ```python image1 = Image.open("./1.png").convert("RGB").resize((512, 512)) image2 = Image.open("./2.png").convert("RGB").resize((512, 512)) inputs = { "image": [image1, image2], "prompt": prompt, "generator": torch.manual_seed(0), "true_cfg_scale": 4.0, "negative_prompt": "", "num_inference_steps": 8, } ``` -------------------------------- ### Define positive_magic Dictionary Source: https://huggingface.co/Qwen/Qwen-Image/commit/f6d79c4e57d8e93896682e1c68d783571e7ff7bf Example of defining a dictionary for prompt enhancement. ```python positive_magic = { "en": "Ultra HD, 4K, cinematic composition." # for english prompt, "zh": "超清,4K,电影级构图" # for chinese prompt, } ``` -------------------------------- ### Run Qwen-Image with bitsandbytes on 16GB VRAM Source: https://huggingface.co/Qwen/Qwen-Image/blame/93ea1cbcbb4cfed308d0a6d7449e9b6cb2c56d57/README.md Uses bitsandbytes 4-bit quantization for both the transformer and text encoder to fit the model on 16GB GPUs. Requires the bitsandbytes library to be installed. ```python # make sure bitsandbytes is installed: `pip install -U bitsandbytes import torch from transformers import BitsAndBytesConfig as TransformersBitsAndBytesConfig from transformers import Qwen2_5_VLForConditionalGeneration from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig from diffusers import QwenImagePipeline, QwenImageTransformer2DModel model_id = "Qwen/Qwen-Image" torch_dtype = torch.bfloat16 device = "cuda" quantization_config = DiffusersBitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, llm_int8_skip_modules=["transformer_blocks.0.img_mod"], ) transformer = QwenImageTransformer2DModel.from_pretrained( model_id, subfolder="transformer", quantization_config=quantization_config, torch_dtype=torch_dtype, ) transformer = transformer.to("cpu") quantization_config = TransformersBitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, ) text_encoder = Qwen2_5_VLForConditionalGeneration.from_pretrained( model_id, subfolder="text_encoder", quantization_config=quantization_config, torch_dtype=torch_dtype, ) text_encoder = text_encoder.to("cpu") pipe = QwenImagePipeline.from_pretrained( model_id, transformer=transformer, text_encoder=text_encoder, torch_dtype=torch_dtype ) pipe.enable_model_cpu_offload() prompt = "a woman and a man sitting at a cafe, the woman has red hair and she's wearing purple sweater with a black scarf and a white hat, the man is sitting on the other side of the table and he's wearing a white shirt with a purple scarf and red hat, both of them are sipping their coffee while in the table there's some cake slices on their respective plates, each with forks and knives at each side." negative_prompt = "" generator = torch.Generator(device="cuda").manual_seed(42) image = pipe( prompt=prompt, negative_prompt=negative_prompt, width=1664, height=928, num_inference_steps=25, true_cfg_scale=4.0, generator=generator, ).images[0] image.save("qwen_bnb.png") ``` -------------------------------- ### Generate images with Qwen/Qwen-Image Source: https://huggingface.co/Qwen/Qwen-Image/blame/dd1f2c3c5026397847430cfb82d628371dfbb4aa/README.md Uses the Diffusers library to load the model and generate an image based on a text prompt. Requires torch and diffusers installed, and automatically detects CUDA availability. ```python from diffusers import DiffusionPipeline import torch model_name = "Qwen/Qwen-Image" # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = { "en": "Ultra HD, 4K, cinematic composition.", # for english prompt, "zh": "超清,4K,电影级构图", # for chinese prompt, } # Generate image prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition''' negative_prompt = " " # Generate with different aspect ratios aspect_ratios = { "1:1": (1328, 1328), "16:9": (1664, 928), "9:16": (928, 1664), "4:3": (1472, 1140), "3:4": (1140, 1472) } width, height = aspect_ratios["16:9"] image = pipe( prompt=prompt + positive_magic["en"], negative_prompt=negative_prompt, width=width, height=height, num_inference_steps=50, true_cfg_scale=4.0, generator=torch.Generator(device="cuda").manual_seed(42) ).images[0] image.save("example.png") ``` -------------------------------- ### Troubleshoot iterative image editing Source: https://huggingface.co/Qwen/Qwen-Image-Edit/discussions/31 Example code demonstrating an iterative image editing loop that reportedly results in quality degradation. ```python import os from PIL import Image import torch from diffusers import QwenImageEditPipeline model_name = "Qwen/Qwen-Image-Edit" # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipeline = QwenImageEditPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipeline = pipeline.to(device) positive_magic = ", black and white, sketch, composition, simple." prompts = ["A bar lounge with a LED-lit sign near the restroom entrance", "Add a man sitting at the counter", "Add a coloured mojito on the right of the counter"] image_path = "./canvas.png" # A 1024*1024 white PNG image for idx, prompt in enumerate(prompts): print(image_path) print(prompt + positive_magic) # checking I'm using the right things inputs = { "image": Image.open(image_path).convert("RGB"), "prompt": prompt + positive_magic, "negative_prompt": negative_prompt, "generator": torch.Generator(device="cuda").manual_seed(42), #"true_cfg_scale": 4.0, # tried with and without it "num_inference_steps": 50, } with torch.inference_mode(): output = pipeline(**inputs) output_image = output.images[0] output_path = f"lounge_{idx}.png" output_image.save(output_path) print("image saved at", os.path.abspath(output_path)) # Reassign the newly generated image as the input for the next iteration image_path = output_path ``` -------------------------------- ### Concurrent Pipeline Execution Pattern Source: https://huggingface.co/Qwen/Qwen-Image/discussions/82 Example of the pipeline initialization and inference call pattern that triggers the index out of bounds error when executed concurrently. ```python import threading from diffusers import DiffusionPipeline import torch self.device = "cuda" self.torch_dtype = torch.bfloat16 self.pipe = DiffusionPipeline.from_pretrained( "Qwen/Qwen-Image", torch_dtype=self.torch_dtype, local_files_only=True, cache_dir=self.cache_dir ).to(self.device) images = self.pipe( prompt=prompt, negative_prompt=negative_prompt, width=width, height=height, num_inference_steps=50, true_cfg_scale=true_cfg_scale, num_images_per_prompt=num_images_per_prompt, generator=generator ).images ``` -------------------------------- ### Initialize and Run Qwen-Image Pipeline Source: https://huggingface.co/Qwen/Qwen-Image/blob/a8cc93b660f29d71f08f4af9f753f6e37bc9146c/README.md Sets up the DiffusionPipeline with appropriate device and dtype, then generates an image using a specific prompt and aspect ratio. ```python # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = { "en": "Ultra HD, 4K, cinematic composition.", # for english prompt "zh": "超清,4K,电影级构图" # for chinese prompt, } # Generate image prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition''' negative_prompt = " " # using an empty string if you do not have specific concept to remove # Generate with different aspect ratios aspect_ratios = { "1:1": (1328, 1328), "16:9": (1664, 928), "9:16": (928, 1664), "4:3": (1472, 1140), "3:4": (1140, 1472), "3:2": (1584, 1056), "2:3": (1056, 1584), } width, height = aspect_ratios["16:9"] image = pipe( prompt=prompt + positive_magic["en"], negative_prompt=negative_prompt, width=width, height=height, num_inference_steps=50, true_cfg_scale=4.0, generator=torch.Generator(device="cuda").manual_seed(42) ).images[0] image.save("example.png") ``` -------------------------------- ### Initialize and Run Qwen-Image Pipeline Source: https://huggingface.co/Qwen/Qwen-Image/blob/0770fddc587fa1795e0a9eb01dd40218fcbdd524/README.md Configures the device and data type for the pipeline, defines prompts, and executes image generation with specific aspect ratios. ```python # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = { "en": ", Ultra HD, 4K, cinematic composition.", # for english prompt "zh": ", 超清,4K,电影级构图." # for chinese prompt } # Generate image prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition''' negative_prompt = " " # using an empty string if you do not have specific concept to remove # Generate with different aspect ratios aspect_ratios = { "1:1": (1328, 1328), "16:9": (1664, 928), "9:16": (928, 1664), "4:3": (1472, 1140), "3:4": (1140, 1472), "3:2": (1584, 1056), "2:3": (1056, 1584), } width, height = aspect_ratios["16:9"] image = pipe( prompt=prompt + positive_magic["en"], negative_prompt=negative_prompt, width=width, height=height, num_inference_steps=50, true_cfg_scale=4.0, generator=torch.Generator(device="cuda").manual_seed(42) ).images[0] image.save("example.png") ``` -------------------------------- ### Initialize and Generate with Qwen-Image Source: https://huggingface.co/Qwen/Qwen-Image/commit/9bbb8fdd50de6233389bd2f26a45c7dc9940c182 Configures the pipeline based on hardware availability and generates an image using a prompt and specific aspect ratio. ```python model_name = "Qwen/Qwen-Image" # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = "Ultra HD, 4K, cinematic composition." # for english prompt # positive_magic = "超清,4K,电影级构图" # for chinese prompt # Generate image prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition''' negative_prompt = " " # Generate with different aspect ratios aspect_ratios = { "1:1": (1328, 1328), "16:9": (1664, 928), "9:16": (928, 1664), "4:3": (1472, 1140), "3:4": (1140, 1472) } width, height = aspect_ratios["16:9"] image = pipe( prompt=prompt + positive_magic, width=width, height=height, num_inference_steps=50, true_cfg_scale=4.0, generator=torch.Generator(device="cuda").manual_seed(42) ).images[0] image.save("example.png") ``` -------------------------------- ### Initialize QwenImageEditPipeline Source: https://huggingface.co/Qwen/Qwen-Image-Edit/blob/ac7f9318f633fc4b5778c59367c8128225f1e3de/README.md?code=true Load the specific QwenImageEditPipeline and process an image with a text prompt. ```python import os from PIL import Image import torch from diffusers import QwenImageEditPipeline pipeline = QwenImageEditPipeline.from_pretrained("Qwen/Qwen-Image-Edit") print("pipeline loaded") pipeline.to(torch.bfloat16) pipeline.to("cuda") pipeline.set_progress_bar_config(disable=None) image = Image.open("./input.png").convert("RGB") prompt = "Change the rabbit's color to purple, with a flash light background." inputs = { "image": image, "prompt": prompt, "generator": torch.manual_seed(0), ``` -------------------------------- ### Initialize and Run Qwen-Image Pipeline Source: https://huggingface.co/Qwen/Qwen-Image/blob/93ea1cbcbb4cfed308d0a6d7449e9b6cb2c56d57/README.md Configures the model with 4-bit quantization and runs inference using a text prompt. ```python import torch from transformers import BitsAndBytesConfig as TransformersBitsAndBytesConfig from transformers import Qwen2_5_VLForConditionalGeneration from diffusers import BitsAndBytesConfig as DiffusersBitsAndBytesConfig from diffusers import QwenImagePipeline, QwenImageTransformer2DModel model_id = "Qwen/Qwen-Image" torch_dtype = torch.bfloat16 device = "cuda" quantization_config = DiffusersBitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, llm_int8_skip_modules=["transformer_blocks.0.img_mod"], ) transformer = QwenImageTransformer2DModel.from_pretrained( model_id, subfolder="transformer", quantization_config=quantization_config, torch_dtype=torch_dtype, ) transformer = transformer.to("cpu") quantization_config = TransformersBitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16, ) text_encoder = Qwen2_5_VLForConditionalGeneration.from_pretrained( model_id, subfolder="text_encoder", quantization_config=quantization_config, torch_dtype=torch_dtype, ) text_encoder = text_encoder.to("cpu") pipe = QwenImagePipeline.from_pretrained( model_id, transformer=transformer, text_encoder=text_encoder, torch_dtype=torch_dtype ) pipe.enable_model_cpu_offload() prompt = "a woman and a man sitting at a cafe, the woman has red hair and she's wearing purple sweater with a black scarf and a white hat, the man is sitting on the other side of the table and he's wearing a white shirt with a purple scarf and red hat, both of them are sipping their coffee while in the table there's some cake slices on their respective plates, each with forks and knives at each side." negative_prompt = "" generator = torch.Generator(device="cuda").manual_seed(42) image = pipe( prompt=prompt, negative_prompt=negative_prompt, width=1664, height=928, num_inference_steps=25, true_cfg_scale=4.0, generator=generator, ).images[0] image.save("qwen_bnb.png") ``` -------------------------------- ### CUDA Out of Memory Error Source: https://huggingface.co/Qwen/Qwen-Image-Edit/discussions/6 Example of a PyTorch CUDA memory allocation error encountered during inference. ```text torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 8.21 GiB. GPU 0 has a total capacity of 23.64 GiB of which 1.55 GiB is free. Including non-PyTorch memory, this process has 22.07 GiB memory in use. Of the allocated memory 21.77 GiB is allocated by PyTorch, and 101.13 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables) ``` -------------------------------- ### Generate images with Qwen-Image Source: https://huggingface.co/Qwen/Qwen-Image/commit/b2ddf47364de0dd3735eb5cf74455a985e23f34c Demonstrates loading the Qwen-Image pipeline, configuring hardware acceleration, and generating an image with specific aspect ratios. ```python from diffusers import DiffusionPipeline import torch model_name = "Qwen/Qwen-Image" # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = "Ultra HD, 4K, cinematic composition." # for english prompt # positive_magic = "超清,4K,电影级构图" # for chinese prompt # Generate image prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition''' negative_prompt = " " # Generate with different aspect ratios aspect_ratios = { "1:1": (1328, 1328), "16:9": (1664, 928), "9:16": (928, 1664), "4:3": (1472, 1140), "3:4": (1140, 1472) } width, height = aspect_ratios["16:9"] image = pipe( prompt=prompt + positive_magic, width=width, height=height, num_inference_steps=50, true_cfg_scale=4.0, generator=torch.Generator(device="cuda").manual_seed(42) ).images[0] image.save("example.png") ``` -------------------------------- ### Initialize and run Qwen-Image-Edit pipeline Source: https://huggingface.co/Qwen/Qwen-Image-Edit/discussions/18 Configures the model with 4-bit quantization and CPU offloading to optimize memory usage on a 24GB GPU. ```python scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config) quant_config_transformers = TransformersBitsAndBytesConfig(load_in_4bit=True,bnb_4bit_quant_type="nf4",bnb_4bit_use_double_quant=True,bnb_4bit_compute_dtype=torch.bfloat16) transformer = QwenImageTransformer2DModel.from_pretrained("Qwen/Qwen-Image-Edit", subfolder="transformer", quantization_config=quant_config_transformers, torch_dtype=torch.bfloat16) pipe = QwenImageEditPipeline.from_pretrained("Qwen/Qwen-Image-Edit", transformer=transformer, scheduler=scheduler, torch_dtype=torch.bfloat16) pipe.load_lora_weights("lightx2v/Qwen-Image-Lightning", weight_name="Qwen-Image-Edit-Lightning-4steps-V1.0-bf16.safetensors") pipe.enable_model_cpu_offload() image = load_image(r"C:\Users\dogbowl\Downloads\theimage.png").convert("RGB") prompt = "change Rhino into tiger" negative_prompt = " " image = pipe(image=image, prompt=prompt,negative_prompt=negative_prompt,num_inference_steps=4, true_cfg_scale=1.0).images[0] image.save("qwenimage_edit.png") ``` -------------------------------- ### Initialize and Configure Qwen-Image Pipeline Source: https://huggingface.co/Qwen/Qwen-Image/blob/48c2192f09c9182f8bcf990090cc15a00625506c/README.md Sets up the DiffusionPipeline with automatic device detection for CUDA or CPU and defines prompt modifiers. ```python # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = [ "en": "Ultra HD, 4K, cinematic composition.", # for english prompt "zh": "超清,4K,电影级构图" # for chinese prompt, ] ``` -------------------------------- ### Configure Qwen-Image with TorchAo Source: https://huggingface.co/Qwen/Qwen-Image/discussions/27 Initializes the Qwen-Image pipeline with TorchAo quantization for local model files. ```python from diffusers import QwenImagePipeline, QwenImageTransformer2DModel, TorchAoConfig model_id = "./models/qwen-image" torch_dtype = torch.bfloat16 device = "cuda" quantization_config = TorchAoConfig("int8wo") transformer = QwenImageTransformer2DModel.from_pretrained( model_id, subfolder="transformer", quantization_config=quantization_config, torch_dtype=torch_dtype, local_files_only=True, ) pipe = QwenImagePipeline.from_pretrained( model_id, transformer=transformer, torch_dtype=torch_dtype, local_files_only=True, ) ``` -------------------------------- ### Initialize QwenImageEditPipeline and Load LoRA Source: https://huggingface.co/Qwen/Qwen-Image-Edit/discussions/6 Standard initialization of the pipeline with transformer and text encoder components, followed by loading LoRA weights for accelerated inference. ```python pipe = QwenImageEditPipeline.from_pretrained( model_id, transformer=transformer, text_encoder=text_encoder, torch_dtype=torch_dtype, cache_dir=cache_dir, ) # optionally load LoRA weights to speed up inference pipe.load_lora_weights( "lightx2v/Qwen-Image-Lightning", weight_name="Qwen-Image-Lightning-8steps-V1.1.safetensors", cache_dir=cache_dir ) # pipe.load_lora_weights( # "lightx2v/Qwen-Image-Lightning", # weight_name="Qwen-Image-Lightning-4steps-V1.0-bf16.safetensors", # cache_dir=cache_dir, # ) pipe.enable_model_cpu_offload() ``` -------------------------------- ### Initialize and Run Qwen-Image Pipeline Source: https://huggingface.co/Qwen/Qwen-Image/blob/562c3d12cb3f4686100298ff1ed4921e070cee83/README.md Configures the pipeline for CUDA or CPU execution and generates an image with specific dimensions and inference parameters. ```python # Load the pipeline if torch.cuda.is_available(): torch_dtype = torch.bfloat16 device = "cuda" else: torch_dtype = torch.float32 device = "cpu" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype) pipe = pipe.to(device) positive_magic = { "en": "Ultra HD, 4K, cinematic composition." # for english prompt, "zh": "超清,4K,电影级构图" # for chinese prompt, } # Generate image prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition''' negative_prompt = " " # using an empty string if you do not have specific concept to remove # Generate with different aspect ratios aspect_ratios = { "1:1": (1328, 1328), "16:9": (1664, 928), "9:16": (928, 1664), "4:3": (1472, 1140), "3:4": (1140, 1472), "3:2": (1584, 1056), "2:3": (1056, 1584), } width, height = aspect_ratios["16:9"] image = pipe( prompt=prompt + positive_magic["en"], negative_prompt=negative_prompt, width=width, height=height, num_inference_steps=50, true_cfg_scale=4.0, generator=torch.Generator(device="cuda").manual_seed(42) ).images[0] image.save("example.png") ``` -------------------------------- ### Run Qwen-Image with TorchAO quantization Source: https://huggingface.co/Qwen/Qwen-Image/blob/93ea1cbcbb4cfed308d0a6d7449e9b6cb2c56d57/README.md Initializes the model with int8 weight-only quantization and executes an image generation pipeline. ```python import torch from diffusers import AutoModel, DiffusionPipeline, TorchAoConfig model_id = "Qwen/Qwen-Image" torch_dtype = torch.bfloat16 device = "cuda" quantization_config = TorchAoConfig("int8wo") transformer = AutoModel.from_pretrained( model_id, subfolder="transformer", quantization_config=quantization_config, torch_dtype=torch_dtype, ) pipe = DiffusionPipeline.from_pretrained(model_id, transformer=transformer, torch_dtype=torch_dtype) pipe.enable_model_cpu_offload() prompt = "a woman and a man sitting at a cafe, the woman has red hair and she's wearing purple sweater with a black scarf and a white hat, the man is sitting on the other side of the table and he's wearing a white shirt with a purple scarf and red hat, both of them are sipping their coffee while in the table there's some cake slices on their respective plates, each with forks and knives at each side." negative_prompt = "" generator = torch.Generator(device="cuda").manual_seed(42) image = pipe( prompt=prompt, negative_prompt=negative_prompt, width=1664, height=928, num_inference_steps=25, true_cfg_scale=4.0, generator=generator, ).images[0] image.save("qwen_torchao.png") ``` -------------------------------- ### DiffusionPipeline.from_pretrained Source: https://huggingface.co/Qwen/Qwen-Image/blame/0770fddc587fa1795e0a9eb01dd40218fcbdd524/LICENSE Initializes the Qwen-Image model pipeline using the Hugging Face Diffusers library. ```APIDOC ## DiffusionPipeline.from_pretrained("Qwen/Qwen-Image") ### Description Loads the pre-trained Qwen-Image model into a pipeline for inference. ### Parameters - **pretrained_model_name_or_path** (string) - Required - The model identifier "Qwen/Qwen-Image". - **dtype** (torch.dtype) - Optional - The data type for model weights (e.g., torch.bfloat16). - **device_map** (string) - Optional - The device to run the model on (e.g., "cuda"). ### Usage Example ```python from diffusers import DiffusionPipeline import torch pipe = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image", dtype=torch.bfloat16, device_map="cuda") ``` ``` -------------------------------- ### Serve Qwen3-Embedding with vLLM Source: https://huggingface.co/Qwen/Qwen-Image-Edit/discussions/28 Run vLLM with specific Matryoshka embedding dimensions to avoid errors. ```bash vllm serve Qwen/Qwen3-Embedding-4B --hf-overrides '{"is_matryoshka": true, "matryoshka_dimensions":[32,64,128,256,512,768,1024,2560]}' ``` -------------------------------- ### Model Metadata Configuration Source: https://huggingface.co/Qwen/Qwen-Image/blame/48c2192f09c9182f8bcf990090cc15a00625506c/README.md YAML frontmatter defining model license, language, and library compatibility. ```yaml --- license: apache-2.0 language: - en - zh library_name: diffusers pipeline_tag: text-to-image --- ```