Skip to content

[Fix] Support cls pooling in ModernBertPooler #20067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 25, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion vllm/model_executor/models/modernbert.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,22 @@
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size,
config.classifier_bias)
self.pooling_type = config.classifier_pooling
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's good to see that the pooling type is being loaded from the model config. However, ensure that config.classifier_pooling is validated elsewhere to prevent unexpected values from being used. If not, consider adding a validation step here or in the config definition to ensure it's one of the supported types (cls or mean).

self.act = nn.GELU()
self.norm = nn.LayerNorm(config.hidden_size,
eps=config.norm_eps,
bias=config.norm_bias)

def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
pooled_output = hidden_states
pooled_output = pooled_output.mean(dim=0, keepdim=False)
if self.pooling_type == "mean":
pooled_output = pooled_output.mean(dim=0, keepdim=False)
elif self.pooling_type == "cls":
pooled_output = pooled_output[0, :]
else:
raise ValueError(
f"Pooling type should be either `cls` or `mean`, but got {self.pooling_type}"

Check failure on line 275 in vllm/model_executor/models/modernbert.py

View workflow job for this annotation

GitHub Actions / pre-commit

Ruff (E501)

vllm/model_executor/models/modernbert.py:275:81: E501 Line too long (93 > 80)
)
pooled_output = self.norm(self.act(self.dense(pooled_output)))
return pooled_output

Expand Down
Loading