Text-to-Image
Diffusers
Safetensors
English
Flux2KleinPipeline
flux
flux2-klein
quantization
sdnq
4-bit precision
dynamic-quantization
low-vram
google-colab
t4
batch-image-edit
background-removal
8-bit precision
Instructions to use codeShare/FLUX.2-klein-AIO-SDNQ-4bit-dynamic with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use codeShare/FLUX.2-klein-AIO-SDNQ-4bit-dynamic with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("codeShare/FLUX.2-klein-AIO-SDNQ-4bit-dynamic", torch_dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
Create modeling_flux2.py
Browse files
transformer/modeling_flux2.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from diffusers import ModelMixin, ConfigMixin
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class Flux2Transformer2DModel(ModelMixin, ConfigMixin):
|
| 7 |
+
config_name = "config.json"
|
| 8 |
+
|
| 9 |
+
def __init__(self, **kwargs):
|
| 10 |
+
super().__init__()
|
| 11 |
+
|
| 12 |
+
# Store config
|
| 13 |
+
self.register_to_config(**kwargs)
|
| 14 |
+
|
| 15 |
+
# Internal storage (safe for arbitrary keys)
|
| 16 |
+
self._sd = {}
|
| 17 |
+
|
| 18 |
+
# -----------------------------
|
| 19 |
+
# LOAD: accept ANY state dict
|
| 20 |
+
# -----------------------------
|
| 21 |
+
def load_state_dict(self, state_dict, strict=False):
|
| 22 |
+
"""
|
| 23 |
+
Store raw tensors exactly as-is.
|
| 24 |
+
No validation, no structure assumptions.
|
| 25 |
+
"""
|
| 26 |
+
self._sd = {}
|
| 27 |
+
|
| 28 |
+
for k, v in state_dict.items():
|
| 29 |
+
if isinstance(v, torch.Tensor):
|
| 30 |
+
self._sd[k] = v.contiguous()
|
| 31 |
+
else:
|
| 32 |
+
self._sd[k] = v
|
| 33 |
+
|
| 34 |
+
# Pretend everything matched
|
| 35 |
+
return torch.nn.modules.module._IncompatibleKeys([], [])
|
| 36 |
+
|
| 37 |
+
# -----------------------------
|
| 38 |
+
# SAVE: return original weights
|
| 39 |
+
# -----------------------------
|
| 40 |
+
def state_dict(self, *args, **kwargs):
|
| 41 |
+
return dict(self._sd)
|
| 42 |
+
|
| 43 |
+
# -----------------------------
|
| 44 |
+
# Required by diffusers internals
|
| 45 |
+
# -----------------------------
|
| 46 |
+
def _convert_deprecated_attention_blocks(self, state_dict):
|
| 47 |
+
# Diffusers sometimes calls this
|
| 48 |
+
return state_dict
|
| 49 |
+
|
| 50 |
+
# -----------------------------
|
| 51 |
+
# Forward (dummy)
|
| 52 |
+
# -----------------------------
|
| 53 |
+
def forward(self, *args, **kwargs):
|
| 54 |
+
raise RuntimeError(
|
| 55 |
+
"Flux2Transformer2DModel is a stub loader. "
|
| 56 |
+
"It cannot run inference."
|
| 57 |
+
)
|