apple-mpsgraph 0.2.1

Safe Rust bindings for Apple's MetalPerformanceShadersGraph framework on macOS, backed by a Swift bridge
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
use crate::data::TensorData;
use crate::error::{Error, Result};
use crate::ffi;
use crate::graph::{Executable, FeedDescription, Graph, Tensor};
use crate::types::{collect_owned_tensors, collect_shaped_type_array_box, collect_tensor_data_array_box, ShapedType};
use apple_metal::{CommandQueue, MetalDevice};
use core::ffi::c_void;
use core::ptr;
use std::ffi::CString;

fn release_handle(ptr: &mut *mut c_void) {
    if !ptr.is_null() {
        // SAFETY: `ptr` is a +1 retained Swift/ObjC object pointer owned by this wrapper.
        unsafe { ffi::mpsgraph_object_release(*ptr) };
        *ptr = ptr::null_mut();
    }
}

fn copy_string(
    len: unsafe extern "C" fn(*mut c_void) -> usize,
    copy: unsafe extern "C" fn(*mut c_void, *mut u8, usize) -> bool,
    handle: *mut c_void,
) -> Result<String> {
    // SAFETY: the function pointers belong to Swift shims that treat `handle` as immutable for the duration of the call.
    let len = unsafe { len(handle) };
    let mut bytes = vec![0_u8; len];
    // SAFETY: the buffer is valid for exactly `len` bytes.
    let ok = unsafe { copy(handle, bytes.as_mut_ptr(), len) };
    if ok {
        String::from_utf8(bytes).map_err(|_| Error::OperationFailed("bridge returned invalid UTF-8"))
    } else {
        Err(Error::OperationFailed("failed to copy string from bridge"))
    }
}

/// `MPSGraphOptions` constants.
pub mod graph_options {
    pub const NONE: u64 = 0;
    pub const SYNCHRONIZE_RESULTS: u64 = 1;
    pub const VERBOSE: u64 = 2;
    pub const DEFAULT: u64 = SYNCHRONIZE_RESULTS;
}

/// `MPSGraphOptimization` constants.
pub mod optimization {
    pub const LEVEL0: u64 = 0;
    pub const LEVEL1: u64 = 1;
}

/// `MPSGraphOptimizationProfile` constants.
pub mod optimization_profile {
    pub const PERFORMANCE: u64 = 0;
    pub const POWER_EFFICIENCY: u64 = 1;
}

/// `MPSGraphReducedPrecisionFastMath` bit flags.
pub mod reduced_precision_fast_math {
    pub const NONE: usize = 0;
    pub const ALLOW_FP16_CONV2D_WINOGRAD_TRANSFORM_INTERMEDIATE: usize = 1 << 1;
    pub const ALLOW_FP16_INTERMEDIATES: usize = ALLOW_FP16_CONV2D_WINOGRAD_TRANSFORM_INTERMEDIATE;
    pub const DEFAULT: usize = NONE;
}

/// `MPSGraphDeploymentPlatform` constants.
pub mod deployment_platform {
    pub const MACOS: u64 = 0;
    pub const IOS: u64 = 1;
    pub const TVOS: u64 = 2;
    pub const VISIONOS: u64 = 3;
}

/// Safe owner for `MPSGraphCompilationDescriptor`.
pub struct CompilationDescriptor {
    ptr: *mut c_void,
}

unsafe impl Send for CompilationDescriptor {}
unsafe impl Sync for CompilationDescriptor {}

impl Drop for CompilationDescriptor {
    fn drop(&mut self) {
        release_handle(&mut self.ptr);
    }
}

impl CompilationDescriptor {
    #[must_use]
    pub fn new() -> Option<Self> {
        // SAFETY: pure constructor.
        let ptr = unsafe { ffi::mpsgraph_compilation_descriptor_new() };
        if ptr.is_null() {
            None
        } else {
            Some(Self { ptr })
        }
    }

