-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathRadeonProML.h
278 lines (237 loc) · 8.11 KB
/
RadeonProML.h
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
#pragma once
/**
* @file Machine learning model runner API.
*
* Typical usage is as follows:
* -# Create a context with mlCreateContext().
* -# Set model parameters #ml_model_params.
* -# Create a model with mlCreateModel() using the parameters.
* -# Get input image information with mlGetModelInfo().
* -# Specify required input image dimensions.
* -# Set up model input image dimensions with mlSetModelInputInfo().
* -# Get output image information with mlGetModelInfo().
* -# Create input image.
* -# Create output image.
* -# Fill input image with data using mlMapImage() and mlUnmapImage().
* -# Run inference with mlInfer().
* -# Get output image data using mlMapImage() and mlUnmapImage().
* -# If image size is changed, repeat from the step 5.
* -# If image size is unchanged, repeat from the step 10.
* -# In a case of a failure invoke the mlGetLastError() to get details.
* -# Release the images using mlReleaseImage().
* -# Release the model using mlReleaseModel().
* -# Release the context using mlReleaseContext().
*/
#include <stddef.h>
#if defined(_WIN32)
#ifdef RADEONPROML_BUILD
#define ML_API_ENTRY __declspec(dllexport)
#else
#define ML_API_ENTRY __declspec(dllimport)
#endif
#elif defined(__GNUC__)
#ifdef RADEONPROML_BUILD
#define ML_API_ENTRY __attribute__((visibility ("default")))
#else
#define ML_API_ENTRY
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* Model parameters. All unused values must be initialized to 0.
*/
typedef struct _ml_model_params
{
char const* model_path; /**< Path to a model in protobuf format. */
char const* input_node; /**< Input graph node name, autodetect if null. */
char const* output_node; /**< Output graph node name, autodetect if null. */
float gpu_memory_fraction; /**<
* Fraction of GPU memory allowed to use
* by versions with GPU support, (0..1].
* All memory is used by default.
*/
char const* visible_devices; /**<
* Comma-delimited list of GPU devices
* accessible for calculations.
* All devices are visible by default.
*/
} ml_model_params;
/**
* Context handle.
*/
typedef struct _ml_context* ml_context;
/**
* Model handle.
*/
typedef struct _ml_model* ml_model;
/**
* Image handle.
*/
typedef struct _ml_image* ml_image;
/**
* Operation status.
*/
typedef enum _ml_status
{
ML_OK,
ML_FAIL
} ml_status;
/**
* Image underlying data type.
*/
typedef enum _ml_data_type
{
ML_FLOAT32,
ML_FLOAT16,
ML_INT32,
} ml_data_type;
/**
* Image access mode.
*/
typedef enum _ml_access_mode
{
ML_READ_ONLY,
ML_WRITE_ONLY,
ML_READ_WRITE
} ml_access_mode;
/**
* 3D image description.
*/
typedef struct _ml_image_info
{
ml_data_type dtype; /**< Underlying data type. */
size_t height; /**< Image height, in pixels. 0 if unspecified. */
size_t width; /**< Image width. in pixels. 0 if unspecified. */
size_t channels; /**< Image channel count. 0 if unspecified. */
} ml_image_info;
/**
* Creates a context.
*
* @return A valid context handle in case of success, NULL otherwise.
* The context should be released with mlReleaseContext().
*/
ML_API_ENTRY ml_context mlCreateContext();
/**
* Releases a context created with mlCreateContext(), invalidates the handle.
*
* @param model A valid context handle.
*/
ML_API_ENTRY void mlReleaseContext(ml_context context);
/**
* Creates a 3D image with a given description.
* Image dimension order is (height, width, channels).
*
* @param[in] context A valid context handle.
* @param[in] info Image description with all dimensions specified.
* @param[in] mode Image data access mode.
*
* @return A valid image handle in case of success, NULL otherwise.
* The image should be released with mlReleaseImage().
* To get more details in case of failure, call mlGetLastError().
*/
ML_API_ENTRY ml_image mlCreateImage(ml_context context, ml_image_info const* info, ml_access_mode mode);
/**
* Returns image description.
*
* @param[in] image A valid image handle.
* @param[out] info A pointer to a info structure.
*
* @return ML_OK in case of success, ML_FAIL otherwise.
*/
ML_API_ENTRY ml_status mlGetImageInfo(ml_image image, ml_image_info* info);
/**
* Map the image data into the host address and returns a pointer
* to the mapped region.
*
* @param[in] image A valid image handle.
* @param[out] size A pointer to a size variable. If not null, the referenced
* value is set to image size, in bytes.
*
* @return A pointer to image data.
*/
ML_API_ENTRY void* mlMapImage(ml_image image, size_t* size);
/**
* Unmaps a previously mapped image data.
*
* @param[in] image A valid image handle.
* @param[in] data A pointer to the previously mapped data.
*
* @return ML_OK in case of success, ML_FAIL otherwise.
*/
ML_API_ENTRY ml_status mlUnmapImage(ml_image image, void* data);
/**
* Releases an image created with mlCreateImage(), invalidates the handle.
*
* @param[in] image A valid image handle.
*/
ML_API_ENTRY void mlReleaseImage(ml_image image);
/**
* Loads model data from a file.
*
* @param[in] context A valid context handle.
* @param[in] params Model parameters. @see #ml_model_params.
*
* @return ML_OK in case of success, ML_FAIL otherwise.
* To get more details in case of failure, call mlGetLastError().
*/
ML_API_ENTRY ml_model mlCreateModel(ml_context context, ml_model_params const* params);
/**
* Returns input image information.
*
* @param[in] model A valid model handle.
* @param[out] input_info A pointer to the result input info structure, may be null.
* If mlSetModelInputInfo() was not previously called,
* some dimensions may not be specified.
* @param[out] output_info A pointer to the result output info structure, may be null.
* If mlSetModelInputInfo() was not previously called,
* some dimensions may not be specified.
*
* @return ML_OK in case of success, ML_FAIL otherwise.
* To get more details in case of failure, call mlGetLastError().
*/
ML_API_ENTRY ml_status mlGetModelInfo(ml_model model,
ml_image_info* input_info,
ml_image_info* output_info);
/**
* Updates input image information. All image dimensions must be specified.
* @note This is a heavy operation, so avoid using it frequently.
*
* @param[in] model A valid model handle.
* @param[in] info Input image information. The specified dimensions must
* match the dimensions already known by the model.
*/
ML_API_ENTRY ml_status mlSetModelInputInfo(ml_model model, ml_image_info const* info);
/**
* Gets an input image and fills an output image.
*
* @param[in] model A valid model handle.
* @param[in] input A valid input image descriptor.
* @param[in] output A valid output image descriptor.
*
* @return ML_OK in case of success, ML_FAIL otherwise.
* To get more details in case of failure, call mlGetLastError().
*/
ML_API_ENTRY ml_status mlInfer(ml_model model, ml_image input, ml_image output);
/**
* Releases a model loaded with mlCreateModel(), invalidates the handle.
*
* @param model A valid model handle.
*/
ML_API_ENTRY void mlReleaseModel(ml_model model);
/**
* Returns a null-terminated string containing the last operation error message.
* May be called after some operation returns ML_FAIL or NULL.
* The error message is owned by the library and must NOT be freed by a client.
* The message is stored in a thread local storage, so this function
* should be called from the thread where the failure occured.
*
* @param[out] size Optional, the size of the error message (excluding the null-terminator).
*
* @return A pointer to the formatted message.
*/
ML_API_ENTRY const char* mlGetLastError(size_t* size);
#ifdef __cplusplus
} // extern "C"
#endif