| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import functools |
| 4 | import hashlib |
| 5 | import inspect |
| 6 | import types |
| 7 | import weakref |
| 8 | from pathlib import Path |
| 9 | from typing import Any |
| 10 | |
| 11 | |
| 12 | def hash_source(*srcs: str | Any) -> str: |
| 13 | """Utility method to hash the sources of functions or objects. |
| 14 | |
| 15 | Args: |
| 16 | srcs: strings or objects to add to the hash. |
| 17 | Objects and functions have their source inspected. |
| 18 | |
| 19 | """ |
| 20 | hasher = hashlib.sha256() |
| 21 | for src in srcs: |
| 22 | if src is None: |
| 23 | src_str = "None" |
| 24 | elif isinstance(src, str): |
| 25 | src_str = src |
| 26 | elif isinstance(src, Path): |
| 27 | src_str = src.read_text() |
| 28 | elif isinstance(src, (types.FunctionType, type)): |
| 29 | src_str = inspect.getsource(src) |
| 30 | else: |
| 31 | # object instance |
| 32 | src_str = inspect.getsource(src.__class__) |
| 33 | hasher.update(src_str.encode("utf-8")) |
| 34 | return hasher.hexdigest() |
| 35 | |
| 36 | |
| 37 | def weak_lru_cache(maxsize: int | None = 128, typed: bool = False): |
| 38 | """LRU Cache decorator that keeps a weak reference to 'self'. |
| 39 | This avoids memory leakage, which happens when functools.lru_cache |
| 40 | stores a reference to self in the global cache. |
| 41 | |
| 42 | Taken from: https://stackoverflow.com/a/68052994/5082708 |
| 43 | """ |
| 44 | |
| 45 | def wrapper(func): |
| 46 | @functools.lru_cache(maxsize, typed) |
| 47 | def _func(_self, *args, **kwargs): |
| 48 | return func(_self(), *args, **kwargs) |
| 49 | |
| 50 | @functools.wraps(func) |
| 51 | def inner(self, *args, **kwargs): |
| 52 | return _func(weakref.ref(self), *args, **kwargs) |
| 53 | |
| 54 | return inner |
| 55 | |
| 56 | return wrapper |
| 57 | |
| 58 | |
| 59 | def weak_cache(user_function, /): |
| 60 | """Simple weak equivalent to functools.cache.""" |
| 61 | return weak_lru_cache(maxsize=None)(user_function) |