[Refactor] Remove unused utils (#38153)
Signed-off-by: yewentao256 <zhyanwentao@126.com>
This commit is contained in:
@@ -4,64 +4,7 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from vllm.utils.func_utils import deprecate_kwargs, supports_kw
|
from vllm.utils.func_utils import supports_kw
|
||||||
|
|
||||||
from ..utils import error_on_warning
|
|
||||||
|
|
||||||
|
|
||||||
def test_deprecate_kwargs_always():
|
|
||||||
@deprecate_kwargs("old_arg", is_deprecated=True)
|
|
||||||
def dummy(*, old_arg: object = None, new_arg: object = None):
|
|
||||||
pass
|
|
||||||
|
|
||||||
with pytest.warns(DeprecationWarning, match="'old_arg'"):
|
|
||||||
dummy(old_arg=1)
|
|
||||||
|
|
||||||
with error_on_warning(DeprecationWarning):
|
|
||||||
dummy(new_arg=1)
|
|
||||||
|
|
||||||
|
|
||||||
def test_deprecate_kwargs_never():
|
|
||||||
@deprecate_kwargs("old_arg", is_deprecated=False)
|
|
||||||
def dummy(*, old_arg: object = None, new_arg: object = None):
|
|
||||||
pass
|
|
||||||
|
|
||||||
with error_on_warning(DeprecationWarning):
|
|
||||||
dummy(old_arg=1)
|
|
||||||
|
|
||||||
with error_on_warning(DeprecationWarning):
|
|
||||||
dummy(new_arg=1)
|
|
||||||
|
|
||||||
|
|
||||||
def test_deprecate_kwargs_dynamic():
|
|
||||||
is_deprecated = True
|
|
||||||
|
|
||||||
@deprecate_kwargs("old_arg", is_deprecated=lambda: is_deprecated)
|
|
||||||
def dummy(*, old_arg: object = None, new_arg: object = None):
|
|
||||||
pass
|
|
||||||
|
|
||||||
with pytest.warns(DeprecationWarning, match="'old_arg'"):
|
|
||||||
dummy(old_arg=1)
|
|
||||||
|
|
||||||
with error_on_warning(DeprecationWarning):
|
|
||||||
dummy(new_arg=1)
|
|
||||||
|
|
||||||
is_deprecated = False
|
|
||||||
|
|
||||||
with error_on_warning(DeprecationWarning):
|
|
||||||
dummy(old_arg=1)
|
|
||||||
|
|
||||||
with error_on_warning(DeprecationWarning):
|
|
||||||
dummy(new_arg=1)
|
|
||||||
|
|
||||||
|
|
||||||
def test_deprecate_kwargs_additional_message():
|
|
||||||
@deprecate_kwargs("old_arg", is_deprecated=True, additional_message="abcd")
|
|
||||||
def dummy(*, old_arg: object = None, new_arg: object = None):
|
|
||||||
pass
|
|
||||||
|
|
||||||
with pytest.warns(DeprecationWarning, match="abcd"):
|
|
||||||
dummy(old_arg=1)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|||||||
@@ -8,9 +8,8 @@ This is similar in concept to the `functools` module.
|
|||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
import threading
|
import threading
|
||||||
import warnings
|
|
||||||
from collections.abc import Callable, Mapping
|
from collections.abc import Callable, Mapping
|
||||||
from functools import lru_cache, partial, wraps
|
from functools import lru_cache
|
||||||
from typing import Any, TypeVar
|
from typing import Any, TypeVar
|
||||||
|
|
||||||
from typing_extensions import ParamSpec
|
from typing_extensions import ParamSpec
|
||||||
@@ -45,81 +44,6 @@ def run_once(f: Callable[P, None]) -> Callable[P, None]:
|
|||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
def deprecate_args(
|
|
||||||
start_index: int,
|
|
||||||
is_deprecated: bool | Callable[[], bool] = True,
|
|
||||||
additional_message: str | None = None,
|
|
||||||
) -> Callable[[F], F]:
|
|
||||||
if not callable(is_deprecated):
|
|
||||||
is_deprecated = partial(identity, is_deprecated)
|
|
||||||
|
|
||||||
def wrapper(fn: F) -> F:
|
|
||||||
params = inspect.signature(fn).parameters
|
|
||||||
pos_types = (
|
|
||||||
inspect.Parameter.POSITIONAL_ONLY,
|
|
||||||
inspect.Parameter.POSITIONAL_OR_KEYWORD,
|
|
||||||
)
|
|
||||||
pos_kws = [kw for kw, param in params.items() if param.kind in pos_types]
|
|
||||||
|
|
||||||
@wraps(fn)
|
|
||||||
def inner(*args, **kwargs):
|
|
||||||
if is_deprecated():
|
|
||||||
deprecated_args = pos_kws[start_index : len(args)]
|
|
||||||
if deprecated_args:
|
|
||||||
msg = (
|
|
||||||
f"The positional arguments {deprecated_args} are "
|
|
||||||
"deprecated and will be removed in a future update."
|
|
||||||
)
|
|
||||||
if additional_message is not None:
|
|
||||||
msg += f" {additional_message}"
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
DeprecationWarning(msg),
|
|
||||||
stacklevel=3, # The inner function takes up one level
|
|
||||||
)
|
|
||||||
|
|
||||||
return fn(*args, **kwargs)
|
|
||||||
|
|
||||||
return inner # type: ignore
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
def deprecate_kwargs(
|
|
||||||
*kws: str,
|
|
||||||
is_deprecated: bool | Callable[[], bool] = True,
|
|
||||||
additional_message: str | None = None,
|
|
||||||
) -> Callable[[F], F]:
|
|
||||||
deprecated_kws = set(kws)
|
|
||||||
|
|
||||||
if not callable(is_deprecated):
|
|
||||||
is_deprecated = partial(identity, is_deprecated)
|
|
||||||
|
|
||||||
def wrapper(fn: F) -> F:
|
|
||||||
@wraps(fn)
|
|
||||||
def inner(*args, **kwargs):
|
|
||||||
if is_deprecated():
|
|
||||||
deprecated_kwargs = kwargs.keys() & deprecated_kws
|
|
||||||
if deprecated_kwargs:
|
|
||||||
msg = (
|
|
||||||
f"The keyword arguments {deprecated_kwargs} are "
|
|
||||||
"deprecated and will be removed in a future update."
|
|
||||||
)
|
|
||||||
if additional_message is not None:
|
|
||||||
msg += f" {additional_message}"
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
DeprecationWarning(msg),
|
|
||||||
stacklevel=3, # The inner function takes up one level
|
|
||||||
)
|
|
||||||
|
|
||||||
return fn(*args, **kwargs)
|
|
||||||
|
|
||||||
return inner # type: ignore
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
@lru_cache
|
@lru_cache
|
||||||
def supports_kw(
|
def supports_kw(
|
||||||
callable: Callable[..., object],
|
callable: Callable[..., object],
|
||||||
|
|||||||
@@ -17,11 +17,6 @@ def next_power_of_2(n: int) -> int:
|
|||||||
return 1 if n < 1 else 1 << (n - 1).bit_length()
|
return 1 if n < 1 else 1 << (n - 1).bit_length()
|
||||||
|
|
||||||
|
|
||||||
def prev_power_of_2(n: int) -> int:
|
|
||||||
"""The previous power of 2 (inclusive)"""
|
|
||||||
return 0 if n <= 0 else 1 << (n.bit_length() - 1)
|
|
||||||
|
|
||||||
|
|
||||||
def round_up(x: int, y: int) -> int:
|
def round_up(x: int, y: int) -> int:
|
||||||
"""Round up x to the nearest multiple of y."""
|
"""Round up x to the nearest multiple of y."""
|
||||||
return ((x + y - 1) // y) * y
|
return ((x + y - 1) // y) * y
|
||||||
|
|||||||
Reference in New Issue
Block a user