patch_embedding
patch_embedding
¶
PatchEmbedType
¶
Bases: StrEnum
Patch embedding implementation types.
PatchEmbedding
¶
Bases: Module
Flexible patch embedding supporting multiple strategies.
Source code in src/versatil/models/layers/patch_embedding.py
forward
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Tensor of images with shape (batch size, channels, height, width) |
required |
return_patch_size
|
bool
|
If True, also return the effective patch size after embedding. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor | tuple[Tensor, int, int]
|
For PROGRESSIVE: Tensor of shape (batch size, H', W', embedding_dimension) |
Tensor | tuple[Tensor, int, int]
|
For STANDARD/OVERLAPPING: Tensor of shape (batch size, N, embedding_dimension) where N = num_patches |
Source code in src/versatil/models/layers/patch_embedding.py
PatchMerging
¶
Bases: Module
Patch Merging Layer: Downsamples spatial dims by 2x using strided conv, changes channel dim. Input: [B, H, W, C] (H/W should be even for integer downsampling). Output: [B, H//2, W//2, out_dim].
Source code in src/versatil/models/layers/patch_embedding.py
forward
¶
Forward pass. Args: x: [B, H, W, C] input tokens. Returns: [B, H//2, W//2, out_dim] merged tokens (approximate for odd dimensions).
Note
The stride-2 convolution handles odd spatial dimensions naturally via rounding. This matches the original DFormerv2 implementation behavior.