Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -234,20 +234,89 @@ scheduler_config = {
|
|
| 234 |
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
|
| 235 |
|
| 236 |
# Load the model pipeline
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
pipe.load_lora_weights(
|
| 241 |
-
|
| 242 |
-
|
| 243 |
)
|
| 244 |
pipe.fuse_lora()
|
|
|
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
# # Apply the same optimizations from the first version
|
| 253 |
# pipe.transformer.__class__ = QwenImageTransformer2DModel
|
|
|
|
| 234 |
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
|
| 235 |
|
| 236 |
# Load the model pipeline
|
| 237 |
+
from safetensors.torch import load_file
|
| 238 |
+
from huggingface_hub import hf_hub_download
|
| 239 |
+
import torch.nn.functional as F
|
| 240 |
+
|
| 241 |
+
# --- 1. setup pipeline with lightning (this works fine) ---
|
| 242 |
+
pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 243 |
+
"Qwen/Qwen-Image-Edit-2511",
|
| 244 |
+
scheduler=scheduler,
|
| 245 |
+
torch_dtype=dtype
|
| 246 |
+
).to(device)
|
| 247 |
+
|
| 248 |
+
print("loading lightning lora...")
|
| 249 |
pipe.load_lora_weights(
|
| 250 |
+
"lightx2v/Qwen-Image-Edit-2511-Lightning",
|
| 251 |
+
weight_name="Qwen-Image-Edit-2511-Lightning-4steps-V1.0-bf16.safetensors"
|
| 252 |
)
|
| 253 |
pipe.fuse_lora()
|
| 254 |
+
print("lightning lora fused.")
|
| 255 |
|
| 256 |
+
# --- 2. manual surgery for lokr (snofs) ---
|
| 257 |
+
print("attempting manual lokr injection for snofs...")
|
| 258 |
+
|
| 259 |
+
try:
|
| 260 |
+
# download the file directly
|
| 261 |
+
lora_path = hf_hub_download(repo_id="headlesssetton/kjfakjf", filename="Qwen_Snofs_1_2.safetensors")
|
| 262 |
+
state_dict = load_file(lora_path)
|
| 263 |
+
|
| 264 |
+
# lokr injection parameters
|
| 265 |
+
lokr_scale = 1.0 # adjust strength here
|
| 266 |
+
|
| 267 |
+
# iterate and merge
|
| 268 |
+
updates = 0
|
| 269 |
+
with torch.no_grad():
|
| 270 |
+
# group keys by layer prefix
|
| 271 |
+
prefixes = set()
|
| 272 |
+
for key in state_dict.keys():
|
| 273 |
+
if "lokr_w1" in key:
|
| 274 |
+
prefixes.add(key.replace(".lokr_w1", ""))
|
| 275 |
+
|
| 276 |
+
for prefix in prefixes:
|
| 277 |
+
# extract weights
|
| 278 |
+
w1 = state_dict[f"{prefix}.lokr_w1"].to(device, dtype=dtype)
|
| 279 |
+
w2 = state_dict[f"{prefix}.lokr_w2"].to(device, dtype=dtype)
|
| 280 |
+
alpha = state_dict.get(f"{prefix}.alpha", None)
|
| 281 |
+
|
| 282 |
+
# calculate scaling
|
| 283 |
+
# lokr usually uses alpha / sqrt(rank) or similar, but often just alpha is enough
|
| 284 |
+
# if alpha is present, scale = alpha / w1.shape[0] (or similar convention)
|
| 285 |
+
# here we will assume simple multiplication or alpha scaling if provided
|
| 286 |
+
scale = lokr_scale
|
| 287 |
+
if alpha is not None:
|
| 288 |
+
scale *= (alpha / w1.shape[0]) # standard lora scaling convention, might vary for lokr
|
| 289 |
+
|
| 290 |
+
# compute delta: kronecker product
|
| 291 |
+
# w1: (a, b), w2: (c, d) -> result: (a*c, b*d)
|
| 292 |
+
# torch.kron is (a*c, b*d)
|
| 293 |
+
delta = torch.kron(w1, w2) * scale
|
| 294 |
+
|
| 295 |
+
# find target layer in model
|
| 296 |
+
# prefix example: "transformer_blocks.0.attn.add_k_proj"
|
| 297 |
+
# pipe.transformer matches this structure directly
|
| 298 |
+
path_parts = prefix.split('.')
|
| 299 |
+
target = pipe.transformer
|
| 300 |
+
try:
|
| 301 |
+
for part in path_parts:
|
| 302 |
+
target = getattr(target, part)
|
| 303 |
+
|
| 304 |
+
# check shapes
|
| 305 |
+
if target.weight.shape == delta.shape:
|
| 306 |
+
target.weight.add_(delta) # in-place merge
|
| 307 |
+
updates += 1
|
| 308 |
+
else:
|
| 309 |
+
print(f"shape mismatch for {prefix}: model {target.weight.shape} vs lora {delta.shape}")
|
| 310 |
+
except AttributeError:
|
| 311 |
+
print(f"layer not found: {prefix}")
|
| 312 |
+
|
| 313 |
+
print(f"successfully injected {updates} lokr layers manually.")
|
| 314 |
+
|
| 315 |
+
except Exception as e:
|
| 316 |
+
print(f"lokr injection failed: {e}")
|
| 317 |
+
print("running with lightning lora only.")
|
| 318 |
+
|
| 319 |
+
# --- end of surgery ---
|
| 320 |
|
| 321 |
# # Apply the same optimizations from the first version
|
| 322 |
# pipe.transformer.__class__ = QwenImageTransformer2DModel
|