|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Unified HF checkpoint inference with vLLM. |
| 17 | +
|
| 18 | +Usage: |
| 19 | + python run_vllm.py --model /path/to/quantized/model |
| 20 | + python run_vllm.py --model /path/to/model --tp 4 |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import argparse |
| 26 | + |
| 27 | +from example_utils import ( |
| 28 | + ensure_tokenizer_files, |
| 29 | + get_model_type_from_config, |
| 30 | + get_quantization_format, |
| 31 | + get_sampling_params_from_config, |
| 32 | +) |
| 33 | +from transformers import AutoConfig, AutoProcessor |
| 34 | +from vllm import LLM, SamplingParams |
| 35 | + |
| 36 | + |
| 37 | +def main(): |
| 38 | + parser = argparse.ArgumentParser(description="Run unified hf checkpoint inference with vLLM") |
| 39 | + parser.add_argument("--model", type=str, required=True, help="Model ID or path") |
| 40 | + parser.add_argument("--tp", type=int, default=1, help="Tensor parallel size") |
| 41 | + parser.add_argument( |
| 42 | + "--max-model-len", |
| 43 | + type=int, |
| 44 | + default=None, |
| 45 | + help="Max model length (auto-detected from config if not specified)", |
| 46 | + ) |
| 47 | + parser.add_argument("--prompt", type=str, default="What in Nvidia?", help="Text prompt") |
| 48 | + parser.add_argument( |
| 49 | + "--tokenizer", type=str, default=None, help="Tokenizer ID or path (defaults to model path)" |
| 50 | + ) |
| 51 | + parser.add_argument("--temperature", type=float, default=0.7, help="Sampling temperature") |
| 52 | + parser.add_argument("--top-p", type=float, default=0.9, help="Top-p sampling") |
| 53 | + parser.add_argument("--top-k", type=int, default=-1, help="Top-k sampling (-1 to disable)") |
| 54 | + parser.add_argument("--max-tokens", type=int, default=512, help="Max tokens to generate") |
| 55 | + |
| 56 | + args = parser.parse_args() |
| 57 | + |
| 58 | + # Detect model type from config |
| 59 | + model_type = get_model_type_from_config(args.model) |
| 60 | + print(f"Detected model type: {model_type}") |
| 61 | + |
| 62 | + # Detect quantization format |
| 63 | + quantization = get_quantization_format(args.model) |
| 64 | + print(f"Detected quantization: {quantization}") |
| 65 | + |
| 66 | + # Get max_model_len from config if not specified |
| 67 | + if args.max_model_len is None: |
| 68 | + config = AutoConfig.from_pretrained(args.model, trust_remote_code=True) |
| 69 | + args.max_model_len = getattr(config, "max_position_embeddings", 4096) |
| 70 | + print(f"Using max_model_len from config: {args.max_model_len}") |
| 71 | + |
| 72 | + # Determine tokenizer source |
| 73 | + tokenizer_id = args.tokenizer or args.model |
| 74 | + |
| 75 | + # Load processor for chat template |
| 76 | + processor = AutoProcessor.from_pretrained(tokenizer_id, trust_remote_code=True) |
| 77 | + |
| 78 | + # Text-only conversations |
| 79 | + conversations = [ |
| 80 | + [ |
| 81 | + { |
| 82 | + "role": "user", |
| 83 | + "content": [{"type": "text", "text": args.prompt}], |
| 84 | + } |
| 85 | + ], |
| 86 | + ] |
| 87 | + |
| 88 | + # Apply chat template |
| 89 | + apply_chat_kwargs = { |
| 90 | + "add_generation_prompt": True, |
| 91 | + "tokenize": False, |
| 92 | + } |
| 93 | + # Qwen3Omni-specific: disable thinking mode |
| 94 | + if model_type == "qwen3omni": |
| 95 | + apply_chat_kwargs["enable_thinking"] = False |
| 96 | + |
| 97 | + texts = processor.apply_chat_template(conversations, **apply_chat_kwargs) |
| 98 | + |
| 99 | + # Ensure tokenizer files exist in local model dir (vLLM loads processor from model path) |
| 100 | + if args.tokenizer: |
| 101 | + ensure_tokenizer_files(args.model, args.tokenizer) |
| 102 | + |
| 103 | + print(f"Loading model: {args.model}") |
| 104 | + llm = LLM( |
| 105 | + model=args.model, |
| 106 | + tokenizer=tokenizer_id, |
| 107 | + tensor_parallel_size=args.tp, |
| 108 | + max_model_len=args.max_model_len, |
| 109 | + trust_remote_code=True, |
| 110 | + quantization=quantization, |
| 111 | + ) |
| 112 | + |
| 113 | + # Get sampling params from config, with CLI/defaults as fallback |
| 114 | + config_params = get_sampling_params_from_config(args.model) |
| 115 | + sampling_kwargs = { |
| 116 | + "temperature": config_params.get("temperature", args.temperature), |
| 117 | + "top_p": config_params.get("top_p", args.top_p), |
| 118 | + "max_tokens": config_params.get("max_tokens", args.max_tokens), |
| 119 | + } |
| 120 | + top_k = config_params.get("top_k", args.top_k) |
| 121 | + if top_k > 0: |
| 122 | + sampling_kwargs["top_k"] = top_k |
| 123 | + print(f"Sampling params: {sampling_kwargs}") |
| 124 | + sampling_params = SamplingParams(**sampling_kwargs) |
| 125 | + |
| 126 | + print("Running inference...") |
| 127 | + outputs = llm.generate(texts, sampling_params) |
| 128 | + |
| 129 | + for output in outputs: |
| 130 | + generated_text = output.outputs[0].text |
| 131 | + print("-" * 80) |
| 132 | + print(f"Generated: {generated_text}") |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + main() |
0 commit comments