[Model][0/N] Improve all pooling task | clean up (#25817)
Signed-off-by: wang.yuqi <noooop@126.com>
This commit is contained in:
@@ -13,7 +13,10 @@ from vllm.config import VllmConfig
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.activation import get_act_fn
|
||||
from vllm.model_executor.models.config import VerifyAndUpdateConfig
|
||||
from vllm.transformers_utils.config import get_hf_file_bytes, get_hf_file_to_dict
|
||||
from vllm.transformers_utils.config import (
|
||||
get_hf_file_bytes,
|
||||
try_get_dense_modules,
|
||||
)
|
||||
|
||||
from .interfaces_base import VllmModelForPooling, is_pooling_model
|
||||
|
||||
@@ -35,43 +38,25 @@ _GENERATE_SUFFIXES = [
|
||||
def _load_st_projector(model_config: "ModelConfig") -> nn.Module | None:
|
||||
"""Load Sentence-Transformers Dense projection layers."""
|
||||
|
||||
dense_modules = try_get_dense_modules(
|
||||
model_config.model, revision=model_config.revision
|
||||
)
|
||||
|
||||
if dense_modules is None:
|
||||
return
|
||||
|
||||
try:
|
||||
modules = get_hf_file_to_dict(
|
||||
"modules.json", model_config.model, model_config.revision
|
||||
)
|
||||
if not modules:
|
||||
return None
|
||||
|
||||
if isinstance(modules, dict):
|
||||
modules = modules.get("modules", [])
|
||||
|
||||
dense_modules = [
|
||||
m for m in modules if m.get("type") == "sentence_transformers.models.Dense"
|
||||
]
|
||||
if not dense_modules:
|
||||
return None
|
||||
|
||||
layers = []
|
||||
for module in dense_modules:
|
||||
folder = module.get("path", "")
|
||||
|
||||
config_path = f"{folder}/config.json" if folder else "config.json"
|
||||
layer_config = get_hf_file_to_dict(
|
||||
config_path, model_config.model, model_config.revision
|
||||
)
|
||||
if not layer_config:
|
||||
continue
|
||||
|
||||
for layer_config in dense_modules:
|
||||
folder = layer_config["folder"]
|
||||
linear = nn.Linear(
|
||||
layer_config.get("in_features", 768),
|
||||
layer_config.get("out_features", 768),
|
||||
layer_config["in_features"],
|
||||
layer_config["out_features"],
|
||||
bias=layer_config.get("bias", True),
|
||||
dtype=model_config.head_dtype,
|
||||
)
|
||||
|
||||
if not _load_dense_weights(linear, folder, model_config):
|
||||
continue
|
||||
|
||||
layers.append(linear)
|
||||
if act_name := layer_config.get("activation_function"):
|
||||
layers.append(get_act_fn(act_name))
|
||||
@@ -303,18 +288,18 @@ def as_seq_cls_model(cls: _T) -> _T:
|
||||
from vllm.model_executor.models.interfaces import SupportsCrossEncoding
|
||||
from vllm.sequence import IntermediateTensors
|
||||
|
||||
from .utils import get_model_hidden_size, maybe_prefix
|
||||
from .utils import maybe_prefix
|
||||
|
||||
class ModelForSequenceClassification(
|
||||
_create_pooling_model_cls(cls), SupportsCrossEncoding
|
||||
):
|
||||
def _init_pooler(self, vllm_config: "VllmConfig", prefix: str = ""):
|
||||
config = vllm_config.model_config.hf_config
|
||||
model_config = vllm_config.model_config
|
||||
quant_config = vllm_config.quant_config
|
||||
hidden_size = get_model_hidden_size(config)
|
||||
|
||||
self.score = ReplicatedLinear(
|
||||
hidden_size,
|
||||
model_config.hidden_size,
|
||||
config.num_labels,
|
||||
bias=False,
|
||||
params_dtype=torch.float32,
|
||||
|
||||
Reference in New Issue
Block a user