AQIT 0.1.0
Loading...
Searching...
No Matches
tab_queue.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2# This file is part of the Aquin Engine. Unauthorized copying, modification,
3# distribution, or use of this file, via any medium, is strictly prohibited.
4# Proprietary and confidential. See LICENSE for terms.
5
6"""
7Ingested from inspection-backend/tab_queue.py.
8FastAPI HTTPException replaced with RuntimeError — CLI has no HTTP layer.
9"""
10from __future__ import annotations
11
12import asyncio
13import os
14
15
16class TabQueue:
17 def __init__(self):
18 self._inference_slots = self._calculate_inference_slots()
19 self.inference = asyncio.Semaphore(self._inference_slots)
20 self.training = asyncio.Semaphore(int(os.getenv("TRAINING_SLOTS", "1")))
23 self._training_active = False
26 override = os.getenv("INFERENCE_SLOTS")
27 if override:
28 return int(override)
29 return 1
30
31 async def inference_request(self, fn, timeout: float = 30.0):
32 if self._training_active:
33 raise RuntimeError("Model is busy with a training run — try again when training completes")
34 self._inference_waiters += 1
35 try:
36 await asyncio.wait_for(self.inference.acquire(), timeout=timeout)
37 except asyncio.TimeoutError:
38 raise RuntimeError("GPU busy, try again shortly")
39 finally:
40 self._inference_waiters -= 1
41 try:
42 return await fn()
43 finally:
44 self.inference.release()
45
46 async def training_request(self, fn):
47 self._training_waiters += 1
48 await self.training.acquire()
49 self._training_waiters -= 1
51 await self.inference.acquire()
52 self._training_active = True
53 try:
54 return await fn()
55 finally:
56 self._training_active = False
57 self.inference.release()
58 self.training.release()
59
60 def status(self) -> dict:
61 inference_active = self._inference_slots - self.inference._value
62 return {
63 "inference_slots": self._inference_slots,
64 "inference_active": inference_active,
65 "inference_queued": self._inference_waiters,
66 "training_slots": int(os.getenv("TRAINING_SLOTS", "1")),
67 "training_active": int(self._training_active),
68 "training_queued": self._training_waiters,
69 }
70
71
72tab_queue: TabQueue | None = None
73
74
75def init_tab_queue() -> TabQueue:
76 global tab_queue
77 tab_queue = TabQueue()
78 return tab_queue
inference_request(self, fn, float timeout=30.0)
Definition tab_queue.py:35
TabQueue init_tab_queue()
Definition tab_queue.py:79