Skip to main content

Crate apple_mpsgraph

Crate apple_mpsgraph 

Source
Expand description

§mpsgraph-rs

Safe Rust bindings for Apple’s MetalPerformanceShadersGraph framework on macOS.

The GitHub repository is mpsgraph-rs; the published crates.io package is apple-mpsgraph because the short package name is already taken.

§Install

cargo add apple-mpsgraph apple-metal

§Quick start

use apple_metal::MetalDevice;
use apple_mpsgraph::{data_type, Feed, Graph, TensorData};

let device = MetalDevice::system_default().expect("no Metal device");
let graph = Graph::new().expect("graph");
let input = graph
    .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
    .expect("input placeholder");
let bias = graph.constant_scalar(1.0, data_type::FLOAT32).expect("bias");
let added = graph.addition(&input, &bias, Some("add")).expect("add");
let output = graph.relu(&added, Some("relu")).expect("relu");
let data = TensorData::from_f32_slice(&device, &[1.0, -2.0, 3.0, -4.0], &[2, 2])
    .expect("tensor data");
let results = graph
    .run(&[Feed::new(&input, &data)], &[&output])
    .expect("graph run");
let values = results[0].read_f32().expect("read result");
assert_eq!(values, vec![2.0, 0.0, 4.0, 0.0]);

§v0.1 surface

  • Graph, Tensor, and ordered Feed / FeedDescription helpers
  • TensorData from CPU bytes, f32 slices, or existing MTLBuffers
  • Direct graph execution plus compiled Executable runs on MTLCommandQueue
  • Core graph construction ops:
    • placeholders and constants
    • addition, subtraction, multiplication, division
    • matrix multiplication
    • reshape, transpose/permute, slice, broadcast
    • reLU, sigmoid, softMax
    • reduction sum / max / min and mean
    • 2D convolution, max pooling, and normalization helpers
  • Shared constants for MPSDataType, MPSGraphTensorNamedDataLayout, and MPSGraphPaddingStyle

§Smoke examples

cargo run --example 01_add_relu
cargo run --example 02_compile_matmul

Re-exports§

pub use crate::data::TensorData;
pub use crate::error::Error;
pub use crate::error::Result;
pub use crate::graph::data_type;
pub use crate::graph::data_type_size;
pub use crate::graph::padding_style;
pub use crate::graph::tensor_named_data_layout;
pub use crate::graph::Convolution2DDescriptor;
pub use crate::graph::Convolution2DDescriptorInfo;
pub use crate::graph::Executable;
pub use crate::graph::Feed;
pub use crate::graph::FeedDescription;
pub use crate::graph::Graph;
pub use crate::graph::Pooling2DDescriptor;
pub use crate::graph::Pooling2DDescriptorInfo;
pub use crate::graph::Tensor;

Modules§

data
error
ffi
graph