Skip to main content

Graph

Struct Graph 

Source
pub struct Graph { /* private fields */ }

Implementations§

Source§

impl Graph

Source

pub fn options(&self) -> u64

Return the graph’s MPSGraphOptions bitmask.

Source

pub fn set_options(&self, options: u64) -> Result<()>

Replace the graph’s options bitmask.

Source

pub fn placeholder_tensors(&self) -> Vec<Tensor>

Return the graph’s placeholder tensors in insertion order.

Source

pub fn compile_with_descriptor( &self, device: Option<&MetalDevice>, feeds: &[FeedDescription<'_>], targets: &[&Tensor], descriptor: Option<&CompilationDescriptor>, ) -> Option<Executable>

Compile the graph with an optional compilation descriptor.

Examples found in repository?
examples/04_descriptor_compile.rs (lines 26-31)
7fn main() {
8    let device = MetalDevice::system_default().expect("no Metal device available");
9    let graph = Graph::new().expect("graph");
10    let input = graph
11        .placeholder(Some(&[4]), data_type::FLOAT32, Some("input"))
12        .expect("placeholder");
13    let output = graph
14        .unary_arithmetic(UnaryArithmeticOp::Absolute, &input, Some("abs"))
15        .expect("absolute");
16
17    let descriptor = CompilationDescriptor::new().expect("compilation descriptor");
18    descriptor
19        .set_optimization_level(optimization::LEVEL1)
20        .expect("set optimization level");
21    descriptor
22        .set_wait_for_compilation_completion(true)
23        .expect("set wait");
24
25    let executable = graph
26        .compile_with_descriptor(
27            Some(&device),
28            &[FeedDescription::new(&input, &[4], data_type::FLOAT32)],
29            &[&output],
30            Some(&descriptor),
31        )
32        .expect("compile");
33    let input_type = ShapedType::new(Some(&[4]), data_type::FLOAT32).expect("shaped type");
34    let output_types = executable
35        .output_types(Some(&device), &[&input_type], Some(&descriptor))
36        .expect("output types");
37
38    println!("feed tensors: {}", executable.feed_tensors().len());
39    println!("target tensors: {}", executable.target_tensors().len());
40    println!("output type: {:?}", output_types[0].shape());
41}
Source§

impl Graph

Source

pub const fn as_ptr(&self) -> *mut c_void

Source§

impl Graph

Source

pub fn new() -> Option<Self>

Examples found in repository?
examples/05_concat_split.rs (line 6)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("graph");
7    let input = graph
8        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
9        .expect("placeholder");
10    let concat = graph
11        .concat_pair(&input, &input, 1, Some("concat"))
12        .expect("concat");
13    let split = graph.split_num(&concat, 2, 1, Some("split"));
14    let stacked = graph.stack(&[&split[0], &split[1]], 0, Some("stack")).expect("stack");
15
16    let input_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0], &[2, 2])
17        .expect("tensor data");
18    let results = graph
19        .run(&[Feed::new(&input, &input_data)], &[&stacked])
20        .expect("run");
21
22    println!("stacked tensor bytes: {}", results[0].byte_len().expect("byte len"));
23}
More examples
Hide additional examples
examples/03_arithmetic_topk.rs (line 8)
6fn main() {
7    let device = MetalDevice::system_default().expect("no Metal device available");
8    let graph = Graph::new().expect("graph");
9    let input = graph
10        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("input"))
11        .expect("placeholder");
12    let squared = graph
13        .unary_arithmetic(UnaryArithmeticOp::Square, &input, Some("square"))
14        .expect("square");
15    let row_sum = graph
16        .reduce_axes(ReductionAxesOp::Sum, &squared, &[1], Some("row_sum"))
17        .expect("reduce");
18    let topk = graph.top_k(&input, 2, Some("topk")).expect("topk");
19
20    let input_data = TensorData::from_f32_slice(&device, &[1.0, 3.0, 2.0, 4.0, 6.0, 5.0], &[2, 3])
21        .expect("tensor data");
22    let results = graph
23        .run(&[Feed::new(&input, &input_data)], &[&row_sum, &topk.0])
24        .expect("run");
25
26    println!("row sums: {:?}", results[0].read_f32().expect("row sums"));
27    println!("top-k values: {:?}", results[1].read_f32().expect("topk values"));
28}
examples/01_add_relu.rs (line 6)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("failed to create MPSGraph");
7
8    let input = graph
9        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
10        .expect("failed to create placeholder");
11    let bias = graph
12        .constant_scalar(1.0, data_type::FLOAT32)
13        .expect("failed to create scalar constant");
14    let added = graph
15        .addition(&input, &bias, Some("add"))
16        .expect("failed to create addition op");
17    let output = graph
18        .relu(&added, Some("relu"))
19        .expect("failed to create relu op");
20
21    let input_data = TensorData::from_f32_slice(&device, &[1.0, -2.0, 3.0, -4.0], &[2, 2])
22        .expect("failed to create tensor data");
23    let results = graph
24        .run(&[Feed::new(&input, &input_data)], &[&output])
25        .expect("failed to execute graph");
26    let values = results[0].read_f32().expect("failed to read tensor output");
27
28    assert_eq!(values, vec![2.0, 0.0, 4.0, 0.0]);
29    println!("add+relu smoke passed: {values:?}");
30}
examples/04_descriptor_compile.rs (line 9)
7fn main() {
8    let device = MetalDevice::system_default().expect("no Metal device available");
9    let graph = Graph::new().expect("graph");
10    let input = graph
11        .placeholder(Some(&[4]), data_type::FLOAT32, Some("input"))
12        .expect("placeholder");
13    let output = graph
14        .unary_arithmetic(UnaryArithmeticOp::Absolute, &input, Some("abs"))
15        .expect("absolute");
16
17    let descriptor = CompilationDescriptor::new().expect("compilation descriptor");
18    descriptor
19        .set_optimization_level(optimization::LEVEL1)
20        .expect("set optimization level");
21    descriptor
22        .set_wait_for_compilation_completion(true)
23        .expect("set wait");
24
25    let executable = graph
26        .compile_with_descriptor(
27            Some(&device),
28            &[FeedDescription::new(&input, &[4], data_type::FLOAT32)],
29            &[&output],
30            Some(&descriptor),
31        )
32        .expect("compile");
33    let input_type = ShapedType::new(Some(&[4]), data_type::FLOAT32).expect("shaped type");
34    let output_types = executable
35        .output_types(Some(&device), &[&input_type], Some(&descriptor))
36        .expect("output types");
37
38    println!("feed tensors: {}", executable.feed_tensors().len());
39    println!("target tensors: {}", executable.target_tensors().len());
40    println!("output type: {:?}", output_types[0].shape());
41}
examples/02_compile_matmul.rs (line 9)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let queue = device
7        .new_command_queue()
8        .expect("failed to create command queue");
9    let graph = Graph::new().expect("failed to create MPSGraph");
10
11    let left = graph
12        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("left"))
13        .expect("failed to create left placeholder");
14    let right = graph
15        .placeholder(Some(&[3, 2]), data_type::FLOAT32, Some("right"))
16        .expect("failed to create right placeholder");
17    let output = graph
18        .matrix_multiplication(&left, &right, Some("matmul"))
19        .expect("failed to create matrix multiplication op");
20
21    let executable = graph
22        .compile(
23            &device,
24            &[
25                FeedDescription::new(&left, &[2, 3], data_type::FLOAT32),
26                FeedDescription::new(&right, &[3, 2], data_type::FLOAT32),
27            ],
28            &[&output],
29        )
30        .expect("failed to compile executable");
31
32    let left_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3])
33        .expect("failed to create left tensor data");
34    let right_data =
35        TensorData::from_f32_slice(&device, &[7.0, 8.0, 9.0, 10.0, 11.0, 12.0], &[3, 2])
36            .expect("failed to create right tensor data");
37
38    let results = executable
39        .run(&queue, &[&left_data, &right_data])
40        .expect("failed to run executable");
41    let values = results[0].read_f32().expect("failed to read tensor output");
42    let expected = [58.0_f32, 64.0, 139.0, 154.0];
43    for (actual, expected_value) in values.iter().zip(expected) {
44        assert!(
45            (actual - expected_value).abs() < 1.0e-4,
46            "unexpected matrix multiply result: {values:?}"
47        );
48    }
49
50    println!("compile+matmul smoke passed: {values:?}");
51}
Source

