vllm.model_executor.models.llama4 ΒΆ
Inference-only LLaMA model compatible with HuggingFace weights.
Llama4Attention ΒΆ
Bases: Module
Source code in vllm/model_executor/models/llama4.py
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 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 |
|
attn instance-attribute
ΒΆ
attn = attn_cls(
num_heads,
head_dim,
scaling,
num_kv_heads=num_kv_heads,
cache_config=cache_config,
quant_config=quant_config,
prefix=f"{prefix}.attn",
**(
{"attention_chunk_size": attention_chunk_size}
if use_chunked_local_attn
else {}
),
)
attn_temperature_tuning instance-attribute
ΒΆ
o_proj instance-attribute
ΒΆ
o_proj = RowParallelLinear(
input_size=total_num_heads * head_dim,
output_size=hidden_size,
bias=bias_o_proj,
quant_config=quant_config,
prefix=f"{prefix}.o_proj",
)
qk_norm instance-attribute
ΒΆ
qk_norm = (
RMSNorm(
hidden_size=head_dim,
eps=rms_norm_eps,
has_weight=False,
dtype=float32,
)
if use_qk_norm
else None
)
qkv_proj instance-attribute
ΒΆ
qkv_proj = QKVParallelLinear(
hidden_size=hidden_size,
head_size=head_dim,
total_num_heads=total_num_heads,
total_num_kv_heads=total_num_kv_heads,
bias=bias,
quant_config=quant_config,
prefix=f"{prefix}.qkv_proj",
)
rotary_emb instance-attribute
ΒΆ
rotary_emb = (
get_rope(
head_dim,
rotary_dim=head_dim,
max_position=max_position_embeddings,
base=int(rope_theta),
rope_scaling=rope_scaling
if rope_scaling != "default"
else None,
is_neox_style=is_neox_style,
)
if not nope
else None
)
__init__ ΒΆ
__init__(
config: Llama4TextConfig,
hidden_size: int,
num_heads: int,
num_kv_heads: int,
rope_theta: float = 10000,
rope_scaling: Optional[dict[str, Any]] = None,
max_position_embeddings: int = 8192,
quant_config: Optional[QuantizationConfig] = None,
bias: bool = False,
bias_o_proj: bool = False,
cache_config: Optional[CacheConfig] = None,
prefix: str = "",
) -> None
Source code in vllm/model_executor/models/llama4.py
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
_get_attn_scale ΒΆ
forward ΒΆ
Source code in vllm/model_executor/models/llama4.py
Llama4DecoderLayer ΒΆ
Bases: Module
Source code in vllm/model_executor/models/llama4.py
feed_forward instance-attribute
ΒΆ
feed_forward = Llama4MoE(
vllm_config=vllm_config, prefix=f"{prefix}.feed_forward"
)
post_attention_layernorm instance-attribute
ΒΆ
post_attention_layernorm = RMSNorm(
hidden_size, eps=rms_norm_eps
)
self_attn instance-attribute
ΒΆ
self_attn = Llama4Attention(
config=config,
hidden_size=hidden_size,
num_heads=num_attention_heads,
num_kv_heads=num_key_value_heads,
rope_theta=rope_theta,
rope_scaling=rope_scaling,
max_position_embeddings=max_position_embeddings,
quant_config=quant_config,
bias=False,
bias_o_proj=False,
cache_config=cache_config,
prefix=f"{prefix}.self_attn",
)
__init__ ΒΆ
__init__(
vllm_config: VllmConfig,
prefix: str = "",
config: Optional[Llama4TextConfig] = None,
) -> None
Source code in vllm/model_executor/models/llama4.py
forward ΒΆ
forward(
positions: Tensor,
hidden_states: Tensor,
residual: Optional[Tensor],
) -> tuple[Tensor, Tensor]
Source code in vllm/model_executor/models/llama4.py
Llama4ForCausalLM ΒΆ
Bases: LlamaForCausalLM
Source code in vllm/model_executor/models/llama4.py
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 |
|
packed_modules_mapping class-attribute
instance-attribute
ΒΆ
packed_modules_mapping = {
"qkv_proj": ["q_proj", "k_proj", "v_proj"],
"gate_up_proj": ["gate_proj", "up_proj"],
}
__init__ ΒΆ
__init__(*, vllm_config: VllmConfig, prefix: str = '')
Source code in vllm/model_executor/models/llama4.py
_init_model ΒΆ
_init_model(
vllm_config: VllmConfig,
prefix: str = "",
layer_type: type[
Llama4DecoderLayer
] = Llama4DecoderLayer,
)
load_weights ΒΆ
Source code in vllm/model_executor/models/llama4.py
permute_qk_weight_for_rotary ΒΆ
Source code in vllm/model_executor/models/llama4.py
Llama4MoE ΒΆ
Bases: Module
Source code in vllm/model_executor/models/llama4.py
experts instance-attribute
ΒΆ
experts = SharedFusedMoE(
shared_experts=shared_expert,
num_experts=num_local_experts,
top_k=num_experts_per_tok,
hidden_size=hidden_size,
custom_routing_function=custom_routing_function,
intermediate_size=intermediate_size_moe,
apply_router_weight_on_input=True,
reduce_results=False,
renormalize=False,
quant_config=quant_config,
prefix=f"{prefix}.experts",
is_sequence_parallel=is_sequence_parallel,
)
router instance-attribute
ΒΆ
router = ReplicatedLinear(
hidden_size,
num_local_experts,
bias=False,
quant_config=None,
prefix=f"{prefix}.router",
)
shared_expert instance-attribute
ΒΆ
shared_expert = LlamaMLP(
hidden_size=hidden_size,
intermediate_size=intermediate_size_moe,
hidden_act="silu",
quant_config=quant_config,
bias=False,
prefix=f"{prefix}.shared_expert",
reduce_results=False,
disable_tp=is_sequence_parallel,
)
__init__ ΒΆ
__init__(vllm_config: VllmConfig, prefix: str = '')
Source code in vllm/model_executor/models/llama4.py
custom_routing_function staticmethod
ΒΆ
custom_routing_function(
hidden_states: Tensor,
gating_output: Tensor,
topk: int,
renormalize: bool,
) -> tuple[Tensor, Tensor]
Source code in vllm/model_executor/models/llama4.py
forward ΒΆ
Source code in vllm/model_executor/models/llama4.py
Llama4Model ΒΆ
Bases: LlamaModel
Source code in vllm/model_executor/models/llama4.py
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 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 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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
|
__init__ ΒΆ
__init__(
*,
vllm_config: VllmConfig,
prefix: str = "",
layer_type: type[
Llama4DecoderLayer
] = Llama4DecoderLayer,
)
Source code in vllm/model_executor/models/llama4.py
load_moe_expert_weights ΒΆ
load_moe_expert_weights(
name: str,
loaded_weight: Tensor,
params_dict: dict[str, Parameter],
loaded_params: set[str],
expert_params_mapping: list[tuple[str, str, int, str]],
fused: bool = True,
) -> bool
Load MoE expert weights.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name | str | The name of the weight to load. | required |
loaded_weight | Tensor | The weight to load. | required |
params_dict | dict[str, Parameter] | The dictionary of module parameters. | required |
loaded_params | set[str] | The set of already loaded parameters. | required |
expert_params_mapping | list[tuple[str, str, int, str]] | The mapping of expert parameters. Must be generated by FusedMoE.make_expert_params_mapping(). | required |
fused | bool | Whether the expert weights are fused into a single weight tensor or are separate weight tensors for each expert. When fused is True, loaded_weight should have shape of: [num_experts, hidden_in, hidden_out] for gate/up/down proj and [hidden_out, hidden_in] for the others like router. When fused is False, loaded_weight should have shape of: [hidden_out, hidden_in]. | True |
Returns:
Type | Description |
---|---|
bool | True if loaded_weight is one of MoE weights and the MoE expert |
bool | weights are loaded successfully, False otherwise. |
Source code in vllm/model_executor/models/llama4.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 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 |
|
load_weights ΒΆ
Source code in vllm/model_executor/models/llama4.py
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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
|