[V1] Remove V0 code paths for Hybrid models (#25400)
Signed-off-by: Thomas Parnell <tpa@zurich.ibm.com>
This commit is contained in:
@@ -15,12 +15,10 @@ import torch
|
||||
from torch import nn
|
||||
from transformers import Zamba2Config
|
||||
|
||||
from vllm import envs
|
||||
from vllm.attention.layer import Attention
|
||||
from vllm.compilation.decorators import support_torch_compile
|
||||
from vllm.config import CacheConfig, ModelConfig, VllmConfig
|
||||
from vllm.distributed import get_tensor_model_parallel_world_size
|
||||
from vllm.forward_context import get_forward_context
|
||||
from vllm.model_executor.layers.activation import GeluAndMul
|
||||
from vllm.model_executor.layers.layernorm import RMSNorm
|
||||
from vllm.model_executor.layers.linear import (ColumnParallelLinear,
|
||||
@@ -29,8 +27,6 @@ from vllm.model_executor.layers.linear import (ColumnParallelLinear,
|
||||
ReplicatedLinear,
|
||||
RowParallelLinear)
|
||||
from vllm.model_executor.layers.logits_processor import LogitsProcessor
|
||||
from vllm.model_executor.layers.mamba.mamba2_metadata import (
|
||||
Mamba2Metadata, prepare_mamba2_metadata)
|
||||
from vllm.model_executor.layers.mamba.mamba_mixer2 import MambaMixer2
|
||||
from vllm.model_executor.layers.mamba.mamba_utils import (
|
||||
MambaStateDtypeCalculator, MambaStateShapeCalculator)
|
||||
@@ -39,8 +35,6 @@ from vllm.model_executor.layers.rotary_embedding import get_rope
|
||||
from vllm.model_executor.layers.vocab_parallel_embedding import (
|
||||
DEFAULT_VOCAB_PADDING_SIZE, ParallelLMHead, VocabParallelEmbedding)
|
||||
from vllm.model_executor.model_loader.weight_utils import default_weight_loader
|
||||
from vllm.model_executor.models.mamba_cache import (MambaCacheManager,
|
||||
MambaCacheParams)
|
||||
from vllm.sequence import IntermediateTensors
|
||||
|
||||
from .interfaces import HasInnerState, IsHybrid
|
||||
@@ -515,8 +509,6 @@ class Zamba2MambaDecoderLayer(nn.Module):
|
||||
def forward(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
mamba_cache_params: MambaCacheParams,
|
||||
mamba2_metadata: Mamba2Metadata,
|
||||
transformer_hidden_states: Optional[torch.Tensor] = None,
|
||||
positions: Optional[torch.Tensor] = None,
|
||||
original_hidden_states: Optional[torch.Tensor] = None,
|
||||
@@ -525,8 +517,6 @@ class Zamba2MambaDecoderLayer(nn.Module):
|
||||
|
||||
Args:
|
||||
hidden_states: Input tensor [batch_size, seq_len, hidden_size]
|
||||
mamba_cache_params: Parameters for Mamba's state caches
|
||||
(one for conv, one for ssm)
|
||||
transformer_hidden_states: Optional output from transformer path
|
||||
Added to input if provided (used in hybrid architecture)
|
||||
positions: Optional position IDs (unused in Mamba)
|
||||
@@ -555,8 +545,6 @@ class Zamba2MambaDecoderLayer(nn.Module):
|
||||
self.mamba(
|
||||
hidden_states,
|
||||
output,
|
||||
mamba_cache_params=mamba_cache_params,
|
||||
mamba2_metadata=mamba2_metadata,
|
||||
)
|
||||
|
||||
# residual connection after mamba
|
||||
@@ -607,8 +595,6 @@ class Zamba2HybridLayer(nn.Module):
|
||||
hidden_states: torch.Tensor,
|
||||
original_hidden_states: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
mamba_cache_params: MambaCacheParams,
|
||||
mamba2_metadata: Mamba2Metadata,
|
||||
) -> torch.Tensor:
|
||||
"""Forward pass through the hybrid layer.
|
||||
|
||||
@@ -623,8 +609,6 @@ class Zamba2HybridLayer(nn.Module):
|
||||
original_hidden_states: Original input for transformer residual
|
||||
connection
|
||||
positions: Position IDs for positional embeddings
|
||||
mamba_cache_params: Parameters for Mamba's state caches
|
||||
(one for conv, one for ssm)
|
||||
|
||||
Returns:
|
||||
Output tensor combining transformer and Mamba representations
|
||||
@@ -644,8 +628,6 @@ class Zamba2HybridLayer(nn.Module):
|
||||
layer_outputs = self.mamba_decoder(
|
||||
hidden_states,
|
||||
transformer_hidden_states=transformer_hidden_states,
|
||||
mamba_cache_params=mamba_cache_params,
|
||||
mamba2_metadata=mamba2_metadata,
|
||||
)
|
||||
|
||||
return layer_outputs
|
||||
@@ -752,7 +734,6 @@ class Zamba2Model(nn.Module):
|
||||
self,
|
||||
input_ids: torch.Tensor,
|
||||
positions: torch.Tensor,
|
||||
mamba_cache_params: MambaCacheParams,
|
||||
inputs_embeds: Optional[torch.Tensor] = None,
|
||||
) -> Union[torch.Tensor, IntermediateTensors]:
|
||||
"""Forward pass through the model.
|
||||
@@ -760,8 +741,6 @@ class Zamba2Model(nn.Module):
|
||||
Args:
|
||||
input_ids: Input token IDs
|
||||
positions: Position IDs for embeddings
|
||||
mamba_cache_params: Parameters for Mamba's state caches
|
||||
(one for conv, one for ssm)
|
||||
inputs_embeds: Optional pre-computed input embeddings
|
||||
|
||||
Returns:
|
||||
@@ -773,33 +752,13 @@ class Zamba2Model(nn.Module):
|
||||
inputs_embeds = self.get_input_embeddings(input_ids)
|
||||
hidden_states = inputs_embeds
|
||||
|
||||
attn_metadata = get_forward_context().attn_metadata
|
||||
|
||||
if not envs.VLLM_USE_V1:
|
||||
mamba2_metadata = prepare_mamba2_metadata(
|
||||
chunk_size=self.config.chunk_size,
|
||||
attn_metadata=attn_metadata,
|
||||
)
|
||||
else:
|
||||
# v1 get mamba2_metadata from forward_context
|
||||
mamba2_metadata = None
|
||||
|
||||
# Process through layers
|
||||
original_hidden_states = torch.clone(hidden_states)
|
||||
for layer_idx, layer in enumerate(self.layers):
|
||||
|
||||
layer_mamba_cache_params = None
|
||||
if (isinstance(layer, (Zamba2HybridLayer, Zamba2MambaDecoderLayer))
|
||||
and mamba_cache_params):
|
||||
layer_mamba_cache_params = mamba_cache_params.at_layer_idx(
|
||||
layer_idx)
|
||||
|
||||
layer_outputs = layer(
|
||||
hidden_states,
|
||||
original_hidden_states=original_hidden_states,
|
||||
positions=positions,
|
||||
mamba_cache_params=layer_mamba_cache_params,
|
||||
mamba2_metadata=mamba2_metadata,
|
||||
)
|
||||
hidden_states = layer_outputs
|
||||
|
||||
@@ -870,13 +829,11 @@ class Zamba2ForCausalLM(nn.Module, HasInnerState, IsHybrid):
|
||||
def get_mamba_state_shape_from_config(
|
||||
cls,
|
||||
vllm_config: "VllmConfig",
|
||||
use_v1: bool = True,
|
||||
) -> tuple[tuple[int, int], tuple[int, int, int]]:
|
||||
"""Calculate shapes for Mamba's convolutional and state caches.
|
||||
|
||||
Args:
|
||||
vllm_config: vLLM config
|
||||
use_v1: Get shapes for V1 (or V0)
|
||||
|
||||
Returns:
|
||||
Tuple containing:
|
||||
@@ -896,7 +853,6 @@ class Zamba2ForCausalLM(nn.Module, HasInnerState, IsHybrid):
|
||||
head_dim=hf_config.mamba_headdim,
|
||||
state_size=hf_config.mamba_d_state,
|
||||
conv_kernel=hf_config.mamba_d_conv,
|
||||
use_v1=use_v1,
|
||||
)
|
||||
|
||||
def __init__(self, *, vllm_config: VllmConfig, prefix: str = "") -> None:
|
||||
@@ -945,9 +901,6 @@ class Zamba2ForCausalLM(nn.Module, HasInnerState, IsHybrid):
|
||||
# Tie weights with input embeddings if using same dimensions
|
||||
self.lm_head = self.lm_head.tie_weights(self.model.embed_tokens)
|
||||
|
||||
# Used to track and store by the Mamba cache between steps.
|
||||
self.mamba_cache: Optional[MambaCacheManager] = None
|
||||
|
||||
# Initialize logits processing and sampling
|
||||
self.logits_processor = LogitsProcessor(self.unpadded_vocab_size,
|
||||
config.vocab_size)
|
||||
@@ -977,61 +930,15 @@ class Zamba2ForCausalLM(nn.Module, HasInnerState, IsHybrid):
|
||||
Returns:
|
||||
Output hidden states
|
||||
"""
|
||||
# Initialize Mamba cache if needed
|
||||
mamba_cache_params = None
|
||||
if not envs.VLLM_USE_V1:
|
||||
if self.mamba_cache is None:
|
||||
num_mamba_layers = self.config.num_hidden_layers
|
||||
mamba_state_shape = \
|
||||
self.get_mamba_state_shape_from_config(
|
||||
self.vllm_config, use_v1=False)
|
||||
mamba_state_dtype = \
|
||||
self.get_mamba_state_dtype_from_config(
|
||||
self.vllm_config)
|
||||
self.mamba_cache = MambaCacheManager(self.vllm_config,
|
||||
num_mamba_layers,
|
||||
*mamba_state_shape,
|
||||
*mamba_state_dtype)
|
||||
|
||||
# Get cache parameters for current run
|
||||
mamba_cache_params = self.mamba_cache.current_run_tensors(**kwargs)
|
||||
|
||||
# Forward pass through model
|
||||
hidden_states = self.model(
|
||||
input_ids,
|
||||
positions,
|
||||
mamba_cache_params,
|
||||
inputs_embeds,
|
||||
)
|
||||
|
||||
return hidden_states
|
||||
|
||||
def copy_inputs_before_cuda_graphs(
|
||||
self, input_buffers: dict[str, torch.Tensor],
|
||||
**kwargs: Any) -> dict[str, torch.Tensor]:
|
||||
"""Copy inputs before CUDA graph capture.
|
||||
|
||||
Args:
|
||||
input_buffers: Dictionary of input tensors
|
||||
**kwargs: Additional arguments passed to cache manager
|
||||
|
||||
Returns:
|
||||
Updated input buffers
|
||||
"""
|
||||
return self.mamba_cache.copy_inputs_before_cuda_graphs(
|
||||
input_buffers, **kwargs)
|
||||
|
||||
def get_seqlen_agnostic_capture_inputs(
|
||||
self, batch_size: int) -> dict[str, torch.Tensor]:
|
||||
"""Get inputs for sequence-length-agnostic graph capture.
|
||||
|
||||
Args:
|
||||
batch_size: Size of batch to capture
|
||||
Returns:
|
||||
Dictionary of capture inputs
|
||||
"""
|
||||
return self.mamba_cache.get_seqlen_agnostic_capture_inputs(batch_size)
|
||||
|
||||
def compute_logits(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
|
||||
Reference in New Issue
Block a user