Image-to-Image
Diffusers
Safetensors
Transformers
GGUF
English
Flux2Pipeline
image-generation
image-editing
text-to-image
flux2
flux
quantization
hqq
optimization
quantized
2bit
8-bit precision
Instructions to use AlekseyCalvin/FLUX2_dev_2bit_hqq with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use AlekseyCalvin/FLUX2_dev_2bit_hqq with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("AlekseyCalvin/FLUX2_dev_2bit_hqq", 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] - Transformers
How to use AlekseyCalvin/FLUX2_dev_2bit_hqq with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-image" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("image-to-image", model="AlekseyCalvin/FLUX2_dev_2bit_hqq")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("AlekseyCalvin/FLUX2_dev_2bit_hqq", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -33,7 +33,95 @@ To use, make sure to install the following libraries:
|
|
| 33 |
pip install git+https://github.com/huggingface/diffusers.git@main
|
| 34 |
pip install transformers>=4.53.1
|
| 35 |
pip install -U hqq
|
|
|
|
| 36 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# MORE INFO:
|
| 39 |
[HQQ doc](https://huggingface.co/docs/transformers/en/quantization/hqq) at HugingFace. <br>
|
|
|
|
| 33 |
pip install git+https://github.com/huggingface/diffusers.git@main
|
| 34 |
pip install transformers>=4.53.1
|
| 35 |
pip install -U hqq
|
| 36 |
+
pip install accelerate huggingface_hub safetensors
|
| 37 |
```
|
| 38 |
+
+ torch, naturally, as you might compile/install it for your device.
|
| 39 |
+
+
|
| 40 |
+
# INFERENCE
|
| 41 |
+
*(Sorry, but you may have to re-construct thee pipe on-thee-fly, as they say...)*
|
| 42 |
+
|
| 43 |
+
```
|
| 44 |
+
import torch
|
| 45 |
+
from diffusers import Flux2Pipeline, Flux2Transformer2DModel
|
| 46 |
+
from transformers import AutoModel
|
| 47 |
+
from hqq.core.quantize import HQQLinear, BaseQuantizeConfig
|
| 48 |
+
from huggingface_hub import hf_hub_download
|
| 49 |
+
from safetensors.torch import load_file
|
| 50 |
+
|
| 51 |
+
def replace_with_hqq(model, quant_config):
|
| 52 |
+
"""
|
| 53 |
+
Recursively replaces nn.Linear layers with HQQLinear layers.
|
| 54 |
+
This must match the exact logic used during quantization.
|
| 55 |
+
"""
|
| 56 |
+
for name, child in model.named_children():
|
| 57 |
+
if isinstance(child, torch.nn.Linear):
|
| 58 |
+
# Create empty HQQ layer
|
| 59 |
+
hqq_layer = HQQLinear(
|
| 60 |
+
child,
|
| 61 |
+
quant_config=quant_config,
|
| 62 |
+
compute_dtype=torch.bfloat16,
|
| 63 |
+
device="cuda",
|
| 64 |
+
initialize=False
|
| 65 |
+
)
|
| 66 |
+
setattr(model, name, hqq_layer)
|
| 67 |
+
else:
|
| 68 |
+
replace_with_hqq(child, quant_config)
|
| 69 |
+
|
| 70 |
+
hqq_config = BaseQuantizeConfig(
|
| 71 |
+
nbits=2,
|
| 72 |
+
group_size=64,
|
| 73 |
+
axis=1
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
model_id = "AlekseyCalvin/FLUX2_dev_2bit_hqq"
|
| 77 |
+
|
| 78 |
+
print("Loading Text Encoder (Mistral)...")
|
| 79 |
+
# Initialize skeleton
|
| 80 |
+
text_encoder = AutoModel.from_pretrained(
|
| 81 |
+
"black-forest-labs/FLUX.2-dev", # Load config from base model
|
| 82 |
+
subfolder="text_encoder",
|
| 83 |
+
torch_dtype=torch.bfloat16
|
| 84 |
+
)
|
| 85 |
+
# Swap layers
|
| 86 |
+
replace_with_hqq(text_encoder, hqq_config)
|
| 87 |
+
# Load quantized weights
|
| 88 |
+
te_path = hf_hub_download(model_id, filename="text_encoder/model.safetensors")
|
| 89 |
+
te_state_dict = load_file(te_path)
|
| 90 |
+
text_encoder.load_state_dict(te_state_dict)
|
| 91 |
+
text_encoder = text_encoder.to("cuda")
|
| 92 |
+
|
| 93 |
+
print("Loading Transformer (Flux 2)...")
|
| 94 |
+
# Initialize skeleton
|
| 95 |
+
transformer = Flux2Transformer2DModel.from_pretrained(
|
| 96 |
+
"black-forest-labs/FLUX.2-dev",
|
| 97 |
+
subfolder="transformer",
|
| 98 |
+
torch_dtype=torch.bfloat16
|
| 99 |
+
)
|
| 100 |
+
# Swap layers
|
| 101 |
+
replace_with_hqq(transformer, hqq_config)
|
| 102 |
+
# Load quantized weights
|
| 103 |
+
tr_path = hf_hub_download(model_id, filename="transformer/diffusion_pytorch_model.safetensors")
|
| 104 |
+
tr_state_dict = load_file(tr_path)
|
| 105 |
+
transformer.load_state_dict(tr_state_dict)
|
| 106 |
+
transformer = transformer.to("cuda")
|
| 107 |
+
|
| 108 |
+
print("Assembling Pipeline...")
|
| 109 |
+
pipe = Flux2Pipeline.from_pretrained(
|
| 110 |
+
"black-forest-labs/FLUX.2-dev",
|
| 111 |
+
transformer=transformer,
|
| 112 |
+
text_encoder=text_encoder,
|
| 113 |
+
torch_dtype=torch.bfloat16
|
| 114 |
+
)
|
| 115 |
+
pipe.enable_model_cpu_offload()
|
| 116 |
+
|
| 117 |
+
print("Ready for Inference!")
|
| 118 |
+
prompt = "A photo of a sneaky koala hiding behind book stacks at a library, calm snowy landscape visible through large window in the backdrop..."
|
| 119 |
+
image = pipe(prompt, guidance_scale=4, num_inference_steps=40).images[0]
|
| 120 |
+
image.save("KoalaTesting.png")
|
| 121 |
+
```
|
| 122 |
+
If the above doesn't work, try the inference method at the [HQQ Git Repo](https://github.com/dropbox/hqq)... <br>
|
| 123 |
+
If neither works, please leave comment. I will do more testing soon and revise, if need be. <br>
|
| 124 |
+
Crucially: HQQ should work with PEFT/LoRA inference + training. <br>
|
| 125 |
|
| 126 |
# MORE INFO:
|
| 127 |
[HQQ doc](https://huggingface.co/docs/transformers/en/quantization/hqq) at HugingFace. <br>
|