[Model] Make the BERT/RoBERTa embedding class a class attribute (#59348)

Signed-off-by: Avishek Goswami <avishek.goswami@ibm.com>
Signed-off-by: Avishek Goswami <86944690+GOavi101@users.noreply.github.com>
Co-authored-by: Avishek Goswami <avishek.goswami@ibm.com>
This commit is contained in:
Avishek Goswami
2026-09-30 08:57:21 +00:00
committed by GitHub
co-authored by Avishek Goswami
parent 3627a6a124
commit fb91712b38
2 changed files with 17 additions and 7 deletions
+9 -4
View File
@@ -461,6 +461,7 @@ class BertEmbeddingModel(nn.Module, SupportsQuant):
"""
is_pooling_model = True
embedding_class: type[nn.Module] = BertEmbedding
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
super().__init__()
@@ -504,7 +505,7 @@ class BertEmbeddingModel(nn.Module, SupportsQuant):
def _build_model(self, vllm_config: VllmConfig, prefix: str = "") -> BertModel:
return BertModel(
vllm_config=vllm_config, prefix=prefix, embedding_class=BertEmbedding
vllm_config=vllm_config, prefix=prefix, embedding_class=self.embedding_class
)
def _build_pooler(self, pooler_config: PoolerConfig) -> Pooler:
@@ -781,6 +782,7 @@ class BertForSequenceClassification(nn.Module, SupportsCrossEncoding, SupportsQu
"""
is_pooling_model = True
embedding_class: type[nn.Module] = BertEmbedding
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
super().__init__()
@@ -790,7 +792,7 @@ class BertForSequenceClassification(nn.Module, SupportsCrossEncoding, SupportsQu
self.bert = BertPoolingModel(
vllm_config=vllm_config,
prefix=maybe_prefix(prefix, "bert"),
embedding_class=BertEmbedding,
embedding_class=self.embedding_class,
)
self.classifier = nn.Linear(
config.hidden_size,
@@ -840,6 +842,7 @@ class BertForSequenceClassification(nn.Module, SupportsCrossEncoding, SupportsQu
@default_pooling_type(tok_pooling_type="ALL")
class BertForTokenClassification(nn.Module):
is_pooling_model = True
embedding_class: type[nn.Module] = BertEmbedding
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
super().__init__()
@@ -849,7 +852,7 @@ class BertForTokenClassification(nn.Module):
self.bert = BertModel(
vllm_config=vllm_config,
prefix=maybe_prefix(prefix, "bert"),
embedding_class=BertEmbedding,
embedding_class=self.embedding_class,
)
self.classifier = nn.Linear(
config.hidden_size, config.num_labels, dtype=self.head_dtype
@@ -927,13 +930,15 @@ class BertForMaskedLM(nn.Module):
}
)
embedding_class: type[nn.Module] = BertEmbedding
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
super().__init__()
config = vllm_config.model_config.hf_config
self.bert = BertModel(
vllm_config=vllm_config,
prefix=maybe_prefix(prefix, "bert"),
embedding_class=BertEmbedding,
embedding_class=self.embedding_class,
)
self.mlm_head = BertMLMHead(
hidden_size=config.hidden_size,
+8 -3
View File
@@ -117,6 +117,8 @@ class RobertaClassificationHead(nn.Module):
class RobertaEmbeddingModel(BertEmbeddingModel):
"""A model that uses Roberta to provide embedding functionalities."""
embedding_class: type[nn.Module] = RobertaEmbedding
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
super().__init__(vllm_config=vllm_config, prefix=prefix)
self.padding_idx: int = vllm_config.model_config.hf_config.pad_token_id
@@ -143,7 +145,7 @@ class RobertaEmbeddingModel(BertEmbeddingModel):
return BertModel(
vllm_config=vllm_config,
prefix=prefix,
embedding_class=RobertaEmbedding,
embedding_class=self.embedding_class,
)
else:
return JinaRobertaModel(vllm_config=vllm_config, prefix=prefix)
@@ -293,6 +295,8 @@ class RobertaForSequenceClassification(nn.Module, SupportsCrossEncoding, Support
}
)
embedding_class: type[nn.Module] = RobertaEmbedding
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
super().__init__()
config = vllm_config.model_config.hf_config
@@ -303,7 +307,7 @@ class RobertaForSequenceClassification(nn.Module, SupportsCrossEncoding, Support
self.roberta = BertModel(
vllm_config=vllm_config,
prefix=maybe_prefix(prefix, "bert"),
embedding_class=RobertaEmbedding,
embedding_class=self.embedding_class,
)
self.classifier = RobertaClassificationHead(vllm_config.model_config)
@@ -355,6 +359,7 @@ class RobertaForTokenClassification(nn.Module):
"""
is_pooling_model = True
embedding_class: type[nn.Module] = RobertaEmbedding
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
super().__init__()
@@ -364,7 +369,7 @@ class RobertaForTokenClassification(nn.Module):
self.roberta = BertModel(
vllm_config=vllm_config,
prefix=maybe_prefix(prefix, "roberta"),
embedding_class=RobertaEmbedding,
embedding_class=self.embedding_class,
)
self.classifier = nn.Linear(
config.hidden_size, config.num_labels, dtype=self.head_dtype