Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -526,20 +526,27 @@ def load_models() -> str | None:
|
|
| 526 |
custom_transformer.rope.inv_freq = 1.0 / (
|
| 527 |
rope_theta ** (torch.arange(0, 2 * rope_freq_dim, 2, dtype=torch.float32) / (2 * rope_freq_dim))
|
| 528 |
)
|
|
|
|
|
|
|
| 529 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 530 |
# `_keep_in_fp32_modules` is normally enforced by `from_pretrained`'s own post-load dtype pass — a
|
| 531 |
# step this manual meta-device + `load_state_dict` path never goes through. The official checkpoint
|
| 532 |
# genuinely ships these modules as float32 while everything else is bfloat16; if the finetune's file
|
| 533 |
-
# is uniformly bf16 (common for a community single-file export),
|
| 534 |
-
#
|
| 535 |
-
for name, tensor in list(
|
| 536 |
if any(keep in name for keep in MiniMaxH3Transformer3DModel._keep_in_fp32_modules):
|
| 537 |
tensor.data = tensor.data.to(torch.float32)
|
| 538 |
|
| 539 |
-
pipe.update_components(transformer=custom_transformer)
|
| 540 |
-
print(f"[gen] transformer replaced with {CUSTOM_TRANSFORMER_REPO}/{CUSTOM_TRANSFORMER_FILE}", flush=True)
|
| 541 |
-
|
| 542 |
-
pipe.load_components(dtype=torch.bfloat16)
|
| 543 |
pipe.transformer.set_attention_backend(ATTENTION)
|
| 544 |
|
| 545 |
# --- Diagnostic: dump the LoRA files' key names/shapes and the transformer's own shapes to the Space
|
|
|
|
| 526 |
custom_transformer.rope.inv_freq = 1.0 / (
|
| 527 |
rope_theta ** (torch.arange(0, 2 * rope_freq_dim, 2, dtype=torch.float32) / (2 * rope_freq_dim))
|
| 528 |
)
|
| 529 |
+
pipe.update_components(transformer=custom_transformer)
|
| 530 |
+
print(f"[gen] transformer replaced with {CUSTOM_TRANSFORMER_REPO}/{CUSTOM_TRANSFORMER_FILE}", flush=True)
|
| 531 |
|
| 532 |
+
pipe.load_components(dtype=torch.bfloat16)
|
| 533 |
+
|
| 534 |
+
if CUSTOM_TRANSFORMER_REPO:
|
| 535 |
+
# Moved to *after* `load_components(dtype=torch.bfloat16)` above, not before it: that call runs
|
| 536 |
+
# unconditionally and its `dtype=` argument applies to every component regardless of whether it was
|
| 537 |
+
# freshly fetched or already installed via `update_components` — doing this upcast beforehand had it
|
| 538 |
+
# silently re-cast straight back to bf16 one line later, which is why the first attempt at this fix
|
| 539 |
+
# had no visible effect at all despite being otherwise correct.
|
| 540 |
+
#
|
| 541 |
# `_keep_in_fp32_modules` is normally enforced by `from_pretrained`'s own post-load dtype pass — a
|
| 542 |
# step this manual meta-device + `load_state_dict` path never goes through. The official checkpoint
|
| 543 |
# genuinely ships these modules as float32 while everything else is bfloat16; if the finetune's file
|
| 544 |
+
# is uniformly bf16 (common for a community single-file export), it adopted that dtype for these
|
| 545 |
+
# layers too, silently dropping precision the model actually needs to run correctly.
|
| 546 |
+
for name, tensor in list(pipe.transformer.named_parameters()) + list(pipe.transformer.named_buffers()):
|
| 547 |
if any(keep in name for keep in MiniMaxH3Transformer3DModel._keep_in_fp32_modules):
|
| 548 |
tensor.data = tensor.data.to(torch.float32)
|
| 549 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
pipe.transformer.set_attention_backend(ATTENTION)
|
| 551 |
|
| 552 |
# --- Diagnostic: dump the LoRA files' key names/shapes and the transformer's own shapes to the Space
|