vllm_mlx.model_runner¶
MLX Model Runner for vLLM.
View the complete module source at #L1-L476.
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.model_runner
¶
MLX Model Runner for vLLM.
This module implements the model runner that bridges vLLM's request handling with mlx-lm's inference capabilities.
Includes low-level optimizations: - mx.compile() for kernel fusion - Memory bandwidth optimization - Prefill chunking for L2 cache efficiency
vllm_mlx.model_runner.SamplerOutput
dataclass
¶
vllm_mlx.model_runner.MLXModelRunnerOutput
dataclass
¶
MLXModelRunnerOutput(req_id_to_token_ids: dict[str, list[int]], req_id_to_logprobs: dict[str, list[dict]] | None = None, num_tokens_generated: int = 0, generation_time_s: float = 0.0)
Output from MLX model runner, compatible with vLLM's ModelRunnerOutput.
vllm_mlx.model_runner.MLXModelRunnerOutput.req_id_to_token_ids
instance-attribute
¶
vllm_mlx.model_runner.MLXModelRunnerOutput.req_id_to_logprobs
class-attribute
instance-attribute
¶
vllm_mlx.model_runner.MLXModelRunnerOutput.num_tokens_generated
class-attribute
instance-attribute
¶
vllm_mlx.model_runner.MLXModelRunnerOutput.generation_time_s
class-attribute
instance-attribute
¶
vllm_mlx.model_runner.MLXModelRunner
¶
Model runner that uses mlx-lm for inference.
This class handles: - Model loading via mlx-lm - Converting vLLM requests to mlx-lm format - Running inference and returning results in vLLM format - KV cache management (delegated to mlx-lm)
Optimizations: - mx.compile() for kernel fusion (fuses multiple ops into single Metal kernel) - Memory optimization for bandwidth efficiency - Prefill chunking for L2 cache utilization
Initialize MLX model runner.
Parameters:
-
vllm_config(VllmConfig) –vLLM configuration
-
enable_optimizations(bool, default:True) –Whether to enable low-level optimizations
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner.model_config
instance-attribute
¶
vllm_mlx.model_runner.MLXModelRunner.cache_config
instance-attribute
¶
vllm_mlx.model_runner.MLXModelRunner.scheduler_config
instance-attribute
¶
vllm_mlx.model_runner.MLXModelRunner._enable_optimizations
instance-attribute
¶
vllm_mlx.model_runner.MLXModelRunner._compiled_forward
instance-attribute
¶
vllm_mlx.model_runner.MLXModelRunner.load_model
¶
Load model using mlx-lm with optimizations.
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner._apply_optimizations
¶
Apply low-level optimizations for maximum performance.
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner._setup_compiled_forward
¶
Setup compiled forward pass using mx.compile() for kernel fusion.
This fuses multiple operations into single Metal kernels, reducing kernel launch overhead and improving throughput.
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner._create_default_sampler
¶
Create default sampler for generation.
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner.initialize_cache
¶
vllm_mlx.model_runner.MLXModelRunner.get_kv_cache_spec
¶
vllm_mlx.model_runner.MLXModelRunner.get_cache_block_size_bytes
¶
Calculate cache block size in bytes.
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner.warm_up
¶
Warm up model with a test generation.
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner.execute_model
¶
execute_model(scheduler_output: SchedulerOutput) -> MLXModelRunnerOutput
Execute model inference for scheduled requests.
Parameters:
-
scheduler_output(SchedulerOutput) –Contains requests to process
Returns:
-
MLXModelRunnerOutput–MLXModelRunnerOutput with generated tokens
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner._prefill_with_chunking
¶
Process prompt with optimal chunking for L2 cache efficiency.
Long prompts are broken into chunks that fit in L2 cache, maximizing memory bandwidth utilization during prefill.
Parameters:
-
input_ids(array) –Input token IDs [1, seq_len]
-
cache(Optional[Any], default:None) –Optional existing KV cache
Returns:
-
tuple[array, Any]–Tuple of (logits, updated_cache)
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner._generate_for_request
¶
_generate_for_request(prompt_token_ids: list[int], sampling_params: Any, max_tokens: int = 1) -> list[int]
Generate tokens for a single request.
Uses optimizations when enabled: - Compiled forward pass (kernel fusion) - Prefill chunking for long prompts
Parameters:
-
prompt_token_ids(list[int]) –Input token IDs
-
sampling_params(Any) –Sampling parameters
-
max_tokens(int, default:1) –Maximum tokens to generate
Returns:
-
list[int]–List of generated token IDs
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner._continue_generation
¶
Continue generation for an existing request.
This is a placeholder - in a full implementation, we would use cached KV states to continue generation efficiently.
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner.decode_tokens
¶
vllm_mlx.model_runner.MLXModelRunner.get_model_info
¶
Get information about the loaded model and optimizations.
Source code in vllm_mlx/model_runner.py
vllm_mlx.model_runner.MLXModelRunner.__repr__
¶
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.model_runner.SamplerOutput · class
Output from sampling.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
token_ids |
list[int] |
yes |
none |
Required constructor field. |
logprobs |
list[dict] \| None |
no |
None |
Optional constructor field; defaults to None. |
Returns
- Constructs:
vllm_mlx.model_runner.SamplerOutput
Exceptions and behavior
Class SamplerOutput declares 0 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunnerOutput · class
vllm_mlx.model_runner.MLXModelRunnerOutput(req_id_to_token_ids: dict[str, list[int]], req_id_to_logprobs: dict[str, list[dict]] | None = None, num_tokens_generated: int = 0, generation_time_s: float = 0.0)
Output from MLX model runner, compatible with vLLM's ModelRunnerOutput.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
req_id_to_token_ids |
dict[str, list[int]] |
yes |
none |
Required constructor field. |
req_id_to_logprobs |
dict[str, list[dict]] \| None |
no |
None |
Optional constructor field; defaults to None. |
num_tokens_generated |
int |
no |
0 |
Optional constructor field; defaults to 0. |
generation_time_s |
float |
no |
0.0 |
Optional constructor field; defaults to 0.0. |
Returns
- Constructs:
vllm_mlx.model_runner.MLXModelRunnerOutput
Exceptions and behavior
Class MLXModelRunnerOutput declares 0 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner · class
Model runner that uses mlx-lm for inference.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
vllm_config |
'VllmConfig' |
yes |
none |
vLLM configuration |
enable_optimizations |
bool |
no |
True |
Whether to enable low-level optimizations |
Returns
- Constructs:
vllm_mlx.model_runner.MLXModelRunner
Exceptions and behavior
Class MLXModelRunner declares 16 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.__init__ · method
vllm_mlx.model_runner.MLXModelRunner.__init__(vllm_config: 'VllmConfig', enable_optimizations: bool = True) -> not annotated
Initialize MLX model runner.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
vllm_config |
'VllmConfig' |
yes |
none |
vLLM configuration |
enable_optimizations |
bool |
no |
True |
Whether to enable low-level optimizations |
Returns
- Type:
not annotated
Exceptions and behavior
Method MLXModelRunner.__init__ updates self.vllm_config, self.model_config, self.cache_config, self.scheduler_config; calls logger.info.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.load_model · method
Load model using mlx-lm with optimizations.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method MLXModelRunner.load_model updates self.model, self.tokenizer, self._loaded; calls logger.info, time.time, load, self._create_default_sampler; can raise ImportError; returns None.
Directly raised exceptions: ImportError.
vllm_mlx.model_runner.MLXModelRunner._apply_optimizations · method
Apply low-level optimizations for maximum performance.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None
Exceptions and behavior
Method MLXModelRunner._apply_optimizations updates self._hardware_info; calls detect_hardware, logger.info, configure_memory_optimization, self._setup_compiled_forward.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner._setup_compiled_forward · method
Setup compiled forward pass using mx.compile() for kernel fusion.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Method MLXModelRunner._setup_compiled_forward updates self._compiled_forward; calls hasattr, mx.compile, logger.info, logger.warning; returns None.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner._create_default_sampler · method
Create default sampler for generation.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None
Exceptions and behavior
Method MLXModelRunner._create_default_sampler updates self._sampler; calls make_sampler, logger.warning.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.initialize_cache · method
Initialize KV cache.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
num_blocks |
int |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
None
Exceptions and behavior
Method MLXModelRunner.initialize_cache updates self._num_cache_blocks; calls logger.info.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.get_kv_cache_spec · method
Get KV cache specification.
Parameters
This callable has no explicit inputs.
Returns
- Type:
dict - Direct return expressions:
{'num_blocks': self._num_cache_blocks, 'block_size': self.cache_config.block_size}
Exceptions and behavior
Method MLXModelRunner.get_kv_cache_spec returns {'num_blocks': self._num_cache_blocks, 'block_size': self.cache_config.block_size}.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.get_cache_block_size_bytes · method
Calculate cache block size in bytes.
Parameters
This callable has no explicit inputs.
Returns
- Type:
int - Direct return expressions:
0;2 * block_size * num_layers * num_kv_heads * head_size * 2
Exceptions and behavior
Method MLXModelRunner.get_cache_block_size_bytes calls getattr; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.warm_up · method
Warm up model with a test generation.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None
Exceptions and behavior
Method MLXModelRunner.warm_up calls self.load_model, logger.info, generate, logger.warning.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.execute_model · method
vllm_mlx.model_runner.MLXModelRunner.execute_model(scheduler_output: 'SchedulerOutput') -> MLXModelRunnerOutput
Execute model inference for scheduled requests.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
scheduler_output |
'SchedulerOutput' |
yes |
none |
Contains requests to process |
Returns
- Type:
MLXModelRunnerOutput - Direct return expressions:
MLXModelRunnerOutput(req_id_to_token_ids=req_id_to_token_ids, num_tokens_generated=total_tokens, generation_time_s=gene…
Exceptions and behavior
Method MLXModelRunner.execute_model calls RuntimeError, time.time, self._generate_for_request, len; can raise RuntimeError; returns MLXModelRunnerOutput(req_id_to_token_ids=req_id_to_token_ids, num_tokens_generated=total_tokens, generation_time_s=gene….
Directly raised exceptions: RuntimeError.
vllm_mlx.model_runner.MLXModelRunner._prefill_with_chunking · method
vllm_mlx.model_runner.MLXModelRunner._prefill_with_chunking(input_ids: mx.array, cache: Optional[Any] = None) -> tuple[mx.array, Any]
Process prompt with optimal chunking for L2 cache efficiency.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
input_ids |
mx.array |
yes |
none |
Input token IDs [1, seq_len] |
cache |
Optional[Any] |
no |
None |
Optional existing KV cache |
Returns
- Type:
tuple[mx.array, Any] - Direct return expressions:
forward_fn(input_ids, cache=cache);(logits, cache)
Exceptions and behavior
Method MLXModelRunner._prefill_with_chunking calls len, get_optimal_prefill_size, input_ids.reshape, forward_fn; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner._prefill_with_chunking.get_optimal_prefill_size · nested function
vllm_mlx.model_runner.MLXModelRunner._prefill_with_chunking.get_optimal_prefill_size(seq_len) -> not annotated
Nested Function MLXModelRunner._prefill_with_chunking.get_optimal_prefill_size calls min; returns min(512, seq_len).
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
seq_len |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated - Direct return expressions:
min(512, seq_len)
Exceptions and behavior
Nested Function MLXModelRunner._prefill_with_chunking.get_optimal_prefill_size calls min; returns min(512, seq_len).
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner._generate_for_request · method
vllm_mlx.model_runner.MLXModelRunner._generate_for_request(prompt_token_ids: list[int], sampling_params: Any, max_tokens: int = 1) -> list[int]
Generate tokens for a single request.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
prompt_token_ids |
list[int] |
yes |
none |
Input token IDs |
sampling_params |
Any |
yes |
none |
Sampling parameters |
max_tokens |
int |
no |
1 |
Maximum tokens to generate |
Returns
- Type:
list[int] - Direct return expressions:
generated_ids;[]
Exceptions and behavior
Method MLXModelRunner._generate_for_request calls getattr, make_sampler, mx.array, generate_step; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner._continue_generation · method
Continue generation for an existing request.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
req_id |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[int] - Direct return expressions:
[]
Exceptions and behavior
Method MLXModelRunner._continue_generation returns [].
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.decode_tokens · method
Decode token IDs to text.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
token_ids |
list[int] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str - Direct return expressions:
'';self.tokenizer.decode(token_ids)
Exceptions and behavior
Method MLXModelRunner.decode_tokens calls self.tokenizer.decode; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.get_model_info · method
Get information about the loaded model and optimizations.
Parameters
This callable has no explicit inputs.
Returns
- Type:
dict - Direct return expressions:
info
Exceptions and behavior
Method MLXModelRunner.get_model_info calls getattr, info.update; returns info.
No direct raise statement appears in this definition.
vllm_mlx.model_runner.MLXModelRunner.__repr__ · method
Method MLXModelRunner.__repr__ returns f'<MLXModelRunner model={self.model_config.model} status={status} mode={opt_status}>'.
Parameters
This callable has no explicit inputs.
Returns
- Type:
str - Direct return expressions:
f'<MLXModelRunner model={self.model_config.model} status={status} mode={opt_status}>'
Exceptions and behavior
Method MLXModelRunner.__repr__ returns f'<MLXModelRunner model={self.model_config.model} status={status} mode={opt_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 |
|---|---|---|---|---|
SamplerOutput |
class | SamplerOutput(token_ids: list[int], logprobs: list[dict] \| None = None) |
Output from sampling. | #L29-L33 |
MLXModelRunnerOutput |
class | MLXModelRunnerOutput(req_id_to_token_ids: dict[str, list[int]], req_id_to_logprobs: dict[str, list[dict]] \| None = None, num_tokens_generated: int = 0, generation_time_s: float = 0.0) |
Output from MLX model runner, compatible with vLLM's ModelRunnerOutput. | #L37-L50 |
MLXModelRunner |
class | MLXModelRunner(vllm_config: 'VllmConfig', enable_optimizations: bool = True) |
Model runner that uses mlx-lm for inference. | #L53-L476 |
MLXModelRunner.__init__ |
method | MLXModelRunner.__init__(vllm_config: 'VllmConfig', enable_optimizations: bool = True) -> not annotated |
Initialize MLX model runner. | #L69-L104 |
MLXModelRunner.load_model |
method | MLXModelRunner.load_model() -> None |
Load model using mlx-lm with optimizations. | #L106-L145 |
MLXModelRunner._apply_optimizations |
method | MLXModelRunner._apply_optimizations() -> None |
Apply low-level optimizations for maximum performance. | #L147-L168 |
MLXModelRunner._setup_compiled_forward |
method | MLXModelRunner._setup_compiled_forward() -> None |
Setup compiled forward pass using mx.compile() for kernel fusion. | #L170-L193 |
MLXModelRunner._create_default_sampler |
method | MLXModelRunner._create_default_sampler() -> None |
Create default sampler for generation. | #L195-L205 |
MLXModelRunner.initialize_cache |
method | MLXModelRunner.initialize_cache(num_blocks: int) -> None |
Initialize KV cache. | #L207-L210 |
MLXModelRunner.get_kv_cache_spec |
method | MLXModelRunner.get_kv_cache_spec() -> dict |
Get KV cache specification. | #L215-L220 |
MLXModelRunner.get_cache_block_size_bytes |
method | MLXModelRunner.get_cache_block_size_bytes() -> int |
Calculate cache block size in bytes. | #L222-L240 |
MLXModelRunner.warm_up |
method | MLXModelRunner.warm_up() -> None |
Warm up model with a test generation. | #L242-L263 |
MLXModelRunner.execute_model |
method | MLXModelRunner.execute_model(scheduler_output: 'SchedulerOutput') -> MLXModelRunnerOutput |
Execute model inference for scheduled requests. | #L265-L315 |
MLXModelRunner._prefill_with_chunking |
method | MLXModelRunner._prefill_with_chunking(input_ids: mx.array, cache: Optional[Any] = None) -> tuple[mx.array, Any] |
Process prompt with optimal chunking for L2 cache efficiency. | #L317-L362 |
MLXModelRunner._prefill_with_chunking.get_optimal_prefill_size |
nested function | MLXModelRunner._prefill_with_chunking.get_optimal_prefill_size(seq_len) -> not annotated |
Nested Function MLXModelRunner._prefill_with_chunking.get_optimal_prefill_size calls min; returns min(512, seq_len). |
#L339-L340 |
MLXModelRunner._generate_for_request |
method | MLXModelRunner._generate_for_request(prompt_token_ids: list[int], sampling_params: Any, max_tokens: int = 1) -> list[int] |
Generate tokens for a single request. | #L364-L418 |
MLXModelRunner._continue_generation |
method | MLXModelRunner._continue_generation(req_id: str) -> list[int] |
Continue generation for an existing request. | #L420-L428 |
MLXModelRunner.decode_tokens |
method | MLXModelRunner.decode_tokens(token_ids: list[int]) -> str |
Decode token IDs to text. | #L430-L434 |
MLXModelRunner.get_model_info |
method | MLXModelRunner.get_model_info() -> dict |
Get information about the loaded model and optimizations. | #L436-L471 |
MLXModelRunner.__repr__ |
method | MLXModelRunner.__repr__() -> str |
Method MLXModelRunner.__repr__ returns f'<MLXModelRunner model={self.model_config.model} status={status} mode={opt_status}>'. |
#L473-L476 |