sample_id
stringlengths
21
196
text
stringlengths
105
936k
metadata
dict
category
stringclasses
6 values
vllm-project/vllm:tests/v1/attention/test_attention_backends.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Tests for v1 attention backends without GPUModelRunner dependency.""" from functools import partial import pytest import torch from torch.nn.attention.flex_attention import create_block_mask, flex_attention from tests.v1.attention.utils import ( BatchSpec, create_common_attn_metadata, create_standard_kv_cache_spec, create_vllm_config, try_backend_includes_kv_cache_update, try_get_attention_backend, ) from vllm.config import ModelConfig from vllm.platforms import current_platform from vllm.utils.math_utils import cdiv from vllm.utils.torch_utils import ( STR_DTYPE_TO_TORCH_DTYPE, is_torch_equal_or_newer, set_random_seed, ) from vllm.v1.attention.backend import AttentionType, CommonAttentionMetadata from vllm.v1.attention.backends.registry import AttentionBackendEnum from vllm.v1.attention.backends.utils import ( set_kv_cache_layout, ) from vllm.v1.kv_cache_interface import FullAttentionSpec BACKENDS_TO_TEST = [ AttentionBackendEnum.FLASH_ATTN, AttentionBackendEnum.FLASHINFER, AttentionBackendEnum.FLEX_ATTENTION, AttentionBackendEnum.TRITON_ATTN, AttentionBackendEnum.TREE_ATTN, "FLEX_ATTENTION_SLOW", ] # Remove flashinfer from the list if it's not available try: import flashinfer # noqa: F401 except ImportError: BACKENDS_TO_TEST.remove(AttentionBackendEnum.FLASHINFER) def _convert_dtype_to_torch(dtype): """Convert ModelDType to torch.dtype.""" if isinstance(dtype, str): if dtype == "auto": return torch.float16 # Default dtype for testing elif dtype in STR_DTYPE_TO_TORCH_DTYPE: return STR_DTYPE_TO_TORCH_DTYPE[dtype] else: raise ValueError(f"Unknown dtype: {dtype}") elif isinstance(dtype, torch.dtype): return dtype else: raise ValueError(f"Unknown dtype: {dtype}") # Define common batch configurations BATCH_SPECS = { "small_decode": BatchSpec(seq_lens=[32, 40], query_lens=[1, 1]), "small_prefill": BatchSpec(seq_lens=[32, 40], query_lens=[8, 8]), "mixed_small": BatchSpec(seq_lens=[32, 40, 48, 56], query_lens=[1, 1, 5, 5]), "medium_decode": BatchSpec( seq_lens=[128, 256, 512, 1024, 128, 256, 512, 1024], query_lens=[1, 1, 1, 1, 1, 1, 1, 1], ), "medium_prefill": BatchSpec( seq_lens=[256, 512, 1024, 2048], query_lens=[16, 16, 16, 16] ), "mixed_medium": BatchSpec( seq_lens=[512, 1024, 2048, 512, 1024, 2048], query_lens=[1, 1, 1, 7, 7, 7] ), "large_decode": BatchSpec(seq_lens=[2048] * 32, query_lens=[1] * 32), "large_prefill": BatchSpec(seq_lens=[4096] * 8, query_lens=[32] * 8), "mixed_large": BatchSpec( seq_lens=[1024, 2048, 4096, 1024, 2048, 4096], query_lens=[1, 1, 1, 32, 32, 32] ), "single_decode": BatchSpec(seq_lens=[1024], query_lens=[1]), "single_prefill": BatchSpec(seq_lens=[1024], query_lens=[64]), # encoder-only "small_encoder_prefill": BatchSpec( seq_lens=[32, 64, 128, 256], query_lens=[32, 64, 128, 256] ), "medium_encoder_prefill": BatchSpec( seq_lens=[256, 512, 1024, 2048], query_lens=[256, 512, 1024, 2048] ), } def create_and_prepopulate_kv_cache( k_contexts: list[torch.Tensor], v_contexts: list[torch.Tensor], block_size: int, num_kv_heads: int, head_size: int, dtype: torch.dtype, device: torch.device, num_blocks: int, common_attn_metadata: CommonAttentionMetadata, randomize_blocks: bool = True, ) -> torch.Tensor: """Create and prepopulate a KV cache with context data. Args: k_contexts: List of key context tensors for each sequence v_contexts: List of value context tensors for each sequence seq_lens: List of sequence lengths block_size: Size of each block num_kv_heads: Number of KV heads head_size: Size of each head dtype: Data type for the cache device: Device to create the cache on num_blocks: Total number of blocks in the cache block_table: Block table tensor to populate randomize_blocks: Whether to randomly permute blocks or use sequential order Returns: Tuple of (kv_cache, updated_block_table) """ batch_size = len(k_contexts) seq_lens = common_attn_metadata.seq_lens.cpu() query_lens = ( common_attn_metadata.query_start_loc_cpu[1:] - common_attn_metadata.query_start_loc_cpu[:-1] ) context_lens = seq_lens - query_lens block_table = common_attn_metadata.block_table_tensor slot_mapping = common_attn_metadata.slot_mapping # Create KV cache kv_cache = torch.zeros( 2, num_blocks, block_size, num_kv_heads, head_size, dtype=dtype, device=device ) kv_cache_flat = kv_cache.view(2, -1, num_kv_heads, head_size) # Populate the cache with the context tokens # Start from block_id=1 since block_id=0 is considered the null block start_block_idx = 1 for i in range(batch_size): k_context, v_context = k_contexts[i], v_contexts[i] start = start_block_idx * block_size end = start + k_context.shape[0] kv_cache_flat[0, start:end, ...] = k_context kv_cache_flat[1, start:end, ...] = v_context # Stay block aligned and allocate enough blocks for the new tokens start_block_idx += cdiv(int(seq_lens[i]), block_size) blocks_end = start_block_idx # Permute the context blocks (excluding block 0 which is null) if randomize_blocks: # Random permutation starting from block 1 perm = torch.randperm(blocks_end - 1) + 1 else: # Sequential order starting from block 1 perm = torch.arange(1, blocks_end) inv_perm = torch.zeros(blocks_end, dtype=torch.long, device=device) # Add 1 to account for starting from block 1 inv_perm[1:] = torch.argsort(perm) + 1 kv_cache[:, 1:blocks_end, ...] = kv_cache[:, perm, ...] # Construct the right block table # Start from block_id=1 since block_id=0 is considered the null block start_block_idx = 1 for i in range(batch_size): num_blocks_for_seq = cdiv(int(seq_lens[i]), block_size) start = start_block_idx end = start + num_blocks_for_seq block_table[i, :num_blocks_for_seq] = inv_perm[start:end] start_block_idx += num_blocks_for_seq # Create a realistic slot mapping that corresponds to the block table for i in range(batch_size): token_offsets = torch.arange(int(query_lens[i])) + int(context_lens[i]) block_indices = token_offsets // block_size token_inter_block_offsets = token_offsets % block_size start = common_attn_metadata.query_start_loc_cpu[i] end = common_attn_metadata.query_start_loc_cpu[i + 1] slot_mapping[start:end] = block_table[ i, block_indices ] * block_size + token_inter_block_offsets.to(device) return kv_cache class MockAttentionLayer: """A mock attention layer for testing.""" def __init__(self, device: torch.device): self._q_scale = torch.tensor(1.0, device=device) self._k_scale = torch.tensor(1.0, device=device) self._v_scale = torch.tensor(1.0, device=device) # Add float versions for flashinfer self._q_scale_float = 1.0 self._k_scale_float = 1.0 self._v_scale_float = 1.0 def run_attention_backend( backend: AttentionBackendEnum, kv_cache_spec: FullAttentionSpec, layer_names: list[str], vllm_config, device: torch.device, common_attn_metadata: CommonAttentionMetadata, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, kv_cache: torch.Tensor, attn_type: AttentionType = AttentionType.DECODER, sliding_window: int | None = None, ) -> torch.Tensor: """Run attention computation using the specified backend's AttentionImpl.""" # Handle special case for FLEX_ATTENTION_SLOW actual_backend = backend use_direct_block_mask = is_torch_equal_or_newer("2.9.0.dev0") if backend == "FLEX_ATTENTION_SLOW": actual_backend = AttentionBackendEnum.FLEX_ATTENTION use_direct_block_mask = False builder_cls, impl_cls = try_get_attention_backend(actual_backend) # Mock flashinfer's get_per_layer_parameters if needed if actual_backend == AttentionBackendEnum.FLASHINFER: import unittest.mock from vllm.v1.attention.backends.utils import PerLayerParameters def mock_get_per_layer_parameters(vllm_config, layer_names, impl_cls): # Return mock parameters for a single layer head_size = vllm_config.model_config.get_head_size() return { layer_name: PerLayerParameters( window_left=-1, # No sliding window logits_soft_cap=0.0, # No soft cap sm_scale=1.0 / (head_size**0.5), # Standard scale ) for layer_name in layer_names } with unittest.mock.patch( "vllm.v1.attention.backends.flashinfer.get_per_layer_parameters", mock_get_per_layer_parameters, ): builder = builder_cls(kv_cache_spec, layer_names, vllm_config, device) attn_metadata = builder.build( common_prefix_len=0, common_attn_metadata=common_attn_metadata, ) else: # Build metadata builder = builder_cls(kv_cache_spec, layer_names, vllm_config, device) if actual_backend == AttentionBackendEnum.FLEX_ATTENTION: builder.direct_build = use_direct_block_mask attn_metadata = builder.build( common_prefix_len=0, common_attn_metadata=common_attn_metadata, ) # Instantiate implementation num_heads = vllm_config.model_config.get_num_attention_heads( vllm_config.parallel_config ) num_kv_heads = vllm_config.model_config.get_num_kv_heads( vllm_config.parallel_config ) head_size = vllm_config.model_config.get_head_size() scale = 1.0 / (head_size**0.5) impl = impl_cls( num_heads=num_heads, head_size=head_size, scale=scale, num_kv_heads=num_kv_heads, alibi_slopes=None, sliding_window=sliding_window, attn_type=attn_type, kv_cache_dtype="auto", ) # Create mock layer and output buffer mock_layer = MockAttentionLayer(device) output = torch.empty_like(query) # Run forward pass # NOTE: The query, key, and value are already shaped correctly # in the calling test function. if not try_backend_includes_kv_cache_update(actual_backend): impl.do_kv_cache_update( mock_layer, key, value, kv_cache, attn_metadata.slot_mapping ) output = impl.forward( mock_layer, query, key, value, kv_cache, attn_metadata, output=output ) return output def _test_backend_correctness( batch_spec: BatchSpec, model: str, backend_to_test: list[AttentionBackendEnum | str], mask_mod, *, attn_type: AttentionType = AttentionType.DECODER, block_size: int = 16, atol: float = 1e-2, rtol: float = 1e-2, tensor_parallel_size: int = 1, ): """ Test that all backends produce similar outputs to a reference implementation using torch.nn.functional.scaled_dot_product_attention. This test works by: 1. Generating a batch of sequences with specified context and query lengths. 2. Computing a ground-truth attention output using torch.sdpa on contiguous Q, K, and V tensors. 3. Simulating vLLM's paged KV cache: It takes the context portion of the K/V tensors and manually places them into a paged buffer according to the test's (randomly generated) block table. 4. Running each vLLM attention backend with the new queries and the simulated paged KV cache. 5. Comparing the vLLM backend's output to the ground-truth SDPA output. Note: When tensor_parallel_size > 1, we simulate the head partitioning by overriding the model config to use fewer heads, without requiring multiple GPUs. This tests that backends work correctly with different head counts. """ set_random_seed(42) hf_config_override = None if tensor_parallel_size > 1: from vllm.config import ModelConfig temp_config = ModelConfig(model=model, max_model_len=1) original_num_heads = temp_config.hf_text_config.num_attention_heads original_num_kv_heads = getattr( temp_config.hf_text_config, "num_key_value_heads", None ) hf_config_override = { "num_attention_heads": original_num_heads // tensor_parallel_size, } if original_num_kv_heads is not None: hf_config_override["num_key_value_heads"] = max( 1, original_num_kv_heads // tensor_parallel_size ) vllm_config = create_vllm_config( model_name=model, tensor_parallel_size=1, # Always use TP=1 to avoid multi-GPU requirements max_model_len=max(batch_spec.seq_lens), block_size=block_size, num_gpu_blocks=8192, hf_config_override=hf_config_override, ) device = torch.device("cuda:0") kv_cache_spec = create_standard_kv_cache_spec(vllm_config) # 1. Setup batch_size = batch_spec.batch_size seq_lens = batch_spec.seq_lens query_lens = batch_spec.query_lens num_q_heads = vllm_config.model_config.get_num_attention_heads( vllm_config.parallel_config ) num_kv_heads = vllm_config.model_config.get_num_kv_heads( vllm_config.parallel_config ) head_size = vllm_config.model_config.get_head_size() sliding_window = vllm_config.model_config.get_sliding_window() dtype = _convert_dtype_to_torch(vllm_config.model_config.dtype) block_size = vllm_config.cache_config.block_size scale = 1.0 / (head_size**0.5) # 2. Generate data and compute SDPA reference output all_q_vllm, all_k_vllm, all_v_vllm = [], [], [] all_sdpa_outputs = [] k_contexts, v_contexts = [], [] for i in range(batch_size): s_len = seq_lens[i] q_len = query_lens[i] context_len = s_len - q_len # Generate Q, K, V for the whole sequence to be used in SDPA q = torch.randn(q_len, num_q_heads, head_size, dtype=dtype, device=device) k_full = torch.randn(s_len, num_kv_heads, head_size, dtype=dtype, device=device) v_full = torch.randn(s_len, num_kv_heads, head_size, dtype=dtype, device=device) # SDPA expects (N, H, L, D), so unsqueeze batch and permute q_sdpa_in = q.unsqueeze(0).transpose(1, 2) k_sdpa_in = k_full.unsqueeze(0).transpose(1, 2) v_sdpa_in = v_full.unsqueeze(0).transpose(1, 2) if num_q_heads != num_kv_heads: assert num_q_heads % num_kv_heads == 0, ( f"num_q_heads ({num_q_heads}) must be divisible by " f"num_kv_heads ({num_kv_heads})" ) repeats = num_q_heads // num_kv_heads k_sdpa_in = k_sdpa_in.repeat_interleave(repeats, dim=1) v_sdpa_in = v_sdpa_in.repeat_interleave(repeats, dim=1) # Create causal mask: query token i attends to positions 0 to # (context_len + i) kv_len = s_len final_mask_mod = partial(mask_mod, context_len=context_len) block_mask = create_block_mask( final_mask_mod, B=None, H=None, Q_LEN=q_len, KV_LEN=kv_len, device=device ) sdpa_out_i = flex_attention( q_sdpa_in, k_sdpa_in, v_sdpa_in, block_mask=block_mask, scale=scale, enable_gqa=True, ) all_sdpa_outputs.append(sdpa_out_i.transpose(1, 2).squeeze(0)) # Inputs for vLLM backends are just the new tokens all_q_vllm.append(q) all_k_vllm.append(k_full[context_len:]) all_v_vllm.append(v_full[context_len:]) # Contextual K/V data used to populate the paged cache k_contexts.append(k_full[:context_len]) v_contexts.append(v_full[:context_len]) query_vllm = torch.cat(all_q_vllm, dim=0) key_vllm = torch.cat(all_k_vllm, dim=0) value_vllm = torch.cat(all_v_vllm, dim=0) sdpa_output = torch.cat(all_sdpa_outputs, dim=0) common_attn_metadata = create_common_attn_metadata( batch_spec, vllm_config.cache_config.block_size, device ) if attn_type == AttentionType.ENCODER_ONLY: # For encoder-only, all tokens are prefill tokens common_attn_metadata.causal = False # 3. Simulate Paged KV Cache and a realistic slot_mapping kv_cache = create_and_prepopulate_kv_cache( k_contexts=k_contexts, v_contexts=v_contexts, block_size=block_size, num_kv_heads=num_kv_heads, head_size=head_size, dtype=dtype, device=device, num_blocks=vllm_config.cache_config.num_gpu_blocks or 1000, common_attn_metadata=common_attn_metadata, randomize_blocks=True, ) # 4. Run vLLM backends and compare # Note: flex_attention has known Triton kernel compatibility issues # with test infrastructures for backend_name in backend_to_test: # FlashAttentionm + FlexAttention: # [2, num_blocks, block_size, num_kv_heads, head_size] # FlashInfer + Triton: # [num_blocks, 2, block_size, num_kv_heads, head_size] # Select the appropriate KV cache format for each backend kv_cache_for_backend = kv_cache reset_kv_cache_layout = False if backend_name in ( AttentionBackendEnum.FLASHINFER, AttentionBackendEnum.TRITON_ATTN, ): kv_cache_for_backend = kv_cache.transpose(0, 1) if backend_name == AttentionBackendEnum.FLASHINFER: # For FlashInfer default to HND layout and kv_cache_for_backend = ( kv_cache_for_backend.transpose(2, 3).contiguous().transpose(2, 3) ) set_kv_cache_layout("HND") reset_kv_cache_layout = True elif backend_name == AttentionBackendEnum.TRITON_ATTN: kv_cache_for_backend = kv_cache_for_backend.contiguous() try: backend_output = run_attention_backend( backend_name, kv_cache_spec, ["placeholder"], vllm_config, device, common_attn_metadata, query_vllm, key_vllm, value_vllm, kv_cache_for_backend, sliding_window=sliding_window, attn_type=attn_type, ) finally: if reset_kv_cache_layout: set_kv_cache_layout(None) # Check shape and dtype consistency assert backend_output.shape == sdpa_output.shape, ( f"[{backend_name}] shape {backend_output.shape} != " f"SDPA shape {sdpa_output.shape}" ) assert backend_output.dtype == sdpa_output.dtype, ( f"[{backend_name}] dtype {backend_output.dtype} != " f"SDPA dtype {sdpa_output.dtype}" ) assert torch.isfinite(backend_output).all(), ( f"[{backend_name}] produced non-finite values" ) # Check numerical similarity def error_msg(msg: str, backend_name: str): return f"[{backend_name}] output differs from SDPA baseline. {msg}" torch.testing.assert_close( backend_output, sdpa_output, rtol=rtol, atol=atol, msg=partial(error_msg, backend_name=backend_name), ) @pytest.mark.parametrize( "batch_spec_name", [ "small_decode", "small_prefill", "mixed_small", "medium_decode", "medium_prefill", "mixed_medium", "large_decode", "large_prefill", "single_decode", "single_prefill", ], ) @pytest.mark.parametrize("model", ["meta-llama/Meta-Llama-3-8B"]) @pytest.mark.parametrize("tensor_parallel_size", [1, 2, 4]) def test_causal_backend_correctness( default_vllm_config, batch_spec_name: str, model: str, tensor_parallel_size: int ): """Test backend's correctness with causal attention.""" def causal_mask_mod( b: torch.Tensor, h: torch.Tensor, q_idx: torch.Tensor, kv_idx: torch.Tensor, *, context_len: int, ): return (q_idx + context_len) >= kv_idx batch_spec = BATCH_SPECS[batch_spec_name] LARGE_BLOCK_BACKENDS = ( [AttentionBackendEnum.FLEX_ATTENTION] if is_torch_equal_or_newer("2.9.0.dev0") else [] ) if current_platform.is_rocm(): SMALL_BLOCK_BACKENDS = [ x for x in BACKENDS_TO_TEST if ( x not in LARGE_BLOCK_BACKENDS and x is not AttentionBackendEnum.FLASH_ATTN ) ] else: SMALL_BLOCK_BACKENDS = [ x for x in BACKENDS_TO_TEST if x not in LARGE_BLOCK_BACKENDS ] _test_backend_correctness( batch_spec, model, SMALL_BLOCK_BACKENDS, causal_mask_mod, tensor_parallel_size=tensor_parallel_size, ) # Fast FlexAttention needs to run with block_size=128 if LARGE_BLOCK_BACKENDS: _test_backend_correctness( batch_spec, model, LARGE_BLOCK_BACKENDS, causal_mask_mod, block_size=128, tensor_parallel_size=tensor_parallel_size, ) if current_platform.is_rocm(): # FLASH_ATTN is not supported on ROCm SLIDING_WINDOW_BACKENDS_TO_TEST = [ AttentionBackendEnum.FLEX_ATTENTION, AttentionBackendEnum.TRITON_ATTN, "FLEX_ATTENTION_SLOW", ] else: SLIDING_WINDOW_BACKENDS_TO_TEST = [ AttentionBackendEnum.FLASH_ATTN, AttentionBackendEnum.FLEX_ATTENTION, AttentionBackendEnum.TRITON_ATTN, "FLEX_ATTENTION_SLOW", ] @pytest.mark.parametrize( "batch_spec_name", [ "small_decode", "small_prefill", "mixed_medium", "large_decode", "large_prefill", "mixed_large", ], ) @pytest.mark.parametrize("model", ["microsoft/Phi-tiny-MoE-instruct"]) @pytest.mark.parametrize("tensor_parallel_size", [1, 2, 4]) def test_sliding_window_backend_correctness( batch_spec_name: str, model: str, tensor_parallel_size: int ): """Test backend's correctness with sliding window attention.""" def sliding_window_mask_mod( b: torch.Tensor, h: torch.Tensor, q_idx: torch.Tensor, kv_idx: torch.Tensor, *, context_len: int, sliding_window: int, ): causal_mask = q_idx + context_len >= kv_idx window_mask = q_idx + context_len - kv_idx < sliding_window return causal_mask & window_mask batch_spec = BATCH_SPECS[batch_spec_name] model_config = ModelConfig(model=model, max_model_len=max(batch_spec.seq_lens)) sliding_window = model_config.get_sliding_window() sliding_window_mask_mod_fn = partial( sliding_window_mask_mod, sliding_window=sliding_window ) LARGE_BLOCK_BACKENDS = ( [AttentionBackendEnum.FLEX_ATTENTION] if is_torch_equal_or_newer("2.9.0.dev0") else [] ) SMALL_BLOCK_BACKENDS = [ x for x in SLIDING_WINDOW_BACKENDS_TO_TEST if x not in LARGE_BLOCK_BACKENDS ] _test_backend_correctness( batch_spec, model, SMALL_BLOCK_BACKENDS, sliding_window_mask_mod_fn, tensor_parallel_size=tensor_parallel_size, ) # Fast FlexAttention needs to run with block_size=128 if LARGE_BLOCK_BACKENDS: _test_backend_correctness( batch_spec, model, LARGE_BLOCK_BACKENDS, sliding_window_mask_mod_fn, block_size=128, tensor_parallel_size=tensor_parallel_size, ) @pytest.mark.parametrize( "batch_spec_name", [ "small_encoder_prefill", "medium_encoder_prefill", ], ) @pytest.mark.parametrize("model", ["google/embeddinggemma-300m"]) @pytest.mark.parametrize("tensor_parallel_size", [1, 2]) def test_sliding_window_encoder_backend_correctness( batch_spec_name: str, model: str, tensor_parallel_size: int ): """Test backend's correctness with sliding window attention.""" def bidi_sliding_window_mask_mod( b: torch.Tensor, h: torch.Tensor, q_idx: torch.Tensor, kv_idx: torch.Tensor, *, context_len: int, sliding_window: int, ): return torch.abs(q_idx + context_len - kv_idx) < sliding_window batch_spec = BATCH_SPECS[batch_spec_name] model_config = ModelConfig(model=model, max_model_len=max(batch_spec.seq_lens)) sliding_window = model_config.get_sliding_window() sliding_window_mask_mod_fn = partial( bidi_sliding_window_mask_mod, sliding_window=sliding_window ) _test_backend_correctness( batch_spec, model, SLIDING_WINDOW_BACKENDS_TO_TEST, sliding_window_mask_mod_fn, attn_type=AttentionType.ENCODER_ONLY, tensor_parallel_size=tensor_parallel_size, )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/v1/attention/test_attention_backends.py", "license": "Apache License 2.0", "lines": 652, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/v1/attention/utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Utility functions for attention-related v1 tests.""" from dataclasses import dataclass import pytest import torch from vllm.config import ( CacheConfig, CompilationConfig, DeviceConfig, LoadConfig, ModelConfig, ParallelConfig, SchedulerConfig, VllmConfig, ) from vllm.config.model import ModelDType from vllm.v1.attention.backend import ( AttentionImpl, AttentionMetadataBuilder, CommonAttentionMetadata, ) from vllm.v1.attention.backends.registry import AttentionBackendEnum from vllm.v1.kv_cache_interface import FullAttentionSpec @dataclass class BatchSpec: """Specification for a batch configuration (workload shape only).""" seq_lens: list[int] query_lens: list[int] name: str = "unnamed" @property def batch_size(self): return len(self.seq_lens) def __post_init__(self): assert len(self.seq_lens) == len(self.query_lens) def compute_num_tokens(self): return sum(self.query_lens) def create_common_attn_metadata( batch_spec: BatchSpec, block_size: int, device: torch.device, max_block_idx: int = 1000, arange_block_indices: bool = False, ) -> CommonAttentionMetadata: """Create CommonAttentionMetadata from a BatchSpec and ModelParams.""" # Create query start locations query_start_loc = torch.zeros( batch_spec.batch_size + 1, dtype=torch.int32, device=device ) query_start_loc[1:] = torch.tensor( batch_spec.query_lens, dtype=torch.int32, device=device ).cumsum(0) query_start_loc_cpu = query_start_loc.cpu() num_tokens = batch_spec.compute_num_tokens() # Create sequence lengths seq_lens = torch.tensor(batch_spec.seq_lens, dtype=torch.int32, device=device) seq_lens_cpu = seq_lens.cpu() max_seq_len = int(seq_lens_cpu.max()) # Create computed tokens (context length for each sequence) context_lens = [ batch_spec.seq_lens[i] - batch_spec.query_lens[i] for i in range(batch_spec.batch_size) ] num_computed_tokens_cpu = torch.tensor(context_lens, dtype=torch.int32) # Create block table and slot mapping max_blocks = (max(batch_spec.seq_lens) + block_size - 1) // block_size if arange_block_indices: num_blocks = batch_spec.batch_size * max_blocks block_table_tensor = torch.arange( num_blocks, dtype=torch.int32, device=device ).view(batch_spec.batch_size, max_blocks) slot_mapping = torch.arange(num_tokens, dtype=torch.int64, device=device).view( num_tokens ) else: block_table_tensor = torch.randint( 0, max_block_idx, (batch_spec.batch_size, max_blocks), dtype=torch.int32, device=device, ) slot_mapping = torch.randint( 0, max_block_idx, (num_tokens,), dtype=torch.int64, device=device ) # Calculate max query length max_query_len = max(batch_spec.query_lens) return CommonAttentionMetadata( query_start_loc=query_start_loc, query_start_loc_cpu=query_start_loc_cpu, seq_lens=seq_lens, _seq_lens_cpu=seq_lens_cpu, _num_computed_tokens_cpu=num_computed_tokens_cpu, num_reqs=batch_spec.batch_size, num_actual_tokens=num_tokens, max_query_len=max_query_len, max_seq_len=max_seq_len, block_table_tensor=block_table_tensor, slot_mapping=slot_mapping, causal=True, ) def try_get_attention_backend( backend: AttentionBackendEnum, ) -> tuple[type[AttentionMetadataBuilder], type[AttentionImpl]]: """Try to get the attention backend class, skipping test if not found.""" try: backend_class = backend.get_class() return backend_class.get_builder_cls(), backend_class.get_impl_cls() except ImportError as e: pytest.skip(f"{backend.name} not available: {e}") raise AssertionError("unreachable") from None def try_backend_includes_kv_cache_update( backend: AttentionBackendEnum, ) -> bool: """Try to get the attention backend class, skipping test if not found.""" try: backend_class = backend.get_class() return backend_class.forward_includes_kv_cache_update except ImportError as e: pytest.skip(f"{backend.name} not available: {e}") raise AssertionError("unreachable") from None def create_standard_kv_cache_spec(vllm_config: VllmConfig) -> FullAttentionSpec: """Create a FullAttentionSpec from ModelParams only.""" return FullAttentionSpec( block_size=vllm_config.cache_config.block_size, num_kv_heads=vllm_config.model_config.get_num_kv_heads( vllm_config.parallel_config ), head_size=vllm_config.model_config.get_head_size(), dtype=vllm_config.model_config.dtype, sliding_window=vllm_config.model_config.get_sliding_window(), ) def create_vllm_config( model_name: str = "meta-llama/Meta-Llama-3-8B", tensor_parallel_size: int = 1, max_model_len: int = 1024, dtype: ModelDType | torch.dtype = "auto", num_gpu_blocks: int = 1000, block_size: int = 16, max_num_seqs: int = 256, max_num_batched_tokens: int = 8192, enable_chunked_prefill: bool = True, add_mock_model_methods: bool = True, hf_config_override: dict | None = None, ) -> VllmConfig: """Create a VllmConfig for testing with reasonable defaults.""" model_config = ModelConfig( model=model_name, tokenizer=model_name, trust_remote_code=False, dtype=dtype, seed=0, max_model_len=max_model_len, ) cache_config = CacheConfig( block_size=block_size, cache_dtype="auto", swap_space=0, ) # Set cache blocks for testing # (these may be set during initialization normally) cache_config.num_gpu_blocks = num_gpu_blocks cache_config.num_cpu_blocks = 0 parallel_config = ParallelConfig( tensor_parallel_size=tensor_parallel_size, ) scheduler_config = SchedulerConfig( max_num_seqs=max_num_seqs, max_num_batched_tokens=max_num_batched_tokens, enable_chunked_prefill=enable_chunked_prefill, max_model_len=model_config.max_model_len, is_encoder_decoder=model_config.is_encoder_decoder, ) device_config = DeviceConfig() load_config = LoadConfig() compilation_config = CompilationConfig() if add_mock_model_methods: # Add mock methods to satisfy backends that need them # This is a workaround because tests don't build full, real models, # but some backends expect to query the model for layer-specific # parameters import types model_config.get_num_layers = types.MethodType(lambda self: 1, model_config) model_config.get_sliding_window_for_layer = types.MethodType( lambda self, i: None, model_config ) model_config.get_logits_soft_cap_for_layer = types.MethodType( lambda self, i: 0.0, model_config ) model_config.get_sm_scale_for_layer = types.MethodType( lambda self, i: 1.0 / model_config.get_head_size() ** 0.5, model_config ) if hf_config_override: model_config.hf_config.update(hf_config_override) return VllmConfig( model_config=model_config, cache_config=cache_config, parallel_config=parallel_config, scheduler_config=scheduler_config, device_config=device_config, load_config=load_config, compilation_config=compilation_config, ) def create_dummy_kv_cache( block_size: int, num_kv_heads: int, head_size: int, dtype: torch.dtype, device: torch.device, num_blocks: int = 100, ) -> torch.Tensor: """Create a dummy KV cache tensor for testing.""" kv_cache = torch.randn( num_blocks, 2, # K and V block_size, num_kv_heads, head_size, dtype=dtype, device=device, ) return kv_cache @dataclass class BackendConfig: name: str attention_config: dict comp_config: dict specific_gpu_arch: tuple | None = None # Define all backend configurations of full cudagraph to be tested full_cg_backend_configs = { # FA3 on Hopper "FA3": BackendConfig( name="FA3", attention_config={ "backend": "FLASH_ATTN", "flash_attn_version": 3, "flash_attn_max_num_splits_for_cuda_graph": 16, }, comp_config={ "cudagraph_mode": "FULL", }, specific_gpu_arch=(9, 0), ), # FlashMLA on Hopper "FlashMLA": BackendConfig( name="FlashMLA", attention_config={"backend": "FLASHMLA"}, comp_config={ "cudagraph_mode": "FULL_AND_PIECEWISE", }, specific_gpu_arch=(9, 0), ), # Cutlass MLA on Blackwell "CutlassMLA": BackendConfig( name="CutlassMLA", attention_config={"backend": "CUTLASS_MLA"}, comp_config={ "cudagraph_mode": "FULL_AND_PIECEWISE", }, specific_gpu_arch=(10, 0), ), # FlashInfer MLA on Blackwell "FlashInferMLA": BackendConfig( name="FlashInferMLA", attention_config={"backend": "FLASHINFER_MLA"}, comp_config={ "cudagraph_mode": "FULL_AND_PIECEWISE", }, specific_gpu_arch=(10, 0), ), # FlashAttention MLA on Hopper "FlashAttentionMLA": BackendConfig( name="FlashAttentionMLA", attention_config={ "backend": "FLASH_ATTN_MLA", "flash_attn_max_num_splits_for_cuda_graph": 16, }, comp_config={ "cudagraph_mode": "FULL_DECODE_ONLY", }, specific_gpu_arch=(9, 0), ), # FA2 "FA2": BackendConfig( name="FA2", attention_config={ "backend": "FLASH_ATTN", "flash_attn_version": 2, "flash_attn_max_num_splits_for_cuda_graph": 16, }, comp_config={ "cudagraph_mode": "FULL_AND_PIECEWISE", }, ), # Triton Attention "TritonAttn": BackendConfig( name="TritonAttn", attention_config={"backend": "TRITON_ATTN"}, comp_config={ "cudagraph_mode": "FULL_AND_PIECEWISE", }, ), # FlashInfer "FlashInfer": BackendConfig( name="FlashInfer", attention_config={"backend": "FLASHINFER"}, comp_config={ "cudagraph_mode": "FULL_AND_PIECEWISE", }, ), "RocmAttn": BackendConfig( name="RocmAttn", attention_config={ "backend": "ROCM_ATTN", "use_prefill_decode_attention": True, }, comp_config={ "cudagraph_mode": "FULL", }, ), }
{ "repo_id": "vllm-project/vllm", "file_path": "tests/v1/attention/utils.py", "license": "Apache License 2.0", "lines": 321, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/model_executor/layers/quantization/inc.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from fractions import Fraction from typing import TYPE_CHECKING, Any import regex as re import torch from vllm.logger import init_logger from vllm.model_executor.layers.linear import LinearBase, UnquantizedLinearMethod from vllm.model_executor.layers.quantization import ( QuantizationConfig, QuantizationMethods, ) from vllm.model_executor.layers.vocab_parallel_embedding import ParallelLMHead from vllm.platforms import current_platform from vllm.scalar_type import scalar_types if TYPE_CHECKING: from vllm.model_executor.models.utils import WeightsMapper logger = init_logger(__name__) class INCConfig(QuantizationConfig): """Config class for Intel Neural Compressor (INC). Repo: https://github.com/intel/neural-compressor """ SUPPORTED_BITS = {2, 3, 4, 8} SUPPORTED_DTYPES = {"int"} SUPPORTED_FORMATS = {"auto_round:auto_gptq", "auto_round:auto_awq"} SUPPORTED_BACKENDS = { "auto", "gptq", "gptq:marlin", "awq", "awq:marlin", "marlin", } def __init__( self, weight_bits: int, group_size: int, sym: bool = True, packing_format: str = "auto_round:auto_gptq", block_name_to_quantize: str | list[str] | None = None, extra_config: dict[str, Any] | None = None, data_type: str = "int", backend: str = "auto", ) -> None: super().__init__() if weight_bits not in self.SUPPORTED_BITS: raise ValueError( f"Unsupported weight_bits: {weight_bits}, " f"currently only support {self.SUPPORTED_BITS}." ) if data_type not in self.SUPPORTED_DTYPES: raise ValueError( f"Unsupported data_type: {data_type}," f" currently only support {self.SUPPORTED_DTYPES}." ) if packing_format not in self.SUPPORTED_FORMATS: raise ValueError( f"Unsupported packing_format: {packing_format}, " f"currently only support {self.SUPPORTED_FORMATS}." ) if backend not in self.SUPPORTED_BACKENDS: raise ValueError( f"Unsupported backend: {backend}, " f"currently only support {self.SUPPORTED_BACKENDS}." ) self.weight_bits = weight_bits self.group_size = group_size self.sym = sym self.packing_format = packing_format self.block_name_to_quantize = ( block_name_to_quantize.split(",") if isinstance(block_name_to_quantize, str) else block_name_to_quantize ) self.extra_config = extra_config self.data_type = data_type self.backend = backend self.pack_factor = Fraction(32, weight_bits) def __repr__(self) -> str: return ( f"INCConfig(weight_bits={self.weight_bits}, " f"group_size={self.group_size}, sym={self.sym})" ) @classmethod def get_name(cls) -> QuantizationMethods: return "inc" @classmethod def get_supported_act_dtypes(cls) -> list[torch.dtype]: return [torch.half, torch.bfloat16] @classmethod def get_min_capability(cls) -> int: return 60 @classmethod def get_config_filenames(cls) -> list[str]: return ["quantization_config.json"] @classmethod def from_config(cls, config: dict[str, Any]) -> "INCConfig": return cls( weight_bits=cls.get_from_keys(config, ["bits"]), group_size=cls.get_from_keys(config, ["group_size"]), sym=cls.get_from_keys(config, ["sym"]), packing_format=cls.get_from_keys_or( config, ["packing_format"], "auto_round:auto_gptq" ), block_name_to_quantize=cls.get_from_keys_or( config, ["block_name_to_quantize", "to_quant_block_names"], None ), extra_config=cls.get_from_keys_or(config, ["extra_config"], None), data_type=cls.get_from_keys_or(config, ["data_type"], "int"), backend=cls.get_from_keys_or(config, ["backend", "vllm_backend"], "auto"), ) def get_layer_config(self, layer, layer_name: str): def get_config(name: str, quantized: bool = True): if not self.extra_config: return ( self.weight_bits if quantized else 16, self.group_size if quantized else -1, self.sym if quantized else True, ) # exact match first if name in self.extra_config: cfg = self.extra_config[name] return ( cfg.get("bits", self.weight_bits if quantized else 16), cfg.get("group_size", self.group_size if quantized else -1), cfg.get("sym", self.sym if quantized else True), ) REGEX_SPECIAL_CHARS = set(r"*+?^$()[]{}|\\") for pattern, cfg in self.extra_config.items(): if not isinstance(pattern, str) or not any( c in REGEX_SPECIAL_CHARS for c in pattern ): continue try: if re.search(re.compile(pattern), name) is not None: return ( cfg.get("bits", self.weight_bits if quantized else 16), cfg.get("group_size", self.group_size if quantized else -1), cfg.get("sym", self.sym if quantized else True), ) except re.error: # Invalid regex, ignore. continue return ( self.weight_bits if quantized else 16, self.group_size if quantized else -1, self.sym if quantized else True, ) # 1. Exact match from config if self.extra_config and layer_name in self.extra_config: return get_config(layer_name) # 2. Determine whether layer should be quantized quantized = not isinstance(layer, ParallelLMHead) if self.block_name_to_quantize: quantized = any( layer_name.startswith(name) for name in self.block_name_to_quantize ) # 3. Handle fused MoE if self.extra_config and "fusedmoe" in layer.__class__.__name__.lower(): moe_configs = [ get_config(name, quantized) for name in self.extra_config if name.startswith(layer_name) ] if moe_configs: if len(set(moe_configs)) == 1: return moe_configs[0] raise ValueError( f"Fused MoE layer '{layer_name}' requires " f"consistent quant config for all sub-layers" ) # 4. Handle fused QKV or other patterns if self.extra_config: for fusion_key, sub_keys in self.packed_modules_mapping.items(): if fusion_key in layer_name and layer_name.count(fusion_key) == 1: sub_names = [ layer_name.replace(fusion_key, sub_key) for sub_key in sub_keys ] sub_configs = [get_config(name, quantized) for name in sub_names] if len(set(sub_configs)) == 1: return sub_configs[0] raise ValueError( f"Fused module '{layer_name}' requires " f"consistent quant config for {sub_names}" ) # 5. Fallback or try a regular expression match return get_config(layer_name, quantized) def check_quantized(self, weight_bits: int) -> bool: return weight_bits < 16 def apply_vllm_mapper(self, hf_to_vllm_mapper: "WeightsMapper"): if self.block_name_to_quantize is not None: self.block_name_to_quantize = hf_to_vllm_mapper.apply_list( self.block_name_to_quantize ) if self.extra_config is not None: self.extra_config = hf_to_vllm_mapper.apply_dict(self.extra_config) def apply_awq_quant_layer(self, layer, prefix: str, backend: str = "auto"): from vllm.model_executor.layers.fused_moe import FusedMoE from vllm.model_executor.layers.quantization.utils.marlin_utils import ( check_marlin_supported, check_moe_marlin_supports_layer, ) weight_bits, group_size, sym = self.get_layer_config(layer, prefix) if not self.check_quantized(weight_bits): if isinstance(layer, (LinearBase, ParallelLMHead)): return UnquantizedLinearMethod() else: return None logger.debug( "[%s] Type: %s, Bits: %s, Group Size: %s, Sym: %s", prefix, layer.__class__.__name__, weight_bits, group_size, sym, ) if backend == "auto" or "marlin" in backend: AWQ_TYPE_MAP = { 4: scalar_types.uint4, 8: scalar_types.uint8, } use_marlin = (weight_bits in AWQ_TYPE_MAP) and check_marlin_supported( AWQ_TYPE_MAP[weight_bits], group_size, not sym ) if isinstance(layer, FusedMoE): use_marlin = use_marlin and check_moe_marlin_supports_layer( layer, group_size ) else: use_marlin = False if use_marlin: from vllm.model_executor.layers.quantization.awq_marlin import ( AWQMarlinConfig, AWQMarlinLinearMethod, AWQMarlinMoEMethod, ) quant_args_marlin = AWQMarlinConfig( weight_bits=weight_bits, group_size=group_size, zero_point=not sym, lm_head_quantized=False, full_config={}, modules_to_not_convert=[], ) else: from vllm.model_executor.layers.quantization.awq import ( AWQConfig, AWQLinearMethod, ) quant_args = AWQConfig( weight_bits=weight_bits, group_size=group_size, zero_point=not sym, ) if isinstance(layer, FusedMoE): if use_marlin: return AWQMarlinMoEMethod(quant_args_marlin, layer.moe_config) from vllm.model_executor.layers.quantization.moe_wna16 import MoeWNA16Config config = { "quant_method": "awq", "bits": weight_bits, "group_size": group_size, "zero_point": not sym, "lm_head": False, } return MoeWNA16Config.from_config(config).get_quant_method(layer, prefix) if isinstance(layer, (LinearBase, ParallelLMHead)): if use_marlin: return AWQMarlinLinearMethod(quant_args_marlin) else: return AWQLinearMethod(quant_args) return None def apply_gptq_quant_layer(self, layer, prefix: str, backend: str = "auto"): from vllm.model_executor.layers.fused_moe import FusedMoE from vllm.model_executor.layers.quantization.utils.marlin_utils import ( check_marlin_supported, check_moe_marlin_supports_layer, ) weight_bits, group_size, sym = self.get_layer_config(layer, prefix) if not self.check_quantized(weight_bits): if isinstance(layer, (LinearBase, ParallelLMHead)): return UnquantizedLinearMethod() else: return None logger.debug( "[%s] Type: %s, Bits: %s, Group Size: %s, Sym: %s", prefix, layer.__class__.__name__, weight_bits, group_size, sym, ) if backend == "auto" or "marlin" in backend: GPTQ_TYPE_MAP = { (4, True): scalar_types.uint4b8, (8, True): scalar_types.uint8b128, } use_marlin = (weight_bits, sym) in GPTQ_TYPE_MAP and check_marlin_supported( GPTQ_TYPE_MAP[(weight_bits, sym)], group_size, has_zp=not sym ) if isinstance(layer, FusedMoE): use_marlin = use_marlin and check_moe_marlin_supports_layer( layer, group_size ) else: use_marlin = False if use_marlin: from vllm.model_executor.layers.quantization.gptq_marlin import ( GPTQMarlinConfig, GPTQMarlinLinearMethod, GPTQMarlinMoEMethod, ) quant_args_marlin = GPTQMarlinConfig( weight_bits=weight_bits, group_size=group_size, is_sym=sym, lm_head_quantized=False, desc_act=False, dynamic={}, full_config={}, ) else: from vllm.model_executor.layers.quantization.gptq import ( GPTQConfig, GPTQLinearMethod, ) quant_args = GPTQConfig( weight_bits=weight_bits, group_size=group_size, lm_head_quantized=False, desc_act=False, dynamic={}, ) if isinstance(layer, FusedMoE): if use_marlin: return GPTQMarlinMoEMethod(quant_args_marlin, layer.moe_config) else: from vllm.model_executor.layers.quantization.moe_wna16 import ( MoeWNA16Config, ) config = { "quant_method": "gptq", "bits": weight_bits, "group_size": group_size, "sym": sym, "lm_head": False, } return MoeWNA16Config.from_config(config).get_quant_method( layer, prefix ) if isinstance(layer, (LinearBase, ParallelLMHead)): if use_marlin: return GPTQMarlinLinearMethod(quant_args_marlin) else: return GPTQLinearMethod(quant_args) return None def apply_ipex_quant_layer(self, layer, prefix: str): weight_bits, group_size, sym = self.get_layer_config(layer, prefix) if not self.check_quantized(weight_bits): if isinstance(layer, (LinearBase, ParallelLMHead)): return UnquantizedLinearMethod() else: return None raise NotImplementedError( "INC quantization is not supported during xpu kernel migration." ) def get_quant_method(self, layer: torch.nn.Module, prefix: str): if prefix and self.extra_config: for layer_name in self.extra_config: if ( layer_name == prefix or layer_name == f"model.{prefix}" ) and self.extra_config[layer_name].get("bits", 16) >= 16: return UnquantizedLinearMethod() if ( current_platform.is_cpu() or current_platform.is_xpu() or self.backend == "ipex" ): return self.apply_ipex_quant_layer(layer, prefix) if "gptq" in self.packing_format or "gptq" in self.backend: return self.apply_gptq_quant_layer(layer, prefix) if "awq" in self.packing_format or "awq" in self.backend: return self.apply_awq_quant_layer(layer, prefix) @classmethod def override_quantization_method( cls, hf_quant_cfg, user_quant ) -> "QuantizationMethods | None": """Override the `auto-round` method to `inc`.""" is_auto_round_format = hf_quant_cfg.get("quant_method", None) == "auto-round" if is_auto_round_format: return cls.get_name() return None
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/quantization/inc.py", "license": "Apache License 2.0", "lines": 392, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/llama4_eagle.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Copyright 2025 the LLAMA4, Meta Inc., vLLM, and HuggingFace Inc. team. # All rights reserved. # # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from collections.abc import Iterable import torch import torch.nn as nn from vllm.compilation.decorators import support_torch_compile from vllm.config import VllmConfig from vllm.logger import init_logger from vllm.model_executor.layers.layernorm import RMSNorm from vllm.model_executor.layers.logits_processor import LogitsProcessor from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.quantization.torchao import TorchAOConfig from vllm.model_executor.layers.vocab_parallel_embedding import ( ParallelLMHead, VocabParallelEmbedding, ) from vllm.model_executor.model_loader.weight_utils import default_weight_loader from vllm.model_executor.models.llama4 import Llama4DecoderLayer, Llama4ForCausalLM from vllm.model_executor.models.utils import extract_layer_index from .interfaces import SupportsMultiModal from .utils import AutoWeightsLoader, maybe_prefix, process_eagle_weight logger = init_logger(__name__) @support_torch_compile class LlamaModel(nn.Module): def __init__( self, *, vllm_config: VllmConfig, prefix: str = "", start_layer_id: int = 0, quant_config: QuantizationConfig | None = None, ) -> None: super().__init__() self.config = vllm_config.speculative_config.draft_model_config.hf_config self.validate_and_update_config(start_layer_id, quant_config) self.vocab_size = self.config.vocab_size self.embed_tokens = VocabParallelEmbedding( self.config.vocab_size, self.config.hidden_size, prefix=maybe_prefix(prefix, "embed_tokens"), ) # Temporarily modify vllm_config.quant_config for draft model layers original_quant_config = vllm_config.quant_config vllm_config.quant_config = quant_config try: self.layers = nn.ModuleList( [ Llama4DecoderLayer( vllm_config=vllm_config, prefix=maybe_prefix(prefix, f"layers.{i + start_layer_id}"), config=self.config, ) for i in range(self.config.num_hidden_layers) ] ) finally: # Restore original quant_config vllm_config.quant_config = original_quant_config self.fc = torch.nn.Linear( self.config.hidden_size * 2, self.config.hidden_size, bias=False ) self.norm = RMSNorm(self.config.hidden_size, eps=self.config.rms_norm_eps) def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.embed_tokens(input_ids) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, hidden_states: torch.Tensor, inputs_embeds: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: if inputs_embeds is None: inputs_embeds = self.embed_input_ids(input_ids) hidden_states = self.fc(torch.cat((inputs_embeds, hidden_states), dim=-1)) residual = None for layer in self.layers: hidden_states, residual = layer( positions, hidden_states, residual, ) hidden_states, _ = self.norm(hidden_states, residual) return hidden_states, hidden_states def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: stacked_params_mapping = [ # (param_name, shard_name, shard_id) (".qkv_proj", ".q_proj", "q"), (".qkv_proj", ".k_proj", "k"), (".qkv_proj", ".v_proj", "v"), (".gate_up_proj", ".gate_proj", 0), (".gate_up_proj", ".up_proj", 1), ] params_dict = dict(self.named_parameters()) loaded_params: set[str] = set() for name, loaded_weight in weights: name = name.removeprefix("model.") for param_name, weight_name, shard_id in stacked_params_mapping: if weight_name not in name: continue name = name.replace(weight_name, param_name) param = params_dict[name] weight_loader = param.weight_loader weight_loader(param, loaded_weight, shard_id) break else: param = params_dict[name] weight_loader = getattr(param, "weight_loader", default_weight_loader) weight_loader(param, loaded_weight) loaded_params.add(name) for name in params_dict: assert name in loaded_params, f"{name} is not loaded!" return loaded_params def validate_and_update_config( self, start_layer_id: int, quant_config: QuantizationConfig | None = None ) -> None: # yoco and moe is not supported by draft model yet assert self.config.yoco_global_kv_layer is None assert self.config.yoco_local_kv_layer is None assert len(self.config.moe_layers) == 0 # draft model layer index is increased by start_layer_id, # so we need to pad relevant configs accordingly self.config.no_rope_layers = [0] * start_layer_id + self.config.no_rope_layers # currently only TorchAO quantization is supported if isinstance(quant_config, TorchAOConfig): def pad_layer_name(layer: str) -> str: layer_index = extract_layer_index(layer) return layer.replace( str(layer_index), str(layer_index + start_layer_id) ) torchao_config = quant_config.torchao_config torchao_config.module_fqn_to_config = { pad_layer_name(layer): quantization for layer, quantization in torchao_config.module_fqn_to_config.items() } class EagleLlama4ForCausalLM(Llama4ForCausalLM): def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): nn.Module.__init__(self) self.config = vllm_config.speculative_config.draft_model_config.hf_config target_layer_num = vllm_config.model_config.get_num_layers( vllm_config.parallel_config ) # draft model quantization config may differ from target model quant_config = VllmConfig.get_quantization_config( vllm_config.speculative_config.draft_model_config, vllm_config.load_config ) self.model = LlamaModel( vllm_config=vllm_config, prefix="model", start_layer_id=target_layer_num, quant_config=quant_config, ) logit_scale = getattr(self.config, "logit_scale", 1.0) self.logits_processor = LogitsProcessor( self.config.vocab_size, scale=logit_scale ) self.lm_head = ParallelLMHead( self.config.draft_vocab_size, self.config.hidden_size, prefix=maybe_prefix(prefix, "lm_head"), ) # Set MoE hyperparameters self.set_moe_parameters() def get_language_model(self) -> torch.nn.Module: return self.model embed_input_ids = SupportsMultiModal.embed_input_ids # type: ignore def forward( self, input_ids: torch.Tensor, positions: torch.Tensor, hidden_states: torch.Tensor, inputs_embeds: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: return self.model(input_ids, positions, hidden_states, inputs_embeds) def get_top_tokens( self, hidden_states: torch.Tensor, ) -> torch.Tensor: """Vocab-parallel argmax without all-gathering full logits. Falls back to full logits when draft_id_to_target_id remapping is active, since the shared lm_head covers the full target vocab but the draft model only predicts over a subset (draft_vocab_size). """ if ( hasattr(self, "draft_id_to_target_id") and self.draft_id_to_target_id is not None ): return self.compute_logits(hidden_states).argmax(dim=-1) return self.logits_processor.get_top_tokens(self.lm_head, hidden_states) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> None: def transform(inputs): name, loaded_weight = inputs name, weight = self.permute_qk_weight_for_rotary(name, loaded_weight) if "lm_head" not in name: name = "model." + name process_eagle_weight(self, name) return name, weight loader = AutoWeightsLoader( self, # lm_head is tied with target model (Llama4ForCausalLM) skip_prefixes=([]), ) loader.load_weights(map(transform, weights))
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/llama4_eagle.py", "license": "Apache License 2.0", "lines": 217, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/models/multimodal/generation/test_voxtral.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import json import pytest from mistral_common.audio import Audio from mistral_common.protocol.instruct.chunk import AudioChunk, RawAudio, TextChunk from mistral_common.protocol.instruct.messages import UserMessage from transformers import VoxtralForConditionalGeneration from vllm.tokenizers.mistral import MistralTokenizer from ....conftest import AudioTestAssets from ....utils import RemoteOpenAIServer from ...utils import check_logprobs_close from .test_ultravox import MULTI_AUDIO_PROMPT, run_multi_audio_test from .vlm_utils import model_utils MODEL_NAME = "mistralai/Voxtral-Mini-3B-2507" MISTRAL_FORMAT_ARGS = [ "--tokenizer_mode", "mistral", "--config_format", "mistral", "--load_format", "mistral", ] def _get_prompt(audio_assets: AudioTestAssets, question: str) -> list[int]: """Build a token-ID prompt via mistral_common for vLLM offline inference.""" tokenizer = MistralTokenizer.from_pretrained(MODEL_NAME) audios = [ Audio.from_file(str(asset.get_local_path()), strict=False) for asset in audio_assets ] audio_chunks = [ AudioChunk(input_audio=RawAudio.from_audio(audio)) for audio in audios ] messages = [ UserMessage(content=[*audio_chunks, TextChunk(text=question)]).to_openai() ] return tokenizer.apply_chat_template(messages=messages) @pytest.mark.core_model @pytest.mark.parametrize("dtype", ["half"]) @pytest.mark.parametrize("max_tokens", [128]) @pytest.mark.parametrize("num_logprobs", [5]) def test_models_with_multiple_audios( vllm_runner, audio_assets: AudioTestAssets, dtype: str, max_tokens: int, num_logprobs: int, ) -> None: vllm_prompt = _get_prompt(audio_assets, MULTI_AUDIO_PROMPT) run_multi_audio_test( vllm_runner, [(vllm_prompt, [a.audio_and_sample_rate for a in audio_assets])], # type: ignore[list-item] MODEL_NAME, dtype=dtype, max_tokens=max_tokens, num_logprobs=num_logprobs, tokenizer_mode="mistral", ) def test_online_serving(vllm_runner, audio_assets: AudioTestAssets): """Two-layer accuracy and serving validation using Mistral format. 1. Offline vLLM greedy output (runs first to avoid CUDA fork issues with multiprocessing - see vlm_utils/core.py). 2. Online OpenAI-compatible API output must match offline — validates that the serving path (chat template, audio encoding, tokenization) does not corrupt anything. Steps run sequentially so each releases the GPU before the next starts. """ question = f"What's happening in these {len(audio_assets)} audio clips?" max_tokens = 10 audio_data = [asset.audio_and_sample_rate for asset in audio_assets] vllm_prompt = _get_prompt(audio_assets, question) with vllm_runner( MODEL_NAME, dtype="half", enforce_eager=True, tokenizer_mode="mistral", config_format="mistral", load_format="mistral", limit_mm_per_prompt={"audio": len(audio_assets)}, ) as vllm_model: offline_outputs = vllm_model.generate_greedy( [vllm_prompt], max_tokens, audios=[audio_data], ) offline_text = offline_outputs[0][1] assert offline_text, "Offline vLLM inference produced empty output" def _asset_to_openai_chunk(asset): audio = Audio.from_file(str(asset.get_local_path()), strict=False) audio.format = "wav" return AudioChunk.from_audio(audio).to_openai() messages = [ { "role": "user", "content": [ *[_asset_to_openai_chunk(a) for a in audio_assets], {"type": "text", "text": question}, ], } ] server_args = [ "--enforce-eager", "--limit-mm-per-prompt", json.dumps({"audio": len(audio_assets)}), *MISTRAL_FORMAT_ARGS, ] with RemoteOpenAIServer( MODEL_NAME, server_args, env_dict={"VLLM_AUDIO_FETCH_TIMEOUT": "30"}, ) as remote_server: client = remote_server.get_client() completion = client.chat.completions.create( model=MODEL_NAME, messages=messages, max_tokens=max_tokens, temperature=0, ) assert len(completion.choices) == 1 choice = completion.choices[0] assert choice.finish_reason == "length" assert choice.message.content == offline_text, ( f"Online serving output does not match offline inference.\n" f" Online: {choice.message.content!r}\n" f" Offline: {offline_text!r}" ) def test_hf_reference(hf_runner, vllm_runner, audio_assets: AudioTestAssets): """Compare vLLM Mistral-format output against HF Transformers reference. Instead of requiring an exact text match (which is brittle across attention backends), we compare per-token logprobs using the standard check_logprobs_close helper: when tokens diverge at a position, each runner's chosen token must appear in the other's top-k logprobs. Marked xfail(strict=False) so remaining edge-case mismatches don't block CI. """ question = f"What's happening in these {len(audio_assets)} audio clips?" max_tokens = 10 num_logprobs = 5 audio_data = [asset.audio_and_sample_rate for asset in audio_assets] vllm_prompt = _get_prompt(audio_assets, question) with vllm_runner( MODEL_NAME, dtype="half", enforce_eager=True, tokenizer_mode="mistral", config_format="mistral", load_format="mistral", limit_mm_per_prompt={"audio": len(audio_assets)}, ) as vllm_model: vllm_outputs = vllm_model.generate_greedy_logprobs( [vllm_prompt], max_tokens, num_logprobs, audios=[audio_data], ) assert vllm_outputs[0][1], "vLLM inference produced empty output" with hf_runner( MODEL_NAME, dtype="half", auto_cls=VoxtralForConditionalGeneration, ) as hf_model: hf_model = model_utils.voxtral_patch_hf_runner(hf_model) hf_outputs = hf_model.generate_greedy_logprobs_limit( [question], max_tokens, num_logprobs, audios=[audio_data], ) assert hf_outputs[0][1], "HF Transformers produced empty output" print( f"HF Reference Comparison\n" f" vLLM: {vllm_outputs[0][1]!r}\n" f" HF: {hf_outputs[0][1]!r}" ) check_logprobs_close( outputs_0_lst=vllm_outputs, outputs_1_lst=hf_outputs, name_0="vllm", name_1="hf", )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/models/multimodal/generation/test_voxtral.py", "license": "Apache License 2.0", "lines": 180, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/model_executor/models/voxtral.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import math from collections.abc import Iterable, Mapping, Sequence from functools import cached_property, partial from math import ceil from typing import Literal, cast import numpy as np import regex as re import torch import torch.nn as nn from mistral_common.audio import mel_filter_bank from mistral_common.protocol.instruct.chunk import AudioChunk, RawAudio, TextChunk from mistral_common.protocol.instruct.messages import UserMessage from mistral_common.protocol.instruct.request import ChatCompletionRequest from mistral_common.protocol.transcription.request import TranscriptionRequest from mistral_common.tokens.tokenizers.audio import ( Audio, AudioEncoder, ) from transformers import BatchFeature, TensorType, WhisperConfig from transformers.tokenization_utils_base import TextInput from vllm.config import ModelConfig, SpeechToTextConfig, VllmConfig from vllm.config.multimodal import BaseDummyOptions from vllm.inputs.data import PromptType, TokensPrompt from vllm.logger import init_logger from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.model_loader.weight_utils import default_weight_loader from vllm.model_executor.models import SupportsPP from vllm.model_executor.models.module_mapping import MultiModelKeys from vllm.model_executor.models.whisper import ( WhisperEncoder, _create_fake_bias_for_k_proj, ) from vllm.model_executor.models.whisper_causal import WhisperCausalEncoder from vllm.multimodal import MULTIMODAL_REGISTRY from vllm.multimodal.inputs import ( MultiModalDataDict, MultiModalFieldConfig, MultiModalKwargsItems, NestedTensors, ) from vllm.multimodal.parse import ( AudioProcessorItems, MultiModalDataItems, MultiModalDataParser, ) from vllm.multimodal.processing import BaseDummyInputsBuilder from vllm.multimodal.processing.processor import ( BaseMultiModalProcessor, BaseProcessingInfo, MultiModalProcessingInfo, PlaceholderFeaturesInfo, ProcessorInputs, PromptReplacement, PromptUpdate, TimingContext, ) from vllm.sequence import IntermediateTensors from vllm.tokenizers import cached_tokenizer_from_config from vllm.tokenizers.mistral import MistralTokenizer from .interfaces import SupportsLoRA, SupportsMultiModal, SupportsTranscription from .utils import init_vllm_registered_model, maybe_prefix logger = init_logger(__name__) ISO639_1_SUPPORTED_LANGS = { "ar": "Arabic", "nl": "Dutch", "en": "English", "fr": "French", "de": "German", "hi": "Hindi", "it": "Italian", "pt": "Portuguese", "es": "Spanish", } class VoxtralProcessorAdapter: """ Provide a HF-compatible interface for :class:`mistral_common.tokens.tokenizers.multimodal.AudioEncoder`. """ def __init__(self, tokenizer: MistralTokenizer) -> None: super().__init__() self.tokenizer = tokenizer @cached_property def _audio_processor(self) -> AudioEncoder: audio_encoder = self.tokenizer.instruct.audio_encoder assert isinstance(audio_encoder, AudioEncoder) return audio_encoder @cached_property def audio_token_id(self) -> int: return self._audio_processor.special_ids.audio @cached_property def begin_audio_token_id(self) -> int: return self._audio_processor.special_ids.begin_audio @cached_property def sampling_rate(self) -> int: return self._audio_processor.audio_config.sampling_rate @cached_property def frame_rate(self) -> float: return self._audio_processor.audio_config.frame_rate def get_num_audio_tokens( self, audio_length: int, ) -> int: return ceil(audio_length / (self.sampling_rate // self.frame_rate)) def __call__( self, text: TextInput | list[TextInput] | None = None, audios: np.ndarray | list[np.ndarray] | None = None, return_tensors: str | TensorType | None = None, **kwargs, ) -> Mapping[str, NestedTensors]: if text is None: text = [] if not isinstance(text, list): text = [text] if audios is None: audios = [] if not isinstance(audios, list): audios = [audios] if not audios: input_ids = self.tokenizer(text).input_ids return {"input_ids": torch.tensor(input_ids)} # Allow dummy text, which is used for profiling as well as token inputs if any(len(t) > 0 for t in text): raise ValueError( "You've passed text inputs instead of token inputs. " "Make sure to process your input via `mistral_common`'s " "tokenizer or pass a chat completion request. " "For more info, see: " "https://github.com/vllm-project/vllm/issues/8411." ) audios_tokens = list[torch.Tensor]() audios_processed = list[torch.Tensor]() for audio in audios: assert isinstance(audio, np.ndarray) assert audio.ndim == 1 if not self._audio_processor.audio_config.is_streaming: audio = self._audio_processor.pad(audio, self.sampling_rate) audio_tokens = [self.begin_audio_token_id] + [ self.audio_token_id ] * self.get_num_audio_tokens(len(audio)) audios_tokens.append(torch.tensor(audio_tokens)) audios_processed.append(torch.tensor(audio)) return BatchFeature( { "input_ids": torch.cat(audios_tokens)[None].expand(len(text), -1), "audio_arrays": audios_processed, } ) class VoxtralProcessingInfo(BaseProcessingInfo): def get_tokenizer(self) -> MistralTokenizer: tokenizer = cached_tokenizer_from_config(self.ctx.model_config) if not isinstance(tokenizer, MistralTokenizer): raise ValueError("This model requires `--tokenizer-mode mistral`") return tokenizer def get_hf_processor(self) -> VoxtralProcessorAdapter: return VoxtralProcessorAdapter(self.get_tokenizer()) def get_data_parser(self): return MultiModalDataParser( target_sr=self.get_hf_processor().sampling_rate, target_channels=1, expected_hidden_size=self._get_expected_hidden_size(), ) def get_supported_mm_limits(self) -> Mapping[str, int | None]: return {"audio": 5} # Performance tends to degrade after 5 def get_mm_max_tokens_per_item( self, seq_len: int, mm_counts: Mapping[str, int], ) -> Mapping[str, int]: return {"audio": self.get_max_audio_tokens()} def get_max_audio_tokens(self) -> int: return self.ctx.model_config.max_model_len def get_max_audio_array_len(self) -> int: processor = self.get_hf_processor() return self.get_max_audio_tokens() * int( processor.sampling_rate // processor.frame_rate ) class VoxtralDummyInputsBuilder(BaseDummyInputsBuilder[VoxtralProcessingInfo]): def get_dummy_text(self, mm_counts: Mapping[str, int]) -> str: return "" def get_dummy_mm_data( self, seq_len: int, mm_counts: Mapping[str, int], mm_options: Mapping[str, BaseDummyOptions], ) -> MultiModalDataDict: num_audios = mm_counts.get("audio", 0) target_length = self.info.get_max_audio_array_len() audio_overrides = mm_options.get("audio") return { "audio": self._get_dummy_audios( length=target_length, num_audios=num_audios, overrides=audio_overrides, ) } def get_dummy_processor_inputs( self, seq_len: int, mm_counts: Mapping[str, int], mm_options: Mapping[str, BaseDummyOptions], ) -> ProcessorInputs: tokenizer = self.info.get_tokenizer() dummy_text = self.get_dummy_text(mm_counts) dummy_mm_data = self.get_dummy_mm_data(seq_len, mm_counts, mm_options) dummy_audios = dummy_mm_data.get("audio", []) audio_chunks: list[AudioChunk] = [] format = "wav" for audio in dummy_audios: audio_item = Audio( audio_array=audio, sampling_rate=self.info.get_hf_processor().sampling_rate, format=format, ) chunk = AudioChunk(input_audio=RawAudio.from_audio(audio_item)) audio_chunks.append(chunk) request = ChatCompletionRequest( messages=[ UserMessage(content=[TextChunk(text=dummy_text), *audio_chunks]), ] ) res = tokenizer.mistral.encode_chat_completion(request) dummy_tokens = res.tokens dummy_mm_items = self.info.parse_mm_data( # whixtral tokenizer adds padding to the audio # so we need to update the audio arrays {**dummy_mm_data, "audio": [a.audio_array for a in res.audios]}, ) return ProcessorInputs(prompt=dummy_tokens, mm_data_items=dummy_mm_items) class VoxtralMultiModalProcessor(BaseMultiModalProcessor[VoxtralProcessingInfo]): def _get_mm_fields_config( self, hf_inputs: Mapping[str, NestedTensors], hf_processor_mm_kwargs: Mapping[str, object], ) -> Mapping[str, MultiModalFieldConfig]: return dict(audio_arrays=MultiModalFieldConfig.batched("audio")) def _validate_mm_placeholders( self, mm_placeholders: Mapping[str, list[PlaceholderFeaturesInfo]], mm_item_counts: Mapping[str, int], ) -> None: # mistral_common's tokenizer's does not follow HF's placeholder norms # skip validation here ... def _apply_hf_processor_mm_only( self, mm_items: MultiModalDataItems, hf_processor_mm_kwargs: Mapping[str, object], tokenization_kwargs: Mapping[str, object], ) -> BatchFeature: processor = self.info.get_hf_processor(**hf_processor_mm_kwargs) processor_data, passthrough_data = self._get_hf_mm_data(mm_items) audios = processor_data.get("audios", []) if not isinstance(audios, list): audios = [audios] audio_config = processor._audio_processor.audio_config audio_tensors: list[torch.Tensor] = [] for audio in audios: audio = np.asarray(audio, dtype=np.float32).ravel() if not audio_config.is_streaming: audio = processor._audio_processor.pad( audio, processor.sampling_rate, audio_config.is_streaming, ) audio_tensors.append(torch.tensor(audio)) result = BatchFeature({"audio_arrays": audio_tensors} if audio_tensors else {}) result.update(passthrough_data) return result def _get_prompt_updates( self, mm_items: MultiModalDataItems, hf_processor_mm_kwargs: Mapping[str, object], out_mm_kwargs: MultiModalKwargsItems, ) -> Sequence[PromptUpdate]: processor = self.info.get_hf_processor(**hf_processor_mm_kwargs) audio_id = processor.audio_token_id out_mm_data = out_mm_kwargs.require_data() out_audio_items = out_mm_data.get("audio", []) def get_replacement(item_idx: int): if item_idx < len(out_audio_items): out_audio_data = out_audio_items[item_idx].get_data() audio_arr = out_audio_data["audio_arrays"] if isinstance(audio_arr, (torch.Tensor, np.ndarray)): audio_len = len(audio_arr) else: raise TypeError( "Unexpected type for audio_arrays in out_mm_kwargs: " f"{type(audio_arr)}" ) else: # Fallback for unexpected processor outputs. audios = mm_items.get_items("audio", AudioProcessorItems) audio_len = audios.get_audio_length(item_idx) nb_audio_tokens = processor.get_num_audio_tokens(audio_len) return [audio_id] * nb_audio_tokens return [ PromptReplacement( modality="audio", target="", # Never match the prompt (see below note) replacement=get_replacement, ), ] def _cached_apply_hf_processor( self, inputs: ProcessorInputs, timing_ctx: TimingContext, ) -> tuple[list[int], MultiModalProcessingInfo, bool]: prompt_ids, mm_info, _ = super()._cached_apply_hf_processor(inputs, timing_ctx) # NOTE: The tokens are already inserted by the chat template return prompt_ids, mm_info, True @MULTIMODAL_REGISTRY.register_processor( VoxtralMultiModalProcessor, info=VoxtralProcessingInfo, dummy_inputs=VoxtralDummyInputsBuilder, ) class VoxtralForConditionalGeneration( nn.Module, SupportsMultiModal, SupportsPP, SupportsLoRA, SupportsTranscription ): supported_languages = ISO639_1_SUPPORTED_LANGS # transformers' currently has limited support for MistralCommon backend # and cached_get_processor. Let's skip until fixed skip_warmup_audio_preprocessing = True packed_modules_mapping = { "qkv_proj": ["q_proj", "k_proj", "v_proj"], "gate_up_proj": ["gate_proj", "up_proj"], } def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__() self.tokenizer = cached_tokenizer_from_config(vllm_config.model_config) # update quant config to so that ignored module and target module names # match the vLLM model names if hasattr(vllm_config, "quant_config"): vllm_config.quant_config = self.maybe_update_quant_config( vllm_config.quant_config ) config = vllm_config.model_config.hf_config self.config = config self.downsample_factor = self.config.audio_config.downsample_factor with self._mark_language_model(vllm_config): self.language_model = init_vllm_registered_model( vllm_config=vllm_config, hf_config=config.text_config, prefix=maybe_prefix(prefix, "language_model"), ) with self._mark_tower_model(vllm_config, "audio"): self.whisper_encoder = VoxtralEncoderModel( vllm_config.with_hf_config(config.audio_config), prefix=maybe_prefix(prefix, "whisper_encoder"), ) self.audio_language_adapter = AudioLanguageAdapter( hidden_size=config.audio_config.d_model * self.downsample_factor, dim=config.text_config.hidden_size, ) def get_mm_mapping(self) -> MultiModelKeys: """Get module prefix for multimodal models to filter LoRA modules.""" return MultiModelKeys.from_string_field( language_model="language_model", connector="audio_language_adapter", tower_model=["whisper_encoder"], ) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, **kwargs: object, ) -> torch.Tensor | IntermediateTensors: if intermediate_tensors is not None: inputs_embeds = None hidden_states = self.language_model.model( input_ids, positions, intermediate_tensors, inputs_embeds=inputs_embeds ) return hidden_states def embed_multimodal( self, **kwargs ) -> list[torch.Tensor] | torch.Tensor | tuple[torch.Tensor, ...] | None: audio_inputs = self._parse_and_validate_audio_arrays(**kwargs) if audio_inputs is None: return None audio_embeddings = self.whisper_encoder(audio_inputs) for i, audio_embedding in enumerate(audio_embeddings): seq_len, dim = audio_embedding.shape # Pad such that seq_len is divisible by downsample_factor target_seq_len = self.downsample_factor * math.ceil( seq_len / self.downsample_factor ) audio_embedding = torch.nn.functional.pad( audio_embedding, (0, 0, 0, target_seq_len - seq_len), ) audio_embeddings[i] = audio_embedding.reshape( target_seq_len // self.downsample_factor, dim * self.downsample_factor ) # Concat, project and resplit audio_embeddings_packed = torch.cat(audio_embeddings, dim=0) audio_embeddings_packed = self.audio_language_adapter(audio_embeddings_packed) audio_embeddings = torch.split( audio_embeddings_packed, [a.shape[0] for a in audio_embeddings], dim=0 ) return audio_embeddings def _parse_and_validate_audio_arrays( self, **kwargs: object ) -> list[torch.Tensor] | None: audio_arrays = kwargs.pop("audio_arrays", None) if audio_arrays is None: return None if not isinstance(audio_arrays, (torch.Tensor, list)): raise ValueError( f"Incorrect type of audio_arrays. Got type: {type(audio_arrays)}" ) if isinstance(audio_arrays, torch.Tensor): audio_arrays = list(audio_arrays.unbind(0)) return audio_arrays def compute_logits( self, hidden_states: torch.Tensor, ) -> torch.Tensor | None: return self.language_model.compute_logits(hidden_states) @classmethod def get_speech_to_text_config( cls, model_config: ModelConfig, task_type: str ) -> SpeechToTextConfig: tokenizer = cached_tokenizer_from_config(model_config) audio_config = tokenizer.instruct.audio_encoder.audio_config max_audio_clip_s = audio_config.chunk_length_s sample_rate = audio_config.sampling_rate return SpeechToTextConfig( max_audio_clip_s=max_audio_clip_s, sample_rate=sample_rate, # mistral_common and whisper encoder take care of chunking min_energy_split_window_size=None, ) @classmethod # for speech-to-text transcription def get_generation_prompt( cls, audio: np.ndarray, model_config: ModelConfig, stt_config: SpeechToTextConfig, language: str | None, task_type: Literal["transcribe", "translate"], request_prompt: str, to_language: str | None, ) -> PromptType: tokenizer = cached_tokenizer_from_config(model_config) audio = Audio(audio, int(stt_config.sample_rate), format="wav") # lossless req = TranscriptionRequest( model=model_config.model, audio=RawAudio.from_audio(audio), language=language, ) tokenized = tokenizer.instruct.encode_transcription(req) return TokensPrompt( prompt_token_ids=tokenized.tokens, multi_modal_data={ "audio": [ (audio.audio_array, stt_config.sample_rate) for audio in tokenized.audios ], }, ) @classmethod def get_num_audio_tokens( cls, audio_duration_s: float, stt_config: SpeechToTextConfig, model_config: ModelConfig, ) -> int | None: """ Map from audio duration to number of audio tokens produced by the ASR model, without running a forward pass. This is used for estimating the amount of processing for this audio. """ tokenizer = cached_tokenizer_from_config(model_config) adapter = VoxtralProcessorAdapter(tokenizer) return adapter.get_num_audio_tokens( int(audio_duration_s * stt_config.sample_rate) ) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: remapping_rules = [ (r"mm_streams_embeddings.embedding_module\.(.*)", r"\1"), (r"mm_whisper_embeddings\.(.*)", r"\1"), (r"audio_language_projection\.(.*)", r"audio_language_adapter.\1"), ( r"audio_language_adapter\.0\.weight", r"audio_language_adapter.w_in.weight", ), ( r"audio_language_adapter\.2\.weight", r"audio_language_adapter.w_out.weight", ), ] audio_params = dict( nn.ModuleDict( { "audio_language_adapter": self.audio_language_adapter, } ).named_parameters() ) weights = _create_fake_bias_for_k_proj(weights, ".wk.weight") loaded_weights = set() def llm_weights_generator(): nonlocal loaded_weights for name, w in weights: is_encoder = False for k in [ "mm_whisper_embeddings", "mm_streams_embeddings.embedding_module", ]: is_encoder |= ( name.startswith(k) and not name.startswith(f"{k}.tok_embeddings") and not name.startswith(f"{k}.audio_language_projection") ) for pattern, repl in remapping_rules: if re.fullmatch(pattern, name): name = re.sub(pattern, repl, name) if is_encoder: name = self.whisper_encoder.load_weight((name, w)) loaded_weights.add(f"whisper_encoder.{name}") continue if name in audio_params: param = audio_params[name] with torch.no_grad(): default_weight_loader(param, w) loaded_weights.add(name) else: yield (name, w) for name in self.language_model.load_weights(llm_weights_generator()): loaded_weights.add(f"language_model.{name}") # potentially manually add position embeddings sin_key = "whisper_encoder.whisper_encoder.embed_positions.weight" if sin_key not in loaded_weights: # make sure we don't hit an error here loaded_weights.add(sin_key) return loaded_weights def maybe_update_quant_config( self, quant_config: QuantizationConfig ) -> QuantizationConfig: """ Update quant config to so that ignored module and target module names match the vLLM model names. Right now this is specific for compressed-tensors format and load_format mistral. """ remapping_rules = [ (r"output", r"language_model.lm_head"), ( r"layers\.(\d+)\.attention\.wo", r"language_model.model.layers.\1.self_attn.out_proj", ), ( r"layers\.(\d+)\.attention\.w(.*)", r"language_model.model.layers.\1.self_attn.\2_proj", ), ( r"layers\.(\d+)\.feed_forward\.w1", r"language_model.model.layers.\1.mlp.gate_proj", ), ( r"layers\.(\d+)\.feed_forward\.w2", r"language_model.model.layers.\1.mlp.down_proj", ), ( r"layers\.(\d+)\.feed_forward\.w3", r"language_model.model.layers.\1.mlp.up_proj", ), ( r"mm_whisper_embeddings\.whisper_encoder\.transformer\.layers\.(\d+)\.attention.w(.*)", r"whisper_encoder.whisper_encoder.layers.\1.layers.self_attn.\2_proj", ), ( r"mm_whisper_embeddings\.whisper_encoder\.transformer\.layers\.(\d+)\.attention.wo", r"whisper_encoder.whisper_encoder.layers.\1.layers.self_attn.out_proj", ), ( r"mm_whisper_embeddings\.whisper_encoder\.transformer\.layers\.(\d+)\.feed_forward.w(\d+)", r"whisper_encoder.whisper_encoder.layers.\1.layers.mlp.fc\2", ), ( r"mm_whisper_embeddings\.whisper_encoder\.conv_layers\.0", r"whisper_encoder.whisper_encoder.conv1", ), ( r"mm_whisper_embeddings\.whisper_encoder\.conv_layers\.1", r"whisper_encoder.whisper_encoder.conv2", ), ( r"mm_whisper_embeddings\.audio_language_projection\.0", r"audio_language_adapter.w_in", ), ( r"mm_whisper_embeddings\.audio_language_projection\.2", r"audio_language_adapter.w_out", ), ] # Update ignore list if hasattr(quant_config, "ignore"): mistral_ignore = [] for name in quant_config.ignore: mistral_name = name for pattern, repl in remapping_rules: if re.fullmatch(pattern, name): mistral_name = re.sub(pattern, repl, name) mistral_ignore.append(mistral_name) quant_config.ignore = mistral_ignore # Update target list if hasattr(quant_config, "config_groups"): config_groups = quant_config.config_groups for group_name in config_groups: if "targets" in config_groups[group_name]: targets = [] for name in config_groups[group_name]["targets"]: mistral_name = name for pattern, repl in remapping_rules: if re.fullmatch(pattern, name): mistral_name = re.sub(pattern, repl, name) targets.append(mistral_name) config_groups[group_name]["targets"] = targets quant_config.config_groups = config_groups return quant_config class AudioLanguageAdapter(nn.Module): def __init__(self, hidden_size: int, dim: int) -> None: super().__init__() self.w_in = nn.Linear(hidden_size, dim, bias=False) self.gelu = nn.GELU() self.w_out = nn.Linear(dim, dim, bias=False) def forward(self, x: torch.Tensor) -> torch.Tensor: return self.w_out(self.gelu(self.w_in(x))) class VoxtralEncoderModel(nn.Module): packed_modules_mapping = {"qkv_proj": ["q_proj", "k_proj", "v_proj"]} mistral_remapping = [ (r"mm_streams_embeddings.embedding_module\.(.*)", r"\1"), ( r"whisper_encoder\.conv_layers\.0\.(weight|bias)", r"whisper_encoder.conv1.\1", ), ( r"whisper_encoder\.conv_layers\.1\.(weight|bias)", r"whisper_encoder.conv2.\1", ), ( r"whisper_encoder\.conv_layers\.0\.conv\.(weight|bias)", r"whisper_encoder.conv1.\1", ), # noqa: E501 ( r"whisper_encoder\.conv_layers\.1\.conv\.(weight|bias)", r"whisper_encoder.conv2.\1", ), # noqa: E501 ( r"whisper_encoder\.transformer\.layers\.(\d+)\.attention\.w([qkv])\.(weight|bias)", # noqa: E501 r"whisper_encoder.layers.\1.self_attn.\2_proj.\3", ), ( r"whisper_encoder\.transformer\.layers\.(\d+)\.attention\.wo\.(weight|bias)", # noqa: E501 r"whisper_encoder.layers.\1.self_attn.out_proj.\2", ), ( r"whisper_encoder\.transformer\.layers\.(\d+)\.attention_norm\.(weight|bias)", # noqa: E501 r"whisper_encoder.layers.\1.self_attn_layer_norm.\2", ), ( r"whisper_encoder\.transformer\.layers\.(\d+)\.feed_forward\.w1\.(weight|bias)", # noqa: E501 r"whisper_encoder.layers.\1.mlp.fc1.\2", ), ( r"whisper_encoder\.transformer\.layers\.(\d+)\.feed_forward\.w2\.(weight|bias)", # noqa: E501 r"whisper_encoder.layers.\1.mlp.fc2.\2", ), ( r"whisper_encoder\.transformer\.layers\.(\d+)\.feed_forward\.w3\.(weight|bias)", r"whisper_encoder.layers.\1.mlp.fc3.\2", ), # noqa: E501 ( r"whisper_encoder\.transformer\.layers\.(\d+)\.ffn_norm\.(weight|bias)", r"whisper_encoder.layers.\1.final_layer_norm.\2", ), ( r"whisper_encoder\.transformer\.norm\.(weight|bias)", r"whisper_encoder.layer_norm.\1", ), ] def __init__( self, vllm_config: VllmConfig, *, prefix: str = "", ) -> None: super().__init__() self.config = cast(WhisperConfig, vllm_config.model_config.hf_config) self.dtype: torch.dtype = vllm_config.model_config.dtype self.is_causal = getattr(self.config, "is_causal", False) if self.is_causal: WhisperEncoderCls = WhisperCausalEncoder else: WhisperEncoderCls = partial(WhisperEncoder, init_in_fp32=True) self.whisper_encoder = WhisperEncoderCls( vllm_config=vllm_config, prefix=maybe_prefix(prefix, "whisper_encoder"), ) mel_filters = mel_filter_bank( num_frequency_bins=1 + self.config.window_size // 2, num_mel_bins=self.config.num_mel_bins, min_frequency=0.0, max_frequency=8000.0, sampling_rate=self.config.sampling_rate, ) self.mel_filters = torch.tensor(mel_filters, dtype=torch.float32) def compute_whisper_melspec( self, audio_waveforms: torch.Tensor, ) -> torch.Tensor: input_dtype = audio_waveforms.dtype window = torch.hann_window( self.config.window_size, device=audio_waveforms.device ) stft = torch.stft( audio_waveforms, self.config.window_size, self.config.hop_length, window=window, return_complex=True, ) magnitudes = stft[..., :-1].abs() ** 2 mel_spec = self.mel_filters.T @ magnitudes log_spec = torch.clamp(mel_spec, min=1e-10).log10() if global_log_mel_max := self.config.global_log_mel_max: if not isinstance(global_log_mel_max, float): raise TypeError(f"{global_log_mel_max=} needs to be of type float.") log_spec_max = torch.tensor( global_log_mel_max, device=log_spec.device, dtype=log_spec.dtype, ) else: log_spec_max = log_spec.max() log_spec = torch.maximum(log_spec, log_spec_max - 8.0) log_spec = (log_spec + 4.0) / 4.0 return log_spec.to(input_dtype) @property def downsample_factor(self) -> int: return ( self.whisper_encoder.conv1.stride[0] * self.whisper_encoder.conv2.stride[0] ) @property def chunk_size(self) -> int: return self.config.max_source_positions * self.downsample_factor def prepare_inputs_for_conv( self, audio_waveforms: list[torch.Tensor], ) -> tuple[torch.Tensor, list[int]]: assert isinstance(audio_waveforms, list) # list[num_mel_bins, seq_len] input_features = [ self.compute_whisper_melspec(audio).to(self.dtype) for audio in audio_waveforms ] chunked_features: list[torch.Tensor] = [] chunks_per_example: list[int] = [] for feature in input_features: chunks = feature.split(self.chunk_size, dim=-1) chunked_features += chunks chunks_per_example.append(len(chunks)) # [total_num_chunks, num_mel_bins, chunk_size] return torch.stack(chunked_features), chunks_per_example def forward( self, input_features: torch.Tensor | list[torch.Tensor] ) -> list[torch.Tensor]: if not isinstance(input_features, list): input_features = [input_features] # Split long inputs into chunks input_embeds, chunks_per_example = self.prepare_inputs_for_conv(input_features) # [total_num_chunks, ceil(chunk_size / downsample_factor), hidden_size] out = self.whisper_encoder([input_embeds]) # Re-concatenate the chunks chunk_idx = 0 results = [] for n_chunks in chunks_per_example: result = out[chunk_idx : chunk_idx + n_chunks].flatten(0, 1) results.append(result) chunk_idx += n_chunks return results def load_weight(self, weight: tuple[str, torch.Tensor]) -> str: stacked_params_mapping = [ # (param_name, shard_name, shard_id) ("qkv_proj", "q_proj", "q"), ("qkv_proj", "k_proj", "k"), ("qkv_proj", "v_proj", "v"), ] params_mapping = [] if self.is_causal: # For `WhisperCausalEncoder` we need # some more renaming stacked_params_mapping.extend( [ (".mlp.gate_up_proj", ".mlp.fc1", 0), (".mlp.gate_up_proj", ".mlp.fc3", 1), ] ) params_mapping.extend( [ (".mlp.down_proj", ".mlp.fc2"), ] ) params_dict = dict(self.named_parameters()) name, loaded_weight = weight for pattern, repl in self.mistral_remapping: if re.fullmatch(pattern, name): name = re.sub(pattern, repl, name) for param_name, weight_name, shard_id in stacked_params_mapping: if weight_name not in name: continue name = name.replace(weight_name, param_name) param = params_dict[name] weight_loader = param.weight_loader weight_loader(param, loaded_weight, shard_id) break else: for param_name, weight_name in params_mapping: if weight_name not in name: continue name = name.replace(weight_name, param_name) param = params_dict[name] weight_loader = getattr(param, "weight_loader", default_weight_loader) weight_loader(param, loaded_weight) return name
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/voxtral.py", "license": "Apache License 2.0", "lines": 830, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/v1/core/test_async_scheduler.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections import deque import pytest from vllm.v1.core.sched.output import SchedulerOutput from vllm.v1.outputs import ModelRunnerOutput from vllm.v1.request import RequestStatus from vllm.v1.utils import ConstantList from .utils import create_requests, create_scheduler pytestmark = pytest.mark.cpu_test def _make_model_runner_output( scheduler_output: SchedulerOutput, ) -> ModelRunnerOutput: req_ids = list(scheduler_output.num_scheduled_tokens.keys()) return ModelRunnerOutput( req_ids=req_ids, req_id_to_index={req_id: i for i, req_id in enumerate(req_ids)}, sampled_token_ids=[[i] for i in range(len(req_ids))], logprobs=None, prompt_logprobs_dict={}, pooler_output=[], ) @pytest.mark.parametrize("max_tokens", [1, 2, 3, 5]) def test_stop_by_max_tokens(max_tokens: int): scheduler = create_scheduler(async_scheduling=True) requests = create_requests(num_requests=2, max_tokens=max_tokens) req0, req1 = requests expected_total_num_scheduled_tokens = 0 sched_outputs: deque[SchedulerOutput] = deque() scheduler.add_request(req0) sched_outputs.append(scheduler.schedule()) expected_total_num_scheduled_tokens += req0.num_prompt_tokens + max_tokens - 1 scheduler.add_request(req1) sched_outputs.append(scheduler.schedule()) expected_total_num_scheduled_tokens += req1.num_prompt_tokens + max_tokens - 1 total_num_scheduled_tokens = 0 while sched_outputs: sched_output = sched_outputs.popleft() total_num_scheduled_tokens += sched_output.total_num_scheduled_tokens model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) assert scheduler.get_num_unfinished_requests() == 0 assert req0.num_output_tokens == max_tokens assert req1.num_output_tokens == max_tokens # Ensure we aren't scheduling more tokens than necessary. assert total_num_scheduled_tokens == expected_total_num_scheduled_tokens def test_abort(): scheduler = create_scheduler(async_scheduling=True) requests = create_requests(num_requests=10, max_tokens=20) for req in requests: scheduler.add_request(req) sched_outputs: deque[SchedulerOutput] = deque() sched_outputs.append(scheduler.schedule()) sched_outputs.append(scheduler.schedule()) abort_order = [0, 8, 3, 1, 6, 4, 2, 5, 7, 9] abort_order_copy = abort_order.copy() def abort_request(): if not abort_order: return req = requests[abort_order.pop(0)] scheduler.finish_requests(req.request_id, RequestStatus.FINISHED_ABORTED) while sched_outputs: # Abort a scheduled request. abort_request() sched_output = sched_outputs.popleft() model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) for i, req in enumerate(requests): assert req.status == RequestStatus.FINISHED_ABORTED assert req.num_output_tokens == abort_order_copy.index(i) def test_preempt(): scheduler = create_scheduler(async_scheduling=True) requests = create_requests(num_requests=10, max_tokens=20) for req in requests: scheduler.add_request(req) sched_outputs: deque[SchedulerOutput] = deque() sched_outputs.append(scheduler.schedule()) sched_outputs.append(scheduler.schedule()) abort_order = [0, 8, 3, 1, 6, 4, 2, 5, 7, 9] abort_order_copy = abort_order.copy() def abort_request(): if not abort_order: return req = requests[abort_order.pop(0)] scheduler.finish_requests(req.request_id, RequestStatus.FINISHED_ABORTED) while sched_outputs: # Abort a scheduled request. abort_request() sched_output = sched_outputs.popleft() model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) for i, req in enumerate(requests): assert req.status == RequestStatus.FINISHED_ABORTED assert req.num_output_tokens == abort_order_copy.index(i) def test_prefix_caching_for_prefill_dedup(): CHUNK_SIZE = 1000 BLOCK_SIZE = 16 num_prompt_tokens = 100 scheduler = create_scheduler( async_scheduling=True, max_num_batched_tokens=CHUNK_SIZE, enable_prefix_caching=True, block_size=BLOCK_SIZE, ) requests = create_requests( num_requests=5, num_tokens=num_prompt_tokens, max_tokens=3, same_prompt=True, block_size=BLOCK_SIZE, ) requests_copy = requests.copy() # Two requests with the same prompt. req0 = requests.pop(0) req1 = requests.pop(0) scheduler.add_request(req0) scheduler.add_request(req1) sched_outputs: deque[SchedulerOutput] = deque() sched_output = scheduler.schedule() sched_outputs.append(sched_output) # Make sure prefix caching de-duplicates the prompts in the same step, # so all the blocks except the last are shared between the two requests. assert len(sched_output.num_scheduled_tokens) == 2 num_blocks = num_prompt_tokens // BLOCK_SIZE assert req0.num_cached_tokens == 0 assert req1.num_cached_tokens >= num_blocks * BLOCK_SIZE sched_outputs.append(scheduler.schedule()) while sched_outputs: if requests: scheduler.add_request(requests.pop(0)) sched_output = sched_outputs.popleft() model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) # Other requests scheduled after the two requests should also get # prefix cache hit. assert scheduler.get_num_unfinished_requests() == 0 for req in requests_copy[1:]: assert req.num_cached_tokens >= num_blocks * BLOCK_SIZE def test_prefix_caching_for_multi_turn(): CHUNK_SIZE = 1000 BLOCK_SIZE = 16 num_prompt_tokens = 100 num_output_tokens = 200 scheduler = create_scheduler( async_scheduling=True, max_num_batched_tokens=CHUNK_SIZE, enable_prefix_caching=True, block_size=BLOCK_SIZE, ) requests = create_requests( num_requests=5, num_tokens=num_prompt_tokens, max_tokens=num_output_tokens, block_size=BLOCK_SIZE, ) for req in requests: scheduler.add_request(req) sched_outputs: deque[SchedulerOutput] = deque() sched_outputs.append(scheduler.schedule()) sched_outputs.append(scheduler.schedule()) # Process the requests. while sched_outputs: sched_output = sched_outputs.popleft() model_runner_output = _make_model_runner_output(sched_output) scheduler.update_from_output(sched_output, model_runner_output) sched_output = scheduler.schedule() if sched_output.num_scheduled_tokens: sched_outputs.append(sched_output) assert scheduler.get_num_unfinished_requests() == 0 # Create next-turn requests whose prompts are the full output of the # previous turn. next_turn_requests = create_requests( num_requests=5, num_tokens=num_prompt_tokens + num_output_tokens, max_tokens=num_output_tokens, block_size=BLOCK_SIZE, ) for i, req in enumerate(next_turn_requests): req.prompt_token_ids = requests[i].prompt_token_ids + list( requests[i].output_token_ids ) req._all_token_ids = req.prompt_token_ids.copy() req.all_token_ids = ConstantList(req._all_token_ids) req.block_hashes = [] req.update_block_hashes() # Schedule the next-turn requests. for req in next_turn_requests: scheduler.add_request(req) sched_outputs.append(scheduler.schedule()) # Make sure the next-turn requests get prefix cache hit by the previous # requests. for req in next_turn_requests: assert req.num_cached_tokens == req.num_prompt_tokens // BLOCK_SIZE * BLOCK_SIZE
{ "repo_id": "vllm-project/vllm", "file_path": "tests/v1/core/test_async_scheduler.py", "license": "Apache License 2.0", "lines": 205, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/v1/core/utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from tests.v1.kv_connector.unit.utils import MockKVConfig from vllm.config import ( CacheConfig, ECTransferConfig, KVTransferConfig, ModelConfig, ParallelConfig, SchedulerConfig, SpeculativeConfig, VllmConfig, ) from vllm.multimodal.inputs import ( MultiModalFeatureSpec, MultiModalKwargsItem, PlaceholderRange, ) from vllm.sampling_params import SamplingParams from vllm.utils.hashing import sha256 from vllm.v1.core.kv_cache_utils import get_request_block_hasher, init_none_hash from vllm.v1.core.sched.async_scheduler import AsyncScheduler from vllm.v1.core.sched.scheduler import Scheduler from vllm.v1.kv_cache_interface import ( FullAttentionSpec, KVCacheConfig, KVCacheGroupSpec, ) from vllm.v1.request import Request from vllm.v1.structured_output import StructuredOutputManager EOS_TOKEN_ID = 50256 def mock_kv(matched_tokens: int, is_async: bool): return MockKVConfig(matched_tokens=matched_tokens, is_async=is_async) def create_scheduler( model: str = "facebook/opt-125m", max_num_seqs: int = 16, max_num_batched_tokens: int = 8192, enable_chunked_prefill: bool = True, enable_prefix_caching: bool = False, long_prefill_token_threshold: int = 0, disable_chunked_mm_input: bool = False, use_kv_connector: None | bool | MockKVConfig = None, num_blocks: int = 10000, block_size: int = 16, max_model_len: int | None = None, num_speculative_tokens: int | None = None, skip_tokenizer_init: bool = False, async_scheduling: bool = False, pipeline_parallel_size: int = 1, use_ec_connector: bool = False, ec_role: str | None = None, ) -> Scheduler | AsyncScheduler: """Create scheduler under test. Args: model: model under test max_num_seqs: max sequences to schedule max_num_batch_tokens: max num tokens to batch enable_prefix_caching: optionally force APC config (True/False) or use default (False) Returns: {class}`Scheduler` instance """ model_config = ModelConfig( model=model, trust_remote_code=True, dtype="float16", seed=42, skip_tokenizer_init=skip_tokenizer_init, ) if max_model_len is None: max_model_len = max_num_batched_tokens scheduler_config = SchedulerConfig( max_num_seqs=max_num_seqs, max_num_batched_tokens=max_num_batched_tokens, max_model_len=max_model_len, long_prefill_token_threshold=long_prefill_token_threshold, disable_chunked_mm_input=disable_chunked_mm_input, enable_chunked_prefill=enable_chunked_prefill, async_scheduling=async_scheduling, is_encoder_decoder=model_config.is_encoder_decoder, ) # Cache config, optionally force APC cache_config = CacheConfig( block_size=block_size, gpu_memory_utilization=0.9, swap_space=0, cache_dtype="auto", enable_prefix_caching=enable_prefix_caching, ) kv_transfer_config = None if isinstance(use_kv_connector, MockKVConfig): kv_transfer_config = KVTransferConfig( kv_connector="MockKVConnector", kv_role="kv_both", kv_connector_extra_config={ "matched_tokens": use_kv_connector.matched_tokens, "is_async": use_kv_connector.is_async, }, ) elif use_kv_connector: kv_transfer_config = KVTransferConfig( kv_connector="ExampleConnector", kv_role="kv_both", kv_connector_extra_config={"shared_storage_path": "local_storage"}, ) speculative_config: SpeculativeConfig | None = None if num_speculative_tokens is not None: speculative_config = SpeculativeConfig( model="ngram", num_speculative_tokens=num_speculative_tokens ) ec_transfer_config = ( ECTransferConfig( ec_connector="ECExampleConnector", ec_role=ec_role, ec_connector_extra_config={"shared_storage_path": "/tmp/ec_test"}, ) if use_ec_connector else None ) vllm_config = VllmConfig( scheduler_config=scheduler_config, model_config=model_config, cache_config=cache_config, parallel_config=ParallelConfig(pipeline_parallel_size=pipeline_parallel_size), kv_transfer_config=kv_transfer_config, speculative_config=speculative_config, ec_transfer_config=ec_transfer_config, ) kv_cache_config = KVCacheConfig( num_blocks=num_blocks, # A large number of blocks to hold all requests kv_cache_tensors=[], kv_cache_groups=[ KVCacheGroupSpec( ["layer"], FullAttentionSpec( block_size=block_size, num_kv_heads=1, head_size=1, dtype=torch.float32, ), ) ], ) cache_config.num_gpu_blocks = num_blocks scheduler_cls = AsyncScheduler if async_scheduling else Scheduler return scheduler_cls( vllm_config=vllm_config, kv_cache_config=kv_cache_config, block_size=block_size, log_stats=True, structured_output_manager=StructuredOutputManager(vllm_config), ) _none_hash_initialized = False def create_requests( num_requests: int, num_tokens: int = 10, mm_hashes_list: list[list[str]] | None = None, mm_positions: list[list[PlaceholderRange]] | None = None, ignore_eos: bool = False, max_tokens: int = 16, stop_token_ids: list[int] | None = None, prompt_logprobs: int | None = None, same_prompt: bool = False, block_size: int = 16, req_ids: list[str] | None = None, ) -> list[Request]: global _none_hash_initialized if not _none_hash_initialized: init_none_hash(sha256) _none_hash_initialized = True block_hasher = get_request_block_hasher(block_size, sha256) sampling_params = SamplingParams( ignore_eos=ignore_eos, max_tokens=max_tokens, stop_token_ids=stop_token_ids, prompt_logprobs=prompt_logprobs, ) sampling_params.update_from_generation_config({}, EOS_TOKEN_ID) requests = [] if mm_hashes_list is not None: # NOTE: allow manual input; some mm items can have the same identifier # no. of mm_hashes and mm_positions for each request should be identical assert mm_positions is not None, ( "mm_positions must be provided when mm_hashes_list is provided" ) assert len(mm_hashes_list) == len(mm_positions) == num_requests assert [len(h) for h in mm_hashes_list] == [len(p) for p in mm_positions] # Since same identifier would imply they are identical encoder output # Verify mm items with identical identifier are having mm_position.length seen_hashes: dict[str, int] = {} if req_ids: assert len(req_ids) == num_requests else: req_ids = [f"{i}" for i in range(num_requests)] for i in range(num_requests): mm_features = [] for j, position in enumerate( mm_positions[i] if mm_positions is not None else [] ): if mm_hashes_list is not None: identifier = mm_hashes_list[i][j] # Verify if position length is identical position_length = position.length if identifier in seen_hashes: assert seen_hashes[identifier] == position_length, ( f"mm_hash '{identifier}' has inconsistent position lengths: " f"previously {seen_hashes[identifier]}, now {position_length} " f"at request {i}, position {j}" ) else: seen_hashes[identifier] = position_length else: # Unique dummy hash for each mm item identifier = f"hash{i}_{j}" mm_feature = MultiModalFeatureSpec( data=MultiModalKwargsItem.dummy(), mm_position=position, identifier=identifier, modality="image", ) mm_features.append(mm_feature) prompt_token_ids = [0] * num_tokens if same_prompt else [i] * num_tokens request = Request( request_id=req_ids[i], prompt_token_ids=prompt_token_ids, sampling_params=sampling_params, pooling_params=None, mm_features=mm_features if mm_features else None, block_hasher=block_hasher, ) requests.append(request) return requests
{ "repo_id": "vllm-project/vllm", "file_path": "tests/v1/core/utils.py", "license": "Apache License 2.0", "lines": 234, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/v1/core/sched/async_scheduler.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from vllm.logger import init_logger from vllm.v1.core.sched.output import SchedulerOutput from vllm.v1.core.sched.scheduler import Scheduler from vllm.v1.request import Request, RequestStatus logger = init_logger(__name__) class AsyncScheduler(Scheduler): def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) # reusable read-only placeholder list for speculative decoding. self._spec_token_placeholders: list[int] = [-1] * self.num_spec_tokens def _update_after_schedule(self, scheduler_output: SchedulerOutput) -> None: super()._update_after_schedule(scheduler_output) spec_decode_tokens = scheduler_output.scheduled_spec_decode_tokens for req_id in scheduler_output.num_scheduled_tokens: request = self.requests[req_id] if request.is_prefill_chunk: continue scheduler_output.pending_structured_output_tokens |= ( request.use_structured_output and request.num_output_placeholders > 0 ) # The request will generate a new token plus num_spec_tokens # in this scheduling step. cur_num_spec_tokens = len(spec_decode_tokens.get(req_id, ())) request.num_output_placeholders += 1 + cur_num_spec_tokens # Add placeholders for the new draft/spec tokens. # We will update the actual spec token ids in the worker process. request.spec_token_ids = self._spec_token_placeholders def _update_request_with_output( self, request: Request, new_token_ids: list[int] ) -> tuple[list[int], bool]: if request.discard_latest_async_tokens: # If the request is force preempted in reset_prefix_cache, we # should discard the latest async token. request.discard_latest_async_tokens = False return [], False status_before_update = request.status new_token_ids, stopped = super()._update_request_with_output( request, new_token_ids ) # Update the number of output placeholders. request.num_output_placeholders -= len(new_token_ids) assert request.num_output_placeholders >= 0 # Cache the new tokens. Preempted requests should be skipped. if status_before_update == RequestStatus.RUNNING: self.kv_cache_manager.cache_blocks( request, request.num_computed_tokens - request.num_output_placeholders ) return new_token_ids, stopped
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/core/sched/async_scheduler.py", "license": "Apache License 2.0", "lines": 50, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/bailing_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Adapted from # https://github.com/inclusionAI/Ling/blob/master/models/modeling_bailing_moe.py # Copyright 2023 The vLLM team. # Copyright 2023 Antgroup and The HuggingFace Inc. team. All rights reserved. # # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX # and OPT implementations in this library. It has been modified from its # original forms to accommodate minor architectural differences compared # to GPT-NeoX and OPT used by the Meta AI team that trained the model. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Inference-only BailingMoE model compatible with HuggingFace weights.""" from collections.abc import Iterable from itertools import islice import torch import torch.nn.functional as F from torch import nn from transformers.configuration_utils import PretrainedConfig from vllm.compilation.decorators import support_torch_compile from vllm.config import CacheConfig, VllmConfig from vllm.distributed import ( get_pp_group, get_tensor_model_parallel_rank, get_tensor_model_parallel_world_size, ) from vllm.model_executor.layers.activation import SiluAndMul from vllm.model_executor.layers.attention import Attention from vllm.model_executor.layers.fused_moe import SharedFusedMoE from vllm.model_executor.layers.layernorm import RMSNorm from vllm.model_executor.layers.linear import ( MergedColumnParallelLinear, QKVParallelLinear, RowParallelLinear, ) from vllm.model_executor.layers.logits_processor import LogitsProcessor from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.rotary_embedding import get_rope from vllm.model_executor.layers.vocab_parallel_embedding import ( ParallelLMHead, VocabParallelEmbedding, ) from vllm.model_executor.model_loader.weight_utils import default_weight_loader from vllm.sequence import IntermediateTensors from .interfaces import SupportsLoRA, SupportsPP from .utils import ( AutoWeightsLoader, PPMissingLayer, is_pp_missing_parameter, make_empty_intermediate_tensors_factory, make_layers, maybe_prefix, ) class BailingAttention(nn.Module): def __init__( self, config: PretrainedConfig, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, reduce_results: bool = True, prefix: str = "", ): super().__init__() self.hidden_size = config.hidden_size self.total_num_heads = config.num_attention_heads self.total_kv_heads = config.num_key_value_heads tp_size = get_tensor_model_parallel_world_size() assert self.total_num_heads % tp_size == 0 assert self.total_num_heads >= self.total_kv_heads self.num_heads = self.total_num_heads // tp_size self.head_dim = config.head_dim or (self.hidden_size // self.total_num_heads) self.q_size_per_rank = self.head_dim * self.num_heads self.num_kv_heads = max(1, self.total_kv_heads // tp_size) self.kv_size_per_rank = self.num_kv_heads * self.head_dim self.scale = self.head_dim**-0.5 self.use_qk_norm = getattr(config, "use_qk_norm", False) self.use_rmsnorm = getattr(config, "use_rmsnorm", False) self.query_key_value = QKVParallelLinear( self.hidden_size, self.head_dim, self.total_num_heads, self.total_kv_heads, bias=(config.use_bias or config.use_qkv_bias), quant_config=quant_config, prefix=f"{prefix}.query_key_value", ) if self.use_qk_norm: self.query_layernorm = ( RMSNorm(self.head_dim, eps=config.rms_norm_eps) if self.use_rmsnorm else nn.LayerNorm(self.head_dim, eps=1e-6) ) self.key_layernorm = ( RMSNorm(self.head_dim, eps=config.rms_norm_eps) if self.use_rmsnorm else nn.LayerNorm(self.head_dim, eps=1e-6) ) self.dense = RowParallelLinear( self.total_num_heads * self.head_dim, self.hidden_size, bias=config.use_bias, quant_config=quant_config, reduce_results=reduce_results, prefix=f"{prefix}.dense", ) rotary_dim = getattr(config, "rotary_dim", self.head_dim) config.rope_parameters["partial_rotary_factor"] = rotary_dim / self.head_dim self.rotary_emb = get_rope( self.head_dim, max_position=config.max_position_embeddings, rope_parameters=config.rope_parameters, is_neox_style=True, ) self.attn = Attention( self.num_heads, self.head_dim, self.scale, num_kv_heads=self.num_kv_heads, cache_config=cache_config, prefix=f"{prefix}.attn", ) def forward( self, hidden_states: torch.Tensor, position_ids: torch.Tensor, ) -> torch.Tensor: qkv, _ = self.query_key_value(hidden_states) q, k, v = qkv.split( [self.q_size_per_rank, self.kv_size_per_rank, self.kv_size_per_rank], dim=-1 ) if self.use_qk_norm: q = q.view(-1, self.num_heads, self.head_dim) k = k.view(-1, self.num_kv_heads, self.head_dim) q = self.query_layernorm(q) k = self.key_layernorm(k) q = q.view(-1, self.q_size_per_rank) k = k.view(-1, self.kv_size_per_rank) q, k = self.rotary_emb(position_ids, q, k) context_layer = self.attn(q, k, v) attn_output, _ = self.dense(context_layer) return attn_output class BailingMLP(nn.Module): def __init__( self, intermediate_size: int, config: PretrainedConfig, quant_config: QuantizationConfig | None = None, reduce_results: bool | None = True, prefix: str = "", ) -> None: super().__init__() self.gate_up_proj = MergedColumnParallelLinear( config.hidden_size, [intermediate_size] * 2, bias=config.use_bias, quant_config=quant_config, prefix=f"{prefix}.gate_up_proj", ) self.down_proj = RowParallelLinear( intermediate_size, config.hidden_size, bias=config.use_bias, quant_config=quant_config, reduce_results=reduce_results, prefix=f"{prefix}.down_proj", ) self.act_fn = SiluAndMul() def forward(self, x): x, _ = self.gate_up_proj(x) x = self.act_fn(x) x, _ = self.down_proj(x) return x class BailingMoE(nn.Module): def __init__( self, intermediate_size: int, config: PretrainedConfig, quant_config: QuantizationConfig | None = None, reduce_results: bool | None = True, prefix: str = "", ): super().__init__() self.tp_size = get_tensor_model_parallel_world_size() self.tp_rank = get_tensor_model_parallel_rank() self.num_experts = config.num_experts self.top_k = config.num_experts_per_tok self.norm_expert_prob = config.norm_topk_prob self.hidden_size = config.hidden_size self.quant_config = quant_config self.num_shared_experts = config.num_shared_experts self.score_function = getattr(config, "score_function", None) self.n_group = getattr(config, "n_group", None) self.topk_group = getattr(config, "topk_group", None) self.use_grouped_topk = self.n_group is not None and self.topk_group is not None self.routed_scaling_factor = getattr(config, "routed_scaling_factor", 1.0) router_dtype = getattr(config, "router_dtype", None) if router_dtype is None: self.router_dtype = None elif router_dtype == "fp32": self.router_dtype = torch.float32 else: self.router_dtype = torch.bfloat16 self.gate = nn.Linear( self.hidden_size, self.num_experts, bias=False, dtype=self.router_dtype, ) if getattr(config, "moe_router_enable_expert_bias", False): self.gate.expert_bias = nn.Parameter( torch.empty((config.num_experts,), dtype=torch.float32) ) else: self.gate.expert_bias = None self.correction_bias = ( self.gate.expert_bias.data if self.gate.expert_bias is not None else None ) if self.score_function is not None: assert ( self.score_function == "softmax" and self.correction_bias is None ) or ( self.score_function == "sigmoid" and self.correction_bias is not None ), ( "score_function and correction_bias should be in 2 combination (softmax, None) or (sigmoid, not None)" # noqa: E501 ) else: # default value for scoring_func self.score_function = "softmax" if self.num_shared_experts > 0: if hasattr(config, "moe_shared_expert_intermediate_size"): intermediate_size = config.moe_shared_expert_intermediate_size else: intermediate_size = config.moe_intermediate_size intermediate_size *= config.num_shared_experts self.shared_experts = BailingMLP( intermediate_size=intermediate_size, config=config, quant_config=quant_config, reduce_results=False, prefix=f"{prefix}.shared_experts", ) else: self.shared_experts = None self.experts = SharedFusedMoE( shared_experts=self.shared_experts, num_experts=self.num_experts, top_k=self.top_k, hidden_size=self.hidden_size, intermediate_size=config.moe_intermediate_size, reduce_results=False, renormalize=self.norm_expert_prob, quant_config=quant_config, prefix=f"{prefix}.experts", scoring_func=self.score_function, e_score_correction_bias=self.gate.expert_bias, num_expert_group=self.n_group, topk_group=self.topk_group, use_grouped_topk=self.use_grouped_topk, router_logits_dtype=self.router_dtype, ) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: num_tokens, hidden_size = hidden_states.shape hidden_states = hidden_states.view(-1, hidden_size) # router_logits: (num_tokens, n_experts) router_logits = self.gate(hidden_states.to(self.router_dtype)) router_logits = router_logits.to(hidden_states.dtype) final_hidden_states = self.experts( hidden_states=hidden_states, router_logits=router_logits ) if self.shared_experts is not None: shared_output, final_hidden_states = final_hidden_states else: shared_output = None final_hidden_states *= self.routed_scaling_factor if shared_output is not None: final_hidden_states = final_hidden_states + shared_output if self.tp_size > 1: final_hidden_states = self.experts.maybe_all_reduce_tensor_model_parallel( final_hidden_states ) return final_hidden_states.view(num_tokens, hidden_size) class BailingMoeBlock(nn.Module): def __init__( self, config: PretrainedConfig, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() layer_idx = int(prefix.split(".")[-1]) self.config = config hidden_size = config.hidden_size intermediate_size = config.intermediate_size self.input_layernorm = RMSNorm(hidden_size, eps=config.rms_norm_eps) self.attention = BailingAttention( config, cache_config, quant_config, prefix=f"{prefix}.attention" ) self.post_attention_layernorm = RMSNorm(hidden_size, eps=config.rms_norm_eps) # Choose MLP class based on the number of experts and layer index if layer_idx < config.first_k_dense_replace: mlp_class = BailingMLP else: mlp_class = BailingMoE self.mlp = mlp_class( intermediate_size, config, quant_config, True, prefix=f"{prefix}.mlp" ) def forward( self, hidden_states: torch.Tensor, position_ids: torch.Tensor, residual: torch.Tensor | None, ) -> torch.Tensor: if residual is None: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) else: hidden_states, residual = self.input_layernorm(hidden_states, residual) hidden_states = self.attention( hidden_states=hidden_states, position_ids=position_ids, ) hidden_states, residual = self.post_attention_layernorm(hidden_states, residual) hidden_states = self.mlp(hidden_states) return hidden_states, residual @support_torch_compile class BailingMoeModel(nn.Module): def __init__( self, *, vllm_config: VllmConfig, prefix: str = "", ): super().__init__() config = vllm_config.model_config.hf_config cache_config = vllm_config.cache_config quant_config = vllm_config.quant_config self.config = config self.vocab_size = config.vocab_size self.embed_dim = config.hidden_size self.tie_word_embeddings = getattr(config, "tie_word_embeddings", False) if get_pp_group().is_first_rank or ( self.tie_word_embeddings and get_pp_group().is_last_rank ): self.word_embeddings = VocabParallelEmbedding( self.vocab_size, self.embed_dim, quant_config=quant_config, prefix=f"{prefix}.word_embeddings", ) else: self.word_embeddings = PPMissingLayer() self.embedding_dropout = torch.nn.Dropout(config.embedding_dropout) self.start_layer, self.end_layer, self.layers = make_layers( config.num_hidden_layers, lambda prefix: BailingMoeBlock( config=config, cache_config=cache_config, quant_config=quant_config, prefix=prefix, ), prefix=f"{prefix}.layers", ) self.make_empty_intermediate_tensors = make_empty_intermediate_tensors_factory( ["hidden_states", "residual"], config.hidden_size ) if get_pp_group().is_last_rank: self.norm = RMSNorm(self.embed_dim, eps=config.rms_norm_eps) else: self.norm = PPMissingLayer() def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.word_embeddings(input_ids) def forward( self, input_ids: torch.Tensor | None, position_ids: torch.Tensor, intermediate_tensors: IntermediateTensors | None, inputs_embeds: torch.Tensor | None = None, ) -> torch.Tensor | IntermediateTensors: if get_pp_group().is_first_rank: if inputs_embeds is not None: hidden_states = inputs_embeds else: hidden_states = self.embed_input_ids(input_ids) residual = None else: assert intermediate_tensors is not None hidden_states = intermediate_tensors["hidden_states"] residual = intermediate_tensors["residual"] for layer in islice(self.layers, self.start_layer, self.end_layer): hidden_states, residual = layer( hidden_states, position_ids, residual, ) if not get_pp_group().is_last_rank: return IntermediateTensors( {"hidden_states": hidden_states, "residual": residual} ) else: if residual is None: hidden_states = self.norm(hidden_states) else: hidden_states, _ = self.norm(hidden_states, residual) return hidden_states def get_expert_mapping(self) -> list[tuple[str, str, int, str]]: return SharedFusedMoE.make_expert_params_mapping( self, ckpt_gate_proj_name="gate_proj", ckpt_down_proj_name="down_proj", ckpt_up_proj_name="up_proj", num_experts=self.config.num_experts, ) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: stacked_params_mapping = [ # (param_name, shard_name, shard_id) ("gate_up_proj", "gate_proj", 0), ("gate_up_proj", "up_proj", 1), ] params_dict = dict(self.named_parameters(remove_duplicate=False)) loaded_params: set[str] = set() expert_params_mapping = self.get_expert_mapping() for name, loaded_weight in weights: if ( hasattr(self.config, "norm_head") and self.config.norm_head and "lm_head.weight" in name ): loaded_weight = F.normalize(loaded_weight, dim=0, p=2, eps=1e-7) for param_name, weight_name, shard_id in stacked_params_mapping: if weight_name not in name: continue if "mlp.experts" in name: continue name = name.replace(weight_name, param_name) # Skip loading extra bias for GPTQ models. if name.endswith(".bias") and name not in params_dict: continue if name not in params_dict: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = param.weight_loader weight_loader(param, loaded_weight, shard_id) break else: for mapping in expert_params_mapping: param_name, weight_name, expert_id, shard_id = mapping if weight_name not in name: continue name = name.replace(weight_name, param_name) if is_pp_missing_parameter(name, self): continue if name not in params_dict: continue param = params_dict[name] weight_loader = param.weight_loader weight_loader( param, loaded_weight, name, shard_id=shard_id, expert_id=expert_id, ) break else: if name.endswith(".bias") and name not in params_dict: continue if name not in params_dict: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = getattr( param, "weight_loader", default_weight_loader ) weight_loader(param, loaded_weight) loaded_params.add(name) return loaded_params class BailingMoeForCausalLM(nn.Module, SupportsPP, SupportsLoRA): packed_modules_mapping = { "query_key_value": ["query_key_value"], "gate_up_proj": [ "gate_proj", "up_proj", ], } def __init__( self, *, vllm_config: VllmConfig, prefix: str = "", ) -> None: super().__init__() config = vllm_config.model_config.hf_config.get_text_config() vllm_config.model_config.hf_config = config quant_config = vllm_config.quant_config self.config = config self.quant_config = quant_config self.max_position_embeddings = config.max_position_embeddings self.model = BailingMoeModel( vllm_config=vllm_config, prefix=maybe_prefix(prefix, "model") ) self.tie_word_embeddings = getattr(config, "tie_word_embeddings", False) if get_pp_group().is_last_rank: if self.tie_word_embeddings: self.lm_head = self.model.word_embeddings else: self.lm_head = ParallelLMHead( config.vocab_size, config.hidden_size, quant_config=quant_config, prefix=maybe_prefix(prefix, "lm_head"), ) self.logits_processor = LogitsProcessor(config.vocab_size) else: self.lm_head = PPMissingLayer() self.make_empty_intermediate_tensors = ( self.model.make_empty_intermediate_tensors ) def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.model.embed_input_ids(input_ids) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, ) -> torch.Tensor | IntermediateTensors: model_output = self.model( input_ids, positions, intermediate_tensors, inputs_embeds ) return model_output def compute_logits( self, hidden_states: torch.Tensor, ) -> torch.Tensor | None: logits = self.logits_processor(self.lm_head, hidden_states) return logits def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: loader = AutoWeightsLoader( self, skip_prefixes=(["lm_head."] if self.tie_word_embeddings else None), ) return loader.load_weights(weights) def get_expert_mapping(self) -> list[tuple[str, str, int, str]]: return self.model.get_expert_mapping() class BailingMoeV2ForCausalLM(BailingMoeForCausalLM): pass
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/bailing_moe.py", "license": "Apache License 2.0", "lines": 561, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/kernels/moe/modular_kernel_tools/cli_args.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import argparse import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig from .common import Config from .mk_objects import ( MK_ALL_PREPARE_FINALIZE_TYPES, MK_FUSED_EXPERT_TYPES, MK_SINGLE_GPU_PREPARE_FINALIZE_TYPES, ) def make_config_arg_parser(description: str): def to_pf_class_type(s: str) -> mk.FusedMoEPrepareAndFinalize: for pf in MK_ALL_PREPARE_FINALIZE_TYPES: if pf.__name__ == s: return pf raise ValueError(f"Cannot find a PrepareFinalize type that matches {s}") def to_experts_class_type(s: str) -> mk.FusedMoEPermuteExpertsUnpermute: for fe in MK_FUSED_EXPERT_TYPES: if fe.__name__ == s: return fe raise ValueError(f"Cannot find a FusedExperts type that matches {s}") def to_quant_torch_dtype(s: str) -> torch.dtype: if s == "torch.float8_e4m3fn": return torch.float8_e4m3fn raise ValueError(f"Unsupported quant type {s}") parser = argparse.ArgumentParser(description=description) parser.add_argument( "--world-size", type=int, default=2, help="Number of ranks that participate in all2all", ) parser.add_argument( "--pf-type", type=to_pf_class_type, required=True, help=( "Choose a PrepareFinalize Type : " f"{[x.__name__ for x in MK_ALL_PREPARE_FINALIZE_TYPES]}" ), ) parser.add_argument( "--experts-type", type=to_experts_class_type, required=True, help=( f"Choose a FusedExpert type : {[x.__name__ for x in MK_FUSED_EXPERT_TYPES]}" ), ) parser.add_argument( "-m", nargs="+", type=int, default=[64], help="num tokens per rank", ) parser.add_argument( "-k", type=int, default=7168, help="hidden-size", ) parser.add_argument( "-n", type=int, default=1024, help="N dimension of the first fused-moe matmul", ) parser.add_argument( "--num-experts", type=int, default=32, help="Global num experts" ) parser.add_argument("--topk", nargs="+", type=int, default=[4, 1], help="num topk") parser.add_argument( "--fused-moe-chunk-size", type=int, help="Fused moe chunk size used for the non-batched fused experts impl.", ) # Quant args parser.add_argument( "--quant-dtype", type=to_quant_torch_dtype, help="Quant datatype" ) parser.add_argument( "--per-token-quantized-activations", action="store_true", help=("The input activations must be per-token quantized"), ) parser.add_argument( "--per-channel-quantized-weights", action="store_true", help="The weights must be per-channel quantized.", ) parser.add_argument( "--block-shape", nargs="+", type=int, help="Quantization block shape" ) # Torch trace profile generation args parser.add_argument( "--torch-trace-dir-path", type=str, default=None, help="Get torch trace for single execution", ) return parser def _validate_args(args: argparse.Namespace): if args.quant_dtype is not None: assert args.quant_dtype == torch.float8_e4m3fn if args.block_shape is not None: assert len(args.block_shape) == 2, ( f"block shape must have 2 elements. got {args.block_shape}" ) if args.experts_type in MK_SINGLE_GPU_PREPARE_FINALIZE_TYPES: assert args.world_size == 1, "Single GPU objects need world size set to 1" if args.torch_trace_dir_path is not None: from pathlib import Path assert Path(args.torch_trace_dir_path).is_dir(), ( f"Please create {args.torch_trace_dir_path}" ) def make_config(args: argparse.Namespace) -> Config: _validate_args(args) quant_config = None if args.quant_dtype is not None: quant_config = FusedMoEQuantConfig.make( quant_dtype=args.quant_dtype, per_act_token_quant=args.per_token_quantized_activations, per_out_ch_quant=args.per_channel_quantized_weights, block_shape=args.block_shape, ) return Config( Ms=args.m, K=args.k, N=args.n, E=args.num_experts, topks=args.topk, dtype=torch.bfloat16, # hard-code quant_config=quant_config, prepare_finalize_type=args.pf_type, fused_experts_type=args.experts_type, fused_moe_chunk_size=args.fused_moe_chunk_size, world_size=args.world_size, torch_trace_dir_path=args.torch_trace_dir_path, )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/modular_kernel_tools/cli_args.py", "license": "Apache License 2.0", "lines": 142, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/modular_kernel_tools/common.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from dataclasses import dataclass from typing import Any import torch import vllm._custom_ops as ops import vllm.model_executor.layers.fused_moe.modular_kernel as mk from tests.kernels.moe.utils import make_test_weights, per_token_cast_to_fp8 from tests.kernels.quantization.nvfp4_utils import ( FLOAT4_E2M1_MAX, FLOAT8_E4M3_MAX, dequantize_nvfp4_to_dtype, ) from tests.kernels.utils import torch_experts from vllm.config import VllmConfig from vllm.distributed import ( get_dp_group, get_pcp_group, get_tensor_model_parallel_world_size, ) from vllm.forward_context import set_forward_context from vllm.model_executor.layers.fused_moe import fused_topk from vllm.model_executor.layers.fused_moe.activation import MoEActivation from vllm.model_executor.layers.fused_moe.all2all_utils import ( maybe_make_prepare_finalize, ) from vllm.model_executor.layers.fused_moe.config import ( FusedMoEConfig, FusedMoEParallelConfig, FusedMoEQuantConfig, RoutingMethodType, ) from vllm.utils.import_utils import ( has_aiter, has_deep_ep, has_deep_gemm, has_mori, ) from .mk_objects import ( TestMoEQuantConfig, expert_info, make_fused_experts, prepare_finalize_info, ) from .parallel_utils import ProcessGroupInfo def _describe_tensor(t: torch.Tensor | None, name: str) -> str: if t is None: return f"{name} : None" else: return f"{name} : {t.shape} {t.dtype} {t.device}" @dataclass class Config: Ms: list[int] | int K: int N: int E: int topks: list[int] | int dtype: torch.dtype quant_config: TestMoEQuantConfig | None prepare_finalize_type: mk.FusedMoEPrepareAndFinalize fused_experts_type: mk.FusedMoEPermuteExpertsUnpermute fused_moe_chunk_size: int | None world_size: int torch_trace_dir_path: str | None = None def __post_init__(self): if self.quant_config is None: self.quant_config = TestMoEQuantConfig(None, False, False, None) def describe(self) -> str: s = "" s += "== Config:\n" s += f" world_size={self.world_size}\n" s += f" PF={self.prepare_finalize_type.__name__}\n" s += f" FE={self.fused_experts_type.__name__}\n" s += f" E={self.E}\n" s += f" Ms={self.Ms}\n" s += f" N={self.N}\n" s += f" K={self.K}\n" s += f" topk={self.topks}\n" s += f" dtype={self.dtype}\n" s += f" fused_moe_chunk_size={self.fused_moe_chunk_size}\n" s += " Quant:\n" if self.quant_config is not None: s += f" q_dtype={self.quant_dtype}\n" s += f" q_block_shape={self.quant_block_shape}\n" s += f" q_per_out_ch_quant={self.is_per_out_ch_quant}\n" s += f" q_per_act_token={self.is_per_act_token_quant}\n" else: s += " quant=None\n" return s @property def M(self) -> int: assert isinstance(self.Ms, int) return self.Ms @property def quant_dtype(self) -> torch.dtype | str | None: assert self.quant_config is not None return self.quant_config.quant_dtype @property def is_per_act_token_quant(self) -> bool: assert self.quant_config is not None return self.quant_config.per_act_token_quant @property def is_per_tensor_act_quant(self) -> bool: return not self.is_per_act_token_quant and self.quant_block_shape is None @property def is_per_out_ch_quant(self) -> bool: assert self.quant_config is not None return self.quant_config.per_out_ch_quant @property def quant_block_shape(self) -> list[int] | None: assert self.quant_config is not None return self.quant_config.block_shape @property def topk(self) -> int: assert isinstance(self.topks, int) return self.topks @property def num_local_experts(self) -> int: return self.E // self.world_size def make_env_data(self) -> tuple[VllmConfig, dict[Any, Any]]: """ make env data for vllm launch. """ vllm_config = VllmConfig() vllm_config.parallel_config.data_parallel_size = self.world_size vllm_config.parallel_config.enable_expert_parallel = True env_dict = { "VLLM_USE_DEEP_GEMM": str(int(self.needs_deep_gemm())), } vllm_config.parallel_config.all2all_backend = self.all2all_backend() if self.fused_moe_chunk_size is not None: env_dict.update( {"VLLM_FUSED_MOE_CHUNK_SIZE": str(self.fused_moe_chunk_size)} ) return vllm_config, env_dict def is_fp8_block_quantized(self): return ( self.quant_dtype == torch.float8_e4m3fn and self.quant_block_shape is not None ) def is_batched_prepare_finalize(self): info = prepare_finalize_info(self.prepare_finalize_type) return mk.FusedMoEActivationFormat.BatchedExperts == info.activation_format def is_batched_fused_experts(self): info = expert_info(self.fused_experts_type) return mk.FusedMoEActivationFormat.BatchedExperts == info.activation_format def is_standard_fused_experts(self): info = expert_info(self.fused_experts_type) return mk.FusedMoEActivationFormat.Standard == info.activation_format def fe_supported_types(self): info = expert_info(self.fused_experts_type) return info.supported_dtypes def pf_supported_types(self): info = prepare_finalize_info(self.prepare_finalize_type) return info.supported_dtypes def is_block_quant_supported(self): info = expert_info(self.fused_experts_type) return info.blocked_quantization_support def is_fe_supports_chunking(self): info = expert_info(self.fused_experts_type) return info.supports_chunking def supports_expert_map(self): info = expert_info(self.fused_experts_type) return info.supports_expert_map def supports_apply_weight_on_input(self): info = prepare_finalize_info(self.prepare_finalize_type) return info.supports_apply_weight_on_input def needs_deep_gemm(self): info = expert_info(self.fused_experts_type) return info.needs_deep_gemm def needs_deep_ep(self): info = prepare_finalize_info(self.prepare_finalize_type) return ( info.backend == "deepep_high_throughput" or info.backend == "deepep_low_latency" ) def needs_aiter(self): info = expert_info(self.fused_experts_type) return info.needs_aiter def needs_mori(self): info = prepare_finalize_info(self.prepare_finalize_type) return info.backend == "mori" def all2all_backend(self): info = prepare_finalize_info(self.prepare_finalize_type) return info.backend def is_valid(self) -> tuple[bool, str | None]: # Check prepare-finalize and fused-experts compatibility if self.is_batched_prepare_finalize(): if not self.is_batched_fused_experts(): return False, "Mismatched format." else: if not self.is_standard_fused_experts(): return False, "Mismatched format." use_chunking = self.fused_moe_chunk_size is not None if use_chunking and not self.is_fe_supports_chunking(): return False, "Chunking not supported." # Check quantization sanity if ( int(self.is_per_act_token_quant) + int(self.is_per_tensor_act_quant) + int(self.quant_block_shape is not None) ) > 1: # invalid quant config return False, f"Bad quant_config {self.quant_config}." # check type support if self.quant_dtype is None: if ( self.dtype not in self.pf_supported_types() or self.dtype not in self.fe_supported_types() ): return False, ( f"Unsupported type {self.dtype} not in " f"{self.pf_supported_types()} and " f"{self.fe_supported_types()}." ) else: if ( self.quant_dtype not in self.pf_supported_types() or self.quant_dtype not in self.fe_supported_types() ): return False, ( f"Unsupported quant type {self.quant_dtype} " f"not in {self.pf_supported_types()} and " f"{self.fe_supported_types()}." ) # Check block quantization support is_block_quantized = self.quant_block_shape is not None if is_block_quantized and self.quant_dtype is None: return False, "No block quantization support." if is_block_quantized and not self.is_block_quant_supported(): return False, "Mismatched block quantization support." # deep_gemm only works with block-quantized if self.needs_deep_gemm() and not is_block_quantized: return False, "Needs DeepGEMM but not block quantized." # Check dependencies (turn into asserts?) if self.needs_deep_ep() and not has_deep_ep(): return False, "Needs DeepEP, but DeepEP not available." if self.needs_deep_gemm() and not has_deep_gemm(): return False, "Needs DeepGEMM, but DeepGEMM not available." if self.needs_aiter() and not has_aiter(): # noqa: SIM103 return False, "Needs Aiter, but Aiter not available." if self.needs_mori() and not has_mori(): # noqa: SIM103 return False, "Needs MoRI, but MoRI not available." return True, None @dataclass class WeightTensors: w1: torch.Tensor w2: torch.Tensor w1_scale: torch.Tensor | None w2_scale: torch.Tensor | None w1_gs: torch.Tensor | None = None w2_gs: torch.Tensor | None = None def describe(self): s = "" s += "== Weight Tensors: \n" s += f" - {_describe_tensor(self.w1, 'w1')} \n" s += f" - {_describe_tensor(self.w2, 'w2')} \n" s += f" - {_describe_tensor(self.w1_scale, 'w1_scale')} \n" s += f" - {_describe_tensor(self.w2_scale, 'w2_scale')} \n" s += f" - {_describe_tensor(self.w1_gs, 'w1_gs')} \n" s += f" - {_describe_tensor(self.w2_gs, 'w2_gs')} \n" return s def is_quantized(self) -> bool: # or w1_scale is not None? return ( self.w1.dtype == torch.float8_e4m3fn or self.w1.dtype == torch.uint8 or self.w1.dtype == torch.int8 ) def to_current_device(self): device = torch.cuda.current_device() self.w1 = self.w1.to(device=device) self.w2 = self.w2.to(device=device) if self.w1_scale is not None: self.w1_scale = self.w1_scale.to(device=device) if self.w2_scale is not None: self.w2_scale = self.w2_scale.to(device=device) if self.w1_gs is not None: self.w1_gs = self.w1_gs.to(device=device) if self.w2_gs is not None: self.w2_gs = self.w2_gs.to(device=device) def slice_weights(self, rank: int, num_local_experts: int) -> "WeightTensors": s = rank * num_local_experts e = s + num_local_experts w1 = self.w1[s:e, :, :] w2 = self.w2[s:e, :, :] w1_scale = self.w1_scale[s:e, :, :] if self.w1_scale is not None else None w2_scale = self.w2_scale[s:e, :, :] if self.w2_scale is not None else None w1_gs = self.w1_gs[s:e] if self.w1_gs is not None else None w2_gs = self.w2_gs[s:e] if self.w2_gs is not None else None return WeightTensors(w1, w2, w1_scale, w2_scale, w1_gs, w2_gs) @staticmethod def make(config: Config) -> "WeightTensors": (_, w1, w1_scale, w1_gs), (_, w2, w2_scale, w2_gs) = make_test_weights( e=config.E, n=config.N, k=config.K, in_dtype=config.dtype, quant_dtype=config.quant_dtype, block_shape=config.quant_block_shape, # or config.is_per_out_ch_quant per_out_ch_quant=config.is_per_act_token_quant, ) return WeightTensors( w1=w1, w2=w2, w1_scale=w1_scale, w2_scale=w2_scale, w1_gs=w1_gs, w2_gs=w2_gs ) @dataclass class RankTensors: hidden_states: torch.Tensor hidden_states_scale: torch.Tensor | None topk_weights: torch.Tensor topk_ids: torch.Tensor expert_map: torch.Tensor | None def describe(self): s = "" s += "== Rank Tensors: \n" s += f" - {_describe_tensor(self.hidden_states, 'HS')} \n" s += f" - {_describe_tensor(self.hidden_states_scale, 'HS_scale')} \n" s += f" - {_describe_tensor(self.topk_weights, 'topk_weights')} \n" s += f" - {_describe_tensor(self.topk_ids, 'topk_ids')} \n" s += f" - {_describe_tensor(self.expert_map, 'expert_map')} \n" return s @staticmethod def make_hidden_states( config: Config, ) -> tuple[torch.Tensor, torch.Tensor | None]: """ Return hidden_states """ m, k, dtype = (config.M, config.K, config.dtype) a = torch.randn((m, k), device=torch.cuda.current_device(), dtype=dtype) / 15.0 if config.quant_dtype is None: return a, None # We dequant and use that as hidden_states so the tests are stable. # quantizing and dequantizing yield slightly different results # depending on the hardware. Here we, quantize and dequantize # first - so further quantize and dequantize will yield the same # values. if config.is_per_tensor_act_quant: a_q, a_scales = ops.scaled_fp8_quant(a, use_per_token_if_dynamic=False) return a_q.float().mul(a_scales).to(dtype), a_scales if config.is_per_act_token_quant: a_q, a_scales = ops.scaled_fp8_quant(a, use_per_token_if_dynamic=True) return a_q.float().mul(a_scales).to(dtype), None assert config.quant_block_shape is not None block_k = config.quant_block_shape[1] a_q, a_scales = per_token_cast_to_fp8(a, block_size=block_k) return a_q.float().view((-1, block_k)).mul(a_scales.view(-1, 1)).view(m, k).to( dtype ), None @staticmethod def make(config: Config, pgi: ProcessGroupInfo): dtype = config.dtype topk, m, _ = (config.topk, config.M, config.K) hidden_states, hidden_states_scale = RankTensors.make_hidden_states(config) num_local_experts, global_num_experts = (config.num_local_experts, config.E) score = torch.randn((m, global_num_experts), device="cuda", dtype=dtype) topk_weights, topk_ids, _ = fused_topk(hidden_states, score, topk, False) # distribute topk_ids evenly for mi in range(m): topk_ids[mi] = torch.randperm(config.E)[:topk] topk_ids = topk_ids.to(device=torch.cuda.current_device()) expert_map = None if config.world_size > 1 and config.supports_expert_map(): expert_map = torch.full( (global_num_experts,), fill_value=-1, dtype=torch.int32 ) s = pgi.rank * num_local_experts e = s + num_local_experts expert_map[s:e] = torch.tensor(list(range(num_local_experts))) expert_map = expert_map.to( device=torch.cuda.current_device(), dtype=torch.int32 ) return RankTensors( hidden_states=hidden_states, hidden_states_scale=hidden_states_scale, topk_weights=topk_weights, topk_ids=topk_ids, expert_map=expert_map, ) def reference_moe_impl( config: Config, weights: WeightTensors, rank_tensors: RankTensors ) -> torch.Tensor: if config.quant_dtype == "nvfp4": quant_blocksize = 16 dtype = config.dtype w1_q = weights.w1 w1_blockscale = weights.w1_scale w1_gs = weights.w1_gs w2_q = weights.w2 w2_blockscale = weights.w2_scale w2_gs = weights.w2_gs a_global_scale = ( (FLOAT8_E4M3_MAX * FLOAT4_E2M1_MAX) / torch.amax(rank_tensors.hidden_states.flatten(), dim=-1) ).to(torch.float32) assert w1_gs is not None assert w2_gs is not None assert w1_blockscale is not None assert w2_blockscale is not None assert w1_blockscale.shape[1] % 128 == 0 assert w1_blockscale.shape[2] % 4 == 0 assert w2_blockscale.shape[1] % 128 == 0 assert w2_blockscale.shape[2] % 4 == 0 a_fp4, a_scale_interleaved = ops.scaled_fp4_quant( rank_tensors.hidden_states, a_global_scale ) a = dequantize_nvfp4_to_dtype( a_fp4, a_scale_interleaved, a_global_scale, dtype=dtype, device=a_fp4.device, block_size=quant_blocksize, ) e = w1_q.shape[0] n = w1_q.shape[1] // 2 k = w2_q.shape[1] w1 = torch.zeros((e, 2 * n, k), device="cuda", dtype=dtype) w2 = torch.zeros((e, k, n), device="cuda", dtype=dtype) for idx in range(0, e): w1[idx] = dequantize_nvfp4_to_dtype( w1_q[idx], w1_blockscale[idx], w1_gs[idx], dtype=dtype, device=w1_q.device, block_size=quant_blocksize, ) w2[idx] = dequantize_nvfp4_to_dtype( w2_q[idx], w2_blockscale[idx], w2_gs[idx], dtype=dtype, device=w2_q.device, block_size=quant_blocksize, ) a_scale = None w1_scale = None w2_scale = None quant_dtype = None per_act_token_quant = False block_shape = None else: a = rank_tensors.hidden_states a_scale = rank_tensors.hidden_states_scale w1 = weights.w1 w1_scale = weights.w1_scale w2 = weights.w2 w2_scale = weights.w2_scale quant_dtype = config.quant_dtype per_act_token_quant = config.is_per_act_token_quant block_shape = config.quant_block_shape return torch_experts( a=a, w1=w1, w2=w2, topk_weight=rank_tensors.topk_weights, topk_ids=rank_tensors.topk_ids, global_num_experts=config.E, expert_map=None, w1_scale=w1_scale, w2_scale=w2_scale, a1_scale=a_scale, quant_dtype=quant_dtype, per_act_token_quant=per_act_token_quant, block_shape=block_shape, apply_router_weights_on_input=config.topk == 1 and config.supports_apply_weight_on_input(), ) def _make_gscale(num_experts: int) -> torch.Tensor: return torch.ones( (num_experts,), device=torch.cuda.current_device(), dtype=torch.float32 ) def make_modular_kernel( config: Config, vllm_config: VllmConfig, quant_config: FusedMoEQuantConfig, ) -> mk.FusedMoEModularKernel: def next_power_of_2(x): import math if x == 0: return 1 return 2 ** math.ceil(math.log2(x)) # make moe config moe_parallel_config: FusedMoEParallelConfig = FusedMoEParallelConfig.make( tp_size_=get_tensor_model_parallel_world_size(), pcp_size_=get_pcp_group().world_size, dp_size_=get_dp_group().world_size, sp_size_=1, vllm_parallel_config=vllm_config.parallel_config, ) moe = FusedMoEConfig( num_experts=config.E, experts_per_token=config.topk, hidden_dim=config.K, intermediate_size_per_partition=config.N, num_local_experts=config.num_local_experts, num_logical_experts=config.E, moe_parallel_config=moe_parallel_config, in_dtype=config.dtype, max_num_tokens=next_power_of_2(config.M), activation=MoEActivation.SILU, device=vllm_config.device_config.device, routing_method=RoutingMethodType.DeepSeekV3, ) prepare_finalize = maybe_make_prepare_finalize( moe=moe, quant_config=quant_config, allow_new_interface=True, ) assert prepare_finalize is not None fused_experts = make_fused_experts( config.fused_experts_type, moe, quant_config, prepare_finalize.num_dispatchers(), config.N, ) modular_kernel = mk.FusedMoEModularKernel( prepare_finalize=prepare_finalize, fused_experts=fused_experts, inplace=False, ) return modular_kernel def run_modular_kernel( pgi: ProcessGroupInfo, vllm_config: VllmConfig, config: Config, weights: WeightTensors, rank_tensors: RankTensors, ) -> torch.Tensor: assert isinstance(config.Ms, int) assert isinstance(config.topks, int) # weights for rank rank_weights = weights.slice_weights(pgi.rank, config.num_local_experts) if config.quant_dtype == "nvfp4": gscale = _make_gscale(config.num_local_experts) else: gscale = None quant_config = FusedMoEQuantConfig.make( config.quant_dtype, w1_scale=rank_weights.w1_scale, w2_scale=rank_weights.w2_scale, a1_scale=rank_tensors.hidden_states_scale, g1_alphas=(1 / rank_weights.w1_gs) if rank_weights.w1_gs is not None else None, g2_alphas=(1 / rank_weights.w2_gs) if rank_weights.w2_gs is not None else None, a1_gscale=gscale, a2_gscale=gscale, block_shape=config.quant_block_shape, per_act_token_quant=config.is_per_act_token_quant, per_out_ch_quant=config.is_per_out_ch_quant, ) mk = make_modular_kernel(config, vllm_config, quant_config) # impls might update the tensor in place hidden_states = rank_tensors.hidden_states.clone() topk_ids = rank_tensors.topk_ids.to(mk.prepare_finalize.topk_indices_dtype()) mk_kwargs = { "hidden_states": hidden_states, "w1": rank_weights.w1, "w2": rank_weights.w2, "topk_weights": rank_tensors.topk_weights, "topk_ids": topk_ids, "expert_map": rank_tensors.expert_map, "global_num_experts": config.E, "apply_router_weight_on_input": config.topk == 1 and config.supports_apply_weight_on_input(), } num_tokens = rank_tensors.hidden_states.shape[0] num_tokens_across_dp = torch.tensor( [num_tokens] * config.world_size, device="cuda", dtype=torch.int ) with set_forward_context( None, vllm_config, num_tokens=num_tokens, num_tokens_across_dp=num_tokens_across_dp, ): out = mk.forward(**mk_kwargs) return out
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/modular_kernel_tools/common.py", "license": "Apache License 2.0", "lines": 580, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/modular_kernel_tools/make_feature_matrix.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import copy from enum import Enum from itertools import product import torch from tqdm import tqdm from vllm.config import VllmConfig, set_current_vllm_config from vllm.model_executor.layers.fused_moe.config import FUSED_MOE_UNQUANTIZED_CONFIG from vllm.utils.torch_utils import set_random_seed from .common import ( Config, RankTensors, WeightTensors, reference_moe_impl, run_modular_kernel, ) from .mk_objects import ( MK_FUSED_EXPERT_TYPES, MK_MULTI_GPU_PREPARE_FINALIZE_TYPES, MK_QUANT_CONFIGS, ) from .parallel_utils import ProcessGroupInfo, parallel_launch_with_config class Result(Enum): PASS = 1 FAIL = 2 SKIP = 3 def rank_worker( pgi: ProcessGroupInfo, vllm_config: VllmConfig, cpu_group, config: Config, weights: WeightTensors, ): set_random_seed(pgi.rank) # sanity check from vllm import envs if config.fused_moe_chunk_size is not None: assert config.fused_moe_chunk_size == envs.VLLM_FUSED_MOE_CHUNK_SIZE # get weights to this device weights.to_current_device() Ms = config.Ms assert isinstance(Ms, list) TOPKs = config.topks assert isinstance(TOPKs, list) for m, topk in product(Ms, TOPKs): print(f"Running m={m}, topk={topk} ...") # override m and topk cfgx = copy.deepcopy(config) cfgx.Ms = m cfgx.topks = topk # inputs for rank rank_tensors = RankTensors.make(cfgx, pgi) # modular kernel out mk_out = run_modular_kernel(pgi, vllm_config, cfgx, weights, rank_tensors) with set_current_vllm_config(vllm_config): ref_out = reference_moe_impl(cfgx, weights, rank_tensors) torch.testing.assert_close(ref_out, mk_out, atol=3e-2, rtol=3e-2) def make_feature_matrix(csv_file_path: str): from dataclasses import asdict import pandas as pd def add_to_results( config: Config, success: Result, results_df: pd.DataFrame | None = None ): config_dict = asdict(config) config_dict["prepare_finalize_type"] = config_dict[ "prepare_finalize_type" ].__name__ config_dict["fused_experts_type"] = config_dict["fused_experts_type"].__name__ config_dict["per_tensor_act_quant"] = config.is_per_tensor_act_quant quant_config_dict = config_dict["quant_config"] del config_dict["quant_config"] if quant_config_dict is None: quant_config = FUSED_MOE_UNQUANTIZED_CONFIG quant_config_dict = asdict(quant_config) config_dict |= quant_config_dict result_dict = config_dict | {"success": success.name} result_df = pd.DataFrame([result_dict]) if results_df is None: results_df = result_df else: results_df = pd.concat([results_df, result_df], ignore_index=True) return results_df Ms = [64] Ks = [7168] # hidden sizes Ns = [2048] TOPKs = [[4, 1]] Es = [32] DTYPEs = [torch.bfloat16] PF_TYPES = MK_MULTI_GPU_PREPARE_FINALIZE_TYPES FE_TYPES = MK_FUSED_EXPERT_TYPES Q_TYPES = MK_QUANT_CONFIGS combinations = list( product(Ms, Ks, Ns, Es, TOPKs, DTYPEs, PF_TYPES, FE_TYPES, Q_TYPES) ) results_df: pd.DataFrame | None = None for m, k, n, e, topks, dtype, pf_type, experts_type, quant_config in tqdm( combinations ): config = Config( Ms=[m], K=k, N=n, E=e, topks=topks, dtype=dtype, prepare_finalize_type=pf_type, fused_experts_type=experts_type, quant_config=quant_config, world_size=2, fused_moe_chunk_size=None, ) success = None if config.is_valid()[0]: print(f"Running config : {config.describe()} ...") try: weights: WeightTensors = WeightTensors.make(config) vllm_config, env_dict = config.make_env_data() parallel_launch_with_config( config.world_size, rank_worker, vllm_config, env_dict, config, weights, ) success = Result.PASS except Exception as _: success = Result.FAIL else: success = Result.SKIP results_df = add_to_results(config, success, results_df) if results_df is not None: results_df.to_csv(f"{csv_file_path}") if __name__ == "__main__": import argparse from pathlib import Path parser = argparse.ArgumentParser( description=( "Make ModularKernel feature matrix \n" "Example : python3 -m tests.kernels.moe.modular_kernel_tools.make_feature_matrix " # noqa: E501 "-f ./feature_matrices/feature_matrix.csv" ) ) parser.add_argument( "-f", "--feature-matrix-csv-file-path", type=str, required=True, help="File name to Generate a .csv file", ) args = parser.parse_args() csv_path = args.feature_matrix_csv_file_path assert csv_path.endswith("csv"), ( f"Need a file path ending with .csv, got {csv_path}" ) assert Path(csv_path).parent.is_dir(), ( f"Cannot find parent directory for {Path(csv_path).parent}" ) make_feature_matrix(args.feature_matrix_csv_file_path)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/modular_kernel_tools/make_feature_matrix.py", "license": "Apache License 2.0", "lines": 160, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/modular_kernel_tools/mk_objects.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from dataclasses import dataclass import torch # Fused experts and PrepareFinalize imports import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.model_executor.layers.fused_moe import TritonExperts from vllm.model_executor.layers.fused_moe.batched_deep_gemm_moe import ( BatchedDeepGemmExperts, ) from vllm.model_executor.layers.fused_moe.config import ( FusedMoEConfig, FusedMoEQuantConfig, ) from vllm.model_executor.layers.fused_moe.deep_gemm_moe import DeepGemmExperts from vllm.model_executor.layers.fused_moe.fused_batched_moe import ( BatchedTritonExperts, NaiveBatchedExperts, ) from vllm.model_executor.layers.fused_moe.prepare_finalize import ( MoEPrepareAndFinalizeNoEP, ) from vllm.model_executor.layers.fused_moe.triton_deep_gemm_moe import ( TritonOrDeepGemmExperts, ) from vllm.model_executor.layers.quantization.utils.nvfp4_utils import ( cutlass_fp4_supported, ) from vllm.model_executor.layers.quantization.utils.w8a8_utils import ( cutlass_fp8_supported, ) from vllm.platforms import current_platform from vllm.utils.deep_gemm import is_deep_gemm_supported from vllm.utils.flashinfer import has_flashinfer_cutlass_fused_moe from vllm.utils.import_utils import ( has_aiter, has_deep_ep, has_deep_gemm, has_mori, ) @dataclass class TestMoEQuantConfig: quant_dtype: torch.dtype | str | None per_out_ch_quant: bool per_act_token_quant: bool block_shape: list[int] | None @dataclass class PrepareFinalizeInfo: activation_format: mk.FusedMoEActivationFormat supported_dtypes: list[torch.dtype | str] blocked_quantization_support: bool backend: str | None supports_apply_weight_on_input: bool = True @dataclass class ExpertInfo: activation_format: mk.FusedMoEActivationFormat supported_dtypes: list[torch.dtype | str] blocked_quantization_support: bool supports_chunking: bool supports_expert_map: bool needs_matching_quant: bool = False needs_deep_gemm: bool = False needs_aiter: bool = False PREPARE_FINALIZE_INFO: dict[mk.FusedMoEPrepareAndFinalize, PrepareFinalizeInfo] = {} EXPERT_INFO: dict[mk.FusedMoEPermuteExpertsUnpermute, ExpertInfo] = {} MK_ALL_PREPARE_FINALIZE_TYPES: list[mk.FusedMoEPrepareAndFinalize] = [] MK_MULTI_GPU_PREPARE_FINALIZE_TYPES: list[mk.FusedMoEPrepareAndFinalize] = [] MK_SINGLE_GPU_PREPARE_FINALIZE_TYPES: list[mk.FusedMoEPrepareAndFinalize] = [] MK_FUSED_EXPERT_TYPES: list[mk.FusedMoEPermuteExpertsUnpermute] = [] standard_format = mk.FusedMoEActivationFormat.Standard batched_format = mk.FusedMoEActivationFormat.BatchedExperts common_float_types: list[torch.dtype | str] = [ torch.float8_e4m3fn, torch.bfloat16, torch.float16, torch.float32, ] common_float_and_int_types = common_float_types + [torch.int8] nvfp4_types = ["nvfp4"] fp8_types = [torch.float8_e4m3fn] def register_prepare_and_finalize( kind, activation_format: mk.FusedMoEActivationFormat, supported_dtypes: list[torch.dtype | str], blocked_quantization_support: bool, backend: str | None, force_multigpu: bool = False, supports_apply_weight_on_input: bool = True, ): global PREPARE_FINALIZE_INFO global MK_ALL_PREPARE_FINALIZE_TYPES global MK_MULTI_GPU_PREPARE_FINALIZE_TYPES global MK_SINGLE_GPU_PREPARE_FINALIZE_TYPES assert kind not in PREPARE_FINALIZE_INFO PREPARE_FINALIZE_INFO[kind] = PrepareFinalizeInfo( activation_format, supported_dtypes, blocked_quantization_support, backend, supports_apply_weight_on_input, ) MK_ALL_PREPARE_FINALIZE_TYPES.append(kind) if backend is not None or force_multigpu: MK_MULTI_GPU_PREPARE_FINALIZE_TYPES.append(kind) else: MK_SINGLE_GPU_PREPARE_FINALIZE_TYPES.append(kind) def register_experts( kind, activation_format: mk.FusedMoEActivationFormat, supported_dtypes: list[torch.dtype | str], blocked_quantization_support: bool, supports_chunking: bool, supports_expert_map: bool, needs_matching_quant: bool = False, needs_deep_gemm: bool = False, needs_aiter: bool = False, ): global EXPERT_INFO global MK_FUSED_EXPERT_TYPES assert kind not in EXPERT_INFO EXPERT_INFO[kind] = ExpertInfo( activation_format, supported_dtypes, blocked_quantization_support, supports_chunking, supports_expert_map, needs_matching_quant, needs_deep_gemm, needs_aiter, ) MK_FUSED_EXPERT_TYPES.append(kind) def prepare_finalize_info(kind) -> PrepareFinalizeInfo: info = PREPARE_FINALIZE_INFO.get(kind) assert info is not None return info def expert_info(kind) -> ExpertInfo: info = EXPERT_INFO.get(kind) assert info is not None return info register_prepare_and_finalize( MoEPrepareAndFinalizeNoEP, standard_format, common_float_types, blocked_quantization_support=True, backend=None, ) register_experts( BatchedTritonExperts, batched_format, common_float_types, blocked_quantization_support=True, supports_chunking=False, supports_expert_map=False, needs_matching_quant=True, ) register_experts( TritonExperts, standard_format, common_float_and_int_types, blocked_quantization_support=True, supports_chunking=True, supports_expert_map=True, needs_matching_quant=True, ) register_experts( NaiveBatchedExperts, batched_format, common_float_and_int_types, blocked_quantization_support=True, supports_chunking=False, supports_expert_map=True, ) # Disable on blackwell for now if has_deep_ep() and not current_platform.has_device_capability(100): from vllm.model_executor.layers.fused_moe.deepep_ht_prepare_finalize import ( DeepEPHTPrepareAndFinalize, ) from vllm.model_executor.layers.fused_moe.deepep_ll_prepare_finalize import ( DeepEPLLPrepareAndFinalize, ) register_prepare_and_finalize( DeepEPHTPrepareAndFinalize, standard_format, common_float_types, blocked_quantization_support=True, backend="deepep_high_throughput", ) register_prepare_and_finalize( DeepEPLLPrepareAndFinalize, batched_format, common_float_types, blocked_quantization_support=True, backend="deepep_low_latency", ) if has_mori(): from vllm.model_executor.layers.fused_moe.mori_prepare_finalize import ( MoriPrepareAndFinalize, ) register_prepare_and_finalize( MoriPrepareAndFinalize, standard_format, fp8_types, blocked_quantization_support=True, backend="mori", supports_apply_weight_on_input=False, ) if has_flashinfer_cutlass_fused_moe() and current_platform.has_device_capability(100): from vllm.model_executor.layers.fused_moe.flashinfer_a2a_prepare_finalize import ( # noqa: E501 FlashInferCutlassMoEPrepareAndFinalize, ) from vllm.model_executor.layers.fused_moe.flashinfer_cutlass_moe import ( FlashInferExperts, ) register_prepare_and_finalize( FlashInferCutlassMoEPrepareAndFinalize, standard_format, nvfp4_types + fp8_types, blocked_quantization_support=True, backend=None, force_multigpu=True, supports_apply_weight_on_input=False, ) register_experts( FlashInferExperts, standard_format, nvfp4_types + fp8_types, blocked_quantization_support=True, supports_chunking=True, # Note: this is a hack to get it to run for now supports_expert_map=True, ) else: FlashInferCutlassMoEPrepareAndFinalize = None FlashInferExperts = None if has_aiter(): from vllm.model_executor.layers.fused_moe.rocm_aiter_fused_moe import ( AiterExperts, ) register_experts( AiterExperts, standard_format, fp8_types, blocked_quantization_support=True, supports_chunking=True, supports_expert_map=True, needs_aiter=True, ) else: AiterExperts = None if has_deep_gemm() and is_deep_gemm_supported(): register_experts( BatchedDeepGemmExperts, batched_format, fp8_types, blocked_quantization_support=True, supports_chunking=False, supports_expert_map=False, needs_matching_quant=False, needs_deep_gemm=True, ) register_experts( DeepGemmExperts, standard_format, fp8_types, blocked_quantization_support=True, supports_chunking=True, supports_expert_map=True, needs_matching_quant=False, needs_deep_gemm=True, ) register_experts( TritonOrDeepGemmExperts, standard_format, common_float_and_int_types, blocked_quantization_support=True, supports_chunking=True, supports_expert_map=True, needs_matching_quant=True, needs_deep_gemm=True, ) if cutlass_fp8_supported(): from vllm.model_executor.layers.fused_moe import ( CutlassBatchedExpertsFp8, CutlassExpertsFp8, ) register_experts( CutlassExpertsFp8, standard_format, fp8_types, blocked_quantization_support=False, supports_chunking=True, supports_expert_map=False, ) register_experts( CutlassBatchedExpertsFp8, batched_format, fp8_types, blocked_quantization_support=False, supports_chunking=False, supports_expert_map=False, ) else: CutlassBatchedExpertsFp8 = None CutlassExpertsFp8 = None if cutlass_fp4_supported(): from vllm.model_executor.layers.fused_moe.cutlass_moe import CutlassExpertsFp4 register_experts( CutlassExpertsFp4, standard_format, nvfp4_types, blocked_quantization_support=True, supports_chunking=True, supports_expert_map=False, ) else: CutlassExpertsFp4 = None MK_QUANT_CONFIGS: list[TestMoEQuantConfig | None] = [ None, # per-channel / per-column weights and per-tensor activations TestMoEQuantConfig( quant_dtype=torch.float8_e4m3fn, per_out_ch_quant=True, per_act_token_quant=False, block_shape=None, ), # per-channel / per-column weights and per-token activations TestMoEQuantConfig( quant_dtype=torch.float8_e4m3fn, per_out_ch_quant=True, per_act_token_quant=True, block_shape=None, ), # per-tensor weights and per-tensor activations TestMoEQuantConfig( quant_dtype=torch.float8_e4m3fn, per_out_ch_quant=False, per_act_token_quant=False, block_shape=None, ), # per-tensor weights and per-token activations TestMoEQuantConfig( quant_dtype=torch.float8_e4m3fn, per_out_ch_quant=False, per_act_token_quant=True, block_shape=None, ), # block-quantized weights and 128 block per-token activations TestMoEQuantConfig( quant_dtype=torch.float8_e4m3fn, per_out_ch_quant=False, per_act_token_quant=False, block_shape=[128, 128], ), # TODO (varun) : Should we test the following combinations ? # block-quantized weights and per-token activations # block-quantized weights and per-tensor activations ] if cutlass_fp4_supported() or has_flashinfer_cutlass_fused_moe(): MK_QUANT_CONFIGS += [ TestMoEQuantConfig( quant_dtype="nvfp4", per_out_ch_quant=False, per_act_token_quant=False, block_shape=None, ), ] def _slice(rank: int, num_local_experts: int, t: torch.Tensor) -> torch.Tensor: s = rank * num_local_experts e = s + num_local_experts return t[s:e] def make_cutlass_strides( e: int, n: int, k: int, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: ab_strides1 = torch.full((e,), k, device="cuda", dtype=torch.int64) ab_strides2 = torch.full((e,), n, device="cuda", dtype=torch.int64) c_strides1 = torch.full((e,), 2 * n, device="cuda", dtype=torch.int64) c_strides2 = torch.full((e,), k, device="cuda", dtype=torch.int64) return ab_strides1, ab_strides2, c_strides1, c_strides2 def make_fused_experts( fused_experts_type: mk.FusedMoEPermuteExpertsUnpermute, moe: FusedMoEConfig, quant_config: FusedMoEQuantConfig, num_dispatchers: int, N: int, ) -> mk.FusedMoEPermuteExpertsUnpermute: if ( fused_experts_type.activation_format() == mk.FusedMoEActivationFormat.BatchedExperts ): kwargs = { "moe_config": moe, "quant_config": quant_config, "max_num_tokens": moe.max_num_tokens, "num_dispatchers": num_dispatchers, } else: kwargs = { "moe_config": moe, "quant_config": quant_config, } torch.set_printoptions(threshold=0, edgeitems=0, linewidth=10000) print(f"Making {fused_experts_type.__class__.__name__} {kwargs} ...") experts = fused_experts_type(**kwargs) torch.set_printoptions(threshold=1000, edgeitems=5, linewidth=80) return experts
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/modular_kernel_tools/mk_objects.py", "license": "Apache License 2.0", "lines": 407, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/modular_kernel_tools/parallel_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import dataclasses import os import traceback from collections.abc import Callable from typing import Any, Concatenate import torch from torch.multiprocessing import spawn # pyright: ignore[reportPrivateImportUsage] from typing_extensions import ParamSpec from vllm.config import VllmConfig, set_current_vllm_config from vllm.distributed import init_distributed_environment, initialize_model_parallel from vllm.utils.network_utils import get_open_port ## Parallel Processes Utils P = ParamSpec("P") @dataclasses.dataclass class ProcessGroupInfo: world_size: int world_local_size: int rank: int node_rank: int local_rank: int device: torch.device def _set_vllm_config( vllm_config: VllmConfig, world_size: int, rank: int, local_rank: int ): import tempfile temp_file = tempfile.mkstemp()[1] with set_current_vllm_config(vllm_config): init_distributed_environment( world_size=world_size, rank=rank, distributed_init_method=f"file://{temp_file}", local_rank=local_rank, backend="nccl", ) initialize_model_parallel( tensor_model_parallel_size=vllm_config.parallel_config.tensor_parallel_size, pipeline_model_parallel_size=vllm_config.parallel_config.pipeline_parallel_size, ) cpu_group = torch.distributed.new_group(list(range(world_size)), backend="gloo") return cpu_group def _worker_parallel_launch( local_rank: int, world_size: int, world_local_size: int, node_rank: int, init_method: str, worker: Callable[Concatenate[ProcessGroupInfo, VllmConfig | None, Any, P], None], vllm_config: VllmConfig | None, env_dict: dict | None, *args: P.args, **kwargs: P.kwargs, ) -> None: rank = node_rank * world_local_size + local_rank torch.cuda.set_device(local_rank) device = torch.device("cuda", local_rank) torch.distributed.init_process_group( backend="cpu:gloo,cuda:nccl", init_method=init_method, rank=rank, world_size=world_size, device_id=device, ) barrier = torch.tensor([rank], device=device) torch.distributed.all_reduce(barrier) if env_dict is not None: os.environ.update(env_dict) cpu_group = None if vllm_config is not None: cpu_group = _set_vllm_config(vllm_config, world_size, rank, local_rank) try: worker( ProcessGroupInfo( world_size=world_size, world_local_size=world_local_size, rank=rank, node_rank=node_rank, local_rank=local_rank, device=device, ), vllm_config, cpu_group, *args, **kwargs, ) except Exception as ex: print(ex) traceback.print_exc() raise finally: torch.distributed.destroy_process_group() def parallel_launch_with_config( world_size: int, worker: Callable[Concatenate[ProcessGroupInfo, VllmConfig, Any, P], None], vllm_config: VllmConfig, env_dict: dict[Any, Any], *args: P.args, **kwargs: P.kwargs, ) -> None: assert not kwargs spawn( _worker_parallel_launch, args=( world_size, world_size, 0, f"tcp://{os.getenv('LOCALHOST', 'localhost')}:{get_open_port()}", worker, vllm_config, env_dict, ) + args, nprocs=world_size, join=True, )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/modular_kernel_tools/parallel_utils.py", "license": "Apache License 2.0", "lines": 116, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/modular_kernel_tools/profile_modular_kernel.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import copy from collections.abc import Callable from itertools import product from typing import Any import torch from vllm.config import VllmConfig from vllm.utils.torch_utils import set_random_seed from .common import Config, RankTensors, WeightTensors, make_modular_kernel from .parallel_utils import ProcessGroupInfo, parallel_launch_with_config def do_profile( fn: Callable, fn_kwargs: dict[Any, Any], pgi: ProcessGroupInfo, config: Config, num_warmups: int = 5, ): for _ in range(num_warmups): fn(**fn_kwargs) with torch.profiler.profile( activities=[ torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA, ], with_stack=True, record_shapes=True, ) as tprof: fn(**fn_kwargs) torch.cuda.synchronize(torch.cuda.current_device()) # TODO (varun): Add a descriptive trace file name tprof.export_chrome_trace( f"{config.torch_trace_dir_path}/m{config.M}_{pgi.rank}_trace.json" ) def profile_modular_kernel( pgi: ProcessGroupInfo, vllm_config: VllmConfig, config: Config, weights: WeightTensors, rank_tensors: RankTensors, ) -> None: assert isinstance(config.Ms, int) assert isinstance(config.topks, int) # weights for rank rank_weights = weights.slice_weights(pgi.rank, config.num_local_experts) # make modular kernel mk = make_modular_kernel(config, vllm_config, weights) mk_kwargs = { "hidden_states": rank_tensors.hidden_states, "w1": rank_weights.w1, "w2": rank_weights.w2, "topk_weights": rank_tensors.topk_weights, "topk_ids": rank_tensors.topk_ids, "expert_map": rank_tensors.expert_map, "w1_scale": rank_weights.w1_scale, "w2_scale": rank_weights.w2_scale, "a1_scale": rank_tensors.hidden_states_scale, "global_num_experts": config.E, "apply_router_weight_on_input": config.topk == 1, } do_profile(mk.forward, mk_kwargs, pgi, config) def rank_worker( pgi: ProcessGroupInfo, vllm_config: VllmConfig, cpu_group, config: Config, weights: WeightTensors, ): set_random_seed(pgi.rank) # sanity check from vllm import envs if config.fused_moe_chunk_size is not None: assert config.fused_moe_chunk_size == envs.VLLM_FUSED_MOE_CHUNK_SIZE # get weights to this device weights.to_current_device() Ms = config.Ms assert isinstance(Ms, list) TOPKs = config.topks assert isinstance(TOPKs, list) for m, topk in product(Ms, TOPKs): print(f"Running m={m}, topk={topk} ...") # override m and topk cfgx = copy.deepcopy(config) cfgx.Ms = m cfgx.topks = topk # inputs for rank rank_tensors = RankTensors.make(cfgx, pgi) profile_modular_kernel(pgi, vllm_config, cfgx, weights, rank_tensors) def run(config: Config): weights: WeightTensors = WeightTensors.make(config) vllm_config, env_dict = config.make_env_data() parallel_launch_with_config( config.world_size, rank_worker, vllm_config, env_dict, config, weights ) if __name__ == "__main__": from .cli_args import make_config, make_config_arg_parser parser = make_config_arg_parser( description=( "Run single prepare-finalize & fused-experts combination test" "Example : python3 -m tests.kernels.moe.modular_kernel_tools.profile_modular_kernel " # noqa: E501 "--pf-type DeepEPLLPrepareAndFinalize --experts-type BatchedTritonExperts" ) ) args = parser.parse_args() assert args.torch_trace_dir_path is not None, ( "Please pass in a directory to store torch traces" ) config = make_config(args) run(config)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/modular_kernel_tools/profile_modular_kernel.py", "license": "Apache License 2.0", "lines": 109, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/test_modular_kernel_combinations.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import copy import textwrap import traceback from itertools import product from typing import Any import pytest import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.config import VllmConfig, set_current_vllm_config from vllm.platforms import current_platform from vllm.utils.flashinfer import has_flashinfer_cutlass_fused_moe from vllm.utils.import_utils import has_deep_ep, has_deep_gemm from vllm.utils.torch_utils import cuda_device_count_stateless, set_random_seed from vllm.v1.worker.workspace import init_workspace_manager from .modular_kernel_tools.common import ( Config, RankTensors, WeightTensors, reference_moe_impl, run_modular_kernel, ) from .modular_kernel_tools.mk_objects import ( MK_FUSED_EXPERT_TYPES, MK_MULTI_GPU_PREPARE_FINALIZE_TYPES, MK_QUANT_CONFIGS, MK_SINGLE_GPU_PREPARE_FINALIZE_TYPES, TestMoEQuantConfig, expert_info, ) from .modular_kernel_tools.parallel_utils import ( ProcessGroupInfo, parallel_launch_with_config, ) has_any_multi_gpu_package = ( has_deep_ep() or has_deep_gemm() or has_flashinfer_cutlass_fused_moe() ) meets_multi_gpu_requirements = pytest.mark.skipif( not has_any_multi_gpu_package, reason="Requires deep_ep or deep_gemm or flashinfer packages", ) if current_platform.is_fp8_fnuz(): pytest.skip( "Tests in this file require float8_e4m3fn and platform does not support", allow_module_level=True, ) def format_result(verbose, msg, ex=None): if ex is not None: x = str(ex) newx = x.strip(" \n\t")[:16] if len(newx) < len(x): newx = newx + " ..." prefix = "E\t" print(f"{textwrap.indent(traceback.format_exc(), prefix)}") print(f"FAILED {msg} - {newx}\n") elif verbose: print(f"PASSED {msg}") else: print(".", end="") def rank_worker( pgi: ProcessGroupInfo, vllm_config: VllmConfig, cpu_group, base_config: Config, weights: WeightTensors, verbose: bool, ): # Initialize workspace manager in child process device = torch.device(f"cuda:{pgi.local_rank}") init_workspace_manager(device) set_random_seed(pgi.rank) # sanity check from vllm import envs if base_config.fused_moe_chunk_size is not None: assert base_config.fused_moe_chunk_size == envs.VLLM_FUSED_MOE_CHUNK_SIZE # get weights to this device weights.to_current_device() Ms = base_config.Ms assert isinstance(Ms, list) TOPKs = base_config.topks assert isinstance(TOPKs, list) exceptions = [] count = 0 for m, topk in product(Ms, TOPKs): # override m and topk config = copy.deepcopy(base_config) config.Ms = m config.topks = topk try: print(f"Running[{pgi.rank}]: m={m}, topk={topk} ...") count = count + 1 # inputs for rank rank_tensors = RankTensors.make(config, pgi) # modular kernel out mk_out = run_modular_kernel(pgi, vllm_config, config, weights, rank_tensors) with set_current_vllm_config(vllm_config): ref_out = reference_moe_impl(config, weights, rank_tensors) if config.quant_dtype == "nvfp4": atol = 1e-1 if config.K < 4096 else 2e-1 rtol = 1e-1 if config.K < 4096 else 2e-1 else: atol = 3e-2 rtol = 3e-2 torch.testing.assert_close(ref_out, mk_out, atol=atol, rtol=rtol) format_result(verbose, config.describe()) except Exception as ex: format_result(verbose, config.describe(), ex) exceptions.append(ex) if len(exceptions) > 0: raise RuntimeError( f"{len(exceptions)} of {count} tests failed in child process, " f"rank={pgi.rank}." ) else: print(f"{count} of {count} tests passed in child process, rank={pgi.rank}.") def run(config: Config, verbose: bool): assert config.is_valid()[0] assert not is_nyi_config(config) weights: WeightTensors = WeightTensors.make(config) vllm_config, env_dict = config.make_env_data() parallel_launch_with_config( config.world_size, rank_worker, vllm_config, env_dict, config, weights, verbose ) Ms = [32, 64] # hidden sizes, making this too large will cause fp4 tests to fail. # Also needs to be a multiple of 1024 for deep_gemm. Ks = [2048] Ns = [1024] TOPKs = [4, 1] Es = [32] DTYPEs = [torch.bfloat16] FUSED_MOE_CHUNK_SIZEs = [None, 16] def is_nyi_config(config: Config) -> bool: # We know these configs to be legitimate. but still fail. info = expert_info(config.fused_experts_type) if info.needs_matching_quant: # The triton kernels expect both per-act-token-quant and # per-out-ch-quant or neither. unsupported_quant_config = ( config.is_per_act_token_quant + config.is_per_out_ch_quant ) == 1 return unsupported_quant_config return not info.supports_expert_map def generate_valid_test_cases( world_size: int, prepare_finalize_types ) -> list[tuple[Any, ...]]: cases = [] total = 0 for k, n, e, dtype, quant_config, combination, chunk_size in product( Ks, Ns, Es, DTYPEs, MK_QUANT_CONFIGS, product(prepare_finalize_types, MK_FUSED_EXPERT_TYPES), FUSED_MOE_CHUNK_SIZEs, ): total = total + 1 config = Config( Ms=Ms, K=k, N=n, E=e, topks=TOPKs, dtype=dtype, quant_config=quant_config, prepare_finalize_type=combination[0], fused_experts_type=combination[1], fused_moe_chunk_size=chunk_size, world_size=world_size, ) # TODO(bnell): figure out how to get verbose flag here. verbose = False # pytestconfig.getoption('verbose') > 0 valid, reason = config.is_valid() if not valid: if verbose: print(f"Test config {config} is not valid: {reason}") continue if is_nyi_config(config): if verbose: print(f"Test config {config} is nyi.") continue cases.append( ( k, n, e, dtype, quant_config, combination[0], combination[1], chunk_size, world_size, ) ) print(f"{len(cases)} of {total} valid configs generated.") return cases @pytest.mark.parametrize( "k,n,e,dtype,quant_config,prepare_finalize_type,fused_experts_type,chunk_size,world_size", generate_valid_test_cases( world_size=2, prepare_finalize_types=MK_MULTI_GPU_PREPARE_FINALIZE_TYPES ), ) @meets_multi_gpu_requirements def test_modular_kernel_combinations_multigpu( k: int, n: int, e: int, dtype: torch.dtype, quant_config: TestMoEQuantConfig | None, prepare_finalize_type: mk.FusedMoEPrepareAndFinalize, fused_experts_type: mk.FusedMoEPermuteExpertsUnpermute, chunk_size: int | None, world_size: int, pytestconfig, ): if cuda_device_count_stateless() < world_size: pytest.skip( f"Not enough GPUs available to run, got " f"{cuda_device_count_stateless()} exepected " f"{world_size}." ) config = Config( Ms=Ms, K=k, N=n, E=e, topks=TOPKs, dtype=dtype, quant_config=quant_config, prepare_finalize_type=prepare_finalize_type, fused_experts_type=fused_experts_type, fused_moe_chunk_size=chunk_size, world_size=world_size, ) verbosity = pytestconfig.getoption("verbose") run(config, verbosity > 0) @pytest.mark.parametrize( "k,n,e,dtype,quant_config,prepare_finalize_type,fused_experts_type,chunk_size,world_size", generate_valid_test_cases( world_size=1, prepare_finalize_types=MK_SINGLE_GPU_PREPARE_FINALIZE_TYPES ), ) def test_modular_kernel_combinations_singlegpu( k: int, n: int, e: int, dtype: torch.dtype, quant_config: TestMoEQuantConfig | None, prepare_finalize_type: mk.FusedMoEPrepareAndFinalize, fused_experts_type: mk.FusedMoEPermuteExpertsUnpermute, chunk_size: int | None, world_size: int, pytestconfig, workspace_init, ): """Note: float8_e4m3fn is not supported on CUDA architecture < 89, and those tests will be skipped on unsupported hardware.""" config = Config( Ms=Ms, K=k, N=n, E=e, topks=TOPKs, dtype=dtype, quant_config=quant_config, prepare_finalize_type=prepare_finalize_type, fused_experts_type=fused_experts_type, fused_moe_chunk_size=chunk_size, world_size=world_size, ) if ( quant_config is not None and quant_config.quant_dtype == torch.float8_e4m3fn ) and not current_platform.has_device_capability(89): pytest.skip( "Triton limitation: fp8e4nv data type is not supported on CUDA arch < 89" ) verbosity = pytestconfig.getoption("verbose") run(config, verbosity > 0) if __name__ == "__main__": # Ability to test individual PrepareAndFinalize and FusedExperts combination from .modular_kernel_tools.cli_args import make_config, make_config_arg_parser parser = make_config_arg_parser( description=( "Run single prepare-finalize & fused-experts combination test" "Example : python3 -m tests.kernels.moe.test_modular_kernel_combinations " "--pf-type DeepEPLLPrepareAndFinalize --experts-type BatchedTritonExperts" ) ) args = parser.parse_args() config = make_config(args) run(config, True)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/test_modular_kernel_combinations.py", "license": "Apache License 2.0", "lines": 293, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/model_executor/layers/mamba/abstract.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from abc import abstractmethod from collections.abc import Iterable import torch from vllm.config import VllmConfig from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase from vllm.v1.attention.backend import AttentionBackend from vllm.v1.attention.selector import get_mamba_attn_backend from vllm.v1.kv_cache_interface import KVCacheSpec, MambaSpec class MambaBase(AttentionLayerBase): """ Base class for Mamba-like layers which support the v1 engine. Inherit from this class if you implement a custom layer. """ # Contains the KV cache (mamba state) for the layer # in the shape specified by `self.get_state_shape`. kv_cache: tuple[torch.Tensor, ...] @abstractmethod def get_state_shape(self) -> Iterable[tuple[int, ...]]: """ Defines the shape of the state. For mamba layers this is usually a (conv_state, ssm_state) tuple. In this case, returns (conv_state_shape, ssm_state_shape). """ pass @property @abstractmethod def mamba_type(self) -> str: pass @abstractmethod def get_state_dtype(self) -> tuple[torch.dtype, ...]: pass def get_kv_cache_spec(self, vllm_config: VllmConfig) -> KVCacheSpec | None: mamba_block_size = vllm_config.cache_config.mamba_block_size page_size_padded = vllm_config.cache_config.mamba_page_size_padded return MambaSpec( shapes=self.get_state_shape(), dtypes=self.get_state_dtype(), block_size=mamba_block_size, page_size_padded=page_size_padded, mamba_type=self.mamba_type, mamba_cache_mode=vllm_config.cache_config.mamba_cache_mode, num_speculative_blocks=( vllm_config.speculative_config.num_speculative_tokens if vllm_config.speculative_config else 0 ), ) def get_attn_backend(self) -> type[AttentionBackend]: """Get the attention backend class for this Mamba layer.""" return get_mamba_attn_backend(self.mamba_type)
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/mamba/abstract.py", "license": "Apache License 2.0", "lines": 52, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/layers/quantization/input_quant_fp8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import torch.nn.functional as F from vllm import _custom_ops as ops from vllm._aiter_ops import rocm_aiter_ops from vllm.model_executor.custom_op import CustomOp from vllm.model_executor.layers.quantization.utils.quant_utils import ( GroupShape, get_fp8_min_max, group_broadcast, prep_scale_for_group_broadcast, ) from vllm.platforms import current_platform from vllm.utils.deep_gemm import ( DeepGemmQuantScaleFMT, is_deep_gemm_e8m0_used, is_deep_gemm_supported, ) _FP8_DTYPE = current_platform.fp8_dtype() _FP8_MIN, _FP8_MAX = get_fp8_min_max() _FP8_MIN_SCALING_FACTOR = 1.0 / (_FP8_MAX * 512.0) # --8<-- [start:quant_fp8] @CustomOp.register("quant_fp8") class QuantFP8(CustomOp): """ Quantize input tensor to FP8 (per-tensor, per-token, per-channel, or per-group). This CustomOp supports both static and dynamic quantization. """ # --8<-- [end:quant_fp8] def __init__( self, static: bool, group_shape: GroupShape, num_token_padding: int | None = None, column_major_scales: bool = False, tma_aligned_scales: bool = False, use_ue8m0: bool | None = None, # for Torch compile compile_native: bool = True, ): """ :param static: static or dynamic quantization :param group_shape: quantization group shape (PER_TOKEN, PER_TENSOR, PER_CHANNEL, or arbitrary block size) :param num_token_padding: Pad the token dimension of output to this size :param tma_aligned_scales: For group quantization, output scales in TMA-aligned layout :param column_major_scales: For group quantization, output scales in column major format :param compile_native: Manually compile forward_native if compile mode > None """ super().__init__(compile_native=compile_native) self.static = static self.group_shape = group_shape self.use_per_token_if_dynamic = group_shape == GroupShape.PER_TOKEN self.num_token_padding = num_token_padding self.column_major_scales = column_major_scales self.tma_aligned_scales = tma_aligned_scales self.use_ue8m0 = is_deep_gemm_e8m0_used() if use_ue8m0 is None else use_ue8m0 self.use_deep_gemm_supported = is_deep_gemm_supported() self.use_aiter = rocm_aiter_ops.is_linear_fp8_enabled() self.is_group_quant = group_shape.is_per_group() if self.is_group_quant: self.group_size = group_shape.col else: self.use_per_token_if_dynamic = group_shape == GroupShape.PER_TOKEN if not static: assert group_shape in (GroupShape.PER_TOKEN, GroupShape.PER_TENSOR), ( "Only per-token or per-tensor scales are supported for dynamic " "non-group quantization." ) def forward_cuda( self, x: torch.Tensor, scale: torch.Tensor | None = None, scale_ub: torch.Tensor | None = None, use_triton: bool = False, ) -> tuple[torch.Tensor, torch.Tensor]: from vllm.model_executor.layers.quantization.utils import fp8_utils if ( self.is_group_quant and self.use_deep_gemm_supported and (DeepGemmQuantScaleFMT.from_oracle() == DeepGemmQuantScaleFMT.UE8M0) ): return fp8_utils.per_token_group_quant_fp8_packed_for_deepgemm( x, group_size=self.group_size, use_ue8m0=True, ) if self.is_group_quant and not self.static: assert scale is None, "Dynamic group quantization does not use scale" return fp8_utils.per_token_group_quant_fp8( x, group_size=self.group_size, column_major_scales=self.column_major_scales, tma_aligned_scales=self.tma_aligned_scales, dtype=_FP8_DTYPE, use_ue8m0=self.use_ue8m0, ) assert (scale is not None) == self.static assert scale_ub is None or ( not self.static and self.group_shape == GroupShape.PER_TOKEN and scale_ub.numel() == 1 ) return ops.scaled_fp8_quant( x, scale, num_token_padding=self.num_token_padding, scale_ub=scale_ub, use_per_token_if_dynamic=self.use_per_token_if_dynamic, group_shape=(self.group_shape.row, self.group_shape.col) if self.static else None, ) def forward_hip( self, x: torch.Tensor, scale: torch.Tensor | None = None, scale_ub: torch.Tensor | None = None, use_triton: bool = False, ) -> tuple[torch.Tensor, torch.Tensor]: if self.is_group_quant and use_triton: assert scale is None, "Dynamic group quantization does not use scale" return torch.ops.vllm.triton_per_token_group_quant_fp8(x, self.group_size) use_aiter_quant = self.use_aiter and scale_ub is None and x.is_contiguous() use_aiter_per_tensor_quant = ( use_aiter_quant and self.group_shape.is_per_tensor() ) use_aiter_per_token_quant = use_aiter_quant and self.group_shape.is_per_token() use_aiter_per_group_quant = use_aiter_quant and self.group_shape.is_per_group() if use_aiter_per_group_quant: return rocm_aiter_ops.group_fp8_quant(x, self.group_size) if use_aiter_per_tensor_quant: return rocm_aiter_ops.per_tensor_quant(x, _FP8_DTYPE, scale) if use_aiter_per_token_quant: return rocm_aiter_ops.per_token_quant(x, _FP8_DTYPE, scale) # Fallback to native implementation for group quantization. if self.is_group_quant: assert scale is None, "Dynamic group quantization does not use scale" return self._quantize_group_native(x) # Fallback to CUDA implementation return self.forward_cuda(x, scale, scale_ub) def forward_native( self, x: torch.Tensor, scale: torch.Tensor | None = None, scale_ub: torch.Tensor | None = None, use_triton: bool = False, ): if self.is_group_quant and not self.static: assert scale is None, "Dynamic group quantization does not use scale" return self._quantize_group_native(x) assert (scale is not None) == self.static assert scale_ub is None or ( not self.static and self.group_shape == GroupShape.PER_TOKEN and scale_ub.numel() == 1 ) if scale is None: if self.group_shape == GroupShape.PER_TOKEN: x_max, _ = x.abs().max(dim=-1) x_max = x_max.unsqueeze(-1).to(torch.float32) if scale_ub is not None: x_max = x_max.clamp(max=scale_ub) else: x_max = x.abs().max().unsqueeze(-1).to(torch.float32) scale = (x_max / _FP8_MAX).clamp(min=_FP8_MIN_SCALING_FACTOR) else: scale = prep_scale_for_group_broadcast(scale, x, self.group_shape) # Even for dynamic per-token scales, # reciprocal performs slightly better than division out = ( x.to(torch.float32) * group_broadcast(scale.to(torch.float32), x.shape[-2:]).reciprocal() ) out = out.clamp(_FP8_MIN, _FP8_MAX).to(_FP8_DTYPE) # This currently generates an extra Triton kernel in compilation. # Fortunately, we don't use padding if compiling. # TODO(luka): benchmark torch._scaled_mm to hopefully remove padding # in general. if self.num_token_padding is not None: padding = max(self.num_token_padding - out.size(0), 0) out = F.pad(out, (0, 0, 0, padding), "constant", 0.0) return out, scale def _quantize_group_native( self, x: torch.Tensor ) -> tuple[torch.Tensor, torch.Tensor]: orig_shape = x.shape hidden_dim = x.shape[-1] num_groups = (hidden_dim + self.group_size - 1) // self.group_size padded_dim = num_groups * self.group_size if padded_dim != hidden_dim: padding = padded_dim - hidden_dim x = F.pad(x, (0, padding), mode="constant", value=0.0) x_grouped = x.view(-1, num_groups, self.group_size) absmax = x_grouped.abs().max(dim=-1, keepdim=True)[0].float() scales_raw = absmax / _FP8_MAX if self.use_ue8m0: scales_raw = torch.exp2(torch.ceil(torch.log2(scales_raw))) scales = (scales_raw).clamp(min=_FP8_MIN_SCALING_FACTOR) x_scaled = x_grouped / scales x_quant = x_scaled.clamp(_FP8_MIN, _FP8_MAX).to(_FP8_DTYPE) x_quant = x_quant.view(-1, padded_dim) if padded_dim != hidden_dim: x_quant = x_quant[..., :hidden_dim] x_quant = x_quant.view(orig_shape) scales = scales.squeeze(-1) scales = scales.reshape(orig_shape[:-1] + (num_groups,)) if self.column_major_scales: scales = scales.transpose(-2, -1).contiguous().transpose(-1, -2) return x_quant, scales
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/quantization/input_quant_fp8.py", "license": "Apache License 2.0", "lines": 213, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/utils/deep_gemm.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Compatibility wrapper for DeepGEMM API changes. Users of vLLM should always import **only** these wrappers. """ import functools import importlib import os from collections.abc import Callable from enum import Enum from typing import Any, NoReturn import torch import vllm.envs as envs from vllm.logger import logger from vllm.model_executor.layers.quantization.utils.quant_utils import ( get_fp8_min_max, ) from vllm.platforms import current_platform from vllm.utils.import_utils import has_deep_gemm from vllm.utils.math_utils import cdiv class DeepGemmQuantScaleFMT(Enum): # Float32 scales in Float32 tensor FLOAT32 = 0 # Compute float32 scales and ceil the scales to UE8M0. # Keep the scales in Float32 tensor. FLOAT32_CEIL_UE8M0 = 1 # Compute float32 scales and ceil the scales to UE8M0. # Pack the scales into a int32 tensor where each int32 # element contains 4 scale values. UE8M0 = 2 @classmethod def init_oracle_cache(cls) -> None: """Initialize the oracle decision and store it in the class cache""" cached = getattr(cls, "_oracle_cache", None) if cached is not None: return use_e8m0 = ( envs.VLLM_USE_DEEP_GEMM_E8M0 and is_deep_gemm_supported() and (_fp8_gemm_nt_impl is not None) ) if not use_e8m0: cls._oracle_cache = cls.FLOAT32 # type: ignore return cls._oracle_cache = ( # type: ignore cls.UE8M0 if current_platform.is_device_capability_family(100) else cls.FLOAT32_CEIL_UE8M0 ) @classmethod def from_oracle(cls) -> "DeepGemmQuantScaleFMT": """Return the pre-initialized oracle decision""" cached = getattr(cls, "_oracle_cache", None) assert cached is not None, "DeepGemmQuantScaleFMT oracle cache not initialized" return cached @functools.cache def is_deep_gemm_supported() -> bool: """Return `True` if DeepGEMM is supported on the current platform. Currently, only Hopper and Blackwell GPUs are supported. """ is_supported_arch = current_platform.is_cuda() and ( current_platform.is_device_capability(90) or current_platform.is_device_capability_family(100) ) return envs.VLLM_USE_DEEP_GEMM and has_deep_gemm() and is_supported_arch @functools.cache def is_deep_gemm_e8m0_used() -> bool: """Return `True` if vLLM is configured to use DeepGEMM " "E8M0 scale on a Hopper or Blackwell-class GPU. """ if not is_deep_gemm_supported(): logger.debug_once( "DeepGEMM E8M0 disabled: DeepGEMM not supported on this system." ) return False _lazy_init() if _fp8_gemm_nt_impl is None: logger.info_once( "DeepGEMM E8M0 disabled: _fp8_gemm_nt_impl not found", scope="local" ) return False if envs.VLLM_USE_DEEP_GEMM_E8M0: logger.info_once("DeepGEMM E8M0 enabled on current platform.", scope="local") return True logger.info_once("DeepGEMM E8M0 disabled on current configuration.", scope="local") return False def _missing(*_: Any, **__: Any) -> NoReturn: """Placeholder for unavailable DeepGEMM backend.""" raise RuntimeError( "DeepGEMM backend is not available or outdated. Please install or " "update the `deep_gemm` to a newer version to enable FP8 kernels." ) _fp8_gemm_nt_impl: Callable[..., Any] | None = None _grouped_impl: Callable[..., Any] | None = None _grouped_masked_impl: Callable[..., Any] | None = None _fp8_mqa_logits_impl: Callable[..., Any] | None = None _fp8_paged_mqa_logits_impl: Callable[..., Any] | None = None _get_paged_mqa_logits_metadata_impl: Callable[..., Any] | None = None _get_mn_major_tma_aligned_tensor_impl: Callable[..., Any] | None = None _get_mk_alignment_for_contiguous_layout_impl: Callable[..., Any] | None = None _transform_sf_into_required_layout_impl: Callable[..., Any] | None = None def _lazy_init() -> None: """Import deep_gemm and resolve symbols on first use.""" global _fp8_gemm_nt_impl, _grouped_impl, _grouped_masked_impl global _fp8_mqa_logits_impl, _fp8_paged_mqa_logits_impl global _get_paged_mqa_logits_metadata_impl global _get_mn_major_tma_aligned_tensor_impl global _get_mk_alignment_for_contiguous_layout_impl global _transform_sf_into_required_layout_impl # fast path if ( _fp8_gemm_nt_impl is not None or _grouped_impl is not None or _grouped_masked_impl is not None or _fp8_mqa_logits_impl is not None or _fp8_paged_mqa_logits_impl is not None or _get_paged_mqa_logits_metadata_impl is not None or _get_mk_alignment_for_contiguous_layout_impl is not None or _transform_sf_into_required_layout_impl is not None ): return if not has_deep_gemm(): return # Set up deep_gemm cache path DEEP_GEMM_JIT_CACHE_ENV_NAME = "DG_JIT_CACHE_DIR" if not os.environ.get(DEEP_GEMM_JIT_CACHE_ENV_NAME, None): os.environ[DEEP_GEMM_JIT_CACHE_ENV_NAME] = os.path.join( envs.VLLM_CACHE_ROOT, "deep_gemm" ) _dg = importlib.import_module("deep_gemm") _fp8_gemm_nt_impl = getattr(_dg, "fp8_gemm_nt", None) _grouped_impl = getattr(_dg, "m_grouped_fp8_gemm_nt_contiguous", None) _grouped_masked_impl = getattr(_dg, "fp8_m_grouped_gemm_nt_masked", None) _fp8_mqa_logits_impl = getattr(_dg, "fp8_mqa_logits", None) _fp8_paged_mqa_logits_impl = getattr(_dg, "fp8_paged_mqa_logits", None) _get_paged_mqa_logits_metadata_impl = getattr( _dg, "get_paged_mqa_logits_metadata", None ) _get_mn_major_tma_aligned_tensor_impl = getattr( _dg, "get_mn_major_tma_aligned_tensor", None ) _get_mk_alignment_for_contiguous_layout_impl = getattr( _dg, "get_mk_alignment_for_contiguous_layout", None ) _transform_sf_into_required_layout_impl = getattr( _dg, "transform_sf_into_required_layout", None ) DeepGemmQuantScaleFMT.init_oracle_cache() def get_num_sms() -> int: _lazy_init() _dg = importlib.import_module("deep_gemm") return int(_dg.get_num_sms()) @functools.cache def get_mk_alignment_for_contiguous_layout() -> list[int]: _lazy_init() if _get_mk_alignment_for_contiguous_layout_impl is None: return _missing() mk_align_size = _get_mk_alignment_for_contiguous_layout_impl() return [mk_align_size, mk_align_size] def get_col_major_tma_aligned_tensor(x: torch.Tensor) -> torch.Tensor: """Wrapper for DeepGEMM's get_mn_major_tma_aligned_tensor""" _lazy_init() if _get_mn_major_tma_aligned_tensor_impl is None: return _missing() return _get_mn_major_tma_aligned_tensor_impl(x) def fp8_gemm_nt(*args, **kwargs): _lazy_init() if _fp8_gemm_nt_impl is None: return _missing(*args, **kwargs) if "is_deep_gemm_e8m0_used" in kwargs: use_ue8m0 = kwargs["is_deep_gemm_e8m0_used"] del kwargs["is_deep_gemm_e8m0_used"] else: use_ue8m0 = is_deep_gemm_e8m0_used() return _fp8_gemm_nt_impl(*args, disable_ue8m0_cast=not use_ue8m0, **kwargs) def m_grouped_fp8_gemm_nt_contiguous(*args, **kwargs): _lazy_init() if _grouped_impl is None: return _missing(*args, **kwargs) return _grouped_impl( *args, disable_ue8m0_cast=not is_deep_gemm_e8m0_used(), **kwargs ) def fp8_m_grouped_gemm_nt_masked(*args, **kwargs): _lazy_init() if _grouped_masked_impl is None: return _missing(*args, **kwargs) return _grouped_masked_impl( *args, disable_ue8m0_cast=not is_deep_gemm_e8m0_used(), **kwargs ) def transform_sf_into_required_layout(*args, **kwargs): _lazy_init() if _transform_sf_into_required_layout_impl is None: return _missing(*args, **kwargs) return _transform_sf_into_required_layout_impl( *args, disable_ue8m0_cast=not is_deep_gemm_e8m0_used(), **kwargs ) def fp8_mqa_logits( q: torch.Tensor, kv: tuple[torch.Tensor, torch.Tensor], weights: torch.Tensor, cu_seqlen_ks: torch.Tensor, cu_seqlen_ke: torch.Tensor, clean_logits: bool, ) -> torch.Tensor: """Compute FP8 MQA logits for a single sequence without KV paging. Args: q: Query tensor of shape [M, H, D]. Casted to `torch.float8_e4m3fn` by caller. kv: Tuple `(k_fp8, k_scales)` where `k_fp8` has shape [N, D] with dtype `torch.float8_e4m3fn` and `k_scales` has shape [N]) with dtype `torch.float32`. weights: weights of shape [M, H], dtype `torch.float32`. cu_seqlen_ks: Start indices (inclusive) for valid K per query position, shape [M], dtype int32. cu_seqlen_ke: End indices (exclusive) for valid K per query position, shape [M], dtype int32. clean_logits: Whether to clean the unfilled logits into `-inf`. Returns: Logits tensor of shape [M, N], dtype `torch.float32`. """ _lazy_init() if _fp8_mqa_logits_impl is None: return _missing() return _fp8_mqa_logits_impl( q, kv, weights, cu_seqlen_ks, cu_seqlen_ke, clean_logits=clean_logits ) def get_paged_mqa_logits_metadata( context_lens: torch.Tensor, block_size: int, num_sms: int ) -> torch.Tensor: """Build scheduling metadata for paged MQA logits. Args: context_lens: Tensor of shape [B], dtype int32; effective context length per batch element. block_size: KV-cache block size in tokens (e.g., 64). num_sms: Number of SMs available. 132 for Hopper Returns: Backend-specific tensor consumed by `fp8_paged_mqa_logits` to schedule work across SMs. """ _lazy_init() if _get_paged_mqa_logits_metadata_impl is None: return _missing() return _get_paged_mqa_logits_metadata_impl(context_lens, block_size, num_sms) def fp8_paged_mqa_logits( q_fp8: torch.Tensor, kv_cache_fp8: torch.Tensor, weights: torch.Tensor, context_lens: torch.Tensor, block_tables: torch.Tensor, schedule_metadata: torch.Tensor, max_model_len: int, clean_logits: bool, ) -> torch.Tensor: """Compute FP8 MQA logits using paged KV-cache. Args: q_fp8: Query tensor of shape [B, next_n, H, D]. Casted to `torch.float8_e4m3fn` by caller. kv_cache_fp8: Paged KV-cache in packed FP8+scale layout with shape [num_blocks, block_size, 1, D+4], dtype `torch.uint8`. The last 4 bytes per (block,pos) store the `float` dequant scale. weights: Tensor of shape [B * next_n, H], dtype `torch.float32`. context_lens: Tensor of shape [B], dtype int32; effective context length for each batch element. block_tables: Tensor of shape [B, max_blocks], dtype int32; maps logical block indices to physical blocks in the paged cache. schedule_metadata: Returned by `get_paged_mqa_logits_metadata`; used to distribute work across SMs. max_model_len: Maximum sequence length used to size the logits output. clean_logits: Whether to clean the unfilled logits into `-inf`. Returns: Logits tensor of shape [B * next_n, max_model_len], dtype `torch.float32`. """ _lazy_init() if _fp8_paged_mqa_logits_impl is None: return _missing() return _fp8_paged_mqa_logits_impl( q_fp8, kv_cache_fp8, weights, context_lens, block_tables, schedule_metadata, max_model_len, clean_logits=clean_logits, ) def _ceil_to_ue8m0(x: torch.Tensor): return torch.pow(2.0, torch.ceil(torch.log2(x.abs()))) def _align(x: int, y: int) -> int: return cdiv(x, y) * y # Taken from https://github.com/deepseek-ai/DeepGEMM/blob/v2.1.1/csrc/utils/math.hpp#L19 def get_tma_aligned_size(x: int, element_size: int) -> int: return _align(x, 16 // element_size) DEFAULT_BLOCK_SIZE = [128, 128] # Taken from https://github.com/deepseek-ai/DeepGEMM/blob/dd6ed14acbc7445dcef224248a77ab4d22b5f240/deep_gemm/utils/math.py#L38 @torch.compile(dynamic=True, backend=current_platform.simple_compile_backend) def per_block_cast_to_fp8( x: torch.Tensor, block_size: list[int] = DEFAULT_BLOCK_SIZE, use_ue8m0: bool = False ) -> tuple[torch.Tensor, torch.Tensor]: fp8_dtype = current_platform.fp8_dtype() assert x.dim() == 2 m, n = x.shape block_m, block_n = block_size x_padded = torch.zeros( (_align(m, block_m), _align(n, block_n)), dtype=x.dtype, device=x.device ) x_padded[:m, :n] = x x_view = x_padded.view(-1, block_m, x_padded.size(1) // block_n, block_n) x_amax = x_view.abs().float().amax(dim=(1, 3), keepdim=True).clamp(1e-4) _, fp8_max = get_fp8_min_max() sf = x_amax / fp8_max sf = _ceil_to_ue8m0(sf) if use_ue8m0 else sf x_scaled = (x_view * (1.0 / sf)).to(fp8_dtype) return x_scaled.view_as(x_padded)[:m, :n].contiguous(), sf.view( x_view.size(0), x_view.size(2) ) def calc_diff(x: torch.Tensor, y: torch.Tensor): """Return a global difference metric for unit tests. DeepGEMM kernels on Blackwell/B200 currently exhibit noticeable per-element error, causing `torch.testing.assert_close` to fail. Instead of checking every element, we compute a cosine-style similarity over the whole tensor and report `1 - sim`. Once kernel accuracy improves this helper can be removed. """ x, y = x.double(), y.double() denominator = (x * x + y * y).sum() sim = 2 * (x * y).sum() / denominator return 1 - sim def should_use_deepgemm_for_fp8_linear( output_dtype: torch.dtype, weight: torch.Tensor, supports_deep_gemm: bool | None = None, ): if supports_deep_gemm is None: supports_deep_gemm = is_deep_gemm_supported() # Verify DeepGEMM N/K dims requirements # NOTE: Also synchronized with test_w8a8_block_fp8_deep_gemm_matmul # test inside kernels/quantization/test_block_fp8.py N_MULTIPLE = 64 K_MULTIPLE = 128 return ( supports_deep_gemm and output_dtype == torch.bfloat16 and weight.shape[0] % N_MULTIPLE == 0 and weight.shape[1] % K_MULTIPLE == 0 ) def fp8_mqa_logits_torch( q: torch.Tensor, kv: tuple[torch.Tensor, torch.Tensor], weights: torch.Tensor, cu_seqlen_ks: torch.Tensor, cu_seqlen_ke: torch.Tensor, ) -> torch.Tensor: """Compute FP8 MQA logits for a single sequence without KV paging (CUDA fallback). This is a pure PyTorch fallback for CUDA when DeepGEMM is not available. Args: q: Query tensor of shape [M, H, D]. Casted to `torch.float8_e4m3fn` by caller. kv: Tuple `(k_fp8, k_scales)` where `k_fp8` has shape [N, D] with dtype `torch.float8_e4m3fn` and `k_scales` has shape [N] (or [N, 1]) with dtype `torch.float32`. weights: weights of shape [M, H], dtype `torch.float32`. cu_seqlen_ks: Start indices (inclusive) for valid K per query position, shape [M], dtype int32. cu_seqlen_ke: End indices (exclusive) for valid K per query position, shape [M], dtype int32. Returns: Logits tensor of shape [M, N], dtype `torch.float32`. """ kv_fp8, scale = kv seq_len_kv = kv_fp8.shape[0] k = kv_fp8.to(torch.bfloat16) q = q.to(torch.bfloat16) mask_lo = ( torch.arange(0, seq_len_kv, device=q.device)[None, :] >= cu_seqlen_ks[:, None] ) mask_hi = ( torch.arange(0, seq_len_kv, device=q.device)[None, :] < cu_seqlen_ke[:, None] ) mask = mask_lo & mask_hi score = torch.einsum("mhd,nd->hmn", q, k).float() * scale logits = (score.relu() * weights.unsqueeze(-1).transpose(0, 1)).sum(dim=0) logits = logits.masked_fill(~mask, float("-inf")) return logits def fp8_paged_mqa_logits_torch( q: torch.Tensor, kv_cache: torch.Tensor, weights: torch.Tensor, context_lens: torch.Tensor, block_tables: torch.Tensor, max_model_len: int, ) -> torch.Tensor: """Compute FP8 MQA logits using paged KV-cache (CUDA fallback). This is a pure PyTorch fallback for CUDA when DeepGEMM is not available. Handles head_dim = 132 (128 + 4 for RoPE). Args: q: Query tensor of shape [B, next_n, H, D]. kv_cache: Paged KV-cache in packed FP8+scale layout with shape [num_blocks, block_size, 1, D+4], dtype `torch.uint8`. The last 4 bytes per (block,pos) store the `float` dequant scale. weights: Tensor of shape [B * next_n, H], dtype `torch.float32`. context_lens: Tensor of shape [B], dtype int32; effective context length for each batch element. block_tables: Tensor of shape [B, max_blocks], dtype int32; maps logical block indices to physical blocks in the paged cache. max_model_len: Maximum sequence length used to size the logits output. Returns: Logits tensor of shape [B * next_n, max_model_len], dtype `torch.float32`. """ fp8_dtype = current_platform.fp8_dtype() batch_size, next_n, heads, dim = q.size() kv_cache, scale = kv_cache[..., :dim], kv_cache[..., dim:] scale = scale.contiguous().view(torch.float) q = q.float() kv_cache = kv_cache.view(fp8_dtype).float() * scale num_blocks, block_size, _, dim = kv_cache.size() logits = torch.full( [batch_size * next_n, max_model_len], float("-inf"), device=q.device, dtype=torch.float32, ) for i in range(batch_size): context_len = context_lens[i].item() q_offsets = torch.arange(context_len - next_n, context_len, device=q.device) weight_slice = ( weights[i * next_n : (i + 1) * next_n, :].transpose(0, 1).contiguous() ) for block_idx in range(cdiv(context_len, block_size)): block_id = block_tables[i][block_idx] qx, kx = q[i], kv_cache[block_id] k_offsets = torch.arange( block_idx * block_size, (block_idx + 1) * block_size, device=q.device ) mask = (k_offsets[None, :] < context_len) & ( k_offsets[None, :] <= q_offsets[:, None] ) s = torch.where( mask[None, :, :], (qx.transpose(0, 1) @ kx.transpose(0, 1).transpose(1, 2)).to( logits.dtype ), float("-inf"), ) s = torch.relu(s) * weight_slice[..., None] s = s.sum(dim=0) logits[ i * next_n : (i + 1) * next_n, block_idx * block_size : (block_idx + 1) * block_size, ] = torch.where(k_offsets[None, :] <= q_offsets[:, None], s, float("-inf")) return logits __all__ = [ "calc_diff", "DeepGemmQuantScaleFMT", "fp8_gemm_nt", "m_grouped_fp8_gemm_nt_contiguous", "fp8_m_grouped_gemm_nt_masked", "fp8_mqa_logits", "fp8_mqa_logits_torch", "fp8_paged_mqa_logits", "fp8_paged_mqa_logits_torch", "get_paged_mqa_logits_metadata", "per_block_cast_to_fp8", "is_deep_gemm_e8m0_used", "is_deep_gemm_supported", "get_num_sms", "should_use_deepgemm_for_fp8_linear", "get_col_major_tma_aligned_tensor", "get_mk_alignment_for_contiguous_layout", ]
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/utils/deep_gemm.py", "license": "Apache License 2.0", "lines": 471, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/layers/fused_moe/topk_weight_and_reduce.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm._custom_ops as ops import vllm.model_executor.layers.fused_moe.modular_kernel as mk class TopKWeightAndReduceDelegate(mk.TopKWeightAndReduce): """ Useful in the case when some FusedMoEPermuteExpertsUnpermute implementation does not perform weight application and reduction but cannot address the needs of all the compatible PrepareAndFinalize implementations. For example, BatchedTritonExperts is compatible with both batched PrepareAndFinalize implementations like DeepEPLLPrepareAndFinalize and BatchedPrepareAndFinalize. Some PrepareAndFinalize implementations do the weight-application + reduction as part of the combine kernel, while BatchedPrepareAndFinalize needs an explicit implementation. To facilitate this case, the BatchedTritonExperts could use TopKWeightAndReduceDelegate so the PrepareAndFinalize implementations could choose how to weight + reduce. """ def __eq__(self, other): return isinstance(other, TopKWeightAndReduceDelegate) def apply( self, output: torch.Tensor | None, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, ) -> torch.Tensor: raise RuntimeError( "The caller is expected to choose an appropriate " "TopKWeightAndReduce implementation." ) class TopKWeightAndReduceNoOP(mk.TopKWeightAndReduce): """ The fused_experts outputs have already been weight applied and reduced. This implementation is a no-op. """ def __eq__(self, other): return isinstance(other, TopKWeightAndReduceNoOP) def apply( self, output: torch.Tensor | None, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, ) -> torch.Tensor: # Weight application and reduction operations are already done. if output is None: return fused_expert_output # MoEPrepareAndFinalizeNoEP needs the output to be in the `output` # tensor. assert output.size() == fused_expert_output.size(), ( "output shape is expected to match the fused_expert_output shape. " f"But got output={output.size()}, " f"used_expert_output={fused_expert_output.size()}" ) output.copy_(fused_expert_output, non_blocking=True) return output class TopKWeightAndReduceContiguous(mk.TopKWeightAndReduce): """ TopKWeightAndReduce implementation for a fused_experts output of shape (m, topk, K) """ def __eq__(self, other): return isinstance(other, TopKWeightAndReduceContiguous) def apply( self, output: torch.Tensor | None, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, ) -> torch.Tensor: m, num_topk = topk_ids.size() k = fused_expert_output.size(-1) if fused_expert_output.ndim == 2: fused_expert_output = fused_expert_output.view(m, num_topk, k) assert fused_expert_output.size() == (m, num_topk, k), ( f"Expected fused_expert_output size {(m, num_topk, k)}. But got " f"{fused_expert_output.size()}" ) if not apply_router_weight_on_input: fused_expert_output.mul_(topk_weights.view(m, -1, 1)) if output is None: output = torch.empty( (m, k), device=fused_expert_output.device, dtype=fused_expert_output.dtype, ) assert output.size() == (m, k), ( f"Expected output size {(m, k)}. But got {output.size()}" ) ops.moe_sum(fused_expert_output, output) return output class TopKWeightAndReduceNaiveBatched(mk.TopKWeightAndReduce): """ TopKWeightAndReduce implementation for a fused_experts output of shape (num_experts, batch_size, K) """ def __init__(self, rank: int): self.rank = rank def __eq__(self, other): return isinstance(other, TopKWeightAndReduceNaiveBatched) and ( other.rank == self.rank ) def apply( self, output: torch.Tensor | None, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, ) -> torch.Tensor: assert fused_expert_output.ndim == 3 num_tokens = topk_ids.size(0) num_local_experts = fused_expert_output.size(0) K = fused_expert_output.size(-1) if output is None: output = torch.zeros( (num_tokens, K), device=fused_expert_output.device, dtype=fused_expert_output.dtype, ) else: output.fill_(0) assert output.size() == (num_tokens, K), ( f"Expected output size {(num_tokens, K)}, but got {output.size()}" ) first_expert = num_local_experts * self.rank last_expert = first_expert + num_local_experts for expert_id in range(first_expert, last_expert): matching_tokens = topk_ids == expert_id topks = torch.any(matching_tokens, dim=1).flatten() rows = torch.count_nonzero(topks) rhs = fused_expert_output[expert_id - first_expert, :rows, :] if not apply_router_weight_on_input: rhs.mul_(topk_weights[matching_tokens].view(rhs.size(0), 1)) output[topks] = output[topks] + rhs return output
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/fused_moe/topk_weight_and_reduce.py", "license": "Apache License 2.0", "lines": 142, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/kernels/moe/test_count_expert_num_tokens.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Tests compute_expert_num_tokens kernels """ import dataclasses import pytest import torch from vllm.model_executor.layers.fused_moe.utils import count_expert_num_tokens @dataclasses.dataclass class TestTensors: topk_ids: torch.Tensor expert_map: torch.Tensor | None = None def to_device(self, device: str): self.topk_ids = self.topk_ids.to(device=device) if self.expert_map is not None: self.expert_map = self.expert_map.to(device=device) @staticmethod def make( num_tokens: int, num_topk: int, num_experts: int, device: str, topk_ids_dtype: torch.dtype, ) -> "TestTensors": # make topk ids topk_ids = torch.empty((num_tokens, num_topk), device=device, dtype=torch.int64) for x in range(num_tokens): topk_ids[x] = torch.randperm(num_experts)[:num_topk] topk_ids = topk_ids.to(dtype=torch.int64) return TestTensors(topk_ids=topk_ids) def with_ep_rank( self, ep_rank: int, num_global_experts: int, num_local_experts: int, device: str ): # make an expert map expert_map = torch.empty((num_global_experts), device=device, dtype=torch.int32) expert_map.fill_(-1) s = ep_rank * num_local_experts e = s + num_local_experts expert_map[s:e] = torch.tensor(list(range(num_local_experts)), device=device) return TestTensors(topk_ids=self.topk_ids.clone(), expert_map=expert_map) def ref_impl(tt: TestTensors, expert_num_tokens: torch.Tensor): # do the reference in cpu tt.to_device("cpu") expert_ids, counts = tt.topk_ids.unique(return_counts=True) for eid, count in zip(expert_ids, counts): if eid != -1 and tt.expert_map is not None: eid = tt.expert_map[eid] if eid == -1: continue expert_num_tokens[eid] += count def do_test_compute_expert_num_tokens( num_tokens: int, num_topk: int, num_experts: int, ep_size: int, topk_ids_dtype: torch.dtype, ): assert num_topk <= num_experts tt = TestTensors.make( num_tokens, num_topk, num_experts, topk_ids_dtype=topk_ids_dtype, device="cpu" ) num_global_experts = num_experts assert num_global_experts % ep_size == 0 num_local_experts = num_global_experts // ep_size for ep_rank in range(ep_size): tt_rank = tt.with_ep_rank(ep_rank, num_global_experts, num_local_experts, "cpu") ref_expert_num_tokens = torch.zeros( (num_local_experts), device="cpu", dtype=torch.int32 ) ref_impl(tt_rank, ref_expert_num_tokens) ref_expert_num_tokens = ref_expert_num_tokens.to("cuda") tt_rank.to_device("cuda") # Test with expert_map triton_expert_num_tokens_w_emap = count_expert_num_tokens( tt_rank.topk_ids, num_local_experts, tt_rank.expert_map ) # Test without expert map topk_ids = tt_rank.expert_map[tt_rank.topk_ids].to(topk_ids_dtype) triton_expert_num_tokens_wo_emap = count_expert_num_tokens( topk_ids, num_local_experts, expert_map=None ) torch.testing.assert_close( ref_expert_num_tokens, triton_expert_num_tokens_w_emap, atol=0, rtol=0 ) torch.testing.assert_close( ref_expert_num_tokens, triton_expert_num_tokens_wo_emap, atol=0, rtol=0 ) @pytest.mark.parametrize("num_tokens", [1, 4, 8, 11, 127, 128, 3333, 7317]) @pytest.mark.parametrize("num_topk", [2, 6, 8]) @pytest.mark.parametrize("num_experts", [64]) @pytest.mark.parametrize("ep_size", [1, 2, 4]) @pytest.mark.parametrize("topk_ids_dtype", [torch.int64]) def test_compute_expert_num_tokens( num_tokens: int, num_topk: int, num_experts: int, ep_size: int, topk_ids_dtype: torch.dtype, ): do_test_compute_expert_num_tokens( num_tokens, num_topk, num_experts, ep_size, topk_ids_dtype ) @pytest.mark.parametrize("numel", list(range(1, 8192, 111))) @pytest.mark.parametrize("num_experts", [32]) @pytest.mark.parametrize("ep_size", [2]) @pytest.mark.parametrize("topk_ids_dtype", [torch.int64]) def test_compute_expert_num_tokens_from_numel( numel: int, num_experts: int, ep_size: int, topk_ids_dtype: torch.dtype ): do_test_compute_expert_num_tokens( num_tokens=numel, num_topk=1, num_experts=num_experts, ep_size=ep_size, topk_ids_dtype=topk_ids_dtype, )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/test_count_expert_num_tokens.py", "license": "Apache License 2.0", "lines": 117, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/entrypoints/openai/test_default_mm_loras.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import os import openai # use the official client for correctness check import pytest import pytest_asyncio from huggingface_hub import snapshot_download from ...conftest import AudioTestAssets from ...utils import RemoteOpenAIServer # NOTE - the tests in this module are currently analogous to test_chat, but are # separated to avoid OOM killing due to module-scoped servers, since we # need a multimodal model for these tests. # Contains a modality specific lora alongside the base model MULTIMODAL_MODEL_NAME = snapshot_download("microsoft/Phi-4-multimodal-instruct") AUDIO_LORA_PATH = os.path.join(MULTIMODAL_MODEL_NAME, "speech-lora") ACTIVE_MM_LORA_RESPONSE = "Spoken text: The first words I spoke in the original chronograph, a little piece of practical poetry. Mary had a little lamb, it slept with quite a snow, and everywhere that Mary went, the lamb was sure to go." # noqa: E501 @pytest.fixture(scope="module") def multimodal_server(): args = [ # use half precision for speed and memory savings in CI environment "--dtype", "half", "--max-model-len", "4096", "--enforce-eager", # lora config below "--enable-lora", "--lora-modules", f"speech={AUDIO_LORA_PATH}", "--max-lora-rank", "320", "--max-num-seqs", "2", "--trust-remote-code", "--gpu-memory-utilization", "0.8", "--default-mm-loras", f'{{"audio": "{AUDIO_LORA_PATH}"}}', ] with RemoteOpenAIServer( MULTIMODAL_MODEL_NAME, args, max_wait_seconds=480 ) as remote_server: yield remote_server @pytest_asyncio.fixture async def multi_modal_client(multimodal_server): async with multimodal_server.get_async_client() as async_client: yield async_client @pytest.mark.asyncio @pytest.mark.parametrize( # base model with default lora should give the same response as lora model "model_name", [MULTIMODAL_MODEL_NAME, "speech"], ) async def test_default_mm_lora_chat_completions( model_name: str, multi_modal_client: openai.AsyncOpenAI, audio_assets: AudioTestAssets, ): messages = [ { "role": "user", "content": [ { "type": "text", "text": "Can you transcribe this audio?", }, { "type": "audio_url", "audio_url": {"url": audio_assets[0].url}, }, ], } ] chat_completion = await multi_modal_client.chat.completions.create( model=model_name, messages=messages, max_completion_tokens=128, temperature=0.0 ) assert len(chat_completion.choices) > 0 message = chat_completion.choices[0].message assert message.content is not None and len(message.content) >= 0 assert message.content == ACTIVE_MM_LORA_RESPONSE
{ "repo_id": "vllm-project/vllm", "file_path": "tests/entrypoints/openai/test_default_mm_loras.py", "license": "Apache License 2.0", "lines": 80, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/lora/test_default_mm_loras.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Tests for applying default registered multimodal loras. """ import os import unittest.mock as mock import pytest from huggingface_hub import snapshot_download from vllm.lora.request import LoRARequest from ..conftest import AudioTestAssets, VllmRunner from ..utils import create_new_process_for_each_test MODEL_PATH = snapshot_download("microsoft/Phi-4-multimodal-instruct") AUDIO_LORA_PATH = os.path.join(MODEL_PATH, "speech-lora") IMAGE_LORA_PATH = os.path.join(MODEL_PATH, "vision-lora") AUDIO_PROMPT = "<|user|><|audio_1|>Can you transcribe this audio?<|end|><|assistant|>" # noqa: E501 # Responses are greedy decoded; we just check the end of # the generated text. If the lora is inactive, this model # generates commentary on the transcription. RESPONSE_SUFFIX_WITH_LORA = "Spoken text: The first words I spoke in the original chronograph, a little piece of practical poetry. Mary had a little lamb, it slept with quite a snow, and everywhere that Mary went, the lamb was sure to go." # noqa: E501 RESPONSE_SUFFIX_WITHOUT_LORA = "Certainly! Here is the transcription of the audio you provided:\n\nThe first words I spoke in the original phonograph record: A little piece of practical poetry. Mary had a little lamb; its fleece was white as snow, and everywhere that Mary went, the lamb was sure to go." # noqa: E501 VLLM_RUNNER_BASE_KWARGS = { "model_name": MODEL_PATH, "dtype": "half", "enable_lora": "True", "max_num_seqs": 2, "max_lora_rank": 320, # Keep these LoRA tests on short-RoPE for determinism post-LongRoPE change. "max_model_len": 4096, "gpu_memory_utilization": 0.8, "limit_mm_per_prompt": {"audio": 1}, "enforce_eager": True, } def run_test(vllm_runner, audio_assets, lora_request, expected_suffix, **kwargs): inputs = [([AUDIO_PROMPT], [audio_assets[0].audio_and_sample_rate[0]])] # Apply any additional kwargs as overrides to the base kwargs vllm_runner_kwargs = {**VLLM_RUNNER_BASE_KWARGS, **kwargs} with vllm_runner(**vllm_runner_kwargs) as vllm_model: vllm_outputs_with_default_lora = [ vllm_model.generate_greedy( prompts, max_tokens=128, audios=audios, lora_request=lora_request, ) for prompts, audios in inputs ] assert vllm_outputs_with_default_lora[-1][-1][-1].endswith(expected_suffix) @create_new_process_for_each_test() def test_active_default_mm_lora( vllm_runner: type[VllmRunner], audio_assets: AudioTestAssets, ): """Ensure that we can use the default audio lora.""" run_test( vllm_runner, audio_assets, lora_request=None, default_mm_loras={"audio": AUDIO_LORA_PATH}, expected_suffix=RESPONSE_SUFFIX_WITH_LORA, ) @create_new_process_for_each_test() def test_inactive_default_mm_lora( vllm_runner: type[VllmRunner], audio_assets: AudioTestAssets, ): """Ensure that modalities are filtered properly.""" # Default image lora won't be active since we only pass audio run_test( vllm_runner, audio_assets, lora_request=None, default_mm_loras={"image": IMAGE_LORA_PATH}, expected_suffix=RESPONSE_SUFFIX_WITHOUT_LORA, ) @create_new_process_for_each_test() def test_default_mm_lora_succeeds_with_redundant_lora_request( vllm_runner: type[VllmRunner], audio_assets: AudioTestAssets, ): """Ensure that redundantly providing the lora works.""" run_test( vllm_runner, audio_assets, lora_request=LoRARequest("audio", 1, AUDIO_LORA_PATH), default_mm_loras={"audio": AUDIO_LORA_PATH}, expected_suffix=RESPONSE_SUFFIX_WITH_LORA, ) @create_new_process_for_each_test() def test_default_mm_lora_fails_with_overridden_lora_request( vllm_runner: type[VllmRunner], audio_assets: AudioTestAssets, ): """Ensure that if the lora_request conflicts with default_mm_loras, we use the lora_request.""" run_test( vllm_runner, audio_assets, lora_request=LoRARequest("speech", 2, AUDIO_LORA_PATH), default_mm_loras={"audio": IMAGE_LORA_PATH}, expected_suffix=RESPONSE_SUFFIX_WITH_LORA, ) @create_new_process_for_each_test() def test_default_mm_lora_does_not_expand_string_reqs(vllm_runner): class MockEngineException(Exception): pass # Regression test for ensuring default multimodal lora resolution # does not expand the lora req if the prompt type is a string. vllm_runner_kwargs = { **VLLM_RUNNER_BASE_KWARGS, **{"default_mm_loras": {"audio": AUDIO_LORA_PATH}}, } # Avoid the full generation call since these tests are expensive; # just check what lora request is actually submitted to the engine mock_err = "Engine is mocked for this test" with ( mock.patch( "vllm.v1.engine.llm_engine.LLMEngine.add_request", side_effect=MockEngineException(mock_err), ) as mock_add_request, vllm_runner(**vllm_runner_kwargs) as vllm_model, ): # Die once we actually submit the request to the engine with pytest.raises(MockEngineException): vllm_model.llm.generate(prompts=AUDIO_PROMPT) # Then check to make sure the submitted lora request # and text prompt were zipped together correctly engine_args, engine_kwargs = mock_add_request.call_args assert engine_args[1]["prompt"] == AUDIO_PROMPT assert engine_kwargs["lora_request"] is None
{ "repo_id": "vllm-project/vllm", "file_path": "tests/lora/test_default_mm_loras.py", "license": "Apache License 2.0", "lines": 130, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/v1/structured_output/backend_outlines.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright 2025-present the Outlines developers # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from __future__ import annotations import ast import importlib import json import sys from dataclasses import dataclass, field from typing import TYPE_CHECKING import torch from regex import escape as regex_escape from vllm.sampling_params import SamplingParams from vllm.utils.import_utils import LazyLoader from vllm.v1.structured_output.backend_types import ( StructuredOutputBackend, StructuredOutputGrammar, StructuredOutputOptions, ) from vllm.v1.structured_output.utils import ( OutlinesVocabulary, get_outlines_cache, get_outlines_vocabulary, ) if TYPE_CHECKING: import outlines_core as oc import outlines_core.json_schema as json_schema else: oc = LazyLoader("oc", globals(), "outlines_core") json_schema = LazyLoader("json_schema", globals(), "outlines_core.json_schema") # Python 3.11+ sre_parse and sre_constants # are deprecated, so we must import them from re if sys.version_info >= (3, 11): # Hack to get around pre-commit regex module rule # because going through re is the only way to get sre_parse # and sre_constants in Python 3.11+ _re = importlib.import_module("re") sre_parse = _re._parser sre_constants = _re._constants else: import sre_constants import sre_parse @dataclass class OutlinesBackend(StructuredOutputBackend): def __post_init__(self): self.vocabulary = get_outlines_vocabulary(self.tokenizer) self.cache = get_outlines_cache() def _compile_index( self, regex_string: str, vocabulary: OutlinesVocabulary ) -> oc.Index: cache_key = f"{vocabulary._hash}_{regex_string}" if cache_key in self.cache: return self.cache[cache_key] index = oc.Index(regex_string, vocabulary.inner) self.cache[cache_key] = index return index def compile_grammar( self, request_type: StructuredOutputOptions, grammar_spec: str ) -> StructuredOutputGrammar: if request_type == StructuredOutputOptions.JSON: regex = json_schema.build_regex_from_schema(grammar_spec) elif request_type == StructuredOutputOptions.REGEX: regex = grammar_spec elif request_type == StructuredOutputOptions.CHOICE: choices = ast.literal_eval(grammar_spec) choices = [regex_escape(c) for c in choices] regex = "(" + "|".join(choices) + ")" else: raise ValueError( f"Invalid request type for Outlines backend ({request_type!s})" ) index = self._compile_index(regex, self.vocabulary) max_rollback_tokens = ( self.vllm_config.speculative_config.num_speculative_tokens if self.vllm_config.speculative_config is not None else 0 ) return OutlinesGrammar( vocab_size=self.vocab_size, guide=oc.Guide(index, max_rollback=max_rollback_tokens), ) def allocate_token_bitmask(self, max_num_seqs: int) -> torch.Tensor: return torch.full( (max_num_seqs, (self.vocab_size + 31) // 32), -1, dtype=torch.int32, pin_memory=torch.cuda.is_available(), ) def destroy(self): pass @dataclass class OutlinesGrammar(StructuredOutputGrammar): vocab_size: int guide: oc.Guide = field(hash=False) num_processed_tokens: int = field( default_factory=lambda: 0, repr=False, hash=False, init=False ) # outlines_core signals done on DFA accept; vLLM expects done after EOS. # We delay the finished flag by one step so EOS can still be emitted. _prev_finished: bool = field(default=False, init=False, repr=False, hash=False) def accept_tokens(self, request_id: str, tokens: list[int]) -> bool: """Accepts a list of tokens and advances the FSM. Returns True if the FSM was advanced successfully. Returns False if the FSM failed to advance. """ if self.guide.accepts_tokens(tokens): # Advance can fail when the next state reached after advancing with # the current tokens is a dead state. This is because Guide.accepts_tokens() # only checks whether the current tokens can be accepted, # whereas guide.advance() additionally checks the next state # after all tokens are accepted. # We need to be aware that the FSM must be prepared without dead states. for t in tokens: self.guide.advance(t) self.num_processed_tokens += 1 return True return False def rollback(self, num_tokens: int) -> None: self.guide.rollback_state(num_tokens) self.num_processed_tokens -= num_tokens def validate_tokens(self, tokens: list[int]) -> list[int]: accepted: list[int] = [] for tok in tokens: accepted.append(tok) if not self.guide.accepts_tokens(accepted): accepted.pop() break return accepted def fill_bitmask(self, bitmask: torch.Tensor, idx: int) -> None: mask = bitmask[idx] self.guide.write_mask_into(mask.data_ptr(), mask.numel(), mask.element_size()) def is_terminated(self) -> bool: curr = self.guide.is_finished() prev = self._prev_finished self._prev_finished = curr return prev def reset(self): self.num_processed_tokens = 0 self._prev_finished = False self.guide.reset() def validate_structured_output_request_outlines(params: SamplingParams): if params.structured_outputs is None: return so_params = params.structured_outputs if so_params.regex: validate_regex_is_buildable(so_params.regex) elif so_params.json: if isinstance(so_params.json, str): try: # make sure schema is valid json json.loads(so_params.json) schema = so_params.json except json.JSONDecodeError as e: raise ValueError("Invalid JSON grammar specification.") from e else: try: schema = json.dumps(so_params.json) except Exception as e: raise ValueError( f"Error serializing structured outputs jsonschema: {e}" ) from e pattern = json_schema.build_regex_from_schema(schema) validate_regex_is_buildable(pattern) elif so_params.choice: choices = [regex_escape(str(choice)) for choice in so_params.choice] regex = "(" + "|".join(choices) + ")" validate_regex_is_buildable(regex) elif so_params.grammar: raise ValueError( "Outlines structured outputs backend " "does not support grammar specifications" ) def _prefix_needs_context(parsed) -> bool: """Return True if there's a look-around/anchor before any consumer.""" def subpattern_consumes(parsed) -> bool: """Return True if subpattern can consume at least one character.""" tokens = parsed.data if hasattr(parsed, "data") else parsed for ttype, tval in tokens: # literal, character class, or dot always consumes if ttype in (sre_parse.LITERAL, sre_parse.IN, sre_parse.ANY): return True # quantified subpattern: check inner pattern elif ttype == sre_parse.MAX_REPEAT: _, mx, sub = tval if mx != 0 and subpattern_consumes(sub): return True # alternation: if any branch consumes, the whole does elif ttype == sre_parse.BRANCH: _, branches = tval if any(subpattern_consumes(br) for br in branches): return True # grouped subpattern: recurse into its contents elif ttype == sre_parse.SUBPATTERN and subpattern_consumes(tval[3]): return True # No consumers, return False return False tokens = parsed.data if hasattr(parsed, "data") else parsed for ttype, tval in tokens: # Direct anchors or look-around if ttype == sre_parse.AT or ttype in ( sre_constants.ASSERT, sre_constants.ASSERT_NOT, ): return True # Nested subpattern: check if ttype == sre_parse.SUBPATTERN: # tval: (group, add_flags, del_flags, subpattern) if _prefix_needs_context(tval[3]): return True if subpattern_consumes(tval[3]): return False # if any branch has a prefix anchor => True, # else if at least one branch consumes => prefix ends => False elif ttype == sre_parse.BRANCH: saw_consumer = False for br in tval[1]: if _prefix_needs_context(br): return True if subpattern_consumes(br): saw_consumer = True if saw_consumer: return False # Immediate consumer tokens elif ttype in (sre_parse.LITERAL, sre_parse.IN, sre_parse.ANY): return False # if subpattern has anchor => True, if it can consume => stop elif ttype == sre_parse.MAX_REPEAT: if _prefix_needs_context(tval[2]): return True if subpattern_consumes(tval[2]): return False return False def _check_unsupported(parsed) -> None: """Check for regex features unsupported by regex-automata""" tokens = parsed.data if hasattr(parsed, "data") else parsed for ttype, tval in tokens: # backreference if ttype in (sre_parse.GROUPREF, sre_parse.GROUPREF_EXISTS): raise ValueError("Backreferences are unsupported.") # look-around assertion elif ttype in (sre_constants.ASSERT, sre_constants.ASSERT_NOT): raise ValueError("Look-Around assertion are unsupported.") # unicode word boundaries elif ttype == sre_parse.AT: if tval in (sre_constants.AT_BOUNDARY, sre_constants.AT_NON_BOUNDARY): raise ValueError("Unicode word boundaries are unsupported.") elif ttype == sre_parse.BRANCH: # tval is (None, branches) for branch in tval[1]: _check_unsupported(branch) # tval is (min, max, subpattern) elif ttype == sre_parse.MAX_REPEAT: _check_unsupported(tval[2]) def validate_regex_is_buildable(pattern: str) -> None: """ Validates that the input regex is not using unsupported features of the `regex-automata` crate (outlines_core regex engine) and has a universal start state. definition of universal start state used can be found at: https://docs.rs/regex-automata/latest/regex_automata/dfa/trait.Automaton.html#method.universal_start_state """ try: parsed = sre_parse.parse(pattern) except sre_constants.error as e: raise ValueError(f"Error parsing regex: {e}") from e try: _check_unsupported(parsed) except ValueError as e: raise ValueError( f"Regex uses unsupported feature for structured outputs: {e}. " "Only basic matching constructs are supported—lookarounds, " "backreferences, and unicode boundaries are not." ) from e if _prefix_needs_context(parsed): raise ValueError( "Regex does not have a anchored universal start state" "This means that the Regex uses anchors (^) or look-arounds " "in a way which requires context before any token is matched." "structured outputs needs regexes that can match without needing " "that context. Try rewriting the pattern without using these " f"constructs. Pattern:\n{pattern}" )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/structured_output/backend_outlines.py", "license": "Apache License 2.0", "lines": 282, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/jina_vl.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Iterable, Mapping import torch import torch.nn as nn from transformers import BatchFeature from vllm.config import ModelConfig, VllmConfig from vllm.inputs import TokensPrompt from vllm.logger import init_logger from vllm.model_executor.layers.linear import ColumnParallelLinear, RowParallelLinear from vllm.model_executor.layers.pooler import DispatchPooler from vllm.multimodal import MULTIMODAL_REGISTRY from vllm.sequence import IntermediateTensors from .interfaces import SupportsCrossEncoding, SupportsMultiModal, SupportsScoreTemplate from .qwen2_vl import ( Qwen2VLDummyInputsBuilder, Qwen2VLForConditionalGeneration, Qwen2VLMultiModalProcessor, Qwen2VLProcessingInfo, ) from .utils import AutoWeightsLoader, WeightsMapper, maybe_prefix logger = init_logger(__name__) class JinaVLScorer(nn.Module): def __init__(self, model_config: "ModelConfig", prefix: str = ""): super().__init__() config = model_config.hf_config.get_text_config() head_dtype = model_config.head_dtype self.dense = ColumnParallelLinear( config.hidden_size, config.hidden_size, params_dtype=head_dtype, bias=True, prefix=f"{prefix}.dense", ) self.out_proj = RowParallelLinear( config.hidden_size, config.num_labels, params_dtype=head_dtype, bias=True, prefix=f"{prefix}.out_proj", ) def forward(self, x, **kwargs): x, _ = self.dense(x) x = torch.relu(x) x, _ = self.out_proj(x) return x class JinaVLMultiModalProcessor(Qwen2VLMultiModalProcessor): def _call_hf_processor( self, prompt: str, mm_data: Mapping[str, object], mm_kwargs: Mapping[str, object], tok_kwargs: Mapping[str, object], ) -> BatchFeature: # NOTE: We should reverse the order of the mm_data because the # query prompt is placed after the document prompt in the score # template for JinaVLForRanking model, but in mm_data they are # stored in the opposite order (query first, then document). for _, value in mm_data.items(): value.reverse() return super()._call_hf_processor(prompt, mm_data, mm_kwargs, tok_kwargs) @MULTIMODAL_REGISTRY.register_processor( JinaVLMultiModalProcessor, info=Qwen2VLProcessingInfo, dummy_inputs=Qwen2VLDummyInputsBuilder, ) class JinaVLForSequenceClassification( Qwen2VLForConditionalGeneration, SupportsCrossEncoding, SupportsMultiModal, SupportsScoreTemplate, ): is_pooling_model = True weight_mapper = WeightsMapper( orig_to_new_prefix={ "score.0.": "score.dense.", "score.2.": "score.out_proj.", # mapping for new names in checkpoint saved after transformers v4.52 "model.language_model.": "language_model.model.", "visual.": "visual.", # mapping for original checkpoint "lm_head.": "language_model.lm_head.", "model.": "language_model.model.", } ) def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__( vllm_config=vllm_config, prefix=maybe_prefix(prefix, "qwen2_vl") ) pooler_config = vllm_config.model_config.pooler_config assert pooler_config is not None self.score = JinaVLScorer( vllm_config.model_config, prefix=maybe_prefix(prefix, "score") ) self.pooler = DispatchPooler.for_seq_cls(pooler_config, classifier=self.score) @classmethod def get_placeholder_str(cls, modality: str, i: int) -> str | None: if modality.startswith("image"): return "<|vision_start|><|image_pad|><|vision_end|>" raise ValueError("Only image modality is supported") @classmethod def get_score_template(cls, query: str, document: str) -> str | None: return f"**Document**:\n{document}\n**Query**:\n{query}" @classmethod def post_process_tokens(cls, prompt: TokensPrompt) -> None: # add score target token at the end of prompt tokens prompt["prompt_token_ids"].append(100) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, **kwargs: object, ) -> torch.Tensor: hidden_states = super().forward( input_ids=input_ids, positions=positions, intermediate_tensors=intermediate_tensors, inputs_embeds=inputs_embeds, **kwargs, ) return hidden_states def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]): loader = AutoWeightsLoader(self) return loader.load_weights(weights, mapper=self.weight_mapper)
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/jina_vl.py", "license": "Apache License 2.0", "lines": 126, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/reasoning/test_hunyuan_reasoning_parser.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest from transformers import AutoTokenizer from tests.reasoning.utils import run_reasoning_extraction from vllm.reasoning import ReasoningParser, ReasoningParserManager parser_name = "hunyuan_a13b" START_REASONING = "<think>\n" START_RESPONSE = "\n</think>\n<answer>\n" END_RESPONSE = "\n</answer>" NO_REASONING_QUICK_THOUGHT = { "output": f"{START_REASONING}{START_RESPONSE}This is the rest{END_RESPONSE}", # noqa: E501 "reasoning": None, "content": "This is the rest", } SIMPLE_REASONING = { "output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}This is the rest{END_RESPONSE}", # noqa: E501 "reasoning": "This is a reasoning section", "content": "This is the rest", } COMPLETE_REASONING = { "output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}", "reasoning": "This is a reasoning section", "content": None, } COMPLETE_REASONING_WITH_SYMBOL = { "output": f"{START_REASONING}This is a reasoning section!{START_RESPONSE}", "reasoning": "This is a reasoning section!", "content": None, } NO_REASONING = { "output": "This is content", "reasoning": None, "content": "This is content", } MULTIPLE_LINES = { "output": f"{START_REASONING}This\nThat{START_RESPONSE}This is the rest\nThat", "reasoning": "This\nThat", "content": "This is the rest\nThat", } REASONING_WITH_THINK = { "output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}This is the rest", # noqa: E501 "reasoning": "This is a reasoning section", "content": "This is the rest", } COMPLETE_REASONING_WITH_THINK = { "output": f"{START_REASONING}This is a reasoning section{START_RESPONSE}", "reasoning": "This is a reasoning section", "content": None, } MULTIPLE_LINES_WITH_THINK = { "output": f"{START_REASONING}This\nThat{START_RESPONSE}This is the rest\nThat", "reasoning": "This\nThat", "content": "This is the rest\nThat", } TEST_CASES = [ pytest.param( False, SIMPLE_REASONING, id="simple_reasoning", ), pytest.param( False, COMPLETE_REASONING, id="complete_reasoning", ), pytest.param( False, COMPLETE_REASONING_WITH_SYMBOL, id="complete_reasoning_with_symbol", ), pytest.param( False, NO_REASONING, id="no_reasoning", ), pytest.param(False, NO_REASONING_QUICK_THOUGHT, id="no_reasoning_quick"), pytest.param( False, MULTIPLE_LINES, id="multiple_lines", ), pytest.param( False, REASONING_WITH_THINK, id="reasoning_with_think", ), pytest.param( False, COMPLETE_REASONING_WITH_THINK, id="complete_reasoning_with_think", ), pytest.param( False, MULTIPLE_LINES_WITH_THINK, id="multiple_lines_with_think", ), pytest.param( True, SIMPLE_REASONING, id="simple_reasoning_streaming", ), pytest.param( True, COMPLETE_REASONING, id="complete_reasoning_streaming", ), pytest.param( True, NO_REASONING, id="no_reasoning_streaming", ), pytest.param(True, NO_REASONING_QUICK_THOUGHT, id="no_reasoning_quick_stream"), pytest.param( True, MULTIPLE_LINES, id="multiple_lines_streaming", ), pytest.param( True, REASONING_WITH_THINK, id="reasoning_with_think_streaming", ), pytest.param( True, COMPLETE_REASONING_WITH_THINK, id="complete_reasoning_with_think_streaming", ), pytest.param( True, MULTIPLE_LINES_WITH_THINK, id="multiple_lines_with_think_streaming", ), ] # Global tokenizer initialization to avoid repeated loading tokenizer = AutoTokenizer.from_pretrained( "tencent/Hunyuan-A13B-Instruct", trust_remote_code=True ) @pytest.mark.parametrize("streaming, param_dict", TEST_CASES) def test_reasoning( streaming: bool, param_dict: dict, ): output = tokenizer.tokenize(param_dict["output"]) # decode everything to tokens output_tokens: list[str] = [ tokenizer.convert_tokens_to_string([token]) for token in output ] parser: ReasoningParser = ReasoningParserManager.get_reasoning_parser(parser_name)( tokenizer ) reasoning, content = run_reasoning_extraction( parser, output_tokens, streaming=streaming ) assert reasoning == param_dict["reasoning"] assert content == param_dict["content"]
{ "repo_id": "vllm-project/vllm", "file_path": "tests/reasoning/test_hunyuan_reasoning_parser.py", "license": "Apache License 2.0", "lines": 156, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/reasoning/hunyuan_a13b_reasoning_parser.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Sequence import regex as re from transformers import PreTrainedTokenizerBase from vllm.entrypoints.openai.chat_completion.protocol import ( ChatCompletionRequest, ) from vllm.entrypoints.openai.engine.protocol import DeltaMessage from vllm.logger import init_logger from vllm.reasoning import ReasoningParser logger = init_logger(__name__) class HunyuanA13BReasoningParser(ReasoningParser): """ Reasoning parser for Hunyuan A13B Model HunyuanReasoningParser This class implements a reasoning parser specifically designed for the Hunyuan A13B Model. It is responsible for parsing and extracting structured reasoning and answer segments from model outputs that follow a specific pattern. Key Features: - For non-stream output , Recognizes and extracts reasoning ("think") and answer ("answer") sections from text using regular expressions. - For stream process, it requires a token id sequences to change the reasoning state and other state so it maintains internal state to manage parsing across multiple token. think start: "<think>\n": [14023, 771, 397] think ends: "\n</think>\n<answer>\n": [198, 524, 27963, 397, 27, 9399, 397] response ends: "\n</answer>": [524, 9399, 29] """ def __init__(self, tokenizer: PreTrainedTokenizerBase, *args, **kwargs): super().__init__(tokenizer, *args, **kwargs) self.think_start_expr = r"<think>\n" self.think_end_expr = r"\n</think>\n" self.response_start_expr = r"\n</think>\n<answer>\n" self.response_end_expr = r"\n</answer>" self.full_match_reasoning_regex = re.compile( rf"(?:{self.think_start_expr}(.*?){self.response_start_expr})?(.*?){self.response_end_expr}", re.DOTALL, ) self.half_match_reasoning_regex = re.compile( rf"{self.think_start_expr}(.*?){self.response_start_expr}(.*)", re.DOTALL ) self.think_start_ids = [14023, 771, 397] self.think_start_ids_fast = [14023, 771, 1363] self.response_start_ids = [198, 524, 27963, 397, 27, 9399, 397] self.response_start_ids_fast = [524, 27963, 397, 27, 9399, 397] self.response_end_ids = [198, 524, 9399, 29] self.fast_think_ids = [14023, 771, 1363, 524, 27963, 397, 27, 9399, 397] # when state change, send out all the buffered text in last state self.buffered_text = [] self.buffered_ids = [] self.current_state = "reasoning" self.all_states = ["reasoning", "response"] self.current_state = "idle" self.expected_sequence = self.think_start_ids # this sequence only for the think start, it has two way to start. self.expected_sequence_side = self.think_start_ids_fast self.sequence_index = 0 self.token_buffer = [] self.text_buffer = "" def is_reasoning_end(self, input_ids: Sequence[int]) -> bool: return self.current_state == "response" def extract_content_ids(self, input_ids: list[int]) -> list[int]: # for hunyuan streaming reason parsing, the stream parse # will call first, and the same token will be called in # is_reasoning_end and extract_content_ids # this id is not part of content, so just return [] here. return [] def extract_reasoning( self, model_output: str, request: ChatCompletionRequest ) -> tuple[str | None, str | None]: """Extract the reasoning content & content sections, respectively. If the sequence doesn't match what we expect, i.e., the model generates something else, all content is considered non-reasoning content. Args: model_output (str): Output of the model to be parsed. request (ChatCompletionRequest): Request being processed. Returns: tuple[Optional[str], Optional[str]]: Tuple pair containing the reasoning content and non-reasoning content. """ re_match = self.full_match_reasoning_regex.findall(model_output) if re_match: reasoning, response_content = re_match[0] if len(reasoning) == 0: reasoning = None if len(response_content) == 0: response_content = None return reasoning, response_content fallback_regex = self.half_match_reasoning_regex fallback_match = fallback_regex.findall(model_output) if fallback_match: reasoning, response_content = fallback_match[0] if response_content.endswith(self.response_end_expr): response_content = response_content[: -len(self.response_end_expr)] if len(reasoning) == 0: reasoning = None if len(response_content) == 0: response_content = None return reasoning, response_content return None, model_output def _is_strict_increasing_subsequence( self, subsequence: Sequence[int], sequence: Sequence[int] ) -> bool: if not subsequence: return False sub_idx = 0 for num in sequence: if sub_idx < len(subsequence) and num == subsequence[sub_idx]: sub_idx += 1 return sub_idx == len(subsequence) def extract_reasoning_streaming( self, previous_text: str, current_text: str, delta_text: str, previous_token_ids: Sequence[int], current_token_ids: Sequence[int], delta_token_ids: Sequence[int], ) -> DeltaMessage | None: """Extract content using token ID sequence state machine""" # Define sequences think_start_sequence = self.think_start_ids response_start_sequence = self.response_start_ids response_end_sequence = self.response_end_ids assert len(delta_token_ids) == 1 # Process each token in the delta token = delta_token_ids[0] def check_token_with_sequence(token): if self.current_state == "idle" or self.current_state == "think": return ( token == self.expected_sequence[self.sequence_index] or token == self.expected_sequence_side[self.sequence_index] ) else: return token == self.expected_sequence[self.sequence_index] def check_last_token(token): if self.current_state == "idle" or self.current_state == "think": # only return true if it's judge using a side sequence. if ( self.sequence_index - 1 < len(self.expected_sequence_side) and token == self.expected_sequence_side[self.sequence_index - 1] ): return self.sequence_index == len(self.expected_sequence_side) else: return self.sequence_index == len(self.expected_sequence) else: return self.sequence_index == len(self.expected_sequence) # Check if token matches expected sequence token_in_state_seq = check_token_with_sequence(token) if token_in_state_seq: # Store matching token self.token_buffer.append(token) self.text_buffer += delta_text self.sequence_index += 1 ## state change from idle->think->response->idle # Check if sequence fully matched if check_last_token(token): # State transition if self.current_state == "idle": self.current_state = "think" self.expected_sequence = response_start_sequence self.expected_sequence_side = self.response_start_ids_fast elif self.current_state == "think": self.current_state = "response" self.expected_sequence = response_end_sequence elif self.current_state == "response": self.current_state = "idle" self.expected_sequence = think_start_sequence self.expected_sequence_side = self.think_start_ids_fast # Reset matching state self.sequence_index = 0 self.token_buffer = [] self.text_buffer = "" # Do not send content for state transition texts. else: # Sequence broken - handle buffered content if self.token_buffer and len(self.token_buffer) > 0: # Send buffered tokens buffered_content = self.text_buffer + delta_text # Reset matching state self.sequence_index = 0 self.token_buffer = [] self.text_buffer = "" # Return content based on current state if self.current_state == "think": return DeltaMessage(reasoning=buffered_content, content=None) else: return DeltaMessage(reasoning=None, content=buffered_content) else: # No buffered content, send normally if self.current_state == "think": return DeltaMessage(reasoning=delta_text, content=None) else: return DeltaMessage(reasoning=None, content=delta_text) # If no content to send in this delta return None
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/reasoning/hunyuan_a13b_reasoning_parser.py", "license": "Apache License 2.0", "lines": 198, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:docs/mkdocs/hooks/generate_argparse.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import importlib.metadata import importlib.util import logging import sys import textwrap import traceback from argparse import SUPPRESS, Action, HelpFormatter from collections.abc import Iterable from importlib.machinery import ModuleSpec from pathlib import Path from typing import TYPE_CHECKING, Literal from unittest.mock import MagicMock, patch from pydantic_core import core_schema logger = logging.getLogger("mkdocs") ROOT_DIR = Path(__file__).parent.parent.parent.parent ARGPARSE_DOC_DIR = ROOT_DIR / "docs/generated/argparse" sys.path.insert(0, str(ROOT_DIR)) def mock_if_no_torch(mock_module: str, mock: MagicMock): if not importlib.util.find_spec("torch"): sys.modules[mock_module] = mock # Mock custom op code class MockCustomOp: @staticmethod def register(name): def decorator(cls): return cls return decorator mock_if_no_torch("vllm._C", MagicMock()) mock_if_no_torch("vllm.model_executor.custom_op", MagicMock(CustomOp=MockCustomOp)) mock_if_no_torch( "vllm.utils.torch_utils", MagicMock(direct_register_custom_op=lambda *a, **k: None) ) # Mock any version checks by reading from compiled CI requirements with open(ROOT_DIR / "requirements/test.txt") as f: VERSIONS = dict(line.strip().split("==") for line in f if "==" in line) importlib.metadata.version = lambda name: VERSIONS.get(name) or "0.0.0" # Make torch.nn.Parameter safe to inherit from mock_if_no_torch("torch.nn", MagicMock(Parameter=object)) class PydanticMagicMock(MagicMock): """`MagicMock` that's able to generate pydantic-core schemas.""" def __init__(self, *args, **kwargs): name = kwargs.pop("name", None) super().__init__(*args, **kwargs) self.__spec__ = ModuleSpec(name, None) def __get_pydantic_core_schema__(self, source_type, handler): return core_schema.any_schema() def auto_mock(module_name: str, attr: str, max_mocks: int = 100): """Function that automatically mocks missing modules during imports.""" logger.info("Importing %s from %s", attr, module_name) for _ in range(max_mocks): try: module = importlib.import_module(module_name) # First treat attr as an attr, then as a submodule if hasattr(module, attr): return getattr(module, attr) return importlib.import_module(f"{module_name}.{attr}") except ModuleNotFoundError as e: assert e.name is not None logger.info("Mocking %s for argparse doc generation", e.name) sys.modules[e.name] = PydanticMagicMock(name=e.name) except Exception: logger.exception("Failed to import %s.%s: %s", module_name, attr) raise ImportError( f"Failed to import {module_name}.{attr} after mocking {max_mocks} imports" ) bench_latency = auto_mock("vllm.benchmarks", "latency") bench_mm_processor = auto_mock("vllm.benchmarks", "mm_processor") bench_serve = auto_mock("vllm.benchmarks", "serve") bench_sweep_plot = auto_mock("vllm.benchmarks.sweep.plot", "SweepPlotArgs") bench_sweep_plot_pareto = auto_mock( "vllm.benchmarks.sweep.plot_pareto", "SweepPlotParetoArgs" ) bench_sweep_serve = auto_mock("vllm.benchmarks.sweep.serve", "SweepServeArgs") bench_sweep_serve_workload = auto_mock( "vllm.benchmarks.sweep.serve_workload", "SweepServeWorkloadArgs" ) bench_throughput = auto_mock("vllm.benchmarks", "throughput") AsyncEngineArgs = auto_mock("vllm.engine.arg_utils", "AsyncEngineArgs") EngineArgs = auto_mock("vllm.engine.arg_utils", "EngineArgs") ChatCommand = auto_mock("vllm.entrypoints.cli.openai", "ChatCommand") CompleteCommand = auto_mock("vllm.entrypoints.cli.openai", "CompleteCommand") openai_cli_args = auto_mock("vllm.entrypoints.openai", "cli_args") openai_run_batch = auto_mock("vllm.entrypoints.openai", "run_batch") if TYPE_CHECKING: from vllm.utils.argparse_utils import FlexibleArgumentParser else: FlexibleArgumentParser = auto_mock( "vllm.utils.argparse_utils", "FlexibleArgumentParser" ) class MarkdownFormatter(HelpFormatter): """Custom formatter that generates markdown for argument groups.""" def __init__(self, prog: str, starting_heading_level: int = 3): super().__init__(prog, max_help_position=sys.maxsize, width=sys.maxsize) self._section_heading_prefix = "#" * starting_heading_level self._argument_heading_prefix = "#" * (starting_heading_level + 1) self._markdown_output = [] def start_section(self, heading: str): if heading not in {"positional arguments", "options"}: heading_md = f"\n{self._section_heading_prefix} {heading}\n\n" self._markdown_output.append(heading_md) def end_section(self): pass def add_text(self, text: str): if text: self._markdown_output.append(f"{text.strip()}\n\n") def add_usage(self, usage, actions, groups, prefix=None): pass def add_arguments(self, actions: Iterable[Action]): for action in actions: if len(action.option_strings) == 0 or "--help" in action.option_strings: continue option_strings = f"`{'`, `'.join(action.option_strings)}`" heading_md = f"{self._argument_heading_prefix} {option_strings}\n\n" self._markdown_output.append(heading_md) if action.choices or isinstance(action.metavar, (list, tuple)): choices_iterable = action.choices or action.metavar choices = f"`{'`, `'.join(str(c) for c in choices_iterable)}`" self._markdown_output.append(f": Possible choices: {choices}\n\n") if action.help: help_dd = ":" + textwrap.indent(action.help, " ")[1:] self._markdown_output.append(f"{help_dd}\n\n") # None usually means the default is determined at runtime if (default := action.default) != SUPPRESS and default is not None: # Make empty string defaults visible if default == "": default = '""' self._markdown_output.append(f": Default: `{default}`\n\n") def format_help(self): """Return the formatted help as markdown.""" return "".join(self._markdown_output) def create_parser(add_cli_args, **kwargs) -> FlexibleArgumentParser: """Create a parser for the given class with markdown formatting. Args: cls: The class to create a parser for **kwargs: Additional keyword arguments to pass to `cls.add_cli_args`. Returns: FlexibleArgumentParser: A parser with markdown formatting for the class. """ try: parser = FlexibleArgumentParser(add_json_tip=False) parser.formatter_class = MarkdownFormatter with patch("vllm.config.DeviceConfig.__post_init__"): _parser = add_cli_args(parser, **kwargs) except ModuleNotFoundError as e: # Auto-mock runtime imports if tb_list := traceback.extract_tb(e.__traceback__): path = Path(tb_list[-1].filename).relative_to(ROOT_DIR) auto_mock(module_name=".".join(path.parent.parts), attr=path.stem) return create_parser(add_cli_args, **kwargs) else: raise e # add_cli_args might be in-place so return parser if _parser is None return _parser or parser def on_startup(command: Literal["build", "gh-deploy", "serve"], dirty: bool): logger.info("Generating argparse documentation") logger.debug("Root directory: %s", ROOT_DIR.resolve()) logger.debug("Output directory: %s", ARGPARSE_DOC_DIR.resolve()) # Create the ARGPARSE_DOC_DIR if it doesn't exist if not ARGPARSE_DOC_DIR.exists(): ARGPARSE_DOC_DIR.mkdir(parents=True) # Create parsers to document parsers = { # Engine args "engine_args": create_parser(EngineArgs.add_cli_args), "async_engine_args": create_parser( AsyncEngineArgs.add_cli_args, async_args_only=True ), # CLI "serve": create_parser(openai_cli_args.make_arg_parser), "chat": create_parser(ChatCommand.add_cli_args), "complete": create_parser(CompleteCommand.add_cli_args), "run-batch": create_parser(openai_run_batch.make_arg_parser), # Benchmark CLI "bench_latency": create_parser(bench_latency.add_cli_args), "bench_mm_processor": create_parser(bench_mm_processor.add_cli_args), "bench_serve": create_parser(bench_serve.add_cli_args), "bench_sweep_plot": create_parser(bench_sweep_plot.add_cli_args), "bench_sweep_plot_pareto": create_parser(bench_sweep_plot_pareto.add_cli_args), "bench_sweep_serve": create_parser(bench_sweep_serve.add_cli_args), "bench_sweep_serve_workload": create_parser( bench_sweep_serve_workload.add_cli_args ), "bench_throughput": create_parser(bench_throughput.add_cli_args), } # Generate documentation for each parser for stem, parser in parsers.items(): doc_path = ARGPARSE_DOC_DIR / f"{stem}.inc.md" # Specify encoding for building on Windows with open(doc_path, "w", encoding="utf-8") as f: f.write(super(type(parser), parser).format_help()) logger.info("Argparse generated: %s", doc_path.relative_to(ROOT_DIR)) if __name__ == "__main__": on_startup("build", False)
{ "repo_id": "vllm-project/vllm", "file_path": "docs/mkdocs/hooks/generate_argparse.py", "license": "Apache License 2.0", "lines": 195, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/quantization/reference_mxfp4.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch BFLOAT16_EXP_BIAS = 127 BFLOAT16_MANTISSA_BITS = 7 BFLOAT16_EXP_BITS = 8 FLOAT16_EXP_BIAS = 15 FLOAT16_MANTISSA_BITS = 10 FLOAT16_EXP_BITS = 5 FLOAT8_E8M0_MAX_EXP = 127 FLOAT4_EXP_BIAS = 1 FLOAT4_MANTISSA_BITS = 1 FLOAT16_VAL_TO_ADD = 1 << (FLOAT16_MANTISSA_BITS - FLOAT4_MANTISSA_BITS - 1) FLOAT16_SIGN_EXPONENT_MASK = ( (1 << (FLOAT16_EXP_BITS + 1)) - 1 ) << FLOAT16_MANTISSA_BITS BFLOAT16_VAL_TO_ADD = 1 << (BFLOAT16_MANTISSA_BITS - FLOAT4_MANTISSA_BITS - 1) BFLOAT16_SIGN_EXPONENT_MASK = ( (1 << (BFLOAT16_EXP_BITS + 1)) - 1 ) << BFLOAT16_MANTISSA_BITS def e8m0_to_half(scale, half_dtype: torch.dtype): assert scale.dtype == torch.uint8 scale_exp = scale.to(torch.int16) - 127 # This can be implemented with bitwise operations in a proper kernel. scale_half = 2.0 ** (scale_exp.to(torch.float)) return scale_half.to(half_dtype) def upcast_fp4_to_fp16_or_bf16( val, float_dtype: torch.dtype, half_exp_bias: int, half_mantissa_bits: int ): assert val.dtype == torch.uint8 unpacked = torch.zeros( *val.shape[:-1], val.shape[-1] * 2, dtype=torch.uint8, device=val.device ) unpacked[..., 1::2] = (val >> 4) & 0x0F # Extract high 4 bits. unpacked[..., ::2] = val & 0x0F # Extract low 4 bits. # Takes one float4 values represented as b0000xxxx, # and converts it to the corresponding float16 value. sign = unpacked >> 3 exp = (unpacked >> 1) & 3 new_mantissa = unpacked & 1 # if exp == 0 and new_mantissa == 0: # new_exp = 0 # else: # new_exp = exp - FLOAT4_EXP_BIAS + FLOAT16_EXP_BIAS # int8_t works with float16, but may overflow with bfloat16. new_exp = exp - FLOAT4_EXP_BIAS + half_exp_bias # Cast b0000 to 0. in fp16/bf16. new_exp = new_exp * torch.logical_or(exp > 0, new_mantissa > 0) # Cast b0001 to 0.5 in fp16/bf16. new_mantissa = torch.logical_and(new_mantissa, exp > 0) new_mantissa = new_mantissa.to(torch.int32) new_exp = new_exp.to(torch.int32) sign = sign.to(torch.int32) qdq_val = ( (sign << 15) + (new_exp << half_mantissa_bits) + (new_mantissa << (half_mantissa_bits - 1)) ) assert qdq_val.max() <= 65535 assert qdq_val.min() >= 0 qdq_val = qdq_val.to(torch.uint16) result = qdq_val.view(float_dtype) return result def dq_mxfp4_torch( x: torch.Tensor, scale: torch.Tensor, float_dtype: torch.dtype ) -> torch.Tensor: assert x.dtype == torch.uint8 assert scale.dtype == torch.uint8 if float_dtype == torch.float16: half_exp_bias = FLOAT16_EXP_BIAS half_mantissa_bits = FLOAT16_MANTISSA_BITS elif float_dtype == torch.bfloat16: half_exp_bias = BFLOAT16_EXP_BIAS half_mantissa_bits = BFLOAT16_MANTISSA_BITS scale_half = e8m0_to_half(scale, half_dtype=float_dtype) x_half = upcast_fp4_to_fp16_or_bf16( x, float_dtype=float_dtype, half_exp_bias=half_exp_bias, half_mantissa_bits=half_mantissa_bits, ) x_half = x_half.reshape(*x_half.shape[:-1], -1, 32) x_half = x_half * scale_half[..., None] x_half = x_half.reshape(*x_half.shape[:-2], -1) return x_half def fp16_to_fp4_simulate( val, half_mantissa_bits: int, half_exp_bits: int, half_exp_bias: int ): # Casts an fp16/bf16 input to the restricted values of float4_e2m1, # that is to say [0., 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0, -0.0, # -0.5, -1.0, -1.5, -2.0, -3.0, -4.0, -6.0]. float_type = val.dtype # "rshift_cuda" not implemented for 'UInt16' val_view = val.view(torch.int16) # .to(torch.int32) exp = val_view >> half_mantissa_bits exp = exp & ((1 << half_exp_bits) - 1) exp = exp.view(torch.uint16).to(torch.int32) sign = (val_view >> (half_mantissa_bits + half_exp_bits)) & 1 mantissa_last = (val_view >> (half_mantissa_bits - 1)) & 1 exp_unbias = exp - half_exp_bias new_exp = exp_unbias + FLOAT4_EXP_BIAS exp_shift = (new_exp <= 0) * (1 - new_exp) # Typically 9. # Take the min to prevent overflow on `uint16_t half`. This is the case for # very small values, correctly mapped to `round_close`. tail_bits = half_mantissa_bits - FLOAT4_MANTISSA_BITS + exp_shift tail_bits[tail_bits >= 16] = 16 mantissa_plus_one = val_view & ((1 << (half_mantissa_bits + 1)) - 1) half = 1 << (tail_bits - 1) tail = mantissa_plus_one & ((1 << tail_bits) - 1) round_close = tail < half # round towards 0 round_away = tail > half # round away from 0 tie = tail == half new_mantissa_close = torch.zeros(val.shape, device=val.device, dtype=torch.bool) new_exp_close = torch.zeros(val.shape, device=val.device, dtype=torch.uint16) new_mantissa_away = torch.zeros(val.shape, device=val.device, dtype=torch.bool) new_exp_away = torch.zeros(val.shape, device=val.device, dtype=torch.uint16) new_exp_tie = torch.zeros(val.shape, device=val.device, dtype=torch.uint16) # 1. round down # if new_exp == 0: # case [0.5, 0.749999] # new_mantissa = 0 # elif new_exp < 0: # case [0, 0.24999] # new_mantissa = 0 # else: # new_mantissa = mantissa_last new_mantissa_close = (new_exp > 0) * mantissa_last new_exp_close = exp # # 2. round up # if new_exp <= 0: # case [0.250001, 0.499999] and [0.75001, 0.99999] # new_mantissa = 0 # new_exp += 1 # elif mantissa_last == 0: # new_mantissa = 1 # else: # new_mantissa = 0 # new_exp += 1 new_mantissa_away = torch.logical_and(new_exp > 0, mantissa_last == 0) new_exp_away = exp + torch.logical_or(new_exp <= 0, mantissa_last == 1) # # 3. tie # 0.25 -> 0. (handled by `exp > (half_exp_bias - 2)`) # 0.75 -> 1. # 1.25 -> 1. # 1.75 -> 2. # 2.5 -> 2. # 3.5 -> 4. # 5. -> 4. new_exp_tie = (exp > (half_exp_bias - 2)) * (exp + (mantissa_last == 1)) # Gather round up, round down and tie. new_exp = ( round_away * new_exp_away + round_close * new_exp_close + tie * new_exp_tie ) new_mantissa = round_away * new_mantissa_away + round_close * new_mantissa_close # if new_exp > 3: # new_mantissa = 1 new_mantissa = new_mantissa + (new_exp > (2 + half_exp_bias)) * (new_mantissa == 0) # Clamp the exponent to acceptable values. new_exp = (new_exp >= (half_exp_bias - 2)) * torch.clamp( new_exp, half_exp_bias - 2, half_exp_bias + 2 ) sign = sign.to(torch.int32) new_mantissa = new_mantissa.to(torch.int32) qdq_val = ( (sign << 15) + (new_exp << half_mantissa_bits) + (new_mantissa << (half_mantissa_bits - 1)) ) assert qdq_val.max() <= 65535 assert qdq_val.min() >= 0 assert qdq_val.dtype == torch.int32 qdq_val = qdq_val.to(torch.uint16) result = qdq_val.view(float_type) return result def qdq_mxfp4_torch( x: torch.Tensor, scale_calculation_mode: str = "even" ) -> torch.Tensor: half_dtype = x.dtype if half_dtype == torch.float16: half_mantissa_bits = FLOAT16_MANTISSA_BITS half_exp_bits = FLOAT16_EXP_BITS half_exp_bias = FLOAT16_EXP_BIAS val_to_add = FLOAT16_VAL_TO_ADD sign_exponent_mask = FLOAT16_SIGN_EXPONENT_MASK elif half_dtype == torch.bfloat16: half_mantissa_bits = BFLOAT16_MANTISSA_BITS half_exp_bits = BFLOAT16_EXP_BITS half_exp_bias = BFLOAT16_EXP_BIAS val_to_add = BFLOAT16_VAL_TO_ADD sign_exponent_mask = BFLOAT16_SIGN_EXPONENT_MASK else: raise ValueError("not implemented") x = x.reshape(*x.shape[:-1], -1, 32) block_max = torch.max(torch.abs(x), dim=-1).values block_max = block_max.view(torch.uint16).to(torch.int32) block_max_uint = torch.bitwise_and(block_max + val_to_add, sign_exponent_mask) assert block_max_uint.max() <= 65535 assert block_max_uint.min() >= 0 assert block_max_uint.dtype == torch.int32 block_max_uint = block_max_uint.to(torch.uint16) block_max = block_max_uint.view(half_dtype) scale_exp = ( FLOAT8_E8M0_MAX_EXP + torch.floor(torch.log2(block_max)).to(torch.int32) - 2 ) scale_exp = torch.clamp(scale_exp, 0, 2 * FLOAT8_E8M0_MAX_EXP) scale = 2.0 ** (scale_exp - FLOAT8_E8M0_MAX_EXP) scale = scale.to(half_dtype) x = x / scale[..., None] x_fp4 = fp16_to_fp4_simulate( x, half_exp_bits=half_exp_bits, half_mantissa_bits=half_mantissa_bits, half_exp_bias=half_exp_bias, ) x_fp4 = x_fp4 * scale[..., None] return x_fp4.reshape(*x_fp4.shape[:-2], -1)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/quantization/reference_mxfp4.py", "license": "Apache License 2.0", "lines": 214, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/ray/ray_env.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import json import os import vllm.envs as envs from vllm.logger import init_logger logger = init_logger(__name__) CONFIG_HOME = envs.VLLM_CONFIG_ROOT # Env vars that should NOT be copied from the driver to Ray workers. RAY_NON_CARRY_OVER_ENV_VARS_FILE = os.path.join( CONFIG_HOME, "ray_non_carry_over_env_vars.json" ) try: if os.path.exists(RAY_NON_CARRY_OVER_ENV_VARS_FILE): with open(RAY_NON_CARRY_OVER_ENV_VARS_FILE) as f: RAY_NON_CARRY_OVER_ENV_VARS = set(json.load(f)) else: RAY_NON_CARRY_OVER_ENV_VARS = set() except json.JSONDecodeError: logger.warning( "Failed to parse %s. Using an empty set for non-carry-over env vars.", RAY_NON_CARRY_OVER_ENV_VARS_FILE, ) RAY_NON_CARRY_OVER_ENV_VARS = set() # --------------------------------------------------------------------------- # Built-in defaults for env var propagation. # Users can add more via VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY and # VLLM_RAY_EXTRA_ENV_VARS_TO_COPY (additive, not replacing). # --------------------------------------------------------------------------- DEFAULT_ENV_VAR_PREFIXES: set[str] = { "VLLM_", "LMCACHE_", "NCCL_", "UCX_", "HF_", "HUGGING_FACE_", } DEFAULT_EXTRA_ENV_VARS: set[str] = { "PYTHONHASHSEED", } def _parse_csv(value: str) -> set[str]: """Split a comma-separated string into a set of stripped, non-empty tokens.""" return {tok.strip() for tok in value.split(",") if tok.strip()} def get_env_vars_to_copy( exclude_vars: set[str] | None = None, additional_vars: set[str] | None = None, destination: str | None = None, ) -> set[str]: """Return the env var names to copy from the driver to Ray actors. The result is the union of: 1. Env vars registered in ``vllm.envs.environment_variables``. 2. Env vars in ``os.environ`` matching a prefix in ``DEFAULT_ENV_VAR_PREFIXES`` + ``VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY``. 3. Individual names in ``DEFAULT_EXTRA_ENV_VARS`` + ``VLLM_RAY_EXTRA_ENV_VARS_TO_COPY``. 4. Caller-supplied *additional_vars* (e.g. platform-specific). Minus any names in *exclude_vars* or ``RAY_NON_CARRY_OVER_ENV_VARS``. Args: exclude_vars: Env vars to exclude (e.g. worker-specific ones). additional_vars: Extra individual env var names to copy. Useful for caller-specific vars (e.g. platform env vars). destination: Label used in log messages only. """ exclude = (exclude_vars or set()) | RAY_NON_CARRY_OVER_ENV_VARS # -- prefixes (built-in + user-supplied, additive) ---------------------- prefixes = DEFAULT_ENV_VAR_PREFIXES | _parse_csv( envs.VLLM_RAY_EXTRA_ENV_VAR_PREFIXES_TO_COPY ) # -- collect env var names ---------------------------------------------- # 1. vLLM's registered env vars result = set(envs.environment_variables) # 2. Prefix-matched vars present in the current environment result |= {name for name in os.environ if any(name.startswith(p) for p in prefixes)} # 3. Individual extra vars (built-in + user-supplied, additive) result |= DEFAULT_EXTRA_ENV_VARS | _parse_csv(envs.VLLM_RAY_EXTRA_ENV_VARS_TO_COPY) # 4. Caller-supplied extra vars (e.g. platform-specific) result |= additional_vars or set() # 5. Exclude worker-specific and user-blacklisted vars result -= exclude # -- logging ------------------------------------------------------------ dest = f" to {destination}" if destination else "" logger.info("Env var prefixes to copy: %s", sorted(prefixes)) logger.info( "Copying the following environment variables%s: %s", dest, sorted(v for v in result if v in os.environ), ) if RAY_NON_CARRY_OVER_ENV_VARS: logger.info( "RAY_NON_CARRY_OVER_ENV_VARS from config: %s", RAY_NON_CARRY_OVER_ENV_VARS, ) logger.info( "To exclude env vars from copying, add them to %s", RAY_NON_CARRY_OVER_ENV_VARS_FILE, ) return result
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/ray/ray_env.py", "license": "Apache License 2.0", "lines": 97, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/config/test_mp_reducer.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import sys from unittest.mock import patch from vllm.config import VllmConfig from vllm.engine.arg_utils import AsyncEngineArgs from vllm.v1.engine.async_llm import AsyncLLM def test_mp_reducer(): """ Test that _reduce_config reducer is registered when AsyncLLM is instantiated without transformers_modules. This is a regression test for https://github.com/vllm-project/vllm/pull/18640. """ # Ensure transformers_modules is not in sys.modules if "transformers_modules" in sys.modules: del sys.modules["transformers_modules"] with patch("multiprocessing.reducer.register") as mock_register: engine_args = AsyncEngineArgs( model="facebook/opt-125m", max_model_len=32, gpu_memory_utilization=0.1, disable_log_stats=True, ) async_llm = AsyncLLM.from_engine_args( engine_args, start_engine_loop=False, ) assert mock_register.called, ( "multiprocessing.reducer.register should have been called" ) vllm_config_registered = False for call_args in mock_register.call_args_list: # Verify that a reducer for VllmConfig was registered if len(call_args[0]) >= 2 and call_args[0][0] == VllmConfig: vllm_config_registered = True reducer_func = call_args[0][1] assert callable(reducer_func), "Reducer function should be callable" break assert vllm_config_registered, ( "VllmConfig should have been registered to multiprocessing.reducer" ) async_llm.shutdown()
{ "repo_id": "vllm-project/vllm", "file_path": "tests/config/test_mp_reducer.py", "license": "Apache License 2.0", "lines": 42, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/v1/engine/utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import contextlib import os import weakref from collections.abc import Callable, Iterator from dataclasses import dataclass from enum import Enum, auto from multiprocessing import Process, connection from multiprocessing.process import BaseProcess from typing import TYPE_CHECKING from unittest.mock import patch import msgspec import zmq from vllm import envs from vllm.config import CacheConfig, ParallelConfig, VllmConfig from vllm.logger import init_logger from vllm.platforms import current_platform from vllm.ray.ray_env import get_env_vars_to_copy from vllm.utils.network_utils import get_open_zmq_ipc_path, zmq_socket_ctx from vllm.utils.system_utils import get_mp_context from vllm.v1.engine.coordinator import DPCoordinator from vllm.v1.executor import Executor from vllm.v1.utils import get_engine_client_zmq_addr, shutdown if TYPE_CHECKING: from ray.util.placement_group import PlacementGroup logger = init_logger(__name__) STARTUP_POLL_PERIOD_MS = 10000 class CoreEngineState(Enum): NEW = auto() CONNECTED = auto() READY = auto() class CoreEngine: """One per data parallel rank, used to track state during handshaking.""" def __init__(self, index: int = 0, local: bool = True): self.local = local self.identity = index.to_bytes(2, "little") self.state = CoreEngineState.NEW @dataclass class EngineZmqAddresses: # ZMQ input socket addresses for each front-end client (requests) inputs: list[str] # ZMQ output socket addresses for each front-end client (responses) outputs: list[str] # ZMQ input socket address of DP coordinator if applicable coordinator_input: str | None = None # ZMQ output socket address of DP coordinator if applicable coordinator_output: str | None = None # ZMQ socket for front-end to connect to DP coordinator. # Not used by engine, just relayed to front-end in handshake response. # Only required for external DP LB case. frontend_stats_publish_address: str | None = None @dataclass class EngineHandshakeMetadata: """Metadata sent to each engine process during startup handshake, including addresses of the front-end ZMQ queues that they should connect to. """ addresses: EngineZmqAddresses parallel_config: dict[str, int | str | list[int]] class CoreEngineProcManager: """ Utility class to handle creation, readiness, and shutdown of background processes used by the AsyncLLM and LLMEngine. """ def __init__( self, target_fn: Callable, local_engine_count: int, start_index: int, local_start_index: int, vllm_config: VllmConfig, local_client: bool, handshake_address: str, executor_class: type[Executor], log_stats: bool, client_handshake_address: str | None = None, ): context = get_mp_context() common_kwargs = { "vllm_config": vllm_config, "local_client": local_client, "handshake_address": handshake_address, "executor_class": executor_class, "log_stats": log_stats, } if client_handshake_address: common_kwargs["client_handshake_address"] = client_handshake_address self.processes: list[BaseProcess] = [] local_dp_ranks = [] for index in range(local_engine_count): local_index = local_start_index + index global_index = start_index + index # Start EngineCore in background process. local_dp_ranks.append(local_index) self.processes.append( context.Process( target=target_fn, name=f"EngineCore_DP{global_index}", kwargs=common_kwargs | { "dp_rank": global_index, "local_dp_rank": local_index, }, ) ) self._finalizer = weakref.finalize(self, shutdown, self.processes) data_parallel = vllm_config.parallel_config.data_parallel_size > 1 try: for proc, local_dp_rank in zip(self.processes, local_dp_ranks): # Adjust device control in DP for non-CUDA platforms # as well as external and ray launchers # For CUDA platforms, we use torch.cuda.set_device() with ( set_device_control_env_var(vllm_config, local_dp_rank) if ( data_parallel and ( not current_platform.is_cuda_alike() or vllm_config.parallel_config.use_ray ) ) else contextlib.nullcontext() ): proc.start() finally: # Kill other procs if not all are running. if self.finished_procs(): self.close() def close(self): """Shutdown all procs.""" self._finalizer() def join_first(self): """Wait for any process to exit.""" connection.wait(proc.sentinel for proc in self.processes) def sentinels(self) -> list: return [proc.sentinel for proc in self.processes] def finished_procs(self) -> dict[str, int]: """Returns dict of proc name -> exit code for any finished procs.""" return { proc.name: proc.exitcode for proc in self.processes if proc.exitcode is not None } @contextlib.contextmanager def set_device_control_env_var( vllm_config: VllmConfig, local_dp_rank: int ) -> Iterator[None]: """ Temporarily set CUDA_VISIBLE_DEVICES or equivalent for engine subprocess. """ world_size = vllm_config.parallel_config.world_size local_world_size = vllm_config.parallel_config.local_world_size evar = current_platform.device_control_env_var value = get_device_indices(evar, local_dp_rank, world_size, local_world_size) with patch.dict(os.environ, values=((evar, value),)): yield def get_device_indices( device_control_env_var: str, local_dp_rank: int, world_size: int, local_world_size: int | None = None, ): """ Returns a comma-separated string of device indices for the specified data parallel rank. For example, if world_size=2 and local_dp_rank=1, and there are 4 devices, this will select devices 2 and 3 for local_dp_rank=1. """ if local_world_size is None: local_world_size = world_size try: value = ",".join( str(current_platform.device_id_to_physical_device_id(i)) for i in range( local_dp_rank * world_size, local_dp_rank * world_size + local_world_size, ) ) except IndexError as e: raise Exception( f"Error setting {device_control_env_var}: " f"local range: [{local_dp_rank * world_size}, " f"{(local_dp_rank + 1) * world_size}) " "base value: " f'"{os.getenv(device_control_env_var)}"' ) from e return value class CoreEngineActorManager: """ Utility class to handle creation, readiness, and shutdown of core engine Ray actors used by the AsyncLLM and LLMEngine. Different from CoreEngineProcManager, this class manages core engines for both local and remote nodes. """ def __init__( self, vllm_config: VllmConfig, addresses: EngineZmqAddresses, executor_class: type[Executor], log_stats: bool, placement_groups: list["PlacementGroup"] | None = None, local_dp_ranks: list[int] | None = None, ): import copy import ray from ray.runtime_env import RuntimeEnv from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy from vllm.v1.engine.core import DPMoEEngineCoreActor, EngineCoreActor dp_size = vllm_config.parallel_config.data_parallel_size actor_class = ( DPMoEEngineCoreActor if dp_size > 1 and vllm_config.model_config.is_moe else EngineCoreActor ) self.local_engine_actors: list[ray.ActorHandle] = [] self.remote_engine_actors: list[ray.ActorHandle] = [] env_vars_list = get_env_vars_to_copy(destination=actor_class.__name__) self.env_vars_dict = { name: os.environ[name] for name in env_vars_list if name in os.environ } runtime_env = RuntimeEnv(env_vars=self.env_vars_dict) self.addresses = addresses self.executor_class = executor_class self.log_stats = log_stats local_engine_count = vllm_config.parallel_config.data_parallel_size_local world_size = vllm_config.parallel_config.world_size if ray.is_initialized(): logger.info("Ray is already initialized. Skipping Ray initialization.") else: ray.init() vllm_config.parallel_config.allocate_elastic_ep_ports() if placement_groups is not None: assert local_dp_ranks is not None, ( "local_dp_ranks must be provided if placement_groups is provided" ) assert len(placement_groups) == len(local_dp_ranks), ( "placement_groups and local_dp_ranks must have the same length" ) logger.info("Using provided placement groups") # TODO(rui): validate passed-in placement groups self.created_placement_groups = [] else: placement_groups, local_dp_ranks = ( CoreEngineActorManager.create_dp_placement_groups(vllm_config) ) self.created_placement_groups = placement_groups assert len(placement_groups) == dp_size, ( "Number of placement groups must match data parallel size" ) self.placement_group_is_local = [] refs = [] for index, local_index, pg in zip( range(dp_size), local_dp_ranks, placement_groups ): dp_vllm_config = copy.deepcopy(vllm_config) dp_vllm_config.parallel_config.placement_group = pg local_client = index < local_engine_count if dp_size > 1 and dp_vllm_config.kv_transfer_config is not None: # modify the engine_id and append the local_dp_rank to it to ensure # that the kv_transfer_config is unique for each DP rank. dp_vllm_config.kv_transfer_config.engine_id = ( f"{dp_vllm_config.kv_transfer_config.engine_id}_dp{local_index}" ) # Ray XPU known issue: dpctl initializes the GPU runtime early, so # setting device env vars in Ray actor's initialization method # will not affect device selection. See: # https://github.com/ray-project/ray/blob/master/python/ray/_private/accelerators/intel_gpu.py#L56 # noqa: E501 if current_platform.is_xpu(): device_evar = current_platform.device_control_env_var device_indices = get_device_indices( device_evar, local_index, world_size ) actor_env_vars = self.env_vars_dict.copy() actor_env_vars[device_evar] = device_indices runtime_env = RuntimeEnv(env_vars=actor_env_vars) actor = ( ray.remote(actor_class) .options( scheduling_strategy=PlacementGroupSchedulingStrategy( placement_group=pg, placement_group_bundle_index=world_size, ), runtime_env=runtime_env, ) .remote( vllm_config=dp_vllm_config, executor_class=executor_class, log_stats=log_stats, local_client=local_client, addresses=addresses, dp_rank=index, local_dp_rank=local_index, ) ) if local_client: self.local_engine_actors.append(actor) else: self.remote_engine_actors.append(actor) self.placement_group_is_local.append(local_client) refs.append(actor.wait_for_init.remote()) ray.get(refs) self.run_refs = [] for actor in self.local_engine_actors + self.remote_engine_actors: self.run_refs.append(actor.run.remote()) @staticmethod def create_dp_placement_groups( vllm_config: VllmConfig, ) -> tuple[list["PlacementGroup"], list[int]]: """ Create placement groups for data parallel. """ import ray from ray._private.state import available_resources_per_node logger.info("Creating placement groups for data parallel") dp_master_ip = vllm_config.parallel_config.data_parallel_master_ip dp_size = vllm_config.parallel_config.data_parallel_size dp_size_local = vllm_config.parallel_config.data_parallel_size_local available_resources = available_resources_per_node() world_size = vllm_config.parallel_config.world_size placement_groups: list[PlacementGroup] = [] local_dp_ranks: list[int] = [] dp_master_ip_key = f"node:{dp_master_ip}" nodes = sorted( available_resources.values(), key=lambda x: dp_master_ip_key not in x ) assert len(nodes) > 0, "No nodes with resources found in Ray cluster." assert dp_master_ip_key in nodes[0], ( f"The DP master node (ip: {dp_master_ip}) is missing or dead" ) device_str = current_platform.ray_device_key n_node_devices: list[int] = [ int(node_resources[device_str]) for node_resources in nodes if device_str in node_resources ] assert n_node_devices, f"No {device_str} found in Ray cluster." max_device_per_node = max(n_node_devices) pack_strategy = envs.VLLM_RAY_DP_PACK_STRATEGY _supported_pack_strategies = ("strict", "fill", "span") if pack_strategy not in _supported_pack_strategies: raise ValueError( f"{envs.VLLM_RAY_DP_PACK_STRATEGY} is not supported. " "Make sure to set `VLLM_RAY_DP_PACK_STRATEGY` " f"to one of {_supported_pack_strategies}" ) all2all_backend = vllm_config.parallel_config.all2all_backend if pack_strategy == "fill" and ( all2all_backend == "deepep_high_throughput" or all2all_backend == "deepep_low_latency" ): raise ValueError( "DeepEP kernels require EP ranks [0,7] (same for [8,15], ...) " "to be on the same node, but VLLM_RAY_DP_PACK_STRATEGY=fill " "does not guarantee that. " "Please use VLLM_RAY_DP_PACK_STRATEGY=strict instead." ) if pack_strategy in ("strict", "fill"): placement_strategy = "STRICT_PACK" else: placement_strategy = "PACK" assert world_size > max_device_per_node, ( f"World size {world_size} is smaller than the " "maximum number of devices per node " f"{max_device_per_node}. Make sure to set " "`VLLM_RAY_DP_PACK_STRATEGY` to `strict` or `fill`" ) # if we need multiple nodes per dp group, we require for now that # available nodes are homogenous assert set(n_node_devices) == {max_device_per_node}, ( f"Nodes are not homogenous, {nodes}" ) assert world_size % max_device_per_node == 0, ( f"For multi-node data parallel groups, world_size ({world_size}) must " f"be a multiple of number of devices per node ({max_device_per_node})." ) assert len(n_node_devices) * max_device_per_node >= world_size * dp_size, ( f"Not enough total available nodes ({len(n_node_devices)}) " f"and devices per node ({max_device_per_node}) " f"to satisfy required world size {world_size} and data parallel size " f"{dp_size}" ) assert dp_size_local == 1, ( f"data-parallel-size-local {dp_size_local} should be set as the " "default (1) for VLLM_RAY_DP_PACK_STRATEGY=span. " "The actual data-parallel-size-local will be auto determined." ) # bundles collected for a single DP rank from multiple nodes, # for "span" pack strategy collected_bundles = [] for node_resources in nodes: node_ip_keys = [ key for key in node_resources if key != "node:__internal_head__" and key.startswith("node:") ] assert len(node_ip_keys) == 1, ( f"Zero or multiple node IP keys found in node resources: {node_ip_keys}" ) node_ip_key = node_ip_keys[0] node_ip = node_ip_key.split(":")[1] n_device_on_node = int(node_resources.get(device_str, 0)) if pack_strategy == "span" and n_device_on_node != 0: # Strictly speaking, # dp_size_available = n_device_on_node / world_size # and is a fraction, but we use 1 for easier processing dp_size_available = 1 else: dp_size_available = n_device_on_node // world_size if node_ip == dp_master_ip: if dp_size_available < dp_size_local: raise ValueError( f"Not enough resources to allocate {dp_size_local} DP ranks " f"on DP master node {dp_master_ip}, possible to fit " f"{dp_size_available} DP ranks." ) dp_size_to_allocate = dp_size_local elif pack_strategy == "strict": if dp_size_available < dp_size_local: logger.info( "Skipping node %s as %s DP ranks could not fit, " "possible to fit %s DP ranks", node_ip, dp_size_local, dp_size_available, ) continue dp_size_to_allocate = dp_size_local else: # for "pack_strategy" in "fill" and "span" # we always take everything that's available dp_size_to_allocate = dp_size_available for i in range(dp_size_to_allocate): device_bundle = [{device_str: 1.0, "node:" + node_ip: 0.001}] if pack_strategy == "span": collected_bundles += device_bundle * n_device_on_node assert len(collected_bundles) <= world_size, ( "collected_bundles should be <= world_size, " f"but got {len(collected_bundles)=} and {world_size=}" ) # we only create a placement group if we collected enough devices if len(collected_bundles) < world_size: continue bundles = collected_bundles + [{"CPU": 1.0}] collected_bundles = [] else: bundles = device_bundle * world_size + [{"CPU": 1.0}] pg = ray.util.placement_group( name=f"dp_rank_{len(placement_groups)}", strategy=placement_strategy, bundles=bundles, ) placement_groups.append(pg) local_dp_ranks.append(i) if len(placement_groups) == dp_size: break if len(placement_groups) < dp_size: raise ValueError( f"Not enough resources to allocate {dp_size} " "placement groups, only created " f"{len(placement_groups)} placement groups. " "Available resources: " f"{available_resources}" ) assert len(placement_groups) == dp_size, ( f"Created {len(placement_groups)} DP placement groups, expected {dp_size}" ) assert len(local_dp_ranks) == dp_size, ( f"local_dp_ranks length {len(local_dp_ranks)} does not match " f"expected {dp_size}" ) return placement_groups, local_dp_ranks @staticmethod def add_dp_placement_groups( old_vllm_config: VllmConfig, new_data_parallel_size: int ) -> tuple[list["PlacementGroup"], list[int]]: """ Add placement groups for new data parallel size. """ import ray from ray._private.state import ( available_resources_per_node, total_resources_per_node, ) from ray.util.state import list_nodes old_dp_size = old_vllm_config.parallel_config.data_parallel_size num_pg_to_create = new_data_parallel_size - old_dp_size if num_pg_to_create <= 0: return [], [] dp_master_ip = old_vllm_config.parallel_config.data_parallel_master_ip world_size = old_vllm_config.parallel_config.world_size nodes = list_nodes() nodes = sorted(nodes, key=lambda node: node.node_ip != dp_master_ip) assert nodes[0].node_ip == dp_master_ip, "The first node must be the head node" assert len(nodes) == 1 or nodes[1].node_ip != dp_master_ip, ( "There can only be one head node" ) available_resources = available_resources_per_node() total_resources = total_resources_per_node() placement_groups = [] local_dp_ranks = [] num_pg_created = 0 device_str = current_platform.ray_device_key for node in nodes: if num_pg_created >= num_pg_to_create: break node_ip = node.node_ip node_id = node.node_id if device_str not in available_resources[node_id]: continue available_gpus = int(available_resources[node_id][device_str]) # Get total GPUs on this node from the node's resources # Ray stores node resources with node ID as key total_gpus = int(total_resources[node_id][device_str]) # Calculate used GPUs and used engines on this node used_gpus = max(0, total_gpus - available_gpus) used_engines_on_node = used_gpus // world_size # Calculate how many new engines this node can accommodate available_engine_count = available_gpus // world_size # Create placement groups for new engines on this node for i in range(available_engine_count): if num_pg_created >= num_pg_to_create: break rank = old_dp_size + num_pg_created # Create bundles with node constraint for master node if node_ip == dp_master_ip: bundles = [ {device_str: 1.0, "node:" + dp_master_ip: 0.001} ] * world_size + [{"CPU": 1.0}] else: bundles = [{device_str: 1.0}] * world_size + [{"CPU": 1.0}] pg = ray.util.placement_group( name=f"dp_rank_{rank}", strategy="STRICT_PACK", bundles=bundles, ) placement_groups.append(pg) # Local rank starts from the number of engines already used # on this node local_rank = used_engines_on_node + i local_dp_ranks.append(local_rank) num_pg_created += 1 return placement_groups, local_dp_ranks def scale_up_elastic_ep( self, cur_vllm_config: VllmConfig, new_data_parallel_size: int ) -> None: import copy import ray from ray.runtime_env import RuntimeEnv from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy from vllm.v1.engine.core import DPMoEEngineCoreActor, EngineCoreActor actor_class = ( DPMoEEngineCoreActor if cur_vllm_config.model_config.is_moe else EngineCoreActor ) cur_data_parallel_size = len(self.local_engine_actors) + len( self.remote_engine_actors ) assert new_data_parallel_size > cur_data_parallel_size, ( f"New data parallel size {new_data_parallel_size} must be greater " f"than current data parallel size {cur_data_parallel_size} " "for scale up" ) placement_groups, local_dp_ranks = self.add_dp_placement_groups( cur_vllm_config, new_data_parallel_size ) world_size = cur_vllm_config.parallel_config.world_size dp_master_ip = cur_vllm_config.parallel_config.data_parallel_master_ip new_local_engines = 0 runtime_env = RuntimeEnv( env_vars=self.env_vars_dict | {"VLLM_ELASTIC_EP_SCALE_UP_LAUNCH": "1"} ) for i, (pg, local_rank) in enumerate(zip(placement_groups, local_dp_ranks)): rank = cur_data_parallel_size + i dp_vllm_config = copy.deepcopy(cur_vllm_config) dp_vllm_config.parallel_config.data_parallel_size = new_data_parallel_size dp_vllm_config.parallel_config.placement_group = pg # Check if this placement group is on the head node local_client = any( bundle.get("node:" + dp_master_ip, 0) > 0 for bundle in pg.bundle_specs ) if local_client: new_local_engines += 1 # Update data_parallel_size_local dp_vllm_config.parallel_config.data_parallel_size_local = ( cur_vllm_config.parallel_config.data_parallel_size_local + new_local_engines ) actor = ( ray.remote(actor_class) .options( scheduling_strategy=PlacementGroupSchedulingStrategy( placement_group=pg, placement_group_bundle_index=world_size, ), runtime_env=runtime_env, ) .remote( vllm_config=dp_vllm_config, executor_class=self.executor_class, log_stats=self.log_stats, local_client=local_client, addresses=self.addresses, dp_rank=rank, local_dp_rank=local_rank, ) ) if local_client: self.local_engine_actors.append(actor) else: self.remote_engine_actors.append(actor) self.created_placement_groups.append(pg) self.placement_group_is_local.append(local_client) ray.get( [ actor.wait_for_init.remote() for actor in ( self.local_engine_actors[-new_local_engines:] if new_local_engines > 0 else [] ) + self.remote_engine_actors[ -(len(placement_groups) - new_local_engines) : ] ] ) actors = ( self.local_engine_actors[-new_local_engines:] if new_local_engines > 0 else [] ) + self.remote_engine_actors[-(len(placement_groups) - new_local_engines) :] for actor in actors: self.run_refs.append(actor.run.remote()) cur_vllm_config.parallel_config.data_parallel_size = new_data_parallel_size # Update old_vllm_config with new data_parallel_size_local if any new # local engines were added if new_local_engines > 0: cur_vllm_config.parallel_config.data_parallel_size_local += ( new_local_engines ) def scale_down_elastic_ep( self, cur_data_parallel_size: int, new_data_parallel_size: int ) -> None: import ray assert cur_data_parallel_size > new_data_parallel_size, ( f"cur_data_parallel_size {cur_data_parallel_size} must be greater " f"than new_data_parallel_size {new_data_parallel_size} " "for scale down" ) for _ in range(cur_data_parallel_size - new_data_parallel_size): pg = self.created_placement_groups.pop() is_local = self.placement_group_is_local.pop() if is_local: self.local_engine_actors.pop() else: self.remote_engine_actors.pop() ray.util.remove_placement_group(pg) def get_run_refs(self): return self.run_refs def close(self): import ray for actor in self.local_engine_actors + self.remote_engine_actors: ray.kill(actor) for pg in self.created_placement_groups: ray.util.remove_placement_group(pg) def get_engine_zmq_addresses( vllm_config: VllmConfig, num_api_servers: int = 1, ) -> EngineZmqAddresses: """Allocate ZMQ addresses for engine-client communication.""" parallel_config = vllm_config.parallel_config local_engine_count = parallel_config.data_parallel_size_local local_start_index = parallel_config.data_parallel_rank_local dp_size = parallel_config.data_parallel_size host = parallel_config.data_parallel_master_ip local_engines_only = parallel_config.local_engines_only # In offline mode there is an LLM instance per DP rank and # one core engine per LLM, see # examples/offline_inference/data_parallel.py. offline_mode = local_start_index is not None # client_local_only = True for cases where this front-end # sends requests only to colocated engines. client_local_only = ( offline_mode or local_engines_only or (local_engine_count == dp_size) ) # NOTE(yongji): handling scaling from intra-node to inter-node if parallel_config.enable_elastic_ep: client_local_only = False return EngineZmqAddresses( inputs=[ get_engine_client_zmq_addr(client_local_only, host) for _ in range(num_api_servers) ], outputs=[ get_engine_client_zmq_addr(client_local_only, host) for _ in range(num_api_servers) ], ) @contextlib.contextmanager def launch_core_engines( vllm_config: VllmConfig, executor_class: type[Executor], log_stats: bool, addresses: EngineZmqAddresses, num_api_servers: int = 1, ) -> Iterator[ tuple[ CoreEngineProcManager | CoreEngineActorManager | None, DPCoordinator | None, EngineZmqAddresses, ] ]: """Launch engine and DP coordinator processes as needed.""" parallel_config = vllm_config.parallel_config dp_size = parallel_config.data_parallel_size local_engine_count = parallel_config.data_parallel_size_local local_start_index = parallel_config.data_parallel_rank_local dp_rank = parallel_config.data_parallel_rank host = parallel_config.data_parallel_master_ip local_engines_only = parallel_config.local_engines_only offline_mode = local_start_index is not None # Run the DP Coordinator process with rank 0 when in online DP mode. # The coordinator is needed for: # 1. Internal/hybrid LB: collecting and publishing queue stats for load balancing # 2. MoE models: wave coordination in addition to stats run_coordinator = ( vllm_config.needs_dp_coordinator and not offline_mode and dp_rank == 0 ) if run_coordinator: coordinator = DPCoordinator( parallel_config, enable_wave_coordination=vllm_config.model_config.is_moe, ) addresses.coordinator_input, addresses.coordinator_output = ( coordinator.get_engine_socket_addresses() ) addresses.frontend_stats_publish_address = ( coordinator.get_stats_publish_address() ) logger.info("Started DP Coordinator process (PID: %d)", coordinator.proc.pid) else: coordinator = None if parallel_config.data_parallel_backend == "ray": logger.info("Starting ray-based data parallel backend") engine_actor_manager = CoreEngineActorManager( vllm_config=vllm_config, addresses=addresses, executor_class=executor_class, log_stats=log_stats, ) yield engine_actor_manager, coordinator, addresses return if offline_mode: assert local_engine_count == 1 engines_to_handshake = [CoreEngine(index=dp_rank, local=True)] elif dp_rank == 0: # Rank 0 holds Coordinator, so it handshakes with all Cores # in both external dplb and internal dplb mode. # Note this also covers the case where we have zero local engines # and rank 0 is headless. engines_to_handshake = [ CoreEngine(index=i, local=(i < local_engine_count)) for i in range(dp_size) ] else: # Rank > 0 handshakes with just the local cores it is managing. assert local_engines_only, ( "Attempting to launch core_engines from dp_rank > 0, but " "found internal DPLB, which is incompatible." ) engines_to_handshake = [ CoreEngine(index=i, local=True) for i in range(dp_rank, dp_rank + local_engine_count) ] # Whether the started engines will handshake only with co-located # front-end processes. In external_dp_lb mode, ranks > 0 handshake with # their co-located frontend and also the rank 0 front-end, and hence this # will be False. handshake_local_only = offline_mode or local_engine_count == dp_size # NOTE(yongji): handling scaling from intra-node to inter-node if parallel_config.enable_elastic_ep: handshake_local_only = False handshake_address = get_engine_client_zmq_addr( handshake_local_only, host, parallel_config.data_parallel_rpc_port ) if local_engines_only and dp_rank > 0: assert not handshake_local_only local_handshake_address = get_open_zmq_ipc_path() client_handshake_address = local_handshake_address else: local_handshake_address = handshake_address client_handshake_address = None with zmq_socket_ctx( local_handshake_address, zmq.ROUTER, bind=True ) as handshake_socket: from vllm.v1.engine.core import EngineCoreProc # Start local engines. if local_engine_count: local_engine_manager = CoreEngineProcManager( EngineCoreProc.run_engine_core, vllm_config=vllm_config, executor_class=executor_class, log_stats=log_stats, handshake_address=handshake_address, client_handshake_address=client_handshake_address, local_client=True, local_engine_count=local_engine_count, start_index=dp_rank, local_start_index=local_start_index or 0, ) else: local_engine_manager = None yield local_engine_manager, coordinator, addresses # Now wait for engines to start. wait_for_engine_startup( handshake_socket, addresses, engines_to_handshake, parallel_config, dp_size > 1 and vllm_config.model_config.is_moe, vllm_config.cache_config, local_engine_manager, coordinator.proc if coordinator else None, ) def wait_for_engine_startup( handshake_socket: zmq.Socket, addresses: EngineZmqAddresses, core_engines: list[CoreEngine], parallel_config: ParallelConfig, coordinated_dp: bool, cache_config: CacheConfig, proc_manager: CoreEngineProcManager | None, coord_process: Process | None, ): # Wait for engine core process(es) to send ready messages. local_count = parallel_config.data_parallel_size_local remote_count = len(core_engines) - local_count # [local, remote] counts conn_pending, start_pending = [local_count, remote_count], [0, 0] poller = zmq.Poller() poller.register(handshake_socket, zmq.POLLIN) remote_should_be_headless = ( not parallel_config.data_parallel_hybrid_lb and not parallel_config.data_parallel_external_lb ) if proc_manager is not None: for sentinel in proc_manager.sentinels(): poller.register(sentinel, zmq.POLLIN) if coord_process is not None: poller.register(coord_process.sentinel, zmq.POLLIN) while any(conn_pending) or any(start_pending): events = poller.poll(STARTUP_POLL_PERIOD_MS) if not events: if any(conn_pending): logger.debug( "Waiting for %d local, %d remote core engine proc(s) to connect.", *conn_pending, ) if any(start_pending): logger.debug( "Waiting for %d local, %d remote core engine proc(s) to start.", *start_pending, ) continue if len(events) > 1 or events[0][0] != handshake_socket: # One of the local core processes exited. finished = proc_manager.finished_procs() if proc_manager else {} if coord_process is not None and coord_process.exitcode is not None: finished[coord_process.name] = coord_process.exitcode raise RuntimeError( "Engine core initialization failed. " "See root cause above. " f"Failed core proc(s): {finished}" ) # Receive HELLO and READY messages from the input socket. eng_identity, ready_msg_bytes = handshake_socket.recv_multipart() eng_index = int.from_bytes(eng_identity, "little") engine = next((e for e in core_engines if e.identity == eng_identity), None) if engine is None: raise RuntimeError( f"Message from engine with unexpected data parallel rank: {eng_index}" ) msg = msgspec.msgpack.decode(ready_msg_bytes) status, local, headless = msg["status"], msg["local"], msg["headless"] if local != engine.local: raise RuntimeError( f"{status} message from " f"{'local' if local else 'remote'} " f"engine {eng_index}, expected it to be " f"{'local' if engine.local else 'remote'}" ) # Remote engines must be headless iff we aren't in hybrid dp lb mode. if not local and headless != remote_should_be_headless: if headless: raise RuntimeError( f"Remote engine {eng_index} must not use " f"--headless in external or hybrid dp lb " f"mode" ) else: raise RuntimeError( f"Remote engine {eng_index} must use " f"--headless unless in external or hybrid " f"dp lb mode" ) if status == "HELLO" and engine.state == CoreEngineState.NEW: # Send init message with DP config info. init_message = msgspec.msgpack.encode( EngineHandshakeMetadata( addresses=addresses, parallel_config={ k: getattr(parallel_config, k) for k in ( "data_parallel_master_ip", "data_parallel_master_port", "_data_parallel_master_port_list", "data_parallel_size", ) } if coordinated_dp else {}, ) ) handshake_socket.send_multipart((eng_identity, init_message), copy=False) conn_pending[0 if local else 1] -= 1 start_pending[0 if local else 1] += 1 engine.state = CoreEngineState.CONNECTED elif status == "READY" and engine.state == CoreEngineState.CONNECTED: # Setup KV cache config with initialization state from # engine core process. Sum values from all engines in DP case. num_gpu_blocks = cache_config.num_gpu_blocks or 0 num_gpu_blocks += msg["num_gpu_blocks"] cache_config.num_gpu_blocks = num_gpu_blocks # In external DP LB mode, the coordinator address that the # front-end procs connect to is obtained from rank 0 via # one of the engine handshakes, and passed to the local # front-end process in the response from the other. if addresses.frontend_stats_publish_address is None: addresses.frontend_stats_publish_address = msg.get("dp_stats_address") # Validate config hash consistency across DP workers for MoE models. if coordinated_dp: worker_config_hash = msg.get("parallel_config_hash") expected_hash = parallel_config.compute_hash() if worker_config_hash != expected_hash: raise RuntimeError( f"Configuration mismatch detected for engine " f"{eng_index}. All DP workers must have identical " f"configurations for parameters that affect collective " f"communication (e.g., enable_eplb, " f"eplb_config.log_balancedness). " f"Worker hash: {worker_config_hash}, " f"Expected hash: {expected_hash}. " f"Please ensure all workers are started with the same " f"command-line arguments." ) start_pending[0 if local else 1] -= 1 engine.state = CoreEngineState.READY else: raise RuntimeError( f"Unexpected {status} message for " f"{'local' if local else 'remote'} engine " f"{eng_index} in {engine.state} state." ) logger.debug( "%s from %s core engine process %s.", status, "local" if local else "remote", eng_index, )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/engine/utils.py", "license": "Apache License 2.0", "lines": 970, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/kernels/moe/parallel_utils.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ DeepEP test utilities """ import dataclasses import os import traceback from collections.abc import Callable from typing import Concatenate import torch from torch.distributed import ProcessGroup from torch.multiprocessing import spawn # pyright: ignore[reportPrivateImportUsage] from typing_extensions import ParamSpec from vllm.utils.import_utils import has_deep_ep from vllm.utils.network_utils import get_open_port if has_deep_ep(): from vllm.model_executor.layers.fused_moe.deepep_ht_prepare_finalize import ( DeepEPHTPrepareAndFinalize, ) from vllm.model_executor.layers.fused_moe.deepep_ll_prepare_finalize import ( DeepEPLLPrepareAndFinalize, ) ## Parallel Processes Utils P = ParamSpec("P") @dataclasses.dataclass class ProcessGroupInfo: world_size: int world_local_size: int rank: int node_rank: int local_rank: int device: torch.device def _worker_parallel_launch( local_rank: int, world_size: int, world_local_size: int, node_rank: int, init_method: str, worker: Callable[Concatenate[ProcessGroupInfo, P], None], *args: P.args, **kwargs: P.kwargs, ) -> None: rank = node_rank * world_local_size + local_rank torch.cuda.set_device(local_rank) device = torch.device("cuda", local_rank) torch.distributed.init_process_group( backend="cpu:gloo,cuda:nccl", init_method=init_method, rank=rank, world_size=world_size, device_id=device, ) barrier = torch.tensor([rank], device=device) torch.distributed.all_reduce(barrier) try: worker( ProcessGroupInfo( world_size=world_size, world_local_size=world_local_size, rank=rank, node_rank=node_rank, local_rank=local_rank, device=device, ), *args, **kwargs, ) except Exception as ex: print(ex) traceback.print_exc() raise finally: torch.distributed.destroy_process_group() def parallel_launch( world_size: int, worker: Callable[Concatenate[ProcessGroupInfo, P], None], *args: P.args, **kwargs: P.kwargs, ) -> None: assert not kwargs spawn( _worker_parallel_launch, args=( world_size, world_size, 0, f"tcp://{os.getenv('LOCALHOST', 'localhost')}:{get_open_port()}", worker, ) + args, nprocs=world_size, join=True, ) ## DeepEP specific utils @dataclasses.dataclass class DeepEPHTArgs: num_local_experts: int @dataclasses.dataclass class DeepEPLLArgs: max_tokens_per_rank: int hidden_size: int num_experts: int use_fp8_dispatch: bool def make_deepep_ht_a2a( pg: ProcessGroup, pgi: ProcessGroupInfo, dp_size: int, ht_args: DeepEPHTArgs, q_dtype: torch.dtype | None = None, block_shape: list[int] | None = None, ): import deep_ep # high throughput a2a num_nvl_bytes = 1024 * 1024 * 1024 # 1GB num_rdma_bytes, low_latency_mode, num_qps_per_rank = 0, False, 1 buffer = deep_ep.Buffer( group=pg, num_nvl_bytes=num_nvl_bytes, num_rdma_bytes=num_rdma_bytes, low_latency_mode=low_latency_mode, num_qps_per_rank=num_qps_per_rank, ) return DeepEPHTPrepareAndFinalize( buffer=buffer, num_dispatchers=pgi.world_size, dp_size=dp_size, rank_expert_offset=pgi.rank * ht_args.num_local_experts, ) def make_deepep_ll_a2a( pg: ProcessGroup, pgi: ProcessGroupInfo, deepep_ll_args: DeepEPLLArgs, q_dtype: torch.dtype | None = None, block_shape: list[int] | None = None, ): import deep_ep # low-latency a2a num_rdma_bytes = deep_ep.Buffer.get_low_latency_rdma_size_hint( deepep_ll_args.max_tokens_per_rank, deepep_ll_args.hidden_size, pgi.world_size, deepep_ll_args.num_experts, ) buffer = deep_ep.Buffer( group=pg, num_rdma_bytes=num_rdma_bytes, low_latency_mode=True, num_qps_per_rank=deepep_ll_args.num_experts // pgi.world_size, ) return DeepEPLLPrepareAndFinalize( buffer=buffer, num_dispatchers=pgi.world_size, max_tokens_per_rank=deepep_ll_args.max_tokens_per_rank, use_fp8_dispatch=deepep_ll_args.use_fp8_dispatch, ) def make_deepep_a2a( pg: ProcessGroup, pgi: ProcessGroupInfo, dp_size: int, deepep_ht_args: DeepEPHTArgs | None, deepep_ll_args: DeepEPLLArgs | None, q_dtype: torch.dtype | None = None, block_shape: list[int] | None = None, ): if deepep_ht_args is not None: assert deepep_ll_args is None return make_deepep_ht_a2a( pg, pgi, dp_size, deepep_ht_args, q_dtype, block_shape ) assert deepep_ll_args is not None return make_deepep_ll_a2a(pg, pgi, deepep_ll_args, q_dtype, block_shape)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/parallel_utils.py", "license": "Apache License 2.0", "lines": 172, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/test_block_fp8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from tests.kernels.moe.utils import ( make_dummy_moe_config, make_test_quant_config, make_test_weights, modular_triton_fused_moe, ) from tests.kernels.quant_utils import ( native_per_token_group_quant_fp8, native_w8a8_block_matmul, ) from vllm.config import VllmConfig, set_current_vllm_config from vllm.model_executor.layers.activation import SiluAndMul from vllm.model_executor.layers.fused_moe import ( fused_experts, fused_topk, ) from vllm.model_executor.layers.fused_moe.config import ( fp8_w8a8_moe_quant_config, ) from vllm.model_executor.layers.fused_moe.deep_gemm_moe import ( _valid_deep_gemm_shape, ) from vllm.model_executor.layers.fused_moe.prepare_finalize import ( MoEPrepareAndFinalizeNoEP, ) from vllm.model_executor.layers.fused_moe.triton_deep_gemm_moe import ( TritonOrDeepGemmExperts, ) from vllm.platforms import current_platform from vllm.utils.deep_gemm import ( get_mk_alignment_for_contiguous_layout, is_deep_gemm_e8m0_used, ) from vllm.utils.import_utils import has_deep_gemm dg_available = has_deep_gemm() if current_platform.get_device_capability() < (9, 0): pytest.skip("FP8 Triton requires CUDA 9.0 or higher", allow_module_level=True) if current_platform.is_fp8_fnuz(): pytest.skip( "Tests in this file require float8_e4m3fn and platform does not support", allow_module_level=True, ) vllm_config = VllmConfig() # Test configurations DTYPES = [torch.bfloat16] # [torch.half, torch.bfloat16, torch.float32] # Deepseek-V3's intermediate size 18432, so N is 18432*2/8=4608 at TP8 # and its hidden size is 7168. MNK_FACTORS = [ (1, 128, 128), (1, 128, 7168), (1, 1024, 7168), (1, 4608, 128), (1, 4608, 7168), (83, 128, 128), (83, 512, 512), (83, 4608, 512), (83, 4608, 7168), (128, 512, 512), (128, 1024, 7168), (128, 4608, 7168), (2048, 128, 128), (2048, 1024, 7168), (2048, 4608, 512), (2048, 4608, 7168), (8192, 128, 128), (8192, 128, 7168), (8192, 1024, 7168), (8192, 4608, 7168), ] MNK_FACTORS_DG = [ (128, 128, 128), (128, 128, 7168), (128, 1024, 7168), (128, 4608, 128), (128, 4608, 7168), (192, 512, 512), (192, 1024, 7168), (192, 4608, 7168), (1335, 128, 128), (1335, 1024, 7168), (1335, 4608, 512), (1335, 4608, 7168), (2048, 128, 128), (2048, 128, 7168), (2048, 1024, 7168), (2048, 4608, 7168), ] BLOCK_SIZE = [[128, 128]] E = [2, 8, 16] # [128, 256] TOP_KS = [1, 2, 6] SEEDS = [0] def torch_w8a8_block_fp8_moe(a, w1, w2, w1_s, w2_s, topk_weight, topk_ids, block_shape): """Fused moe with block-wise quantization using native torch.""" B, D = a.shape topk = topk_ids.size(1) a = a.view(B, -1, D).repeat(1, topk, 1).reshape(-1, D) out = torch.zeros(B * topk, w2.shape[1], dtype=a.dtype, device=a.device) topk_weight = topk_weight.view(-1) topk_ids = topk_ids.view(-1) _, block_k = block_shape[0], block_shape[1] a_q, a_s = native_per_token_group_quant_fp8(a, block_k) a_q = a_q.to(torch.float32) for i in range(w1.shape[0]): mask = topk_ids == i if mask.sum(): inter_out = native_w8a8_block_matmul( a_q[mask], w1[i], a_s[mask], w1_s[i], block_shape, output_dtype=a.dtype ) act_out = SiluAndMul().forward_native(inter_out) act_out_q, act_out_s = native_per_token_group_quant_fp8(act_out, block_k) out[mask] = native_w8a8_block_matmul( act_out_q, w2[i], act_out_s, w2_s[i], block_shape, output_dtype=a.dtype ) return ( out.view(B, -1, w2.shape[1]) * topk_weight.view(B, -1, 1).to(out.dtype) ).sum(dim=1) # Skip all tests if CUDA is not available pytest.importorskip("torch.cuda") @pytest.fixture(autouse=True) def setup_cuda(): torch.set_default_device("cuda") @pytest.mark.parametrize(("M", "N", "K"), MNK_FACTORS) @pytest.mark.parametrize("E", E) @pytest.mark.parametrize("topk", TOP_KS) @pytest.mark.parametrize("block_size", BLOCK_SIZE) @pytest.mark.parametrize("dtype", DTYPES) @pytest.mark.parametrize("seed", SEEDS) @torch.inference_mode() def test_w8a8_block_fp8_fused_moe( M, N, K, E, topk, block_size, dtype, seed, monkeypatch, workspace_init ): if topk > E: pytest.skip(f"Skipping test; topk={topk} > E={E}") torch.manual_seed(seed) monkeypatch.setenv("VLLM_FUSED_MOE_CHUNK_SIZE", "2048") a = torch.randn((M, K), dtype=dtype) / 10 score = torch.randn((M, E), dtype=dtype) w1, w2, quant_config = make_test_quant_config( E, N, K, dtype, quant_dtype=torch.float8_e4m3fn, per_act_token_quant=False, block_shape=block_size, ) m_fused_moe = modular_triton_fused_moe(make_dummy_moe_config(), quant_config) topk_weights, topk_ids, _ = fused_topk(a, score.float(), topk, False) # Set the context to avoid lots of warning spam. with set_current_vllm_config(vllm_config): ref_out = torch_w8a8_block_fp8_moe( a, w1, w2, quant_config.w1_scale, quant_config.w2_scale, topk_weights, topk_ids, block_size, ) out = fused_experts( a, w1, w2, topk_weights, topk_ids, quant_config=quant_config ) m_out = m_fused_moe(a, w1, w2, topk_weights, topk_ids) # 0.039 only needed for M >= 8192 tol = 0.035 if M < 8192 else 0.039 torch.testing.assert_close(out, ref_out, atol=tol, rtol=tol) torch.testing.assert_close(m_out, ref_out, atol=tol, rtol=tol) @pytest.mark.parametrize(("M", "N", "K"), MNK_FACTORS_DG) @pytest.mark.parametrize("E", E) @pytest.mark.parametrize("topk", TOP_KS) @pytest.mark.parametrize("seed", SEEDS) @pytest.mark.skipif(not dg_available, reason="DeepGemm kernels not available.") @pytest.mark.skipif(is_deep_gemm_e8m0_used(), reason="Not E8M0 scale MOE") @torch.inference_mode() def test_w8a8_block_fp8_deep_gemm_fused_moe(M, N, K, E, topk, seed, monkeypatch): if topk > E: pytest.skip(f"Skipping test: topk={topk} > E={E}") if not _valid_deep_gemm_shape(M, N, K): pytest.skip(f"Skipping test: invalid size m={M}, n={N}, k={K}") chunk_size = 1024 torch.manual_seed(seed) monkeypatch.setenv("VLLM_FUSED_MOE_CHUNK_SIZE", str(chunk_size)) block_size = get_mk_alignment_for_contiguous_layout() dtype = torch.bfloat16 a = torch.randn((M, K), dtype=dtype) / 10 score = torch.randn((M, E), dtype=dtype) (_, w1, w1_s, _), (_, w2, w2_s, _) = make_test_weights( E, N, K, dtype, torch.float8_e4m3fn, per_out_ch_quant=False, block_shape=block_size, ) # Note: for now use_compile will error out if the problem size is # large enough to trigger chunking. I'm leaving the flag and # setup code in case we are able to revisit this later. use_compile = False use_cudagraph = ( chunk_size < M and N >= 1024 and K >= 1024 and current_platform.is_cuda_alike() ) topk_weights, topk_ids, _ = fused_topk(a, score.float(), topk, False) quant_config = fp8_w8a8_moe_quant_config( w1_scale=w1_s, w2_scale=w2_s, block_shape=block_size, ) deep_gemm_experts = mk.FusedMoEModularKernel( prepare_finalize=MoEPrepareAndFinalizeNoEP(), fused_experts=TritonOrDeepGemmExperts( moe_config=make_dummy_moe_config(), quant_config=quant_config, ), inplace=False, ) def deep_gemm_moe_fp8(a, w1, w2, w1_s, w2_s, topk_weights, topk_ids): return deep_gemm_experts( hidden_states=a, w1=w1, w2=w2, topk_weights=topk_weights, topk_ids=topk_ids, ) # Set the context to avoid lots of warning spam. with set_current_vllm_config(vllm_config): ref_out = torch_w8a8_block_fp8_moe( a, w1, w2, w1_s, w2_s, topk_weights, topk_ids, block_size ) if use_compile: deep_gemm_moe_fp8_fn = torch.compile( deep_gemm_moe_fp8, backend="inductor", fullgraph=True ) torch._dynamo.mark_dynamic(a, 0) torch._dynamo.mark_dynamic(topk_weights, 0) torch._dynamo.mark_dynamic(topk_ids, 0) else: deep_gemm_moe_fp8_fn = deep_gemm_moe_fp8 out = deep_gemm_moe_fp8_fn(a, w1, w2, w1_s, w2_s, topk_weights, topk_ids) if use_cudagraph: out.fill_(0) stream = torch.cuda.Stream() graph = torch.cuda.CUDAGraph() with torch.cuda.graph(graph, stream=stream): out = deep_gemm_moe_fp8_fn( a, w1, w2, w1_s, w2_s, topk_weights, topk_ids ) torch.cuda.synchronize() graph.replay() torch.cuda.synchronize() torch.testing.assert_close(out, ref_out, atol=0.035, rtol=0.035)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/test_block_fp8.py", "license": "Apache License 2.0", "lines": 257, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/test_block_int8.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import torch from tests.kernels.moe.utils import make_test_quant_config from tests.kernels.quant_utils import ( native_per_token_group_quant_int8, native_w8a8_block_matmul, ) from vllm.config import VllmConfig, set_current_vllm_config from vllm.model_executor.layers.activation import SiluAndMul from vllm.model_executor.layers.fused_moe import fused_experts, fused_topk from vllm.platforms import current_platform if current_platform.get_device_capability() < (7, 0): pytest.skip("INT8 Triton requires CUDA 7.0 or higher", allow_module_level=True) vllm_config = VllmConfig() DTYPES = [torch.bfloat16] MNK_FACTORS = [ (1, 128, 128), (1, 128, 7168), (1, 1024, 7168), (1, 4096, 512), (1, 4096, 7168), (33, 512, 512), (33, 128, 7168), (33, 1024, 7168), (33, 4096, 128), (33, 4096, 7168), (128, 128, 128), (128, 1024, 7168), (128, 4096, 512), (128, 4096, 7168), (222, 512, 512), (222, 1024, 7168), (222, 4096, 7168), (2048, 128, 128), (2048, 1024, 7168), (2048, 4096, 4096), ] E = [8, 24] TOP_KS = [2, 6] # BLOCK_SIZE = [[64, 64], [64, 128], [128, 64], [128, 128]] BLOCK_SIZE = [[128, 128]] SEEDS = [0] # For test def torch_w8a8_block_int8_moe(a, w1, w2, w1_s, w2_s, score, topk, block_shape): """This function performs fused moe with block-wise quantization using native torch.""" B, D = a.shape a = a.view(B, -1, D).repeat(1, topk, 1).reshape(-1, D) out = torch.zeros(B * topk, w2.shape[1], dtype=a.dtype, device=a.device) score = torch.softmax(score, dim=-1, dtype=torch.float32) topk_weight, topk_ids = torch.topk(score, topk) topk_weight = topk_weight.view(-1) topk_ids = topk_ids.view(-1) _, block_k = block_shape[0], block_shape[1] a_q, a_s = native_per_token_group_quant_int8(a, block_k) for i in range(w1.shape[0]): mask = topk_ids == i if mask.sum(): inter_out = native_w8a8_block_matmul( a_q[mask], w1[i], a_s[mask], w1_s[i], block_shape, output_dtype=a.dtype ) act_out = SiluAndMul().forward_native(inter_out) act_out_q, act_out_s = native_per_token_group_quant_int8(act_out, block_k) act_out = act_out.to(torch.float32) out[mask] = native_w8a8_block_matmul( act_out_q, w2[i], act_out_s, w2_s[i], block_shape, output_dtype=a.dtype ) return ( out.view(B, -1, w2.shape[1]) * topk_weight.view(B, -1, 1).to(out.dtype) ).sum(dim=1) @pytest.fixture(autouse=True, scope="module") def setup_cuda(): """Sets the default CUDA device for all tests in this module.""" torch.set_default_device("cuda") @pytest.mark.parametrize(("M", "N", "K"), MNK_FACTORS) @pytest.mark.parametrize("E", E) @pytest.mark.parametrize("topk", TOP_KS) @pytest.mark.parametrize("block_size", BLOCK_SIZE) @pytest.mark.parametrize("dtype", DTYPES) @pytest.mark.parametrize("seed", SEEDS) @torch.inference_mode() def test_w8a8_block_int8_fused_moe(M, N, K, E, topk, block_size, dtype, seed): """Tests the fused_moe kernel with W8A8 INT8 block quantization against a native torch reference.""" torch.manual_seed(seed) a = torch.randn((M, K), dtype=dtype) / 10 score = torch.randn((M, E), dtype=dtype) topk_weights, topk_ids, _ = fused_topk(a, score.float(), topk, False) w1, w2, quant_config = make_test_quant_config( E, N, K, dtype, quant_dtype=torch.int8, per_act_token_quant=False, block_shape=block_size, ) # Set the context to avoid lots of warning spam. with set_current_vllm_config(vllm_config): out = fused_experts( a, w1, w2, topk_weights, topk_ids, quant_config=quant_config ) ref_out = torch_w8a8_block_int8_moe( a, w1, w2, quant_config.w1_scale, quant_config.w2_scale, score, topk, block_size, ) # Check results torch.testing.assert_close(out, ref_out, atol=0.065, rtol=0.065)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/test_block_int8.py", "license": "Apache License 2.0", "lines": 116, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/model_executor/layers/fused_moe/config.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from dataclasses import dataclass from enum import IntEnum from typing import Union import torch import vllm.envs as envs from vllm.config import ParallelConfig from vllm.distributed import get_dp_group, get_pcp_group, get_tensor_model_parallel_rank from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.activation import MoEActivation from vllm.model_executor.layers.quantization.utils.ocp_mx_utils import ( OCP_MX_DTYPES, OCP_MX_Scheme, ) from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape from vllm.platforms import current_platform from vllm.utils.import_utils import has_triton_kernels from vllm.utils.math_utils import cdiv logger = init_logger(__name__) if has_triton_kernels(): try: from triton_kernels.matmul_ogs import PrecisionConfig except (ImportError, AttributeError) as e: logger.error( "Failed to import Triton kernels. Please make sure your triton " "version is compatible. Error: %s", e, ) def _get_config_dtype_str( dtype: torch.dtype, use_fp8_w8a8: bool = False, use_fp8_w8a16: bool = False, use_int8_w8a16: bool = False, use_int4_w4a16: bool = False, ocp_mx_scheme: str | None = None, ) -> str | None: """ Return a string used to construct the filename that contains the tuning info for a particular quantization scheme. See try_get_optimal_moe_config in fused_moe.py. """ if use_fp8_w8a8: return "fp8_w8a8" elif use_fp8_w8a16: return "fp8_w8a16" elif use_int8_w8a16: return "int8_w8a16" elif use_int4_w4a16: return "int4_w4a16" elif ocp_mx_scheme is not None: # The output of this function is passed to `try_get_optimal_moe_config`, # and as we only simulate OCP MX execution in fused_moe for now, # we will NOT look for `*,dtype=w_mxfp4_a_mxfp4.json` for now. return None elif dtype == torch.float: # avoiding cases where kernel fails when float32 MoE # use fp16/bfloat16 configs return "float32" return None def _quant_flags_to_group_shape( quant_dtype: torch.dtype | str | None, per_act_token_quant: bool, per_out_ch_quant: bool, block_shape: list[int] | None, ) -> tuple[GroupShape | None, GroupShape | None]: """ Convert MoE quantization flags into more generic GroupShapes. """ a_shape: GroupShape | None w_shape: GroupShape | None if block_shape is not None: assert not per_act_token_quant assert not per_out_ch_quant # TODO(bnell): this is not quite right for activations since first # dim should be 1. a_shape = GroupShape(row=block_shape[0], col=block_shape[1]) w_shape = GroupShape(row=block_shape[0], col=block_shape[1]) else: w_shape = None a_shape = None if quant_dtype is None else GroupShape.PER_TENSOR if per_act_token_quant: a_shape = GroupShape.PER_TOKEN if per_out_ch_quant: w_shape = GroupShape.PER_TOKEN return a_shape, w_shape # The type of method in top-K routing # Please keep this in sync with the counterpart defined in https://github.com/flashinfer-ai/flashinfer/blob/main/include/flashinfer/trtllm/fused_moe/runner.h class RoutingMethodType(IntEnum): # Default: Softmax -> TopK Default = (0,) # Renormalize: TopK -> Softmax/Sigmoid Renormalize = (1,) # DeepSeekV3: Sigmoid -> RoutingBiasAdd -> Top2 in group -> Top4 groups # -> Top8 experts from the Top4 groups DeepSeekV3 = (2,) # Llama4: Top1 -> Sigmoid Llama4 = (3,) # RenormalizeNaive: Softmax/Sigmoid -> TopK -> Renormalize RenormalizeNaive = (4,) # TopK: TopK (no softmax) TopK = (5,) # Custom Custom = (6,) # Simulated Simulated = (7,) # Unspecified Unspecified = 8.0 def get_routing_method_type( scoring_func: str, top_k: int, renormalize: bool, num_expert_group: int | None, has_e_score_bias: bool, ) -> RoutingMethodType: if has_e_score_bias: if (num_expert_group or 0) > 0 and scoring_func == "sigmoid": return RoutingMethodType.DeepSeekV3 else: return RoutingMethodType.Unspecified if scoring_func == "sigmoid": if top_k == 1: return RoutingMethodType.Llama4 else: return RoutingMethodType.Unspecified if scoring_func == "softmax": if renormalize: return RoutingMethodType.Renormalize else: return RoutingMethodType.Default return RoutingMethodType.Unspecified @dataclass class FusedMoEQuantDesc: """ A quantization descriptor for fused MoE ops. This class can describe either activations or weights. """ # The quantized type of this parameters. None means unquantized or # already quantized. # TODO (bnell): use scalar_type instead of Union. dtype: torch.dtype | str | None = None # A field that describes the quantization group shape, from quant_utils.py. # * (-1, -1) for per-tensor quantization # * (1, -1) for per-row quantization # * (-1, 1) for per-column quantization # * (128, 128) for 128x128 deepseek style block quantization # * (1, 128) for deepseek style activation quantization # (i.e. per-token-per-group) shape: GroupShape | None = None # Quantization scales. # TODO(bnell): maybe put PrecisionConfigs in subclass of QuantDesc? scale: Union[torch.Tensor, "PrecisionConfig", None] = None # Quantization alphas or gscales, used for nvfp4 types. # W4A8 FP8: used for per-channel scales # TODO(bnell): put some of these in subclasses alpha_or_gscale: torch.Tensor | None = None # Zero points for int4/int8 types zp: torch.Tensor | None = None # Biases for GPT triton MoE bias: torch.Tensor | None = None # TODO(bnell): have subclasses for specific moe methods? # e.g. for specific arguments bias, precision, etc. @dataclass class FusedMoEQuantConfig: """ The FusedMoEQuantConfig contains all the quantization parameters for a single FusedMoEMethodBase operation. It consists of four FusedMoEQuantDescs, one for each activation and set of weights. Each FusedMoEMethodBase must implement a get_fused_moe_quant_config method to construct a FusedMoEQuantConfig for use with that class. FusedMoEQuant configs are only used for modular kernels, fused_experts (from fused_moe.py), cutlass_moe_fp[48], rocm_aiter_fused_experts and triton_kernel_moe_forward. Other MoE methods can ignore the FusedMoEQuantConfig (for now) and hardcode it to None. There are currently some restrictions on what can be expressed: - Most MoE ops only support similar quantization strategies for each parameter, e.g. both weights must have the same GroupShape and both activations must share the same GroupShape. One exception to this is the cutlass moe which allows per channel quantization on the outputs. Note: this restrictions are not always rigorously checked. - Not all fused MoE functions support all the parameters, e.g. zero points, global scales, alphas and biases are not universally supported. - Fully general GroupShapes are not allowed. Activations only support per token, per tensor or K-blocked. - Weights are not required to have a GroupShape since they have already been quantized. Other notes: - PrecisionConfigs are specific to GPT OSS Triton. - As a follow up it would probably make sense to subclass FusedMoEQuantDesc or FusedMoEQuantConfig for particular FusedMoEMethodBase subclasses so that only the required quantization parameters are used/stored. """ # TODO(bnell) make sure a1_scales/a2_scales don't interfere with chunking _a1: FusedMoEQuantDesc _a2: FusedMoEQuantDesc _w1: FusedMoEQuantDesc _w2: FusedMoEQuantDesc def __post_init__(self): assert not self.per_act_token_quant or self.block_shape is None, ( "illegal quantization" ) # # Convenience accessors for various properties. # @property def quant_dtype(self) -> torch.dtype | str | None: return self._a1.dtype @property def weight_quant_dtype(self) -> torch.dtype | str | None: return self._w1.dtype @property def is_quantized(self) -> bool: return self.quant_dtype is not None @property def is_per_act_token(self) -> bool: return self._a1.shape == GroupShape.PER_TOKEN @property def per_act_token_quant(self) -> bool: return self._a1.shape == GroupShape.PER_TOKEN @property def per_out_ch_quant(self) -> bool: return self._w1.shape == GroupShape.PER_TOKEN @property def is_per_tensor(self) -> bool: return self._a1.shape == GroupShape.PER_TENSOR @property def block_shape(self) -> list[int] | None: if ( self._a1.shape is not None and self._a1.shape != GroupShape.PER_TENSOR and self._a1.shape != GroupShape.PER_TOKEN ): return [self._a1.shape.row, self._a1.shape.col] else: return None @property def is_block_quantized(self) -> bool: return self.block_shape is not None @property def a1_scale(self) -> torch.Tensor | None: assert self._a1.scale is None or isinstance(self._a1.scale, torch.Tensor) return self._a1.scale @property def a1_gscale(self) -> torch.Tensor | None: return self._a1.alpha_or_gscale @property def a2_scale(self) -> torch.Tensor | None: assert self._a2.scale is None or isinstance(self._a2.scale, torch.Tensor) return self._a2.scale @property def a2_gscale(self) -> torch.Tensor | None: return self._a2.alpha_or_gscale @property def w1_scale(self) -> torch.Tensor | None: assert self._w1.scale is None or isinstance(self._w1.scale, torch.Tensor) return self._w1.scale @property def w1_zp(self) -> torch.Tensor | None: return self._w1.zp @property def w1_bias(self) -> torch.Tensor | None: return self._w1.bias @property def w1_precision(self) -> "PrecisionConfig | None": assert self._w1.scale is None or isinstance(self._w1.scale, PrecisionConfig) return self._w1.scale @property def g1_alphas(self) -> torch.Tensor | None: return self._w1.alpha_or_gscale @property def w2_scale(self) -> torch.Tensor | None: assert self._w2.scale is None or isinstance(self._w2.scale, torch.Tensor) return self._w2.scale @property def w2_zp(self) -> torch.Tensor | None: return self._w2.zp @property def w2_bias(self) -> torch.Tensor | None: return self._w2.bias @property def w2_precision(self) -> "PrecisionConfig | None": assert self._w2.scale is None or isinstance(self._w2.scale, PrecisionConfig) return self._w2.scale @property def g2_alphas(self) -> torch.Tensor | None: return self._w2.alpha_or_gscale @property def use_fp8_w8a8(self) -> bool: return self.quant_dtype == torch.float8_e4m3fn @property def use_int8_w8a8(self) -> bool: return self.quant_dtype == torch.int8 @property def use_int8_w8a16(self) -> bool: return self._a1.dtype is None and self._w1.dtype == torch.int8 @property def use_fp8_w8a16(self) -> bool: return self._a1.dtype is None and self._w1.dtype == current_platform.fp8_dtype() @property def use_int4_w4a16(self) -> bool: return self._a1.dtype is None and self._w1.dtype == "int4" @property def use_nvfp4_w4a16(self) -> bool: return self._a1.dtype is None and self._w1.dtype == "nvfp4" @property def ocp_mx_scheme(self) -> str | None: if not hasattr(self, "_ocp_mx_scheme"): if (self._a1.dtype is not None and not isinstance(self._a1.dtype, str)) or ( self._w1.dtype is not None and not isinstance(self._w1.dtype, str) ): self._ocp_mx_scheme = None else: ocp_mx_scheme = OCP_MX_Scheme.from_quant_dtype( self._a1.dtype, self._w1.dtype ) if ocp_mx_scheme is not None: ocp_mx_scheme = ocp_mx_scheme.value self._ocp_mx_scheme = ocp_mx_scheme return self._ocp_mx_scheme @property def use_mxfp4_w4a16(self) -> bool: return self._a1.dtype is None and self._w1.dtype == "mxfp4" @property def use_mxfp4_w4a4(self) -> bool: return self._a1.dtype == "mxfp4" and self._w1.dtype == "mxfp4" @property def use_nvfp4_w4a4(self) -> bool: return self.quant_dtype == "nvfp4" @property def use_mxfp4_w4a8(self) -> bool: return self._a1.dtype == "fp8" and self._w1.dtype == "mxfp4" def config_name(self, dtype: torch.dtype) -> str | None: """ Return a string used to construct the filename that contains the tuning info for a particular quantization scheme. See try_get_optimal_moe_config in fused_moe.py. """ return _get_config_dtype_str( use_fp8_w8a8=self.use_fp8_w8a8, use_fp8_w8a16=self.use_fp8_w8a16, use_int8_w8a16=self.use_int8_w8a16, use_int4_w4a16=self.use_int4_w4a16, ocp_mx_scheme=self.ocp_mx_scheme, dtype=dtype, ) def scale_shape( self, max_tokens: int, hidden_dim: int, ) -> tuple[int, int] | None: """ Construct the proper activation scale shape for this config. """ if self.is_quantized: if self.is_block_quantized: assert self.block_shape is not None _, block_k = self.block_shape k_tiles = cdiv(hidden_dim, block_k) return (max_tokens, k_tiles) elif self.is_per_act_token: return (max_tokens, 1) else: return (1, 1) else: return None def batched_scale_shape( self, num_experts: int, max_tokens: int, hidden_dim: int, ) -> tuple[int, int, int] | None: """ Construct the proper activation batched scale shape for this config, e.g. (num experts, *scale_shape). """ if self.is_quantized: scale_shape = self.scale_shape(max_tokens, hidden_dim) assert scale_shape is not None return (num_experts, *scale_shape) else: return None @staticmethod def make( quant_dtype: torch.dtype | str | None = None, per_act_token_quant: bool = False, per_out_ch_quant: bool = False, block_shape: list[int] | None = None, w1_scale: Union[torch.Tensor, "PrecisionConfig", None] = None, w2_scale: Union[torch.Tensor, "PrecisionConfig", None] = None, a1_scale: torch.Tensor | None = None, a2_scale: torch.Tensor | None = None, g1_alphas: torch.Tensor | None = None, g2_alphas: torch.Tensor | None = None, a1_gscale: torch.Tensor | None = None, a2_gscale: torch.Tensor | None = None, w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, w1_zp: torch.Tensor | None = None, w2_zp: torch.Tensor | None = None, weight_dtype: torch.dtype | str | None = None, ) -> "FusedMoEQuantConfig": """ General builder function for a FusedMoEQuantConfig. - quant_dtype: Optional quantization type. None if activations are unquantized or quantized prior to calling. Note: "nvfp4", "mxfp4", "mxfp6_e3m2", "mxfp6_e2m3" are the only valid string values for quant_dtype. - per_act_token_quant: Activations have per token quantization. - per_out_ch_quant: Outputs have per channel quantization. (only for cutlass). - block_shape: Optional block size for block-wise quantization. Incompatible with per_act_token and per_out_ch quant. - w1_scale: Optional scale to be used for w1. - w2_scale: Optional scale to be used for w2. - a1_scale: Optional scale to be used for a1. - a2_scale: Optional scale to be used for a2. - g1_alphas: Optional global quantization scales for w1 (for nvfp4). Optional per-channel scales for w1 (for W4A8 FP8). Optional dq scale i.e. w_scale * a_scale (for W8A8 fp8). - g2_alphas: Optional global quantization scales for w2 (for nvfp4). Optional per-channel scales for w2 (for W4A8 FP8). Optional dq scale i.e. w_scale * a_scale (for W8A8 fp8). - a1_gscale: Optional global quantization scales for a1 (1.0 /a2_scale). - a2_gscale: Optional global quantization scales for a2 (1.0 /a2_scale). - w1_bias: Optional biases for w1 (GPT OSS Triton). - w2_bias: Optional biases for w1 (GPT OSS Triton). - w1_zp: Optional w1 zero points for int4/int8 quantization. - w2_zp: Optional w2 zero points for int4/int8 quantization. """ assert not isinstance(quant_dtype, str) or quant_dtype in { "nvfp4", "mxfp4", "mxfp6_e3m2", "mxfp6_e2m3", "mxfp8", } assert not isinstance(weight_dtype, str) or weight_dtype in { "nvfp4", "mxfp4", "mxfp6_e3m2", "mxfp6_e2m3", "int4", "mxfp8", } if weight_dtype is None: weight_dtype = quant_dtype a_shape, w_shape = _quant_flags_to_group_shape( quant_dtype, per_act_token_quant, per_out_ch_quant, block_shape ) quant_config = FusedMoEQuantConfig( _a1=FusedMoEQuantDesc(quant_dtype, a_shape, a1_scale, a1_gscale), _a2=FusedMoEQuantDesc(quant_dtype, a_shape, a2_scale, a2_gscale), _w1=FusedMoEQuantDesc( weight_dtype, w_shape, w1_scale, g1_alphas, w1_zp, w1_bias ), _w2=FusedMoEQuantDesc( weight_dtype, w_shape, w2_scale, g2_alphas, w2_zp, w2_bias ), ) assert quant_config.per_act_token_quant == per_act_token_quant assert quant_config.per_out_ch_quant == per_out_ch_quant assert quant_config.block_shape == block_shape return quant_config def fp8_w8a8_moe_quant_config( w1_scale: torch.Tensor, w2_scale: torch.Tensor, a1_scale: torch.Tensor | None = None, a2_scale: torch.Tensor | None = None, w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, per_act_token_quant: bool = False, per_out_ch_quant: bool = False, block_shape: list[int] | None = None, a1_gscale: torch.Tensor | None = None, a2_gscale: torch.Tensor | None = None, g1_alphas: torch.Tensor | None = None, g2_alphas: torch.Tensor | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for fp8 activations and fp8 weights. """ return FusedMoEQuantConfig.make( torch.float8_e4m3fn, w1_scale=w1_scale, g1_alphas=g1_alphas, w2_scale=w2_scale, g2_alphas=g2_alphas, w1_bias=w1_bias, w2_bias=w2_bias, a1_scale=a1_scale, a1_gscale=a1_gscale, a2_scale=a2_scale, a2_gscale=a2_gscale, per_act_token_quant=per_act_token_quant, per_out_ch_quant=per_out_ch_quant, block_shape=block_shape, ) def int8_w8a8_moe_quant_config( w1_scale: torch.Tensor, w2_scale: torch.Tensor, a1_scale: torch.Tensor | None, a2_scale: torch.Tensor | None, w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, per_act_token_quant: bool = False, ) -> FusedMoEQuantConfig: """ Construct a quant config for int8 activations and int8 weights. """ return FusedMoEQuantConfig.make( torch.int8, w1_scale=w1_scale, w2_scale=w2_scale, a1_scale=a1_scale, a2_scale=a2_scale, w1_bias=w1_bias, w2_bias=w2_bias, per_act_token_quant=per_act_token_quant, per_out_ch_quant=False, block_shape=None, ) def gptq_marlin_moe_quant_config( w1_scale: torch.Tensor, w2_scale: torch.Tensor, weight_bits: int, group_size: int, w1_zp: torch.Tensor | None = None, w2_zp: torch.Tensor | None = None, w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, ): """ Construct a quant config for gptq marlin quantization. """ from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape w_shape = None if group_size == -1 else GroupShape(row=1, col=group_size) # Activations are NOT quantized for GPTQ (fp16/bf16) a_shape = w_shape # Same as weight shape for alignment # Determine weight dtype if weight_bits == 4: weight_dtype = "int4" elif weight_bits == 8: weight_dtype = torch.int8 else: raise ValueError(f"Unsupported weight_bits: {weight_bits}") return FusedMoEQuantConfig( _a1=FusedMoEQuantDesc(dtype=None, shape=a_shape), _a2=FusedMoEQuantDesc(dtype=None, shape=a_shape), _w1=FusedMoEQuantDesc(weight_dtype, w_shape, w1_scale, None, w1_zp, w1_bias), _w2=FusedMoEQuantDesc(weight_dtype, w_shape, w2_scale, None, w2_zp, w2_bias), ) def mxfp4_w4a16_moe_quant_config( w1_scale: Union[torch.Tensor, "PrecisionConfig"], w2_scale: Union[torch.Tensor, "PrecisionConfig"], w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for unquantized activations and mxfp4 weights. """ return FusedMoEQuantConfig( _a1=FusedMoEQuantDesc(), _a2=FusedMoEQuantDesc(), _w1=FusedMoEQuantDesc("mxfp4", None, w1_scale, None, None, w1_bias), _w2=FusedMoEQuantDesc("mxfp4", None, w2_scale, None, None, w2_bias), ) def mxfp4_mxfp8_moe_quant_config( w1_scale: Union[torch.Tensor, "PrecisionConfig"], w2_scale: Union[torch.Tensor, "PrecisionConfig"], a1_scale: torch.Tensor | None = None, a2_scale: torch.Tensor | None = None, w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, block_shape: list[int] | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for mxfp4 activations and mxfp4 weights. """ return FusedMoEQuantConfig( _a1=FusedMoEQuantDesc("mxfp8"), _a2=FusedMoEQuantDesc("mxfp8"), _w1=FusedMoEQuantDesc("mxfp4", None, w1_scale, None, None, w1_bias), _w2=FusedMoEQuantDesc("mxfp4", None, w2_scale, None, None, w2_bias), ) def mxfp4_w4a8_moe_quant_config( w1_scale: Union[torch.Tensor, "PrecisionConfig"], w2_scale: Union[torch.Tensor, "PrecisionConfig"], a1_scale: torch.Tensor | None = None, a2_scale: torch.Tensor | None = None, w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, block_shape: list[int] | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for fp8 activations and mxfp4 weights. """ return FusedMoEQuantConfig( _a1=FusedMoEQuantDesc("fp8", None, a1_scale, None, None, None), _a2=FusedMoEQuantDesc("fp8", None, a2_scale, None, None, None), _w1=FusedMoEQuantDesc("mxfp4", None, w1_scale, None, None, w1_bias), _w2=FusedMoEQuantDesc("mxfp4", None, w2_scale, None, None, w2_bias), ) def ocp_mx_moe_quant_config( quant_dtype: str, w1_scale: Union[torch.Tensor, "PrecisionConfig"], w2_scale: Union[torch.Tensor, "PrecisionConfig"], weight_dtype: str | None = None, a1_scale: torch.Tensor | None = None, a2_scale: torch.Tensor | None = None, w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, block_shape: list[int] | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for mxfp4 activations and mxfp4 weights. """ assert quant_dtype in OCP_MX_DTYPES return FusedMoEQuantConfig.make( quant_dtype=quant_dtype, weight_dtype=weight_dtype, w1_scale=w1_scale, w2_scale=w2_scale, a1_scale=a1_scale, a2_scale=a2_scale, w1_bias=w1_bias, w2_bias=w2_bias, per_act_token_quant=False, per_out_ch_quant=False, block_shape=block_shape, ) def nvfp4_moe_quant_config( g1_alphas: torch.Tensor, g2_alphas: torch.Tensor, a1_gscale: torch.Tensor, a2_gscale: torch.Tensor, w1_scale: torch.Tensor, w2_scale: torch.Tensor, w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for mxfp4 activations and nvp4 weights. """ return FusedMoEQuantConfig.make( "nvfp4", w1_scale=w1_scale, w2_scale=w2_scale, w1_bias=w1_bias, w2_bias=w2_bias, a1_gscale=a1_gscale, a2_gscale=a2_gscale, g1_alphas=g1_alphas, g2_alphas=g2_alphas, per_act_token_quant=False, per_out_ch_quant=False, block_shape=None, ) def nvfp4_w4a16_moe_quant_config( g1_alphas: torch.Tensor, g2_alphas: torch.Tensor, w1_scale: torch.Tensor, w2_scale: torch.Tensor, ) -> FusedMoEQuantConfig: """ Construct a quant config for 16-but activations and nvp4 weights. """ return FusedMoEQuantConfig.make( quant_dtype=None, w1_scale=w1_scale, w2_scale=w2_scale, g1_alphas=g1_alphas, g2_alphas=g2_alphas, weight_dtype="nvfp4", ) def int4_w4a16_moe_quant_config( w1_scale: torch.Tensor, w2_scale: torch.Tensor, w1_zp: torch.Tensor | None, w2_zp: torch.Tensor | None, block_shape: list[int] | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for 16-bit float activations and int4 weights. """ group_shape = GroupShape(*block_shape) if block_shape is not None else None return FusedMoEQuantConfig( _a1=FusedMoEQuantDesc(shape=group_shape), _a2=FusedMoEQuantDesc(shape=group_shape), _w1=FusedMoEQuantDesc("int4", group_shape, w1_scale, None, w1_zp), _w2=FusedMoEQuantDesc("int4", group_shape, w2_scale, None, w2_zp), ) def fp8_w8a16_moe_quant_config( w1_scale: torch.Tensor, w2_scale: torch.Tensor, block_shape: list[int] | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for 16-bit float activations and fp8 weights. """ group_shape = GroupShape(*block_shape) if block_shape is not None else None return FusedMoEQuantConfig( _a1=FusedMoEQuantDesc(), _a2=FusedMoEQuantDesc(), _w1=FusedMoEQuantDesc( current_platform.fp8_dtype(), group_shape, w1_scale, None, None ), _w2=FusedMoEQuantDesc( current_platform.fp8_dtype(), group_shape, w2_scale, None, None ), ) def int8_w8a16_moe_quant_config( w1_scale: torch.Tensor, w2_scale: torch.Tensor, w1_zp: torch.Tensor | None, w2_zp: torch.Tensor | None, block_shape: list[int] | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for 16-bit float activations and int8 weights. """ group_shape = GroupShape(*block_shape) if block_shape is not None else None return FusedMoEQuantConfig( _a1=FusedMoEQuantDesc(shape=group_shape), _a2=FusedMoEQuantDesc(shape=group_shape), _w1=FusedMoEQuantDesc(torch.int8, group_shape, w1_scale, None, w1_zp), _w2=FusedMoEQuantDesc(torch.int8, group_shape, w2_scale, None, w2_zp), ) def int4_w4afp8_moe_quant_config( w1_scale: torch.Tensor, w2_scale: torch.Tensor, g1_alphas: torch.Tensor, g2_alphas: torch.Tensor, per_act_token_quant: bool = False, per_out_ch_quant: bool = False, block_shape: list[int] | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for fp8 activations and int4 weights. """ return FusedMoEQuantConfig.make( torch.float8_e4m3fn, # quant dtype for activations w1_scale=w1_scale, w2_scale=w2_scale, g1_alphas=g1_alphas, g2_alphas=g2_alphas, per_act_token_quant=per_act_token_quant, per_out_ch_quant=per_out_ch_quant, block_shape=block_shape, weight_dtype="int4", # weight dtype for weights ) def awq_marlin_moe_quant_config( w1_scale: torch.Tensor, w2_scale: torch.Tensor, w1_zp: torch.Tensor | None, w2_zp: torch.Tensor | None, weight_bits: int, group_size: int, w1_bias: torch.Tensor | None = None, w2_bias: torch.Tensor | None = None, ) -> FusedMoEQuantConfig: """ Construct a quant config for awq marlin quantization. """ from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape w_shape = None if group_size == -1 else GroupShape(row=1, col=group_size) # Activations are NOT quantized for AWQ (fp16/bf16) a_shape = w_shape # Same as weight shape for alignment # Determine weight dtype if weight_bits == 4: weight_dtype = "int4" elif weight_bits == 8: weight_dtype = torch.int8 else: raise ValueError(f"Unsupported weight_bits: {weight_bits}") return FusedMoEQuantConfig( _a1=FusedMoEQuantDesc(dtype=None, shape=a_shape), _a2=FusedMoEQuantDesc(dtype=None, shape=a_shape), _w1=FusedMoEQuantDesc(weight_dtype, w_shape, w1_scale, None, w1_zp, w1_bias), _w2=FusedMoEQuantDesc(weight_dtype, w_shape, w2_scale, None, w2_zp, w2_bias), ) def biased_moe_quant_config( w1_bias: torch.Tensor | None, w2_bias: torch.Tensor | None, ) -> FusedMoEQuantConfig: """ Construct a quant config for unquantized activations with biases. """ return FusedMoEQuantConfig( _a1=FusedMoEQuantDesc(), _a2=FusedMoEQuantDesc(), _w1=FusedMoEQuantDesc(bias=w1_bias), _w2=FusedMoEQuantDesc(bias=w2_bias), ) # A FusedMoEQuantConfig constant for an unquantized MoE op. FUSED_MOE_UNQUANTIZED_CONFIG: FusedMoEQuantConfig = FusedMoEQuantConfig.make() @dataclass class FusedMoEParallelConfig: tp_size: int pcp_size: int dp_size: int ep_size: int tp_rank: int pcp_rank: int dp_rank: int ep_rank: int sp_size: int use_ep: bool # whether to use EP or not all2all_backend: str # all2all backend for MoE communication enable_eplb: bool # whether to enable expert load balancing @property def is_sequence_parallel(self) -> bool: return self.sp_size > 1 @property def use_all2all_kernels(self): return self.dp_size > 1 and self.use_ep @property def use_deepep_ht_kernels(self): return ( self.use_all2all_kernels and self.all2all_backend == "deepep_high_throughput" ) @property def use_deepep_ll_kernels(self): return self.use_all2all_kernels and self.all2all_backend == "deepep_low_latency" @property def use_fi_all2allv_kernels(self): return ( self.use_all2all_kernels and self.all2all_backend == "flashinfer_all2allv" ) @property def use_batched_activation_format(self): return self.use_deepep_ll_kernels @property def use_naive_all2all_kernels(self): return self.use_all2all_kernels and ( self.all2all_backend in ["naive", "allgather_reducescatter"] ) @property def use_mori_kernels(self): return self.use_all2all_kernels and self.all2all_backend == "mori" @staticmethod def flatten_tp_across_dp_and_pcp( tp_size: int, dp_size: int, dp_rank: int, pcp_size: int, pcp_rank: int ) -> tuple[int, int]: tp_rank = 0 if tp_size == 1 else get_tensor_model_parallel_rank() # There are actually dp_size * pcp_size * tp_size devices. # Update tp_size and tp_rank so we shard across all devices. flatten_tp_size = dp_size * pcp_size * tp_size flatten_tp_rank = dp_rank * pcp_size * tp_size + pcp_rank * tp_size + tp_rank return flatten_tp_size, flatten_tp_rank @staticmethod def make( tp_size_: int, pcp_size_: int, dp_size_: int, sp_size_: int, vllm_parallel_config: ParallelConfig, ) -> "FusedMoEParallelConfig": """ Determine MoE parallel configuration. Based on the input `tp_size_`, `dp_size_` and vllm's parallel config, determine what level's of parallelism to use in the fused moe layer. Args: tp_size_ (int): `tp_size` passed into the FusedMoE constructor. pcp_size_ (int): `pcp_size` passed into the FusedMoE constructor. dp_size_ (int): `dp_size` passed into the FusedMoE constructor. vllm_parallel_config (ParallelConfig): vLLM's parallel config object which contains the `enable_expert_parallel` flag. Examples: When there is no parallelism requested, i.e. `tp_size_` = `pcp_size_` = `dp_size_` = 1, we simply return the sizes unaltered and the ranks set to 0. Expert Parallelism is considered only when either `dp_size_`, `pcp_size_` or `tp_size_` is non trivial. Note that PCP serves the same function as DP here. When TP = 2, DP(PCP) = 1 and EP = False, the configuration on different devices: - device 0 : TP = {2, 0} DP = {1, 0} EP = {1, 0} // legend : {size, rank} - device 1 : TP = {2, 1} DP = {1, 0} EP = {1, 0} - Comment : Tensors are sharded across 2 devices. When TP = 1, DP(PCP) = 2 and EP = False, the configuration on different devices: - device 0 : TP = {2, 0} DP = {2, 0} EP = {1, 0} - device 1 : TP = {2, 1} DP = {2, 1} EP = {1, 0} - Comment: There are 2 engine instances and the tensors are sharded across 2 decvices. When TP = 2, DP(PCP) = 2 and EP = False, the configuration on different devices: - device 0: TP = {4, 0} DP = {2, 0} EP = {1, 0} - device 1: TP = {4, 1} DP = {2, 0} EP = {1, 0} - device 2: TP = {4, 2} DP = {2, 1} EP = {1, 0} - device 3: TP = {4, 3} DP = {2, 1} EP = {1, 0} - Comment: There are 2 engine instances and the tensors are sharded across 4 devices. When, TP = 2, DP(PCP) = 1 and EP = True, the configuration on different devices: - device 0: TP = {1, 0} DP = {1, 0} EP = {2, 0} - device 1: TP = {1, 0} DP = {1, 0} EP = {2, 1} - Comment: The experts are split between the 2 devices. When, TP = 1, DP(PCP) = 2 and EP = True, the configuration on different devices: - device 0: TP = {1, 0} DP = {2, 0} EP = {2, 0} - device 1: TP = {1, 0} DP = {2, 1} EP = {2, 1} - Comment: There are 2 engine instances and the experts are split between the 2 devices. When TP = 2, DP(PCP) = 2 and EP = True, the configuration on different devices: - device 0: TP = {1, 0} DP = {2, 0} EP = {4, 0} - device 1: TP = {1, 0} DP = {2, 0} EP = {4, 1} - device 2: TP = {1, 0} DP = {2, 1} EP = {4, 2} - device 3: TP = {1, 0} DP = {2, 1} EP = {4, 3} - Comment: There are 2 engine instances and the experts are split between the 4 devices. """ use_ep = ( dp_size_ * pcp_size_ * tp_size_ > 1 and vllm_parallel_config.enable_expert_parallel ) dp_size = dp_size_ dp_rank = get_dp_group().rank_in_group if dp_size > 1 else 0 pcp_size = pcp_size_ pcp_rank = get_pcp_group().rank_in_group if pcp_size > 1 else 0 tp_size, tp_rank = FusedMoEParallelConfig.flatten_tp_across_dp_and_pcp( tp_size_, dp_size_, dp_rank, pcp_size_, pcp_rank ) if not use_ep: return FusedMoEParallelConfig( tp_size=tp_size, tp_rank=tp_rank, pcp_size=pcp_size, pcp_rank=pcp_rank, dp_size=dp_size, dp_rank=dp_rank, ep_size=1, ep_rank=0, sp_size=sp_size_, use_ep=False, all2all_backend=vllm_parallel_config.all2all_backend, enable_eplb=vllm_parallel_config.enable_eplb, ) # DP + EP / TP + EP / DP + TP + EP assert use_ep # In EP, each device owns a set of experts fully. There is no tensor # parallel update tp_size, tp_rank, ep_size and ep_rank to reflect that. ep_size = tp_size ep_rank = tp_rank return FusedMoEParallelConfig( tp_size=1, tp_rank=0, pcp_size=pcp_size, pcp_rank=pcp_rank, dp_size=dp_size, dp_rank=dp_rank, ep_size=ep_size, ep_rank=ep_rank, sp_size=sp_size_, use_ep=True, all2all_backend=vllm_parallel_config.all2all_backend, enable_eplb=vllm_parallel_config.enable_eplb, ) @classmethod def make_no_parallel(cls) -> "FusedMoEParallelConfig": """For usage in CI/CD and testing.""" return FusedMoEParallelConfig( tp_size=1, tp_rank=0, pcp_size=1, pcp_rank=0, dp_size=1, dp_rank=0, ep_size=1, ep_rank=0, sp_size=1, use_ep=False, all2all_backend="naive", enable_eplb=False, ) # Adapted from pplx-kernels tests/all_to_all_utils.py @dataclass class FusedMoEConfig: num_experts: int experts_per_token: int hidden_dim: int intermediate_size_per_partition: int num_local_experts: int num_logical_experts: int activation: MoEActivation device: torch.device | str routing_method: RoutingMethodType moe_parallel_config: FusedMoEParallelConfig # The activation type. in_dtype: torch.dtype # Defaults to in_dtype if not specified. router_logits_dtype: torch.dtype | None = None moe_backend: str = "auto" max_num_tokens: int = envs.VLLM_MOE_DP_CHUNK_SIZE has_bias: bool = False is_act_and_mul: bool = True is_lora_enabled: bool = False # This flag is used to disable the inplace optimization # in MoE kernels. If this flag is True then the kernel # should not be using inplace. If the flag is false, the # kernel is free to use inplace or not. disable_inplace: bool = True def __post_init__(self): if self.dp_size > 1: logger.debug_once( "Using FusedMoEConfig::max_num_tokens=%d", self.max_num_tokens ) assert self.max_num_tokens > 0 if self.router_logits_dtype is None: self.router_logits_dtype = self.in_dtype @property def tp_size(self): return self.moe_parallel_config.tp_size @property def dp_size(self): return self.moe_parallel_config.dp_size @property def pcp_size(self): return self.moe_parallel_config.pcp_size @property def ep_size(self): return self.moe_parallel_config.ep_size @property def sp_size(self): return self.moe_parallel_config.sp_size @property def is_sequence_parallel(self): return self.moe_parallel_config.is_sequence_parallel @property def tp_rank(self): return self.moe_parallel_config.tp_rank @property def dp_rank(self): return self.moe_parallel_config.dp_rank @property def pcp_rank(self): return self.moe_parallel_config.pcp_rank @property def ep_rank(self): return self.moe_parallel_config.ep_rank @property def use_ep(self): return self.moe_parallel_config.use_ep @property def use_deepep_ht_kernels(self): return self.moe_parallel_config.use_deepep_ht_kernels @property def use_deepep_ll_kernels(self): return self.moe_parallel_config.use_deepep_ll_kernels @property def use_mori_kernels(self): return self.moe_parallel_config.use_mori_kernels @property def use_fi_all2allv_kernels(self): return self.moe_parallel_config.use_fi_all2allv_kernels @property def use_naive_all2all_kernels(self): return self.moe_parallel_config.use_naive_all2all_kernels
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/fused_moe/config.py", "license": "Apache License 2.0", "lines": 1062, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/ernie45.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Copyright 2025 The Baidu team. # Copyright 2023 The vLLM team. # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. # # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX # and OPT implementations in this library. It has been modified from its # original forms to accommodate minor architectural differences compared # to GPT-NeoX and OPT used by the Meta AI team that trained the model. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Inference-only Erine model compatible with HuggingFace weights.""" from vllm.compilation.decorators import support_torch_compile from vllm.config import VllmConfig from vllm.model_executor.models.llama import LlamaForCausalLM from .utils import PPMissingLayer @support_torch_compile( # set dynamic_arg_dims to support mrope dynamic_arg_dims={ "input_ids": 0, "positions": -1, "intermediate_tensors": 0, "inputs_embeds": 0, } ) class Ernie4_5ForCausalLM(LlamaForCausalLM): def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__(vllm_config=vllm_config, prefix=prefix) # Hack Llama model to fit HF format Ernie4.5 dense implementation # Attention difference between Ernie and Llama: # 1. rotary_dim and no Neox style. # 2. There is no bias for o_proj in attention for layer in self.model.layers: if not isinstance(layer, PPMissingLayer): layer.self_attn.rotary_emb.is_neox_style = False layer.self_attn.o_proj.bias = None layer.self_attn.o_proj.skip_bias_add = True
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/ernie45.py", "license": "Apache License 2.0", "lines": 48, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/ernie45_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Copyright 2025 The Baidu team. # Copyright 2023 The vLLM team. # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. # # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX # and OPT implementations in this library. It has been modified from its # original forms to accommodate minor architectural differences compared # to GPT-NeoX and OPT used by the Meta AI team that trained the model. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Inference-only ErineMoE model compatible with HuggingFace weights.""" import typing from collections.abc import Callable, Iterable from itertools import islice from typing import Any import torch from torch import nn from transformers import PretrainedConfig from vllm.compilation.decorators import support_torch_compile from vllm.config import CacheConfig, VllmConfig, get_current_vllm_config from vllm.distributed import ( get_ep_group, get_pp_group, get_tensor_model_parallel_world_size, ) from vllm.logger import init_logger from vllm.model_executor.layers.activation import SiluAndMul from vllm.model_executor.layers.attention import Attention from vllm.model_executor.layers.fused_moe import SharedFusedMoE from vllm.model_executor.layers.layernorm import RMSNorm from vllm.model_executor.layers.linear import ( MergedColumnParallelLinear, QKVParallelLinear, ReplicatedLinear, RowParallelLinear, ) from vllm.model_executor.layers.logits_processor import LogitsProcessor from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.rotary_embedding import get_rope from vllm.model_executor.layers.vocab_parallel_embedding import ( ParallelLMHead, VocabParallelEmbedding, ) from vllm.model_executor.model_loader.weight_utils import ( default_weight_loader, maybe_remap_kv_scale_name, ) from vllm.sequence import IntermediateTensors from vllm.transformers_utils.config import set_default_rope_theta from .interfaces import MixtureOfExperts, SupportsLoRA, SupportsPP from .utils import ( AutoWeightsLoader, PPMissingLayer, extract_layer_index, is_pp_missing_parameter, make_empty_intermediate_tensors_factory, make_layers, maybe_prefix, ) logger = init_logger(__name__) class Ernie4_5_MoeMLP(nn.Module): def __init__( self, hidden_size: int, intermediate_size: int, hidden_act: str, use_bias: bool = False, quant_config: QuantizationConfig | None = None, reduce_results: bool = True, prefix: str = "", ) -> None: super().__init__() self.gate_up_proj = MergedColumnParallelLinear( hidden_size, [intermediate_size] * 2, bias=use_bias, quant_config=quant_config, prefix=f"{prefix}.gate_up_proj", ) self.down_proj = RowParallelLinear( intermediate_size, hidden_size, bias=use_bias, quant_config=quant_config, reduce_results=reduce_results, prefix=f"{prefix}.down_proj", ) if hidden_act != "silu": raise ValueError( f"Unsupported activation: {hidden_act}. Only silu is supported for now." ) self.act_fn = SiluAndMul() def forward(self, x): gate_up, _ = self.gate_up_proj(x) x = self.act_fn(gate_up) x, _ = self.down_proj(x) return x class Ernie4_5_MoeMoE(nn.Module): def __init__( self, config: PretrainedConfig, quant_config: QuantizationConfig | None = None, prefix: str = "", enable_eplb: bool = False, ): super().__init__() layer_idx = extract_layer_index(prefix) self.layer_idx = layer_idx self.tp_size = get_tensor_model_parallel_world_size() self.moe_num_shared_experts = getattr(config, "moe_num_shared_experts", None) self.ep_group = get_ep_group().device_group self.ep_rank = get_ep_group().rank_in_group self.ep_size = self.ep_group.size() self.n_routed_experts: int = config.moe_num_experts self.n_shared_experts: int = self.moe_num_shared_experts # Load balancing settings. vllm_config = get_current_vllm_config() eplb_config = vllm_config.parallel_config.eplb_config self.enable_eplb = enable_eplb self.n_redundant_experts = eplb_config.num_redundant_experts self.n_logical_experts = self.n_routed_experts self.n_physical_experts = self.n_logical_experts + self.n_redundant_experts self.n_local_physical_experts = self.n_physical_experts // self.ep_size self.physical_expert_start = self.ep_rank * self.n_local_physical_experts self.physical_expert_end = ( self.physical_expert_start + self.n_local_physical_experts ) self.has_shared_experts = getattr(config, "moe_num_shared_experts", 0) > 0 if self.tp_size > config.moe_num_experts: raise ValueError( f"Tensor parallel size {self.tp_size} is greater than " f"the number of experts {config.moe_num_experts}." ) self.gate = ReplicatedLinear( config.hidden_size, config.moe_num_experts, bias=False, params_dtype=torch.float32, quant_config=None, prefix=f"{prefix}.gate", ) self.gate.e_score_correction_bias = nn.Parameter( torch.empty(config.moe_num_experts, dtype=torch.float32) ) if self.has_shared_experts: intermediate_size = ( config.moe_intermediate_size * config.moe_num_shared_experts ) self.shared_experts = Ernie4_5_MoeMLP( hidden_size=config.hidden_size, intermediate_size=intermediate_size, hidden_act=config.hidden_act, quant_config=quant_config, prefix=f"{prefix}.shared_experts", reduce_results=False, ) else: self.shared_experts = None self.experts = SharedFusedMoE( shared_experts=self.shared_experts, num_experts=config.moe_num_experts, top_k=config.moe_k, hidden_size=config.hidden_size, intermediate_size=config.moe_intermediate_size, reduce_results=False, renormalize=True, quant_config=quant_config, prefix=f"{prefix}.experts", e_score_correction_bias=self.gate.e_score_correction_bias, enable_eplb=self.enable_eplb, num_redundant_experts=self.n_redundant_experts, router_logits_dtype=torch.float32, ) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: orig_shape = hidden_states.shape hidden_dim = hidden_states.shape[-1] hidden_states = hidden_states.view(-1, hidden_dim) router_logits, _ = self.gate(hidden_states.to(dtype=torch.float32)) final_hidden_states = self.experts( hidden_states=hidden_states, router_logits=router_logits ) if self.has_shared_experts: final_hidden_states = final_hidden_states[0] + final_hidden_states[1] else: final_hidden_states = final_hidden_states[1] if self.tp_size > 1: final_hidden_states = self.experts.maybe_all_reduce_tensor_model_parallel( final_hidden_states ) return final_hidden_states.view(orig_shape) class Ernie4_5_MoeAttention(nn.Module): def __init__( self, hidden_size: int, num_heads: int, num_kv_heads: int, rope_parameters: dict[str, Any], head_dim: int | None = None, max_position_embeddings: int = 131072, rms_norm_eps: float = 1e-05, qkv_bias: bool = False, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> None: super().__init__() layer_idx = extract_layer_index(prefix) if len(prefix) > 0 else 0 self.layer_idx = layer_idx self.hidden_size = hidden_size tp_size = get_tensor_model_parallel_world_size() self.total_num_heads = num_heads assert self.total_num_heads % tp_size == 0 self.num_heads = self.total_num_heads // tp_size self.total_num_kv_heads = num_kv_heads if self.total_num_kv_heads >= tp_size: # Number of KV heads is greater than TP size, so we partition # the KV heads across multiple tensor parallel GPUs. assert self.total_num_kv_heads % tp_size == 0 else: # Number of KV heads is less than TP size, so we replicate # the KV heads across multiple tensor parallel GPUs. assert tp_size % self.total_num_kv_heads == 0 self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size) self.head_dim = head_dim or (hidden_size // self.total_num_heads) self.q_size = self.num_heads * self.head_dim self.kv_size = self.num_kv_heads * self.head_dim self.scaling = self.head_dim**-0.5 self.max_position_embeddings = max_position_embeddings self.qkv_proj = QKVParallelLinear( hidden_size, self.head_dim, self.total_num_heads, self.total_num_kv_heads, bias=qkv_bias, quant_config=quant_config, prefix=f"{prefix}.qkv_proj", ) self.o_proj = RowParallelLinear( self.total_num_heads * self.head_dim, hidden_size, bias=False, quant_config=quant_config, prefix=f"{prefix}.o_proj", ) self.rotary_emb = get_rope( self.head_dim, max_position=max_position_embeddings, rope_parameters=rope_parameters, is_neox_style=False, ) self.attn = Attention( self.num_heads, self.head_dim, self.scaling, num_kv_heads=self.num_kv_heads, cache_config=cache_config, quant_config=quant_config, prefix=f"{prefix}.attn", ) def forward( self, positions: torch.Tensor, hidden_states: torch.Tensor, ) -> torch.Tensor: qkv, _ = self.qkv_proj(hidden_states) q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1) q, k = self.rotary_emb(positions, q, k) # Attention attn_output = self.attn(q, k, v) # Output projection output, _ = self.o_proj(attn_output) return output class Ernie4_5_MoeDecoderLayer(nn.Module): def __init__( self, config: PretrainedConfig, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, prefix: str = "", enable_eplb: bool = False, ) -> None: super().__init__() self.hidden_size = config.hidden_size set_default_rope_theta(config, default_theta=500000) max_position_embeddings = getattr(config, "max_position_embeddings", 131072) self.self_attn = Ernie4_5_MoeAttention( hidden_size=self.hidden_size, num_heads=config.num_attention_heads, num_kv_heads=config.num_key_value_heads, head_dim=getattr(config, "head_dim", None), rope_parameters=config.rope_parameters, max_position_embeddings=max_position_embeddings, rms_norm_eps=config.rms_norm_eps, qkv_bias=getattr(config, "use_bias", False), cache_config=cache_config, quant_config=quant_config, prefix=f"{prefix}.self_attn", ) layer_idx = extract_layer_index(prefix) self.layer_idx = layer_idx # MoE moe_num_experts = getattr(config, "moe_num_experts", 0) moe_layer_start_index = getattr(config, "moe_layer_start_index", 0) moe_layer_end_index = getattr( config, "moe_layer_end_index", config.num_hidden_layers - 1 ) moe_layer_interval = getattr(config, "moe_layer_interval", 1) use_moe = getattr(config, "use_moe", moe_num_experts > 0) if ( use_moe and ((layer_idx + 1) % moe_layer_interval == 0) and layer_idx >= moe_layer_start_index and layer_idx <= moe_layer_end_index ): self.mlp = Ernie4_5_MoeMoE( config=config, quant_config=quant_config, prefix=f"{prefix}.mlp", enable_eplb=enable_eplb, ) else: self.mlp = Ernie4_5_MoeMLP( hidden_size=config.hidden_size, intermediate_size=config.intermediate_size, hidden_act=config.hidden_act, use_bias=getattr(config, "use_bias", False), quant_config=quant_config, prefix=f"{prefix}.mlp", ) self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.post_attention_layernorm = RMSNorm( config.hidden_size, eps=config.rms_norm_eps ) def forward( self, positions: torch.Tensor, hidden_states: torch.Tensor, residual: torch.Tensor | None, ) -> torch.Tensor: # Self Attention if residual is None: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) else: hidden_states, residual = self.input_layernorm(hidden_states, residual) hidden_states = self.self_attn( positions=positions, hidden_states=hidden_states, ) # Fully Connected hidden_states, residual = self.post_attention_layernorm(hidden_states, residual) hidden_states = self.mlp(hidden_states) return hidden_states, residual @support_torch_compile class Ernie4_5_MoeModel(nn.Module): def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__() config = vllm_config.model_config.hf_config cache_config = vllm_config.cache_config quant_config = vllm_config.quant_config self.vocab_size = config.vocab_size self.config = config parallel_config = vllm_config.parallel_config eplb_config = parallel_config.eplb_config enable_eplb = parallel_config.enable_eplb self.num_redundant_experts = eplb_config.num_redundant_experts if get_pp_group().is_first_rank: self.embed_tokens = VocabParallelEmbedding( config.vocab_size, config.hidden_size, quant_config=quant_config, prefix=f"{prefix}.embed_tokens", ) else: self.embed_tokens = PPMissingLayer() self.start_layer, self.end_layer, self.layers = make_layers( config.num_hidden_layers, lambda prefix: Ernie4_5_MoeDecoderLayer( config=config, cache_config=cache_config, quant_config=quant_config, prefix=prefix, enable_eplb=enable_eplb, ), prefix=f"{prefix}.layers", ) if get_pp_group().is_last_rank: self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) else: self.norm = PPMissingLayer() self.make_empty_intermediate_tensors = make_empty_intermediate_tensors_factory( ["hidden_states", "residual"], config.hidden_size ) def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.embed_tokens(input_ids) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, ) -> torch.Tensor | IntermediateTensors: if get_pp_group().is_first_rank: if inputs_embeds is not None: hidden_states = inputs_embeds else: hidden_states = self.embed_input_ids(input_ids) residual = None else: assert intermediate_tensors is not None hidden_states = intermediate_tensors["hidden_states"] residual = intermediate_tensors["residual"] for layer in islice(self.layers, self.start_layer, self.end_layer): hidden_states, residual = layer(positions, hidden_states, residual) if not get_pp_group().is_last_rank: return IntermediateTensors( {"hidden_states": hidden_states, "residual": residual} ) hidden_states, _ = self.norm(hidden_states, residual) return hidden_states def get_expert_mapping(self) -> list[tuple[str, str, int, str]]: # Params for weights, fp8 weight scales, fp8 activation scales # (param_name, weight_name, expert_id, shard_id) return SharedFusedMoE.make_expert_params_mapping( self, ckpt_gate_proj_name="gate_proj", ckpt_down_proj_name="down_proj", ckpt_up_proj_name="up_proj", num_experts=self.config.moe_num_experts, num_redundant_experts=self.num_redundant_experts, ) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: stacked_params_mapping = [ # (param_name, shard_name, shard_id) ("qkv_proj", "q_proj", "q"), ("qkv_proj", "k_proj", "k"), ("qkv_proj", "v_proj", "v"), ("gate_up_proj", "gate_proj", 0), ("gate_up_proj", "up_proj", 1), ] params_dict = dict(self.named_parameters()) loaded_params: set[str] = set() expert_params_mapping = self.get_expert_mapping() for name, loaded_weight in weights: if self.config.tie_word_embeddings and name.endswith("lm_head.weight"): continue # MTP will be supported soon. if "mtp" in name: continue if "e_score_correction_bias" in name: name = name.replace("moe_statics", "gate") loaded_weight = loaded_weight.squeeze(0) for param_name, weight_name, shard_id in stacked_params_mapping: # Skip non-stacked layers and experts (experts handled below). if weight_name not in name: continue if ("mlp.experts." in name) and name not in params_dict: continue name = name.replace(weight_name, param_name) # Skip loading extra bias for GPTQ models. if ( name.endswith(".bias") or name.endswith("_bias") ) and name not in params_dict: continue # Skip layers on other devices. if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = param.weight_loader weight_loader(param, loaded_weight, shard_id) break else: is_expert_weight = False for mapping in expert_params_mapping: param_name, weight_name, expert_id, shard_id = mapping if weight_name not in name: continue # Anyway, this is an expert weight and should not be # attempted to load as other weights later is_expert_weight = True # Do not modify `name` since the loop may continue here # Instead, create a new variable name_mapped = name.replace(weight_name, param_name) # Skip layers on other devices. if is_pp_missing_parameter(name_mapped, self): continue # Skip loading extra bias for GPTQ models. if ( name_mapped.endswith(".bias") or name_mapped.endswith("_bias") ) and name_mapped not in params_dict: continue param = params_dict[name_mapped] # We should ask the weight loader to return success or not # here since otherwise we may skip experts with other # available replicas. weight_loader = typing.cast( Callable[..., bool], param.weight_loader ) success = weight_loader( param, loaded_weight, name_mapped, shard_id=shard_id, expert_id=expert_id, return_success=True, ) if success: name = name_mapped break else: if is_expert_weight: # We've checked that this is an expert weight # However it's not mapped locally to this rank # So we simply skip it continue # Skip loading extra bias for GPTQ models. if ( name.endswith(".bias") or name.endswith("_bias") ) and name not in params_dict: continue # Skip layers on other devices. if is_pp_missing_parameter(name, self): continue # Remapping the name of FP8 kv-scale. name = maybe_remap_kv_scale_name(name, params_dict) if name is None: continue param = params_dict[name] weight_loader = getattr( param, "weight_loader", default_weight_loader ) weight_loader(param, loaded_weight) loaded_params.add(name) return loaded_params class Ernie4_5_MoeForCausalLM(nn.Module, SupportsPP, SupportsLoRA, MixtureOfExperts): packed_modules_mapping = { "qkv_proj": [ "q_proj", "k_proj", "v_proj", ], "gate_up_proj": [ "gate_proj", "up_proj", ], } fall_back_to_pt_during_load = False def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__() config = vllm_config.model_config.hf_config quant_config = vllm_config.quant_config self.config = config self.quant_config = quant_config self.model = Ernie4_5_MoeModel( vllm_config=vllm_config, prefix=maybe_prefix(prefix, "model") ) if get_pp_group().is_last_rank: self.lm_head = ParallelLMHead( config.vocab_size, config.hidden_size, quant_config=quant_config, prefix=maybe_prefix(prefix, "lm_head"), ) else: self.lm_head = PPMissingLayer() if self.config.tie_word_embeddings: self.lm_head.weight = self.model.embed_tokens.weight self.logits_processor = LogitsProcessor(config.vocab_size) self.make_empty_intermediate_tensors = ( self.model.make_empty_intermediate_tensors ) self.expert_weights = [] # Set MoE hyperparameters moe_layers_indices = [ i for i in range(config.num_hidden_layers) if ( i >= config.moe_layer_start_index and i <= config.moe_layer_end_index and (i + 1) % config.moe_layer_interval == 0 ) ] self.num_moe_layers = len(moe_layers_indices) self.num_expert_groups = 1 self.moe_layers: list[SharedFusedMoE] = [] example_moe = None for layer in self.model.layers: if isinstance(layer, PPMissingLayer): continue assert isinstance(layer, Ernie4_5_MoeDecoderLayer) if isinstance(layer.mlp, Ernie4_5_MoeMoE): example_moe = layer.mlp self.moe_layers.append(layer.mlp.experts) if example_moe is None: logger.warning("No Ernie4_5_MoeMoE layer found in model.layers.") self.num_logical_experts = 0 self.num_physical_experts = 0 self.num_local_physical_experts = 0 self.num_routed_experts = 0 self.num_shared_experts = 0 self.num_redundant_experts = 0 else: self.num_logical_experts = example_moe.n_logical_experts self.num_physical_experts = example_moe.n_physical_experts self.num_local_physical_experts = example_moe.n_local_physical_experts self.num_routed_experts = example_moe.n_routed_experts self.num_shared_experts = example_moe.n_shared_experts self.num_redundant_experts = example_moe.n_redundant_experts def update_physical_experts_metadata( self, num_physical_experts: int, num_local_physical_experts: int, ) -> None: assert self.num_local_physical_experts == num_local_physical_experts self.num_physical_experts = num_physical_experts self.num_local_physical_experts = num_local_physical_experts self.num_redundant_experts = num_physical_experts - self.num_logical_experts for layer in self.model.layers: if isinstance(layer.mlp, Ernie4_5_MoeMoE): moe = layer.mlp moe.n_local_physical_experts = num_local_physical_experts moe.n_physical_experts = num_physical_experts moe.n_redundant_experts = self.num_redundant_experts moe.experts.update_expert_map() def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.model.embed_input_ids(input_ids) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, ) -> torch.Tensor | IntermediateTensors: hidden_states = self.model( input_ids, positions, intermediate_tensors, inputs_embeds ) return hidden_states def compute_logits( self, hidden_states: torch.Tensor, ) -> torch.Tensor | None: logits = self.logits_processor(self.lm_head, hidden_states) return logits def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: loader = AutoWeightsLoader( self, skip_prefixes=(["lm_head."] if self.config.tie_word_embeddings else None), ) return loader.load_weights(weights) def get_expert_mapping(self) -> list[tuple[str, str, int, str]]: return self.model.get_expert_mapping()
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/ernie45_moe.py", "license": "Apache License 2.0", "lines": 663, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/keye.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import math from abc import abstractmethod from collections.abc import Iterable, Mapping, Sequence from functools import partial from typing import Annotated, Any, Literal, TypeAlias, TypeVar import numpy as np import torch import torch.nn as nn from einops import rearrange from transformers import BaseImageProcessor, PretrainedConfig from transformers.activations import GELUActivation from transformers.feature_extraction_utils import BatchFeature from transformers.modeling_outputs import BaseModelOutput, BaseModelOutputWithPooling from transformers.utils import torch_int from vllm.config import VllmConfig from vllm.config.multimodal import BaseDummyOptions from vllm.distributed import get_tensor_model_parallel_world_size from vllm.logger import init_logger from vllm.model_executor.layers.attention import ( MMEncoderAttention, ) from vllm.model_executor.layers.conv import Conv2dLayer from vllm.model_executor.layers.linear import ( ColumnParallelLinear, QKVParallelLinear, RowParallelLinear, ) from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.rotary_embedding.common import ( ApplyRotaryEmb, ) from vllm.model_executor.model_loader.weight_utils import ( default_weight_loader, maybe_remap_kv_scale_name, ) from vllm.model_executor.models.module_mapping import MultiModelKeys from vllm.multimodal import MULTIMODAL_REGISTRY from vllm.multimodal.inputs import ( ImageItem, ModalityData, MultiModalDataDict, MultiModalFeatureSpec, MultiModalFieldConfig, MultiModalKwargsItems, VideoItem, ) from vllm.multimodal.parse import ( DictEmbeddingItems, ImageSize, ModalityDataItems, MultiModalDataItems, MultiModalDataParser, ) from vllm.multimodal.processing import ( BaseDummyInputsBuilder, BaseMultiModalProcessor, BaseProcessingInfo, PromptReplacement, PromptUpdate, ) from vllm.sequence import IntermediateTensors from vllm.utils.tensor_schema import TensorSchema, TensorShape from .interfaces import ( MultiModalEmbeddings, SupportsLoRA, SupportsMRoPE, SupportsMultiModal, SupportsPP, ) from .siglip import SiglipMLP from .utils import ( AutoWeightsLoader, WeightsMapper, init_vllm_registered_model, is_pp_missing_parameter, maybe_prefix, ) from .vision import is_vit_use_data_parallel logger = init_logger(__name__) def smart_resize( height: int, width: int, factor: int, min_pixels: int, max_pixels: int, ): if height < factor: logger.warning( "smart_resize: height=%s < factor=%s, reset height=factor", height, factor, ) width = round((width * factor) / height) height = factor if width < factor: logger.warning( "smart_resize: width=%s < factor=%s, reset width=factor", width, factor, ) height = round((height * factor) / width) width = factor if max(height, width) / min(height, width) > 200: raise ValueError( "absolute aspect ratio must be smaller than 200, got " "{max(height, width) / min(height, width)}" ) h_bar = round(height / factor) * factor w_bar = round(width / factor) * factor if h_bar * w_bar > max_pixels: beta = math.sqrt((height * width) / max_pixels) h_bar = math.floor(height / beta / factor) * factor w_bar = math.floor(width / beta / factor) * factor elif h_bar * w_bar < min_pixels: beta = math.sqrt(min_pixels / (height * width)) h_bar = math.ceil(height * beta / factor) * factor w_bar = math.ceil(width * beta / factor) * factor return h_bar, w_bar class KeyeImagePixelInputs(TensorSchema): """ Dimensions: - bnp: Batch size * Number of patches - c: Number of channels - ps: Patch size - ni: Number of images - g: Grid dimensions (3 for t, h, w) """ type: Literal["pixel_values"] pixel_values: Annotated[ torch.Tensor, TensorShape("bnp", 3, "ps", "ps", dynamic_dims={"bnp"}) ] image_grid_thw: Annotated[torch.Tensor, TensorShape("ni", 3)] class KeyeImageEmbeddingInputs(TensorSchema): """ Dimensions: - nf: Number of image features - hs: Hidden size (must match the hidden size of language model backbone) - ni: Number of images - g: Grid dimensions (3 for t, h, w) """ type: Literal["image_embeds"] image_embeds: Annotated[torch.Tensor, TensorShape("nf", "hs")] image_grid_thw: Annotated[torch.Tensor, TensorShape("ni", 3)] KeyeImageInputs: TypeAlias = KeyeImagePixelInputs | KeyeImageEmbeddingInputs class KeyeVideoPixelInputs(TensorSchema): """ Dimensions: - bnp: Batch size * Number of patches - c: Number of channels - ps: Patch size - ni: Number of images - g: Grid dimensions (3 for t, h, w) """ type: Literal["pixel_values_videos"] pixel_values_videos: Annotated[ torch.Tensor, TensorShape("bnp", 3, "ps", "ps", dynamic_dims={"bnp"}) ] video_grid_thw: Annotated[torch.Tensor, TensorShape("nv", 3)] class KeyeVideoEmbeddingInputs(TensorSchema): """ Dimensions: - nf: Number of video features - hs: Hidden size (must match the hidden size of language model backbone) - nv: Number of videos - g: Grid dimensions (3 for t, h, w) """ type: Literal["video_embeds"] video_embeds: Annotated[torch.Tensor, TensorShape("nf", "hs")] video_grid_thw: Annotated[torch.Tensor, TensorShape("nv", 3)] KeyeVideoInputs: TypeAlias = KeyeVideoPixelInputs | KeyeVideoEmbeddingInputs class KeyeVisionEmbeddings(nn.Module): def __init__(self, config: PretrainedConfig): super().__init__() self.config = config self.embed_dim = config.hidden_size self.image_size = config.image_size self.patch_size = config.patch_size self.patch_embedding = Conv2dLayer( in_channels=config.num_channels, out_channels=self.embed_dim, kernel_size=self.patch_size, stride=self.patch_size, padding="valid", ) self.num_patches = (self.image_size // self.patch_size) ** 2 self.num_positions = self.num_patches self.cache_position_embedding = dict() self.cache_position_count = dict() self.position_embedding = nn.Embedding(self.num_positions, self.embed_dim) self.packing_position_embedding = nn.Embedding(32768, self.embed_dim) self.register_buffer( "position_ids", torch.arange(self.num_positions).expand((1, -1)), persistent=False, ) def interpolate_pos_encoding( self, embeddings: torch.Tensor, height: int, width: int, is_after_patchify: bool = False, ) -> torch.Tensor: num_positions = self.position_embedding.weight.shape[0] patch_pos_embed = self.position_embedding.weight.unsqueeze(0) dim = embeddings.shape[-1] if is_after_patchify: new_height = height new_width = width else: new_height = height // self.patch_size new_width = width // self.patch_size sqrt_num_positions = torch_int(num_positions**0.5) patch_pos_embed = patch_pos_embed.reshape( 1, sqrt_num_positions, sqrt_num_positions, dim ) patch_pos_embed = patch_pos_embed.permute(0, 3, 1, 2) patch_pos_embed = nn.functional.interpolate( patch_pos_embed, size=(new_height, new_width), mode="bilinear", align_corners=False, ) patch_pos_embed = patch_pos_embed.permute(0, 2, 3, 1).view(1, -1, dim) return patch_pos_embed def fetch_position_embedding_lfu_cache(self, embeddings, h, w, max_cache: int = 20): grid = (h, w) if grid in self.cache_position_embedding: self.cache_position_count[grid] += 1 return self.cache_position_embedding[grid] if len(self.cache_position_embedding) >= max_cache: min_hit_grid = min( self.cache_position_count, key=self.cache_position_count.get, ) self.cache_position_count.pop(min_hit_grid) self.cache_position_embedding.pop(min_hit_grid) position_embedding = self.interpolate_pos_encoding(embeddings, h, w, True) self.cache_position_count[grid] = 1 self.cache_position_embedding[grid] = position_embedding return position_embedding def forward( self, pixel_values: torch.FloatTensor, position_ids: torch.Tensor | None = None, image_grid_thw: list[tuple[int, int, int] | list[tuple[int, int, int]]] | None = None, interpolate_pos_encoding=False, ) -> torch.Tensor: if pixel_values.dim() == 4: pixel_values = pixel_values.unsqueeze(0) if pixel_values.dim() == 5: if position_ids is None: raise ValueError( "position_ids cannot be None when pixel_values.dim() is 5." ) ( batch_size, squence_len, channel, height, width, ) = pixel_values.shape target_dtype = self.patch_embedding.weight.dtype pixel_values = rearrange(pixel_values, "b l c h w -> (b l) c h w") patch_embeds = self.patch_embedding(pixel_values.to(dtype=target_dtype)) embeddings = patch_embeds.flatten(-2).squeeze(-1) if interpolate_pos_encoding and image_grid_thw is not None: start = 0 tmp_embeddings = list() for image_grid in image_grid_thw: t, h, w = image_grid end = start + t * h * w image_embeddings = embeddings[start:end, :] position_embedding = ( self.interpolate_pos_encoding(image_embeddings, h, w, True) .squeeze(0) .repeat(t, 1) ) image_embeddings = image_embeddings + position_embedding tmp_embeddings.append(image_embeddings) start = end embeddings = torch.concat(tmp_embeddings, dim=0).unsqueeze(0) else: embeddings = embeddings + self.packing_position_embedding(position_ids) return embeddings else: raise ValueError( "Unsupported pixel_values dimension:" f" {pixel_values.dim()}. Expected 4 or 5." ) def apply_rotary_pos_emb_flashatt( q: torch.Tensor, k: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor, apply_rotary_emb: ApplyRotaryEmb, ) -> tuple[torch.Tensor, torch.Tensor]: cos = cos.chunk(2, dim=-1)[0].contiguous() sin = sin.chunk(2, dim=-1)[0].contiguous() q_embed = apply_rotary_emb(q, cos, sin) k_embed = apply_rotary_emb(k, cos, sin) return q_embed, k_embed class KeyeSiglipAttention(nn.Module): """Multi-headed attention from 'Attention Is All You Need' paper.""" def __init__( self, config: PretrainedConfig, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() self.config = config hidden_size = config.hidden_size self.hidden_size = config.hidden_size use_data_parallel = is_vit_use_data_parallel() tp_size = 1 if use_data_parallel else get_tensor_model_parallel_world_size() self.total_num_heads = config.num_attention_heads assert self.total_num_heads % tp_size == 0 self.num_heads = self.total_num_heads // tp_size self.total_num_kv_heads = config.num_attention_heads if self.total_num_kv_heads >= tp_size: assert self.total_num_kv_heads % tp_size == 0 else: assert tp_size % self.total_num_kv_heads == 0 self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size) self.head_dim = config.hidden_size // self.total_num_heads self.q_size = self.num_heads * self.head_dim self.kv_size = self.num_kv_heads * self.head_dim self.scale = self.head_dim**-0.5 self.qkv_proj = QKVParallelLinear( hidden_size, self.head_dim, self.total_num_heads, self.total_num_kv_heads, bias=True, quant_config=quant_config, prefix=f"{prefix}.qkv_proj", ) self.out_proj = RowParallelLinear( input_size=hidden_size, output_size=hidden_size, quant_config=quant_config, prefix=f"{prefix}.out_proj", ) self.attn = MMEncoderAttention( num_heads=self.num_heads, head_size=self.head_dim, scale=self.scale, num_kv_heads=self.num_kv_heads, prefix=f"{prefix}.attn", ) self.apply_rotary_emb = ApplyRotaryEmb( enforce_enable=True, enable_fp32_compute=True, ) def forward( self, hidden_states: torch.Tensor, attention_mask: torch.Tensor | None = None, output_attentions: bool | None = False, cu_seqlens: list[torch.Tensor] | None = None, rope_emb: tuple[torch.Tensor, torch.Tensor] | None = None, ) -> torch.Tensor: qkv, _ = self.qkv_proj(hidden_states) q, k, v = qkv.split( [self.q_size, self.kv_size, self.kv_size], dim=-1, ) max_seqlen = (cu_seqlens[1:] - cu_seqlens[:-1]).max() if rope_emb is None: q = q.view(*q.shape[:-1], self.num_heads, self.head_dim) k = k.view( *k.shape[:-1], self.num_kv_heads, self.head_dim, ) v = v.view( *v.shape[:-1], self.num_kv_heads, self.head_dim, ) else: if cu_seqlens is None: raise ValueError("cu_seqlens cannot be None when rope_emb is not None.") cos, sin = rope_emb q = q.view(*q.shape[:-1], self.num_heads, self.head_dim) k = k.view( *k.shape[:-1], self.num_kv_heads, self.head_dim, ) q, k = apply_rotary_pos_emb_flashatt(q, k, cos, sin, self.apply_rotary_emb) v = v.view( *v.shape[:-1], self.num_kv_heads, self.head_dim, ) context_layer = self.attn( query=q, key=k, value=v, cu_seqlens=cu_seqlens, max_seqlen=max_seqlen, ) context_layer = rearrange(context_layer, "b s h d -> b s (h d)") output, _ = self.out_proj(context_layer) return output class SigLIPRotaryEmbedding(nn.Module): def __init__(self, dim: int, theta: float = 10000.0) -> None: super().__init__() self.dim = dim self.theta = theta self.rope_init() def rope_init(self): inv_freq = 1.0 / ( self.theta ** (torch.arange(0, self.dim, 2, dtype=torch.float) / self.dim) ) self.register_buffer("inv_freq", inv_freq, persistent=False) def forward(self, seqlen: int) -> torch.Tensor: seq = torch.arange( seqlen, device=self.inv_freq.device, dtype=self.inv_freq.dtype, ) freqs = torch.outer(seq, self.inv_freq) return freqs class KeyeSiglipEncoderLayer(nn.Module): def __init__( self, config: PretrainedConfig, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() self.embed_dim = config.hidden_size self.layer_norm1 = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_eps) self.self_attn = KeyeSiglipAttention( config, quant_config=quant_config, prefix=f"{prefix}.self_attn", ) self.layer_norm2 = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_eps) self.mlp = SiglipMLP( config, quant_config=quant_config, prefix=f"{prefix}.mlp", ) def forward( self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: bool | None = False, cu_seqlens: list[torch.Tensor] | None = None, rope_emb: tuple[torch.Tensor, torch.Tensor] | None = None, ) -> tuple[torch.FloatTensor]: residual = hidden_states hidden_states = self.layer_norm1(hidden_states) hidden_states = self.self_attn( hidden_states=hidden_states, attention_mask=attention_mask, output_attentions=output_attentions, cu_seqlens=cu_seqlens, rope_emb=rope_emb, ) hidden_states = residual + hidden_states residual = hidden_states hidden_states = self.layer_norm2(hidden_states) hidden_states = self.mlp(hidden_states) hidden_states = residual + hidden_states return hidden_states class KeyeSiglipEncoder(nn.Module): def __init__( self, config: PretrainedConfig, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() self.config = config embed_dim = config.hidden_size num_heads = config.num_attention_heads head_dim = embed_dim // num_heads self.layers = nn.ModuleList( [ KeyeSiglipEncoderLayer( config, quant_config=quant_config, prefix=f"{prefix}.layers.{layer_idx}", ) for layer_idx in range(config.num_hidden_layers) ] ) self.rotary_pos_emb = SigLIPRotaryEmbedding(head_dim // 2) @staticmethod def flatten_list(image_grid_thw): tmp_image_grid_thw = list() for image_grid in image_grid_thw: if isinstance(image_grid, list): tmp_image_grid_thw.extend(image_grid) else: tmp_image_grid_thw.append(image_grid) return tmp_image_grid_thw def forward( self, inputs_embeds, attention_mask: torch.Tensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, cu_seqlens: list[torch.Tensor] | None = None, image_grid_thw: list[tuple[int, int, int] | list[tuple[int, int, int]]] | None = None, height_position_ids: torch.Tensor | None = None, width_position_ids: torch.Tensor | None = None, use_rope: bool | None = False, window_size: bool | None = -1, vision_or_text: str = "vision", ) -> BaseModelOutput: device = inputs_embeds.device hidden_states = inputs_embeds if use_rope is True: flatten_image_grid_thw = self.flatten_list(image_grid_thw) if width_position_ids is None or height_position_ids is None: split_hids = list() split_wids = list() for t, h, w in flatten_image_grid_thw: image_pids = torch.arange(t * h * w, device=device) % (h * w) sample_hids = image_pids // w sample_wids = image_pids % w split_hids.append(sample_hids) split_wids.append(sample_wids) width_position_ids = torch.concat(split_wids, dim=0) height_position_ids = torch.concat(split_hids, dim=0) pids = torch.stack( [height_position_ids, width_position_ids], dim=-1, ) max_grid_size = pids.max() + 1 rope_emb_max_grid = self.rotary_pos_emb(max_grid_size) rope_emb = rope_emb_max_grid[pids].flatten(1) rope_emb = rope_emb.repeat(1, 2) rope_emb = (rope_emb.cos(), rope_emb.sin()) else: rope_emb = None attn_cu_seqlens = cu_seqlens hidden_states = inputs_embeds assert attention_mask is None for encoder_layer in self.layers: hidden_states = encoder_layer( hidden_states, attention_mask, output_attentions=output_attentions, cu_seqlens=attn_cu_seqlens, rope_emb=rope_emb, ) return hidden_states class KeyeSiglipVisionTransformer(nn.Module): def __init__( self, config: PretrainedConfig, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() self.config = config embed_dim = config.hidden_size self.embeddings = KeyeVisionEmbeddings(config) self.encoder = KeyeSiglipEncoder( config, quant_config=quant_config, prefix=f"{prefix}.encoder", ) self.post_layernorm = nn.LayerNorm(embed_dim, eps=config.layer_norm_eps) def forward( self, pixel_values, output_attentions: bool | None = None, output_hidden_states: bool | None = None, interpolate_pos_encoding: bool | None = False, attention_mask: torch.Tensor | None = None, sample_indices: torch.Tensor | None = None, image_indices: torch.Tensor | None = None, position_ids: torch.Tensor | None = None, height_position_ids: torch.Tensor | None = None, width_position_ids: torch.Tensor | None = None, cu_seqlens: list[torch.Tensor] | None = None, padding_mask: torch.Tensor | None = None, vision_return_embed_list: bool | None = False, image_grid_thw: list[tuple[int, int, int] | list[tuple[int, int, int]]] | None = None, return_pooler_output: bool | None = True, use_rope: bool | None = False, window_size: bool | None = -1, ) -> BaseModelOutputWithPooling: hidden_states = self.embeddings( pixel_values, interpolate_pos_encoding=interpolate_pos_encoding, position_ids=position_ids, image_grid_thw=image_grid_thw, ) last_hidden_state = self.encoder( inputs_embeds=hidden_states, output_attentions=output_attentions, output_hidden_states=output_hidden_states, attention_mask=attention_mask, cu_seqlens=cu_seqlens, image_grid_thw=image_grid_thw, use_rope=use_rope, height_position_ids=height_position_ids, width_position_ids=width_position_ids, window_size=window_size, vision_or_text="vision", ) last_hidden_state = self.post_layernorm(last_hidden_state) sample_hidden_state = list() if cu_seqlens is None: raise ValueError( "cu_seqlens cannot be None for " "SiglipVisionTransformer output processing." ) for i in range(cu_seqlens.shape[0] - 1): start = cu_seqlens[i] end = cu_seqlens[i + 1] tensor = last_hidden_state[:, start:end, :].squeeze(0) sample_hidden_state.append(tensor) return sample_hidden_state class KeyeSiglipVisionModel(nn.Module): config_class = PretrainedConfig main_input_name = "pixel_values" def __init__( self, config: PretrainedConfig, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() self.vision_model = KeyeSiglipVisionTransformer( config, quant_config=quant_config, prefix=f"{prefix}.vision_model", ) self.quant_config = quant_config @property def dtype(self) -> torch.dtype: return self.vision_model.embeddings.patch_embedding.weight.dtype @property def device(self) -> torch.device: return self.vision_model.embeddings.patch_embedding.weight.device def get_input_embeddings(self) -> nn.Module: return self.vision_model.embeddings.patch_embedding def forward( self, pixel_values, sample_indices: torch.Tensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, interpolate_pos_encoding: bool = False, position_ids: torch.Tensor | None = None, vision_return_embed_list: bool | None = False, image_grid_thw: list[tuple[int, int, int] | list[tuple[int, int, int]]] | None = None, cu_seqlens: list[torch.Tensor] | None = None, return_pooler_output: bool | None = True, use_rope: bool | None = False, window_size: bool | None = -1, ) -> BaseModelOutputWithPooling: return self.vision_model( pixel_values=pixel_values, output_attentions=output_attentions, output_hidden_states=output_hidden_states, interpolate_pos_encoding=interpolate_pos_encoding, position_ids=position_ids, vision_return_embed_list=vision_return_embed_list, image_grid_thw=image_grid_thw, sample_indices=sample_indices, cu_seqlens=cu_seqlens, return_pooler_output=return_pooler_output, use_rope=use_rope, window_size=window_size, ) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: stacked_params_mapping = [ ("qkv_proj", "q_proj", "q"), ("qkv_proj", "k_proj", "k"), ("qkv_proj", "v_proj", "v"), ] params_dict = dict(self.named_parameters(remove_duplicate=False)) loaded_params: set[str] = set() for name, loaded_weight in weights: if "rotary_emb.inv_freq" in name: continue if "head.attention" in name or "head.layernorm" in name: continue if "head.mlp" in name or "head.probe" in name: continue if self.quant_config is not None and ( scale_name := self.quant_config.get_cache_scale(name) ): param = params_dict[scale_name] weight_loader = getattr( param, "weight_loader", default_weight_loader, ) loaded_weight = ( loaded_weight if loaded_weight.dim() == 0 else loaded_weight[0] ) weight_loader(param, loaded_weight) loaded_params.add(scale_name) continue for ( param_name, weight_name, shard_id, ) in stacked_params_mapping: if weight_name not in name: continue name = name.replace(weight_name, param_name) if name.endswith(".bias") and name not in params_dict: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = param.weight_loader weight_loader(param, loaded_weight, shard_id) break else: if name.endswith(".bias") and name not in params_dict: continue name = maybe_remap_kv_scale_name(name, params_dict) if name is None: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = getattr( param, "weight_loader", default_weight_loader, ) weight_loader(param, loaded_weight) loaded_params.add(name) return loaded_params class Projector(nn.Module): def __init__( self, text_config: PretrainedConfig, vision_config: PretrainedConfig, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() self.text_config = text_config self.vision_config = vision_config self.merge_kernel_size = (2, 2) self.hidden_size = ( self.vision_config.hidden_size * self.merge_kernel_size[0] * self.merge_kernel_size[1] ) self.pre_norm = torch.nn.LayerNorm(self.vision_config.hidden_size, eps=1e-05) self.act = GELUActivation() self.linear_1 = ColumnParallelLinear( self.hidden_size, self.hidden_size, bias=True, quant_config=quant_config, prefix=f"{prefix}.linear_1", ) self.linear_2 = RowParallelLinear( self.hidden_size, self.text_config.hidden_size, bias=True, quant_config=quant_config, prefix=f"{prefix}.linear_2", ) def forward( self, image_features: torch.Tensor | list[torch.Tensor], image_grid_thw: list[tuple[int, int, int]], ) -> torch.Tensor | list[torch.Tensor]: m1, m2 = self.merge_kernel_size if isinstance(image_features, (list, tuple)): processed_features = list() for image_feature, image_grid in zip(image_features, image_grid_thw): image_feature = self.pre_norm(image_feature) t, h, w = image_grid image_feature = rearrange( image_feature, "(t h p1 w p2) d -> (t h w) (p1 p2 d)", t=t, h=h // m1, p1=m1, w=w // m2, p2=m2, ) hidden_states, _ = self.linear_1(image_feature) hidden_states = self.act(hidden_states) hidden_states, _ = self.linear_2(hidden_states) processed_features.append(hidden_states) return processed_features dims = image_features.shape[:-1] dim = image_features.shape[-1] image_features = image_features.view(np.prod(dims), dim) hidden_states = self.pre_norm(image_features).view(-1, self.hidden_size) hidden_states = self.linear_1(hidden_states) hidden_states = self.act(hidden_states) hidden_states = self.linear_2(hidden_states) return hidden_states.view(*dims, -1) def _keye_field_config( hf_inputs: Mapping[str, torch.Tensor], ): image_grid_thw = hf_inputs.get("image_grid_thw", torch.empty((0, 3))) image_grid_sizes = image_grid_thw.prod(-1) video_grid_thw = hf_inputs.get("video_grid_thw", torch.empty((0, 3))) video_grid_sizes = video_grid_thw.prod(-1) return dict( pixel_values=MultiModalFieldConfig.flat_from_sizes("image", image_grid_sizes), image_embeds=MultiModalFieldConfig.flat_from_sizes("image", image_grid_sizes), image_grid_thw=MultiModalFieldConfig.batched("image"), pixel_values_videos=MultiModalFieldConfig.flat_from_sizes( "video", video_grid_sizes ), video_embeds=MultiModalFieldConfig.flat_from_sizes("video", video_grid_sizes), video_grid_thw=MultiModalFieldConfig.batched("video"), ) class KeyeMultiModalDataParser(MultiModalDataParser): def _parse_image_data( self, data: dict[str, torch.Tensor] | ModalityData[ImageItem], ) -> ModalityDataItems[Any, Any] | None: if isinstance(data, dict): return DictEmbeddingItems( data, modality="image", required_fields={ "image_embeds", "image_grid_thw", }, fields_factory=_keye_field_config, ) return super()._parse_image_data(data) def _parse_video_data( self, data: dict[str, torch.Tensor] | ModalityData[VideoItem], ) -> ModalityDataItems[Any, Any] | None: if isinstance(data, dict): return DictEmbeddingItems( data, modality="video", required_fields={ "video_embeds", "video_grid_thw", }, fields_factory=_keye_field_config, ) return super()._parse_video_data(data) class KeyeProcessingInfo(BaseProcessingInfo): def get_max_image_size(self) -> int: return 9999999 # _MAX_IMAGE_SIZE def get_max_frame_per_video(self) -> int: return 16 # _MAX_FRAMES_PER_VIDEO def get_image_processor(self, **kwargs: object): return self.get_hf_processor(**kwargs).image_processor def get_data_parser(self): return KeyeMultiModalDataParser( expected_hidden_size=self._get_expected_hidden_size(), ) def get_supported_mm_limits( self, ) -> Mapping[str, int | None]: return {"image": None, "video": None} def get_mm_max_tokens_per_item( self, seq_len: int, mm_counts: Mapping[str, int], ) -> Mapping[str, int]: return { "image": self.get_max_image_tokens(), "video": self.get_max_video_tokens(seq_len), } def _get_vision_info( self, *, image_width: int, image_height: int, num_frames: int = 1, do_resize: bool = True, image_processor: BaseImageProcessor, mm_kwargs: Mapping[str, object], ) -> tuple[ImageSize, int]: hf_config = self.get_hf_config() vision_config = hf_config.vision_config patch_size = vision_config.patch_size merge_size = vision_config.spatial_merge_size temporal_patch_size = 1 mm_kwargs = self.ctx.get_merged_mm_kwargs(mm_kwargs) size = image_processor.size if override_size := mm_kwargs.get("size"): size = size | override_size if (override_min_pixels := mm_kwargs.get("min_pixels")) is not None: size = size | {"min_pixels": override_min_pixels} if (override_max_pixels := mm_kwargs.get("max_pixels")) is not None: size = size | {"max_pixels": override_max_pixels} if do_resize: resized_height, resized_width = smart_resize( height=image_height, width=image_width, factor=patch_size * merge_size, min_pixels=size["min_pixels"], max_pixels=size["max_pixels"], ) preprocessed_size = ImageSize(width=resized_width, height=resized_height) else: preprocessed_size = ImageSize(width=image_width, height=image_height) padded_num_frames = num_frames + num_frames % temporal_patch_size grid_t = max(padded_num_frames // temporal_patch_size, 1) grid_h = preprocessed_size.height // patch_size grid_w = preprocessed_size.width // patch_size num_patches = grid_t * grid_h * grid_w num_vision_tokens = num_patches // (merge_size**2) return preprocessed_size, num_vision_tokens def get_num_image_tokens( self, *, image_width: int, image_height: int, image_processor: BaseImageProcessor, mm_kwargs: Mapping[str, object], ) -> int: _, num_image_tokens = self._get_vision_info( image_width=image_width, image_height=image_height, image_processor=image_processor, mm_kwargs=mm_kwargs, ) return num_image_tokens def get_num_video_tokens( self, *, image_width: int, image_height: int, num_frames: int, image_processor: BaseImageProcessor, mm_kwargs: Mapping[str, object], ) -> int: _, num_video_tokens = self._get_vision_info( image_width=image_width, image_height=image_height, num_frames=num_frames, image_processor=image_processor, mm_kwargs=mm_kwargs, ) return num_video_tokens def get_image_size_with_most_features(self) -> ImageSize: image_processor = self.get_image_processor() max_image_size, _ = self._get_vision_info( image_width=self.get_max_image_size(), image_height=self.get_max_image_size(), image_processor=image_processor, mm_kwargs={}, ) return max_image_size def get_max_image_tokens(self) -> int: image_processor = self.get_image_processor() target_width, target_height = self.get_image_size_with_most_features() return self.get_num_image_tokens( image_width=target_width, image_height=target_height, image_processor=image_processor, mm_kwargs={}, ) def _get_max_video_frames(self, max_tokens: int) -> int: image_processor = self.get_image_processor() target_width, target_height = self.get_image_size_with_most_features() num_frames = 0 while True: next_num_frames = num_frames + 1 next_max_tokens = self.get_num_video_tokens( image_width=target_width, image_height=target_height, num_frames=next_num_frames, image_processor=image_processor, mm_kwargs={}, ) if next_max_tokens > max_tokens: break num_frames = next_num_frames return num_frames def get_num_frames_with_most_features(self, seq_len: int) -> int: mm_config = self.ctx.get_mm_config() max_images = mm_config.get_limit_per_prompt("image") max_videos = mm_config.get_limit_per_prompt("video") max_image_tokens = self.get_max_image_tokens() * max_images max_total_frames = self._get_max_video_frames(seq_len - max_image_tokens) max_frames_per_video = min( max_total_frames // max(max_videos, 1), self.get_max_frame_per_video(), ) return max(max_frames_per_video, 1) def get_max_video_tokens(self, seq_len: int) -> int: image_processor = self.get_image_processor() target_width, target_height = self.get_image_size_with_most_features() return self.get_num_video_tokens( image_width=target_width, image_height=target_height, num_frames=self.get_num_frames_with_most_features(seq_len), image_processor=image_processor, mm_kwargs={}, ) _I = TypeVar("_I", bound=KeyeProcessingInfo) class KeyeBaseDummyInputsBuilder(BaseDummyInputsBuilder[_I]): def get_dummy_text(self, mm_counts: Mapping[str, int]) -> str: num_images = mm_counts.get("image", 0) num_videos = mm_counts.get("video", 0) hf_processor = self.info.get_hf_processor() image_token: str = hf_processor.image_token video_token: str = hf_processor.video_token return image_token * num_images + video_token * num_videos def get_dummy_mm_data( self, seq_len: int, mm_counts: Mapping[str, int], mm_options: Mapping[str, BaseDummyOptions], ) -> MultiModalDataDict: num_images = mm_counts.get("image", 0) num_videos = mm_counts.get("video", 0) target_width, target_height = self.info.get_image_size_with_most_features() target_num_frames = self.info.get_num_frames_with_most_features(seq_len) image_overrides = mm_options.get("image") video_overrides = mm_options.get("video") mm_data = { "image": self._get_dummy_images( width=target_width, height=target_height, num_images=num_images, overrides=image_overrides, ), "video": self._get_dummy_videos( width=target_width, height=target_height, num_frames=target_num_frames, num_videos=num_videos, overrides=video_overrides, ), } return mm_data class KeyeDummyInputsBuilder(KeyeBaseDummyInputsBuilder[KeyeProcessingInfo]): pass class KeyeMultiModalProcessor(BaseMultiModalProcessor[KeyeProcessingInfo]): def _get_prompt_updates( self, mm_items: MultiModalDataItems, hf_processor_mm_kwargs: Mapping[str, Any], out_mm_kwargs: MultiModalKwargsItems, ) -> Sequence[PromptUpdate]: hf_processor = self.info.get_hf_processor(**hf_processor_mm_kwargs) image_processor = self.info.get_image_processor(**hf_processor_mm_kwargs) tokenizer = self.info.get_tokenizer() vocab = tokenizer.get_vocab() placeholder = { "image": vocab[hf_processor.image_token], "video": vocab[hf_processor.video_token], } merge_length = image_processor.merge_size**2 def get_replacement_keye(item_idx: int, modality: str): out_item = out_mm_kwargs[modality][item_idx] grid_thw = out_item[f"{modality}_grid_thw"].data assert isinstance(grid_thw, torch.Tensor) num_tokens = int(grid_thw.prod()) // merge_length return [placeholder[modality]] * num_tokens return [ PromptReplacement( modality=modality, target=[placeholder[modality]], replacement=partial(get_replacement_keye, modality=modality), ) for modality in ("image", "video") ] def _get_mm_fields_config( self, hf_inputs: BatchFeature, hf_processor_mm_kwargs: Mapping[str, object], ) -> Mapping[str, MultiModalFieldConfig]: return _keye_field_config(hf_inputs) class BaseKeyeModule(nn.Module, SupportsMultiModal): packed_modules_mapping = { "qkv_proj": [ "q_proj", "k_proj", "v_proj", ], "gate_up_proj": [ "gate_proj", "up_proj", ], } hf_to_vllm_mapper = WeightsMapper( orig_to_new_prefix={ "lm_head.": "language_model.lm_head.", "model.": "language_model.model.", } ) @classmethod def get_placeholder_str(cls, modality: str, i: int) -> str | None: if modality.startswith("image"): return "<|vision_start|><|image_pad|><|vision_end|>" if modality.startswith("video"): return "<|vision_start|><|video_pad|><|vision_end|>" raise ValueError("Only image or video modality is supported") def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__() config: PretrainedConfig = vllm_config.model_config.hf_config quant_config = vllm_config.quant_config self.config = config with self._mark_tower_model(vllm_config, {"image", "video"}): self.visual = KeyeSiglipVisionModel( config.vision_config, quant_config=quant_config, prefix=maybe_prefix(prefix, "visual"), ) self.mlp_AR = self._build_projector( config, config.vision_config, quant_config=quant_config, prefix=maybe_prefix(prefix, "mlp_AR"), ) with self._mark_language_model(vllm_config): self.language_model = init_vllm_registered_model( vllm_config=vllm_config, prefix=maybe_prefix(prefix, "language_model"), architectures=["Qwen3ForCausalLM"], ) self.make_empty_intermediate_tensors = ( self.language_model.make_empty_intermediate_tensors ) @abstractmethod def _build_projector( self, text_config: PretrainedConfig, vision_config: PretrainedConfig, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> nn.Module: raise NotImplementedError("Need projector") def _process_image_input(self, image_input: Any) -> tuple[torch.Tensor, ...]: siglip_position_ids = list() image_grid_hws = list() sample_indices = list() cu_seqlens = [0] image_grid_thw = image_input["image_grid_thw"] assert image_grid_thw.ndim == 2 for idx, thaw in enumerate(image_grid_thw): thw_tuple = tuple(thaw.detach().cpu().numpy().tolist()) numel = np.prod(thw_tuple) image_grid_hws.append(thw_tuple) image_position_ids = torch.arange(numel) % np.prod(thw_tuple[1:]) siglip_position_ids.append(image_position_ids) sample_indices.append(torch.full((numel,), idx, dtype=torch.int64)) cu_seqlens.append(cu_seqlens[-1] + numel) if image_input["type"] == "image_embeds": raise ValueError( "Image embeddings are not supported for this processing path." ) else: pixel_values = image_input["pixel_values"].type(self.visual.dtype) siglip_position_ids = torch.concat(siglip_position_ids, dim=0).to( pixel_values.device ) cu_seqlens = torch.tensor(cu_seqlens, dtype=torch.int32).to( pixel_values.device ) sample_indices = torch.concat(sample_indices, dim=0).to(pixel_values.device) image_embeds = self.visual( pixel_values=pixel_values, image_grid_thw=image_grid_hws, position_ids=siglip_position_ids, vision_return_embed_list=False, interpolate_pos_encoding=True, sample_indices=sample_indices, cu_seqlens=cu_seqlens, use_rope=True, window_size=-1, ) image_embeds = tuple(self.mlp_AR(image_embeds, image_grid_thw)) return image_embeds def _process_video_embeds( self, video_type: Literal["video_embeds", "pixel_values_videos"], video_grid_thw: list[torch.Tensor], pixel_values_videos: torch.Tensor | None = None, ) -> torch.Tensor | list[torch.Tensor]: siglip_position_ids = list() video_grid_hws = list() sample_indices = list() cu_seqlens = [0] assert video_grid_thw.ndim == 2 for idx, sub_thw in enumerate(video_grid_thw): thw_tuple = tuple(sub_thw.detach().cpu().numpy().tolist()) numel = np.prod(thw_tuple) video_grid_hws.append(thw_tuple) video_position_ids = torch.arange(numel) % np.prod(thw_tuple[1:]) siglip_position_ids.append(video_position_ids) sample_indices.append(torch.full((numel,), idx, dtype=torch.int64)) cu_seqlens.append(cu_seqlens[-1] + numel) if video_type == "video_embeds": raise ValueError( "Video embeddings are not supported for this processing path." ) else: pixel_values_videos = pixel_values_videos.type(self.visual.dtype) siglip_position_ids = torch.concat(siglip_position_ids, dim=0).to( pixel_values_videos.device ) cu_seqlens = torch.tensor(cu_seqlens, dtype=torch.int32).to( pixel_values_videos.device ) sample_indices = torch.concat(sample_indices, dim=0).to( pixel_values_videos.device ) video_embeds = self.visual( pixel_values=pixel_values_videos, image_grid_thw=video_grid_hws, position_ids=siglip_position_ids, vision_return_embed_list=True, interpolate_pos_encoding=True, sample_indices=sample_indices, cu_seqlens=cu_seqlens, use_rope=True, window_size=-1, ) video_embeds = self.mlp_AR(video_embeds, video_grid_thw) return video_embeds def _parse_and_validate_multimodal_inputs(self, **kwargs: object) -> dict: modalities = {} for input_key in kwargs: if ( input_key in ("pixel_values", "image_embeds") and "images" not in modalities ): modalities["images"] = self._parse_and_validate_image_input(**kwargs) if ( input_key in ("pixel_values_videos", "video_embeds") and "videos" not in modalities ): modalities["videos"] = self._parse_and_validate_video_input(**kwargs) return modalities def embed_multimodal(self, **kwargs: object) -> MultiModalEmbeddings | None: modalities = self._parse_and_validate_multimodal_inputs(**kwargs) if not modalities: return None multimodal_embeddings: tuple[torch.Tensor, ...] = () for modality in modalities: if modality == "images": image_input = modalities["images"] image_embeddings = self._process_image_input(image_input) multimodal_embeddings += tuple(image_embeddings) if modality == "videos": video_input = modalities["videos"] video_embeddings = self._process_video_input(video_input) multimodal_embeddings += tuple(video_embeddings) return multimodal_embeddings def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, **kwargs: object, ) -> torch.Tensor | IntermediateTensors: """Run forward pass for Keye-VL. Args: input_ids: Flattened (concatenated) input_ids corresponding to a batch. positions: Flattened (concatenated) position ids corresponding to a batch. **NOTE**: If mrope is enabled (default setting for Qwen2-VL opensource models), the shape will be `(3, seq_len)`, otherwise it will be `(seq_len,)`. intermediate_tensors: Intermediate tensors from prior forward pass. inputs_embeds: Optional tensor of input embeddings. """ if intermediate_tensors is not None: inputs_embeds = None hidden_states = self.language_model.model( input_ids=input_ids, positions=positions, intermediate_tensors=intermediate_tensors, inputs_embeds=inputs_embeds, ) return hidden_states def compute_logits( self, hidden_states: torch.Tensor, ) -> torch.Tensor | None: return self.language_model.compute_logits(hidden_states) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: loader = AutoWeightsLoader(self) return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper) def get_mm_mapping(self) -> MultiModelKeys: """Get the module prefix in multimodal models.""" return MultiModelKeys.from_string_field( language_model="language_model", connector="mlp_AR.", tower_model="visual.", ) @MULTIMODAL_REGISTRY.register_processor( KeyeMultiModalProcessor, info=KeyeProcessingInfo, dummy_inputs=KeyeDummyInputsBuilder, ) class KeyeForConditionalGeneration( BaseKeyeModule, SupportsMultiModal, SupportsLoRA, SupportsPP, SupportsMRoPE ): def _build_projector( self, text_config: PretrainedConfig, vision_config: PretrainedConfig, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> nn.Module: return Projector(text_config, vision_config, quant_config, prefix) def _parse_and_validate_image_input( self, **kwargs: object ) -> KeyeImageInputs | None: pixel_values = kwargs.pop("pixel_values", None) image_embeds = kwargs.pop("image_embeds", None) image_grid_thw = kwargs.pop("image_grid_thw", None) if pixel_values is None and image_embeds is None: return None if pixel_values is not None: return KeyeImagePixelInputs( type="pixel_values", pixel_values=pixel_values, image_grid_thw=image_grid_thw, ) if image_embeds is not None: return KeyeImageEmbeddingInputs( type="image_embeds", image_embeds=image_embeds, image_grid_thw=image_grid_thw, ) def _parse_and_validate_video_input( self, **kwargs: object ) -> KeyeVideoInputs | None: pixel_values_videos = kwargs.pop("pixel_values_videos", None) video_embeds = kwargs.pop("video_embeds", None) video_grid_thw = kwargs.pop("video_grid_thw", None) if pixel_values_videos is None and video_embeds is None: return None if pixel_values_videos is not None: return KeyeVideoPixelInputs( type="pixel_values_videos", pixel_values_videos=pixel_values_videos, video_grid_thw=video_grid_thw, ) if video_embeds is not None: return KeyeVideoEmbeddingInputs( type="video_embeds", video_embeds=video_embeds, video_grid_thw=video_grid_thw, ) def _process_video_input( self, video_input: KeyeVideoInputs ) -> tuple[torch.Tensor, ...]: video_type = video_input["type"] video_grid_thw = video_input["video_grid_thw"] pixel_values_videos = video_input.get("pixel_values_videos", None) return tuple( self._process_video_embeds(video_type, video_grid_thw, pixel_values_videos) ) def get_mrope_input_positions( self, input_tokens: list[int], mm_features: list[MultiModalFeatureSpec], ) -> tuple[torch.Tensor, int]: kwargs = MultiModalFeatureSpec.gather_kwargs( mm_features, {"image_grid_thw", "video_grid_thw"}, ) image_grid_thw = [item.tolist() for item in kwargs.get("image_grid_thw", [])] video_grid_thw = [item.tolist() for item in kwargs.get("video_grid_thw", [])] if isinstance(video_grid_thw, list) and len(video_grid_thw) > 0: video_grid_thw = video_grid_thw[0] def split_thw(grid_thw: torch.Tensor | list[int]) -> list[list[int]]: """ Split grid_thw along the t dimension. Args: grid_thw: shape [N, 3] tensor or nested list of [t, h, w]. Returns: List of [1, h, w] rows, repeated t times for each original row. """ if isinstance(grid_thw, list): grid_thw = torch.tensor(grid_thw, dtype=torch.long) if grid_thw.numel() == 0: return [] t, hw = grid_thw[:, 0], grid_thw[:, 1:] ones = torch.ones_like(hw[:, :1]) # [N,1] out = torch.cat([ones, hw], dim=1).repeat_interleave(t, dim=0) return out.tolist() video_grid_thw = split_thw(video_grid_thw) hf_config = self.config image_token_id = hf_config.image_token_id video_token_id = hf_config.video_token_id spatial_merge_size = hf_config.vision_config.spatial_merge_size image_nums = len(image_grid_thw) frame_nums = len(video_grid_thw) llm_pos_ids_list: list = [] st = 0 remain_images, remain_frames = image_nums, frame_nums image_index, video_index = 0, 0 for _ in range(image_nums + frame_nums): if remain_images > 0: try: ed_image = input_tokens.index(image_token_id, st) except ValueError: ed_image = len(input_tokens) + 1 else: ed_image = len(input_tokens) + 1 if remain_frames > 0: try: ed_video = input_tokens.index(video_token_id, st) except ValueError: ed_video = len(input_tokens) + 1 else: ed_video = len(input_tokens) + 1 if ed_image < ed_video: t, h, w = image_grid_thw[image_index] image_index += 1 remain_images -= 1 ed = ed_image else: t, h, w = video_grid_thw[video_index] video_index += 1 remain_frames -= 1 ed = ed_video llm_grid_t, llm_grid_h, llm_grid_w = ( t, h // spatial_merge_size, w // spatial_merge_size, ) text_len = ed - st st_idx = llm_pos_ids_list[-1].max() + 1 if len(llm_pos_ids_list) > 0 else 0 llm_pos_ids_list.append( torch.arange(text_len).view(1, -1).expand(3, -1) + st_idx ) t_index = ( ( torch.arange(llm_grid_t) .view(-1, 1) .expand(-1, llm_grid_h * llm_grid_w) ) .long() .flatten() ) h_index = ( torch.arange(llm_grid_h) .view(1, -1, 1) .expand(llm_grid_t, -1, llm_grid_w) .flatten() ) w_index = ( torch.arange(llm_grid_w) .view(1, 1, -1) .expand(llm_grid_t, llm_grid_h, -1) .flatten() ) llm_pos_ids_list.append( torch.stack([t_index, h_index, w_index]) + text_len + st_idx ) st = ed + llm_grid_t * llm_grid_h * llm_grid_w if st < len(input_tokens): st_idx = llm_pos_ids_list[-1].max() + 1 if len(llm_pos_ids_list) > 0 else 0 text_len = len(input_tokens) - st llm_pos_ids_list.append( torch.arange(text_len).view(1, -1).expand(3, -1) + st_idx ) llm_positions = torch.cat(llm_pos_ids_list, dim=1).reshape(3, -1) mrope_position_delta = (llm_positions.max() + 1 - len(input_tokens)).item() return llm_positions, mrope_position_delta
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/keye.py", "license": "Apache License 2.0", "lines": 1484, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/glm4_1v.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Adapted from # https://github.com/huggingface/transformers/blob/main/src/transformers/models/Glm4v/modeling_Glm4v.py # Copyright 2025 The vLLM team. # Copyright 2025 The ZhipuAI Team. # Copyright 2025 The HuggingFace Inc. team. # All rights reserved. # # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX # and OPT implementations in this library. It has been modified from its # original forms to accommodate minor architectural differences compared # to GPT-NeoX and OPT used by the Meta AI team that trained the model. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Inference-only GLM-4.1V & GLM-4.6V-Flash, AutoGLM-Phone-9B model compatible with HuggingFace weights.""" import math from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence from functools import partial from typing import Annotated, Any, Literal, TypeAlias import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from einops import rearrange from transformers import BatchFeature, Glm4vProcessor from transformers.models.glm4v.configuration_glm4v import Glm4vVisionConfig from transformers.models.glm4v.image_processing_glm4v import ( Glm4vImageProcessor, smart_resize, ) from transformers.models.glm4v.video_processing_glm4v import Glm4vVideoProcessor from transformers.video_utils import VideoMetadata from vllm.config import VllmConfig from vllm.config.multimodal import BaseDummyOptions, VideoDummyOptions from vllm.distributed import get_tensor_model_parallel_world_size, parallel_state from vllm.distributed import utils as dist_utils from vllm.logger import init_logger from vllm.model_executor.layers.attention import ( MMEncoderAttention, ) from vllm.model_executor.layers.conv import Conv2dLayer, Conv3dLayer from vllm.model_executor.layers.layernorm import RMSNorm from vllm.model_executor.layers.linear import ( ColumnParallelLinear, MergedColumnParallelLinear, QKVParallelLinear, RowParallelLinear, ) from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.rotary_embedding import get_rope from vllm.model_executor.layers.rotary_embedding.common import ( ApplyRotaryEmb, ) from vllm.model_executor.model_loader.weight_utils import default_weight_loader from vllm.model_executor.models.module_mapping import MultiModelKeys from vllm.multimodal import MULTIMODAL_REGISTRY from vllm.multimodal.inputs import ( MultiModalDataDict, MultiModalFeatureSpec, MultiModalFieldConfig, MultiModalKwargsItems, VideoItem, ) from vllm.multimodal.parse import ImageSize, MultiModalDataItems, MultiModalDataParser from vllm.multimodal.processing import ( BaseDummyInputsBuilder, BaseMultiModalProcessor, BaseProcessingInfo, PromptReplacement, PromptUpdate, PromptUpdateDetails, ) from vllm.sequence import IntermediateTensors from vllm.utils.tensor_schema import TensorSchema, TensorShape from vllm.v1.attention.backends.registry import AttentionBackendEnum from ..layers.activation import SiluAndMul from .interfaces import ( MultiModalEmbeddings, SupportsLoRA, SupportsMRoPE, SupportsMultiModal, SupportsPP, ) from .qwen2_vl import _create_qwen2vl_field_factory from .utils import ( AutoWeightsLoader, WeightsMapper, init_vllm_registered_model, maybe_prefix, ) from .vision import ( get_vit_attn_backend, is_vit_use_data_parallel, run_dp_sharded_mrope_vision_model, ) logger = init_logger(__name__) # For profile run _MAX_FRAMES_PER_VIDEO = 600 # === Vision Inputs === # class Glm4vImagePixelInputs(TensorSchema): """ Dimensions: - np: Number of patches - cpp: Number of channels * patch_size * patch_size - ni: Number of images - g: Grid dimensions (3 for grid_t, grid_h, grid_w) """ type: Literal["pixel_values"] = "pixel_values" pixel_values: Annotated[torch.Tensor, TensorShape("np", "cpp")] image_grid_thw: Annotated[torch.Tensor, TensorShape("ni", 3)] class Glm4vImageEmbeddingInputs(TensorSchema): """ Dimensions: - f: Number of image features (varies based on image resolution) - h: Hidden size (must match language model backbone) - n: Number of images - g: Grid dimensions (3 for grid_t, grid_h, grid_w) """ type: Literal["image_embeds"] = "image_embeds" image_embeds: Annotated[torch.Tensor, TensorShape("f", "h")] image_grid_thw: Annotated[torch.Tensor, TensorShape("n", 3)] Glm4vImageInputs: TypeAlias = Glm4vImagePixelInputs | Glm4vImageEmbeddingInputs class Glm4vVideoPixelInputs(TensorSchema): """ Dimensions: - np: Number of patches - ctpp: Number of channels * temporal_patch_size * patch_size * patch_size - f: Number of frames - g: Grid dimensions (3 for grid_t which is usually 1 for processed video, grid_h, grid_w) """ type: Literal["pixel_values_videos"] = "pixel_values_videos" pixel_values_videos: Annotated[torch.Tensor, TensorShape("np", "ctpp")] video_grid_thw: Annotated[torch.Tensor, TensorShape("f", 3)] class Glm4vVideoEmbeddingInputs(TensorSchema): """ Dimensions: - p: Number of video patches across all frames - h: Hidden size (must match language model backbone) - f: Number of frames - g: Grid dimensions (3 for grid_t which is usually 1 for processed video, grid_h, grid_w) """ type: Literal["video_embeds"] = "video_embeds" video_embeds: Annotated[torch.Tensor, TensorShape("p", "h")] video_grid_thw: Annotated[torch.Tensor, TensorShape("f", 3)] Glm4vVideoInputs: TypeAlias = Glm4vVideoPixelInputs | Glm4vVideoEmbeddingInputs # ==== Vision Encoder ==== # class Glm4vVisionMLP(nn.Module): def __init__( self, in_features: int, hidden_features: int, bias: bool = False, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() use_data_parallel = is_vit_use_data_parallel() self.gate_up_proj = MergedColumnParallelLinear( input_size=in_features, output_sizes=[hidden_features] * 2, bias=bias, quant_config=quant_config, prefix=f"{prefix}.gate_up_proj", disable_tp=use_data_parallel, ) self.down_proj = RowParallelLinear( hidden_features, in_features, bias=bias, quant_config=quant_config, prefix=f"{prefix}.down_proj", disable_tp=use_data_parallel, ) self.act_fn = SiluAndMul() def forward(self, x: torch.Tensor): x, _ = self.gate_up_proj(x) x = self.act_fn(x) x, _ = self.down_proj(x) return x def all_gather_interleave(local_tensor, hidden_size: int, tp_size: int): """All-gather the input tensor interleavely across model parallel group.""" import torch.distributed as dist gathered_tensors = [torch.zeros_like(local_tensor) for _ in range(tp_size)] dist.all_gather( gathered_tensors, local_tensor, group=parallel_state.get_tp_group().device_group, ) gathered_tensors_split = [ torch.split(tensor, hidden_size // tp_size, -1) for tensor in gathered_tensors ] ordered_tensors = [ tensor for pair in zip(*gathered_tensors_split) for tensor in pair ] result_tensor = torch.cat(ordered_tensors, dim=-1) return result_tensor class Glm4vVisionAttention(nn.Module): def __init__( self, embed_dim: int, num_heads: int, projection_size: int, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> None: super().__init__() # Per attention head and per partition values. use_data_parallel = is_vit_use_data_parallel() self.tp_size = ( 1 if use_data_parallel else get_tensor_model_parallel_world_size() ) self.tp_rank = ( 0 if use_data_parallel else parallel_state.get_tensor_model_parallel_rank() ) self.hidden_size_per_attention_head = dist_utils.divide( projection_size, num_heads ) self.num_attention_heads_per_partition = dist_utils.divide( num_heads, self.tp_size ) self.qkv = QKVParallelLinear( hidden_size=embed_dim, head_size=self.hidden_size_per_attention_head, total_num_heads=num_heads, total_num_kv_heads=num_heads, bias=False, quant_config=quant_config, # Change qkv prefix to align with GLM-4.5V-FP8 quantization cfg prefix=f"{prefix}.qkv_proj" if quant_config else f"{prefix}.qkv", disable_tp=use_data_parallel, ) self.proj = RowParallelLinear( input_size=projection_size, output_size=embed_dim, quant_config=quant_config, prefix=f"{prefix}.proj", bias=False, disable_tp=use_data_parallel, ) self.attn = MMEncoderAttention( num_heads=self.num_attention_heads_per_partition, head_size=self.hidden_size_per_attention_head, scale=self.hidden_size_per_attention_head**-0.5, prefix=f"{prefix}.attn", ) self.apply_rotary_emb = ApplyRotaryEmb(enforce_enable=True) def split_qkv(self, qkv: torch.Tensor) -> tuple[torch.Tensor, ...]: # [s, b, 3 * head * head_dim] seq_len, bs, _ = qkv.shape # [s, b, 3 * head * head_dim] -> 3 * [s, b, head * head_dim] q, k, v = qkv.chunk(3, dim=2) # 3 * [s, b, head * head_dim] -> 3 * [s, b, head, head_dim] new_shape = ( seq_len, bs, self.num_attention_heads_per_partition, self.hidden_size_per_attention_head, ) q, k, v = (x.view(*new_shape) for x in (q, k, v)) return q, k, v def forward( self, x: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb_cos: torch.Tensor, rotary_pos_emb_sin: torch.Tensor, max_seqlen: torch.Tensor | None = None, # Only used for Flash Attention ) -> torch.Tensor: # [s, b, c] --> [s, b, head * 3 * head_dim] x, _ = self.qkv(x) # [s, b, 3 * head * head_dim] -> 3 * [s, b, head, head_dim] q, k, v = self.split_qkv(x) q, k, v = (rearrange(x, "s b ... -> b s ...").contiguous() for x in (q, k, v)) if rotary_pos_emb_cos is not None and rotary_pos_emb_sin is not None: # [2 * b, s, heads, head_dim] qk_concat = torch.cat([q, k], dim=0) qk_rotated = self.apply_rotary_emb( qk_concat, rotary_pos_emb_cos, rotary_pos_emb_sin, ) q, k = torch.chunk(qk_rotated, 2, dim=0) context_layer = self.attn( query=q, key=k, value=v, cu_seqlens=cu_seqlens, max_seqlen=max_seqlen, ) context_layer = rearrange(context_layer, "b s h d -> s b (h d)").contiguous() output, _ = self.proj(context_layer) return output class Glm4vVisionBlock(nn.Module): def __init__( self, dim: int, num_heads: int, mlp_hidden_dim: int, norm_layer: Callable[[int], nn.Module] | None = None, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> None: super().__init__() if norm_layer is None: norm_layer = partial(nn.LayerNorm, eps=1e-6) self.norm1 = norm_layer(dim) self.norm2 = norm_layer(dim) self.attn = Glm4vVisionAttention( embed_dim=dim, num_heads=num_heads, projection_size=dim, quant_config=quant_config, prefix=f"{prefix}.attn", ) self.mlp = Glm4vVisionMLP( dim, mlp_hidden_dim, bias=False, quant_config=quant_config, prefix=f"{prefix}.mlp", ) def forward( self, x: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb_cos: torch.Tensor, rotary_pos_emb_sin: torch.Tensor, max_seqlen: int | None = None, # Only used for Flash Attention ) -> torch.Tensor: x_attn = self.attn( self.norm1(x), cu_seqlens=cu_seqlens, rotary_pos_emb_cos=rotary_pos_emb_cos, rotary_pos_emb_sin=rotary_pos_emb_sin, max_seqlen=max_seqlen, ) x_fused_norm, residual = self.norm2(x, residual=x_attn) x = residual + self.mlp(x_fused_norm) return x class Glm4vVisionPatchEmbed(nn.Module): def __init__( self, patch_size: int = 14, temporal_patch_size: int = 1, in_channels: int = 3, hidden_size: int = 1536, ) -> None: super().__init__() self.patch_size = patch_size self.temporal_patch_size = temporal_patch_size self.hidden_size = hidden_size kernel_size = (temporal_patch_size, patch_size, patch_size) self.proj = Conv3dLayer( in_channels, hidden_size, kernel_size=kernel_size, stride=kernel_size, bias=True, ) def forward(self, x: torch.Tensor) -> torch.Tensor: L, C = x.shape x = x.view(L, -1, self.temporal_patch_size, self.patch_size, self.patch_size) x = self.proj(x).view(L, self.hidden_size) return x class Glm4vPatchMerger(nn.Module): def __init__( self, d_model: int, context_dim: int, quant_config: QuantizationConfig | None = None, bias: bool = False, prefix: str = "", ) -> None: super().__init__() use_data_parallel = is_vit_use_data_parallel() self.hidden_size = d_model self.proj = ColumnParallelLinear( self.hidden_size, self.hidden_size, bias=bias, gather_output=True, quant_config=quant_config, prefix=f"{prefix}.proj", disable_tp=use_data_parallel, ) self.post_projection_norm = nn.LayerNorm(self.hidden_size) self.gate_up_proj = MergedColumnParallelLinear( input_size=self.hidden_size, output_sizes=[context_dim] * 2, bias=bias, quant_config=quant_config, prefix=f"{prefix}.gate_up_proj", disable_tp=use_data_parallel, ) self.down_proj = RowParallelLinear( context_dim, self.hidden_size, bias=bias, quant_config=quant_config, prefix=f"{prefix}.down_proj", disable_tp=use_data_parallel, ) self.act_fn = SiluAndMul() self.extra_activation_func = nn.GELU() def forward(self, x: torch.Tensor): x, _ = self.proj(x) x = self.extra_activation_func(self.post_projection_norm(x)) gate_up, _ = self.gate_up_proj(x) x = self.act_fn(gate_up) x, _ = self.down_proj(x) return x class Glm4vVisionEmbeddings(nn.Module): def __init__(self, config: Glm4vVisionConfig): super().__init__() self.config = config self.embed_dim = config.hidden_size self.image_size = config.image_size self.patch_size = config.patch_size self.num_patches = (self.image_size // self.patch_size) ** 2 self.num_positions = self.num_patches self.position_embedding = nn.Embedding(self.num_positions, self.embed_dim) self.register_buffer( "position_ids", torch.arange(self.num_positions).expand((1, -1)), persistent=False, ) def forward( self, embeddings, lengths, image_shapes, h_coords, w_coords ) -> torch.Tensor: pos_embed_weight = self.position_embedding.weight hidden_size = pos_embed_weight.shape[1] total_seq = h_coords.shape[0] device = pos_embed_weight.device # Move coordinates to correct device h_coords, w_coords = h_coords.to(device), w_coords.to(device) # Handle empty sequence case if total_seq == 0: adapted_pos_embed = torch.empty( 0, hidden_size, device=device, dtype=pos_embed_weight.dtype ) else: # Convert inputs to tensors if needed if isinstance(lengths, list): lengths = torch.tensor(lengths, device=device, dtype=torch.long) if not isinstance(image_shapes, torch.Tensor): image_shapes = torch.tensor( image_shapes, device=device, dtype=torch.long ) # Prepare 2D position embedding orig_size_sq = pos_embed_weight.shape[0] orig_size = int(orig_size_sq**0.5) pos_embed_2d = ( pos_embed_weight.view(orig_size, orig_size, hidden_size) .permute(2, 0, 1) .unsqueeze(0) .to(device=device, dtype=torch.float32) ) # Calculate target dimensions for each patch # Add bounds checking for data parallel mode if len(lengths) > image_shapes.shape[0]: # In data parallel mode, some GPUs might not have all # image shapes # Use available image shapes, cycling if necessary target_h_list = [] target_w_list = [] for i in range(len(lengths)): # Cycle through available shapes shape_idx = i % image_shapes.shape[0] target_h_list.append(image_shapes[shape_idx, 1].repeat(lengths[i])) target_w_list.append(image_shapes[shape_idx, 2].repeat(lengths[i])) target_h = torch.cat(target_h_list).to( device=device, dtype=torch.float32 ) target_w = torch.cat(target_w_list).to( device=device, dtype=torch.float32 ) else: target_h = torch.cat( [image_shapes[i, 1].repeat(lengths[i]) for i in range(len(lengths))] ).to(device=device, dtype=torch.float32) target_w = torch.cat( [image_shapes[i, 2].repeat(lengths[i]) for i in range(len(lengths))] ).to(device=device, dtype=torch.float32) # Normalize coordinates to [-1, 1] range for grid_sample h_coords = h_coords.to(device=device, dtype=torch.float32) w_coords = w_coords.to(device=device, dtype=torch.float32) norm_w = ((w_coords + 0.5) / target_w) * 2 - 1 norm_h = ((h_coords + 0.5) / target_h) * 2 - 1 # Create sampling grid grid = torch.stack((norm_w, norm_h), dim=-1).unsqueeze(0).unsqueeze(2) # Perform bicubic interpolation interpolated_embed_fp32 = F.grid_sample( pos_embed_2d, grid, mode="bicubic", align_corners=False, padding_mode="border", ) # Reshape and convert back to original dtype adapted_pos_embed_fp32 = ( interpolated_embed_fp32.squeeze(0).squeeze(-1).permute(1, 0) ) adapted_pos_embed = adapted_pos_embed_fp32.to(pos_embed_weight.dtype).to( embeddings.device ) # Add adapted position encoding to embeddings embeddings = embeddings + adapted_pos_embed return embeddings class Glm4vVisionTransformer(nn.Module): def __init__( self, vision_config: Glm4vVisionConfig, norm_eps: float = 1e-6, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> None: super().__init__() patch_size = vision_config.patch_size temporal_patch_size = vision_config.temporal_patch_size in_channels = vision_config.in_channels depth = vision_config.depth self.hidden_size = vision_config.hidden_size self.num_heads = vision_config.num_heads self.patch_size = vision_config.patch_size self.spatial_merge_size = vision_config.spatial_merge_size self.out_hidden_size = vision_config.out_hidden_size self.patch_embed = Glm4vVisionPatchEmbed( patch_size=patch_size, temporal_patch_size=temporal_patch_size, in_channels=in_channels, hidden_size=self.hidden_size, ) norm_layer = partial(RMSNorm, eps=norm_eps) head_dim = self.hidden_size // self.num_heads self.rotary_pos_emb = get_rope( head_size=head_dim, max_position=8192, is_neox_style=True, rope_parameters={"partial_rotary_factor": 0.5}, ) self.blocks = nn.ModuleList( [ Glm4vVisionBlock( dim=self.hidden_size, num_heads=self.num_heads, mlp_hidden_dim=vision_config.out_hidden_size, norm_layer=norm_layer, quant_config=quant_config, prefix=f"{prefix}.blocks.{layer_idx}", ) for layer_idx in range(depth) ] ) self.merger = Glm4vPatchMerger( d_model=vision_config.out_hidden_size, context_dim=vision_config.intermediate_size, quant_config=quant_config, bias=False, prefix=f"{prefix}.merger", ) self.embeddings = Glm4vVisionEmbeddings(vision_config) self.post_conv_layernorm = RMSNorm( vision_config.hidden_size, eps=vision_config.rms_norm_eps ) self.downsample = Conv2dLayer( in_channels=vision_config.hidden_size, out_channels=vision_config.out_hidden_size, kernel_size=vision_config.spatial_merge_size, stride=vision_config.spatial_merge_size, ) self.post_layernorm = RMSNorm( vision_config.hidden_size, eps=vision_config.rms_norm_eps ) self.attn_backend = get_vit_attn_backend( head_size=head_dim, dtype=torch.get_default_dtype(), ) @property def dtype(self) -> torch.dtype: return self.patch_embed.proj.weight.dtype @property def device(self) -> torch.device: return self.patch_embed.proj.weight.device def rot_pos_emb( self, grid_thw: torch.Tensor ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: pos_ids = [] for t, h, w in grid_thw: hpos_ids = torch.arange(h).unsqueeze(1).expand(-1, w) wpos_ids = torch.arange(w).unsqueeze(0).expand(h, -1) hpos_ids = ( hpos_ids.reshape( h // self.spatial_merge_size, self.spatial_merge_size, w // self.spatial_merge_size, self.spatial_merge_size, ) .permute(0, 2, 1, 3) .flatten() ) wpos_ids = ( wpos_ids.reshape( h // self.spatial_merge_size, self.spatial_merge_size, w // self.spatial_merge_size, self.spatial_merge_size, ) .permute(0, 2, 1, 3) .flatten() ) pos_ids.append(torch.stack([hpos_ids, wpos_ids], dim=-1).repeat(t, 1)) pos_ids = torch.cat(pos_ids, dim=0) max_grid_size = grid_thw[:, 1:].max() # Use pre-computed cos_sin_cache from RotaryEmbedding cos, sin = self.rotary_pos_emb.get_cos_sin(max_grid_size) cos_combined = cos[pos_ids].flatten(1) sin_combined = sin[pos_ids].flatten(1) return cos_combined, sin_combined, pos_ids def compute_attn_mask_seqlen( self, cu_seqlens: torch.Tensor, ) -> torch.Tensor | None: max_seqlen = None if self.attn_backend in { AttentionBackendEnum.FLASH_ATTN, AttentionBackendEnum.ROCM_AITER_FA, AttentionBackendEnum.TRITON_ATTN, }: max_seqlen = (cu_seqlens[1:] - cu_seqlens[:-1]).max() return max_seqlen def forward( self, x: torch.Tensor, grid_thw: torch.Tensor | list[list[int]], ) -> torch.Tensor: if isinstance(grid_thw, list): grid_thw = torch.tensor(grid_thw, dtype=torch.int32) # patchify x = x.to(device=self.device, dtype=self.dtype) x = self.patch_embed(x) x = self.post_conv_layernorm(x) # compute position embedding rotary_pos_emb_cos, rotary_pos_emb_sin, image_type_ids = self.rot_pos_emb( grid_thw ) # compute cu_seqlens cu_seqlens = torch.repeat_interleave( grid_thw[:, 1] * grid_thw[:, 2], grid_thw[:, 0] ).cumsum(dim=0, dtype=torch.int32) cu_seqlens = torch.cat([cu_seqlens.new_zeros(1), cu_seqlens]) cu_seqlens = cu_seqlens.to(self.device, non_blocking=True) # pre-compute max_seqlen for attn mask to reduce cuMemcpy operations max_seqlen = self.compute_attn_mask_seqlen(cu_seqlens) seqlens = (cu_seqlens[1:] - cu_seqlens[:-1]).tolist() x = self.embeddings( x, seqlens, grid_thw, image_type_ids[:, 0], image_type_ids[:, 1] ) # transformers x = x.unsqueeze(1) for blk in self.blocks: x = blk( x, cu_seqlens=cu_seqlens, rotary_pos_emb_cos=rotary_pos_emb_cos, rotary_pos_emb_sin=rotary_pos_emb_sin, max_seqlen=max_seqlen, ) # adapter x = self.post_layernorm(x) x = x.view(-1, self.spatial_merge_size, self.spatial_merge_size, x.shape[-1]) x = x.permute(0, 3, 1, 2) x = self.downsample(x).view(-1, self.out_hidden_size) x = self.merger(x) return x def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: stacked_params_mapping = [ # (param_name, shard_name, shard_id) ("attn.qkv.", "attn.q.", "q"), ("attn.qkv.", "attn.k.", "k"), ("attn.qkv.", "attn.v.", "v"), ("gate_up_proj", "gate_proj", 0), ("gate_up_proj", "up_proj", 1), ] params_dict = dict(self.named_parameters(remove_duplicate=False)) loaded_params: set[str] = set() for name, loaded_weight in weights: for param_name, weight_name, shard_id in stacked_params_mapping: if weight_name not in name: continue name = name.replace(weight_name, param_name) param = params_dict[name] weight_loader = param.weight_loader weight_loader(param, loaded_weight, shard_id) break else: param = params_dict[name] weight_loader = getattr(param, "weight_loader", default_weight_loader) weight_loader(param, loaded_weight) loaded_params.add(name) return loaded_params class Glm4vProcessingInfo(BaseProcessingInfo): def get_supported_mm_limits(self) -> Mapping[str, int | None]: return {"image": None, "video": 1} def get_image_processor(self, **kwargs: object) -> Glm4vImageProcessor: return self.get_hf_processor(**kwargs).image_processor def get_video_processor(self, **kwargs: object) -> Glm4vVideoProcessor: return self.get_hf_processor(**kwargs).video_processor def get_data_parser(self): return MultiModalDataParser( video_needs_metadata=True, expected_hidden_size=self._get_expected_hidden_size(), ) def _get_vision_info( self, *, image_width: int, image_height: int, num_frames: int = 16, do_resize: bool = True, max_image_pixels: int = 28 * 28 * 2 * 30000, ) -> tuple[ImageSize, int]: hf_config = self.get_hf_config() vision_config = hf_config.vision_config patch_size = vision_config.patch_size merge_size = vision_config.spatial_merge_size temporal_patch_size = vision_config.temporal_patch_size if do_resize: resized_height, resized_width = smart_resize( num_frames=num_frames if num_frames > temporal_patch_size else temporal_patch_size, height=image_height, width=image_width, factor=patch_size * merge_size, max_pixels=max_image_pixels, ) preprocessed_size = ImageSize(width=resized_width, height=resized_height) else: preprocessed_size = ImageSize(width=image_width, height=image_height) # NOTE: Frames are padded to be divisible by `temporal_patch_size` # https://github.com/huggingface/transformers/blob/v4.48.3/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py#L294 padded_num_frames = num_frames + num_frames % temporal_patch_size grid_t = max(padded_num_frames // temporal_patch_size, 1) grid_h = preprocessed_size.height // patch_size grid_w = preprocessed_size.width // patch_size num_patches = grid_t * grid_h * grid_w num_vision_tokens = num_patches // (merge_size**2) return preprocessed_size, num_vision_tokens def _get_image_max_pixels(self) -> int: """Read max_pixels from the HF image processor config. Despite the name, ``longest_edge`` is a pixel **area** (total pixel count), not an edge length. The HF processor passes it directly to ``smart_resize`` as the ``max_pixels`` argument, which constrains ``t_bar * h_bar * w_bar <= max_pixels``. """ return self.get_image_processor().size["longest_edge"] def get_image_size_with_most_features(self) -> ImageSize: # Use num_frames=1 for single-image budget estimation. # _get_vision_info defaults to num_frames=16 (video), which # makes smart_resize constrain 16*H*W <= max_pixels, vastly # underestimating the spatial budget for a single image and # causing encoder cache overflow for large images # (see https://github.com/vllm-project/vllm/issues/34040). max_image_size, _ = self._get_vision_info( image_width=9999999, image_height=9999999, num_frames=1, max_image_pixels=self._get_image_max_pixels(), ) return max_image_size def get_num_image_tokens( self, *, image_width: int, image_height: int, ) -> int: _, num_image_tokens = self._get_vision_info( image_width=image_width, image_height=image_height, num_frames=1, max_image_pixels=self._get_image_max_pixels(), ) return num_image_tokens def get_max_image_tokens(self) -> int: target_width, target_height = self.get_image_size_with_most_features() return self.get_num_image_tokens( image_width=target_width, image_height=target_height, ) def get_num_video_tokens( self, *, image_width: int, image_height: int, num_frames: int, ) -> int: _, num_video_tokens = self._get_vision_info( image_width=image_width, image_height=image_height, num_frames=num_frames, max_image_pixels=28 * 28 * 2 * 30000, ) return num_video_tokens def _get_max_video_frames(self, max_tokens: int) -> int: target_width, target_height = self.get_image_size_with_most_features() num_frames = 0 while True: next_num_frames = num_frames + 1 next_max_tokens = self.get_num_video_tokens( image_width=target_width, image_height=target_height, num_frames=next_num_frames, ) if next_max_tokens > max_tokens or next_max_tokens == 0: break num_frames = next_num_frames return num_frames def get_num_frames_with_most_features( self, seq_len: int, mm_counts: Mapping[str, int], ) -> int: max_images = mm_counts.get("image", 0) max_videos = mm_counts.get("video", 0) max_image_tokens = self.get_max_image_tokens() * max_images max_total_frames = self._get_max_video_frames(seq_len - max_image_tokens) max_frames_per_video = min( max_total_frames // max(max_videos, 1), _MAX_FRAMES_PER_VIDEO ) return max(max_frames_per_video, 1) def _get_video_second_idx_glm4v( self, metadata: dict[str, Any], total_frames: int ) -> list[int]: video_processor = self.get_video_processor() video_fps = metadata.get("fps", video_processor.fps) meta_frames = metadata.get("total_num_frames", total_frames) max_frame_idx = meta_frames - 1 duration = metadata.get("duration", round(max_frame_idx / video_fps) + 1) do_sample_frames = metadata["do_sample_frames"] if not do_sample_frames: frame_indices = metadata["frames_indices"] else: if duration <= video_processor.max_duration: n = int(math.floor(duration * video_processor.fps)) frame_indices = [ min( max_frame_idx, int(math.ceil(i * video_fps / video_processor.fps)), ) for i in range(n) ] else: num_samples = int(video_processor.max_duration * video_processor.fps) if num_samples >= meta_frames: frame_indices = list(range(meta_frames)) else: target_seconds = np.linspace( 0, duration, num_samples, endpoint=True ) frame_indices = [ min(max_frame_idx, int(math.ceil(t * video_fps))) for t in target_seconds ] seen, uniq = set(), [] for idx in frame_indices: if idx not in seen: seen.add(idx) uniq.append(idx) if len(uniq) & 1: uniq.append(uniq[-1]) frame_indices = uniq full_second_idxs = [int(idx / video_fps) for idx in frame_indices] timestamps_list = full_second_idxs[::2] selected_timestamps = [] for idx in range(0, len(timestamps_list)): selected_timestamps.append(timestamps_list[idx]) return selected_timestamps def _get_video_second_idx_glm46v( self, metadata: dict[str, Any], total_frames: int ) -> list[int]: video_processor = self.get_video_processor() video_fps = metadata["fps"] meta_frames = metadata.get("total_num_frames", total_frames) max_frame_idx = meta_frames - 1 duration = metadata.get("duration", round(max_frame_idx / video_fps) + 1) do_sample_frames = metadata.get("do_sample_frames", True) if not do_sample_frames: frame_indices = metadata["frames_indices"] else: DYNAMIC_FPS_THRES = {30: 3, 300: 1, 2400: 0.5} MAX_FRAME_COUNT_DYNAMIC = 640 MAX_DURATION = 2400 effective_duration = min(duration, MAX_DURATION) if effective_duration <= 30: target_fps = DYNAMIC_FPS_THRES[30] elif effective_duration <= 300: target_fps = DYNAMIC_FPS_THRES[300] else: target_fps = DYNAMIC_FPS_THRES[2400] temporal_patch_size = getattr(video_processor, "temporal_patch_size", 1) extract_t = int(effective_duration * target_fps * temporal_patch_size) extract_t = min(extract_t, MAX_FRAME_COUNT_DYNAMIC) duration_per_frame = 1 / video_fps timestamps = [i * duration_per_frame for i in range(meta_frames)] max_second = int(duration) if meta_frames < extract_t: frame_indices = np.linspace( 0, meta_frames - 1, extract_t, dtype=int ).tolist() else: frame_indices = [] current_second = 0.0 inv_fps = 1 / (temporal_patch_size * target_fps) for frame_index in range(meta_frames): if timestamps[frame_index] >= current_second: current_second += inv_fps frame_indices.append(frame_index) if current_second >= max_second: break if len(frame_indices) < extract_t: if len(frame_indices) == 0: start, end = 0, max(meta_frames - 1, 0) else: start, end = frame_indices[0], frame_indices[-1] frame_indices = np.linspace(start, end, extract_t, dtype=int).tolist() elif len(frame_indices) > extract_t: frame_indices = np.linspace( 0, meta_frames - 1, extract_t, dtype=int ).tolist() seen, uniq = set(), [] for idx in frame_indices: if idx not in seen: seen.add(idx) uniq.append(idx) if len(uniq) & 1: uniq.append(uniq[-1]) frame_indices = uniq full_second_idxs = [int(idx / video_fps) for idx in frame_indices] timestamps_list = full_second_idxs[::2] selected_timestamps = [] for idx in range(len(timestamps_list)): selected_timestamps.append(timestamps_list[idx]) return selected_timestamps def _construct_video_placeholder( self, video_array: np.ndarray, metadata: dict[str, Any], grid_thw: torch.Tensor, ) -> str: hf_processor = self.get_hf_processor() tokenizer = self.get_tokenizer() image_processor = hf_processor.image_processor hf_config = self.get_hf_config() boi_token_id = hf_config.image_start_token_id eoi_token_id = hf_config.image_end_token_id bov_token_id = hf_config.video_start_token_id eov_token_id = hf_config.video_end_token_id merge_length = image_processor.merge_size**2 assert isinstance(grid_thw, torch.Tensor) timestamps = ( self._get_video_second_idx_glm4v(metadata, len(video_array)) if isinstance(hf_processor, Glm4vProcessor) else self._get_video_second_idx_glm46v(metadata, len(video_array)) ) timestamp_format = ( "{}" if isinstance(hf_processor, Glm4vProcessor) else "{:.1f} seconds" ) frames_idx_token = [ tokenizer.encode(timestamp_format.format(i), add_special_tokens=False) for i in timestamps ] T, H, W = grid_thw num_tokens_per_frame = int(H * W) // merge_length placeholder = [] placeholder.append(bov_token_id) for frame_idx in frames_idx_token: placeholder.append(boi_token_id) placeholder.extend([hf_processor.video_token_id] * num_tokens_per_frame) placeholder.append(eoi_token_id) placeholder.extend(frame_idx) placeholder.append(eov_token_id) return placeholder class Glm4vDummyInputsBuilder(BaseDummyInputsBuilder[Glm4vProcessingInfo]): def get_dummy_text(self, mm_counts: Mapping[str, int]) -> str: num_images = mm_counts.get("image", 0) num_videos = mm_counts.get("video", 0) hf_config = self.info.get_hf_config() hf_processor = self.info.get_hf_processor() tokenizer = self.info.get_tokenizer() image_token: str = hf_processor.image_token video_token_ids = [ hf_config.video_start_token_id, hf_processor.video_token_id, hf_config.video_end_token_id, ] video_token = tokenizer.decode(video_token_ids) return image_token * num_images + video_token * num_videos def get_dummy_mm_data( self, seq_len: int, mm_counts: Mapping[str, int], mm_options: Mapping[str, BaseDummyOptions], ) -> MultiModalDataDict: num_images = mm_counts.get("image", 0) num_videos = mm_counts.get("video", 0) target_width, target_height = self.info.get_image_size_with_most_features() target_num_frames = self.info.get_num_frames_with_most_features( seq_len, mm_counts ) image_overrides = mm_options.get("image") video_overrides = mm_options.get("video") return { "image": self._get_dummy_images( width=target_width, height=target_height, num_images=num_images, overrides=image_overrides, ), "video": self._get_dummy_videos( width=target_width, height=target_height, num_frames=target_num_frames, num_videos=num_videos, overrides=video_overrides, ), } def _get_dummy_videos( self, *, width: int, height: int, num_frames: int, num_videos: int, overrides: VideoDummyOptions | None = None, ) -> list[VideoItem]: if overrides: if overrides.num_frames: if overrides.num_frames > num_frames: logger.warning( "video.num_frames override (%d) exceeds model's " "maximum number of frames (%d), will be ignored", overrides.num_frames, num_frames, ) num_frames = min(num_frames, overrides.num_frames) if overrides.width: if overrides.width > width: logger.warning( "video.width override (%d) exceeds model's " "maximum width (%d), will be ignored", overrides.width, width, ) width = min(width, overrides.width) if overrides.height: if overrides.height > height: logger.warning( "video.height override (%d) exceeds model's " "maximum height (%d), will be ignored", overrides.height, height, ) height = min(height, overrides.height) num_frames = max(num_frames, 2) # GLM 4.6V requires 2 frames video = np.full((num_frames, width, height, 3), 255, dtype=np.uint8) video_items = [] for i in range(num_videos): video_metadata = { "fps": 2.0, "duration": num_frames / 2.0, "total_num_frames": num_frames, "frames_indices": [i for i in range(num_frames)], "video_backend": "opencv", "do_sample_frames": False, } video_item = (video.copy(), video_metadata) video_items.append(video_item) return video_items class Glm4vMultiModalProcessor(BaseMultiModalProcessor[Glm4vProcessingInfo]): def _call_hf_processor( self, prompt: str, mm_data: Mapping[str, object], mm_kwargs: Mapping[str, object], tok_kwargs: Mapping[str, object], ) -> BatchFeature: mm_data = dict(mm_data) processor = self.info.get_hf_processor(**mm_kwargs) # GLM-4.1V use `image_token_id` as video placeholder, we need to # replace it with `video_token_id` for video processing. So we # separate video processing from image processing. if ( "videos" in mm_data and isinstance(mm_data["videos"], list) and len(mm_data["videos"]) > 0 ): video_grid_thw_lst = [] pixel_values_videos_lst = [] for item in mm_data.pop("videos", []): video_array, metadata = item # don't update mm_kwargs inplace video_mm_kwargs = dict(**mm_kwargs) video_mm_kwargs["do_sample_frames"] = metadata.get( "do_sample_frames", True ) video_mm_data = dict() video_mm_data["videos"] = [[video_array]] unuse_metadata = ["do_sample_frames"] video_mm_data["video_metadata"] = [ [ VideoMetadata( **{ k: metadata[k] for k in metadata if k not in unuse_metadata } ) ] ] video_outputs = super()._call_hf_processor( prompt="<|begin_of_video|><|video|><|end_of_video|>", mm_data=video_mm_data, mm_kwargs=video_mm_kwargs, tok_kwargs=tok_kwargs, ) input_ids = video_outputs.pop("input_ids") input_ids[input_ids == processor.image_token_id] = ( processor.video_token_id ) video_placeholder = processor.tokenizer.batch_decode(input_ids)[0] prompt = prompt.replace( "<|begin_of_video|><|video|><|end_of_video|>", video_placeholder, 1, ) video_grid_thw_lst.append(video_outputs["video_grid_thw"]) pixel_values_videos_lst.append(video_outputs["pixel_values_videos"]) video_outputs = dict( pixel_values_videos=torch.cat(pixel_values_videos_lst), video_grid_thw=torch.cat(video_grid_thw_lst), ) else: video_outputs = dict() processed_outputs = super()._call_hf_processor( prompt=prompt, mm_data=mm_data, mm_kwargs=mm_kwargs, tok_kwargs=tok_kwargs, ) combined_outputs = dict( processed_outputs, **video_outputs, ) return BatchFeature(combined_outputs) def _get_mm_fields_config( self, hf_inputs: BatchFeature, hf_processor_mm_kwargs: Mapping[str, object], ) -> Mapping[str, MultiModalFieldConfig]: return _create_qwen2vl_field_factory( self.info.get_hf_config().vision_config.spatial_merge_size )(hf_inputs) def _get_prompt_updates( self, mm_items: MultiModalDataItems, hf_processor_mm_kwargs: Mapping[str, Any], out_mm_kwargs: MultiModalKwargsItems, ) -> Sequence[PromptUpdate]: hf_processor = self.info.get_hf_processor(**hf_processor_mm_kwargs) image_processor = self.info.get_image_processor(**hf_processor_mm_kwargs) merge_length = image_processor.merge_size**2 def get_image_replacement_glm4v(item_idx: int): out_item = out_mm_kwargs["image"][item_idx] grid_thw = out_item["image_grid_thw"].data assert isinstance(grid_thw, torch.Tensor) num_tokens = int(grid_thw.prod()) // merge_length return [hf_processor.image_token_id] * num_tokens def get_video_replacement_glm4v(item_idx: int): out_item = out_mm_kwargs["video"][item_idx] grid_thw = out_item["video_grid_thw"].data assert isinstance(grid_thw, torch.Tensor) video, metadata = mm_items["video"][item_idx] placeholder = self.info._construct_video_placeholder( video, metadata, grid_thw ) return PromptUpdateDetails.select_token_id( placeholder, embed_token_id=hf_processor.video_token_id, ) return [ PromptReplacement( modality="image", target=hf_processor.image_token, replacement=get_image_replacement_glm4v, ), PromptReplacement( modality="video", target="<|begin_of_video|><|video|><|end_of_video|>", replacement=get_video_replacement_glm4v, ), ] @MULTIMODAL_REGISTRY.register_processor( Glm4vMultiModalProcessor, info=Glm4vProcessingInfo, dummy_inputs=Glm4vDummyInputsBuilder, ) class Glm4vForConditionalGeneration( nn.Module, SupportsMultiModal, SupportsLoRA, SupportsPP, SupportsMRoPE ): packed_modules_mapping = { "qkv_proj": [ "q_proj", "k_proj", "v_proj", ], "gate_up_proj": ["gate_up_proj"], } # To ensure correct weight loading and mapping. hf_to_vllm_mapper = WeightsMapper( orig_to_new_prefix={ "lm_head.": "language_model.lm_head.", "model.language_model.": "language_model.model.", "model.visual.": "visual.", } ) supports_encoder_tp_data = True @classmethod def get_placeholder_str(cls, modality: str, i: int) -> str | None: if modality.startswith("image"): return "<|begin_of_image|><|image|><|end_of_image|>" if modality.startswith("video"): return "<|begin_of_video|><|video|><|end_of_video|>" raise ValueError("Only image or video modality is supported") def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__() config = vllm_config.model_config.hf_config quant_config = vllm_config.quant_config multimodal_config = vllm_config.model_config.multimodal_config self.config = config self.multimodal_config = multimodal_config self.use_data_parallel = multimodal_config.mm_encoder_tp_mode == "data" with self._mark_tower_model(vllm_config, {"image", "video"}): self.visual = Glm4vVisionTransformer( config.vision_config, norm_eps=getattr(config, "rms_norm_eps", 1e-5), quant_config=quant_config, prefix=maybe_prefix(prefix, "visual"), ) if config.model_type in ("glm4v", "glm_ocr"): architectures = ["Glm4ForCausalLM"] elif config.model_type == "glm4v_moe": architectures = ["Glm4MoeForCausalLM"] else: architectures = None with self._mark_language_model(vllm_config): self.language_model = init_vllm_registered_model( vllm_config=vllm_config, hf_config=config.text_config, prefix=maybe_prefix(prefix, "language_model"), architectures=architectures, ) self.make_empty_intermediate_tensors = ( self.language_model.make_empty_intermediate_tensors ) def _parse_and_validate_image_input( self, **kwargs: object ) -> Glm4vImageInputs | None: pixel_values = kwargs.pop("pixel_values", None) image_embeds = kwargs.pop("image_embeds", None) image_grid_thw = kwargs.pop("image_grid_thw", None) if pixel_values is None and image_embeds is None: return None if pixel_values is not None: return Glm4vImagePixelInputs( type="pixel_values", pixel_values=pixel_values, image_grid_thw=image_grid_thw, ) if image_embeds is not None: return Glm4vImageEmbeddingInputs( type="image_embeds", image_embeds=image_embeds, image_grid_thw=image_grid_thw, ) def _parse_and_validate_video_input( self, **kwargs: object ) -> Glm4vVideoInputs | None: pixel_values_videos = kwargs.pop("pixel_values_videos", None) video_embeds = kwargs.pop("video_embeds", None) video_grid_thw = kwargs.pop("video_grid_thw", None) if pixel_values_videos is None and video_embeds is None: return None if pixel_values_videos is not None: return Glm4vVideoPixelInputs( type="pixel_values_videos", pixel_values_videos=pixel_values_videos, video_grid_thw=video_grid_thw, ) if video_embeds is not None: return Glm4vVideoEmbeddingInputs( type="video_embeds", video_embeds=video_embeds, video_grid_thw=video_grid_thw, ) def _process_image_input( self, image_input: Glm4vImageInputs ) -> tuple[torch.Tensor, ...]: grid_thw = image_input["image_grid_thw"] assert grid_thw.ndim == 2 if image_input["type"] == "image_embeds": image_embeds = image_input["image_embeds"].type(self.visual.dtype) else: pixel_values = image_input["pixel_values"].type(self.visual.dtype) if self.use_data_parallel: return run_dp_sharded_mrope_vision_model( self.visual, pixel_values, grid_thw.tolist(), rope_type="rope_3d" ) else: image_embeds = self.visual(pixel_values, grid_thw=grid_thw) merge_size = self.visual.spatial_merge_size sizes = (grid_thw.prod(-1) // merge_size // merge_size).tolist() return image_embeds.split(sizes) def _process_video_input( self, video_input: Glm4vVideoInputs ) -> tuple[torch.Tensor, ...]: grid_thw = video_input["video_grid_thw"] assert grid_thw.ndim == 2 if video_input["type"] == "video_embeds": video_embeds = video_input["video_embeds"].type(self.visual.dtype) else: pixel_values_videos = video_input["pixel_values_videos"].type( self.visual.dtype ) if self.use_data_parallel: return run_dp_sharded_mrope_vision_model( self.visual, pixel_values_videos, grid_thw.tolist(), rope_type="rope_3d", ) else: video_embeds = self.visual(pixel_values_videos, grid_thw=grid_thw) # Split concatenated embeddings for each video item. merge_size = self.visual.spatial_merge_size sizes = (grid_thw.prod(-1) // merge_size // merge_size).tolist() return video_embeds.split(sizes) def _parse_and_validate_multimodal_inputs(self, **kwargs: object) -> dict: mm_input_by_modality = {} # Preserve the order of modalities if there are multiple of them # from the order of kwargs. for input_key in kwargs: if ( input_key in ("pixel_values", "image_embeds") and "image" not in mm_input_by_modality ): mm_input_by_modality["image"] = self._parse_and_validate_image_input( **kwargs ) if ( input_key in ("pixel_values_videos", "video_embeds") and "video" not in mm_input_by_modality ): mm_input_by_modality["video"] = self._parse_and_validate_video_input( **kwargs ) return mm_input_by_modality def embed_multimodal(self, **kwargs: object) -> MultiModalEmbeddings | None: mm_input_by_modality = self._parse_and_validate_multimodal_inputs(**kwargs) if not mm_input_by_modality: return None # The result multimodal_embeddings is tuple of tensors, with each # tensor corresponding to a multimodal data item (image or video). multimodal_embeddings: tuple[torch.Tensor, ...] = () # NOTE: It is important to iterate over the keys in this dictionary # to preserve the order of the modalities. for modality in mm_input_by_modality: multimodal_input = mm_input_by_modality[modality] if modality == "image": image_embeddings = self._process_image_input(multimodal_input) multimodal_embeddings += tuple(image_embeddings) if modality == "video": video_embeddings = self._process_video_input(multimodal_input) multimodal_embeddings += tuple(video_embeddings) return multimodal_embeddings def iter_mm_grid_thw( self, mm_features: list[MultiModalFeatureSpec] ) -> Iterator[tuple[int, int, int, int]]: hf_config = self.config spatial_merge_size = hf_config.vision_config.spatial_merge_size for mm_feature in sorted(mm_features, key=lambda f: f.mm_position.offset): offset = mm_feature.mm_position.offset if mm_feature.modality == "image": t, h, w = mm_feature.data["image_grid_thw"].data.tolist() assert t == 1, f"Image must have 1 frame, got {t}" yield offset, t, h // spatial_merge_size, w // spatial_merge_size elif mm_feature.modality == "video": t, h, w = mm_feature.data["video_grid_thw"].data.tolist() yield ( offset, t, h // spatial_merge_size, w // spatial_merge_size, ) else: raise ValueError(f"Unsupported modality: {mm_feature.modality}") def get_mrope_input_positions( self, input_tokens: list[int], mm_features: list[MultiModalFeatureSpec], ) -> tuple[torch.Tensor, int]: llm_pos_ids_list: list = [] st = 0 for ( offset, llm_grid_t, llm_grid_h, llm_grid_w, ) in self.iter_mm_grid_thw(mm_features): text_len = offset - st st_idx = llm_pos_ids_list[-1].max() + 1 if len(llm_pos_ids_list) > 0 else 0 llm_pos_ids_list.append( np.broadcast_to(np.arange(text_len), (3, text_len)) + st_idx ) grid_indices = np.indices((llm_grid_t, llm_grid_h, llm_grid_w)).reshape( 3, -1 ) llm_pos_ids_list.append(grid_indices + text_len + st_idx) st = offset + llm_grid_t * llm_grid_h * llm_grid_w if st < len(input_tokens): text_len = len(input_tokens) - st st_idx = llm_pos_ids_list[-1].max() + 1 if len(llm_pos_ids_list) > 0 else 0 llm_pos_ids_list.append( np.broadcast_to(np.arange(text_len), (3, text_len)) + st_idx ) llm_positions = np.concatenate(llm_pos_ids_list, axis=1).reshape(3, -1) mrope_position_delta = (llm_positions.max() + 1 - len(input_tokens)).item() return torch.from_numpy(llm_positions), mrope_position_delta def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, **kwargs: object, ) -> torch.Tensor | IntermediateTensors: """Run forward pass for GLM-4V. Args: input_ids: Flattened (concatenated) input_ids corresponding to a batch. positions: Flattened (concatenated) position ids corresponding to a batch. **NOTE**: If mrope is enabled (default setting for GLM-4V opensource models), the shape will be `(3, seq_len)`, otherwise it will be `(seq_len,). intermediate_tensors: Optional intermediate tensors for pipeline parallelism. inputs_embeds: Optional pre-computed input embeddings. **kwargs: Additional keyword arguments. """ if intermediate_tensors is not None: inputs_embeds = None hidden_states = self.language_model.model( input_ids=input_ids, positions=positions, intermediate_tensors=intermediate_tensors, inputs_embeds=inputs_embeds, ) return hidden_states def compute_logits( self, hidden_states: torch.Tensor, ) -> torch.Tensor | None: return self.language_model.compute_logits(hidden_states) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: loader = AutoWeightsLoader(self) return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper) def get_mm_mapping(self) -> MultiModelKeys: """ Get the module prefix in multimodal models """ return MultiModelKeys.from_string_field( language_model="language_model.model", connector="visual.merger.", tower_model="visual.", ) def get_num_mm_encoder_tokens( self, num_image_tokens: int, ) -> int: merge_size = self.config.vision_config.spatial_merge_size return num_image_tokens * (merge_size**2) def get_num_mm_connector_tokens( self, num_vision_tokens: int, ) -> int: merge_size = self.config.vision_config.spatial_merge_size return num_vision_tokens // (merge_size**2) @MULTIMODAL_REGISTRY.register_processor( Glm4vMultiModalProcessor, info=Glm4vProcessingInfo, dummy_inputs=Glm4vDummyInputsBuilder, ) class Glm4vMoeForConditionalGeneration(Glm4vForConditionalGeneration): packed_modules_mapping = { "qkv_proj": [ "q_proj", "k_proj", "v_proj", ], "gate_up_proj": [ "gate_proj", "up_proj", ], }
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/glm4_1v.py", "license": "Apache License 2.0", "lines": 1529, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/tools/test_config_validator.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import ast import pytest from tools.pre_commit.validate_config import validate_ast _TestConfig1 = ''' @config class _TestConfig1: a: int """docstring""" ''' _TestConfig2 = """ @config class _TestConfig2: a: int = 1 """ _TestConfig3 = ''' @config class _TestConfig3: a: Union[Literal[1], Literal[2]] = 1 """docstring""" ''' @pytest.mark.parametrize( ("test_config", "expected_error"), [ (_TestConfig1, "must have a default"), (_TestConfig2, "must have a docstring"), (_TestConfig3, "must use a single Literal"), ], ) def test_config(test_config, expected_error): tree = ast.parse(test_config) with pytest.raises(Exception, match=expected_error): validate_ast(tree)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/tools/test_config_validator.py", "license": "Apache License 2.0", "lines": 34, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/model_executor/layers/fused_moe/cpu_fused_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import weakref from collections.abc import Callable import torch from torch.nn import functional as F from vllm import _custom_ops as ops from vllm._custom_ops import cpu_fused_moe, cpu_prepack_moe_weight from vllm.model_executor.layers.activation import SiluAndMul from vllm.model_executor.layers.fused_moe.activation import MoEActivation from vllm.model_executor.layers.quantization.utils.layer_utils import replace_parameter from vllm.utils.torch_utils import direct_register_custom_op _CPU_MOE_LAYER_CACHE = {} def _swigluoai_forward_native( x: torch.Tensor, alpha: float = 1.702, limit: float = 7.0, ) -> torch.Tensor: """PyTorch-native implementation of SwigluOAIAndMul.forward_native. Standalone function to avoid instantiating SwigluOAIAndMul (a CustomOp) which would trigger get_current_vllm_config() before config is set. """ gate, up = x[..., ::2], x[..., 1::2] gate = gate.clamp(min=None, max=limit) up = up.clamp(min=-limit, max=limit) glu = gate * torch.sigmoid(gate * alpha) gated_output = (up + 1) * glu return gated_output # Map activation names to their native forward functions. # Uses static methods or standalone functions to avoid instantiating CustomOp # classes, which would call get_current_vllm_config() before config is set. _CPU_MOE_ACT_FN: dict[MoEActivation, Callable[[torch.Tensor], torch.Tensor]] = { MoEActivation.SILU: SiluAndMul.forward_native, MoEActivation.SWIGLUOAI: _swigluoai_forward_native, } def grouped_topk( hidden_states: torch.Tensor, gating_output: torch.Tensor, topk: int, renormalize: bool, num_expert_group: int = 0, topk_group: int = 0, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: assert hidden_states.shape[0] == gating_output.shape[0], "Number of tokens mismatch" gating_output = gating_output.float() if scoring_func == "softmax": scores = torch.softmax(gating_output, dim=-1) elif scoring_func == "sigmoid": scores = gating_output.sigmoid() else: raise ValueError(f"Unsupported scoring function: {scoring_func}") num_token = scores.shape[0] if e_score_correction_bias is not None: original_scores = scores scores = scores + e_score_correction_bias.unsqueeze(0) group_scores = ( scores.view(num_token, num_expert_group, -1).topk(2, dim=-1)[0].sum(dim=-1) ) else: group_scores = ( scores.view(num_token, num_expert_group, -1).max(dim=-1).values ) # [n, n_group] group_idx = torch.topk(group_scores, k=topk_group, dim=-1, sorted=False)[ 1 ] # [n, top_k_group] group_mask = torch.zeros_like(group_scores) # [n, n_group] group_mask.scatter_(1, group_idx, 1) # [n, n_group] score_mask = ( group_mask.unsqueeze(-1) .expand(num_token, num_expert_group, scores.shape[-1] // num_expert_group) .reshape(num_token, -1) ) # [n, e] tmp_scores = scores.masked_fill(~score_mask.bool(), float("-inf")) # [n, e] if e_score_correction_bias is not None: topk_ids = torch.topk(tmp_scores, k=topk, dim=-1, sorted=False)[1] topk_weights = original_scores.gather(1, topk_ids) else: topk_weights, topk_ids = torch.topk(tmp_scores, k=topk, dim=-1, sorted=False) if renormalize: topk_weights = topk_weights / topk_weights.sum(dim=-1, keepdim=True) if routed_scaling_factor != 1.0: topk_weights = topk_weights * routed_scaling_factor return topk_weights, topk_ids.to(torch.int32) def select_experts( hidden_states: torch.Tensor, router_logits: torch.Tensor, top_k: int, use_grouped_topk: bool, renormalize: bool, topk_group: int | None = None, num_expert_group: int | None = None, custom_routing_function: Callable | None = None, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: if use_grouped_topk: assert topk_group is not None assert num_expert_group is not None return grouped_topk( hidden_states=hidden_states, gating_output=router_logits, topk=top_k, renormalize=renormalize, num_expert_group=num_expert_group, topk_group=topk_group, scoring_func=scoring_func, routed_scaling_factor=routed_scaling_factor, e_score_correction_bias=e_score_correction_bias, ) elif custom_routing_function is None: assert scoring_func == "softmax" topk_logit_vals, topk_idx = torch.topk( router_logits, k=top_k, dim=-1, sorted=False ) if renormalize: topk_vals = torch.softmax(topk_logit_vals, dim=-1) else: logZ = torch.logsumexp(router_logits, dim=-1, keepdim=True) topk_vals = (topk_logit_vals - logZ).exp() return topk_vals.to(torch.float32), topk_idx.to(torch.int32) else: return custom_routing_function( hidden_states=hidden_states, gating_output=router_logits, topk=top_k, renormalize=renormalize, ) class SGLFusedMOE: def __init__(self, layer: torch.nn.Module) -> None: pass def __call__( self, layer: torch.nn.Module, x: torch.Tensor, use_grouped_topk: bool, top_k: int, router_logits: torch.Tensor, renormalize: bool, topk_group: int | None = None, num_expert_group: int | None = None, global_num_experts: int = -1, expert_map: torch.Tensor | None = None, custom_routing_function: Callable | None = None, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, apply_router_weight_on_input: bool = False, activation: MoEActivation = MoEActivation.SILU, ) -> torch.Tensor: assert activation == MoEActivation.SILU, f"{activation} is not supported." assert not apply_router_weight_on_input topk_weights, topk_ids = select_experts( hidden_states=x, router_logits=router_logits, use_grouped_topk=use_grouped_topk, top_k=top_k, renormalize=renormalize, topk_group=topk_group, num_expert_group=num_expert_group, custom_routing_function=custom_routing_function, scoring_func=scoring_func, routed_scaling_factor=routed_scaling_factor, e_score_correction_bias=e_score_correction_bias, ) torch.ops._C.fused_experts_cpu( x, layer.w13_weight, layer.w2_weight, topk_weights, topk_ids, True, False, False, None, None, None, None, None, True, ) return x class CPUFusedMOE: """CPU-based fused MoE implementation.""" def __init__(self, layer: torch.nn.Module) -> None: use_grouped_gemm, isa = self.check_grouped_gemm(layer) self.isa = isa if use_grouped_gemm: self.forward_method = self.forward_grouped_gemm self.init_moe_grouped_gemm(layer=layer) else: self.forward_method = self.forward_torch self.init_moe_torch(layer=layer) def __call__( self, layer: torch.nn.Module, x: torch.Tensor, use_grouped_topk: bool, top_k: int, router_logits: torch.Tensor, renormalize: bool, topk_group: int | None = None, num_expert_group: int | None = None, global_num_experts: int = -1, expert_map: torch.Tensor | None = None, custom_routing_function: Callable | None = None, scoring_func: str = "softmax", routed_scaling_factor: float = 1.0, e_score_correction_bias: torch.Tensor | None = None, apply_router_weight_on_input: bool = False, activation: MoEActivation = MoEActivation.SILU, ) -> torch.Tensor: assert activation in _CPU_MOE_ACT_FN, f"{activation} is not supported." topk_weights, topk_ids = select_experts( hidden_states=x, router_logits=router_logits, use_grouped_topk=use_grouped_topk, top_k=top_k, renormalize=renormalize, topk_group=topk_group, num_expert_group=num_expert_group, custom_routing_function=custom_routing_function, scoring_func=scoring_func, routed_scaling_factor=routed_scaling_factor, e_score_correction_bias=e_score_correction_bias, ) return self.forward_method( layer, x, topk_weights, topk_ids, activation, global_num_experts, apply_router_weight_on_input, ) def check_grouped_gemm( self, layer: torch.nn.Module, ) -> tuple[bool, str]: if not hasattr(torch.ops._C, "prepack_moe_weight"): return False, "none" dtype = layer.w13_weight.dtype w13_input_size = layer.w13_weight.size(2) w13_output_size = layer.w13_weight.size(1) w2_input_size = layer.w2_weight.size(2) w2_output_size = layer.w2_weight.size(1) if not (w13_output_size % 32 == 0 and w2_output_size % 32 == 0): return False, "none" supports_amx = torch._C._cpu._is_amx_tile_supported() if ( supports_amx and dtype == torch.bfloat16 and w13_input_size % 32 == 0 and w2_input_size % 32 == 0 ): return True, "amx" if supports_amx: return False, "none" return True, "vec" def init_moe_grouped_gemm( self, layer: torch.nn.Module, ) -> None: new_w13 = cpu_prepack_moe_weight(layer.w13_weight, self.isa) replace_parameter(layer, "w13_weight", new_w13) new_w2 = cpu_prepack_moe_weight(layer.w2_weight, self.isa) replace_parameter(layer, "w2_weight", new_w2) def init_moe_torch( self, layer: torch.nn.Module, ) -> None: use_onednn_mm = ops._supports_onednn and ops.is_onednn_acl_supported() num_experts = layer.w13_weight.size(0) has_w13_bias = hasattr(layer, "w13_bias") has_w2_bias = hasattr(layer, "w2_bias") layer.gate_up_linear = [] layer.down_linear = [] for i in range(num_experts): layer_w13_weight = layer.w13_weight[i] layer_w13_bias = layer.w13_bias[i] if has_w13_bias else None layer_w2_weight = layer.w2_weight[i] layer_w2_bias = layer.w2_bias[i] if has_w2_bias else None if use_onednn_mm: gate_up_handle = ops.create_onednn_mm(layer_w13_weight.t(), 32) layer.gate_up_linear.append( lambda x, handle=gate_up_handle, bias=layer_w13_bias: ops.onednn_mm( handle, x, bias ) ) down_handle = ops.create_onednn_mm(layer_w2_weight.t(), 32) layer.down_linear.append( lambda x, handle=down_handle, bias=layer_w2_bias: ops.onednn_mm( handle, x, bias ) ) else: layer.gate_up_linear.append( lambda x, w=layer_w13_weight, b=layer_w13_bias: F.linear(x, w, b) ) layer.down_linear.append( lambda x, w=layer_w2_weight, b=layer_w2_bias: F.linear(x, w, b) ) if use_onednn_mm: # remove weight layer.w13_weight = torch.nn.Parameter(torch.empty(0), requires_grad=False) layer.w2_weight = torch.nn.Parameter(torch.empty(0), requires_grad=False) _CPU_MOE_LAYER_CACHE[id(layer)] = weakref.ref(layer) def forward_grouped_gemm( self, layer: torch.nn.Module, input: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: MoEActivation, global_num_experts: int = -1, skip_weighted: bool = False, ) -> torch.Tensor: if skip_weighted: assert topk_ids.size(1) == 1, ( "apply_router_weight_on_input is only implemented for topk=1" ) input.mul_(topk_weights.to(input.dtype)) output = cpu_fused_moe( input, layer.w13_weight, layer.w2_weight, getattr(layer, "w13_bias", None), getattr(layer, "w2_bias", None), topk_weights, topk_ids, activation.value, self.isa, skip_weighted, ) return output def forward_torch( self, layer: torch.nn.Module, input: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: MoEActivation, global_num_experts: int = -1, skip_weighted: bool = False, ) -> torch.Tensor: if skip_weighted: assert topk_ids.size(1) == 1, ( "apply_router_weight_on_input is only implemented for topk=1" ) input.mul_(topk_weights.to(input.dtype)) output = torch.empty_like(input) layer_id = id(layer) torch.ops.vllm.cpu_fused_moe_torch( layer_id, output, input, topk_weights, topk_ids, activation.value, global_num_experts, skip_weighted, ) return output def cpu_fused_moe_torch( layer_id: int, output: torch.Tensor, input: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: str, global_num_experts: int = -1, skip_weighted: bool = False, ) -> None: act = MoEActivation.from_str(activation) layer = _CPU_MOE_LAYER_CACHE[layer_id]() # Ref code from https://github.com/sgl-project/sglang/blob/716e682721397df103f347d22da8bd46c6016dab/python/sglang/srt/layers/moe/fused_moe_native.py#L53 len_experts = global_num_experts cnts = topk_ids.new_zeros((topk_ids.shape[0], len_experts)) cnts.scatter_(1, topk_ids.to(torch.int64), 1) tokens_per_expert = cnts.sum(dim=0) idxs = topk_ids.view(-1).argsort() sorted_tokens = input[idxs // topk_ids.shape[1]] tokens_per_expert = tokens_per_expert.cpu().numpy() outputs = [] start_idx = 0 for i, num_tokens in enumerate(tokens_per_expert): end_idx = start_idx + num_tokens if num_tokens == 0: continue tokens_for_this_expert = sorted_tokens[start_idx:end_idx] gate_up = layer.gate_up_linear[i](tokens_for_this_expert) # type: ignore gate_up = _CPU_MOE_ACT_FN[act](gate_up) expert_out = layer.down_linear[i](gate_up) # type: ignore outputs.append(expert_out) start_idx = end_idx outs = torch.cat(outputs, dim=0) if len(outputs) else sorted_tokens.new_empty(0) new_x = torch.empty_like(outs) new_x[idxs] = outs if skip_weighted: final_out = new_x else: final_out = ( new_x.view(*topk_ids.shape, -1) .type(topk_weights.dtype) .mul_(topk_weights.unsqueeze(dim=-1)) .sum(dim=1) .type(new_x.dtype) ) output.copy_(final_out) direct_register_custom_op( op_name="cpu_fused_moe_torch", op_func=cpu_fused_moe_torch, mutates_args=["output"], )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/fused_moe/cpu_fused_moe.py", "license": "Apache License 2.0", "lines": 416, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/kernels/moe/test_deepgemm.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Unit-test DeepGEMM FP8 kernels (no DeepEP). Compare DeepGEMM path against the Triton fallback inside vLLM's fused_experts. """ import importlib import math import pytest import torch # vLLM fused-expert reference (Triton fallback + DeepGEMM option) import vllm.model_executor.layers.fused_moe.modular_kernel as mk from tests.kernels.moe.utils import make_dummy_moe_config from vllm.model_executor.layers.fused_moe.config import ( fp8_w8a8_moe_quant_config, ) from vllm.model_executor.layers.fused_moe.fused_moe import fused_experts from vllm.model_executor.layers.fused_moe.prepare_finalize import ( MoEPrepareAndFinalizeNoEP, ) from vllm.model_executor.layers.fused_moe.triton_deep_gemm_moe import ( TritonOrDeepGemmExperts, ) from vllm.model_executor.layers.quantization.utils.fp8_utils import ( per_token_group_quant_fp8, ) from vllm.utils.deep_gemm import ( calc_diff, is_deep_gemm_supported, per_block_cast_to_fp8, ) BLOCK_SIZE = [128, 128] def make_block_quant_fp8_weights( e: int, n: int, k: int, block_size: list[int], ): """ Generate (w1, w2) expert weights and their per-block scale tensors in FP8 block-quantized format. w1 shape: (E, 2N, K) w2 shape: (E, K, N) """ dtype = torch.bfloat16 fp8_max, fp8_min = ( torch.finfo(torch.float8_e4m3fn).max, torch.finfo(torch.float8_e4m3fn).min, ) # bf16 reference weights w1_bf16 = torch.randn(e, 2 * n, k, device="cuda", dtype=dtype) / 10 w2_bf16 = torch.randn(e, k, n, device="cuda", dtype=dtype) / 10 w1_bf16.clamp_(fp8_min, fp8_max) w2_bf16.clamp_(fp8_min, fp8_max) block_n, block_k = block_size n_tiles_w1 = math.ceil((2 * n) / block_n) k_tiles_w1 = math.ceil(k / block_k) n_tiles_w2 = math.ceil(k / block_n) k_tiles_w2 = math.ceil(n / block_k) w1 = torch.empty_like(w1_bf16, dtype=torch.float8_e4m3fn) w2 = torch.empty_like(w2_bf16, dtype=torch.float8_e4m3fn) w1_s = torch.empty(e, n_tiles_w1, k_tiles_w1, device="cuda", dtype=torch.float32) w2_s = torch.empty(e, n_tiles_w2, k_tiles_w2, device="cuda", dtype=torch.float32) for i in range(e): w1[i], w1_s[i] = per_block_cast_to_fp8( w1_bf16[i], block_size=block_size, use_ue8m0=True ) w2[i], w2_s[i] = per_block_cast_to_fp8( w2_bf16[i], block_size=block_size, use_ue8m0=True ) return w1, w2, w1_s, w2_s def run_single_case(m, n, k, topk, num_experts, block_size): """ Run one (M,N,K) configuration on a single GPU and assert DeepGEMM == Triton baseline within tolerance. """ tokens_bf16 = ( torch.randn(m, k, device="cuda", dtype=torch.bfloat16) .clamp_min_(-1) .clamp_max_(1) ) _, a1_scale = per_token_group_quant_fp8(tokens_bf16, block_size[1]) # expert weight tensors w1, w2, w1_s, w2_s = make_block_quant_fp8_weights(num_experts, n, k, block_size) router_logits = torch.randn(m, num_experts, device="cuda", dtype=torch.float32) topk_weights, topk_ids = torch.topk(router_logits, k=topk, dim=-1) topk_weights = torch.nn.functional.softmax(topk_weights, dim=-1) quant_config = fp8_w8a8_moe_quant_config( w1_scale=w1_s, w2_scale=w2_s, a1_scale=a1_scale, block_shape=block_size, ) deep_gemm_experts = mk.FusedMoEModularKernel( prepare_finalize=MoEPrepareAndFinalizeNoEP(), fused_experts=TritonOrDeepGemmExperts( moe_config=make_dummy_moe_config(), quant_config=quant_config, ), inplace=False, ) # triton reference out_triton = fused_experts( hidden_states=tokens_bf16, w1=w1, w2=w2, topk_weights=topk_weights, topk_ids=topk_ids, inplace=False, quant_config=quant_config, ) # DeepGemm out_deepgemm = deep_gemm_experts( hidden_states=tokens_bf16, w1=w1, w2=w2, topk_weights=topk_weights, topk_ids=topk_ids, ) diff = calc_diff(out_deepgemm, out_triton) assert diff < 0.001, f"Diff exceeded 1%: {diff}" # Note: N <= 512 will disable the deepgemm path due to performance issues. MNKs = [ (1024, 768, 128), (2048, 768, 512), (512, 1024, 1024), (4096, 4096, 1024), ] TOPKS = [2, 6] NUM_EXPERTS = [32] @pytest.mark.parametrize(("m", "n", "k"), MNKs) @pytest.mark.parametrize("topk", TOPKS) @pytest.mark.parametrize("num_experts", NUM_EXPERTS) @pytest.mark.skipif(not is_deep_gemm_supported(), reason="Requires deep_gemm kernels") def test_deepgemm_vs_triton(m, n, k, topk, num_experts, monkeypatch, workspace_init): with monkeypatch.context() as mp: mp.setenv("VLLM_USE_DEEP_GEMM", "1") _DeepGemmExperts = importlib.import_module( "vllm.model_executor.layers.fused_moe.deep_gemm_moe" ).DeepGemmExperts call_counter = {"cnt": 0} orig_fn = _DeepGemmExperts.apply def _spy_apply(*args, **kwargs): call_counter["cnt"] += 1 return orig_fn(*args, **kwargs) monkeypatch.setattr(_DeepGemmExperts, "apply", _spy_apply) if topk > num_experts: pytest.skip(f"topk={topk} > num_experts={num_experts}") run_single_case( m=m, n=n, k=k, topk=topk, num_experts=num_experts, block_size=BLOCK_SIZE, ) # ensure that the DeepGEMM path was indeed taken. assert call_counter["cnt"] == 1, ( f"DeepGEMM path was not executed during the test. " f"Call counter: {call_counter['cnt']}" )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/test_deepgemm.py", "license": "Apache License 2.0", "lines": 161, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/model_executor/models/dots1.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Adapted from # https://github.com/huggingface/transformers/blob/v4.28.0/src/transformers/models/llama/modeling_llama.py # Copyright 2025 The rednote-hilab team. # Copyright 2023 The vLLM team. # Copyright 2023 DeepSeek-AI and the HuggingFace Inc. team. All rights reserved. # # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX # and OPT implementations in this library. It has been modified from its # original forms to accommodate minor architectural differences compared # to GPT-NeoX and OPT used by the Meta AI team that trained the model. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Inference-only dots1 model.""" from collections.abc import Iterable from itertools import islice import torch from torch import nn from transformers import Dots1Config from vllm.compilation.decorators import support_torch_compile from vllm.config import CacheConfig, ModelConfig, VllmConfig from vllm.distributed import ( get_pp_group, get_tensor_model_parallel_world_size, tensor_model_parallel_all_reduce, ) from vllm.model_executor.layers.activation import SiluAndMul from vllm.model_executor.layers.attention import Attention from vllm.model_executor.layers.fused_moe import SharedFusedMoE from vllm.model_executor.layers.layernorm import RMSNorm from vllm.model_executor.layers.linear import ( MergedColumnParallelLinear, QKVParallelLinear, ReplicatedLinear, RowParallelLinear, ) from vllm.model_executor.layers.logits_processor import LogitsProcessor from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.rotary_embedding import get_rope from vllm.model_executor.layers.vocab_parallel_embedding import ( ParallelLMHead, VocabParallelEmbedding, ) from vllm.model_executor.model_loader.weight_utils import ( default_weight_loader, maybe_remap_kv_scale_name, ) from vllm.sequence import IntermediateTensors from .interfaces import SupportsLoRA, SupportsPP from .utils import ( AutoWeightsLoader, PPMissingLayer, is_pp_missing_parameter, make_empty_intermediate_tensors_factory, make_layers, maybe_prefix, ) class Dots1MLP(nn.Module): def __init__( self, hidden_size: int, intermediate_size: int, hidden_act: str, quant_config: QuantizationConfig | None = None, reduce_results: bool = True, prefix: str = "", ) -> None: super().__init__() self.gate_up_proj = MergedColumnParallelLinear( hidden_size, [intermediate_size] * 2, bias=False, quant_config=quant_config, prefix=f"{prefix}.gate_up_proj", ) self.down_proj = RowParallelLinear( intermediate_size, hidden_size, bias=False, quant_config=quant_config, reduce_results=reduce_results, prefix=f"{prefix}.down_proj", ) if hidden_act != "silu": raise ValueError( f"Unsupported activation: {hidden_act}. Only silu is supported for now." ) self.act_fn = SiluAndMul() def forward(self, x): gate_up, _ = self.gate_up_proj(x) x = self.act_fn(gate_up) x, _ = self.down_proj(x) return x class Dots1MoE(nn.Module): def __init__( self, config: Dots1Config, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() self.tp_size = get_tensor_model_parallel_world_size() self.routed_scaling_factor = config.routed_scaling_factor self.n_shared_experts = config.n_shared_experts if config.hidden_act != "silu": raise ValueError( f"Unsupported activation: {config.hidden_act}. " "Only silu is supported for now." ) self.gate = ReplicatedLinear( config.hidden_size, config.n_routed_experts, bias=False, quant_config=None, prefix=f"{prefix}.gate", ) if config.topk_method == "noaux_tc": self.gate.e_score_correction_bias = nn.Parameter( torch.empty(config.n_routed_experts) ) else: self.gate.e_score_correction_bias = None if config.n_shared_experts is not None: intermediate_size = config.moe_intermediate_size * config.n_shared_experts self.shared_experts = Dots1MLP( hidden_size=config.hidden_size, intermediate_size=intermediate_size, hidden_act=config.hidden_act, quant_config=quant_config, reduce_results=False, prefix=f"{prefix}.shared_experts", ) else: self.shared_experts = None self.experts = SharedFusedMoE( shared_experts=self.shared_experts, num_experts=config.n_routed_experts, top_k=config.num_experts_per_tok, hidden_size=config.hidden_size, intermediate_size=config.moe_intermediate_size, reduce_results=False, renormalize=config.norm_topk_prob, quant_config=quant_config, use_grouped_topk=True, num_expert_group=config.n_group, topk_group=config.topk_group, prefix=f"{prefix}.experts", scoring_func=config.scoring_func, # we do scaling outside, set factor to 1.0 to avoid double mul routed_scaling_factor=1.0, e_score_correction_bias=self.gate.e_score_correction_bias, ) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: num_tokens, hidden_dim = hidden_states.shape hidden_states = hidden_states.view(-1, hidden_dim) router_logits, _ = self.gate(hidden_states) shared_out, routed_out = self.experts( hidden_states=hidden_states, router_logits=router_logits ) if self.shared_experts is not None: final_hidden_states = (routed_out + shared_out) * self.routed_scaling_factor else: final_hidden_states = routed_out * self.routed_scaling_factor if self.tp_size > 1: final_hidden_states = tensor_model_parallel_all_reduce(final_hidden_states) return final_hidden_states.view(num_tokens, hidden_dim) class Dots1Attention(nn.Module): def __init__( self, hidden_size: int, num_heads: int, num_kv_heads: int, config: Dots1Config, max_position_embeddings: int = 8192, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> None: super().__init__() self.hidden_size = hidden_size tp_size = get_tensor_model_parallel_world_size() self.total_num_heads = num_heads assert self.total_num_heads % tp_size == 0 self.num_heads = self.total_num_heads // tp_size self.total_num_kv_heads = num_kv_heads if self.total_num_kv_heads >= tp_size: # Number of KV heads is greater than TP size, so we partition # the KV heads across multiple tensor parallel GPUs. assert self.total_num_kv_heads % tp_size == 0 else: # Number of KV heads is less than TP size, so we replicate # the KV heads across multiple tensor parallel GPUs. assert tp_size % self.total_num_kv_heads == 0 self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size) self.head_dim = getattr(config, "head_dim", hidden_size // self.total_num_heads) self.q_size = self.num_heads * self.head_dim self.kv_size = self.num_kv_heads * self.head_dim self.scaling = self.head_dim**-0.5 self.max_position_embeddings = max_position_embeddings attention_bias = config.attention_bias self.qkv_proj = QKVParallelLinear( hidden_size, self.head_dim, self.total_num_heads, self.total_num_kv_heads, bias=attention_bias, quant_config=quant_config, prefix=f"{prefix}.qkv_proj", ) self.o_proj = RowParallelLinear( self.total_num_heads * self.head_dim, hidden_size, bias=False, quant_config=quant_config, prefix=f"{prefix}.o_proj", ) self.rotary_emb = get_rope( self.head_dim, max_position=max_position_embeddings, rope_parameters=config.rope_parameters, ) self.attn = Attention( self.num_heads, self.head_dim, self.scaling, num_kv_heads=self.num_kv_heads, cache_config=cache_config, quant_config=quant_config, prefix=f"{prefix}.attn", ) self.q_norm = RMSNorm(self.head_dim, eps=config.rms_norm_eps) self.k_norm = RMSNorm(self.head_dim, eps=config.rms_norm_eps) def forward( self, positions: torch.Tensor, hidden_states: torch.Tensor ) -> torch.Tensor: qkv, _ = self.qkv_proj(hidden_states) q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1) q = self.q_norm(q.reshape(-1, self.num_heads, self.head_dim)).reshape(q.shape) k = self.k_norm(k.reshape(-1, self.num_kv_heads, self.head_dim)).reshape( k.shape ) q, k = self.rotary_emb(positions, q, k) attn_output = self.attn(q, k, v) output, _ = self.o_proj(attn_output) return output class Dots1DecoderLayer(nn.Module): def __init__( self, config: Dots1Config, prefix: str, model_config: ModelConfig, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, ) -> None: super().__init__() self.hidden_size = config.hidden_size max_position_embeddings = getattr(config, "max_position_embeddings", 8192) layer_idx = int(prefix.split(sep=".")[-1]) self.layer_idx = layer_idx self.self_attn = Dots1Attention( hidden_size=self.hidden_size, num_heads=config.num_attention_heads, num_kv_heads=config.num_key_value_heads, config=config, max_position_embeddings=max_position_embeddings, cache_config=cache_config, quant_config=quant_config, prefix=f"{prefix}.self_attn", ) if ( config.n_routed_experts is not None and layer_idx >= config.first_k_dense_replace and layer_idx % config.moe_layer_freq == 0 ): self.mlp = Dots1MoE( config=config, quant_config=quant_config, prefix=f"{prefix}.mlp" ) else: self.mlp = Dots1MLP( hidden_size=config.hidden_size, intermediate_size=config.intermediate_size, hidden_act=config.hidden_act, quant_config=quant_config, prefix=f"{prefix}.mlp", ) self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.post_attention_layernorm = RMSNorm( config.hidden_size, eps=config.rms_norm_eps ) self.routed_scaling_factor = config.routed_scaling_factor def forward( self, positions: torch.Tensor, hidden_states: torch.Tensor, residual: torch.Tensor | None, ) -> torch.Tensor: if residual is None: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) else: hidden_states, residual = self.input_layernorm(hidden_states, residual) hidden_states = self.self_attn(positions=positions, hidden_states=hidden_states) hidden_states, residual = self.post_attention_layernorm(hidden_states, residual) hidden_states = self.mlp(hidden_states) return hidden_states, residual @support_torch_compile class Dots1Model(nn.Module): fall_back_to_pt_during_load = False def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__() config = vllm_config.model_config.hf_config model_config = vllm_config.model_config cache_config = vllm_config.cache_config quant_config = vllm_config.quant_config self.config = config self.vocab_size = config.vocab_size if get_pp_group().is_first_rank: self.embed_tokens = VocabParallelEmbedding( config.vocab_size, config.hidden_size, quant_config=quant_config, prefix=f"{prefix}.embed_tokens", ) else: self.embed_tokens = PPMissingLayer() self.start_layer, self.end_layer, self.layers = make_layers( config.num_hidden_layers, lambda prefix: Dots1DecoderLayer( config, prefix, model_config=model_config, cache_config=cache_config, quant_config=quant_config, ), prefix=f"{prefix}.layers", ) if get_pp_group().is_last_rank: self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) else: self.norm = PPMissingLayer() self.make_empty_intermediate_tensors = make_empty_intermediate_tensors_factory( ["hidden_states", "residual"], config.hidden_size ) def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.embed_tokens(input_ids) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None, inputs_embeds: torch.Tensor | None = None, ) -> torch.Tensor | IntermediateTensors: if get_pp_group().is_first_rank: if inputs_embeds is not None: hidden_states = inputs_embeds else: hidden_states = self.embed_input_ids(input_ids) residual = None else: assert intermediate_tensors is not None hidden_states = intermediate_tensors["hidden_states"] residual = intermediate_tensors["residual"] for layer in islice(self.layers, self.start_layer, self.end_layer): hidden_states, residual = layer( positions, hidden_states, residual, ) if not get_pp_group().is_last_rank: return IntermediateTensors( {"hidden_states": hidden_states, "residual": residual} ) hidden_states, _ = self.norm(hidden_states, residual) return hidden_states def get_expert_mapping(self) -> list[tuple[str, str, int, str]]: return SharedFusedMoE.make_expert_params_mapping( self, ckpt_gate_proj_name="gate_proj", ckpt_down_proj_name="down_proj", ckpt_up_proj_name="up_proj", num_experts=self.config.n_routed_experts, ) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: stacked_params_mapping = [ ("qkv_proj", "q_proj", "q"), ("qkv_proj", "k_proj", "k"), ("qkv_proj", "v_proj", "v"), ("gate_up_proj", "gate_proj", 0), ("gate_up_proj", "up_proj", 1), ] params_dict = dict(self.named_parameters()) loaded_params: set[str] = set() expert_params_mapping = self.get_expert_mapping() for name, loaded_weight in weights: if "rotary_emb.inv_freq" in name: continue for param_name, weight_name, shard_id in stacked_params_mapping: if weight_name not in name: continue if ("mlp.experts." in name) and name not in params_dict: continue name = name.replace(weight_name, param_name) if name.endswith(".bias") and name not in params_dict: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = param.weight_loader weight_loader(param, loaded_weight, shard_id) break else: for mapping in expert_params_mapping: param_name, weight_name, expert_id, shard_id = mapping if weight_name not in name: continue name = name.replace(weight_name, param_name) if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = param.weight_loader weight_loader( param, loaded_weight, name, shard_id=shard_id, expert_id=expert_id, ) break else: if name.endswith(".bias") and name not in params_dict: continue name = maybe_remap_kv_scale_name(name, params_dict) if name is None: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = getattr( param, "weight_loader", default_weight_loader ) weight_loader(param, loaded_weight) loaded_params.add(name) return loaded_params class Dots1ForCausalLM(nn.Module, SupportsPP, SupportsLoRA): packed_modules_mapping = { "qkv_proj": [ "q_proj", "k_proj", "v_proj", ], "gate_up_proj": [ "gate_proj", "up_proj", ], } def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__() config = vllm_config.model_config.hf_config quant_config = vllm_config.quant_config self.config = config self.quant_config = quant_config self.model = Dots1Model( vllm_config=vllm_config, prefix=maybe_prefix(prefix, "model") ) if get_pp_group().is_last_rank: self.lm_head = ParallelLMHead( config.vocab_size, config.hidden_size, quant_config=quant_config, prefix=maybe_prefix(prefix, "lm_head"), ) else: self.lm_head = PPMissingLayer() self.logits_processor = LogitsProcessor(config.vocab_size) self.make_empty_intermediate_tensors = ( self.model.make_empty_intermediate_tensors ) def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.model.embed_input_ids(input_ids) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, ) -> torch.Tensor | IntermediateTensors: hidden_states = self.model( input_ids, positions, intermediate_tensors, inputs_embeds, ) return hidden_states def compute_logits( self, hidden_states: torch.Tensor, ) -> torch.Tensor | None: logits = self.logits_processor(self.lm_head, hidden_states) return logits def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: loader = AutoWeightsLoader(self) return loader.load_weights(weights) def get_expert_mapping(self) -> list[tuple[str, str, int, str]]: return self.model.get_expert_mapping()
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/dots1.py", "license": "Apache License 2.0", "lines": 515, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/gemma3n.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Copyright 2025 The vLLM team. # Copyright 2025 Google Inc. HuggingFace Inc. team. All rights reserved. # # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from collections.abc import Iterable import torch from torch import nn from transformers.models.gemma3n.configuration_gemma3n import Gemma3nTextConfig from vllm.compilation.decorators import support_torch_compile from vllm.config import CacheConfig, VllmConfig from vllm.distributed import get_tensor_model_parallel_world_size from vllm.forward_context import get_forward_context from vllm.logger import init_logger from vllm.model_executor.layers.activation import ( _ACTIVATION_REGISTRY, GeluAndMul, GeluAndMulSparse, ) from vllm.model_executor.layers.attention import Attention from vllm.model_executor.layers.layernorm import RMSNorm from vllm.model_executor.layers.linear import ( ColumnParallelLinear, MergedColumnParallelLinear, QKVParallelLinear, ReplicatedLinear, RowParallelLinear, ) from vllm.model_executor.layers.logits_processor import LogitsProcessor from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.rotary_embedding import get_rope from vllm.model_executor.layers.vocab_parallel_embedding import VocabParallelEmbedding from vllm.model_executor.model_loader.weight_utils import ( default_weight_loader, maybe_remap_kv_scale_name, ) from vllm.sequence import IntermediateTensors from vllm.v1.attention.backends.utils import KVSharingFastPrefillMetadata from .interfaces import SupportsQuant from .utils import ( AutoWeightsLoader, extract_layer_index, is_pp_missing_parameter, make_layers, maybe_prefix, ) logger = init_logger(__name__) EPS = torch.tensor(torch.finfo().min) class Gemma3nAltUp(nn.Module): """Alternating updates (Altup) The AltUp module wraps transformer layers. The `predict` step modifies the input to the transformer layer, and the `correct` step propagates the output of the transformer layer to the sparsely updated dimensions. See more in the research paper: https://proceedings.neurips.cc/paper_files/paper/2023/file/f2059277ac6ce66e7e5543001afa8bb5-Paper-Conference.pdf """ def __init__( self, hidden_size: int, rms_norm_eps: float, altup_num_inputs: int, altup_coef_clip: float, altup_active_idx: int, quant_config: QuantizationConfig, prefix: str, ): super().__init__() self.altup_num_inputs = altup_num_inputs self.altup_active_idx = altup_active_idx self.altup_coef_clip = altup_coef_clip self.correction_coefs = ReplicatedLinear( altup_num_inputs, altup_num_inputs, bias=False, quant_config=quant_config, prefix=f"{prefix}.correction_coefs", return_bias=False, ) self.prediction_coefs = ReplicatedLinear( altup_num_inputs, altup_num_inputs**2, bias=False, quant_config=quant_config, prefix=f"{prefix}.prediction_coefs", return_bias=False, ) self.modality_router = ReplicatedLinear( hidden_size, altup_num_inputs, bias=False, quant_config=quant_config, prefix=f"{prefix}.modality_router", return_bias=False, ) self.router_norm = RMSNorm( hidden_size=hidden_size, eps=rms_norm_eps, ) self.router_input_scale = torch.tensor( hidden_size**-1.0, dtype=self.modality_router.weight.dtype ) self.correct_output_scale = nn.Parameter( torch.zeros(hidden_size, dtype=torch.float32) ) def _compute_router_modalities(self, x: torch.Tensor) -> torch.Tensor: router_inputs = self.router_norm(x) * self.router_input_scale routed = self.modality_router(router_inputs) return torch.tanh(routed.float()).type_as(x) def scale_corrected_output(self, corrected: torch.Tensor) -> torch.Tensor: return ( corrected.type_as(self.correct_output_scale) * self.correct_output_scale ).type_as(corrected) def predict(self, hidden_states: torch.Tensor) -> torch.Tensor: # hidden: [altup_num_inputs, num_tokens, hidden_size] # modalities: [num_tokens, num_altup_inputs] # all_coefs: [num_tokens, num_altup_inputs ** 2] modalities = self._compute_router_modalities( hidden_states[self.altup_active_idx] ) all_coefs = self.prediction_coefs(modalities) # Reshape and transpose the 2D matrix for the matmul. # all_coefs_T: [num_tokens, num_altup_inputs, num_altup_inputs] all_coefs_T = all_coefs.reshape( -1, self.altup_num_inputs, self.altup_num_inputs, ).permute(0, 2, 1) # hidden_states to [num_tokens, hidden_size, altup_num_inputs] predictions = torch.matmul(hidden_states.permute(1, 2, 0), all_coefs_T) # [altup_num_inputs, num_tokens, hidden_size] predictions = predictions.permute(2, 0, 1) predictions += hidden_states return predictions.contiguous() def correct( self, predictions: torch.Tensor, activated: torch.Tensor ) -> torch.Tensor: # predictions: [altup_num_inputs, num_tokens, hidden_size] # activated: [num_tokens, hidden_size] # modalities: [num_tokens, altup_num_inputs] modalities = self._compute_router_modalities(activated) # innovation: [num_tokens, altup_num_inputs] innovation = activated - predictions[self.altup_active_idx] # innovation: [altup_num_inputs, num_tokens, hidden_size] innovation = innovation.repeat(self.altup_num_inputs, 1, 1) # Permute to [altup_num_inputs, num_tokens] as the last dim # is a scalar applied to each altup input and expand on # num_tokens dim for broadcastability over hidden_size. # all_coefs: [num_tokens, altup_num_inputs] all_coefs = self.correction_coefs(modalities) + 1.0 # all_coefs: [altup_num_inputs, num_tokens, 1] all_coefs = all_coefs.T.unsqueeze(-1) # Elementwise (broadcast over hidden_size). corrected = torch.mul(innovation, all_coefs) corrected += predictions return corrected.contiguous() class Gemma3nLaurelBlock(nn.Module): """Learned Augmented Residual Layer""" def __init__( self, hidden_size: int, laurel_rank: int, rms_norm_eps: float, *, quant_config: QuantizationConfig | None = None, prefix: str, ) -> None: super().__init__() self.linear_left = ColumnParallelLinear( hidden_size, laurel_rank, bias=False, quant_config=quant_config, prefix=f"{prefix}.linear_left", return_bias=False, ) self.linear_right = RowParallelLinear( laurel_rank, hidden_size, bias=False, quant_config=quant_config, prefix=f"{prefix}.linear_right", return_bias=False, ) self.post_laurel_norm = RMSNorm( hidden_size=hidden_size, eps=rms_norm_eps, ) def forward(self, x: torch.Tensor) -> torch.Tensor: laurel_x = self.linear_left(x) laurel_x = self.linear_right(laurel_x) normed_laurel_x = self.post_laurel_norm(laurel_x) return x + normed_laurel_x class Gemma3nMLP(nn.Module): def __init__( self, hidden_size: int, intermediate_size: int, hidden_activation: str, activation_sparsity: float = 0.0, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> None: super().__init__() self.gate_up_proj = MergedColumnParallelLinear( hidden_size, [intermediate_size] * 2, bias=False, quant_config=quant_config, prefix=f"{prefix}.gate_up_proj", ) self.down_proj = RowParallelLinear( intermediate_size, hidden_size, bias=False, quant_config=quant_config, prefix=f"{prefix}.down_proj", ) if hidden_activation != "gelu_pytorch_tanh": raise ValueError( "Gemma3 uses `gelu_pytorch_tanh` as the hidden activation " "function. Please set `hidden_act` and `hidden_activation` to " "`gelu_pytorch_tanh`." ) self.act_fn = ( GeluAndMulSparse( activation_sparsity=activation_sparsity, approximate="tanh" ) if activation_sparsity > 0.0 else GeluAndMul(approximate="tanh") ) def forward(self, x: torch.Tensor) -> torch.Tensor: gate_up, _ = self.gate_up_proj(x) x = self.act_fn(gate_up) x, _ = self.down_proj(x) return x class Gemma3nAttention(nn.Module): def __init__( self, config: Gemma3nTextConfig, hidden_size: int, num_heads: int, num_kv_heads: int, head_dim: int, max_position_embeddings: int, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> None: super().__init__() self.config = config self.hidden_size = hidden_size tp_size = get_tensor_model_parallel_world_size() self.total_num_heads = num_heads assert self.total_num_heads % tp_size == 0 self.num_heads = self.total_num_heads // tp_size self.total_num_kv_heads = num_kv_heads if self.total_num_kv_heads >= tp_size: # Number of KV heads is greater than TP size, so we partition # the KV heads across multiple tensor parallel GPUs. assert self.total_num_kv_heads % tp_size == 0 else: # Number of KV heads is less than TP size, so we replicate # the KV heads across multiple tensor parallel GPUs. assert tp_size % self.total_num_kv_heads == 0 self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size) self.head_dim = head_dim self.q_size = self.num_heads * self.head_dim self.kv_size = self.num_kv_heads * self.head_dim self.qkv_proj = QKVParallelLinear( hidden_size, self.head_dim, self.total_num_heads, self.total_num_kv_heads, bias=config.attention_bias, quant_config=quant_config, prefix=f"{prefix}.qkv_proj", ) self.o_proj = RowParallelLinear( self.total_num_heads * self.head_dim, hidden_size, bias=config.attention_bias, quant_config=quant_config, prefix=f"{prefix}.o_proj", ) self.q_norm = RMSNorm(hidden_size=self.head_dim, eps=config.rms_norm_eps) self.k_norm = RMSNorm(hidden_size=self.head_dim, eps=config.rms_norm_eps) self.v_norm = RMSNorm( hidden_size=self.head_dim, eps=config.rms_norm_eps, has_weight=False ) layer_idx = extract_layer_index(prefix) layer_type = config.layer_types[layer_idx] is_sliding = layer_type == "sliding_attention" self.sliding_window = config.sliding_window if is_sliding else None # Initialize the rotary embedding. if layer_type in config.rope_parameters: # Transformers v5 rope config. rope_parameters = config.rope_parameters[layer_type] else: # Transformers v4 rope config. # Global attention. Use the values in config.json. rope_parameters = config.rope_parameters.copy() # Local attention. Override the values in config.json. if is_sliding: rope_parameters["rope_theta"] = config.rope_local_base_freq first_kv_shared_layer_idx = ( config.num_hidden_layers - config.num_kv_shared_layers ) self.is_kv_shared = layer_idx >= first_kv_shared_layer_idx kv_sharing_target_layer_name = None if self.is_kv_shared: # Last full attention layer is 1 before sharing # Last sliding attention layer is 2 before sharing offset = 2 if self.sliding_window is not None else 1 kv_shared_layer_index = first_kv_shared_layer_idx - offset if kv_shared_layer_index >= 0: # Different model wrappers expose layer parameters under # different parent attributes. # For example: # - Gemma3nForCausalLM → parameters live under "model.layers" # - Gemma3nForConditionalGeneration → # under "language_model.model.layers" # This logic extracts the portion of the parameter name # *before* ".layers." # so downstream code can consistently reference the correct # model root regardless of which wrapper class was used. if ".layers." in prefix: param_name_before_layers = prefix.split(".layers.")[0] else: raise ValueError( "Unexpected prefix format for Gemma3nAttention: " f"'{prefix}'. The prefix is expected to contain " "'.layers.' to correctly determine the KV sharing " "target layer." ) # Only the greater layer is required to specify sharing. kv_sharing_target_layer_name = f"{param_name_before_layers}.layers.{kv_shared_layer_index}.self_attn.attn" # noqa: E501 self.rotary_emb = get_rope( self.head_dim, max_position=max_position_embeddings, rope_parameters=rope_parameters, is_neox_style=True, ) self.attn = Attention( num_heads=self.num_heads, head_size=self.head_dim, scale=1.0, num_kv_heads=self.num_kv_heads, cache_config=cache_config, quant_config=quant_config, per_layer_sliding_window=self.sliding_window, kv_sharing_target_layer_name=kv_sharing_target_layer_name, prefix=f"{prefix}.attn", ) def forward( self, positions: torch.Tensor, hidden_states: torch.Tensor, **kwargs, ) -> torch.Tensor: qkv, _ = self.qkv_proj(hidden_states) q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1) q = q.unflatten(-1, (self.num_heads, self.head_dim)) q = self.q_norm(q) q = q.flatten(-2, -1) k = k.unflatten(-1, (self.num_kv_heads, self.head_dim)) k = self.k_norm(k) k = k.flatten(-2, -1) v = v.unflatten(-1, (self.num_kv_heads, self.head_dim)) v = self.v_norm(v) v = v.flatten(-2, -1) q, k = self.rotary_emb(positions, q, k) attn_output = self.attn(q, k, v) output, _ = self.o_proj(attn_output) return output class Gemma3nDecoderLayer(nn.Module): def __init__( self, config: Gemma3nTextConfig, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> None: super().__init__() assert isinstance(config, Gemma3nTextConfig) self.altup_active_idx = config.altup_active_idx assert config.altup_correct_scale self.altup = Gemma3nAltUp( hidden_size=config.hidden_size, rms_norm_eps=config.rms_norm_eps, altup_num_inputs=config.altup_num_inputs, altup_coef_clip=config.altup_coef_clip, altup_active_idx=config.altup_active_idx, quant_config=quant_config, prefix=f"{prefix}.altup", ) self.self_attn = Gemma3nAttention( config=config, hidden_size=config.hidden_size, num_heads=config.num_attention_heads, num_kv_heads=config.num_key_value_heads, head_dim=config.head_dim, max_position_embeddings=config.max_position_embeddings, cache_config=cache_config, quant_config=quant_config, prefix=f"{prefix}.self_attn", ) self.mlp = Gemma3nMLP( hidden_size=config.hidden_size, # NOTE: Matformer https://github.com/huggingface/transformers/blob/a52478253bbe522a420e88ea3940d4d98a935300/src/transformers/models/gemma3n/modular_gemma3n.py#L258 # noqa: E501 intermediate_size=config.intermediate_size[extract_layer_index(prefix)], hidden_activation=config.hidden_activation, quant_config=quant_config, activation_sparsity=config.activation_sparsity_pattern[ extract_layer_index(prefix) ], prefix=f"{prefix}.mlp", ) self.laurel = Gemma3nLaurelBlock( hidden_size=config.hidden_size, laurel_rank=config.laurel_rank, rms_norm_eps=config.rms_norm_eps, quant_config=quant_config, prefix=f"{prefix}.laurel", ) # NOTE(rob): should be ColumnParallelLinear and RowParallelLinear # But, we need to add per_layer_input_gate(x) to per_layer_input. # per_layer_input cannot be sharded, so we replicate for now. self.per_layer_input_gate = ReplicatedLinear( config.hidden_size, config.hidden_size_per_layer_input, bias=False, quant_config=quant_config, prefix=f"{prefix}.per_layer_input_gate", return_bias=False, ) self.per_layer_projection = ReplicatedLinear( config.hidden_size_per_layer_input, config.hidden_size, bias=False, quant_config=quant_config, prefix=f"{prefix}.per_layer_projection", return_bias=False, ) # LayerNorms. self.input_layernorm = RMSNorm( config.hidden_size, eps=config.rms_norm_eps, ) self.post_attention_layernorm = RMSNorm( config.hidden_size, eps=config.rms_norm_eps, ) self.pre_feedforward_layernorm = RMSNorm( config.hidden_size, eps=config.rms_norm_eps, ) self.post_feedforward_layernorm = RMSNorm( config.hidden_size, eps=config.rms_norm_eps, ) self.post_per_layer_input_norm = RMSNorm( config.hidden_size, eps=config.rms_norm_eps, ) self.act_fn = _ACTIVATION_REGISTRY[config.hidden_activation] def forward( self, positions: torch.Tensor, hidden_states: torch.Tensor, per_layer_input: torch.Tensor, **kwargs, ) -> tuple[torch.Tensor, torch.Tensor]: # ActUp (predict). predictions = self.altup.predict(hidden_states) active_prediction = predictions[self.altup_active_idx] active_prediction_normed = self.input_layernorm(active_prediction) laurel_output = self.laurel(active_prediction_normed) # Attention. attn = self.self_attn( positions=positions, hidden_states=active_prediction_normed, **kwargs, ) attn = self.post_attention_layernorm(attn) attn_gated = attn + active_prediction attn_laurel = (attn_gated + laurel_output) / torch.sqrt(torch.tensor(2.0)) # MLP. attn_norm = self.pre_feedforward_layernorm(attn_laurel) attn_ffw = self.mlp(attn_norm) attn_ffw_norm = self.post_feedforward_layernorm(attn_ffw) attn_ffw_laurel_gated = attn_laurel + attn_ffw_norm # ActUp (connect). corrected_predictions = self.altup.correct(predictions, attn_ffw_laurel_gated) first_prediction = corrected_predictions[self.altup_active_idx] first_prediction = self.altup.scale_corrected_output(first_prediction) # per_layer_input_gate adapted from jax.numpy.einsum("btd,dp->btp", ...) first_prediction = self.per_layer_input_gate(first_prediction) first_prediction = self.act_fn(first_prediction) first_prediction = torch.mul(first_prediction, per_layer_input) # per_layer_projection adapted from jax.numpy.einsum("btp,pd->btd", ...) first_prediction = self.per_layer_projection(first_prediction) first_prediction = self.post_per_layer_input_norm(first_prediction) corrected_predictions[1:] += first_prediction return corrected_predictions # This enables torch.compile if --kv-sharing-fast-prefill passed @support_torch_compile( enable_if=lambda vllm_config: vllm_config.cache_config.kv_sharing_fast_prefill ) class Gemma3nSelfDecoder(nn.Module): """ Includes altup embedding and self decoder layers """ def __init__( self, *, vllm_config: VllmConfig, prefix: str = "", decoder_layers: list[Gemma3nDecoderLayer], layer_idx_start: int, ): super().__init__() self.decoder_layers = decoder_layers self.layer_idx_start = layer_idx_start config = vllm_config.model_config.hf_config self.config = config quant_config = vllm_config.quant_config self.embed_tokens = VocabParallelEmbedding( config.vocab_size, config.hidden_size, quant_config=quant_config, prefix=f"{prefix}.embed_tokens", ) self.embed_scale = torch.tensor( config.hidden_size**0.5, dtype=self.embed_tokens.weight.dtype, ) # Additional per-layer embeddings (PLE) self.embed_tokens_per_layer = VocabParallelEmbedding( config.vocab_size_per_layer_input, config.num_hidden_layers * config.hidden_size_per_layer_input, quant_config=quant_config, prefix=f"{prefix}.per_layer_embed_tokens", ) self.embed_scale_per_layer = torch.tensor( config.hidden_size_per_layer_input**0.5, dtype=self.embed_tokens.weight.dtype, ) self.per_layer_model_projection = ColumnParallelLinear( config.hidden_size, config.num_hidden_layers * config.hidden_size_per_layer_input, bias=False, gather_output=True, return_bias=False, quant_config=quant_config, prefix=f"{prefix}.per_layer_model_projection", ) self.per_layer_projection_norm = RMSNorm( hidden_size=config.hidden_size_per_layer_input, eps=config.rms_norm_eps, ) self.per_layer_input_scale = torch.rsqrt(torch.tensor(2.0)).to( self.embed_tokens.weight.dtype ) self.per_layer_projection_scale = torch.tensor( config.hidden_size**0.5, dtype=self.embed_tokens.weight.dtype, ) self.altup_projections = nn.ModuleList( [ ColumnParallelLinear( config.hidden_size, config.hidden_size, bias=False, gather_output=True, return_bias=False, quant_config=quant_config, prefix=f"{prefix}.altup_projections.{idx - 1}", ) for idx in range(1, self.config.altup_num_inputs) ] ) def get_per_layer_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor: # Deal with the fact that vocab_size_per_layer_input < vocab_size # which causes us to have some out of vocab tokens by setting # those token ids to 0. This matches the HF implementation. per_layer_inputs_mask = torch.logical_and( input_ids >= 0, input_ids < self.config.vocab_size_per_layer_input ) per_layer_inputs_tokens = torch.where( per_layer_inputs_mask, input_ids, torch.zeros_like(input_ids) ) return ( self.embed_tokens_per_layer(per_layer_inputs_tokens) * self.embed_scale_per_layer ) def get_per_layer_inputs( self, hidden_states_0: torch.Tensor, per_layer_inputs: torch.Tensor | None, ) -> torch.Tensor: per_layer_projection = self.per_layer_model_projection(hidden_states_0) per_layer_projection = per_layer_projection.reshape( *hidden_states_0.shape[:-1], self.config.num_hidden_layers, self.config.hidden_size_per_layer_input, ) per_layer_projection = self.per_layer_projection_norm(per_layer_projection) if per_layer_inputs is not None: # Profiling run does not compute per_layer_inputs per_layer_inputs = per_layer_projection + per_layer_inputs per_layer_inputs *= self.per_layer_input_scale else: per_layer_inputs = per_layer_projection return per_layer_inputs def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.embed_tokens(input_ids) * self.embed_scale def altup_embed(self, hidden_states_0: torch.Tensor) -> torch.Tensor: # Altup embed. hidden_states = [hidden_states_0] * self.config.altup_num_inputs target_magnitude = torch.mean(hidden_states_0**2, dim=-1, keepdim=True) ** 0.5 for i in range(1, self.config.altup_num_inputs): hidden_states[i] = self.altup_projections[i - 1](hidden_states[i]) new_magnitude = ( torch.mean(hidden_states[i] ** 2, dim=-1, keepdim=True) ** 0.5 ) hidden_states[i] *= target_magnitude / torch.maximum(new_magnitude, EPS) hidden_states = torch.stack(hidden_states, dim=-1) return hidden_states def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, inputs_embeds: torch.Tensor | None = None, per_layer_inputs: torch.Tensor | None = None, **kwargs, ) -> tuple[torch.Tensor, torch.Tensor]: if inputs_embeds is not None: hidden_states_0 = inputs_embeds else: hidden_states_0 = self.embed_input_ids(input_ids) adjusted_per_layer_inputs = self.get_per_layer_inputs( hidden_states_0, per_layer_inputs ) hidden_states = self.altup_embed(hidden_states_0) # [altnum_inputs, num_tokens, hidden_size] hidden_states = hidden_states.permute(2, 0, 1) for idx, layer in enumerate(self.decoder_layers): layer_idx = idx + self.layer_idx_start # [altup_num_inputs, num_tokens, hidden_size] hidden_states = layer( positions=positions, hidden_states=hidden_states, per_layer_input=adjusted_per_layer_inputs[:, layer_idx, :], **kwargs, ) # [num_tokens, hidden_size, altnum_inputs] hidden_states = hidden_states.permute(1, 2, 0) return hidden_states, adjusted_per_layer_inputs # This enables torch.compile if --kv-sharing-fast-prefill passed @support_torch_compile( enable_if=lambda vllm_config: vllm_config.cache_config.kv_sharing_fast_prefill ) class Gemma3nCrossDecoder(nn.Module): """ Cross-decoder layers """ def __init__( self, *, vllm_config: VllmConfig, prefix: str = "", decoder_layers: list[Gemma3nDecoderLayer], layer_idx_start: int, ): super().__init__() self.decoder_layers = decoder_layers self.layer_idx_start = layer_idx_start def forward( self, positions: torch.Tensor, hidden_states: torch.Tensor, per_layer_inputs: torch.Tensor, **kwargs, ) -> torch.Tensor: # [altnum_inputs, num_tokens, hidden_size] hidden_states = hidden_states.permute(2, 0, 1) for idx, layer in enumerate(self.decoder_layers): layer_idx = idx + self.layer_idx_start # [altup_num_inputs, num_tokens, hidden_size] hidden_states = layer( positions=positions, hidden_states=hidden_states, per_layer_input=per_layer_inputs[:, layer_idx, :], **kwargs, ) # [num_tokens, hidden_size, altnum_inputs] hidden_states = hidden_states.permute(1, 2, 0) return hidden_states # This disables torch.compile if --kv-sharing-fast-prefill passed @support_torch_compile( enable_if=lambda vllm_config: not vllm_config.cache_config.kv_sharing_fast_prefill ) class Gemma3nTextModel(nn.Module, SupportsQuant): def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__() config = vllm_config.model_config.hf_config cache_config = vllm_config.cache_config quant_config = vllm_config.quant_config self.config = config self.quant_config = quant_config self.altup_unembed_projections = nn.ModuleList( [ ColumnParallelLinear( config.hidden_size, config.hidden_size, bias=False, gather_output=True, return_bias=False, quant_config=quant_config, prefix=f"{prefix}.altup_unembed_projections.{idx - 1}", ) for idx in range(1, self.config.altup_num_inputs) ] ) # Allocate config.num_kv_shared_layers layers for self-decoder self.start_layer, self.end_layer, self.layers = make_layers( config.num_hidden_layers, lambda prefix: Gemma3nDecoderLayer( config, cache_config, quant_config, prefix=prefix ), prefix=f"{prefix}.layers", ) first_kv_shared_layer_idx = ( config.num_hidden_layers - config.num_kv_shared_layers ) # NOTE(sarckk): importing this top level seems to cause issues # during running of tests. from vllm.compilation.backends import set_model_tag # Layer idx 0-19 are self-decoder layers in You Only Cache Once (YOCO) with set_model_tag("self_decoder"): self.self_decoder = Gemma3nSelfDecoder( vllm_config=vllm_config, prefix=f"{prefix}.self_decoder", decoder_layers=self.layers[:first_kv_shared_layer_idx], layer_idx_start=0, ) # Layer idx 20-30 are cross-decoder layers in YOCO with set_model_tag("cross_decoder"): self.cross_decoder = Gemma3nCrossDecoder( vllm_config=vllm_config, prefix=f"{prefix}.cross_decoder", decoder_layers=self.layers[first_kv_shared_layer_idx:], layer_idx_start=first_kv_shared_layer_idx, ) self.norm = RMSNorm( config.hidden_size, eps=config.rms_norm_eps, ) self.fast_prefill_enabled = cache_config.kv_sharing_fast_prefill if self.fast_prefill_enabled: # Allocate static buffers for CUDAGraph # TODO(sarckk): Extract this functionality to interface max_num_tokens = vllm_config.scheduler_config.max_num_batched_tokens device = next(self.parameters()).device self.positions = torch.zeros( max_num_tokens, dtype=torch.int64, device=device ) self.hidden_states = torch.zeros( (max_num_tokens, config.hidden_size, self.config.altup_num_inputs), dtype=self.embed_tokens.weight.dtype, device=device, ) self.per_layer_inputs = torch.zeros( ( max_num_tokens, self.config.num_hidden_layers, self.config.hidden_size_per_layer_input, ), dtype=self.embed_tokens.weight.dtype, device=device, ) @property def embed_tokens(self): return self.self_decoder.embed_tokens def get_per_layer_input_embeddings(self, input_ids: torch.Tensor) -> torch.Tensor: return self.self_decoder.get_per_layer_input_embeddings(input_ids) def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.self_decoder.embed_input_ids(input_ids) def fast_prefill_forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, inputs_embeds: torch.Tensor | None = None, per_layer_inputs: torch.Tensor | None = None, **kwargs, ) -> torch.Tensor: logits_indices_padded, num_logits_indices = None, None attn_metadata = get_forward_context().attn_metadata # attn_metadata is None during dummy runs if self.fast_prefill_enabled and attn_metadata is not None: assert isinstance(attn_metadata, dict) # Last layer is a KV sharing layer layer_attn_metadata = attn_metadata[ self.layers[-1].self_attn.attn.layer_name ] if isinstance(layer_attn_metadata, KVSharingFastPrefillMetadata): logits_indices_padded = layer_attn_metadata.logits_indices_padded num_logits_indices = layer_attn_metadata.num_logits_indices # Copy inputs for cudagraph batch_size = positions.size(0) self.positions[:batch_size].copy_(positions) self_decoder_hidden_states, per_layer_inputs_adjusted = self.self_decoder( input_ids=input_ids, positions=self.positions[:batch_size], inputs_embeds=inputs_embeds, per_layer_inputs=per_layer_inputs, **kwargs, ) if logits_indices_padded is None: logits_indices_padded = torch.arange( positions.size(0), dtype=positions.dtype, device=positions.device, ) # NOTE(sarckk): There is currently a bug caused by # vLLM converting output of last piecewise CUDA graph # to weakref, causing memory to be prematurely freed # when there are multiple compilation units # Keep .clone() until fix in # https://github.com/vllm-project/vllm/pull/22282 hidden_states = self_decoder_hidden_states.clone() # Copy inputs for cudagraph num_padded_logits_indices = logits_indices_padded.size(0) self.positions[:num_padded_logits_indices].copy_( positions[logits_indices_padded] ) self.hidden_states[:num_padded_logits_indices].copy_( self_decoder_hidden_states[logits_indices_padded] ) self.per_layer_inputs[:num_padded_logits_indices].copy_( per_layer_inputs_adjusted[logits_indices_padded] ) cross_decoder_hidden_states = self.cross_decoder( positions=self.positions[:num_padded_logits_indices], hidden_states=self.hidden_states[:num_padded_logits_indices], per_layer_inputs=self.per_layer_inputs[:num_padded_logits_indices], **kwargs, ) if num_logits_indices is not None: assert num_logits_indices > 0 # Merge cross-decoder and self-decoder hidden states hidden_states[logits_indices_padded[:num_logits_indices]] = ( cross_decoder_hidden_states[:num_logits_indices] ) else: hidden_states = cross_decoder_hidden_states return hidden_states def normal_forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, inputs_embeds: torch.Tensor | None = None, per_layer_inputs: torch.Tensor | None = None, **kwargs, ) -> torch.Tensor: hidden_states, per_layer_inputs = self.self_decoder( input_ids=input_ids, positions=positions, inputs_embeds=inputs_embeds, per_layer_inputs=per_layer_inputs, **kwargs, ) hidden_states = self.cross_decoder( positions=positions, hidden_states=hidden_states, per_layer_inputs=per_layer_inputs, **kwargs, ) return hidden_states def altup_unembed( self, hidden_states: torch.Tensor, ) -> torch.Tensor: # Altup unembed. target_magnitude = ( torch.mean(hidden_states[..., 0] ** 2, dim=-1, keepdim=True) ** 0.5 ) for i in range(1, self.config.altup_num_inputs): hidden_states[..., i] = self.altup_unembed_projections[i - 1]( hidden_states[..., i] ) new_magnitude = ( torch.mean(hidden_states[..., i] ** 2, dim=-1, keepdim=True) ** 0.5 ) hidden_states[..., i] *= target_magnitude / torch.maximum( new_magnitude, EPS ) # [num_tokens,hidden_size, altup_num_inputs] -> [num_tokens,hidden_size] hidden_states = torch.mean(hidden_states, dim=-1) return hidden_states def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, per_layer_inputs: torch.Tensor | None = None, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, **kwargs, ) -> torch.Tensor | IntermediateTensors: if self.fast_prefill_enabled: hidden_states = self.fast_prefill_forward( input_ids, positions, inputs_embeds, per_layer_inputs, **kwargs, ) else: hidden_states = self.normal_forward( input_ids, positions, inputs_embeds, per_layer_inputs, **kwargs, ) hidden_states = self.altup_unembed(hidden_states) return self.norm(hidden_states) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: stacked_params_mapping = [ # (param_name, shard_name, shard_id) ("qkv_proj", "q_proj", "q"), ("qkv_proj", "k_proj", "k"), ("qkv_proj", "v_proj", "v"), ("gate_up_proj", "gate_proj", 0), ("gate_up_proj", "up_proj", 1), ] params_dict = dict(self.named_parameters()) loaded_params: set[str] = set() for name, loaded_weight in weights: # decoder layer weights, altup_unembed_projections and rmsnorm # are initialized in text model, others are in self decoder if ( not name.startswith("layers") and not name.startswith("altup_unembed_projections") and not name.startswith("norm") ): name = f"self_decoder.{name}" if self.quant_config is not None and ( scale_name := self.quant_config.get_cache_scale(name) ): # Loading kv cache scales for compressed-tensors quantization param = params_dict[scale_name] weight_loader = getattr(param, "weight_loader", default_weight_loader) loaded_weight = loaded_weight[0] weight_loader(param, loaded_weight) loaded_params.add(scale_name) continue for param_name, shard_name, shard_id in stacked_params_mapping: if shard_name not in name: continue # Avoid spurious match with ".up_proj". if "altup_projections" in name: continue name = name.replace(shard_name, param_name) # Skip loading extra bias for GPTQ models. if name.endswith(".bias") and name not in params_dict: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = param.weight_loader weight_loader(param, loaded_weight, shard_id) break else: # Skip loading extra bias for GPTQ models. if name.endswith(".bias") and name not in params_dict: continue # Remapping the name of FP8 kv-scale. name = maybe_remap_kv_scale_name(name, params_dict) if name is None: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = getattr(param, "weight_loader", default_weight_loader) weight_loader(param, loaded_weight) loaded_params.add(name) return loaded_params class Gemma3nForCausalLM(nn.Module): packed_modules_mapping = { "qkv_proj": [ "q_proj", "k_proj", "v_proj", ], "gate_up_proj": [ "gate_proj", "up_proj", ], } def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): config = vllm_config.model_config.hf_config super().__init__() self.config = config self.cache_config = vllm_config.cache_config self.model = Gemma3nTextModel( vllm_config=vllm_config, prefix=maybe_prefix(prefix, "model") ) self.logits_processor = LogitsProcessor( config.vocab_size, soft_cap=config.final_logit_softcapping ) def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.model.embed_input_ids(input_ids) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, *, per_layer_inputs: torch.Tensor | None = None, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, **kwargs, ) -> torch.Tensor | IntermediateTensors: hidden_states = self.model( input_ids, positions, per_layer_inputs=per_layer_inputs, intermediate_tensors=intermediate_tensors, inputs_embeds=inputs_embeds, **kwargs, ) return hidden_states def compute_logits( self, hidden_states: torch.Tensor, ) -> torch.Tensor | None: logits = self.logits_processor(self.model.embed_tokens, hidden_states) return logits def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: loader = AutoWeightsLoader( self, skip_substrs=( ["embed_audio.", "embed_vision.", "audio_tower.", "vision_tower."] ), ) return loader.load_weights(weights)
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/gemma3n.py", "license": "Apache License 2.0", "lines": 1058, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/config.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from copy import deepcopy from math import lcm from typing import TYPE_CHECKING from vllm.logger import init_logger from vllm.model_executor.models import ModelRegistry from vllm.platforms import current_platform from vllm.utils.math_utils import cdiv, round_up from vllm.utils.torch_utils import STR_DTYPE_TO_TORCH_DTYPE from vllm.v1.attention.backends.registry import AttentionBackendEnum from vllm.v1.kv_cache_interface import FullAttentionSpec, MambaSpec, MLAAttentionSpec if TYPE_CHECKING: from vllm.config import ModelConfig, VllmConfig logger = init_logger(__name__) class VerifyAndUpdateConfig: @staticmethod def verify_and_update_config(vllm_config: "VllmConfig") -> None: return @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: return class Gemma3TextModelConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: hf_config = model_config.hf_config hf_config.is_causal = not hf_config.use_bidirectional_attention class GteNewModelConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: config = model_config.hf_config assert config.__class__.__name__ == "NewConfig" assert config.hidden_act == "gelu" config.hidden_act = "geglu" head_dim = config.hidden_size // config.num_attention_heads rotary_dim = getattr(config, "rotary_emb_dim", head_dim) config.rope_parameters["partial_rotary_factor"] = rotary_dim / head_dim config.rotary_kwargs = { "head_size": head_dim, "max_position": config.max_position_embeddings, "rope_parameters": config.rope_parameters, } class JambaForSequenceClassificationConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: pooler_config = model_config.pooler_config if pooler_config.use_activation is None: pooler_config.use_activation = False class JinaRobertaModelConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: config = model_config.hf_config if config.position_embedding_type == "rotary": assert config.__class__.__name__ == "XLMRobertaFlashConfig" head_dim = config.hidden_size // config.num_attention_heads max_position = config.max_position_embeddings # Jina-embeddings-v3 has max_position_embeddings=8194, which will cause # out-of-bound index issue at RoPE for long prompts with torch.compile, # because it can't be divided by triton num_warps(default=4 or 8). # To deal with this, we increase max_position to multiple of n_warps, # so that triton kernel won't hit out-of-bound index in RoPE cache. if not model_config.enforce_eager: max_position = round_up(max_position, 8) rotary_dim = getattr(config, "rotary_emb_dim", head_dim) config.rope_parameters["partial_rotary_factor"] = rotary_dim / head_dim config.rotary_kwargs = { "head_size": head_dim, "max_position": max_position, "rope_parameters": config.rope_parameters, } class LlamaBidirectionalConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: from vllm.config.pooler import SequencePoolingType hf_config = model_config.hf_config hf_config.is_causal = False pooling_type_map: dict[str, SequencePoolingType] = { "avg": "MEAN", "cls": "CLS", "last": "LAST", } pooling_type = pooling_type_map.get(hf_config.pooling, None) if pooling_type is None: raise ValueError(f"pool_type {hf_config.pooling!r} not supported") model_config.pooler_config.seq_pooling_type = pooling_type class LlamaNemotronVLConfig(VerifyAndUpdateConfig): """Config handler for LlamaNemotronVL embedding models.""" @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: from vllm.config.pooler import SequencePoolingType hf_config = model_config.hf_config # Set bidirectional attention on the language model config hf_config.is_causal = False if hasattr(hf_config, "llm_config"): hf_config.llm_config.is_causal = False if hasattr(hf_config, "vision_config"): hf_config.patch_size = hf_config.vision_config.patch_size # Set up pooling type pooling_type_map: dict[str, SequencePoolingType] = { "avg": "MEAN", "cls": "CLS", "last": "LAST", } # Get pooling type from config (check both top-level and llm_config) pooling = getattr(hf_config, "pooling", None) if pooling is None and hasattr(hf_config, "llm_config"): pooling = getattr(hf_config.llm_config, "pooling", "avg") pooling_type = pooling_type_map.get(pooling) if pooling_type is None: raise ValueError(f"pool_type {pooling!r} not supported") model_config.pooler_config.seq_pooling_type = pooling_type class NomicBertModelConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: config = model_config.hf_config assert config.__class__.__name__ == "NomicBertConfig" assert config.activation_function in ["swiglu", "gelu"] config.position_embedding_type = getattr( config, "position_embedding_type", "rope" ) if config.activation_function == "swiglu": config.hidden_act = "silu" else: config.hidden_act = config.activation_function assert config.mlp_fc1_bias == config.mlp_fc2_bias == config.qkv_proj_bias config.bias = config.qkv_proj_bias assert config.rotary_emb_scale_base is None assert not config.rotary_emb_interleaved config.layer_norm_eps = config.layer_norm_epsilon config.intermediate_size = config.n_inner config.hidden_size = config.n_embd config.num_hidden_layers = config.n_layer model_config.model_arch_config.hidden_size = config.hidden_size model_config.model_arch_config.total_num_hidden_layers = ( config.num_hidden_layers ) head_dim = config.hidden_size // config.num_attention_heads max_trained_positions = getattr(config, "max_trained_positions", 2048) config.rotary_kwargs = { "head_size": head_dim, "max_position": max_trained_positions, "rope_parameters": config.rope_parameters, } # we ignore config.rotary_scaling_factor so that for datasets shorter # than max_trained_positions 2048, the results are consistent # with SentenceTransformer. # The context extension uses vllm style rope_theta and rope_parameters. # See #17785 #18755 if ( not model_config.hf_overrides and model_config.original_max_model_len is None ): # Default # Reset max_model_len to max_trained_positions. # nomic-embed-text-v2-moe the length is set to 512 # by sentence_bert_config.json. max_model_len_before = model_config.max_model_len max_model_len = min(model_config.max_model_len, max_trained_positions) model_config.max_model_len = model_config.get_and_verify_max_len( max_model_len ) if model_config.max_model_len != max_model_len_before: logger.warning( "Nomic context extension is disabled. " "Changing max_model_len from %s to %s. " "To enable context extension, see: " "https://github.com/vllm-project/vllm/tree/main/examples/offline_inference/context_extension.py", max_model_len_before, model_config.max_model_len, ) else: # We need to re-verify max_model_len to avoid lengths # greater than position_embedding. hf_text_config = model_config.hf_text_config if isinstance(model_config.hf_overrides, dict): # hf_overrides_kw max_model_len = model_config.hf_overrides.get( "max_model_len", model_config.max_model_len ) else: # hf_overrides_fn # This might be overridden by sentence_bert_config.json. max_model_len = model_config.max_model_len # reset hf_text_config for recalculate_max_model_len. if hasattr(hf_text_config, "max_model_len"): delattr(hf_text_config, "max_model_len") hf_text_config.max_position_embeddings = max_trained_positions hf_text_config.rope_parameters = config.rotary_kwargs["rope_parameters"] # Update the cached derived_max_model_len to enforce the limit model_config.model_arch_config.derived_max_model_len_and_key = ( float(max_trained_positions), "max_position_embeddings", ) # The priority of sentence_bert_config.json is higher # than max_position_embeddings encoder_config = deepcopy(model_config.encoder_config) encoder_config.pop("max_seq_length", None) model_config.encoder_config = encoder_config model_config.max_model_len = model_config.get_and_verify_max_len( max_model_len ) class Qwen2ForProcessRewardModelConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: pooler_config = model_config.pooler_config if pooler_config.step_tag_id is None: pooler_config.step_tag_id = 151651 class Qwen2ForRewardModelConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: pooler_config = model_config.pooler_config if pooler_config.use_activation is None: pooler_config.use_activation = False class Qwen3ForSequenceClassificationConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: config = model_config.hf_config is_original_qwen3_reranker = getattr( config, "is_original_qwen3_reranker", False ) if not is_original_qwen3_reranker: return tokens = getattr(config, "classifier_from_token", None) assert tokens is not None and len(tokens) == 2, ( "Try loading the original Qwen3 Reranker?, see: " "https://github.com/vllm-project/vllm/tree/main/examples/pooling/score/qwen3_reranker_offline.py" ) text_config = config.get_text_config() text_config.method = "from_2_way_softmax" text_config.classifier_from_token = tokens class Qwen3VLForSequenceClassificationConfig(Qwen3ForSequenceClassificationConfig): pass class JinaVLForSequenceClassificationConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: config = model_config.hf_config config.num_labels = 1 pooler_config = model_config.pooler_config if pooler_config.logit_bias is None: pooler_config.logit_bias = 2.65 class SnowflakeGteNewModelConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: config = model_config.hf_config assert config.__class__.__name__ == "GteConfig" assert config.hidden_act == "gelu" config.hidden_act = "geglu" head_dim = config.hidden_size // config.num_attention_heads rotary_dim = getattr(config, "rotary_emb_dim", head_dim) config.rope_parameters["partial_rotary_factor"] = rotary_dim / head_dim config.rotary_kwargs = { "head_size": head_dim, "max_position": config.max_position_embeddings, "rope_parameters": config.rope_parameters, } class GptOssForCausalLMConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_config(vllm_config: "VllmConfig") -> None: structured_outputs_config = vllm_config.structured_outputs_config if structured_outputs_config.reasoning_parser == "": structured_outputs_config.reasoning_parser = "openai_gptoss" # Increase the max capture size from 512 to 1024 for performance. # NOTE(woosuk): This will increase the number of CUDA graphs # from 67 to 83. compilation_config = vllm_config.compilation_config # Only override when the user has not set either of # cudagraph_capture_sizes or max_cudagraph_capture_size. if ( compilation_config.cudagraph_capture_sizes is None and compilation_config.max_cudagraph_capture_size is None ): compilation_config.max_cudagraph_capture_size = 1024 logger.info( "Overriding max cuda graph capture size to %d for performance.", 1024 ) class MambaModelConfig(VerifyAndUpdateConfig): @classmethod def verify_and_update_config(cls, vllm_config: "VllmConfig") -> None: """ Enable FULL_AND_PIECEWISE cuda graph mode by default (required to get good performance for mamba layers in V1). Args: vllm_config: vLLM Config """ model_config = vllm_config.model_config cache_config = vllm_config.cache_config if cache_config.enable_prefix_caching: if cache_config.mamba_cache_mode == "none": cache_config.mamba_cache_mode = ( "all" if model_config.supports_mamba_prefix_caching else "align" ) logger.warning( "Mamba cache mode is set to '%s' for %s by default " "when prefix caching is enabled", cache_config.mamba_cache_mode, model_config.architecture, ) if ( cache_config.mamba_cache_mode == "all" and not model_config.supports_mamba_prefix_caching ): cache_config.mamba_cache_mode = "align" logger.warning( "Hybrid or mamba-based model detected without support " "for prefix caching with Mamba cache 'all' mode: " "falling back to 'align' mode." ) if cache_config.mamba_cache_mode == "align": assert vllm_config.scheduler_config.enable_chunked_prefill, ( "Chunked prefill is required for mamba cache mode 'align'." ) logger.info( "Warning: Prefix caching in Mamba cache '%s' " "mode is currently enabled. " "Its support for Mamba layers is experimental. " "Please report any issues you may observe.", cache_config.mamba_cache_mode, ) # By default, mamba block size will be set to max_model_len (see # below). When enabling prefix caching, we align mamba block size # to the block size as the basic granularity for prefix caching. if cache_config.mamba_block_size is None: cache_config.mamba_block_size = cache_config.block_size else: if cache_config.mamba_cache_mode != "none": cache_config.mamba_cache_mode = "none" logger.warning( "Mamba cache mode is set to 'none' when prefix caching is disabled" ) if cache_config.mamba_block_size is None: cache_config.mamba_block_size = model_config.max_model_len class HybridAttentionMambaModelConfig(VerifyAndUpdateConfig): @classmethod def verify_and_update_config(cls, vllm_config: "VllmConfig") -> None: """ Ensure that page size of attention layers is greater than or equal to the mamba layers. If not, automatically set the attention block size to ensure that it is. If the attention page size is strictly greater than the mamba page size, we pad the mamba page size to make them equal. Args: vllm_config: vLLM Config """ # Save the user input before it gets modified by MambaModelConfig mamba_block_size = vllm_config.cache_config.mamba_block_size # Enable FULL_AND_PIECEWISE by default MambaModelConfig.verify_and_update_config(vllm_config) attention_config = vllm_config.attention_config cache_config = vllm_config.cache_config model_config = vllm_config.model_config parallel_config = vllm_config.parallel_config if cache_config.cache_dtype == "auto": kv_cache_dtype = model_config.dtype else: kv_cache_dtype = STR_DTYPE_TO_TORCH_DTYPE[cache_config.cache_dtype] # get attention page size (for 1 token) # Attention backend constraints: # - FlashAttention (FA) requires block size to be multiple of 16 # - MLA (Multi-head Latent Attention) requires larger alignment: # * CUTLASS_MLA backend: kernel_block_size 128 alignment # * Other MLA backends: kernel_block_size 64 alignment if model_config.use_mla: use_cutlass_mla = ( attention_config.backend == AttentionBackendEnum.CUTLASS_MLA ) kernel_block_alignment_size = 128 if use_cutlass_mla else 64 attn_page_size_1_token = MLAAttentionSpec( block_size=1, num_kv_heads=model_config.get_num_kv_heads(parallel_config), head_size=model_config.get_head_size(), dtype=kv_cache_dtype, ).page_size_bytes else: kernel_block_alignment_size = 16 if ( current_platform.is_device_capability_family(100) and model_config.get_head_size() == 256 and ( attention_config.backend is None or attention_config.backend == AttentionBackendEnum.FLASHINFER ) ): # https://github.com/flashinfer-ai/flashinfer/issues/1993 reports that` # head size 256 and block size 16 is not supported on blackwell. kernel_block_alignment_size = 32 attn_page_size_1_token = FullAttentionSpec( block_size=1, num_kv_heads=model_config.get_num_kv_heads(parallel_config), head_size=model_config.get_head_size(), dtype=kv_cache_dtype, ).page_size_bytes model_cls, _ = ModelRegistry.resolve_model_cls( model_config.architecture, model_config=model_config, ) # get mamba page size mamba_page_size = MambaSpec( shapes=model_cls.get_mamba_state_shape_from_config(vllm_config), dtypes=model_cls.get_mamba_state_dtype_from_config(vllm_config), block_size=-1, # block_size doesn't matter for mamba page size ).page_size_bytes # Model may be marked as is_hybrid # but mamba is skipped via config, # return directly if mamba_page_size == 0: return if cache_config.mamba_cache_mode == "all": # With prefix caching, select attention block size to # optimize for mamba kernel performance # Mamba2 SSD kernel uses a chunk_size, e.g. 256 # Align the block to the kernel: use lowest multiple of chunk_size # of attention tokens that would fit mamba_page_size: # e.g. for mamba page size = 788kB # attn_1_token = 2kB -> fits ~394 tokens # then round up to a multiple of 256 -> 512 tokens # End result: # attn_block_size = 512 # mamba_block_size = 512 (aligned to a multiple of chunk_size) # TODO(tdoublep): this constraint can be relaxed fairly # easily by changing the way we layout chunks in the # mamba2 kernels. base_chunk_size = mamba_block_size or model_config.get_mamba_chunk_size() attn_tokens_per_mamba_state = cdiv(mamba_page_size, attn_page_size_1_token) chunk_size = lcm(base_chunk_size, kernel_block_alignment_size) attn_block_size = chunk_size * cdiv(attn_tokens_per_mamba_state, chunk_size) cache_config.mamba_block_size = attn_block_size else: # Without prefix caching, select minimum valid attention block size # to minimize mamba state padding # Calculate minimum attention block size that satisfies both: # 1. Backend alignment requirements (kernel_block_alignment_size) # 2. Mamba page size compatibility (attn_page_size >= mamba_page_size) attn_block_size = kernel_block_alignment_size * cdiv( mamba_page_size, kernel_block_alignment_size * attn_page_size_1_token ) # override attention block size if either (a) the # user has not set it or (b) the user has set it # too small. if cache_config.block_size is None or cache_config.block_size < attn_block_size: cache_config.block_size = attn_block_size logger.info( "Setting attention block size to %d tokens " "to ensure that attention page size is >= mamba page size.", attn_block_size, ) # By default, mamba block size will be set to max_model_len. # When enabling prefix caching and using align mamba cache # mode, we align mamba block size to the block size as the # basic granularity for prefix caching. if cache_config.mamba_cache_mode == "align": cache_config.mamba_block_size = cache_config.block_size # compute new attention page size attn_page_size = cache_config.block_size * attn_page_size_1_token assert attn_page_size >= mamba_page_size if attn_page_size == mamba_page_size: # don't need to pad mamba page size return # pad mamba page size to exactly match attention if ( cache_config.mamba_page_size_padded is None or cache_config.mamba_page_size_padded != attn_page_size ): cache_config.mamba_page_size_padded = attn_page_size mamba_padding_pct = ( 100 * (attn_page_size - mamba_page_size) / mamba_page_size ) logger.info( "Padding mamba page size by %.2f%% to ensure " "that mamba page size and attention page size are " "exactly equal.", mamba_padding_pct, ) class DeepseekV32ForCausalLM(VerifyAndUpdateConfig): @classmethod def verify_and_update_config(cls, vllm_config: "VllmConfig") -> None: """ Updated fp8 cache to custom "fp8_ds_mla" format for DeepSeekV32 """ hf_config = vllm_config.model_config.hf_config # Mirror the check in vllm/model_executor/models/deepseek_v2.py is_v32 = hasattr(hf_config, "index_topk") assert is_v32 # For DeepSeekV3.2, a custom fp8 format is used when fp8 kv-cache is enabled. cache_config = vllm_config.cache_config if cache_config.cache_dtype.startswith("fp8"): cache_config.cache_dtype = "fp8_ds_mla" logger.info("Using custom fp8 kv-cache format for DeepSeekV3.2") if cache_config.cache_dtype == "bfloat16": cache_config.cache_dtype = "auto" logger.info("Using bfloat16 kv-cache for DeepSeekV3.2") class NemotronHForCausalLMConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_config(vllm_config: "VllmConfig") -> None: """Update mamba_ssm_cache_dtype for NemotronH models when set to 'auto' (or not explicitly set), to the value specified in the HF config, or to float16 if not specified. """ cache_config = vllm_config.cache_config if cache_config.mamba_ssm_cache_dtype == "auto": hf_config = vllm_config.model_config.hf_config mamba_ssm_cache_dtype = getattr( hf_config, "mamba_ssm_cache_dtype", "float16" ) logger.info( "Updating mamba_ssm_cache_dtype to '%s' for NemotronH model", mamba_ssm_cache_dtype, ) cache_config.mamba_ssm_cache_dtype = mamba_ssm_cache_dtype class Qwen3_5ForConditionalGenerationConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_config(vllm_config: "VllmConfig") -> None: """Update mamba_ssm_cache_dtype for Qwen3.5 models when set to 'auto' (or not explicitly set), to the value specified in the HF config's mamba_ssm_dtype field. Warn if the user explicitly overrides it to a different value. """ cache_config = vllm_config.cache_config hf_text_config = vllm_config.model_config.hf_text_config mamba_ssm_dtype = getattr(hf_text_config, "mamba_ssm_dtype", None) if cache_config.mamba_ssm_cache_dtype == "auto": if mamba_ssm_dtype is not None: cache_config.mamba_ssm_cache_dtype = mamba_ssm_dtype elif ( mamba_ssm_dtype is not None and cache_config.mamba_ssm_cache_dtype != mamba_ssm_dtype ): logger.warning( "Qwen3.5 model specifies mamba_ssm_dtype='%s' in its config, " "but --mamba-ssm-cache-dtype='%s' was passed. " "Using the user-specified value.", mamba_ssm_dtype, cache_config.mamba_ssm_cache_dtype, ) class VoyageQwen3BidirectionalEmbedModelConfig(VerifyAndUpdateConfig): @staticmethod def verify_and_update_model_config(model_config: "ModelConfig") -> None: model_config.hf_config.is_causal = False model_config.hf_config.embedding_size = model_config.hf_config.num_labels MODELS_CONFIG_MAP: dict[str, type[VerifyAndUpdateConfig]] = { "GteModel": SnowflakeGteNewModelConfig, "GteNewModel": GteNewModelConfig, "GteNewForSequenceClassification": GteNewModelConfig, "Gemma3TextModel": Gemma3TextModelConfig, "LlamaBidirectionalForSequenceClassification": LlamaBidirectionalConfig, "LlamaBidirectionalModel": LlamaBidirectionalConfig, "LlamaNemotronVLModel": LlamaNemotronVLConfig, "NomicBertModel": NomicBertModelConfig, "Qwen2ForProcessRewardModel": Qwen2ForProcessRewardModelConfig, "Qwen2ForRewardModel": Qwen2ForRewardModelConfig, "Qwen3ForSequenceClassification": Qwen3ForSequenceClassificationConfig, "Qwen3VLForSequenceClassification": Qwen3VLForSequenceClassificationConfig, "XLMRobertaModel": JinaRobertaModelConfig, "ColBERTJinaRobertaModel": JinaRobertaModelConfig, "JinaVLForRanking": JinaVLForSequenceClassificationConfig, "JambaForSequenceClassification": JambaForSequenceClassificationConfig, "GptOssForCausalLM": GptOssForCausalLMConfig, "MambaForCausalLM": MambaModelConfig, "Mamba2ForCausalLM": MambaModelConfig, "FalconMambaForCausalLM": MambaModelConfig, "DeepseekV32ForCausalLM": DeepseekV32ForCausalLM, "NemotronHForCausalLM": NemotronHForCausalLMConfig, "NemotronHPuzzleForCausalLM": NemotronHForCausalLMConfig, "Qwen3_5ForConditionalGeneration": Qwen3_5ForConditionalGenerationConfig, "Qwen3_5MoeForConditionalGeneration": Qwen3_5ForConditionalGenerationConfig, "VoyageQwen3BidirectionalEmbedModel": VoyageQwen3BidirectionalEmbedModelConfig, }
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/config.py", "license": "Apache License 2.0", "lines": 566, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/distributed/test_quick_all_reduce.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import multiprocessing import random import pytest import ray import torch import torch.distributed as dist from vllm import _custom_ops as ops from vllm.distributed.communication_op import tensor_model_parallel_all_reduce # noqa from vllm.distributed.parallel_state import get_tp_group, graph_capture from vllm.platforms import current_platform from ..utils import ( ensure_model_parallel_initialized, init_test_distributed_environment, multi_process_parallel, ) torch.manual_seed(42) random.seed(44) # Size over 8MB is sufficient for custom quick allreduce. test_sizes = [random.randint(8 * 1024 * 1024, 10 * 1024 * 1024) for _ in range(8)] for i, v in enumerate(test_sizes): test_sizes[i] -= v % 8 @ray.remote(num_gpus=1, max_calls=1) def graph_quickreduce( monkeypatch: pytest.MonkeyPatch, tp_size, pp_size, rank, distributed_init_port, ): with monkeypatch.context() as m: m.delenv("CUDA_VISIBLE_DEVICES", raising=False) device = torch.device(f"cuda:{rank}") torch.cuda.set_device(device) init_test_distributed_environment(tp_size, pp_size, rank, distributed_init_port) ensure_model_parallel_initialized(tp_size, pp_size) group = get_tp_group().device_group # A small all_reduce for warmup. # this is needed because device communicators might be created lazily # (e.g. NCCL). This will ensure that the communicator is initialized # before any communication happens, so that this group can be used for # graph capture immediately. data = torch.zeros(1) data = data.to(device=device) torch.distributed.all_reduce(data, group=group) torch.cuda.synchronize() del data # we use the first group to communicate once # and the second group to communicate twice # and so on # this is used to demonstrate that each group can # communicate independently num_communication = rank // tp_size + 1 for sz in test_sizes: for dtype in [torch.float16, torch.bfloat16]: with graph_capture(device=device) as graph_capture_context: inp1 = torch.randint( 1, 23, (sz,), dtype=dtype, device=torch.cuda.current_device() ) inp2 = torch.randint( -23, 1, (sz,), dtype=dtype, device=torch.cuda.current_device() ) torch.cuda.synchronize() graph = torch.cuda.CUDAGraph() with torch.cuda.graph(graph, stream=graph_capture_context.stream): for _ in range(num_communication): out1 = tensor_model_parallel_all_reduce(inp1) dist.all_reduce(inp1, group=group) out2 = tensor_model_parallel_all_reduce(inp2) dist.all_reduce(inp2, group=group) graph.replay() torch.testing.assert_close(out1, inp1, atol=2.5, rtol=0.1) torch.testing.assert_close(out2, inp2, atol=2.5, rtol=0.1) @ray.remote(num_gpus=1, max_calls=1) def eager_quickreduce( monkeypatch: pytest.MonkeyPatch, tp_size, pp_size, rank, distributed_init_port, ): with monkeypatch.context() as m: m.delenv("CUDA_VISIBLE_DEVICES", raising=False) device = torch.device(f"cuda:{rank}") torch.cuda.set_device(device) init_test_distributed_environment(tp_size, pp_size, rank, distributed_init_port) # Size over 8MB is sufficient for custom quick allreduce. sz = 16 * 1024 * 1024 fa = get_tp_group().device_communicator.qr_comm inp = torch.tensor( [1.0 * ((i) % 23) for i in range(sz)], dtype=torch.float16, device=device ) out = fa.quick_all_reduce(inp) torch.testing.assert_close(out, inp * tp_size, atol=2.5, rtol=0.1) inp = torch.tensor( [1.0 * ((i) % 23) for i in range(sz)], dtype=torch.bfloat16, device=device ) out = fa.quick_all_reduce(inp) torch.testing.assert_close(out, inp * tp_size, atol=2.5, rtol=0.1) @pytest.mark.skipif( not current_platform.is_rocm(), reason="only test quick allreduce for rocm" ) @pytest.mark.parametrize("quant_mode", ["FP", "INT8", "INT6", "INT4"]) @pytest.mark.parametrize("tp_size", [2]) @pytest.mark.parametrize("pipeline_parallel_size", [1, 2]) @pytest.mark.parametrize("test_target", [graph_quickreduce, eager_quickreduce]) def test_custom_quick_allreduce( monkeypatch: pytest.MonkeyPatch, tp_size, pipeline_parallel_size, test_target, quant_mode, ): world_size = tp_size * pipeline_parallel_size if world_size > torch.cuda.device_count(): pytest.skip("Not enough GPUs to run the test.") monkeypatch.setenv("VLLM_ROCM_QUICK_REDUCE_QUANTIZATION", quant_mode) multi_process_parallel(monkeypatch, tp_size, pipeline_parallel_size, test_target) def qr_variable_input(rank, world_size): """ When the tensor parallelism is set to 4 or 8, frequent changes in the input shape can cause QuickReduce to hang (this issue has been observed with the gpt_oss model). """ device = torch.device(f"cuda:{rank}") torch.cuda.set_device(device) qr_max_size = None # MB _ptr = ops.init_custom_qr(rank, world_size, qr_max_size) ranks = [] for i in range(world_size): ranks.append(i) dist.init_process_group( backend="nccl", init_method="tcp://127.0.0.1:29500", rank=rank, world_size=world_size, ) cpu_group = torch.distributed.new_group(ranks, backend="nccl") handle = ops.qr_get_handle(_ptr) world_size = dist.get_world_size(group=cpu_group) handles = [None] * world_size dist.all_gather_object(handles, handle, group=cpu_group) ops.qr_open_handles(_ptr, handles) num = 1 s1 = 1024 while num < 50000: # 50000 is sufficient to identify issues. dtype = torch.float16 if num % 2 == 0: s2 = 1024 inp1 = torch.zeros( (s1, s2), dtype=dtype, device=torch.cuda.current_device() ) else: s2 = 2048 inp1 = torch.ones((s1, s2), dtype=dtype, device=torch.cuda.current_device()) result = torch.empty_like(inp1) # FP = 0 INT8 = 1 INT6 = 2 INT4 = 3 NONE = 4 ops.qr_all_reduce(_ptr, inp1, result, 3, cast_bf2half=True) try: if inp1[0, 0] == 0: assert torch.all(result == 0) else: assert torch.all(result == world_size) except AssertionError: print("Assertion failed! Allreduce results are incorrect.") raise num += 1 @pytest.mark.skipif( not current_platform.is_rocm(), reason="only test quick allreduce for rocm" ) @pytest.mark.parametrize("tp_size", [4, 8]) @pytest.mark.parametrize("pipeline_parallel_size", [1]) def test_custom_quick_allreduce_variable_input(tp_size, pipeline_parallel_size): world_size = tp_size * pipeline_parallel_size if world_size > torch.cuda.device_count(): pytest.skip("Not enough GPUs to run the test.") multiprocessing.set_start_method("spawn", force=True) # 60s is enough timeout = 60 processes = [] for rank in range(tp_size): p = multiprocessing.Process(target=qr_variable_input, args=(rank, tp_size)) p.start() processes.append((rank, p)) for rank, p in processes: p.join(timeout=timeout) if p.is_alive(): for r, proc in processes: if proc.is_alive(): proc.terminate() proc.join() raise RuntimeError(f"QuickReduce hang detected after {timeout} seconds!") if __name__ == "__main__": test_custom_quick_allreduce_variable_input(tp_size=4, pipeline_parallel_size=1)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/distributed/test_quick_all_reduce.py", "license": "Apache License 2.0", "lines": 195, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/distributed/device_communicators/quick_all_reduce.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from enum import Enum import torch import torch.distributed as dist from torch.distributed import ProcessGroup import vllm.envs as envs from vllm import _custom_ops as ops from vllm.config import get_current_vllm_config_or_none from vllm.distributed.parallel_state import in_the_same_node_as from vllm.logger import init_logger from vllm.platforms import current_platform from vllm.utils.torch_utils import cuda_device_count_stateless logger = init_logger(__name__) try: ops.qr_max_size() quick_ar = True except Exception: # For CPUs and CUDA quick_ar = False def is_weak_contiguous(inp: torch.Tensor): return inp.is_contiguous() or ( inp.storage().nbytes() - inp.storage_offset() * inp.element_size() == inp.numel() * inp.element_size() ) class QuickReduceRegime(Enum): FP = 0 INT8 = 1 INT6 = 2 INT4 = 3 NONE = 4 MB = 1024 * 1024 class QuickAllReduce: _SUPPORTED_WORLD_SIZES = [2, 4, 8] _SUPPORTED_DTYPES = [torch.float16, torch.bfloat16] # The following data is based on kernel tests. # In this order [FP, INT8, INT6, INT4]. _QR_MIN_SIZE = { (torch.float16, 2): [1 * MB, 2 * MB, 2 * MB, 1 * MB], (torch.float16, 4): [1 * MB, 16 * MB, 4 * MB, 2 * MB], (torch.float16, 8): [16 * MB, 4 * MB, 4 * MB, 2 * MB], (torch.bfloat16, 2): [2 * MB, 8 * MB, 8 * MB, 8 * MB], (torch.bfloat16, 4): [8 * MB, 64 * MB, 64 * MB, 16 * MB], (torch.bfloat16, 8): [16 * MB, 2048 * MB, 2048 * MB, 2048 * MB], } def __init__(self, group: ProcessGroup, device: int | str | torch.device) -> None: """ Custom allreduce provides non-destructive acceleration and is available for CUDA and ROCm MI300 series. Custom quick allreduce leverages quantization for further acceleration on ROCm. It currently supports Q8, Q6, and Q4 quantization formats and FP(float16, bfloat16). Quick allreduce is designed as a complement to custom allreduce. Its initialization requires even stricter conditions. Only the ROCm MI300 series is supported for quick allreduce at this time. Args: group: the process group to work on. If None, it will use the default process group. device: the device to bind the CustomAllreduce to. If None, it will be bound to f"cuda:{local_rank}". It is the caller's responsibility to make sure each communicator is bind to a unique device, and all communicators in this group are in the same node. """ self.disabled = True if not self._rocm_arch_available(): logger.debug( "Custom quick allreduce is only supported on ROCm MI300 series." ) return if not quick_ar: # disable because of missing quick reduce library # e.g. in a cuda environment logger.info( "Custom quick allreduce is disabled because " "of missing custom quick allreduce library" ) return self.group = group assert dist.get_backend(group) != dist.Backend.NCCL, ( "Custom quick allreduce should be attached to a non-NCCL group." ) if not all(in_the_same_node_as(group, source_rank=0)): # No need to initialize custom quick allreduce for # multi-node case. logger.warning( "Custom quick allreduce is disabled because this " "process group spans across nodes." ) return rank = dist.get_rank(group=self.group) world_size = dist.get_world_size(group=self.group) self.rank = rank self.world_size = world_size if world_size == 1: # No need to initialize QuickReduce for single GPU case. return if world_size not in QuickAllReduce._SUPPORTED_WORLD_SIZES: logger.warning( "Custom quick allreduce is disabled due to an " "unsupported world size: %d. Supported world sizes: %s.", world_size, str(QuickAllReduce._SUPPORTED_WORLD_SIZES), ) return if isinstance(device, int): device = torch.device(f"cuda:{device}") elif isinstance(device, str): device = torch.device(device) assert isinstance(device, torch.device) self.device = device cuda_visible_devices = envs.CUDA_VISIBLE_DEVICES if cuda_visible_devices: device_ids = list(map(int, cuda_visible_devices.split(","))) else: device_ids = list(range(cuda_device_count_stateless())) physical_device_id = device_ids[device.index] tensor = torch.tensor([physical_device_id], dtype=torch.int, device="cpu") gather_list = [ torch.tensor([0], dtype=torch.int, device="cpu") for _ in range(self.world_size) ] dist.all_gather(gather_list, tensor, group=self.group) physical_device_ids = [t.item() for t in gather_list] # test nvlink first, this will filter out most of the cases # where custom quick allreduce is not supported # this checks hardware and driver support for NVLink assert current_platform.is_cuda_alike() self.fully_connected = current_platform.is_fully_connected(physical_device_ids) if self.world_size > 2 and not self.fully_connected: logger.debug( "Custom quick allreduce is disabled because it's not supported " "on more than two PCIe-only GPUs. " ) return self.init_quick_all_reduce() def init_quick_all_reduce(self): # On RocM, bfloat16 kernels are slower than fp16 # due to slower match operations # If environment variable is set to 1, we convert input to fp16 self.use_fp16_kernels = envs.VLLM_ROCM_QUICK_REDUCE_CAST_BF16_TO_FP16 regime_str = envs.VLLM_ROCM_QUICK_REDUCE_QUANTIZATION if regime_str not in QuickReduceRegime.__members__: logger.warning( "Custom quick allreduce:", f"Invalid quantization level: {regime_str}. " "Supported levels: " f"{list(QuickReduceRegime.__members__.keys())}", ) return if regime_str == "NONE": logger.debug( "Custom quick allreduce is disabled based " "on env variable " "VLLM_ROCM_QUICK_REDUCE_QUANTIZATION='NONE'" ) return self.qr_quant_level = QuickReduceRegime[regime_str] vllm_config = get_current_vllm_config_or_none() if ( vllm_config is not None and hasattr(vllm_config, "model_config") and hasattr(vllm_config.model_config, "dtype") ): dtype = vllm_config.model_config.dtype if dtype not in [torch.float16, torch.bfloat16]: logger.debug( "Custom quick allreduce disabled: only supports " "float16 and float16, but get %s.", dtype, ) return if dtype == torch.bfloat16 and self.use_fp16_kernels: logger.info( "Custom quick allreduce: BF16 inputs will be converted " "to FP16 to improve performance. set " "envs.VLLM_ROCM_QUICK_REDUCE_CAST_BF16_TO_FP16=0 " "to turn off." ) # VLLM_ROCM_QUICK_REDUCE_MAX_SIZE_BYTES_MB is specified in MB qr_max_size = envs.VLLM_ROCM_QUICK_REDUCE_MAX_SIZE_BYTES_MB if qr_max_size is not None: if qr_max_size < 1: logger.info( "You should not set a max_size smaller than 1MB, which can " "lead to error or degradation to custom allreduce or rccl." ) qr_max_size = qr_max_size * MB self._ptr = ops.init_custom_qr(self.rank, self.world_size, qr_max_size) self.qr_max_size = qr_max_size if qr_max_size is not None else ops.qr_max_size() self.create_shared_buffer() self.disabled = False def _rocm_arch_available(self): if not current_platform.is_rocm(): return False try: props = torch.cuda.get_device_properties(0) gcn_arch = getattr(props, "gcnArchName", "") supported_archs = ["gfx94", "gfx95"] return any(gfx in gcn_arch for gfx in supported_archs) except Exception as e: logger.warning("Failed to determine ROCm for quick allreduce: %s", e) return False def create_shared_buffer(self): """ Creates a shared buffer for quickreduce. Has to be called after init_custom_qr """ handle = ops.qr_get_handle(self._ptr) world_size = dist.get_world_size(group=self.group) handles = [None] * world_size dist.all_gather_object(handles, handle, group=self.group) ops.qr_open_handles(self._ptr, handles) def should_quick_allreduce(self, inp: torch.Tensor): """ Check if quickreduce is available """ if self.disabled: return False if inp.dtype not in self._SUPPORTED_DTYPES: return False inp_size = inp.numel() * inp.element_size() # custom quick allreduce requires input byte size to be # multiples of 16 if inp_size % 16 != 0: return False if not is_weak_contiguous(inp): return False dtype = inp.dtype if self.use_fp16_kernels: dtype = torch.float16 return ( inp_size <= self.qr_max_size and inp_size >= self._QR_MIN_SIZE[(dtype, self.world_size)][self.qr_quant_level.value] ) def quick_all_reduce(self, inp: torch.Tensor, *, out: torch.Tensor = None): """Performs an out-of-place custom quick all reduce.""" # quick allreduce doesn't require a separate graph mode, # as QR uses static IPC buffer. if out is None: out = torch.empty_like(inp) ops.qr_all_reduce( self._ptr, inp, out, self.qr_quant_level.value, self.use_fp16_kernels ) return out def close(self): if not self.disabled and getattr(self, "_ptr", None): if ops is not None: ops.qr_destroy(self._ptr) self._ptr = 0 self.disabled = True def __del__(self): self.close()
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/distributed/device_communicators/quick_all_reduce.py", "license": "Apache License 2.0", "lines": 255, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/distributed/test_eplb_algo.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import numpy as np import pytest import torch from vllm.distributed.eplb.policy.default import DefaultEplbPolicy def test_basic_rebalance(): """Test basic rebalancing functionality""" # Example from https://github.com/deepseek-ai/eplb weight = torch.tensor( [ [90, 132, 40, 61, 104, 165, 39, 4, 73, 56, 183, 86], [20, 107, 104, 64, 19, 197, 187, 157, 172, 86, 16, 27], ] ) num_layers = weight.shape[0] num_replicas = 16 num_groups = 4 num_nodes = 2 num_gpus = 8 phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts( weight, num_replicas, num_groups, num_nodes, num_gpus ) # Verify output shapes assert phy2log.shape == ( 2, 16, ), f"Expected `phy2log` shape (2, 16), got {phy2log.shape}" assert log2phy.shape[0] == 2, ( f"Expected `log2phy` first dimension 2, got {log2phy.shape[0]}" ) assert log2phy.shape[1] == 12, ( f"Expected `log2phy` second dimension 12, got {log2phy.shape[1]}" ) assert logcnt.shape == ( 2, 12, ), f"Expected `logcnt` shape (2, 12), got {logcnt.shape}" # Verify physical to logical expert mapping range is correct assert torch.all(phy2log >= 0) and torch.all(phy2log < 12), ( "Physical to logical mapping should be in range [0, 12)" ) # Verify expert count reasonableness assert torch.all(logcnt >= 1), "Each logical expert should have at least 1 replica" assert torch.sum(logcnt, dim=1).sum() == num_replicas * num_layers, ( f"Total replicas should be {num_replicas * num_layers}" ) # Verify expected output expected_phy2log = torch.tensor( [ [5, 6, 5, 7, 8, 4, 3, 4, 10, 9, 10, 2, 0, 1, 11, 1], [7, 10, 6, 8, 6, 11, 8, 9, 2, 4, 5, 1, 5, 0, 3, 1], ] ) assert torch.all(phy2log == expected_phy2log) expected_logcnt = torch.tensor( [[1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1], [1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 1]] ) assert torch.all(logcnt == expected_logcnt) def test_single_gpu_case(): """Test single GPU case""" weight = torch.tensor([[10, 20, 30, 40]]) num_replicas = 4 num_groups = 1 num_nodes = 1 num_gpus = 1 phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts( weight, num_replicas, num_groups, num_nodes, num_gpus ) # Verify shapes assert phy2log.shape == (1, 4) assert log2phy.shape[0] == 1 assert log2phy.shape[1] == 4 assert logcnt.shape == (1, 4) # Verify all logical experts are mapped assert set(phy2log[0].tolist()) == {0, 1, 2, 3} def test_equal_weights(): """Test case with equal weights""" weight = torch.tensor([[50, 50, 50, 50, 50, 50, 50, 50]]) num_replicas = 8 num_groups = 2 num_nodes = 2 num_gpus = 4 phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts( weight, num_replicas, num_groups, num_nodes, num_gpus ) # Verify shapes assert phy2log.shape == (1, 8) assert logcnt.shape == (1, 8) # With equal weights, each expert should have exactly one replica assert torch.all(logcnt == 1), ( "With equal weights and no replication, " "each expert should have exactly 1 replica" ) def test_extreme_weight_imbalance(): """Test extreme weight imbalance case""" weight = torch.tensor([[1000, 1, 1, 1, 1, 1, 1, 1]]) num_replicas = 12 num_groups = 2 num_nodes = 2 num_gpus = 4 phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts( weight, num_replicas, num_groups, num_nodes, num_gpus ) # Verify shapes assert phy2log.shape == (1, 12) assert logcnt.shape == (1, 8) # Expert with highest weight (index 0) should have more replicas assert logcnt[0, 0] > logcnt[0, 1], ( "Expert with highest weight should have more replicas" ) def test_multiple_layers(): """Test multiple layers case""" weight = torch.tensor( [ [10, 20, 30, 40, 50, 60], # First layer [60, 50, 40, 30, 20, 10], # Second layer (opposite weight pattern) [25, 25, 25, 25, 25, 25], # Third layer (equal weights) ] ) num_replicas = 8 num_groups = 2 num_nodes = 2 num_gpus = 4 phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts( weight, num_replicas, num_groups, num_nodes, num_gpus ) # Verify shapes assert phy2log.shape == (3, 8) assert logcnt.shape == (3, 6) # Verify expert allocation is reasonable for each layer for layer in range(3): assert torch.all(phy2log[layer] >= 0) and torch.all(phy2log[layer] < 6), ( f"Layer {layer} physical to logical mappingshould be in range [0, 6)" ) assert torch.sum(logcnt[layer]) == num_replicas, ( f"Layer {layer} total replicas should be {num_replicas}" ) def test_parameter_validation(): """Test parameter validation""" weight = torch.tensor([[10, 20, 30, 40]]) # Test non-divisible case - this should handle normally without throwing # errors because the function will fall back to global load balancing # strategy phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts(weight, 8, 3, 2, 4) assert phy2log.shape == (1, 8) assert logcnt.shape == (1, 4) # Test cases that will actually cause errors: # num_physical_experts not divisible by num_gpus with pytest.raises(AssertionError): DefaultEplbPolicy.rebalance_experts(weight, 7, 2, 2, 4) # 7 not divisible by 4 def test_small_scale_hierarchical(): """Test small-scale hierarchical load balancing""" weight = torch.tensor( [ [100, 50, 200, 75, 150, 25, 300, 80], # 8 experts ] ) num_replicas = 12 num_groups = 4 # 4 groups, 2 experts each num_nodes = 2 # 2 nodes num_gpus = 4 # 4 GPUs phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts( weight, num_replicas, num_groups, num_nodes, num_gpus ) # Verify basic constraints assert phy2log.shape == (1, 12) assert logcnt.shape == (1, 8) assert torch.sum(logcnt) == num_replicas assert torch.all(logcnt >= 1) # Expert with highest weight should have more replicas max_weight_expert = torch.argmax(weight[0]) assert logcnt[0, max_weight_expert] >= 2, ( "Highest weight expert should have multiple replicas" ) def test_global_load_balance_fallback(): """Test global load balancing fallback case""" # When num_groups % num_nodes != 0, should fall back to global load # balancing weight = torch.tensor([[10, 20, 30, 40, 50, 60]]) num_replicas = 8 num_groups = 3 # Cannot be divided evenly by num_nodes=2 num_nodes = 2 num_gpus = 4 phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts( weight, num_replicas, num_groups, num_nodes, num_gpus ) # Should work normally, just using global load balancing strategy assert phy2log.shape == (1, 8) assert logcnt.shape == (1, 6) assert torch.sum(logcnt) == num_replicas @pytest.mark.parametrize("device", ["cpu", "cuda"]) def test_device_compatibility(device): """Test device compatibility""" if device == "cuda" and not torch.cuda.is_available(): pytest.skip("CUDA not available") weight = torch.tensor([[10, 20, 30, 40]], device=device) num_replicas = 6 num_groups = 2 num_nodes = 1 num_gpus = 2 phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts( weight, num_replicas, num_groups, num_nodes, num_gpus ) # Function will convert to CPU internally, but should handle different # device inputs normally assert phy2log.shape == (1, 6) assert logcnt.shape == (1, 4) def test_additional_cases(): """Test more edge cases and different parameter combinations""" # Test case 1: Large-scale distributed setup weight1 = torch.tensor( [[50, 100, 75, 120, 90, 60, 80, 110, 40, 70, 95, 85, 65, 55, 45, 35]] ) phy2log1, log2phy1, logcnt1 = DefaultEplbPolicy.rebalance_experts( weight1, 24, 8, 4, 8 ) assert phy2log1.shape == (1, 24) assert logcnt1.shape == (1, 16) assert torch.sum(logcnt1) == 24 # Test case 2: Different weight distributions weight2 = torch.tensor( [ [200, 150, 100, 50, 25, 12], # Decreasing weights [12, 25, 50, 100, 150, 200], # Increasing weights ] ) phy2log2, log2phy2, logcnt2 = DefaultEplbPolicy.rebalance_experts( weight2, 10, 3, 1, 2 ) assert phy2log2.shape == (2, 10) assert logcnt2.shape == (2, 6) # Verify high-weight experts have more replicas for layer in range(2): max_weight_idx = torch.argmax(weight2[layer]) assert logcnt2[layer, max_weight_idx] >= 2 if __name__ == "__main__": weight = torch.tensor( [ [90, 132, 40, 61, 104, 165, 39, 4, 73, 56, 183, 86], [20, 107, 104, 64, 19, 197, 187, 157, 172, 86, 16, 27], ] ) num_replicas = 16 num_groups = 4 num_nodes = 2 num_gpus = 8 phy2log, log2phy, logcnt = DefaultEplbPolicy.rebalance_experts( weight, num_replicas, num_groups, num_nodes, num_gpus ) print(phy2log) test_basic_rebalance() def _make_phy_replicas_idx_from_phy2log(phy2log: np.ndarray) -> np.ndarray: """Create replicas indices mapping from phy2log.""" pr = np.zeros_like(phy2log, dtype=np.int64) for layer in range(phy2log.shape[0]): seen: dict[int, int] = {} row = phy2log[layer].tolist() for i, expert in enumerate(row): r = seen.get(expert, 0) pr[layer, i] = r seen[expert] = r + 1 return pr def _validate_intragpu_rearrangement( old_global_expert_indices: np.ndarray, new_phy2log: np.ndarray, new_phy_replicas_idx: np.ndarray, post_phy2log: np.ndarray, post_phy_replicas_idx: np.ndarray, num_ranks: int, slots_per_gpu: int, ): # Per-GPU checks for gpu_idx in range(num_ranks): start = gpu_idx * slots_per_gpu end = start + slots_per_gpu old_seg = old_global_expert_indices[0, start:end] new_seg = new_phy2log[0, start:end] new_rnk = new_phy_replicas_idx[0, start:end] post_seg = post_phy2log[0, start:end] post_rnk = post_phy_replicas_idx[0, start:end] # Pairwise equality for (expert, rank) pairs to ensure nothing is lost def sorted_pairs(seg, rnk): pairs = list(zip(seg.tolist(), rnk.tolist())) pairs.sort() return pairs assert sorted_pairs(post_seg, post_rnk) == sorted_pairs(new_seg, new_rnk), ( f"Per-GPU pairs of (expert,rank) must match new mapping for GPU {gpu_idx}" ) # For experts that remain on the same GPU, the old slot is preserved # for at least one occurrence; rank at that slot must be valid for that expert old_list = old_seg.tolist() new_list = new_seg.tolist() post_list = post_seg.tolist() remained = set(old_list) & set(new_list) new_ranks_for_expert: dict[int, list[int]] = {} for v, r in zip(new_list, new_rnk.tolist()): new_ranks_for_expert.setdefault(v, []).append(r) for expert in remained: old_pos = old_list.index(expert) assert post_list[old_pos] == expert, ( f"Expert {expert} on GPU {gpu_idx} should stay at old slot {old_pos}" ) # Rank at preserved slot must be one of the ranks # the expert has in new mapping assert post_rnk.tolist()[old_pos] in new_ranks_for_expert[expert], ( f"Rank for expert {expert} at preserved slot on GPU {gpu_idx} " "must come from new mapping" ) @pytest.mark.parametrize( "num_ranks, slots_per_gpu, old_phy2log, new_phy2log", [ pytest.param( # Setup: 2 GPUs, 4 slots each, 1 layer # Old mapping: GPU0 -> [0,1,2,3], GPU1 -> [4,5,6,7] # New mapping shuffles within GPU0 and brings 4,5 into GPU0. # GPU0 new -> [1,5,0,4]; GPU1 new -> [6,2,7,3] 2, 4, np.array([[0, 1, 2, 3, 4, 5, 6, 7]]), np.array([[1, 5, 0, 4, 6, 2, 7, 3]]), id="simple", ), pytest.param( # Setup: 2 GPUs, 5 slots each (total 10 physical experts), 1 layer # Old mapping: # GPU0 -> [0, 1, 0, 2, 3] (expert 0 duplicated) # GPU1 -> [4, 5, 6, 1, 2] # New mapping reorders within GPUs and moves some experts across GPUs, # while still including duplicates: # GPU0 new -> [0, 5, 4, 0, 1] (expert 0 duplicated, 4/5 incoming) # GPU1 new -> [6, 2, 3, 2, 1] (expert 2 duplicated) 2, 5, np.array([[0, 1, 0, 2, 3, 4, 5, 6, 1, 2]]), np.array([[0, 5, 4, 0, 1, 6, 2, 3, 2, 1]]), id="duplicates", ), pytest.param( # Setup: 3 GPUs, 4 slots each (total 12 physical experts), 1 layer # Old mapping: # GPU0 -> [0, 1, 2, 3] # GPU1 -> [0, 1, 2, 3] # GPU2 -> [0, 1, 2, 3] # New mapping decides to use one expert on 2 GPUs and shuffles # experts on the third GPU, # GPU0 new -> [0, 0, 0, 0] # GPU1 new -> [0, 0, 0, 0] # GPU2 new -> [1, 2, 3, 0] 3, 4, np.array([[0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]]), np.array([[0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0]]), id="skewed_expert", ), ], ) def test_preserve_intragpu_slots( num_ranks: int, slots_per_gpu: int, old_phy2log: torch.Tensor, new_phy2log: torch.Tensor, ): """Experts that stay on a GPU keep their old slots; incoming not lost.""" phy_replicas_idx = _make_phy_replicas_idx_from_phy2log(new_phy2log) post_phy2log, post_phy_replicas_idx = DefaultEplbPolicy.preserve_intragpu_slots( new_phy2log, phy_replicas_idx, num_ranks, old_phy2log ) # Shapes preserved assert post_phy2log.shape == new_phy2log.shape assert post_phy_replicas_idx.shape == phy_replicas_idx.shape _validate_intragpu_rearrangement( old_phy2log, new_phy2log, phy_replicas_idx, post_phy2log, post_phy_replicas_idx, num_ranks, slots_per_gpu, )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/distributed/test_eplb_algo.py", "license": "Apache License 2.0", "lines": 380, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/distributed/test_eplb_execute.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import random import pytest import torch import torch.distributed from vllm.config import VllmConfig, set_current_vllm_config from vllm.distributed.eplb.rebalance_execute import ( move_from_buffer, rearrange_expert_weights_inplace, transfer_layer, ) from vllm.distributed.parallel_state import ( ensure_model_parallel_initialized, get_tp_group, ) from .eplb_utils import distributed_run, set_env_vars_and_device def create_expert_indices_with_redundancy( num_layers: int, num_logical_experts: int, total_physical_experts: int, redundancy_config: list[int], # redundancy for each logical expert ) -> torch.Tensor: """ Create expert indices with redundancy. Args: num_layers: number of layers num_logical_experts: number of logical experts total_physical_experts: total number of physical experts redundancy_config: redundancy for each logical expert Returns: indices: Shape (num_layers, total_physical_experts) """ assert sum(redundancy_config) == total_physical_experts assert len(redundancy_config) == num_logical_experts indices = torch.zeros(num_layers, total_physical_experts, dtype=torch.long) for layer in range(num_layers): physical_pos = 0 for logical_expert_id, redundancy in enumerate(redundancy_config): for _ in range(redundancy): indices[layer, physical_pos] = logical_expert_id physical_pos += 1 # Shuffle the indices at dim 1 for layer in range(num_layers): indices[layer] = indices[layer][torch.randperm(indices.shape[1])] return indices def create_expert_weights( num_layers: int, num_local_experts: int, hidden_sizes: list[int], rank: int, device: torch.device, physical_to_logical_mapping: torch.Tensor, ) -> list[list[torch.Tensor]]: """ Create fake expert weights tensor for testing. Use `arange` to generate predictable weights values, based on logical expert ID. All replicas of the same logical expert should have the same weights. Args: physical_to_logical_mapping: Shape (num_layers, num_local_experts) mapping[layer, physical_pos] = logical_expert_id """ expert_weights = [] for layer in range(num_layers): layer_weights = [] for weight_idx, hidden_size in enumerate(hidden_sizes): weight_tensor = torch.zeros( num_local_experts, hidden_size, device=device, dtype=torch.float32 ) for local_expert in range(num_local_experts): # Get the logical expert ID for this physical expert global_pos = rank * num_local_experts + local_expert logical_expert_id = physical_to_logical_mapping[ layer, global_pos ].item() # Generate weights based on logical expert ID # (so that all replicas of the same logical expert have the # same weights) base_value = logical_expert_id * 1000 + layer * 100 + weight_idx * 10 weight_tensor[local_expert] = torch.arange( base_value, base_value + hidden_size, device=device, dtype=torch.float32, ) layer_weights.append(weight_tensor) expert_weights.append(layer_weights) return expert_weights def create_redundancy_config( num_logical_experts: int, num_physical_experts: int, ) -> list[int]: """Create a redundancy configuration.""" redundancy_config = [1] * num_logical_experts remaining = num_physical_experts - num_logical_experts # Randomly assign the remaining physical experts to the logical experts for _ in range(remaining): redundancy_config[random.choice(range(num_logical_experts))] += 1 return redundancy_config def verify_expert_weights_after_shuffle( expert_weights: list[list[torch.Tensor]], new_indices: torch.Tensor, hidden_sizes: list[int], ep_rank: int, num_local_experts: int, ): """Verify the weights after shuffling are correct.""" num_layers = len(expert_weights) for layer in range(num_layers): for weight_idx, hidden_size in enumerate(hidden_sizes): weight_tensor = expert_weights[layer][weight_idx] for local_expert in range(num_local_experts): # Calculate the global expert ID for this local expert global_pos = ep_rank * num_local_experts + local_expert expected_logical_expert = new_indices[layer, global_pos].item() # Check if the weights are correct actual_weights = weight_tensor[local_expert] expected_base = ( expected_logical_expert * 1000 + layer * 100 + weight_idx * 10 ) expected_weights = torch.arange( expected_base, expected_base + hidden_size, device=actual_weights.device, dtype=actual_weights.dtype, ) torch.testing.assert_close( actual_weights, expected_weights, msg=f"Layer {layer}, weight {weight_idx}," f"local expert {local_expert}: " f"weights do not match. " f"Expected logical expert {expected_logical_expert}", ) def verify_redundant_experts_have_same_weights( expert_weights: list[list[torch.Tensor]], indices: torch.Tensor, hidden_sizes: list[int], world_size: int, num_local_experts: int, ): """ Verify that all replicas of the same logical expert have the same weights. """ num_layers = len(expert_weights) total_physical_experts = world_size * num_local_experts for layer in range(num_layers): # Collect weights for all physical experts for each weight matrix all_weights: list[torch.Tensor] = [] for weight_idx, hidden_size in enumerate(hidden_sizes): # Create tensor to store all expert weights # Shape: [total_physical_experts, hidden_size] gathered_weights = torch.zeros( total_physical_experts, hidden_size, device=expert_weights[layer][weight_idx].device, dtype=expert_weights[layer][weight_idx].dtype, ) # Use all_gather to collect expert weights from current node # expert_weights[layer][weight_idx] shape: # [num_local_experts, hidden_size] local_weights = expert_weights[layer][ weight_idx ] # [num_local_experts, hidden_size] # Split tensor along dim 0 into a list for all_gather gathered_weights_list = torch.chunk(gathered_weights, world_size, dim=0) torch.distributed.all_gather( # Output list: each element corresponds to one rank's weights list(gathered_weights_list), local_weights, # Input: current rank's local weights ) all_weights.append(gathered_weights) # Verify that all replicas of the same logical expert have the same # weights logical_expert_weights: dict[int, dict[int, torch.Tensor]] = {} for physical_pos in range(total_physical_experts): logical_expert_id = int(indices[layer, physical_pos].item()) if logical_expert_id not in logical_expert_weights: # First time encountering this logical expert, save its weights logical_expert_weights[logical_expert_id] = { weight_idx: all_weights[weight_idx][physical_pos] for weight_idx in range(len(hidden_sizes)) } else: # Verify that current physical expert's weights match the # previously saved logical expert weights for weight_idx in range(len(hidden_sizes)): torch.testing.assert_close( all_weights[weight_idx][physical_pos], logical_expert_weights[logical_expert_id][weight_idx], msg=f"Layer {layer}, weight {weight_idx}," f"logical expert {logical_expert_id}: " f"Physical expert {physical_pos} has different weights" f"than expected", ) def _test_async_transfer_layer_without_mtp_worker( env, world_size: int, num_layers: int, num_local_experts: int, num_logical_experts: int, ) -> None: set_env_vars_and_device(env) vllm_config = VllmConfig() vllm_config.parallel_config.tensor_parallel_size = world_size with set_current_vllm_config(vllm_config): ensure_model_parallel_initialized( tensor_model_parallel_size=world_size, pipeline_model_parallel_size=1 ) tp_group = get_tp_group() ep_group = tp_group.device_group ep_rank = torch.distributed.get_rank() device = torch.device(f"cuda:{ep_rank}") total_physical_experts = world_size * num_local_experts hidden_sizes = [16, 32] redundancy_config = create_redundancy_config( num_logical_experts, total_physical_experts, ) old_indices = create_expert_indices_with_redundancy( num_layers, num_logical_experts, total_physical_experts, redundancy_config, ) new_redundancy_config = create_redundancy_config( num_logical_experts, total_physical_experts, ) new_indices = create_expert_indices_with_redundancy( num_layers, num_logical_experts, total_physical_experts, new_redundancy_config, ) expert_weights = create_expert_weights( num_layers, num_local_experts, hidden_sizes, ep_rank, device, old_indices, ) old_indices_cpu = old_indices.cpu() new_indices_cpu = new_indices.cpu() expert_buffer = [torch.empty_like(w) for w in expert_weights[0]] cuda_stream = torch.cuda.Stream(device=device) for layer_idx in range(num_layers): is_unchanged, is_received_locally, recv_metadata = asyncio.run( transfer_layer( old_layer_indices=old_indices_cpu[layer_idx], new_layer_indices=new_indices_cpu[layer_idx], expert_weights=expert_weights[layer_idx], expert_weights_buffer=expert_buffer, ep_group=ep_group, cuda_stream=cuda_stream, ) ) cuda_stream.synchronize() move_from_buffer( expert_weights=expert_weights[layer_idx], expert_weights_buffers=expert_buffer, is_unchanged=is_unchanged, is_received_locally=is_received_locally, recv_metadata=recv_metadata, new_indices=new_indices_cpu[layer_idx].numpy(), ep_rank=ep_rank, ) verify_expert_weights_after_shuffle( expert_weights, new_indices, hidden_sizes, ep_rank, num_local_experts, ) verify_redundant_experts_have_same_weights( expert_weights, new_indices, hidden_sizes, world_size, num_local_experts, ) def _test_rearrange_expert_weights_with_redundancy( env, world_size, num_layers, num_local_experts, num_logical_experts ) -> None: # Initialize model parallel (using tensor parallel as an entrypoint # to expert parallel) set_env_vars_and_device(env) vllm_config = VllmConfig() vllm_config.parallel_config.tensor_parallel_size = world_size with set_current_vllm_config(vllm_config): ensure_model_parallel_initialized( tensor_model_parallel_size=world_size, pipeline_model_parallel_size=1 ) ep_group = get_tp_group().cpu_group ep_rank = torch.distributed.get_rank() device = torch.device(f"cuda:{ep_rank}") # Test parameters total_physical_experts = world_size * num_local_experts hidden_sizes = [32, 64] # Two different weight matrices # Create old expert indices (with redundancy) redundancy_config = create_redundancy_config( num_logical_experts, total_physical_experts ) old_indices = create_expert_indices_with_redundancy( num_layers, num_logical_experts, total_physical_experts, redundancy_config, ) # Create new expert indices (with redundancy) new_redundancy_config = create_redundancy_config( num_logical_experts, total_physical_experts ) new_indices = create_expert_indices_with_redundancy( num_layers, num_logical_experts, total_physical_experts, new_redundancy_config, ) # Create expert weights expert_weights = create_expert_weights( num_layers, num_local_experts, hidden_sizes, ep_rank, device, old_indices ) # Execute weight rearrangement rearrange_expert_weights_inplace( old_indices, new_indices, expert_weights, ep_group, is_profile=False, ) # Verify the rearrangement result verify_expert_weights_after_shuffle( expert_weights, new_indices, hidden_sizes, ep_rank, num_local_experts, ) verify_redundant_experts_have_same_weights( expert_weights, new_indices, hidden_sizes, world_size, num_local_experts, ) @pytest.mark.parametrize( "world_size,num_layers,num_local_experts,num_logical_experts", [ # 2 GPU, 2 experts per GPU # 3 logical experts, 4 physical experts, 1 redundant experts (2, 1, 2, 3), # 2 GPU, 3 experts per GPU # 4 logical experts, 6 physical experts, 2 redundant experts (2, 2, 3, 4), # 2 GPU, 8 experts per GPU # 16 logical experts, 16 physical experts, 0 redundant experts (2, 4, 8, 16), # 4 GPU, 2 experts per GPU # 6 logical experts, 8 physical experts, 2 redundant experts (4, 1, 2, 6), # 4 GPU, 2 experts per GPU # 5 logical experts, 8 physical experts, 3 redundant experts (4, 2, 2, 5), # 4 GPU, 8 experts per GPU # 16 logical experts, 32 physical experts, 16 redundant experts (4, 8, 8, 16), ], ) def test_rearrange_expert_weights_with_redundancy( world_size, num_layers, num_local_experts, num_logical_experts ): """Test the functionality of rearranging expert weights with redundancy.""" if torch.cuda.device_count() < world_size: pytest.skip(f"Need at least {world_size} GPUs to run the test") distributed_run( _test_rearrange_expert_weights_with_redundancy, world_size, num_layers, num_local_experts, num_logical_experts, ) def _test_rearrange_expert_weights_no_change(env, world_size) -> None: set_env_vars_and_device(env) vllm_config = VllmConfig() vllm_config.parallel_config.tensor_parallel_size = world_size with set_current_vllm_config(vllm_config): ensure_model_parallel_initialized( tensor_model_parallel_size=world_size, pipeline_model_parallel_size=1 ) ep_group = get_tp_group().cpu_group ep_rank = torch.distributed.get_rank() device = torch.device(f"cuda:{ep_rank}") num_layers = 2 num_local_experts = 2 total_physical_experts = world_size * num_local_experts num_logical_experts = total_physical_experts // 2 # Some redundancy hidden_sizes = [32, 64] # Create redundancy configuration redundancy_config = [2] * num_logical_experts # Same indices - no change indices = create_expert_indices_with_redundancy( num_layers, num_logical_experts, total_physical_experts, redundancy_config ) expert_weights = create_expert_weights( num_layers, num_local_experts, hidden_sizes, ep_rank, device, indices ) # Save original weights original_weights = [] for layer_weights in expert_weights: layer_copy = [] for weight in layer_weights: layer_copy.append(weight.clone()) original_weights.append(layer_copy) # Execute rearrangement (should be no change) rearrange_expert_weights_inplace( indices, indices, # Same indices expert_weights, ep_group, is_profile=False, ) # Verify that the weights have not changed for layer in range(num_layers): for weight_idx in range(len(hidden_sizes)): torch.testing.assert_close( expert_weights[layer][weight_idx], original_weights[layer][weight_idx], msg=f"""Layer {layer}, weight {weight_idx} should remain unchanged""", ) @pytest.mark.parametrize( "world_size,num_layers,num_local_experts,num_logical_experts", [ (2, 2, 2, 3), ], ) def test_async_transfer_layer_without_mtp( world_size: int, num_layers: int, num_local_experts: int, num_logical_experts: int, ): """Exercise async EPLB transfer path without MTP/spec decode.""" if torch.cuda.device_count() < world_size: pytest.skip(f"Need at least {world_size} GPUs to run the test") distributed_run( _test_async_transfer_layer_without_mtp_worker, world_size, num_layers, num_local_experts, num_logical_experts, ) @pytest.mark.parametrize("world_size", [2, 4]) def test_rearrange_expert_weights_no_change(world_size): """ Test that when the indices do not change, the weights should remain unchanged. """ if torch.cuda.device_count() < world_size: pytest.skip(f"Need at least {world_size} GPUs to run the test") distributed_run(_test_rearrange_expert_weights_no_change, world_size) def _test_rearrange_expert_weights_profile_mode(env, world_size) -> None: set_env_vars_and_device(env) vllm_config = VllmConfig() vllm_config.parallel_config.tensor_parallel_size = world_size with set_current_vllm_config(vllm_config): ensure_model_parallel_initialized( tensor_model_parallel_size=world_size, pipeline_model_parallel_size=1 ) ep_group = get_tp_group().cpu_group ep_rank = torch.distributed.get_rank() device = torch.device(f"cuda:{ep_rank}") num_layers = 1 num_local_experts = 2 total_physical_experts = world_size * num_local_experts num_logical_experts = total_physical_experts // 2 hidden_sizes = [32] # Create different index distributions old_redundancy = create_redundancy_config( num_logical_experts, total_physical_experts ) new_redundancy = create_redundancy_config( num_logical_experts, total_physical_experts ) old_indices = create_expert_indices_with_redundancy( num_layers, num_logical_experts, total_physical_experts, old_redundancy ) new_indices = create_expert_indices_with_redundancy( num_layers, num_logical_experts, total_physical_experts, new_redundancy ) expert_weights = create_expert_weights( num_layers, num_local_experts, hidden_sizes, ep_rank, device, old_indices ) # Save original weights original_weights = [] for layer_weights in expert_weights: layer_copy = [] for weight in layer_weights: layer_copy.append(weight.clone()) original_weights.append(layer_copy) # Execute profile mode rearrangement rearrange_expert_weights_inplace( old_indices, new_indices, expert_weights, ep_group, is_profile=True, # Profile mode ) # In profile mode, the weights should remain unchanged for layer in range(num_layers): for weight_idx in range(len(hidden_sizes)): torch.testing.assert_close( expert_weights[layer][weight_idx], original_weights[layer][weight_idx], msg="In profile mode, the weights should remain unchanged", ) @pytest.mark.parametrize("world_size", [2, 4]) def test_rearrange_expert_weights_profile_mode(world_size): """Test profile mode (should not copy actual weights)""" if torch.cuda.device_count() < world_size: pytest.skip(f"Need at least {world_size} GPUs to run the test") distributed_run(_test_rearrange_expert_weights_profile_mode, world_size)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/distributed/test_eplb_execute.py", "license": "Apache License 2.0", "lines": 526, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/distributed/eplb/eplb_state.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Expert parallelism load balancer (EPLB) metrics and states. # Glossary - **Logical Expert**: An expert that is part of the model's logical structure. It holds a set of weights and is replicated across multiple physical experts. - **Redundant Expert**: To achieve load balancing, for some popular logical experts, we create additional copies of the expert weights. During inference, each of these copies can be routed to by the same set of tokens. - **Physical Expert**: An expert that is instantiated on a specific device. It is a replica of a logical expert and can be rearranged across devices. I.e., one logical expert may have multiple sets of weights initialized on different devices, and each of these sets is a physical expert. - **Local Physical Expert**: A physical expert that is instantiated on the current device. For example: DeepSeek-R1 has 256 logical experts, so each MoE layer has 256 sets of linear layer weights in the model parameters. If we add 32 redundant experts, DeepSeek-R1 will have 256 + 32 = 288 physical experts in total. And when deploying, we'll have 288 sets of linear layer weights for each MoE layer. If we have 32 EP ranks, then each GPU will hold 288 / 32 = 9 local physical experts. """ import threading from collections.abc import Sequence from dataclasses import dataclass import numpy as np import torch from torch.distributed import ProcessGroup, all_reduce from vllm.config import ModelConfig, ParallelConfig from vllm.distributed.parallel_state import ( get_ep_group, get_node_count, in_the_same_node_as, ) from vllm.distributed.stateless_coordinator import StatelessGroupCoordinator from vllm.distributed.utils import StatelessProcessGroup from vllm.logger import init_logger from vllm.model_executor.models.interfaces import MixtureOfExperts from .async_worker import start_async_worker from .policy import EPLB_POLICIES, AbstractEplbPolicy, DefaultEplbPolicy from .rebalance_execute import ( RecvMetadata, move_from_buffer, rearrange_expert_weights_inplace, ) logger = init_logger(__name__) @dataclass class EplbStats: """ Model stats used in EPLB rebalancing algorithm. """ global_expert_load_window: torch.Tensor """ Experts load window. Shape: (window_size, num_moe_layers, num_physical_experts) """ num_replicas: int """ Number of physical experts. """ num_groups: int """ Number of expert groups. """ num_nodes: int """ Number of nodes. """ num_gpus: int """ Number of GPUs. """ @dataclass class EplbModelState: """EPLB metrics.""" physical_to_logical_map: torch.Tensor """ Mapping from physical experts to logical experts. Shape: (num_moe_layers, num_physical_experts) # Example For a 2-layer MoE model with 6 physical experts and 4 logical experts on 3 EP ranks, the mapping could look like this: ``` [[0, 1, 2, 3, 0, 1], [0, 2, 0, 1, 0, 3]] ``` """ logical_to_physical_map: torch.Tensor """ Mapping from logical experts to physical experts. This is a sparse matrix, where -1 indicates no mapping. Shape: (num_moe_layers, num_logical_experts, num_redundant_experts + 1) # Example For a 2-layer MoE model with 6 physical experts and 4 logical experts on 3 EP ranks, the mapping could look like this: ``` [[[0, 4, -1], [1, 5, -1], [2, -1, -1], [3, -1, -1]], [[0, 2, 4], [3, -1, -1], [1, -1, -1], [5, -1, -1]]] ``` """ logical_replica_count: torch.Tensor """ Number of replicas for each logical expert. This is exactly the non-`-1` count in the `logical_to_physical_map`. Shape: (num_moe_layers, num_logical_experts) # Example For a 2-layer MoE model with 6 physical experts and 4 logical experts on 3 EP ranks, the count could look like this: ``` [[2, 2, 1, 1], [3, 1, 1, 1]] """ expert_load_pass: torch.Tensor """ Expert load during this forward pass. We use the token count each expert processes as the load. Shape: (num_moe_layers, num_physical_experts) """ expert_load_window: torch.Tensor """ A sliding window of expert load. Shape: (window_size, num_moe_layers, num_physical_experts) NOTE: The expert_load_view now records load for all physical experts rather than just local experts. This ensures consistent load statistics across different dispatch methods (naive all-to-all, DeepEP). The recorded load will be multiplied by dp_size when using naive all-to-all due to each DP rank contributing the same token set to the calculation. See: https://github.com/vllm-project/vllm/pull/22167#pullrequestreview-3086143856 """ model_name: str model: MixtureOfExperts expert_buffer: list[torch.Tensor] """ The buffer to store the expert weights during transfer. """ buffer_lock: threading.Lock """ The lock to protect the expert buffer. """ buffer_ready_event: torch.cuda.Event | None """ CUDA event recorded when the async worker finishes filling the buffer. The main thread waits on this before consuming the buffer. """ buffer_consumed_event: torch.cuda.Event | None """ CUDA event recorded after the main thread finishes consuming the buffer. The async worker waits on this before writing to the buffer again. """ window_ready_event: torch.cuda.Event | None """ CUDA event recorded after all-reduce and clone on the main thread. The async worker waits on this before accessing global_expert_load_window. """ ep_buffer_ready: int """ The flag indicates whether the expert buffer is ready for transfer. 0 or 1. """ layer_to_transfer: int """ The layer index to transfer in async mode. """ rebalanced: bool """ The flag indicates whether the experts rebalance have been computed. """ pending_global_ready_check: bool """ Whether the async EPLB needs to poll peers for buffer readiness. """ eplb_stats: EplbStats | None """ EPLB stats for the model. """ is_unchanged: np.ndarray """ intermediate variable between `move_to_buffer` and `move_to_workspace`. The size is same as the num of physical experts in the current layer. """ is_received_locally: np.ndarray """ intermediate variable between `move_to_buffer` and `move_to_workspace`. The size is same as the num of physical experts in the current layer. """ recv_metadata: RecvMetadata """ intermediate variable between `move_to_buffer` and `move_to_workspace`. """ cuda_device_index: int | None """ CUDA device index for the async EPLB worker thread. """ new_physical_to_logical_map: torch.Tensor | None = None """ intermediate variable between `move_to_buffer` and `move_to_workspace`. the size is same as physical_to_logical_map """ new_logical_to_physical_map: torch.Tensor | None = None """ intermediate variable between `move_to_buffer` and `move_to_workspace`. the size is same as logical_to_physical_map """ new_logical_replica_count: torch.Tensor | None = None """ intermediate variable between `move_to_buffer` and `move_to_workspace`. the size is same as logical_replica_count """ class EplbState: """ EplbState of each expert parallel model. Key is the model config hash. """ def __init__(self, parallel_config: ParallelConfig, device: torch.device): self.parallel_config = parallel_config self.device = device self.model_states: dict[str, EplbModelState] = {} self.policy: type[AbstractEplbPolicy] = DefaultEplbPolicy """ Selected EPLB algorithm class """ self.expert_load_window_step: int = 0 """ Current step in the sliding window. Different from `expert_rearrangement_step`, each EP rank may have its own `expert_load_window_step`. """ self.expert_load_window_size: int = 0 """ Size of the expert load sliding window. This is a constant and is taken from the config. """ self.expert_rearrangement_step: int = 0 """ Steps after last rearrangement. Will trigger a rearrangement if it exceeds the threshold. NOTE: Keep in mind that all EP ranks need to have the same `expert_rearrangement_step` value to ensure synchronization. Otherwise, the rearrangement will hang at collective communication calls. """ self.expert_rearrangement_step_interval: int = 0 """ Interval for expert rearrangement steps. This is a constant and is taken from the config. """ self.is_async: bool = False """ The flag indicates whether the EPLB is running in async mode. """ self.rearrange_event = threading.Event() """ Event to signal when a new rearrangement is needed for the async thread. """ self.async_worker: threading.Thread | None = None """ Background thread handling async transfers. """ self.cuda_device_index: int | None = None """ CUDA device index for the async EPLB worker thread. """ self.num_valid_physical_experts: int = 0 """ Number of valid physical experts. This is the number of physical experts that are actually mapped to logical experts. In elastic EP, newly started EP ranks may not have physical experts mapped yet. """ if self.device.type == "cuda": self.cuda_device_index = self.device.index if self.cuda_device_index is None and torch.cuda.is_available(): self.cuda_device_index = torch.cuda.current_device() @staticmethod def build_initial_global_physical_to_logical_map( num_routed_experts: int, num_redundant_experts: int, ) -> Sequence[int]: """ Build an initial expert arrangement using the following structure: [original routed experts, redundant experts] Returns: physical_to_logical_map (Sequence[int]): A list of integers, where each integer is the index of the logical expert that the corresponding physical expert maps to. """ global_physical_to_logical_map = list(range(num_routed_experts)) global_physical_to_logical_map += [ i % num_routed_experts for i in range(num_redundant_experts) ] return global_physical_to_logical_map def validate_ep_configuration(self, new_model: MixtureOfExperts): """ Validate that the expert parallel configuration of the new model is the same as the existing models. """ if len(self.model_states) > 0: model = next(iter(self.model_states.values())).model if ( model.num_routed_experts != new_model.num_routed_experts or model.num_redundant_experts != new_model.num_redundant_experts or model.num_physical_experts != new_model.num_physical_experts or model.num_logical_experts != new_model.num_logical_experts or model.num_expert_groups != new_model.num_expert_groups ): raise RuntimeError( "Model: {} " "with config {} " "{} {} {} {} " "mismatch with new model {} " "with config {} " "{} {} {} {}".format( type(model), model.num_routed_experts, model.num_redundant_experts, model.num_physical_experts, model.num_logical_experts, model.num_expert_groups, type(new_model), new_model.num_routed_experts, new_model.num_redundant_experts, new_model.num_physical_experts, new_model.num_logical_experts, new_model.num_expert_groups, ) ) def add_model( self, model: MixtureOfExperts, model_config: ModelConfig, ): """ Build the initial EPLB state. """ self.validate_ep_configuration(model) self.is_async = self.parallel_config.eplb_config.use_async physical_to_logical_map_list = ( EplbState.build_initial_global_physical_to_logical_map( model.num_routed_experts, model.num_redundant_experts, ) ) physical_to_logical_map = torch.tensor( physical_to_logical_map_list, device=self.device, ) # Assuming 8 GPUs per node, this supports up to # (1023 + 1) / 8 = 128 nodes for now. # TODO(rui): make this configurable MAX_EXPERT_REDUNDANCY = 1023 assert model.num_redundant_experts <= MAX_EXPERT_REDUNDANCY, ( f"num_redundant_experts {model.num_redundant_experts} " f"must be less than or equal to {MAX_EXPERT_REDUNDANCY}" ) max_slots_per_logical_expert = MAX_EXPERT_REDUNDANCY + 1 logical_to_physical_map = torch.full( (model.num_logical_experts, max_slots_per_logical_expert), -1, device=self.device, ) logical_replica_count = torch.zeros( (model.num_logical_experts,), device=self.device, dtype=torch.long, ) for i in range(model.num_physical_experts): logical_idx = physical_to_logical_map[i] logical_to_physical_map[logical_idx, logical_replica_count[logical_idx]] = i logical_replica_count[logical_idx] += 1 # Duplicate initial mapping for all layers physical_to_logical_map = ( physical_to_logical_map.unsqueeze(0) .expand( model.num_moe_layers, -1, ) .contiguous() ) logical_to_physical_map = ( logical_to_physical_map.unsqueeze(0) .expand( model.num_moe_layers, -1, -1, ) .contiguous() ) logical_replica_count = ( logical_replica_count.unsqueeze(0) .expand( model.num_moe_layers, -1, ) .contiguous() ) expert_load_pass = torch.zeros( (model.num_moe_layers, model.num_physical_experts), dtype=torch.int32, device=self.device, ) self.expert_load_window_size = self.parallel_config.eplb_config.window_size expert_load_window = torch.zeros( ( self.expert_load_window_size, model.num_moe_layers, model.num_physical_experts, ), dtype=torch.int32, device=self.device, ) # Set the initial progress of rearrangement to 3/4 eplb_step_interval = self.parallel_config.eplb_config.step_interval self.expert_rearrangement_step = max( 0, eplb_step_interval - eplb_step_interval // 4 ) self.expert_rearrangement_step_interval = eplb_step_interval policy_type = self.parallel_config.eplb_config.policy self.policy = EPLB_POLICIES[policy_type] logger.debug("Selected EPLB policy: %s", policy_type) model.set_eplb_state( expert_load_pass, logical_to_physical_map, logical_replica_count, ) expert_buffer = [torch.empty_like(w) for w in model.expert_weights[0]] model_state = EplbModelState( physical_to_logical_map=physical_to_logical_map, logical_to_physical_map=logical_to_physical_map, logical_replica_count=logical_replica_count, expert_load_pass=expert_load_pass, expert_load_window=expert_load_window, model_name=model_config.model, model=model, expert_buffer=expert_buffer, buffer_lock=threading.Lock(), buffer_ready_event=None, buffer_consumed_event=None, window_ready_event=None, ep_buffer_ready=0, layer_to_transfer=0, rebalanced=False, pending_global_ready_check=False, eplb_stats=None, is_unchanged=np.array([]), is_received_locally=np.array([]), recv_metadata=RecvMetadata( recv_primary_mask=np.array([]), recv_count=0, recv_expert_ids=np.array([]), recv_dst_rows=np.array([]), ), cuda_device_index=self.cuda_device_index, new_physical_to_logical_map=None, new_logical_to_physical_map=None, new_logical_replica_count=None, ) self.model_states[model_config.compute_hash()] = model_state self.num_valid_physical_experts = model.num_physical_experts def step( self, is_dummy: bool = False, is_profile: bool = False, log_stats: bool = False, ) -> None: """ Step the EPLB state. Args: is_dummy (bool): If `True`, this is a dummy step and the load metrics recorded in this forward pass will not count. Defaults to `False`. is_profile (bool): If `True`, perform a dummy rearrangement with maximum communication cost. This is used in `profile_run` to reserve enough memory for the communication buffer. log_stats (bool): If `True`, log the expert load metrics. # Stats The metrics are all summed up across layers. - `avg_tokens`: The average load across ranks. - `max_tokens`: The maximum load across ranks. - `balancedness`: The ratio of average load to maximum load. """ ep_group = get_ep_group().device_group if is_profile: self.rearrange(is_profile=True) return if is_dummy: # Do not record load metrics for dummy steps for eplb_model_state in self.model_states.values(): eplb_model_state.expert_load_pass.zero_() if ( log_stats and self.expert_rearrangement_step % self.parallel_config.eplb_config.log_balancedness_interval == 0 ): # Sync the expert load pass for each model (main and drafter). # expert_load_pass: (num_moe_layers, num_physical_experts) expert_load_pass_list = self._sync_load_pass() ep_group = get_ep_group().device_group for expert_load_pass, eplb_model_state in zip( expert_load_pass_list, self.model_states.values() ): # num_tokens_per_rank: (num_moe_layers, num_ranks) num_tokens_per_rank = ( expert_load_pass.reshape( expert_load_pass.shape[0], ep_group.size(), -1 ) .sum(dim=-1) .float() ) # Compute balancedness ratio: # for each layer: # (mean load across ranks) / (max load across ranks) avg_tokens_tensor = num_tokens_per_rank.mean(dim=0).sum(dim=0) max_tokens_tensor = num_tokens_per_rank.max(dim=0).values.sum(dim=0) # Just to make type checker happy tokens_tensors: list[float] = torch.stack( [avg_tokens_tensor, max_tokens_tensor] ).tolist() avg_tokens, max_tokens = tokens_tensors balancedness = avg_tokens / max_tokens if max_tokens > 0 else 0.0 if ep_group.rank() == 0: logger.info( "EPLB step: %d for model %s: avg_tokens=%.2f, " "max_tokens=%d, balancedness=%.4f, " "steps until the next rearrangement: %d", self.expert_rearrangement_step, eplb_model_state.model_name, avg_tokens, max_tokens, balancedness, self.expert_rearrangement_step_interval - self.expert_rearrangement_step, ) # Update the expert load sliding window if not is_dummy: for eplb_model_state in self.model_states.values(): eplb_model_state.expert_load_window[self.expert_load_window_step] = ( eplb_model_state.expert_load_pass.clone() ) eplb_model_state.expert_load_pass.zero_() self.expert_load_window_step += 1 if self.expert_load_window_step >= self.expert_load_window_size: self.expert_load_window_step = 0 # Step the expert rearrangement step # Note that even if this is a dummy step, we still increment the # rearrangement step and perform rearrangement to ensure all ranks are # performing collective communication. self.expert_rearrangement_step += 1 if self.is_async: for eplb_model_state in self.model_states.values(): all_ranks_buffer_ready = False if eplb_model_state.pending_global_ready_check: all_ranks_buffer_ready = self._all_ranks_buffer_ready( eplb_model_state ) if eplb_model_state.ep_buffer_ready and all_ranks_buffer_ready: self.move_to_workspace( model_state=eplb_model_state, ep_group=ep_group, is_profile=is_profile, ) if self.expert_rearrangement_step >= self.expert_rearrangement_step_interval: if self.is_async and any( eplb_model_state.rebalanced for eplb_model_state in self.model_states.values() ): # Still performing asynchronous rearrangement return self.expert_rearrangement_step = 0 self.rearrange() def rearrange( self, is_profile: bool = False, rank_mapping: dict[int, int] | None = None, ) -> torch.Tensor | None: """ Rearrange the experts according to the current load. Args: is_profile (bool): If `True`, perform a dummy rearrangement. This is used in `profile_run` to reserve enough memory, no memory movement will be performed. Default is False. rank_mapping (dict[int, int] | None): The rank mapping when scaling is done in EEP. """ ep_group = get_ep_group().device_group ep_rank = ep_group.rank() start_event = None end_event = None is_main_rank = ep_rank == 0 if is_main_rank: if not self.is_async or is_profile: start_event = torch.cuda.Event(enable_timing=True) end_event = torch.cuda.Event(enable_timing=True) start_event.record() logger.info( "Rearranging experts %s %s...", "(async mode)" if self.is_async else "sync mode", "(profile)" if is_profile else "", ) # Map the physical expert load to global logical experts global_expert_load_windows = [] for eplb_model_state in self.model_states.values(): expert_load_window = eplb_model_state.expert_load_window[ :, :, : self.num_valid_physical_experts ] logical_expert_load_window = torch.zeros( self.expert_load_window_size, eplb_model_state.model.num_moe_layers, eplb_model_state.model.num_logical_experts, dtype=eplb_model_state.expert_load_window.dtype, device=eplb_model_state.expert_load_window.device, ) logical_expert_load_window.scatter_add_( dim=-1, index=eplb_model_state.physical_to_logical_map[ :, : self.num_valid_physical_experts ] .unsqueeze(0) .expand_as(expert_load_window) .long(), src=expert_load_window, ) global_expert_load_window = logical_expert_load_window.sum(dim=0) global_expert_load_windows.append(global_expert_load_window) # Perform all-reduce to get the expert load across all ranks for each model global_expert_load_windows = self._allreduce_list(global_expert_load_windows) # TODO(bowen): Treat differently for prefill and decode nodes eplb_model_state = next(iter(self.model_states.values())) model = eplb_model_state.model num_replicas = model.num_physical_experts num_groups = model.num_expert_groups if rank_mapping is not None and len(rank_mapping) == ep_group.size(): # NOTE(yongji): scale down, we need to rebalance the experts on # remaining GPUs, transfer the experts while we haven't shutdown # the GPUs to be released. coordinator = get_ep_group() assert isinstance(coordinator, StatelessGroupCoordinator) tcp_store_group = coordinator.tcp_store_group num_nodes = _node_count_with_rank_mapping(tcp_store_group, rank_mapping) num_gpus = sum(new_rank != -1 for new_rank in rank_mapping.values()) num_replicas = ( num_replicas // ep_group.size() * num_gpus ) # handle num replicas change else: num_nodes = get_node_count() num_gpus = ep_group.size() if num_gpus % num_nodes != 0: num_nodes = 1 logger.warning_once( f"num_gpus % num_nodes != 0, " "not using hierarchical rearrangement algorithm.\n" f"{num_gpus=}, {num_nodes=}" ) # Get new expert mappings for eplb_model_state, global_expert_load_window in zip( self.model_states.values(), global_expert_load_windows ): if not self.is_async or is_profile: # Get new expert mappings for the model ( new_physical_to_logical_map, new_logical_to_physical_map, new_logical_replica_count, ) = self.policy.rebalance_experts( global_expert_load_window, num_replicas, num_groups, num_nodes, num_gpus, eplb_model_state.physical_to_logical_map, ) # Update expert weights rearrange_expert_weights_inplace( eplb_model_state.physical_to_logical_map, new_physical_to_logical_map, eplb_model_state.model.expert_weights, ep_group, is_profile, rank_mapping, ) if not is_profile: if ( eplb_model_state.physical_to_logical_map.shape[1] != new_physical_to_logical_map.shape[1] ): eplb_model_state.physical_to_logical_map = ( new_physical_to_logical_map.to( eplb_model_state.physical_to_logical_map.device ) ) else: eplb_model_state.physical_to_logical_map.copy_( new_physical_to_logical_map ) max_physical_slots = new_logical_to_physical_map.shape[-1] assert ( max_physical_slots <= eplb_model_state.logical_to_physical_map.shape[-1] ) new_logical_to_physical_map = torch.nn.functional.pad( new_logical_to_physical_map, ( 0, eplb_model_state.logical_to_physical_map.shape[-1] - max_physical_slots, ), value=-1, ) eplb_model_state.logical_to_physical_map.copy_( new_logical_to_physical_map ) eplb_model_state.logical_replica_count.copy_( new_logical_replica_count ) if is_main_rank: assert start_event is not None assert end_event is not None end_event.record() end_event.synchronize() gpu_elapsed = start_event.elapsed_time(end_event) / 1000.0 logger.info( "Rearranged experts %s in %.2f s.", " (profile) " if is_profile else " ", gpu_elapsed, ) else: eplb_model_state.eplb_stats = EplbStats( # We copy the tensor to snapshot the global_expert_load_window # on the main thread so that async worker can access it safely # while the main thread is running. global_expert_load_window=global_expert_load_window.clone(), num_replicas=num_replicas, num_groups=num_groups, num_nodes=num_nodes, num_gpus=num_gpus, ) # Record event after clone to signal async worker # that load stats data is ready sync_event = torch.cuda.Event() sync_event.record() eplb_model_state.window_ready_event = sync_event eplb_model_state.rebalanced = True eplb_model_state.layer_to_transfer = 0 eplb_model_state.pending_global_ready_check = True # Signal async thread to start transferring layers if self.is_async and (not is_profile): self.rearrange_event.set() return None def start_async_loop( self, rank_mapping: dict[int, int] | None = None, is_profile: bool = False, ): if not self.is_async: return if self.async_worker is None: self.async_worker = start_async_worker( self, is_profile=is_profile, ) def _update_layer_mapping_from_new( self, model_state: EplbModelState, layer: int ) -> None: if ( model_state.new_physical_to_logical_map is None or model_state.new_logical_to_physical_map is None or model_state.new_logical_replica_count is None ): return target_device = model_state.physical_to_logical_map.device new_physical = model_state.new_physical_to_logical_map # If the number of physical experts has changed, then the new map needs to # be copied synchronously to avoid a race condition with the async worker if model_state.physical_to_logical_map.shape[1] != new_physical.shape[1]: model_state.physical_to_logical_map = new_physical.to(target_device) else: model_state.physical_to_logical_map[layer].copy_( new_physical[layer].to(target_device, non_blocking=True) ) logical_device = model_state.logical_to_physical_map.device new_logical = model_state.new_logical_to_physical_map[layer].to(logical_device) max_slots = model_state.logical_to_physical_map.shape[-1] slot_delta = max_slots - new_logical.shape[-1] if slot_delta > 0: new_logical = torch.nn.functional.pad( new_logical, (0, slot_delta), value=-1 ) model_state.logical_to_physical_map[layer].copy_(new_logical) replica_device = model_state.logical_replica_count.device model_state.logical_replica_count[layer].copy_( model_state.new_logical_replica_count[layer].to(replica_device) ) def _all_ranks_buffer_ready(self, model_state: EplbModelState) -> bool: parallel_state = get_ep_group() cpu_group = getattr(parallel_state, "cpu_group", None) if cpu_group is not None and cpu_group.size() > 1: flag = torch.tensor( (int(model_state.ep_buffer_ready),), dtype=torch.int32, device="cpu" ) all_reduce(flag, group=cpu_group) return int(flag.item()) == cpu_group.size() device_group = parallel_state.device_group if device_group.size() <= 1: return bool(model_state.ep_buffer_ready) device = getattr( parallel_state, "device", model_state.physical_to_logical_map.device ) flag = torch.tensor( (int(model_state.ep_buffer_ready),), dtype=torch.int32, device=device ) all_reduce(flag, group=device_group) return int(flag.item()) == device_group.size() def move_to_workspace( self, model_state: EplbModelState, ep_group: ProcessGroup, is_profile: bool = False, ): # We call move_to_workspace only when ep_buffer_ready is 1. # It means we only need to wait for the lock for a short time. max_retries = 6 # 1 minute max retries = 0 while not model_state.buffer_lock.acquire(blocking=True, timeout=10.0): retries += 1 if retries >= max_retries: raise RuntimeError( f"Rank {ep_group.rank()}: buffer_lock timeout after " "{max_retries * 10}s" ) logger.warning( "Rank %d: EPLB buffer_lock acquire failed, retrying (%d/%d)", ep_group.rank(), retries, max_retries, ) try: assert model_state.new_physical_to_logical_map is not None device_index = model_state.cuda_device_index or self.cuda_device_index if model_state.buffer_ready_event is not None and device_index is not None: stream = torch.cuda.current_stream(device=device_index) stream.wait_event(model_state.buffer_ready_event) model_state.buffer_ready_event = None expert_weights = model_state.model.expert_weights[ model_state.layer_to_transfer ] expert_weights_buffer = model_state.expert_buffer new_indices = model_state.new_physical_to_logical_map[ model_state.layer_to_transfer ].numpy() move_from_buffer( expert_weights=expert_weights, expert_weights_buffers=expert_weights_buffer, is_unchanged=model_state.is_unchanged, is_received_locally=model_state.is_received_locally, recv_metadata=model_state.recv_metadata, new_indices=new_indices, ep_rank=ep_group.rank(), ) # Record event after consuming buffer to signal async thread # that it's safe to overwrite the intermediate buffer consumed_event = torch.cuda.Event() consumed_event.record() model_state.buffer_consumed_event = consumed_event transferred_layer = model_state.layer_to_transfer self._update_layer_mapping_from_new(model_state, transferred_layer) # After the main thread consumes, advance layer_to_transfer model_state.layer_to_transfer += 1 model_state.ep_buffer_ready = 0 logger.debug( "model %s successfully move_to_workspace layer %d", model_state.model_name, transferred_layer, ) if model_state.layer_to_transfer >= model_state.model.num_moe_layers: self.post_eplb(model_state, is_profile) model_state.rebalanced = False model_state.layer_to_transfer = 0 model_state.pending_global_ready_check = False logger.info( "finish async transfer for model %s rank %d layer %d", model_state.model_name, ep_group.rank(), model_state.model.num_moe_layers, ) finally: try: model_state.buffer_lock.release() except Exception as e: logger.error( "Rank %d: buffer_lock release failed in move_to_workspace: %s", ep_group.rank(), str(e), ) def post_eplb(self, model_state: EplbModelState, is_profile: bool = False) -> None: assert model_state.new_physical_to_logical_map is not None assert model_state.new_logical_to_physical_map is not None assert model_state.new_logical_replica_count is not None model_state.new_physical_to_logical_map = None model_state.new_logical_to_physical_map = None model_state.new_logical_replica_count = None def _allreduce_list(self, tensor_list: list[torch.Tensor]) -> list[torch.Tensor]: """ All-reduce a list of tensors. """ if len(tensor_list) == 1: all_reduce(tensor_list[0], group=get_ep_group().device_group) return tensor_list assert all(t.dim() == 2 for t in tensor_list), "All tensors must be 2D." assert all(t.shape[1] == tensor_list[0].shape[1] for t in tensor_list), ( "All tensors must have the same shape[1]." ) # Concatenate, all_reduce, then unpack to original shapes. # We assume all tensors are 2D and shape[1] (num_physical_experts) # is the same across all models. shapes = [t.shape for t in tensor_list] concat_tensor = torch.cat(tensor_list, dim=0) ep_group = get_ep_group().device_group all_reduce(concat_tensor, group=ep_group) all_reduce_list = [] offset = 0 for shape in shapes: all_reduce_list.append(concat_tensor[offset : offset + shape[0], :]) offset += shape[0] return all_reduce_list def _sync_load_pass(self) -> list[torch.Tensor]: """ Sync the expert load pass across all ranks for log stats. Doesn't update the expert load pass in eplb_model_state. """ load_pass_list = [] for eplb_model_state in self.model_states.values(): load_pass_list.append(eplb_model_state.expert_load_pass.clone()) return self._allreduce_list(load_pass_list) @classmethod def from_mapping( cls, model: MixtureOfExperts, model_config: ModelConfig, device: torch.device, parallel_config: ParallelConfig, expanded_physical_to_logical: torch.Tensor, num_valid_physical_experts: int, ) -> "EplbState": eplb_state = cls( parallel_config=parallel_config, device=device, ) eplb_state.add_model( model=model, model_config=model_config, ) eplb_state.num_valid_physical_experts = num_valid_physical_experts num_moe_layers = expanded_physical_to_logical.shape[0] num_physical_experts = expanded_physical_to_logical.shape[1] eplb_model_state = eplb_state.model_states[model_config.compute_hash()] eplb_model_state.physical_to_logical_map.copy_(expanded_physical_to_logical) logical_to_physical_map = torch.full( ( num_moe_layers, model.num_logical_experts, eplb_model_state.logical_to_physical_map.shape[2], ), -1, dtype=torch.int64, ) logical_replica_count = torch.zeros( (num_moe_layers, model.num_logical_experts), dtype=torch.int64, ) expanded_physical_to_logical_numpy = expanded_physical_to_logical.cpu().numpy() for layer_idx in range(num_moe_layers): for phys_idx in range(num_physical_experts): logical_idx = expanded_physical_to_logical_numpy[layer_idx, phys_idx] if logical_idx >= 0: replica_idx = logical_replica_count[layer_idx, logical_idx] logical_to_physical_map[layer_idx, logical_idx, replica_idx] = ( phys_idx ) logical_replica_count[layer_idx, logical_idx] += 1 logical_to_physical_map = logical_to_physical_map.to(device) logical_replica_count = logical_replica_count.to(device) eplb_model_state.logical_to_physical_map.copy_(logical_to_physical_map) eplb_model_state.logical_replica_count.copy_(logical_replica_count) return eplb_state @dataclass class EplbLayerState: """Runtime EPLB data stored in the MoE layer.""" expert_load_view: torch.Tensor | None = None logical_to_physical_map: torch.Tensor | None = None logical_replica_count: torch.Tensor | None = None def _node_count_with_rank_mapping( pg: ProcessGroup | StatelessProcessGroup, rank_mapping: dict[int, int], ) -> int: if isinstance(pg, ProcessGroup): world_size = torch.distributed.get_world_size(group=pg) else: world_size = pg.world_size if world_size == 1: return 1 # Build node assignment map node_assignment = [0] * world_size # rank -> node_id next_node_id = 0 for current_rank in range(world_size): if node_assignment[current_rank] != 0: continue # Already assigned to a node assert current_rank in rank_mapping if rank_mapping[current_rank] == -1: continue # Pending shutdown # Assign current rank to a new node next_node_id += 1 node_assignment[current_rank] = next_node_id # Find all ranks on the same node as current_rank same_node_flags = in_the_same_node_as(pg, current_rank) for other_rank, is_same_node in enumerate(same_node_flags): if is_same_node and node_assignment[other_rank] == 0: node_assignment[other_rank] = next_node_id return next_node_id
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/distributed/eplb/eplb_state.py", "license": "Apache License 2.0", "lines": 1028, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/distributed/eplb/rebalance_execute.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ The actual execution of the rearrangement. This involves the exchange of expert weights between GPUs. """ from collections.abc import Sequence from dataclasses import dataclass import numpy as np import torch from torch.distributed import ( P2POp, ProcessGroup, all_gather, batch_isend_irecv, get_global_rank, ) from vllm.distributed.parallel_state import get_ep_group from vllm.distributed.stateless_coordinator import StatelessGroupCoordinator from vllm.logger import init_logger logger = init_logger(__name__) @dataclass class RecvMetadata: """Metadata describing remote receives during EPLB rebalancing.""" recv_primary_mask: np.ndarray """Mask of (num_local_experts,) indicating primary experts received.""" recv_count: int """Number of received experts for the layer.""" recv_expert_ids: np.ndarray """Expert ids (num_local_experts,) of remote primary experts.""" recv_dst_rows: np.ndarray """Target expert indices (num_local_experts,) in local tensors to send.""" # Type alias for the result of move_to_buffer or transfer_layer MoveToBufferResult = tuple[np.ndarray, np.ndarray, RecvMetadata] def get_ep_ranks_with_experts_batch( expert_ids: np.ndarray, num_local_experts: int, old_indices: np.ndarray, new_indices: np.ndarray, ) -> tuple[dict[int, list[int]], dict[int, list[int]]]: """ Get the ranks of the experts that need to be exchanged. Args: expert_ids: 1D array of expert indices to query. num_local_experts: The number of local experts. old_indices: The old indices of the experts. new_indices: The new indices of the experts. Returns: A tuple of two dictionaries mapping expert_id to: - ranks_to_send: The ranks that have this expert and need to send. - ranks_to_recv: The ranks that need to receive this expert. """ ranks_to_send_map: dict[int, list[int]] = {} ranks_to_recv_map: dict[int, list[int]] = {} # Fast path: if no experts, return empty dicts if expert_ids.size == 0: return ranks_to_send_map, ranks_to_recv_map unique_experts = np.unique(expert_ids) num_positions = len(old_indices) position_indices = np.arange(num_positions, dtype=np.int32) # Vectorized approach: find all positions matching any query expert in one pass # Use np.isin to get boolean masks for all relevant positions at once old_relevant_mask = np.isin(old_indices, unique_experts) new_relevant_mask = np.isin(new_indices, unique_experts) # Process old_indices (send ranks) if np.any(old_relevant_mask): old_relevant_positions = position_indices[old_relevant_mask] old_relevant_experts = old_indices[old_relevant_mask] old_relevant_ranks = old_relevant_positions // num_local_experts # Sort by expert first, then by position (to maintain first-appearance order) sort_order = np.lexsort((old_relevant_positions, old_relevant_experts)) sorted_experts = old_relevant_experts[sort_order] sorted_ranks = old_relevant_ranks[sort_order] # Find boundaries where expert changes expert_boundaries = np.concatenate( [[0], np.where(np.diff(sorted_experts) != 0)[0] + 1, [len(sorted_experts)]] ) # For each expert, extract unique ranks in order of first appearance for i in range(len(expert_boundaries) - 1): start, end = expert_boundaries[i], expert_boundaries[i + 1] expert = int(sorted_experts[start]) expert_ranks = sorted_ranks[start:end] # Get unique ranks preserving order _, unique_idx = np.unique(expert_ranks, return_index=True) unique_ranks = expert_ranks[np.sort(unique_idx)] ranks_to_send_map[expert] = unique_ranks.tolist() # Process new_indices (recv ranks) if np.any(new_relevant_mask): new_relevant_positions = position_indices[new_relevant_mask] new_relevant_experts = new_indices[new_relevant_mask] new_relevant_ranks = new_relevant_positions // num_local_experts # Sort by expert first, then by position sort_order = np.lexsort((new_relevant_positions, new_relevant_experts)) sorted_experts = new_relevant_experts[sort_order] sorted_ranks = new_relevant_ranks[sort_order] # Find boundaries where expert changes expert_boundaries = np.concatenate( [[0], np.where(np.diff(sorted_experts) != 0)[0] + 1, [len(sorted_experts)]] ) # For each expert, extract unique ranks and exclude local copies for i in range(len(expert_boundaries) - 1): start, end = expert_boundaries[i], expert_boundaries[i + 1] expert = int(sorted_experts[start]) expert_ranks = sorted_ranks[start:end] # Get unique ranks preserving order _, unique_idx = np.unique(expert_ranks, return_index=True) unique_ranks = expert_ranks[np.sort(unique_idx)] # Remove ranks that have local copies (in send map) send_ranks_set = set(ranks_to_send_map.get(expert, [])) recv_ranks_actual = [ int(r) for r in unique_ranks if r not in send_ranks_set ] ranks_to_recv_map[expert] = recv_ranks_actual # Handle experts that only appear in old (send only) or new (recv only) for expert in unique_experts: expert = int(expert) if expert not in ranks_to_send_map: ranks_to_send_map[expert] = [] if expert not in ranks_to_recv_map: ranks_to_recv_map[expert] = [] return ranks_to_send_map, ranks_to_recv_map def move_to_buffer( num_local_experts: int, old_indices: np.ndarray, new_indices: np.ndarray, expert_weights: Sequence[torch.Tensor], expert_weights_buffers: Sequence[torch.Tensor], cuda_stream: torch.cuda.Stream | None, ep_group: ProcessGroup, ) -> MoveToBufferResult: """ Rearranges expert weights during EPLB rebalancing. Args: num_local_experts: Number of local experts. old_indices: (num_experts_total,) ndarray of current (old) global-to-local expert assignments. new_indices: (num_experts_total,) ndarray of desired (new) global-to-local assignments after rebalance. expert_weights: Original expert weights for the layer. expert_weights_buffers: Intermediate buffers (one per tensor). cuda_stream: CUDA stream for async copies (can be None for sync mode). ep_group: Distributed process group for expert parallel comms. Returns: is_unchanged (np.ndarray): (num_local_experts,), True where an expert row is unchanged after rebalance. is_received_locally (np.ndarray): (num_local_experts,), True where a row can be updated from local data. RecvMetadata: Metadata needed for completing remote weight transfers. """ assert old_indices.shape == new_indices.shape ep_rank = ep_group.rank() recv_primary_mask = np.zeros((num_local_experts,), dtype=np.bool_) send_expert_ids = np.full((num_local_experts,), -1, dtype=np.int64) send_src_rows = np.full((num_local_experts,), -1, dtype=np.int32) recv_expert_ids = np.full((num_local_experts,), -1, dtype=np.int64) recv_dst_rows = np.full((num_local_experts,), -1, dtype=np.int32) base = ep_rank * num_local_experts local_rows = np.arange(num_local_experts, dtype=np.int32) local_global = base + local_rows old_local_expert_ids = old_indices[local_global] new_local_expert_ids = new_indices[local_global] # Unchanged mask is_unchanged = old_local_expert_ids == new_local_expert_ids # Local receive eligibility new_valid = new_local_expert_ids != -1 can_recv_local = np.isin( new_local_expert_ids, old_local_expert_ids, assume_unique=False ) is_received_locally = np.logical_or( is_unchanged, np.logical_and(new_valid, can_recv_local) ) # Send map: first src row per unique expert present locally in old mapping send_count = 0 valid_old = old_local_expert_ids != -1 if np.any(valid_old): uniq_experts, first_idx = np.unique( old_local_expert_ids[valid_old], return_index=True ) filtered_rows = local_rows[valid_old] src_rows = filtered_rows[first_idx] send_count = int(uniq_experts.shape[0]) send_expert_ids[:send_count] = uniq_experts send_src_rows[:send_count] = src_rows # Recv map: primary dst per unique expert needed remotely recv_count = 0 need_recv_mask = np.logical_and(~is_received_locally, new_valid) if np.any(need_recv_mask): desired_experts = new_local_expert_ids[need_recv_mask] desired_dsts = local_rows[need_recv_mask] uniq_recv_experts, uniq_indices = np.unique(desired_experts, return_index=True) dst_rows = desired_dsts[uniq_indices] recv_count = int(uniq_recv_experts.shape[0]) recv_expert_ids[:recv_count] = uniq_recv_experts recv_dst_rows[:recv_count] = dst_rows recv_primary_mask[dst_rows] = True eligible_local_buffer_mask = np.logical_and(~is_unchanged, is_received_locally) # 1. Local moves into tmp buffers if bool(eligible_local_buffer_mask.any()) and send_count > 0: dest_indices = np.nonzero(eligible_local_buffer_mask)[0].tolist() expert_to_src_map = dict( zip(send_expert_ids[:send_count], send_src_rows[:send_count]) ) for dst in dest_indices: expert = new_local_expert_ids[dst] src_local = expert_to_src_map.get(expert, -1) if src_local != -1: for w, b in zip(expert_weights, expert_weights_buffers): b[dst].copy_(w[src_local], non_blocking=True) p2p_ops: list[P2POp] = [] if isinstance(get_ep_group(), StatelessGroupCoordinator): ep_group = get_ep_group() is_stateless = True else: is_stateless = False # Pre-compute global ranks mapping (only needed for non-stateless groups) ep_size = ep_group.size() if not is_stateless: rank_to_global = { rank: get_global_rank(ep_group, rank) for rank in range(ep_size) } # 2. Post sends if send_count > 0: experts = send_expert_ids[:send_count] srcs = send_src_rows[:send_count] order = np.argsort(experts, kind="stable") experts = experts[order] srcs = srcs[order] send_map, recv_map = get_ep_ranks_with_experts_batch( experts, num_local_experts, old_indices, new_indices, ) for expert, src in zip(experts.tolist(), srcs.tolist()): ranks_to_send = send_map[expert] ranks_to_recv = recv_map[expert] if not ranks_to_send or not ranks_to_recv: continue num_dst_per_sender = len(ranks_to_recv) // len(ranks_to_send) sender_pos = ranks_to_send.index(ep_rank) recv_begin = sender_pos * num_dst_per_sender recv_end = recv_begin + num_dst_per_sender recv_ranks = ranks_to_recv[recv_begin:recv_end] remainder_start = len(ranks_to_send) * num_dst_per_sender recver_pos = remainder_start + sender_pos if recver_pos < len(ranks_to_recv): recv_ranks.append(ranks_to_recv[recver_pos]) for dst in recv_ranks: if is_stateless: for w in expert_weights: op = object.__new__(P2POp) op.op = torch.distributed.isend op.tensor = w[src] op.group_peer = dst p2p_ops.append(op) else: dst_global = rank_to_global[dst] p2p_ops += [ P2POp( torch.distributed.isend, w[src], dst_global, ) for w in expert_weights ] # 3. Post recvs if recv_count > 0: experts = recv_expert_ids[:recv_count] dsts = recv_dst_rows[:recv_count] order = np.argsort(experts, kind="stable") experts = experts[order] dsts = dsts[order] send_map, recv_map = get_ep_ranks_with_experts_batch( experts, num_local_experts, old_indices, new_indices, ) for expert, dst in zip(experts.tolist(), dsts.tolist()): ranks_to_send = send_map[expert] ranks_to_recv = recv_map[expert] if not ranks_to_send or not ranks_to_recv: continue num_dst_per_sender = len(ranks_to_recv) // len(ranks_to_send) recver_pos = ranks_to_recv.index(ep_rank) remainder_start = len(ranks_to_send) * num_dst_per_sender if recver_pos < remainder_start: src = ranks_to_send[recver_pos // num_dst_per_sender] else: src = ranks_to_send[recver_pos - remainder_start] if is_stateless: for b in expert_weights_buffers: op = object.__new__(P2POp) op.op = torch.distributed.irecv op.tensor = b[dst] op.group_peer = src p2p_ops.append(op) else: src_global = rank_to_global[src] p2p_ops += [ P2POp( torch.distributed.irecv, b[dst], src_global, ) for b in expert_weights_buffers ] # 4. Execute the P2P operations. The real communication happens here. if p2p_ops and cuda_stream is not None: with torch.cuda.stream(cuda_stream): if is_stateless: ep_group.device_communicator.batch_isend_irecv(p2p_ops) else: reqs = batch_isend_irecv(p2p_ops) for req in reqs: req.wait() elif p2p_ops: if is_stateless: ep_group.device_communicator.batch_isend_irecv(p2p_ops) else: reqs = batch_isend_irecv(p2p_ops) for req in reqs: req.wait() # wait for the communication to finish return ( is_unchanged, is_received_locally, RecvMetadata( recv_primary_mask=recv_primary_mask, recv_count=recv_count, recv_expert_ids=recv_expert_ids, recv_dst_rows=recv_dst_rows, ), ) def move_from_buffer( expert_weights: Sequence[torch.Tensor], expert_weights_buffers: list[torch.Tensor], is_unchanged: np.ndarray, is_received_locally: np.ndarray, recv_metadata: RecvMetadata, new_indices: np.ndarray, ep_rank: int, ) -> None: """ Copies expert weights from communication buffers back to the target weight tensors after EPLB rebalancing. Args: expert_weights: List of the actual MoE layer weights used in the execution. expert_weights_buffers: Intermediate buffers containing the experts weights after the transfer is completed. is_unchanged: (num_local_experts,), True where an expert row is unchanged. is_received_locally: (num_local_experts,), True where a row is updated locally. recv_metadata: RecvMetadata containing remote receive metadata. new_indices: (num_experts_total,) mapping from local rows to desired (possibly global) expert id, after rebalance. ep_rank: Rank of the process in the expert parallel group. """ recv_primary_mask = recv_metadata.recv_primary_mask recv_count = recv_metadata.recv_count recv_expert_ids = recv_metadata.recv_expert_ids recv_dst_rows = recv_metadata.recv_dst_rows num_local_experts = is_unchanged.shape[0] # Mask for rows to copy back from buffers: # copy if locally received OR remote primary recv copy_mask = np.logical_or(is_received_locally, recv_primary_mask) dest_mask_np = np.logical_and(~is_unchanged, copy_mask) if bool(dest_mask_np.any()): dest_indices = np.nonzero(dest_mask_np)[0].tolist() for dst in dest_indices: for w, b in zip(expert_weights, expert_weights_buffers): w[dst].copy_(b[dst], non_blocking=True) if recv_count == 0: return # Duplicate remote received rows to non-primary duplicate dsts base = ep_rank * num_local_experts local_experts = new_indices[base + np.arange(num_local_experts, dtype=np.int32)] duplicate_mask = np.logical_and( np.logical_and(~is_unchanged, ~is_received_locally), np.logical_and(~recv_primary_mask, local_experts != -1), ) # All received experts are unique in the destination, so no need to copy duplicates if not bool(duplicate_mask.any()): return dup_dst_rows = np.nonzero(duplicate_mask)[0] dup_experts = local_experts[dup_dst_rows] prim_experts = recv_expert_ids[:recv_count] prim_dsts = recv_dst_rows[:recv_count] order = np.argsort(prim_experts, kind="stable") prim_experts_sorted = prim_experts[order] prim_dsts_sorted = prim_dsts[order] pos = np.searchsorted(prim_experts_sorted, dup_experts) valid = np.logical_and( pos < prim_experts_sorted.shape[0], prim_experts_sorted[np.minimum(pos, prim_experts_sorted.shape[0] - 1)] == dup_experts, ) if not bool(valid.any()): return matched_dst_rows = dup_dst_rows[valid] matched_src_rows = prim_dsts_sorted[pos[valid]] for dst, src in zip(matched_dst_rows.tolist(), matched_src_rows.tolist()): for w in expert_weights: w[dst].copy_(w[src], non_blocking=True) async def transfer_layer( old_layer_indices: torch.Tensor, new_layer_indices: torch.Tensor, expert_weights: Sequence[torch.Tensor], expert_weights_buffer: Sequence[torch.Tensor], ep_group: ProcessGroup, is_profile: bool = False, cuda_stream: torch.cuda.Stream | None = None, rank_mapping: dict[int, int] | None = None, ) -> MoveToBufferResult: """ Rearranges the expert weights in place according to the new expert indices. The value of the indices arguments are logical indices of the experts, while keys are physical. Args: old_layer_indices: Shape (num_physical_experts,). new_layer_indices: Shape (num_physical_experts,). expert_weights: Iterable of weight tensors for this layer, each with shape (num_local_physical_experts, hidden_size_i). For example, a linear layer may have up and down projection. expert_weights_buffer: Intermediate buffers (one per weight tensor). ep_group: The device process group for expert parallelism. is_profile (bool): If `True`, do not perform any actual weight copy. This is used during profile run, where we only perform dummy communications to reserve enough memory for the buffers. cuda_stream: CUDA stream for async copies (can be None for sync mode). rank_mapping: Optional rank mapping for elastic expert parallelism. Returns: is_unchanged (np.ndarray): (num_local_experts,), True where expert is left unchanged. is_received_locally (np.ndarray): (num_local_experts,), True where expert can be received locally. RecvMetadata: Metadata needed for completing remote weight transfers. """ ep_size = ep_group.size() if rank_mapping is not None: # Add a layer dimension for compatibility with mapping functions old_layer_indices_2d = old_layer_indices.unsqueeze(0) new_layer_indices_2d = new_layer_indices.unsqueeze(0) if len(rank_mapping) == ep_group.size(): # scale down new_layer_indices_2d = _map_new_expert_indices_with_rank_mapping( new_layer_indices_2d, rank_mapping, ) else: # scale up old_layer_indices_2d = _map_old_expert_indices_with_rank_mapping( old_layer_indices_2d, rank_mapping, ep_group.size(), ) # Remove the layer dimension old_layer_indices = old_layer_indices_2d.squeeze(0) new_layer_indices = new_layer_indices_2d.squeeze(0) assert old_layer_indices.shape == new_layer_indices.shape num_physical_experts = old_layer_indices.shape[0] assert len(expert_weights[0]) >= 1 num_local_physical_experts = expert_weights[0].shape[0] assert num_physical_experts == ep_size * num_local_physical_experts old_layer_indices_np = old_layer_indices.cpu().numpy() new_layer_indices_np = new_layer_indices.cpu().numpy() is_unchanged, is_received_locally, recv_metadata = move_to_buffer( num_local_experts=num_local_physical_experts, old_indices=old_layer_indices_np, new_indices=new_layer_indices_np, expert_weights=expert_weights, expert_weights_buffers=expert_weights_buffer, cuda_stream=cuda_stream, ep_group=ep_group, ) return is_unchanged, is_received_locally, recv_metadata def rearrange_expert_weights_inplace( old_global_expert_indices: torch.Tensor, new_global_expert_indices: torch.Tensor, expert_weights: Sequence[Sequence[torch.Tensor]], ep_group: ProcessGroup, is_profile: bool = False, rank_mapping: dict[int, int] | None = None, ) -> None: """ Rearranges the expert weights in place according to the new expert indices. The value of the indices arguments are logical indices of the experts, while keys are physical. Args: old_global_expert_indices: Shape (num_moe_layers, num_physical_experts). new_global_expert_indices: Shape (num_moe_layers, num_physical_experts). expert_weights: A sequence of shape (num_moe_layers)(weight_count) of tensors of shape (num_local_physical_experts, hidden_size_i). For example, a linear layer may have up and down projection, so weight_count = 2. Each weight's hidden size can be different. ep_group: The device process group for expert parallelism. is_profile (bool): If `True`, do not perform any actual weight copy. This is used during profile run, where we only perform dummy communications to reserve enough memory for the buffers. rank_mapping: A dictionary mapping old rank to new rank. """ if rank_mapping is not None: if len(rank_mapping) == ep_group.size(): # scale down new_global_expert_indices = _map_new_expert_indices_with_rank_mapping( new_global_expert_indices, rank_mapping, ) else: # scale up old_global_expert_indices = _map_old_expert_indices_with_rank_mapping( old_global_expert_indices, rank_mapping, ep_group.size(), ) assert old_global_expert_indices.shape[1] == new_global_expert_indices.shape[1] num_moe_layers, num_physical_experts = old_global_expert_indices.shape assert len(expert_weights) == num_moe_layers assert len(expert_weights[0]) >= 1 num_local_physical_experts = expert_weights[0][0].shape[0] assert new_global_expert_indices.shape == (num_moe_layers, num_physical_experts) ep_size = ep_group.size() assert num_physical_experts == ep_size * num_local_physical_experts first_layer_weights = list(expert_weights[0]) # Buffers to hold the expert weights during the exchange. # NOTE: Currently we assume the same weights across different layers # have the same shape. weights_buffer: list[torch.Tensor] = [ torch.empty_like(w) for w in first_layer_weights ] if is_profile: # Reserve communication buffers via a minimal dummy all_gather on first layer for weight, buffer in zip(expert_weights[0], weights_buffer): dummy_recv_buffer = [buffer for _ in range(ep_size)] torch.distributed.barrier() all_gather( dummy_recv_buffer, weight, group=ep_group, ) return # NOTE(bowen): We need this synchronize to run, but I don't know why. # If you figure out the reason, please let me know -- thank you! torch.cuda.synchronize() old_global_expert_indices_cpu = old_global_expert_indices.cpu().numpy() new_global_expert_indices_cpu = new_global_expert_indices.cpu().numpy() for layer_idx in range(num_moe_layers): is_unchanged, is_received_locally, recv_metadata = move_to_buffer( num_local_experts=num_local_physical_experts, old_indices=old_global_expert_indices_cpu[layer_idx], new_indices=new_global_expert_indices_cpu[layer_idx], expert_weights=expert_weights[layer_idx], expert_weights_buffers=weights_buffer, cuda_stream=None, ep_group=ep_group, ) move_from_buffer( expert_weights=expert_weights[layer_idx], expert_weights_buffers=weights_buffer, is_unchanged=is_unchanged, is_received_locally=is_received_locally, recv_metadata=recv_metadata, new_indices=new_global_expert_indices_cpu[layer_idx], ep_rank=ep_group.rank(), ) def _map_old_expert_indices_with_rank_mapping( old_global_expert_indices: torch.Tensor, rank_mapping: dict[int, int], new_ep_size: int, ) -> torch.Tensor: """ Map the old global expert indices to the new global expert indices. Args: old_global_expert_indices: Shape (num_layers, old_ep_size * num_local_physical_experts). rank_mapping: Mapping from old rank to new rank. new_ep_size: New expert parallelism size. Returns: Mapped expert indices with shape (num_layers, new_ep_size * num_local_physical_experts). """ num_layers, old_num_physical_experts = old_global_expert_indices.shape assert rank_mapping, "Rank mapping is required" # Get sizes from parameters and rank_mapping old_ep_size = len(rank_mapping) num_local_physical_experts = old_num_physical_experts // old_ep_size new_num_physical_experts = new_ep_size * num_local_physical_experts # Create mapped tensor with new shape, initialized to -1 mapped_expert_indices = torch.full( (num_layers, new_num_physical_experts), fill_value=-1, dtype=old_global_expert_indices.dtype, device=old_global_expert_indices.device, ) # Handle rank mapping (scale up/down with rank changes) for old_rank in range(old_ep_size): new_rank = rank_mapping.get(old_rank) if new_rank is not None and new_rank >= 0 and new_rank < new_ep_size: # This old rank exists in the new configuration old_start_idx = old_rank * num_local_physical_experts old_end_idx = (old_rank + 1) * num_local_physical_experts new_start_idx = new_rank * num_local_physical_experts new_end_idx = (new_rank + 1) * num_local_physical_experts mapped_expert_indices[:, new_start_idx:new_end_idx] = ( old_global_expert_indices[:, old_start_idx:old_end_idx] ) # If new_rank is None or >= new_ep_size, the experts remain -1 # (scale down case) return mapped_expert_indices def _map_new_expert_indices_with_rank_mapping( new_global_expert_indices: torch.Tensor, rank_mapping: dict[int, int], ) -> torch.Tensor: num_layers, new_num_physical_experts = new_global_expert_indices.shape assert rank_mapping, "Rank mapping is required" # Get sizes from parameters and rank_mapping old_ep_size = len(rank_mapping) new_ep_size = sum(new_rank != -1 for new_rank in rank_mapping.values()) num_local_physical_experts = new_num_physical_experts // new_ep_size old_num_physical_experts = old_ep_size * num_local_physical_experts mapped_expert_indices = torch.full( (num_layers, old_num_physical_experts), fill_value=-1, dtype=new_global_expert_indices.dtype, device=new_global_expert_indices.device, ) for old_rank in range(old_ep_size): new_rank = rank_mapping[old_rank] if new_rank >= 0 and new_rank < new_ep_size: old_start_idx = old_rank * num_local_physical_experts old_end_idx = (old_rank + 1) * num_local_physical_experts new_start_idx = new_rank * num_local_physical_experts new_end_idx = (new_rank + 1) * num_local_physical_experts mapped_expert_indices[:, old_start_idx:old_end_idx] = ( new_global_expert_indices[:, new_start_idx:new_end_idx] ) return mapped_expert_indices __all__ = ["transfer_layer", "move_from_buffer", "RecvMetadata"]
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/distributed/eplb/rebalance_execute.py", "license": "Apache License 2.0", "lines": 637, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/v1/worker/xpu_model_runner.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from contextlib import contextmanager from typing import TYPE_CHECKING import torch from vllm.config import VllmConfig from vllm.logger import init_logger from vllm.utils.torch_utils import supports_xpu_graph from vllm.v1.worker.gpu_model_runner import GPUModelRunner if TYPE_CHECKING: pass logger = init_logger(__name__) class XPUModelRunner(GPUModelRunner): """A model runner for XPU devices.""" def __init__( self, vllm_config: VllmConfig, device: torch.device, ): with _torch_cuda_wrapper(): super().__init__(vllm_config, device) # FIXME: To be verified. self.cascade_attn_enabled = False def _sync_device(self) -> None: torch.xpu.synchronize() @contextmanager def _torch_cuda_wrapper(): try: # replace cuda APIs with xpu APIs, this should work by default torch.cuda.Stream = torch.xpu.Stream torch.cuda.default_stream = torch.xpu.current_stream torch.cuda.current_stream = torch.xpu.current_stream torch.cuda.stream = torch.xpu.stream torch.cuda.mem_get_info = torch.xpu.mem_get_info torch.cuda.synchronize = torch.xpu.synchronize if supports_xpu_graph(): torch.cuda.graph = torch.xpu.graph torch.cuda.CUDAGraph = torch.xpu.XPUGraph torch.cuda.empty_cache = torch.xpu.empty_cache yield finally: pass
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/worker/xpu_model_runner.py", "license": "Apache License 2.0", "lines": 42, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/v1/worker/xpu_worker.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import gc import os from typing import Any import torch from vllm.config import VllmConfig from vllm.logger import init_logger from vllm.platforms import current_platform from vllm.profiler.wrapper import TorchProfilerWrapper from vllm.utils.mem_utils import MemorySnapshot, format_gib from vllm.utils.torch_utils import set_random_seed from vllm.v1.utils import report_usage_stats from vllm.v1.worker.gpu_worker import Worker, init_worker_distributed_environment from vllm.v1.worker.workspace import init_workspace_manager from vllm.v1.worker.xpu_model_runner import XPUModelRunner from .utils import request_memory logger = init_logger(__name__) class XPUWorker(Worker): """A XPU worker class.""" def __init__( self, vllm_config: VllmConfig, local_rank: int, rank: int, distributed_init_method: str, is_driver_worker: bool = False, ): super().__init__( vllm_config, local_rank, rank, distributed_init_method, is_driver_worker ) device_config = self.device_config assert device_config.device_type == "xpu" assert current_platform.is_xpu() # Torch profiler. Enabled and configured through profiler_config. self.profiler: Any | None = None profiler_config = vllm_config.profiler_config if profiler_config.profiler == "torch": worker_name = f"{vllm_config.instance_id}-rank-{self.rank}" self.profiler = TorchProfilerWrapper( profiler_config, worker_name=worker_name, local_rank=self.local_rank, activities=["CPU", "XPU"], ) def init_device(self): device = self.device_config.device if ( isinstance(device, torch.device) and device.type == "xpu" and current_platform.is_xpu() ): self.device = torch.device(f"xpu:{self.local_rank}") current_platform.set_device(self.device) current_platform.check_if_supports_dtype(self.model_config.dtype) torch.xpu.empty_cache() self.init_gpu_memory = torch.xpu.get_device_properties( self.local_rank ).total_memory else: raise RuntimeError(f"Not support device type: {self.device_config.device}") ENV_CCL_ATL_TRANSPORT = os.getenv("CCL_ATL_TRANSPORT", "ofi") ENV_LOCAL_WORLD_SIZE = os.getenv( "LOCAL_WORLD_SIZE", str(self.parallel_config.world_size) ) os.environ["CCL_ATL_TRANSPORT"] = ENV_CCL_ATL_TRANSPORT os.environ["LOCAL_WORLD_SIZE"] = ENV_LOCAL_WORLD_SIZE os.environ["LOCAL_RANK"] = str(self.local_rank) init_worker_distributed_environment( self.vllm_config, self.rank, self.distributed_init_method, self.local_rank, current_platform.dist_backend, ) # Set random seed. set_random_seed(self.model_config.seed) # Now take memory snapshot after NCCL is initialized gc.collect() torch.xpu.empty_cache() # take current memory snapshot self.init_snapshot = init_snapshot = MemorySnapshot(device=self.device) self.requested_memory = request_memory(init_snapshot, self.cache_config) logger.debug("worker init memory snapshot: %r", self.init_snapshot) logger.debug( "worker requested memory: %sGiB", format_gib(self.requested_memory) ) # Initialize workspace manager num_ubatches = 2 if self.vllm_config.parallel_config.enable_dbo else 1 init_workspace_manager(self.device, num_ubatches) # Construct the model runner self.model_runner = XPUModelRunner( # type: ignore self.vllm_config, self.device ) if self.rank == 0: # If usage stat is enabled, collect relevant info. report_usage_stats(self.vllm_config)
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/worker/xpu_worker.py", "license": "Apache License 2.0", "lines": 97, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/config/test_config_generation.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest from vllm.engine.arg_utils import EngineArgs from vllm.model_executor.layers.quantization.quark.utils import deep_compare def test_cuda_empty_vs_unset_configs(monkeypatch: pytest.MonkeyPatch): """Test that configs created with normal (untouched) CUDA_VISIBLE_DEVICES and CUDA_VISIBLE_DEVICES="" are equivalent. This ensures consistent behavior regardless of whether GPU visibility is disabled via empty string or left in its normal state. """ def create_config(): engine_args = EngineArgs( model="deepseek-ai/DeepSeek-V2-Lite", trust_remote_code=True ) return engine_args.create_engine_config() # Create config with CUDA_VISIBLE_DEVICES set normally normal_config = create_config() # Create config with CUDA_VISIBLE_DEVICES="" with monkeypatch.context() as m: m.setenv("CUDA_VISIBLE_DEVICES", "") empty_config = create_config() normal_config_dict = vars(normal_config) empty_config_dict = vars(empty_config) # Remove instance_id before comparison as it's expected to be different normal_config_dict.pop("instance_id", None) empty_config_dict.pop("instance_id", None) assert deep_compare(normal_config_dict, empty_config_dict), ( 'Configs with normal CUDA_VISIBLE_DEVICES and CUDA_VISIBLE_DEVICES=""' " should be equivalent" ) def test_ray_runtime_env(monkeypatch: pytest.MonkeyPatch): # In testing, this method needs to be nested inside as ray does not # see the test module. def create_config(): engine_args = EngineArgs( model="deepseek-ai/DeepSeek-V2-Lite", trust_remote_code=True ) return engine_args.create_engine_config() config = create_config() parallel_config = config.parallel_config assert parallel_config.ray_runtime_env is None import ray ray.init() runtime_env = { "env_vars": { "TEST_ENV_VAR": "test_value", # In future ray versions, this will be default, so when setting a # task or actor with num_gpus=None/0, the visible devices env var # won't be overridden resulting in no GPUs being visible on a gpu # machine. "RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO": "0", }, } config_ref = ray.remote(create_config).options(runtime_env=runtime_env).remote() config = ray.get(config_ref) parallel_config = config.parallel_config assert parallel_config.ray_runtime_env is not None assert ( parallel_config.ray_runtime_env.env_vars().get("TEST_ENV_VAR") == "test_value" ) ray.shutdown() def test_unrecognized_env(monkeypatch): import os from vllm.envs import environment_variables # Remove any existing unrecognized VLLM env vars that might interfere for env in list(os.environ): if env.startswith("VLLM_") and env not in environment_variables: monkeypatch.delenv(env, raising=False) # Test that if fail_on_environ_validation is True, then an error # is raised when an unrecognized vLLM environment variable is set monkeypatch.setenv("VLLM_UNRECOGNIZED_ENV_VAR", "some_value") engine_args = EngineArgs( fail_on_environ_validation=True, ) with pytest.raises(ValueError, match="Unknown vLLM environment variable detected"): engine_args.create_engine_config() # Test that if fail_on_environ_validation is False, then no error is raised engine_args = EngineArgs() engine_args.create_engine_config() # Test that when the unrecognized env var is removed, no error is raised monkeypatch.delenv("VLLM_UNRECOGNIZED_ENV_VAR") engine_args = EngineArgs( fail_on_environ_validation=True, ) engine_args.create_engine_config()
{ "repo_id": "vllm-project/vllm", "file_path": "tests/config/test_config_generation.py", "license": "Apache License 2.0", "lines": 86, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/distributed/test_node_count.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import os import torch.distributed as dist from vllm.distributed.parallel_state import _node_count from vllm.distributed.utils import StatelessProcessGroup from vllm.utils.network_utils import get_ip, get_open_port if __name__ == "__main__": dist.init_process_group(backend="gloo") rank = dist.get_rank() world_size = dist.get_world_size() if rank == 0: port = get_open_port() ip = get_ip() dist.broadcast_object_list([ip, port], src=0) else: recv = [None, None] dist.broadcast_object_list(recv, src=0) ip, port = recv stateless_pg = StatelessProcessGroup.create(ip, port, rank, world_size) for pg in [dist.group.WORLD, stateless_pg]: test_result = _node_count(pg) # Expected node count based on environment variable) expected = int(os.environ.get("NUM_NODES", "1")) assert test_result == expected, f"Expected {expected} nodes, got {test_result}" if pg == dist.group.WORLD: print( f"Node count test passed! Got {test_result} nodes " f"when using torch distributed!" ) else: print( f"Node count test passed! Got {test_result} nodes " f"when using StatelessProcessGroup!" )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/distributed/test_node_count.py", "license": "Apache License 2.0", "lines": 35, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:examples/online_serving/openai_translation_client.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import json import httpx from openai import OpenAI from vllm.assets.audio import AudioAsset def sync_openai(audio_path: str, client: OpenAI, model: str): with open(audio_path, "rb") as f: translation = client.audio.translations.create( file=f, model=model, response_format="json", temperature=0.0, # Additional params not provided by OpenAI API. extra_body=dict( language="it", seed=4419, repetition_penalty=1.3, ), ) print("translation result:", translation.text) async def stream_openai_response( audio_path: str, base_url: str, api_key: str, model: str ): data = { "language": "it", "stream": True, "model": model, } url = base_url + "/audio/translations" headers = {"Authorization": f"Bearer {api_key}"} print("translation result:", end=" ") # OpenAI translation API client does not support streaming. async with httpx.AsyncClient() as client: with open(audio_path, "rb") as f: async with client.stream( "POST", url, files={"file": f}, data=data, headers=headers ) as response: async for line in response.aiter_lines(): # Each line is a JSON object prefixed with 'data: ' if line: if line.startswith("data: "): line = line[len("data: ") :] # Last chunk, stream ends if line.strip() == "[DONE]": break # Parse the JSON response chunk = json.loads(line) # Extract and print the content content = chunk["choices"][0].get("delta", {}).get("content") print(content, end="") def main(): foscolo = str(AudioAsset("azacinto_foscolo").get_local_path()) # Modify OpenAI's API key and API base to use vLLM's API server. openai_api_key = "EMPTY" openai_api_base = "http://localhost:8000/v1" client = OpenAI( api_key=openai_api_key, base_url=openai_api_base, ) model = client.models.list().data[0].id print(f"Using model: {model}") sync_openai(foscolo, client, model) # Run the asynchronous function asyncio.run(stream_openai_response(foscolo, openai_api_base, openai_api_key, model)) if __name__ == "__main__": main()
{ "repo_id": "vllm-project/vllm", "file_path": "examples/online_serving/openai_translation_client.py", "license": "Apache License 2.0", "lines": 68, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/entrypoints/openai/test_translation_validation.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import io # imports for structured outputs tests import json import httpx import librosa import numpy as np import openai import pytest import pytest_asyncio import soundfile as sf from ...utils import RemoteOpenAIServer from .conftest import add_attention_backend SERVER_ARGS = ["--enforce-eager"] def _get_server_args(attention_config): """Get server args with attention backend if specified.""" args = SERVER_ARGS.copy() add_attention_backend(args, attention_config) return args @pytest.fixture( scope="module", params=["openai/whisper-small", "google/gemma-3n-E2B-it"] ) def server(request, rocm_aiter_fa_attention): # Parametrize over model name with RemoteOpenAIServer( request.param, _get_server_args(rocm_aiter_fa_attention) ) as remote_server: yield remote_server, request.param @pytest_asyncio.fixture async def client_and_model(server): server, model_name = server async with server.get_async_client() as async_client: yield async_client, model_name @pytest.mark.asyncio async def test_non_asr_model(foscolo, rocm_aiter_fa_attention): # text to text model model_name = "JackFram/llama-68m" with RemoteOpenAIServer( model_name, _get_server_args(rocm_aiter_fa_attention) ) as remote_server: client = remote_server.get_async_client() with pytest.raises(openai.NotFoundError): await client.audio.translations.create( model=model_name, file=foscolo, temperature=0.0 ) @pytest.mark.asyncio async def test_basic_audio_with_lora(mary_had_lamb, rocm_aiter_fa_attention): """Ensure STT (translate) requests can pass LoRA through to generate.""" # ROCm SPECIFIC CONFIGURATION: # To ensure the test passes on ROCm, we modify the max model length to 512. # We DO NOT apply this to other platforms to maintain strict upstream parity. from vllm.platforms import current_platform # NOTE - careful to call this test before the module scoped server # fixture, otherwise it'll OOMkill the CI model_name = "ibm-granite/granite-speech-3.3-2b" lora_model_name = "speech" server_args = [ "--enforce-eager", "--enable-lora", "--max-lora-rank", "64", "--lora-modules", f"{lora_model_name}={model_name}", "--max-model-len", "512" if current_platform.is_rocm() else "2048", "--max-num-seqs", "1", ] add_attention_backend(server_args, rocm_aiter_fa_attention) # Based on https://github.com/openai/openai-cookbook/blob/main/examples/Whisper_prompting_guide.ipynb. with RemoteOpenAIServer(model_name, server_args) as remote_server: client = remote_server.get_async_client() translation = await client.audio.translations.create( model=lora_model_name, file=mary_had_lamb, extra_body=dict(language="en", to_language="es"), response_format="text", temperature=0.0, ) out = json.loads(translation)["text"].strip().lower() assert "pequeño" in out.split(" ") # NOTE: (NickLucche) the large-v3-turbo model was not trained on translation! @pytest.mark.asyncio async def test_basic_audio(foscolo, client_and_model): client, model_name = client_and_model translation = await client.audio.translations.create( model=model_name, file=foscolo, response_format="text", # TODO remove `language="it"` once language detection is implemented extra_body=dict(language="it", to_language="en"), temperature=0.0, ) out = json.loads(translation)["text"].strip().lower() assert "greek sea" in out @pytest.mark.asyncio async def test_audio_prompt(foscolo, client_and_model): client, model_name = client_and_model # Condition whisper on starting text prompt = "Nor have I ever" transcription = await client.audio.translations.create( model=model_name, file=foscolo, prompt=prompt, extra_body=dict(language="it", to_language="en"), response_format="text", temperature=0.0, ) out = json.loads(transcription)["text"] assert "Nor will I ever touch the sacred" not in out assert prompt not in out @pytest.mark.asyncio async def test_streaming_response(foscolo, client_and_model, server): client, model_name = client_and_model translation = "" res_no_stream = await client.audio.translations.create( model=model_name, file=foscolo, response_format="json", extra_body=dict(language="it", to_language="en", seed=42), temperature=0.0, ) # Stream via HTTPX since OpenAI translation client doesn't expose streaming server, model_name = server url = server.url_for("v1/audio/translations") headers = {"Authorization": f"Bearer {server.DUMMY_API_KEY}"} data = { "model": model_name, "language": "it", "to_language": "en", "stream": True, "temperature": 0.0, "seed": 42, } foscolo.seek(0) async with httpx.AsyncClient() as http_client: files = {"file": foscolo} async with http_client.stream( "POST", url, headers=headers, data=data, files=files ) as response: async for line in response.aiter_lines(): if not line: continue if line.startswith("data: "): line = line[len("data: ") :] if line.strip() == "[DONE]": break chunk = json.loads(line) text = chunk["choices"][0].get("delta", {}).get("content") translation += text or "" res_stream = translation.split() # NOTE There's a small non-deterministic issue here, likely in the attn # computation, which will cause a few tokens to be different, while still # being very close semantically. assert ( sum([x == y for x, y in zip(res_stream, res_no_stream.text.split())]) >= len(res_stream) * 0.9 ) @pytest.mark.asyncio async def test_stream_options(foscolo, server): server, model_name = server url = server.url_for("v1/audio/translations") headers = {"Authorization": f"Bearer {server.DUMMY_API_KEY}"} data = { "model": model_name, "language": "it", "to_language": "en", "stream": True, "stream_include_usage": True, "stream_continuous_usage_stats": True, "temperature": 0.0, } foscolo.seek(0) final = False continuous = True async with httpx.AsyncClient() as http_client: files = {"file": foscolo} async with http_client.stream( "POST", url, headers=headers, data=data, files=files ) as response: async for line in response.aiter_lines(): if not line: continue if line.startswith("data: "): line = line[len("data: ") :] if line.strip() == "[DONE]": break chunk = json.loads(line) choices = chunk.get("choices", []) if not choices: # final usage sent final = True else: continuous = continuous and ("usage" in chunk) assert final and continuous @pytest.mark.asyncio async def test_long_audio_request(foscolo, client_and_model): client, model_name = client_and_model if model_name == "google/gemma-3n-E2B-it": pytest.skip("Gemma3n does not support long audio requests") foscolo.seek(0) audio, sr = librosa.load(foscolo) repeated_audio = np.tile(audio, 2) # Repeated audio to buffer buffer = io.BytesIO() sf.write(buffer, repeated_audio, sr, format="WAV") buffer.seek(0) translation = await client.audio.translations.create( model=model_name, file=buffer, extra_body=dict(language="it", to_language="en"), response_format="text", temperature=0.0, ) out = json.loads(translation)["text"].strip().lower() assert out.count("greek sea") == 2 @pytest.mark.asyncio async def test_audio_with_max_tokens(mary_had_lamb, client_and_model): client, model_name = client_and_model transcription = await client.audio.translations.create( model=model_name, file=mary_had_lamb, response_format="text", temperature=0.0, extra_body={"max_completion_tokens": 1}, ) out = json.loads(transcription) out_text = out["text"] print(out_text) from transformers import AutoTokenizer tok = AutoTokenizer.from_pretrained(model_name) out_tokens = tok(out_text, add_special_tokens=False)["input_ids"] assert len(out_tokens) == 1 # max_completion_tokens > max_model_len # max_model_len=32768 for Gemma-3n-E2B-it transcription = await client.audio.transcriptions.create( model=model_name, file=mary_had_lamb, response_format="text", temperature=0.0, extra_body={ "max_completion_tokens": int(1e6), "repetition_penalty": 1.3, }, ) out = json.loads(transcription) out_text = out["text"] print(out_text) out_tokens = tok(out_text, add_special_tokens=False)["input_ids"] assert len(out_tokens) < 450 # ~Whisper max output len
{ "repo_id": "vllm-project/vllm", "file_path": "tests/entrypoints/openai/test_translation_validation.py", "license": "Apache License 2.0", "lines": 251, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tools/generate_cmake_presets.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import argparse import json import multiprocessing import os import sys from shutil import which try: # Try to get CUDA_HOME from PyTorch installation, which is the # most reliable source of truth for vLLM's build. from torch.utils.cpp_extension import CUDA_HOME except ImportError: print("Warning: PyTorch not found. Falling back to CUDA_HOME environment variable.") CUDA_HOME = os.environ.get("CUDA_HOME") def get_python_executable(): """Get the current Python executable, which is used to run this script.""" return sys.executable def get_cpu_cores(): """Get the number of CPU cores.""" return multiprocessing.cpu_count() def generate_presets(output_path="CMakeUserPresets.json", force_overwrite=False): """Generates the CMakeUserPresets.json file.""" print("Attempting to detect your system configuration...") # Detect NVCC nvcc_path = None if CUDA_HOME: prospective_path = os.path.join(CUDA_HOME, "bin", "nvcc") if os.path.exists(prospective_path): nvcc_path = prospective_path print(f"Found nvcc via torch.utils.cpp_extension.CUDA_HOME: {nvcc_path}") if not nvcc_path: nvcc_path = which("nvcc") if nvcc_path: print(f"Found nvcc in PATH: {nvcc_path}") if not nvcc_path: nvcc_path_input = input( "Could not automatically find 'nvcc'. Please provide the full " "path to nvcc (e.g., /usr/local/cuda/bin/nvcc): " ) nvcc_path = nvcc_path_input.strip() print(f"Using NVCC path: {nvcc_path}") # Detect Python executable python_executable = get_python_executable() if python_executable: print(f"Found Python via sys.executable: {python_executable}") else: python_executable_prompt = ( "Could not automatically find Python executable. Please provide " "the full path to your Python executable for vLLM development " "(typically from your virtual environment, e.g., " "/home/user/venvs/vllm/bin/python): " ) python_executable = input(python_executable_prompt).strip() if not python_executable: raise ValueError( "Could not determine Python executable. Please provide it manually." ) print(f"Using Python executable: {python_executable}") # Get CPU cores cpu_cores = get_cpu_cores() nvcc_threads = min(4, cpu_cores) cmake_jobs = max(1, cpu_cores // nvcc_threads) print( f"Detected {cpu_cores} CPU cores. " f"Setting NVCC_THREADS={nvcc_threads} and CMake jobs={cmake_jobs}." ) # Get vLLM project root (assuming this script is in vllm/tools/) project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) print(f"VLLM project root detected as: {project_root}") # Ensure python_executable path is absolute or resolvable if not os.path.isabs(python_executable) and which(python_executable): python_executable = os.path.abspath(which(python_executable)) elif not os.path.isabs(python_executable): print( f"Warning: Python executable '{python_executable}' is not an " "absolute path and not found in PATH. CMake might not find it." ) cache_variables = { "CMAKE_CUDA_COMPILER": nvcc_path, "CMAKE_BUILD_TYPE": "Release", "VLLM_PYTHON_EXECUTABLE": python_executable, "CMAKE_INSTALL_PREFIX": "${sourceDir}", "CMAKE_CUDA_FLAGS": "", "NVCC_THREADS": str(nvcc_threads), } # Detect compiler cache if which("sccache"): print("Using sccache for compiler caching.") for launcher in ("C", "CXX", "CUDA", "HIP"): cache_variables[f"CMAKE_{launcher}_COMPILER_LAUNCHER"] = "sccache" elif which("ccache"): print("Using ccache for compiler caching.") for launcher in ("C", "CXX", "CUDA", "HIP"): cache_variables[f"CMAKE_{launcher}_COMPILER_LAUNCHER"] = "ccache" else: print("No compiler cache ('ccache' or 'sccache') found.") configure_preset = { "name": "release", "binaryDir": "${sourceDir}/cmake-build-release", "cacheVariables": cache_variables, } if which("ninja"): print("Using Ninja generator.") configure_preset["generator"] = "Ninja" cache_variables["CMAKE_JOB_POOLS"] = f"compile={cmake_jobs}" else: print("Ninja not found, using default generator. Build may be slower.") presets = { "version": 6, # Keep in sync with CMakeLists.txt and requirements/build.txt "cmakeMinimumRequired": {"major": 3, "minor": 26, "patch": 1}, "configurePresets": [configure_preset], "buildPresets": [ { "name": "release", "configurePreset": "release", "jobs": cmake_jobs, } ], } output_file_path = os.path.join(project_root, output_path) if os.path.exists(output_file_path): if force_overwrite: print(f"Overwriting existing file '{output_file_path}'") else: overwrite = ( input(f"'{output_file_path}' already exists. Overwrite? (y/N): ") .strip() .lower() ) if overwrite != "y": print("Generation cancelled.") return try: with open(output_file_path, "w") as f: json.dump(presets, f, indent=4) print(f"Successfully generated '{output_file_path}'") print("\nTo use this preset:") print(f"1. Ensure you are in the vLLM root directory: cd {project_root}") print("2. Initialize CMake: cmake --preset release") print("3. Build+install: cmake --build --preset release --target install") except OSError as e: print(f"Error writing file: {e}") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--force-overwrite", action="store_true", help="Force overwrite existing CMakeUserPresets.json without prompting", ) args = parser.parse_args() generate_presets(force_overwrite=args.force_overwrite)
{ "repo_id": "vllm-project/vllm", "file_path": "tools/generate_cmake_presets.py", "license": "Apache License 2.0", "lines": 153, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/models/language/pooling/test_reward.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import json from typing import TYPE_CHECKING import pytest import torch import torch.nn.functional as F from transformers import AutoModel from vllm.platforms import current_platform from ....conftest import HfRunner from ....utils import VLLM_PATH from ...registry import HF_EXAMPLE_MODELS if TYPE_CHECKING: from _typeshed import StrPath FIXTURES_PATH = VLLM_PATH / "tests/models/fixtures" assert FIXTURES_PATH.exists() FIXTURE_REWARD_RESULT = { "Qwen/Qwen2.5-Math-PRM-7B": FIXTURES_PATH / "qwen2_5_math_prm_reward_step.json", } @pytest.fixture def math_step_prompts(): # ruff: noqa: E501 data = { "system": "Please reason step by step, and put your final answer within \\boxed{}. ", "query": "Sue lives in a fun neighborhood. One weekend, the neighbors decided to play a prank on Sue. On Friday morning, the neighbors placed 18 pink plastic flamingos out on Sue's front yard. On Saturday morning, the neighbors took back one third of the flamingos, painted them white, and put these newly painted white flamingos back out on Sue's front yard. Then, on Sunday morning, they added another 18 pink plastic flamingos to the collection. At noon on Sunday, how many more pink plastic flamingos were out than white plastic flamingos?", "response": [ "To find out how many more pink plastic flamingos were out than white plastic flamingos at noon on Sunday, we can break down the problem into steps. First, on Friday, the neighbors start with 18 pink plastic flamingos.", "On Saturday, they take back one third of the flamingos. Since there were 18 flamingos, (1/3 \\times 18 = 6) flamingos are taken back. So, they have (18 - 6 = 12) flamingos left in their possession. Then, they paint these 6 flamingos white and put them back out on Sue's front yard. Now, Sue has the original 12 pink flamingos plus the 6 new white ones. Thus, by the end of Saturday, Sue has (12 + 6 = 18) pink flamingos and 6 white flamingos.", "On Sunday, the neighbors add another 18 pink plastic flamingos to Sue's front yard. By the end of Sunday morning, Sue has (18 + 18 = 36) pink flamingos and still 6 white flamingos.", "To find the difference, subtract the number of white flamingos from the number of pink flamingos: (36 - 6 = 30). Therefore, at noon on Sunday, there were 30 more pink plastic flamingos out than white plastic flamingos. The answer is (\\boxed{30}).", ], } answer = "<extra_0>".join(data["response"]) + "<extra_0>" prompt = f"<im_start>system\n{data['system']}<im_end>\n<im_start>user\n{data['query']}<im_end>\n<im_start>assistant\n{answer}<im_end><|endoftext|>" return [prompt] def step_reward_patch_hf_model(hf_model: HfRunner): # Patch the hf_runner to use the step reward function def make_step_rewards( logits: torch.Tensor, token_masks: torch.Tensor ) -> list[list[float]]: probabilities = F.softmax(logits, dim=-1) probabilities = probabilities * token_masks.unsqueeze(-1) all_scores_res: list[list[float]] = [] for i in range(probabilities.size(0)): sample = probabilities[i] # seq_len, num_labels positive_probs = sample[sample != 0].view(-1, 2) non_zero_elements_list = positive_probs.cpu().tolist() all_scores_res.append(non_zero_elements_list) return all_scores_res def reward(prompts: list[str]) -> list[list[float]]: input_ids = hf_model.tokenizer(prompts, return_tensors="pt").input_ids input_ids = hf_model.wrap_device(input_ids) outputs = hf_model.model(input_ids=input_ids) step_sep_id = hf_model.tokenizer.encode("<extra_0>")[0] token_masks = input_ids == step_sep_id return make_step_rewards(outputs[0], token_masks) hf_model.reward = reward # type: ignore[attr-defined] return hf_model def dump_reward_outputs(outputs: list[list[float]], filename: "StrPath"): with open(filename, "w", encoding="utf-8") as f: json.dump(outputs, f) def load_reward_outputs(filename: "StrPath") -> list[list[float]]: with open(filename, encoding="utf-8") as f: return json.load(f) @pytest.mark.parametrize( "model", [ pytest.param( "Qwen/Qwen2.5-Math-PRM-7B", marks=[pytest.mark.core_model, pytest.mark.cpu_model], ), ], ) @pytest.mark.parametrize("dtype", ["half"]) def test_prm_models( hf_runner, vllm_runner, math_step_prompts, model: str, dtype: str, ) -> None: model_info = HF_EXAMPLE_MODELS.find_hf_info(model) model_info.check_transformers_version(on_fail="skip") if current_platform.is_cpu(): pytest.skip("CPU only supports V1") with vllm_runner(model, max_model_len=1024, dtype=dtype) as vllm_model: vllm_outputs = vllm_model.reward(math_step_prompts) with hf_runner(model, dtype=dtype, auto_cls=AutoModel) as hf_model: hf_model = step_reward_patch_hf_model(hf_model) hf_outputs = hf_model.reward(math_step_prompts) dump_reward_outputs( hf_outputs, FIXTURE_REWARD_RESULT[model], ) # check logits difference for hf_output, vllm_output in zip(hf_outputs, vllm_outputs): hf_output = torch.tensor(hf_output).float() vllm_output = torch.tensor(vllm_output).float() assert torch.allclose(hf_output, vllm_output, 1.5e-2) @pytest.mark.parametrize( "model", [ pytest.param( "Qwen/Qwen2.5-Math-PRM-7B", marks=[pytest.mark.core_model, pytest.mark.cpu_model], ), ], ) @pytest.mark.parametrize("dtype", ["half"]) def test_prm_models_with_golden_outputs( vllm_runner, math_step_prompts, model: str, dtype: str, ) -> None: if not FIXTURE_REWARD_RESULT.get(model): pytest.skip(f"No available golden outputs for {model}.") with vllm_runner(model, max_model_len=1024, dtype=dtype) as vllm_model: vllm_outputs = vllm_model.reward(math_step_prompts) golden_outputs = load_reward_outputs(FIXTURE_REWARD_RESULT[model]) # check logits difference for golden_output, vllm_output in zip(golden_outputs, vllm_outputs): golden_output = torch.tensor(golden_output).float() vllm_output = torch.tensor(vllm_output).float() assert torch.allclose(golden_output, vllm_output, 1.5e-2)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/models/language/pooling/test_reward.py", "license": "Apache License 2.0", "lines": 125, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/test_silu_mul_fp8_quant_deep_gemm.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import random import pytest import torch from vllm.model_executor.layers.fused_moe.batched_deep_gemm_moe import ( persistent_masked_m_silu_mul_quant, ) from vllm.platforms import current_platform from vllm.utils.deep_gemm import DeepGemmQuantScaleFMT, has_deep_gemm from vllm.utils.math_utils import cdiv, round_up from vllm.utils.torch_utils import set_random_seed if current_platform.is_fp8_fnuz(): pytest.skip( "Tests in this file require float8_e4m3fn and platform does not support", allow_module_level=True, ) fp8_dtype = torch.float8_e4m3fn CASES = [ (1, 1, 128, fp8_dtype), (1, 4, 128 * 1, fp8_dtype), (2, 4, 128 * 2, fp8_dtype), (1, 4, 128 * 3, fp8_dtype), (8, 16, 128 * 4, fp8_dtype), (8, 16, 128 * 5, fp8_dtype), (8, 16, 128 * 6, fp8_dtype), (8, 16, 128 * 7, fp8_dtype), (8, 16, 128 * 8, fp8_dtype), (8, 16, 128 * 9, fp8_dtype), (8, 64, 7168, fp8_dtype), (8, 128, 128 * 33, fp8_dtype), (1, 4, 128 * 10, fp8_dtype), (8, 128, 7168, fp8_dtype), (8, 512, 7168, fp8_dtype), (8, 1024, 7168, fp8_dtype), (17, 31, 768, fp8_dtype), (32, 64, 256, fp8_dtype), (256, 8, 7168, fp8_dtype), (256, 32, 7168, fp8_dtype), (256, 64, 7168, fp8_dtype), # Only add a few fnuz tests to help with long CI times. (8, 512, 7168, torch.float8_e4m3fnuz), (8, 1024, 7168, torch.float8_e4m3fnuz), ] def as_uint8(x) -> torch.Tensor: return ( torch.empty(x.shape, dtype=x.dtype, device=x.device).copy_(x).view(torch.uint8) ) def silu(x: torch.Tensor) -> torch.Tensor: one_f32 = torch.tensor([1.0], device=x.device, dtype=torch.float32) x_f32 = x.to(torch.float32) act_f32 = x_f32 / (one_f32 + torch.exp(-x_f32)) assert act_f32.dtype == torch.float32 return act_f32.to(torch.bfloat16) def do_quant(x: torch.Tensor, group_size: int, ceil_ue8m0: bool): eps_bf16 = torch.tensor([1e-10], device=x.device, dtype=torch.bfloat16) one_bf16 = torch.tensor([1.0], device=x.device, dtype=torch.bfloat16) fp8_max_bf16 = torch.tensor( [torch.finfo(fp8_dtype).max], device=x.device, dtype=torch.bfloat16 ) fp8_min_bf16 = torch.tensor( [torch.finfo(fp8_dtype).min], device=x.device, dtype=torch.bfloat16 ) fp8_max_inv = one_bf16 / fp8_max_bf16 assert fp8_max_inv.dtype == torch.bfloat16 assert x.size(-1) % group_size == 0 num_groups = x.numel() // group_size x_og_shape = x.shape x = x.to(torch.bfloat16) x = x.view((-1, group_size)) amax = x.abs().amax(dim=1).clamp(min=eps_bf16) assert amax.dtype == torch.bfloat16 s = amax * fp8_max_inv if ceil_ue8m0: s = torch.exp2( torch.ceil(torch.log2(s).to(torch.bfloat16)).to(torch.bfloat16) ).to(torch.bfloat16) inv_s = one_bf16 / s inv_s = inv_s.view((num_groups, 1)) xq = torch.clamp(x * inv_s, min=fp8_min_bf16.item(), max=fp8_max_bf16.item()).to( fp8_dtype ) xq = xq.view(x_og_shape) xs = s.view((-1, xq.size(-1) // group_size)) return xq, xs def silu_mul_quant( gate: torch.Tensor, up: torch.Tensor, group_size: int, ceil_ue8m0: bool ) -> tuple[torch.Tensor, torch.Tensor]: assert gate.size(-1) % group_size == 0 assert up.size(-1) % group_size == 0 assert gate.dtype == torch.bfloat16 assert up.dtype == torch.bfloat16 act_bf16 = silu(gate) assert act_bf16.dtype == torch.bfloat16 # act & mul a_m = act_bf16 * up assert a_m.dtype == torch.bfloat16 q, s = do_quant(a_m, group_size, ceil_ue8m0) return q, s def pack_scales(x: torch.Tensor, tokens_per_expert: torch.Tensor) -> torch.Tensor: """ pack float32 scales into a int32 tensor """ assert x.dtype == torch.float32 E, T, G = x.size() # Add i32_padding here so we can view it as a i32 tensor later on. i32_padding = round_up(G, 4) - G ref_s_i8 = torch.empty((E, T, G + i32_padding), dtype=torch.uint8, device="cuda") for e in range(E): nt = tokens_per_expert[e].item() ref_s_i8[e, :nt, :G] = x[e, :nt].view(torch.int32) >> 23 ref_s_i32 = ref_s_i8.view(torch.int32) return ref_s_i32 def ref_with_scale_fmt( E: int, T: int, H: int, group_size: int, tokens_per_expert: torch.Tensor, gate: torch.Tensor, up: torch.Tensor, scale_fmt: DeepGemmQuantScaleFMT, ) -> tuple[torch.Tensor, torch.Tensor]: """ The precision types of the operations triggered by this function match closely with the kernel implementation so we compare more accurately. """ scale_dtype = ( torch.int32 if scale_fmt == DeepGemmQuantScaleFMT.UE8M0 else torch.float32 ) ceil_ue8m0 = scale_fmt in [ DeepGemmQuantScaleFMT.UE8M0, DeepGemmQuantScaleFMT.FLOAT32_CEIL_UE8M0, ] ref_q = torch.empty((E, T, H), dtype=fp8_dtype, device="cuda") ref_s_f32 = torch.empty( (E, T, cdiv(H, group_size)), dtype=torch.float32, device="cuda" ) for e in range(E): nt = tokens_per_expert[e].item() if nt == 0: continue ref_q[e, :nt], ref_s_f32[e, :nt] = silu_mul_quant( gate[e, :nt], up[e, :nt], group_size, ceil_ue8m0=ceil_ue8m0 ) if scale_dtype == torch.float32: return ref_q, ref_s_f32 assert scale_dtype == torch.int32 return ref_q, pack_scales(ref_s_f32, tokens_per_expert) def token_random(E, T, H2, tokens_per_expert): """ Initialize each token in a random range so we test a range of scale values. """ y = torch.empty((E, T, H2), dtype=torch.bfloat16, device="cuda") for e in range(E): for t in range(tokens_per_expert[e].item()): exp = random.choice(range(1, 20)) y[e, t].uniform_(-(2**exp), 2**exp) return y @pytest.mark.parametrize("E,T,H,fp8_type", CASES) @torch.inference_mode() def test_silu_mul_fp8_quant_deep_gemm(E: int, T: int, H: int, fp8_type: torch.dtype): group_size = 128 set_random_seed(42) tokens_per_expert = torch.randint( low=0, high=T, size=(E,), dtype=torch.int32, device="cuda", ) # Input tensor of shape (E, T, 2*H) y = token_random(E, T, 2 * H, tokens_per_expert) gate = y[..., :H].to(torch.bfloat16) up = y[..., H:].to(torch.bfloat16) scale_fmts = [ DeepGemmQuantScaleFMT.FLOAT32, DeepGemmQuantScaleFMT.FLOAT32_CEIL_UE8M0, DeepGemmQuantScaleFMT.UE8M0, ] # Run the SiLU V2 kernel for scale_fmt in scale_fmts: y_q, y_s = persistent_masked_m_silu_mul_quant( y, tokens_per_expert, group_size=group_size, quant_scale_fmt=scale_fmt, ) ref_y_q, ref_y_s = ref_with_scale_fmt( E, T, H, group_size, tokens_per_expert, gate, up, scale_fmt=scale_fmt ) # deepgemm scales transform dg_scales = None if ( has_deep_gemm() and current_platform.has_device_capability(100) and scale_fmt == DeepGemmQuantScaleFMT.UE8M0 ): from deep_gemm import transform_sf_into_required_layout _q, _s = ref_with_scale_fmt( E, T, H, group_size, tokens_per_expert, gate, up, scale_fmt=DeepGemmQuantScaleFMT.FLOAT32_CEIL_UE8M0, ) dg_scales = transform_sf_into_required_layout( sf=_s, mn=_q.size(1), k=_q.size(2), recipe=(1, 128, 128), num_groups=_q.size(0), is_sfa=True, ) expected_scale_dtype = ( torch.int32 if scale_fmt == DeepGemmQuantScaleFMT.UE8M0 else torch.float32 ) assert y_s.dtype == expected_scale_dtype assert ref_y_s.dtype == expected_scale_dtype for e in range(E): nt = tokens_per_expert[e].item() torch.testing.assert_close( y_q[e, :nt].to(torch.float32), ref_y_q[e, :nt].to(torch.float32), ) if scale_fmt == DeepGemmQuantScaleFMT.UE8M0: G = H // group_size y_s_sliced = as_uint8(y_s[e]) ref_s_sliced = as_uint8(ref_y_s[e]) torch.testing.assert_close(y_s_sliced[:nt, :G], ref_s_sliced[:nt, :G]) if dg_scales is not None: dg_sliced = as_uint8(dg_scales[e]) torch.testing.assert_close(y_s_sliced[:nt, :G], dg_sliced[:nt, :G]) else: torch.testing.assert_close( y_s[e, :nt], ref_y_s[e, :nt], )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/test_silu_mul_fp8_quant_deep_gemm.py", "license": "Apache License 2.0", "lines": 243, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/v1/core/sched/request_queue.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import heapq from abc import ABC, abstractmethod from collections import deque from collections.abc import Iterable, Iterator from enum import Enum from vllm.v1.request import Request class SchedulingPolicy(Enum): """Enum for scheduling policies.""" FCFS = "fcfs" PRIORITY = "priority" class RequestQueue(ABC): """Abstract base class for request queues.""" @abstractmethod def add_request(self, request: Request) -> None: """Add a request to the queue according to the policy.""" pass @abstractmethod def pop_request(self) -> Request: """Pop a request from the queue according to the policy.""" pass @abstractmethod def peek_request(self) -> Request: """Peek at the request at the front of the queue without removing it.""" pass @abstractmethod def prepend_request(self, request: Request) -> None: """Prepend a request to the front of the queue.""" pass @abstractmethod def prepend_requests(self, requests: "RequestQueue") -> None: """Prepend all requests from another queue to the front of this queue.""" pass @abstractmethod def remove_request(self, request: Request) -> None: """Remove a specific request from the queue.""" pass @abstractmethod def remove_requests(self, requests: Iterable[Request]) -> None: """Remove multiple specific requests from the queue.""" pass @abstractmethod def __bool__(self) -> bool: """Check if queue has any requests.""" pass @abstractmethod def __len__(self) -> int: """Get number of requests in queue.""" pass @abstractmethod def __iter__(self) -> Iterator[Request]: """Iterate over the queue according to the policy.""" pass class FCFSRequestQueue(deque[Request], RequestQueue): """A first-come-first-served queue that supports deque operations.""" def add_request(self, request: Request) -> None: """Add a request to the queue according to FCFS policy.""" self.append(request) def pop_request(self) -> Request: """Pop a request from the queue according to FCFS policy.""" return self.popleft() def peek_request(self) -> Request: """Peek at the next request in the queue without removing it.""" if not self: raise IndexError("peek from an empty queue") return self[0] def prepend_request(self, request: Request) -> None: """Prepend a request to the front of the queue.""" self.appendleft(request) def prepend_requests(self, requests: RequestQueue) -> None: """Prepend all requests from another queue to the front of this queue. Note: The requests will be prepended in reverse order of their appearance in the `requests` queue. """ self.extendleft(requests) def remove_request(self, request: Request) -> None: """Remove a specific request from the queue.""" self.remove(request) def remove_requests(self, requests: Iterable[Request]) -> None: """Remove multiple specific requests from the queue.""" requests_to_remove = set(requests) filtered_requests = [req for req in self if req not in requests_to_remove] # deque does not support in-place filtering, so we need to clear # and extend self.clear() self.extend(filtered_requests) def __bool__(self) -> bool: """Check if queue has any requests.""" return len(self) > 0 def __len__(self) -> int: """Get number of requests in queue.""" return super().__len__() def __iter__(self) -> Iterator[Request]: """Iterate over the queue according to FCFS policy.""" return super().__iter__() class PriorityRequestQueue(RequestQueue): """ A priority queue that supports heap operations. Respects the ordering defined in the Request class, where requests with a smaller value of `priority` are processed first. If multiple requests have the same priority, the one with the earlier `arrival_time` is processed first. """ def __init__(self) -> None: self._heap: list[Request] = [] def add_request(self, request: Request) -> None: """Add a request to the queue according to priority policy.""" heapq.heappush(self._heap, request) def pop_request(self) -> Request: """Pop a request from the queue according to priority policy.""" if not self._heap: raise IndexError("pop from empty heap") return heapq.heappop(self._heap) def peek_request(self) -> Request: """Peek at the next request in the queue without removing it.""" if not self._heap: raise IndexError("peek from empty heap") return self._heap[0] def prepend_request(self, request: Request) -> None: """Add a request to the queue according to priority policy. Note: In a priority queue, there is no concept of prepending to the front. Requests are ordered by (priority, arrival_time).""" self.add_request(request) def prepend_requests(self, requests: RequestQueue) -> None: """Add all requests from another queue according to priority policy. Note: In a priority queue, there is no concept of prepending to the front. Requests are ordered by (priority, arrival_time).""" for request in requests: self.add_request(request) def remove_request(self, request: Request) -> None: """Remove a specific request from the queue.""" self._heap.remove(request) heapq.heapify(self._heap) def remove_requests(self, requests: Iterable[Request]) -> None: """Remove multiple specific requests from the queue.""" requests_to_remove = requests if isinstance(requests, set) else set(requests) self._heap = [r for r in self._heap if r not in requests_to_remove] heapq.heapify(self._heap) def __bool__(self) -> bool: """Check if queue has any requests.""" return bool(self._heap) def __len__(self) -> int: """Get number of requests in queue.""" return len(self._heap) def __iter__(self) -> Iterator[Request]: """Iterate over the queue according to priority policy.""" heap_copy = self._heap[:] while heap_copy: yield heapq.heappop(heap_copy) def create_request_queue(policy: SchedulingPolicy) -> RequestQueue: """Create request queue based on scheduling policy.""" if policy == SchedulingPolicy.PRIORITY: return PriorityRequestQueue() elif policy == SchedulingPolicy.FCFS: return FCFSRequestQueue() else: raise ValueError(f"Unknown scheduling policy: {policy}")
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/core/sched/request_queue.py", "license": "Apache License 2.0", "lines": 160, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/plugins/vllm_add_dummy_platform/vllm_add_dummy_platform/dummy_custom_ops.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch from vllm.model_executor.layers.rotary_embedding import RotaryEmbedding # Register CustomRotaryEmbedding to CustomOP. @RotaryEmbedding.register_oot class DummyRotaryEmbedding(RotaryEmbedding): """Original rotary positional embedding.""" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.addition_config = True def forward_oot(self, *args, **kwargs) -> tuple[torch.Tensor, torch.Tensor]: return super().forward_oot(*args, **kwargs)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/plugins/vllm_add_dummy_platform/vllm_add_dummy_platform/dummy_custom_ops.py", "license": "Apache License 2.0", "lines": 13, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/cuda/test_cuda_context.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import ctypes from concurrent.futures import ThreadPoolExecutor import pytest import torch from vllm.platforms import current_platform def check_cuda_context(): """Check CUDA driver context status""" try: cuda = ctypes.CDLL("libcuda.so") device = ctypes.c_int() result = cuda.cuCtxGetDevice(ctypes.byref(device)) return (True, device.value) if result == 0 else (False, None) except Exception: return False, None def run_cuda_test_in_thread(device_input, expected_device_id): """Run CUDA context test in separate thread for isolation""" try: # New thread should have no CUDA context initially valid_before, device_before = check_cuda_context() if valid_before: return ( False, "CUDA context should not exist in new thread, " f"got device {device_before}", ) # Test setting CUDA context current_platform.set_device(device_input) # Verify context is created correctly valid_after, device_id = check_cuda_context() if not valid_after: return False, "CUDA context should be valid after set_cuda_context" if device_id != expected_device_id: return False, f"Expected device {expected_device_id}, got {device_id}" return True, "Success" except Exception as e: return False, f"Exception in thread: {str(e)}" class TestSetCudaContext: """Test suite for the set_cuda_context function.""" @pytest.mark.skipif(not current_platform.is_cuda(), reason="CUDA not available") @pytest.mark.parametrize( argnames="device_input,expected_device_id", argvalues=[ (0, 0), (torch.device("cuda:0"), 0), ("cuda:0", 0), ], ids=["int", "torch_device", "string"], ) def test_set_cuda_context_parametrized(self, device_input, expected_device_id): """Test setting CUDA context in isolated threads.""" with ThreadPoolExecutor(max_workers=1) as executor: future = executor.submit( run_cuda_test_in_thread, device_input, expected_device_id ) success, message = future.result(timeout=30) assert success, message @pytest.mark.skipif(not current_platform.is_cuda(), reason="CUDA not available") def test_set_cuda_context_invalid_device_type(self): """Test error handling for invalid device type.""" with pytest.raises(ValueError, match="Expected a cuda device"): current_platform.set_device(torch.device("cpu")) if __name__ == "__main__": pytest.main([__file__, "-v"])
{ "repo_id": "vllm-project/vllm", "file_path": "tests/cuda/test_cuda_context.py", "license": "Apache License 2.0", "lines": 65, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:examples/online_serving/openai_chat_completion_client_with_tools_xlam.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # ruff: noqa: E501 """ Set up this example by starting a vLLM OpenAI-compatible server with tool call options enabled for xLAM-2 models: vllm serve --model Salesforce/Llama-xLAM-2-8b-fc-r --enable-auto-tool-choice --tool-call-parser xlam OR vllm serve --model Salesforce/xLAM-2-3b-fc-r --enable-auto-tool-choice --tool-call-parser xlam """ import json import time from openai import OpenAI # Modify OpenAI's API key and API base to use vLLM's API server. openai_api_key = "empty" openai_api_base = "http://localhost:8000/v1" # Define tool functions def get_weather(location: str, unit: str): return f"Weather in {location} is 22 degrees {unit}." def calculate_expression(expression: str): try: result = eval(expression) return f"The result of {expression} is {result}" except Exception as e: return f"Could not calculate {expression}: {e}" def translate_text(text: str, target_language: str): return f"Translation of '{text}' to {target_language}: [translated content]" # Define tools tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "City and state, e.g., 'San Francisco, CA'", }, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, }, "required": ["location", "unit"], }, }, }, { "type": "function", "function": { "name": "calculate_expression", "description": "Calculate a mathematical expression", "parameters": { "type": "object", "properties": { "expression": { "type": "string", "description": "Mathematical expression to evaluate, needs to be a valid python expression", } }, "required": ["expression"], }, }, }, { "type": "function", "function": { "name": "translate_text", "description": "Translate text to another language", "parameters": { "type": "object", "properties": { "text": {"type": "string", "description": "Text to translate"}, "target_language": { "type": "string", "description": "Target language for translation", }, }, "required": ["text", "target_language"], }, }, }, ] # Map of function names to implementations tool_functions = { "get_weather": get_weather, "calculate_expression": calculate_expression, "translate_text": translate_text, } def process_response(response, tool_functions, original_query): """Process a non-streaming response with possible tool calls""" print("\n--- Response Output ---") # Check if the response has content if response.choices[0].message.content: print(f"Content: {response.choices[0].message.content}") # Check if the response has tool calls if response.choices[0].message.tool_calls: print("--------------------------------") print(f"Tool calls: {response.choices[0].message.tool_calls}") print("--------------------------------") # Collect all tool calls and results before making follow-up request tool_results = [] assistant_message = {"role": "assistant"} if response.choices[0].message.content: assistant_message["content"] = response.choices[0].message.content assistant_tool_calls = [] # Process each tool call for tool_call in response.choices[0].message.tool_calls: function_name = tool_call.function.name function_args = tool_call.function.arguments function_id = tool_call.id print(f"Function called: {function_name}") print(f"Arguments: {function_args}") print(f"Function ID: {function_id}") # Execute the function try: # Parse the JSON arguments args = json.loads(function_args) # Call the function with the arguments function_result = tool_functions[function_name](**args) print(f"\n--- Function Result ---\n{function_result}\n") # Add tool call to assistant message assistant_tool_calls.append( { "id": function_id, "type": "function", "function": {"name": function_name, "arguments": function_args}, } ) # Add tool result to tool_results tool_results.append( { "role": "tool", "tool_call_id": function_id, "content": function_result, } ) except Exception as e: print(f"Error executing function: {e}") # Add tool_calls to assistant message assistant_message["tool_calls"] = assistant_tool_calls # Create a follow-up message with all function results follow_up_messages = [ {"role": "user", "content": original_query}, assistant_message, ] # Add all tool results to the messages follow_up_messages.extend(tool_results) # Get completion with all tool results in a single follow-up follow_up_response = client.chat.completions.create( model=client.models.list().data[0].id, messages=follow_up_messages, stream=False, ) print("\n--- Follow-up Response ---") print(follow_up_response.choices[0].message.content) print("--- End Follow-up ---\n") print("--- End Response ---\n") def run_test_case(query, test_name): """Run a single test case with the given query""" print(f"\n{'=' * 50}\nTEST CASE: {test_name}\n{'=' * 50}") print(f"Query: '{query}'") start_time = time.time() # Create non-streaming chat completion request response = client.chat.completions.create( model=client.models.list().data[0].id, messages=[{"role": "user", "content": query}], tools=tools, tool_choice="auto", stream=False, ) # Process the non-streaming response, passing the original query process_response(response, tool_functions, query) end_time = time.time() print(f"Test completed in {end_time - start_time:.2f} seconds") def main(): # Initialize OpenAI client global client client = OpenAI( api_key=openai_api_key, base_url=openai_api_base, ) # Run test cases test_cases = [ ("I want to know the weather in San Francisco", "Weather Information"), ("Calculate 25 * 17 + 31", "Math Calculation"), ("Translate 'Hello world' to Spanish", "Text Translation"), ("What is the weather in Tokyo and New York in celsius", "Multiple Tool Usage"), ] # Execute all test cases for query, test_name in test_cases: run_test_case(query, test_name) time.sleep(1) # Small delay between tests print("\nAll tests completed.") if __name__ == "__main__": main()
{ "repo_id": "vllm-project/vllm", "file_path": "examples/online_serving/openai_chat_completion_client_with_tools_xlam.py", "license": "Apache License 2.0", "lines": 196, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:examples/online_serving/openai_chat_completion_client_with_tools_xlam_streaming.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # ruff: noqa: E501 """ Set up this example by starting a vLLM OpenAI-compatible server with tool call options enabled for xLAM-2 models: vllm serve --model Salesforce/Llama-xLAM-2-8b-fc-r --enable-auto-tool-choice --tool-call-parser xlam OR vllm serve --model Salesforce/xLAM-2-3b-fc-r --enable-auto-tool-choice --tool-call-parser xlam This example demonstrates streaming tool calls with xLAM models. """ import json import time from openai import OpenAI # Modify OpenAI's API key and API base to use vLLM's API server. openai_api_key = "empty" openai_api_base = "http://localhost:8000/v1" # Define tool functions def get_weather(location: str, unit: str): return f"Weather in {location} is 22 degrees {unit}." def calculate_expression(expression: str): try: result = eval(expression) return f"The result of {expression} is {result}" except Exception as e: return f"Could not calculate {expression}: {e}" def translate_text(text: str, target_language: str): return f"Translation of '{text}' to {target_language}: [translated content]" # Define tools tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "City and state, e.g., 'San Francisco, CA'", }, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, }, "required": ["location", "unit"], }, }, }, { "type": "function", "function": { "name": "calculate_expression", "description": "Calculate a mathematical expression", "parameters": { "type": "object", "properties": { "expression": { "type": "string", "description": "Mathematical expression to evaluate, needs to be a valid Python expression", } }, "required": ["expression"], }, }, }, { "type": "function", "function": { "name": "translate_text", "description": "Translate text to another language", "parameters": { "type": "object", "properties": { "text": {"type": "string", "description": "Text to translate"}, "target_language": { "type": "string", "description": "Target language for translation", }, }, "required": ["text", "target_language"], }, }, }, ] # Map of function names to implementations tool_functions = { "get_weather": get_weather, "calculate_expression": calculate_expression, "translate_text": translate_text, } def process_stream(response, tool_functions, original_query): """Process a streaming response with possible tool calls""" # Track multiple tool calls tool_calls = {} # Dictionary to store tool calls by ID current_id = None print("\n--- Stream Output ---") for chunk in response: # Handle tool calls in the stream if chunk.choices[0].delta.tool_calls: for tool_call_chunk in chunk.choices[0].delta.tool_calls: # Get the tool call ID if hasattr(tool_call_chunk, "id") and tool_call_chunk.id: current_id = tool_call_chunk.id if current_id not in tool_calls: tool_calls[current_id] = { "function_name": None, "function_args": "", "function_id": current_id, } # Extract function information as it comes in chunks if ( hasattr(tool_call_chunk, "function") and current_id and current_id in tool_calls ): if ( hasattr(tool_call_chunk.function, "name") and tool_call_chunk.function.name ): tool_calls[current_id]["function_name"] = ( tool_call_chunk.function.name ) print(f"Function called: {tool_call_chunk.function.name}") if ( hasattr(tool_call_chunk.function, "arguments") and tool_call_chunk.function.arguments ): tool_calls[current_id]["function_args"] += ( tool_call_chunk.function.arguments ) print(f"Arguments chunk: {tool_call_chunk.function.arguments}") # Handle regular content in the stream elif chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="") print("\n--- End Stream ---\n") # Execute each function call and build messages for follow-up follow_up_messages = [{"role": "user", "content": original_query}] for tool_id, tool_data in tool_calls.items(): function_name = tool_data["function_name"] function_args = tool_data["function_args"] function_id = tool_data["function_id"] if function_name and function_args: try: # Parse the JSON arguments args = json.loads(function_args) # Call the function with the arguments function_result = tool_functions[function_name](**args) print( f"\n--- Function Result ({function_name}) ---\n{function_result}\n" ) # Add the assistant message with tool call follow_up_messages.append( { "role": "assistant", "tool_calls": [ { "id": function_id, "type": "function", "function": { "name": function_name, "arguments": function_args, }, } ], } ) # Add the tool message with function result follow_up_messages.append( { "role": "tool", "tool_call_id": function_id, "content": function_result, } ) except Exception as e: print(f"Error executing function: {e}") # Only send follow-up if we have results to process if len(follow_up_messages) > 1: # Create a follow-up message with all the function results follow_up_response = client.chat.completions.create( model=client.models.list().data[0].id, messages=follow_up_messages, stream=True, ) print("\n--- Follow-up Response ---") for chunk in follow_up_response: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="") print("\n--- End Follow-up ---\n") def run_test_case(query, test_name): """Run a single test case with the given query""" print(f"\n{'=' * 50}\nTEST CASE: {test_name}\n{'=' * 50}") print(f"Query: '{query}'") start_time = time.time() # Create streaming chat completion request response = client.chat.completions.create( model=client.models.list().data[0].id, messages=[{"role": "user", "content": query}], tools=tools, tool_choice="auto", stream=True, ) # Process the streaming response process_stream(response, tool_functions, query) end_time = time.time() print(f"Test completed in {end_time - start_time:.2f} seconds") def main(): # Initialize OpenAI client global client client = OpenAI( api_key=openai_api_key, base_url=openai_api_base, ) # Run test cases test_cases = [ ("I want to know the weather in San Francisco", "Weather Information"), ("Calculate 25 * 17 + 31", "Math Calculation"), ("Translate 'Hello world' to Spanish", "Text Translation"), ("What is the weather in Tokyo and New York in celsius", "Multiple Tool Usage"), ] # Execute all test cases for query, test_name in test_cases: run_test_case(query, test_name) time.sleep(1) # Small delay between tests print("\nAll tests completed.") if __name__ == "__main__": main()
{ "repo_id": "vllm-project/vllm", "file_path": "examples/online_serving/openai_chat_completion_client_with_tools_xlam_streaming.py", "license": "Apache License 2.0", "lines": 227, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/models/language/generation/test_gemma.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import numpy as np import pytest MODELS = ["google/gemma-2b", "google/gemma-2-2b", "google/gemma-3-4b-it"] @pytest.mark.parametrize("model", MODELS) def test_dummy_loader(vllm_runner, monkeypatch, model: str) -> None: with monkeypatch.context() as m: m.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1") with vllm_runner( model, load_format="dummy", ) as llm: if model == "google/gemma-3-4b-it": normalizers = llm.llm.collective_rpc( lambda self: self.model_runner.model.language_model.model.normalizer.cpu().item() # noqa: E501 ) config = llm.llm.llm_engine.model_config.hf_config.text_config else: normalizers = llm.llm.collective_rpc( lambda self: self.model_runner.model.model.normalizer.cpu().item() ) config = llm.llm.llm_engine.model_config.hf_config assert np.allclose(normalizers, config.hidden_size**0.5, rtol=2e-3)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/models/language/generation/test_gemma.py", "license": "Apache License 2.0", "lines": 24, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/v1/pool/metadata.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from dataclasses import dataclass import numpy as np import torch from vllm.pooling_params import PoolingParams from vllm.tasks import PoolingTask from vllm.utils.platform_utils import is_pin_memory_available pin_memory = is_pin_memory_available() @dataclass class PoolingCursor: index: list[int] first_token_indices_gpu: torch.Tensor last_token_indices_gpu: torch.Tensor prompt_lens_cpu: torch.Tensor seq_lens_cpu: torch.Tensor num_scheduled_tokens_cpu: torch.Tensor def __getitem__(self, indices: slice): return PoolingCursor( index=self.index[indices], first_token_indices_gpu=self.first_token_indices_gpu[indices], last_token_indices_gpu=self.last_token_indices_gpu[indices], prompt_lens_cpu=self.prompt_lens_cpu[indices], seq_lens_cpu=self.seq_lens_cpu[indices], num_scheduled_tokens_cpu=self.num_scheduled_tokens_cpu[indices], ) def is_partial_prefill(self): return not torch.all(self.prompt_lens_cpu == self.num_scheduled_tokens_cpu) def is_finished(self): return self.prompt_lens_cpu == self.seq_lens_cpu class PoolingStates: def __init__(self): # for chunked prefill with ALL pooling self.hidden_states_cache: list[torch.Tensor] = [] def clean(self): self.hidden_states_cache.clear() @dataclass class PoolingMetadata: """Tensors for pooling.""" prompt_lens: torch.Tensor # CPU Tensor prompt_token_ids: torch.Tensor | None pooling_params: list[PoolingParams] pooling_states: list[PoolingStates] pooling_cursor: PoolingCursor | None = None def __post_init__(self) -> None: pooling_params = self.pooling_params tasks: list[PoolingTask] = [ task for pooling_param in pooling_params if (task := pooling_param.task) is not None ] assert len(pooling_params) == len(tasks) self.tasks = tasks def __getitem__(self, indices: slice): return PoolingMetadata( prompt_lens=self.prompt_lens[indices], prompt_token_ids=None if self.prompt_token_ids is None else self.prompt_token_ids[indices], pooling_params=self.pooling_params[indices], pooling_states=self.pooling_states[indices], pooling_cursor=None if self.pooling_cursor is None else self.pooling_cursor[indices], ) def get_prompt_token_ids(self) -> list[torch.Tensor]: prompt_token_ids = self.prompt_token_ids assert prompt_token_ids is not None, ( "Please set `requires_token_ids=True` in `get_pooling_updates`" ) return [prompt_token_ids[i, :num] for i, num in enumerate(self.prompt_lens)] def get_pooling_cursor(self) -> PoolingCursor: pooling_cursor = self.pooling_cursor assert pooling_cursor is not None, "Should call `build_pooling_cursor` first" return pooling_cursor def build_pooling_cursor( self, num_scheduled_tokens_np: np.ndarray, seq_lens_cpu: torch.Tensor, device: torch.device, ): n_seq = len(num_scheduled_tokens_np) prompt_lens = self.prompt_lens assert len(prompt_lens) == n_seq index = list(range(n_seq)) num_scheduled_tokens_cpu = torch.from_numpy(num_scheduled_tokens_np) cumsum = torch.zeros( n_seq + 1, dtype=torch.int64, pin_memory=pin_memory, device="cpu" ) torch.cumsum(num_scheduled_tokens_cpu, dim=0, out=cumsum[1:]) cumsum = cumsum.to(device, non_blocking=True) self.pooling_cursor = PoolingCursor( index=index, first_token_indices_gpu=cumsum[:n_seq], last_token_indices_gpu=cumsum[1:] - 1, prompt_lens_cpu=prompt_lens, seq_lens_cpu=seq_lens_cpu, num_scheduled_tokens_cpu=num_scheduled_tokens_cpu, )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/pool/metadata.py", "license": "Apache License 2.0", "lines": 99, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/v1/attention/backends/rocm_aiter_fa.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Attention layer with AiterFlashAttention.""" from dataclasses import dataclass from typing import ClassVar import torch from vllm._aiter_ops import rocm_aiter_ops from vllm.config import VllmConfig, get_layers_from_vllm_config from vllm.logger import init_logger from vllm.model_executor.layers.attention import Attention from vllm.platforms import current_platform from vllm.utils.math_utils import cdiv from vllm.utils.platform_utils import num_compute_units from vllm.v1.attention.backend import ( AttentionBackend, AttentionCGSupport, AttentionImpl, AttentionMetadataBuilder, AttentionType, CommonAttentionMetadata, MultipleOf, ) from vllm.v1.attention.backends.utils import ( split_decodes_prefills_and_extends, ) from vllm.v1.attention.ops.merge_attn_states import merge_attn_states from vllm.v1.kv_cache_interface import AttentionSpec _PARTITION_SIZE_ROCM = 256 _CP_TOKENS_PER_ITER_ROCM = 32 * 1024 if current_platform.is_rocm(): from vllm.triton_utils import tl, triton def block_size(x, head_dim): return min(65536 // x.element_size(), triton.next_power_of_2(head_dim)) def num_programs(total_tokens): return min(total_tokens, num_compute_units()) @triton.jit def cp_mha_gather_cache_kernel( key_cache_ptr, # [num_blocks, page_size, num_head, head_size] value_cache_ptr, # [num_blocks, page_size, num_head, head_size] key_ptr, # [num_tokens, num_heads, head_size] value_ptr, # [num_tokens, num_heads, head_size] block_table_ptr, # [num_batches, max_block_num] cu_seqlens_kv_ptr, # [num_batches + 1] token_to_batch_ptr, # [max_cum_tokens] seq_start_ptr, # [num_batches] k_scale_ptr, # [1] / [num_blocks, num_kv_heads, page_size] v_scale_ptr, num_heads, head_size, x, max_block_num, DEQUANT: tl.constexpr, PAGE_SIZE: tl.constexpr, CACHE_FORMAT: tl.constexpr, BLOCK_SIZE: tl.constexpr, ): token_id = tl.program_id(0) head_id = tl.program_id(1) col_offsets = tl.arange(0, BLOCK_SIZE) key_ptr_offset = ( key_ptr + token_id * head_size * num_heads + head_id * head_size ) value_ptr_offset = ( value_ptr + token_id * head_size * num_heads + head_id * head_size ) batch_idx = tl.load(token_to_batch_ptr + token_id) batch_start = tl.load(seq_start_ptr + batch_idx) token_start = tl.load(cu_seqlens_kv_ptr + batch_idx) batch_offset = token_id - token_start + batch_start block_offset = batch_offset // PAGE_SIZE block_id = tl.load( block_table_ptr + max_block_num * batch_idx + block_offset ).to(tl.int64) slot_id = batch_offset % PAGE_SIZE if CACHE_FORMAT == "NHD": # for kv cache layout as # K: [num_blocks, page_size, num_head, head_dim] # V: [num_blocks, page_size, num_head, head_dim] key_cache_ptr_offset = ( key_cache_ptr + block_id * num_heads * head_size * PAGE_SIZE + slot_id * num_heads * head_size + head_id * head_size ) value_cache_ptr_offset = ( value_cache_ptr + block_id * num_heads * head_size * PAGE_SIZE + slot_id * num_heads * head_size + head_id * head_size ) k_reg = tl.load(key_cache_ptr_offset + col_offsets) v_reg = tl.load(value_cache_ptr_offset + col_offsets) if DEQUANT: k_scale = tl.load(k_scale_ptr) v_scale = tl.load(v_scale_ptr) k_dtype = k_reg.dtype v_dtype = v_reg.dtype k_reg = (k_reg.to(tl.float32) * k_scale).to(k_dtype) v_reg = (v_reg.to(tl.float32) * v_scale).to(v_dtype) tl.store(key_ptr_offset + col_offsets, k_reg) tl.store(value_ptr_offset + col_offsets, v_reg) elif CACHE_FORMAT == "SHUFFLE": # for kv cache layout as # K: [num_blocks, num_head, head_dim // x, page_size, x] # V: [num_blocks, num_head, page_size // x, head_dim, x] key_cache_ptr_offset = ( key_cache_ptr + block_id * num_heads * head_size * PAGE_SIZE + head_id * head_size * PAGE_SIZE + slot_id * x ) value_cache_ptr_offset = ( value_cache_ptr + block_id * num_heads * head_size * PAGE_SIZE + head_id * head_size * PAGE_SIZE + (slot_id // x) * head_size * x + slot_id % x ) k_reg_offset = col_offsets // x * PAGE_SIZE * x + col_offsets % x v_reg_offset = col_offsets * x k_reg = tl.load(key_cache_ptr_offset + k_reg_offset) v_reg = tl.load(value_cache_ptr_offset + v_reg_offset) if DEQUANT: k_scale = 1.0 v_scale = 1.0 k_reg = k_reg.to(tl.float32) * k_scale v_reg = v_reg.to(tl.float32) * v_scale tl.store(key_ptr_offset + col_offsets, k_reg) tl.store(value_ptr_offset + col_offsets, v_reg) def cp_mha_gather_cache( key_cache: torch.Tensor, value_cache: torch.Tensor, key: torch.Tensor, value: torch.Tensor, block_tables: torch.Tensor, k_scales: torch.Tensor, v_scales: torch.Tensor, cu_seqlens_kv: torch.Tensor, token_to_batch: torch.Tensor, seq_starts: torch.Tensor, dequant: bool, kv_cache_layout: str, total_tokens: int, ): assert kv_cache_layout in ["NHD", "SHUFFLE"], ( "kv_cache_layout only support NHD, SHUFFLE" ) head_dim = key.shape[2] x = 16 // key_cache.element_size() # assert dequant is True, "Currently, we only support "\ # "gather cache with dequant" # For k cache layout: [num_blocks, num_heads, page_size, head_dim] assert head_dim == key_cache.shape[3], ( "We assume your kv cache layout is [num_blocks, " "page_size, num_heads, head_dim], but got otherwise" ) page_size = key_cache.shape[1] num_heads = key_cache.shape[2] grid = lambda meta: (total_tokens, num_heads) cp_mha_gather_cache_kernel[grid]( key_cache, value_cache, key, value, block_tables, cu_seqlens_kv, token_to_batch, seq_starts, k_scales, v_scales, num_heads, head_dim, x, block_tables.size(1), DEQUANT=dequant, PAGE_SIZE=page_size, CACHE_FORMAT=kv_cache_layout, BLOCK_SIZE=head_dim, ) @triton.jit def reshape_and_cache_shuffle_kernel( key_ptr, # [num_tokens, num_kv_heads, head_size] value_ptr, # [num_tokens, num_kv_heads, head_size] key_cache_ptr, # [num_blocks, num_kv_heads, head_size // x, block_size, x] value_cache_ptr, # [num_blocks, num_kv_heads, block_size // x, head_size, x] slot_mapping_ptr, # [num_tokens] k_scale_ptr, # [num_blocks, num_kv_heads, block_size] v_scale_ptr, # [num_blocks, num_kv_heads, block_size] x, k_stride0, v_stride0, block_size, head_size, num_kv_heads, BLOCK_SIZE: tl.constexpr, QUANT: tl.constexpr, IS_FNUZ: tl.constexpr, ): tid = tl.program_id(0) head_id = tl.program_id(1) offset = tl.arange(0, BLOCK_SIZE) src_offset_k = tid * k_stride0 + head_id * head_size src_offset_v = tid * v_stride0 + head_id * head_size slot_id = tl.load(slot_mapping_ptr + tid) if slot_id < 0: return block_id = slot_id // block_size block_offset = slot_id % block_size dst_offset = ( block_id * num_kv_heads * head_size * block_size + head_id * head_size * block_size ) dst_k_shuffle_offset = ( dst_offset + offset // x * block_size * x + block_offset * x + offset % x ) dst_v_shuffle_offset = ( dst_offset + block_offset // x * head_size * x + offset * x + block_offset % x ) k_val = tl.load(key_ptr + src_offset_k + offset) v_val = tl.load(value_ptr + src_offset_v + offset) if QUANT: k_scale = 1.0 v_scale = 1.0 k_dtype = key_cache_ptr.type.element_ty v_dtype = value_cache_ptr.type.element_ty k_val = (k_val.to(tl.float32) / k_scale).to(k_dtype) v_val = (v_val.to(tl.float32) / v_scale).to(v_dtype) tl.store(key_cache_ptr + dst_k_shuffle_offset, k_val) tl.store(value_cache_ptr + dst_v_shuffle_offset, v_val) def reshape_and_cache_shuffle_triton( key: torch.Tensor, value: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, slot_mapping: torch.Tensor, kv_cache_dtype: str, k_scales: torch.Tensor, v_scales: torch.Tensor, ): num_tokens = slot_mapping.shape[0] _, num_kv_heads, head_size = key.shape num_blocks, block_size, _, _ = key_cache.shape x = 16 // key_cache.element_size() k_cache_template = torch.empty( [num_blocks, num_kv_heads, head_size // x, block_size, x], dtype=key_cache.dtype, device="meta", ) v_cache_template = torch.empty( [num_blocks, num_kv_heads, block_size // x, head_size, x], dtype=value_cache.dtype, device="meta", ) new_key_cache = key_cache.view_as(k_cache_template) new_value_cache = value_cache.view_as(v_cache_template) QUANT = False if kv_cache_dtype.startswith("fp8"): QUANT = True grid = ( num_tokens, num_kv_heads, ) reshape_and_cache_shuffle_kernel[grid]( key, value, new_key_cache, new_value_cache, slot_mapping, k_scales, v_scales, x, key.stride(0), value.stride(0), block_size, head_size, num_kv_heads, BLOCK_SIZE=head_size, QUANT=QUANT, IS_FNUZ=current_platform.fp8_dtype() == torch.float8_e4m3fnuz, ) logger = init_logger(__name__) @dataclass class AiterFlashAttentionDecodeMetadata: max_query_len: int min_query_len: int max_seq_len: int query_start_loc: torch.Tensor @dataclass class AiterFlashAttentionPrefillMetadata: max_query_len: int min_query_len: int max_seq_len: int query_start_loc: torch.Tensor @dataclass class AiterChunkSlidingWindowMetadata: swa_seqlens: torch.Tensor swa_cu_seqlens: torch.Tensor swa_seq_starts: torch.Tensor swa_token_to_batch: torch.Tensor swa_max_seqlens: int swa_total_tokens: int swa_workspace: torch.Tensor @dataclass class AiterChunkContextMetadata: workspace: torch.Tensor cu_seq_lens_chunk: torch.Tensor chunk_starts: torch.Tensor token_to_batch: torch.Tensor seq_tot: list[int] max_seq_lens: list[int] seq_lens: torch.Tensor num_chunks: int total_token_per_batch: list[int] swa_metadata: AiterChunkSlidingWindowMetadata | None @dataclass class AiterFlashAttentionChunkPrefillMetadata: max_query_len: int min_query_len: int max_seq_len: int query_start_loc: torch.Tensor chunk_context_metadata: AiterChunkContextMetadata @dataclass class AiterFlashAttentionMetadata: # NOTE(sang): Definition of context_len, query_len, and seq_len. # |---------- N-1 iteration --------| # |---------------- N iteration ---------------------| # |- tokenA -|......................|-- newTokens ---| # |---------- context_len ----------| # |-------------------- seq_len ---------------------| # |-- query_len ---| num_actual_tokens: int # Number of tokens excluding padding. num_actual_kv_tokens: int max_query_len: int query_start_loc: torch.Tensor max_seq_len: int seq_lens: torch.Tensor slot_mapping: torch.Tensor block_table: torch.Tensor # prefill and deocde split num_decodes: int num_decode_tokens: int num_prefills: int num_prefill_tokens: int num_extends: int num_extend_tokens: int decode_metadata: AiterFlashAttentionDecodeMetadata | None prefill_metadata: AiterFlashAttentionPrefillMetadata | None extend_metadata: AiterFlashAttentionChunkPrefillMetadata | None # For cascade attention. use_cascade: bool common_prefix_len: int total_tokens: int # Only for fp8 shuffle layout kv cache, we allocate kv_scale for each layer # since we might integrate per token quant for kv cache in the future. k_scale: dict[str, torch.Tensor] | None v_scale: dict[str, torch.Tensor] | None class AiterFlashAttentionMetadataBuilder( AttentionMetadataBuilder[AiterFlashAttentionMetadata] ): _cudagraph_support = AttentionCGSupport.UNIFORM_BATCH def __init__( self, kv_cache_spec: AttentionSpec, layer_names: list[str], vllm_config: VllmConfig, device: torch.device, ): super().__init__(kv_cache_spec, layer_names, vllm_config, device) self.model_config = vllm_config.model_config self.parallel_config = vllm_config.parallel_config self.cache_config = vllm_config.cache_config self.num_heads_q = self.model_config.get_num_attention_heads( self.parallel_config ) self.num_heads_kv = self.model_config.get_num_kv_heads(self.parallel_config) self.headdim = self.model_config.get_head_size() self.block_size = kv_cache_spec.block_size # Sliding window size to be used with the AOT scheduler will be # populated on first build() call. self.aot_sliding_window: tuple[int, int] | None = None self.total_tokens: int = 0 self._init_reorder_batch_threshold(1, supports_spec_as_decode=True) sliding_window_configs: set[tuple[int, int] | None] = set() layers = get_layers_from_vllm_config(self.vllm_config, Attention) for name, layer in layers.items(): if name not in layer_names: continue assert isinstance(layer.impl, AiterFlashAttentionImpl), ( "Aiter Flash Attention Metadata Builder can only be used " "with Aiter Flash Attention Impl." ) sliding_window_configs.add(layer.impl.sliding_window) while len(sliding_window_configs) > 0: sliding_window_config = sliding_window_configs.pop() if sliding_window_config is not None and sliding_window_config[0] != -1: assert self.aot_sliding_window is None, ( "Aiter Flash ATTENTION can only support one valid sliding window!" ) self.aot_sliding_window = sliding_window_config self.extend_workspace = torch.empty( [2, _CP_TOKENS_PER_ITER_ROCM, self.num_heads_kv, self.headdim], dtype=self.model_config.dtype, device=device, ) self.scale = torch.tensor([1.0], dtype=torch.float, device=self.device) def build_for_cudagraph_capture( self, common_attn_metadata: CommonAttentionMetadata ): self.total_tokens = ( self.model_config.max_model_len * self.vllm_config.scheduler_config.max_num_partial_prefills ) res = self.build(common_prefix_len=0, common_attn_metadata=common_attn_metadata) self.total_tokens = 0 return res def build( self, common_prefix_len: int, common_attn_metadata: CommonAttentionMetadata, fast_build: bool = False, ) -> "AiterFlashAttentionMetadata": assert self.reorder_batch_threshold is not None split_ret = split_decodes_prefills_and_extends( common_attn_metadata, decode_threshold=self.reorder_batch_threshold, ) # Allocate scales for fp8 shuffle kv cache with shuffle_kv_cache enabled if ( rocm_aiter_ops.is_shuffle_kv_cache_enabled() and self.scale.numel() == 1 and self.vllm_config.cache_config.cache_dtype.startswith("fp8") ): layers = get_layers_from_vllm_config(self.vllm_config, Attention) first_layer_name = [k for k in layers][0] kv_cache_shape = ( self.vllm_config.compilation_config.static_forward_context[ first_layer_name ] .kv_cache[0] .shape ) num_blocks = kv_cache_shape[1] self.scale = torch.ones( [num_blocks, self.num_heads_kv, self.block_size], dtype=torch.float32, device=self.device, ) ( num_decodes, num_extends, num_prefills, num_decode_tokens, num_extend_tokens, num_prefill_tokens, ) = split_ret query_start_loc_cpu = common_attn_metadata.query_start_loc_cpu seq_lens = common_attn_metadata.seq_lens.cpu() query_lens_cpu = query_start_loc_cpu[1:] - query_start_loc_cpu[:-1] decode_metadata = None if num_decodes > 0: decode_metadata = AiterFlashAttentionDecodeMetadata( max_query_len=query_lens_cpu[:num_decodes].max().item(), min_query_len=query_lens_cpu[:num_decodes].min().item(), max_seq_len=seq_lens[:num_decodes].max().item(), query_start_loc=common_attn_metadata.query_start_loc[: num_decodes + 1], ) prefill_metadata = None if num_prefills > 0: query_lens_for_prefill = query_lens_cpu[num_decodes + num_extends :] query_start_loc_device = common_attn_metadata.query_start_loc[ num_decodes + num_extends : ] prefill_metadata = AiterFlashAttentionPrefillMetadata( max_query_len=query_lens_for_prefill.max().item(), min_query_len=query_lens_for_prefill.min().item(), max_seq_len=seq_lens[num_decodes + num_extends :].max().item(), query_start_loc=query_start_loc_device - query_start_loc_device[0], ) extend_metadata = None if num_extends > 0: num_extends_slice = slice(num_decodes, num_decodes + num_extends) query_lens_for_extend = query_lens_cpu[num_extends_slice] seq_lens_for_extend = seq_lens[num_extends_slice] computed_kv_lens = seq_lens_for_extend - query_lens_for_extend swa_metadata = None if self.aot_sliding_window is not None: swa_seqlen_for_extend = torch.minimum( seq_lens_for_extend, query_lens_for_extend + self.aot_sliding_window[0] + 1, ) cu_seq_lens = torch.zeros( num_extends + 1, dtype=torch.int32, device=seq_lens_for_extend.device, ) torch.cumsum( swa_seqlen_for_extend, dim=0, dtype=cu_seq_lens.dtype, out=cu_seq_lens[1:], ) token_to_seq = torch.arange( 0, num_extends, dtype=torch.int32, device=seq_lens_for_extend.device, ) token_to_seq = torch.repeat_interleave( token_to_seq, swa_seqlen_for_extend ) fetched_shape = cu_seq_lens[-1].item() # TODO(ganyi): Maybe reuse these 2 buffer from extend_workspace swa_workspace = torch.empty( (2, fetched_shape, self.num_heads_kv, self.headdim), dtype=self.vllm_config.model_config.dtype, device=self.device, ) seq_starts = seq_lens_for_extend - swa_seqlen_for_extend max_seqlen_k = swa_seqlen_for_extend.max().item() total_tokens = cu_seq_lens[-1].item() swa_metadata = AiterChunkSlidingWindowMetadata( swa_seqlens=swa_seqlen_for_extend.to( self.device, non_blocking=True ), swa_cu_seqlens=cu_seq_lens.to(self.device, non_blocking=True), swa_seq_starts=seq_starts.to(self.device, non_blocking=True), swa_token_to_batch=token_to_seq.to(self.device, non_blocking=True), swa_max_seqlens=max_seqlen_k, swa_total_tokens=total_tokens, swa_workspace=swa_workspace, ) # allocate the equal amount of workspace for # each chunk prefill request max_context_chunk = _CP_TOKENS_PER_ITER_ROCM // num_extends num_chunks = cdiv(computed_kv_lens.max().item(), max_context_chunk) chunk_starts = ( torch.arange(num_chunks, dtype=torch.int32) .unsqueeze(1) .expand(-1, num_extends) * max_context_chunk ) chunk_ends = torch.min( computed_kv_lens.unsqueeze(0), chunk_starts + max_context_chunk ) chunk_seq_lens = (chunk_ends - chunk_starts).clamp( min=0 ) # [num_chunks, num_extends] cu_seq_lens_cpu = torch.zeros( [num_chunks, num_extends + 1], dtype=torch.int32, pin_memory=True ) torch.cumsum( chunk_seq_lens, dim=1, out=cu_seq_lens_cpu[:, 1:], dtype=torch.int32 ) max_cum_tokens = cu_seq_lens_cpu[:, -1].max().item() range_idx = torch.arange(max_cum_tokens, dtype=torch.int32)[None, None, :] idx_to_batch_tensor = range_idx == cu_seq_lens_cpu[:, 1:][:, :, None] idx_to_batch_tensor = idx_to_batch_tensor.sum( dim=1 ) # [num_chunks, max_cum_tokens] token_to_batch_tensor = torch.cumsum(idx_to_batch_tensor, dim=1) chunk_context_metadata = AiterChunkContextMetadata( workspace=self.extend_workspace, cu_seq_lens_chunk=cu_seq_lens_cpu.to(self.device, non_blocking=True), chunk_starts=chunk_starts.to(self.device, non_blocking=True), seq_tot=chunk_seq_lens.sum(dim=1).tolist(), max_seq_lens=chunk_seq_lens.max(dim=1).values.tolist(), seq_lens=chunk_seq_lens, token_to_batch=token_to_batch_tensor.to(self.device, non_blocking=True), num_chunks=num_chunks, total_token_per_batch=cu_seq_lens_cpu[:, -1].tolist(), swa_metadata=swa_metadata, ) query_start_loc_device = common_attn_metadata.query_start_loc[ num_decodes : num_decodes + num_extends + 1 ] seq_lens_device = common_attn_metadata.seq_lens[num_extends_slice] cu_seq_lens = torch.zeros( num_extends + 1, dtype=torch.int32, device=seq_lens_device.device ) torch.cumsum( seq_lens_device, dim=0, dtype=cu_seq_lens.dtype, out=cu_seq_lens[1:] ) extend_metadata = AiterFlashAttentionChunkPrefillMetadata( max_query_len=query_lens_for_extend.max().item(), min_query_len=query_lens_for_extend.min().item(), max_seq_len=seq_lens[num_extends_slice].max().item(), query_start_loc=query_start_loc_device - query_start_loc_device[0], chunk_context_metadata=chunk_context_metadata, ) num_actual_kv_tokens = torch.sum(seq_lens).item() use_cascade = common_prefix_len > 0 attn_metadata = AiterFlashAttentionMetadata( num_actual_tokens=common_attn_metadata.num_actual_tokens, num_actual_kv_tokens=num_actual_kv_tokens, max_query_len=common_attn_metadata.max_query_len, query_start_loc=common_attn_metadata.query_start_loc, max_seq_len=common_attn_metadata.max_seq_len, seq_lens=common_attn_metadata.seq_lens, block_table=common_attn_metadata.block_table_tensor, slot_mapping=common_attn_metadata.slot_mapping, num_decodes=num_decodes, num_decode_tokens=num_decode_tokens, num_prefills=num_prefills, num_prefill_tokens=num_prefill_tokens, num_extends=num_extends, num_extend_tokens=num_extend_tokens, decode_metadata=decode_metadata, prefill_metadata=prefill_metadata, extend_metadata=extend_metadata, use_cascade=use_cascade, common_prefix_len=common_prefix_len, total_tokens=self.total_tokens, k_scale=self.scale, v_scale=self.scale, ) return attn_metadata def build_for_drafting( self, common_attn_metadata: CommonAttentionMetadata, draft_index: int, ) -> AiterFlashAttentionMetadata: """ Build attention metadata for draft model without CPU-GPU sync. During EAGLE drafting all requests are uniform decodes, so we can skip split_decodes_prefills_and_extends() and avoid all .cpu() / .item() calls that would otherwise break CUDA graph capture. """ num_reqs = common_attn_metadata.num_reqs num_tokens = common_attn_metadata.num_actual_tokens decode_metadata = AiterFlashAttentionDecodeMetadata( max_query_len=common_attn_metadata.max_query_len, min_query_len=common_attn_metadata.max_query_len, # uniform batch max_seq_len=common_attn_metadata.max_seq_len, query_start_loc=common_attn_metadata.query_start_loc, ) return AiterFlashAttentionMetadata( num_actual_tokens=num_tokens, num_actual_kv_tokens=0, # not used in unified_attention path max_query_len=common_attn_metadata.max_query_len, query_start_loc=common_attn_metadata.query_start_loc, max_seq_len=common_attn_metadata.max_seq_len, seq_lens=common_attn_metadata.seq_lens, block_table=common_attn_metadata.block_table_tensor, slot_mapping=common_attn_metadata.slot_mapping, num_decodes=num_reqs, num_decode_tokens=num_tokens, num_prefills=0, num_prefill_tokens=0, num_extends=0, num_extend_tokens=0, decode_metadata=decode_metadata, prefill_metadata=None, extend_metadata=None, use_cascade=False, common_prefix_len=0, total_tokens=self.total_tokens, k_scale=self.scale, v_scale=self.scale, ) def use_cascade_attention(self, *args, **kwargs) -> bool: return False class AiterFlashAttentionBackend(AttentionBackend): accept_output_buffer: bool = True supported_dtypes: ClassVar[list[torch.dtype]] = [torch.float16, torch.bfloat16] @staticmethod def get_supported_kernel_block_sizes() -> list[int | MultipleOf]: return [16, 32] @classmethod def get_supported_head_sizes(cls) -> list[int]: return [64, 128, 256] forward_includes_kv_cache_update: bool = False @staticmethod def get_name() -> str: return "FLASH_ATTN" @staticmethod def get_impl_cls() -> type["AiterFlashAttentionImpl"]: return AiterFlashAttentionImpl @staticmethod def get_builder_cls() -> type["AiterFlashAttentionMetadataBuilder"]: return AiterFlashAttentionMetadataBuilder @staticmethod def get_kv_cache_shape( num_blocks: int, block_size: int, num_kv_heads: int, head_size: int, cache_dtype_str: str = "auto", ) -> tuple[int, ...]: if block_size % 16 != 0: raise ValueError("Block size must be a multiple of 16.") return (2, num_blocks, block_size, num_kv_heads, head_size) class AiterFlashAttentionImpl(AttentionImpl): def __init__( self, num_heads: int, head_size: int, scale: float, num_kv_heads: int, alibi_slopes: list[float] | None, sliding_window: int | None, kv_cache_dtype: str, logits_soft_cap: float | None = None, attn_type: AttentionType = AttentionType.DECODER, kv_sharing_target_layer_name: int | None = None, ) -> None: self.num_heads = num_heads self.head_size = head_size self.scale = float(scale) self.num_kv_heads = num_kv_heads if alibi_slopes is not None: alibi_slopes = torch.tensor(alibi_slopes, dtype=torch.float32) self.alibi_slopes = alibi_slopes if sliding_window is None: self.sliding_window = (-1, -1) else: self.sliding_window = (sliding_window - 1, 0) self.kv_cache_dtype = kv_cache_dtype if logits_soft_cap is None: # In flash-attn, setting logits_soft_cap as 0 means no soft cap. logits_soft_cap = 0.0 self.logits_soft_cap = logits_soft_cap self.kv_sharing_target_layer_name = kv_sharing_target_layer_name assert self.num_heads % self.num_kv_heads == 0 self.num_queries_per_kv = self.num_heads // self.num_kv_heads if attn_type not in [AttentionType.DECODER, AttentionType.ENCODER_DECODER]: raise NotImplementedError( "Encoder self-attention is not implemented for FlashAttentionImpl" ) def extend_for_sliding_window( self, attn_metadata: AiterFlashAttentionMetadata, query: torch.Tensor, key_cache, value_cache, output: torch.Tensor, cu_seqlens_q: torch.Tensor, max_seqlen_q: int, block_table: torch.Tensor, k_scale: float, v_scale: float, ): assert attn_metadata.extend_metadata is not None assert attn_metadata.extend_metadata.chunk_context_metadata is not None chunked_metadata = attn_metadata.extend_metadata.chunk_context_metadata swa_metadata = chunked_metadata.swa_metadata assert swa_metadata is not None swa_cu_seqlens = swa_metadata.swa_cu_seqlens swa_seq_starts = swa_metadata.swa_seq_starts swa_token_to_batch = swa_metadata.swa_token_to_batch swa_max_seqlens = swa_metadata.swa_max_seqlens swa_total_tokens = swa_metadata.swa_total_tokens key_fetched, value_fetched = ( swa_metadata.swa_workspace[0], swa_metadata.swa_workspace[1], ) cp_mha_gather_cache( key_cache=key_cache, value_cache=value_cache, key=key_fetched, value=value_fetched, block_tables=block_table, k_scales=k_scale, v_scales=v_scale, cu_seqlens_kv=swa_cu_seqlens, token_to_batch=swa_token_to_batch, seq_starts=swa_seq_starts, dequant=self.kv_cache_dtype.startswith("fp8"), kv_cache_layout="NHD", total_tokens=swa_total_tokens, ) rocm_aiter_ops.flash_attn_varlen_func( q=query, k=key_fetched, v=value_fetched, cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=swa_cu_seqlens, max_seqlen_q=max_seqlen_q, max_seqlen_k=swa_max_seqlens, min_seqlen_q=1, dropout_p=0.0, softmax_scale=self.scale, causal=True, window_size=self.sliding_window, alibi_slopes=self.alibi_slopes, return_lse=False, out=output, ) def extend_forward( self, attn_metadata: AiterFlashAttentionMetadata, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, output: torch.Tensor, cu_seqlens_q: torch.Tensor, max_seqlen_q: int, max_seqlen_k: int, min_seqlen_q: int, block_table: torch.Tensor, slot_mapping: torch.Tensor, k_scale: torch.Tensor, v_scale: torch.Tensor, ): if self.sliding_window[0] != -1: self.extend_for_sliding_window( attn_metadata, query, key_cache, value_cache, output, cu_seqlens_q, max_seqlen_q, block_table, k_scale, v_scale, ) return out, lse = rocm_aiter_ops.flash_attn_varlen_func( q=query, k=key, v=value, cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=cu_seqlens_q, max_seqlen_q=max_seqlen_q, max_seqlen_k=max_seqlen_q, min_seqlen_q=min_seqlen_q, dropout_p=0.0, softmax_scale=self.scale, causal=True, window_size=self.sliding_window, alibi_slopes=self.alibi_slopes, return_lse=True, ) assert attn_metadata.extend_metadata is not None chunk_context_metadata = attn_metadata.extend_metadata.chunk_context_metadata num_chunks = chunk_context_metadata.num_chunks workspace = chunk_context_metadata.workspace cu_seqlens_kv = chunk_context_metadata.cu_seq_lens_chunk max_seqlens = chunk_context_metadata.max_seq_lens chunk_starts = chunk_context_metadata.chunk_starts token_to_batch = chunk_context_metadata.token_to_batch total_token_per_batch = chunk_context_metadata.total_token_per_batch key_fetched, value_fetched = workspace[0], workspace[1] chunked_output = None chunked_lse = None for chunk_idx in range(num_chunks): cp_mha_gather_cache( key_cache=key_cache, value_cache=value_cache, key=key_fetched, value=value_fetched, block_tables=block_table, k_scales=k_scale, v_scales=v_scale, cu_seqlens_kv=cu_seqlens_kv[chunk_idx], token_to_batch=token_to_batch[chunk_idx], seq_starts=chunk_starts[chunk_idx], dequant=self.kv_cache_dtype.startswith("fp8"), kv_cache_layout="SHUFFLE" if rocm_aiter_ops.is_shuffle_kv_cache_enabled() else "NHD", total_tokens=total_token_per_batch[chunk_idx], ) suf_out, suf_lse = rocm_aiter_ops.flash_attn_varlen_func( q=query, k=key_fetched, v=value_fetched, cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=cu_seqlens_kv[chunk_idx], max_seqlen_q=max_seqlen_q, max_seqlen_k=max_seqlens[chunk_idx], min_seqlen_q=min_seqlen_q, dropout_p=0.0, softmax_scale=self.scale, causal=False, window_size=self.sliding_window, alibi_slopes=self.alibi_slopes, return_lse=True, ) if chunked_output is None: chunked_output = suf_out chunked_lse = suf_lse else: tmp_output = torch.empty_like(out) tmp_lse = torch.empty_like(lse) merge_attn_states( output=tmp_output, output_lse=tmp_lse, prefix_output=chunked_output, prefix_lse=chunked_lse, suffix_output=suf_out, suffix_lse=suf_lse, ) chunked_output = tmp_output chunked_lse = tmp_lse merge_attn_states( output=output, prefix_output=chunked_output, prefix_lse=chunked_lse, suffix_output=out, suffix_lse=lse, ) def forward( self, layer: torch.nn.Module, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, kv_cache: torch.Tensor, attn_metadata: AiterFlashAttentionMetadata, output: torch.Tensor | None = None, output_scale: torch.Tensor | None = None, output_block_scale: torch.Tensor | None = None, ) -> torch.Tensor: """Forward pass with AiterFlashAttention. Args: query: shape = [num_tokens, num_heads, head_size] key: shape = [num_tokens, num_kv_heads, head_size] value: shape = [num_tokens, num_kv_heads, head_size] kv_cache: shape = [2, num_blocks, block_size, num_kv_heads, head_size] attn_metadata: Metadata for attention. Returns: shape = [num_tokens, num_heads * head_size] NOTE: FP8 quantization, flash-attn expect the size of {q,k,v}_descale to be (num_sequences, num_kv_heads). We use torch's .expand() to avoid duplicating values """ assert output is not None, "Output tensor must be provided." if output_scale is not None or output_block_scale is not None: raise NotImplementedError( "fused output quantization is not yet supported for FlashAttentionImpl" ) if attn_metadata is None: # Profiling run. return output.fill_(0) # IMPORTANT! # NOTE(woosuk): With piece-wise CUDA graphs, this method is # executed in eager-mode PyTorch. Thus, we need to be careful # about any CPU overhead in this method. For example, `view` # and `slice` (or `[:n]`) operations are surprisingly slow even # in the case they do not invoke any GPU ops. # Minimize the PyTorch ops in this method as much as possible. # Whenever making a change in this method, please benchmark the # performance to make sure it does not introduce any overhead. num_actual_tokens = attn_metadata.num_actual_tokens key_cache, value_cache = kv_cache.unbind(0) if self.kv_cache_dtype.startswith("fp8"): key_cache = key_cache.view(current_platform.fp8_dtype()) value_cache = value_cache.view(current_platform.fp8_dtype()) # decode:extend:prefill query = query[:num_actual_tokens] if key is not None: key = key[:num_actual_tokens] if value is not None: value = value[:num_actual_tokens] output_actual_tokens = output[:num_actual_tokens] num_decodes = attn_metadata.num_decodes num_prefills = attn_metadata.num_prefills num_extends = attn_metadata.num_extends num_decode_tokens = attn_metadata.num_decode_tokens num_extend_tokens = attn_metadata.num_extend_tokens if not attn_metadata.use_cascade: # calculate for pure prefills if num_prefills > 0: assert attn_metadata.prefill_metadata is not None prefill_query = query[num_decode_tokens + num_extend_tokens :] prefill_key = key[num_decode_tokens + num_extend_tokens :] prefill_value = value[num_decode_tokens + num_extend_tokens :] rocm_aiter_ops.flash_attn_varlen_func( q=prefill_query, k=prefill_key, v=prefill_value, cu_seqlens_q=attn_metadata.prefill_metadata.query_start_loc, cu_seqlens_k=attn_metadata.prefill_metadata.query_start_loc, max_seqlen_q=attn_metadata.prefill_metadata.max_query_len, max_seqlen_k=attn_metadata.prefill_metadata.max_seq_len, min_seqlen_q=1, dropout_p=0.0, softmax_scale=self.scale, causal=True, window_size=self.sliding_window, alibi_slopes=self.alibi_slopes, out=output_actual_tokens[num_decode_tokens + num_extend_tokens :], ) # calculate for extends if num_extends > 0: assert attn_metadata.extend_metadata is not None extend_tokens_slice = slice( num_decode_tokens, num_decode_tokens + num_extend_tokens ) extend_querys = query[extend_tokens_slice] extend_keys = key[extend_tokens_slice] extend_values = value[extend_tokens_slice] extend_outputs = output[extend_tokens_slice] k_scale = layer._k_scale v_scale = layer._v_scale if rocm_aiter_ops.is_shuffle_kv_cache_enabled(): k_scale = attn_metadata.k_scale v_scale = attn_metadata.v_scale self.extend_forward( attn_metadata=attn_metadata, query=extend_querys, key=extend_keys, value=extend_values, key_cache=key_cache, value_cache=value_cache, output=extend_outputs, cu_seqlens_q=attn_metadata.extend_metadata.query_start_loc, max_seqlen_q=attn_metadata.extend_metadata.max_query_len, max_seqlen_k=attn_metadata.extend_metadata.max_seq_len, min_seqlen_q=1, block_table=attn_metadata.block_table[ num_decodes : num_decodes + num_extends ], slot_mapping=attn_metadata.slot_mapping[ num_decodes : num_decodes + num_extends ], k_scale=k_scale, v_scale=v_scale, ) # calculate for decodes if num_decodes > 0: assert attn_metadata.decode_metadata is not None decode_max_query_len = attn_metadata.decode_metadata.max_query_len # Use unified_attention for speculative decoding (multi-token) # or when sliding window is enabled if self.sliding_window[0] != -1 or decode_max_query_len > 1: assert not rocm_aiter_ops.is_shuffle_kv_cache_enabled(), ( "Shuffle KV cache layout is not supported with sliding " "window or speculative decoding (multi-token decode)." ) from aiter.ops.triton.unified_attention import ( unified_attention, ) descale_shape = ( attn_metadata.query_start_loc[:num_decodes].shape[0] - 1, key_cache.shape[2], ) unified_attention( q=query[:num_decode_tokens], k=key_cache, v=value_cache, out=output[:num_decode_tokens], cu_seqlens_q=attn_metadata.query_start_loc[:num_decodes], max_seqlen_q=decode_max_query_len, seqused_k=attn_metadata.seq_lens[:num_decodes], max_seqlen_k=attn_metadata.max_seq_len, softmax_scale=self.scale, causal=True, alibi_slopes=self.alibi_slopes, window_size=self.sliding_window, block_table=attn_metadata.block_table[:num_decodes], softcap=self.logits_soft_cap, q_descale=None, k_descale=layer._k_scale.expand(descale_shape), v_descale=layer._v_scale.expand(descale_shape), ) return # The ll4mi kernel in paged_attention_v1 requires # HEAD_SIZE >= 16 * NWARPS (= 64 on ROCm with NWARPS=4). # For smaller head sizes or sliding window attention, # fall back to the unified_attention triton kernel which # handles both correctly. _MIN_HEAD_SIZE_FOR_LL4MI = 64 use_unified_attention = self.head_size < _MIN_HEAD_SIZE_FOR_LL4MI if use_unified_attention: assert not rocm_aiter_ops.is_shuffle_kv_cache_enabled(), ( "unified_attention fallback with shuffle layout " "is not supported yet." ) from aiter.ops.triton.unified_attention import ( unified_attention, ) decode_cu_seqlens_q = attn_metadata.query_start_loc[ : num_decodes + 1 ] descale_shape = ( num_decodes, key_cache.shape[2], ) unified_attention( q=query[:num_decode_tokens], k=key_cache, v=value_cache, out=output[:num_decode_tokens], cu_seqlens_q=decode_cu_seqlens_q, max_seqlen_q=1, seqused_k=attn_metadata.seq_lens[:num_decodes], max_seqlen_k=attn_metadata.max_seq_len, softmax_scale=self.scale, causal=True, alibi_slopes=self.alibi_slopes, window_size=self.sliding_window, block_table=attn_metadata.block_table[:num_decodes], softcap=self.logits_soft_cap, q_descale=None, k_descale=layer._k_scale.expand(descale_shape), v_descale=layer._v_scale.expand(descale_shape), ) elif rocm_aiter_ops.is_shuffle_kv_cache_enabled(): num_blocks, block_size, num_kv_heads, head_size = key_cache.shape x = 16 // key_cache.element_size() k_cache_template = torch.empty( [num_blocks, num_kv_heads, head_size // x, block_size, x], dtype=key_cache.dtype, device="meta", ) v_cache_template = torch.empty( [num_blocks, num_kv_heads, block_size // x, head_size, x], dtype=value_cache.dtype, device="meta", ) new_key_cache = key_cache.view_as(k_cache_template) new_value_cache = value_cache.view_as(v_cache_template) rocm_aiter_ops.pa_fwd_asm( Q=query[:num_decode_tokens], K=new_key_cache, V=new_value_cache, block_tables=attn_metadata.block_table[:num_decodes], context_lens=attn_metadata.seq_lens[:num_decodes], block_tables_stride0=attn_metadata.block_table[ :num_decodes ].stride(0), K_QScale=attn_metadata.k_scale, V_QScale=attn_metadata.v_scale, out_=output[:num_decode_tokens], ) else: _, num_heads, head_size = query.shape nbytes_per_qo_elem = torch.finfo(query.dtype).bits // 8 num_seqs = attn_metadata.seq_lens.shape[0] max_num_partitions = ( attn_metadata.max_seq_len + _PARTITION_SIZE_ROCM - 1 ) // _PARTITION_SIZE_ROCM workspace_buffer = torch.empty( (num_seqs * num_heads * max_num_partitions * head_size) * nbytes_per_qo_elem + 2 * (num_seqs * num_heads * max_num_partitions) * 4, dtype=torch.uint8, device=output.device, ) # import so that aiter register the op to the namespace of # torch.ops.aiter import aiter # noqa: F401 torch.ops.aiter.paged_attention_v1( output[:num_decode_tokens], workspace_buffer, query[:num_decode_tokens], key_cache, value_cache, self.scale, attn_metadata.block_table[:num_decodes], attn_metadata.query_start_loc[:num_decodes], attn_metadata.seq_lens[:num_decodes], attn_metadata.max_seq_len, self.alibi_slopes, self.kv_cache_dtype, "NHD", self.logits_soft_cap, layer._k_scale, layer._v_scale, None, _PARTITION_SIZE_ROCM, 1, self.sliding_window[0] + 1, ) else: raise NotImplementedError( "Cascade attention is not implemented for ROCM AITER" ) return output def do_kv_cache_update( self, layer: Attention, key: torch.Tensor, value: torch.Tensor, kv_cache: torch.Tensor, slot_mapping: torch.Tensor, ): key_cache, value_cache = kv_cache.unbind(0) # key and value may be None in the case of cross attention. They are # calculated once based on the output from the encoder and then cached # in KV cache. if self.kv_cache_dtype.startswith("fp8"): key_cache = key_cache.view(current_platform.fp8_dtype()) value_cache = value_cache.view(current_platform.fp8_dtype()) # Reshape the input keys and values and store them in the cache. # Skip this if sharing KV cache with an earlier attention layer. # NOTE(woosuk): Here, key and value are padded while slot_mapping # is not padded. However, we don't need to do # key[:num_actual_tokens] and value[:num_actual_tokens] because # the reshape_and_cache_flash op uses the slot_mapping's shape # to determine the number of actual tokens. if rocm_aiter_ops.is_shuffle_kv_cache_enabled(): # We may calculate per token quant scale in # reshape_and_cache_shuffle_triton which might differ from # vllm's style when shuffle layout is used. k_scale = layer._k_scale v_scale = layer._v_scale assert k_scale is not None and v_scale is not None, ( "k_scale and v_scale are required for shuffled update" ) reshape_and_cache_shuffle_triton( key, value, key_cache, value_cache, slot_mapping, self.kv_cache_dtype, k_scale, v_scale, ) else: torch.ops._C_cache_ops.reshape_and_cache_flash( key, value, key_cache, value_cache, slot_mapping, self.kv_cache_dtype, layer._k_scale, layer._v_scale, )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/attention/backends/rocm_aiter_fa.py", "license": "Apache License 2.0", "lines": 1229, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/v1/worker/tpu_input_batch.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Datastructures defining a TPU input batch from typing import cast import numpy as np import torch from vllm.lora.request import LoRARequest from vllm.sampling_params import SamplingType from vllm.utils import length_from_prompt_token_ids_or_embeds from vllm.utils.collection_utils import swap_dict_values from vllm.v1.outputs import LogprobsTensors from vllm.v1.worker.block_table import MultiGroupBlockTable from vllm.v1.worker.gpu_input_batch import CachedRequestState _SAMPLING_EPS = 1e-5 class InputBatch: def __init__( self, max_num_reqs: int, max_model_len: int, max_num_batched_tokens: int, device: torch.device, pin_memory: bool, vocab_size: int, block_sizes: list[int], # The block_size of each kv cache group kernel_block_sizes: list[int], ): self.max_num_reqs = max_num_reqs self.max_model_len = max_model_len self.max_num_batched_tokens = max_num_batched_tokens self.device = device self.pin_memory = pin_memory self.vocab_size = vocab_size self._req_ids: list[str | None] = [] self.req_id_to_index: dict[str, int] = {} # TODO(woosuk): This buffer could be too large if max_model_len is big. # Find a way to reduce the CPU memory usage. # This buffer is not directly transferred to the GPU, so it does not # need to be pinned. self.token_ids_cpu_tensor = torch.zeros( (max_num_reqs, max_model_len), device="cpu", dtype=torch.int32, pin_memory=False, ) self.token_ids_cpu = self.token_ids_cpu_tensor.numpy() self.num_tokens_no_spec = np.zeros(max_num_reqs, dtype=np.int32) self.num_prompt_tokens = np.zeros(max_num_reqs, dtype=np.int32) self.num_computed_tokens_cpu_tensor = torch.zeros( (max_num_reqs,), device="cpu", dtype=torch.int32, pin_memory=pin_memory, ) self.num_computed_tokens_cpu = self.num_computed_tokens_cpu_tensor.numpy() # Block table. self.block_table = MultiGroupBlockTable( max_num_reqs=max_num_reqs, max_model_len=max_model_len, max_num_batched_tokens=max_num_batched_tokens, pin_memory=pin_memory, device=device, block_sizes=block_sizes, kernel_block_sizes=kernel_block_sizes, ) # Sampling-related. self.temperature = torch.empty( (max_num_reqs,), dtype=torch.float32, device=device ) self.temperature_cpu_tensor = torch.empty( (max_num_reqs,), dtype=torch.float32, device="cpu", pin_memory=pin_memory ) self.temperature_cpu = self.temperature_cpu_tensor.numpy() self.greedy_reqs: set[str] = set() self.random_reqs: set[str] = set() self.top_p = torch.empty((max_num_reqs,), dtype=torch.float32, device=device) self.top_p_cpu_tensor = torch.empty( (max_num_reqs,), dtype=torch.float32, device="cpu", pin_memory=pin_memory ) self.top_p_cpu = self.top_p_cpu_tensor.numpy() self.top_p_reqs: set[str] = set() self.top_k = torch.empty((max_num_reqs,), dtype=torch.int32, device=device) self.top_k_cpu_tensor = torch.empty( (max_num_reqs,), dtype=torch.int32, device="cpu", pin_memory=pin_memory ) self.top_k_cpu = self.top_k_cpu_tensor.numpy() self.top_k_reqs: set[str] = set() self.min_p = torch.empty((max_num_reqs,), dtype=torch.float32, device=device) self.min_p_cpu_tensor = torch.empty( (max_num_reqs,), dtype=torch.float32, device="cpu", pin_memory=pin_memory ) self.min_p_cpu = self.min_p_cpu_tensor.numpy() self.min_p_reqs: set[str] = set() # Frequency penalty related data structures self.frequency_penalties = torch.empty( (max_num_reqs,), dtype=torch.float, device=device ) self.frequency_penalties_cpu_tensor = torch.empty( (max_num_reqs,), dtype=torch.float, device="cpu", pin_memory=pin_memory ) self.frequency_penalties_cpu = self.frequency_penalties_cpu_tensor.numpy() self.frequency_penalties_reqs: set[str] = set() # Presence penalty related data structures self.presence_penalties = torch.empty( (max_num_reqs,), dtype=torch.float, device=device ) self.presence_penalties_cpu_tensor = torch.empty( (max_num_reqs,), dtype=torch.float, device="cpu", pin_memory=pin_memory ) self.presence_penalties_cpu = self.presence_penalties_cpu_tensor.numpy() self.presence_penalties_reqs: set[str] = set() # Repetition penalty related data structures self.repetition_penalties = torch.empty( (max_num_reqs,), dtype=torch.float, device=device ) self.repetition_penalties_cpu_tensor = torch.empty( (max_num_reqs,), dtype=torch.float, device="cpu", pin_memory=pin_memory ) self.repetition_penalties_cpu = self.repetition_penalties_cpu_tensor.numpy() self.repetition_penalties_reqs: set[str] = set() # req_index -> (min_tokens, stop_token_ids) self.min_tokens: dict[int, tuple[int, set[int]]] = {} # lora related self.request_lora_mapping = np.zeros((self.max_num_reqs,), dtype=np.int64) self.lora_id_to_request_ids: dict[int, set[str]] = {} self.lora_id_to_lora_request: dict[int, LoRARequest] = {} # req_index -> generator # NOTE(woosuk): The indices of the requests that do not have their own # generator should not be included in the dictionary. self.generators: dict[int, torch.Generator] = {} self.num_logprobs: dict[str, int] = {} # To accumulate prompt logprobs tensor chunks across prefill steps. self.in_progress_prompt_logprobs_cpu: dict[str, LogprobsTensors] = {} self.logit_bias: list[dict[int, float] | None] = [None] * max_num_reqs self.has_allowed_token_ids: set[str] = set() # NOTE(lufang): In the mask tensor, if the corresponding token allowed, # the value is False. Since we use masked_fill_ to set -inf. self.allowed_token_ids_mask: torch.Tensor | None = None self.allowed_token_ids_mask_cpu_tensor: torch.Tensor | None = None # req_index -> bad_words_token_ids self.bad_words_token_ids: dict[int, list[list[int]]] = {} self.req_output_token_ids: list[list[int] | None] = [] @property def req_ids(self) -> list[str]: # None elements should only be present transiently # while performing state updates to the batch. return cast(list[str], self._req_ids) def add_request( self, request: "CachedRequestState", req_index: int | None = None, ) -> None: if req_index is None: req_index = self.num_reqs assert req_index < self.max_num_reqs req_id = request.req_id if req_index == len(self._req_ids): self._req_ids.append(req_id) self.req_output_token_ids.append(request.output_token_ids) else: self._req_ids[req_index] = req_id self.req_output_token_ids[req_index] = request.output_token_ids self.req_id_to_index[req_id] = req_index # Copy the prompt token ids and output token ids. num_prompt_tokens = length_from_prompt_token_ids_or_embeds( request.prompt_token_ids, request.prompt_embeds ) # TODO: copy prompt_embeds self.num_prompt_tokens[req_index] = num_prompt_tokens self.token_ids_cpu[req_index, :num_prompt_tokens] = request.prompt_token_ids start_idx = num_prompt_tokens end_idx = start_idx + len(request.output_token_ids) self.token_ids_cpu[req_index, start_idx:end_idx] = request.output_token_ids # Number of tokens without spec decode tokens. self.num_tokens_no_spec[req_index] = request.num_tokens self.num_computed_tokens_cpu[req_index] = request.num_computed_tokens self.block_table.add_row(request.block_ids, req_index) sampling_params = request.sampling_params assert sampling_params is not None, "pooling requests not supported yet" if sampling_params.sampling_type == SamplingType.GREEDY: # Should avoid division by zero later when apply_temperature. self.temperature_cpu[req_index] = 0.0 self.greedy_reqs.add(req_id) else: self.temperature_cpu[req_index] = sampling_params.temperature self.random_reqs.add(req_id) self.top_p_cpu[req_index] = sampling_params.top_p if sampling_params.top_p < 1: self.top_p_reqs.add(req_id) top_k = sampling_params.top_k if 0 < top_k < self.vocab_size: self.top_k_reqs.add(req_id) else: top_k = self.vocab_size self.top_k_cpu[req_index] = top_k self.min_p_cpu[req_index] = sampling_params.min_p self.frequency_penalties_cpu[req_index] = sampling_params.frequency_penalty if sampling_params.min_p > _SAMPLING_EPS: self.min_p_reqs.add(req_id) if sampling_params.frequency_penalty != 0.0: self.frequency_penalties_reqs.add(req_id) self.presence_penalties_cpu[req_index] = sampling_params.presence_penalty if sampling_params.presence_penalty != 0.0: self.presence_penalties_reqs.add(req_id) self.repetition_penalties_cpu[req_index] = sampling_params.repetition_penalty if sampling_params.repetition_penalty != 1.0: self.repetition_penalties_reqs.add(req_id) if sampling_params.min_tokens: self.min_tokens[req_index] = ( sampling_params.min_tokens, sampling_params.all_stop_token_ids, ) # NOTE(woosuk): self.generators should not include the requests that # do not have their own generator. if request.generator is not None: self.generators[req_index] = request.generator if sampling_params.logprobs is not None: self.num_logprobs[req_id] = sampling_params.logprobs if sampling_params.logit_bias is not None: self.logit_bias[req_index] = sampling_params.logit_bias if sampling_params.allowed_token_ids: self.has_allowed_token_ids.add(req_id) if self.allowed_token_ids_mask_cpu_tensor is None: # Lazy allocation for this tensor, which can be large. # False means we don't fill with -inf. self.allowed_token_ids_mask = torch.zeros( self.max_num_reqs, self.vocab_size, dtype=torch.bool, device=self.device, ) self.allowed_token_ids_mask_cpu_tensor = torch.zeros( self.max_num_reqs, self.vocab_size, dtype=torch.bool, device="cpu" ) self.allowed_token_ids_mask_cpu_tensor[req_index] = True # False means we don't fill with -inf. self.allowed_token_ids_mask_cpu_tensor[req_index][ sampling_params.allowed_token_ids ] = False if sampling_params.bad_words_token_ids: self.bad_words_token_ids[req_index] = sampling_params.bad_words_token_ids # Add request lora ID if request.lora_request: lora_id = request.lora_request.lora_int_id if lora_id not in self.lora_id_to_request_ids: self.lora_id_to_request_ids[lora_id] = set() self.request_lora_mapping[req_index] = lora_id self.lora_id_to_request_ids[lora_id].add(request.req_id) self.lora_id_to_lora_request[lora_id] = request.lora_request else: # No LoRA self.request_lora_mapping[req_index] = 0 def remove_request(self, req_id: str) -> int | None: """This method must always be followed by a call to condense().""" req_index = self.req_id_to_index.pop(req_id, None) if req_index is None: return None self._req_ids[req_index] = None self.req_output_token_ids[req_index] = None self.greedy_reqs.discard(req_id) self.random_reqs.discard(req_id) self.top_p_reqs.discard(req_id) self.top_k_reqs.discard(req_id) self.min_p_reqs.discard(req_id) self.min_tokens.pop(req_index, None) self.frequency_penalties_reqs.discard(req_id) self.presence_penalties_reqs.discard(req_id) self.repetition_penalties_reqs.discard(req_id) self.generators.pop(req_index, None) self.num_logprobs.pop(req_id, None) self.in_progress_prompt_logprobs_cpu.pop(req_id, None) # LoRA lora_id = self.request_lora_mapping[req_index] if lora_id != 0: self.lora_id_to_request_ids[lora_id].discard(req_id) if len(self.lora_id_to_request_ids[lora_id]) == 0: self.lora_id_to_request_ids.pop(lora_id) self.lora_id_to_lora_request.pop(lora_id) self.request_lora_mapping[req_index] = 0 self.logit_bias[req_index] = None self.has_allowed_token_ids.discard(req_id) if self.allowed_token_ids_mask_cpu_tensor is not None: # False means we don't fill with -inf. self.allowed_token_ids_mask_cpu_tensor[req_index].fill_(False) self.bad_words_token_ids.pop(req_index, None) return req_index def swap_states(self, i1: int, i2: int) -> None: old_id_i1 = self._req_ids[i1] old_id_i2 = self._req_ids[i2] self._req_ids[i1], self._req_ids[i2] = self._req_ids[i2], self._req_ids[i1] # noqa self.req_output_token_ids[i1], self.req_output_token_ids[i2] = ( self.req_output_token_ids[i2], self.req_output_token_ids[i1], ) assert old_id_i1 is not None and old_id_i2 is not None self.req_id_to_index[old_id_i1], self.req_id_to_index[old_id_i2] = ( self.req_id_to_index[old_id_i2], self.req_id_to_index[old_id_i1], ) self.num_tokens_no_spec[i1], self.num_tokens_no_spec[i2] = ( self.num_tokens_no_spec[i2], self.num_tokens_no_spec[i1], ) self.num_prompt_tokens[i1], self.num_prompt_tokens[i2] = ( self.num_prompt_tokens[i2], self.num_prompt_tokens[i1], ) self.num_computed_tokens_cpu[i1], self.num_computed_tokens_cpu[i2] = ( self.num_computed_tokens_cpu[i2], self.num_computed_tokens_cpu[i1], ) self.temperature_cpu[i1], self.temperature_cpu[i2] = ( self.temperature_cpu[i2], self.temperature_cpu[i1], ) self.top_p_cpu[i1], self.top_p_cpu[i2] = self.top_p_cpu[i2], self.top_p_cpu[i1] self.top_k_cpu[i1], self.top_k_cpu[i2] = self.top_k_cpu[i2], self.top_k_cpu[i1] self.frequency_penalties_cpu[i1], self.frequency_penalties_cpu[i2] = ( self.frequency_penalties_cpu[i2], self.frequency_penalties_cpu[i1], ) self.presence_penalties_cpu[i1], self.presence_penalties_cpu[i2] = ( self.presence_penalties_cpu[i2], self.presence_penalties_cpu[i1], ) self.repetition_penalties_cpu[i1], self.repetition_penalties_cpu[i2] = ( self.repetition_penalties_cpu[i2], self.repetition_penalties_cpu[i1], ) self.min_p_cpu[i1], self.min_p_cpu[i2] = self.min_p_cpu[i2], self.min_p_cpu[i1] # NOTE: the following is unsafe # self.token_ids_cpu[i1, ...], self.token_ids_cpu[i2, ...], =\ # self.token_ids_cpu[i2, ...], self.token_ids_cpu[i1, ...] # instead, we need to temporarily copy the data for one of the indices # TODO(lucas): optimize this by only copying valid indices tmp = self.token_ids_cpu[i1, ...].copy() self.token_ids_cpu[i1, ...] = self.token_ids_cpu[i2, ...] self.token_ids_cpu[i2, ...] = tmp swap_dict_values(self.generators, i1, i2) swap_dict_values(self.min_tokens, i1, i2) swap_dict_values(self.bad_words_token_ids, i1, i2) self.request_lora_mapping[i1], self.request_lora_mapping[i2] = ( self.request_lora_mapping[i2], self.request_lora_mapping[i1], ) self.logit_bias[i1], self.logit_bias[i2] = ( self.logit_bias[i2], self.logit_bias[i1], ) if self.allowed_token_ids_mask_cpu_tensor is not None: ( self.allowed_token_ids_mask_cpu_tensor[i1], self.allowed_token_ids_mask_cpu_tensor[i2], ) = ( self.allowed_token_ids_mask_cpu_tensor[i2], self.allowed_token_ids_mask_cpu_tensor[i1], ) self.block_table.swap_row(i1, i2) def condense(self, empty_req_indices: list[int]) -> None: """Move non-empty requests down into lower, empty indices. Args: empty_req_indices: empty batch indices, sorted descending. """ num_reqs = self.num_reqs if num_reqs == 0: # The batched states are empty. self._req_ids.clear() self.req_output_token_ids.clear() return # NOTE(woosuk): This function assumes that the empty_req_indices # is sorted in descending order. last_req_index = num_reqs + len(empty_req_indices) - 1 while empty_req_indices: # Find the largest non-empty index. while last_req_index in empty_req_indices: last_req_index -= 1 # Find the smallest empty index. empty_index = empty_req_indices.pop() if empty_index >= last_req_index: break # Swap the states. req_id = self._req_ids[last_req_index] output_token_ids = self.req_output_token_ids[last_req_index] assert req_id is not None self._req_ids[empty_index] = req_id self._req_ids[last_req_index] = None self.req_output_token_ids[empty_index] = output_token_ids self.req_output_token_ids[last_req_index] = None self.req_id_to_index[req_id] = empty_index num_tokens = self.num_tokens_no_spec[last_req_index] self.token_ids_cpu[empty_index, :num_tokens] = self.token_ids_cpu[ last_req_index, :num_tokens ] self.num_tokens_no_spec[empty_index] = self.num_tokens_no_spec[ last_req_index ] self.num_prompt_tokens[empty_index] = self.num_prompt_tokens[last_req_index] self.num_computed_tokens_cpu[empty_index] = self.num_computed_tokens_cpu[ last_req_index ] self.block_table.move_row(last_req_index, empty_index) self.temperature_cpu[empty_index] = self.temperature_cpu[last_req_index] self.top_p_cpu[empty_index] = self.top_p_cpu[last_req_index] self.top_k_cpu[empty_index] = self.top_k_cpu[last_req_index] self.frequency_penalties_cpu[empty_index] = self.frequency_penalties_cpu[ last_req_index ] self.presence_penalties_cpu[empty_index] = self.presence_penalties_cpu[ last_req_index ] self.repetition_penalties_cpu[empty_index] = self.repetition_penalties_cpu[ last_req_index ] self.min_p_cpu[empty_index] = self.min_p_cpu[last_req_index] generator = self.generators.pop(last_req_index, None) if generator is not None: self.generators[empty_index] = generator min_token = self.min_tokens.pop(last_req_index, None) if min_token is not None: self.min_tokens[empty_index] = min_token self.request_lora_mapping[empty_index] = self.request_lora_mapping[ last_req_index ] self.logit_bias[empty_index] = self.logit_bias[last_req_index] if self.allowed_token_ids_mask_cpu_tensor is not None: self.allowed_token_ids_mask_cpu_tensor[empty_index] = ( self.allowed_token_ids_mask_cpu_tensor[last_req_index] ) bad_words_token_ids = self.bad_words_token_ids.pop(last_req_index, None) if bad_words_token_ids is not None: self.bad_words_token_ids[empty_index] = bad_words_token_ids # Decrement last_req_index since it is now empty. last_req_index -= 1 # Trim lists to the batch size. del self._req_ids[self.num_reqs :] del self.req_output_token_ids[self.num_reqs :] def _make_prompt_token_ids_tensor(self) -> torch.Tensor: max_prompt_len = self.num_prompt_tokens[: self.num_reqs].max() prompt_token_ids_cpu_tensor = torch.empty( (self.num_reqs, max_prompt_len), device="cpu", dtype=torch.int64, pin_memory=self.pin_memory, ) prompt_token_ids = prompt_token_ids_cpu_tensor.numpy() prompt_token_ids[:] = self.token_ids_cpu[: self.num_reqs, :max_prompt_len] # Use the value of vocab_size as a pad since we don't have a # token_id of this value. for i in range(self.num_reqs): prompt_token_ids[i, self.num_prompt_tokens[i] :] = self.vocab_size return prompt_token_ids_cpu_tensor.to(device=self.device, non_blocking=True) def make_lora_inputs( self, num_scheduled_tokens: np.ndarray, num_sampled_tokens: np.ndarray ) -> tuple[tuple[int, ...], tuple[int, ...], set[LoRARequest]]: """ Given the num_scheduled_tokens for each request in the batch, return datastructures used to activate the current LoRAs. Returns: 1. prompt_lora_mapping: A tuple of size self.num_reqs where, prompt_lora_mapping[i] is the LoRA id to use for the ith prompt. 2. token_lora_mapping: A tuple of size np.sum(num_scheduled_tokens) where, token_lora_mapping[i] is the LoRA id to use for ith token. 3. lora_requests: Set of relevant LoRA requests. """ req_lora_mapping = self.request_lora_mapping[: self.num_reqs] prompt_lora_mapping = tuple(req_lora_mapping) token_lora_mapping = tuple(req_lora_mapping.repeat(num_scheduled_tokens)) active_lora_requests: set[LoRARequest] = set( self.lora_id_to_lora_request.values() ) return prompt_lora_mapping, token_lora_mapping, active_lora_requests @property def num_reqs(self) -> int: return len(self.req_id_to_index) @property def all_greedy(self) -> bool: return len(self.random_reqs) == 0 @property def all_random(self) -> bool: return len(self.greedy_reqs) == 0 @property def no_top_p(self) -> bool: return len(self.top_p_reqs) == 0 @property def no_top_k(self) -> bool: return len(self.top_k_reqs) == 0 @property def no_min_p(self) -> bool: return len(self.min_p_reqs) == 0 @property def no_penalties(self) -> bool: return ( len(self.presence_penalties_reqs) == 0 and len(self.frequency_penalties_reqs) == 0 and len(self.repetition_penalties_reqs) == 0 ) @property def max_num_logprobs(self) -> int | None: return max(self.num_logprobs.values()) if self.num_logprobs else None @property def no_allowed_token_ids(self) -> bool: return len(self.has_allowed_token_ids) == 0
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/worker/tpu_input_batch.py", "license": "Apache License 2.0", "lines": 501, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/distributed/kv_transfer/kv_connector/v1/p2p/p2p_nccl_connector.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from dataclasses import dataclass from typing import TYPE_CHECKING, Any import regex as re import torch from vllm.config import VllmConfig from vllm.distributed.kv_transfer.kv_connector.v1.base import ( KVConnectorBase_V1, KVConnectorMetadata, KVConnectorRole, ) from vllm.distributed.kv_transfer.kv_connector.v1.p2p.p2p_nccl_engine import ( P2pNcclEngine, ) from vllm.distributed.parallel_state import get_world_group from vllm.logger import init_logger from vllm.model_executor.layers.attention.mla_attention import MLACommonMetadata from vllm.v1.attention.backend import AttentionMetadata from vllm.v1.core.sched.output import SchedulerOutput if TYPE_CHECKING: from vllm.forward_context import ForwardContext from vllm.v1.core.kv_cache_manager import KVCacheBlocks from vllm.v1.kv_cache_interface import KVCacheConfig from vllm.v1.request import Request logger = init_logger(__name__) @dataclass class ReqMeta: # Request Id request_id: str # Request block ids block_ids: torch.Tensor # Request num tokens num_tokens: int @staticmethod def make_meta( request_id: str, token_ids: list[int], block_ids: list[int], block_size: int ) -> "ReqMeta": block_ids_tensor = torch.tensor(block_ids) return ReqMeta( request_id=request_id, block_ids=block_ids_tensor, num_tokens=len(token_ids), ) @dataclass class P2pNcclConnectorMetadata(KVConnectorMetadata): requests: list[ReqMeta] def __init__(self): self.requests = [] def add_request( self, request_id: str, token_ids: list[int], block_ids: list[int], block_size: int, ) -> None: self.requests.append( ReqMeta.make_meta(request_id, token_ids, block_ids, block_size) ) class P2pNcclConnector(KVConnectorBase_V1): def __init__( self, vllm_config: "VllmConfig", role: KVConnectorRole, kv_cache_config: "KVCacheConfig | None" = None, ): super().__init__( vllm_config=vllm_config, role=role, kv_cache_config=kv_cache_config, ) self._block_size = vllm_config.cache_config.block_size self._requests_need_load: dict[str, Any] = {} self.is_producer = self._kv_transfer_config.is_kv_producer self.chunked_prefill: dict[str, tuple[list[int], list[int] | None]] = {} self._rank = get_world_group().rank if role == KVConnectorRole.WORKER else 0 self._local_rank = ( get_world_group().local_rank if role == KVConnectorRole.WORKER else 0 ) self.p2p_nccl_engine = ( P2pNcclEngine( local_rank=self._local_rank, config=self._kv_transfer_config, hostname="", port_offset=self._rank, ) if role == KVConnectorRole.WORKER else None ) # ============================== # Worker-side methods # ============================== def start_load_kv(self, forward_context: "ForwardContext", **kwargs: Any) -> None: """Start loading the KV cache from the connector buffer to vLLM's paged KV buffer. Args: forward_context (ForwardContext): the forward context. **kwargs: additional arguments for the load operation Note: The number of elements in kv_caches and layer_names should be the same. """ # Only consumer/decode loads KV Cache if self.is_producer: return assert self.p2p_nccl_engine is not None attn_metadata = forward_context.attn_metadata if attn_metadata is None: return def inject_kv_into_layer( layer: torch.Tensor, kv_cache: torch.Tensor, block_ids: torch.Tensor, request_id: str, ) -> None: """ Inject KV cache data into a given attention layer tensor. This function updates `layer` in-place with values from `kv_cache`, handling different backend layouts: - MLA (Multi-Linear Attention) or FlashInfer: KV tensors are indexed along the first dimension. - FlashAttention: KV tensors are indexed along the second dimension. If the number of provided block IDs does not match the number of KV blocks, only the overlapping portion is updated, and a warning is logged. Args: layer (torch.Tensor): The attention layer KV tensor to update. kv_cache (torch.Tensor): The KV cache tensor to inject. block_ids (torch.Tensor): Indices of the blocks to update. request_id (str): Request identifier used for logging. Returns: None. The function modifies `layer` in-place. """ if ( isinstance(attn_metadata, MLACommonMetadata) or layer.shape[1] == 2 ): # MLA or FlashInfer num_block = kv_cache.shape[0] self.check_tensors_except_dim(layer, kv_cache, 0) if len(block_ids) == num_block: layer[block_ids, ...] = kv_cache else: layer[block_ids[:num_block], ...] = kv_cache logger.warning( "🚧kv_cache does not match, block_ids:%d, " "num_block:%d, request_id:%s", len(block_ids), num_block, request_id, ) elif layer.shape[0] == 2: # FlashAttention num_block = kv_cache.shape[1] self.check_tensors_except_dim(layer, kv_cache, 1) if len(block_ids) == num_block: layer[:, block_ids, ...] = kv_cache else: layer[:, block_ids[:num_block], ...] = kv_cache logger.warning( "🚧kv_cache does not match, block_ids:%d, " "num_block:%d, request_id:%s", len(block_ids), num_block, request_id, ) # Get the metadata metadata: KVConnectorMetadata = self._get_connector_metadata() assert isinstance(metadata, P2pNcclConnectorMetadata) if metadata is None: return # Load the KV for each request each layer for request in metadata.requests: request_id = request.request_id ip, port = self.parse_request_id(request_id, False) remote_address = ip + ":" + str(port + self._rank) for layer_name in forward_context.no_compile_layers: layer = forward_context.no_compile_layers[layer_name] # Only process layers that have kv_cache # attribute (attention layers) Skip non-attention # layers like FusedMoE kv_cache = getattr(layer, "kv_cache", None) if kv_cache is None: continue layer = kv_cache[forward_context.virtual_engine] kv_cache = self.p2p_nccl_engine.recv_tensor( request.request_id + "#" + layer_name, remote_address ) if kv_cache is None: logger.warning("🚧kv_cache is None, %s", request.request_id) continue inject_kv_into_layer( layer, kv_cache, request.block_ids, request.request_id ) def wait_for_layer_load(self, layer_name: str) -> None: """Blocking until the KV for a specific layer is loaded into vLLM's paged buffer. This interface will be useful for layer-by-layer pipelining. Args: layer_name: the name of that layer """ return def save_kv_layer( self, layer_name: str, kv_layer: torch.Tensor, attn_metadata: AttentionMetadata, **kwargs: Any, ) -> None: """Start saving the KV cache of the layer from vLLM's paged buffer to the connector. Args: layer_name (str): the name of the layer. kv_layer (torch.Tensor): the paged KV buffer of the current layer in vLLM. attn_metadata (AttentionMetadata): the attention metadata. **kwargs: additional arguments for the save operation. """ # Only producer/prefill saves KV Cache if not self.is_producer: return assert self.p2p_nccl_engine is not None def extract_kv_from_layer( layer: torch.Tensor, block_ids: torch.Tensor, ) -> torch.Tensor: """ Extract KV cache slices from a given attention layer tensor. This function handles multiple backend layouts: - MLA (Multi-Linear Attention) or FlashInfer: KV tensors are indexed along the first dimension. - FlashAttention: KV tensors are indexed along the second dimension. Args: layer (torch.Tensor): The KV cache from the attention layer. block_ids (torch.Tensor): Indices of blocks to extract. Returns: torch.Tensor: A tensor containing the extracted KV slices. Returns None if the layout is unsupported. """ if ( isinstance(attn_metadata, MLACommonMetadata) or layer.shape[1] == 2 ): # MLA or FlashInfer return layer[block_ids, ...] if layer.shape[0] == 2: # FlashAttention return layer[:, block_ids, ...] return None connector_metadata = self._get_connector_metadata() assert isinstance(connector_metadata, P2pNcclConnectorMetadata) for request in connector_metadata.requests: request_id = request.request_id ip, port = self.parse_request_id(request_id, True) remote_address = ip + ":" + str(port + self._rank) kv_cache = extract_kv_from_layer(kv_layer, request.block_ids) self.p2p_nccl_engine.send_tensor( request_id + "#" + layer_name, kv_cache, remote_address ) def wait_for_save(self): if self.is_producer: assert self.p2p_nccl_engine is not None self.p2p_nccl_engine.wait_for_sent() def get_finished( self, finished_req_ids: set[str], **kwargs: Any ) -> tuple[set[str] | None, set[str] | None]: """ Notifies worker-side connector ids of requests that have finished generating tokens. Returns: ids of requests that have finished asynchronous transfer, tuple of (sending/saving ids, recving/loading ids). The finished saves/sends req ids must belong to a set provided in a call to this method (this call or a prior one). """ assert self.p2p_nccl_engine is not None no_compile_layers = self._vllm_config.compilation_config.static_forward_context return self.p2p_nccl_engine.get_finished(finished_req_ids, no_compile_layers) # ============================== # Scheduler-side methods # ============================== def get_num_new_matched_tokens( self, request: "Request", num_computed_tokens: int, ) -> tuple[int, bool]: """ Get number of new tokens that can be loaded from the external KV cache beyond the num_computed_tokens. Args: request (Request): the request object. num_computed_tokens (int): the number of locally computed tokens for this request Returns: the number of tokens that can be loaded from the external KV cache beyond what is already computed. """ if self.is_producer: return 0, False prompt_token_ids = request.prompt_token_ids or [] num_external_tokens = len(prompt_token_ids) - 1 - num_computed_tokens if num_external_tokens < 0: num_external_tokens = 0 return num_external_tokens, False def update_state_after_alloc( self, request: "Request", blocks: "KVCacheBlocks", num_external_tokens: int ): """ Update KVConnector state after block allocation. """ if not self.is_producer and num_external_tokens > 0: self._requests_need_load[request.request_id] = ( request, blocks.get_block_ids()[0], ) def build_connector_meta( self, scheduler_output: SchedulerOutput, ) -> KVConnectorMetadata: """Build the connector metadata for this step. This function should NOT modify any fields in the scheduler_output. Also, calling this function will reset the state of the connector. Args: scheduler_output (SchedulerOutput): the scheduler output object. """ meta = P2pNcclConnectorMetadata() for new_req in scheduler_output.scheduled_new_reqs: if self.is_producer: num_scheduled_tokens = (scheduler_output.num_scheduled_tokens)[ new_req.req_id ] num_tokens = num_scheduled_tokens + new_req.num_computed_tokens # the request's prompt is chunked prefill if num_tokens < len(new_req.prompt_token_ids or []): # 'CachedRequestData' has no attribute 'prompt_token_ids' self.chunked_prefill[new_req.req_id] = ( new_req.block_ids[0], new_req.prompt_token_ids, ) continue # the request's prompt is not chunked prefill meta.add_request( request_id=new_req.req_id, token_ids=new_req.prompt_token_ids or [], block_ids=new_req.block_ids[0], block_size=self._block_size, ) continue if new_req.req_id in self._requests_need_load: meta.add_request( request_id=new_req.req_id, token_ids=new_req.prompt_token_ids or [], block_ids=new_req.block_ids[0], block_size=self._block_size, ) self._requests_need_load.pop(new_req.req_id) cached_reqs = scheduler_output.scheduled_cached_reqs for i, req_id in enumerate(cached_reqs.req_ids): num_computed_tokens = cached_reqs.num_computed_tokens[i] new_block_ids = cached_reqs.new_block_ids[i] resumed_from_preemption = req_id in cached_reqs.resumed_req_ids if self.is_producer: num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id] num_tokens = num_scheduled_tokens + num_computed_tokens assert req_id in self.chunked_prefill assert new_block_ids is not None block_ids = new_block_ids[0] if not resumed_from_preemption: block_ids = self.chunked_prefill[req_id][0] + block_ids prompt_token_ids = self.chunked_prefill[req_id][1] assert prompt_token_ids is not None # the request's prompt is chunked prefill again if num_tokens < len(prompt_token_ids): self.chunked_prefill[req_id] = (block_ids, prompt_token_ids) continue # the request's prompt is all prefilled finally meta.add_request( request_id=req_id, token_ids=prompt_token_ids, block_ids=block_ids, block_size=self._block_size, ) self.chunked_prefill.pop(req_id, None) continue # NOTE(rob): here we rely on the resumed requests being # the first N requests in the list scheduled_cache_reqs. if not resumed_from_preemption: break if req_id in self._requests_need_load: request, _ = self._requests_need_load.pop(req_id) total_tokens = num_computed_tokens + 1 token_ids = request.all_token_ids[:total_tokens] # NOTE(rob): For resumed req, new_block_ids is all # of the block_ids for the request. assert new_block_ids is not None block_ids = new_block_ids[0] meta.add_request( request_id=req_id, token_ids=token_ids, block_ids=block_ids, block_size=self._block_size, ) self._requests_need_load.clear() return meta def request_finished( self, request: "Request", block_ids: list[int], ) -> tuple[bool, dict[str, Any] | None]: """ Called when a request has finished, before its blocks are freed. Returns: True if the request is being saved/sent asynchronously and blocks should not be freed until the request_id is returned from get_finished(). Optional KVTransferParams to be included in the request outputs returned by the engine. """ self.chunked_prefill.pop(request.request_id, None) return False, None # ============================== # Static methods # ============================== @staticmethod def parse_request_id(request_id: str, is_prefill=True) -> tuple[str, int]: # Regular expression to match the string hostname and integer port if is_prefill: pattern = r"___decode_addr_(.*):(\d+)" else: pattern = r"___prefill_addr_(.*):(\d+)___" # Use re.search to find the pattern in the request_id match = re.search(pattern, request_id) if match: # Extract the ranks ip = match.group(1) port = int(match.group(2)) return ip, port raise ValueError(f"Request id {request_id} does not contain hostname and port") @staticmethod def check_tensors_except_dim(tensor1, tensor2, dim): shape1 = tensor1.size() shape2 = tensor2.size() if len(shape1) != len(shape2) or not all( s1 == s2 for i, (s1, s2) in enumerate(zip(shape1, shape2)) if i != dim ): raise NotImplementedError( "Currently, only symmetric TP is supported. Asymmetric TP, PP," "and others will be supported in future PRs." )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/distributed/kv_transfer/kv_connector/v1/p2p/p2p_nccl_connector.py", "license": "Apache License 2.0", "lines": 445, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/distributed/kv_transfer/kv_connector/v1/p2p/p2p_nccl_engine.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import json import logging import os import threading import time from collections import deque from contextlib import contextmanager from dataclasses import dataclass from typing import Any import msgpack import torch import zmq from vllm.config.kv_transfer import KVTransferConfig from vllm.distributed.device_communicators.pynccl_wrapper import ( NCCLLibrary, buffer_type, cudaStream_t, ncclComm_t, ncclDataTypeEnum, ) from vllm.distributed.kv_transfer.kv_connector.v1.p2p.tensor_memory_pool import ( # noqa: E501 TensorMemoryPool, ) from vllm.utils.network_utils import get_ip from vllm.utils.torch_utils import current_stream logger = logging.getLogger(__name__) DEFAULT_MEM_POOL_SIZE_GB = 32 @contextmanager def set_p2p_nccl_context(num_channels: str): original_values: dict[str, Any] = {} env_vars = [ "NCCL_MAX_NCHANNELS", "NCCL_MIN_NCHANNELS", "NCCL_CUMEM_ENABLE", "NCCL_BUFFSIZE", "NCCL_PROTO", # LL,LL128,SIMPLE "NCCL_ALGO", # RING,TREE ] for var in env_vars: original_values[var] = os.environ.get(var) logger.info("set_p2p_nccl_context, original_values: %s", original_values) try: os.environ["NCCL_MAX_NCHANNELS"] = num_channels os.environ["NCCL_MIN_NCHANNELS"] = num_channels os.environ["NCCL_CUMEM_ENABLE"] = "1" yield finally: for var in env_vars: if original_values[var] is not None: os.environ[var] = original_values[var] else: os.environ.pop(var, None) @dataclass class SendQueueItem: tensor_id: str remote_address: str tensor: torch.Tensor class P2pNcclEngine: def __init__( self, local_rank: int, config: KVTransferConfig, hostname: str = "", port_offset: int = 0, library_path: str | None = None, ) -> None: self.config = config self.rank = port_offset self.local_rank = local_rank self.device = torch.device(f"cuda:{self.local_rank}") self.nccl = NCCLLibrary(library_path) if not hostname: hostname = get_ip() port = int(self.config.kv_port) + port_offset if port == 0: raise ValueError("Port cannot be 0") self._hostname = hostname self._port = port # Each card corresponds to a ZMQ address. self.zmq_address = f"{self._hostname}:{self._port}" # If `proxy_ip` or `proxy_port` is `""`, # then the ping thread will not be enabled. proxy_ip = self.config.get_from_extra_config("proxy_ip", "") proxy_port = self.config.get_from_extra_config("proxy_port", "") if proxy_ip == "" or proxy_port == "": self.proxy_address = "" self.http_address = "" else: self.proxy_address = proxy_ip + ":" + proxy_port # the `http_port` must be consistent with the port of OpenAI. http_port = self.config.get_from_extra_config("http_port", None) if http_port is None: example_cfg = { "kv_connector": "P2pNcclConnector", "kv_connector_extra_config": {"http_port": 8000}, } example = ( f"--port=8000 --kv-transfer-config='{json.dumps(example_cfg)}'" ) raise ValueError( "kv_connector_extra_config.http_port is required. " f"Example: {example}" ) self.http_address = f"{self._hostname}:{http_port}" self.context = zmq.Context() self.router_socket = self.context.socket(zmq.ROUTER) self.router_socket.bind(f"tcp://{self.zmq_address}") self.poller = zmq.Poller() self.poller.register(self.router_socket, zmq.POLLIN) self.send_store_cv = threading.Condition() self.send_queue_cv = threading.Condition() self.recv_store_cv = threading.Condition() self.send_stream = torch.cuda.Stream() self.recv_stream = torch.cuda.Stream() mem_pool_size_gb = float( self.config.get_from_extra_config( "mem_pool_size_gb", DEFAULT_MEM_POOL_SIZE_GB ) ) self.pool = TensorMemoryPool( max_block_size=int(mem_pool_size_gb * 1024**3) ) # GB # The sending type includes tree mutually exclusive options: # PUT, GET, PUT_ASYNC. self.send_type = self.config.get_from_extra_config("send_type", "PUT_ASYNC") if self.send_type == "GET": # tensor_id: torch.Tensor self.send_store: dict[str, torch.Tensor] = {} else: # PUT or PUT_ASYNC # tensor_id: torch.Tensor self.send_queue: deque[SendQueueItem] = deque() if self.send_type == "PUT_ASYNC": self._send_thread = threading.Thread( target=self.send_async, daemon=True ) self._send_thread.start() # tensor_id: torch.Tensor/(addr, dtype, shape) self.recv_store: dict[str, Any] = {} self.recv_request_id_to_tensor_ids: dict[str, set[str]] = {} self.send_request_id_to_tensor_ids: dict[str, set[str]] = {} self.socks: dict[str, Any] = {} # remote_address: client socket self.comms: dict[str, Any] = {} # remote_address: (ncclComm_t, rank) self.buffer_size = 0 self.buffer_size_threshold = float(self.config.kv_buffer_size) self.nccl_num_channels = self.config.get_from_extra_config( "nccl_num_channels", "8" ) self._listener_thread = threading.Thread( target=self.listen_for_requests, daemon=True ) self._listener_thread.start() self._ping_thread = None if port_offset == 0 and self.proxy_address != "": self._ping_thread = threading.Thread(target=self.ping, daemon=True) self._ping_thread.start() logger.info( "💯P2pNcclEngine init, rank:%d, local_rank:%d, http_address:%s, " "zmq_address:%s, proxy_address:%s, send_type:%s, buffer_size_" "threshold:%.2f, nccl_num_channels:%s", self.rank, self.local_rank, self.http_address, self.zmq_address, self.proxy_address, self.send_type, self.buffer_size_threshold, self.nccl_num_channels, ) def create_connect(self, remote_address: str | None = None): assert remote_address is not None if remote_address not in self.socks: sock = self.context.socket(zmq.DEALER) sock.setsockopt_string(zmq.IDENTITY, self.zmq_address) sock.connect(f"tcp://{remote_address}") self.socks[remote_address] = sock if remote_address in self.comms: logger.info( "👋comm exists, remote_address:%s, comms:%s", remote_address, self.comms, ) return sock, self.comms[remote_address] unique_id = self.nccl.ncclGetUniqueId() data = {"cmd": "NEW", "unique_id": bytes(unique_id.internal)} sock.send(msgpack.dumps(data)) with torch.cuda.device(self.device): rank = 0 with set_p2p_nccl_context(self.nccl_num_channels): comm: ncclComm_t = self.nccl.ncclCommInitRank(2, unique_id, rank) self.comms[remote_address] = (comm, rank) logger.info( "🤝ncclCommInitRank Success, %s👉%s, MyRank:%s", self.zmq_address, remote_address, rank, ) return self.socks[remote_address], self.comms[remote_address] def send_tensor( self, tensor_id: str, tensor: torch.Tensor, remote_address: str | None = None, ) -> bool: if remote_address is None: with self.recv_store_cv: self.recv_store[tensor_id] = tensor self.recv_store_cv.notify() return True item = SendQueueItem( tensor_id=tensor_id, remote_address=remote_address, tensor=tensor ) if self.send_type == "PUT": return self.send_sync(item) if self.send_type == "PUT_ASYNC": with self.send_queue_cv: self.send_queue.append(item) self.send_queue_cv.notify() return True # GET with self.send_store_cv: tensor_size = tensor.element_size() * tensor.numel() if tensor_size > self.buffer_size_threshold: logger.warning( "❗[GET]tensor_id:%s, tensor_size:%d, is greater than" "buffer size threshold :%d, skip send to %s, rank:%d", tensor_id, tensor_size, self.buffer_size_threshold, remote_address, self.rank, ) return False while self.buffer_size + tensor_size > self.buffer_size_threshold: assert len(self.send_store) > 0 oldest_tensor_id = next(iter(self.send_store)) oldest_tensor = self.send_store.pop(oldest_tensor_id) oldest_tensor_size = ( oldest_tensor.element_size() * oldest_tensor.numel() ) self.buffer_size -= oldest_tensor_size logger.debug( "⛔[GET]Send to %s, tensor_id:%s, tensor_size:%d," " buffer_size:%d, oldest_tensor_size:%d, rank:%d", remote_address, tensor_id, tensor_size, self.buffer_size, oldest_tensor_size, self.rank, ) self.send_store[tensor_id] = tensor self.buffer_size += tensor_size logger.debug( "🔵[GET]Send to %s, tensor_id:%s, tensor_size:%d, " "shape:%s, rank:%d, buffer_size:%d(%.2f%%)", remote_address, tensor_id, tensor_size, tensor.shape, self.rank, self.buffer_size, self.buffer_size / self.buffer_size_threshold * 100, ) return True def recv_tensor( self, tensor_id: str, remote_address: str | None = None, ) -> torch.Tensor: if self.send_type == "PUT" or self.send_type == "PUT_ASYNC": start_time = time.time() with self.recv_store_cv: while tensor_id not in self.recv_store: self.recv_store_cv.wait() tensor = self.recv_store[tensor_id] if tensor is not None: if isinstance(tensor, tuple): addr, dtype, shape = tensor tensor = self.pool.load_tensor(addr, dtype, shape, self.device) else: self.buffer_size -= tensor.element_size() * tensor.numel() else: duration = time.time() - start_time logger.warning( "🔴[PUT]Recv From %s, tensor_id:%s, duration:%.3fms, rank:%d", remote_address, tensor_id, duration * 1000, self.rank, ) return tensor # GET if remote_address is None: return None if remote_address not in self.socks: self.create_connect(remote_address) sock = self.socks[remote_address] comm, rank = self.comms[remote_address] data = {"cmd": "GET", "tensor_id": tensor_id} sock.send(msgpack.dumps(data)) message = sock.recv() data = msgpack.loads(message) if data["ret"] != 0: logger.warning( "🔴[GET]Recv From %s, tensor_id: %s, ret: %d", remote_address, tensor_id, data["ret"], ) return None with torch.cuda.stream(self.recv_stream): tensor = torch.empty( data["shape"], dtype=getattr(torch, data["dtype"]), device=self.device ) self.recv(comm, tensor, rank ^ 1, self.recv_stream) return tensor def listen_for_requests(self): while True: socks = dict(self.poller.poll()) if self.router_socket not in socks: continue remote_address, message = self.router_socket.recv_multipart() data = msgpack.loads(message) if data["cmd"] == "NEW": unique_id = self.nccl.unique_id_from_bytes(bytes(data["unique_id"])) with torch.cuda.device(self.device): rank = 1 with set_p2p_nccl_context(self.nccl_num_channels): comm: ncclComm_t = self.nccl.ncclCommInitRank( 2, unique_id, rank ) self.comms[remote_address.decode()] = (comm, rank) logger.info( "🤝ncclCommInitRank Success, %s👈%s, MyRank:%s", self.zmq_address, remote_address.decode(), rank, ) elif data["cmd"] == "PUT": tensor_id = data["tensor_id"] try: with torch.cuda.stream(self.recv_stream): tensor = torch.empty( data["shape"], dtype=getattr(torch, data["dtype"]), device=self.device, ) self.router_socket.send_multipart([remote_address, b"0"]) comm, rank = self.comms[remote_address.decode()] self.recv(comm, tensor, rank ^ 1, self.recv_stream) tensor_size = tensor.element_size() * tensor.numel() if self.buffer_size + tensor_size > self.buffer_size_threshold: # Store Tensor in memory pool addr = self.pool.store_tensor(tensor) tensor = (addr, tensor.dtype, tensor.shape) logger.warning( "🔴[PUT]Recv Tensor, Out Of Threshold, " "%s👈%s, data:%s, addr:%d", self.zmq_address, remote_address.decode(), data, addr, ) else: self.buffer_size += tensor_size except torch.cuda.OutOfMemoryError: self.router_socket.send_multipart([remote_address, b"1"]) tensor = None logger.warning( "🔴[PUT]Recv Tensor, Out Of Memory, %s👈%s, data:%s", self.zmq_address, remote_address.decode(), data, ) with self.recv_store_cv: self.recv_store[tensor_id] = tensor self.have_received_tensor_id(tensor_id) self.recv_store_cv.notify() elif data["cmd"] == "GET": tensor_id = data["tensor_id"] with self.send_store_cv: tensor = self.send_store.pop(tensor_id, None) if tensor is not None: data = { "ret": 0, "shape": tensor.shape, "dtype": str(tensor.dtype).replace("torch.", ""), } # LRU self.send_store[tensor_id] = tensor self.have_sent_tensor_id(tensor_id) else: data = {"ret": 1} self.router_socket.send_multipart([remote_address, msgpack.dumps(data)]) if data["ret"] == 0: comm, rank = self.comms[remote_address.decode()] self.send(comm, tensor.to(self.device), rank ^ 1, self.send_stream) else: logger.warning( "🚧Unexpected, Received message from %s, data:%s", remote_address, data, ) def have_sent_tensor_id(self, tensor_id: str): request_id = tensor_id.split("#")[0] if request_id not in self.send_request_id_to_tensor_ids: self.send_request_id_to_tensor_ids[request_id] = set() self.send_request_id_to_tensor_ids[request_id].add(tensor_id) def have_received_tensor_id(self, tensor_id: str): request_id = tensor_id.split("#")[0] if request_id not in self.recv_request_id_to_tensor_ids: self.recv_request_id_to_tensor_ids[request_id] = set() self.recv_request_id_to_tensor_ids[request_id].add(tensor_id) def send_async(self): while True: with self.send_queue_cv: while not self.send_queue: self.send_queue_cv.wait() item = self.send_queue.popleft() if not self.send_queue: self.send_queue_cv.notify() self.send_sync(item) def wait_for_sent(self): if self.send_type == "PUT_ASYNC": start_time = time.time() with self.send_queue_cv: while self.send_queue: self.send_queue_cv.wait() duration = time.time() - start_time logger.debug( "🚧[PUT_ASYNC]It took %.3fms to wait for the send_queue" " to be empty, rank:%d", duration * 1000, self.rank, ) def send_sync(self, item: SendQueueItem) -> bool: if item.remote_address is None: return False if item.remote_address not in self.socks: self.create_connect(item.remote_address) tensor = item.tensor sock = self.socks[item.remote_address] comm, rank = self.comms[item.remote_address] data = { "cmd": "PUT", "tensor_id": item.tensor_id, "shape": tensor.shape, "dtype": str(tensor.dtype).replace("torch.", ""), } sock.send(msgpack.dumps(data)) response = sock.recv() if response != b"0": logger.error( "🔴Send Tensor, Peer Out Of Memory/Threshold, %s 👉 %s, " "MyRank:%s, data:%s, tensor:%s, size:%fGB, response:%s", self.zmq_address, item.remote_address, rank, data, tensor.shape, tensor.element_size() * tensor.numel() / 1024**3, response.decode(), ) return False self.send(comm, tensor.to(self.device), rank ^ 1, self.send_stream) if self.send_type == "PUT_ASYNC": self.have_sent_tensor_id(item.tensor_id) return True def get_finished( self, finished_req_ids: set[str], no_compile_layers ) -> tuple[set[str] | None, set[str] | None]: """ Notifies worker-side connector ids of requests that have finished generating tokens. Returns: ids of requests that have finished asynchronous transfer, tuple of (sending/saving ids, recving/loading ids). The finished saves/sends req ids must belong to a set provided in a call to this method (this call or a prior one). """ # Clear the buffer upon request completion. for request_id in finished_req_ids: for layer_name in no_compile_layers: tensor_id = request_id + "#" + layer_name if tensor_id in self.recv_store: with self.recv_store_cv: tensor = self.recv_store.pop(tensor_id, None) self.send_request_id_to_tensor_ids.pop(request_id, None) self.recv_request_id_to_tensor_ids.pop(request_id, None) if isinstance(tensor, tuple): addr, _, _ = tensor self.pool.free(addr) # TODO:Retrieve requests that have already sent the KV cache. finished_sending: set[str] = set() # TODO:Retrieve requests that have already received the KV cache. finished_recving: set[str] = set() return finished_sending or None, finished_recving or None def ping(self): sock = self.context.socket(zmq.DEALER) sock.setsockopt_string(zmq.IDENTITY, self.zmq_address) logger.debug("ping start, zmq_address:%s", self.zmq_address) sock.connect(f"tcp://{self.proxy_address}") data = { "type": "P" if self.config.is_kv_producer else "D", "http_address": self.http_address, "zmq_address": self.zmq_address, } while True: sock.send(msgpack.dumps(data)) time.sleep(3) def send(self, comm, tensor: torch.Tensor, dst: int, stream=None): assert tensor.device == self.device, ( f"this nccl communicator is created to work on {self.device}, " f"but the input tensor is on {tensor.device}" ) if stream is None: stream = current_stream() with torch.cuda.stream(stream): self.nccl.ncclSend( buffer_type(tensor.data_ptr()), tensor.numel(), ncclDataTypeEnum.from_torch(tensor.dtype), dst, comm, cudaStream_t(stream.cuda_stream), ) stream.synchronize() def recv(self, comm, tensor: torch.Tensor, src: int, stream=None): assert tensor.device == self.device, ( f"this nccl communicator is created to work on {self.device}, " f"but the input tensor is on {tensor.device}" ) if stream is None: stream = current_stream() with torch.cuda.stream(stream): self.nccl.ncclRecv( buffer_type(tensor.data_ptr()), tensor.numel(), ncclDataTypeEnum.from_torch(tensor.dtype), src, comm, cudaStream_t(stream.cuda_stream), ) stream.synchronize() def close(self) -> None: self._listener_thread.join() if self.send_type == "PUT_ASYNC": self._send_thread.join() if self._ping_thread is not None: self._ping_thread.join()
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/distributed/kv_transfer/kv_connector/v1/p2p/p2p_nccl_engine.py", "license": "Apache License 2.0", "lines": 553, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/distributed/kv_transfer/kv_connector/v1/p2p/tensor_memory_pool.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import atexit import ctypes import math from dataclasses import dataclass import torch from vllm.logger import init_logger logger = init_logger(__name__) @dataclass class MemoryBlock: size: int addr: int """A memory pool for managing pinned host memory allocations for tensors. This class implements a buddy allocation system to efficiently manage pinned host memory for tensor storage. It supports allocation, deallocation, and tensor storage/retrieval operations. Key Features: - Uses power-of-two block sizes for efficient buddy allocation - Supports splitting and merging of memory blocks - Provides methods to store CUDA tensors in pinned host memory - Allows loading tensors from pinned memory back to device - Automatically cleans up memory on destruction Attributes: max_block_size (int): Maximum block size (rounded to nearest power of two) min_block_size (int): Minimum block size (rounded to nearest power of two) free_lists (dict): Dictionary of free memory blocks by size allocated_blocks (dict): Dictionary of currently allocated blocks base_tensor (torch.Tensor): Base pinned memory tensor base_address (int): Base memory address of the pinned memory region Example: >>> pool = TensorMemoryPool(max_block_size=1024*1024) >>> tensor = torch.randn(100, device='cuda') >>> addr = pool.store_tensor(tensor) >>> loaded_tensor = pool.load_tensor(addr, tensor.dtype, ... tensor.shape, 'cuda') >>> pool.free(addr) """ class TensorMemoryPool: """Initializes the memory pool with given size constraints. Args: max_block_size (int): Maximum size of memory blocks to manage min_block_size (int, optional): Minimum size of memory blocks to manage. Defaults to 512. Raises: ValueError: If block sizes are invalid or max_block_size is less than min_block_size """ def __init__(self, max_block_size: int, min_block_size: int = 512): if max_block_size <= 0 or min_block_size <= 0: raise ValueError("Block sizes must be positive") if max_block_size < min_block_size: raise ValueError("Max block size must be greater than min block size") self.max_block_size = self._round_to_power_of_two(max_block_size) self.min_block_size = self._round_to_power_of_two(min_block_size) self.free_lists: dict[int, dict[int, MemoryBlock]] = {} self.allocated_blocks: dict[int, MemoryBlock] = {} self._initialize_free_lists() self._allocate_pinned_memory() atexit.register(self.cleanup) def _round_to_power_of_two(self, size: int) -> int: return 1 << (size - 1).bit_length() def _initialize_free_lists(self): size = self.max_block_size while size >= self.min_block_size: self.free_lists[size] = {} size //= 2 def _allocate_pinned_memory(self): self.base_tensor = torch.empty( self.max_block_size // 4, dtype=torch.float32, pin_memory=True ) self.base_address = self.base_tensor.data_ptr() initial_block = MemoryBlock(size=self.max_block_size, addr=self.base_address) self.free_lists[self.max_block_size][initial_block.addr] = initial_block logger.debug( "TensorMemoryPool, base_address:%d, max_block_size:%d", self.base_address, self.max_block_size, ) def allocate(self, size: int) -> int: """Allocates a memory block of at least the requested size. Args: size (int): Minimum size of memory to allocate Returns: int: Address of the allocated memory block Raises: ValueError: If size is invalid or insufficient memory is available """ if size <= 0: raise ValueError("Allocation size must be positive") required_size = self._round_to_power_of_two(max(size, self.min_block_size)) if required_size > self.max_block_size: raise ValueError("Requested size exceeds maximum block size") current_size = required_size while current_size <= self.max_block_size: if self.free_lists[current_size]: _, block = self.free_lists[current_size].popitem() self._split_block(block, required_size) self.allocated_blocks[block.addr] = block return block.addr current_size *= 2 raise ValueError("Insufficient memory") def _split_block(self, block: MemoryBlock, required_size: int): while block.size > required_size and block.size // 2 >= self.min_block_size: buddy_size = block.size // 2 buddy_addr = block.addr + buddy_size buddy = MemoryBlock(size=buddy_size, addr=buddy_addr) block.size = buddy_size self.free_lists[buddy_size][buddy.addr] = buddy def free(self, addr: int): """Frees an allocated memory block. Args: addr (int): Address of the block to free Raises: ValueError: If address is invalid or not allocated """ if addr not in self.allocated_blocks: raise ValueError("Invalid address to free") block = self.allocated_blocks.pop(addr) self._merge_buddies(block) def _merge_buddies(self, block: MemoryBlock): MAX_MERGE_DEPTH = 30 depth = 0 while depth < MAX_MERGE_DEPTH: buddy_offset = ( block.size if (block.addr - self.base_address) % (2 * block.size) == 0 else -block.size ) buddy_addr = block.addr + buddy_offset buddy = self.free_lists[block.size].get(buddy_addr) if buddy: del self.free_lists[buddy.size][buddy.addr] merged_addr = min(block.addr, buddy.addr) merged_size = block.size * 2 block = MemoryBlock(size=merged_size, addr=merged_addr) depth += 1 else: break self.free_lists[block.size][block.addr] = block def store_tensor(self, tensor: torch.Tensor) -> int: """Stores a CUDA tensor in pinned host memory. Args: tensor (torch.Tensor): CUDA tensor to store Returns: int: Address where the tensor is stored Raises: ValueError: If tensor is not on CUDA or allocation fails """ if not tensor.is_cuda: raise ValueError("Only CUDA tensors can be stored") size = tensor.element_size() * tensor.numel() addr = self.allocate(size) block = self.allocated_blocks[addr] if block.size < size: self.free(addr) raise ValueError( f"Allocated block size {block.size} is smaller than " f"required size {size}" ) try: buffer = (ctypes.c_byte * block.size).from_address(block.addr) cpu_tensor = torch.frombuffer( buffer, dtype=tensor.dtype, count=tensor.numel() ).reshape(tensor.shape) except ValueError as err: self.free(addr) raise ValueError(f"Failed to create tensor view: {err}") from err cpu_tensor.copy_(tensor) return addr def load_tensor( self, addr: int, dtype: torch.dtype, shape: tuple[int, ...], device: torch.device, ) -> torch.Tensor: """Loads a tensor from pinned host memory to the specified device. Args: addr (int): Address where tensor is stored dtype (torch.dtype): Data type of the tensor shape (tuple[int, ...]): Shape of the tensor device: Target device for the loaded tensor Returns: torch.Tensor: The loaded tensor on the specified device Raises: ValueError: If address is invalid or sizes don't match """ if addr not in self.allocated_blocks: raise ValueError("Invalid address to load") block = self.allocated_blocks[addr] num_elements = math.prod(shape) dtype_size = torch.tensor([], dtype=dtype).element_size() required_size = num_elements * dtype_size if required_size > block.size: raise ValueError("Requested tensor size exceeds block size") buffer = (ctypes.c_byte * block.size).from_address(block.addr) cpu_tensor = torch.frombuffer(buffer, dtype=dtype, count=num_elements).reshape( shape ) cuda_tensor = torch.empty(shape, dtype=dtype, device=device) cuda_tensor.copy_(cpu_tensor) return cuda_tensor def cleanup(self): """Cleans up all memory resources and resets the pool state.""" self.free_lists.clear() self.allocated_blocks.clear() if hasattr(self, "base_tensor"): del self.base_tensor def __del__(self): self.cleanup()
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/distributed/kv_transfer/kv_connector/v1/p2p/tensor_memory_pool.py", "license": "Apache License 2.0", "lines": 211, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/v1/test_request.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from vllm.v1.request import RequestStatus def test_request_status_fmt_str(): """Test that the string representation of RequestStatus is correct.""" assert f"{RequestStatus.WAITING}" == "WAITING" assert f"{RequestStatus.WAITING_FOR_FSM}" == "WAITING_FOR_FSM" assert f"{RequestStatus.WAITING_FOR_REMOTE_KVS}" == "WAITING_FOR_REMOTE_KVS" assert f"{RequestStatus.WAITING_FOR_STREAMING_REQ}" == "WAITING_FOR_STREAMING_REQ" assert f"{RequestStatus.RUNNING}" == "RUNNING" assert f"{RequestStatus.PREEMPTED}" == "PREEMPTED" assert f"{RequestStatus.FINISHED_STOPPED}" == "FINISHED_STOPPED" assert f"{RequestStatus.FINISHED_LENGTH_CAPPED}" == "FINISHED_LENGTH_CAPPED" assert f"{RequestStatus.FINISHED_ABORTED}" == "FINISHED_ABORTED" assert f"{RequestStatus.FINISHED_IGNORED}" == "FINISHED_IGNORED"
{ "repo_id": "vllm-project/vllm", "file_path": "tests/v1/test_request.py", "license": "Apache License 2.0", "lines": 15, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:benchmarks/kernels/benchmark_moe_align_block_size.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import argparse import itertools import torch from vllm.model_executor.layers.fused_moe.moe_align_block_size import ( moe_align_block_size, ) from vllm.triton_utils import triton def get_topk_ids(num_tokens: int, num_experts: int, topk: int) -> torch.Tensor: return torch.stack( [ torch.randperm(num_experts, dtype=torch.int32, device="cuda")[:topk] for _ in range(num_tokens) ] ) # test configurations num_tokens_range = [1, 16, 256, 4096] num_experts_range = [16, 64, 224, 256, 280, 512] topk_range = [1, 2, 8] ep_size_range = [1, 8] configs = list( itertools.product(num_tokens_range, num_experts_range, topk_range, ep_size_range) ) @triton.testing.perf_report( triton.testing.Benchmark( x_names=["num_tokens", "num_experts", "topk", "ep_size"], x_vals=configs, line_arg="provider", line_vals=["vllm"], line_names=["vLLM"], plot_name="moe-align-block-size-performance", args={}, ) ) def benchmark(num_tokens, num_experts, topk, ep_size, provider): """Benchmark function for Triton.""" block_size = 256 torch.cuda.manual_seed_all(0) topk_ids = get_topk_ids(num_tokens, num_experts, topk) e_map = None if ep_size != 1: local_e = num_experts // ep_size e_ids = torch.randperm(num_experts, device="cuda", dtype=torch.int32)[:local_e] e_map = torch.full((num_experts,), -1, device="cuda", dtype=torch.int32) e_map[e_ids] = torch.arange(local_e, device="cuda", dtype=torch.int32) quantiles = [0.5, 0.2, 0.8] if provider == "vllm": ms, min_ms, max_ms = triton.testing.do_bench( lambda: moe_align_block_size( topk_ids, block_size, num_experts, e_map, ignore_invalid_experts=True ), quantiles=quantiles, ) return 1000 * ms, 1000 * max_ms, 1000 * min_ms if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--num_experts", type=int, default=64, choices=[8, 16, 32, 64, 128, 256], ) parser.add_argument( "--topk", type=int, default=8, choices=[2, 4, 8], help="Top-k value for correctness check.", ) args = parser.parse_args() benchmark.run(print_data=True, show_plots=True)
{ "repo_id": "vllm-project/vllm", "file_path": "benchmarks/kernels/benchmark_moe_align_block_size.py", "license": "Apache License 2.0", "lines": 72, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/kernels/moe/test_moe_align_block_size.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Tests for the MOE align block size function. Run `pytest tests/kernels/moe/test_moe_align_block_size.py`. """ import pytest import torch from vllm.model_executor.layers.fused_moe.moe_align_block_size import ( batched_moe_align_block_size, moe_align_block_size, ) from vllm.utils.math_utils import cdiv, round_up from vllm.utils.torch_utils import set_random_seed NUM_TOKENS = [1, 3, 256, 2256, 4096] NUM_EXPERTS = [32, 160, 256, 257] TOP_KS = [1, 2, 16, 32] BLOCK_SIZES = [32, 128] set_random_seed(0) def _group_tokens_by_expert( sorted_ids: torch.Tensor, expert_ids: torch.Tensor, block_size: int, valid_length: int, total_tokens: int, ) -> dict: num_blocks = valid_length // block_size expert_tokens: dict[int, list[int]] = {} for block_idx in range(num_blocks): expert_id = expert_ids[block_idx].item() block_start = block_idx * block_size block_end = min(block_start + block_size, valid_length) block_tokens = sorted_ids[block_start:block_end] valid_tokens = block_tokens[block_tokens < total_tokens] if expert_id not in expert_tokens: expert_tokens[expert_id] = [] expert_tokens[expert_id].extend(valid_tokens.tolist()) return expert_tokens def _verify_expert_level_sorting( actual_sorted_ids: torch.Tensor, golden_sorted_ids: torch.Tensor, expert_ids: torch.Tensor, block_size: int, valid_length: int, total_tokens: int, ): """ Verify that actual_sorted_ids follows the correct expert-level sorting. The kerne limplementation may or may not preserve original token order in topk_ids in the final sorted_ids however this does not impact quality. """ # Group tokens by expert from the golden implementation golden_expert_tokens = _group_tokens_by_expert( golden_sorted_ids, expert_ids, block_size, valid_length, total_tokens ) actual_expert_tokens = _group_tokens_by_expert( actual_sorted_ids, expert_ids, block_size, valid_length, total_tokens ) assert set(golden_expert_tokens.keys()) == set(actual_expert_tokens.keys()), ( f"Expert IDs mismatch: golden={set(golden_expert_tokens.keys())}, " f"actual={set(actual_expert_tokens.keys())}" ) for expert_id in golden_expert_tokens: golden_tokens = torch.tensor( golden_expert_tokens[expert_id], device=actual_sorted_ids.device ) actual_tokens = torch.tensor( actual_expert_tokens[expert_id], device=actual_sorted_ids.device ) assert torch.equal( torch.sort(golden_tokens)[0], torch.sort(actual_tokens)[0] ), ( f"Expert {expert_id} token mismatch: " f"golden={golden_expert_tokens[expert_id]}, " f"actual={actual_expert_tokens[expert_id]}" ) def torch_moe_align_block_size( topk_ids: torch.Tensor, block_size: int, num_experts: int, expert_map: torch.Tensor | None = None, pad_sorted_ids: bool = False, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Golden torch implementation of moe_align_block_size. This function aligns the token distribution across experts to be compatible with block size for matrix multiplication by sorting tokens by expert and padding to block boundaries. """ max_num_tokens_padded = topk_ids.numel() + num_experts * (block_size - 1) if pad_sorted_ids: max_num_tokens_padded = round_up(max_num_tokens_padded, block_size) if topk_ids.numel() < num_experts: max_num_tokens_padded = topk_ids.numel() * block_size flattened_token_indices = torch.arange( topk_ids.numel(), device=topk_ids.device, dtype=torch.int32 ) flattened_expert_ids = topk_ids.flatten() sorted_expert_ids, sort_indices = torch.sort(flattened_expert_ids, stable=True) sorted_token_indices = flattened_token_indices[sort_indices] expert_token_counts = torch.zeros( num_experts, dtype=torch.int64, device=topk_ids.device ) for expert_id in range(num_experts): mask = sorted_expert_ids == expert_id expert_token_counts[expert_id] = mask.sum() expert_padded_counts = torch.zeros( num_experts, dtype=torch.int64, device=topk_ids.device ) for expert_id in range(num_experts): original_count = expert_token_counts[expert_id] if expert_map is not None and expert_map[expert_id] == -1: continue if original_count > 0: expert_padded_counts[expert_id] = ( (original_count + block_size - 1) // block_size ) * block_size sorted_token_ids = torch.full( (max_num_tokens_padded,), topk_ids.numel(), dtype=torch.int32, device=topk_ids.device, ) max_num_blocks = (max_num_tokens_padded + block_size - 1) // block_size expert_ids = torch.full( (max_num_blocks,), -1, dtype=torch.int32, device=topk_ids.device ) current_pos = 0 current_block = 0 for expert_id in range(num_experts): if expert_map is not None and expert_map[expert_id] == -1: continue expert_mask = sorted_expert_ids == expert_id expert_tokens = sorted_token_indices[expert_mask] num_expert_tokens = expert_tokens.shape[0] if num_expert_tokens > 0: sorted_token_ids[current_pos : current_pos + num_expert_tokens] = ( expert_tokens ) expert_blocks_needed = expert_padded_counts[expert_id] // block_size expert_id_new = expert_id if expert_map is not None: expert_id_new = expert_map[expert_id] expert_ids[current_block : current_block + expert_blocks_needed] = ( expert_id_new ) current_pos += expert_padded_counts[expert_id] current_block += expert_blocks_needed total_padded_tokens = expert_padded_counts.sum() num_tokens_post_pad = torch.tensor( [total_padded_tokens], dtype=torch.int32, device=topk_ids.device ) return sorted_token_ids, expert_ids, num_tokens_post_pad @pytest.mark.parametrize("m", NUM_TOKENS) @pytest.mark.parametrize("topk", TOP_KS) @pytest.mark.parametrize("num_experts", NUM_EXPERTS) @pytest.mark.parametrize("block_size", BLOCK_SIZES) @pytest.mark.parametrize("pad_sorted_ids", [False, True]) def test_moe_align_block_size( m: int, topk: int, num_experts: int, block_size: int, pad_sorted_ids: bool ): """Test moe_align_block_size without expert mapping""" topk_ids = torch.zeros((m, topk), device="cuda", dtype=torch.int32) for i in range(m): experts = torch.randperm(num_experts, device="cuda")[:topk] topk_ids[i] = experts actual_sorted_ids, actual_expert_ids, actual_num_tokens = moe_align_block_size( topk_ids=topk_ids, block_size=block_size, num_experts=num_experts, pad_sorted_ids=pad_sorted_ids, ) golden_sorted_ids, golden_expert_ids, golden_num_tokens = ( torch_moe_align_block_size( topk_ids=topk_ids, block_size=block_size, num_experts=num_experts, pad_sorted_ids=pad_sorted_ids, ) ) torch.testing.assert_close(actual_num_tokens, golden_num_tokens, atol=0, rtol=0) torch.testing.assert_close(actual_expert_ids, golden_expert_ids, atol=0, rtol=0) # For sorted_token_ids, verify block-level correctness rather than exact # order Tokens within each expert's blocks can be in any order, but expert # regions must be correct _verify_expert_level_sorting( actual_sorted_ids, golden_sorted_ids, actual_expert_ids, block_size, actual_num_tokens.item(), m * topk, ) total_tokens = m * topk assert actual_num_tokens.item() % block_size == 0, ( "num_tokens_post_pad should be divisible by block_size" ) assert actual_num_tokens.item() >= total_tokens, ( "num_tokens_post_pad should be at least total_tokens" ) valid_tokens = actual_sorted_ids[actual_sorted_ids < total_tokens] assert len(valid_tokens) == total_tokens, ( f"Should have exactly {total_tokens} valid tokens, got {len(valid_tokens)}" ) actual_num_blocks = cdiv(int(actual_num_tokens.item()), block_size) assert (actual_expert_ids[:actual_num_blocks] >= 0).all() and ( actual_expert_ids[:actual_num_blocks] < num_experts ).all(), "expert_ids should contain valid expert indices" @pytest.mark.parametrize("m", [16, 32, 2048]) @pytest.mark.parametrize("topk", [2, 4]) @pytest.mark.parametrize("num_experts", [8, 64]) @pytest.mark.parametrize("block_size", [64]) def test_moe_align_block_size_with_expert_map( m: int, topk: int, num_experts: int, block_size: int ): """Test moe_align_block_size with expert mapping (EP scenario)""" topk_ids = torch.zeros((m, topk), device="cuda", dtype=torch.int32) for i in range(m): experts = torch.randperm(num_experts, device="cuda")[:topk] topk_ids[i] = experts expert_map = torch.full((num_experts,), -1, device="cuda", dtype=torch.int32) local_experts = list(range(0, num_experts, 2)) for i, expert_id in enumerate(local_experts): expert_map[expert_id] = i actual_sorted_ids, actual_expert_ids, actual_num_tokens = moe_align_block_size( topk_ids=topk_ids, block_size=block_size, num_experts=num_experts, expert_map=expert_map, ignore_invalid_experts=True, ) golden_sorted_ids, golden_expert_ids, golden_num_tokens = ( torch_moe_align_block_size( topk_ids=topk_ids, block_size=block_size, num_experts=num_experts, expert_map=expert_map, ) ) torch.testing.assert_close(actual_num_tokens, golden_num_tokens, atol=0, rtol=0) torch.testing.assert_close(actual_expert_ids, golden_expert_ids, atol=0, rtol=0) _verify_expert_level_sorting( actual_sorted_ids, golden_sorted_ids, actual_expert_ids, block_size, actual_num_tokens.item(), m * topk, ) def test_moe_align_block_size_deterministic(): m, topk, num_experts, block_size = 128, 2, 32, 64 torch.manual_seed(42) topk_ids = torch.randint( 0, num_experts, (m, topk), device="cuda", dtype=torch.int32 ) # expect the results to be reproducible results = [] for _ in range(5): sorted_ids, expert_ids, num_tokens = moe_align_block_size( topk_ids=topk_ids, block_size=block_size, num_experts=num_experts ) results.append((sorted_ids.clone(), expert_ids.clone(), num_tokens.clone())) for i in range(1, len(results)): assert torch.equal(results[0][0], results[i][0]), ( "sorted_ids should be deterministic" ) assert torch.equal(results[0][1], results[i][1]), ( "expert_ids should be deterministic" ) assert torch.equal(results[0][2], results[i][2]), ( "num_tokens should be deterministic" ) @pytest.mark.parametrize("max_tokens_per_batch", [13, 16, 512]) @pytest.mark.parametrize("num_experts", [8, 16, 32, 64]) @pytest.mark.parametrize("block_size", [8, 16, 32, 64]) @pytest.mark.parametrize("simulate_empty_batches", [False, True]) def test_batched_moe_align_block_size( max_tokens_per_batch: int, num_experts: int, block_size: int, simulate_empty_batches: bool, ): def ref_outputs( expert_num_tokens: torch.Tensor, ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: E = expert_num_tokens.size(0) # Round up so each batch can be split to blocks evenly. Msum = round_up(max_tokens_per_batch, block_size) * E ref_sorted_ids = torch.empty((Msum,), dtype=torch.int32) ref_expert_ids = torch.empty((Msum // block_size,), dtype=torch.int32) ref_num_tokens_post_pad = torch.empty((1,), dtype=torch.int32) # Initialize sentinel = E * max_tokens_per_batch ref_sorted_ids.fill_(sentinel) ref_expert_ids.fill_(-1) # Fill ref_sorted_ids i = 0 for expert_id, expert_nt in enumerate(expert_num_tokens): token_offset = expert_id * max_tokens_per_batch for j in range(expert_nt): ref_sorted_ids[i] = token_offset + j i += 1 # round up i to the next block_size i = round_up(i, block_size) ref_num_tokens_post_pad[0] = i # Fill expert_ids nt_ceil_sum = 0 for expert_id, expert_nt in enumerate(expert_num_tokens): expert_ids_offset = nt_ceil_sum // block_size ceil_expert_nt = round_up(int(expert_nt.item()), block_size) num_blocks = ceil_expert_nt // block_size for x in range(num_blocks): ref_expert_ids[expert_ids_offset + x] = expert_id nt_ceil_sum += ceil_expert_nt return ( ref_sorted_ids.to("cuda"), ref_expert_ids.to("cuda"), ref_num_tokens_post_pad.to("cuda"), ) # Compute expert_num_tokens expert_num_tokens = torch.randint( low=0, high=max_tokens_per_batch, size=(num_experts,), device="cpu", dtype=torch.int32, ) if simulate_empty_batches: # mark half the batches to have 0 tokens zero_batches = torch.randperm(num_experts)[: num_experts // 2] expert_num_tokens[zero_batches] = 0 # ref outputs ref_sorted_ids, ref_expert_ids, ref_num_tokens_post_pad = ref_outputs( expert_num_tokens ) # outputs sorted_ids, expert_ids, num_tokens_post_pad = batched_moe_align_block_size( max_tokens_per_batch, block_size, expert_num_tokens.to("cuda") ) assert ref_sorted_ids.size() == sorted_ids.size(), ( f"{ref_sorted_ids.size()} vs {sorted_ids.size()}" ) assert ref_expert_ids.size() == expert_ids.size(), ( f"{ref_expert_ids.size()} vs {expert_ids.size()}" ) assert ref_num_tokens_post_pad.size() == num_tokens_post_pad.size(), ( f"{ref_num_tokens_post_pad.size()} vs {num_tokens_post_pad.size()}" ) torch.testing.assert_close(ref_sorted_ids, sorted_ids, atol=0, rtol=0) torch.testing.assert_close(ref_expert_ids, expert_ids, atol=0, rtol=0) torch.testing.assert_close( ref_num_tokens_post_pad, num_tokens_post_pad, atol=0, rtol=0 )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/test_moe_align_block_size.py", "license": "Apache License 2.0", "lines": 352, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/models/multimodal/test_mapping.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Iterable import pytest import torch import transformers from transformers import AutoConfig, PreTrainedModel from vllm.config import ModelConfig from vllm.model_executor.models.utils import WeightsMapper from vllm.multimodal import MULTIMODAL_REGISTRY from vllm.transformers_utils.config import try_get_safetensors_metadata from ..registry import _MULTIMODAL_EXAMPLE_MODELS, HF_EXAMPLE_MODELS def create_repo_dummy_weights(repo: str) -> Iterable[tuple[str, torch.Tensor]]: """Create weights from safetensors checkpoint metadata""" metadata = try_get_safetensors_metadata(repo) weight_names = list(metadata.weight_map.keys()) with torch.device("meta"): return ((name, torch.empty(0)) for name in weight_names) def create_dummy_model(repo: str, model_arch: str) -> PreTrainedModel: """ Create weights from a dummy meta deserialized hf model with name conversion """ model_cls: PreTrainedModel = getattr(transformers, model_arch) config = AutoConfig.from_pretrained(repo) with torch.device("meta"): model = model_cls._from_config(config) # TODO(hmellor): Remove this once Transformers has fixed tied weights on meta device # https://github.com/huggingface/transformers/issues/43522 if getattr(config.get_text_config(), "tie_word_embeddings", False) or getattr( config, "tie_word_embeddings", False ): model.tie_weights() return model def model_architectures_for_test() -> list[str]: arch_to_test = list[str]() for model_arch, info in _MULTIMODAL_EXAMPLE_MODELS.items(): if not info.trust_remote_code and hasattr(transformers, model_arch): model_cls: PreTrainedModel = getattr(transformers, model_arch) if getattr(model_cls, "_checkpoint_conversion_mapping", None): arch_to_test.append(model_arch) return arch_to_test @pytest.mark.core_model @pytest.mark.parametrize("model_arch", model_architectures_for_test()) def test_hf_model_weights_mapper(model_arch: str): model_info = HF_EXAMPLE_MODELS.get_hf_info(model_arch) model_info.check_available_online(on_fail="skip") model_info.check_transformers_version(on_fail="skip") is_mistral_model = model_arch in [ "Mistral3ForConditionalGeneration", "PixtralForConditionalGeneration", "VoxtralForConditionalGeneration", ] if not is_mistral_model or model_info.tokenizer_mode == "mistral": tokenizer_mode = model_info.tokenizer_mode else: tokenizer_mode = "hf" model_id = model_info.default model_config = ModelConfig( model_id, tokenizer=model_info.tokenizer or model_id, tokenizer_mode=tokenizer_mode, config_format="hf", revision=model_info.revision, trust_remote_code=model_info.trust_remote_code, hf_overrides=model_info.hf_overrides, skip_tokenizer_init=model_info.require_embed_inputs, enable_prompt_embeds=model_info.require_embed_inputs, enable_mm_embeds=model_info.require_embed_inputs, enforce_eager=model_info.enforce_eager, dtype=model_info.dtype, ) model_cls = MULTIMODAL_REGISTRY._get_model_cls(model_config) original_weights = create_repo_dummy_weights(model_id) hf_dummy_model = create_dummy_model(model_id, model_arch) hf_converted_weights = hf_dummy_model.named_parameters() hf_converted_buffers = hf_dummy_model.named_buffers() mapper: WeightsMapper = model_cls.hf_to_vllm_mapper mapped_original_weights = mapper.apply(original_weights) mapped_hf_converted_weights = mapper.apply(hf_converted_weights) mapped_hf_converted_buffers = mapper.apply(hf_converted_buffers) ref_weight_names = set(map(lambda x: x[0], mapped_original_weights)) weight_names = set(map(lambda x: x[0], mapped_hf_converted_weights)) buffer_names = set(map(lambda x: x[0], mapped_hf_converted_buffers)) # Some checkpoints may have buffers, we ignore them for this test ref_weight_names -= buffer_names weights_missing = ref_weight_names - weight_names weights_unmapped = weight_names - ref_weight_names assert not weights_missing and not weights_unmapped, ( f"Following weights are not mapped correctly: {weights_unmapped}, " f"Missing expected weights: {weights_missing}." )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/models/multimodal/test_mapping.py", "license": "Apache License 2.0", "lines": 91, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:examples/online_serving/structured_outputs/structured_outputs.py
# ruff: noqa: E501 # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import argparse import asyncio import enum import os from typing import Any, Literal import openai import pydantic from openai.types.chat import ChatCompletionChunk ConstraintsFormat = Literal[ "choice", "regex", "json", "grammar", "structural_tag", ] async def print_stream_response( stream_response: openai.AsyncStream[ChatCompletionChunk], title: str, args: argparse.Namespace, ): print(f"\n\n{title} (Streaming):") local_reasoning_header_printed = False local_content_header_printed = False async for chunk in stream_response: delta = chunk.choices[0].delta reasoning_chunk_text: str | None = getattr(delta, "reasoning", None) content_chunk_text = delta.content if args.reasoning: if reasoning_chunk_text: if not local_reasoning_header_printed: print(" Reasoning: ", end="") local_reasoning_header_printed = True print(reasoning_chunk_text, end="", flush=True) if content_chunk_text: if not local_content_header_printed: if local_reasoning_header_printed: print() print(" Content: ", end="") local_content_header_printed = True print(content_chunk_text, end="", flush=True) else: if content_chunk_text: if not local_content_header_printed: print(" Content: ", end="") local_content_header_printed = True print(content_chunk_text, end="", flush=True) print() class CarType(str, enum.Enum): SEDAN = "SEDAN" SUV = "SUV" TRUCK = "TRUCK" COUPE = "COUPE" class CarDescription(pydantic.BaseModel): brand: str model: str car_type: CarType PARAMS: dict[ConstraintsFormat, dict[str, Any]] = { "choice": { "messages": [ { "role": "user", "content": "Classify this sentiment: vLLM is wonderful!", } ], "extra_body": {"structured_outputs": {"choice": ["positive", "negative"]}}, }, "regex": { "messages": [ { "role": "user", "content": "Generate an email address for Alan Turing, who works in Enigma. End in .com and new line. Example result: 'alan.turing@enigma.com\n'", } ], "extra_body": { "structured_outputs": {"regex": r"[a-z0-9.]{1,20}@\w{6,10}\.com\n"}, }, }, "json": { "messages": [ { "role": "user", "content": "Generate a JSON with the brand, model and car_type of the most iconic car from the 90's", } ], "response_format": { "type": "json_schema", "json_schema": { "name": "car-description", "schema": CarDescription.model_json_schema(), }, }, }, "grammar": { "messages": [ { "role": "user", "content": "Generate an SQL query to show the 'username' and 'email' from the 'users' table.", } ], "extra_body": { "structured_outputs": { "grammar": """ root ::= select_statement select_statement ::= "SELECT " column " from " table " where " condition column ::= "col_1 " | "col_2 " table ::= "table_1 " | "table_2 " condition ::= column "= " number number ::= "1 " | "2 " """, } }, }, "structural_tag": { "messages": [ { "role": "user", "content": """ You have access to the following function to retrieve the weather in a city: { "name": "get_weather", "parameters": { "city": { "param_type": "string", "description": "The city to get the weather for", "required": True } } } If a you choose to call a function ONLY reply in the following format: <{start_tag}={function_name}>{parameters}{end_tag} where start_tag => `<function` parameters => a JSON dict with the function argument name as key and function argument value as value. end_tag => `</function>` Here is an example, <function=example_function_name>{"example_name": "example_value"}</function> Reminder: - Function calls MUST follow the specified format - Required parameters MUST be specified - Only call one function at a time - Put the entire function call reply on one line - Always add your sources when using search results to answer the user query You are a helpful assistant. Given the previous instructions, what is the weather in New York City, Boston, and San Francisco?""", }, ], "response_format": { "type": "structural_tag", "structures": [ { "begin": "<function=get_weather>", "schema": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, "end": "</function>", } ], "triggers": ["<function="], }, }, } async def cli(): parser = argparse.ArgumentParser( description="Run OpenAI Chat Completion with various structured outputs capabilities", ) _ = parser.add_argument( "--constraint", type=str, nargs="+", choices=[*list(PARAMS), "*"], default=["*"], help="Specify which constraint(s) to run.", ) _ = parser.add_argument( "--stream", action=argparse.BooleanOptionalAction, default=False, help="Enable streaming output", ) _ = parser.add_argument( "--reasoning", action=argparse.BooleanOptionalAction, default=False, help="Enable printing of reasoning traces if available.", ) args = parser.parse_args() base_url = os.getenv("OPENAI_BASE_URL", "http://localhost:8000/v1") client = openai.AsyncOpenAI(base_url=base_url, api_key="EMPTY") constraints = list(PARAMS) if "*" in args.constraint else list(set(args.constraint)) model = (await client.models.list()).data[0].id if args.stream: results = await asyncio.gather( *[ client.chat.completions.create( model=model, max_tokens=1024, stream=True, **PARAMS[name], ) for name in constraints ] ) for constraint, stream in zip(constraints, results): await print_stream_response(stream, constraint, args) else: results = await asyncio.gather( *[ client.chat.completions.create( model=model, max_tokens=1024, stream=False, **PARAMS[name], ) for name in constraints ] ) for constraint, response in zip(constraints, results): print(f"\n\n{constraint}:") message = response.choices[0].message if args.reasoning and hasattr(message, "reasoning"): print(f" Reasoning: {message.reasoning or ''}") print(f" Content: {message.content!r}") def main(): asyncio.run(cli()) if __name__ == "__main__": main()
{ "repo_id": "vllm-project/vllm", "file_path": "examples/online_serving/structured_outputs/structured_outputs.py", "license": "Apache License 2.0", "lines": 233, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:examples/offline_inference/spec_decode.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from transformers import AutoTokenizer from vllm import LLM, SamplingParams from vllm.benchmarks.datasets import add_dataset_parser, get_samples from vllm.utils.argparse_utils import FlexibleArgumentParser from vllm.v1.metrics.reader import Counter, Vector QUESTION = "What is the content of each image?" IMAGE_URLS = [ "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/duck.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/lion.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/flycatcher.jpeg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/somefish.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/starfish.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/snail.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/thistle.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/husky.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/orangetabbycat.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/guineapig.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/rabbit.jpg", "https://vllm-public-assets.s3.us-west-2.amazonaws.com/multimodal_asset/horsepony.jpg", ] def get_custom_mm_prompts(num_prompts): prompts = [] for url in IMAGE_URLS: prompts.append( [ {"type": "image_url", "image_url": {"url": url}}, {"type": "text", "text": QUESTION}, ] ) if num_prompts > len(IMAGE_URLS): prompts = prompts * (num_prompts // len(IMAGE_URLS) + 1) return [[{"role": "user", "content": prompt}] for prompt in prompts[:num_prompts]] def parse_args(): parser = FlexibleArgumentParser() add_dataset_parser(parser) parser.add_argument("--test", action="store_true") parser.add_argument( "--method", type=str, default="eagle", choices=["ngram", "eagle", "eagle3", "mtp", "draft_model"], ) parser.add_argument("--backend", type=str, default="openai") parser.add_argument("--num-spec-tokens", type=int, default=2) parser.add_argument("--prompt-lookup-max", type=int, default=5) parser.add_argument("--prompt-lookup-min", type=int, default=2) parser.add_argument("--tp", type=int, default=1) parser.add_argument("--enforce-eager", action="store_true") parser.add_argument("--enable-chunked-prefill", action="store_true") parser.add_argument("--max-model-len", type=int, default=16384) parser.add_argument("--temp", type=float, default=0) parser.add_argument("--top-p", type=float, default=1.0) parser.add_argument("--top-k", type=int, default=-1) parser.add_argument("--print-output", action="store_true") parser.add_argument("--output-len", type=int, default=256) parser.add_argument("--model-dir", type=str, default=None) parser.add_argument("--eagle-dir", type=str, default=None) parser.add_argument("--draft-model", type=str, default=None) parser.add_argument("--custom-mm-prompts", action="store_true") parser.add_argument("--gpu-memory-utilization", type=float, default=0.9) parser.add_argument("--disable-padded-drafter-batch", action="store_true") parser.add_argument("--max-num-seqs", type=int, default=None) parser.add_argument("--parallel-drafting", action="store_true") parser.add_argument("--allowed-local-media-path", type=str, default="") return parser.parse_args() def main(args): model_dir = args.model_dir if args.model_dir is None: if args.custom_mm_prompts: raise ValueError( "custom_mm_prompts requires mm based models" "default llama3.1-8b-instruct is not mm based" "please specify model_dir to give a mm based model" ) model_dir = "meta-llama/Llama-3.1-8B-Instruct" tokenizer = AutoTokenizer.from_pretrained(model_dir) if args.custom_mm_prompts: prompts = llm_prompts = get_custom_mm_prompts(args.num_prompts) else: prompts = get_samples(args, tokenizer) if args.enable_multimodal_chat: llm_prompts = [p.prompt for p in prompts] else: # add_special_tokens is False to avoid adding bos twice # when using chat templates llm_prompts = [ { "prompt_token_ids": tokenizer.encode( prompt.prompt, add_special_tokens=False ), "multi_modal_data": prompt.multi_modal_data, } for prompt in prompts ] if args.method == "eagle" or args.method == "eagle3": eagle_dir = args.eagle_dir if args.method == "eagle" and eagle_dir is None: eagle_dir = "yuhuili/EAGLE-LLaMA3.1-Instruct-8B" elif args.method == "eagle3" and eagle_dir is None: eagle_dir = "yuhuili/EAGLE3-LLaMA3.1-Instruct-8B" speculative_config = { "method": args.method, "model": eagle_dir, "num_speculative_tokens": args.num_spec_tokens, "disable_padded_drafter_batch": args.disable_padded_drafter_batch, "parallel_drafting": args.parallel_drafting, } elif args.method == "ngram": speculative_config = { "method": "ngram", "num_speculative_tokens": args.num_spec_tokens, "prompt_lookup_max": args.prompt_lookup_max, "prompt_lookup_min": args.prompt_lookup_min, } elif args.method == "draft_model": assert args.draft_model is not None and args.draft_model != "" speculative_config = { "method": args.method, "model": args.draft_model, "num_speculative_tokens": args.num_spec_tokens, "enforce_eager": args.enforce_eager, "max_model_len": args.max_model_len, "parallel_drafting": args.parallel_drafting, } elif args.method == "mtp": speculative_config = { "method": "mtp", "num_speculative_tokens": args.num_spec_tokens, } else: raise ValueError(f"unknown method: {args.method}") llm = LLM( model=model_dir, trust_remote_code=True, tensor_parallel_size=args.tp, enable_chunked_prefill=args.enable_chunked_prefill, enforce_eager=args.enforce_eager, gpu_memory_utilization=args.gpu_memory_utilization, speculative_config=speculative_config, disable_log_stats=False, max_model_len=args.max_model_len, limit_mm_per_prompt={"image": 5}, disable_chunked_mm_input=True, max_num_seqs=args.max_num_seqs, allowed_local_media_path=args.allowed_local_media_path, ) sampling_params = SamplingParams(temperature=args.temp, max_tokens=args.output_len) if args.backend == "openai-chat": outputs = llm.chat(llm_prompts, sampling_params=sampling_params) else: outputs = llm.generate( llm_prompts, sampling_params=sampling_params, ) # print the generated text if args.print_output: for i, output in enumerate(outputs): print("-" * 50) if not args.custom_mm_prompts: print(f"prompt: {prompts[i].prompt}") else: print(f"prompt: {prompts[i]}") print(f"generated text: {output.outputs[0].text}") print("-" * 50) metrics = llm.get_metrics() total_num_output_tokens = sum( len(output.outputs[0].token_ids) for output in outputs ) num_drafts = 0 num_draft_tokens = 0 num_accepted_tokens = 0 acceptance_counts = [0] * args.num_spec_tokens for metric in metrics: if metric.name == "vllm:spec_decode_num_drafts": assert isinstance(metric, Counter) num_drafts += metric.value elif metric.name == "vllm:spec_decode_num_draft_tokens": assert isinstance(metric, Counter) num_draft_tokens += metric.value elif metric.name == "vllm:spec_decode_num_accepted_tokens": assert isinstance(metric, Counter) num_accepted_tokens += metric.value elif metric.name == "vllm:spec_decode_num_accepted_tokens_per_pos": assert isinstance(metric, Vector) for pos in range(len(metric.values)): acceptance_counts[pos] += metric.values[pos] print("-" * 50) print(f"total_num_output_tokens: {total_num_output_tokens}") print(f"num_drafts: {num_drafts}") print(f"num_draft_tokens: {num_draft_tokens}") print(f"num_accepted_tokens: {num_accepted_tokens}") acceptance_length = 1 + (num_accepted_tokens / num_drafts) if num_drafts > 0 else 1 print(f"mean acceptance length: {acceptance_length:.2f}") print("-" * 50) # print acceptance at each token position for i in range(len(acceptance_counts)): acceptance_rate = acceptance_counts[i] / num_drafts if num_drafts > 0 else 0 print(f"acceptance at token {i}: {acceptance_rate:.2f}") return acceptance_length if __name__ == "__main__": args = parse_args() args.enable_multimodal_chat = args.backend == "openai-chat" acceptance_length = main(args) if args.test: # takes ~30s to run on 1xH100 assert args.method in ["eagle", "eagle3"] assert args.tp == 1 assert args.num_spec_tokens == 3 assert args.dataset_name == "hf" assert args.dataset_path == "philschmid/mt-bench" assert args.num_prompts == 80 assert args.temp == 0 assert args.top_p == 1.0 assert args.top_k == -1 assert args.enable_chunked_prefill # check acceptance length is within 2% of expected value rtol = 0.02 expected_acceptance_length = 2.296 if args.method == "eagle" else 2.811 assert ( acceptance_length <= (1 + rtol) * expected_acceptance_length and acceptance_length >= (1 - rtol) * expected_acceptance_length ), ( f"acceptance_length {acceptance_length} is not " f"within {rtol * 100}% of {expected_acceptance_length}" ) print( f"Test passed! Expected AL: " f"{expected_acceptance_length}, got {acceptance_length}" )
{ "repo_id": "vllm-project/vllm", "file_path": "examples/offline_inference/spec_decode.py", "license": "Apache License 2.0", "lines": 231, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/v1/engine/test_fast_incdec_prefix_err.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from transformers import AutoTokenizer from vllm.sampling_params import SamplingParams from vllm.v1.engine import EngineCoreRequest from vllm.v1.engine.detokenizer import IncrementalDetokenizer # ruff: noqa: E501 def test_fast_inc_detok_invalid_utf8_err_case(): """ Test edge case where tokenizer can produce non-monotonic, invalid UTF-8 output, which breaks the internal state of tokenizers' DecodeStream. See https://github.com/vllm-project/vllm/issues/17448. Thanks to reproducer from @fpaupier: https://gist.github.com/fpaupier/0ed1375bd7633c5be6c894b1c7ac1be3. """ tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-1b-it") # Create a test request prompt_token_ids = [107, 4606, 236787, 107] params = SamplingParams(skip_special_tokens=True) request = EngineCoreRequest( request_id="test", external_req_id="test-ext", prompt_token_ids=prompt_token_ids, mm_features=None, sampling_params=params, pooling_params=None, arrival_time=0.0, lora_request=None, cache_salt=None, data_parallel_rank=None, ) detokenizer = IncrementalDetokenizer.from_new_request(tokenizer, request) assert detokenizer.__class__.__name__ == "FastIncrementalDetokenizer", ( "Should use FastIncrementalDetokenizer by default" ) # Process tokens incrementally test_tokens = [ 236840, 107, 138, 236782, 107, 140, 236775, 6265, 1083, 623, 121908, 147418, 827, 107, 140, 236775, 6265, 236779, 2084, 1083, 623, 203292, 827, 107, 140, 236775, 6265, 236779, 7777, 1083, 623, 121908, 147418, 569, 537, 236789, 65880, 569, 537, 236789, 62580, 853, 115693, 210118, 35178, 16055, 1270, 759, 215817, 4758, 1925, 1117, 827, 107, 140, 236775, 5654, 1083, 623, 110733, 46291, 827, 107, 140, 236775, 5654, 236779, 2084, 1083, 623, 136955, 56731, 827, 107, 140, 236775, 5654, 236779, 7777, 1083, 623, 194776, 2947, 496, 109811, 1608, 890, 215817, 4758, 1925, 1117, 2789, 432, 398, 602, 31118, 569, 124866, 134772, 509, 19478, 1640, 33779, 236743, 236770, 236819, 236825, 236771, 432, 398, 432, 237167, 827, 107, 140, 236775, 77984, 1083, 623, 2709, 236745, 2555, 513, 236789, 602, 31118, 569, ] output = "" for i, token_id in enumerate(test_tokens): detokenizer.update([token_id], False) finished = i == len(test_tokens) - 1 output += detokenizer.get_next_output_text(finished, delta=True) assert ( output == r"""[ { "source": "Résultats", "source_type": "CONCEPT", "source_description": "Résultats de l'analyse de l'impact des opérations israéliennes sur la frontière libanaise", "target": "Israël", "target_type": "ORGANIZATION", "target_description": "Pays qui a obtenu à sa frontière libanaise « un niveau de calme inédit depuis les années 1960 »", "relationship": "Obtention d'un niveau de""" )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/v1/engine/test_fast_incdec_prefix_err.py", "license": "Apache License 2.0", "lines": 183, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a4_nvfp4.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import torch from torch.nn.parameter import Parameter from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( CompressedTensorsScheme, ) from vllm.model_executor.layers.quantization.utils.nvfp4_utils import ( apply_nvfp4_linear, convert_to_nvfp4_linear_kernel_format, select_nvfp4_linear_backend, ) from vllm.model_executor.parameter import ( GroupQuantScaleParameter, ModelWeightParameter, PerTensorScaleParameter, ) __all__ = ["CompressedTensorsW4A4Fp4"] class CompressedTensorsW4A4Fp4(CompressedTensorsScheme): def __init__(self): self.backend = select_nvfp4_linear_backend() self.group_size = 16 @classmethod def get_min_capability(cls) -> int: return 75 def create_weights( self, layer: torch.nn.Module, output_partition_sizes: list[int], input_size_per_partition: int, params_dtype: torch.dtype, weight_loader: Callable, **kwargs, ): output_size_per_partition = sum(output_partition_sizes) layer.logical_widths = output_partition_sizes layer.input_size_per_partition = input_size_per_partition layer.output_size_per_partition = output_size_per_partition # Weight weight = ModelWeightParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition // 2, dtype=torch.uint8, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight_packed", weight) # Global Weight Scale weight_global_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("weight_global_scale", weight_global_scale) # Per Group Weight Scale weight_scale = GroupQuantScaleParameter( data=torch.empty( sum(output_partition_sizes), input_size_per_partition // self.group_size, dtype=torch.float8_e4m3fn, ), input_dim=1, output_dim=0, weight_loader=weight_loader, ) layer.register_parameter("weight_scale", weight_scale) input_global_scale = PerTensorScaleParameter( data=torch.empty(len(output_partition_sizes), dtype=torch.float32), weight_loader=weight_loader, ) layer.register_parameter("input_global_scale", input_global_scale) def process_weights_after_loading(self, layer: torch.nn.Module) -> None: # Rename CT checkpoint names to standardized names layer.weight = layer.weight_packed del layer.weight_packed # Process global scales (CT stores as divisors, i.e. 1/scale) input_global_scale_inv = layer.input_global_scale.max().to(torch.float32) layer.input_global_scale = Parameter( (1.0 / input_global_scale_inv).to(torch.float32), requires_grad=False ) weight_global_scale = layer.weight_global_scale.max().to(torch.float32) layer.weight_global_scale = Parameter( 1.0 / weight_global_scale, requires_grad=False ) # Pre-compute alpha and inverse for runtime quantization layer.input_global_scale_inv = Parameter( input_global_scale_inv, requires_grad=False ) layer.alpha = Parameter( layer.input_global_scale * layer.weight_global_scale, requires_grad=False ) # Convert layer to NVFP4 linear kernel format convert_to_nvfp4_linear_kernel_format(self.backend, layer) def apply_weights( self, layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, ) -> torch.Tensor: return apply_nvfp4_linear( backend=self.backend, layer=layer, x=x, bias=bias, )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a4_nvfp4.py", "license": "Apache License 2.0", "lines": 108, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/compile/test_config.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import copy from contextlib import nullcontext from unittest.mock import MagicMock, patch import pytest from pydantic import ValidationError from vllm.compilation.counter import compilation_counter from vllm.compilation.passes.utility.fix_functionalization import ( FixFunctionalizationPass, ) from vllm.config import ( CompilationConfig, CUDAGraphMode, ParallelConfig, SchedulerConfig, VllmConfig, ) from vllm.config.compilation import CompilationMode, PassConfig from vllm.engine.arg_utils import EngineArgs from vllm.platforms import current_platform from vllm.utils.torch_utils import ( _is_torch_equal_or_newer, is_torch_equal, ) from vllm.v1.cudagraph_dispatcher import CudagraphDispatcher # This import automatically registers `torch.ops.silly.attention` from . import silly_attention # noqa: F401 def test_version(): # Test the version comparison logic using the private function assert _is_torch_equal_or_newer("2.8.0.dev20250624+cu128", "2.8.0.dev") assert _is_torch_equal_or_newer("2.8.0a0+gitc82a174", "2.8.0.dev") assert _is_torch_equal_or_newer("2.8.0", "2.8.0.dev") assert _is_torch_equal_or_newer("2.8.1", "2.8.0.dev") assert not _is_torch_equal_or_newer("2.7.1", "2.8.0.dev") def test_get_raw_stream_patch(): """Test that get_raw_stream patch is applied only for torch 2.9.0 or 2.9.1.""" import builtins # Check if get_raw_stream exists in builtins has_patch = hasattr(builtins, "get_raw_stream") # Import torch to get actual version is_torch_2_9 = is_torch_equal("2.9.0") or is_torch_equal("2.9.1") if is_torch_2_9: # For torch 2.9.x, the patch should be applied assert has_patch, "get_raw_stream should be patched for torch 2.9.x" # Verify it's callable (it should be the _cuda_getCurrentRawStream function) get_raw_stream = builtins.get_raw_stream # type: ignore[attr-defined] assert callable(get_raw_stream) # Verify it's the correct function from torch._C from torch._C import _cuda_getCurrentRawStream assert get_raw_stream is _cuda_getCurrentRawStream def test_copy_pass(): vllm_config = VllmConfig() inductor_pass = FixFunctionalizationPass(vllm_config) copied_inductor_pass = copy.deepcopy(inductor_pass) assert ( copied_inductor_pass.compilation_config.use_inductor_graph_partition == vllm_config.compilation_config.use_inductor_graph_partition ) assert ( copied_inductor_pass.compilation_config.splitting_ops == vllm_config.compilation_config.splitting_ops ) def test_custom_op(): # proper syntax _ = CompilationConfig(custom_ops=["+quant_fp8", "-silu_and_mul"]) with pytest.raises(ValueError, match="Invalid syntax '"): _ = CompilationConfig(custom_ops=["quant_fp8"]) # forked needed to workaround https://github.com/vllm-project/vllm/issues/21073 @pytest.mark.forked # NB: We don't test VLLM_DISABLE_COMPILE_CACHE=0 because that depends # on the state of the cache directory on the current machine, which # may be influenced by other tests. @pytest.mark.parametrize("val", ["1"]) def test_VLLM_DISABLE_COMPILE_CACHE(vllm_runner, monkeypatch, val): # Disable multiprocessing so that the counter is in the same process monkeypatch.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", "0") monkeypatch.setenv("VLLM_DISABLE_COMPILE_CACHE", val) compilation_config = { "cudagraph_mode": CUDAGraphMode.NONE, # speed things up a bit } with ( compilation_counter.expect( num_cache_entries_updated=0, num_compiled_artifacts_saved=0 ), # loading the model causes compilation (if enabled) to happen vllm_runner( "facebook/opt-125m", compilation_config=compilation_config, gpu_memory_utilization=0.4, ) as _, ): pass # forked needed to workaround https://github.com/vllm-project/vllm/issues/21073 @pytest.mark.forked @pytest.mark.parametrize( "cudagraph_mode,num_cudagraph_captured", [ (CUDAGraphMode.NONE, 0), (CUDAGraphMode.FULL_DECODE_ONLY, 1), (CUDAGraphMode.PIECEWISE, 13), (CUDAGraphMode.FULL_AND_PIECEWISE, 14), ], ) def test_use_cudagraphs( vllm_runner, monkeypatch, cudagraph_mode, num_cudagraph_captured ): # Disable multiprocessing so that the counter is in the same process monkeypatch.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", "0") compilation_config = { "cudagraph_capture_sizes": [100], "cudagraph_mode": cudagraph_mode, } num_gpu_runner_capture_triggers = 1 if cudagraph_mode != CUDAGraphMode.NONE else 0 with ( compilation_counter.expect( num_graphs_seen=1, num_gpu_runner_capture_triggers=num_gpu_runner_capture_triggers, num_cudagraph_captured=num_cudagraph_captured, ), # loading the model causes compilation (if enabled) to happen vllm_runner( "facebook/opt-125m", compilation_config=compilation_config, gpu_memory_utilization=0.4, ) as _, ): pass # forked needed to workaround https://github.com/vllm-project/vllm/issues/21073 @pytest.mark.forked def test_stock_torch_compile(vllm_runner, monkeypatch): # Disable multiprocessing so that the counter is in the same process monkeypatch.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", "0") with ( compilation_counter.expect(stock_torch_compile_count=1), # loading the model causes compilation (if enabled) to happen vllm_runner( "facebook/opt-125m", compilation_config={"mode": CompilationMode.STOCK_TORCH_COMPILE}, gpu_memory_utilization=0.4, ) as _, ): pass # forked needed to workaround https://github.com/vllm-project/vllm/issues/21073 @pytest.mark.forked def test_no_compilation(vllm_runner, monkeypatch): # Disable multiprocessing so that the counter is in the same process monkeypatch.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", "0") with ( compilation_counter.expect(num_graphs_seen=0, stock_torch_compile_count=0), # loading the model causes compilation (if enabled) to happen vllm_runner( "facebook/opt-125m", compilation_config={"mode": CompilationMode.NONE}, gpu_memory_utilization=0.4, ) as _, ): pass # forked needed to workaround https://github.com/vllm-project/vllm/issues/21073 @pytest.mark.forked def test_enforce_eager(vllm_runner, monkeypatch): # Disable multiprocessing so that the counter is in the same process monkeypatch.setenv("VLLM_ENABLE_V1_MULTIPROCESSING", "0") with ( compilation_counter.expect(num_graphs_seen=0, stock_torch_compile_count=0), # loading the model causes compilation (if enabled) to happen vllm_runner( "facebook/opt-125m", enforce_eager=True, gpu_memory_utilization=0.4 ) as _, ): pass def test_splitting_ops_dynamic(): # Default config config = VllmConfig() # Default V1 config leaves cudagraph mode unset; splitting ops are only # populated when the engine decides to use piecewise compilation. assert config.compilation_config.cudagraph_mode == CUDAGraphMode.FULL_AND_PIECEWISE assert config.compilation_config.splitting_ops_contain_attention() # When use_inductor_graph_partition=True config = VllmConfig( compilation_config=CompilationConfig( mode=CompilationMode.VLLM_COMPILE, use_inductor_graph_partition=True, splitting_ops=["vllm::unified_attention"], ) ) # with inductor partition we use splitting_ops directly for # partition rules assert config.compilation_config.splitting_ops == ["vllm::unified_attention"] # When attn_fusion pass enabled. config = VllmConfig( compilation_config=CompilationConfig( mode=CompilationMode.VLLM_COMPILE, pass_config=PassConfig(fuse_attn_quant=True, eliminate_noops=True), custom_ops=["+quant_fp8"], cudagraph_mode=CUDAGraphMode.PIECEWISE, ) ) assert config.compilation_config.splitting_ops == [] # cudagraph mode also fall back to FULL assert config.compilation_config.cudagraph_mode == CUDAGraphMode.FULL # splitting_ops can not contain attention ops when attn_fusion # pass enabled. with pytest.raises(ValidationError): config = VllmConfig( compilation_config=CompilationConfig( mode=CompilationMode.VLLM_COMPILE, pass_config=PassConfig(fuse_attn_quant=True, eliminate_noops=True), custom_ops=["+quant_fp8"], cudagraph_mode=CUDAGraphMode.PIECEWISE, # work around for accessing all attntion ops splitting_ops=CompilationConfig()._attention_ops, ) ) # When both use_inductor_graph_partition and attn_fusion pass enabled. config = VllmConfig( compilation_config=CompilationConfig( mode=CompilationMode.VLLM_COMPILE, use_inductor_graph_partition=True, pass_config=PassConfig(fuse_attn_quant=True, eliminate_noops=True), custom_ops=["+quant_fp8"], cudagraph_mode=CUDAGraphMode.PIECEWISE, ) ) # With inductor graph partition, attn_fusion and splitting_ops # work together. Default splitting_ops include attention ops. assert config.compilation_config.splitting_ops_contain_attention() # fuse_attn_quant is directly supported under # use_inductor_graph_partition=True, and cudagraph_mode # is unchanged. assert config.compilation_config.cudagraph_mode == CUDAGraphMode.PIECEWISE def test_moe_splitting_ops_deepep_ht_inductor_partition(): # Inductor partition case: user-provided splitting_ops should be # preserved and MoE ops should be appended for DeepEP HT with dp>1. config = VllmConfig( parallel_config=ParallelConfig( all2all_backend="deepep_high_throughput", data_parallel_size=8, ), compilation_config=CompilationConfig( mode=CompilationMode.VLLM_COMPILE, use_inductor_graph_partition=True, splitting_ops=[ "vllm::unified_attention", "vllm::moe_forward", "vllm::moe_forward_shared", ], ), ) splitting_ops = config.compilation_config.splitting_ops assert splitting_ops == [ "vllm::unified_attention", "vllm::moe_forward", "vllm::moe_forward_shared", ] def test_should_split(): import torch from vllm.compilation.partition_rules import should_split graph = torch.fx.Graph() node = torch.fx.Node( graph=graph, name="dummy_node", op="call_function", target=torch.ops.aten.add.default, args=(), kwargs={}, ) # supports OpOverloadPacket splitting_ops = ["aten::add"] assert should_split(node, splitting_ops) # supports OpOverload splitting_ops = ["aten::add.default"] assert should_split(node, splitting_ops) # supports OpOverload splitting_ops = ["aten::add.Tensor"] assert not should_split(node, splitting_ops) q, k, v, out = [torch.randn(1)] * 4 # supports custom ops as OpOverloadPacket node = torch.fx.Node( graph=graph, name="dummy_node", op="call_function", target=torch.ops.silly.attention, args=(q, k, v, out), kwargs={}, ) splitting_ops = ["silly::attention"] assert should_split(node, splitting_ops) # supports custom ops as OpOverload node = torch.fx.Node( graph=graph, name="dummy_node", op="call_function", target=torch.ops.silly.attention.default, args=(q, k, v, out), kwargs={}, ) splitting_ops = ["silly::attention"] assert should_split(node, splitting_ops) splitting_ops = ["silly::attention.default"] assert should_split(node, splitting_ops) @pytest.mark.skipif( not current_platform.support_static_graph_mode(), reason="Skip if not cudagraph mode supported", ) @pytest.mark.parametrize( ( "cudagraph_capture_sizes", "max_cudagraph_capture_size", "tp_size", "enable_sp", "max_num_batched_tokens", "cudagraph_mode", "expected_max_size", ), [ (None, None, 1, False, 2048, CUDAGraphMode.FULL_AND_PIECEWISE, 256), ([1, 2, 4], 4, 1, False, 2048, CUDAGraphMode.FULL_AND_PIECEWISE, 4), ( [1, 2, 4], 8, 1, False, 2048, CUDAGraphMode.FULL_AND_PIECEWISE, ValidationError, ), ([1, 256], None, 1, False, 2048, CUDAGraphMode.FULL_AND_PIECEWISE, 256), ([], None, 1, False, 2048, CUDAGraphMode.NONE, 0), (None, 0, 1, False, 2048, CUDAGraphMode.NONE, 0), # truncated to nearest multiple of 8 or 16 (None, 257, 1, False, 2048, CUDAGraphMode.FULL_AND_PIECEWISE, 256), # max from list ([1, 2, 4, 15], None, 1, False, 2048, CUDAGraphMode.FULL_AND_PIECEWISE, 15), # filtered out 15 due to SP ([1, 2, 4, 15], None, 2, True, 2048, CUDAGraphMode.FULL_AND_PIECEWISE, 4), # limited by the max_tokens ([1, 2, 4, 15], None, 1, False, 8, CUDAGraphMode.FULL_AND_PIECEWISE, 4), # the list should contain at least 1 element when use cudagraph ([], None, 1, False, 2048, CUDAGraphMode.FULL_AND_PIECEWISE, ValidationError), # the max capturing size should be >= 1 when use cudagraph (None, 0, 1, False, 2048, CUDAGraphMode.FULL_AND_PIECEWISE, ValidationError), ], ) def test_cudagraph_sizes_post_init( cudagraph_capture_sizes, max_cudagraph_capture_size, tp_size, enable_sp, max_num_batched_tokens, cudagraph_mode, expected_max_size, ): ctx = nullcontext() if expected_max_size == ValidationError: ctx = pytest.raises(expected_max_size) with ( ctx, patch("vllm.config.parallel.cuda_device_count_stateless", return_value=tp_size), ): compilation_config = CompilationConfig( cudagraph_capture_sizes=cudagraph_capture_sizes, max_cudagraph_capture_size=max_cudagraph_capture_size, pass_config=PassConfig( enable_sp=enable_sp, fuse_norm_quant=True, fuse_act_quant=True, eliminate_noops=True, sp_min_token_num=512 if enable_sp else None, ), cudagraph_mode=cudagraph_mode, ) engine_args = EngineArgs( model="facebook/opt-125m", tensor_parallel_size=tp_size, max_num_seqs=min(max_num_batched_tokens, 128), max_num_batched_tokens=max_num_batched_tokens, compilation_config=compilation_config, ) vllm_config = engine_args.create_engine_config() assert ( vllm_config.compilation_config.max_cudagraph_capture_size == expected_max_size ) def test_cached_compilation_config(default_vllm_config): import torch from torch._inductor.utils import run_and_get_code from vllm.config import get_cached_compilation_config, set_current_vllm_config from vllm.model_executor.layers.quantization.input_quant_fp8 import QuantFP8 from vllm.model_executor.layers.quantization.utils.quant_utils import GroupShape dtype = torch.bfloat16 device = torch.device("cuda:0") batch_size, num_qo_heads, head_size = 8, 16, 128 # access and cache default compilation config # default compilation config does not contain +quant_fp8 custom op. If this is # used, the generated code would use inductor-generated triton kernel instead # of the custom op `torch.ops._C.static_scaled_fp8_quant`. get_cached_compilation_config() vllm_config = VllmConfig( compilation_config=CompilationConfig( mode=CompilationMode.VLLM_COMPILE, custom_ops=["+quant_fp8"], ) ) # set_current_vllm_config should clear cached compilation config and # use the new compilation_config in vllm_config with set_current_vllm_config(vllm_config): query_quant = QuantFP8(static=True, group_shape=GroupShape.PER_TENSOR) query_quant = torch.compile(query_quant) _q_scale = torch.tensor(1.0, dtype=torch.float32, device="cuda") query = torch.randn( batch_size, num_qo_heads * head_size, dtype=dtype, device=device ) _, code = run_and_get_code(query_quant, query, _q_scale) code = " ".join(code) assert "torch.ops._C.static_scaled_fp8_quant.default(" in code def _create_vllm_config_for_validation( compilation_config: CompilationConfig, ) -> MagicMock: """Helper to create a mock VllmConfig for padding validation testing.""" mock_config = MagicMock(spec=VllmConfig) mock_config.compilation_config = compilation_config mock_config.scheduler_config = SchedulerConfig.default_factory(max_num_seqs=8) mock_config.parallel_config = ParallelConfig() mock_config.speculative_config = None mock_config.lora_config = None return mock_config def test_compile_sizes_padding_validation(): """Test that compile_sizes with values that would be padded raises an error.""" # cudagraph_capture_sizes=[1, 2, 4, 8] means: # - size 1 -> padded to 1 # - size 2 -> padded to 2 # - size 3 -> padded to 4 # - size 4 -> padded to 4 # - size 5 -> padded to 8 # etc. # So compile_sizes=[3] should fail because 3 would be padded to 4 with pytest.raises(ValueError, match="would be padded to"): config = CompilationConfig( cudagraph_capture_sizes=[1, 2, 4, 8], max_cudagraph_capture_size=8, compile_sizes=[3], cudagraph_mode=CUDAGraphMode.FULL, ) config.post_init_cudagraph_sizes() dispatcher = CudagraphDispatcher(_create_vllm_config_for_validation(config)) dispatcher.initialize_cudagraph_keys(CUDAGraphMode.FULL) with pytest.raises(ValueError, match="would be padded to"): config = CompilationConfig( cudagraph_capture_sizes=[1, 2, 4, 8], max_cudagraph_capture_size=8, compile_sizes=[5], cudagraph_mode=CUDAGraphMode.FULL, ) config.post_init_cudagraph_sizes() dispatcher = CudagraphDispatcher(_create_vllm_config_for_validation(config)) dispatcher.initialize_cudagraph_keys(CUDAGraphMode.FULL) config = CompilationConfig( cudagraph_capture_sizes=[1, 2, 4, 8], max_cudagraph_capture_size=8, compile_sizes=[1, 2, 4, 8], cudagraph_mode=CUDAGraphMode.FULL, ) config.post_init_cudagraph_sizes() assert sorted(config.compile_sizes) == [1, 2, 4, 8] dispatcher = CudagraphDispatcher(_create_vllm_config_for_validation(config)) dispatcher.initialize_cudagraph_keys(CUDAGraphMode.FULL) # Should not raise config = CompilationConfig( cudagraph_capture_sizes=[1, 2, 4, 8], max_cudagraph_capture_size=8, compile_sizes=["cudagraph_capture_sizes"], cudagraph_mode=CUDAGraphMode.FULL, ) config.post_init_cudagraph_sizes() assert sorted(config.compile_sizes) == [1, 2, 4, 8] # When cudagraphs are disabled (max_cudagraph_capture_size=0), # padding validation should be skipped config = CompilationConfig( cudagraph_capture_sizes=[], max_cudagraph_capture_size=0, compile_sizes=[3, 5, 7], # would be invalid with cudagraphs ) config.post_init_cudagraph_sizes() assert sorted(config.compile_sizes) == [3, 5, 7] # When cudagraph_mode is NONE but capture_sizes is non-empty, # padding validation should still be skipped config = CompilationConfig( cudagraph_capture_sizes=[1, 2, 4, 8], max_cudagraph_capture_size=8, cudagraph_mode=CUDAGraphMode.NONE, compile_sizes=[3, 5, 7], # would be invalid if cudagraphs were enabled ) config.post_init_cudagraph_sizes() assert sorted(config.compile_sizes) == [3, 5, 7] dispatcher = CudagraphDispatcher(_create_vllm_config_for_validation(config)) dispatcher.initialize_cudagraph_keys(CUDAGraphMode.NONE) # Should not raise
{ "repo_id": "vllm-project/vllm", "file_path": "tests/compile/test_config.py", "license": "Apache License 2.0", "lines": 496, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/test_flex_attention.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """Integration tests for FlexAttention backend vs default backend""" import pytest import torch from packaging import version from tests.utils import set_random_seed from tests.v1.attention.utils import ( BatchSpec, create_common_attn_metadata, create_standard_kv_cache_spec, create_vllm_config, ) from vllm.v1.attention.backends.flex_attention import ( FlexAttentionMetadataBuilder, physical_to_logical_mapping, ) from ..models.utils import check_embeddings_close, check_logprobs_close TORCH_VERSION = version.parse(torch.__version__) MINIMUM_TORCH_VERSION = version.parse("2.7.0") DIRECT_BUILD_VERSION = version.parse("2.9.dev0") @pytest.mark.skipif( not torch.cuda.is_available() or TORCH_VERSION < MINIMUM_TORCH_VERSION, reason="CUDA not available or PyTorch version < 2.7", ) def test_flex_attention_vs_default_backend(vllm_runner): """Test that FlexAttention produces the same outputs as the default backend. This test compares the outputs from the FlexAttention backend with the default backend, ensuring they are similar when using the same seed. """ model_name = "Qwen/Qwen2.5-1.5B-Instruct" seed = 42 max_tokens = 24 num_logprobs = 5 prompts = [ "Hello, my name is", "The president of the United States is", "The capital of France is", ] # Run with flex attention set_random_seed(seed) with vllm_runner( model_name, runner="generate", tensor_parallel_size=1, num_gpu_blocks_override=128, enforce_eager=True, attention_config={"backend": "FLEX_ATTENTION"}, ) as llm_flex: output_flex = llm_flex.generate_greedy_logprobs( prompts, max_tokens, num_logprobs ) # Run with default backend set_random_seed(seed) with vllm_runner( model_name, runner="generate", tensor_parallel_size=1, num_gpu_blocks_override=128, enforce_eager=True, gpu_memory_utilization=0.85, ) as llm_default: output_default = llm_default.generate_greedy_logprobs( prompts, max_tokens, num_logprobs ) check_logprobs_close( outputs_0_lst=output_flex, outputs_1_lst=output_default, name_0="flex", name_1="default", ) @pytest.mark.skipif( not torch.cuda.is_available() or TORCH_VERSION < MINIMUM_TORCH_VERSION, reason="CUDA not available or PyTorch version < 2.7", ) def test_encoder_flex_attention_vs_default_backend(vllm_runner): """Test that FlexAttention produces the same outputs as the default backend. This test compares the outputs from the FlexAttention backend with the default backend for encoder models. """ model_name = "BAAI/bge-base-en-v1.5" prompts = [ "Hello, my name is", "The president of the United States is", "The capital of France is", ] # Run with flex attention with vllm_runner( model_name, runner="pooling", dtype=torch.bfloat16, tensor_parallel_size=1, max_model_len=100, enforce_eager=True, attention_config={"backend": "FLEX_ATTENTION"}, ) as llm_flex: flex_outputs = llm_flex.embed(prompts) # Run with default backend with vllm_runner( model_name, runner="pooling", dtype=torch.bfloat16, tensor_parallel_size=1, max_model_len=100, enforce_eager=True, ) as llm_default: default_outputs = llm_default.embed(prompts) check_embeddings_close( embeddings_0_lst=flex_outputs, embeddings_1_lst=default_outputs, name_0="flex", name_1="default", tol=1e-2, ) @pytest.mark.skipif( not torch.cuda.is_available() or TORCH_VERSION < DIRECT_BUILD_VERSION, reason="CUDA not available or PyTorch version < 2.7", ) def test_block_mask_direct_vs_slow_path(): """Test that direct path block mask is a superset of slow path. The direct path may include extra blocks for performance (over-estimation), but must include all blocks that the slow path determines are necessary. """ device = torch.device("cuda") vllm_config = create_vllm_config( model_name="meta-llama/Meta-Llama-3-8B", block_size=16, max_model_len=1024 ) kv_cache_spec = create_standard_kv_cache_spec(vllm_config) # Use a mixed batch that will create groups spanning multiple sequences batch_spec = BatchSpec( seq_lens=[35, 64, 128, 256], query_lens=[33, 5, 32, 64], name="test_mixed_batch" ) common_attn_metadata = create_common_attn_metadata( batch_spec, vllm_config.cache_config.block_size, device ) builder = FlexAttentionMetadataBuilder(kv_cache_spec, [], vllm_config, device) metadata_direct = builder.build( common_prefix_len=0, common_attn_metadata=common_attn_metadata ) builder.direct_build = False metadata_slow = builder.build( common_prefix_len=0, common_attn_metadata=common_attn_metadata ) assert metadata_direct.block_mask is not None assert metadata_slow.block_mask is not None # Extract block indices for comparison, B, H are the same direct_indices = metadata_direct.block_mask.kv_indices[0, 0] slow_indices = metadata_slow.block_mask.kv_indices[0, 0] direct_num = metadata_direct.block_mask.kv_num_blocks[0, 0] slow_num = metadata_slow.block_mask.kv_num_blocks[0, 0] # main test: every block needed by slow path must be in direct path num_groups = direct_num.shape[0] all_contained = True missing_details = [] for group_idx in range(num_groups): direct_blocks = set(direct_indices[group_idx, : direct_num[group_idx]].tolist()) slow_blocks = set(slow_indices[group_idx, : slow_num[group_idx]].tolist()) missing_blocks = slow_blocks - direct_blocks if missing_blocks: all_contained = False missing_details.append( f"Group {group_idx}: missing {sorted(missing_blocks)}" ) assert all_contained, ( "Direct path is missing blocks required by slow path:\n" + "\n".join(missing_details) ) def test_physical_to_logical_mapping_handles_reused_blocks(): """Regression test: reused physical blocks map to the latest logical block. For sliding-window / hybrid attention layers, physical KV-cache blocks can be reused over time. The inverse mapping must therefore select the latest logical block index for a physical block id. """ # Padding should not make physical block 0 look live. block_table = torch.tensor([[6, 0, 0, 0]], dtype=torch.int32) seq_lens = torch.tensor([1 * 16], dtype=torch.int32) # only 1 block valid out = physical_to_logical_mapping( block_table=block_table, seq_lens=seq_lens, block_size=16, total_blocks=10 ) assert out[0, 0].item() == -1 assert out[0, 6].item() == 0 # If a physical block id appears multiple times (block reuse), mapping should # point to the latest logical block index. block_table2 = torch.tensor([[2, 2, 5]], dtype=torch.int32) seq_lens2 = torch.tensor([3 * 16], dtype=torch.int32) out2 = physical_to_logical_mapping( block_table=block_table2, seq_lens=seq_lens2, block_size=16, total_blocks=8 ) assert out2[0, 2].item() == 1 if __name__ == "__main__": pytest.main([__file__])
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/test_flex_attention.py", "license": "Apache License 2.0", "lines": 191, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/v1/core/kv_cache_coordinator.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from abc import ABC, abstractmethod from collections.abc import Sequence from math import lcm from vllm.v1.core.block_pool import BlockPool from vllm.v1.core.kv_cache_metrics import KVCacheMetricsCollector from vllm.v1.core.kv_cache_utils import ( BlockHash, BlockHashList, BlockHashListWithBlockSize, KVCacheBlock, ) from vllm.v1.core.single_type_kv_cache_manager import ( CrossAttentionManager, SingleTypeKVCacheManager, get_manager_for_kv_cache_spec, ) from vllm.v1.kv_cache_interface import ( FullAttentionSpec, KVCacheConfig, KVCacheSpec, ) from vllm.v1.request import Request class KVCacheCoordinator(ABC): """ Coordinate the KV cache of different KV cache groups. """ def __init__( self, kv_cache_config: KVCacheConfig, max_model_len: int, use_eagle: bool, enable_caching: bool, enable_kv_cache_events: bool, dcp_world_size: int, pcp_world_size: int, hash_block_size: int, metrics_collector: KVCacheMetricsCollector | None = None, ): self.kv_cache_config = kv_cache_config self.max_model_len = max_model_len self.enable_caching = enable_caching self.block_pool = BlockPool( kv_cache_config.num_blocks, enable_caching, hash_block_size, enable_kv_cache_events, metrics_collector, ) # Needs special handling for find_longest_cache_hit if eagle is enabled self.use_eagle = use_eagle self.single_type_managers = tuple( get_manager_for_kv_cache_spec( kv_cache_spec=kv_cache_group.kv_cache_spec, block_pool=self.block_pool, enable_caching=enable_caching, kv_cache_group_id=i, dcp_world_size=dcp_world_size, pcp_world_size=pcp_world_size, ) for i, kv_cache_group in enumerate(self.kv_cache_config.kv_cache_groups) ) def get_num_blocks_to_allocate( self, request_id: str, num_tokens: int, new_computed_blocks: tuple[Sequence[KVCacheBlock], ...], num_encoder_tokens: int, total_computed_tokens: int, num_tokens_main_model: int, ) -> int: """ Get the number of blocks needed to be allocated for the request. Args: request_id: The request ID. num_tokens: The total number of tokens that need a slot (including tokens that are already allocated). new_computed_blocks: The new computed blocks just hitting the prefix caching. num_encoder_tokens: The number of encoder tokens for allocating blocks for cross-attention. total_computed_tokens: Include both local and external tokens. num_tokens_main_model: The number of tokens for the main model (aka target model in spec decode). w/o spec decode, it is num_tokens; with spec decode, it is num_tokens - num_lookahead_tokens. Returns: The number of blocks to allocate. """ num_blocks_to_allocate = 0 for i, manager in enumerate(self.single_type_managers): if isinstance(manager, CrossAttentionManager): # For cross-attention, we issue a single static allocation # of blocks based on the number of encoder input tokens. num_blocks_to_allocate += manager.get_num_blocks_to_allocate( request_id, num_encoder_tokens, [], 0, num_encoder_tokens ) else: num_blocks_to_allocate += manager.get_num_blocks_to_allocate( request_id, num_tokens, new_computed_blocks[i], total_computed_tokens, num_tokens_main_model, ) return num_blocks_to_allocate def allocate_new_computed_blocks( self, request_id: str, new_computed_blocks: tuple[Sequence[KVCacheBlock], ...], num_local_computed_tokens: int, num_external_computed_tokens: int, ) -> None: """ Add the new computed blocks to the request. Optionally allocate new blocks for external computed tokens (if any). Args: request_id: The request ID. new_computed_blocks: The new computed blocks just hitting the prefix cache. num_local_computed_tokens: The number of local computed tokens. num_external_computed_tokens: The number of external computed tokens. """ for i, manager in enumerate(self.single_type_managers): manager.allocate_new_computed_blocks( request_id, new_computed_blocks[i], num_local_computed_tokens, num_external_computed_tokens, ) def allocate_new_blocks( self, request_id: str, num_tokens: int, num_tokens_main_model: int, num_encoder_tokens: int = 0, ) -> tuple[list[KVCacheBlock], ...]: """ Allocate new blocks for the request to give it at least `num_tokens` token slots. Args: request_id: The request ID. num_tokens: The total number of tokens that need a slot (including tokens that are already allocated). num_tokens_main_model: The number of tokens for the main model (aka target model in spec decode). w/o spec decode, it is num_tokens; with spec decode, it is num_tokens - num_lookahead_tokens. num_encoder_tokens: The number of encoder tokens for allocating blocks for cross-attention. Returns: The new allocated blocks. """ return tuple( manager.allocate_new_blocks( request_id, num_encoder_tokens if isinstance(manager, CrossAttentionManager) else num_tokens, num_tokens_main_model, ) for manager in self.single_type_managers ) def cache_blocks(self, request: Request, num_computed_tokens: int) -> None: """ Cache the blocks for the request. Args: request: The request. num_computed_tokens: The total number of tokens that need to be cached (including tokens that are already cached). """ for manager in self.single_type_managers: manager.cache_blocks(request, num_computed_tokens) def free(self, request_id: str) -> None: """ Free the blocks for the request. Args: request_id: The request ID. """ for manager in self.single_type_managers: manager.free(request_id) def get_num_common_prefix_blocks(self, running_request_id: str) -> list[int]: """ Get the number of common prefix blocks for all requests with allocated KV cache for each kv cache group. Args: running_request_id: The request ID of any running request, used to identify the common prefix blocks. Returns: list[int]: The number of common prefix blocks for each kv cache group. """ return [ manager.get_num_common_prefix_blocks(running_request_id) for manager in self.single_type_managers ] def remove_skipped_blocks( self, request_id: str, total_computed_tokens: int ) -> None: """ Remove the blocks that are no longer needed from `blocks` and replace the removed blocks with null_block. Args: request_id: The request ID. total_computed_tokens: The total number of computed tokens, including local computed tokens and external computed tokens. """ for manager in self.single_type_managers: manager.remove_skipped_blocks(request_id, total_computed_tokens) def get_blocks(self, request_id: str) -> tuple[list[KVCacheBlock], ...]: """ Get the blocks for the request. """ return tuple( manager.req_to_blocks.get(request_id) or [] for manager in self.single_type_managers ) @abstractmethod def find_longest_cache_hit( self, block_hashes: list[BlockHash], max_cache_hit_length: int, ) -> tuple[tuple[list[KVCacheBlock], ...], int]: pass def new_step_starts(self) -> None: """Called when a new step is started.""" for manager in self.single_type_managers: manager.new_step_starts() class KVCacheCoordinatorNoPrefixCache(KVCacheCoordinator): """ KV cache coordinator to use if prefix caching is disabled or unsupported. In contrast to UnitaryKVCacheCoordinator and HybridKVCacheCoordinator, supports arbitrary numbers of KV cache groups (including 0 groups). Does not implement any features related to prefix caching. """ def __init__( self, kv_cache_config: KVCacheConfig, max_model_len: int, use_eagle: bool, enable_kv_cache_events: bool, dcp_world_size: int, pcp_world_size: int, hash_block_size: int, metrics_collector: KVCacheMetricsCollector | None = None, ): super().__init__( kv_cache_config, max_model_len, use_eagle, False, enable_kv_cache_events, dcp_world_size=dcp_world_size, pcp_world_size=pcp_world_size, hash_block_size=hash_block_size, metrics_collector=metrics_collector, ) self.num_single_type_manager = len(self.single_type_managers) def get_num_common_prefix_blocks(self, running_request_id: str) -> list[int]: return [0] * self.num_single_type_manager def find_longest_cache_hit( self, block_hashes: list[BlockHash], max_cache_hit_length: int, ) -> tuple[tuple[list[KVCacheBlock], ...], int]: blocks: tuple[list[KVCacheBlock], ...] = tuple( [] for _ in range(self.num_single_type_manager) ) return blocks, 0 class UnitaryKVCacheCoordinator(KVCacheCoordinator): """ KV cache coordinator for models with only one KV cache group. This is the case for models with only one KV cache type, e.g., all attention layers use full attention or all attention layers use sliding window attention. """ def __init__( self, kv_cache_config: KVCacheConfig, max_model_len: int, use_eagle: bool, enable_caching: bool, enable_kv_cache_events: bool, dcp_world_size: int, pcp_world_size: int, hash_block_size: int, metrics_collector: KVCacheMetricsCollector | None = None, ): super().__init__( kv_cache_config, max_model_len, use_eagle, enable_caching, enable_kv_cache_events, dcp_world_size=dcp_world_size, pcp_world_size=pcp_world_size, hash_block_size=hash_block_size, metrics_collector=metrics_collector, ) self.kv_cache_spec = self.kv_cache_config.kv_cache_groups[0].kv_cache_spec self.block_size = self.kv_cache_spec.block_size self.dcp_world_size = dcp_world_size self.pcp_world_size = pcp_world_size if dcp_world_size > 1: self.block_size *= dcp_world_size if pcp_world_size > 1: self.block_size *= pcp_world_size # For models using only Mamba, block_size is set to max_model_len when # prefix caching is disabled, and hash_block_size validation is skipped. assert not enable_caching or (hash_block_size == self.block_size), ( "UnitaryKVCacheCoordinator assumes hash_block_size == block_size" ) assert len(self.kv_cache_config.kv_cache_groups) == 1, ( "UnitaryKVCacheCoordinator assumes only one kv cache group" ) def find_longest_cache_hit( self, block_hashes: list[BlockHash], max_cache_hit_length: int, ) -> tuple[tuple[list[KVCacheBlock], ...], int]: hit_blocks = self.single_type_managers[0].find_longest_cache_hit( block_hashes=block_hashes, max_length=max_cache_hit_length, kv_cache_group_ids=[0], block_pool=self.block_pool, kv_cache_spec=self.kv_cache_spec, use_eagle=self.use_eagle, alignment_tokens=self.block_size, dcp_world_size=self.dcp_world_size, pcp_world_size=self.pcp_world_size, ) return hit_blocks, len(hit_blocks[0]) * self.block_size class HybridKVCacheCoordinator(KVCacheCoordinator): """ KV cache coordinator for hybrid models with multiple KV cache types, and thus multiple kv cache groups. """ def __init__( self, kv_cache_config: KVCacheConfig, max_model_len: int, use_eagle: bool, enable_caching: bool, enable_kv_cache_events: bool, dcp_world_size: int, pcp_world_size: int, hash_block_size: int, metrics_collector: KVCacheMetricsCollector | None = None, ): super().__init__( kv_cache_config, max_model_len, use_eagle, enable_caching, enable_kv_cache_events, dcp_world_size=dcp_world_size, pcp_world_size=pcp_world_size, hash_block_size=hash_block_size, metrics_collector=metrics_collector, ) # hash_block_size: the block size used to compute block hashes. # The actual block size usually equals hash_block_size, but in cases where # different KV cache groups have different block sizes, the actual block size # can be a multiple of hash_block_size. self.hash_block_size = hash_block_size assert all( g.kv_cache_spec.block_size % hash_block_size == 0 for g in kv_cache_config.kv_cache_groups ), "block_size must be divisible by hash_block_size" assert dcp_world_size == 1, "DCP not support hybrid attn now." assert pcp_world_size == 1, "PCP not support hybrid attn now." self.verify_and_split_kv_cache_groups() def verify_and_split_kv_cache_groups(self) -> None: """ Groups KV cache groups by their spec type for efficient batch processing during cache hit lookup. """ attention_groups: list[ tuple[KVCacheSpec, list[int], type[SingleTypeKVCacheManager]] ] = [] for i, g in enumerate(self.kv_cache_config.kv_cache_groups): manager_cls = self.single_type_managers[i].__class__ spec = g.kv_cache_spec # Try to find an existing group with the same spec for existing_spec, group_ids, existing_cls in attention_groups: if existing_spec == spec: assert manager_cls is existing_cls, ( "Expected same manager class for identical KV cache specs." ) group_ids.append(i) break else: attention_groups.append((spec, [i], manager_cls)) assert len(attention_groups) > 1, ( "HybridKVCacheCoordinator requires at least two attention groups." ) # Put full attention first: its efficient left-to-right scan provides # a tighter initial bound, reducing work for subsequent groups. self.attention_groups = sorted( attention_groups, key=lambda x: not isinstance(x[0], FullAttentionSpec), ) # The LCM of the block sizes of all attention types. # The cache hit length must be a multiple of the LCM of the block sizes # to make sure the cache hit length is a multiple of the block size of # each attention type. Requiring this because we don't support partial # block cache hit yet. block_sizes = [spec.block_size for spec, _, _ in attention_groups] self.lcm_block_size = lcm(*block_sizes) def find_longest_cache_hit( self, block_hashes: list[BlockHash], max_cache_hit_length: int, ) -> tuple[tuple[list[KVCacheBlock], ...], int]: """ Find the longest cache hit using an iterative fixed-point algorithm. Each attention type either accepts the current candidate length or reduces it. If any type reduces the length, restart checks over all types. This converges because length monotonically decreases and is bounded below by 0. Args: block_hashes: The block hashes of the request. max_cache_hit_length: The maximum length of the cache hit. Returns: A tuple containing: - A tuple of the cache hit blocks for each single type manager. - The number of tokens of the longest cache hit. """ def _get_block_hashes(kv_cache_spec: KVCacheSpec) -> BlockHashList: if kv_cache_spec.block_size == self.hash_block_size: return block_hashes return BlockHashListWithBlockSize( block_hashes, self.hash_block_size, kv_cache_spec.block_size ) num_groups = len(self.kv_cache_config.kv_cache_groups) hit_length = max_cache_hit_length hit_blocks_by_group: list[list[KVCacheBlock] | None] = [None] * num_groups # Simple hybrid (1 full attn + 1 other): one iteration suffices. # Full attn is always first if it exists. This avoids EAGLE drops # being applied multiple times to non-full-attn groups. # FIXME (yifan): However, for complex hybrid models with multiple attn # groups, we still have the EAGLE spiral block dropping problem. See # discussion in issue https://github.com/vllm-project/vllm/issues/32802. is_simple_hybrid = len(self.attention_groups) == 2 and isinstance( self.attention_groups[0][0], FullAttentionSpec ) while True: curr_hit_length = hit_length for spec, group_ids, manager_cls in self.attention_groups: is_full_attn = isinstance(spec, FullAttentionSpec) # Full attention: reuse cached blocks (downward-closed property) cached_blocks = hit_blocks_by_group[group_ids[0]] if is_full_attn and cached_blocks is not None: # For full attention, we only need to compute the cache hit # length once. Starting from the second iteration, if the # curr_hit_length is reduced by other groups, we can simply # keep the first (curr_hit_length // block_size) blocks from # the last iteration. num_blocks = curr_hit_length // spec.block_size curr_hit_length = num_blocks * spec.block_size else: hit_blocks = manager_cls.find_longest_cache_hit( block_hashes=_get_block_hashes(spec), max_length=curr_hit_length, kv_cache_group_ids=group_ids, block_pool=self.block_pool, kv_cache_spec=spec, use_eagle=self.use_eagle, alignment_tokens=self.lcm_block_size, ) curr_hit_length = len(hit_blocks[0]) * spec.block_size for group_id, blocks in zip(group_ids, hit_blocks): hit_blocks_by_group[group_id] = blocks if curr_hit_length >= hit_length: break hit_length = curr_hit_length # Simple hybrid: exit after one iteration if is_simple_hybrid: break # Truncate full attention blocks to final hit_length (if present) spec, group_ids, _ = self.attention_groups[0] if isinstance(spec, FullAttentionSpec): num_blocks = hit_length // spec.block_size for group_id in group_ids: if (blks := hit_blocks_by_group[group_id]) is not None: del blks[num_blocks:] return tuple( blocks if blocks is not None else [] for blocks in hit_blocks_by_group ), hit_length def get_kv_cache_coordinator( kv_cache_config: KVCacheConfig, max_model_len: int, use_eagle: bool, enable_caching: bool, enable_kv_cache_events: bool, dcp_world_size: int, pcp_world_size: int, hash_block_size: int, metrics_collector: KVCacheMetricsCollector | None = None, ) -> KVCacheCoordinator: if not enable_caching: return KVCacheCoordinatorNoPrefixCache( kv_cache_config, max_model_len, use_eagle, enable_kv_cache_events, dcp_world_size=dcp_world_size, pcp_world_size=pcp_world_size, hash_block_size=hash_block_size, metrics_collector=metrics_collector, ) if len(kv_cache_config.kv_cache_groups) == 1: return UnitaryKVCacheCoordinator( kv_cache_config, max_model_len, use_eagle, enable_caching, enable_kv_cache_events, dcp_world_size=dcp_world_size, pcp_world_size=pcp_world_size, hash_block_size=hash_block_size, metrics_collector=metrics_collector, ) return HybridKVCacheCoordinator( kv_cache_config, max_model_len, use_eagle, enable_caching, enable_kv_cache_events, dcp_world_size=dcp_world_size, pcp_world_size=pcp_world_size, hash_block_size=hash_block_size, metrics_collector=metrics_collector, )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/core/kv_cache_coordinator.py", "license": "Apache License 2.0", "lines": 532, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/nemotron_h.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Adapted from https://github.com/vllm-project/vllm/blob/94d8ec8d2bcb4ec55e33022b313c7e978edf05e1/vllm/model_executor/models/bamba.py # Copyright 2024 HuggingFace Inc. team. All rights reserved. # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Inference-only NemotronH model.""" import typing from collections.abc import Callable, Iterable from itertools import islice import torch from torch import nn from vllm.compilation.decorators import support_torch_compile from vllm.config import CacheConfig, ModelConfig, VllmConfig from vllm.config.parallel import ParallelConfig from vllm.distributed import get_ep_group, get_tensor_model_parallel_world_size from vllm.distributed.communication_op import tensor_model_parallel_all_gather from vllm.distributed.parallel_state import get_pp_group from vllm.model_executor.layers.activation import ReLUSquaredActivation from vllm.model_executor.layers.attention import Attention from vllm.model_executor.layers.fused_moe import ( GateLinear, SharedFusedMoE, activation_without_mul, ) from vllm.model_executor.layers.layernorm import RMSNorm from vllm.model_executor.layers.linear import ( ColumnParallelLinear, QKVParallelLinear, ReplicatedLinear, RowParallelLinear, ) from vllm.model_executor.layers.logits_processor import LogitsProcessor from vllm.model_executor.layers.mamba.mamba_mixer2 import MambaMixer2 from vllm.model_executor.layers.mamba.mamba_utils import ( MambaStateCopyFunc, MambaStateCopyFuncCalculator, MambaStateDtypeCalculator, MambaStateShapeCalculator, ) from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.layers.vocab_parallel_embedding import ( ParallelLMHead, VocabParallelEmbedding, ) from vllm.model_executor.model_loader.weight_utils import ( default_weight_loader, maybe_remap_kv_scale_name, ) from vllm.model_executor.models.interfaces import ( HasInnerState, IsHybrid, MixtureOfExperts, SupportsLoRA, SupportsMambaPrefixCaching, SupportsPP, SupportsQuant, ) from vllm.model_executor.models.utils import ( AutoWeightsLoader, WeightsMapper, is_pp_missing_parameter, make_empty_intermediate_tensors_factory, make_layers, maybe_prefix, sequence_parallel_chunk, ) from vllm.sequence import IntermediateTensors from vllm.transformers_utils.configs import NemotronHConfig class NemotronHMLP(nn.Module): def __init__( self, config: NemotronHConfig, hidden_size: int, intermediate_size: int, quant_config: QuantizationConfig | None = None, bias: bool = False, reduce_results: bool = True, is_sequence_parallel: bool = False, prefix: str = "", ) -> None: super().__init__() self.up_proj = ColumnParallelLinear( input_size=hidden_size, output_size=intermediate_size, bias=bias, quant_config=quant_config, disable_tp=is_sequence_parallel, prefix=f"{prefix}.up_proj", ) self.down_proj = RowParallelLinear( input_size=intermediate_size, output_size=hidden_size, bias=bias, quant_config=quant_config, reduce_results=reduce_results, disable_tp=is_sequence_parallel, prefix=f"{prefix}.down_proj", ) self.act_fn = ReLUSquaredActivation() def forward(self, x: torch.Tensor): x, _ = self.up_proj(x) x = self.act_fn(x) x, _ = self.down_proj(x) return x class NemotronHMoE(nn.Module): def __init__( self, config: NemotronHConfig, quant_config: QuantizationConfig | None = None, parallel_config: ParallelConfig | None = None, prefix: str = "", ): super().__init__() self.tp_size = get_tensor_model_parallel_world_size() self.routed_scaling_factor = config.routed_scaling_factor self.ep_group = get_ep_group().device_group self.ep_rank = self.ep_group.rank() self.ep_size = self.ep_group.size() self.n_routed_experts: int = config.n_routed_experts self.n_shared_experts: int = config.n_shared_experts self.use_latent_moe: bool = getattr(config, "moe_latent_size", None) is not None self.moe_hidden_size: int = ( config.moe_latent_size if self.use_latent_moe else config.hidden_size ) self.is_sequence_parallel = parallel_config.use_sequence_parallel_moe self.gate = GateLinear( config.hidden_size, config.n_routed_experts, out_dtype=torch.float32, force_fp32_compute=True, prefix=f"{prefix}.gate", ) self.gate.e_score_correction_bias = nn.Parameter( torch.empty(config.n_routed_experts, dtype=torch.float32) ) # Load balancing settings. self.enable_eplb = parallel_config.enable_eplb self.n_redundant_experts = parallel_config.eplb_config.num_redundant_experts # noqa: E501 self.n_logical_experts = self.n_routed_experts self.n_physical_experts = self.n_logical_experts + self.n_redundant_experts self.n_local_physical_experts = self.n_physical_experts // self.ep_size self.physical_expert_start = self.ep_rank * self.n_local_physical_experts self.physical_expert_end = ( self.physical_expert_start + self.n_local_physical_experts ) if config.n_shared_experts is None or config.n_shared_experts == 0: self.shared_experts = None else: intermediate_size = ( config.moe_shared_expert_intermediate_size * config.n_shared_experts ) self.shared_experts = NemotronHMLP( config=config, hidden_size=config.hidden_size, intermediate_size=intermediate_size, quant_config=quant_config, reduce_results=False, is_sequence_parallel=self.is_sequence_parallel, prefix=f"{prefix}.shared_experts", ) if self.use_latent_moe: self.fc1_latent_proj = ReplicatedLinear( input_size=config.hidden_size, output_size=self.moe_hidden_size, bias=config.mlp_bias, quant_config=quant_config, disable_tp=self.is_sequence_parallel, prefix=f"{prefix}.fc1_latent_proj", ) self.fc2_latent_proj = ReplicatedLinear( input_size=self.moe_hidden_size, output_size=config.hidden_size, bias=config.mlp_bias, quant_config=quant_config, disable_tp=self.is_sequence_parallel, prefix=f"{prefix}.fc2_latent_proj", ) else: self.fc1_latent_proj = None self.fc2_latent_proj = None self.experts = SharedFusedMoE( shared_experts=self.shared_experts, num_experts=config.n_routed_experts, top_k=config.num_experts_per_tok, hidden_size=self.moe_hidden_size, intermediate_size=config.moe_intermediate_size, reduce_results=False, renormalize=config.norm_topk_prob, quant_config=quant_config, use_grouped_topk=True, num_expert_group=config.n_group, topk_group=config.topk_group, prefix=f"{prefix}.experts", scoring_func="sigmoid", e_score_correction_bias=self.gate.e_score_correction_bias, activation=activation_without_mul(config.mlp_hidden_act), is_act_and_mul=False, # non-gated MoE enable_eplb=self.enable_eplb, num_redundant_experts=self.n_redundant_experts, is_sequence_parallel=self.is_sequence_parallel, routed_input_transform=self.fc1_latent_proj, ) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: num_tokens, hidden_dim = hidden_states.shape hidden_states = hidden_states.view(-1, hidden_dim) if self.is_sequence_parallel: hidden_states = sequence_parallel_chunk(hidden_states) # router_logits: (num_tokens, n_experts) router_logits, _ = self.gate(hidden_states) # SharedFusedMoE handles: # - shared experts (with original hidden_states) # - routed_input_transform (fc1_latent_proj) for latent MoE # - multistream parallelism between shared and routed experts shared_output, final_hidden_states = self.experts( hidden_states=hidden_states, router_logits=router_logits ) # Fix FP16 overflow # See DeepseekV2DecoderLayer for more details. if hidden_states.dtype != torch.float16: final_hidden_states *= self.routed_scaling_factor elif self.shared_experts is not None: shared_output *= 1.0 / self.routed_scaling_factor # TODO: See SharedFusedMoE.apply_routed_input_transform # for bandwidth optimization if self.use_latent_moe: final_hidden_states, _ = self.fc2_latent_proj(final_hidden_states) if self.shared_experts is not None: final_hidden_states += shared_output if self.is_sequence_parallel: final_hidden_states = tensor_model_parallel_all_gather( final_hidden_states, 0 ) final_hidden_states = final_hidden_states[:num_tokens] elif self.tp_size > 1: final_hidden_states = self.experts.maybe_all_reduce_tensor_model_parallel( final_hidden_states ) return final_hidden_states.view(num_tokens, hidden_dim) class NemotronHMLPDecoderLayer(nn.Module): def __init__( self, config: NemotronHConfig, layer_idx: int, model_config: ModelConfig | None = None, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, parallel_config: ParallelConfig | None = None, prefix: str = "", ) -> None: super().__init__() self.config = config hybrid_override_pattern = config.hybrid_override_pattern mlp_index = hybrid_override_pattern[: layer_idx + 1].count("-") - 1 # Get per-layer config for heterogeneous models if exist get_layer_config = getattr(config, "get_nemotron_h_config_for_layer", None) layer_config = get_layer_config(layer_idx) if get_layer_config else config config = layer_config if isinstance(config.intermediate_size, list): if len(config.intermediate_size) == 1: intermediate_size = config.intermediate_size[0] else: intermediate_size = config.intermediate_size[mlp_index] else: intermediate_size = config.intermediate_size self.mixer = NemotronHMLP( config, hidden_size=config.hidden_size, intermediate_size=intermediate_size, quant_config=quant_config, bias=config.mlp_bias, prefix=f"{prefix}.mixer", ) self.norm = RMSNorm(config.hidden_size, eps=config.layer_norm_epsilon) def forward( self, hidden_states: torch.Tensor, residual: torch.Tensor | None, **kwargs, ): if residual is None: residual = hidden_states hidden_states = self.norm(hidden_states) else: hidden_states, residual = self.norm(hidden_states, residual) hidden_states = self.mixer(hidden_states) return hidden_states, residual class NemotronHMoEDecoderLayer(nn.Module): def __init__( self, config: NemotronHConfig, layer_idx: int, model_config: ModelConfig | None = None, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, parallel_config: ParallelConfig | None = None, prefix: str = "", ) -> None: super().__init__() self.config = config # Get per-layer config for heterogeneous models if exsist get_layer_config = getattr(config, "get_nemotron_h_config_for_layer", None) layer_config = get_layer_config(layer_idx) if get_layer_config else config self.mixer = NemotronHMoE( layer_config, quant_config=quant_config, parallel_config=parallel_config, prefix=f"{prefix}.mixer", ) self.norm = RMSNorm(config.hidden_size, eps=config.layer_norm_epsilon) def forward( self, hidden_states: torch.Tensor, residual: torch.Tensor | None, **kwargs, ): if residual is None: residual = hidden_states hidden_states = self.norm(hidden_states) else: hidden_states, residual = self.norm(hidden_states, residual) hidden_states = self.mixer(hidden_states) return hidden_states, residual class NemotronHMambaDecoderLayer(nn.Module): def __init__( self, config: NemotronHConfig, layer_idx: int, model_config: ModelConfig | None = None, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, parallel_config: ParallelConfig | None = None, prefix: str = "", ) -> None: super().__init__() self.config = config self.mixer = MambaMixer2( hidden_size=config.hidden_size, ssm_state_size=config.ssm_state_size, conv_kernel_size=config.conv_kernel, intermediate_size=config.mamba_num_heads * config.mamba_head_dim, use_conv_bias=config.use_conv_bias, use_bias=config.use_bias, n_groups=config.n_groups, num_heads=config.mamba_num_heads, head_dim=config.mamba_head_dim, rms_norm_eps=config.layer_norm_epsilon, activation=config.mamba_hidden_act, model_config=model_config, cache_config=cache_config, quant_config=quant_config, prefix=f"{prefix}.mixer", ) self.norm = RMSNorm(config.hidden_size, eps=config.layer_norm_epsilon) def forward( self, hidden_states: torch.Tensor, residual: torch.Tensor | None, **kwargs, ): if residual is None: residual = hidden_states hidden_states = self.norm(hidden_states) else: hidden_states, residual = self.norm(hidden_states, residual) output = self.mixer(hidden_states) return output, residual class NemotronHAttention(nn.Module): def __init__( self, config: NemotronHConfig, layer_idx: int, model_config: ModelConfig | None = None, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, prefix: str = "", ) -> None: super().__init__() self.hidden_size = config.hidden_size tp_size = get_tensor_model_parallel_world_size() self.total_num_heads = config.num_attention_heads assert self.total_num_heads % tp_size == 0 self.num_heads = self.total_num_heads // tp_size self.total_num_kv_heads = config.num_key_value_heads if self.total_num_kv_heads >= tp_size: # Number of KV heads is greater than TP size, so we partition # the KV heads across multiple tensor parallel GPUs. assert self.total_num_kv_heads % tp_size == 0 else: # Number of KV heads is less than TP size, so we replicate # the KV heads across multiple tensor parallel GPUs. assert tp_size % self.total_num_kv_heads == 0 self.num_kv_heads = max(1, self.total_num_kv_heads // tp_size) if hasattr(config, "head_dim") and config.head_dim is not None: self.head_dim = config.head_dim else: self.head_dim = config.hidden_size // self.total_num_heads self.q_size = self.num_heads * self.head_dim self.kv_size = self.num_kv_heads * self.head_dim self.scaling = self.head_dim**-0.5 self.qkv_proj = QKVParallelLinear( config.hidden_size, self.head_dim, self.total_num_heads, self.total_num_kv_heads, bias=False, quant_config=quant_config, prefix=f"{prefix}.qkv_proj", ) self.o_proj = RowParallelLinear( self.total_num_heads * self.head_dim, config.hidden_size, bias=False, quant_config=quant_config, prefix=f"{prefix}.o_proj", ) # Get per-layer sliding window from config (for heterogeneous models) sliding_window = getattr(config, "sliding_window", None) self.attn = Attention( self.num_heads, self.head_dim, self.scaling, num_kv_heads=self.num_kv_heads, cache_config=cache_config, quant_config=quant_config, prefix=f"{prefix}.attn", per_layer_sliding_window=sliding_window, ) def forward( self, hidden_states: torch.Tensor, **kwargs, ) -> torch.Tensor: qkv, _ = self.qkv_proj(hidden_states) q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1) attn_output = self.attn(q, k, v) output, _ = self.o_proj(attn_output) return output class NemotronHAttentionDecoderLayer(nn.Module): def __init__( self, config: NemotronHConfig, layer_idx: int, model_config: ModelConfig | None = None, cache_config: CacheConfig | None = None, quant_config: QuantizationConfig | None = None, parallel_config: ParallelConfig | None = None, prefix: str = "", ) -> None: super().__init__() # Get per-layer config for heterogeneous models if exsist get_layer_config = getattr(config, "get_nemotron_h_config_for_layer", None) layer_config = get_layer_config(layer_idx) if get_layer_config else config self.mixer = NemotronHAttention( layer_config, layer_idx, model_config, cache_config, quant_config, prefix=f"{prefix}.mixer", ) self.norm = RMSNorm(config.hidden_size, eps=config.layer_norm_epsilon) def forward( self, positions: torch.Tensor, hidden_states: torch.Tensor, residual: torch.Tensor | None, **kwargs, ): if residual is None: residual = hidden_states hidden_states = self.norm(hidden_states) else: hidden_states, residual = self.norm(hidden_states, residual) hidden_states = self.mixer(hidden_states=hidden_states) return hidden_states, residual ALL_DECODER_LAYER_TYPES = { "M": NemotronHMambaDecoderLayer, "-": NemotronHMLPDecoderLayer, "*": NemotronHAttentionDecoderLayer, "E": NemotronHMoEDecoderLayer, } @support_torch_compile class NemotronHModel(nn.Module): def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): super().__init__() config: NemotronHConfig = vllm_config.model_config.hf_config model_config = vllm_config.model_config cache_config = vllm_config.cache_config quant_config = vllm_config.quant_config parallel_config = vllm_config.parallel_config self.config = config self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding( self.vocab_size, config.hidden_size, ) self.has_moe = "E" in config.hybrid_override_pattern def get_layer(prefix: str): layer_idx = int(prefix.rsplit(".", 1)[1]) layer_class = ALL_DECODER_LAYER_TYPES[ config.hybrid_override_pattern[layer_idx] ] return layer_class( config=config, layer_idx=layer_idx, model_config=model_config, cache_config=cache_config, quant_config=quant_config, parallel_config=parallel_config, prefix=prefix, ) self.start_layer, self.end_layer, self.layers = make_layers( len(config.hybrid_override_pattern), get_layer, prefix=f"{prefix}.layers" ) self.make_empty_intermediate_tensors = make_empty_intermediate_tensors_factory( ["hidden_states", "residual"], config.hidden_size ) self.norm_f = RMSNorm(config.hidden_size, eps=config.layer_norm_epsilon) def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.embed_tokens(input_ids) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, ) -> torch.Tensor | IntermediateTensors: if get_pp_group().is_first_rank: if inputs_embeds is not None: hidden_states = inputs_embeds else: hidden_states = self.embed_input_ids(input_ids) residual = None else: assert intermediate_tensors is not None hidden_states = intermediate_tensors["hidden_states"] residual = intermediate_tensors["residual"] for layer in islice(self.layers, self.start_layer, self.end_layer): hidden_states, residual = layer( positions=positions, hidden_states=hidden_states, residual=residual, ) if not get_pp_group().is_last_rank: return IntermediateTensors( {"hidden_states": hidden_states, "residual": residual} ) hidden_states, _ = self.norm_f(hidden_states, residual) return hidden_states def is_spec_layer(self, config: NemotronHConfig, weight_name: str) -> bool: return weight_name.startswith("mtp.") def _get_max_n_routed_experts(self) -> int: """Get max n_routed_experts from config or block_configs for puzzle models. For heterogeneous models with varying expert counts per layer, returns the MAX to ensure all expert weights can be loaded. """ # First try top-level attribute n_routed_experts = getattr(self.config, "n_routed_experts", None) if n_routed_experts is not None: return n_routed_experts # For puzzle models, get MAX from all MoE blocks in block_configs # (different layers may have different expert counts) max_experts = 0 block_configs = getattr(self.config, "block_configs", None) if block_configs: for block in block_configs: if isinstance(block, dict): if block.get("block_type") == "moe": max_experts = max(max_experts, block.get("n_routed_experts", 0)) else: # HF converts dicts to objects with attributes if getattr(block, "block_type", "") == "moe": max_experts = max( max_experts, getattr(block, "n_routed_experts", 0) ) return max_experts def get_expert_mapping(self) -> list[tuple[str, str, int, str]]: if self.has_moe: # (param_name, weight_name, expert_id, shard_id) expert_params_mapping = SharedFusedMoE.make_expert_params_mapping( # - FusedMoe.w1 (aka gate_proj) should be up_proj since that's # what the activation is applied to # - FusedMoe.w3 (aka up_proj) should be ignored since we're # using non-gated MoE self, ckpt_gate_proj_name="up_proj", ckpt_down_proj_name="down_proj", ckpt_up_proj_name="", num_experts=self._get_max_n_routed_experts(), num_redundant_experts=getattr(self, "num_redundant_experts", 0), ) return expert_params_mapping return [] def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: stacked_params_mapping = [ # (param_name, shard_name, shard_id) ("qkv_proj", "q_proj", "q"), ("qkv_proj", "k_proj", "k"), ("qkv_proj", "v_proj", "v"), ] expert_params_mapping = self.get_expert_mapping() params_dict = dict(self.named_parameters()) loaded_params: set[str] = set() for name, loaded_weight in weights: if "scale" in name or "zero_point" in name: # Remapping the name of FP8 kv-scale. name = maybe_remap_kv_scale_name(name, params_dict) if name is None: continue # Skip MTP/spec decode layers early (before stacked params mapping) if name.startswith("mtp."): continue # load stacked params for param_name, weight_name, shard_id in stacked_params_mapping: if weight_name not in name: continue name = name.replace(weight_name, param_name) # Skip loading extra bias for GPTQ models. if name.endswith(".bias") and name not in params_dict: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = param.weight_loader weight_loader(param, loaded_weight, shard_id) break # load other params else: is_expert_weight = False for mapping in expert_params_mapping: param_name, weight_name, expert_id, shard_id = mapping if weight_name not in name: continue # Anyway, this is an expert weight and should not be # attempted to load as other weights later is_expert_weight = True # Do not modify `name` since the loop may continue here # Instead, create a new variable name_mapped = name.replace(weight_name, param_name) if is_pp_missing_parameter(name_mapped, self): continue param = params_dict[name_mapped] # We should ask the weight loader to return success or not # here since otherwise we may skip experts with other # available replicas. weight_loader = typing.cast( Callable[..., bool], param.weight_loader ) success = weight_loader( param, loaded_weight, name_mapped, shard_id=shard_id, expert_id=expert_id, return_success=True, ) if success: name = name_mapped break else: if is_expert_weight: continue if is_pp_missing_parameter(name, self): continue param = params_dict[name] weight_loader = getattr( param, "weight_loader", default_weight_loader ) weight_loader(param, loaded_weight) loaded_params.add(name) return loaded_params class NemotronHForCausalLM( nn.Module, HasInnerState, SupportsLoRA, SupportsPP, IsHybrid, SupportsQuant, MixtureOfExperts, SupportsMambaPrefixCaching, ): # Relevant only if self.has_moe is True is_non_gated_moe: bool = True hf_to_vllm_mapper = WeightsMapper( orig_to_new_prefix={"backbone": "model"}, orig_to_new_substr={"A_log": "A", "embeddings": "embed_tokens"}, ) packed_modules_mapping = { "qkv_proj": [ "q_proj", "k_proj", "v_proj", ], } # LoRA specific attributes embedding_modules = { "embed_tokens": "input_embeddings", "lm_head": "output_embeddings", } # Skip MTP (Multi-Token Prediction) layers during LoRA loading lora_skip_prefixes = ["mtp."] @classmethod def get_mamba_state_dtype_from_config( cls, vllm_config: "VllmConfig", ) -> tuple[torch.dtype, torch.dtype]: return MambaStateDtypeCalculator.mamba2_state_dtype( vllm_config.model_config.dtype, vllm_config.cache_config.mamba_cache_dtype, vllm_config.cache_config.mamba_ssm_cache_dtype, ) @classmethod def get_mamba_state_shape_from_config( cls, vllm_config: "VllmConfig", ) -> tuple[tuple[int, int], tuple[int, int, int]]: """Calculate shapes for Mamba's convolutional and state caches. Args: vllm_config: vLLM config Returns: Tuple containing: - conv_state_shape: Shape for convolutional state cache - temporal_state_shape: Shape for state space model cache """ parallel_config = vllm_config.parallel_config hf_config = vllm_config.model_config.hf_config intermediate_size = hf_config.mamba_num_heads * hf_config.mamba_head_dim return MambaStateShapeCalculator.mamba2_state_shape( intermediate_size=intermediate_size, tp_world_size=parallel_config.tensor_parallel_size, n_groups=hf_config.n_groups, num_heads=hf_config.mamba_num_heads, head_dim=hf_config.mamba_head_dim, state_size=hf_config.ssm_state_size, conv_kernel=hf_config.conv_kernel, num_spec=vllm_config.num_speculative_tokens, ) @classmethod def get_mamba_state_copy_func(cls) -> tuple[MambaStateCopyFunc, MambaStateCopyFunc]: return MambaStateCopyFuncCalculator.mamba2_state_copy_func() def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""): config = vllm_config.model_config.hf_config self.vllm_config = vllm_config self.model_config = vllm_config.model_config scheduler_config = vllm_config.scheduler_config self.quant_config = vllm_config.quant_config super().__init__() self.config = config self.scheduler_config = scheduler_config self.model = NemotronHModel( vllm_config=vllm_config, prefix=maybe_prefix(prefix, "model") ) self.lm_head = ParallelLMHead( config.vocab_size, config.hidden_size, prefix=maybe_prefix(prefix, "lm_head"), ) self.logits_processor = LogitsProcessor(config.vocab_size) self.make_empty_intermediate_tensors = ( self.model.make_empty_intermediate_tensors ) # Set MoE hyperparameters if self.model.has_moe: self.expert_weights = [] self.num_expert_groups = config.n_group self.moe_layers = [] example_moe = None for layer in self.model.layers: if isinstance(layer, NemotronHMoEDecoderLayer): # Pick last one layer since the first ones # may be dense layers. example_moe = layer.mixer self.moe_layers.append(layer.mixer.experts) self.num_moe_layers = len(self.moe_layers) self.num_logical_experts = example_moe.n_logical_experts self.num_physical_experts = example_moe.n_physical_experts self.num_local_physical_experts = example_moe.n_local_physical_experts # noqa: E501 self.num_routed_experts = example_moe.n_routed_experts self.num_shared_experts = example_moe.n_shared_experts self.num_redundant_experts = example_moe.n_redundant_experts def update_physical_experts_metadata( self, num_physical_experts: int, num_local_physical_experts: int, ) -> None: assert self.num_local_physical_experts == num_local_physical_experts self.num_physical_experts = num_physical_experts self.num_local_physical_experts = num_local_physical_experts self.num_redundant_experts = num_physical_experts - self.num_logical_experts for layer in self.model.layers: if isinstance(layer, NemotronHMoEDecoderLayer): moe = layer.mixer moe.n_local_physical_experts = num_local_physical_experts moe.n_physical_experts = num_physical_experts moe.n_redundant_experts = self.num_redundant_experts moe.experts.update_expert_map() def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor: return self.model.embed_input_ids(input_ids) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, **kwargs, ): hidden_states = self.model( input_ids, positions, intermediate_tensors, inputs_embeds ) return hidden_states def compute_logits( self, hidden_states: torch.Tensor, ) -> torch.Tensor | None: logits = self.logits_processor(self.lm_head, hidden_states) return logits def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: loader = AutoWeightsLoader(self, skip_prefixes=["mtp"]) return loader.load_weights(weights, mapper=self.hf_to_vllm_mapper)
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/nemotron_h.py", "license": "Apache License 2.0", "lines": 831, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/transformers_utils/configs/nemotron_h.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project # Copyright 2024 HuggingFace Inc. team. All rights reserved. # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """NemotronH model configuration""" import regex as re from transformers.configuration_utils import PretrainedConfig from transformers.utils import logging logger = logging.get_logger(__name__) class NemotronHConfig(PretrainedConfig): r""" This is the configuration class to store the configuration of a [`NemotronHModel`]. It is used to instantiate a NemotronH model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the NemotronH-v0.1 model. Args: vocab_size (`int`, *optional*, defaults to 131072): Vocabulary size of the NemotronH model. Defines the number of different tokens that can be represented by the `inputs_ids` passed when calling [`NemotronHModel`] tie_word_embeddings (`bool`, *optional*, defaults to `False`): Whether the model's input and output word embeddings should be tied. Note that this is only relevant if the model has an output word embedding layer. hidden_size (`int`, *optional*, defaults to 4096): Dimension of the hidden representations. intermediate_size (`int`, *optional*, defaults to 21504): Dimension of the MLP representations. num_hidden_layers (`int`, *optional*, defaults to 52): Number of hidden layers in the Transformer encoder. hybrid_override_pattern (`str`, *optional*, defaults to `"M-M-M-M*-M-M-M-M-M*-M-M-M-M-M*-M-M-M-M-M*-M-M-M-M-M-"`): The pattern of the hybrid model. The pattern is a string of characters where each character represents M: Mamba2, *: Attention, -: MLP mtp_hybrid_override_pattern (`str`, *optional*, defaults to `"*E"`): The pattern of the MTP layers. num_attention_heads (`int`, *optional*, defaults to 32): Number of attention heads for each attention layer in the Transformer encoder. attention_head_dim (`int`, *optional*, defaults to 128): Dimension of each attention head. num_key_value_heads (`int`, *optional*, defaults to 8): This is the number of key_value heads that should be used to implement Grouped Query Attention. If `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. mlp_hidden_act (`str`, *optional*, defaults to "relu2"): The non-linear activation function in the MLP layers. attention_bias (`bool`, *optional*, defaults to `False`): Whether to use bias in attention layers. mlp_bias (`bool`, *optional*, defaults to `False`): Whether to use bias in MLP layers. use_bias (`bool`, *optional*, defaults to `False`): Whether to use bias in the model. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_epsilon (`float`, *optional*, defaults to 1e-5): The epsilon used by the layer normalization layers. residual_in_fp32 (`bool`, *optional*, defaults to `False`): Whether or not residuals should be in `float32`. If set to `False` residuals will keep the same `dtype` as the rest of the model. use_cache (`bool`, *optional*, defaults to `True`): Whether or not the model should return the last key/values attentions (not used by all models). Only relevant if `config.is_decoder=True`. num_logits_to_keep (`int` or `None`, *optional*, defaults to 1): Number of prompt logits to calculate during generation. If `None`, all logits will be calculated. If an integer value, only last `num_logits_to_keep` logits will be calculated. pad_token_id (`int`, *optional*, defaults to 0): The id of the padding token. bos_token_id (`int`, *optional*, defaults to 1): The id of the "beginning-of-sequence" token. eos_token_id (`int`, *optional*, defaults to 2): The id of the "end-of-sequence" token. sliding_window (`int`, *optional*, defaults to None): Sliding window attention window size. max_position_embeddings (`int`, *optional*, defaults to 4096): The maximum sequence length that this model might ever be used with. attention_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for the attention probabilities. hidden_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for the hidden states. use_mamba_kernels (`bool`, *optional*, defaults to `True`): Flag indicating whether or not to use the fast mamba kernels. These are available only if `mamba-ssm` and `causal-conv1d` are installed, and the mamba modules are running on a CUDA device. ssm_state_size (`int`, *optional*, defaults to 128): The dimension of the mamba state space latents. mamba_num_heads (`int`, *optional*, defaults to 128): Number of heads in Mamba layers. mamba_n_groups (`int`, *optional*, defaults to 8): Number of groups in Mamba layers. mamba_head_dim (`int`, *optional*, defaults to 64): Dimension of each Mamba head. mamba_d_conv (`int`, *optional*, defaults to 4): The size of the mamba convolution kernel. mamba_expand (`int`, *optional*, defaults to 2): Expanding factor used to determine the mamba intermediate size. mamba_hidden_act (`str`, *optional*, defaults to "silu"): The non-linear activation function in the Mamba layers. mamba_dt_min (`float`, *optional*, defaults to 0.001): Minimum value for the time step in Mamba. mamba_dt_max (`float`, *optional*, defaults to 0.1): Maximum value for the time step in Mamba. mamba_dt_limit (`tuple`, *optional*, defaults to (0.0, float("inf"))): Limits for the time step in Mamba. mamba_dt_init_floor (`float`, *optional*, defaults to 1e-4): Floor value for time step initialization in Mamba. mamba_conv_bias (`bool`, *optional*, defaults to `True`): Whether to use bias in the convolution layer of the mamba mixer block. mamba_proj_bias (`bool`, *optional*, defaults to `False`): Whether to use bias in the input and output projections of the mamba mixer block. mamba_chunk_size (`int`, *optional*, defaults to 256): Size of chunks for Mamba processing. rescale_prenorm_residual (`bool`, *optional*, defaults to `True`): Whether to rescale the pre-normalization residual connections. """ model_type = "nemotron_h" keys_to_ignore_at_inference = ["past_key_values"] def __init__( self, vocab_size=131072, tie_word_embeddings=False, hidden_size=4096, intermediate_size=21504, num_hidden_layers=52, hybrid_override_pattern="M-M-M-M*-M-M-M-M-M*-M-M-M-M-M*-M-M-M-M-M*-M-M-M-M-M-", mtp_hybrid_override_pattern="*E", num_attention_heads=32, head_dim=128, num_key_value_heads=8, # nemo: num_query_groups mlp_hidden_act="relu2", attention_bias=False, mlp_bias=False, use_bias=False, initializer_range=0.02, # nemo: init_method_std layer_norm_epsilon=1e-5, # nemo: layernorm_epsilon residual_in_fp32=False, # Megatron Core default value use_cache=True, num_logits_to_keep=1, pad_token_id=0, bos_token_id=1, eos_token_id=2, sliding_window=None, max_position_embeddings=4096, attention_dropout=0.0, hidden_dropout=0.0, # * ADDED use_mamba_kernels=True, ssm_state_size=128, # mamba_state_size mamba_num_heads=128, mamba_n_groups=8, # nemo: mamba_ssm_ngroups = num_heads mamba_head_dim=64, mamba_d_conv=4, mamba_expand=2, mamba_hidden_act="silu", mamba_dt_min=0.001, mamba_dt_max=0.1, mamba_dt_limit=(0.0, float("inf")), mamba_dt_init_floor=1e-4, mamba_conv_bias=True, mamba_proj_bias=False, mamba_chunk_size=256, rescale_prenorm_residual=True, n_routed_experts=8, n_shared_experts=1, moe_intermediate_size=7688, moe_shared_expert_intermediate_size=7688, moe_latent_size=None, num_experts_per_tok=2, routed_scaling_factor=1.0, n_group=1, topk_group=1, norm_topk_prob=True, **kwargs, ): self.vocab_size = vocab_size self.tie_word_embeddings = tie_word_embeddings self.hidden_size = hidden_size self.intermediate_size = intermediate_size self.num_hidden_layers = num_hidden_layers self.hybrid_override_pattern = hybrid_override_pattern self.mtp_hybrid_override_pattern = mtp_hybrid_override_pattern self.num_attention_heads = num_attention_heads self.head_dim = head_dim self.sliding_window = sliding_window self.max_position_embeddings = max_position_embeddings self.attention_dropout = attention_dropout self.hidden_dropout = hidden_dropout # Validate hybrid_override_pattern # M: Mamba2, *: Attention, -: MLP assert len(self.hybrid_override_pattern) == self.num_hidden_layers, ( "hybrid_override_pattern must have same length as num_hidden_layers" ) assert re.match(r"^[*-ME]+$", self.hybrid_override_pattern), ( "hybrid_override_pattern must only contain characters 'M', '*', '-', or 'E'" ) # for backward compatibility if num_key_value_heads is None: num_key_value_heads = num_attention_heads self.num_key_value_heads = num_key_value_heads self.mlp_hidden_act = mlp_hidden_act self.attention_bias = attention_bias self.mlp_bias = mlp_bias self.use_bias = use_bias self.initializer_range = initializer_range self.layer_norm_epsilon = layer_norm_epsilon self.residual_in_fp32 = residual_in_fp32 self.use_cache = use_cache self.num_logits_to_keep = num_logits_to_keep self.use_mamba_kernels = use_mamba_kernels self.n_groups = mamba_n_groups self.mamba_head_dim = mamba_head_dim self.ssm_state_size = ssm_state_size self.mamba_num_heads = mamba_num_heads self.conv_kernel = mamba_d_conv self.expand = mamba_expand self.mamba_hidden_act = mamba_hidden_act self.time_step_min = mamba_dt_min self.time_step_max = mamba_dt_max self.time_step_limit = mamba_dt_limit self.time_step_floor = mamba_dt_init_floor self.use_conv_bias = mamba_conv_bias self.mamba_proj_bias = mamba_proj_bias self.chunk_size = mamba_chunk_size self.rescale_prenorm_residual = rescale_prenorm_residual self.n_routed_experts = n_routed_experts self.n_shared_experts = n_shared_experts self.moe_intermediate_size = moe_intermediate_size self.moe_shared_expert_intermediate_size = moe_shared_expert_intermediate_size # noqa: E501 self.moe_latent_size = moe_latent_size self.num_experts_per_tok = num_experts_per_tok self.routed_scaling_factor = routed_scaling_factor self.n_group = n_group self.topk_group = topk_group self.norm_topk_prob = norm_topk_prob super().__init__( pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, tie_word_embeddings=tie_word_embeddings, **kwargs, ) @property def layers_block_type(self): return [ "mamba" if self.hybrid_override_pattern[i] == "M" else "attention" if self.hybrid_override_pattern[i] == "*" else "mlp" if self.hybrid_override_pattern[i] == "-" else "moe" for i in range(self.num_hidden_layers) ]
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/transformers_utils/configs/nemotron_h.py", "license": "Apache License 2.0", "lines": 274, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/layers/fused_moe/batched_deep_gemm_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.forward_context import get_forward_context, is_forward_context_available from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.activation import MoEActivation from vllm.model_executor.layers.fused_moe.config import ( FusedMoEConfig, FusedMoEParallelConfig, FusedMoEQuantConfig, ) from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceDelegate, ) from vllm.model_executor.layers.fused_moe.utils import _resize_cache from vllm.model_executor.layers.quantization.utils.quant_utils import ( QuantKey, kFp8Dynamic128Sym, kFp8Static128BlockSym, ) from vllm.platforms import current_platform from vllm.triton_utils import tl, triton from vllm.utils.deep_gemm import ( DeepGemmQuantScaleFMT, fp8_m_grouped_gemm_nt_masked, get_mk_alignment_for_contiguous_layout, is_deep_gemm_e8m0_used, is_deep_gemm_supported, ) from vllm.utils.math_utils import cdiv, round_up logger = init_logger(__name__) def scales_shape_stride_dtype( E: int, T: int, G: int, quant_scale_fmt: DeepGemmQuantScaleFMT ) -> tuple[tuple[int, ...], tuple[int, ...], torch.dtype]: shape = (E, T, G) strides = (T * G, 1, T) if quant_scale_fmt in [ DeepGemmQuantScaleFMT.FLOAT32, DeepGemmQuantScaleFMT.FLOAT32_CEIL_UE8M0, ]: return shape, strides, torch.float32 assert quant_scale_fmt == DeepGemmQuantScaleFMT.UE8M0 shape = (E, T, cdiv(G, 4)) strides = (T * cdiv(G, 4), 1, T) return shape, strides, torch.int32 @triton.jit def _silu_mul_fp8_quant_deep_gemm( # Pointers ------------------------------------------------------------ input_ptr, # 16-bit activations (E, T, 2*H) y_q_ptr, # fp8 quantized activations (E, T, H) y_s_ptr, # 16-bit scales (E, T, G) counts_ptr, # int32 num tokens per expert (E) # Sizes --------------------------------------------------------------- H: tl.constexpr, # hidden dimension (per output) GROUP_SIZE: tl.constexpr, # elements per group (usually 128) # Strides for input (elements) --------------------------------------- stride_i_e, stride_i_t, stride_i_h, # Strides for y_q (elements) ----------------------------------------- stride_yq_e, stride_yq_t, stride_yq_h, # Strides for y_s (elements) ----------------------------------------- stride_ys_e, stride_ys_t, stride_ys_g, # Stride for counts (elements) stride_counts_e, # Numeric params ------------------------------------------------------ eps: tl.constexpr, fp8_min: tl.constexpr, fp8_max: tl.constexpr, ceil_ue8m0: tl.constexpr, # Meta --------------------------------------------------------------- BLOCK: tl.constexpr, NUM_STAGES: tl.constexpr, ): G = H // GROUP_SIZE # map program id -> (e, g) pid = tl.program_id(0) e = pid // G g = pid % G e = e.to(tl.int64) g = g.to(tl.int64) # number of valid tokens for this expert n_tokens = tl.load(counts_ptr + e * stride_counts_e).to(tl.int64) cols = tl.arange(0, BLOCK).to(tl.int64) mask = cols < BLOCK base_input_offset = e * stride_i_e + g * GROUP_SIZE * stride_i_h base_gate_offset = base_input_offset + cols * stride_i_h base_up_offset = base_input_offset + H * stride_i_h + cols * stride_i_h base_yq_offset = e * stride_yq_e + g * GROUP_SIZE * stride_yq_h + cols * stride_yq_h base_ys_offset = e * stride_ys_e + g * stride_ys_g for t in tl.range(0, n_tokens, num_stages=NUM_STAGES): gate = tl.load( input_ptr + base_gate_offset + t * stride_i_t, mask=mask, other=0.0 ).to(tl.float32) up = tl.load(input_ptr + base_up_offset + t * stride_i_t, mask=mask, other=0.0) gate = gate * (1.0 / (1.0 + tl.exp(-gate))) y = gate * up y_s = tl.maximum(tl.max(tl.abs(y)), eps) / fp8_max if ceil_ue8m0: y_s = tl.exp2(tl.ceil(tl.log2(y_s))) y_q = tl.clamp(y / y_s, fp8_min, fp8_max).to(y_q_ptr.dtype.element_ty) tl.store(y_q_ptr + base_yq_offset + t * stride_yq_t, y_q, mask=mask) tl.store(y_s_ptr + base_ys_offset + t * stride_ys_t, y_s) def persistent_masked_m_silu_mul_quant( y: torch.Tensor, # (E, T, 2*H) tokens_per_expert: torch.Tensor, # (E,) number of valid tokens per expert num_parallel_tokens=16, group_size: int = 128, quant_scale_fmt: DeepGemmQuantScaleFMT = DeepGemmQuantScaleFMT.FLOAT32, ) -> tuple[torch.Tensor, torch.Tensor]: """Quantize silu(y[..., :H]) * y[..., H:] to FP8 with group per-token scales y has shape (E, T, 2*H). The first half of the last dimension is silu-activated, multiplied by the second half, then quantized into FP8. We launch a fixed grid of threads to accommodate CUDA graphs. Let `P2` be a parallelization factor for persistent_masked_m_silu_mul_quant over the hidden dimension. Let `expert_offsets = [0] + [num_tokens.cumsum()]` and `total_tokens = expert_offsets[-1]`. persistent_masked_m_silu_mul_quant launches `total_tokens x P2` number of thread blocks. Each thread block contains `NUM_WARPS` warps. Every thread block needs to find it's corresponding expert by warp-parallel scanning over the `expert_offsets` array. The i-th warp in the first thread block processes `[i * warp_chunk_size, (i + 1) * warp_chunk_size]` groups sequentially, where `warp_chunk_size = ((H / GROUP_SIZE) / P2) / NUM_WARPS`, pipelining loads and computes. The shared memory layout for 4 warps with a 2-stage pipeline for SiLU V2 can is visualized like so: stage0 stage1 ┌─────┬───┬─────┬───┬─────┬───┬─────┬───┬─────┬───┬─────┬───┬─────┬───┬─────┬───┐ │gate0│up0│gate1│up1│gate2│up2│gate3│up3│gate0│up0│gate1│up1│gate2│up2│gate3│up3│ └─────┴───┴─────┴───┴─────┴───┴─────┴───┴─────┴───┴─────┴───┴─────┴───┴─────┴───┘ with the main difference between V1 and V2 being the global load stride between warps, and between half-warps. Regarding the latter stride, we assign the first half warp of every warp for `gate` loads and the second half-warp to `up` loads. Returns `(y_q, y_s)` where * `y_q`: FP8 tensor, shape (E, T, H), same layout as y[..., :H] * `y_s` depends on quant_scale_fmt, - quant_scale_fmt == FLOAT32, `y_s`: FP32 tensor, shape (E, T, H // group_size), strides (T*G, 1, T) - quant_scale_fmt == E8M0, `y_s`: Int32 tensor, shape (E, T, H // group_size // 4), strides (T*G, 1, T) - quant_scale_fmt == E8M0_FLOAT32_SPARSE `y_s`: FP32 tensor, shape (E, T, H // group_size), strides (T*G, 1, T) Let NUM_WARPS be the number of warps in a single thread block and `GROUP_SIZE = 128` be the size of the quantization group. """ assert y.ndim == 3, "y must be (E, T, 2*H)" E, T, H2 = y.shape assert H2 % 2 == 0, "last dim of y must be even (2*H)" H = H2 // 2 G = (H + group_size - 1) // group_size assert H % 8 == 0, "H must be divisible by 8" assert group_size == 128, "H must be divisible by 8" assert tokens_per_expert.ndim == 1 and tokens_per_expert.shape[0] == E tokens_per_expert = tokens_per_expert.to(device=y.device, dtype=torch.int32) fp8_dtype = torch.float8_e4m3fn y_q = torch.empty((E, T, H), dtype=fp8_dtype, device=y.device) ys_shape, ys_strides, ys_dtype = scales_shape_stride_dtype(E, T, G, quant_scale_fmt) y_s = torch.empty_strided( ys_shape, ys_strides, dtype=ys_dtype, device=y.device, ) ceil_ue8m0 = quant_scale_fmt in [ DeepGemmQuantScaleFMT.FLOAT32_CEIL_UE8M0, DeepGemmQuantScaleFMT.UE8M0, ] cuda_arch = current_platform.get_device_capability( device_id=y.device.index ).to_int() if cuda_arch >= 80: torch.ops._C.persistent_masked_m_silu_mul_quant( y, tokens_per_expert, y_q, y_s, ceil_ue8m0 ) else: stride_cnt_e = tokens_per_expert.stride()[0] # Static grid over experts and H-groups. # A loop inside the kernel handles the token dim grid = (E * G,) # strides (elements) stride_i_e, stride_i_t, stride_i_h = y.stride() stride_yq_e, stride_yq_t, stride_yq_h = y_q.stride() f_info = torch.finfo(fp8_dtype) fp8_max = f_info.max fp8_min = f_info.min eps: float = 1e-10 assert y_s.dtype == torch.float32, ( "_silu_mul_fp8_quant_deep_gemm does" "not support {y_s.dtype} scales. Only torch.float32 supported." ) _silu_mul_fp8_quant_deep_gemm[grid]( y, y_q, y_s, tokens_per_expert, H, group_size, stride_i_e, stride_i_t, stride_i_h, stride_yq_e, stride_yq_t, stride_yq_h, ys_strides[0], ys_strides[1], ys_strides[2], stride_cnt_e, eps, fp8_min, fp8_max, ceil_ue8m0, BLOCK=group_size, NUM_STAGES=4, num_warps=1, ) return y_q, y_s class BatchedDeepGemmExperts(mk.FusedMoEPermuteExpertsUnpermute): def __init__( self, moe_config: FusedMoEConfig, quant_config: FusedMoEQuantConfig, max_num_tokens: int, num_dispatchers: int, ): """ max_num_tokens: Maximum number of tokens from a DP Rank num_dispatchers: The number of DP dispatchers. quant_config: Quantization configuration """ super().__init__( moe_config=moe_config, quant_config=quant_config, max_num_tokens=max_num_tokens, num_dispatchers=num_dispatchers, ) assert self.block_shape == get_mk_alignment_for_contiguous_layout() assert self.quant_config.use_fp8_w8a8 @staticmethod def activation_format() -> mk.FusedMoEActivationFormat: return mk.FusedMoEActivationFormat.BatchedExperts @staticmethod def _supports_current_device() -> bool: return is_deep_gemm_supported() @staticmethod def _supports_no_act_and_mul() -> bool: return False @staticmethod def _supports_quant_scheme( weight_key: QuantKey | None, activation_key: QuantKey | None, ) -> bool: SUPPORTED_W_A = [(kFp8Static128BlockSym, kFp8Dynamic128Sym)] return (weight_key, activation_key) in SUPPORTED_W_A @staticmethod def _supports_activation(activation: MoEActivation) -> bool: return activation == MoEActivation.SILU @staticmethod def _supports_parallel_config(moe_parallel_config: FusedMoEParallelConfig) -> bool: return True def supports_chunking(self) -> bool: return False def supports_expert_map(self) -> bool: return False def supports_packed_ue8m0_act_scales(self) -> bool: """ DeepGemm supports packed ue8m0 activation scales format in devices == sm100 """ return ( is_deep_gemm_e8m0_used() and current_platform.is_device_capability_family(100) ) def finalize_weight_and_reduce_impl(self) -> mk.TopKWeightAndReduce: # Let PrepareAndFinalize::finalize() decide the impl. return TopKWeightAndReduceDelegate() def workspace_shapes( self, M: int, N: int, K: int, topk: int, global_num_experts: int, local_num_experts: int, expert_tokens_meta: mk.ExpertTokensMetadata | None, activation: MoEActivation, ) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]: # FIXME (varun): We should be able to dispatch only from the leader # DP ranks in the case of TP > 1. At the moment, all the Ranks # end up sending their tokens. This needs to be fixed. assert self.num_dispatchers is not None assert self.max_num_tokens is not None num_dispatchers = self.num_dispatchers num_experts = local_num_experts max_num_tokens = M if self.max_num_tokens is None else self.max_num_tokens activation_out_dim = self.adjust_N_for_activation(N, activation) workspace13 = (num_experts, max_num_tokens * num_dispatchers, max(K, N)) workspace2 = (num_experts, max_num_tokens * num_dispatchers, activation_out_dim) output = (num_experts, max_num_tokens * num_dispatchers, K) return (workspace13, workspace2, output) def estimate_expected_m( self, global_num_experts: int, max_tokens_per_expert: int, topk: int ) -> int: dp_meta = ( get_forward_context().dp_metadata if is_forward_context_available() else None ) if dp_meta is None: logger.warning_once( "DPMetadata unavailable. Defaulting expected_m to " f"{max_tokens_per_expert}.", scope="local", ) return max_tokens_per_expert total_num_tokens = dp_meta.num_tokens_across_dp_cpu.sum().item() total_num_tokens_replicated = total_num_tokens * topk # Assume even load balancing assert global_num_experts != 0 estimate = round_up(int(total_num_tokens_replicated // global_num_experts), 16) # clamp estimate estimate = max(estimate, 16) estimate = min(max_tokens_per_expert, estimate) return estimate def apply( self, output: torch.Tensor, hidden_states: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, activation: MoEActivation, global_num_experts: int, expert_map: torch.Tensor | None, a1q_scale: torch.Tensor | None, a2_scale: torch.Tensor | None, workspace13: torch.Tensor, workspace2: torch.Tensor, expert_tokens_meta: mk.ExpertTokensMetadata | None, apply_router_weight_on_input: bool, ): assert expert_tokens_meta is not None expert_num_tokens = expert_tokens_meta.expert_num_tokens assert hidden_states.ndim == 3 assert self.block_shape is not None a1q = hidden_states _, N, K = w1.size() assert w2.size(1) == K E, max_num_tokens, N, K, _ = self.moe_problem_size( hidden_states, w1, w2, topk_ids ) workspace1 = _resize_cache(workspace13, (E, max_num_tokens, N)) expected_m = self.estimate_expected_m( global_num_experts=global_num_experts, max_tokens_per_expert=max_num_tokens, topk=topk_ids.size(-1), ) fp8_m_grouped_gemm_nt_masked( (a1q, a1q_scale), (w1, self.w1_scale), workspace1, expert_num_tokens, expected_m, ) quant_scale_fmt = DeepGemmQuantScaleFMT.from_oracle() a2q, a2q_scale = persistent_masked_m_silu_mul_quant( workspace1, expert_num_tokens, quant_scale_fmt=quant_scale_fmt, ) fp8_m_grouped_gemm_nt_masked( (a2q, a2q_scale), (w2, self.w2_scale), output, expert_num_tokens, expected_m, )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/fused_moe/batched_deep_gemm_moe.py", "license": "Apache License 2.0", "lines": 384, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:examples/online_serving/multi_instance_data_parallel.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import asyncio import threading from vllm.engine.arg_utils import AsyncEngineArgs from vllm.engine.async_llm_engine import AsyncLLMEngine from vllm.outputs import RequestOutput from vllm.sampling_params import SamplingParams from vllm.v1.metrics.loggers import AggregatedLoggingStatLogger """ To run this example, run the following commands simultaneously with different CUDA_VISIBLE_DEVICES: python examples/online_serving/multi_instance_data_parallel.py vllm serve ibm-research/PowerMoE-3b -dp 2 -dpr 1 \ --data-parallel-address 127.0.0.1 --data-parallel-rpc-port 62300 \ --data-parallel-size-local 1 --enforce-eager --headless Once both instances have completed the handshake, this example will send a request to the instance with DP rank 1. """ def _do_background_logging(engine, interval, stop_event): try: while not stop_event.is_set(): asyncio.run(engine.do_log_stats()) stop_event.wait(interval) except Exception as e: print(f"vLLM background logging shutdown: {e}") pass async def main(): engine_args = AsyncEngineArgs( model="ibm-research/PowerMoE-3b", data_parallel_size=2, tensor_parallel_size=1, dtype="auto", max_model_len=2048, data_parallel_address="127.0.0.1", data_parallel_rpc_port=62300, data_parallel_size_local=1, enforce_eager=True, enable_log_requests=True, disable_custom_all_reduce=True, ) engine_client = AsyncLLMEngine.from_engine_args( engine_args, # Example: Using aggregated logger stat_loggers=[AggregatedLoggingStatLogger], ) stop_logging_event = threading.Event() logging_thread = threading.Thread( target=_do_background_logging, args=(engine_client, 5, stop_logging_event), daemon=True, ) logging_thread.start() sampling_params = SamplingParams( temperature=0.7, top_p=0.9, max_tokens=100, ) num_prompts = 10 for i in range(num_prompts): prompt = "Who won the 2004 World Series?" final_output: RequestOutput | None = None async for output in engine_client.generate( prompt=prompt, sampling_params=sampling_params, request_id=f"abcdef-{i}", data_parallel_rank=1, ): final_output = output if final_output: print(final_output.outputs[0].text) stop_logging_event.set() logging_thread.join() if __name__ == "__main__": asyncio.run(main())
{ "repo_id": "vllm-project/vllm", "file_path": "examples/online_serving/multi_instance_data_parallel.py", "license": "Apache License 2.0", "lines": 75, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/v1/attention/backends/mla/cutlass_mla.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import os from typing import ClassVar import torch import vllm._custom_ops as ops from vllm.config.cache import CacheDType from vllm.logger import init_logger from vllm.model_executor.layers.attention.mla_attention import ( MLACommonBackend, MLACommonImpl, MLACommonMetadata, MLACommonMetadataBuilder, ) from vllm.platforms.interface import DeviceCapability from vllm.utils.platform_utils import num_compute_units from vllm.v1.attention.backend import ( AttentionCGSupport, AttentionLayer, AttentionType, MultipleOf, is_quantized_kv_cache, ) logger = init_logger(__name__) class CutlassMLAMetadataBuilder(MLACommonMetadataBuilder[MLACommonMetadata]): # enable full CUDA Graph support for decode-only capture _cudagraph_support: ClassVar[AttentionCGSupport] = ( AttentionCGSupport.UNIFORM_SINGLE_TOKEN_DECODE ) class CutlassMLABackend(MLACommonBackend): supported_dtypes: ClassVar[list[torch.dtype]] = [torch.float16, torch.bfloat16] supported_kv_cache_dtypes: ClassVar[list[CacheDType]] = [ "auto", "bfloat16", "fp8", "fp8_e4m3", ] @staticmethod def get_supported_kernel_block_sizes() -> list[int | MultipleOf]: return [128] @staticmethod def get_name() -> str: return "CUTLASS_MLA" @staticmethod def get_impl_cls() -> type["CutlassMLAImpl"]: return CutlassMLAImpl @staticmethod def get_builder_cls() -> type["CutlassMLAMetadataBuilder"]: return CutlassMLAMetadataBuilder @classmethod def supports_compute_capability(cls, capability: DeviceCapability) -> bool: return capability.major == 10 class SM100Workspace: def __init__(self, initial_workspace_size): self._workspace_buf = torch.empty( initial_workspace_size, device="cuda", dtype=torch.uint8 ) self._block_size = 128 # Forced to 128 # Pre-compute sm_count to avoid recomputing it. Use device 0 as a proxy # (assumes all devices are similar) self._sm_count = num_compute_units(0) def get_buf(self): return self._workspace_buf def ensure_size(self, attn_metadata: MLACommonMetadata, num_kv_splits: int): batch_size = attn_metadata.num_reqs max_seq_len = attn_metadata.max_query_len workspace_size = ops.sm100_cutlass_mla_get_workspace_size( max_seq_len * self._block_size, batch_size, self._sm_count, num_kv_splits=num_kv_splits, ) if self._workspace_buf.shape[0] < workspace_size: self._workspace_buf.resize_(workspace_size) g_sm100_workspace = SM100Workspace(128 * 1024 * 1024) # 128MB MAX_HEADS = 128 class CutlassMLAImpl(MLACommonImpl[MLACommonMetadata]): can_return_lse_for_decode: bool = True def __init__( self, num_heads: int, head_size: int, scale: float, num_kv_heads: int, alibi_slopes: list[float] | None, sliding_window: int | None, kv_cache_dtype: str, logits_soft_cap: float | None, attn_type: str, kv_sharing_target_layer_name: str | None, # MLA Specific Arguments **mla_args, ) -> None: super().__init__( num_heads, head_size, scale, num_kv_heads, alibi_slopes, sliding_window, kv_cache_dtype, logits_soft_cap, attn_type, kv_sharing_target_layer_name, q_pad_num_heads=MAX_HEADS, **mla_args, ) unsupported_features = [alibi_slopes, sliding_window, logits_soft_cap] if any(unsupported_features): raise NotImplementedError( "CutlassMLAImpl does not support one of the following: " "alibi_slopes, sliding_window, logits_soft_cap" ) if attn_type != AttentionType.DECODER: raise NotImplementedError( "Encoder self-attention and " "encoder/decoder cross-attention " "are not implemented for " "CutlassMLAImpl" ) # TODO: Currently, num_kv_splits is limited to 16 to avoid hanging # issues. In case the code hangs, use: # FORCE_NUM_KV_SPLITS=1 force_num_kv_splits = os.environ.get("FORCE_NUM_KV_SPLITS", None) if force_num_kv_splits: logger.debug_once("Forcing num_kv_splits to %d", int(force_num_kv_splits)) self._num_kv_splits = int(force_num_kv_splits) else: self._num_kv_splits = -1 # => Auto-detect # Share workspace buffer across all executions self._workspace = g_sm100_workspace def _sm100_cutlass_mla_decode( self, q_nope: torch.Tensor, q_pe: torch.Tensor, kv_c_and_k_pe_cache: torch.Tensor, seq_lens: torch.Tensor, page_table: torch.Tensor, workspace: torch.Tensor, sm_scale: float, num_kv_splits: int, ) -> tuple[torch.Tensor, torch.Tensor]: assert q_nope.ndim == 3, f"q_nope must be a 3D tensor, but got {q_nope.ndim}" assert q_pe.ndim == 3, f"q_pe must be a 3D tensor, but got {q_pe.ndim}" assert kv_c_and_k_pe_cache.ndim == 3, ( "kv_c_and_k_pe_cache must be a 3D tensor, but got {}".format( kv_c_and_k_pe_cache.ndim ) ) B_q, H, D_q_nope = q_nope.shape B_q_2, H_2, D_q_pe = q_pe.shape assert (B_q == B_q_2) and (H == H_2) _, PAGE_SIZE, D_ckv = kv_c_and_k_pe_cache.shape D_latent = 512 D_rope = 64 assert D_q_nope == D_latent assert D_q_pe == D_rope assert D_ckv == D_latent + D_rope MAX_HEADS = 128 assert H <= MAX_HEADS, f"H must be <= {MAX_HEADS}, but got {H}" assert len(page_table.shape) == 2 B_block_table, block_num = page_table.shape assert B_block_table == B_q assert block_num > 0, f"block num must be greater than 0, got {block_num}" assert block_num % (128 / PAGE_SIZE) == 0 assert q_nope.dtype in (torch.float16, torch.bfloat16, torch.float8_e4m3fn), ( f"q_nope.dtype needs to be fp16 or bf16 or e4m3 but got {q_nope.dtype}." ) assert q_nope.dtype == q_pe.dtype == kv_c_and_k_pe_cache.dtype assert seq_lens.dtype == torch.int32, ( f"seq_lens.dtype needs to be int32 but got {seq_lens.dtype}." ) assert page_table.dtype == torch.int32, ( f"page_table.dtype needs to be int32 but got {page_table.dtype}." ) dtype = ( torch.bfloat16 if is_quantized_kv_cache(self.kv_cache_dtype) else q_nope.dtype ) out = q_nope.new_empty((B_q, MAX_HEADS, D_latent), dtype=dtype) lse = ( torch.empty((B_q, MAX_HEADS), dtype=torch.float32, device=q_nope.device) if self.need_to_return_lse_for_decode else torch.Tensor() ) ops.sm100_cutlass_mla_decode( out, lse, q_nope, q_pe, kv_c_and_k_pe_cache, seq_lens, page_table, workspace, sm_scale, num_kv_splits, ) if H < MAX_HEADS: # Extract the subsets of the outputs lse = lse[:, :H] if self.need_to_return_lse_for_decode else lse out = out[:, :H] return out, lse def forward_mqa( self, q: torch.Tensor | tuple[torch.Tensor, torch.Tensor], kv_c_and_k_pe_cache: torch.Tensor, attn_metadata: MLACommonMetadata, layer: AttentionLayer, ) -> tuple[torch.Tensor, torch.Tensor | None]: assert kv_c_and_k_pe_cache.numel() > 0 assert attn_metadata.decode is not None if type(q) is tuple: q_nope, q_pe = q else: q_nope, q_pe = torch.split( q, [self.kv_lora_rank, self.qk_rope_head_dim], dim=-1 ) # Adjust workspace size (if necessary) self._workspace.ensure_size(attn_metadata, self._num_kv_splits) # Run MLA o, lse = self._sm100_cutlass_mla_decode( q_nope, q_pe, kv_c_and_k_pe_cache, attn_metadata.decode.seq_lens, attn_metadata.decode.block_table, self._workspace.get_buf(), self.scale, self._num_kv_splits, ) return o, (lse if self.need_to_return_lse_for_decode else None)
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/attention/backends/mla/cutlass_mla.py", "license": "Apache License 2.0", "lines": 232, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/kernels/test_apply_repetition_penalties.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import pytest import torch from tests.kernels.utils import opcheck from vllm._custom_ops import ( apply_repetition_penalties_cuda, apply_repetition_penalties_torch, ) from vllm.platforms import current_platform from vllm.utils.torch_utils import set_random_seed NUM_SEQS = [1, 2, 3, 4, 8, 13, 17, 32, 37, 256, 1023, 1024, 1025] # [stress, stress, stress, Qwen, llama 4] VOCAB_SIZES = [17, 256, 1019, 151936, 202048] REPETITION_PENALTY_VALUES = [1.05] SEEDS = [0] DTYPES = [torch.float32, torch.float16] @pytest.mark.parametrize("num_seqs", NUM_SEQS) @pytest.mark.parametrize("vocab_size", VOCAB_SIZES) @pytest.mark.parametrize("repetition_penalty", REPETITION_PENALTY_VALUES) @pytest.mark.parametrize("dtype", DTYPES) @pytest.mark.parametrize("seed", SEEDS) @pytest.mark.skipif( not current_platform.is_cuda(), reason="This test for checking CUDA kernel" ) @torch.inference_mode() def test_apply_repetition_penalties( num_seqs: int, vocab_size: int, repetition_penalty: float, dtype: torch.dtype, seed: int, ) -> None: """ Test the apply_repetition_penalties custom op against a reference implementation. """ set_random_seed(seed) torch.set_default_device("cuda:0") # Create test data logits = torch.randn(num_seqs, vocab_size, dtype=dtype) # Create masks with some random tokens marked as repeated prompt_mask = torch.zeros(num_seqs, vocab_size, dtype=torch.bool) output_mask = torch.zeros(num_seqs, vocab_size, dtype=torch.bool) # Mark some tokens as repeated in prompt and output prompt_indices = torch.randint(0, vocab_size, (num_seqs, max(1, vocab_size // 200))) output_indices = torch.randint(0, vocab_size, (num_seqs, max(1, vocab_size // 200))) for i in range(num_seqs): prompt_mask[i, prompt_indices[i]] = True output_mask[i, output_indices[i]] = True # Create repetition penalties tensor repetition_penalties = torch.full((num_seqs,), repetition_penalty, dtype=dtype) # Run all three implementations logits_torch = logits.clone() logits_cuda = logits.clone() apply_repetition_penalties_torch( logits_torch, prompt_mask, output_mask, repetition_penalties ) apply_repetition_penalties_cuda( logits_cuda, prompt_mask, output_mask, repetition_penalties ) # Compare all outputs to reference torch.testing.assert_close(logits_torch, logits_cuda, rtol=1e-3, atol=1e-3) # Test the operator by applying the opcheck utility opcheck( torch.ops._C.apply_repetition_penalties_, (logits.clone(), prompt_mask, output_mask, repetition_penalties), ) @pytest.mark.skipif( not current_platform.is_cuda(), reason="This test for checking CUDA kernel" ) @torch.inference_mode() def test_apply_repetition_penalties_zero_seqs() -> None: """ Test the apply_repetition_penalties custom op with num_seqs=0 against a reference implementation. """ num_seqs = 0 vocab_size = 17 repetition_penalty = 1.05 dtype = torch.float32 seed = 0 set_random_seed(seed) torch.set_default_device("cuda:0") # Create test data logits = torch.randn(num_seqs, vocab_size, dtype=dtype) # Create masks with some random tokens marked as repeated prompt_mask = torch.zeros(num_seqs, vocab_size, dtype=torch.bool) output_mask = torch.zeros(num_seqs, vocab_size, dtype=torch.bool) # No tokens to mark as repeated since num_seqs=0 # Create repetition penalties tensor repetition_penalties = torch.full((num_seqs,), repetition_penalty, dtype=dtype) # Run all three implementations logits_torch = logits.clone() logits_cuda = logits.clone() apply_repetition_penalties_torch( logits_torch, prompt_mask, output_mask, repetition_penalties ) apply_repetition_penalties_cuda( logits_cuda, prompt_mask, output_mask, repetition_penalties ) # Compare all outputs to reference torch.testing.assert_close(logits_torch, logits_cuda, rtol=1e-3, atol=1e-3) # Test the operator by applying the opcheck utility opcheck( torch.ops._C.apply_repetition_penalties_, (logits.clone(), prompt_mask, output_mask, repetition_penalties), )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/test_apply_repetition_penalties.py", "license": "Apache License 2.0", "lines": 108, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/v1/attention/backends/cpu_attn.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from dataclasses import dataclass from typing import ClassVar import torch from vllm import _custom_ops as ops from vllm.config import VllmConfig from vllm.logger import init_logger from vllm.platforms import CpuArchEnum, current_platform from vllm.v1.attention.backend import ( AttentionBackend, AttentionImpl, AttentionLayer, AttentionMetadataBuilder, AttentionType, CommonAttentionMetadata, is_quantized_kv_cache, ) from vllm.v1.attention.backends.utils import ( split_decodes_and_prefills, ) from vllm.v1.kv_cache_interface import AttentionSpec, CrossAttentionSpec logger = init_logger(__name__) _CPU_ARCH_PREFER_MIXED_BATCH = (CpuArchEnum.X86, CpuArchEnum.ARM, CpuArchEnum.S390X) class CPUAttentionBackend(AttentionBackend): accept_output_buffer: bool = True supported_dtypes: ClassVar[list[torch.dtype]] = [ torch.float16, torch.bfloat16, torch.float32, ] @classmethod def get_supported_dtypes(cls) -> list[torch.dtype]: return [torch.float16, torch.bfloat16, torch.float32] @classmethod def get_supported_head_sizes(cls) -> list[int]: return [32, 64, 80, 96, 112, 128, 160, 192, 224, 256] @staticmethod def get_name() -> str: return "CPU_ATTN" @classmethod def supports_attn_type(cls, attn_type: str) -> bool: """CPU attention supports decoder, encoder-only and encoder-decoder attention.""" return attn_type in ( AttentionType.DECODER, AttentionType.ENCODER, AttentionType.ENCODER_ONLY, AttentionType.ENCODER_DECODER, ) @staticmethod def get_impl_cls() -> type["CPUAttentionBackendImpl"]: return CPUAttentionBackendImpl @staticmethod def get_builder_cls() -> type["CPUAttentionMetadataBuilder"]: return CPUAttentionMetadataBuilder @staticmethod def get_kv_cache_shape( num_blocks: int, block_size: int, num_kv_heads: int, head_size: int, cache_dtype_str: str = "auto", ) -> tuple[int, ...]: return 2, num_blocks, num_kv_heads, block_size, head_size @staticmethod def use_cascade_attention(*args, **kwargs) -> bool: return False @dataclass class CPUAttentionMetadata: isa: str num_actual_tokens: int # Number of tokens excluding padding. max_query_len: int query_start_loc: torch.Tensor max_seq_len: int seq_lens: torch.Tensor block_table: torch.Tensor slot_mapping: torch.Tensor scheduler_metadata: torch.Tensor | None causal: bool = True # can be removed after deprecate sdpa use_sdpa_prefill: bool = False num_decode_tokens: int = 0 sdpa_attn_masks: list[torch.Tensor | None] | None = None sdpa_start_loc: torch.Tensor | None = None class CPUAttentionMetadataBuilder(AttentionMetadataBuilder[CPUAttentionMetadata]): def __init__( self, kv_cache_spec: AttentionSpec, layer_names: list[str], vllm_config: VllmConfig, device: torch.device, ) -> None: super().__init__(kv_cache_spec, layer_names, vllm_config, device) self.use_sdpa_prefill = False reorder_batch_threshold = None if current_platform.get_cpu_architecture() not in _CPU_ARCH_PREFER_MIXED_BATCH: # in this case, decode seqs are reordered to the front of prefill seqs # to split decode and prefill. Then use SDPA for prefill and # cpu_attention_with_kv_cache for decode reorder_batch_threshold = 1 self.use_sdpa_prefill = True self._init_reorder_batch_threshold(reorder_batch_threshold, False) self.kv_cache_spec = kv_cache_spec self.vllm_config = vllm_config parallel_config = vllm_config.parallel_config self.num_kv_heads = vllm_config.model_config.get_num_kv_heads(parallel_config) self.num_heads = vllm_config.model_config.get_num_attention_heads( parallel_config ) self.head_dim = kv_cache_spec.head_size self.dtype = vllm_config.model_config.dtype self.window_size = getattr(kv_cache_spec, "sliding_window", -1) if self.window_size is None: self.window_size = -1 self.block_size = vllm_config.cache_config.block_size self.isa = _get_attn_isa(self.dtype, self.block_size, self.head_dim) self.is_cross_attention = isinstance(kv_cache_spec, CrossAttentionSpec) def build( self, common_prefix_len: int, common_attn_metadata: CommonAttentionMetadata, fast_build: bool = False, ) -> CPUAttentionMetadata: num_reqs = common_attn_metadata.num_reqs num_actual_tokens = common_attn_metadata.num_actual_tokens max_query_len = common_attn_metadata.max_query_len max_seq_len = common_attn_metadata.max_seq_len query_start_loc = common_attn_metadata.query_start_loc seq_lens = common_attn_metadata.seq_lens block_table_tensor = common_attn_metadata.block_table_tensor slot_mapping = common_attn_metadata.slot_mapping causal = False if self.is_cross_attention else common_attn_metadata.causal sdpa_start_loc = query_start_loc num_decode_tokens = 0 if self.use_sdpa_prefill and causal: # Decoder, need reorder and truncate assert self.reorder_batch_threshold (num_decodes, num_prefills, num_decode_tokens, num_prefill_tokens) = ( split_decodes_and_prefills( common_attn_metadata, decode_threshold=self.reorder_batch_threshold, require_uniform=True, ) ) num_reqs = num_decodes sdpa_start_loc = sdpa_start_loc[num_decodes:] - num_decode_tokens seq_lens = seq_lens[:num_decodes] query_start_loc = query_start_loc[: num_decodes + 1] block_table_tensor = block_table_tensor[:num_decodes] sheduler_metadata = ops.cpu_attn_get_scheduler_metadata( num_reqs=num_reqs, num_heads=self.num_heads, num_kv_heads=self.num_kv_heads, head_dim=self.head_dim, seq_lens=seq_lens, dtype=self.dtype, query_start_loc=query_start_loc, causal=causal, sliding_window_size=self.window_size, isa=self.isa, enable_kv_split=True, ) attn_metadata = CPUAttentionMetadata( isa=self.isa, num_actual_tokens=num_actual_tokens, max_query_len=max_query_len, query_start_loc=query_start_loc, max_seq_len=max_seq_len, seq_lens=seq_lens, block_table=block_table_tensor, slot_mapping=slot_mapping, scheduler_metadata=sheduler_metadata, causal=causal, use_sdpa_prefill=self.use_sdpa_prefill, num_decode_tokens=num_decode_tokens, sdpa_start_loc=sdpa_start_loc, ) return attn_metadata class CPUAttentionBackendImpl(AttentionImpl): def __init__( self, num_heads: int, head_size: int, scale: float, num_kv_heads: int, alibi_slopes: list[float] | None, sliding_window: int | None, kv_cache_dtype: str, logits_soft_cap: float | None = None, attn_type: str = AttentionType.DECODER, kv_sharing_target_layer_name: str | None = None, sinks: torch.Tensor | None = None, ) -> None: self.kv_sharing_target_layer_name = kv_sharing_target_layer_name self.num_heads = num_heads self.head_size = head_size self.scale = float(scale) if logits_soft_cap is not None and attn_type in ( AttentionType.ENCODER, AttentionType.ENCODER_ONLY, ): logger.warning_once( "CPU_ATTN does not support logits softcap for" " ENCODER and ENCODER_ONLY, outputs may be slightly off" ) if logits_soft_cap is None: logits_soft_cap = 0 self.logits_soft_cap = logits_soft_cap self.num_kv_heads = num_kv_heads if alibi_slopes is not None: alibi_slopes = torch.tensor(alibi_slopes, dtype=torch.float32) self.alibi_slopes = alibi_slopes if sliding_window is None: self.sliding_window = (-1, -1) elif attn_type == AttentionType.ENCODER_ONLY: self.sliding_window = (sliding_window - 1, sliding_window - 1) else: self.sliding_window = (sliding_window - 1, 0) self.kv_cache_dtype = kv_cache_dtype self.num_queries_per_kv = self.num_heads // self.num_kv_heads if is_quantized_kv_cache(kv_cache_dtype): raise NotImplementedError("FP8 KV cache is unsupported in CPU_ATTN") self.attn_type = attn_type self.sinks = sinks if self.sinks is not None: assert self.sinks.shape[0] == num_heads, ( "Sinks must have the same number of heads as the number of " "heads in the layer" ) def forward( self, layer: AttentionLayer, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, kv_cache: torch.Tensor, attn_metadata: CPUAttentionMetadata | None, output: torch.Tensor | None = None, output_scale: torch.Tensor | None = None, output_block_scale: torch.Tensor | None = None, ) -> torch.Tensor: """Forward pass for CPU attention backend. Args: query: shape = [num_tokens, num_heads, head_size] key: shape = [num_tokens, num_kv_heads, head_size] value: shape = [num_tokens, num_kv_heads, head_size] kv_cache: shape = [2, num_blocks, num_kv_heads, block_size, head_size] attn_metadata: Metadata for attention. Returns: shape = [num_tokens, num_heads * head_size] """ assert output is not None, "Output tensor must be provided." if output_scale is not None or output_block_scale is not None: raise NotImplementedError( "fused output quantization is not yet supported" " for CPUAttentionBackendImpl" ) # For warming-up if attn_metadata is None: return output num_actual_tokens = attn_metadata.num_actual_tokens # Handle encoder attention differently - no KV cache needed if self.attn_type in (AttentionType.ENCODER_ONLY, AttentionType.ENCODER): # For encoder attention, return self._run_sdpa_forward( query[:num_actual_tokens], key[:num_actual_tokens], value[:num_actual_tokens], output[:num_actual_tokens], attn_metadata, self.attn_type, ) # For decoder and cross-attention, use KV cache, size are # [num_blocks, num_kv_heads, block_size, head_size] key_cache, value_cache = kv_cache.unbind(0) # key and value may be None in the case of cross attention. They are # calculated once based on the output from the encoder and then cached # in KV cache. if ( self.kv_sharing_target_layer_name is None and key is not None and value is not None ): ops.cpu_attn_reshape_and_cache( key, value, key_cache, value_cache, attn_metadata.slot_mapping, attn_metadata.isa, ) if attn_metadata.use_sdpa_prefill: assert self.sinks is None, "Attention sink is unsupported in SDPA prefill" num_decode_tokens = attn_metadata.num_decode_tokens self._run_sdpa_forward( query[num_decode_tokens:num_actual_tokens], key[num_decode_tokens:num_actual_tokens], value[num_decode_tokens:num_actual_tokens], output[num_decode_tokens:num_actual_tokens], attn_metadata, self.attn_type, ) num_actual_tokens = num_decode_tokens if num_actual_tokens > 0: ops.cpu_attention_with_kv_cache( query=query[:num_actual_tokens], key_cache=key_cache, value_cache=value_cache, output=output[:num_actual_tokens], # type: ignore query_start_loc=attn_metadata.query_start_loc, seq_lens=attn_metadata.seq_lens, scale=self.scale, causal=attn_metadata.causal, alibi_slopes=self.alibi_slopes, # type: ignore sliding_window=self.sliding_window, block_table=attn_metadata.block_table, softcap=self.logits_soft_cap, scheduler_metadata=attn_metadata.scheduler_metadata, s_aux=self.sinks, ) return output def _run_sdpa_forward( self, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, output: torch.Tensor, attn_metadata: CPUAttentionMetadata, attn_type: str, ) -> torch.Tensor: attn_masks = attn_metadata.sdpa_attn_masks if attn_masks is None: if self.alibi_slopes is not None: attn_masks = _make_alibi_bias( self.alibi_slopes, query.dtype, attn_metadata.sdpa_start_loc, ) elif self.sliding_window[0] != -1 or self.sliding_window[1] != -1: assert attn_metadata.seq_lens is not None attn_masks = _make_sliding_window_bias( attn_metadata.sdpa_start_loc, self.sliding_window[0], self.sliding_window[1], query.dtype, ) else: attn_masks = [None] * (attn_metadata.sdpa_start_loc.size(0) - 1) # type: ignore attn_metadata.sdpa_attn_masks = attn_masks query = query.movedim(0, query.dim() - 2) key = key.movedim(0, key.dim() - 2) value = value.movedim(0, value.dim() - 2) causal_attn = attn_type == AttentionType.DECODER sdpa_start_loc = attn_metadata.sdpa_start_loc.numpy() # type: ignore for i in range(len(attn_masks)): mask = attn_masks[i] start_q = sdpa_start_loc[i] end_q = sdpa_start_loc[i + 1] sub_out = ( torch.nn.functional.scaled_dot_product_attention( query[None, :, start_q:end_q, :], key[None, :, start_q:end_q, :], value[None, :, start_q:end_q, :], attn_mask=mask, dropout_p=0.0, is_causal=causal_attn and mask is None, scale=self.scale, enable_gqa=self.num_heads > self.num_kv_heads, ) .squeeze(0) .movedim(query.dim() - 2, 0) ) output[start_q:end_q, :, :] = sub_out return output def _make_alibi_bias( alibi_slopes: torch.Tensor, dtype: torch.dtype, sdpa_start_loc: torch.Tensor, ) -> list[torch.Tensor]: attn_biases: list[torch.Tensor] = [] seq_num = sdpa_start_loc.size(0) - 1 sdpa_start_loc = sdpa_start_loc.numpy() # type: ignore for i in range(seq_num): seq_len = sdpa_start_loc[i + 1] - sdpa_start_loc[i] bias = torch.arange(seq_len, dtype=dtype) # type: ignore # NOTE(zhuohan): HF uses # `bias = bias[None, :].repeat(seq_len, 1)` # here. We find that both biases give the same results, but # the bias below more accurately follows the original ALiBi # paper. bias = bias[None, :] - bias[:, None] num_heads = alibi_slopes.shape[0] bias = bias[None, :].repeat((num_heads, 1, 1)) bias.mul_(alibi_slopes[:, None, None]).unsqueeze_(0) inf_mask = ( torch.empty((1, seq_len, seq_len), dtype=bias.dtype) # type: ignore .fill_(-torch.inf) .triu_(diagonal=1) ) attn_biases.append((bias + inf_mask).to(dtype)) return attn_biases def _make_sliding_window_bias( sdpa_start_loc: torch.Tensor, left_window_size: int, right_window_size: int, dtype: torch.dtype, ) -> list[torch.Tensor]: attn_biases: list[torch.Tensor] = [] seq_num = sdpa_start_loc.size(0) - 1 sdpa_start_loc = sdpa_start_loc.numpy() # type: ignore for i in range(seq_num): seq_len = sdpa_start_loc[i + 1] - sdpa_start_loc[i] mask = torch.full( # type: ignore (1, seq_len, seq_len), # type: ignore fill_value=1, dtype=dtype, ) if right_window_size != -1: mask = torch.tril(mask, diagonal=right_window_size) if left_window_size != -1: mask = torch.triu(mask, diagonal=-left_window_size) mask = torch.log(mask) attn_biases.append(mask) return attn_biases def _get_attn_isa( dtype: torch.dtype, block_size: int, head_size: int | None = None ) -> str: if head_size is not None and head_size % 32 != 0 and head_size % 16 == 0: return "vec16" supports_amx = torch._C._cpu._is_amx_tile_supported() supports_arm = current_platform.get_cpu_architecture() == CpuArchEnum.ARM supports_vxe = current_platform.get_cpu_architecture() == CpuArchEnum.S390X if supports_amx and dtype in (torch.bfloat16,) and block_size % 32 == 0: return "amx" elif block_size % 32 == 0: if supports_arm: # support ARM NEON FMLA and BFMMLA (bf16) for block size 32 return "neon" elif supports_vxe: return "vxe" else: return "vec" else: return "vec16"
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/attention/backends/cpu_attn.py", "license": "Apache License 2.0", "lines": 446, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/v1/worker/cpu_model_runner.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from contextlib import contextmanager from typing import Any import torch import torch.nn as nn from vllm.config import VllmConfig from vllm.logger import init_logger from vllm.model_executor.model_loader import get_model from vllm.tracing import instrument from vllm.v1.utils import CpuGpuBuffer from vllm.v1.worker.gpu_model_runner import GPUModelRunner logger = init_logger(__name__) class CPUModelRunner(GPUModelRunner): def __init__(self, vllm_config: VllmConfig, device: torch.device): with _torch_cuda_wrapper(): super().__init__(vllm_config, device) assert device == torch.device("cpu") assert self.speculative_config is None, "spec decode is not supported." self.use_cuda_graph = False self.cascade_attn_enabled = False self._postprocess_tensors() def _postprocess_tensors(self) -> None: # Note: replace device tensors with cpu tensors def replace_tensor(obj: Any, cpu_attr_name: str, device_attr_name) -> None: cpu_tensor = getattr(obj, cpu_attr_name, None) device_tensor = getattr(obj, device_attr_name, None) if cpu_tensor is not None and device_tensor is not None: assert isinstance(cpu_tensor, torch.Tensor) assert isinstance(device_tensor, torch.Tensor) setattr(obj, device_attr_name, cpu_tensor) for v in vars(self).values(): if isinstance(v, CpuGpuBuffer): v.gpu = v.cpu for k, v in vars(self.input_batch).items(): if k.endswith("_cpu_tensor") and isinstance(v, torch.Tensor): replace_tensor(self.input_batch, k, k[:-11]) for block_table in self.input_batch.block_table.block_tables: for v in vars(block_table).values(): if isinstance(v, CpuGpuBuffer): v.gpu = v.cpu @instrument(span_name="Loading (CPU)") def load_model(self, load_dummy_weights: bool = False) -> None: if load_dummy_weights: raise ValueError( "Loading dummy weights (needed for elastic EP scale-up) " "Is not supported by the CPU Model Runner." ) logger.info("Starting to load model %s...", self.model_config.model) self.model = get_model(vllm_config=self.vllm_config) if self.lora_config: self.model = self.load_lora_model(self.model, self.vllm_config, self.device) def get_model(self) -> nn.Module: return self.model @instrument(span_name="Warmup (CPU)") def warming_up_model(self) -> None: logger.info("Warming up model for the compilation...") # Only generate graph for the generic shape with _set_global_compilation_settings(self.vllm_config): self._dummy_run( min( max(16, self.max_num_reqs), self.scheduler_config.max_num_batched_tokens, ) ) logger.info("Warming up done.") def _init_device_properties(self) -> None: pass def _sync_device(self) -> None: pass def get_dp_padding(self, num_tokens: int) -> tuple[int, torch.Tensor | None]: # Note: For CPU backend, dp padding is not required for now. return 0, None @contextmanager def _torch_cuda_wrapper(): class _EventPlaceholder: def __init__(self, *args, **kwargs) -> None: self.record = lambda: None self.synchronize = lambda: None class _StreamPlaceholder: def __init__(self, *args, **kwargs) -> None: pass cuda_event = torch.Event cuda_stream = torch.cuda.Stream try: torch.Event = _EventPlaceholder torch.cuda.Stream = _StreamPlaceholder yield finally: torch.Event = cuda_event torch.cuda.Stream = cuda_stream @contextmanager def _set_global_compilation_settings(config: VllmConfig): import torch._inductor.config as torch_inductor_config inductor_config = config.compilation_config.inductor_compile_config # Note: The MKLDNN and CPPGEMM backend requires freezing parameters. freezing_value = torch_inductor_config.freezing try: if inductor_config.get("max_autotune", False): torch_inductor_config.freezing = True yield finally: torch_inductor_config.freezing = freezing_value
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/worker/cpu_model_runner.py", "license": "Apache License 2.0", "lines": 103, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/v1/worker/cpu_worker.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import os import platform from collections.abc import Callable from typing import Any import torch from vllm import envs from vllm.config import VllmConfig from vllm.logger import init_logger from vllm.platforms import CpuArchEnum, current_platform from vllm.platforms.cpu import CpuPlatform, LogicalCPUInfo from vllm.profiler.wrapper import TorchProfilerWrapper from vllm.utils.torch_utils import set_random_seed from vllm.v1.worker.cpu_model_runner import CPUModelRunner from vllm.v1.worker.gpu_worker import Worker, init_worker_distributed_environment logger = init_logger(__name__) class CPUWorker(Worker): def __init__( self, vllm_config: VllmConfig, local_rank: int, rank: int, distributed_init_method: str, is_driver_worker: bool = False, ): super().__init__( vllm_config, local_rank, rank, distributed_init_method, is_driver_worker=is_driver_worker, ) self.parallel_config.disable_custom_all_reduce = True # Torch profiler. Enabled and configured through profiler_config. self.profiler: Any | None = None profiler_config = vllm_config.profiler_config if profiler_config.profiler == "torch": worker_name = f"{vllm_config.instance_id}-rank-{self.rank}" self.profiler = TorchProfilerWrapper( profiler_config, worker_name=worker_name, local_rank=self.local_rank, activities=["CPU"], ) def init_device(self): # Setup OpenMP threads affinity. omp_cpuids = envs.VLLM_CPU_OMP_THREADS_BIND # Under numa binding some cores reserved for kv transfer in nixl_connector.py if omp_cpuids == "auto" and platform.system() == "Linux": cpu_arch = current_platform.get_cpu_architecture() if cpu_arch in (CpuArchEnum.POWERPC, CpuArchEnum.S390X): # For S390X/POWERPC SMT-8/4/2 self.local_omp_cpuid = self._get_autobind_cpu_ids( lambda cpus: [cpu for cpu in cpus if cpu.id % 8 < 4] ) elif cpu_arch == CpuArchEnum.X86: # For x86 SMT-2, use 1 CPU per core self.local_omp_cpuid = self._get_autobind_cpu_ids( lambda cpus: cpus[-1:] ) elif cpu_arch == CpuArchEnum.ARM: # For AArch64, no SMT self.local_omp_cpuid = self._get_autobind_cpu_ids(lambda cpus: cpus) else: self.local_omp_cpuid = "nobind" elif omp_cpuids == "nobind": self.local_omp_cpuid = "nobind" else: local_dp_rank = self.parallel_config.data_parallel_rank_local omp_cpuids_list = omp_cpuids.split("|") if local_dp_rank is not None: world_size = self.parallel_config.world_size omp_cpuids_list = omp_cpuids_list[ local_dp_rank * world_size : (local_dp_rank + 1) * world_size ] self.local_omp_cpuid = omp_cpuids_list[self.rank] if self.local_omp_cpuid != "nobind": ret = torch.ops._C.init_cpu_threads_env(self.local_omp_cpuid) if ret: logger.info(ret) # Note: unique identifier for creating allreduce shared memory os.environ["VLLM_DIST_IDENT"] = self.distributed_init_method.split(":")[-1] # Initialize the distributed environment. init_worker_distributed_environment( self.vllm_config, self.rank, self.distributed_init_method, self.local_rank, current_platform.dist_backend, ) # Set random seed. set_random_seed(self.model_config.seed) # Construct the model runner self.model_runner: CPUModelRunner = CPUModelRunner( self.vllm_config, torch.device("cpu") ) def sleep(self, level: int = 1) -> None: logger.warning("sleep mode is not supported on CPU, ignore it.") pass def wake_up(self, tags: list[str] | None = None) -> None: logger.warning("sleep mode is not supported on CPU, ignore it.") pass def determine_available_memory(self) -> int: return self.cache_config.cpu_kvcache_space_bytes or 0 def compile_or_warm_up_model(self) -> float: # Reset the seed to ensure that the random state is not affected by # the model initialization and profiling. set_random_seed(self.model_config.seed) self.model_runner.warming_up_model() return self.compilation_config.compilation_time def _get_autobind_cpu_ids( self, cpu_selector: Callable[[list[LogicalCPUInfo]], list[LogicalCPUInfo]] ) -> str: """ Return CPU ids to bind based on NUMA nodes. Currently for rank N, only CPU ids on the N-th node in available NUMA node list will be selected. Args: cpu_selector: a callable object to select CPUs from a CPU list of a physical core. The input is a LogicalCPUInfo list, sorted by the LogicalCPUInfo.id. A selected LogicalCPUInfo list should be returned. """ # simulate multiple numa nodes, for testing sim_multi_numa_nodes = os.environ.get("VLLM_CPU_SIM_MULTI_NUMA", "0") != "0" allowed_numa_nodes, logical_cpu_list = ( CpuPlatform.get_allowed_cpu_core_node_list() ) assert ( len(allowed_numa_nodes) >= self.parallel_config.world_size or sim_multi_numa_nodes ), ( f"Not enough allowed NUMA nodes to bind threads of " f"{self.parallel_config.world_size} CPUWorkers. " f"Allowed NUMA nodes are {allowed_numa_nodes}. " "Please try to bind threads manually." ) if not sim_multi_numa_nodes: # Get CPUs on NUMA node `allowed_numa_nodes[local_rank]` selected_numa_node = allowed_numa_nodes[self.local_rank] # type: ignore logical_cpu_list = [ x for x in logical_cpu_list if x.numa_node == selected_numa_node ] else: # This is a bit tricky because the internal DP size # is always 1 for non-MoE models world_size_across_dp = ( self.parallel_config.world_size * self.parallel_config._api_process_count ) assert len(logical_cpu_list) >= world_size_across_dp logical_cpu_list = sorted(logical_cpu_list, key=lambda x: x.numa_node) sim_cpu_num_per_node = len(logical_cpu_list) // world_size_across_dp assert self.parallel_config.data_parallel_rank_local is not None start_idx = ( self.local_rank + self.parallel_config.world_size * self.parallel_config.data_parallel_rank_local ) * sim_cpu_num_per_node logical_cpu_list = logical_cpu_list[ start_idx : (start_idx + sim_cpu_num_per_node) ] # Select CPUs from each physical core via cpu_selector core_to_cpus: dict[int, list[LogicalCPUInfo]] = {} for cpu_info in logical_cpu_list: if cpu_info.physical_core not in core_to_cpus: core_to_cpus[cpu_info.physical_core] = [] core_to_cpus[cpu_info.physical_core].append(cpu_info) logical_cpu_list = [] for cpu_list in core_to_cpus.values(): cpu_list = sorted(cpu_list, key=lambda x: x.id) logical_cpu_list.extend(cpu_selector(cpu_list)) logical_cpu_list = sorted(logical_cpu_list, key=lambda x: x.id) # Reserve CPUs for other processes reserve_cpu_num = envs.VLLM_CPU_NUM_OF_RESERVED_CPU if reserve_cpu_num is None: need_reserve = ( self.parallel_config.world_size > 1 or self.parallel_config.data_parallel_size_local > 1 ) reserve_cpu_num = 1 if need_reserve else 0 assert len(logical_cpu_list) > reserve_cpu_num, ( f"VLLM_CPU_NUM_OF_RESERVED_CPU ({reserve_cpu_num}) " f"should less than {len(logical_cpu_list)}." ) if reserve_cpu_num != 0: logical_cpu_list = logical_cpu_list[:-reserve_cpu_num] logger.info( "auto thread-binding list (id, physical core): %s", [(x.id, x.physical_core) for x in logical_cpu_list], ) return ",".join([str(x.id) for x in logical_cpu_list]) def profile(self, is_start: bool = True, profile_prefix: str | None = None): if self.profiler is None: raise RuntimeError("Profiler is not enabled.") if is_start: self.profiler.start() else: self.profiler.stop()
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/v1/worker/cpu_worker.py", "license": "Apache License 2.0", "lines": 200, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/kernels/moe/test_deepep_deepgemm_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Test DeepEP + DeepGEMM integration DeepGEMM are gemm kernels specialized for the fp8 block-quantized case. """ import dataclasses from contextlib import contextmanager import pytest import torch.distributed from torch.distributed import ProcessGroup from typing_extensions import ParamSpec from vllm.config import VllmConfig, set_current_vllm_config from vllm.forward_context import set_forward_context from vllm.model_executor.layers.fused_moe.activation import MoEActivation from vllm.model_executor.layers.fused_moe.config import ( FusedMoEQuantConfig, fp8_w8a8_moe_quant_config, ) from vllm.model_executor.layers.fused_moe.fused_moe import fused_experts from vllm.model_executor.layers.fused_moe.modular_kernel import FusedMoEModularKernel from vllm.utils.deep_gemm import ( get_mk_alignment_for_contiguous_layout, is_deep_gemm_e8m0_used, is_deep_gemm_supported, ) from vllm.utils.import_utils import has_deep_ep, has_deep_gemm from vllm.utils.torch_utils import set_random_seed from vllm.v1.worker.workspace import init_workspace_manager from ...utils import multi_gpu_test from .parallel_utils import ProcessGroupInfo, parallel_launch from .utils import make_dummy_moe_config, make_test_weights if has_deep_ep(): from vllm.model_executor.layers.fused_moe.deepep_ht_prepare_finalize import ( DeepEPHTPrepareAndFinalize, ) from vllm.model_executor.layers.fused_moe.deepep_ll_prepare_finalize import ( DeepEPLLPrepareAndFinalize, ) from .parallel_utils import DeepEPHTArgs, DeepEPLLArgs, make_deepep_a2a if has_deep_gemm(): from vllm.model_executor.layers.fused_moe.batched_deep_gemm_moe import ( BatchedDeepGemmExperts, ) from vllm.model_executor.layers.fused_moe.deep_gemm_moe import DeepGemmExperts requires_deep_ep = pytest.mark.skipif( not has_deep_ep(), reason="Requires deep_ep kernels", ) requires_deep_gemm = pytest.mark.skipif( not is_deep_gemm_supported(), reason="Requires deep_gemm kernels", ) P = ParamSpec("P") @contextmanager def with_dp_metadata(M: int, world_size: int): num_tokens_across_dp = torch.tensor([M] * world_size, device="cpu", dtype=torch.int) vllm_config = VllmConfig() vllm_config.parallel_config.data_parallel_size = world_size vllm_config.parallel_config.enable_expert_parallel = True with set_forward_context( None, vllm_config, num_tokens=M, num_tokens_across_dp=num_tokens_across_dp, ): yield def next_power_of_2(x): import math if x == 0: return 1 return 2 ** math.ceil(math.log2(x)) def make_block_quant_fp8_weights( e: int, n: int, k: int, block_size: list[int], ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: """ Return weights w1q, w2q, w1_scale, w2_scale """ (_, w1q, w1_scale, _), (_, w2q, w2_scale, _) = make_test_weights( e, n, k, torch.bfloat16, torch.float8_e4m3fn, block_shape=block_size ) return w1q, w2q, w1_scale, w2_scale @dataclasses.dataclass class TestConfig: topk: int m: int k: int n: int num_experts: int per_act_token_quant: bool block_size: list[int] # configs for testing low-latency kernels low_latency: bool use_fp8_dispatch: bool | None = False @dataclasses.dataclass class TestTensors: rank_tokens: torch.Tensor # all ranks make this many tokens rank_token_scales: torch.Tensor | None topk: torch.Tensor topk_weights: torch.Tensor config: TestConfig @staticmethod def make(config: TestConfig, rank) -> "TestTensors": dtype = torch.bfloat16 topk, m, k = (config.topk, config.m, config.k) fp8_info = torch.finfo(torch.float8_e4m3fn) fp8_max, fp8_min = fp8_info.max, fp8_info.min rank_tokens = ( torch.randn((m, k), device=torch.cuda.current_device(), dtype=dtype) / 10.0 ) rank_tokens = rank_tokens.clamp(min=fp8_min, max=fp8_max) rank_token_scales = None topk_ids = torch.randint( low=0, high=config.num_experts, size=(m, topk), device=torch.cuda.current_device(), ).to(dtype=torch.int64) topk_weights = torch.randn( topk_ids.shape, dtype=torch.float32, device=torch.cuda.current_device() ) return TestTensors( rank_tokens=rank_tokens, rank_token_scales=rank_token_scales, topk=topk_ids, topk_weights=topk_weights, config=config, ) def make_ll_modular_kernel( pg: ProcessGroup, pgi: ProcessGroupInfo, max_tokens_per_rank: int, dp_size: int, hidden_size: int, q_dtype: torch.dtype | None, test_config: TestConfig, quant_config: FusedMoEQuantConfig, ) -> FusedMoEModularKernel: assert test_config.low_latency assert test_config.use_fp8_dispatch is not None a2a: DeepEPLLPrepareAndFinalize = make_deepep_a2a( pg=pg, pgi=pgi, dp_size=dp_size, deepep_ht_args=None, deepep_ll_args=DeepEPLLArgs( max_tokens_per_rank=max_tokens_per_rank, hidden_size=hidden_size, num_experts=test_config.num_experts, use_fp8_dispatch=test_config.use_fp8_dispatch, ), q_dtype=q_dtype, block_shape=test_config.block_size, ) fused_experts = BatchedDeepGemmExperts( max_num_tokens=max_tokens_per_rank, num_dispatchers=pgi.world_size // dp_size, quant_config=quant_config, moe_config=make_dummy_moe_config(), ) return FusedMoEModularKernel( prepare_finalize=a2a, fused_experts=fused_experts, inplace=False, ) def make_ht_modular_kernel( pg: ProcessGroup, pgi: ProcessGroupInfo, dp_size: int, num_local_experts: int, q_dtype: torch.dtype | None, test_config: TestConfig, quant_config: FusedMoEQuantConfig, ) -> FusedMoEModularKernel: assert not test_config.low_latency assert test_config.use_fp8_dispatch is None a2a: DeepEPHTPrepareAndFinalize = make_deepep_a2a( pg=pg, pgi=pgi, dp_size=dp_size, deepep_ht_args=DeepEPHTArgs(num_local_experts=num_local_experts), deepep_ll_args=None, q_dtype=q_dtype, block_shape=test_config.block_size, ) fused_experts = DeepGemmExperts( moe_config=make_dummy_moe_config(), quant_config=quant_config, ) return FusedMoEModularKernel( prepare_finalize=a2a, fused_experts=fused_experts, inplace=False, ) def make_modular_kernel( pg: ProcessGroup, pgi: ProcessGroupInfo, dp_size: int, num_local_experts: int, test_tensors: TestTensors, quant_config: FusedMoEQuantConfig, ) -> FusedMoEModularKernel: q_dtype = torch.float8_e4m3fn test_config = test_tensors.config mk: FusedMoEModularKernel # Make modular kernel if test_config.low_latency: max_tokens_per_rank = max(64, next_power_of_2(test_tensors.rank_tokens.size(0))) hidden_size = test_tensors.rank_tokens.size(-1) mk = make_ll_modular_kernel( pg=pg, pgi=pgi, max_tokens_per_rank=max_tokens_per_rank, dp_size=dp_size, hidden_size=hidden_size, q_dtype=q_dtype, test_config=test_config, quant_config=quant_config, ) else: mk = make_ht_modular_kernel( pg, pgi, dp_size, num_local_experts, q_dtype, test_config, quant_config=quant_config, ) return mk def deepep_deepgemm_moe_impl( pg: ProcessGroup, pgi: ProcessGroupInfo, dp_size: int, test_tensors: TestTensors, w1: torch.Tensor, w2: torch.Tensor, w1_scale: torch.Tensor | None, w2_scale: torch.Tensor | None, ) -> torch.Tensor: test_config = test_tensors.config num_experts = test_config.num_experts num_local_experts = w1.size(0) def build_expert_map(): num_local_experts = w1.size(0) expert_map = torch.full((num_experts,), fill_value=-1, dtype=torch.int32) s = pgi.rank * num_local_experts e = s + num_local_experts expert_map[s:e] = torch.tensor(list(range(num_local_experts))) return expert_map.to(device=torch.cuda.current_device(), dtype=torch.int32) quant_config = fp8_w8a8_moe_quant_config( w1_scale=w1_scale, w2_scale=w2_scale, # Low-Latency kernels can't dispatch scales. a1_scale=(None if test_config.low_latency else test_tensors.rank_token_scales), block_shape=test_config.block_size, ) # Make modular kernel mk: FusedMoEModularKernel = make_modular_kernel( pg=pg, pgi=pgi, dp_size=dp_size, num_local_experts=num_local_experts, test_tensors=test_tensors, quant_config=quant_config, ) with with_dp_metadata( M=test_tensors.rank_tokens.size(0), world_size=pgi.world_size ): out = mk.forward( hidden_states=test_tensors.rank_tokens, w1=w1, w2=w2, topk_weights=test_tensors.topk_weights, topk_ids=test_tensors.topk, activation=MoEActivation.SILU, global_num_experts=num_experts, expert_map=build_expert_map(), apply_router_weight_on_input=False, ) return out def triton_impl( a: torch.Tensor, topk_ids: torch.Tensor, topk_weights: torch.Tensor, w1: torch.Tensor, w2: torch.Tensor, w1_scale: torch.Tensor, w2_scale: torch.Tensor, a1_scale: torch.Tensor, block_shape: list[int], ): quant_config = fp8_w8a8_moe_quant_config( w1_scale=w1_scale, w2_scale=w2_scale, a1_scale=a1_scale, block_shape=block_shape, ) return fused_experts( hidden_states=a, w1=w1, w2=w2, topk_weights=topk_weights, topk_ids=topk_ids, inplace=False, quant_config=quant_config, ) def _test_deepep_deepgemm_moe( pgi: ProcessGroupInfo, dp_size: int, config: TestConfig, w1: torch.Tensor, w2: torch.Tensor, w1_scale: torch.Tensor, w2_scale: torch.Tensor, ): device = torch.device(f"cuda:{pgi.local_rank}") init_workspace_manager(device) set_random_seed(pgi.rank) w1 = w1.to(device=torch.cuda.current_device()) w2 = w2.to(device=torch.cuda.current_device()) w1_scale = w1_scale.to(device=torch.cuda.current_device()) w2_scale = w2_scale.to(device=torch.cuda.current_device()) pg = torch.distributed.new_group(list(range(pgi.world_size))) test_tensors = TestTensors.make(config, pgi.rank) block_shape = [w1.size(1) // w1_scale.size(1), w1.size(2) // w1_scale.size(2)] with set_current_vllm_config(VllmConfig()): # Reference triton_moe = triton_impl( a=test_tensors.rank_tokens, topk_ids=test_tensors.topk, topk_weights=test_tensors.topk_weights, w1=w1, w2=w2, w1_scale=w1_scale, w2_scale=w2_scale, a1_scale=test_tensors.rank_token_scales, block_shape=block_shape, ) # Slice experts for this rank. num_local_experts = config.num_experts // pgi.world_size e_start = num_local_experts * pgi.rank e_end = e_start + num_local_experts w1_ep = w1[e_start:e_end] w2_ep = w2[e_start:e_end] w1_scale_ep = w1_scale[e_start:e_end] w2_scale_ep = w2_scale[e_start:e_end] deepep_moe = deepep_deepgemm_moe_impl( pg, pgi, dp_size, test_tensors, w1_ep, w2_ep, w1_scale_ep, w2_scale_ep, ) torch.testing.assert_close( triton_moe, deepep_moe, atol=6e-2, rtol=6e-2, ) MNKs = [ (8, 128, 128), (8, 128, 512), (3, 1024, 2048), (32, 128, 1024), (45, 512, 2048), (64, 1024, 1024), (129, 128, 256), (129, 1024, 2048), (222, 1024, 2048), ] TOPKS = [2, 6] NUM_EXPERTS = [32] @pytest.mark.parametrize("mnk", MNKs) @pytest.mark.parametrize("num_experts", NUM_EXPERTS) @pytest.mark.parametrize("topk", TOPKS) @pytest.mark.parametrize("world_dp_size", [(2, 1)]) @multi_gpu_test(num_gpus=2) @requires_deep_ep @requires_deep_gemm def test_ht_deepep_deepgemm_moe( mnk: tuple[int, int, int], num_experts: int, topk: int, world_dp_size: tuple[int, int], disable_deepgemm_ue8m0, workspace_init, ): """ Tests for High-Throughput DeepEP + DeepGemm integration. """ m, n, k = mnk set_random_seed(7) if topk > num_experts: pytest.skip(f"Skipping test: topk={topk} > E={num_experts}") block_m = get_mk_alignment_for_contiguous_layout()[0] block_size = [block_m, block_m] world_size, dp_size = world_dp_size config = TestConfig( topk=topk, m=m, k=k, n=n, num_experts=num_experts, per_act_token_quant=False, block_size=block_size, low_latency=False, use_fp8_dispatch=None, ) w1, w2, w1_scale, w2_scale = make_block_quant_fp8_weights( num_experts, n, k, block_size ) parallel_launch( world_size, _test_deepep_deepgemm_moe, dp_size, config, w1, w2, w1_scale, w2_scale, ) MNKs = [ (1, 128, 2560), (2, 128, 2560), (3, 1024, 2560), (32, 128, 2560), (45, 512, 2560), (64, 1024, 2560), (222, 1024, 2560), ] # Fix tests for USE_FP8_DISPATCH=True USE_FP8_DISPATCH = [False] @pytest.mark.parametrize("mnk", MNKs) @pytest.mark.parametrize("num_experts", NUM_EXPERTS) @pytest.mark.parametrize("topk", TOPKS) @pytest.mark.parametrize("use_fp8_dispatch", USE_FP8_DISPATCH) @pytest.mark.parametrize("block_size", [[128, 128]]) @pytest.mark.parametrize("world_dp_size", [(2, 1)]) @multi_gpu_test(num_gpus=2) @requires_deep_ep @requires_deep_gemm def test_ll_deepep_deepgemm_moe( mnk: tuple[int, int, int], num_experts: int, topk: int, use_fp8_dispatch: bool, block_size: list[int], world_dp_size: tuple[int, int], disable_deepgemm_ue8m0, workspace_init, ): """ Tests for Low-Latency DeepEP + DeepGemm integration. """ assert not is_deep_gemm_e8m0_used() m, n, k = mnk set_random_seed(7) if topk > num_experts: pytest.skip(f"Skipping test: topk={topk} > E={num_experts}") world_size, dp_size = world_dp_size config = TestConfig( topk=topk, m=m, k=k, n=n, num_experts=num_experts, per_act_token_quant=False, block_size=block_size, low_latency=True, use_fp8_dispatch=use_fp8_dispatch, ) w1, w2, w1_scale, w2_scale = make_block_quant_fp8_weights( num_experts, n, k, block_size ) parallel_launch( world_size, _test_deepep_deepgemm_moe, dp_size, config, w1, w2, w1_scale, w2_scale, )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/test_deepep_deepgemm_moe.py", "license": "Apache License 2.0", "lines": 492, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:tests/kernels/moe/test_deepep_moe.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project """ Test deepep dispatch-combine logic """ import dataclasses import pytest import torch.distributed from torch.distributed import ProcessGroup from tests.kernels.moe.utils import make_dummy_moe_config from vllm import _custom_ops as ops from vllm.config import VllmConfig, set_current_vllm_config from vllm.model_executor.layers.activation import SiluAndMul from vllm.model_executor.layers.fused_moe import TritonExperts from vllm.model_executor.layers.fused_moe.activation import MoEActivation from vllm.model_executor.layers.fused_moe.config import ( FusedMoEQuantConfig, ) from vllm.model_executor.layers.fused_moe.fused_batched_moe import BatchedTritonExperts from vllm.model_executor.layers.fused_moe.modular_kernel import FusedMoEModularKernel from vllm.model_executor.layers.quantization.utils.fp8_utils import ( per_token_group_quant_fp8, ) from vllm.utils.import_utils import has_deep_ep from vllm.utils.torch_utils import set_random_seed from vllm.v1.worker.workspace import init_workspace_manager from ...utils import multi_gpu_test from .parallel_utils import ProcessGroupInfo, parallel_launch if has_deep_ep(): from vllm.model_executor.layers.fused_moe.deepep_ht_prepare_finalize import ( DeepEPHTPrepareAndFinalize, ) from vllm.model_executor.layers.fused_moe.deepep_ll_prepare_finalize import ( DeepEPLLPrepareAndFinalize, ) from .parallel_utils import DeepEPHTArgs, DeepEPLLArgs, make_deepep_a2a requires_deep_ep = pytest.mark.skipif( not has_deep_ep(), reason="Requires deep_ep kernels", ) MAX_TOKENS_PER_RANK = 64 def make_weights( e, n, k, dtype ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: """ Return weights w1, w2, w1_scale, w2_scale """ if dtype in [torch.float16, torch.bfloat16]: w1 = torch.randn((e, 2 * n, k), device="cuda", dtype=dtype) / 10 w2 = torch.randn((e, k, n), device="cuda", dtype=dtype) / 10 return w1, w2, None, None # per-out-channel weight quantization assert dtype == torch.float8_e4m3fn w1 = torch.empty((e, 2 * n, k), device="cuda", dtype=torch.float16) w2 = torch.empty((e, k, n), device="cuda", dtype=torch.float16) n_b_scales = 2 * n k_b_scales = k w1_q = torch.empty_like(w1, dtype=dtype) w2_q = torch.empty_like(w2, dtype=dtype) w1_scale = torch.empty((e, n_b_scales, 1), device="cuda", dtype=torch.float32) w2_scale = torch.empty((e, k_b_scales, 1), device="cuda", dtype=torch.float32) for expert in range(e): w1_q[expert], w1_scale[expert] = ops.scaled_fp8_quant( w1[expert], use_per_token_if_dynamic=True ) w2_q[expert], w2_scale[expert] = ops.scaled_fp8_quant( w2[expert], use_per_token_if_dynamic=True ) return w1_q, w2_q, w1_scale, w2_scale @dataclasses.dataclass class TestConfig: dtype: torch.dtype topk: int m: int k: int n: int num_experts: int @dataclasses.dataclass class TestTensors: rank_tokens: torch.Tensor # all ranks make this many tokens rank_token_scales: torch.Tensor | None topk: torch.Tensor topk_weights: torch.Tensor config: TestConfig @staticmethod def make(config: TestConfig, low_latency_mode: bool) -> "TestTensors": # TODO (varun) - check that float16 works ? assert config.dtype in [torch.bfloat16, torch.float8_e4m3fn] token_dtype = ( torch.bfloat16 if config.dtype == torch.float8_e4m3fn else config.dtype ) rank_tokens = ( torch.randn((config.m, config.k), device="cuda", dtype=token_dtype) / 10 ) rank_token_scales = None topk = torch.randint( low=0, high=config.num_experts, size=(config.m, config.topk), device="cuda" ).to(dtype=torch.int64) topk_weights = torch.randn(topk.shape, dtype=torch.float32, device="cuda") return TestTensors( rank_tokens=rank_tokens, rank_token_scales=rank_token_scales, topk=topk, topk_weights=topk_weights, config=config, ) def make_modular_kernel( pg: ProcessGroup, pgi: ProcessGroupInfo, low_latency_mode: bool, hidden_size: int, dp_size: int, num_experts: int, num_local_experts: int, q_dtype: torch.dtype | None, use_fp8_dispatch: bool, quant_config: FusedMoEQuantConfig, ) -> FusedMoEModularKernel: ht_args: DeepEPHTArgs | None = None ll_args: DeepEPLLArgs | None = None if low_latency_mode: ll_args = DeepEPLLArgs( max_tokens_per_rank=MAX_TOKENS_PER_RANK, hidden_size=hidden_size, num_experts=num_experts, use_fp8_dispatch=use_fp8_dispatch, ) else: assert not use_fp8_dispatch, ( "FP8 Dispatch is valid only for low-latency kernels" ) ht_args = DeepEPHTArgs(num_local_experts=num_local_experts) a2a: DeepEPHTPrepareAndFinalize | DeepEPLLPrepareAndFinalize = make_deepep_a2a( pg=pg, pgi=pgi, dp_size=dp_size, q_dtype=q_dtype, block_shape=None, deepep_ht_args=ht_args, deepep_ll_args=ll_args, ) num_dispatchers = pgi.world_size // dp_size moe_config = make_dummy_moe_config() if low_latency_mode: assert not quant_config.per_act_token_quant, "not supported in ll mode" fused_experts = BatchedTritonExperts( max_num_tokens=MAX_TOKENS_PER_RANK, num_dispatchers=num_dispatchers, moe_config=moe_config, quant_config=quant_config, ) else: fused_experts = TritonExperts( moe_config=moe_config, quant_config=quant_config, ) mk = FusedMoEModularKernel( prepare_finalize=a2a, fused_experts=fused_experts, inplace=False, ) return mk def deep_ep_moe_impl( pg: ProcessGroup, pgi: ProcessGroupInfo, low_latency_mode: bool, dp_size: int, test_tensors: TestTensors, w1: torch.Tensor, w2: torch.Tensor, w1_scale: torch.Tensor | None, w2_scale: torch.Tensor | None, num_experts: int, use_fp8_dispatch: bool, per_act_token_quant: bool, ) -> torch.Tensor: num_local_experts = w1.size(0) def build_expert_map(): num_local_experts = w1.size(0) expert_map = torch.full((num_experts,), fill_value=-1, dtype=torch.int32) s = pgi.rank * num_local_experts e = s + num_local_experts expert_map[s:e] = torch.tensor(list(range(num_local_experts))) return expert_map.to(device=torch.cuda.current_device(), dtype=torch.int32) hidden_size = test_tensors.rank_tokens.size(1) is_quantized = w1.dtype == torch.float8_e4m3fn q_dtype = None if is_quantized: q_dtype = torch.float8_e4m3fn out_hidden_states = torch.empty_like(test_tensors.rank_tokens) total_num_tokens = test_tensors.rank_tokens.size(0) def process_chunk(chunk_start, chunk_end, skip_result_store=False): rank_tokens_chunk = test_tensors.rank_tokens[chunk_start:chunk_end] topk_weights_chunk = test_tensors.topk_weights[chunk_start:chunk_end] topk_chunk = test_tensors.topk[chunk_start:chunk_end] rank_token_scales_chunk = test_tensors.rank_token_scales if ( rank_token_scales_chunk is not None and rank_token_scales_chunk.size(0) == total_num_tokens ): # per act token rank_token_scales_chunk = rank_token_scales_chunk[chunk_start:chunk_end] quant_config = FusedMoEQuantConfig.make( q_dtype, w1_scale=w1_scale, w2_scale=w2_scale, per_act_token_quant=per_act_token_quant, a1_scale=rank_token_scales_chunk, ) # Make modular kernel mk: FusedMoEModularKernel = make_modular_kernel( pg, pgi, low_latency_mode, hidden_size, dp_size, num_experts, num_local_experts, q_dtype, use_fp8_dispatch, quant_config, ) out = mk.forward( hidden_states=rank_tokens_chunk, w1=w1, w2=w2, topk_weights=topk_weights_chunk, topk_ids=topk_chunk, activation=MoEActivation.SILU, global_num_experts=num_experts, expert_map=build_expert_map(), apply_router_weight_on_input=False, ) if not skip_result_store: out_hidden_states[chunk_start:chunk_end, :].copy_(out, non_blocking=True) max_num_tokens_per_dp = ( MAX_TOKENS_PER_RANK if low_latency_mode else total_num_tokens ) for chunk_start_ in range(0, total_num_tokens, max_num_tokens_per_dp): chunk_start = chunk_start_ chunk_end = min(chunk_start + max_num_tokens_per_dp, total_num_tokens) # clamp start and end chunk_start = min(chunk_start, total_num_tokens - 1) chunk_end = min(chunk_end, total_num_tokens) process_chunk( chunk_start, chunk_end, skip_result_store=chunk_start_ >= total_num_tokens ) return out_hidden_states def torch_moe_impl( test_tensors: TestTensors, w1: torch.Tensor, w2: torch.Tensor, w1_scale: torch.Tensor | None, w2_scale: torch.Tensor | None, using_fp8_dispatch: bool, per_act_token_quant: bool, ): a, topk_ids, topk_weights = ( test_tensors.rank_tokens, test_tensors.topk, test_tensors.topk_weights, ) if using_fp8_dispatch: # The DeepEP implementation is requested to dispatch using FP8. # For numerical stability for testing, emulate the fp8 dispatch by # blockwise quant and de-quant. assert not per_act_token_quant a = test_tensors.rank_tokens aq, aq_scale = per_token_group_quant_fp8(a, 128, use_ue8m0=False) a = ( (aq.view(-1, 128).to(torch.float32) * aq_scale.view(-1, 1)) .view(a.shape) .to(a.dtype) ) is_quantized = w1.dtype == torch.float8_e4m3fn a_dtype = a.dtype if is_quantized: w1 = w1.to(dtype=torch.float32) * w1_scale w2 = w2.to(dtype=torch.float32) * w2_scale a = a.to(dtype=torch.float32) m, _ = a.shape topk = topk_ids.size(1) out = torch.zeros_like(a) for i in range(m): a_i = a[i] o_i = out[i] for j in range(topk): e = topk_ids[i][j] e_w = topk_weights[i][j] w1_e = w1[e] w2_e = w2[e] o_i += ( SiluAndMul()(a_i @ w1_e.transpose(0, 1)) @ w2_e.transpose(0, 1) ) * e_w if is_quantized: out = out.to(dtype=a_dtype) return out def _deep_ep_moe( pgi: ProcessGroupInfo, low_latency_mode: bool, dp_size: int, config: TestConfig, w1: torch.Tensor, w2: torch.Tensor, w1_scale: torch.Tensor | None, w2_scale: torch.Tensor | None, use_fp8_dispatch: bool, per_act_token_quant: bool, ): device = torch.device(f"cuda:{pgi.local_rank}") init_workspace_manager(device) if not low_latency_mode: assert not use_fp8_dispatch, ( "FP8 dispatch interface is available only in low-latency mode" ) is_quantized = w1.dtype == torch.float8_e4m3fn w1 = w1.to(device=torch.cuda.current_device()) w2 = w2.to(device=torch.cuda.current_device()) if is_quantized: w1_scale = w1_scale.to( # type: ignore device=torch.cuda.current_device() ) w2_scale = w2_scale.to( # type: ignore device=torch.cuda.current_device() ) pg = torch.distributed.new_group(list(range(pgi.world_size))) test_tensors = TestTensors.make(config, low_latency_mode) with set_current_vllm_config(VllmConfig()): # Reference torch_combined = torch_moe_impl( test_tensors, w1, w2, w1_scale, w2_scale, use_fp8_dispatch, per_act_token_quant, ) # Splice experts for this rank. num_local_experts = config.num_experts // pgi.world_size e_start = num_local_experts * pgi.rank e_end = e_start + num_local_experts w1_ep = w1[e_start:e_end] w2_ep = w2[e_start:e_end] w1_scale_ep, w2_scale_ep = None, None if is_quantized: w1_scale_ep = w1_scale[e_start:e_end] # type: ignore w2_scale_ep = w2_scale[e_start:e_end] # type: ignore deepep_combined = deep_ep_moe_impl( pg, pgi, low_latency_mode, dp_size, test_tensors, w1_ep, w2_ep, w1_scale_ep, w2_scale_ep, config.num_experts, use_fp8_dispatch, per_act_token_quant, ) torch.testing.assert_close( torch_combined, deepep_combined, atol=6e-2, rtol=6e-2, ) MNKs = [ (1, 128, 128), (2, 128, 512), (3, 1024, 2048), (32, 128, 1024), (45, 512, 2048), (64, 1024, 1024), (222, 1024, 2048), ] DTYPES = [torch.bfloat16, torch.float8_e4m3fn] @pytest.mark.parametrize("dtype", DTYPES) @pytest.mark.parametrize("m,n,k", MNKs) @pytest.mark.parametrize("num_experts", [32]) @pytest.mark.parametrize("topk", [6]) @pytest.mark.parametrize("world_dp_size", [(2, 1)]) @pytest.mark.parametrize("per_act_token_quant", [False, True]) @multi_gpu_test(num_gpus=2) @requires_deep_ep def test_deep_ep_moe( dtype: torch.dtype, m: int, n: int, k: int, num_experts: int, topk: int, world_dp_size: tuple[int, int], per_act_token_quant: bool, workspace_init, ): low_latency_mode = False use_fp8_dispatch = False set_random_seed(7) world_size, dp_size = world_dp_size config = TestConfig(dtype=dtype, topk=topk, m=m, k=k, n=n, num_experts=num_experts) w1, w2, w1_scale, w2_scale = make_weights(num_experts, n, k, dtype) parallel_launch( world_size, _deep_ep_moe, low_latency_mode, dp_size, config, w1, w2, w1_scale, w2_scale, use_fp8_dispatch, per_act_token_quant, ) MNKs = [ (1, 128, 2560), (2, 128, 2560), (3, 1024, 2560), (32, 128, 2560), (45, 512, 2560), (64, 1024, 2560), (222, 1024, 2560), ] DTYPES = [torch.float8_e4m3fn, torch.bfloat16] USE_FP8_DISPATCH = [True, False] @pytest.mark.parametrize("dtype", DTYPES) @pytest.mark.parametrize("m,n,k", MNKs) @pytest.mark.parametrize("num_experts", [32]) @pytest.mark.parametrize("topk", [6]) @pytest.mark.parametrize("world_dp_size", [(2, 1)]) @pytest.mark.parametrize("use_fp8_dispatch", USE_FP8_DISPATCH) @multi_gpu_test(num_gpus=2) @requires_deep_ep def test_low_latency_deep_ep_moe( dtype: torch.dtype, m: int, n: int, k: int, num_experts: int, topk: int, world_dp_size: tuple[int, int], use_fp8_dispatch: bool, workspace_init, ): low_latency_mode = True if low_latency_mode and k not in DeepEPLLPrepareAndFinalize.SUPPORTED_HIDDEN_SIZES: pytest.skip( f"Skipping test as hidden size {k} is not in list of supported " f"hidden sizes {DeepEPLLPrepareAndFinalize.SUPPORTED_HIDDEN_SIZES}" ) set_random_seed(7) world_size, dp_size = world_dp_size config = TestConfig(dtype=dtype, topk=topk, m=m, k=k, n=n, num_experts=num_experts) w1, w2, w1_scale, w2_scale = make_weights(num_experts, n, k, dtype) parallel_launch( world_size, _deep_ep_moe, low_latency_mode, dp_size, config, w1, w2, w1_scale, w2_scale, use_fp8_dispatch, False, )
{ "repo_id": "vllm-project/vllm", "file_path": "tests/kernels/moe/test_deepep_moe.py", "license": "Apache License 2.0", "lines": 469, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test
vllm-project/vllm:vllm/model_executor/layers/fused_moe/deepep_ht_prepare_finalize.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import deep_ep import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceContiguous, TopKWeightAndReduceDelegate, ) from vllm.model_executor.layers.fused_moe.utils import moe_kernel_quantize_input from vllm.utils.math_utils import round_up from vllm.v1.worker.ubatching import ( dbo_current_ubatch_id, dbo_enabled, dbo_get_previous_event, dbo_switch_to_comm, dbo_switch_to_compute, dbo_switch_to_compute_sync, dbo_yield_and_switch_from_comm_to_compute, dbo_yield_and_switch_from_compute_to_comm, ) class DeepEPHTPrepareAndFinalize(mk.FusedMoEPrepareAndFinalize): """ Prepare/Finalize using DeepEP High-Throughput kernels. """ @staticmethod def maybe_roundup_layer_hidden_size(hidden_size: int, dtype: torch.dtype) -> int: # Round up hidden size so it is compatible with DeepEP High Throughput # kernels. # DeepEP intranode kernels make copies in units of, # 32(warp-size) int4 elements. Round up hidden size to respect this. # For example, an input hidden size of 2880 with dtype torch.bfloat16 # will be rounded up to 3072. hidden_size_bytes = hidden_size * dtype.itemsize xfer_atom_size = 512 # 32 * 16 (size(int4)) if hidden_size_bytes % xfer_atom_size == 0: return hidden_size hidden_size_bytes = round_up(hidden_size_bytes, xfer_atom_size) return hidden_size_bytes // dtype.itemsize def __init__( self, buffer: deep_ep.Buffer, num_dispatchers: int, dp_size: int, rank_expert_offset: int, ): super().__init__() self.buffer = buffer self.num_dispatchers_ = num_dispatchers self.dp_size = dp_size self.rank_expert_offset = rank_expert_offset self.async_prepare = True # The dispatch function returns a handle that the combine function # requires. Under DBO microbatching we must track one handle per # micro-batch to avoid races between threads. self.handles = [None, None] # From https://github.com/deepseek-ai/DeepEP/blob/9fe9021f29c9083cd1808ab36b740208524d9f63/deep_ep/buffer.py#L164 self.available_rank_configs = [2, 4, 8, 16, 24, 32, 64, 128, 144, 160] def num_dispatchers(self) -> int: return self.num_dispatchers_ def output_is_reduced(self) -> bool: return True @property def activation_format(self) -> mk.FusedMoEActivationFormat: return mk.FusedMoEActivationFormat.Standard def max_num_tokens_per_rank(self) -> int | None: return None def topk_indices_dtype(self) -> torch.dtype | None: return torch.int64 def _get_dispatch_config(self) -> deep_ep.Config | None: if self.num_dispatchers_ not in self.available_rank_configs: return None return deep_ep.Buffer.get_dispatch_config(self.num_dispatchers_) def _get_combine_config(self) -> deep_ep.Config | None: if self.num_dispatchers_ not in self.available_rank_configs: return None return deep_ep.Buffer.get_combine_config(self.num_dispatchers_) def _do_dispatch( self, tokens: torch.Tensor, token_scales: torch.Tensor | None, rank_topk_ids: torch.Tensor, rank_topk_weights: torch.Tensor, num_experts: int, a1_scale: torch.Tensor | None, quant_config: FusedMoEQuantConfig, defer_input_quant: bool, ) -> Callable: has_scales = token_scales is not None # We yield before launching the dispatch kernel since the dispatch # kernel will block the CPU so we want to queue up all the compute # for the other ubatch before the dispatch kernel starts. dbo_yield_and_switch_from_compute_to_comm() # capture a DeepEP event and pass it as previous_event so # DeepEP honors the dependency internally. previous_event = dbo_get_previous_event(self.buffer.capture) ( num_tokens_per_rank, num_tokens_per_rdma_rank, dispatch_expert_num_tokens, is_token_in_rank, event, ) = self.buffer.get_dispatch_layout( topk_idx=rank_topk_ids, num_experts=num_experts, previous_event=previous_event, async_finish=False, allocate_on_comm_stream=False, ) token_data = tokens if has_scales: token_data = (tokens, token_scales) ( token_data, expert_topk_ids, expert_topk_weights, expert_num_tokens_per_expert_list, handle, event, ) = self.buffer.dispatch( x=token_data, handle=None, num_tokens_per_rank=num_tokens_per_rank, num_tokens_per_rdma_rank=num_tokens_per_rdma_rank, is_token_in_rank=is_token_in_rank, num_tokens_per_expert=dispatch_expert_num_tokens, topk_idx=rank_topk_ids, topk_weights=rank_topk_weights, # expert_alignment rounds the number of tokens per expert # to this value. expert_alignment=1, config=self._get_dispatch_config(), previous_event=previous_event, async_finish=self.async_prepare and not dbo_enabled(), allocate_on_comm_stream=False, ) # record the handle for this ubatch a2a_idx = dbo_current_ubatch_id() self.handles[a2a_idx] = handle dbo_switch_to_compute_sync() return lambda: self._receiver( event, has_scales, token_data, expert_topk_ids, num_experts, expert_num_tokens_per_expert_list, expert_topk_weights, a1_scale, quant_config, defer_input_quant=defer_input_quant, ) def _receiver( self, event: deep_ep.EventOverlap, has_scales: bool, token_data: tuple[torch.Tensor, torch.Tensor] | torch.Tensor, expert_topk_ids: torch.Tensor | None, num_experts: int, expert_num_tokens_per_expert_list: list[int], expert_topk_weights: torch.Tensor | None, a1_scale: torch.Tensor | None, quant_config: FusedMoEQuantConfig, defer_input_quant: bool, ) -> mk.PrepareResultType: if event.event is not None: event.current_stream_wait() if has_scales: expert_x, expert_x_scale = token_data else: expert_x, expert_x_scale = token_data, None # The existing MOE kernels assume that all entries of topk_ids are # valid. To that effect, set the -1s in expert_topk_ids to some expert # outside this rank so the expert_map can remap it to -1 when safe. # With Expert Parallel, the experts are divided amongst the rank # sequentially. For rank 0, set it to num_experts - 1 and for all other # ranks set it to 0 as we know that expert_map will have a -1 in those # regions for those ranks. # # DeepEP's topk_ids output refers to the local experts directly. Offset # the topk_ids to move it back to the global experts space so it aligns # with existing vLLM interfaces. assert expert_topk_ids is not None expert_topk_ids = torch.where( expert_topk_ids == -1, num_experts - 1 if self.rank_expert_offset == 0 else 0, expert_topk_ids + self.rank_expert_offset, ) # Makes a GPU-CPU copy. # TODO (varun): Maybe it is better to re-compute the expert_num_tokens # on GPU. expert_tokens_meta = mk.ExpertTokensMetadata.make_from_list( expert_num_tokens_per_expert_list, device=expert_x.device ) # * For non-block quant, dispatch in b16 and quantize now as # DeepEP kernels only support dispatching block scales. # * For expert kernels that require unquantized inputs, # defer quantization to FusedMoEExpertsPermuteUnpermute. if not quant_config.is_block_quantized and not defer_input_quant: # Quantize after dispatch. expert_x_scale = None if expert_x.numel() != 0: # TODO: support per_act_token_quant, expert_x, expert_x_scale = moe_kernel_quantize_input( expert_x, a1_scale, quant_dtype=quant_config.quant_dtype, per_act_token_quant=False, block_shape=quant_config.block_shape, ) return ( expert_x, expert_x_scale, expert_tokens_meta, expert_topk_ids, expert_topk_weights, ) def supports_async(self) -> bool: return True def prepare_async( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, num_experts: int, expert_map: torch.Tensor | None, apply_router_weight_on_input: bool, quant_config: FusedMoEQuantConfig, defer_input_quant: bool = False, ) -> mk.ReceiverType: if apply_router_weight_on_input: topk = topk_ids.size(1) # TODO: this only works for topK=1, will need to update for topK>1 assert topk == 1, ( "apply_router_weight_on_input is only implemented for topk=1" ) a1 = a1 * topk_weights.to(a1.dtype) # * DeepEP only supports fp8 block scales so quantize # before the dispatch for these models. # * For all other quantization, dispatch after. # * For expert kernels that require unquantized inputs, # defer quantization to FusedMoEExpertsPermuteUnpermute. if quant_config.is_block_quantized and not defer_input_quant: a1q, a1q_scale = moe_kernel_quantize_input( a1, quant_config.a1_scale, quant_dtype=quant_config.quant_dtype, per_act_token_quant=quant_config.per_act_token_quant, block_shape=quant_config.block_shape, ) if a1q_scale is not None and a1q_scale.numel() == 1: a1q_scale = a1q_scale.view(1, 1) a1_post_scale = None else: a1q = a1 a1q_scale = None a1_post_scale = ( quant_config.a1_gscale if quant_config.quant_dtype == "nvfp4" else quant_config.a1_scale ) return self._do_dispatch( tokens=a1q, token_scales=a1q_scale, rank_topk_ids=topk_ids, rank_topk_weights=topk_weights, num_experts=num_experts, a1_scale=a1_post_scale, quant_config=quant_config, defer_input_quant=defer_input_quant, ) def prepare( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, num_experts: int, expert_map: torch.Tensor | None, apply_router_weight_on_input: bool, quant_config: FusedMoEQuantConfig, defer_input_quant: bool = False, ) -> mk.PrepareResultType: receiver = self.prepare_async( a1, topk_weights, topk_ids, num_experts, expert_map, apply_router_weight_on_input, quant_config, defer_input_quant, ) return receiver() def _finalize( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, do_async: bool, ) -> Callable | None: a2a_idx = dbo_current_ubatch_id() handle = self.handles[a2a_idx] assert handle is not None # fused_expert_output can have 0 tokens - This happens when none of the # tokens from the all2all reach this EP rank. if fused_expert_output.numel() != 0: if isinstance(weight_and_reduce_impl, TopKWeightAndReduceDelegate): weight_and_reduce_impl = TopKWeightAndReduceContiguous() fused_expert_output = weight_and_reduce_impl.apply( output=None, fused_expert_output=fused_expert_output, topk_weights=topk_weights, topk_ids=topk_ids, apply_router_weight_on_input=apply_router_weight_on_input, ) dbo_yield_and_switch_from_compute_to_comm() assert fused_expert_output.dtype == torch.bfloat16, ( f"Expected fused_expert_output bfloat16, got {fused_expert_output.dtype}" ) previous_event = dbo_get_previous_event(self.buffer.capture) combined_x, _, event = self.buffer.combine( # HT combine only supports BF16 x=fused_expert_output, handle=handle, topk_weights=None, config=self._get_combine_config(), previous_event=previous_event, async_finish=do_async and not dbo_enabled(), allocate_on_comm_stream=False, ) dbo_switch_to_compute() if do_async: def _receiver(): if event.event is not None: event.current_stream_wait() dbo_switch_to_comm() # Respect inplace outputs. output.copy_(combined_x, non_blocking=True) # TODO(lucas): refactor the modular kernel so this will be # handled there dbo_yield_and_switch_from_comm_to_compute() return _receiver else: # TODO(lucas): support this case with the refactored modular kernel assert not dbo_enabled() # Respect inplace outputs. output.copy_(combined_x, non_blocking=True) return None def finalize_async( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, ) -> Callable: receiver = self._finalize( output, fused_expert_output, topk_weights, topk_ids, apply_router_weight_on_input, weight_and_reduce_impl, True, ) assert receiver is not None return receiver def finalize( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, ) -> None: self._finalize( output, fused_expert_output, topk_weights, topk_ids, apply_router_weight_on_input, weight_and_reduce_impl, False, )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/fused_moe/deepep_ht_prepare_finalize.py", "license": "Apache License 2.0", "lines": 391, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/layers/fused_moe/deepep_ll_prepare_finalize.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project from collections.abc import Callable import deep_ep import torch import vllm.model_executor.layers.fused_moe.modular_kernel as mk from vllm import envs from vllm.logger import init_logger from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig from vllm.model_executor.layers.fused_moe.topk_weight_and_reduce import ( TopKWeightAndReduceDelegate, ) from vllm.model_executor.layers.fused_moe.utils import ( moe_kernel_quantize_input, normalize_batched_scales_shape, ) from vllm.v1.worker.ubatching import ( dbo_current_ubatch_id, dbo_enabled, dbo_maybe_run_recv_hook, ) logger = init_logger(__name__) # DeepEP kernels quantize dispatch inputs in 128 element chunks. DEEPEP_QUANT_BLOCK_SIZE = 128 DEEPEP_QUANT_BLOCK_SHAPE = [DEEPEP_QUANT_BLOCK_SIZE, DEEPEP_QUANT_BLOCK_SIZE] logger = init_logger(__name__) def dequant_fp8( expert_x_fp8: torch.Tensor, expert_x_scales: torch.Tensor ) -> torch.Tensor: """ Return dequantized tensor in fp32 """ # TODO (varun) : Optimize leverage num_tokens_per_expert counts assert expert_x_fp8.is_contiguous() expert_x_scales = expert_x_scales.contiguous() num_experts = expert_x_fp8.size(0) expert_x_fp32 = expert_x_fp8.to(torch.float32).view( num_experts, -1, DEEPEP_QUANT_BLOCK_SIZE ) expert_x_scales = expert_x_scales.view(num_experts, -1, 1) return (expert_x_fp32 * expert_x_scales).view(expert_x_fp8.size()) class DeepEPLLPrepareAndFinalize(mk.FusedMoEPrepareAndFinalize): """ Prepare/Finalize using DeepEP low-latency kernels. """ # DeepEP low-latency kernels are compiled only for certain # specific hidden sizes. # NOTE: Keep this list sorted, maybe_roundup_layer_hidden_size depends # on it. SUPPORTED_HIDDEN_SIZES = [2048, 2560, 3072, 4096, 5120, 6144, 7168, 8192] @staticmethod def maybe_roundup_layer_hidden_size(hidden_size: int) -> int: # Round up hidden size to the closest supported hidden size. _supported_hs = DeepEPLLPrepareAndFinalize.SUPPORTED_HIDDEN_SIZES # Check sorted num_supported_hs = len(_supported_hs) assert all( [ _supported_hs[i] < _supported_hs[i + 1] for i in range(num_supported_hs - 1) ] ) for x in _supported_hs: if x >= hidden_size: return x raise ValueError( f"Hidden Size {hidden_size} is greater than the " f"maximum supported hidden size {_supported_hs[-1]}" ) def __init__( self, buffer: deep_ep.Buffer, max_tokens_per_rank: int, num_dispatchers: int, use_fp8_dispatch: bool = False, global_to_physical: torch.Tensor | None = None, physical_to_global: torch.Tensor | None = None, local_expert_global_ids: torch.Tensor | None = None, ): super().__init__() self.buffer = buffer self.max_tokens_per_rank = max_tokens_per_rank self.use_fp8_dispatch = use_fp8_dispatch # The dispatch function returns a handle that the combine function # requires. We store the handle here so it is available to the # combine function. self.handles: list[tuple | None] = [None, None] self.num_dispatchers_ = num_dispatchers topk_indices_dtype = self.topk_indices_dtype() def _maybe_cast(tensor: torch.Tensor | None) -> torch.Tensor | None: if tensor is None or topk_indices_dtype is None: return tensor return tensor.to(dtype=topk_indices_dtype) self.global_to_physical = _maybe_cast(global_to_physical) self.physical_to_global = _maybe_cast(physical_to_global) self.local_expert_global_ids = _maybe_cast(local_expert_global_ids) # We don't have enough information to determine if we should dispatch # activation scales in a packed ue8m0 format during object construction # time. This setting is handled by post_init_setup. self.use_ue8m0_dispatch = False def post_init_setup(self, fused_experts: mk.FusedMoEPermuteExpertsUnpermute): if not fused_experts.supports_packed_ue8m0_act_scales(): # Early exit. return if self.use_fp8_dispatch: logger.debug_once( "Update DeepEPLLPrepareFinalize to do packed ue8m0 scales dispatch." ) self.use_ue8m0_dispatch = True else: logger.warning_once( "DeepEPLLPrepareAndFinalize is setup to dispatch raw/unquantized " f"activations despite ({fused_experts.__class__.__name__}) being able " "to support quantized activations.", scope="local", ) def num_dispatchers(self) -> int: return self.num_dispatchers_ def output_is_reduced(self) -> bool: return True @property def activation_format(self) -> mk.FusedMoEActivationFormat: return mk.FusedMoEActivationFormat.BatchedExperts def max_num_tokens_per_rank(self) -> int | None: return self.max_tokens_per_rank def topk_indices_dtype(self) -> torch.dtype | None: return torch.int64 def _map_global_to_physical_ids(self, topk_ids: torch.Tensor) -> torch.Tensor: if self.global_to_physical is None: return topk_ids return self.global_to_physical[topk_ids] def _map_local_to_global_ids(self, expert_topk_ids: torch.Tensor) -> torch.Tensor: if self.local_expert_global_ids is None: return expert_topk_ids return self.local_expert_global_ids[expert_topk_ids] def _do_quant( self, x: torch.Tensor | tuple[torch.Tensor, torch.Tensor], a1_dtype: torch.dtype, quant_config: FusedMoEQuantConfig, ) -> tuple[torch.Tensor, torch.Tensor | None]: if self.use_fp8_dispatch: block_k = ( quant_config.block_shape[1] if quant_config.block_shape is not None else None ) if block_k == DEEPEP_QUANT_BLOCK_SIZE: # DeepEP kernels did the quantization for us. x, x_scales = x return x, x_scales # Dequant to get back the tokens in the datatype we dispatched in. x_fp8, x_scales = x x = dequant_fp8(x_fp8, x_scales).to(dtype=a1_dtype) assert isinstance(x, (torch.Tensor, tuple)) q_dtype = quant_config.quant_dtype if q_dtype == "nvfp4" and envs.VLLM_DEEPEPLL_NVFP4_DISPATCH: logger.info_once( "Since VLLM_DEEPEPLL_NVFP4_DISPATCH==1, make sure " "using the hybrid-ep branch of DeepEP" "(https://github.com/deepseek-ai/DeepEP/tree/hybrid-ep)" ) assert isinstance(x, tuple) x_scales = x[1] x = x[0].permute(2, 0, 1) num_experts, max_tokens, hidden_dim_by_2 = x.shape hidden_dim = hidden_dim_by_2 * 2 logger.info_once( "Quantization is fused with DeepEP nvfp4 dispatch for " "FlashInfer CUTEDSL as VLLM_DEEPEPLL_NVFP4_DISPATCH==1" ) else: if q_dtype == "nvfp4": q_dtype = None logger.info_once( "Using DeepEP bfloat16 dispatch for FlashInfer CUTEDSL as " "VLLM_DEEPEPLL_NVFP4_DISPATCH==0" ) assert isinstance(x, torch.Tensor) num_experts, max_tokens, hidden_dim = x.size() # TODO (varun): Optimization - Use a batched version of quant x = x.view((-1, hidden_dim)) x, x_scales = moe_kernel_quantize_input( x, quant_config.a1_scale, q_dtype, quant_config.per_act_token_quant, quant_config.block_shape, ) x = x.view((num_experts, -1, hidden_dim)) if q_dtype is not None and q_dtype != "nvfp4": assert x_scales is not None x_scales = normalize_batched_scales_shape(x_scales, num_experts) return x, x_scales def supports_async(self) -> bool: return True def prepare_async( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, num_experts: int, expert_map: torch.Tensor | None, apply_router_weight_on_input: bool, quant_config: FusedMoEQuantConfig, defer_input_quant: bool = False, ) -> tuple[Callable, mk.ReceiverType]: if defer_input_quant: raise NotImplementedError( f"{self.__class__.__name__} does not support defer_input_quant=True. " "Please select an MoE kernel that accepts quantized inputs." ) hidden_size = a1.size(1) assert hidden_size in self.SUPPORTED_HIDDEN_SIZES, ( f"Hidden Size {hidden_size} not in supported list of hidden sizes" f"{self.SUPPORTED_HIDDEN_SIZES}" ) a2a_idx = dbo_current_ubatch_id() if self.use_fp8_dispatch: assert hidden_size % 128 == 0, ( "DeepEP kernels quantize the inputs in blocks of shape 128" ) use_nvfp4 = False nvfp4_dispatch = ( quant_config.quant_dtype == "nvfp4" and envs.VLLM_DEEPEPLL_NVFP4_DISPATCH ) if nvfp4_dispatch: use_nvfp4 = True qc_a1_gscale_or_scale = ( quant_config.a1_gscale if nvfp4_dispatch else quant_config.a1_scale ) has_per_token_scales = ( qc_a1_gscale_or_scale.numel() != 1 if qc_a1_gscale_or_scale is not None else ( quant_config.a2_scale.numel() != 1 if quant_config.a2_scale is not None else False ) ) if not use_nvfp4: assert not has_per_token_scales, ( "low_latency kernels doesn't support dispatching per-token scales" ) if apply_router_weight_on_input: topk = topk_ids.size(1) # TODO: this only works for topK=1, will need to update for topK>1 assert topk == 1, ( "apply_router_weight_on_input is only implemented for topk=1" ) a1 = a1 * topk_weights.to(a1.dtype) # Dispatch dispatch_topk_ids = self._map_global_to_physical_ids(topk_ids) expert_x, expert_num_tokens, handle, _, hook = self.buffer.low_latency_dispatch( a1, dispatch_topk_ids, self.max_tokens_per_rank, num_experts, use_fp8=self.use_fp8_dispatch, round_scale=self.use_ue8m0_dispatch, use_ue8m0=self.use_ue8m0_dispatch, **(dict(use_nvfp4=True) if use_nvfp4 else dict()), **( dict(x_global_scale=qc_a1_gscale_or_scale) if qc_a1_gscale_or_scale is not None else dict() ), async_finish=False, return_recv_hook=True, ) self.handles[a2a_idx] = handle return ( hook, lambda: self._receiver( expert_x, expert_num_tokens, quant_config.a1_scale, a1.dtype, quant_config, ), ) def _receiver( self, expert_x: torch.Tensor | tuple[torch.Tensor, torch.Tensor], expert_num_tokens: torch.Tensor, a1_scale: torch.Tensor | None, a1_dtype: torch.dtype, quant_config: FusedMoEQuantConfig, ) -> mk.PrepareResultType: expert_x, expert_x_scale = self._do_quant(expert_x, a1_dtype, quant_config) expert_tokens_meta = mk.ExpertTokensMetadata( expert_num_tokens=expert_num_tokens, expert_num_tokens_cpu=None ) return expert_x, expert_x_scale, expert_tokens_meta, None, None def prepare( self, a1: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, num_experts: int, expert_map: torch.Tensor | None, apply_router_weight_on_input: bool, quant_config: FusedMoEQuantConfig, defer_input_quant: bool = False, ) -> mk.PrepareResultType: if defer_input_quant: raise NotImplementedError( f"{self.__class__.__name__} does not support defer_input_quant=True. " "Please select an MoE kernel that accepts quantized inputs." ) hook, receiver = self.prepare_async( a1, topk_weights, topk_ids, num_experts, expert_map, apply_router_weight_on_input, quant_config, ) hook() return receiver() def _finalize( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, do_async: bool, ) -> tuple[Callable, Callable]: assert isinstance(weight_and_reduce_impl, TopKWeightAndReduceDelegate), ( "Weight application and reduction happens in the combine kernel." ) a2a_idx = dbo_current_ubatch_id() do_recv_hook = dbo_enabled() or do_async handle = self.handles[a2a_idx] assert handle is not None combine_topk_weights = topk_weights if apply_router_weight_on_input: # weights have already been applied. combine_topk_weights = torch.ones_like(topk_weights) combine_topk_ids = self._map_global_to_physical_ids(topk_ids) # TODO (varun) : Enable zero copy mode dbo_maybe_run_recv_hook() _, _, recv_hook = self.buffer.low_latency_combine( fused_expert_output, combine_topk_ids, combine_topk_weights, handle, async_finish=False, zero_copy=False, return_recv_hook=do_recv_hook, out=output, ) return recv_hook, lambda: None def finalize_async( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, ) -> tuple[Callable, Callable]: return self._finalize( output, fused_expert_output, topk_weights, topk_ids, apply_router_weight_on_input, weight_and_reduce_impl, do_async=True, ) def finalize( self, output: torch.Tensor, fused_expert_output: torch.Tensor, topk_weights: torch.Tensor, topk_ids: torch.Tensor, apply_router_weight_on_input: bool, weight_and_reduce_impl: mk.TopKWeightAndReduce, ) -> None: self._finalize( output, fused_expert_output, topk_weights, topk_ids, apply_router_weight_on_input, weight_and_reduce_impl, do_async=False, )
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/layers/fused_moe/deepep_ll_prepare_finalize.py", "license": "Apache License 2.0", "lines": 392, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:vllm/model_executor/models/tarsier.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import math from collections.abc import Iterable, Mapping, Sequence from typing import Annotated, Final, Literal, Protocol, TypeAlias, TypeVar import torch import torch.nn as nn from transformers import ( BatchFeature, CLIPVisionConfig, PretrainedConfig, SiglipVisionConfig, ) from transformers import LlavaConfig as HfLlavaConfig from transformers.image_utils import ImageInput, get_image_size, to_numpy_array from transformers.models.llava import LlavaProcessor from transformers.processing_utils import ProcessingKwargs, Unpack from transformers.tokenization_utils_base import PreTokenizedInput, TextInput from vllm.config import VllmConfig from vllm.model_executor.layers.activation import get_act_fn from vllm.model_executor.layers.linear import ColumnParallelLinear, RowParallelLinear from vllm.model_executor.layers.quantization import QuantizationConfig from vllm.model_executor.models.llava import LlavaDummyInputsBuilder from vllm.multimodal import MULTIMODAL_REGISTRY from vllm.multimodal.cache import BaseMultiModalProcessorCache from vllm.multimodal.inputs import MultiModalFieldConfig, MultiModalKwargsItems from vllm.multimodal.parse import ( ImageEmbeddingItems, ImageProcessorItems, ImageSize, MultiModalDataItems, ) from vllm.multimodal.processing import ( BaseDummyInputsBuilder, BaseMultiModalProcessor, BaseProcessingInfo, InputProcessingContext, PromptReplacement, PromptUpdate, ) from vllm.sequence import IntermediateTensors from vllm.utils.tensor_schema import TensorSchema, TensorShape from .clip import CLIPVisionModel from .interfaces import MultiModalEmbeddings, SupportsMultiModal, SupportsPP from .siglip import SiglipVisionModel from .utils import ( AutoWeightsLoader, get_layer_index, init_vllm_registered_model, maybe_prefix, ) from .vision import ( VisionEncoderInfo, get_num_selected_vision_tokens, get_vision_encoder_info, ) class TarsierImagePixelInputs(TensorSchema): """ Dimensions: - bn: Batch size * number of images - c: Number of channels (3) - h: Height - w: Width """ type: Literal["pixel_values"] = "pixel_values" pixel_values: Annotated[torch.Tensor, TensorShape("bn", 3, "h", "w")] class TarsierImageEmbeddingInputs(TensorSchema): """ Dimensions: - bn: Batch size * number of images - ifs: Image feature size - hs: Hidden size (must match the hidden size of language model backbone) """ type: Literal["image_embeds"] = "image_embeds" data: Annotated[torch.Tensor, TensorShape("bn", "ifs", "hs")] TarsierImageInputs: TypeAlias = TarsierImagePixelInputs | TarsierImageEmbeddingInputs class TarsierHfConfig(Protocol): # Based on the Tarsier's LlavaConfig vision_config: Final[PretrainedConfig] text_config: Final[PretrainedConfig] # Added from Tarsier's LlavaConfig image_token_index: Final[int] vision_feature_select_strategy: Final[str] vision_feature_layer: Final[int | list[int]] projector_hidden_act: Final[str] image_newline_idx: Final[int] image_new_idx: Final[int] multimodal_projector_bias: bool = True class TarsierProcessorKwargs(ProcessingKwargs, total=False): _defaults = { "text_kwargs": { "padding": False, }, "images_kwargs": {}, } class TarsierProcessor(LlavaProcessor): def __call__( self, images: ImageInput = None, text: TextInput | PreTokenizedInput | list[TextInput] | list[PreTokenizedInput] = None, audio=None, videos=None, **kwargs: Unpack[TarsierProcessorKwargs], ) -> BatchFeature: if images is None and text is None: raise ValueError("You have to specify at least one of `images` or `text`.") output_kwargs = self._merge_kwargs( TarsierProcessorKwargs, tokenizer_init_kwargs=self.tokenizer.init_kwargs, **kwargs, ) if images is not None: image_inputs = self.image_processor( images, **output_kwargs["images_kwargs"] ) else: image_inputs = {} if isinstance(text, str): text = [text] elif not isinstance(text, list) and not isinstance(text[0], str): raise ValueError( "Invalid input text. Please provide a string, or a list of strings" ) # try to expand inputs in processing if we have the necessary parts prompt_strings = text if image_inputs.get("pixel_values") is not None: # Replace the image token with the expanded image token sequence pixel_values = image_inputs["pixel_values"] height, width = get_image_size(to_numpy_array(pixel_values[0])) num_image_tokens = ( (height // self.patch_size) * (width // self.patch_size + 1) + self.num_additional_image_tokens + 1 ) if self.vision_feature_select_strategy == "default": num_image_tokens -= 1 prompt_strings = [] for sample in text: sample = sample.replace( self.image_token, self.image_token * num_image_tokens ) prompt_strings.append(sample) return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None) text_inputs = self.tokenizer(prompt_strings, **output_kwargs["text_kwargs"]) return BatchFeature( data={**text_inputs, **image_inputs}, tensor_type=return_tensors ) class TarsierMultiModalProjector(nn.Module): def __init__( self, vision_hidden_size: int, text_hidden_size: int, projector_hidden_act: str, multimodal_projector_bias: bool, quant_config: QuantizationConfig | None = None, prefix: str = "", ): super().__init__() self.linear_1 = ColumnParallelLinear( vision_hidden_size, text_hidden_size, bias=multimodal_projector_bias, quant_config=quant_config, prefix=f"{prefix}.linear_1", ) self.act = get_act_fn(projector_hidden_act) self.linear_2 = RowParallelLinear( text_hidden_size, text_hidden_size, bias=multimodal_projector_bias, quant_config=quant_config, prefix=f"{prefix}.linear_2", ) def forward(self, image_features: torch.Tensor) -> torch.Tensor: hidden_states, _ = self.linear_1(image_features) hidden_states = self.act(hidden_states) hidden_states, _ = self.linear_2(hidden_states) return hidden_states class TarsierProcessingInfo(BaseProcessingInfo): def get_hf_config(self) -> TarsierHfConfig: return self.ctx.get_hf_config(HfLlavaConfig) def get_vision_encoder_info(self) -> VisionEncoderInfo: return get_vision_encoder_info(self.get_hf_config()) def get_hf_processor(self, **kwargs: object) -> TarsierProcessor: vision_info = self.get_vision_encoder_info() kwargs.setdefault("patch_size", vision_info.get_patch_size()) return self.ctx.get_hf_processor(TarsierProcessor, **kwargs) def get_supported_mm_limits(self) -> Mapping[str, int | None]: return {"image": None} def get_num_image_tokens( self, *, image_width: int, image_height: int, ) -> int: hf_config = self.get_hf_config() vision_encoder_info = self.get_vision_encoder_info() num_projected_patches = get_num_selected_vision_tokens( vision_encoder_info.get_num_image_tokens( image_width=image_width, image_height=image_height, ), hf_config.vision_feature_select_strategy, ) if num_projected_patches <= 0: default_size = self.get_image_size_with_most_features() num_projected_patches_default = get_num_selected_vision_tokens( vision_encoder_info.get_num_image_tokens( image_width=default_size.width, image_height=default_size.height, ), hf_config.vision_feature_select_strategy, ) if num_projected_patches_default <= 0: raise ValueError("Could not determine a valid number of image patches.") num_projected_patches = num_projected_patches_default num_height_patches = int(math.sqrt(num_projected_patches)) total_image_tokens_for_llm = num_projected_patches + num_height_patches + 1 return total_image_tokens_for_llm def get_image_size_with_most_features(self) -> ImageSize: vision_encoder_info = self.get_vision_encoder_info() width = height = vision_encoder_info.get_image_size() return ImageSize(width=width, height=height) def get_max_image_tokens(self) -> int: target_width, target_height = self.get_image_size_with_most_features() return self.get_num_image_tokens( image_width=target_width, image_height=target_height, ) def get_image_newline_idx(self) -> int: return self.get_hf_config().image_newline_idx def get_image_new_idx(self) -> int: return self.get_hf_config().image_new_idx _I_Tarsier = TypeVar("_I_Tarsier", bound=TarsierProcessingInfo) class TarsierDummyInputsBuilder(LlavaDummyInputsBuilder[_I_Tarsier]): pass class TarsierMultiModalProcessor(BaseMultiModalProcessor[_I_Tarsier]): def _get_mm_fields_config( self, hf_inputs: BatchFeature, hf_processor_mm_kwargs: Mapping[str, object], ) -> Mapping[str, MultiModalFieldConfig]: return dict( pixel_values=MultiModalFieldConfig.batched("image"), image_embeds=MultiModalFieldConfig.batched("image"), ) def _get_prompt_updates( self, mm_items: MultiModalDataItems, hf_processor_mm_kwargs: Mapping[str, object], out_mm_kwargs: MultiModalKwargsItems, ) -> Sequence[PromptUpdate]: hf_config = self.info.get_hf_config() image_token_id = hf_config.image_token_index # The <IMAGE> token ID def get_replacement(item_idx: int): images = mm_items.get_items( "image", (ImageEmbeddingItems, ImageProcessorItems) ) if isinstance(images, ImageEmbeddingItems): num_projected_patches = images.get_feature_size(item_idx) # This assumes num_projected_patches is a perfect square num_height_patches = int(math.sqrt(num_projected_patches)) num_final_image_tokens = num_projected_patches + num_height_patches + 1 else: image_size = images.get_image_size(item_idx) num_final_image_tokens = self.info.get_num_image_tokens( image_width=image_size.width, image_height=image_size.height, ) return [image_token_id] * num_final_image_tokens return [ PromptReplacement( modality="image", target=[image_token_id], # Replace each single <IMAGE> token replacement=get_replacement, ), ] def _build_tarsier_hf_info(ctx: InputProcessingContext) -> TarsierProcessingInfo: return TarsierProcessingInfo(ctx) def _build_tarsier_hf_processor( info: _I_Tarsier, dummy_inputs: BaseDummyInputsBuilder[_I_Tarsier], *, cache: BaseMultiModalProcessorCache | None = None, ) -> BaseMultiModalProcessor: if isinstance(info, TarsierProcessingInfo): return TarsierMultiModalProcessor( info, dummy_inputs, cache=cache, ) raise NotImplementedError(type(info)) def init_vision_tower_for_tarsier( hf_config: TarsierHfConfig, # Use the Tarsier specific config protocol quant_config: QuantizationConfig | None, *, require_post_norm: bool | None = None, prefix: str = "", ) -> CLIPVisionModel | SiglipVisionModel: vision_config = hf_config.vision_config feature_layers = hf_config.vision_feature_layer base_num_hidden_layers = vision_config.num_hidden_layers if isinstance(feature_layers, int): num_hidden_layers_to_init = get_layer_index( feature_layers, base_num_hidden_layers ) elif isinstance(feature_layers, (list, tuple)): num_hidden_layers_to_init = max( get_layer_index(idx, base_num_hidden_layers) for idx in feature_layers ) else: raise TypeError( f"vision_layer_feature type: {type(feature_layers)} is not supported" ) if isinstance(vision_config, CLIPVisionConfig): return CLIPVisionModel( vision_config, quant_config=quant_config, num_hidden_layers_override=num_hidden_layers_to_init, require_post_norm=require_post_norm, prefix=prefix, ) elif isinstance(vision_config, SiglipVisionConfig): return SiglipVisionModel( vision_config, quant_config=quant_config, num_hidden_layers_override=num_hidden_layers_to_init, require_post_norm=require_post_norm, prefix=prefix, ) msg = f"Unsupported vision config for Tarsier: {type(vision_config)}" raise NotImplementedError(msg) @MULTIMODAL_REGISTRY.register_processor( _build_tarsier_hf_processor, info=_build_tarsier_hf_info, dummy_inputs=TarsierDummyInputsBuilder, ) class TarsierForConditionalGeneration(nn.Module, SupportsMultiModal, SupportsPP): packed_modules_mapping = { "qkv_proj": ["q_proj", "k_proj", "v_proj"], "gate_up_proj": ["gate_proj", "up_proj"], } @classmethod def get_placeholder_str(cls, modality: str, i: int) -> str | None: if modality.startswith("image"): return "<image>" raise ValueError("Only image modality is supported") def __init__(self, *, vllm_config: VllmConfig, prefix: str = "") -> None: super().__init__() config: TarsierHfConfig = vllm_config.model_config.hf_config quant_config = vllm_config.quant_config self.config = config # Storing the Tarsier-specific HF config with self._mark_tower_model(vllm_config, "image"): self.vision_tower = init_vision_tower_for_tarsier( config, quant_config=quant_config, require_post_norm=False, prefix=maybe_prefix(prefix, "vision_tower"), ) projector_bias = getattr(config, "multimodal_projector_bias", True) self.multi_modal_projector = TarsierMultiModalProjector( vision_hidden_size=config.vision_config.hidden_size, text_hidden_size=config.text_config.hidden_size, projector_hidden_act=config.projector_hidden_act, multimodal_projector_bias=projector_bias, quant_config=quant_config, prefix=maybe_prefix(prefix, "multi_modal_projector"), ) self.register_buffer( "image_newline_idx_tensor", torch.tensor([config.image_newline_idx], dtype=torch.long), persistent=False, ) self.register_buffer( "image_new_idx_tensor", torch.tensor([config.image_new_idx], dtype=torch.long), persistent=False, ) with self._mark_language_model(vllm_config): self.language_model = init_vllm_registered_model( vllm_config=vllm_config, # Use text_config from Tarsier's main config hf_config=config.text_config, prefix=maybe_prefix(prefix, "language_model"), ) self.make_empty_intermediate_tensors = ( self.language_model.make_empty_intermediate_tensors ) def _parse_and_validate_image_input( self, **kwargs: object ) -> TarsierImageInputs | None: pixel_values = kwargs.pop("pixel_values", None) image_embeds = kwargs.pop("image_embeds", None) if pixel_values is None and image_embeds is None: return None if pixel_values is not None: return TarsierImagePixelInputs( type="pixel_values", pixel_values=pixel_values, ) if image_embeds is not None: return TarsierImageEmbeddingInputs( type="image_embeds", data=image_embeds, ) raise AssertionError("This line should be unreachable.") def _image_pixels_to_features( self, vision_tower: CLIPVisionModel | SiglipVisionModel, pixel_values: torch.Tensor | list[torch.Tensor], ) -> torch.Tensor | tuple[torch.Tensor, ...]: # From vLLM LLaVA, vision tower output handling return vision_tower( pixel_values, feature_select_strategy=self.config.vision_feature_select_strategy, ) def _add_tarsier_split_tokens( self, projected_image_features: torch.Tensor ) -> torch.Tensor: """ Implements Tarsier's `add_split_tokens` logic. """ num_images, num_projected_patches, embed_dim = projected_image_features.shape num_height_patches = int(math.sqrt(num_projected_patches)) num_width_patches = num_projected_patches // num_height_patches device = projected_image_features.device embedding_layer = self.language_model.model.embed_tokens image_newline_emb = embedding_layer( self.image_newline_idx_tensor.to(device) ).squeeze(0) image_new_emb = embedding_layer(self.image_new_idx_tensor.to(device)).squeeze(0) try: current_image_features_grid = projected_image_features.view( num_images, num_height_patches, num_width_patches, embed_dim ) except RuntimeError as e: raise RuntimeError( "Cannot reshape projected_image_features" f" with shape {projected_image_features.shape} " f"to ({num_images}, {num_height_patches}," f" {num_width_patches}, {embed_dim}). " "Ensure num_projected_patches is compatible" " with a grid structure. " f"num_projected_patches={num_projected_patches}, " f"derived num_height_patches={num_height_patches}. " ) from e image_newline_expanded = image_newline_emb.expand( (num_images, num_height_patches, 1, embed_dim) ) features_with_newlines = torch.cat( [current_image_features_grid, image_newline_expanded], dim=2, # Concatenate along width dim ) new_num_patches_after_newline = num_projected_patches + num_height_patches features_with_newlines_flat = features_with_newlines.view( num_images, new_num_patches_after_newline, embed_dim ) image_new_expanded = image_new_emb.expand((num_images, 1, embed_dim)) final_image_features = torch.cat( [features_with_newlines_flat, image_new_expanded], dim=1, # Concatenate along patch sequence dim ) return final_image_features def _process_image_pixels( self, inputs: TarsierImagePixelInputs, ) -> torch.Tensor | tuple[torch.Tensor, ...]: pixel_values = inputs["pixel_values"] image_features_selected = self._image_pixels_to_features( self.vision_tower, pixel_values ) # type: ignore if isinstance(image_features_selected, torch.Tensor): projected_features = self.multi_modal_projector(image_features_selected) final_features = self._add_tarsier_split_tokens(projected_features) return final_features else: raise TypeError( f"_image_pixels_to_features type:" f" {type(image_features_selected)} is not supported" ) def _process_image_input( self, image_input: TarsierImageInputs, ) -> torch.Tensor | tuple[torch.Tensor, ...]: if image_input["type"] == "image_embeds": projected_features = image_input["data"] if isinstance(projected_features, torch.Tensor): return self._add_tarsier_split_tokens(projected_features) else: raise ValueError( "Incorrect type of image_embeds. " f"Got type: {type(projected_features)}. " ) return self._process_image_pixels(image_input) def embed_multimodal(self, **kwargs: object) -> MultiModalEmbeddings: image_input = self._parse_and_validate_image_input(**kwargs) if image_input is None: return [] return self._process_image_input(image_input) def forward( self, input_ids: torch.Tensor | None, positions: torch.Tensor, intermediate_tensors: IntermediateTensors | None = None, inputs_embeds: torch.Tensor | None = None, **kwargs: object, ) -> torch.Tensor | IntermediateTensors: if intermediate_tensors is not None: inputs_embeds = None hidden_states = self.language_model.model( input_ids=input_ids, positions=positions, intermediate_tensors=intermediate_tensors, inputs_embeds=inputs_embeds, ) return hidden_states def compute_logits( self, hidden_states: torch.Tensor, ) -> torch.Tensor | None: return self.language_model.compute_logits(hidden_states) def load_weights(self, weights: Iterable[tuple[str, torch.Tensor]]) -> set[str]: loader = AutoWeightsLoader(self) return loader.load_weights(weights)
{ "repo_id": "vllm-project/vllm", "file_path": "vllm/model_executor/models/tarsier.py", "license": "Apache License 2.0", "lines": 526, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
license
vllm-project/vllm:tests/entrypoints/test_api_server_process_manager.py
# SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import multiprocessing import socket import threading import time from unittest.mock import patch import pytest from vllm.v1.utils import APIServerProcessManager, wait_for_completion_or_failure # Global variables to control worker behavior WORKER_RUNTIME_SECONDS = 0.5 # Mock implementation of run_api_server_worker def mock_run_api_server_worker(listen_address, sock, args, client_config=None): """Mock run_api_server_worker that runs for a specific time.""" print(f"Mock worker started with client_config: {client_config}") time.sleep(WORKER_RUNTIME_SECONDS) print("Mock worker completed successfully") @pytest.fixture def api_server_args(): """Fixture to provide arguments for APIServerProcessManager.""" sock = socket.socket() return { "target_server_fn": mock_run_api_server_worker, "listen_address": "localhost:8000", "sock": sock, "args": "test_args", # Simple string to avoid pickling issues "num_servers": 3, "input_addresses": [ "tcp://127.0.0.1:5001", "tcp://127.0.0.1:5002", "tcp://127.0.0.1:5003", ], "output_addresses": [ "tcp://127.0.0.1:6001", "tcp://127.0.0.1:6002", "tcp://127.0.0.1:6003", ], "stats_update_address": "tcp://127.0.0.1:7000", } @pytest.mark.parametrize("with_stats_update", [True, False]) def test_api_server_process_manager_init(api_server_args, with_stats_update): """Test initializing the APIServerProcessManager.""" # Set the worker runtime to ensure tests complete in reasonable time global WORKER_RUNTIME_SECONDS WORKER_RUNTIME_SECONDS = 0.5 # Copy the args to avoid mutating them args = api_server_args.copy() if not with_stats_update: args.pop("stats_update_address") manager = APIServerProcessManager(**args) try: # Verify the manager was initialized correctly assert len(manager.processes) == 3 # Verify all processes are running for proc in manager.processes: assert proc.is_alive() print("Waiting for processes to run...") time.sleep(WORKER_RUNTIME_SECONDS / 2) # They should still be alive at this point for proc in manager.processes: assert proc.is_alive() finally: # Always clean up the processes print("Cleaning up processes...") manager.close() # Give processes time to terminate time.sleep(0.2) # Verify all processes were terminated for proc in manager.processes: assert not proc.is_alive() @patch( "vllm.entrypoints.cli.serve.run_api_server_worker_proc", mock_run_api_server_worker ) def test_wait_for_completion_or_failure(api_server_args): """Test that wait_for_completion_or_failure works with failures.""" global WORKER_RUNTIME_SECONDS WORKER_RUNTIME_SECONDS = 1.0 # Create the manager manager = APIServerProcessManager(**api_server_args) try: assert len(manager.processes) == 3 # Create a result capture for the thread result: dict[str, Exception | None] = {"exception": None} def run_with_exception_capture(): try: wait_for_completion_or_failure(api_server_manager=manager) except Exception as e: result["exception"] = e # Start a thread to run wait_for_completion_or_failure wait_thread = threading.Thread(target=run_with_exception_capture, daemon=True) wait_thread.start() # Let all processes run for a short time time.sleep(0.2) # All processes should still be running assert all(proc.is_alive() for proc in manager.processes) # Now simulate a process failure print("Simulating process failure...") manager.processes[0].terminate() # Wait for the wait_for_completion_or_failure # to detect and handle the failure # This should trigger it to terminate all other processes wait_thread.join(timeout=1.0) # The wait thread should have exited assert not wait_thread.is_alive() # Verify that an exception was raised with appropriate error message assert result["exception"] is not None assert "died with exit code" in str(result["exception"]) # All processes should now be terminated for i, proc in enumerate(manager.processes): assert not proc.is_alive(), f"Process {i} should not be alive" finally: manager.close() time.sleep(0.2) @pytest.mark.timeout(30) def test_normal_completion(api_server_args): """Test that wait_for_completion_or_failure works in normal completion.""" global WORKER_RUNTIME_SECONDS WORKER_RUNTIME_SECONDS = 0.1 # Create the manager manager = APIServerProcessManager(**api_server_args) try: # Give processes time to terminate # wait for processes to complete remaining_processes = manager.processes.copy() while remaining_processes: for proc in remaining_processes: if not proc.is_alive(): remaining_processes.remove(proc) time.sleep(0.1) # Verify all processes have terminated for i, proc in enumerate(manager.processes): assert not proc.is_alive(), f"Process {i} still alive after terminate()" # Now call wait_for_completion_or_failure # since all processes have already # terminated, it should return immediately # with no error wait_for_completion_or_failure(api_server_manager=manager) finally: # Clean up just in case manager.close() time.sleep(0.2) @pytest.mark.timeout(30) def test_external_process_monitoring(api_server_args): """Test that wait_for_completion_or_failure handles additional processes.""" global WORKER_RUNTIME_SECONDS WORKER_RUNTIME_SECONDS = 100 # Create and start the external process # (simulates local_engine_manager or coordinator) spawn_context = multiprocessing.get_context("spawn") external_proc = spawn_context.Process( target=mock_run_api_server_worker, name="MockExternalProcess" ) external_proc.start() # Create the class to simulate a coordinator class MockCoordinator: def __init__(self, proc): self.proc = proc def close(self): if self.proc.is_alive(): self.proc.terminate() self.proc.join(timeout=0.5) # Create a mock coordinator with the external process mock_coordinator = MockCoordinator(external_proc) # Create the API server manager manager = APIServerProcessManager(**api_server_args) try: # Verify manager initialization assert len(manager.processes) == 3 # Create a result capture for the thread result: dict[str, Exception | None] = {"exception": None} def run_with_exception_capture(): try: wait_for_completion_or_failure( api_server_manager=manager, coordinator=mock_coordinator ) except Exception as e: result["exception"] = e # Start a thread to run wait_for_completion_or_failure wait_thread = threading.Thread(target=run_with_exception_capture, daemon=True) wait_thread.start() # Terminate the external process to trigger a failure time.sleep(0.2) external_proc.terminate() # Wait for the thread to detect the failure wait_thread.join(timeout=1.0) # The wait thread should have completed assert not wait_thread.is_alive(), ( "wait_for_completion_or_failure thread still running" ) # Verify that an exception was raised with appropriate error message assert result["exception"] is not None, "No exception was raised" error_message = str(result["exception"]) assert "died with exit code" in error_message, ( f"Unexpected error message: {error_message}" ) assert "MockExternalProcess" in error_message, ( f"Error doesn't mention external process: {error_message}" ) # Verify that all API server processes were terminated as a result for i, proc in enumerate(manager.processes): assert not proc.is_alive(), f"API server process {i} was not terminated" finally: # Clean up manager.close() mock_coordinator.close() time.sleep(0.2)
{ "repo_id": "vllm-project/vllm", "file_path": "tests/entrypoints/test_api_server_process_manager.py", "license": "Apache License 2.0", "lines": 206, "canary_id": -1, "canary_value": "", "pii_type": "", "provider": "", "regex_pattern": "", "repetition": -1, "template": "" }
test