pub fn placeholder( &self, shape: Option<&[usize]>, data_type: u32, name: Option<&str>, ) -> Option<Tensor>

Examples found in repository?
examples/05_concat_split.rs (line 8)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("graph");
7    let input = graph
8        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
9        .expect("placeholder");
10    let concat = graph
11        .concat_pair(&input, &input, 1, Some("concat"))
12        .expect("concat");
13    let split = graph.split_num(&concat, 2, 1, Some("split"));
14    let stacked = graph.stack(&[&split[0], &split[1]], 0, Some("stack")).expect("stack");
15
16    let input_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0], &[2, 2])
17        .expect("tensor data");
18    let results = graph
19        .run(&[Feed::new(&input, &input_data)], &[&stacked])
20        .expect("run");
21
22    println!("stacked tensor bytes: {}", results[0].byte_len().expect("byte len"));
23}
More examples
Hide additional examples
examples/03_arithmetic_topk.rs (line 10)
6fn main() {
7    let device = MetalDevice::system_default().expect("no Metal device available");
8    let graph = Graph::new().expect("graph");
9    let input = graph
10        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("input"))
11        .expect("placeholder");
12    let squared = graph
13        .unary_arithmetic(UnaryArithmeticOp::Square, &input, Some("square"))
14        .expect("square");
15    let row_sum = graph
16        .reduce_axes(ReductionAxesOp::Sum, &squared, &[1], Some("row_sum"))
17        .expect("reduce");
18    let topk = graph.top_k(&input, 2, Some("topk")).expect("topk");
19
20    let input_data = TensorData::from_f32_slice(&device, &[1.0, 3.0, 2.0, 4.0, 6.0, 5.0], &[2, 3])
21        .expect("tensor data");
22    let results = graph
23        .run(&[Feed::new(&input, &input_data)], &[&row_sum, &topk.0])
24        .expect("run");
25
26    println!("row sums: {:?}", results[0].read_f32().expect("row sums"));
27    println!("top-k values: {:?}", results[1].read_f32().expect("topk values"));
28}
examples/01_add_relu.rs (line 9)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("failed to create MPSGraph");
7
8    let input = graph
9        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
10        .expect("failed to create placeholder");
11    let bias = graph
12        .constant_scalar(1.0, data_type::FLOAT32)
13        .expect("failed to create scalar constant");
14    let added = graph
15        .addition(&input, &bias, Some("add"))
16        .expect("failed to create addition op");
17    let output = graph
18        .relu(&added, Some("relu"))
19        .expect("failed to create relu op");
20
21    let input_data = TensorData::from_f32_slice(&device, &[1.0, -2.0, 3.0, -4.0], &[2, 2])
22        .expect("failed to create tensor data");
23    let results = graph
24        .run(&[Feed::new(&input, &input_data)], &[&output])
25        .expect("failed to execute graph");
26    let values = results[0].read_f32().expect("failed to read tensor output");
27
28    assert_eq!(values, vec![2.0, 0.0, 4.0, 0.0]);
29    println!("add+relu smoke passed: {values:?}");
30}
examples/04_descriptor_compile.rs (line 11)
7fn main() {
8    let device = MetalDevice::system_default().expect("no Metal device available");
9    let graph = Graph::new().expect("graph");
10    let input = graph
11        .placeholder(Some(&[4]), data_type::FLOAT32, Some("input"))
12        .expect("placeholder");
13    let output = graph
14        .unary_arithmetic(UnaryArithmeticOp::Absolute, &input, Some("abs"))
15        .expect("absolute");
16
17    let descriptor = CompilationDescriptor::new().expect("compilation descriptor");
18    descriptor
19        .set_optimization_level(optimization::LEVEL1)
20        .expect("set optimization level");
21    descriptor
22        .set_wait_for_compilation_completion(true)
23        .expect("set wait");
24
25    let executable = graph
26        .compile_with_descriptor(
27            Some(&device),
28            &[FeedDescription::new(&input, &[4], data_type::FLOAT32)],
29            &[&output],
30            Some(&descriptor),
31        )
32        .expect("compile");
33    let input_type = ShapedType::new(Some(&[4]), data_type::FLOAT32).expect("shaped type");
34    let output_types = executable
35        .output_types(Some(&device), &[&input_type], Some(&descriptor))
36        .expect("output types");
37
38    println!("feed tensors: {}", executable.feed_tensors().len());
39    println!("target tensors: {}", executable.target_tensors().len());
40    println!("output type: {:?}", output_types[0].shape());
41}
examples/02_compile_matmul.rs (line 12)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let queue = device
7        .new_command_queue()
8        .expect("failed to create command queue");
9    let graph = Graph::new().expect("failed to create MPSGraph");
10
11    let left = graph
12        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("left"))
13        .expect("failed to create left placeholder");
14    let right = graph
15        .placeholder(Some(&[3, 2]), data_type::FLOAT32, Some("right"))
16        .expect("failed to create right placeholder");
17    let output = graph
18        .matrix_multiplication(&left, &right, Some("matmul"))
19        .expect("failed to create matrix multiplication op");
20
21    let executable = graph
22        .compile(
23            &device,
24            &[
25                FeedDescription::new(&left, &[2, 3], data_type::FLOAT32),
26                FeedDescription::new(&right, &[3, 2], data_type::FLOAT32),
27            ],
28            &[&output],
29        )
30        .expect("failed to compile executable");
31
32    let left_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3])
33        .expect("failed to create left tensor data");
34    let right_data =
35        TensorData::from_f32_slice(&device, &[7.0, 8.0, 9.0, 10.0, 11.0, 12.0], &[3, 2])
36            .expect("failed to create right tensor data");
37
38    let results = executable
39        .run(&queue, &[&left_data, &right_data])
40        .expect("failed to run executable");
41    let values = results[0].read_f32().expect("failed to read tensor output");
42    let expected = [58.0_f32, 64.0, 139.0, 154.0];
43    for (actual, expected_value) in values.iter().zip(expected) {
44        assert!(
45            (actual - expected_value).abs() < 1.0e-4,
46            "unexpected matrix multiply result: {values:?}"
47        );
48    }
49
50    println!("compile+matmul smoke passed: {values:?}");
51}
Source

