vllm.model_executor.models.moonvit
VL_VISION_ATTENTION_FUNCTIONS
module-attribute
¶
VL_VISION_ATTENTION_FUNCTIONS = {
"flash_attention_2": multihead_attention,
"sdpa": sdpa_attention,
}
Learnable2DInterpPosEmb
¶
Bases: Module
Source code in vllm/model_executor/models/moonvit.py
__init__
¶
Source code in vllm/model_executor/models/moonvit.py
forward
¶
Source code in vllm/model_executor/models/moonvit.py
MLP2
¶
Bases: Module
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dims
|
list[int]
|
[in_dim, hidden_dim, out_dim] |
required |
bias
|
whether to use bias in linear layer. |
True
|
Source code in vllm/model_executor/models/moonvit.py
__init__
¶
Source code in vllm/model_executor/models/moonvit.py
MoonVisionPatchEmbed
¶
Bases: Module
Source code in vllm/model_executor/models/moonvit.py
pos_emb
instance-attribute
¶
pos_emb = Learnable2DInterpPosEmb(
height=pos_emb_height, width=pos_emb_width, dim=out_dim
)
proj
instance-attribute
¶
proj = Conv2d(
in_dim,
out_dim,
kernel_size=patch_size,
stride=patch_size,
)
__init__
¶
__init__(
out_dim: int,
in_dim: int = 3,
patch_size: Union[int, tuple[int, int]] = (14, 14),
pos_emb_height: int = 14,
pos_emb_width: int = 14,
)
Source code in vllm/model_executor/models/moonvit.py
MoonVitEncoder
¶
Bases: Module
Source code in vllm/model_executor/models/moonvit.py
blocks
instance-attribute
¶
blocks = ModuleList(
[
MoonVitEncoderLayer(**block_cfg)
for _ in range(num_layers)
]
)
rope_2d
instance-attribute
¶
rope_2d = Rope2DPosEmb(
block_cfg["hidden_dim"] // block_cfg["num_heads"],
512,
512,
)
__init__
¶
Source code in vllm/model_executor/models/moonvit.py
forward
¶
Source code in vllm/model_executor/models/moonvit.py
MoonVitEncoderLayer
¶
Bases: Module
Source code in vllm/model_executor/models/moonvit.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 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 |
|
hidden_size_per_attention_head
instance-attribute
¶
__init__
¶
__init__(
num_heads: int,
hidden_dim: int,
mlp_dim: int,
*,
attn_implementation: str = "sdpa",
activation=gelu,
attn_bias: bool = False,
)
Source code in vllm/model_executor/models/moonvit.py
attention_qkvpacked
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
(batch_size, seqlen, hidden_dim) |
required |
cu_seqlens
|
Tensor
|
|
required |
Source code in vllm/model_executor/models/moonvit.py
forward
¶
forward(
hidden_states: Tensor,
cu_seqlens: Tensor,
rope_freqs_cis: Union[Tensor, None] = None,
) -> Tensor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_states
|
Tensor
|
non-packed (B, N, D) or packed (L, D). if non-packed, seqlens should be None, if packed, seqlens should be set |
required |
Returns:
Name | Type | Description |
---|---|---|
output |
Tensor
|
same shape of input, non-packed (B, N, D) for non-packed input, (L, D) for packed input |
Source code in vllm/model_executor/models/moonvit.py
MoonVitPretrainedModel
¶
Bases: PreTrainedModel
Source code in vllm/model_executor/models/moonvit.py
encoder
instance-attribute
¶
encoder = MoonVitEncoder(
hidden_dim=hidden_size,
num_layers=num_hidden_layers,
block_cfg={
"num_heads": num_attention_heads,
"hidden_dim": hidden_size,
"mlp_dim": intermediate_size,
"activation": PytorchGELUTanh(),
"attn_bias": True,
"attn_implementation": _attn_implementation,
},
)
patch_embed
instance-attribute
¶
patch_embed = MoonVisionPatchEmbed(
out_dim=hidden_size,
patch_size=patch_size,
pos_emb_height=init_pos_emb_height,
pos_emb_width=init_pos_emb_width,
)
__init__
¶
__init__(config: MoonViTConfig, *inputs, **kwargs)
Source code in vllm/model_executor/models/moonvit.py
forward
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pixel_values
|
Tensor
|
The input pixel values. |
required |
grid_hw
|
Tensor
|
The grid height and width. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The output tokens. |
Source code in vllm/model_executor/models/moonvit.py
MoonVitVLProjector
¶
Bases: Module
Source code in vllm/model_executor/models/moonvit.py
hidden_size
instance-attribute
¶
__init__
¶
__init__(
in_channels: int,
merge_kernel_size: list[int, int],
hidden_act: str = "gelu",
ln_eps: float = 1e-05,
out_dim: int = 4096,
)
Source code in vllm/model_executor/models/moonvit.py
forward
¶
Source code in vllm/model_executor/models/moonvit.py
Rope2DPosEmb
¶
Bases: Module
2D rotary position embedding with multi-resolution support.
This class is intended to be used in the following way:
1. Before training, create an instance of Rope2DPosEmb. This instance will hold the precomputed cis.
2. Before each forward pass, call get_freqs_cis_by_*
to get the freqs_cis
tensor for this iteration.
3. During the forward pass, pass the freqs_cis
tensor to each attention layer, and call apply
just before each attention operation.
The rope is shared across all attention layers and all heads.
Refs: - RoFormer: https://arxiv.org/abs/2104.09864 - VisionLLaMA: https://arxiv.org/abs/2403.00522 - https://github.com/Meituan-AutoML/VisionLLaMA/blob/main/dit/models.py
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dim
|
int
|
usually the multi-head attention dimension, should be divisible by 4 (TODO: relax this constraint if needed) |
required |
max_height
|
int
|
the maximum height of the 2D grid |
required |
max_width
|
int
|
the maximum width of the 2D grid |
required |
theta_base
|
float
|
the base of the theta |
10000
|
device
|
str
|
the device to store the precomputed cis |
'cuda'
|
Source code in vllm/model_executor/models/moonvit.py
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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
precomputed_freqs_cis
cached
property
¶
precomputed_freqs_cis: Tensor
Calculate the cis(freqs) for each position in the 2D grid.
complex tensor of shape (max_height, max_width, dim//2) and value:
height axis: ret[h, w, 2i] = cis(h * theta_base(-4i/dim))
weight axis: ret[h, w, 2i+1] = cis(w * theta_base(-4i/dim)) with (i in [0, dim//4))
note: cis
is a mathematical notation defined by cis x = cos x + i sin x,
__init__
¶
Source code in vllm/model_executor/models/moonvit.py
extra_repr
¶
get_freqs_cis_by_idx
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos_idx
|
Tensor
|
tensor of shape (..., 2), It contains the (h, w) position indices of each 2D token. |
required |
pos_idx_mask
|
Tensor
|
a mask of shape (...), the leading dimensions should be the same as pos_idx.
Rope will only be applied to the tokens with True mask. |
required |
Return: freqs_cis: tensor of shape (..., dim//2)
Source code in vllm/model_executor/models/moonvit.py
get_freqs_cis_by_seqlens
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
grid_hws
|
Tensor
|
containing list of (height, width) or (t, height, width) tuples. |
required |
Returns: freqs_cis: tensor of shape (sum(t * height * width), dim//2)
Source code in vllm/model_executor/models/moonvit.py
_apply_rope_input_validation
¶
Source code in vllm/model_executor/models/moonvit.py
apply_rope
¶
(The leading dimensions of all inputs should be the same)
Name | Type | Description | Default |
---|---|---|---|
xq
|
Tensor
|
query, tensor of shape (..., num_heads, head_dim) |
required |
xk
|
Tensor
|
key, tensor of shape (..., num_heads, head_dim) |
required |
freqs_cis
|
Tensor
|
tensor of shape (..., head_dim/2), dtype=torch.complex64. It contains the precomputed cis(freqs) for each position in the 2D grid. |
required |
Returns: xq_out, xk_out: tensors of shape (..., num_heads, head_dim)
Source code in vllm/model_executor/models/moonvit.py
multihead_attention
¶
multihead_attention(
q: Tensor,
k: Tensor,
v: Tensor,
q_cu_seqlens: Optional[Tensor] = None,
k_cu_seqlens: Optional[Tensor] = None,
)
Multi-head attention using flash attention 2.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q,
|
(k, v)
|
tensor of shape (batch_size, seqlen, num_heads, head_dim), or (tot_seqlens, num_heads, head_dim) if packing. |
required |
q_cu_seqlens
|
Tensor
|
cumulative sequence lengths of q. The first element should be 0 and the last element should be q.shape[0]. |
None
|
k_cu_seqlens
|
Tensor
|
cumulative sequence lengths of k. The first element should be 0 and the last element should be k.shape[0]. |
None
|
Returns:
Name | Type | Description |
---|---|---|
output |
shape (batch_size, seqlen, dim) or (tot_seqlens, dim) if packing, where dim = num_heads * head_dim |
Source code in vllm/model_executor/models/moonvit.py
patch_merger
¶
patch_merger(
x: Tensor,
grid_hw: Tensor,
merge_kernel_size: list[int, int] = (2, 2),
) -> list[Tensor]
Source code in vllm/model_executor/models/moonvit.py
sdpa_attention
¶
sdpa_attention(
q: Tensor,
k: Tensor,
v: Tensor,
q_cu_seqlens: Optional[Tensor] = None,
k_cu_seqlens: Optional[Tensor] = None,
) -> Tensor
SDPA attention.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q,
|
(k, v)
|
tensor of shape (batch_size, seqlen, num_heads, head_dim), or (tot_seqlens, num_heads, head_dim) if packing. |
required |