Update app.py
Browse files
app.py
CHANGED
|
@@ -339,17 +339,31 @@ def apply_loras_to_pipeline(pose_strength: float, general_strength: float, motio
|
|
| 339 |
orig_tmp_target = getattr(tmp_ledger, "_target_device", None)
|
| 340 |
orig_tmp_device = getattr(tmp_ledger, "device", None)
|
| 341 |
try:
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
| 343 |
tmp_ledger.device = torch.device("cpu")
|
| 344 |
print("[LoRA] Building fused transformer on CPU (no GPU allocation)...")
|
| 345 |
-
new_transformer_cpu = tmp_ledger.transformer() #
|
| 346 |
print("[LoRA] Fused transformer built on CPU.")
|
| 347 |
finally:
|
| 348 |
-
# Restore attributes (
|
| 349 |
if orig_tmp_target is not None:
|
| 350 |
tmp_ledger._target_device = orig_tmp_target
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
if orig_tmp_device is not None:
|
| 352 |
tmp_ledger.device = orig_tmp_device
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
# Get the existing transformer instance (the one currently used by the pipeline).
|
| 355 |
global _transformer
|
|
@@ -366,6 +380,14 @@ def apply_loras_to_pipeline(pose_strength: float, general_strength: float, motio
|
|
| 366 |
|
| 367 |
# State dict of CPU model (fused with LoRAs)
|
| 368 |
new_state = new_transformer_cpu.state_dict()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
# Copy CPU tensors into the GPU-resident transformer's params/buffers in-place
|
| 371 |
with torch.no_grad():
|
|
|
|
| 339 |
orig_tmp_target = getattr(tmp_ledger, "_target_device", None)
|
| 340 |
orig_tmp_device = getattr(tmp_ledger, "device", None)
|
| 341 |
try:
|
| 342 |
+
# _target_device is expected to be callable by model_ledger.transformer()
|
| 343 |
+
# set it to a callable that returns CPU so builder.build(device=...) works.
|
| 344 |
+
tmp_ledger._target_device = (lambda: torch.device("cpu"))
|
| 345 |
+
# ledger.device is used after build: set it to CPU so .to(self.device) keeps the model on CPU.
|
| 346 |
tmp_ledger.device = torch.device("cpu")
|
| 347 |
print("[LoRA] Building fused transformer on CPU (no GPU allocation)...")
|
| 348 |
+
new_transformer_cpu = tmp_ledger.transformer() # should now return a CPU model
|
| 349 |
print("[LoRA] Fused transformer built on CPU.")
|
| 350 |
finally:
|
| 351 |
+
# Restore attributes to their previous values (if there were any).
|
| 352 |
if orig_tmp_target is not None:
|
| 353 |
tmp_ledger._target_device = orig_tmp_target
|
| 354 |
+
else:
|
| 355 |
+
# remove attribute if ledger did not have it previously
|
| 356 |
+
try:
|
| 357 |
+
delattr(tmp_ledger, "_target_device")
|
| 358 |
+
except Exception:
|
| 359 |
+
pass
|
| 360 |
if orig_tmp_device is not None:
|
| 361 |
tmp_ledger.device = orig_tmp_device
|
| 362 |
+
else:
|
| 363 |
+
try:
|
| 364 |
+
delattr(tmp_ledger, "device")
|
| 365 |
+
except Exception:
|
| 366 |
+
pass
|
| 367 |
|
| 368 |
# Get the existing transformer instance (the one currently used by the pipeline).
|
| 369 |
global _transformer
|
|
|
|
| 380 |
|
| 381 |
# State dict of CPU model (fused with LoRAs)
|
| 382 |
new_state = new_transformer_cpu.state_dict()
|
| 383 |
+
# diagnostics: how many keys will be copied
|
| 384 |
+
total_keys = len(new_state)
|
| 385 |
+
matched = sum(1 for k in new_state if k in existing_params or k in existing_buffers)
|
| 386 |
+
print(f"[LoRA] Transformer state keys: total={total_keys} matched_for_copy={matched}")
|
| 387 |
+
if matched == 0:
|
| 388 |
+
# helpful hint if naming differs
|
| 389 |
+
sample_keys = list(new_state.keys())[:10]
|
| 390 |
+
print(f"[LoRA] Warning: 0 matching keys found. sample new_state keys: {sample_keys}")
|
| 391 |
|
| 392 |
# Copy CPU tensors into the GPU-resident transformer's params/buffers in-place
|
| 393 |
with torch.no_grad():
|