pub fn constant_bytes( &self, data: &[u8], shape: &[usize], data_type: u32, ) -> Option<Tensor>

Source

pub fn constant_f32_slice( &self, values: &[f32], shape: &[usize], ) -> Option<Tensor>

Source

pub fn constant_scalar(&self, scalar: f64, data_type: u32) -> Option<Tensor>

Examples found in repository?
examples/01_add_relu.rs (line 12)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("failed to create MPSGraph");
7
8    let input = graph
9        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
10        .expect("failed to create placeholder");
11    let bias = graph
12        .constant_scalar(1.0, data_type::FLOAT32)
13        .expect("failed to create scalar constant");
14    let added = graph
15        .addition(&input, &bias, Some("add"))
16        .expect("failed to create addition op");
17    let output = graph
18        .relu(&added, Some("relu"))
19        .expect("failed to create relu op");
20
21    let input_data = TensorData::from_f32_slice(&device, &[1.0, -2.0, 3.0, -4.0], &[2, 2])
22        .expect("failed to create tensor data");
23    let results = graph
24        .run(&[Feed::new(&input, &input_data)], &[&output])
25        .expect("failed to execute graph");
26    let values = results[0].read_f32().expect("failed to read tensor output");
27
28    assert_eq!(values, vec![2.0, 0.0, 4.0, 0.0]);
29    println!("add+relu smoke passed: {values:?}");
30}
Source

