Views
No views yet
![FLUX.1 [dev] Grid](./dev_grid.png)
BitsAndBytes in NF4 format. This enables GPU inference with reduced VRAM requirements, making it
accessible even on the Google Colab free tier or on GPUs with 12GB VRAM.pip install bitsandbytes==0.48.1 diffusers==0.35.1 peft==0.17.1 protobuf==5.29.5 sentencepiece==0.2.1 transformers==4.56.11import torch
2
3print(torch.cuda.get_device_capability()[0])1import torch
2from diffusers import FluxKontextPipeline
3from diffusers.utils import load_image
4
5ckpt_4bit_id = "aniketppanchal/flux.1-kontext-dev-nf4-pkg"
6input_image = load_image("<your_image_path_or_url_here>")
7prompt = "<your_editing_prompt_here>"
8height = 1024
9width = 1024
10
11pipeline = FluxKontextPipeline.from_pretrained(
12 ckpt_4bit_id,
13 torch_dtype=torch.bfloat16,
14 device_map="cuda",
15)
16
17image = pipeline(
18 image=input_image,
19 prompt=prompt,
20 height=height,
21 width=width,
22 num_inference_steps=28,
23 guidance_scale=3.5,
24 max_sequence_length=512,
25 max_area=height * width,
26).images[0]
27image.save("output.png")1import gc
2
3import torch
4from diffusers import FluxKontextPipeline, FluxTransformer2DModel
5from diffusers.pipelines.flux.pipeline_flux_kontext import PREFERRED_KONTEXT_RESOLUTIONS
6from diffusers.utils import load_image
7from transformers import T5EncoderModel
8
9ckpt_4bit_id = "aniketppanchal/flux.1-kontext-dev-nf4-pkg"
10input_image = load_image("<your_image_path_or_url_here>")
11prompt = "<your_editing_prompt_here>"
12height = 1024
13width = 1024
14
15major, _ = torch.cuda.get_device_capability()
16
17# ----------Encode Prompt Embeddings----------
18
19text_encoder_2 = T5EncoderModel.from_pretrained(
20 ckpt_4bit_id,
21 subfolder="text_encoder_2",
22 torch_dtype=torch.bfloat16 if major >= 8 else torch.float16,
23 device_map="cuda",
24)
25pipeline = FluxKontextPipeline.from_pretrained(
26 ckpt_4bit_id,
27 text_encoder_2=text_encoder_2,
28 transformer=None,
29 vae=None,
30 torch_dtype=torch.bfloat16,
31 device_map="cuda",
32)
33
34with torch.no_grad():
35 prompt_embeds, pooled_prompt_embeds, _ = pipeline.encode_prompt(
36 prompt=prompt,
37 max_sequence_length=512,
38 )
39
40del text_encoder_2, pipeline
41gc.collect()
42torch.cuda.empty_cache()
43
44# ----------Preprocess and Encode Image to Latents----------
45
46pipeline = FluxKontextPipeline.from_pretrained(
47 ckpt_4bit_id,
48 text_encoder=None,
49 text_encoder_2=None,
50 tokenizer=None,
51 tokenizer_2=None,
52 transformer=None,
53 torch_dtype=torch.bfloat16,
54 device_map="cuda",
55)
56
57image_h, image_w = pipeline.image_processor.get_default_height_width(input_image)
58aspect_ratio = image_w / image_h
59
60_, pref_image_w, pref_image_h = min(
61 (abs(aspect_ratio - w / h), w, h) for w, h in PREFERRED_KONTEXT_RESOLUTIONS
62)
63multiple_of = pipeline.vae_scale_factor * 2
64new_image_w = pref_image_w // multiple_of * multiple_of
65new_image_h = pref_image_h // multiple_of * multiple_of
66
67processed_image = pipeline.image_processor.resize(
68 input_image.copy(),
69 new_image_h,
70 new_image_w,
71)
72processed_image = pipeline.image_processor.preprocess(
73 processed_image,
74 new_image_h,
75 new_image_w,
76)
77processed_image = processed_image.to(device=pipeline.device, dtype=pipeline.vae.dtype)
78
79with torch.no_grad():
80 image_latents = pipeline._encode_vae_image(processed_image, generator=None)
81
82del processed_image, pipeline
83gc.collect()
84torch.cuda.empty_cache()
85
86# ----------Generate Diffusion Latents----------
87
88transformer = FluxTransformer2DModel.from_pretrained(
89 ckpt_4bit_id,
90 subfolder="transformer",
91 torch_dtype=torch.bfloat16 if major >= 8 else torch.float16,
92 device_map="cuda",
93)
94pipeline = FluxKontextPipeline.from_pretrained(
95 ckpt_4bit_id,
96 text_encoder=None,
97 text_encoder_2=None,
98 tokenizer=None,
99 tokenizer_2=None,
100 transformer=transformer,
101 vae=None,
102 torch_dtype=torch.bfloat16,
103 device_map="cuda",
104)
105
106packed_latents = pipeline(
107 image=image_latents,
108 height=height,
109 width=width,
110 num_inference_steps=28,
111 guidance_scale=3.5,
112 prompt_embeds=prompt_embeds,
113 pooled_prompt_embeds=pooled_prompt_embeds,
114 output_type="latent",
115 max_sequence_length=512,
116 max_area=height * width,
117).images
118
119del (
120 prompt_embeds,
121 pooled_prompt_embeds,
122 image_latents,
123 transformer,
124 pipeline,
125)
126gc.collect()
127torch.cuda.empty_cache()
128
129# ----------Decode Latents to Image----------
130
131pipeline = FluxKontextPipeline.from_pretrained(
132 ckpt_4bit_id,
133 text_encoder=None,
134 text_encoder_2=None,
135 tokenizer=None,
136 tokenizer_2=None,
137 transformer=None,
138 torch_dtype=torch.bfloat16,
139 device_map="cuda",
140)
141
142unpacked_latents = (
143 pipeline._unpack_latents(
144 packed_latents,
145 height=height,
146 width=width,
147 vae_scale_factor=pipeline.vae_scale_factor,
148 )
149 / pipeline.vae.config.scaling_factor
150 + pipeline.vae.config.shift_factor
151).to(device=pipeline.device, dtype=pipeline.vae.dtype)
152
153with torch.no_grad():
154 image_tensor = pipeline.vae.decode(unpacked_latents, return_dict=False)[0]
155
156image = pipeline.image_processor.postprocess(image_tensor)[0]
157image.save("output.png")
158
159del packed_latents, unpacked_latents, image_tensor, pipeline
160gc.collect()
161torch.cuda.empty_cache()LICENSE.md file corresponds
to the frozen state of the original repository as of 3rd November 2025. For the latest version, see
the FLUX.1 [dev] License.