[MachineLearning.Inference] Add Inference APIs for Machine Learning (#940)
[platform/core/csapi/tizenfx.git] / src / Tizen.MachineLearning.Inference / Interop / Interop.Nnstreamer.cs
1 /*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using Tizen.MachineLearning.Inference;
20
21 internal static partial class Interop
22 {
23     internal static partial class Libraries
24     {
25         public const string Nnstreamer = "libcapi-nnstreamer.so.0";
26     }
27
28     internal static partial class SingleShot
29     {
30         /* int ml_single_open (ml_single_h *single, const char *model, const ml_tensors_info_h input_info, const ml_tensors_info_h output_info, ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw) */
31         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_open", CallingConvention = CallingConvention.Cdecl)]
32         internal static extern NNStreamerError OpenSingle(out IntPtr single_handle, string model_path, IntPtr input_info, IntPtr output_info, NNFWType nn_type, HWType hw_type);
33
34         /* int ml_single_close (ml_single_h single) */
35         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_close", CallingConvention = CallingConvention.Cdecl)]
36         internal static extern NNStreamerError CloseSingle(IntPtr single_handle);
37
38         /* int ml_single_invoke (ml_single_h single, const ml_tensors_data_h input, ml_tensors_data_h *output) */
39         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_invoke", CallingConvention = CallingConvention.Cdecl)]
40         internal static extern NNStreamerError InvokeSingle(IntPtr single_handle, IntPtr input_data, out IntPtr output_data);
41
42         /* int ml_single_get_input_info (ml_single_h single, ml_tensors_info_h *info) */
43         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_invoke", CallingConvention = CallingConvention.Cdecl)]
44         internal static extern NNStreamerError GetInputTensorsInfoFromSingle(IntPtr single_handle, out IntPtr input_info);
45
46         /* int ml_single_get_output_info (ml_single_h single, ml_tensors_info_h *info) */
47         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_get_output_info", CallingConvention = CallingConvention.Cdecl)]
48         internal static extern NNStreamerError GetOutputTensorsInfoFromSingle(IntPtr single_handle, out IntPtr output_info);
49     }
50
51     internal static partial class Util
52     {
53         /* int ml_tensors_info_create (ml_tensors_info_h *info) */
54         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_create", CallingConvention = CallingConvention.Cdecl)]
55         internal static extern NNStreamerError CreateTensorsInfo(out IntPtr info);
56             
57         /* int ml_tensors_info_destroy (ml_tensors_info_h info) */
58         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_destroy", CallingConvention = CallingConvention.Cdecl)]
59         internal static extern NNStreamerError DestroyTensorsInfo(IntPtr info);
60
61         /* int ml_tensors_info_validate (const ml_tensors_info_h info, bool *valid) */
62         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_validate", CallingConvention = CallingConvention.Cdecl)]
63         internal static extern NNStreamerError ValidateTensorsInfo(IntPtr info, out bool valid);
64
65         /* int ml_tensors_info_clone (ml_tensors_info_h dest, const ml_tensors_info_h src) */
66         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_clone", CallingConvention = CallingConvention.Cdecl)]
67         internal static extern NNStreamerError CloneTensorsInfo(out IntPtr dest_info, IntPtr src_info);
68
69         /* int ml_tensors_info_set_count (ml_tensors_info_h info, unsigned int count) */
70         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_count", CallingConvention = CallingConvention.Cdecl)]
71         internal static extern NNStreamerError SetTensorsCount(IntPtr info, int count);
72
73         /* int ml_tensors_info_get_count (ml_tensors_info_h info, unsigned int *count) */
74         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_count", CallingConvention = CallingConvention.Cdecl)]
75         internal static extern NNStreamerError GetTensorsCount(IntPtr info, out int count);
76
77         /* int ml_tensors_info_set_tensor_name (ml_tensors_info_h info, unsigned int index, const char *name) */
78         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_tensor_name", CallingConvention = CallingConvention.Cdecl)]
79         internal static extern NNStreamerError SetTensorName(IntPtr info, int index, string name);
80
81         /* int ml_tensors_info_get_tensor_name (ml_tensors_info_h info, unsigned int index, char **name) */
82         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_tensor_name", CallingConvention = CallingConvention.Cdecl)]
83         internal static extern NNStreamerError GetTensorName(IntPtr info, int index, out string name);
84
85         /* int ml_tensors_info_set_tensor_type (ml_tensors_info_h info, unsigned int index, const ml_tensor_type_e type) */
86         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_tensor_type", CallingConvention = CallingConvention.Cdecl)]
87         internal static extern NNStreamerError SetTensorType(IntPtr info, int index, TensorType type);
88
89         /* int ml_tensors_info_get_tensor_type (ml_tensors_info_h info, unsigned int index, ml_tensor_type_e *type) */
90         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_tensor_type", CallingConvention = CallingConvention.Cdecl)]
91         internal static extern NNStreamerError GetTensorType(IntPtr info, int index, out TensorType type);
92
93         /* int ml_tensors_info_set_tensor_dimension (ml_tensors_info_h info, unsigned int index, const ml_tensor_dimension dimension) */
94         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_tensor_dimension", CallingConvention = CallingConvention.Cdecl)]
95         internal static extern NNStreamerError SetTensorDimension(IntPtr info, int index, int[] dimension);
96
97         /* int ml_tensors_info_get_tensor_dimension (ml_tensors_info_h info, unsigned int index, ml_tensor_dimension dimension) */
98         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_tensor_dimension", CallingConvention = CallingConvention.Cdecl)]
99         internal static extern NNStreamerError GetTensorDimension(IntPtr info, int index, out int[] dimension);
100
101         /* size_t ml_tensors_info_get_size (const ml_tensors_info_h info) */
102         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_size", CallingConvention = CallingConvention.Cdecl)]
103         internal static extern int GetTensorsSize(IntPtr info);
104
105         /* int ml_tensors_data_create (const ml_tensors_info_h info, ml_tensors_data_h *data) */
106         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_create", CallingConvention = CallingConvention.Cdecl)]
107         internal static extern NNStreamerError CreateTensorsData(IntPtr info, out IntPtr data);
108
109         /* int ml_tensors_data_destroy (ml_tensors_data_h data) */
110         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_destroy", CallingConvention = CallingConvention.Cdecl)]
111         internal static extern NNStreamerError DestroyTensorsData(IntPtr data);
112
113         /* int ml_tensors_data_get_tensor_data (ml_tensors_data_h data, unsigned int index, void **raw_data, size_t *data_size) */
114         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_get_tensor_data", CallingConvention = CallingConvention.Cdecl)]
115         internal static extern NNStreamerError GetTensorData(IntPtr data, int index, out IntPtr raw_data, out int data_size);
116
117         /* int ml_tensors_data_set_tensor_data (ml_tensors_data_h data, unsigned int index, const void *raw_data, const size_t data_size) */
118         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_set_tensor_data", CallingConvention = CallingConvention.Cdecl)]
119         internal static extern NNStreamerError SetTensorData(IntPtr data, int index, byte[] raw_data, int data_size);
120
121         /* int ml_check_nnfw_availability (ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw, bool *available); */
122         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_check_nnfw_availability", CallingConvention = CallingConvention.Cdecl)]
123         internal static extern NNStreamerError CheckNNFWAvailability(NNFWType nnfw, HWType hw, out bool available);
124
125         /* ml_tensors_data_get_tensor_count (ml_tensors_data_h data, unsigned int *num_tensors) */
126         [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_get_tensor_count", CallingConvention = CallingConvention.Cdecl)]
127         internal static extern NNStreamerError GetTensorsCount(IntPtr data, out uint count);
128
129         internal static byte[] IntPtrToByteArray(IntPtr unmanagedByteArray, int size)
130         {
131             byte[] retByte = new byte[size];
132             Marshal.Copy(unmanagedByteArray, retByte, 0, size);
133             return retByte;
134         }
135     }
136 }