Spaces:
Sleeping
Sleeping
Commit
·
cdac920
1
Parent(s):
fc0ab14
Fix: Pre-create GPU wrappers at module load time for startup detection
Browse files- Create GPU-decorated functions at module load time (not dynamically)
- This ensures spaces.GPU decorators are detected during startup
- Pre-create wrappers for all durations from 60-1800 seconds (every 60s)
- Fix closure variable capture issue
- Slider value now properly controls GPU duration allocation
app.py
CHANGED
|
@@ -387,16 +387,38 @@ def _generate_router_plan_streaming_internal(
|
|
| 387 |
yield "", {}, error_msg, ""
|
| 388 |
|
| 389 |
|
| 390 |
-
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
@spaces.GPU(duration=duration)
|
| 393 |
-
def wrapper(
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
return wrapper
|
| 396 |
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
|
| 401 |
|
| 402 |
def generate_router_plan_streaming(
|
|
@@ -415,17 +437,14 @@ def generate_router_plan_streaming(
|
|
| 415 |
"""
|
| 416 |
Generate router plan with streaming output.
|
| 417 |
|
| 418 |
-
Uses user-specified gpu_duration to
|
| 419 |
"""
|
| 420 |
-
# Round to nearest 60 seconds
|
| 421 |
rounded_duration = ((gpu_duration + 30) // 60) * 60
|
| 422 |
-
rounded_duration = max(60, min(1800, rounded_duration))
|
| 423 |
-
|
| 424 |
-
# Get or create wrapper with this duration
|
| 425 |
-
if rounded_duration not in _gpu_wrapper_cache:
|
| 426 |
-
_gpu_wrapper_cache[rounded_duration] = _create_gpu_wrapper(rounded_duration)
|
| 427 |
|
| 428 |
-
wrapper
|
|
|
|
| 429 |
yield from wrapper(
|
| 430 |
user_task, context, acceptance, extra_guidance,
|
| 431 |
difficulty, tags, model_choice, max_new_tokens,
|
|
|
|
| 387 |
yield "", {}, error_msg, ""
|
| 388 |
|
| 389 |
|
| 390 |
+
# Pre-create GPU wrappers for common durations at module load time
|
| 391 |
+
# This ensures spaces.GPU decorators are detected during startup
|
| 392 |
+
_GPU_WRAPPERS: Dict[int, Any] = {}
|
| 393 |
+
|
| 394 |
+
# Create wrappers for durations: 60, 120, 180, 240, 300, 360, 420, 480, 540, 600,
|
| 395 |
+
# 720, 840, 960, 1080, 1200, 1320, 1440, 1560, 1680, 1800 (every 60s from 60 to 1800)
|
| 396 |
+
def _make_gpu_wrapper(duration: int):
|
| 397 |
+
"""Factory function to create GPU-decorated wrapper with closure over duration."""
|
| 398 |
@spaces.GPU(duration=duration)
|
| 399 |
+
def wrapper(
|
| 400 |
+
user_task: str,
|
| 401 |
+
context: str,
|
| 402 |
+
acceptance: str,
|
| 403 |
+
extra_guidance: str,
|
| 404 |
+
difficulty: str,
|
| 405 |
+
tags: str,
|
| 406 |
+
model_choice: str,
|
| 407 |
+
max_new_tokens: int,
|
| 408 |
+
temperature: float,
|
| 409 |
+
top_p: float,
|
| 410 |
+
gpu_duration: int,
|
| 411 |
+
):
|
| 412 |
+
yield from _generate_router_plan_streaming_internal(
|
| 413 |
+
user_task, context, acceptance, extra_guidance,
|
| 414 |
+
difficulty, tags, model_choice, max_new_tokens,
|
| 415 |
+
temperature, top_p, duration
|
| 416 |
+
)
|
| 417 |
return wrapper
|
| 418 |
|
| 419 |
+
# Pre-create all wrappers at module load time
|
| 420 |
+
for duration in range(60, 1801, 60):
|
| 421 |
+
_GPU_WRAPPERS[duration] = _make_gpu_wrapper(duration)
|
| 422 |
|
| 423 |
|
| 424 |
def generate_router_plan_streaming(
|
|
|
|
| 437 |
"""
|
| 438 |
Generate router plan with streaming output.
|
| 439 |
|
| 440 |
+
Uses user-specified gpu_duration to select the appropriate GPU wrapper.
|
| 441 |
"""
|
| 442 |
+
# Round to nearest 60 seconds and clamp between 60 and 1800
|
| 443 |
rounded_duration = ((gpu_duration + 30) // 60) * 60
|
| 444 |
+
rounded_duration = max(60, min(1800, rounded_duration))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
|
| 446 |
+
# Get the pre-created wrapper with this duration
|
| 447 |
+
wrapper = _GPU_WRAPPERS[rounded_duration]
|
| 448 |
yield from wrapper(
|
| 449 |
user_task, context, acceptance, extra_guidance,
|
| 450 |
difficulty, tags, model_choice, max_new_tokens,
|