[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
@@ -19,6 +19,7 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from transformers import AutoModelForSequenceClassification
|
||||
|
||||
from vllm.config.utils import getattr_iter
|
||||
@@ -26,11 +27,25 @@ from vllm.model_executor.layers.pooler import DispatchPooler
|
||||
from vllm.model_executor.models.interfaces import SupportsCrossEncoding
|
||||
from vllm.model_executor.models.interfaces_base import VllmModelForPooling
|
||||
|
||||
from .base import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.config import VllmConfig
|
||||
|
||||
|
||||
class EmbeddingMixin(VllmModelForPooling):
|
||||
class ClassifierWithReshape(nn.Module):
|
||||
"""Token extraction has already been applied in `pooler.pooling`.
|
||||
|
||||
Add dim to match expected input shape of `classifier.forward`.
|
||||
"""
|
||||
|
||||
def forward(self, *args, **kwargs):
|
||||
if len(args) > 0:
|
||||
args = (args[0].unsqueeze(1), *args[1:])
|
||||
return super().forward(*args, **kwargs)
|
||||
|
||||
|
||||
class EmbeddingMixin(VllmModelForPooling, Base):
|
||||
default_seq_pooling_type = "CLS"
|
||||
|
||||
def __init__(self, *, vllm_config: "VllmConfig", prefix: str = ""):
|
||||
@@ -45,7 +60,7 @@ class EmbeddingMixin(VllmModelForPooling):
|
||||
self.pooler = DispatchPooler.for_embedding(pooler_config)
|
||||
|
||||
|
||||
class SequenceClassificationMixin(SupportsCrossEncoding, VllmModelForPooling):
|
||||
class SequenceClassificationMixin(SupportsCrossEncoding, VllmModelForPooling, Base):
|
||||
default_seq_pooling_type = "CLS"
|
||||
|
||||
def __init__(self, *, vllm_config: "VllmConfig", prefix: str = ""):
|
||||
@@ -83,17 +98,13 @@ class SequenceClassificationMixin(SupportsCrossEncoding, VllmModelForPooling):
|
||||
)
|
||||
self.init_parameters(self.classifier, dtype=self.model_config.head_dtype)
|
||||
|
||||
class ClassifierWithReshape(self.classifier.__class__):
|
||||
"""Token extraction has already been applied in `pooler.pooling`.
|
||||
Add dim to match expected input shape of `classifier.forward`.
|
||||
"""
|
||||
|
||||
def forward(self, *args, **kwargs):
|
||||
if len(args) > 0:
|
||||
args = (args[0].unsqueeze(1), *args[1:])
|
||||
return super().forward(*args, **kwargs)
|
||||
|
||||
self.classifier.__class__ = ClassifierWithReshape
|
||||
# Order `ClassifierWithReshape` ahead of the classifier's own class so that
|
||||
# its `super().forward(...)` reaches the original implementation.
|
||||
self.classifier.__class__ = type(
|
||||
"ClassifierWithReshape",
|
||||
(ClassifierWithReshape, type(self.classifier)),
|
||||
{},
|
||||
)
|
||||
|
||||
self.pooler = DispatchPooler.for_seq_cls(
|
||||
pooler_config,
|
||||
|
||||
Reference in New Issue
Block a user