Convert formatting to use ruff instead of yapf + isort (#26247)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
+48
-39
@@ -1,7 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from typing import (Annotated, Any, Optional, Union, get_args, get_origin,
|
||||
get_type_hints)
|
||||
from typing import Annotated, Any, Optional, Union, get_args, get_origin, get_type_hints
|
||||
|
||||
import torch
|
||||
|
||||
@@ -11,7 +10,6 @@ logger = init_logger(__name__)
|
||||
|
||||
|
||||
class TensorShape:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*dims: Union[int, str],
|
||||
@@ -37,8 +35,7 @@ class TensorShape:
|
||||
for dim in self.dims:
|
||||
if isinstance(dim, str):
|
||||
if dim in self.dynamic_dims:
|
||||
dim_strs.append(
|
||||
f"{dim}*") # Mark dynamic dimensions with *
|
||||
dim_strs.append(f"{dim}*") # Mark dynamic dimensions with *
|
||||
else:
|
||||
dim_strs.append(dim)
|
||||
else:
|
||||
@@ -47,7 +44,6 @@ class TensorShape:
|
||||
|
||||
|
||||
class TensorSchema:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
@@ -101,12 +97,12 @@ class TensorSchema:
|
||||
return str(list(idxs))
|
||||
|
||||
def _validate_field(
|
||||
self,
|
||||
value: object,
|
||||
field_name: str,
|
||||
expected_shape: tuple[Union[int, str], ...],
|
||||
dynamic_dims: set[str],
|
||||
leading_idxs: tuple[int, ...] = (),
|
||||
self,
|
||||
value: object,
|
||||
field_name: str,
|
||||
expected_shape: tuple[Union[int, str], ...],
|
||||
dynamic_dims: set[str],
|
||||
leading_idxs: tuple[int, ...] = (),
|
||||
) -> tuple[int, ...]:
|
||||
"""Validate a field and return the actual shape."""
|
||||
if isinstance(value, (int, float)):
|
||||
@@ -118,11 +114,13 @@ class TensorSchema:
|
||||
raise TypeError(
|
||||
f"{field_name}{self._fmt_indexer(leading_idxs)} is not "
|
||||
f"one of the expected types: int, float, Tensor, list, tuple. "
|
||||
f"Got: {type(value)}")
|
||||
f"Got: {type(value)}"
|
||||
)
|
||||
|
||||
if len(value) == 0:
|
||||
raise ValueError(f"{field_name}{self._fmt_indexer(leading_idxs)} "
|
||||
f"is an empty sequence")
|
||||
raise ValueError(
|
||||
f"{field_name}{self._fmt_indexer(leading_idxs)} is an empty sequence"
|
||||
)
|
||||
|
||||
# Ensure all tensors in the list have the same
|
||||
# shape, besides dynamic dimensions
|
||||
@@ -132,25 +130,26 @@ class TensorSchema:
|
||||
field_name,
|
||||
expected_shape[1:],
|
||||
dynamic_dims,
|
||||
leading_idxs=leading_idxs + (i, ),
|
||||
leading_idxs=leading_idxs + (i,),
|
||||
)
|
||||
|
||||
if i == 0:
|
||||
first_shape = shape
|
||||
elif not self._match_shape_with_dynamic(
|
||||
shape,
|
||||
first_shape,
|
||||
expected_shape,
|
||||
dynamic_dims,
|
||||
shape,
|
||||
first_shape,
|
||||
expected_shape,
|
||||
dynamic_dims,
|
||||
):
|
||||
raise ValueError(
|
||||
f"{field_name}{self._fmt_indexer(leading_idxs)} "
|
||||
f"contains inconsistent shapes: {first_shape} "
|
||||
f"(index 0) vs {shape} (index {i})")
|
||||
f"(index 0) vs {shape} (index {i})"
|
||||
)
|
||||
|
||||
# Treat the list as a stacked tensor:
|
||||
# shape = (len(list), *tensor.shape)
|
||||
return (len(value), ) + first_shape
|
||||
return (len(value),) + first_shape
|
||||
|
||||
def _validate_tensor_shape_expected(
|
||||
self,
|
||||
@@ -163,31 +162,38 @@ class TensorSchema:
|
||||
"""Validate that the actual tensor shape matches the expected shape."""
|
||||
|
||||
if len(actual_shape) != len(expected_shape):
|
||||
raise ValueError(f"{field_name} has rank {len(actual_shape)} "
|
||||
f"but expected {len(expected_shape)}. "
|
||||
f"Expected shape: {expected_shape}, "
|
||||
f"but got {actual_shape}")
|
||||
raise ValueError(
|
||||
f"{field_name} has rank {len(actual_shape)} "
|
||||
f"but expected {len(expected_shape)}. "
|
||||
f"Expected shape: {expected_shape}, "
|
||||
f"but got {actual_shape}"
|
||||
)
|
||||
|
||||
for i, dim in enumerate(expected_shape):
|
||||
if dim in dynamic_dims:
|
||||
continue
|
||||
elif isinstance(dim, int):
|
||||
if actual_shape[i] != dim:
|
||||
raise ValueError(f"{field_name} dim[{i}] expected "
|
||||
f"{dim}, got {actual_shape[i]}. "
|
||||
f"Expected shape: {expected_shape}, "
|
||||
f"but got {actual_shape}")
|
||||
raise ValueError(
|
||||
f"{field_name} dim[{i}] expected "
|
||||
f"{dim}, got {actual_shape[i]}. "
|
||||
f"Expected shape: {expected_shape}, "
|
||||
f"but got {actual_shape}"
|
||||
)
|
||||
elif isinstance(dim, str):
|
||||
if dim in shape_env:
|
||||
if actual_shape[i] != shape_env[dim]:
|
||||
raise ValueError(f"{field_name} dim[{i}] expected "
|
||||
f"'{dim}'={shape_env[dim]}, got "
|
||||
f"{actual_shape[i]}")
|
||||
raise ValueError(
|
||||
f"{field_name} dim[{i}] expected "
|
||||
f"'{dim}'={shape_env[dim]}, got "
|
||||
f"{actual_shape[i]}"
|
||||
)
|
||||
else:
|
||||
shape_env[dim] = actual_shape[i]
|
||||
else:
|
||||
raise TypeError(f"{field_name} dim[{i}] has unsupported "
|
||||
f"type: {type(dim)}")
|
||||
raise TypeError(
|
||||
f"{field_name} dim[{i}] has unsupported type: {type(dim)}"
|
||||
)
|
||||
|
||||
def validate(self) -> None:
|
||||
type_hints = get_type_hints(self.__class__, include_extras=True)
|
||||
@@ -195,8 +201,7 @@ class TensorSchema:
|
||||
|
||||
for field_name, field_type in type_hints.items():
|
||||
# Check if field is missing
|
||||
if (not hasattr(self, field_name)
|
||||
or getattr(self, field_name) is None):
|
||||
if not hasattr(self, field_name) or getattr(self, field_name) is None:
|
||||
# Check if field is marked as optional
|
||||
actual_type = field_type
|
||||
if get_origin(field_type) is Annotated:
|
||||
@@ -228,8 +233,12 @@ class TensorSchema:
|
||||
)
|
||||
|
||||
self._validate_tensor_shape_expected(
|
||||
actual_shape, expected_shape, field_name,
|
||||
shape_env, arg.dynamic_dims)
|
||||
actual_shape,
|
||||
expected_shape,
|
||||
field_name,
|
||||
shape_env,
|
||||
arg.dynamic_dims,
|
||||
)
|
||||
|
||||
def print_shapes(self) -> None:
|
||||
"""Print TensorShape annotations for debugging."""
|
||||
|
||||
Reference in New Issue
Block a user