zkmine commited on
Commit
2096cf1
·
verified ·
1 Parent(s): 1de1b6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -5
app.py CHANGED
@@ -69,22 +69,39 @@ def _device() -> str:
69
 
70
  print("Loading model (this can take a minute on first launch)...")
71
  DEVICE = _device()
72
- DTYPE = torch.float16 if DEVICE in ("cuda", "mps") else torch.float32
 
73
 
74
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
75
 
76
  tokenizer.pad_token = tokenizer.eos_token
77
 
78
  _base = AutoModelForCausalLM.from_pretrained(
79
- MODEL_ID, torch_dtype=DTYPE, trust_remote_code=True
 
 
 
80
  )
81
- # Keep the adapter separate (not merged) so we can toggle it on/off.
 
 
 
 
82
  model = PeftModel.from_pretrained(_base, ADAPTER_DIR)
83
- model = model.to(DEVICE)
 
84
  model.eval()
 
 
 
 
 
 
 
 
85
  print(f"Model loaded on {DEVICE}.")
86
 
87
- @spaces.GPU(duration=120)
88
  def _generate(text: str, max_new_tokens: int = 400) -> str:
89
  """Run one generation with the current adapter state."""
90
  messages = [
 
69
 
70
  print("Loading model (this can take a minute on first launch)...")
71
  DEVICE = _device()
72
+ DTYPE = torch.float32 # ZeroCPU 只有 CPU,强制 fp32
73
+ # DTYPE = torch.float16 if DEVICE in ("cuda", "mps") else torch.float32
74
 
75
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
76
 
77
  tokenizer.pad_token = tokenizer.eos_token
78
 
79
  _base = AutoModelForCausalLM.from_pretrained(
80
+ MODEL_ID,
81
+ torch_dtype=torch.float32,
82
+ device_map="cpu",
83
+ trust_remote_code=True,
84
  )
85
+
86
+ # adapter 加载前临时 patch 掉 spaces 的 torch 拦截
87
+ import safetensors.torch as _st
88
+ _orig = _st.load_file
89
+ _st.load_file = lambda f, **kw: _orig(f, device="cpu")
90
  model = PeftModel.from_pretrained(_base, ADAPTER_DIR)
91
+ _st.load_file = _orig # 还原
92
+
93
  model.eval()
94
+
95
+ # _base = AutoModelForCausalLM.from_pretrained(
96
+ # MODEL_ID, torch_dtype=DTYPE, trust_remote_code=True
97
+ # )
98
+ # # Keep the adapter separate (not merged) so we can toggle it on/off.
99
+ # model = PeftModel.from_pretrained(_base, ADAPTER_DIR)
100
+ # model = model.to(DEVICE)
101
+ # model.eval()
102
  print(f"Model loaded on {DEVICE}.")
103
 
104
+ # @spaces.GPU(duration=120)
105
  def _generate(text: str, max_new_tokens: int = 400) -> str:
106
  """Run one generation with the current adapter state."""
107
  messages = [