multimodalart HF Staff commited on
Commit
c0be6dd
·
verified ·
1 Parent(s): fbf2e8d

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -68,18 +68,15 @@ def load_pipeline() -> PixArtAlphaPipeline:
68
  pipe.vae.to(dtype=torch.float32)
69
 
70
  # Load LoRA adapters for transformer and text encoder.
71
- # PeftModel.from_pretrained calls safe_load_file(device="cuda") because
72
- # spaces patches torch.cuda.is_available() to True at module scope.
73
- # Patch load_peft_weights to force CPU during loading, then .to("cuda").
 
 
74
  from peft import PeftModel
75
- from peft.utils.save_and_load import load_peft_weights as _orig_load_peft
76
- from peft.utils import save_and_load as _peft_sl
77
-
78
- def _cpu_load_peft_weights(peft_model_local_path, device="cpu", **kw):
79
- return _orig_load_peft(peft_model_local_path, device="cpu", **kw)
80
-
81
- _peft_sl.load_peft_weights = _cpu_load_peft_weights
82
 
 
 
83
  try:
84
  pipe.transformer = PeftModel.from_pretrained(
85
  pipe.transformer,
@@ -94,7 +91,7 @@ def load_pipeline() -> PixArtAlphaPipeline:
94
  torch_dtype=DTYPE,
95
  )
96
  finally:
97
- _peft_sl.load_peft_weights = _orig_load_peft
98
 
99
  pipe.to("cuda")
100
  return pipe
 
68
  pipe.vae.to(dtype=torch.float32)
69
 
70
  # Load LoRA adapters for transformer and text encoder.
71
+ # ZeroGPU patches torch.cuda.is_available() to True at module scope, so
72
+ # peft's infer_device() returns "cuda" and safe_load_file tries to use
73
+ # CUDA — which fails because there's no GPU at startup. Temporarily
74
+ # make CUDA "unavailable" so peft loads weights on CPU; the subsequent
75
+ # pipe.to("cuda") is intercepted by ZeroGPU's hijack as expected.
76
  from peft import PeftModel
 
 
 
 
 
 
 
77
 
78
+ _orig_is_available = torch.cuda.is_available
79
+ torch.cuda.is_available = lambda: False
80
  try:
81
  pipe.transformer = PeftModel.from_pretrained(
82
  pipe.transformer,
 
91
  torch_dtype=DTYPE,
92
  )
93
  finally:
94
+ torch.cuda.is_available = _orig_is_available
95
 
96
  pipe.to("cuda")
97
  return pipe