vllm.model_executor.models.zamba2
PyTorch Zamba2 model implementation for vLLM.
This module implements the Zamba2 architecture from https://arxiv.org/abs/2411.15242, which combines Mamba and Transformer architectures in a hybrid model optimized for efficient sequence modeling. The model alternates between state space model layers and attention-based layers.
Zamba2Attention
¶
Bases: Module
Multi-head attention mechanism for the Zamba2 model.
Implements attention with parallel computation, QKV projections, optional adapters and rotary position embeddings. The attention is computed across distributed blocks for efficient processing.
Source code in vllm/model_executor/models/zamba2.py
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 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 270 271 |
|
o_proj
instance-attribute
¶
o_proj = RowParallelLinear(
attention_hidden_size,
hidden_size,
bias=False,
quant_config=quant_config,
)
qkv_proj
instance-attribute
¶
qkv_proj = QKVParallelLinear(
attention_hidden_size,
attention_head_dim,
total_num_attention_heads,
bias=False,
quant_config=quant_config,
)
rotary_emb
instance-attribute
¶
rotary_emb = get_rope(
head_size=attention_head_dim,
rotary_dim=attention_head_dim,
max_position=max_position_embeddings,
base=rope_theta,
rope_scaling=None,
is_neox_style=True,
)
__init__
¶
__init__(
config: Zamba2Config,
bare_block_idx: int,
num_hybrid_layers: int,
cache_config: Optional[CacheConfig] = None,
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
) -> None
Initialize the attention layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Zamba2Config
|
The Zamba2 model configuration |
required |
bare_block_idx
|
int
|
Index of the bare attention block |
required |
num_hybrid_layers
|
int
|
Total number of hybrid layers |
required |
cache_config
|
Optional[CacheConfig]
|
Configuration for key-value caching |
None
|
quant_config
|
Optional[QuantizationConfig]
|
Configuration for model quantization |
None
|
prefix
|
str
|
Optional prefix for parameter names |
''
|
Source code in vllm/model_executor/models/zamba2.py
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 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 |
|
forward
¶
Forward pass through the attention layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_states
|
Tensor
|
Input tensor [batch_size, seq_len, hidden_size] |
required |
position_ids
|
Tensor
|
Position IDs for positional embeddings |
required |
block_idx
|
int
|
Current shared transformer block index |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor [batch_size, seq_len, hidden_size] |
Source code in vllm/model_executor/models/zamba2.py
Zamba2AttentionDecoderLayer
¶
Bases: Module
Single decoder layer combining attention and feed-forward networks.
This layer implements a standard transformer block with: - Input layer normalization - Multi-head self-attention - Pre-feed-forward layer normalization - Feed-forward network (MLP)
Source code in vllm/model_executor/models/zamba2.py
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 |
|
feed_forward
instance-attribute
¶
feed_forward = Zamba2MLP(
config,
bare_block_idx=bare_block_idx,
num_hybrid_layers=num_hybrid_layers,
quant_config=quant_config,
)
self_attn
instance-attribute
¶
self_attn = Zamba2Attention(
config,
bare_block_idx=bare_block_idx,
num_hybrid_layers=num_hybrid_layers,
cache_config=cache_config,
quant_config=quant_config,
prefix=prefix,
)
__init__
¶
__init__(
config: Zamba2Config,
bare_block_idx: int,
num_hybrid_layers: int,
cache_config: Optional[CacheConfig] = None,
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
) -> None
Initialize the decoder layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Zamba2Config
|
The Zamba2 model configuration |
required |
bare_block_idx
|
int
|
Index of the bare block |
required |
num_hybrid_layers
|
int
|
Total number of hybrid layers |
required |
cache_config
|
Optional[CacheConfig]
|
Configuration for key-value caching |
None
|
quant_config
|
Optional[QuantizationConfig]
|
Configuration for model quantization |
None
|
prefix
|
str
|
Optional prefix for parameter names |
''
|
Source code in vllm/model_executor/models/zamba2.py
forward
¶
forward(
hidden_states: Tensor,
original_hidden_states: Tensor,
block_idx: int,
positions: Tensor,
) -> Tensor
Forward pass through the decoder layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_states
|
Tensor
|
Input tensor from previous layer |
required |
original_hidden_states
|
Tensor
|
Original input tensor for residual connection |
required |
block_idx
|
int
|
Current shared transformer block index |
required |
positions
|
Tensor
|
IDs for positional embeddings |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Transformed hidden states after attention and feed-forward |
Source code in vllm/model_executor/models/zamba2.py
Zamba2ForCausalLM
¶
Bases: Module
, HasInnerState
, IsHybrid
Zamba2 model with causal language modeling head.
This class wraps the core Zamba2 model and adds: - A language modeling head for next token prediction - Mamba state caching functionality - Support for model parallelism and quantization - Sampling capabilities for text generation
Source code in vllm/model_executor/models/zamba2.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 |
|
hf_to_vllm_mapper
class-attribute
instance-attribute
¶
hf_to_vllm_mapper = WeightsMapper(
orig_to_new_substr={
"A_log": "A",
"0.weight": "A.weight",
"1.weight": "B.weight",
}
)
logits_processor
instance-attribute
¶
logits_processor = LogitsProcessor(
unpadded_vocab_size, vocab_size
)
model
instance-attribute
¶
model = Zamba2Model(
vllm_config=vllm_config,
prefix=maybe_prefix(prefix, "model"),
)
__init__
¶
__init__(
*, vllm_config: VllmConfig, prefix: str = ""
) -> None
Initialize the Zamba2 model for causal language modeling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vllm_config
|
VllmConfig
|
Configuration containing model, cache, quantization, LoRA and scheduler settings |
required |
prefix
|
str
|
Optional prefix for parameter names |
''
|
Raises:
Type | Description |
---|---|
AssertionError
|
If prefix caching is enabled (not supported by |
Source code in vllm/model_executor/models/zamba2.py
_get_mamba_cache_shape
¶
Calculate shapes for Mamba's convolutional and state caches.
Returns:
Type | Description |
---|---|
tuple[int, int]
|
Tuple containing: |
tuple[int, int]
|
|
tuple[tuple[int, int], tuple[int, int]]
|
|
Source code in vllm/model_executor/models/zamba2.py
compute_logits
¶
compute_logits(
hidden_states: Tensor,
sampling_metadata: SamplingMetadata,
) -> Optional[Tensor]
Compute logits for next token prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_states
|
Tensor
|
Hidden states from model forward pass |
required |
sampling_metadata
|
SamplingMetadata
|
Metadata for sampling process |
required |
Returns:
Type | Description |
---|---|
Optional[Tensor]
|
Logits for next token prediction |
Source code in vllm/model_executor/models/zamba2.py
copy_inputs_before_cuda_graphs
¶
Copy inputs before CUDA graph capture.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_buffers
|
dict[str, Tensor]
|
Dictionary of input tensors |
required |
**kwargs
|
Additional arguments passed to cache manager |
{}
|
Returns:
Type | Description |
---|---|
dict[str, Tensor]
|
Updated input buffers |
Source code in vllm/model_executor/models/zamba2.py
forward
¶
forward(
input_ids: Tensor,
positions: Tensor,
inputs_embeds: Optional[Tensor] = None,
**kwargs,
) -> Tensor
Forward pass through the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
Tensor
|
Input token IDs |
required |
positions
|
Tensor
|
Position IDs for embeddings |
required |
inputs_embeds
|
Optional[Tensor]
|
Optional pre-computed input embeddings |
None
|
**kwargs
|
Additional arguments passed to cache manager |
{}
|
Returns:
Type | Description |
---|---|
Tensor
|
Output hidden states |
Source code in vllm/model_executor/models/zamba2.py
get_input_embeddings
¶
Convert input token IDs to embeddings. Args: input_ids: Tensor of input token IDs Returns: Embedded representation of the input tokens
Source code in vllm/model_executor/models/zamba2.py
get_seqlen_agnostic_capture_inputs
¶
Get inputs for sequence-length-agnostic graph capture.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size
|
int
|
Size of batch to capture |
required |
Returns: Dictionary of capture inputs
Source code in vllm/model_executor/models/zamba2.py
load_weights
¶
Zamba2HybridLayer
¶
Bases: Module
Hybrid layer combining Transformer and Mamba architectures.
This layer implements the hybrid architecture described in the Zamba paper, where a shared transformer pathway processes input in parallel with a Mamba pathway. The transformer output is projected and added to the Mamba input for enhanced representation learning.
Source code in vllm/model_executor/models/zamba2.py
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 |
|
linear
instance-attribute
¶
linear = ReplicatedLinear(
hidden_size,
hidden_size,
bias=False,
quant_config=quant_config,
)
mamba_decoder
instance-attribute
¶
mamba_decoder = Zamba2MambaDecoderLayer(
config, quant_config=quant_config, prefix=prefix
)
__init__
¶
__init__(
shared_transformer: Zamba2AttentionDecoderLayer,
config: Zamba2Config,
block_idx: int,
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
) -> None
Initialize the hybrid layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shared_transformer
|
Zamba2AttentionDecoderLayer
|
Transformer decoder layer for attention pathway |
required |
linear
|
Linear projection for transformer output before Mamba |
required | |
mamba
|
Mamba decoder layer for state space pathway |
required |
Source code in vllm/model_executor/models/zamba2.py
forward
¶
forward(
hidden_states: Tensor,
original_hidden_states: Tensor,
positions: Tensor,
mamba_cache_params: MambaCacheParams,
mamba2_metadata: Mamba2Metadata,
) -> Tensor
Forward pass through the hybrid layer.
Processes input through parallel transformer and Mamba paths: 1. Transformer path processes input with attention 2. Transformer output is projected to match hidden size 3. Projected output is added to Mamba path input 4. Final output combines both paths' representations
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_states
|
Tensor
|
Input tensor [batch_size, seq_len, hidden_size] |
required |
original_hidden_states
|
Tensor
|
Original input for transformer residual connection |
required |
positions
|
Tensor
|
Position IDs for positional embeddings |
required |
mamba_cache_params
|
MambaCacheParams
|
Parameters for Mamba's state caches (one for conv, one for ssm) |
required |
sequence_idx
|
Indices for identifying sequences in batch, required for proper chunked processing in prefill |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor combining transformer and Mamba representations |
Source code in vllm/model_executor/models/zamba2.py
Zamba2LoRA
¶
Bases: Module
LoRA layer for the Zamba2 model.
Implements a LoRA layer that is used in shared attention and gated MLP blocks.
Source code in vllm/model_executor/models/zamba2.py
A
instance-attribute
¶
A = ColumnParallelLinear(
input_dim,
rank,
bias=False,
quant_config=quant_config,
gather_output=True,
)
__init__
¶
__init__(
input_dim: int,
rank: int,
output_dim: Union[int, list[int]],
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
)
Initialize the attention layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim
|
int
|
input dimension |
required |
rank
|
int
|
LoRA rank |
required |
output_dim
|
Union[int, list[int]]
|
output dimension |
required |
quant_config
|
Optional[QuantizationConfig]
|
Configuration for model quantization |
None
|
Source code in vllm/model_executor/models/zamba2.py
Zamba2MLP
¶
Bases: Module
Feed-forward MLP layer for the Zamba2 model.
Implements a gated feed-forward network that projects inputs to a larger intermediate size, applies GELU activation with gating, then projects back to the original size. Includes optional adapter layers for model adaptation.
Source code in vllm/model_executor/models/zamba2.py
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 |
|
down_proj
instance-attribute
¶
down_proj = RowParallelLinear(
intermediate_size,
hidden_size,
bias=add_bias_linear,
quant_config=quant_config,
)
gate_up_proj
instance-attribute
¶
gate_up_proj = MergedColumnParallelLinear(
hidden_size,
2 * [intermediate_size],
bias=add_bias_linear,
quant_config=quant_config,
)
__init__
¶
__init__(
config: Zamba2Config,
bare_block_idx: int,
num_hybrid_layers: dict[int, int],
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
) -> None
Initialize the MLP layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Zamba2Config
|
The Zamba2 model configuration |
required |
bare_block_idx
|
int
|
Index of the bare block in the model |
required |
num_hybrid_layers
|
dict[int, int]
|
Total number of hybrid layers |
required |
quant_config
|
Optional[QuantizationConfig]
|
Configuration for model quantization |
None
|
Source code in vllm/model_executor/models/zamba2.py
forward
¶
Forward pass through the MLP layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_states
|
Tensor
|
Input tensor [batch_size, seq_len, hidden_size] |
required |
block_idx
|
int
|
Current shared transformer block index |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor [batch_size, seq_len, hidden_size] after applying |
Tensor
|
gated feed-forward transformation |
Source code in vllm/model_executor/models/zamba2.py
Zamba2MambaDecoderLayer
¶
Bases: Module
Single Mamba decoder layer with normalization.
This implements a Mamba block. It includes input normalization and can process sequences using either chunked or full computation depending on configuration.
Source code in vllm/model_executor/models/zamba2.py
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 |
|
mamba
instance-attribute
¶
mamba = MambaMixer2(
hidden_size=hidden_size,
ssm_state_size=mamba_d_state,
conv_kernel_size=mamba_d_conv,
intermediate_size=intermediate_size,
use_conv_bias=use_conv_bias,
use_bias=add_bias_linear,
n_groups=mamba_ngroups,
num_heads=n_mamba_heads,
head_dim=intermediate_size // n_mamba_heads,
rms_norm_eps=rms_norm_eps,
activation="silu",
quant_config=quant_config,
prefix=f"{prefix}.mixer",
chunk_size=chunk_size,
)
__init__
¶
__init__(
config: Zamba2Config,
quant_config: Optional[QuantizationConfig] = None,
prefix: str = "",
) -> None
Initialize the Mamba decoder layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Zamba2Config
|
The Zamba2 model configuration |
required |
quant_config
|
Optional[QuantizationConfig]
|
Configuration for model quantization |
None
|
Source code in vllm/model_executor/models/zamba2.py
forward
¶
forward(
hidden_states: Tensor,
mamba_cache_params: MambaCacheParams,
mamba2_metadata: Mamba2Metadata,
transformer_hidden_states: Optional[Tensor] = None,
positions: Optional[Tensor] = None,
original_hidden_states: Optional[Tensor] = None,
) -> Tensor
Forward pass through the Mamba decoder layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_states
|
Tensor
|
Input tensor [batch_size, seq_len, hidden_size] |
required |
mamba_cache_params
|
MambaCacheParams
|
Parameters for Mamba's state caches (one for conv, one for ssm) |
required |
sequence_idx
|
Index tensor for identifying sequences in batch Required for proper chunked processing in prefill |
required | |
transformer_hidden_states
|
Optional[Tensor]
|
Optional output from transformer path Added to input if provided (used in hybrid architecture) |
None
|
positions
|
Optional[Tensor]
|
Optional position IDs (unused in Mamba) |
None
|
original_hidden_states
|
Optional[Tensor]
|
Optional original inputs (unused in Mamba) |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Transformed hidden states with residual connection applied |
Source code in vllm/model_executor/models/zamba2.py
Zamba2Model
¶
Bases: Module
Core Zamba2 model combining transformer and Mamba architectures.
The model processes input through a sequence of hybrid and Mamba-only layers, using token embeddings and final layer normalization.
Source code in vllm/model_executor/models/zamba2.py
650 651 652 653 654 655 656 657 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 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 |
|
embed_tokens
instance-attribute
¶
embed_tokens = VocabParallelEmbedding(
vocab_size, hidden_size, org_num_embeddings=vocab_size
)
__init__
¶
__init__(
*, vllm_config: VllmConfig, prefix: str = ""
) -> None
Initialize the Zamba2 model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vllm_config
|
VllmConfig
|
Configuration object containing model, cache, quantization and LoRA settings |
required |
prefix
|
str
|
Optional prefix for parameter names in state dict |
''
|
Source code in vllm/model_executor/models/zamba2.py
forward
¶
forward(
input_ids: Tensor,
positions: Tensor,
mamba_cache_params: MambaCacheParams,
inputs_embeds: Optional[Tensor] = None,
) -> Union[Tensor, IntermediateTensors]
Forward pass through the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
Tensor
|
Input token IDs |
required |
positions
|
Tensor
|
Position IDs for embeddings |
required |
mamba_cache_params
|
MambaCacheParams
|
Parameters for Mamba's state caches (one for conv, one for ssm) |
required |
inputs_embeds
|
Optional[Tensor]
|
Optional pre-computed input embeddings |
None
|
Returns:
Type | Description |
---|---|
Union[Tensor, IntermediateTensors]
|
Either final hidden states or intermediate tensors for pipeline |
Union[Tensor, IntermediateTensors]
|
parallelism |