pub fn constant_scalar_shaped( &self, scalar: f64, shape: &[usize], data_type: u32, ) -> Option<Tensor>

Source

pub fn addition( &self, primary: &Tensor, secondary: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Examples found in repository?
examples/01_add_relu.rs (line 15)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("failed to create MPSGraph");
7
8    let input = graph
9        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
10        .expect("failed to create placeholder");
11    let bias = graph
12        .constant_scalar(1.0, data_type::FLOAT32)
13        .expect("failed to create scalar constant");
14    let added = graph
15        .addition(&input, &bias, Some("add"))
16        .expect("failed to create addition op");
17    let output = graph
18        .relu(&added, Some("relu"))
19        .expect("failed to create relu op");
20
21    let input_data = TensorData::from_f32_slice(&device, &[1.0, -2.0, 3.0, -4.0], &[2, 2])
22        .expect("failed to create tensor data");
23    let results = graph
24        .run(&[Feed::new(&input, &input_data)], &[&output])
25        .expect("failed to execute graph");
26    let values = results[0].read_f32().expect("failed to read tensor output");
27
28    assert_eq!(values, vec![2.0, 0.0, 4.0, 0.0]);
29    println!("add+relu smoke passed: {values:?}");
30}
Source

pub fn subtraction( &self, primary: &Tensor, secondary: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn multiplication( &self, primary: &Tensor, secondary: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn division( &self, primary: &Tensor, secondary: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn matrix_multiplication( &self, primary: &Tensor, secondary: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Examples found in repository?
examples/02_compile_matmul.rs (line 18)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let queue = device
7        .new_command_queue()
8        .expect("failed to create command queue");
9    let graph = Graph::new().expect("failed to create MPSGraph");
10
11    let left = graph
12        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("left"))
13        .expect("failed to create left placeholder");
14    let right = graph
15        .placeholder(Some(&[3, 2]), data_type::FLOAT32, Some("right"))
16        .expect("failed to create right placeholder");
17    let output = graph
18        .matrix_multiplication(&left, &right, Some("matmul"))
19        .expect("failed to create matrix multiplication op");
20
21    let executable = graph
22        .compile(
23            &device,
24            &[
25                FeedDescription::new(&left, &[2, 3], data_type::FLOAT32),
26                FeedDescription::new(&right, &[3, 2], data_type::FLOAT32),
27            ],
28            &[&output],
29        )
30        .expect("failed to compile executable");
31
32    let left_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3])
33        .expect("failed to create left tensor data");
34    let right_data =
35        TensorData::from_f32_slice(&device, &[7.0, 8.0, 9.0, 10.0, 11.0, 12.0], &[3, 2])
36            .expect("failed to create right tensor data");
37
38    let results = executable
39        .run(&queue, &[&left_data, &right_data])
40        .expect("failed to run executable");
41    let values = results[0].read_f32().expect("failed to read tensor output");
42    let expected = [58.0_f32, 64.0, 139.0, 154.0];
43    for (actual, expected_value) in values.iter().zip(expected) {
44        assert!(
45            (actual - expected_value).abs() < 1.0e-4,
46            "unexpected matrix multiply result: {values:?}"
47        );
48    }
49
50    println!("compile+matmul smoke passed: {values:?}");
51}
Source

