vllm_mlx.rerank_forward¶
MLX forward pass for BERT-family sequence classification models.
View the complete module source at #L1-L265.
API details¶
Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.
vllm_mlx.rerank_forward
¶
MLX forward pass for BERT-family sequence classification models.
Implements a from-weights forward pass for cross-encoder rerankers that use the standard BERT/XLM-RoBERTa architecture with a classification head. This avoids pulling in the full transformers modeling stack at inference time — only the tokenizer is needed from transformers.
vllm_mlx.rerank_forward.classifier_forward
¶
classifier_forward(input_ids: array, attention_mask: array, weights: dict[str, array], config: dict) -> array
Run a BERT-family classifier forward pass on MLX.
Parameters:
-
input_ids(array) –(batch, seq_len) token IDs.
-
attention_mask(array) –(batch, seq_len) attention mask (1=attend, 0=pad).
-
weights(dict[str, array]) –Dict mapping weight name -> mx.array.
-
config(dict) –Model config dict (from config.json).
Returns:
-
logits(array) –(batch, num_labels) classification logits.
Source code in vllm_mlx/rerank_forward.py
vllm_mlx.rerank_forward._position_ids_for_config
¶
Build BERT or RoBERTa-family absolute position IDs.
Source code in vllm_mlx/rerank_forward.py
vllm_mlx.rerank_forward._classification_head_forward
¶
Run BERT flat or XLM-RoBERTa two-layer sequence-classification head.
Source code in vllm_mlx/rerank_forward.py
vllm_mlx.rerank_forward._detect_prefix
¶
Detect the model weight prefix (bert, roberta, xlm-roberta).
Source code in vllm_mlx/rerank_forward.py
vllm_mlx.rerank_forward._layer_norm
¶
Apply layer normalization.
Source code in vllm_mlx/rerank_forward.py
vllm_mlx.rerank_forward._encoder_layer
¶
_encoder_layer(hidden: array, ext_mask: array | None, weights: dict, prefix: str, num_heads: int, head_dim: int, eps: float, config: dict) -> array
Run one BERT encoder layer (self-attention + FFN).
Source code in vllm_mlx/rerank_forward.py
vllm_mlx.rerank_forward._gelu
¶
vllm_mlx.rerank_forward._gelu_new
¶
vllm_mlx.rerank_forward._relu
¶
vllm_mlx.rerank_forward._silu
¶
vllm_mlx.rerank_forward._apply_hidden_activation
¶
Apply the configured encoder hidden activation.
The MLX reranker forward pass targets standard BERT/XLM-RoBERTa-style sequence classifiers. Configs that request an activation outside that supported contract fail explicitly instead of silently using GELU.
Source code in vllm_mlx/rerank_forward.py
Complete contract reference¶
Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.
vllm_mlx.rerank_forward.classifier_forward · function
vllm_mlx.rerank_forward.classifier_forward(input_ids: mx.array, attention_mask: mx.array, weights: dict[str, mx.array], config: dict) -> mx.array
Run a BERT-family classifier forward pass on MLX.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
input_ids |
mx.array |
yes |
none |
(batch, seq_len) token IDs. |
attention_mask |
mx.array |
yes |
none |
(batch, seq_len) attention mask (1=attend, 0=pad). |
weights |
dict[str, mx.array] |
yes |
none |
Dict mapping weight name -> mx.array. |
config |
dict |
yes |
none |
Model config dict (from config.json). |
Returns
- Type:
mx.array - Direct return expressions:
logits
Exceptions and behavior
Function classifier_forward calls config.get, _detect_prefix, _position_ids_for_config, mx.zeros_like; returns logits.
No direct raise statement appears in this definition.
vllm_mlx.rerank_forward._position_ids_for_config · function
vllm_mlx.rerank_forward._position_ids_for_config(config: dict, input_ids: mx.array, attention_mask: mx.array | None) -> mx.array
Build BERT or RoBERTa-family absolute position IDs.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
config |
dict |
yes |
none |
Required positional or keyword input. |
input_ids |
mx.array |
yes |
none |
Required positional or keyword input. |
attention_mask |
mx.array \| None |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
mx.arange(seq_len)[None, :];mx.arange(padding_idx + 1, seq_len + padding_idx + 1)[None, :];positions.astype(mx.int32)
Exceptions and behavior
Function _position_ids_for_config calls str(config.get('model_type', '')).lower, str, config.get, mx.arange; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.rerank_forward._classification_head_forward · function
vllm_mlx.rerank_forward._classification_head_forward(pooled: mx.array, weights: dict[str, mx.array]) -> mx.array
Run BERT flat or XLM-RoBERTa two-layer sequence-classification head.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
pooled |
mx.array |
yes |
none |
Required positional or keyword input. |
weights |
dict[str, mx.array] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
hidden @ weights['classifier.out_proj.weight'].T + weights['classifier.out_proj.bias'];pooled @ weights['classifier.weight'].T + weights['classifier.bias']
Exceptions and behavior
Function _classification_head_forward calls mx.tanh; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.rerank_forward._detect_prefix · function
Detect the model weight prefix (bert, roberta, xlm-roberta).
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
weights |
dict |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str - Direct return expressions:
'bert';'roberta';'xlm-roberta'
Exceptions and behavior
Function _detect_prefix calls key.startswith; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.rerank_forward._layer_norm · function
vllm_mlx.rerank_forward._layer_norm(x: mx.array, weight: mx.array, bias: mx.array, eps: float) -> mx.array
Apply layer normalization.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
x |
mx.array |
yes |
none |
Required positional or keyword input. |
weight |
mx.array |
yes |
none |
Required positional or keyword input. |
bias |
mx.array |
yes |
none |
Required positional or keyword input. |
eps |
float |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
weight * (x - mean) / mx.sqrt(var + eps) + bias
Exceptions and behavior
Function _layer_norm calls mx.mean, mx.var, mx.sqrt; returns weight * (x - mean) / mx.sqrt(var + eps) + bias.
No direct raise statement appears in this definition.
vllm_mlx.rerank_forward._encoder_layer · function
vllm_mlx.rerank_forward._encoder_layer(hidden: mx.array, ext_mask: mx.array | None, weights: dict, prefix: str, num_heads: int, head_dim: int, eps: float, config: dict) -> mx.array
Run one BERT encoder layer (self-attention + FFN).
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
hidden |
mx.array |
yes |
none |
Required positional or keyword input. |
ext_mask |
mx.array \| None |
yes |
none |
Required positional or keyword input. |
weights |
dict |
yes |
none |
Required positional or keyword input. |
prefix |
str |
yes |
none |
Required positional or keyword input. |
num_heads |
int |
yes |
none |
Required positional or keyword input. |
head_dim |
int |
yes |
none |
Required positional or keyword input. |
eps |
float |
yes |
none |
Required positional or keyword input. |
config |
dict |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
hidden
Exceptions and behavior
Function _encoder_layer calls (hidden @ q_w.T + q_b).reshape(batch_size, seq_len, num_heads, head_dim).transpose, (hidden @ q_w.T + q_b).reshape, (hidden @ k_w.T + k_b).reshape(batch_size, seq_len, num_heads, head_dim).transpose, (hidden @ k_w.T + k_b).reshape; returns hidden.
No direct raise statement appears in this definition.
vllm_mlx.rerank_forward._gelu · function
GELU activation (exact form).
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
x |
mx.array |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
nn.gelu(x)
Exceptions and behavior
Function _gelu calls nn.gelu; returns nn.gelu(x).
No direct raise statement appears in this definition.
vllm_mlx.rerank_forward._gelu_new · function
BERT GELU approximation used by transformers gelu_new.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
x |
mx.array |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
0.5 * x * (1.0 + mx.tanh(0.7978845608028654 * (x + 0.044715 * x ** 3)))
Exceptions and behavior
Function _gelu_new calls mx.tanh; returns 0.5 * x * (1.0 + mx.tanh(0.7978845608028654 * (x + 0.044715 * x ** 3))).
No direct raise statement appears in this definition.
vllm_mlx.rerank_forward._relu · function
ReLU activation.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
x |
mx.array |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
mx.maximum(x, 0)
Exceptions and behavior
Function _relu calls mx.maximum; returns mx.maximum(x, 0).
No direct raise statement appears in this definition.
vllm_mlx.rerank_forward._silu · function
SiLU/swish activation.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
x |
mx.array |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
mx.array - Direct return expressions:
x * mx.sigmoid(x)
Exceptions and behavior
Function _silu calls mx.sigmoid; returns x * mx.sigmoid(x).
No direct raise statement appears in this definition.
Complete symbol map¶
This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.
| Symbol | Kind | Signature and inputs | What it does | Source |
|---|---|---|---|---|
classifier_forward |
function | classifier_forward(input_ids: mx.array, attention_mask: mx.array, weights: dict[str, mx.array], config: dict) -> mx.array |
Run a BERT-family classifier forward pass on MLX. | #L16-L85 |
_position_ids_for_config |
function | _position_ids_for_config(config: dict, input_ids: mx.array, attention_mask: mx.array \| None) -> mx.array |
Build BERT or RoBERTa-family absolute position IDs. | #L88-L105 |
_classification_head_forward |
function | _classification_head_forward(pooled: mx.array, weights: dict[str, mx.array]) -> mx.array |
Run BERT flat or XLM-RoBERTa two-layer sequence-classification head. | #L108-L122 |
_detect_prefix |
function | _detect_prefix(weights: dict) -> str |
Detect the model weight prefix (bert, roberta, xlm-roberta). | #L125-L135 |
_layer_norm |
function | _layer_norm(x: mx.array, weight: mx.array, bias: mx.array, eps: float) -> mx.array |
Apply layer normalization. | #L138-L142 |
_encoder_layer |
function | _encoder_layer(hidden: mx.array, ext_mask: mx.array \| None, weights: dict, prefix: str, num_heads: int, head_dim: int, eps: float, config: dict) -> mx.array |
Run one BERT encoder layer (self-attention + FFN). | #L145-L217 |
_gelu |
function | _gelu(x: mx.array) -> mx.array |
GELU activation (exact form). | #L220-L222 |
_gelu_new |
function | _gelu_new(x: mx.array) -> mx.array |
BERT GELU approximation used by transformers gelu_new. | #L225-L227 |
_relu |
function | _relu(x: mx.array) -> mx.array |
ReLU activation. | #L230-L232 |
_silu |
function | _silu(x: mx.array) -> mx.array |
SiLU/swish activation. | #L235-L237 |
_apply_hidden_activation |
function | _apply_hidden_activation(x: mx.array, config: dict) -> mx.array |
Apply the configured encoder hidden activation. | #L240-L265 |