ayf3 commited on
Commit
d365d5c
·
verified ·
1 Parent(s): 966d861

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -29,20 +29,28 @@ def load_model():
29
  model = VoxCPM.from_pretrained("openbmb/VoxCPM2", load_denoiser=False, optimize=False)
30
 
31
  # CRITICAL FIX: Force float32 on CPU
32
- # VoxCPM2 uses bfloat16 by default, which causes "Dimension out of range" errors
33
- # in MiniCPM4's scaled_dot_product_attention on CPU
 
 
34
  if device == "cpu":
35
  print("Converting model to float32 for CPU compatibility...")
 
 
 
 
 
 
36
  model.tts_model = model.tts_model.to(torch.float32)
37
- # Also fix KV caches (they are created with config dtype = bfloat16)
38
  if hasattr(model.tts_model, 'base_lm') and hasattr(model.tts_model.base_lm, 'kv_cache'):
39
  if model.tts_model.base_lm.kv_cache is not None:
40
  model.tts_model.base_lm.kv_cache.kv_cache = model.tts_model.base_lm.kv_cache.kv_cache.to(torch.float32)
41
- print(" base_lm KV cache converted to float32")
42
  if hasattr(model.tts_model, 'residual_lm') and hasattr(model.tts_model.residual_lm, 'kv_cache'):
43
  if model.tts_model.residual_lm.kv_cache is not None:
44
  model.tts_model.residual_lm.kv_cache.kv_cache = model.tts_model.residual_lm.kv_cache.kv_cache.to(torch.float32)
45
- print(" residual_lm KV cache converted to float32")
46
  print("Model conversion to float32 complete!")
47
 
48
  print("Model loaded successfully!")
 
29
  model = VoxCPM.from_pretrained("openbmb/VoxCPM2", load_denoiser=False, optimize=False)
30
 
31
  # CRITICAL FIX: Force float32 on CPU
32
+ # VoxCPM2 uses bfloat16 by default, which causes dimension/dtype errors on CPU:
33
+ # 1. "Dimension out of range" in MiniCPM4's scaled_dot_product_attention (bfloat16 SDPA bug)
34
+ # 2. "mat1 and mat2 must have the same dtype" when model is float32 but inputs are bfloat16
35
+ # Fix: change config.dtype BEFORE anything creates tensors, then convert model
36
  if device == "cpu":
37
  print("Converting model to float32 for CPU compatibility...")
38
+ # Step 1: Change config dtype so _inference creates float32 tensors
39
+ if hasattr(model.tts_model, 'config'):
40
+ old_dtype = model.tts_model.config.dtype
41
+ model.tts_model.config.dtype = "float32"
42
+ print(f" config.dtype: {old_dtype} -> float32")
43
+ # Step 2: Convert all model parameters and buffers to float32
44
  model.tts_model = model.tts_model.to(torch.float32)
45
+ # Step 3: Fix KV caches (created in __init__ with old dtype)
46
  if hasattr(model.tts_model, 'base_lm') and hasattr(model.tts_model.base_lm, 'kv_cache'):
47
  if model.tts_model.base_lm.kv_cache is not None:
48
  model.tts_model.base_lm.kv_cache.kv_cache = model.tts_model.base_lm.kv_cache.kv_cache.to(torch.float32)
49
+ print(" base_lm KV cache -> float32")
50
  if hasattr(model.tts_model, 'residual_lm') and hasattr(model.tts_model.residual_lm, 'kv_cache'):
51
  if model.tts_model.residual_lm.kv_cache is not None:
52
  model.tts_model.residual_lm.kv_cache.kv_cache = model.tts_model.residual_lm.kv_cache.kv_cache.to(torch.float32)
53
+ print(" residual_lm KV cache -> float32")
54
  print("Model conversion to float32 complete!")
55
 
56
  print("Model loaded successfully!")