vllm.model_executor.models.deepencoder ¶
Block ¶
Bases: Module
Transformer blocks with support of window attention and residual propagation blocks
Source code in vllm/model_executor/models/deepencoder.py
attn instance-attribute
¶
attn = RelPosAttention(
dim,
num_heads=num_heads,
qkv_bias=qkv_bias,
use_rel_pos=use_rel_pos,
rel_pos_zero_init=rel_pos_zero_init,
input_size=input_size
if window_size == 0
else (window_size, window_size),
)
mlp instance-attribute
¶
__init__ ¶
__init__(
dim: int,
num_heads: int,
mlp_ratio: float = 4.0,
qkv_bias: bool = True,
norm_layer: type[Module] = LayerNorm,
act_layer: type[Module] = GELU,
use_rel_pos: bool = False,
rel_pos_zero_init: bool = True,
window_size: int = 0,
input_size: tuple[int, int] | None = None,
) -> None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dim | int | Number of input channels. | required |
num_heads | int | Number of attention heads in each ViT block. | required |
mlp_ratio | float | Ratio of mlp hidden dim to embedding dim. | 4.0 |
qkv_bias | bool | If True, add a learnable bias to query, key, value. | True |
norm_layer | Module | Normalization layer. | LayerNorm |
act_layer | Module | Activation layer. | GELU |
use_rel_pos | bool | If True, add relative positional embeddings to the attention map. | False |
rel_pos_zero_init | bool | If True, zero initialize relative positional parameters. | True |
window_size | int | Window size for window attention blocks. If it equals 0, then use global attention. | 0 |
input_size | tuple(int, int) or None | Input resolution for calculating the relative positional parameter size. | None |
Source code in vllm/model_executor/models/deepencoder.py
forward ¶
Source code in vllm/model_executor/models/deepencoder.py
DeepCLIPVisionEmbeddings ¶
Bases: CLIPVisionEmbeddings
Source code in vllm/model_executor/models/deepencoder.py
forward ¶
Source code in vllm/model_executor/models/deepencoder.py
get_abs_pos ¶
Source code in vllm/model_executor/models/deepencoder.py
DeepCLIPVisionTransformer ¶
Bases: Module
Source code in vllm/model_executor/models/deepencoder.py
transformer instance-attribute
¶
transformer = CLIPEncoder(
config=config,
quant_config=quant_config,
num_hidden_layers_override=num_hidden_layers_override,
prefix=f"{prefix}.encoder",
attn_cls=MultiHeadAttention,
)
__init__ ¶
__init__(
config: CLIPVisionConfig,
quant_config: QuantizationConfig | None = None,
*,
num_hidden_layers_override: int | None = None,
prefix: str = "",
) -> None
Source code in vllm/model_executor/models/deepencoder.py
forward ¶
forward(
pixel_values: Tensor,
patch_embeds: Tensor | None = None,
*,
select_layers: list[int] | None = None,
) -> Tensor
Source code in vllm/model_executor/models/deepencoder.py
load_weights ¶
Source code in vllm/model_executor/models/deepencoder.py
ImageEncoderViT ¶
Bases: Module
Source code in vllm/model_executor/models/deepencoder.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
neck instance-attribute
¶
neck = Sequential(
Conv2d(embed_dim, out_chans, kernel_size=1, bias=False),
LayerNorm2d(out_chans),
Conv2d(
out_chans,
out_chans,
kernel_size=3,
padding=1,
bias=False,
),
LayerNorm2d(out_chans),
)
net_2 instance-attribute
¶
net_2 = Conv2d(
256, 512, kernel_size=3, stride=2, padding=1, bias=False
)
net_3 instance-attribute
¶
net_3 = Conv2d(
512,
1024,
kernel_size=3,
stride=2,
padding=1,
bias=False,
)
patch_embed instance-attribute
¶
patch_embed = PatchEmbed(
kernel_size=(patch_size, patch_size),
stride=(patch_size, patch_size),
in_chans=in_chans,
embed_dim=embed_dim,
)
__init__ ¶
__init__(
img_size: int = 1024,
patch_size: int = 16,
in_chans: int = 3,
embed_dim: int = 768,
depth: int = 12,
num_heads: int = 12,
mlp_ratio: float = 4.0,
out_chans: int = 256,
qkv_bias: bool = True,
norm_layer: type[Module] = LayerNorm,
act_layer: type[Module] = GELU,
use_abs_pos: bool = True,
use_rel_pos: bool = False,
rel_pos_zero_init: bool = True,
window_size: int = 0,
global_attn_indexes: tuple[int, ...] = (),
) -> None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_size | int | Input image size. | 1024 |
patch_size | int | Patch size. | 16 |
in_chans | int | Number of input image channels. | 3 |
embed_dim | int | Patch embedding dimension. | 768 |
depth | int | Depth of ViT. | 12 |
num_heads | int | Number of attention heads in each ViT block. | 12 |
mlp_ratio | float | Ratio of mlp hidden dim to embedding dim. | 4.0 |
qkv_bias | bool | If True, add a learnable bias to query, key, value. | True |
norm_layer | Module | Normalization layer. | LayerNorm |
act_layer | Module | Activation layer. | GELU |
use_abs_pos | bool | If True, use absolute positional embeddings. | True |
use_rel_pos | bool | If True, add relative positional embeddings to the attention map. | False |
rel_pos_zero_init | bool | If True, zero initialize relative positional parameters. | True |
window_size | int | Window size for window attention blocks. | 0 |
global_attn_indexes | list | Indexes for blocks using global attention. | () |
Source code in vllm/model_executor/models/deepencoder.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|
forward ¶
Source code in vllm/model_executor/models/deepencoder.py
get_abs_pos ¶
Source code in vllm/model_executor/models/deepencoder.py
LayerNorm2d ¶
Bases: Module
Source code in vllm/model_executor/models/deepencoder.py
MLPBlock ¶
Bases: Module
Source code in vllm/model_executor/models/deepencoder.py
__init__ ¶
Source code in vllm/model_executor/models/deepencoder.py
PatchEmbed ¶
Bases: Module
Image to Patch Embedding.
Source code in vllm/model_executor/models/deepencoder.py
proj instance-attribute
¶
proj = Conv2d(
in_chans,
embed_dim,
kernel_size=kernel_size,
stride=stride,
padding=padding,
)
__init__ ¶
__init__(
kernel_size: tuple[int, int] = (16, 16),
stride: tuple[int, int] = (16, 16),
padding: tuple[int, int] = (0, 0),
in_chans: int = 3,
embed_dim: int = 768,
) -> None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kernel_size | Tuple | kernel size of the projection layer. | (16, 16) |
stride | Tuple | stride of the projection layer. | (16, 16) |
padding | Tuple | padding size of the projection layer. | (0, 0) |
in_chans | int | Number of input image channels. | 3 |
embed_dim | int | Patch embedding dimension. | 768 |
Source code in vllm/model_executor/models/deepencoder.py
RelPosAttention ¶
Bases: Module
Multi-head Attention block with relative position embeddings.
Source code in vllm/model_executor/models/deepencoder.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
|
__init__ ¶
__init__(
dim: int,
num_heads: int = 8,
qkv_bias: bool = True,
use_rel_pos: bool = False,
rel_pos_zero_init: bool = True,
input_size: tuple[int, int] | None = None,
) -> None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dim | int | Number of input channels. | required |
num_heads | int | Number of attention heads. | 8 |
qkv_bias | bool | If True, add a learnable bias to query, key, value. | True |
rel_pos_zero_init | bool | If True, zero initialize relative positional parameters. | True |
input_size | tuple(int, int) or None | Input resolution for calculating the relative positional parameter size. | None |
Source code in vllm/model_executor/models/deepencoder.py
forward ¶
Source code in vllm/model_executor/models/deepencoder.py
_build_sam ¶
Source code in vllm/model_executor/models/deepencoder.py
add_decomposed_rel_pos ¶
add_decomposed_rel_pos(
q: Tensor,
rel_pos_h: Tensor,
rel_pos_w: Tensor,
q_size: tuple[int, int],
k_size: tuple[int, int],
) -> Tensor
Calculate decomposed Relative Positional Embeddings from :paper:mvitv2
. https://github.com/facebookresearch/mvit/blob/19786631e330df9f3622e5402b4a419a263a2c80/mvit/models/attention.py Args: q (Tensor): query q in the attention layer with shape (B, q_h * q_w, C). rel_pos_h (Tensor): relative position embeddings (Lh, C) for height axis. rel_pos_w (Tensor): relative position embeddings (Lw, C) for width axis. q_size (Tuple): spatial sequence size of query q with (q_h, q_w). k_size (Tuple): spatial sequence size of key k with (k_h, k_w).
Returns:
Name | Type | Description |
---|---|---|
attn | Tensor | attention map with added relative positional embeddings. |
Source code in vllm/model_executor/models/deepencoder.py
build_sam_vit_b ¶
get_rel_pos ¶
Get relative positional embeddings according to the relative positions of query and key sizes. Args: q_size (int): size of query q. k_size (int): size of key k. rel_pos (Tensor): relative position embeddings (L, C).
Returns:
Type | Description |
---|---|
Tensor | Extracted positional embeddings according to relative positions. |
Source code in vllm/model_executor/models/deepencoder.py
window_partition ¶
Partition into non-overlapping windows with padding if needed. Args: x (tensor): input tokens with [B, H, W, C]. window_size (int): window size.
Returns:
Name | Type | Description |
---|---|---|
windows | Tensor | windows after partition with [B * num_windows, window_size, window_size, C]. |
(Hp, Wp) | padded height and width before partition |
Source code in vllm/model_executor/models/deepencoder.py
window_unpartition ¶
window_unpartition(
windows: Tensor,
window_size: int,
pad_hw: tuple[int, int],
hw: tuple[int, int],
) -> Tensor
Window unpartition into original sequences and removing padding. Args: windows (tensor): input tokens with [B * num_windows, window_size, window_size, C]. window_size (int): window size. pad_hw (Tuple): padded height and width (Hp, Wp). hw (Tuple): original height and width (H, W) before padding.
Returns:
Name | Type | Description |
---|---|---|
x | Tensor | unpartitioned sequences with [B, H, W, C]. |