    #[must_use]
    pub(crate) const fn as_ptr(&self) -> *mut c_void {
        self.ptr
    }

    pub fn disable_type_inference(&self) -> Result<()> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ok = unsafe { ffi::mpsgraph_compilation_descriptor_disable_type_inference(self.ptr) };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to disable type inference"))
        }
    }

    #[must_use]
    pub fn optimization_level(&self) -> u64 {
        // SAFETY: `self.ptr` is a live descriptor handle.
        unsafe { ffi::mpsgraph_compilation_descriptor_optimization_level(self.ptr) }
    }

    pub fn set_optimization_level(&self, value: u64) -> Result<()> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ok = unsafe { ffi::mpsgraph_compilation_descriptor_set_optimization_level(self.ptr, value) };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set optimization level"))
        }
    }

    #[must_use]
    pub fn wait_for_compilation_completion(&self) -> bool {
        // SAFETY: `self.ptr` is a live descriptor handle.
        unsafe { ffi::mpsgraph_compilation_descriptor_wait_for_completion(self.ptr) }
    }

    pub fn set_wait_for_compilation_completion(&self, value: bool) -> Result<()> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ok = unsafe { ffi::mpsgraph_compilation_descriptor_set_wait_for_completion(self.ptr, value) };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set waitForCompilationCompletion"))
        }
    }

    #[must_use]
    pub fn optimization_profile(&self) -> u64 {
        // SAFETY: `self.ptr` is a live descriptor handle.
        unsafe { ffi::mpsgraph_compilation_descriptor_optimization_profile(self.ptr) }
    }

    pub fn set_optimization_profile(&self, value: u64) -> Result<()> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ok = unsafe { ffi::mpsgraph_compilation_descriptor_set_optimization_profile(self.ptr, value) };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set optimization profile"))
        }
    }

    #[must_use]
    pub fn reduced_precision_fast_math(&self) -> usize {
        // SAFETY: `self.ptr` is a live descriptor handle.
        unsafe { ffi::mpsgraph_compilation_descriptor_reduced_precision_fast_math(self.ptr) }
    }

    pub fn set_reduced_precision_fast_math(&self, value: usize) -> Result<()> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ok = unsafe {
            ffi::mpsgraph_compilation_descriptor_set_reduced_precision_fast_math(self.ptr, value)
        };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set reducedPrecisionFastMath"))
        }
    }

    pub fn set_callable(&self, symbol_name: &str, executable: Option<&Executable>) -> Result<()> {
        let symbol_name =
            CString::new(symbol_name).map_err(|_| Error::OperationFailed("call symbol name contained NUL"))?;
        let executable_ptr = executable.map_or(ptr::null_mut(), Executable::as_ptr);
        // SAFETY: all handles remain valid for the duration of the call.
        let ok = unsafe {
            ffi::mpsgraph_compilation_descriptor_set_callable(
                self.ptr,
                symbol_name.as_ptr(),
                executable_ptr,
            )
        };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set compilation descriptor callable"))
        }
    }
}

/// Safe owner for `MPSGraphExecutionDescriptor`.
pub struct ExecutionDescriptor {
    ptr: *mut c_void,
}

unsafe impl Send for ExecutionDescriptor {}
unsafe impl Sync for ExecutionDescriptor {}

impl Drop for ExecutionDescriptor {
    fn drop(&mut self) {
        release_handle(&mut self.ptr);
    }
}

impl ExecutionDescriptor {
    #[must_use]
    pub fn new() -> Option<Self> {
        // SAFETY: pure constructor.
        let ptr = unsafe { ffi::mpsgraph_execution_descriptor_new() };
        if ptr.is_null() {
            None
        } else {
            Some(Self { ptr })
        }
    }

    #[must_use]
    pub const fn as_ptr(&self) -> *mut c_void {
        self.ptr
    }

    #[must_use]
    pub fn wait_until_completed(&self) -> bool {
        // SAFETY: `self.ptr` is a live descriptor handle.
        unsafe { ffi::mpsgraph_execution_descriptor_wait_until_completed(self.ptr) }
    }

