Skip to main content

Executable

Struct Executable 

Source
pub struct Executable { /* private fields */ }
Expand description

Safe owner for a compiled MPSGraphExecutable.

Implementations§

Source§

impl Executable

Source

pub fn options(&self) -> u64

Return the executable’s MPSGraphOptions bitmask.

Source

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

Replace the executable’s options bitmask.

Source

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

Return feed tensors if this executable was compiled from a graph.

Examples found in repository?
examples/04_descriptor_compile.rs (line 38)
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 target_tensors(&self) -> Vec<Tensor>

Return target tensors if this executable was compiled from a graph.

Examples found in repository?
examples/04_descriptor_compile.rs (line 39)
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 specialize( &self, device: Option<&MetalDevice>, input_types: &[&ShapedType], descriptor: Option<&CompilationDescriptor>, ) -> Result<()>

Specialize the executable for the provided input types.

Source

pub fn output_types( &self, device: Option<&MetalDevice>, input_types: &[&ShapedType], descriptor: Option<&CompilationDescriptor>, ) -> Result<Vec<ShapedType>>

Query specialized output types for the provided input types.

Examples found in repository?
examples/04_descriptor_compile.rs (line 35)
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 run_with_descriptor( &self, command_queue: &CommandQueue, inputs: &[&TensorData], results: Option<&[&TensorData]>, descriptor: Option<&ExecutableExecutionDescriptor>, ) -> Result<Vec<TensorData>>

Run the executable with an optional execution descriptor and optional preallocated results.

Source

pub fn run_async_with_descriptor( &self, command_queue: &CommandQueue, inputs: &[&TensorData], results: Option<&[&TensorData]>, descriptor: Option<&ExecutableExecutionDescriptor>, ) -> Result<Vec<TensorData>>

Asynchronously run the executable with an optional execution descriptor.

Source

pub fn serialize_package( &self, path: &str, descriptor: Option<&ExecutableSerializationDescriptor>, ) -> Result<()>

Serialize the executable to an .mpsgraphpackage path.

Source

pub fn from_package( path: &str, descriptor: Option<&CompilationDescriptor>, ) -> Result<Self>

Load an executable from an existing .mpsgraphpackage.

Source§

impl Executable

Source

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

Source

pub const fn output_count(&self) -> usize

Source

pub fn run( &self, command_queue: &CommandQueue, inputs: &[&TensorData], ) -> Result<Vec<TensorData>>

Examples found in repository?
examples/02_compile_matmul.rs (line 39)
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}

Trait Implementations§

Source§

impl Drop for Executable

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 Executable

Source§

impl Sync for Executable

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.