mithulaartigala commited on
Commit
7fd52ed
·
verified ·
1 Parent(s): 569eb28

Budget first ZeroGPU generation for cold kernel setup

Browse files
Files changed (1) hide show
  1. app.py +11 -0
app.py CHANGED
@@ -7,6 +7,7 @@ import os
7
  from pathlib import Path
8
  from queue import Empty
9
  import sys
 
10
  from threading import Event, Lock, Thread
11
  from time import perf_counter
12
 
@@ -29,6 +30,12 @@ MAX_INPUT_TOKENS = 8 * 1024
29
  MAX_OUTPUT_TOKENS = CONTEXT_LIMIT - MAX_INPUT_TOKENS
30
  OPEN_THINK = "<think>"
31
  CLOSE_THINK = "</think>"
 
 
 
 
 
 
32
 
33
  os.environ.setdefault("TRANSFORMERS_DISABLE_DEEPGEMM_LINEAR", "1")
34
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -300,6 +307,8 @@ def prepare_request(payload: str):
300
 
301
  def reserve_gpu(prepared):
302
  prepared["scheduling_at"] = perf_counter()
 
 
303
  return prepared["duration"]
304
 
305
 
@@ -327,6 +336,8 @@ def resample(prepared):
327
  for message in updates(
328
  prepared["inputs"].to(model.device), prepared["reasoning"], prepared["remake"], prepared, deadline,
329
  ):
 
 
330
  yield encode(message)
331
 
332
 
 
7
  from pathlib import Path
8
  from queue import Empty
9
  import sys
10
+ from tempfile import TemporaryDirectory
11
  from threading import Event, Lock, Thread
12
  from time import perf_counter
13
 
 
30
  MAX_OUTPUT_TOKENS = CONTEXT_LIMIT - MAX_INPUT_TOKENS
31
  OPEN_THINK = "<think>"
32
  CLOSE_THINK = "</think>"
33
+ COLD_GPU_DURATION_SECONDS = 90
34
+
35
+ # ZeroGPU workers share the Space's filesystem. Keep this marker scoped to the
36
+ # current app process so each deployment budgets for first-use kernel setup.
37
+ runtime_dir = TemporaryDirectory(prefix="resampler-")
38
+ gpu_ready = Path(runtime_dir.name) / "gpu-ready"
39
 
40
  os.environ.setdefault("TRANSFORMERS_DISABLE_DEEPGEMM_LINEAR", "1")
41
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
307
 
308
  def reserve_gpu(prepared):
309
  prepared["scheduling_at"] = perf_counter()
310
+ if not gpu_ready.exists():
311
+ prepared["duration"] = COLD_GPU_DURATION_SECONDS
312
  return prepared["duration"]
313
 
314
 
 
336
  for message in updates(
337
  prepared["inputs"].to(model.device), prepared["reasoning"], prepared["remake"], prepared, deadline,
338
  ):
339
+ if message["kind"] == "done" and not gpu_ready.exists():
340
+ gpu_ready.touch()
341
  yield encode(message)
342
 
343