|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +"""Aphrodite Metal runtime - high-performance LLM inference on Apple Silicon. |
| 3 | +
|
| 4 | +This runtime enables Aphrodite to run on Apple Silicon Macs using MLX as the |
| 5 | +primary compute backend, with PyTorch for model loading and interoperability. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | +import os |
| 10 | +import sys |
| 11 | + |
| 12 | +__version__ = "0.2.0" |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +def _configure_logging() -> None: |
| 18 | + """Configure aphrodite.metal logging to mirror Aphrodite settings.""" |
| 19 | + from aphrodite.envs import APHRODITE_LOGGING_LEVEL |
| 20 | + |
| 21 | + aphrodite_logger = logging.getLogger("aphrodite") |
| 22 | + metal_logger = logging.getLogger("aphrodite.metal") |
| 23 | + metal_logger.setLevel(logging.getLevelName(APHRODITE_LOGGING_LEVEL)) |
| 24 | + |
| 25 | + if aphrodite_logger.handlers and not metal_logger.handlers: |
| 26 | + for handler in aphrodite_logger.handlers: |
| 27 | + metal_logger.addHandler(handler) |
| 28 | + metal_logger.propagate = False |
| 29 | + |
| 30 | + |
| 31 | +def _apply_macos_defaults() -> None: |
| 32 | + """Apply safe defaults for macOS when using the Metal plugin. |
| 33 | +
|
| 34 | + Aphrodite's v1 engine launches a worker process. When the start method is |
| 35 | + `fork`, macOS can crash the child process if the parent has imported libraries that |
| 36 | + touched the Objective-C runtime (commonly surfaced as |
| 37 | + `objc_initializeAfterForkError`). |
| 38 | +
|
| 39 | + Defaulting to `spawn` avoids forking a partially-initialized runtime. |
| 40 | + """ |
| 41 | + if sys.platform != "darwin": |
| 42 | + return |
| 43 | + if os.environ.get("APHRODITE_WORKER_MULTIPROC_METHOD") is not None: |
| 44 | + return |
| 45 | + |
| 46 | + # macOS fork-safety: |
| 47 | + # `fork()` with an initialized Objective-C runtime is unsafe and can crash in |
| 48 | + # the child process (commonly observed via `objc_initializeAfterForkError`). |
| 49 | + # Using `spawn` starts a fresh interpreter and avoids inheriting this state. |
| 50 | + # See: https://www.sealiesoftware.com/blog/archive/2017/6/5/Objective-C_and_fork_in_macOS_1013.html |
| 51 | + os.environ["APHRODITE_WORKER_MULTIPROC_METHOD"] = "spawn" |
| 52 | + logger.debug( |
| 53 | + "macOS detected + Metal plugin active: defaulting APHRODITE_WORKER_MULTIPROC_METHOD " |
| 54 | + "to 'spawn' to avoid Objective-C runtime fork-safety crashes. " |
| 55 | + "Set APHRODITE_WORKER_MULTIPROC_METHOD explicitly to override." |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +# Lazy imports to avoid loading heavy runtime dependencies on plain import. |
| 60 | +def __getattr__(name): |
| 61 | + """Lazy import module components.""" |
| 62 | + if name == "MetalConfig": |
| 63 | + from aphrodite.metal.config import MetalConfig |
| 64 | + |
| 65 | + return MetalConfig |
| 66 | + elif name == "get_config": |
| 67 | + from aphrodite.metal.config import get_config |
| 68 | + |
| 69 | + return get_config |
| 70 | + elif name == "reset_config": |
| 71 | + from aphrodite.metal.config import reset_config |
| 72 | + |
| 73 | + return reset_config |
| 74 | + elif name == "MetalPlatform": |
| 75 | + from aphrodite.metal.platform import MetalPlatform |
| 76 | + |
| 77 | + return MetalPlatform |
| 78 | + elif name == "register": |
| 79 | + return _register |
| 80 | + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |
| 81 | + |
| 82 | + |
| 83 | +__all__ = [ |
| 84 | + "MetalConfig", |
| 85 | + "MetalPlatform", |
| 86 | + "get_config", |
| 87 | + "reset_config", |
| 88 | + "register", |
| 89 | +] |
| 90 | + |
| 91 | + |
| 92 | +def _register() -> str | None: |
| 93 | + """Register the Metal platform with Aphrodite. |
| 94 | +
|
| 95 | + Kept for compatibility with Aphrodite's plugin-style platform loader even |
| 96 | + though Metal is wired as a built-in platform. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + Fully qualified class name if platform is available, None otherwise |
| 100 | + """ |
| 101 | + _configure_logging() |
| 102 | + _apply_macos_defaults() |
| 103 | + |
| 104 | + # Register our env vars with Aphrodite's registry so validate_environ() |
| 105 | + # does not warn about unknown APHRODITE_METAL_* / APHRODITE_MLX_* variables. |
| 106 | + import aphrodite.envs |
| 107 | + |
| 108 | + from aphrodite.metal.envs import environment_variables as metal_env_vars |
| 109 | + |
| 110 | + aphrodite.envs.environment_variables.update(metal_env_vars) |
| 111 | + |
| 112 | + from aphrodite.metal.compat import apply_compat_patches |
| 113 | + |
| 114 | + apply_compat_patches() |
| 115 | + |
| 116 | + from aphrodite.metal.platform import MetalPlatform |
| 117 | + |
| 118 | + if MetalPlatform.is_available(): |
| 119 | + return "aphrodite.platforms.metal.MetalPlatform" |
| 120 | + return None |
0 commit comments