techfreakworm commited on
Commit
95305d7
·
unverified ·
1 Parent(s): 88f8bd9

feat(zerogpu): decorate() shim for HF ZeroGPU compatibility

Browse files
Files changed (2) hide show
  1. server/zerogpu.py +25 -0
  2. tests/test_zerogpu.py +33 -0
server/zerogpu.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ZeroGPU decorator shim.
2
+
3
+ When `spaces` is importable (HF ZeroGPU runtime), `decorate` wraps
4
+ functions with `spaces.GPU(duration=...)`. Otherwise it is the
5
+ identity decorator. Local installs and Free CPU Spaces hit the
6
+ no-op branch.
7
+ """
8
+ from __future__ import annotations
9
+
10
+ from typing import Callable, TypeVar
11
+
12
+
13
+ F = TypeVar("F", bound=Callable)
14
+
15
+
16
+ try: # pragma: no cover — covered by a test that injects a fake module
17
+ import spaces # type: ignore[import-not-found]
18
+
19
+ def decorate(fn: F) -> F:
20
+ return spaces.GPU(duration=120)(fn) # type: ignore[no-any-return]
21
+
22
+ except ImportError: # local / Free CPU
23
+
24
+ def decorate(fn: F) -> F:
25
+ return fn
tests/test_zerogpu.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from unittest.mock import MagicMock
3
+
4
+ from server.zerogpu import decorate
5
+
6
+
7
+ def test_decorate_is_passthrough_when_spaces_missing(monkeypatch):
8
+ monkeypatch.setitem(sys.modules, "spaces", None)
9
+
10
+ @decorate
11
+ def fn(x):
12
+ return x * 2
13
+
14
+ assert fn(3) == 6
15
+
16
+
17
+ def test_decorate_uses_spaces_gpu_when_available(monkeypatch):
18
+ fake_spaces = MagicMock()
19
+ fake_decorator = MagicMock(side_effect=lambda f: f)
20
+ fake_spaces.GPU = MagicMock(return_value=fake_decorator)
21
+ monkeypatch.setitem(sys.modules, "spaces", fake_spaces)
22
+
23
+ import importlib
24
+ import server.zerogpu as zg
25
+
26
+ importlib.reload(zg)
27
+
28
+ @zg.decorate
29
+ def fn(x):
30
+ return x + 1
31
+
32
+ assert fn(2) == 3
33
+ fake_spaces.GPU.assert_called_once()