"lib/runtime/src/storage/kv/etcd.rs" did not exist on "08355da6255c491a5d5452ced57f5bdaadd12796"
model.py 3.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.

import logging
from typing import Any, Dict, Tuple

import torch
from transformers import AutoConfig
from utils.protocol import EncodeResponse
from vllm import AsyncEngineArgs
from vllm.utils import get_distributed_init_method, get_ip, get_open_port

logger = logging.getLogger(__name__)


def load_vision_model(model_id: str) -> torch.nn.Module:
    """
    Load a vision model from a HuggingFace model ID.
    """
32
33
34
    # lazy import to avoid cuda error if not on gpu
    from vllm.worker.worker import Worker

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    engine_args = AsyncEngineArgs(model=model_id, trust_remote_code=True)

    engine_config = engine_args.create_engine_config()
    distributed_init_method = get_distributed_init_method(get_ip(), get_open_port())
    worker = Worker(
        vllm_config=engine_config,
        local_rank=0,
        rank=0,
        distributed_init_method=distributed_init_method,
        is_driver_worker=True,
    )
    # Initialize the worker.
    worker.init_device()
    worker.load_model()
    return worker.model_runner.model


def get_vision_embeddings_info(
    model_id: str, num_patches: int
) -> Tuple[Tuple[int, int, int], torch.dtype]:
    """Calculate vision embeddings size and dtype using model config
    Returns a tuple of (batch_size, num_patches, hidden_dim), dtype.
    """
    config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
    assert num_patches > 0, "Number of patches must be positive"
    if not hasattr(config, "torch_dtype"):
        raise ValueError("Model config missing required 'torch_dtype' attribute")
    if not hasattr(config, "hidden_size"):
        logger.warning(
            "Model config missing required 'hidden_size' attribute, using 4096"
        )
        hidden_size = 4096
    else:
        hidden_size = config.hidden_size
    return (1, num_patches, hidden_size), config.torch_dtype


def construct_mm_data(
    model: str,
    encode_output: EncodeResponse,
    image_embeds: torch.Tensor,
    embeddings_dtype: torch.dtype,
) -> Dict[str, torch.Tensor | Dict[str, Any]]:
    """Construct multimodal data for a vLLM request for models that require additional parameters alongside the embeddings"""
    image_embeds = image_embeds.to(embeddings_dtype)
    if "Qwen2" in model:
        return {
            "image": {
                "image_embeds": image_embeds.squeeze(0),
                "image_grid_thw": torch.tensor(encode_output.image_grid_thw).squeeze(0),
            }
        }
    elif "MiniCPM-V" in model:
        return {
            "image": {
                "image_embeds": image_embeds,
                "image_sizes": encode_output.image_sizes,
            }
        }
    else:
        return {"image": image_embeds}