Enable mypy checking on V1 code (#11105)
Signed-off-by: Mark McLoughlin <markmc@redhat.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
import PIL
|
||||
from blake3 import blake3
|
||||
@@ -42,14 +42,14 @@ class MMInputMapperClient:
|
||||
model_config)
|
||||
self.mm_registry.init_mm_limits_per_prompt(model_config)
|
||||
|
||||
self.mm_cache = LRUDictCache(MM_CACHE_SIZE)
|
||||
self.mm_cache = LRUDictCache[str, MultiModalKwargs](MM_CACHE_SIZE)
|
||||
|
||||
# DEBUG: Set to None to disable
|
||||
self.mm_debug_cache_hit_ratio_steps = None
|
||||
self.mm_cache_hits = 0
|
||||
self.mm_cache_total = 0
|
||||
|
||||
def cache_hit_ratio(self, steps) -> float:
|
||||
def cache_hit_ratio(self, steps):
|
||||
if self.mm_cache_total > 0 and self.mm_cache_total % steps == 0:
|
||||
logger.debug("MMInputMapper: cache_hit_ratio = %.2f ",
|
||||
self.mm_cache_hits / self.mm_cache_total)
|
||||
@@ -60,7 +60,7 @@ class MMInputMapperClient:
|
||||
mm_hashes: Optional[List[str]],
|
||||
mm_processor_kwargs: Optional[Dict[str, Any]],
|
||||
precomputed_mm_inputs: Optional[List[MultiModalKwargs]],
|
||||
) -> List[MultiModalKwargs]:
|
||||
) -> Tuple[List[MultiModalKwargs], Optional[List[str]]]:
|
||||
if precomputed_mm_inputs is None:
|
||||
image_inputs = mm_data["image"]
|
||||
if not isinstance(image_inputs, list):
|
||||
@@ -72,6 +72,7 @@ class MMInputMapperClient:
|
||||
# Check if hash is enabled
|
||||
use_hash = mm_hashes is not None
|
||||
if use_hash:
|
||||
assert mm_hashes is not None
|
||||
assert num_inputs == len(
|
||||
mm_hashes), "num_inputs = {} len(mm_hashes) = {}".format(
|
||||
num_inputs, len(mm_hashes))
|
||||
@@ -79,7 +80,7 @@ class MMInputMapperClient:
|
||||
# Process each image input separately, so that later we can schedule
|
||||
# them in a fine-grained manner.
|
||||
# Apply caching (if enabled) and reuse precomputed inputs (if provided)
|
||||
ret_hashes = [] if use_hash else None
|
||||
ret_hashes: Optional[List[str]] = [] if use_hash else None
|
||||
ret_inputs: List[MultiModalKwargs] = []
|
||||
for input_id in range(num_inputs):
|
||||
if self.mm_debug_cache_hit_ratio_steps is not None:
|
||||
@@ -88,6 +89,7 @@ class MMInputMapperClient:
|
||||
mm_hash = None
|
||||
mm_input = None
|
||||
if use_hash:
|
||||
assert mm_hashes is not None
|
||||
mm_hash = mm_hashes[input_id]
|
||||
mm_input = self.mm_cache.get(mm_hash)
|
||||
|
||||
@@ -105,12 +107,15 @@ class MMInputMapperClient:
|
||||
|
||||
if use_hash:
|
||||
# Add to cache
|
||||
assert mm_hash is not None
|
||||
self.mm_cache.put(mm_hash, mm_input)
|
||||
else:
|
||||
self.mm_cache_hits += 1
|
||||
mm_input = None # Avoids sending mm_input to Server
|
||||
|
||||
if use_hash:
|
||||
assert mm_hash is not None
|
||||
assert ret_hashes is not None
|
||||
ret_hashes.append(mm_hash)
|
||||
ret_inputs.append(mm_input)
|
||||
|
||||
@@ -120,17 +125,18 @@ class MMInputMapperClient:
|
||||
class MMInputMapperServer:
|
||||
|
||||
def __init__(self, ):
|
||||
self.mm_cache = LRUDictCache(MM_CACHE_SIZE)
|
||||
self.mm_cache = LRUDictCache[str, MultiModalKwargs](MM_CACHE_SIZE)
|
||||
|
||||
def process_inputs(
|
||||
self,
|
||||
mm_inputs: List[Optional[MultiModalKwargs]],
|
||||
mm_hashes: List[Optional[str]],
|
||||
mm_hashes: List[str],
|
||||
) -> List[MultiModalKwargs]:
|
||||
assert len(mm_inputs) == len(mm_hashes)
|
||||
|
||||
full_mm_inputs = []
|
||||
for mm_input, mm_hash in zip(mm_inputs, mm_hashes):
|
||||
assert mm_hash is not None
|
||||
if mm_input is None:
|
||||
mm_input = self.mm_cache.get(mm_hash)
|
||||
assert mm_input is not None
|
||||
|
||||
Reference in New Issue
Block a user