Upload generation/vllm_generator.py with huggingface_hub
Browse files- generation/vllm_generator.py +71 -4
generation/vllm_generator.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
"""VLLM-based text-to-speech generation logic with async streaming"""
|
| 2 |
|
| 3 |
import asyncio
|
|
|
|
| 4 |
import time
|
| 5 |
-
import
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
-
|
|
|
|
| 8 |
from transformers import AutoTokenizer
|
|
|
|
| 9 |
|
| 10 |
from config import (
|
| 11 |
MODEL_NAME, TOKENIZER_NAME, START_OF_HUMAN, END_OF_TEXT, END_OF_HUMAN, END_OF_AI,
|
|
@@ -14,6 +18,68 @@ from config import (
|
|
| 14 |
)
|
| 15 |
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
class VLLMTTSGenerator:
|
| 18 |
def __init__(self, tensor_parallel_size=1, gpu_memory_utilization=0.9, max_model_len=2048):
|
| 19 |
"""Initialize VLLM-based TTS generator with async streaming support
|
|
@@ -23,11 +89,12 @@ class VLLMTTSGenerator:
|
|
| 23 |
gpu_memory_utilization: Fraction of GPU memory to use (0.0 to 1.0)
|
| 24 |
max_model_len: Maximum sequence length
|
| 25 |
"""
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
# Configure engine arguments
|
| 29 |
engine_args = AsyncEngineArgs(
|
| 30 |
-
model=
|
| 31 |
tokenizer=TOKENIZER_NAME,
|
| 32 |
tensor_parallel_size=tensor_parallel_size,
|
| 33 |
max_model_len=max_model_len,
|
|
|
|
| 1 |
"""VLLM-based text-to-speech generation logic with async streaming"""
|
| 2 |
|
| 3 |
import asyncio
|
| 4 |
+
import os
|
| 5 |
import time
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
import numpy as np
|
| 9 |
+
import torch
|
| 10 |
+
from huggingface_hub import list_repo_files, snapshot_download
|
| 11 |
from transformers import AutoTokenizer
|
| 12 |
+
from vllm import AsyncEngineArgs, AsyncLLMEngine, SamplingParams
|
| 13 |
|
| 14 |
from config import (
|
| 15 |
MODEL_NAME, TOKENIZER_NAME, START_OF_HUMAN, END_OF_TEXT, END_OF_HUMAN, END_OF_AI,
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
|
| 21 |
+
def _pick_model_dir(candidate_dirs):
|
| 22 |
+
if not candidate_dirs:
|
| 23 |
+
return None
|
| 24 |
+
ranked_dirs = sorted(
|
| 25 |
+
{str(Path(directory)) for directory in candidate_dirs},
|
| 26 |
+
key=lambda directory: (
|
| 27 |
+
0 if "/models/" in directory else 1,
|
| 28 |
+
len(Path(directory).parts),
|
| 29 |
+
directory,
|
| 30 |
+
),
|
| 31 |
+
)
|
| 32 |
+
return ranked_dirs[0]
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def _find_local_model_root(model_name):
|
| 36 |
+
model_path = Path(model_name).expanduser()
|
| 37 |
+
if not model_path.exists():
|
| 38 |
+
return None
|
| 39 |
+
|
| 40 |
+
if (model_path / "config.json").exists() or (model_path / "params.json").exists():
|
| 41 |
+
return str(model_path)
|
| 42 |
+
|
| 43 |
+
config_candidates = list(model_path.rglob("config.json")) + list(model_path.rglob("params.json"))
|
| 44 |
+
resolved_dir = _pick_model_dir(path.parent for path in config_candidates)
|
| 45 |
+
if resolved_dir:
|
| 46 |
+
print(f"Resolved local model root for vLLM: {resolved_dir}")
|
| 47 |
+
return resolved_dir
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _resolve_model_name(model_name):
|
| 51 |
+
local_model_root = _find_local_model_root(model_name)
|
| 52 |
+
if local_model_root:
|
| 53 |
+
return local_model_root
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
repo_files = list_repo_files(model_name)
|
| 57 |
+
except Exception:
|
| 58 |
+
return model_name
|
| 59 |
+
|
| 60 |
+
if "config.json" in repo_files or "params.json" in repo_files:
|
| 61 |
+
return model_name
|
| 62 |
+
|
| 63 |
+
config_candidates = [
|
| 64 |
+
file_path for file_path in repo_files
|
| 65 |
+
if file_path.endswith("/config.json") or file_path.endswith("/params.json")
|
| 66 |
+
]
|
| 67 |
+
if not config_candidates:
|
| 68 |
+
return model_name
|
| 69 |
+
|
| 70 |
+
model_subdir = _pick_model_dir(Path(file_path).parent for file_path in config_candidates)
|
| 71 |
+
if model_subdir is None:
|
| 72 |
+
return model_name
|
| 73 |
+
|
| 74 |
+
snapshot_dir = snapshot_download(
|
| 75 |
+
repo_id=model_name,
|
| 76 |
+
allow_patterns=[f"{model_subdir}/*"],
|
| 77 |
+
)
|
| 78 |
+
resolved_model_root = os.path.join(snapshot_dir, model_subdir)
|
| 79 |
+
print(f"Resolved HF model root for vLLM: {resolved_model_root}")
|
| 80 |
+
return resolved_model_root
|
| 81 |
+
|
| 82 |
+
|
| 83 |
class VLLMTTSGenerator:
|
| 84 |
def __init__(self, tensor_parallel_size=1, gpu_memory_utilization=0.9, max_model_len=2048):
|
| 85 |
"""Initialize VLLM-based TTS generator with async streaming support
|
|
|
|
| 89 |
gpu_memory_utilization: Fraction of GPU memory to use (0.0 to 1.0)
|
| 90 |
max_model_len: Maximum sequence length
|
| 91 |
"""
|
| 92 |
+
resolved_model_name = _resolve_model_name(MODEL_NAME)
|
| 93 |
+
print(f"Loading VLLM AsyncLLMEngine model: {resolved_model_name}")
|
| 94 |
|
| 95 |
# Configure engine arguments
|
| 96 |
engine_args = AsyncEngineArgs(
|
| 97 |
+
model=resolved_model_name,
|
| 98 |
tokenizer=TOKENIZER_NAME,
|
| 99 |
tensor_parallel_size=tensor_parallel_size,
|
| 100 |
max_model_len=max_model_len,
|