utils.py 14.4 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
import math
4
from collections import defaultdict
5
from dataclasses import dataclass, field
6

7
import torch
8
from typing_extensions import deprecated
9

10
from vllm.attention.layer import Attention
11
from vllm.config import CacheConfig, ModelConfig, SchedulerConfig, VllmConfig
12
from vllm.logger import init_logger
13
from vllm.model_executor.models.interfaces import MultiModalEmbeddings
14
from vllm.model_executor.models.utils import extract_layer_index
15
from vllm.multimodal.cache import processor_only_cache_from_config
16
from vllm.multimodal.registry import MultiModalRegistry
17
from vllm.platforms import current_platform
18
from vllm.utils.mem_utils import MemorySnapshot, format_gib
19
from vllm.v1.attention.backend import AttentionBackend, AttentionMetadataBuilder
20
from vllm.v1.core.encoder_cache_manager import compute_mm_encoder_budget
21
from vllm.v1.kv_cache_interface import KVCacheGroupSpec, KVCacheSpec
22

23
24
logger = init_logger(__name__)

25

26
27
28
29
30
class MultiModalBudget:
    """Helper class to calculate budget information for multi-modal models."""

    def __init__(
        self,
31
        model_config: ModelConfig,
32
33
34
35
36
        scheduler_config: SchedulerConfig,
        mm_registry: MultiModalRegistry,
    ) -> None:
        super().__init__()

37
        self.model_config = model_config
38
39
        self.scheduler_config = scheduler_config
        self.mm_registry = mm_registry
40
        self.cache = cache = processor_only_cache_from_config(model_config, mm_registry)
41

42
        self.max_model_len = model_config.max_model_len
43
44
        self.max_num_reqs = scheduler_config.max_num_seqs

45
        self.mm_limits = mm_registry.get_mm_limits_per_prompt(model_config, cache=cache)
46

47
        max_tokens_by_modality = mm_registry.get_max_tokens_per_item_by_modality(
48
            model_config,
49
50
            cache=cache,
            profiler_limits=self.mm_limits,
51
        )
52
53
54
55

        encoder_compute_budget, encoder_cache_size = compute_mm_encoder_budget(
            scheduler_config,
            max_tokens_by_modality,
56
57
        )

58
        self.encoder_compute_budget = encoder_compute_budget
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        self.encoder_cache_size = encoder_cache_size

        max_items_per_prompt_by_modality = dict[str, int]()
        max_items_per_batch_by_modality = dict[str, int]()

        for modality, max_tokens in max_tokens_by_modality.items():
            (
                max_items_per_prompt,
                max_items_per_batch,
            ) = self.get_max_items(modality, max_tokens)

            max_items_per_prompt_by_modality[modality] = max_items_per_prompt
            max_items_per_batch_by_modality[modality] = max_items_per_batch

        self.max_tokens_by_modality = max_tokens_by_modality
        self.max_items_per_prompt_by_modality = max_items_per_prompt_by_modality
        self.max_items_per_batch_by_modality = max_items_per_batch_by_modality

77
    def get_modality_with_max_tokens(self) -> str:
78
        max_tokens_by_modality = self.max_tokens_by_modality
79
        modality, _ = max(max_tokens_by_modality.items(), key=lambda x: x[1])
80

81
        return modality
82
83

    def get_encoder_budget(self) -> int:
