Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import math
|
|
|
|
| 2 |
import torch
|
| 3 |
import torch.nn as nn
|
| 4 |
import torch.nn.functional as F
|
|
@@ -258,7 +259,7 @@ model, tokenizer, load_status = load_model()
|
|
| 258 |
|
| 259 |
@torch.no_grad()
|
| 260 |
def generate_reply_stream(ids: list[int], max_new_tokens=150, temperature=0.8, top_k=50):
|
| 261 |
-
"""Autoregressive generation with ROSA updates, yielding generated token lists step-by-step."""
|
| 262 |
device = next(model.parameters()).device
|
| 263 |
eos_id = tokenizer.eos_token_id
|
| 264 |
|
|
@@ -270,6 +271,8 @@ def generate_reply_stream(ids: list[int], max_new_tokens=150, temperature=0.8, t
|
|
| 270 |
logits = out.logits[0, -1]
|
| 271 |
generated = list(ids)
|
| 272 |
reply_tokens = []
|
|
|
|
|
|
|
| 273 |
|
| 274 |
for _ in range(max_new_tokens):
|
| 275 |
scaled = logits / max(temperature, 1e-5)
|
|
@@ -283,7 +286,9 @@ def generate_reply_stream(ids: list[int], max_new_tokens=150, temperature=0.8, t
|
|
| 283 |
break
|
| 284 |
|
| 285 |
reply_tokens.append(next_token)
|
| 286 |
-
|
|
|
|
|
|
|
| 287 |
|
| 288 |
# ROSA for the extended sequence, use the last prediction
|
| 289 |
next_rosa = rosa(generated)[-1]
|
|
@@ -296,7 +301,7 @@ def generate_reply_stream(ids: list[int], max_new_tokens=150, temperature=0.8, t
|
|
| 296 |
|
| 297 |
|
| 298 |
def chat_function(message, history):
|
| 299 |
-
"""Gradio ChatInterface streaming callback."""
|
| 300 |
messages = []
|
| 301 |
for turn in history:
|
| 302 |
if isinstance(turn, (list, tuple)):
|
|
@@ -330,10 +335,10 @@ def chat_function(message, history):
|
|
| 330 |
# Cue the assistant to start replying
|
| 331 |
ids.append(asst_id)
|
| 332 |
|
| 333 |
-
# Stream generated tokens in real time
|
| 334 |
-
for reply_tokens in generate_reply_stream(ids, max_new_tokens=150, temperature=0.8, top_k=50):
|
| 335 |
reply = tokenizer.decode(reply_tokens, skip_special_tokens=True).strip()
|
| 336 |
-
yield reply
|
| 337 |
|
| 338 |
|
| 339 |
# -------------------------------------------------------------------------
|
|
|
|
| 1 |
import math
|
| 2 |
+
import time
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
| 5 |
import torch.nn.functional as F
|
|
|
|
| 259 |
|
| 260 |
@torch.no_grad()
|
| 261 |
def generate_reply_stream(ids: list[int], max_new_tokens=150, temperature=0.8, top_k=50):
|
| 262 |
+
"""Autoregressive generation with ROSA updates, yielding generated token lists and speed (tok/s) step-by-step."""
|
| 263 |
device = next(model.parameters()).device
|
| 264 |
eos_id = tokenizer.eos_token_id
|
| 265 |
|
|
|
|
| 271 |
logits = out.logits[0, -1]
|
| 272 |
generated = list(ids)
|
| 273 |
reply_tokens = []
|
| 274 |
+
|
| 275 |
+
start_time = time.perf_counter()
|
| 276 |
|
| 277 |
for _ in range(max_new_tokens):
|
| 278 |
scaled = logits / max(temperature, 1e-5)
|
|
|
|
| 286 |
break
|
| 287 |
|
| 288 |
reply_tokens.append(next_token)
|
| 289 |
+
elapsed = time.perf_counter() - start_time
|
| 290 |
+
tps = len(reply_tokens) / elapsed if elapsed > 0 else 0.0
|
| 291 |
+
yield reply_tokens, tps
|
| 292 |
|
| 293 |
# ROSA for the extended sequence, use the last prediction
|
| 294 |
next_rosa = rosa(generated)[-1]
|
|
|
|
| 301 |
|
| 302 |
|
| 303 |
def chat_function(message, history):
|
| 304 |
+
"""Gradio ChatInterface streaming callback with tok/s throughput display."""
|
| 305 |
messages = []
|
| 306 |
for turn in history:
|
| 307 |
if isinstance(turn, (list, tuple)):
|
|
|
|
| 335 |
# Cue the assistant to start replying
|
| 336 |
ids.append(asst_id)
|
| 337 |
|
| 338 |
+
# Stream generated tokens and tok/s throughput in real time
|
| 339 |
+
for reply_tokens, tps in generate_reply_stream(ids, max_new_tokens=150, temperature=0.8, top_k=50):
|
| 340 |
reply = tokenizer.decode(reply_tokens, skip_special_tokens=True).strip()
|
| 341 |
+
yield f"{reply}\n\n⚡ *{tps:.1f} tok/s*"
|
| 342 |
|
| 343 |
|
| 344 |
# -------------------------------------------------------------------------
|