pub fn relu(&self, tensor: &Tensor, name: Option<&str>) -> Option<Tensor>

Examples found in repository?
examples/01_add_relu.rs (line 18)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("failed to create MPSGraph");
7
8    let input = graph
9        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
10        .expect("failed to create placeholder");
11    let bias = graph
12        .constant_scalar(1.0, data_type::FLOAT32)
13        .expect("failed to create scalar constant");
14    let added = graph
15        .addition(&input, &bias, Some("add"))
16        .expect("failed to create addition op");
17    let output = graph
18        .relu(&added, Some("relu"))
19        .expect("failed to create relu op");
20
21    let input_data = TensorData::from_f32_slice(&device, &[1.0, -2.0, 3.0, -4.0], &[2, 2])
22        .expect("failed to create tensor data");
23    let results = graph
24        .run(&[Feed::new(&input, &input_data)], &[&output])
25        .expect("failed to execute graph");
26    let values = results[0].read_f32().expect("failed to read tensor output");
27
28    assert_eq!(values, vec![2.0, 0.0, 4.0, 0.0]);
29    println!("add+relu smoke passed: {values:?}");
30}
Source

pub fn sigmoid(&self, tensor: &Tensor, name: Option<&str>) -> Option<Tensor>

Source

pub fn reduction_sum( &self, tensor: &Tensor, axes: &[usize], name: Option<&str>, ) -> Option<Tensor>

Source

pub fn reduction_maximum( &self, tensor: &Tensor, axes: &[usize], name: Option<&str>, ) -> Option<Tensor>

Source

pub fn reduction_minimum( &self, tensor: &Tensor, axes: &[usize], name: Option<&str>, ) -> Option<Tensor>

Source

pub fn mean( &self, tensor: &Tensor, axes: &[usize], name: Option<&str>, ) -> Option<Tensor>

Source

