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.2 surface
- Core wrappers for
Graph,Tensor,TensorData,Executable,Feed, andFeedDescription - Metadata and descriptor coverage for
GraphDevice,ShapedType,Operation,CompilationDescriptor,ExecutionDescriptor,ExecutableExecutionDescriptor, andExecutableSerializationDescriptor - Graph / executable introspection helpers such as
placeholder_tensors,feed_tensors,target_tensors,output_types, tensorshape, tensordata_type, and tensor-datagraph_device - Graph construction and execution helpers for:
- placeholders and constants
- matrix multiplication
- unary arithmetic (
identity, exponent/log variants, square/sqrt/reciprocal, abs/neg/sign, rounding, trig/hyperbolic,isNaN,isInfinite) - binary arithmetic (
+,-,*,/,divisionNoNaN,power, min/max, comparisons, logicaland/or,atan2,floorModulo,select) - activations (
reLU,leakyReLU,sigmoid,softMax) and gradient helpers forreLU,sigmoid, andsoftMax - shape ops (
reshape,transpose/permute,slice,broadcast,concat,split,stack,pad) - reductions (existing sum/max/min/mean plus axis/axes sum/max/min/product)
topK- 2D convolution, max pooling, and normalization helpers
- Shared constants for
MPSDataType,MPSGraphTensorNamedDataLayout,MPSGraphPaddingStyle, graph options, optimization levels, and deployment platform values
This crate still covers a subset of the full SDK. See COVERAGE.md for the audited header-by-header status and deferred areas.
§Smoke examples
cargo run --example 01_add_relu
cargo run --example 02_compile_matmul
cargo run --example 03_arithmetic_topk
cargo run --example 04_descriptor_compile
cargo run --example 05_concat_splitRe-exports§
pub use crate::data::TensorData;pub use crate::error::Error;pub use crate::error::Result;pub use crate::execution::deployment_platform;pub use crate::execution::graph_options;pub use crate::execution::optimization;pub use crate::execution::optimization_profile;pub use crate::execution::reduced_precision_fast_math;pub use crate::execution::CompilationDescriptor;pub use crate::execution::ExecutableExecutionDescriptor;pub use crate::execution::ExecutableSerializationDescriptor;pub use crate::execution::ExecutionDescriptor;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;pub use crate::ops::BinaryArithmeticOp;pub use crate::ops::ReductionAxesOp;pub use crate::ops::ReductionAxisOp;pub use crate::ops::UnaryArithmeticOp;pub use crate::types::graph_device_type;pub use crate::types::GraphDevice;pub use crate::types::Operation;pub use crate::types::ShapedType;