Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -259,57 +259,62 @@ v21_path = hf_hub_download(
|
|
| 259 |
filename="v21/Qwen-Rapid-AIO-NSFW-v21.safetensors",
|
| 260 |
repo_type="model"
|
| 261 |
)
|
| 262 |
-
print(f"file ready at: {v21_path}")
|
| 263 |
|
| 264 |
-
# 2. load the base architecture
|
| 265 |
-
# we
|
|
|
|
| 266 |
print("loading base pipeline architecture...")
|
| 267 |
pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 268 |
"Qwen/Qwen-Image-Edit-2511",
|
| 269 |
-
scheduler=EulerAncestralDiscreteScheduler.from_pretrained(
|
| 270 |
-
"Qwen/Qwen-Image-Edit-2511",
|
| 271 |
-
subfolder="scheduler"
|
| 272 |
-
),
|
| 273 |
torch_dtype=torch.bfloat16
|
| 274 |
).to("cuda")
|
| 275 |
|
| 276 |
-
# 3.
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
state_dict = load_file(v21_path)
|
| 279 |
|
| 280 |
-
#
|
| 281 |
-
#
|
| 282 |
-
#
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
| 296 |
else:
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
|
|
|
| 312 |
del state_dict
|
|
|
|
|
|
|
| 313 |
gc.collect()
|
| 314 |
torch.cuda.empty_cache()
|
| 315 |
|
|
|
|
| 259 |
filename="v21/Qwen-Rapid-AIO-NSFW-v21.safetensors",
|
| 260 |
repo_type="model"
|
| 261 |
)
|
|
|
|
| 262 |
|
| 263 |
+
# 2. load the base architecture
|
| 264 |
+
# we use the default flowmatch scheduler first to ensure the pipe inits correctly,
|
| 265 |
+
# then we swap it to euler_a later
|
| 266 |
print("loading base pipeline architecture...")
|
| 267 |
pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 268 |
"Qwen/Qwen-Image-Edit-2511",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
torch_dtype=torch.bfloat16
|
| 270 |
).to("cuda")
|
| 271 |
|
| 272 |
+
# 3. switch scheduler to Euler Ancestral (Lightning requirement)
|
| 273 |
+
# we configure it with the base config to keep timestep spacing correct
|
| 274 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 275 |
+
|
| 276 |
+
# 4. load the massive 28GB v21 weights
|
| 277 |
+
print(f"loading v21 weights from {v21_path}...")
|
| 278 |
state_dict = load_file(v21_path)
|
| 279 |
|
| 280 |
+
# 5. The "Brutal" Injection
|
| 281 |
+
# Because this is an AIO file, keys might be prefixed with "model." or "transformer."
|
| 282 |
+
# or they might match the pipeline exactly. We try the root load first.
|
| 283 |
+
print("injecting AIO weights...")
|
| 284 |
+
|
| 285 |
+
# clean up keys if necessary (common in comfyui > diffusers conversions)
|
| 286 |
+
# this removes 'model.diffusion_model.' prefixes if they exist to match diffusers 'transformer.'
|
| 287 |
+
new_state_dict = {}
|
| 288 |
+
for k, v in state_dict.items():
|
| 289 |
+
if k.startswith("model.diffusion_model."):
|
| 290 |
+
new_key = k.replace("model.diffusion_model.", "transformer.")
|
| 291 |
+
new_state_dict[new_key] = v
|
| 292 |
+
elif k.startswith("first_stage_model."):
|
| 293 |
+
new_key = k.replace("first_stage_model.", "vae.")
|
| 294 |
+
new_state_dict[new_key] = v
|
| 295 |
+
elif k.startswith("conditioner.embedders.0."):
|
| 296 |
+
new_key = k.replace("conditioner.embedders.0.", "text_encoder.")
|
| 297 |
+
new_state_dict[new_key] = v
|
| 298 |
else:
|
| 299 |
+
new_state_dict[k] = v
|
| 300 |
+
|
| 301 |
+
# if no keys were renamed, just use the original
|
| 302 |
+
if len(new_state_dict) == len(state_dict):
|
| 303 |
+
final_dict = state_dict
|
| 304 |
+
else:
|
| 305 |
+
print("detected comfyui keys, remapped for diffusers.")
|
| 306 |
+
final_dict = new_state_dict
|
| 307 |
+
|
| 308 |
+
# attempt load
|
| 309 |
+
mismatched = pipe.load_state_dict(final_dict, strict=False)
|
| 310 |
+
print("weights loaded.")
|
| 311 |
+
print(f"missing keys (ignore if just config/aux): {len(mismatched.missing_keys)}")
|
| 312 |
+
print(f"unexpected keys (ignore if comfy artifacts): {len(mismatched.unexpected_keys)}")
|
| 313 |
+
|
| 314 |
+
# 6. cleanup
|
| 315 |
del state_dict
|
| 316 |
+
del new_state_dict
|
| 317 |
+
del final_dict
|
| 318 |
gc.collect()
|
| 319 |
torch.cuda.empty_cache()
|
| 320 |
|