vllm_mlx.models.llm¶
MLX Language Model wrapper.
View the complete module source at #L1-L422.
API details¶
Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.
vllm_mlx.models.llm
¶
MLX Language Model wrapper.
This module provides a wrapper around mlx-lm for LLM inference, integrating with vLLM's model execution system.
vllm_mlx.models.llm.GenerationOutput
dataclass
¶
Output from text generation.
vllm_mlx.models.llm.StreamingOutput
dataclass
¶
StreamingOutput(text: str, token: int, finished: bool = False, finish_reason: str | None = None, prompt_tokens: int = 0)
vllm_mlx.models.llm.MLXLanguageModel
¶
MLXLanguageModel(model_name: str, tokenizer_name: str | None = None, trust_remote_code: bool = False, mtp: bool = False, mtp_num_draft_tokens: int = 1)
Wrapper around mlx-lm for LLM inference.
This class provides a unified interface for loading and running inference on language models using Apple's MLX framework.
Example
model = MLXLanguageModel("mlx-community/Llama-3.2-3B-Instruct-4bit") output = model.generate("Hello, how are you?", max_tokens=100) print(output.text)
Initialize the MLX language model.
Parameters:
-
model_name(str) –HuggingFace model name or local path
-
tokenizer_name(str | None, default:None) –Optional separate tokenizer name
-
trust_remote_code(bool, default:False) –Whether to trust remote code
-
mtp(bool, default:False) –Enable native MTP speculative decoding (model must have MTP head)
-
mtp_num_draft_tokens(int, default:1) –Draft tokens per speculative MTP step
Source code in vllm_mlx/models/llm.py
vllm_mlx.models.llm.MLXLanguageModel.tokenizer_name
instance-attribute
¶
vllm_mlx.models.llm.MLXLanguageModel.trust_remote_code
instance-attribute
¶
vllm_mlx.models.llm.MLXLanguageModel._mtp_num_draft_tokens
instance-attribute
¶
vllm_mlx.models.llm.MLXLanguageModel.load
¶
Load the model and tokenizer.
Source code in vllm_mlx/models/llm.py
vllm_mlx.models.llm.MLXLanguageModel._create_sampler
¶
Create a sampler for text generation.
Source code in vllm_mlx/models/llm.py
vllm_mlx.models.llm.MLXLanguageModel._create_logits_processors
¶
Create logits processors for penalty-based sampling.
Source code in vllm_mlx/models/llm.py
vllm_mlx.models.llm.MLXLanguageModel.generate
¶
generate(prompt: str, max_tokens: int = 256, temperature: float = 0.7, top_p: float = 0.9, top_k: int = 0, min_p: float = 0.0, presence_penalty: float = 0.0, repetition_penalty: float = 1.0, stop: list[str] | None = None, logits_processors: list | None = None, **kwargs) -> GenerationOutput
Generate text from a prompt.
Parameters:
-
prompt(str) –Input prompt text
-
max_tokens(int, default:256) –Maximum number of tokens to generate
-
temperature(float, default:0.7) –Sampling temperature (0 = greedy)
-
top_p(float, default:0.9) –Top-p (nucleus) sampling parameter
-
top_k(int, default:0) –Top-k sampling (0 = disabled)
-
min_p(float, default:0.0) –Minimum probability threshold
-
presence_penalty(float, default:0.0) –Additive penalty for token presence
-
repetition_penalty(float, default:1.0) –Multiplicative penalty for repeating tokens
-
stop(list[str] | None, default:None) –List of stop sequences
-
logits_processors(list | None, default:None) –Optional externally-supplied logits processors (e.g. JSON schema constrained decoding). Merged with built-in penalty processors.
Returns:
-
GenerationOutput–GenerationOutput with generated text and tokens
Source code in vllm_mlx/models/llm.py
vllm_mlx.models.llm.MLXLanguageModel.stream_generate
¶
stream_generate(prompt: Union[str, array, list[int]], max_tokens: int = 256, temperature: float = 0.7, top_p: float = 0.9, top_k: int = 0, min_p: float = 0.0, presence_penalty: float = 0.0, repetition_penalty: float = 1.0, stop: list[str] | None = None, logits_processors: list | None = None, prompt_cache=None, **kwargs) -> Iterator[StreamingOutput]
Stream text generation token by token.
Parameters:
-
prompt(Union[str, array, list[int]]) –Input prompt text, token array, or token id list
-
max_tokens(int, default:256) –Maximum number of tokens to generate
-
temperature(float, default:0.7) –Sampling temperature (0 = greedy)
-
top_p(float, default:0.9) –Top-p (nucleus) sampling parameter
-
top_k(int, default:0) –Top-k sampling (0 = disabled)
-
min_p(float, default:0.0) –Minimum probability threshold
-
presence_penalty(float, default:0.0) –Additive penalty for token presence
-
repetition_penalty(float, default:1.0) –Multiplicative penalty for repeating tokens
-
stop(list[str] | None, default:None) –List of stop sequences
-
prompt_cache–Pre-populated KV cache (e.g. from SpecPrefill)
Yields:
-
StreamingOutput–StreamingOutput for each generated token
Source code in vllm_mlx/models/llm.py
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 262 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
vllm_mlx.models.llm.MLXLanguageModel.chat
¶
chat(messages: list[dict], max_tokens: int = 256, temperature: float = 0.7, top_p: float = 0.9, tools: list | None = None, chat_template_kwargs: dict | None = None, **kwargs) -> GenerationOutput
Generate a chat response.
Parameters:
-
messages(list[dict]) –List of chat messages [{"role": "user", "content": "..."}]
-
max_tokens(int, default:256) –Maximum tokens to generate
-
temperature(float, default:0.7) –Sampling temperature
-
top_p(float, default:0.9) –Top-p sampling parameter
-
tools(list | None, default:None) –Optional list of tools for function calling
-
**kwargs–Additional generation parameters
Returns:
-
GenerationOutput–GenerationOutput with the assistant's response
Source code in vllm_mlx/models/llm.py
vllm_mlx.models.llm.MLXLanguageModel.get_model_info
¶
Get information about the loaded model.
Source code in vllm_mlx/models/llm.py
Complete contract reference¶
Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.
vllm_mlx.models.llm.GenerationOutput · class
vllm_mlx.models.llm.GenerationOutput(text: str, tokens: list[int], finish_reason: str | None = None)
Output from text generation.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required constructor field. |
tokens |
list[int] |
yes |
none |
Required constructor field. |
finish_reason |
str \| None |
no |
None |
Optional constructor field; defaults to None. |
Returns
- Constructs:
vllm_mlx.models.llm.GenerationOutput
Exceptions and behavior
Class GenerationOutput declares 0 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.models.llm.StreamingOutput · class
vllm_mlx.models.llm.StreamingOutput(text: str, token: int, finished: bool = False, finish_reason: str | None = None, prompt_tokens: int = 0)
Streaming output chunk.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required constructor field. |
token |
int |
yes |
none |
Required constructor field. |
finished |
bool |
no |
False |
Optional constructor field; defaults to False. |
finish_reason |
str \| None |
no |
None |
Optional constructor field; defaults to None. |
prompt_tokens |
int |
no |
0 |
Optional constructor field; defaults to 0. |
Returns
- Constructs:
vllm_mlx.models.llm.StreamingOutput
Exceptions and behavior
Class StreamingOutput declares 0 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.models.llm.MLXLanguageModel · class
vllm_mlx.models.llm.MLXLanguageModel(model_name: str, tokenizer_name: str | None = None, trust_remote_code: bool = False, mtp: bool = False, mtp_num_draft_tokens: int = 1)
Wrapper around mlx-lm for LLM inference.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
model_name |
str |
yes |
none |
HuggingFace model name or local path |
tokenizer_name |
str \| None |
no |
None |
Optional separate tokenizer name |
trust_remote_code |
bool |
no |
False |
Whether to trust remote code |
mtp |
bool |
no |
False |
Enable native MTP speculative decoding (model must have MTP head) |
mtp_num_draft_tokens |
int |
no |
1 |
Draft tokens per speculative MTP step |
Returns
- Constructs:
vllm_mlx.models.llm.MLXLanguageModel
Exceptions and behavior
Class MLXLanguageModel declares 9 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.models.llm.MLXLanguageModel.__init__ · method
vllm_mlx.models.llm.MLXLanguageModel.__init__(model_name: str, tokenizer_name: str | None = None, trust_remote_code: bool = False, mtp: bool = False, mtp_num_draft_tokens: int = 1) -> not annotated
Initialize the MLX language model.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
model_name |
str |
yes |
none |
HuggingFace model name or local path |
tokenizer_name |
str \| None |
no |
None |
Optional separate tokenizer name |
trust_remote_code |
bool |
no |
False |
Whether to trust remote code |
mtp |
bool |
no |
False |
Enable native MTP speculative decoding (model must have MTP head) |
mtp_num_draft_tokens |
int |
no |
1 |
Draft tokens per speculative MTP step |
Returns
- Type:
not annotated
Exceptions and behavior
Method MLXLanguageModel.__init__ updates self.model_name, self.tokenizer_name, self.trust_remote_code, self._mtp.
No direct raise statement appears in this definition.
vllm_mlx.models.llm.MLXLanguageModel.load · method
Load the model and tokenizer.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method MLXLanguageModel.load updates self.model, self.tokenizer, self._loaded; calls logger.info, self.model_name.lower, load_model_with_fallback, ImportError; can raise ImportError; returns None.
Directly raised exceptions: ImportError.
vllm_mlx.models.llm.MLXLanguageModel._create_sampler · method
vllm_mlx.models.llm.MLXLanguageModel._create_sampler(temperature: float = 0.7, top_p: float = 0.9, top_k: int = 0, min_p: float = 0.0) -> not annotated
Create a sampler for text generation.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
temperature |
float |
no |
0.7 |
Optional positional or keyword input; defaults to 0.7. |
top_p |
float |
no |
0.9 |
Optional positional or keyword input; defaults to 0.9. |
top_k |
int |
no |
0 |
Optional positional or keyword input; defaults to 0. |
min_p |
float |
no |
0.0 |
Optional positional or keyword input; defaults to 0.0. |
Returns
- Type:
not annotated - Direct return expressions:
make_sampler(temp=temperature, top_p=top_p, top_k=top_k, min_p=min_p)
Exceptions and behavior
Method MLXLanguageModel._create_sampler calls make_sampler; returns make_sampler(temp=temperature, top_p=top_p, top_k=top_k, min_p=min_p).
No direct raise statement appears in this definition.
vllm_mlx.models.llm.MLXLanguageModel._create_logits_processors · method
vllm_mlx.models.llm.MLXLanguageModel._create_logits_processors(presence_penalty: float = 0.0, repetition_penalty: float = 1.0) -> not annotated
Create logits processors for penalty-based sampling.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
presence_penalty |
float |
no |
0.0 |
Optional positional or keyword input; defaults to 0.0. |
repetition_penalty |
float |
no |
1.0 |
Optional positional or keyword input; defaults to 1.0. |
Returns
- Type:
not annotated - Direct return expressions:
processors if processors else None
Exceptions and behavior
Method MLXLanguageModel._create_logits_processors calls make_logits_processors; returns processors if processors else None.
No direct raise statement appears in this definition.
vllm_mlx.models.llm.MLXLanguageModel.generate · method
vllm_mlx.models.llm.MLXLanguageModel.generate(prompt: str, max_tokens: int = 256, temperature: float = 0.7, top_p: float = 0.9, top_k: int = 0, min_p: float = 0.0, presence_penalty: float = 0.0, repetition_penalty: float = 1.0, stop: list[str] | None = None, logits_processors: list | None = None, **kwargs) -> GenerationOutput
Generate text from a prompt.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
prompt |
str |
yes |
none |
Input prompt text |
max_tokens |
int |
no |
256 |
Maximum number of tokens to generate |
temperature |
float |
no |
0.7 |
Sampling temperature (0 = greedy) |
top_p |
float |
no |
0.9 |
Top-p (nucleus) sampling parameter |
top_k |
int |
no |
0 |
Top-k sampling (0 = disabled) |
min_p |
float |
no |
0.0 |
Minimum probability threshold |
presence_penalty |
float |
no |
0.0 |
Additive penalty for token presence |
repetition_penalty |
float |
no |
1.0 |
Multiplicative penalty for repeating tokens |
stop |
list[str] \| None |
no |
None |
List of stop sequences |
logits_processors |
list \| None |
no |
None |
Optional externally-supplied logits processors (e.g. JSON schema constrained decoding). Merged with built-in penalty processors. |
**kwargs |
not annotated |
no |
none |
Additional variadic keyword inputs accepted by this callable. |
Returns
- Type:
GenerationOutput - Direct return expressions:
GenerationOutput(text=output_text, tokens=tokens, finish_reason=finish_reason)
Exceptions and behavior
Method MLXLanguageModel.generate calls self.load, self._create_sampler, self._create_logits_processors, list; returns GenerationOutput(text=output_text, tokens=tokens, finish_reason=finish_reason).
No direct raise statement appears in this definition.
vllm_mlx.models.llm.MLXLanguageModel.stream_generate · method
vllm_mlx.models.llm.MLXLanguageModel.stream_generate(prompt: Union[str, 'mx.array', list[int]], max_tokens: int = 256, temperature: float = 0.7, top_p: float = 0.9, top_k: int = 0, min_p: float = 0.0, presence_penalty: float = 0.0, repetition_penalty: float = 1.0, stop: list[str] | None = None, logits_processors: list | None = None, prompt_cache = None, **kwargs) -> Iterator[StreamingOutput]
Stream text generation token by token.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
prompt |
Union[str, 'mx.array', list[int]] |
yes |
none |
Input prompt text, token array, or token id list |
max_tokens |
int |
no |
256 |
Maximum number of tokens to generate |
temperature |
float |
no |
0.7 |
Sampling temperature (0 = greedy) |
top_p |
float |
no |
0.9 |
Top-p (nucleus) sampling parameter |
top_k |
int |
no |
0 |
Top-k sampling (0 = disabled) |
min_p |
float |
no |
0.0 |
Minimum probability threshold |
presence_penalty |
float |
no |
0.0 |
Additive penalty for token presence |
repetition_penalty |
float |
no |
1.0 |
Multiplicative penalty for repeating tokens |
stop |
list[str] \| None |
no |
None |
List of stop sequences |
logits_processors |
list \| None |
no |
None |
Optional positional or keyword input; defaults to None. |
prompt_cache |
not annotated |
no |
None |
Pre-populated KV cache (e.g. from SpecPrefill) |
**kwargs |
not annotated |
no |
none |
Additional variadic keyword inputs accepted by this callable. |
Returns
- Type:
Iterator[StreamingOutput] - Yields values incrementally.
Exceptions and behavior
Method MLXLanguageModel.stream_generate calls self.load, self._create_sampler, self._create_logits_processors, isinstance; yields values incrementally.
No direct raise statement appears in this definition.
vllm_mlx.models.llm.MLXLanguageModel.chat · method
vllm_mlx.models.llm.MLXLanguageModel.chat(messages: list[dict], max_tokens: int = 256, temperature: float = 0.7, top_p: float = 0.9, tools: list | None = None, chat_template_kwargs: dict | None = None, **kwargs) -> GenerationOutput
Generate a chat response.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
messages |
list[dict] |
yes |
none |
List of chat messages [{"role": "user", "content": "..."}] |
max_tokens |
int |
no |
256 |
Maximum tokens to generate |
temperature |
float |
no |
0.7 |
Sampling temperature |
top_p |
float |
no |
0.9 |
Top-p sampling parameter |
tools |
list \| None |
no |
None |
Optional list of tools for function calling |
chat_template_kwargs |
dict \| None |
no |
None |
Optional positional or keyword input; defaults to None. |
**kwargs |
not annotated |
no |
none |
Additional generation parameters |
Returns
- Type:
GenerationOutput - Direct return expressions:
self.generate(prompt=prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p, **kwargs)
Exceptions and behavior
Method MLXLanguageModel.chat calls self.load, hasattr, template_kwargs.update, self.tokenizer.apply_chat_template; returns self.generate(prompt=prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p, **kwargs).
No direct raise statement appears in this definition.
vllm_mlx.models.llm.MLXLanguageModel.get_model_info · method
Get information about the loaded model.
Parameters
This callable has no explicit inputs.
Returns
- Type:
dict - Direct return expressions:
{'loaded': False, 'model_name': self.model_name};info
Exceptions and behavior
Method MLXLanguageModel.get_model_info calls hasattr, info.update, getattr; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.models.llm.MLXLanguageModel.__repr__ · method
Method MLXLanguageModel.__repr__ returns f'<MLXLanguageModel model={self.model_name} status={status}>'.
Parameters
This callable has no explicit inputs.
Returns
- Type:
str - Direct return expressions:
f'<MLXLanguageModel model={self.model_name} status={status}>'
Exceptions and behavior
Method MLXLanguageModel.__repr__ returns f'<MLXLanguageModel model={self.model_name} status={status}>'.
No direct raise statement appears in this definition.
Complete symbol map¶
This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.
| Symbol | Kind | Signature and inputs | What it does | Source |
|---|---|---|---|---|
GenerationOutput |
class | GenerationOutput(text: str, tokens: list[int], finish_reason: str \| None = None) |
Output from text generation. | #L21-L26 |
StreamingOutput |
class | StreamingOutput(text: str, token: int, finished: bool = False, finish_reason: str \| None = None, prompt_tokens: int = 0) |
Streaming output chunk. | #L30-L37 |
MLXLanguageModel |
class | MLXLanguageModel(model_name: str, tokenizer_name: str \| None = None, trust_remote_code: bool = False, mtp: bool = False, mtp_num_draft_tokens: int = 1) |
Wrapper around mlx-lm for LLM inference. | #L40-L422 |
MLXLanguageModel.__init__ |
method | MLXLanguageModel.__init__(model_name: str, tokenizer_name: str \| None = None, trust_remote_code: bool = False, mtp: bool = False, mtp_num_draft_tokens: int = 1) -> not annotated |
Initialize the MLX language model. | #L53-L79 |
MLXLanguageModel.load |
method | MLXLanguageModel.load() -> None |
Load the model and tokenizer. | #L81-L114 |
MLXLanguageModel._create_sampler |
method | MLXLanguageModel._create_sampler(temperature: float = 0.7, top_p: float = 0.9, top_k: int = 0, min_p: float = 0.0) -> not annotated |
Create a sampler for text generation. | #L116-L131 |
MLXLanguageModel._create_logits_processors |
method | MLXLanguageModel._create_logits_processors(presence_penalty: float = 0.0, repetition_penalty: float = 1.0) -> not annotated |
Create logits processors for penalty-based sampling. | #L133-L147 |
MLXLanguageModel.generate |
method | MLXLanguageModel.generate(prompt: str, max_tokens: int = 256, temperature: float = 0.7, top_p: float = 0.9, top_k: int = 0, min_p: float = 0.0, presence_penalty: float = 0.0, repetition_penalty: float = 1.0, stop: list[str] \| None = None, logits_processors: list \| None = None, **kwargs) -> GenerationOutput |
Generate text from a prompt. | #L149-L219 |
MLXLanguageModel.stream_generate |
method | MLXLanguageModel.stream_generate(prompt: Union[str, 'mx.array', list[int]], max_tokens: int = 256, temperature: float = 0.7, top_p: float = 0.9, top_k: int = 0, min_p: float = 0.0, presence_penalty: float = 0.0, repetition_penalty: float = 1.0, stop: list[str] \| None = None, logits_processors: list \| None = None, prompt_cache = None, **kwargs) -> Iterator[StreamingOutput] |
Stream text generation token by token. | #L221-L325 |
MLXLanguageModel.chat |
method | MLXLanguageModel.chat(messages: list[dict], max_tokens: int = 256, temperature: float = 0.7, top_p: float = 0.9, tools: list \| None = None, chat_template_kwargs: dict \| None = None, **kwargs) -> GenerationOutput |
Generate a chat response. | #L327-L393 |
MLXLanguageModel.get_model_info |
method | MLXLanguageModel.get_model_info() -> dict |
Get information about the loaded model. | #L395-L418 |
MLXLanguageModel.__repr__ |
method | MLXLanguageModel.__repr__() -> str |
Method MLXLanguageModel.__repr__ returns f'<MLXLanguageModel model={self.model_name} status={status}>'. |
#L420-L422 |