[Mypy] Fix mypy typing for Transformers models (#54320)
Signed-off-by: Taneem Ibrahim <taneem.ibrahim@gmail.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
co-authored by
Harry Mellor
parent
b78fbab4be
commit
e960ead0aa
@@ -18,11 +18,10 @@
|
||||
|
||||
from collections.abc import Callable, Iterator
|
||||
from contextlib import contextmanager
|
||||
from functools import lru_cache
|
||||
from itertools import chain
|
||||
from operator import attrgetter
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Literal, TypeVar
|
||||
from typing import TYPE_CHECKING, Literal, TypeAlias, TypeVar
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
@@ -70,7 +69,7 @@ def init_on_device_without_buffers(device: torch.device):
|
||||
module._parameters[name].to(device), **kwargs
|
||||
)
|
||||
|
||||
tensor_constructors_to_patch = {}
|
||||
tensor_constructors_to_patch: dict[str, Callable] = {}
|
||||
|
||||
def patch_tensor_constructor(fn):
|
||||
def wrapper(*args, **kwargs):
|
||||
@@ -147,8 +146,8 @@ def replace_linear_class(
|
||||
)
|
||||
|
||||
|
||||
TorchConv = nn.Conv2d | nn.Conv3d
|
||||
VllmConv = Conv2dLayer | Conv3dLayer
|
||||
TorchConv: TypeAlias = nn.Conv2d | nn.Conv3d
|
||||
VllmConv: TypeAlias = Conv2dLayer | Conv3dLayer
|
||||
|
||||
|
||||
def replace_conv_class(conv: TorchConv) -> VllmConv | TorchConv:
|
||||
@@ -212,7 +211,9 @@ class _VocabParallelEmbeddingBase(VocabParallelEmbedding, _UninitializedEmbeddin
|
||||
`super().forward(...)` in an `nn.Embedding` subclass reaches vLLM's embedding."""
|
||||
|
||||
|
||||
@lru_cache
|
||||
_rebased_embedding_classes: dict[type[nn.Embedding], type[VocabParallelEmbedding]] = {}
|
||||
|
||||
|
||||
def _rebase_on_vocab_parallel(cls: type[nn.Embedding]) -> type[VocabParallelEmbedding]:
|
||||
"""Subclass `cls` so that `VocabParallelEmbedding` supersedes its `nn.Embedding`.
|
||||
|
||||
@@ -224,7 +225,10 @@ def _rebase_on_vocab_parallel(cls: type[nn.Embedding]) -> type[VocabParallelEmbe
|
||||
The new class, to assign to `__class__` of an instance of `cls`.
|
||||
|
||||
"""
|
||||
return type(cls.__name__, (cls, _VocabParallelEmbeddingBase), {})
|
||||
if cls not in _rebased_embedding_classes:
|
||||
rebased_cls = type(cls.__name__, (cls, _VocabParallelEmbeddingBase), {})
|
||||
_rebased_embedding_classes[cls] = rebased_cls
|
||||
return _rebased_embedding_classes[cls]
|
||||
|
||||
|
||||
def replace_embedding_class(
|
||||
@@ -278,10 +282,9 @@ def recursive_replace_linear(
|
||||
qual_name = maybe_prefix(prefix, child_name)
|
||||
# Replace modules as needed
|
||||
if isinstance(child_module, nn.Linear):
|
||||
style = "replicate"
|
||||
new_module = replace_linear_class(
|
||||
child_module,
|
||||
style,
|
||||
"replicate",
|
||||
quant_config,
|
||||
prefix=qual_name,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user