PyObjC: MetalPerformanceShadersGraph
PyObjC-framework-MetalPerformanceShadersGraph provides Python wrappers for Apple's MetalPerformanceShadersGraph framework on macOS. This framework enables high-performance, energy-efficient computation on Apple platforms by leveraging various hardware compute blocks, allowing users to generate symbolic compute graphs of operations for GPU execution. The library is actively maintained with frequent releases, often synchronized with new macOS SDK updates. Its current version is 12.1.
Warnings
- breaking PyObjC has dropped support for older Python versions. PyObjC 12.0 dropped support for Python 3.9, and PyObjC 11.0 dropped support for Python 3.8.
- breaking The behavior of `__init__` and object instantiation was significantly changed in PyObjC 10.3 and 11.1. PyObjC 10.3 initially broke `__init__` for classes not defining their own `__new__`, though 10.3.1 partially reverted this. PyObjC 11.1 aligned reference counting for 'init' methods with `clang`'s ARC documentation. Direct calls to methods on partially initialized objects (`SomeClass.alloc()`) may lead to unexpected behavior or crashes in recent versions, as such patterns used to 'accidentally work' but hid reference counting bugs.
- gotcha PyObjC does not support the experimental free-threading feature (PEP 703) introduced in Python 3.13. Using PyObjC in a free-threaded Python environment may lead to undefined behavior or crashes.
- gotcha Key-Value Observing (KVO) usage for subclasses of `NSProxy` defined in Python was automatically disabled in PyObjC 12.1 due to prior issues.
- breaking The `IMServicePlugIn` framework bindings were removed in PyObjC 10.0 because the entire framework was deprecated in macOS 10.13 and removed in macOS 14.
Install
-
pip install pyobjc-framework-metalperformanceshadersgraph
Imports
- MPSGraph
from MetalPerformanceShadersGraph import MPSGraph
- NSObject
from Foundation import NSObject
Quickstart
from MetalPerformanceShadersGraph import MPSGraph
from Foundation import NSObject # For general PyObjC interaction
# Instantiate a Metal Performance Shaders Graph
# Note: This is a minimal example. Actual graph creation involves defining
# tensors, operations, and compiling for a Metal device.
# Refer to Apple's MetalPerformanceShadersGraph documentation for usage.
try:
graph = MPSGraph.alloc().init()
# Alternatively, for PyObjC 10.4+ a more Pythonic way often works:
# graph = MPSGraph()
print(f"Successfully instantiated MPSGraph: {graph}")
print(f"MPSGraph options: {graph.options()}")
except Exception as e:
print(f"Failed to instantiate MPSGraph or access options: {e}")
print("Ensure you are running on macOS with Metal support.")
# Example of a basic NSObject to confirm PyObjC is working generally
obj = NSObject.alloc().init()
print(f"Successfully instantiated NSObject: {obj}")