memory ¶
Memory-tier helpers for Apple Silicon MLX/MPS experiments.
macOS does not expose a perfect "pretend this machine only has 16 GB unified memory" switch. MLX can cap the allocator used by the Apple-native DiT path, and PyTorch MPS exposes process-level watermark environment variables for the hybrid prompt/decode stages. Applying both gives benchmark and generation entrypoints a practical, explicit way to exercise memory-tier presets.
Classes¶
fastvideo.mlx_runtime.memory.AppliedMemoryLimits dataclass ¶
AppliedMemoryLimits(mlx_memory_limit_gib: float | None = None, mlx_cache_limit_gib: float | None = None, mlx_disable_cache: bool = False, mlx_wired_limit_gib: float | None = None, torch_mps_high_watermark_ratio: float | None = None, torch_mps_low_watermark_ratio: float | None = None, applied_bytes: dict[str, int] = dict(), previous_bytes: dict[str, int] = dict(), errors: dict[str, str] = dict())
Memory limits applied for one Apple Silicon benchmark/generation process.
Methods:¶
fastvideo.mlx_runtime.memory.AppliedMemoryLimits.as_metrics ¶
Flatten the configured memory limits, applied values, previous values, and errors into a metrics dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, int | float | str | bool | None] | dict[str, int | float | str | bool | None]: Metrics keyed by limit names and their corresponding values. |
Source code in fastvideo/mlx_runtime/memory.py
Functions:¶
fastvideo.mlx_runtime.memory.add_memory_limit_args ¶
add_memory_limit_args(parser: ArgumentParser, *, mlx_memory_limit_gib: float | None = None, mlx_cache_limit_gib: float | None = None, mlx_disable_cache: bool = False, mlx_wired_limit_gib: float | None = None, torch_mps_high_watermark_ratio: float | None = None, torch_mps_low_watermark_ratio: float | None = None) -> None
Add configurable Apple Silicon memory-limit options to an argument parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser | ArgumentParser | Parser to which the options are added. | required |
mlx_memory_limit_gib | float | None | Default MLX memory limit in GiB. | None |
mlx_cache_limit_gib | float | None | Default MLX cache limit in GiB. | None |
mlx_disable_cache | bool | Whether the cache limit defaults to zero. | False |
mlx_wired_limit_gib | float | None | Default MLX wired-memory limit in GiB. | None |
torch_mps_high_watermark_ratio | float | None | Default PyTorch MPS high-watermark ratio. | None |
torch_mps_low_watermark_ratio | float | None | Default PyTorch MPS low-watermark ratio. | None |
Source code in fastvideo/mlx_runtime/memory.py
fastvideo.mlx_runtime.memory.apply_memory_limits ¶
apply_memory_limits(*, mlx_memory_limit_gib: float | None = None, mlx_cache_limit_gib: float | None = None, mlx_disable_cache: bool = False, mlx_wired_limit_gib: float | None = None, torch_mps_high_watermark_ratio: float | None = None, torch_mps_low_watermark_ratio: float | None = None, mx_module: Any | None = None) -> AppliedMemoryLimits
Apply optional MLX allocator limits and PyTorch MPS watermarks.
PyTorch reads MPS watermark variables when the MPS backend initializes, so call this before importing PyTorch. Specifying only a high watermark sets the low watermark to 0.0. MLX limit-setting failures are recorded in the result and do not prevent other limits from being applied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mlx_memory_limit_gib | float | None | Maximum MLX memory in GiB. | None |
mlx_cache_limit_gib | float | None | Maximum MLX cache size in GiB. | None |
mlx_disable_cache | bool | Whether to disable the MLX cache. | False |
mlx_wired_limit_gib | float | None | Maximum MLX wired memory in GiB. | None |
torch_mps_high_watermark_ratio | float | None | PyTorch MPS high watermark ratio. | None |
torch_mps_low_watermark_ratio | float | None | PyTorch MPS low watermark ratio. | None |
Returns:
| Name | Type | Description |
|---|---|---|
AppliedMemoryLimits | AppliedMemoryLimits | Configured values, applied and previous MLX byte limits, MPS watermark values, and per-limit errors. |
Source code in fastvideo/mlx_runtime/memory.py
fastvideo.mlx_runtime.memory.cleanup_mlx ¶
cleanup_mlx(mx_module: Any | None = None) -> None
Collect unreachable MLX objects, then release their allocator cache.
fastvideo.mlx_runtime.memory.cleanup_torch_mps ¶
cleanup_torch_mps(torch_module: Any | None = None) -> None
Collect unreachable Torch objects, then release the MPS allocator cache.
Source code in fastvideo/mlx_runtime/memory.py
fastvideo.mlx_runtime.memory.gib_to_bytes ¶
Convert a positive memory limit from GiB to bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value | float | None | Memory limit in GiB, or | required |
Returns:
| Type | Description |
|---|---|
int | None | int | None: The memory limit in bytes, or |
Raises:
| Type | Description |
|---|---|
ValueError | If |