84
        return min(self.encoder_compute_budget, self.encoder_cache_size)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

    def get_max_items(
        self,
        modality: str,
        max_tokens_per_item: int,
    ) -> tuple[int, int]:
        if max_tokens_per_item == 0:
            return 0, 0

        # Check how many items of this modality can be supported by
        # the encoder budget.
        encoder_budget = self.get_encoder_budget()

        # TODO: handle encoder-decoder models once we support them.
        if encoder_budget == 0:
            return 0, 0

        max_encoder_items_per_batch = encoder_budget // max_tokens_per_item

        # Check how many items of this modality can be supported by
        # the decoder budget.
        mm_limit = self.mm_limits[modality]

        max_items_per_prompt = max(
            1,
            min(mm_limit, self.max_model_len // max_tokens_per_item),
        )

        scheduler_config = self.scheduler_config
        max_num_reqs = self.max_num_reqs

        if not scheduler_config.enable_chunked_prefill:
            max_num_reqs = min(
                max_num_reqs,
                scheduler_config.max_num_batched_tokens // max_tokens_per_item,
            )

        max_decoder_items_per_batch = max_num_reqs * max_items_per_prompt

        max_items_per_batch = max(
            1,
            min(max_encoder_items_per_batch, max_decoder_items_per_batch),
        )

        return max_items_per_prompt, max_items_per_batch

131
132
133
134
    def reset_cache(self) -> None:
        if self.cache is not None:
            self.cache.clear_cache()

135

136
137
138
139
@dataclass
class AttentionGroup:
    backend: type[AttentionBackend]
    layer_names: list[str]
140
    kv_cache_spec: KVCacheSpec
141
    kv_cache_group_id: int
142
    # When ubatching is enabled we will have a metadata builder for each ubatch
143
    # so that if they use internal persistent buffers for cudagraphs, and they
144
145
146
147
    # won't have to worry about conflicting with the other ubatches.
    metadata_builders: list[AttentionMetadataBuilder] = field(
        default_factory=lambda: []
    )
148

149
150
151
152
153
    def create_metadata_builders(
        self,
        vllm_config,
        device,
        kernel_block_size: int | None,
154
        num_metadata_builders: int = 1,
155
156
157
158
159
160
161
162
163
164
165
166
167
    ):
        kv_cache_spec_builder = (
            self.kv_cache_spec.copy_with_new_block_size(kernel_block_size)
            if kernel_block_size is not None
            else self.kv_cache_spec
        )
        self.metadata_builders = [
            self.backend.get_builder_cls()(
                kv_cache_spec_builder,
                self.layer_names,
                vllm_config,
                device,
            )
168
169
170
            for _ in range(num_metadata_builders)
        ]

171
    def get_metadata_builder(self, ubatch_id: int = 0) -> AttentionMetadataBuilder:
172
173
174
        assert len(self.metadata_builders) > ubatch_id
        return self.metadata_builders[ubatch_id]

175

176
def sanity_check_mm_encoder_outputs(
177
    mm_embeddings: MultiModalEmbeddings,
178
179
180
181
    expected_num_items: int,
) -> None:
    """
    Perform sanity checks for the result of
182
    [`vllm.model_executor.models.SupportsMultiModal.embed_multimodal`][].
183
184
185
186
187
    """
    assert isinstance(mm_embeddings, (list, tuple, torch.Tensor)), (
        "Expected multimodal embeddings to be a list/tuple of 2D tensors, "
        f"or a single 3D tensor, but got {type(mm_embeddings)} "
        "instead. This is most likely due to incorrect implementation "
188
        "of the model's `embed_multimodal` method."
189
    )
190
191
192
193
194

    assert len(mm_embeddings) == expected_num_items, (
        "Expected number of multimodal embeddings to match number of "
        f"input items: {expected_num_items}, but got {len(mm_embeddings)=} "
        "instead. This is most likely due to incorrect implementation "
195
        "of the model's `embed_multimodal` method."
196
    )
197
198
199
200
201

    assert all(e.ndim == 2 for e in mm_embeddings), (
        "Expected multimodal embeddings to be a sequence of 2D tensors, "
        f"but got tensors with shapes {[e.shape for e in mm_embeddings]} "
        "instead. This is most likely due to incorrect implementation "
202
        "of the model's `embed_multimodal` method."
203
    )
204
205


206
@deprecated("`scatter_mm_placeholders` is deprecated and will be removed in v0.15.0.")
207
208
def scatter_mm_placeholders(
    embeds: torch.Tensor,
209
    is_embed: torch.Tensor | None,
210
211
212
213
214
) -> torch.Tensor:
    """
    Scatter the multimodal embeddings into a contiguous tensor that represents
    the placeholder tokens.

215
    [`vllm.multimodal.processing.PromptUpdateDetails.is_embed`][].
216
217
218

    Args:
        embeds: The multimodal embeddings.
219
            Shape: `(num_embeds, embed_dim)`
220
        is_embed: A boolean mask indicating which positions in the placeholder
221
222
            tokens need to be filled with multimodal embeddings.
            Shape: `(num_placeholders, num_embeds)`
223
224
225
226
227
228
229
230
231
232
233
234
    """
    if is_embed is None:
        return embeds

    placeholders = embeds.new_full(
        (is_embed.shape[0], embeds.shape[-1]),
        fill_value=torch.nan,
    )
    placeholders[is_embed] = embeds
    return placeholders


235
@deprecated("`gather_mm_placeholders` is deprecated and will be removed in v0.15.0.")
236
237
def gather_mm_placeholders(
    placeholders: torch.Tensor,
238
    is_embed: torch.Tensor | None,
239
240
241
242
) -> torch.Tensor:
    """
    Reconstructs the embeddings from the placeholder tokens.

243
244
    This is the operation of [`scatter_mm_placeholders`]
    [vllm.v1.worker.utils.scatter_mm_placeholders].
245
246
247
248
249
    """
    if is_embed is None:
        return placeholders

    return placeholders[is_embed]
250
251


252
def request_memory(init_snapshot: MemorySnapshot, cache_config: CacheConfig) -> int:
253
254
255
256
    """
    Calculate the amount of memory required by vLLM, then validate
    that the current amount of free memory is sufficient for that.
    """
257
258
259
    requested_memory = math.ceil(
        init_snapshot.total_memory * cache_config.gpu_memory_utilization
    )
260
261
262
263

    if init_snapshot.free_memory < requested_memory:
        raise ValueError(
            f"Free memory on device {init_snapshot.device_} "
264
265
            f"({format_gib(init_snapshot.free_memory)}/"
            f"{format_gib(init_snapshot.total_memory)} GiB) on startup "
266
267
            f"is less than desired GPU memory utilization "
            f"({cache_config.gpu_memory_utilization}, "
268
            f"{format_gib(requested_memory)} GiB). Decrease GPU memory "
269
270
271
272
273
274
            f"utilization or reduce GPU memory used by other processes."
        )

    return requested_memory


275
def add_kv_sharing_layers_to_kv_cache_groups(
276
277
    shared_kv_cache_layers: dict[str, str],
    kv_cache_groups: list[KVCacheGroupSpec],
278
    runner_only_attn_layers: set[str] | None = None,
279
280
281
282
283
284
285
286
287
288
289
290
291
292
) -> None:
    """
    Sets up KV cache sharing by reusing the allocated KV caches in `kv_caches`
    for layers that do not allocate its own KV cache, based on the mapping in
    `shared_kv_cache_layers`. Adds these layers to the corresponding KV cache
    group, which is needed to ensure that attention metadata is assigned later.

    Args:
        shared_kv_cache_layers: Layer pairings for cross-layer KV sharing.
            If an Attention layer `layer_name` is in the keys of this dict, it
            means this layer will perform attention using the keys and values
            from the KV cache of `shared_kv_cache_layers[layer_name]`.
        kv_cache_groups: The KV cache groups of the model.
    """
293
294
295
296
    layer_to_kv_cache_group: dict[str, KVCacheGroupSpec] = {}
    for kv_cache_group in kv_cache_groups:
        for layer_name in kv_cache_group.layer_names:
            layer_to_kv_cache_group[layer_name] = kv_cache_group
297
298

    for layer_name, target_layer_name in shared_kv_cache_layers.items():
299
300
        tgt_kv_cache_group = layer_to_kv_cache_group[target_layer_name]
        tgt_kv_cache_group.layer_names.append(layer_name)
301

302
303
304
        if runner_only_attn_layers is not None:
            runner_only_attn_layers.add(layer_name)

305
306
307

def bind_kv_cache(
    kv_caches: dict[str, torch.Tensor],
308
    forward_context: dict[str, Attention],
309
    runner_kv_caches: list[torch.Tensor],
310
    num_attn_module: int = 1,
311
312
313
314
315
316
317
318
319
320
321
322
323
324
) -> None:
    """
    Bind the allocated KV cache to both ModelRunner and forward context so
    that the KV cache can be used in the forward pass.

    This function:
      1) Fills the ModelRunner's kv cache list (`runner_kv_caches`) with
         kv_caches.
      2) Associates each attention layer in the `forward_context` with its
         corresponding KV cache in kv_caches.

    Args:
        kv_caches: The allocated kv_caches with layer names as keys.
        forward_context: The global forward context containing all Attention
325
            layers with layer names as keys.
326
327
328
329
330
331
332
333
        runner_kv_caches: The kv_cache declared by ModelRunner.
    """
    # Bind kv_caches to ModelRunner
    assert len(runner_kv_caches) == 0

    # Convert kv_caches dict to a list of tensors in the order of layer_index.
    index2name = defaultdict(list)
    for layer_name in kv_caches:
334
        index2name[extract_layer_index(layer_name, num_attn_module)].append(layer_name)
335
336
337
338
339
340
341

    for layer_index in sorted(index2name.keys()):
        layer_names = index2name[layer_index]
        if len(layer_names) > 1:
            # One typical case is encoder-decoder model, e.g., bart.
            # The cross attention and self attention in the same decoder layer
            # has different layer_name but the same layer_index.
342
343
344
345

            # TODO - analyze where runner_kv_caches is used and the right
            # way to ensure it properly reflects multiple attention layers
            # in the same decoder block.
346
347
348
349
350
351
            if (
                current_platform.is_cuda_alike()
                or current_platform.is_xpu()
                or current_platform.is_cpu()
            ):
                # We know that the GPU / CPU runner is not impacted by this
352
353
354
355
356
                # case. Some test code depends on runner_kv_caches, but
                # not in a way that's impacted by ignoring this.
                pass
            else:
                raise NotImplementedError
357
358
359
360
361
362
363
        layer_name = layer_names[0]
        runner_kv_caches.append(kv_caches[layer_name])

    # Bind kv_caches to forward context
    for layer_name, kv_cache in kv_caches.items():
        # NOTE: Use list because of v0 PP virtual engine.
        forward_context[layer_name].kv_cache = [kv_cache]
364
365


366
367
368
def is_residual_scattered_for_sp(
    vllm_config: VllmConfig, num_input_tokens: int
) -> bool:
369
370
371
    """Check if the residual tensor is scattered for sequence parallelism.

    The residual tensor is scattered across tensor parallel ranks when sequence
372
373
    parallelism and tensor parallelism is enabled.

374
    This follows the same logic as SequenceParallelismPass.is_applicable_for_range():
375
376
377
    - In full-graph compilation mode (no splitting ops or using inductor graph
      partition), SP is always applied
    - Otherwise, SP is only applied for specific shapes in compile_sizes
378
    """
379
    if not vllm_config.compilation_config.pass_config.enable_sp:
380
381
382
383
384
385
386
387
388
389
390
        return False

    tp = vllm_config.parallel_config.tensor_parallel_size

    if tp == 1:
        return False

    # When sequence parallelism is enabled, we always pad num_input_tokens
    # to be a multiple of tensor_parallel_size (tp) earlier.
    assert num_input_tokens % tp == 0

391
392
393
394
395
    if (
        not vllm_config.compilation_config.splitting_ops
        or vllm_config.compilation_config.use_inductor_graph_partition
    ):
        return True
396
397
398
399
    compile_sizes = vllm_config.compilation_config.compile_sizes
    if compile_sizes is None:
        return False
    return num_input_tokens in compile_sizes