|
| 1 | +import base64 |
| 2 | +import io |
| 3 | +import torch |
| 4 | +from diffusers import FluxPipeline |
| 5 | +import gc |
| 6 | +from diffusers import FluxTransformer2DModel |
| 7 | +from diffusers import AutoencoderKL |
| 8 | +from diffusers.image_processor import VaeImageProcessor |
| 9 | + |
| 10 | +from app.config import RESTAI_DEFAULT_DEVICE |
| 11 | + |
| 12 | +def flush(): |
| 13 | + gc.collect() |
| 14 | + torch.cuda.empty_cache() |
| 15 | + torch.cuda.reset_max_memory_allocated() |
| 16 | + torch.cuda.reset_peak_memory_stats() |
| 17 | + |
| 18 | +def worker(prompt, sharedmem): |
| 19 | + |
| 20 | + pipeline = FluxPipeline.from_pretrained( |
| 21 | + "black-forest-labs/FLUX.1-dev", |
| 22 | + transformer=None, |
| 23 | + vae=None, |
| 24 | + device_map="balanced", |
| 25 | + max_memory={0: "24GB", 1: "24GB"}, |
| 26 | + torch_dtype=torch.bfloat16 |
| 27 | + ) |
| 28 | + with torch.no_grad(): |
| 29 | + print("Encoding prompts.") |
| 30 | + prompt_embeds, pooled_prompt_embeds, text_ids = pipeline.encode_prompt( |
| 31 | + prompt=prompt, prompt_2=None, max_sequence_length=512 |
| 32 | + ) |
| 33 | + |
| 34 | + del pipeline.text_encoder |
| 35 | + del pipeline.text_encoder_2 |
| 36 | + del pipeline.tokenizer |
| 37 | + del pipeline.tokenizer_2 |
| 38 | + del pipeline |
| 39 | + |
| 40 | + flush() |
| 41 | + |
| 42 | + transformer = FluxTransformer2DModel.from_pretrained( |
| 43 | + "black-forest-labs/FLUX.1-dev", |
| 44 | + subfolder="transformer", |
| 45 | + device_map="auto", |
| 46 | + torch_dtype=torch.bfloat16 |
| 47 | + ) |
| 48 | + |
| 49 | + pipeline = FluxPipeline.from_pretrained( |
| 50 | + "black-forest-labs/FLUX.1-dev", |
| 51 | + text_encoder=None, |
| 52 | + text_encoder_2=None, |
| 53 | + tokenizer=None, |
| 54 | + tokenizer_2=None, |
| 55 | + vae=None, |
| 56 | + transformer=transformer, |
| 57 | + torch_dtype=torch.bfloat16 |
| 58 | + ) |
| 59 | + |
| 60 | + print("Running denoising.") |
| 61 | + height, width = 768, 1360 |
| 62 | + latents = pipeline( |
| 63 | + prompt_embeds=prompt_embeds, |
| 64 | + pooled_prompt_embeds=pooled_prompt_embeds, |
| 65 | + num_inference_steps=50, |
| 66 | + guidance_scale=3.5, |
| 67 | + height=height, |
| 68 | + width=width, |
| 69 | + output_type="latent", |
| 70 | + ).images |
| 71 | + |
| 72 | + del pipeline.transformer |
| 73 | + del pipeline |
| 74 | + |
| 75 | + flush() |
| 76 | + |
| 77 | + vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=torch.bfloat16).to("cuda") |
| 78 | + vae_scale_factor = 2 ** (len(vae.config.block_out_channels)) |
| 79 | + image_processor = VaeImageProcessor(vae_scale_factor=vae_scale_factor) |
| 80 | + |
| 81 | + with torch.no_grad(): |
| 82 | + print("Running decoding.") |
| 83 | + latents = FluxPipeline._unpack_latents(latents, height, width, vae_scale_factor) |
| 84 | + latents = (latents / vae.config.scaling_factor) + vae.config.shift_factor |
| 85 | + |
| 86 | + image = vae.decode(latents, return_dict=False)[0] |
| 87 | + image = image_processor.postprocess(image, output_type="pil") |
| 88 | + |
| 89 | + image_data = io.BytesIO() |
| 90 | + image[0].save(image_data, format="JPEG") |
| 91 | + image_base64 = base64.b64encode(image_data.getvalue()).decode('utf-8') |
| 92 | + |
| 93 | + sharedmem["image"] = image_base64 |
0 commit comments