[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:
@@ -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)
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import threading
|
||||
|
||||
|
||||
class Counter:
|
||||
def __init__(self, start: int = 0) -> None:
|
||||
super().__init__()
|
||||
|
||||
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: int = 0) -> None:
|
||||
"""Initialize a new atomic counter to given initial value"""
|
||||
super().__init__()
|
||||
|
||||
self._value = initial
|
||||
self._lock = threading.Lock()
|
||||
|
||||
@property
|
||||
def value(self) -> int:
|
||||
return self._value
|
||||
|
||||
def inc(self, num: int = 1) -> int:
|
||||
"""Atomically increment the counter by num and return the new value"""
|
||||
with self._lock:
|
||||
self._value += num
|
||||
return self._value
|
||||
|
||||
def dec(self, num: int = 1) -> int:
|
||||
"""Atomically decrement the counter by num and return the new value"""
|
||||
with self._lock:
|
||||
self._value -= num
|
||||
return self._value
|
||||
Reference in New Issue
Block a user