Skip to content

Commit a57e9d6

Browse files
authored
Merge pull request #1 from kirigen-ai/main
Enhance OrpheusModel with Robust OOM Error Handling for VLLM
2 parents 77c017b + 3d14635 commit a57e9d6

2 files changed

Lines changed: 100 additions & 17 deletions

File tree

orpheus_tts/engine_class.py

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,70 @@
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
44
from transformers import AutoTokenizer
5-
import threadingc
6-
import queue
75
from .decoder import tokens_decoder_sync
86

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+
1165
self.model_name = self._map_model_params(model_name)
1266
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)
1468
self.available_voices = ["zoe", "zac","jess", "leo", "mia", "julia", "leah"]
1569
self.tokeniser = AutoTokenizer.from_pretrained(model_name)
1670

@@ -38,11 +92,42 @@ def _map_model_params(self, model_name):
3892
else:
3993
return model_name
4094

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+
42119
engine_args = AsyncEngineArgs(
43120
model=self.model_name,
44121
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
45129
)
130+
46131
return AsyncLLMEngine.from_engine_args(engine_args)
47132

48133
def validate_voice(self, voice):
@@ -73,18 +158,16 @@ def _format_prompt(self, prompt, voice="tara", model_type="larger"):
73158
prompt_string = self.tokeniser.decode(all_input_ids[0])
74159
return prompt_string
75160

76-
77-
78161

79162
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):
80163
prompt_string = self._format_prompt(prompt, voice)
81164
print(prompt)
82165
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,
88171
)
89172

90173
token_queue = queue.Queue()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name="orpheus-speech",
6-
version="0.1.0",
6+
version="0.1.1",
77
packages=find_packages(),
88
install_requires=["snac", "vllm"],
99
author="Amu Varma",

0 commit comments

Comments
 (0)