vllm.model_executor.models.deepencoder ¶
Classes:
-
Block–Transformer blocks with support of window attention and residual propagation
-
ImageEncoderViT– -
PatchEmbed–Image to Patch Embedding.
-
RelPosAttention–Multi-head Attention block with relative position embeddings.
Functions:
-
add_decomposed_rel_pos–Calculate decomposed Relative Positional Embeddings from :paper:
mvitv2. -
deepencoder_rel_pos_attention–Run DeepEncoder global attention with fused relative-position bias.
-
get_rel_pos–Get relative positional embeddings according to the relative positions of
-
window_partition–Partition into non-overlapping windows with padding if needed.
-
window_unpartition–Window unpartition into original sequences and removing padding.
Block ¶
Bases: Module
Transformer blocks with support of window attention and residual propagation blocks
Methods:
-
__init__–Args:
Source code in vllm/model_executor/models/deepencoder.py
__init__(dim, num_heads, mlp_ratio=4.0, qkv_bias=True, norm_layer=nn.LayerNorm, act_layer=nn.GELU, use_rel_pos=False, rel_pos_zero_init=True, window_size=0, input_size=None) ¶
Parameters:
-
(dim¶int) –Number of input channels.
-
(num_heads¶int) –Number of attention heads in each ViT block.
-
(mlp_ratio¶float, default:4.0) –Ratio of mlp hidden dim to embedding dim.
-
(qkv_bias¶bool, default:True) –If True, add a learnable bias to query, key, value.
-
(norm_layer¶Module, default:LayerNorm) –Normalization layer.
-
(act_layer¶Module, default:GELU) –Activation layer.
-
(use_rel_pos¶bool, default:False) –If True, add relative positional embeddings to the attention map.
-
(rel_pos_zero_init¶bool, default:True) –If True, zero initialize relative positional parameters.
-
(window_size¶int, default:0) –Window size for window attention blocks. If it equals 0, then use global attention.
-
(input_size¶tuple(int, int) or None, default:None) –Input resolution for calculating the relative positional parameter size.
Source code in vllm/model_executor/models/deepencoder.py
ImageEncoderViT ¶
Bases: Module
Methods:
-
__init__–Args:
Source code in vllm/model_executor/models/deepencoder.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
__init__(img_size=1024, patch_size=16, in_chans=3, embed_dim=768, depth=12, num_heads=12, mlp_ratio=4.0, out_chans=256, qkv_bias=True, norm_layer=nn.LayerNorm, act_layer=nn.GELU, use_abs_pos=True, use_rel_pos=False, rel_pos_zero_init=True, window_size=0, global_attn_indexes=(), last_conv_output=1024) ¶
Parameters:
-
(img_size¶int, default:1024) –Input image size.
-
(patch_size¶int, default:16) –Patch size.
-
(in_chans¶int, default:3) –Number of input image channels.
-
(embed_dim¶int, default:768) –Patch embedding dimension.
-
(depth¶int, default:12) –Depth of ViT.
-
(num_heads¶int, default:12) –Number of attention heads in each ViT block.
-
(mlp_ratio¶float, default:4.0) –Ratio of mlp hidden dim to embedding dim.
-
(qkv_bias¶bool, default:True) –If True, add a learnable bias to query, key, value.
-
(norm_layer¶Module, default:LayerNorm) –Normalization layer.
-
(act_layer¶Module, default:GELU) –Activation layer.
-
(use_abs_pos¶bool, default:True) –If True, use absolute positional embeddings.
-
(use_rel_pos¶bool, default:False) –If True, add relative positional embeddings to the attention map.
-
(rel_pos_zero_init¶bool, default:True) –If True, zero initialize relative positional parameters.
-
(window_size¶int, default:0) –Window size for window attention blocks.
-
(global_attn_indexes¶list, default:()) –Indexes for blocks using global attention.
Source code in vllm/model_executor/models/deepencoder.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 | |
PatchEmbed ¶
Bases: Module
Image to Patch Embedding.
Methods:
-
__init__–Args:
Source code in vllm/model_executor/models/deepencoder.py
__init__(kernel_size=(16, 16), stride=(16, 16), padding=(0, 0), in_chans=3, embed_dim=768) ¶
Parameters:
-
(kernel_size¶Tuple, default:(16, 16)) –kernel size of the projection layer.
-
(stride¶Tuple, default:(16, 16)) –stride of the projection layer.
-
(padding¶Tuple, default:(0, 0)) –padding size of the projection layer.
-
(in_chans¶int, default:3) –Number of input image channels.
-
(embed_dim¶int, default:768) –Patch embedding dimension.
Source code in vllm/model_executor/models/deepencoder.py
RelPosAttention ¶
Bases: PluggableLayer
Multi-head Attention block with relative position embeddings.
Methods:
-
__init__–Args:
Source code in vllm/model_executor/models/deepencoder.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
__init__(dim, num_heads=8, qkv_bias=True, use_rel_pos=False, use_triton_attention=False, rel_pos_zero_init=True, input_size=None) ¶
Parameters:
-
(dim¶int) –Number of input channels.
-
(num_heads¶int, default:8) –Number of attention heads.
-
(qkv_bias¶bool, default:True) –If True, add a learnable bias to query, key, value.
-
(use_triton_attention¶bool, default:False) –If True, fuse relative position bias in a Triton attention kernel on CUDA-alike platforms.
-
(rel_pos_zero_init¶bool, default:True) –If True, zero initialize relative positional parameters.
-
(input_size¶tuple(int, int) or None, default:None) –Input resolution for calculating the relative positional parameter size.
Source code in vllm/model_executor/models/deepencoder.py
add_decomposed_rel_pos(q, rel_pos_h, rel_pos_w, q_size, k_size) ¶
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:
-
attn(Tensor) –attention map with added relative positional embeddings.
Source code in vllm/model_executor/models/deepencoder.py
deepencoder_rel_pos_attention(q, k, v, rel_h, rel_w, width, scale) ¶
Run DeepEncoder global attention with fused relative-position bias.
Source code in vllm/model_executor/models/deepencoder.py
get_rel_pos(q_size, k_size, 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:
-
Tensor–Extracted positional embeddings according to relative positions.
Source code in vllm/model_executor/models/deepencoder.py
window_partition(x, window_size) ¶
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:
-
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(windows, window_size, pad_hw, hw) ¶
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:
-
x(Tensor) –unpartitioned sequences with [B, H, W, C].