Skip to main content

apple_mpsgraph/ffi/
mod.rs

1use core::ffi::{c_char, c_void};
2
3unsafe extern "C" {
4    pub fn mpsgraph_object_release(handle: *mut c_void);
5
6    pub fn mpsgraph_tensor_data_new_with_bytes(
7        device_handle: *mut c_void,
8        bytes: *const c_void,
9        byte_len: usize,
10        shape: *const usize,
11        shape_len: usize,
12        data_type: u32,
13    ) -> *mut c_void;
14    pub fn mpsgraph_tensor_data_new_with_buffer(
15        buffer_handle: *mut c_void,
16        shape: *const usize,
17        shape_len: usize,
18        data_type: u32,
19    ) -> *mut c_void;
20    pub fn mpsgraph_tensor_data_data_type(handle: *mut c_void) -> u32;
21    pub fn mpsgraph_tensor_data_shape_len(handle: *mut c_void) -> usize;
22    pub fn mpsgraph_tensor_data_copy_shape(handle: *mut c_void, out_shape: *mut usize);
23    pub fn mpsgraph_tensor_data_read_bytes(
24        handle: *mut c_void,
25        dst: *mut c_void,
26        dst_len: usize,
27    ) -> bool;
28    pub fn mpsgraph_tensor_data_device(handle: *mut c_void) -> *mut c_void;
29
30    pub fn mpsgraph_graph_new() -> *mut c_void;
31    pub fn mpsgraph_graph_placeholder(
32        graph_handle: *mut c_void,
33        shape: *const usize,
34        shape_len: usize,
35        data_type: u32,
36        name: *const c_char,
37    ) -> *mut c_void;
38    pub fn mpsgraph_graph_constant_data(
39        graph_handle: *mut c_void,
40        bytes: *const c_void,
41        byte_len: usize,
42        shape: *const usize,
43        shape_len: usize,
44        data_type: u32,
45    ) -> *mut c_void;
46    pub fn mpsgraph_graph_constant_scalar(
47        graph_handle: *mut c_void,
48        scalar: f64,
49        data_type: u32,
50    ) -> *mut c_void;
51    pub fn mpsgraph_graph_constant_scalar_shaped(
52        graph_handle: *mut c_void,
53        scalar: f64,
54        shape: *const usize,
55        shape_len: usize,
56        data_type: u32,
57    ) -> *mut c_void;
58
59    pub fn mpsgraph_graph_addition(
60        graph_handle: *mut c_void,
61        primary_tensor: *mut c_void,
62        secondary_tensor: *mut c_void,
63        name: *const c_char,
64    ) -> *mut c_void;
65    pub fn mpsgraph_graph_subtraction(
66        graph_handle: *mut c_void,
67        primary_tensor: *mut c_void,
68        secondary_tensor: *mut c_void,
69        name: *const c_char,
70    ) -> *mut c_void;
71    pub fn mpsgraph_graph_multiplication(
72        graph_handle: *mut c_void,
73        primary_tensor: *mut c_void,
74        secondary_tensor: *mut c_void,
75        name: *const c_char,
76    ) -> *mut c_void;
77    pub fn mpsgraph_graph_division(
78        graph_handle: *mut c_void,
79        primary_tensor: *mut c_void,
80        secondary_tensor: *mut c_void,
81        name: *const c_char,
82    ) -> *mut c_void;
83    pub fn mpsgraph_graph_matrix_multiplication(
84        graph_handle: *mut c_void,
85        primary_tensor: *mut c_void,
86        secondary_tensor: *mut c_void,
87        name: *const c_char,
88    ) -> *mut c_void;
89    pub fn mpsgraph_graph_relu(
90        graph_handle: *mut c_void,
91        tensor: *mut c_void,
92        name: *const c_char,
93    ) -> *mut c_void;
94    pub fn mpsgraph_graph_sigmoid(
95        graph_handle: *mut c_void,
96        tensor: *mut c_void,
97        name: *const c_char,
98    ) -> *mut c_void;
99    pub fn mpsgraph_graph_softmax(
100        graph_handle: *mut c_void,
101        tensor: *mut c_void,
102        axis: isize,
103        name: *const c_char,
104    ) -> *mut c_void;
105    pub fn mpsgraph_graph_reshape(
106        graph_handle: *mut c_void,
107        tensor: *mut c_void,
108        shape: *const usize,
109        shape_len: usize,
110        name: *const c_char,
111    ) -> *mut c_void;
112    pub fn mpsgraph_graph_transpose(
113        graph_handle: *mut c_void,
114        tensor: *mut c_void,
115        permutation: *const usize,
116        permutation_len: usize,
117        name: *const c_char,
118    ) -> *mut c_void;
119    pub fn mpsgraph_graph_slice(
120        graph_handle: *mut c_void,
121        tensor: *mut c_void,
122        dimension: usize,
123        start: isize,
124        length: isize,
125        name: *const c_char,
126    ) -> *mut c_void;
127    pub fn mpsgraph_graph_broadcast(
128        graph_handle: *mut c_void,
129        tensor: *mut c_void,
130        shape: *const usize,
131        shape_len: usize,
132        name: *const c_char,
133    ) -> *mut c_void;
134    pub fn mpsgraph_graph_reduction_sum(
135        graph_handle: *mut c_void,
136        tensor: *mut c_void,
137        axes: *const usize,
138        axes_len: usize,
139        name: *const c_char,
140    ) -> *mut c_void;
141    pub fn mpsgraph_graph_reduction_maximum(
142        graph_handle: *mut c_void,
143        tensor: *mut c_void,
144        axes: *const usize,
145        axes_len: usize,
146        name: *const c_char,
147    ) -> *mut c_void;
148    pub fn mpsgraph_graph_reduction_minimum(
149        graph_handle: *mut c_void,
150        tensor: *mut c_void,
151        axes: *const usize,
152        axes_len: usize,
153        name: *const c_char,
154    ) -> *mut c_void;
155    pub fn mpsgraph_graph_mean(
156        graph_handle: *mut c_void,
157        tensor: *mut c_void,
158        axes: *const usize,
159        axes_len: usize,
160        name: *const c_char,
161    ) -> *mut c_void;
162
163    pub fn mpsgraph_convolution2d_descriptor_new(
164        stride_in_x: usize,
165        stride_in_y: usize,
166        dilation_rate_in_x: usize,
167        dilation_rate_in_y: usize,
168        groups: usize,
169        padding_left: usize,
170        padding_right: usize,
171        padding_top: usize,
172        padding_bottom: usize,
173        padding_style: usize,
174        data_layout: usize,
175        weights_layout: usize,
176    ) -> *mut c_void;
177    pub fn mpsgraph_pooling2d_descriptor_new(
178        kernel_width: usize,
179        kernel_height: usize,
180        stride_in_x: usize,
181        stride_in_y: usize,
182        dilation_rate_in_x: usize,
183        dilation_rate_in_y: usize,
184        padding_left: usize,
185        padding_right: usize,
186        padding_top: usize,
187        padding_bottom: usize,
188        padding_style: usize,
189        data_layout: usize,
190    ) -> *mut c_void;
191    pub fn mpsgraph_graph_convolution2d(
192        graph_handle: *mut c_void,
193        source_tensor: *mut c_void,
194        weights_tensor: *mut c_void,
195        descriptor_handle: *mut c_void,
196        name: *const c_char,
197    ) -> *mut c_void;
198    pub fn mpsgraph_graph_max_pooling2d(
199        graph_handle: *mut c_void,
200        source_tensor: *mut c_void,
201        descriptor_handle: *mut c_void,
202        name: *const c_char,
203    ) -> *mut c_void;
204    pub fn mpsgraph_graph_normalize(
205        graph_handle: *mut c_void,
206        tensor: *mut c_void,
207        mean_tensor: *mut c_void,
208        variance_tensor: *mut c_void,
209        gamma_tensor: *mut c_void,
210        beta_tensor: *mut c_void,
211        epsilon: f32,
212        name: *const c_char,
213    ) -> *mut c_void;
214
215    pub fn mpsgraph_graph_run(
216        graph_handle: *mut c_void,
217        feed_tensors: *const *mut c_void,
218        feed_data: *const *mut c_void,
219        feed_count: usize,
220        target_tensors: *const *mut c_void,
221        target_count: usize,
222        out_results: *mut *mut c_void,
223    ) -> bool;
224    pub fn mpsgraph_graph_run_with_command_queue(
225        graph_handle: *mut c_void,
226        command_queue_handle: *mut c_void,
227        feed_tensors: *const *mut c_void,
228        feed_data: *const *mut c_void,
229        feed_count: usize,
230        target_tensors: *const *mut c_void,
231        target_count: usize,
232        out_results: *mut *mut c_void,
233    ) -> bool;
234    pub fn mpsgraph_graph_compile(
235        graph_handle: *mut c_void,
236        device_handle: *mut c_void,
237        feed_tensors: *const *mut c_void,
238        feed_count: usize,
239        flat_shapes: *const usize,
240        shape_lengths: *const usize,
241        data_types: *const u32,
242        target_tensors: *const *mut c_void,
243        target_count: usize,
244    ) -> *mut c_void;
245    pub fn mpsgraph_executable_run(
246        executable_handle: *mut c_void,
247        command_queue_handle: *mut c_void,
248        input_data: *const *mut c_void,
249        input_count: usize,
250        output_count: usize,
251        out_results: *mut *mut c_void,
252    ) -> bool;
253
254    pub fn mpsgraph_tensor_array_box_len(handle: *mut c_void) -> usize;
255    pub fn mpsgraph_tensor_array_box_get(handle: *mut c_void, index: usize) -> *mut c_void;
256    pub fn mpsgraph_tensor_data_array_box_len(handle: *mut c_void) -> usize;
257    pub fn mpsgraph_tensor_data_array_box_get(handle: *mut c_void, index: usize) -> *mut c_void;
258    pub fn mpsgraph_shaped_type_array_box_len(handle: *mut c_void) -> usize;
259    pub fn mpsgraph_shaped_type_array_box_get(handle: *mut c_void, index: usize) -> *mut c_void;
260
261    pub fn mpsgraph_device_new_with_metal_device(metal_device_handle: *mut c_void) -> *mut c_void;
262    pub fn mpsgraph_device_type(handle: *mut c_void) -> u32;
263
264    pub fn mpsgraph_shaped_type_new(
265        shape: *const isize,
266        shape_len: usize,
267        data_type: u32,
268    ) -> *mut c_void;
269    pub fn mpsgraph_shaped_type_has_shape(handle: *mut c_void) -> bool;
270    pub fn mpsgraph_shaped_type_shape_len(handle: *mut c_void) -> usize;
271    pub fn mpsgraph_shaped_type_copy_shape(handle: *mut c_void, out_shape: *mut isize);
272    pub fn mpsgraph_shaped_type_data_type(handle: *mut c_void) -> u32;
273    pub fn mpsgraph_shaped_type_set_shape(
274        handle: *mut c_void,
275        shape: *const isize,
276        shape_len: usize,
277    ) -> bool;
278    pub fn mpsgraph_shaped_type_set_data_type(handle: *mut c_void, data_type: u32) -> bool;
279    pub fn mpsgraph_shaped_type_is_equal(handle: *mut c_void, other_handle: *mut c_void) -> bool;
280
281    pub fn mpsgraph_tensor_has_shape(handle: *mut c_void) -> bool;
282    pub fn mpsgraph_tensor_shape_len(handle: *mut c_void) -> usize;
283    pub fn mpsgraph_tensor_copy_shape(handle: *mut c_void, out_shape: *mut isize);
284    pub fn mpsgraph_tensor_data_type(handle: *mut c_void) -> u32;
285    pub fn mpsgraph_tensor_operation(handle: *mut c_void) -> *mut c_void;
286
287    pub fn mpsgraph_graph_options(handle: *mut c_void) -> u64;
288    pub fn mpsgraph_graph_set_options(handle: *mut c_void, raw_value: u64) -> bool;
289    pub fn mpsgraph_graph_placeholder_tensors(handle: *mut c_void) -> *mut c_void;
290
291    pub fn mpsgraph_compilation_descriptor_new() -> *mut c_void;
292    pub fn mpsgraph_compilation_descriptor_disable_type_inference(handle: *mut c_void) -> bool;
293    pub fn mpsgraph_compilation_descriptor_optimization_level(handle: *mut c_void) -> u64;
294    pub fn mpsgraph_compilation_descriptor_set_optimization_level(
295        handle: *mut c_void,
296        raw_value: u64,
297    ) -> bool;
298    pub fn mpsgraph_compilation_descriptor_wait_for_completion(handle: *mut c_void) -> bool;
299    pub fn mpsgraph_compilation_descriptor_set_wait_for_completion(
300        handle: *mut c_void,
301        value: bool,
302    ) -> bool;
303    pub fn mpsgraph_compilation_descriptor_optimization_profile(handle: *mut c_void) -> u64;
304    pub fn mpsgraph_compilation_descriptor_set_optimization_profile(
305        handle: *mut c_void,
306        raw_value: u64,
307    ) -> bool;
308    pub fn mpsgraph_compilation_descriptor_reduced_precision_fast_math(handle: *mut c_void)
309    -> usize;
310    pub fn mpsgraph_compilation_descriptor_set_reduced_precision_fast_math(
311        handle: *mut c_void,
312        raw_value: usize,
313    ) -> bool;
314
315    pub fn mpsgraph_execution_descriptor_new() -> *mut c_void;
316    pub fn mpsgraph_execution_descriptor_wait_until_completed(handle: *mut c_void) -> bool;
317    pub fn mpsgraph_execution_descriptor_set_wait_until_completed(
318        handle: *mut c_void,
319        value: bool,
320    ) -> bool;
321    pub fn mpsgraph_execution_descriptor_compilation_descriptor(handle: *mut c_void) -> *mut c_void;
322    pub fn mpsgraph_execution_descriptor_set_compilation_descriptor(
323        handle: *mut c_void,
324        compilation_descriptor_handle: *mut c_void,
325    ) -> bool;
326
327    pub fn mpsgraph_executable_execution_descriptor_new() -> *mut c_void;
328    pub fn mpsgraph_executable_execution_descriptor_wait_until_completed(
329        handle: *mut c_void,
330    ) -> bool;
331    pub fn mpsgraph_executable_execution_descriptor_set_wait_until_completed(
332        handle: *mut c_void,
333        value: bool,
334    ) -> bool;
335
336    pub fn mpsgraph_executable_serialization_descriptor_new() -> *mut c_void;
337    pub fn mpsgraph_executable_serialization_descriptor_append(handle: *mut c_void) -> bool;
338    pub fn mpsgraph_executable_serialization_descriptor_set_append(
339        handle: *mut c_void,
340        value: bool,
341    ) -> bool;
342    pub fn mpsgraph_executable_serialization_descriptor_deployment_platform(
343        handle: *mut c_void,
344    ) -> u64;
345    pub fn mpsgraph_executable_serialization_descriptor_set_deployment_platform(
346        handle: *mut c_void,
347        raw_value: u64,
348    ) -> bool;
349    pub fn mpsgraph_executable_serialization_descriptor_minimum_deployment_target_len(
350        handle: *mut c_void,
351    ) -> usize;
352    pub fn mpsgraph_executable_serialization_descriptor_copy_minimum_deployment_target(
353        handle: *mut c_void,
354        out_bytes: *mut u8,
355        out_len: usize,
356    ) -> bool;
357    pub fn mpsgraph_executable_serialization_descriptor_set_minimum_deployment_target(
358        handle: *mut c_void,
359        value: *const c_char,
360    ) -> bool;
361
362    pub fn mpsgraph_graph_compile_with_descriptor(
363        graph_handle: *mut c_void,
364        device_handle: *mut c_void,
365        feed_tensors: *const *mut c_void,
366        feed_count: usize,
367        flat_shapes: *const usize,
368        shape_lengths: *const usize,
369        data_types: *const u32,
370        target_tensors: *const *mut c_void,
371        target_count: usize,
372        compilation_descriptor_handle: *mut c_void,
373    ) -> *mut c_void;
374
375    pub fn mpsgraph_executable_options(handle: *mut c_void) -> u64;
376    pub fn mpsgraph_executable_set_options(handle: *mut c_void, raw_value: u64) -> bool;
377    pub fn mpsgraph_executable_feed_tensors(handle: *mut c_void) -> *mut c_void;
378    pub fn mpsgraph_executable_target_tensors(handle: *mut c_void) -> *mut c_void;
379    pub fn mpsgraph_executable_specialize(
380        handle: *mut c_void,
381        device_handle: *mut c_void,
382        input_type_handles: *const *mut c_void,
383        input_type_count: usize,
384        compilation_descriptor_handle: *mut c_void,
385    ) -> bool;
386    pub fn mpsgraph_executable_get_output_types(
387        handle: *mut c_void,
388        device_handle: *mut c_void,
389        input_type_handles: *const *mut c_void,
390        input_type_count: usize,
391        compilation_descriptor_handle: *mut c_void,
392    ) -> *mut c_void;
393    pub fn mpsgraph_executable_run_with_descriptor(
394        executable_handle: *mut c_void,
395        command_queue_handle: *mut c_void,
396        input_handles: *const *mut c_void,
397        input_count: usize,
398        result_handles: *const *mut c_void,
399        result_count: usize,
400        execution_descriptor_handle: *mut c_void,
401    ) -> *mut c_void;
402    pub fn mpsgraph_executable_run_async_with_descriptor(
403        executable_handle: *mut c_void,
404        command_queue_handle: *mut c_void,
405        input_handles: *const *mut c_void,
406        input_count: usize,
407        result_handles: *const *mut c_void,
408        result_count: usize,
409        execution_descriptor_handle: *mut c_void,
410    ) -> *mut c_void;
411    pub fn mpsgraph_executable_serialize_package(
412        handle: *mut c_void,
413        path: *const c_char,
414        descriptor_handle: *mut c_void,
415    ) -> bool;
416    pub fn mpsgraph_executable_new_with_package(
417        path: *const c_char,
418        compilation_descriptor_handle: *mut c_void,
419    ) -> *mut c_void;
420
421    pub fn mpsgraph_graph_arithmetic_unary(
422        graph_handle: *mut c_void,
423        op: u32,
424        tensor_handle: *mut c_void,
425        name: *const c_char,
426    ) -> *mut c_void;
427    pub fn mpsgraph_graph_arithmetic_binary(
428        graph_handle: *mut c_void,
429        op: u32,
430        primary_handle: *mut c_void,
431        secondary_handle: *mut c_void,
432        name: *const c_char,
433    ) -> *mut c_void;
434    pub fn mpsgraph_graph_select(
435        graph_handle: *mut c_void,
436        predicate_handle: *mut c_void,
437        true_handle: *mut c_void,
438        false_handle: *mut c_void,
439        name: *const c_char,
440    ) -> *mut c_void;
441    pub fn mpsgraph_graph_relu_gradient(
442        graph_handle: *mut c_void,
443        gradient_handle: *mut c_void,
444        source_handle: *mut c_void,
445        name: *const c_char,
446    ) -> *mut c_void;
447    pub fn mpsgraph_graph_sigmoid_gradient(
448        graph_handle: *mut c_void,
449        gradient_handle: *mut c_void,
450        source_handle: *mut c_void,
451        name: *const c_char,
452    ) -> *mut c_void;
453    pub fn mpsgraph_graph_softmax_gradient(
454        graph_handle: *mut c_void,
455        gradient_handle: *mut c_void,
456        source_handle: *mut c_void,
457        axis: isize,
458        name: *const c_char,
459    ) -> *mut c_void;
460    pub fn mpsgraph_graph_leaky_relu_scalar(
461        graph_handle: *mut c_void,
462        tensor_handle: *mut c_void,
463        alpha: f64,
464        name: *const c_char,
465    ) -> *mut c_void;
466    pub fn mpsgraph_graph_leaky_relu_tensor(
467        graph_handle: *mut c_void,
468        tensor_handle: *mut c_void,
469        alpha_tensor_handle: *mut c_void,
470        name: *const c_char,
471    ) -> *mut c_void;
472    pub fn mpsgraph_graph_leaky_relu_gradient(
473        graph_handle: *mut c_void,
474        gradient_handle: *mut c_void,
475        source_handle: *mut c_void,
476        alpha_tensor_handle: *mut c_void,
477        name: *const c_char,
478    ) -> *mut c_void;
479    pub fn mpsgraph_graph_reduction_axis(
480        graph_handle: *mut c_void,
481        op: u32,
482        tensor_handle: *mut c_void,
483        axis: isize,
484        name: *const c_char,
485    ) -> *mut c_void;
486    pub fn mpsgraph_graph_reduction_axes(
487        graph_handle: *mut c_void,
488        op: u32,
489        tensor_handle: *mut c_void,
490        axes: *const usize,
491        axes_len: usize,
492        name: *const c_char,
493    ) -> *mut c_void;
494    pub fn mpsgraph_graph_concat_pair(
495        graph_handle: *mut c_void,
496        first_handle: *mut c_void,
497        second_handle: *mut c_void,
498        dimension: isize,
499        name: *const c_char,
500    ) -> *mut c_void;
501    pub fn mpsgraph_graph_concat_tensors(
502        graph_handle: *mut c_void,
503        tensor_handles: *const *mut c_void,
504        tensor_count: usize,
505        dimension: isize,
506        interleave: bool,
507        name: *const c_char,
508    ) -> *mut c_void;
509    pub fn mpsgraph_graph_split_sizes(
510        graph_handle: *mut c_void,
511        tensor_handle: *mut c_void,
512        split_sizes: *const usize,
513        split_count: usize,
514        axis: isize,
515        name: *const c_char,
516    ) -> *mut c_void;
517    pub fn mpsgraph_graph_split_sizes_tensor(
518        graph_handle: *mut c_void,
519        tensor_handle: *mut c_void,
520        split_sizes_tensor_handle: *mut c_void,
521        axis: isize,
522        name: *const c_char,
523    ) -> *mut c_void;
524    pub fn mpsgraph_graph_split_num(
525        graph_handle: *mut c_void,
526        tensor_handle: *mut c_void,
527        num_splits: usize,
528        axis: isize,
529        name: *const c_char,
530    ) -> *mut c_void;
531    pub fn mpsgraph_graph_stack(
532        graph_handle: *mut c_void,
533        tensor_handles: *const *mut c_void,
534        tensor_count: usize,
535        axis: isize,
536        name: *const c_char,
537    ) -> *mut c_void;
538    pub fn mpsgraph_graph_pad(
539        graph_handle: *mut c_void,
540        tensor_handle: *mut c_void,
541        padding_mode: isize,
542        left_padding: *const isize,
543        left_padding_len: usize,
544        right_padding: *const isize,
545        right_padding_len: usize,
546        constant_value: f64,
547        name: *const c_char,
548    ) -> *mut c_void;
549    pub fn mpsgraph_graph_top_k(
550        graph_handle: *mut c_void,
551        source_handle: *mut c_void,
552        k: usize,
553        name: *const c_char,
554    ) -> *mut c_void;
555    pub fn mpsgraph_graph_top_k_tensor(
556        graph_handle: *mut c_void,
557        source_handle: *mut c_void,
558        k_tensor_handle: *mut c_void,
559        name: *const c_char,
560    ) -> *mut c_void;
561}