Skip to content

vllm.utils.counter

AtomicCounter

An atomic, thread-safe counter

Source code in vllm/utils/counter.py
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

_lock instance-attribute

_lock = Lock()

_value instance-attribute

_value = initial

value property

value: int

__init__

__init__(initial: int = 0) -> None

Initialize a new atomic counter to given initial value

Source code in vllm/utils/counter.py
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()

dec

dec(num: int = 1) -> int

Atomically decrement the counter by num and return the new value

Source code in vllm/utils/counter.py
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

inc

inc(num: int = 1) -> int

Atomically increment the counter by num and return the new value

Source code in vllm/utils/counter.py
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

Counter

Source code in vllm/utils/counter.py
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

counter instance-attribute

counter = start

__init__

__init__(start: int = 0) -> None
Source code in vllm/utils/counter.py
def __init__(self, start: int = 0) -> None:
    super().__init__()

    self.counter = start

__next__

__next__() -> int
Source code in vllm/utils/counter.py
def __next__(self) -> int:
    i = self.counter
    self.counter += 1
    return i

reset

reset() -> None
Source code in vllm/utils/counter.py
def reset(self) -> None:
    self.counter = 0