[Model] Re-add the implicit conversion feature for as_seq_cls_model (#21103)
Signed-off-by: wang.yuqi <noooop@126.com>
This commit is contained in:
@@ -12,7 +12,7 @@ import sys
|
||||
import tempfile
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Set
|
||||
from dataclasses import dataclass, field
|
||||
from dataclasses import asdict, dataclass, field
|
||||
from functools import lru_cache
|
||||
from typing import Callable, Optional, TypeVar, Union
|
||||
|
||||
@@ -181,10 +181,6 @@ _CROSS_ENCODER_MODELS = {
|
||||
"ModernBertForSequenceClassification": ("modernbert",
|
||||
"ModernBertForSequenceClassification"),
|
||||
# [Auto-converted (see adapters.py)]
|
||||
"GemmaForSequenceClassification": ("gemma", "GemmaForSequenceClassification"), # noqa: E501
|
||||
"Qwen2ForSequenceClassification": ("qwen2", "Qwen2ForSequenceClassification"), # noqa: E501
|
||||
"Qwen3ForSequenceClassification": ("qwen3", "Qwen3ForSequenceClassification"), # noqa: E501
|
||||
"LlamaForSequenceClassification": ("llama", "LlamaForSequenceClassification"), # noqa: E501
|
||||
"JinaVLForRanking": ("jina_vl", "JinaVLForSequenceClassification"), # noqa: E501,
|
||||
}
|
||||
|
||||
@@ -462,10 +458,26 @@ class _ModelRegistry:
|
||||
return _try_load_model_cls(model_arch, self.models[model_arch])
|
||||
|
||||
def _try_inspect_model_cls(self, model_arch: str) -> Optional[_ModelInfo]:
|
||||
if model_arch not in self.models:
|
||||
return None
|
||||
if model_arch in self.models:
|
||||
return _try_inspect_model_cls(model_arch, self.models[model_arch])
|
||||
|
||||
return _try_inspect_model_cls(model_arch, self.models[model_arch])
|
||||
if model_arch.endswith("ForSequenceClassification"):
|
||||
causal_lm_arch = model_arch.replace("ForSequenceClassification",
|
||||
"ForCausalLM")
|
||||
if causal_lm_arch not in self.models:
|
||||
return None
|
||||
|
||||
info = _try_inspect_model_cls(causal_lm_arch,
|
||||
self.models[causal_lm_arch])
|
||||
|
||||
info = _ModelInfo(**dict(
|
||||
asdict(info), **{
|
||||
"architecture": model_arch,
|
||||
"supports_cross_encoding": True
|
||||
}))
|
||||
return info
|
||||
|
||||
return None
|
||||
|
||||
def _normalize_archs(
|
||||
self,
|
||||
@@ -480,6 +492,15 @@ class _ModelRegistry:
|
||||
normalized_arch = list(
|
||||
filter(lambda model: model in self.models, architectures))
|
||||
|
||||
# try automatic conversion in adapters.py
|
||||
for arch in architectures:
|
||||
if not arch.endswith("ForSequenceClassification"):
|
||||
continue
|
||||
causal_lm_arch = arch.replace("ForSequenceClassification",
|
||||
"ForCausalLM")
|
||||
if causal_lm_arch in self.models:
|
||||
normalized_arch.append(arch)
|
||||
|
||||
# make sure Transformers backend is put at the last as a fallback
|
||||
if len(normalized_arch) != len(architectures):
|
||||
normalized_arch.append("TransformersForCausalLM")
|
||||
|
||||
Reference in New Issue
Block a user