[Misc] Separate out utils.counter and move utils.Device to engine (#27588)

Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
Cyrus Leung
2025-10-28 12:20:46 +00:00
committed by GitHub
parent 44b5ce956d
commit f58d9b6404
8 changed files with 59 additions and 54 deletions
-44
View File
@@ -3,7 +3,6 @@
import enum
import inspect
import threading
import uuid
import warnings
from functools import wraps
@@ -68,54 +67,11 @@ STR_INVALID_VAL: str = "INVALID"
T = TypeVar("T")
class Device(enum.Enum):
GPU = enum.auto()
CPU = enum.auto()
class LayerBlockType(enum.Enum):
attention = "attention"
mamba = "mamba"
class Counter:
def __init__(self, start: int = 0) -> None:
self.counter = start
def __next__(self) -> int:
i = self.counter
self.counter += 1
return i
def reset(self) -> None:
self.counter = 0
class AtomicCounter:
"""An atomic, thread-safe counter"""
def __init__(self, initial=0):
"""Initialize a new atomic counter to given initial value"""
self._value = initial
self._lock = threading.Lock()
def inc(self, num=1):
"""Atomically increment the counter by num and return the new value"""
with self._lock:
self._value += num
return self._value
def dec(self, num=1):
"""Atomically decrement the counter by num and return the new value"""
with self._lock:
self._value -= num
return self._value
@property
def value(self):
return self._value
def random_uuid() -> str:
return str(uuid.uuid4().hex)