    pub fn set_wait_until_completed(&self, value: bool) -> Result<()> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ok = unsafe { ffi::mpsgraph_execution_descriptor_set_wait_until_completed(self.ptr, value) };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set waitUntilCompleted"))
        }
    }

    #[must_use]
    pub fn compilation_descriptor(&self) -> Option<CompilationDescriptor> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ptr = unsafe { ffi::mpsgraph_execution_descriptor_compilation_descriptor(self.ptr) };
        if ptr.is_null() {
            None
        } else {
            Some(CompilationDescriptor { ptr })
        }
    }

    pub fn set_compilation_descriptor(&self, descriptor: Option<&CompilationDescriptor>) -> Result<()> {
        let descriptor_ptr = descriptor.map_or(ptr::null_mut(), CompilationDescriptor::as_ptr);
        // SAFETY: all handles remain valid for the duration of the call.
        let ok = unsafe {
            ffi::mpsgraph_execution_descriptor_set_compilation_descriptor(self.ptr, descriptor_ptr)
        };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set compilation descriptor"))
        }
    }
}

/// Safe owner for `MPSGraphExecutableExecutionDescriptor`.
pub struct ExecutableExecutionDescriptor {
    ptr: *mut c_void,
}

unsafe impl Send for ExecutableExecutionDescriptor {}
unsafe impl Sync for ExecutableExecutionDescriptor {}

impl Drop for ExecutableExecutionDescriptor {
    fn drop(&mut self) {
        release_handle(&mut self.ptr);
    }
}

impl ExecutableExecutionDescriptor {
    #[must_use]
    pub fn new() -> Option<Self> {
        // SAFETY: pure constructor.
        let ptr = unsafe { ffi::mpsgraph_executable_execution_descriptor_new() };
        if ptr.is_null() {
            None
        } else {
            Some(Self { ptr })
        }
    }

    #[must_use]
    pub(crate) const fn as_ptr(&self) -> *mut c_void {
        self.ptr
    }

    #[must_use]
    pub fn wait_until_completed(&self) -> bool {
        // SAFETY: `self.ptr` is a live descriptor handle.
        unsafe { ffi::mpsgraph_executable_execution_descriptor_wait_until_completed(self.ptr) }
    }

    pub fn set_wait_until_completed(&self, value: bool) -> Result<()> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ok = unsafe {
            ffi::mpsgraph_executable_execution_descriptor_set_wait_until_completed(self.ptr, value)
        };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set executable waitUntilCompleted"))
        }
    }
}

/// Safe owner for `MPSGraphExecutableSerializationDescriptor`.
pub struct ExecutableSerializationDescriptor {
    ptr: *mut c_void,
}

unsafe impl Send for ExecutableSerializationDescriptor {}
unsafe impl Sync for ExecutableSerializationDescriptor {}

impl Drop for ExecutableSerializationDescriptor {
    fn drop(&mut self) {
        release_handle(&mut self.ptr);
    }
}

impl ExecutableSerializationDescriptor {
    #[must_use]
    pub fn new() -> Option<Self> {
        // SAFETY: pure constructor.
        let ptr = unsafe { ffi::mpsgraph_executable_serialization_descriptor_new() };
        if ptr.is_null() {
            None
        } else {
            Some(Self { ptr })
        }
    }

    #[must_use]
    pub(crate) const fn as_ptr(&self) -> *mut c_void {
        self.ptr
    }

    #[must_use]
    pub fn append(&self) -> bool {
        // SAFETY: `self.ptr` is a live descriptor handle.
        unsafe { ffi::mpsgraph_executable_serialization_descriptor_append(self.ptr) }
    }

