multimodalart HF Staff commited on
Commit
c0d3092
·
verified ·
1 Parent(s): f855beb

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -35,9 +35,15 @@ def load_model_zerogpu():
35
  """Load the MuScriptor model in a ZeroGPU-compatible way.
36
 
37
  On ZeroGPU, safetensors.load_file(device="cuda") fails because there's no
38
- real GPU at module scope. We load weights on CPU, build the model on CPU,
39
- then call .to("cuda") which ZeroGPU intercepts to pack weights to disk
40
- and stream into VRAM on the first @spaces.GPU call.
 
 
 
 
 
 
41
  """
42
  device = torch.device("cuda") # ZeroGPU intercepts this
43
 
@@ -48,17 +54,20 @@ def load_model_zerogpu():
48
  ))
49
  cfg = _resolve_config(source, weights_path)
50
 
51
- # Build the model on CPU first
52
- cpu_device = torch.device("cpu")
53
- model = _build_model(cpu_device, cfg)
54
  model.eval()
55
 
56
- # Load weights on CPU
57
  state_dict = load_file(str(weights_path), device="cpu")
58
  state_dict = _remap_single_codebook_keys(state_dict)
 
 
 
59
  model.load_state_dict(state_dict)
60
 
61
- # Now move to "cuda" — ZeroGPU intercepts this and packs to disk
62
  model.to("cuda")
63
 
64
  tokenizer = MT3Tokenizer(
 
35
  """Load the MuScriptor model in a ZeroGPU-compatible way.
36
 
37
  On ZeroGPU, safetensors.load_file(device="cuda") fails because there's no
38
+ real GPU at module scope. However, .to("cuda") is intercepted by the
39
+ spaces hijack. So we:
40
+ 1. Build the model with device="cuda" (conditioners store this as
41
+ self.device and use it at runtime to route tensors).
42
+ 2. Load safetensors weights on CPU.
43
+ 3. Load_state_dict into the model (weights land on CPU because the
44
+ model's tensors are still fake-CUDA via the hijack).
45
+ 4. Call model.to("cuda") — ZeroGPU intercepts this and packs weights
46
+ to disk, streaming them into VRAM on the first @spaces.GPU call.
47
  """
48
  device = torch.device("cuda") # ZeroGPU intercepts this
49
 
 
54
  ))
55
  cfg = _resolve_config(source, weights_path)
56
 
57
+ # Build the model with device="cuda" — conditioners store this and use it
58
+ # at runtime to route tensors. ZeroGPU intercepts .to("cuda") calls inside.
59
+ model = _build_model(device, cfg)
60
  model.eval()
61
 
62
+ # Load weights on CPU (safetensors can't load to fake CUDA)
63
  state_dict = load_file(str(weights_path), device="cpu")
64
  state_dict = _remap_single_codebook_keys(state_dict)
65
+
66
+ # Load state dict — the model's parameters are fake-CUDA (ZeroGPU),
67
+ # but load_state_dict copies CPU data into them, which is fine.
68
  model.load_state_dict(state_dict)
69
 
70
+ # Move everything to "cuda" — ZeroGPU intercepts this and packs to disk
71
  model.to("cuda")
72
 
73
  tokenizer = MT3Tokenizer(