PyObjC MLCompute Framework
PyObjC-framework-MLCompute provides Python wrappers for Apple's MLCompute framework on macOS, enabling Python applications to leverage GPU acceleration for machine learning tasks. It is part of the larger PyObjC project, currently at version 12.1, with releases typically tied to macOS SDK updates and Python version support.
Warnings
- breaking PyObjC 12.0 and later dropped support for Python 3.9. Attempting to install or run on Python 3.9 will result in errors.
- breaking PyObjC 11.0 and later dropped support for Python 3.8. Attempting to install or run on Python 3.8 will result in errors.
- breaking The `IMServicePlugIn` framework bindings were entirely removed in PyObjC 10.0 as the framework was deprecated by Apple in macOS 10.13 and completely removed in macOS 14.
- gotcha From PyObjC 11.1, the core bridge's behavior for initializer methods (those in the 'init' family) was updated to align with Clang's Automatic Reference Counting (ARC) documentation. Init methods now correctly model stealing a reference to `self` and returning a new one.
- gotcha PyObjC versions 10.3 and later introduced specific limitations regarding the interaction between `__init__` and `__new__` in custom Python subclasses of Objective-C classes. While 10.3.1 partially re-enabled `__init__` for user-implemented `__new__` methods, code using PyObjC's provided `__new__` still cannot use `__init__`.
Install
-
pip install pyobjc-framework-mlcompute -
pip install pyobjc
Imports
- MLCDevice
from MLCompute import MLCDevice
- MLCGraph
from MLCompute import MLCGraph
Quickstart
from MLCompute import MLCDevice, MLCDeviceTypeCPU, MLCDeviceTypeGPU
def quickstart_mlcompute():
# Get the default CPU device
cpu_device = MLCDevice.cpuDevice()
print(f"CPU Device: {cpu_device.description()} (Type: {cpu_device.deviceType()})")
# Get the default GPU device if available
gpu_device = MLCDevice.gpuDevice()
if gpu_device:
print(f"GPU Device: {gpu_device.description()} (Type: {gpu_device.deviceType()})")
else:
print("No GPU device found or MLCompute is running on a Mac without a Metal GPU.")
# Get a device of any type (prefers GPU if available)
any_device = MLCDevice.deviceWithAnyType()
print(f"Any Device: {any_device.description()} (Type: {any_device.deviceType()})")
quickstart_mlcompute()