    pub fn set_append(&self, value: bool) -> Result<()> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ok = unsafe { ffi::mpsgraph_executable_serialization_descriptor_set_append(self.ptr, value) };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set append"))
        }
    }

    #[must_use]
    pub fn deployment_platform(&self) -> u64 {
        // SAFETY: `self.ptr` is a live descriptor handle.
        unsafe { ffi::mpsgraph_executable_serialization_descriptor_deployment_platform(self.ptr) }
    }

    pub fn set_deployment_platform(&self, value: u64) -> Result<()> {
        // SAFETY: `self.ptr` is a live descriptor handle.
        let ok = unsafe {
            ffi::mpsgraph_executable_serialization_descriptor_set_deployment_platform(self.ptr, value)
        };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set deployment platform"))
        }
    }

    pub fn minimum_deployment_target(&self) -> Result<String> {
        copy_string(
            ffi::mpsgraph_executable_serialization_descriptor_minimum_deployment_target_len,
            ffi::mpsgraph_executable_serialization_descriptor_copy_minimum_deployment_target,
            self.ptr,
        )
    }

    pub fn set_minimum_deployment_target(&self, value: &str) -> Result<()> {
        let value = CString::new(value).map_err(|_| Error::OperationFailed("minimum deployment target contained NUL"))?;
        // SAFETY: the CString stays alive for the duration of the call.
        let ok = unsafe {
            ffi::mpsgraph_executable_serialization_descriptor_set_minimum_deployment_target(
                self.ptr,
                value.as_ptr(),
            )
        };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set minimum deployment target"))
        }
    }
}

impl Graph {
    /// Return the graph's `MPSGraphOptions` bitmask.
    #[must_use]
    pub fn options(&self) -> u64 {
        // SAFETY: `self` owns a live graph handle.
        unsafe { ffi::mpsgraph_graph_options(self.as_ptr()) }
    }

    /// Replace the graph's options bitmask.
    pub fn set_options(&self, options: u64) -> Result<()> {
        // SAFETY: `self` owns a live graph handle.
        let ok = unsafe { ffi::mpsgraph_graph_set_options(self.as_ptr(), options) };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set graph options"))
        }
    }

    /// Return the graph's placeholder tensors in insertion order.
    #[must_use]
    pub fn placeholder_tensors(&self) -> Vec<Tensor> {
        // SAFETY: `self` owns a live graph handle.
        let box_handle = unsafe { ffi::mpsgraph_graph_placeholder_tensors(self.as_ptr()) };
        collect_owned_tensors(box_handle)
    }

    /// Compile the graph with an optional compilation descriptor.
    #[must_use]
    pub fn compile_with_descriptor(
        &self,
        device: Option<&MetalDevice>,
        feeds: &[FeedDescription<'_>],
        targets: &[&Tensor],
        descriptor: Option<&CompilationDescriptor>,
    ) -> Option<Executable> {
        let feed_tensors = feeds.iter().map(|feed| feed.tensor.as_ptr()).collect::<Vec<_>>();
        let shape_lengths = feeds.iter().map(|feed| feed.shape.len()).collect::<Vec<_>>();
        let data_types = feeds.iter().map(|feed| feed.data_type).collect::<Vec<_>>();
        let flat_shapes = feeds
            .iter()
            .flat_map(|feed| feed.shape.iter().copied())
            .collect::<Vec<_>>();
        let target_tensors = targets.iter().map(|tensor| tensor.as_ptr()).collect::<Vec<_>>();
        let device_ptr = device.map_or(ptr::null_mut(), MetalDevice::as_ptr);
        let descriptor_ptr = descriptor.map_or(ptr::null_mut(), CompilationDescriptor::as_ptr);

        // SAFETY: all pointer arrays stay alive for the duration of the call.
        let ptr = unsafe {
            ffi::mpsgraph_graph_compile_with_descriptor(
                self.as_ptr(),
                device_ptr,
                feed_tensors.as_ptr(),
                feeds.len(),
                flat_shapes.as_ptr(),
                shape_lengths.as_ptr(),
                data_types.as_ptr(),
                target_tensors.as_ptr(),
                targets.len(),
                descriptor_ptr,
            )
        };
        if ptr.is_null() {
            None
        } else {
            Some(Executable::from_raw(ptr, targets.len()))
        }
    }
}

