|
1 | | -import asyncio |
2 | | -import torch |
3 | | -from vllm import AsyncLLMEngine, AsyncEngineArgs, SamplingParams |
| 1 | +import asyncio, torch, threading, queue |
| 2 | +from vllm import AsyncLLMEngine, AsyncEngineArgs, SamplingParams, EngineArgs |
| 3 | +from typing import Optional |
4 | 4 | from transformers import AutoTokenizer |
5 | | -import threadingc |
6 | | -import queue |
7 | 5 | from .decoder import tokens_decoder_sync |
8 | 6 |
|
9 | | -class OrpheusModel: |
10 | | - def __init__(self, model_name, dtype=torch.bfloat16): |
| 7 | +class OrpheusModel: |
| 8 | + def __init__(self, |
| 9 | + model_name, |
| 10 | + dtype=torch.bfloat16, |
| 11 | + |
| 12 | + seed: int = 0, |
| 13 | + max_model_len: Optional[int] = None, |
| 14 | + cpu_offload_gb: float = 0, # GiB |
| 15 | + gpu_memory_utilization: float = 0.90, |
| 16 | + quantization: Optional[str] = None, |
| 17 | + max_seq_len_to_capture: int = 8192, |
| 18 | + enforce_eager: Optional[bool] = None |
| 19 | + ): |
| 20 | + """ |
| 21 | + Initialize the Orpheus Text-to-Speech engine. |
| 22 | + |
| 23 | + This class provides high-quality text-to-speech synthesis using neural models. |
| 24 | + It handles model loading, voice selection, and audio generation with various |
| 25 | + configuration options for performance and quality. |
| 26 | + |
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + model_name : str |
| 30 | + Name or path of the model to use (e.g., "medium-3b" or a custom model path). |
| 31 | + dtype : torch.dtype, default=torch.bfloat16 |
| 32 | + Data type for model computation. BFloat16 offers good performance balance. |
| 33 | + seed : int, default=0 |
| 34 | + Random seed for reproducible generation. |
| 35 | + max_model_len : Optional[int], default=None |
| 36 | + Maximum sequence length the model can process. |
| 37 | + cpu_offload_gb : float, default=0 |
| 38 | + Amount of model weights (in GiB) to offload to CPU when GPU memory is limited. |
| 39 | + gpu_memory_utilization : float, default=0.90 |
| 40 | + Target GPU memory utilization (0.0 to 1.0). |
| 41 | + quantization : Optional[str], default=None |
| 42 | + Quantization method for reduced memory usage ("int8", "int4", etc.). |
| 43 | + max_seq_len_to_capture : int, default=8192 |
| 44 | + Maximum sequence length for KV cache optimization. |
| 45 | + enforce_eager : Optional[bool], default=None |
| 46 | + Whether to disable CUDA graph optimizations for debugging. |
| 47 | + |
| 48 | + Attributes |
| 49 | + ---------- |
| 50 | + model_name : str |
| 51 | + The resolved model path after parameter mapping. |
| 52 | + engine : AsyncLLMEngine |
| 53 | + The underlying inference engine. |
| 54 | + available_voices : list |
| 55 | + Available voice identifiers ("zoe", "zac", etc.). |
| 56 | + tokeniser : AutoTokenizer |
| 57 | + Tokenizer for text processing. |
| 58 | + |
| 59 | + Examples |
| 60 | + -------- |
| 61 | + >>> model = OrpheusModel("medium-3b") |
| 62 | + >>> audio = model.generate_speech(prompt="Hello world!", voice="zoe") |
| 63 | + """ |
| 64 | + |
11 | 65 | self.model_name = self._map_model_params(model_name) |
12 | 66 | self.dtype = dtype |
13 | | - self.engine = self._setup_engine() |
| 67 | + self.engine = self._setup_engine(seed, max_model_len, cpu_offload_gb, gpu_memory_utilization, quantization, max_seq_len_to_capture, enforce_eager) |
14 | 68 | self.available_voices = ["zoe", "zac","jess", "leo", "mia", "julia", "leah"] |
15 | 69 | self.tokeniser = AutoTokenizer.from_pretrained(model_name) |
16 | 70 |
|
@@ -38,11 +92,42 @@ def _map_model_params(self, model_name): |
38 | 92 | else: |
39 | 93 | return model_name |
40 | 94 |
|
41 | | - def _setup_engine(self): |
| 95 | + def _setup_engine( |
| 96 | + self, |
| 97 | + seed: int = 0, |
| 98 | + max_model_len: Optional[int] = None, |
| 99 | + cpu_offload_gb: float = 0, # GiB |
| 100 | + gpu_memory_utilization: float = 0.90, |
| 101 | + quantization: Optional[str] = None, |
| 102 | + max_seq_len_to_capture: int = 8192, |
| 103 | + enforce_eager: Optional[bool] = None |
| 104 | + ): |
| 105 | + """ |
| 106 | + Sets up and initializes the LLM engine with specified configuration. |
| 107 | + Args: |
| 108 | + seed (int, optional): Random seed for reproducibility. Defaults to 0. |
| 109 | + max_model_len (Optional[int], optional): Maximum sequence length the model can handle. Defaults to None. |
| 110 | + cpu_offload_gb (float, optional): Amount of memory in GiB to offload to CPU. Defaults to 0. |
| 111 | + gpu_memory_utilization (float, optional): Fraction of GPU memory to utilize. Defaults to 0.90 (90%). |
| 112 | + quantization (Optional[str], optional): Quantization method to use. Defaults to None. |
| 113 | + max_seq_len_to_capture (int, optional): Maximum sequence length to capture for optimization. Defaults to 8192. |
| 114 | + enforce_eager (Optional[bool], optional): Whether to enforce eager execution. Defaults to None. |
| 115 | + Returns: |
| 116 | + AsyncLLMEngine: Initialized language model engine ready for inference. |
| 117 | + """ |
| 118 | + |
42 | 119 | engine_args = AsyncEngineArgs( |
43 | 120 | model=self.model_name, |
44 | 121 | dtype=self.dtype, |
| 122 | + max_model_len=max_model_len, |
| 123 | + cpu_offload_gb=cpu_offload_gb, |
| 124 | + gpu_memory_utilization=gpu_memory_utilization, |
| 125 | + quantization=quantization, |
| 126 | + max_seq_len_to_capture=max_seq_len_to_capture, |
| 127 | + enforce_eager=enforce_eager, |
| 128 | + seed=seed |
45 | 129 | ) |
| 130 | + |
46 | 131 | return AsyncLLMEngine.from_engine_args(engine_args) |
47 | 132 |
|
48 | 133 | def validate_voice(self, voice): |
@@ -73,18 +158,16 @@ def _format_prompt(self, prompt, voice="tara", model_type="larger"): |
73 | 158 | prompt_string = self.tokeniser.decode(all_input_ids[0]) |
74 | 159 | return prompt_string |
75 | 160 |
|
76 | | - |
77 | | - |
78 | 161 |
|
79 | 162 | def generate_tokens_sync(self, prompt, voice=None, request_id="req-001", temperature=0.6, top_p=0.8, max_tokens=1200, stop_token_ids = [49158], repetition_penalty=1.3): |
80 | 163 | prompt_string = self._format_prompt(prompt, voice) |
81 | 164 | print(prompt) |
82 | 165 | sampling_params = SamplingParams( |
83 | | - temperature=temperature, |
84 | | - top_p=top_p, |
85 | | - max_tokens=max_tokens, # Adjust max_tokens as needed. |
86 | | - stop_token_ids = stop_token_ids, |
87 | | - repetition_penalty=repetition_penalty, |
| 166 | + temperature=temperature, |
| 167 | + top_p=top_p, |
| 168 | + max_tokens=max_tokens, # Adjust max_tokens as needed. |
| 169 | + stop_token_ids = stop_token_ids, |
| 170 | + repetition_penalty=repetition_penalty, |
88 | 171 | ) |
89 | 172 |
|
90 | 173 | token_queue = queue.Queue() |
|
0 commit comments