pub fn softmax( &self, tensor: &Tensor, axis: isize, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn reshape( &self, tensor: &Tensor, shape: &[usize], name: Option<&str>, ) -> Option<Tensor>

Source

pub fn transpose( &self, tensor: &Tensor, permutation: &[usize], name: Option<&str>, ) -> Option<Tensor>

Source

pub fn slice( &self, tensor: &Tensor, dimension: usize, start: isize, length: isize, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn broadcast( &self, tensor: &Tensor, shape: &[usize], name: Option<&str>, ) -> Option<Tensor>

Source

pub fn convolution2d( &self, source: &Tensor, weights: &Tensor, descriptor: &Convolution2DDescriptor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn max_pooling2d( &self, source: &Tensor, descriptor: &Pooling2DDescriptor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn normalize( &self, tensor: &Tensor, mean: &Tensor, variance: &Tensor, gamma: Option<&Tensor>, beta: Option<&Tensor>, epsilon: f32, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn run( &self, feeds: &[Feed<'_>], targets: &[&Tensor], ) -> Result<Vec<TensorData>>

Examples found in repository?
examples/05_concat_split.rs (line 19)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("graph");
7    let input = graph
8        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
9        .expect("placeholder");
10    let concat = graph
11        .concat_pair(&input, &input, 1, Some("concat"))
12        .expect("concat");
13    let split = graph.split_num(&concat, 2, 1, Some("split"));
14    let stacked = graph.stack(&[&split[0], &split[1]], 0, Some("stack")).expect("stack");
15
16    let input_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0], &[2, 2])
17        .expect("tensor data");
18    let results = graph
19        .run(&[Feed::new(&input, &input_data)], &[&stacked])
20        .expect("run");
21
22    println!("stacked tensor bytes: {}", results[0].byte_len().expect("byte len"));
23}
More examples
Hide additional examples
examples/03_arithmetic_topk.rs (line 23)
6fn main() {
7    let device = MetalDevice::system_default().expect("no Metal device available");
8    let graph = Graph::new().expect("graph");
9    let input = graph
10        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("input"))
11        .expect("placeholder");
12    let squared = graph
13        .unary_arithmetic(UnaryArithmeticOp::Square, &input, Some("square"))
14        .expect("square");
15    let row_sum = graph
16        .reduce_axes(ReductionAxesOp::Sum, &squared, &[1], Some("row_sum"))
17        .expect("reduce");
18    let topk = graph.top_k(&input, 2, Some("topk")).expect("topk");
19
20    let input_data = TensorData::from_f32_slice(&device, &[1.0, 3.0, 2.0, 4.0, 6.0, 5.0], &[2, 3])
21        .expect("tensor data");
22    let results = graph
23        .run(&[Feed::new(&input, &input_data)], &[&row_sum, &topk.0])
24        .expect("run");
25
26    println!("row sums: {:?}", results[0].read_f32().expect("row sums"));
27    println!("top-k values: {:?}", results[1].read_f32().expect("topk values"));
28}
examples/01_add_relu.rs (line 24)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("failed to create MPSGraph");
7
8    let input = graph
9        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
10        .expect("failed to create placeholder");
11    let bias = graph
12        .constant_scalar(1.0, data_type::FLOAT32)
13        .expect("failed to create scalar constant");
14    let added = graph
15        .addition(&input, &bias, Some("add"))
16        .expect("failed to create addition op");
17    let output = graph
18        .relu(&added, Some("relu"))
19        .expect("failed to create relu op");
20
21    let input_data = TensorData::from_f32_slice(&device, &[1.0, -2.0, 3.0, -4.0], &[2, 2])
22        .expect("failed to create tensor data");
23    let results = graph
24        .run(&[Feed::new(&input, &input_data)], &[&output])
25        .expect("failed to execute graph");
26    let values = results[0].read_f32().expect("failed to read tensor output");
27
28    assert_eq!(values, vec![2.0, 0.0, 4.0, 0.0]);
29    println!("add+relu smoke passed: {values:?}");
30}
Source

pub fn run_with_command_queue( &self, command_queue: &CommandQueue, feeds: &[Feed<'_>], targets: &[&Tensor], ) -> Result<Vec<TensorData>>

Source

pub fn compile( &self, device: &MetalDevice, feeds: &[FeedDescription<'_>], targets: &[&Tensor], ) -> Option<Executable>

Examples found in repository?
examples/02_compile_matmul.rs (lines 22-29)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let queue = device
7        .new_command_queue()
8        .expect("failed to create command queue");
9    let graph = Graph::new().expect("failed to create MPSGraph");
10
11    let left = graph
12        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("left"))
13        .expect("failed to create left placeholder");
14    let right = graph
15        .placeholder(Some(&[3, 2]), data_type::FLOAT32, Some("right"))
16        .expect("failed to create right placeholder");
17    let output = graph
18        .matrix_multiplication(&left, &right, Some("matmul"))
19        .expect("failed to create matrix multiplication op");
20
21    let executable = graph
22        .compile(
23            &device,
24            &[
25                FeedDescription::new(&left, &[2, 3], data_type::FLOAT32),
26                FeedDescription::new(&right, &[3, 2], data_type::FLOAT32),
27            ],
28            &[&output],
29        )
30        .expect("failed to compile executable");
31
32    let left_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3])
33        .expect("failed to create left tensor data");
34    let right_data =
35        TensorData::from_f32_slice(&device, &[7.0, 8.0, 9.0, 10.0, 11.0, 12.0], &[3, 2])
36            .expect("failed to create right tensor data");
37
38    let results = executable
39        .run(&queue, &[&left_data, &right_data])
40        .expect("failed to run executable");
41    let values = results[0].read_f32().expect("failed to read tensor output");
42    let expected = [58.0_f32, 64.0, 139.0, 154.0];
43    for (actual, expected_value) in values.iter().zip(expected) {
44        assert!(
45            (actual - expected_value).abs() < 1.0e-4,
46            "unexpected matrix multiply result: {values:?}"
47        );
48    }
49
50    println!("compile+matmul smoke passed: {values:?}");
51}
Source§

impl Graph

Source

pub fn unary_arithmetic( &self, op: UnaryArithmeticOp, tensor: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Examples found in repository?
examples/03_arithmetic_topk.rs (line 13)
6fn main() {
7    let device = MetalDevice::system_default().expect("no Metal device available");
8    let graph = Graph::new().expect("graph");
9    let input = graph
10        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("input"))
11        .expect("placeholder");
12    let squared = graph
13        .unary_arithmetic(UnaryArithmeticOp::Square, &input, Some("square"))
14        .expect("square");
15    let row_sum = graph
16        .reduce_axes(ReductionAxesOp::Sum, &squared, &[1], Some("row_sum"))
17        .expect("reduce");
18    let topk = graph.top_k(&input, 2, Some("topk")).expect("topk");
19
20    let input_data = TensorData::from_f32_slice(&device, &[1.0, 3.0, 2.0, 4.0, 6.0, 5.0], &[2, 3])
21        .expect("tensor data");
22    let results = graph
23        .run(&[Feed::new(&input, &input_data)], &[&row_sum, &topk.0])
24        .expect("run");
25
26    println!("row sums: {:?}", results[0].read_f32().expect("row sums"));
27    println!("top-k values: {:?}", results[1].read_f32().expect("topk values"));
28}
More examples
Hide additional examples
examples/04_descriptor_compile.rs (line 14)
7fn main() {
8    let device = MetalDevice::system_default().expect("no Metal device available");
9    let graph = Graph::new().expect("graph");
10    let input = graph
11        .placeholder(Some(&[4]), data_type::FLOAT32, Some("input"))
12        .expect("placeholder");
13    let output = graph
14        .unary_arithmetic(UnaryArithmeticOp::Absolute, &input, Some("abs"))
15        .expect("absolute");
16
17    let descriptor = CompilationDescriptor::new().expect("compilation descriptor");
18    descriptor
19        .set_optimization_level(optimization::LEVEL1)
20        .expect("set optimization level");
21    descriptor
22        .set_wait_for_compilation_completion(true)
23        .expect("set wait");
24
25    let executable = graph
26        .compile_with_descriptor(
27            Some(&device),
28            &[FeedDescription::new(&input, &[4], data_type::FLOAT32)],
29            &[&output],
30            Some(&descriptor),
31        )
32        .expect("compile");
33    let input_type = ShapedType::new(Some(&[4]), data_type::FLOAT32).expect("shaped type");
34    let output_types = executable
35        .output_types(Some(&device), &[&input_type], Some(&descriptor))
36        .expect("output types");
37
38    println!("feed tensors: {}", executable.feed_tensors().len());
39    println!("target tensors: {}", executable.target_tensors().len());
40    println!("output type: {:?}", output_types[0].shape());
41}
Source

pub fn binary_arithmetic( &self, op: BinaryArithmeticOp, primary: &Tensor, secondary: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn select( &self, predicate: &Tensor, true_tensor: &Tensor, false_tensor: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn relu_gradient( &self, gradient: &Tensor, source: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn sigmoid_gradient( &self, gradient: &Tensor, source: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn softmax_gradient( &self, gradient: &Tensor, source: &Tensor, axis: isize, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn leaky_relu( &self, tensor: &Tensor, alpha: f64, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn leaky_relu_tensor( &self, tensor: &Tensor, alpha_tensor: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn leaky_relu_gradient( &self, gradient: &Tensor, source: &Tensor, alpha_tensor: &Tensor, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn reduce_axis( &self, op: ReductionAxisOp, tensor: &Tensor, axis: isize, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn reduce_axes( &self, op: ReductionAxesOp, tensor: &Tensor, axes: &[usize], name: Option<&str>, ) -> Option<Tensor>

Examples found in repository?
examples/03_arithmetic_topk.rs (line 16)
6fn main() {
7    let device = MetalDevice::system_default().expect("no Metal device available");
8    let graph = Graph::new().expect("graph");
9    let input = graph
10        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("input"))
11        .expect("placeholder");
12    let squared = graph
13        .unary_arithmetic(UnaryArithmeticOp::Square, &input, Some("square"))
14        .expect("square");
15    let row_sum = graph
16        .reduce_axes(ReductionAxesOp::Sum, &squared, &[1], Some("row_sum"))
17        .expect("reduce");
18    let topk = graph.top_k(&input, 2, Some("topk")).expect("topk");
19
20    let input_data = TensorData::from_f32_slice(&device, &[1.0, 3.0, 2.0, 4.0, 6.0, 5.0], &[2, 3])
21        .expect("tensor data");
22    let results = graph
23        .run(&[Feed::new(&input, &input_data)], &[&row_sum, &topk.0])
24        .expect("run");
25
26    println!("row sums: {:?}", results[0].read_f32().expect("row sums"));
27    println!("top-k values: {:?}", results[1].read_f32().expect("topk values"));
28}
Source

pub fn concat_pair( &self, first: &Tensor, second: &Tensor, dimension: isize, name: Option<&str>, ) -> Option<Tensor>

Examples found in repository?
examples/05_concat_split.rs (line 11)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("graph");
7    let input = graph
8        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
9        .expect("placeholder");
10    let concat = graph
11        .concat_pair(&input, &input, 1, Some("concat"))
12        .expect("concat");
13    let split = graph.split_num(&concat, 2, 1, Some("split"));
14    let stacked = graph.stack(&[&split[0], &split[1]], 0, Some("stack")).expect("stack");
15
16    let input_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0], &[2, 2])
17        .expect("tensor data");
18    let results = graph
19        .run(&[Feed::new(&input, &input_data)], &[&stacked])
20        .expect("run");
21
22    println!("stacked tensor bytes: {}", results[0].byte_len().expect("byte len"));
23}
Source

pub fn concat_tensors( &self, tensors: &[&Tensor], dimension: isize, interleave: bool, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn split_sizes( &self, tensor: &Tensor, split_sizes: &[usize], axis: isize, name: Option<&str>, ) -> Vec<Tensor>

Source

pub fn split_sizes_tensor( &self, tensor: &Tensor, split_sizes_tensor: &Tensor, axis: isize, name: Option<&str>, ) -> Vec<Tensor>

Source

pub fn split_num( &self, tensor: &Tensor, num_splits: usize, axis: isize, name: Option<&str>, ) -> Vec<Tensor>

Examples found in repository?
examples/05_concat_split.rs (line 13)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("graph");
7    let input = graph
8        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
9        .expect("placeholder");
10    let concat = graph
11        .concat_pair(&input, &input, 1, Some("concat"))
12        .expect("concat");
13    let split = graph.split_num(&concat, 2, 1, Some("split"));
14    let stacked = graph.stack(&[&split[0], &split[1]], 0, Some("stack")).expect("stack");
15
16    let input_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0], &[2, 2])
17        .expect("tensor data");
18    let results = graph
19        .run(&[Feed::new(&input, &input_data)], &[&stacked])
20        .expect("run");
21
22    println!("stacked tensor bytes: {}", results[0].byte_len().expect("byte len"));
23}
Source

pub fn stack( &self, tensors: &[&Tensor], axis: isize, name: Option<&str>, ) -> Option<Tensor>

Examples found in repository?
examples/05_concat_split.rs (line 14)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let graph = Graph::new().expect("graph");
7    let input = graph
8        .placeholder(Some(&[2, 2]), data_type::FLOAT32, Some("input"))
9        .expect("placeholder");
10    let concat = graph
11        .concat_pair(&input, &input, 1, Some("concat"))
12        .expect("concat");
13    let split = graph.split_num(&concat, 2, 1, Some("split"));
14    let stacked = graph.stack(&[&split[0], &split[1]], 0, Some("stack")).expect("stack");
15
16    let input_data = TensorData::from_f32_slice(&device, &[1.0, 2.0, 3.0, 4.0], &[2, 2])
17        .expect("tensor data");
18    let results = graph
19        .run(&[Feed::new(&input, &input_data)], &[&stacked])
20        .expect("run");
21
22    println!("stacked tensor bytes: {}", results[0].byte_len().expect("byte len"));
23}
Source

pub fn pad( &self, tensor: &Tensor, padding_mode: isize, left_padding: &[isize], right_padding: &[isize], constant_value: f64, name: Option<&str>, ) -> Option<Tensor>

Source

pub fn top_k( &self, source: &Tensor, k: usize, name: Option<&str>, ) -> Option<(Tensor, Tensor)>

Examples found in repository?
examples/03_arithmetic_topk.rs (line 18)
6fn main() {
7    let device = MetalDevice::system_default().expect("no Metal device available");
8    let graph = Graph::new().expect("graph");
9    let input = graph
10        .placeholder(Some(&[2, 3]), data_type::FLOAT32, Some("input"))
11        .expect("placeholder");
12    let squared = graph
13        .unary_arithmetic(UnaryArithmeticOp::Square, &input, Some("square"))
14        .expect("square");
15    let row_sum = graph
16        .reduce_axes(ReductionAxesOp::Sum, &squared, &[1], Some("row_sum"))
17        .expect("reduce");
18    let topk = graph.top_k(&input, 2, Some("topk")).expect("topk");
19
20    let input_data = TensorData::from_f32_slice(&device, &[1.0, 3.0, 2.0, 4.0, 6.0, 5.0], &[2, 3])
21        .expect("tensor data");
22    let results = graph
23        .run(&[Feed::new(&input, &input_data)], &[&row_sum, &topk.0])
24        .expect("run");
25
26    println!("row sums: {:?}", results[0].read_f32().expect("row sums"));
27    println!("top-k values: {:?}", results[1].read_f32().expect("topk values"));
28}
Source

pub fn top_k_tensor( &self, source: &Tensor, k_tensor: &Tensor, name: Option<&str>, ) -> Option<(Tensor, Tensor)>

Trait Implementations§

Source§

impl Drop for Graph

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for Graph

Source§

impl Sync for Graph

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.