impl Executable {
    /// Return the executable's `MPSGraphOptions` bitmask.
    #[must_use]
    pub fn options(&self) -> u64 {
        // SAFETY: `self` owns a live executable handle.
        unsafe { ffi::mpsgraph_executable_options(self.as_ptr()) }
    }

    /// Replace the executable's options bitmask.
    pub fn set_options(&self, options: u64) -> Result<()> {
        // SAFETY: `self` owns a live executable handle.
        let ok = unsafe { ffi::mpsgraph_executable_set_options(self.as_ptr(), options) };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to set executable options"))
        }
    }

    /// Return feed tensors if this executable was compiled from a graph.
    #[must_use]
    pub fn feed_tensors(&self) -> Vec<Tensor> {
        // SAFETY: `self` owns a live executable handle.
        let box_handle = unsafe { ffi::mpsgraph_executable_feed_tensors(self.as_ptr()) };
        collect_owned_tensors(box_handle)
    }

    /// Return target tensors if this executable was compiled from a graph.
    #[must_use]
    pub fn target_tensors(&self) -> Vec<Tensor> {
        // SAFETY: `self` owns a live executable handle.
        let box_handle = unsafe { ffi::mpsgraph_executable_target_tensors(self.as_ptr()) };
        collect_owned_tensors(box_handle)
    }

    /// Specialize the executable for the provided input types.
    pub fn specialize(
        &self,
        device: Option<&MetalDevice>,
        input_types: &[&ShapedType],
        descriptor: Option<&CompilationDescriptor>,
    ) -> Result<()> {
        let input_type_handles = input_types
            .iter()
            .map(|value| value.as_ptr())
            .collect::<Vec<_>>();
        let device_ptr = device.map_or(ptr::null_mut(), MetalDevice::as_ptr);
        let descriptor_ptr = descriptor.map_or(ptr::null_mut(), CompilationDescriptor::as_ptr);

        // SAFETY: all pointer arrays stay alive for the duration of the call.
        let ok = unsafe {
            ffi::mpsgraph_executable_specialize(
                self.as_ptr(),
                device_ptr,
                input_type_handles.as_ptr(),
                input_types.len(),
                descriptor_ptr,
            )
        };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to specialize executable"))
        }
    }

    /// Query specialized output types for the provided input types.
    pub fn output_types(
        &self,
        device: Option<&MetalDevice>,
        input_types: &[&ShapedType],
        descriptor: Option<&CompilationDescriptor>,
    ) -> Result<Vec<ShapedType>> {
        let input_type_handles = input_types
            .iter()
            .map(|value| value.as_ptr())
            .collect::<Vec<_>>();
        let device_ptr = device.map_or(ptr::null_mut(), MetalDevice::as_ptr);
        let descriptor_ptr = descriptor.map_or(ptr::null_mut(), CompilationDescriptor::as_ptr);

        // SAFETY: all pointer arrays stay alive for the duration of the call.
        let box_handle = unsafe {
            ffi::mpsgraph_executable_get_output_types(
                self.as_ptr(),
                device_ptr,
                input_type_handles.as_ptr(),
                input_types.len(),
                descriptor_ptr,
            )
        };
        if box_handle.is_null() {
            Err(Error::OperationFailed("failed to get executable output types"))
        } else {
            Ok(collect_shaped_type_array_box(box_handle))
        }
    }

    /// Run the executable with an optional execution descriptor and optional preallocated results.
    pub fn run_with_descriptor(
        &self,
        command_queue: &CommandQueue,
        inputs: &[&TensorData],
        results: Option<&[&TensorData]>,
        descriptor: Option<&ExecutableExecutionDescriptor>,
    ) -> Result<Vec<TensorData>> {
        let input_handles = inputs.iter().map(|value| value.as_ptr()).collect::<Vec<_>>();
        let result_handles = results
            .map(|values| values.iter().map(|value| value.as_ptr()).collect::<Vec<_>>())
            .unwrap_or_default();
        let descriptor_ptr = descriptor.map_or(ptr::null_mut(), ExecutableExecutionDescriptor::as_ptr);

        // SAFETY: all pointer arrays stay alive for the duration of the call.
        let box_handle = unsafe {
            ffi::mpsgraph_executable_run_with_descriptor(
                self.as_ptr(),
                command_queue.as_ptr(),
                input_handles.as_ptr(),
                inputs.len(),
                result_handles.as_ptr(),
                result_handles.len(),
                descriptor_ptr,
            )
        };
        if box_handle.is_null() {
            Err(Error::OperationFailed("failed to run executable"))
        } else {
            Ok(collect_tensor_data_array_box(box_handle))
        }
    }

    /// Asynchronously run the executable with an optional execution descriptor.
    pub fn run_async_with_descriptor(
        &self,
        command_queue: &CommandQueue,
        inputs: &[&TensorData],
        results: Option<&[&TensorData]>,
        descriptor: Option<&ExecutableExecutionDescriptor>,
    ) -> Result<Vec<TensorData>> {
        let input_handles = inputs.iter().map(|value| value.as_ptr()).collect::<Vec<_>>();
        let result_handles = results
            .map(|values| values.iter().map(|value| value.as_ptr()).collect::<Vec<_>>())
            .unwrap_or_default();
        let descriptor_ptr = descriptor.map_or(ptr::null_mut(), ExecutableExecutionDescriptor::as_ptr);

        // SAFETY: all pointer arrays stay alive for the duration of the call.
        let box_handle = unsafe {
            ffi::mpsgraph_executable_run_async_with_descriptor(
                self.as_ptr(),
                command_queue.as_ptr(),
                input_handles.as_ptr(),
                inputs.len(),
                result_handles.as_ptr(),
                result_handles.len(),
                descriptor_ptr,
            )
        };
        if box_handle.is_null() {
            Err(Error::OperationFailed("failed to run executable asynchronously"))
        } else {
            Ok(collect_tensor_data_array_box(box_handle))
        }
    }

    /// Serialize the executable to an `.mpsgraphpackage` path.
    pub fn serialize_package(
        &self,
        path: &str,
        descriptor: Option<&ExecutableSerializationDescriptor>,
    ) -> Result<()> {
        let path = CString::new(path).map_err(|_| Error::OperationFailed("package path contained NUL"))?;
        let descriptor_ptr = descriptor.map_or(ptr::null_mut(), ExecutableSerializationDescriptor::as_ptr);
        // SAFETY: the CString stays alive for the duration of the call.
        let ok = unsafe { ffi::mpsgraph_executable_serialize_package(self.as_ptr(), path.as_ptr(), descriptor_ptr) };
        if ok {
            Ok(())
        } else {
            Err(Error::OperationFailed("failed to serialize executable package"))
        }
    }

    /// Load an executable from an existing `.mpsgraphpackage`.
    pub fn from_package(path: &str, descriptor: Option<&CompilationDescriptor>) -> Result<Self> {
        let path = CString::new(path).map_err(|_| Error::OperationFailed("package path contained NUL"))?;
        let descriptor_ptr = descriptor.map_or(ptr::null_mut(), CompilationDescriptor::as_ptr);
        // SAFETY: the CString stays alive for the duration of the call.
        let ptr = unsafe { ffi::mpsgraph_executable_new_with_package(path.as_ptr(), descriptor_ptr) };
        if ptr.is_null() {
            return Err(Error::OperationFailed("failed to load executable package"));
        }
        let output_count = {
            // SAFETY: `ptr` is a live executable handle returned just above.
            let box_handle = unsafe { ffi::mpsgraph_executable_target_tensors(ptr) };
            collect_owned_tensors(box_handle).len()
        };
        Ok(Self::from_raw(ptr, output_count))
    }
}