PyObjC MLCompute Framework

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

Demonstrates how to import MLCompute classes and query available CPU and GPU devices. MLCompute provides classes like MLCDevice and MLCGraph for building and executing machine learning models using Apple's Metal Performance Shaders.

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()

view raw JSON →