Views
No views yet
Note: This is a repackaging of the black-forest-labs/FLUX.2-dev model. Only theflux2-dev.safetensorsfile located in the root directory was removed, as it contained the same model as the one defined in thetransformer/folder. Because of this,diffuserswas loading the transformer twice, causing out-of-memory (OOM) errors. By removing this file, the duplicate loading was avoided and memory usage during inference was reduced from approximately 178 GB to 110 GB, enabling stable execution.


FLUX.2 [dev] is a 32 billion parameter rectified flow transformer capable of generating, editing and combining images based on text instructions.
For more information, please read our blog post.FLUX.2 [dev] more efficient.FLUX.2 [dev], as well as sampling code, in a dedicated github repository.
Developers and creatives looking to build on top of FLUX.2 [dev] are encouraged to use this as a starting point.1import torch
2from diffusers import Flux2Pipeline
3from diffusers.utils import load_image
4from huggingface_hub import get_token
5import requests
6import io
7
8repo_id = "diffusers/FLUX.2-dev-bnb-4bit" #quantized text-encoder and DiT. VAE still in bf16
9device = "cuda:0"
10torch_dtype = torch.bfloat16
11
12def remote_text_encoder(prompts):
13 response = requests.post(
14 "https://remote-text-encoder-flux-2.huggingface.co/predict",
15 json={"prompt": prompts},
16 headers={
17 "Authorization": f"Bearer {get_token()}",
18 "Content-Type": "application/json"
19 }
20 )
21 prompt_embeds = torch.load(io.BytesIO(response.content))
22
23 return prompt_embeds.to(device)
24
25pipe = Flux2Pipeline.from_pretrained(
26 repo_id, text_encoder=None, torch_dtype=torch_dtype
27).to(device)
28
29prompt = "Realistic macro photograph of a hermit crab using a soda can as its shell, partially emerging from the can, captured with sharp detail and natural colors, on a sunlit beach with soft shadows and a shallow depth of field, with blurred ocean waves in the background. The can has the text `BFL Diffusers` on it and it has a color gradient that start with #FF5733 at the top and transitions to #33FF57 at the bottom."
30
31#cat_image = load_image("https://huggingface.co/spaces/zerogpu-aoti/FLUX.1-Kontext-Dev-fp8-dynamic/resolve/main/cat.png")
32image = pipe(
33 prompt_embeds=remote_text_encoder(prompt),
34 #image=[cat_image] #optional multi-image input
35 generator=torch.Generator(device=device).manual_seed(42),
36 num_inference_steps=50, #28 steps can be a good trade-off
37 guidance_scale=4,
38).images[0]
39
40image.save("flux2_output.png")1import torch
2from diffusers.pipelines.flux2.pipeline_flux2 import Flux2Pipeline
3from transformers import Mistral3ForConditionalGeneration
4from diffusers.models.transformers.transformer_flux2 import Flux2Transformer2DModel
5from diffusers.models.autoencoders.autoencoder_kl_flux2 import AutoencoderKLFlux2
6
7MODEL_ID = "Aquiles-ai/FLUX.2-dev"
8
9text_encoder = Mistral3ForConditionalGeneration.from_pretrained(
10 MODEL_ID, subfolder="text_encoder", torch_dtype=torch.bfloat16, device_map="cuda"
11)
12
13dit = Flux2Transformer2DModel.from_pretrained(
14 MODEL_ID, subfolder="transformer", torch_dtype=torch.bfloat16, device_map="cuda"
15)
16
17vae = AutoencoderKLFlux2.from_pretrained(
18 MODEL_ID,
19 subfolder="vae",
20 torch_dtype=torch.bfloat16.to("cuda")
21)
22
23pipeline = Flux2Pipeline.from_pretrained(
24 MODEL_ID, text_encoder=text_encoder, transformer=dit, vae=vae, dtype=torch.bfloat16
25).to(device="cuda")
26
27prompt = "Realistic macro photograph of a hermit crab using a soda can as its shell, partially emerging from the can, captured with sharp detail and natural colors, on a sunlit beach with soft shadows and a shallow depth of field, with blurred ocean waves in the background. The can has the text `BFL Diffusers` on it and it has a color gradient that start with #FF5733 at the top and transitions to #33FF57 at the bottom."
28
29output = pipeline(
30 prompt=prompt,
31 num_inference_steps=50,
32 generator=torch.Generator(device="cuda").manual_seed(42),
33 guidance_scale=4,
34).images[0]
35
36output.save("flux2_output.png")1import torch
2from diffusers.pipelines.flux2.pipeline_flux2 import Flux2Pipeline
3from transformers import Mistral3ForConditionalGeneration
4from diffusers.models.transformers.transformer_flux2 import Flux2Transformer2DModel
5from diffusers.models.autoencoders.autoencoder_kl_flux2 import AutoencoderKLFlux2
6
7MODEL_ID = "Aquiles-ai/FLUX.2-dev"
8
9MODEL_4BIT = "diffusers/FLUX.2-dev-bnb-4bit"
10
11text_encoder = Mistral3ForConditionalGeneration.from_pretrained(
12 MODEL_4BIT, subfolder="text_encoder", torch_dtype=torch.bfloat16, device_map="cuda"
13)
14
15dit = Flux2Transformer2DModel.from_pretrained(
16 MODEL_ID, subfolder="transformer", torch_dtype=torch.bfloat16, device_map="cuda"
17)
18
19vae = AutoencoderKLFlux2.from_pretrained(
20 MODEL_ID,
21 subfolder="vae",
22 torch_dtype=torch.bfloat16.to("cuda")
23)
24
25pipeline = Flux2Pipeline.from_pretrained(
26 MODEL_ID, text_encoder=text_encoder, transformer=dit, vae=vae, dtype=torch.bfloat16
27).to(device="cuda")
28
29prompt = "Realistic macro photograph of a hermit crab using a soda can as its shell, partially emerging from the can, captured with sharp detail and natural colors, on a sunlit beach with soft shadows and a shallow depth of field, with blurred ocean waves in the background. The can has the text `BFL Diffusers` on it and it has a color gradient that start with #FF5733 at the top and transitions to #33FF57 at the bottom."
30
31output = pipeline(
32 prompt=prompt,
33 num_inference_steps=50,
34 generator=torch.Generator(device="cuda").manual_seed(42),
35 guidance_scale=4,
36).images[0]
37
38output.save("flux2_output.png")