unitorch.models.vllm¤
VLLMForGeneration¤
Text generation model backed by vLLM offline inference engine.
Wraps vllm.LLM for synchronous and asynchronous token generation.
Accepts tokenized input_ids tensors (compatible with unitorch-infer)
and returns token-ID tensors via GenerationOutputs.
Initializes the vLLM text generation engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hf_name_or_folder
|
str
|
Path to the HuggingFace model folder. |
required |
tensor_parallel_size
|
int
|
Number of GPUs for tensor parallelism. Defaults to 1. |
1
|
pipeline_parallel_size
|
int
|
Number of GPUs for pipeline parallelism. Defaults to 1. |
1
|
gpu_memory_utilization
|
float
|
Fraction of GPU memory to reserve for vLLM. Defaults to 0.90. |
0.9
|
max_model_len
|
int
|
Maximum sequence length. None uses model default. |
None
|
max_num_seqs
|
int
|
Maximum number of concurrent sequences. Defaults to 256. |
256
|
enable_prefix_caching
|
bool
|
Enable automatic KV-cache prefix sharing. Defaults to True. |
True
|
trust_remote_code
|
bool
|
Allow remote model code execution. Defaults to True. |
True
|
dtype
|
str
|
Model weight dtype ( |
'auto'
|
enforce_eager
|
bool
|
Disable CUDA graph capture (useful for debugging). Defaults to False. |
False
|
quantization
|
str
|
Quantization method ( |
None
|
Source code in src/unitorch/models/vllm/modeling.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 | |
cuda ¤
cuda(device=None)
Source code in src/unitorch/models/vllm/modeling.py
67 68 69 70 | |
eval ¤
eval()
Source code in src/unitorch/models/vllm/modeling.py
72 73 74 | |
train ¤
train(mode=True)
Source code in src/unitorch/models/vllm/modeling.py
76 77 78 | |
from_checkpoint ¤
from_checkpoint(ckpt_dir, **kwargs)
Source code in src/unitorch/models/vllm/modeling.py
80 81 82 83 | |
shutdown ¤
shutdown()
Shutdown the vLLM engine and release GPU memory held by worker processes.
Source code in src/unitorch/models/vllm/modeling.py
85 86 87 88 89 90 | |
generate ¤
generate(
input_ids: Tensor,
max_gen_seq_length: Optional[int] = 512,
min_gen_seq_length: Optional[int] = 0,
num_return_sequences: Optional[int] = 1,
do_sample: Optional[bool] = False,
temperature: Optional[float] = 1.0,
top_k: Optional[int] = 50,
top_p: Optional[float] = 1.0,
repetition_penalty: Optional[float] = 1.0,
stop: Optional[Union[str, List[str]]] = None,
pad_token_id: Optional[int] = 0,
) -> List[List[List[int]]]
Generates token sequences for the given input_ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor
|
Input token ID tensor of shape |
required |
max_gen_seq_length
|
int
|
Maximum number of new tokens to generate. Defaults to 512. |
512
|
min_gen_seq_length
|
int
|
Minimum number of new tokens to generate. Defaults to 0. |
0
|
num_return_sequences
|
int
|
Number of completions per prompt. Defaults to 1. |
1
|
num_beams
|
int
|
Beam search width (used when |
required |
do_sample
|
bool
|
Enable sampling; when False uses greedy/beam decoding. Defaults to False. |
False
|
temperature
|
float
|
Sampling temperature. Defaults to 1.0. |
1.0
|
top_k
|
int
|
Top-k sampling parameter. Defaults to 50. |
50
|
top_p
|
float
|
Top-p (nucleus) sampling parameter. Defaults to 1.0. |
1.0
|
repetition_penalty
|
float
|
Penalty for token repetition. Defaults to 1.0. |
1.0
|
length_penalty
|
float
|
Exponential length penalty for beam search. Defaults to 1.0. |
required |
stop
|
str or List[str]
|
Stop strings that terminate generation. |
None
|
use_beam_search
|
bool
|
Use beam search instead of sampling. Defaults to False. |
required |
Returns:
| Type | Description |
|---|---|
List[List[List[int]]]
|
List[List[List[int]]]: Generated token ID sequences, |
List[List[List[int]]]
|
shape |
Source code in src/unitorch/models/vllm/modeling.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
async_generate
async
¤
async_generate(
input_ids: Tensor,
max_gen_seq_length: Optional[int] = 512,
min_gen_seq_length: Optional[int] = 0,
num_return_sequences: Optional[int] = 1,
do_sample: Optional[bool] = False,
temperature: Optional[float] = 1.0,
top_k: Optional[int] = 50,
top_p: Optional[float] = 1.0,
repetition_penalty: Optional[float] = 1.0,
stop: Optional[Union[str, List[str]]] = None,
) -> List[List[int]]
Asynchronously generates token sequences for a single-row input_ids tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor
|
Input token ID tensor of shape |
required |
max_gen_seq_length
|
int
|
Maximum tokens to generate. Defaults to 512. |
512
|
min_gen_seq_length
|
int
|
Minimum tokens to generate. Defaults to 0. |
0
|
num_return_sequences
|
int
|
Number of completions. Defaults to 1. |
1
|
do_sample
|
bool
|
Enable sampling. Defaults to False. |
False
|
temperature
|
float
|
Sampling temperature. Defaults to 1.0. |
1.0
|
top_k
|
int
|
Top-k sampling. Defaults to 50. |
50
|
top_p
|
float
|
Top-p sampling. Defaults to 1.0. |
1.0
|
repetition_penalty
|
float
|
Repetition penalty. Defaults to 1.0. |
1.0
|
stop
|
str or List[str]
|
Stop strings. |
None
|
Returns:
| Type | Description |
|---|---|
List[List[int]]
|
List[List[int]]: Generated token ID sequences for the single prompt. |
Source code in src/unitorch/models/vllm/modeling.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
VLLMVLForGeneration¤
Vision-language generation model backed by vLLM offline inference engine.
Wraps vllm.LLM for multimodal (text + image) generation supporting
both single and multi-image inputs via the vLLM multimodal data API.
Accepts tokenized input_ids tensors and pixel-values tensors
(compatible with unitorch-infer) in addition to raw PIL.Image inputs.
Initializes the vLLM vision-language generation engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hf_name_or_folder
|
str
|
Path to the HuggingFace model folder. |
required |
tensor_parallel_size
|
int
|
Number of GPUs for tensor parallelism. Defaults to 1. |
1
|
pipeline_parallel_size
|
int
|
Number of GPUs for pipeline parallelism. Defaults to 1. |
1
|
gpu_memory_utilization
|
float
|
Fraction of GPU memory to reserve. Defaults to 0.90. |
0.9
|
max_model_len
|
int
|
Maximum total sequence length. None uses model default. |
None
|
max_num_seqs
|
int
|
Maximum concurrent sequences. Defaults to 128. |
128
|
max_num_images
|
int
|
Maximum images per request (vLLM limit_mm_per_prompt). Defaults to 8. |
8
|
enable_prefix_caching
|
bool
|
Enable KV-cache prefix sharing. Defaults to False. |
False
|
trust_remote_code
|
bool
|
Allow remote model code. Defaults to True. |
True
|
dtype
|
str
|
Weight dtype. Defaults to |
'auto'
|
enforce_eager
|
bool
|
Disable CUDA graph capture. Defaults to False. |
False
|
quantization
|
str
|
Quantization method. |
None
|
Source code in src/unitorch/models/vllm/modeling_vl.py
22 23 24 25 26 27 28 29 30 31 32 33 34 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 | |
cuda ¤
cuda(device=None)
Source code in src/unitorch/models/vllm/modeling_vl.py
75 76 77 78 | |
eval ¤
eval()
Source code in src/unitorch/models/vllm/modeling_vl.py
80 81 82 | |
train ¤
train(mode=True)
Source code in src/unitorch/models/vllm/modeling_vl.py
84 85 86 | |
from_checkpoint ¤
from_checkpoint(ckpt_dir, **kwargs)
Source code in src/unitorch/models/vllm/modeling_vl.py
88 89 90 91 | |
shutdown ¤
shutdown()
Shutdown the vLLM engine and release GPU memory held by worker processes.
Source code in src/unitorch/models/vllm/modeling_vl.py
93 94 95 96 97 98 | |
_decode_prompt ¤
_decode_prompt(token_ids: List[int]) -> str
Decode prompt token IDs back to the multimodal prompt string expected by vLLM.
The unitorch processor expands a single <|image_pad|> / <|video_pad|>
placeholder into a long run of repeated special tokens based on the visual
grid size. vLLM expects the unexpanded chat-template string and performs
the multimodal expansion internally, so we collapse those runs before
decoding the prompt text.
Source code in src/unitorch/models/vllm/modeling_vl.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
_normalize_images ¤
_normalize_images(
images: Optional[Union[Tensor, Image, List]],
batch_size: int,
) -> Optional[List[Optional[List[Image]]]]
Normalize images input to List[Optional[List[PIL.Image]]] of length batch_size.
Accepts:
- None: no images for any prompt.
- torch.Tensor: shape (B, C, H, W) or (C, H, W) pixel-values tensor.
- PIL.Image: single image shared across all prompts.
- List[PIL.Image]: one image per prompt.
- List[torch.Tensor]: one pixel-values tensor per prompt.
- List[List[PIL.Image or torch.Tensor]]: multiple images per prompt.
Source code in src/unitorch/models/vllm/modeling_vl.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
generate ¤
generate(
input_ids: Tensor,
images: Optional[Union[Tensor, Image, List]] = None,
max_gen_seq_length: Optional[int] = 512,
min_gen_seq_length: Optional[int] = 0,
num_return_sequences: Optional[int] = 1,
do_sample: Optional[bool] = False,
temperature: Optional[float] = 1.0,
top_k: Optional[int] = 50,
top_p: Optional[float] = 1.0,
repetition_penalty: Optional[float] = 1.0,
stop: Optional[Union[str, List[str]]] = None,
pad_token_id: Optional[int] = 0,
) -> List[List[List[int]]]
Generates token sequences for the given text and image inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor
|
Input token ID tensor of shape |
required |
images
|
Optional[Union[Tensor, Image, List]]
|
Input image(s). Accepts:
|
None
|
max_gen_seq_length
|
int
|
Maximum new tokens. Defaults to 512. |
512
|
min_gen_seq_length
|
int
|
Minimum new tokens. Defaults to 0. |
0
|
num_return_sequences
|
int
|
Completions per prompt. Defaults to 1. |
1
|
do_sample
|
bool
|
Enable sampling. Defaults to False. |
False
|
temperature
|
float
|
Sampling temperature. Defaults to 1.0. |
1.0
|
top_k
|
int
|
Top-k sampling. Defaults to 50. |
50
|
top_p
|
float
|
Top-p sampling. Defaults to 1.0. |
1.0
|
repetition_penalty
|
float
|
Repetition penalty. Defaults to 1.0. |
1.0
|
stop
|
str or List[str]
|
Stop strings. |
None
|
Returns:
| Type | Description |
|---|---|
List[List[List[int]]]
|
List[List[List[int]]]: Generated token ID sequences, |
List[List[List[int]]]
|
shape |
Source code in src/unitorch/models/vllm/modeling_vl.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
async_generate
async
¤
async_generate(
input_ids: Tensor,
images: Optional[Union[Tensor, Image, List]] = None,
max_gen_seq_length: Optional[int] = 512,
min_gen_seq_length: Optional[int] = 0,
num_return_sequences: Optional[int] = 1,
do_sample: Optional[bool] = False,
temperature: Optional[float] = 1.0,
top_k: Optional[int] = 50,
top_p: Optional[float] = 1.0,
repetition_penalty: Optional[float] = 1.0,
stop: Optional[Union[str, List[str]]] = None,
) -> List[List[int]]
Asynchronously generates token sequences for a single-row input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor
|
Token ID tensor of shape |
required |
images
|
Optional[Union[Tensor, Image, List]]
|
Optional image(s) for the single prompt (same formats as |
None
|
max_gen_seq_length
|
int
|
Maximum tokens to generate. Defaults to 512. |
512
|
min_gen_seq_length
|
int
|
Minimum tokens to generate. Defaults to 0. |
0
|
num_return_sequences
|
int
|
Number of completions. Defaults to 1. |
1
|
do_sample
|
bool
|
Enable sampling. Defaults to False. |
False
|
temperature
|
float
|
Sampling temperature. Defaults to 1.0. |
1.0
|
top_k
|
int
|
Top-k sampling. Defaults to 50. |
50
|
top_p
|
float
|
Top-p sampling. Defaults to 1.0. |
1.0
|
repetition_penalty
|
float
|
Repetition penalty. Defaults to 1.0. |
1.0
|
stop
|
str or List[str]
|
Stop strings. |
None
|
Returns:
| Type | Description |
|---|---|
List[List[int]]
|
List[List[int]]: Generated token ID sequences for the single prompt. |
Source code in src/unitorch/models/vllm/modeling_vl.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |