chris0809 commited on
Commit
ab08654
·
verified ·
1 Parent(s): b3b1dc5

Configure ZeroGPU runtime

Browse files
Files changed (2) hide show
  1. README.md +5 -5
  2. app.py +13 -3
README.md CHANGED
@@ -17,9 +17,9 @@ models:
17
 
18
  # tinyLLM 0.51B Demo
19
 
20
- This CPU Space loads the public tinyLLM SFT model and switches the ARC GRPO or
21
- IFEval OPD LoRA without keeping multiple base models in memory.
22
 
23
- The first response is slower because the 0.51B checkpoint is downloaded and
24
- loaded on demand. The full multimodal demo remains available in the GitHub
25
- repository; the free CPU Space focuses on text and adapter switching.
 
17
 
18
  # tinyLLM 0.51B Demo
19
 
20
+ This free ZeroGPU Space loads the public tinyLLM SFT model and switches the ARC
21
+ GRPO or IFEval OPD LoRA without keeping multiple base models in memory.
22
 
23
+ The first start is slower while the 0.51B checkpoint is downloaded. The full
24
+ multimodal demo remains available in the GitHub repository; this small hosted
25
+ Space focuses on text and adapter switching.
app.py CHANGED
@@ -4,6 +4,7 @@ from __future__ import annotations
4
  import threading
5
 
6
  import gradio as gr
 
7
  import torch
8
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
9
 
@@ -29,8 +30,8 @@ def load_runtime():
29
  _model = AutoModelForCausalLM.from_pretrained(
30
  BASE_REPO,
31
  trust_remote_code=True,
32
- dtype=torch.float32,
33
- ).eval()
34
  return _model, _tokenizer
35
 
36
 
@@ -47,6 +48,7 @@ def select_adapter(model, mode: str) -> None:
47
  model.activate_single_lora(name)
48
 
49
 
 
50
  def respond(message: str, history: list[dict], mode: str, max_new_tokens: int):
51
  if not message.strip():
52
  yield "请输入一个问题。"
@@ -65,7 +67,10 @@ def respond(message: str, history: list[dict], mode: str, max_new_tokens: int):
65
  tokenize=False,
66
  add_generation_prompt=True,
67
  )
68
- inputs = tokenizer(prompt, return_tensors="pt")
 
 
 
69
  streamer = TextIteratorStreamer(
70
  tokenizer,
71
  skip_prompt=True,
@@ -89,6 +94,11 @@ def respond(message: str, history: list[dict], mode: str, max_new_tokens: int):
89
  worker.join()
90
 
91
 
 
 
 
 
 
92
  with gr.Blocks(title="tinyLLM 0.51B") as demo:
93
  gr.Markdown(
94
  "# tinyLLM 0.51B\n"
 
4
  import threading
5
 
6
  import gradio as gr
7
+ import spaces
8
  import torch
9
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
10
 
 
30
  _model = AutoModelForCausalLM.from_pretrained(
31
  BASE_REPO,
32
  trust_remote_code=True,
33
+ dtype=torch.bfloat16,
34
+ ).to("cuda").eval()
35
  return _model, _tokenizer
36
 
37
 
 
48
  model.activate_single_lora(name)
49
 
50
 
51
+ @spaces.GPU(duration=120)
52
  def respond(message: str, history: list[dict], mode: str, max_new_tokens: int):
53
  if not message.strip():
54
  yield "请输入一个问题。"
 
67
  tokenize=False,
68
  add_generation_prompt=True,
69
  )
70
+ inputs = {
71
+ key: value.to(next(model.parameters()).device)
72
+ for key, value in tokenizer(prompt, return_tensors="pt").items()
73
+ }
74
  streamer = TextIteratorStreamer(
75
  tokenizer,
76
  skip_prompt=True,
 
94
  worker.join()
95
 
96
 
97
+ # ZeroGPU provides a CUDA emulation context during startup, so the weights can
98
+ # stay GPU-resident and are ready when the decorated function receives a slot.
99
+ load_runtime()
100
+
101
+
102
  with gr.Blocks(title="tinyLLM 0.51B") as demo:
103
  gr.Markdown(
104
  "# tinyLLM 0.51B\n"