Spaces:
Paused
Paused
Update app_quant_latent.py
Browse files- app_quant_latent.py +221 -8
app_quant_latent.py
CHANGED
|
@@ -116,30 +116,242 @@ def latent_to_image(latent):
|
|
| 116 |
# SAFE TRANSFORMER INSPECTION
|
| 117 |
# ============================================================
|
| 118 |
def inspect_transformer(model, name):
|
| 119 |
-
log(f"\n
|
|
|
|
|
|
|
| 120 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
candidates = ["transformer_blocks", "blocks", "layers", "encoder", "model"]
|
| 122 |
blocks = None
|
|
|
|
| 123 |
|
| 124 |
for attr in candidates:
|
| 125 |
if hasattr(model, attr):
|
| 126 |
blocks = getattr(model, attr)
|
|
|
|
| 127 |
break
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
if blocks is None:
|
| 130 |
-
log(
|
| 131 |
return
|
| 132 |
|
| 133 |
-
if hasattr(blocks, "__len__"):
|
| 134 |
-
log(f"Total Blocks = {len(blocks)}")
|
| 135 |
-
else:
|
| 136 |
log("โ ๏ธ Blocks exist but are not iterable")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
-
log(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
# ============================================================
|
|
@@ -240,6 +452,7 @@ try:
|
|
| 240 |
|
| 241 |
pipe.set_adapters(["lora",], adapter_weights=[1.])
|
| 242 |
pipe.fuse_lora(adapter_names=["lora"], lora_scale=0.75)
|
|
|
|
| 243 |
# pipe.unload_lora_weights()
|
| 244 |
pipe.to("cuda")
|
| 245 |
log("โ
Pipeline built successfully.")
|
|
|
|
| 116 |
# SAFE TRANSFORMER INSPECTION
|
| 117 |
# ============================================================
|
| 118 |
def inspect_transformer(model, name):
|
| 119 |
+
log(f"\n๐๐ FULL TRANSFORMER DEBUG DUMP: {name}")
|
| 120 |
+
log("=" * 80)
|
| 121 |
+
|
| 122 |
try:
|
| 123 |
+
log(f"Model class : {model.__class__.__name__}")
|
| 124 |
+
log(f"DType : {getattr(model, 'dtype', 'unknown')}")
|
| 125 |
+
log(f"Device : {next(model.parameters()).device}")
|
| 126 |
+
log(f"Requires Grad? : {any(p.requires_grad for p in model.parameters())}")
|
| 127 |
+
|
| 128 |
+
# Check quantization
|
| 129 |
+
if hasattr(model, "is_loaded_in_4bit"):
|
| 130 |
+
log(f"4bit Quantization : {model.is_loaded_in_4bit}")
|
| 131 |
+
if hasattr(model, "is_loaded_in_8bit"):
|
| 132 |
+
log(f"8bit Quantization : {model.is_loaded_in_8bit}")
|
| 133 |
+
|
| 134 |
+
# Find blocks
|
| 135 |
candidates = ["transformer_blocks", "blocks", "layers", "encoder", "model"]
|
| 136 |
blocks = None
|
| 137 |
+
chosen_attr = None
|
| 138 |
|
| 139 |
for attr in candidates:
|
| 140 |
if hasattr(model, attr):
|
| 141 |
blocks = getattr(model, attr)
|
| 142 |
+
chosen_attr = attr
|
| 143 |
break
|
| 144 |
|
| 145 |
+
log(f"Block container attr : {chosen_attr}")
|
| 146 |
+
|
| 147 |
+
if blocks is None:
|
| 148 |
+
log("โ ๏ธ No valid block container found.")
|
| 149 |
+
return
|
| 150 |
+
|
| 151 |
+
if not hasattr(blocks, "__len__"):
|
| 152 |
+
log("โ ๏ธ Blocks exist but not iterable.")
|
| 153 |
+
return
|
| 154 |
+
|
| 155 |
+
total = len(blocks)
|
| 156 |
+
log(f"Total Blocks : {total}")
|
| 157 |
+
log("-" * 80)
|
| 158 |
+
|
| 159 |
+
# Inspect first N blocks
|
| 160 |
+
N = min(20, total)
|
| 161 |
+
for i in range(N):
|
| 162 |
+
block = blocks[i]
|
| 163 |
+
log(f"\n๐งฉ Block [{i}/{total-1}]")
|
| 164 |
+
log(f"Class: {block.__class__.__name__}")
|
| 165 |
+
|
| 166 |
+
# Print submodules
|
| 167 |
+
for n, m in block.named_children():
|
| 168 |
+
log(f" โโ {n}: {m.__class__.__name__}")
|
| 169 |
+
|
| 170 |
+
# Print attention related
|
| 171 |
+
if hasattr(block, "attn"):
|
| 172 |
+
attn = block.attn
|
| 173 |
+
log(f" โโ Attention: {attn.__class__.__name__}")
|
| 174 |
+
log(f" โ Heads : {getattr(attn, 'num_heads', 'unknown')}")
|
| 175 |
+
log(f" โ Dim : {getattr(attn, 'hidden_size', 'unknown')}")
|
| 176 |
+
log(f" โ Backend : {getattr(attn, 'attention_backend', 'unknown')}")
|
| 177 |
+
|
| 178 |
+
# Device + dtype info
|
| 179 |
+
try:
|
| 180 |
+
dev = next(block.parameters()).device
|
| 181 |
+
log(f" โโ Device : {dev}")
|
| 182 |
+
except StopIteration:
|
| 183 |
+
pass
|
| 184 |
+
|
| 185 |
+
try:
|
| 186 |
+
dt = next(block.parameters()).dtype
|
| 187 |
+
log(f" โโ DType : {dt}")
|
| 188 |
+
except StopIteration:
|
| 189 |
+
pass
|
| 190 |
+
|
| 191 |
+
log("\n๐ END TRANSFORMER DEBUG DUMP")
|
| 192 |
+
log("=" * 80)
|
| 193 |
+
|
| 194 |
+
except Exception as e:
|
| 195 |
+
log(f"โ ERROR IN INSPECTOR: {e}")
|
| 196 |
+
import torch
|
| 197 |
+
import time
|
| 198 |
+
|
| 199 |
+
# ---------- UTILITY ----------
|
| 200 |
+
def pretty_header(title):
|
| 201 |
+
log("\n\n" + "=" * 80)
|
| 202 |
+
log(f"๐๏ธ {title}")
|
| 203 |
+
log("=" * 80 + "\n")
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# ---------- MEMORY ----------
|
| 207 |
+
def get_vram(prefix=""):
|
| 208 |
+
try:
|
| 209 |
+
allocated = torch.cuda.memory_allocated() / 1024**2
|
| 210 |
+
reserved = torch.cuda.memory_reserved() / 1024**2
|
| 211 |
+
log(f"{prefix}Allocated VRAM : {allocated:.2f} MB")
|
| 212 |
+
log(f"{prefix}Reserved VRAM : {reserved:.2f} MB")
|
| 213 |
+
except:
|
| 214 |
+
log(f"{prefix}VRAM: CUDA not available")
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
# ---------- MODULE INSPECT ----------
|
| 218 |
+
def inspect_module(name, module):
|
| 219 |
+
pretty_header(f"๐ฌ Inspecting {name}")
|
| 220 |
+
|
| 221 |
+
try:
|
| 222 |
+
log(f"๐ฆ Class : {module.__class__.__name__}")
|
| 223 |
+
log(f"๐ข DType : {getattr(module, 'dtype', 'unknown')}")
|
| 224 |
+
log(f"๐ป Device : {next(module.parameters()).device}")
|
| 225 |
+
log(f"๐งฎ Params : {sum(p.numel() for p in module.parameters()):,}")
|
| 226 |
+
|
| 227 |
+
# Quantization state
|
| 228 |
+
if hasattr(module, "is_loaded_in_4bit"):
|
| 229 |
+
log(f"โ๏ธ 4-bit QLoRA : {module.is_loaded_in_4bit}")
|
| 230 |
+
if hasattr(module, "is_loaded_in_8bit"):
|
| 231 |
+
log(f"โ๏ธ 8-bit load : {module.is_loaded_in_8bit}")
|
| 232 |
+
|
| 233 |
+
# Attention backend (DiT)
|
| 234 |
+
if hasattr(module, "set_attention_backend"):
|
| 235 |
+
try:
|
| 236 |
+
attn = getattr(module, "attention_backend", None)
|
| 237 |
+
log(f"๐ Attention Backend: {attn}")
|
| 238 |
+
except:
|
| 239 |
+
pass
|
| 240 |
+
|
| 241 |
+
# Search for blocks
|
| 242 |
+
candidates = ["transformer_blocks", "blocks", "layers", "encoder", "model"]
|
| 243 |
+
blocks = None
|
| 244 |
+
chosen_attr = None
|
| 245 |
+
|
| 246 |
+
for attr in candidates:
|
| 247 |
+
if hasattr(module, attr):
|
| 248 |
+
blocks = getattr(module, attr)
|
| 249 |
+
chosen_attr = attr
|
| 250 |
+
break
|
| 251 |
+
|
| 252 |
+
log(f"\n๐ Block Container : {chosen_attr}")
|
| 253 |
+
|
| 254 |
if blocks is None:
|
| 255 |
+
log("โ ๏ธ No block structure found")
|
| 256 |
return
|
| 257 |
|
| 258 |
+
if not hasattr(blocks, "__len__"):
|
|
|
|
|
|
|
| 259 |
log("โ ๏ธ Blocks exist but are not iterable")
|
| 260 |
+
return
|
| 261 |
+
|
| 262 |
+
total = len(blocks)
|
| 263 |
+
log(f"๐ข Total Blocks : {total}\n")
|
| 264 |
+
|
| 265 |
+
# Inspect first 15 blocks
|
| 266 |
+
N = min(15, total)
|
| 267 |
+
|
| 268 |
+
for i in range(N):
|
| 269 |
+
blk = blocks[i]
|
| 270 |
+
log(f"\n๐งฉ Block [{i}/{total-1}] โ {blk.__class__.__name__}")
|
| 271 |
+
|
| 272 |
+
for n, m in blk.named_children():
|
| 273 |
+
log(f" โโ {n:<15} {m.__class__.__name__}")
|
| 274 |
+
|
| 275 |
+
# Attention details
|
| 276 |
+
if hasattr(blk, "attn"):
|
| 277 |
+
a = blk.attn
|
| 278 |
+
log(f" โโ Attention")
|
| 279 |
+
log(f" โ Heads : {getattr(a, 'num_heads', 'unknown')}")
|
| 280 |
+
log(f" โ Dim : {getattr(a, 'hidden_size', 'unknown')}")
|
| 281 |
+
log(f" โ Backend : {getattr(a, 'attention_backend', 'unknown')}")
|
| 282 |
+
|
| 283 |
+
# Device / dtype
|
| 284 |
+
try:
|
| 285 |
+
log(f" โโ Device : {next(blk.parameters()).device}")
|
| 286 |
+
log(f" โโ DType : {next(blk.parameters()).dtype}")
|
| 287 |
+
except StopIteration:
|
| 288 |
+
pass
|
| 289 |
+
|
| 290 |
+
get_vram(" โถ ")
|
| 291 |
+
|
| 292 |
+
except Exception as e:
|
| 293 |
+
log(f"โ Module inspect error: {e}")
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
# ---------- LORA INSPECTION ----------
|
| 297 |
+
def inspect_loras(pipe):
|
| 298 |
+
pretty_header("๐งฉ LoRA ADAPTERS")
|
| 299 |
+
|
| 300 |
+
try:
|
| 301 |
+
if not hasattr(pipe, "lora_state_dict") and not hasattr(pipe, "adapter_names"):
|
| 302 |
+
log("โ ๏ธ No LoRA system detected.")
|
| 303 |
+
return
|
| 304 |
|
| 305 |
+
if hasattr(pipe, "adapter_names"):
|
| 306 |
+
names = pipe.adapter_names
|
| 307 |
+
log(f"Available Adapters: {names}")
|
| 308 |
+
|
| 309 |
+
if hasattr(pipe, "active_adapters"):
|
| 310 |
+
log(f"Active Adapters : {pipe.active_adapters}")
|
| 311 |
+
|
| 312 |
+
if hasattr(pipe, "lora_scale"):
|
| 313 |
+
log(f"LoRA Scale : {pipe.lora_scale}")
|
| 314 |
+
|
| 315 |
+
# LoRA modules
|
| 316 |
+
if hasattr(pipe, "transformer") and hasattr(pipe.transformer, "modules"):
|
| 317 |
+
for name, module in pipe.transformer.named_modules():
|
| 318 |
+
if "lora" in name.lower():
|
| 319 |
+
log(f" ๐ง LoRA Module: {name} ({module.__class__.__name__})")
|
| 320 |
|
| 321 |
except Exception as e:
|
| 322 |
+
log(f"โ LoRA inspect error: {e}")
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
# ---------- PIPELINE INSPECTOR ----------
|
| 326 |
+
def debug_pipeline(pipe):
|
| 327 |
+
pretty_header("๐ FULL PIPELINE DEBUGGING")
|
| 328 |
+
|
| 329 |
+
try:
|
| 330 |
+
log(f"Pipeline Class : {pipe.__class__.__name__}")
|
| 331 |
+
log(f"Attention Impl : {getattr(pipe, 'attn_implementation', 'unknown')}")
|
| 332 |
+
log(f"Device : {pipe.device}")
|
| 333 |
+
except:
|
| 334 |
+
pass
|
| 335 |
+
|
| 336 |
+
get_vram("โถ ")
|
| 337 |
+
|
| 338 |
+
# Inspect TRANSFORMER
|
| 339 |
+
if hasattr(pipe, "transformer"):
|
| 340 |
+
inspect_module("Transformer", pipe.transformer)
|
| 341 |
+
|
| 342 |
+
# Inspect TEXT ENCODER
|
| 343 |
+
if hasattr(pipe, "text_encoder") and pipe.text_encoder is not None:
|
| 344 |
+
inspect_module("Text Encoder", pipe.text_encoder)
|
| 345 |
+
|
| 346 |
+
# Inspect UNET (if ZImage pipeline has it)
|
| 347 |
+
if hasattr(pipe, "unet"):
|
| 348 |
+
inspect_module("UNet", pipe.unet)
|
| 349 |
+
|
| 350 |
+
# LoRA adapters
|
| 351 |
+
inspect_loras(pipe)
|
| 352 |
+
|
| 353 |
+
pretty_header("๐ END DEBUG REPORT")
|
| 354 |
+
|
| 355 |
|
| 356 |
|
| 357 |
# ============================================================
|
|
|
|
| 452 |
|
| 453 |
pipe.set_adapters(["lora",], adapter_weights=[1.])
|
| 454 |
pipe.fuse_lora(adapter_names=["lora"], lora_scale=0.75)
|
| 455 |
+
debug_pipeline(pipe)
|
| 456 |
# pipe.unload_lora_weights()
|
| 457 |
pipe.to("cuda")
|
| 458 |
log("โ
Pipeline built successfully.")
|