Add pydocstyle to the ruff rules (#52136)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Signed-off-by: Harry Mellor <hej.mellor@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
804e5377aa
commit
8c1557a79c
+21
-32
@@ -52,23 +52,19 @@ _ENABLE_TORCH_WRAP: bool = True
|
||||
|
||||
|
||||
def set_default_torch_wrap(enable: bool = True) -> None:
|
||||
"""
|
||||
Permanently set the torch wrap flag.
|
||||
"""
|
||||
"""Permanently set the torch wrap flag."""
|
||||
global _ENABLE_TORCH_WRAP
|
||||
_ENABLE_TORCH_WRAP = enable
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def enable_torch_wrap(enable: bool = True):
|
||||
"""
|
||||
Context manager to enable/disable torch custom op wrapping for vLLM IR ops.
|
||||
"""Context manager to enable/disable torch custom op wrapping for vLLM IR ops.
|
||||
When torch wrapping is disabled, the torch custom op layer is skipped
|
||||
and IR ops dispatch directly to the implementation.
|
||||
Helpful for avoiding torch dispatch overhead in eager mode
|
||||
and avoiding the need for lowering for platforms not using Inductor.
|
||||
"""
|
||||
|
||||
global _ENABLE_TORCH_WRAP
|
||||
old = _ENABLE_TORCH_WRAP
|
||||
try:
|
||||
@@ -110,8 +106,7 @@ def register_op(
|
||||
activations: list[str] | None = None,
|
||||
allow_inplace: bool = False,
|
||||
) -> "IrOp | Callable[[Callable], IrOp]":
|
||||
"""
|
||||
Register a new vLLM IR op.
|
||||
"""Register a new vLLM IR op.
|
||||
|
||||
Args:
|
||||
f: the native implementation of the op
|
||||
@@ -131,7 +126,10 @@ def register_op(
|
||||
|
||||
@vllm.ir.register_op(name="custom_mul")
|
||||
def multiply(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
|
||||
return x * y"""
|
||||
return x * y
|
||||
```
|
||||
|
||||
"""
|
||||
|
||||
def decorator(_f: Callable):
|
||||
op_name: str = _f.__name__ if name is None else name
|
||||
@@ -226,16 +224,14 @@ class IrOp:
|
||||
self.torch_op: torch._ops.OpOverload = getattr(torch_ops, name).default
|
||||
|
||||
def register_fake(self, fn: Callable) -> Callable:
|
||||
"""
|
||||
Register a fake impl for the torch custom op. If this method is not called,
|
||||
"""Register a fake impl for the torch custom op. If this method is not called,
|
||||
the native implementation is used directly for the fake implementation.
|
||||
"""
|
||||
self._fake_fn = fn
|
||||
return fn
|
||||
|
||||
def _fake_call(self, *args, **kwargs) -> Any:
|
||||
"""
|
||||
Call to the fake implementation of the op. We use indirection because we want
|
||||
"""Call to the fake implementation of the op. We use indirection because we want
|
||||
users to be able to register fake later but also want it to fall back to native
|
||||
directly by default, instead of going through the dispatching mechanism.
|
||||
"""
|
||||
@@ -249,8 +245,8 @@ class IrOp:
|
||||
supports_args: Callable[..., bool] | None = None,
|
||||
inplace: bool = False,
|
||||
) -> Callable[[Callable[..., Any]], "IrOpImpl"]:
|
||||
"""
|
||||
Register an implementation for this custom op.
|
||||
"""Register an implementation for this custom op.
|
||||
|
||||
Args:
|
||||
provider: The name of the provider, must be unique.
|
||||
supported: Static support check, use this to check platform support.
|
||||
@@ -302,8 +298,7 @@ class IrOp:
|
||||
return _register_impl
|
||||
|
||||
def _inner_call(self, *args, **kwargs) -> Any:
|
||||
"""
|
||||
Eager call to torch op lands here. When torch wrapping is disabled,
|
||||
"""Eager call to torch op lands here. When torch wrapping is disabled,
|
||||
__call__ routes straight here instead of going through torch op dispatching.
|
||||
"""
|
||||
impl = self.dispatch(*args, **kwargs)
|
||||
@@ -313,8 +308,7 @@ class IrOp:
|
||||
return impl.func_impl_fn(*args, **kwargs)
|
||||
|
||||
def apply_arg_defaults(self, args) -> tuple:
|
||||
"""
|
||||
Return args with default values applied.
|
||||
"""Return args with default values applied.
|
||||
Defaults are taken from the native implementation signature.
|
||||
|
||||
SHOULD NOT BE USED IN THE DISPATCH PATH (SLOW).
|
||||
@@ -325,8 +319,7 @@ class IrOp:
|
||||
return bound_args.args
|
||||
|
||||
def dispatch(self, *args, **kwargs) -> "IrOpImpl":
|
||||
"""
|
||||
Dispatch to the appropriate implementation based on current priority
|
||||
"""Dispatch to the appropriate implementation based on current priority
|
||||
and argument support checks. Returns the selected IrOpImpl.
|
||||
|
||||
THIS FUNCTION IS ON THE HOT PATH (OP DISPATCH), MUST BE FAST.
|
||||
@@ -413,8 +406,7 @@ class IrOp:
|
||||
return filtered_impls
|
||||
|
||||
def set_default(self, priority: list[str]) -> None:
|
||||
"""
|
||||
Permanently set the dispatch priority for this op. Use this for
|
||||
"""Permanently set the dispatch priority for this op. Use this for
|
||||
process-lifetime setup (e.g., worker startup). For scoped overrides,
|
||||
use ``set_priority`` instead.
|
||||
"""
|
||||
@@ -427,9 +419,7 @@ class IrOp:
|
||||
|
||||
@contextlib.contextmanager
|
||||
def set_priority(self, priority: list[str]):
|
||||
"""
|
||||
Context manager to set the dispatch priority for implementations for this op.
|
||||
"""
|
||||
"""Context manager setting the dispatch priority for this op's impls."""
|
||||
old_priority_impls = self._priority_impls
|
||||
try:
|
||||
self._priority_impls = self._filter_priority_impls(priority)
|
||||
@@ -636,10 +626,10 @@ class IrOpImpl:
|
||||
|
||||
@weak_cache
|
||||
def uuid(self):
|
||||
"""
|
||||
Compile-time hash to uniquely determine whether the implementation has changed.
|
||||
Used by vllm-compile hash mechanism and torch.compile lowering pass uuid to
|
||||
control the vLLM compile cache and AOTAutograd/Inductor caches respectively.
|
||||
"""Compile-time hash to uniquely determine whether the implementation has
|
||||
changed. Used by vllm-compile hash mechanism and torch.compile lowering pass
|
||||
uuid to control the vLLM compile cache and AOTAutograd/Inductor caches
|
||||
respectively.
|
||||
|
||||
Source file contents do not change so we cache uuid.
|
||||
TODO(luka): Cache the file hash as multiple impls are likely in the same file.
|
||||
@@ -648,8 +638,7 @@ class IrOpImpl:
|
||||
return hash_source(*sources)
|
||||
|
||||
def func_impl_fn(self, *args, **kwargs) -> Any:
|
||||
"""
|
||||
Copy any inputs in activations if this is an inplace impl,
|
||||
"""Copy any inputs in activations if this is an inplace impl,
|
||||
to ensure functional semantics.
|
||||
"""
|
||||
if not self.inplace:
|
||||
|
||||
@@ -10,7 +10,7 @@ from ..op import register_op
|
||||
def rms_norm(
|
||||
x: Tensor, weight: Tensor | None, epsilon: float, variance_size: int | None = None
|
||||
) -> Tensor:
|
||||
"""Weighted root-mean-square layer normalization"""
|
||||
"""Weighted root-mean-square layer normalization."""
|
||||
orig_dtype = x.dtype
|
||||
x = x.to(torch.float32)
|
||||
x_var = x if variance_size is None else x[..., :variance_size]
|
||||
@@ -48,7 +48,7 @@ def fused_add_rms_norm(
|
||||
epsilon: float,
|
||||
variance_size: int | None = None,
|
||||
) -> tuple[Tensor, Tensor]:
|
||||
"""Fused add and weighted root-mean-square layer normalization"""
|
||||
"""Fused add and weighted root-mean-square layer normalization."""
|
||||
orig_dtype = x.dtype
|
||||
x = x.to(torch.float32)
|
||||
x = x + x_residual.to(torch.float32)
|
||||
|
||||
+5
-5
@@ -10,11 +10,12 @@ from typing import Any
|
||||
|
||||
|
||||
def hash_source(*srcs: str | Any) -> str:
|
||||
"""
|
||||
Utility method to hash the sources of functions or objects.
|
||||
"""Utility method to hash the sources of functions or objects.
|
||||
|
||||
Args:
|
||||
srcs: strings or objects to add to the hash.
|
||||
Objects and functions have their source inspected.
|
||||
|
||||
"""
|
||||
hasher = hashlib.sha256()
|
||||
for src in srcs:
|
||||
@@ -34,8 +35,7 @@ def hash_source(*srcs: str | Any) -> str:
|
||||
|
||||
|
||||
def weak_lru_cache(maxsize: int | None = 128, typed: bool = False):
|
||||
"""
|
||||
LRU Cache decorator that keeps a weak reference to 'self'.
|
||||
"""LRU Cache decorator that keeps a weak reference to 'self'.
|
||||
This avoids memory leakage, which happens when functools.lru_cache
|
||||
stores a reference to self in the global cache.
|
||||
|
||||
@@ -57,5 +57,5 @@ def weak_lru_cache(maxsize: int | None = 128, typed: bool = False):
|
||||
|
||||
|
||||
def weak_cache(user_function, /):
|
||||
"""Simple weak equivalent to functools.cache"""
|
||||
"""Simple weak equivalent to functools.cache."""
|
||||
return weak_lru_cache(maxsize=None)(user_function)
|
||||
|
||||
Reference in New Issue
Block a user