pooling_head
pooling_head
¶
Pooling strategies for spatial feature maps and token sequences.
PoolingHead
¶
Bases: Module, ABC
Abstract base class for pooling operations on spatial feature maps or token sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dimension
|
int
|
Feature vector size, i.e. channel count for spatial feature maps (B, C, H, W), or hidden dimension for token sequences (B, S, D). |
required |
Source code in src/versatil/models/layers/pooling/pooling_head.py
SpatialSoftmaxPooling
¶
Bases: PoolingHead
Spatial softmax pooling on feature maps.
Initialize spatial softmax pooling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dimension
|
int
|
Number of feature channels. |
required |
spatial_height
|
int
|
Height of the feature map. |
required |
spatial_width
|
int
|
Width of the feature map. |
required |
Source code in src/versatil/models/layers/pooling/pooling_head.py
forward
¶
Compute per-channel spatial softmax keypoints from (B, C, H, W).
GlobalAveragePooling
¶
Bases: PoolingHead
Global average pooling over spatial dimensions.
Source code in src/versatil/models/layers/pooling/pooling_head.py
MaxPooling
¶
Bases: PoolingHead
Global max pooling over spatial dimensions.
Source code in src/versatil/models/layers/pooling/pooling_head.py
forward
¶
SpatialIdentityPooling
¶
Bases: PoolingHead
No pooling — returns spatial feature maps unchanged.
output_dim returns (C, -1, -1) where -1 indicates dynamic
spatial dimensions resolved at forward time from the actual feature map.
Source code in src/versatil/models/layers/pooling/pooling_head.py
SpatialLearnedAggregationPooling
¶
Bases: PoolingHead
Learned aggregation of spatial feature maps through attention.
Source code in src/versatil/models/layers/pooling/pooling_head.py
forward
¶
TokenPoolingHead
¶
Bases: PoolingHead
Pooling head for token sequences (B, S, D).
Reduces a sequence of token embeddings to a single vector (B, D) via
CLS token selection, mean pooling, or learned aggregation. With
pooling_method=NONE, returns the sequence with prefix tokens stripped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dimension
|
int
|
Hidden dimension of the token embeddings. |
required |
pooling_method
|
str
|
Pooling strategy from PoolingMethod enum. |
required |
sequence_length
|
int
|
Fixed sequence length for NONE output dim (-1 for variable). |
-1
|
num_prefix_tokens
|
int
|
Number of prefix tokens (CLS, registers) to exclude from AVERAGE, LEARNED_AGGREGATION, and NONE pooling. The first prefix token is still used for DEFAULT (CLS) pooling. |
0
|
Source code in src/versatil/models/layers/pooling/pooling_head.py
forward
¶
Pool token sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Token embeddings of shape (B, S, D). |
required |
padding_mask
|
Tensor | None
|
Optional padding mask of shape (B, S), where True means padded. Used by pooling methods that aggregate tokens. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Pooled features of shape (B, D) or (B, S', D) for NONE. |
Source code in src/versatil/models/layers/pooling/pooling_head.py
create_spatial_pooling_head
¶
Create a pooling head for spatial feature maps (B, C, H, W).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pooling_method
|
str
|
Pooling strategy from PoolingMethod enum. |
required |
input_dimension
|
int
|
Number of feature channels. |
required |
spatial_height
|
int
|
Height of the feature map. |
required |
spatial_width
|
int
|
Width of the feature map. |
required |
Returns:
| Type | Description |
|---|---|
PoolingHead
|
Configured spatial pooling head. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If pooling_method is not supported for spatial features. |
Source code in src/versatil/models/layers/pooling/pooling_head.py
create_token_pooling_head
¶
Create a pooling head for token sequences (B, S, D).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pooling_method
|
str
|
Pooling strategy from PoolingMethod enum. |
required |
input_dimension
|
int
|
Hidden dimension of the token embeddings. |
required |
sequence_length
|
int
|
Fixed sequence length for NONE output dim (-1 for variable). |
-1
|
num_prefix_tokens
|
int
|
Number of prefix tokens (CLS, registers) to strip. |
0
|
Returns:
| Type | Description |
|---|---|
TokenPoolingHead
|
Configured token pooling head. |