|
| 1 | +/* |
| 2 | + * Copyright (c) Intel Corporation |
| 3 | + * |
| 4 | + * Licensed under the BSD License (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. See the license file found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <dlfcn.h> |
| 12 | +#include <cstddef> |
| 13 | +#include <cstdint> |
| 14 | +#include <memory> |
| 15 | + |
| 16 | +namespace executorch { |
| 17 | +namespace backends { |
| 18 | +namespace openvino { |
| 19 | + |
| 20 | +// Forward declarations matching the OpenVINO C API opaque types. |
| 21 | +// Only pointer types are used so struct layout is irrelevant. |
| 22 | +typedef struct ov_core ov_core_t; |
| 23 | +typedef struct ov_compiled_model ov_compiled_model_t; |
| 24 | +typedef struct ov_infer_request ov_infer_request_t; |
| 25 | +typedef struct ov_tensor ov_tensor_t; |
| 26 | + |
| 27 | +// Value types reproduced from openvino/c/ov_shape.h and ov_common.h. |
| 28 | +// These are stable C ABI — pinned via version constraint in pyproject.toml. |
| 29 | +typedef struct { |
| 30 | + int64_t rank; |
| 31 | + int64_t* dims; |
| 32 | +} ov_shape_t; |
| 33 | + |
| 34 | +typedef struct { |
| 35 | + char** devices; |
| 36 | + size_t size; |
| 37 | +} ov_available_devices_t; |
| 38 | + |
| 39 | +// Intentionally partial — only OV_STATUS_OK is needed for success checks. |
| 40 | +// The full enum is defined in openvino/c/ov_common.h. |
| 41 | +typedef enum { |
| 42 | + OV_STATUS_OK = 0, |
| 43 | + OV_STATUS_GENERAL_ERROR = -1, |
| 44 | +} ov_status_e; |
| 45 | + |
| 46 | +// Values aligned with ov::element::Type_t (sequential enum). |
| 47 | +typedef enum { |
| 48 | + OV_ELEMENT_UNDEFINED = 0, |
| 49 | + OV_ELEMENT_BOOLEAN = 1, |
| 50 | + OV_ELEMENT_BF16 = 2, |
| 51 | + OV_ELEMENT_F16 = 3, |
| 52 | + OV_ELEMENT_F32 = 4, |
| 53 | + OV_ELEMENT_F64 = 5, |
| 54 | + OV_ELEMENT_I4 = 6, |
| 55 | + OV_ELEMENT_I8 = 7, |
| 56 | + OV_ELEMENT_I16 = 8, |
| 57 | + OV_ELEMENT_I32 = 9, |
| 58 | + OV_ELEMENT_I64 = 10, |
| 59 | + OV_ELEMENT_U1 = 11, |
| 60 | + OV_ELEMENT_U2 = 12, |
| 61 | + OV_ELEMENT_U3 = 13, |
| 62 | + OV_ELEMENT_U4 = 14, |
| 63 | + OV_ELEMENT_U6 = 15, |
| 64 | + OV_ELEMENT_U8 = 16, |
| 65 | +} ov_element_type_e; |
| 66 | + |
| 67 | +// Function pointer types for each OpenVINO C API function we use. |
| 68 | +using ov_core_create_fn = ov_status_e (*)(ov_core_t**); |
| 69 | +using ov_core_free_fn = void (*)(ov_core_t*); |
| 70 | +using ov_core_get_available_devices_fn = |
| 71 | + ov_status_e (*)(const ov_core_t*, ov_available_devices_t*); |
| 72 | +using ov_available_devices_free_fn = void (*)(ov_available_devices_t*); |
| 73 | +using ov_core_import_model_fn = ov_status_e (*)( |
| 74 | + const ov_core_t*, |
| 75 | + const char*, |
| 76 | + size_t, |
| 77 | + const char*, |
| 78 | + ov_compiled_model_t**); |
| 79 | +using ov_compiled_model_create_infer_request_fn = |
| 80 | + ov_status_e (*)(const ov_compiled_model_t*, ov_infer_request_t**); |
| 81 | +using ov_compiled_model_inputs_size_fn = |
| 82 | + ov_status_e (*)(const ov_compiled_model_t*, size_t*); |
| 83 | +using ov_compiled_model_outputs_size_fn = |
| 84 | + ov_status_e (*)(const ov_compiled_model_t*, size_t*); |
| 85 | +using ov_compiled_model_free_fn = void (*)(ov_compiled_model_t*); |
| 86 | +using ov_infer_request_set_input_tensor_by_index_fn = |
| 87 | + ov_status_e (*)(ov_infer_request_t*, size_t, const ov_tensor_t*); |
| 88 | +using ov_infer_request_set_output_tensor_by_index_fn = |
| 89 | + ov_status_e (*)(ov_infer_request_t*, size_t, const ov_tensor_t*); |
| 90 | +using ov_infer_request_infer_fn = ov_status_e (*)(ov_infer_request_t*); |
| 91 | +using ov_infer_request_free_fn = void (*)(ov_infer_request_t*); |
| 92 | +using ov_tensor_create_from_host_ptr_fn = |
| 93 | + ov_status_e (*)(ov_element_type_e, ov_shape_t, void*, ov_tensor_t**); |
| 94 | +using ov_tensor_free_fn = void (*)(ov_tensor_t*); |
| 95 | +using ov_shape_create_fn = |
| 96 | + ov_status_e (*)(int64_t, const int64_t*, ov_shape_t*); |
| 97 | +using ov_shape_free_fn = ov_status_e (*)(ov_shape_t*); |
| 98 | + |
| 99 | +struct DlCloser { |
| 100 | + void operator()(void* handle) { |
| 101 | + if (handle) { |
| 102 | + dlclose(handle); |
| 103 | + } |
| 104 | + } |
| 105 | +}; |
| 106 | +using DlHandle = std::unique_ptr<void, DlCloser>; |
| 107 | + |
| 108 | +struct OpenvinoFunctions { |
| 109 | + ov_core_create_fn core_create = nullptr; |
| 110 | + ov_core_free_fn core_free = nullptr; |
| 111 | + ov_core_get_available_devices_fn core_get_available_devices = nullptr; |
| 112 | + ov_available_devices_free_fn available_devices_free = nullptr; |
| 113 | + ov_core_import_model_fn core_import_model = nullptr; |
| 114 | + ov_compiled_model_create_infer_request_fn |
| 115 | + compiled_model_create_infer_request = nullptr; |
| 116 | + ov_compiled_model_inputs_size_fn compiled_model_inputs_size = nullptr; |
| 117 | + ov_compiled_model_outputs_size_fn compiled_model_outputs_size = nullptr; |
| 118 | + ov_compiled_model_free_fn compiled_model_free = nullptr; |
| 119 | + ov_infer_request_set_input_tensor_by_index_fn |
| 120 | + infer_request_set_input_tensor_by_index = nullptr; |
| 121 | + ov_infer_request_set_output_tensor_by_index_fn |
| 122 | + infer_request_set_output_tensor_by_index = nullptr; |
| 123 | + ov_infer_request_infer_fn infer_request_infer = nullptr; |
| 124 | + ov_infer_request_free_fn infer_request_free = nullptr; |
| 125 | + ov_tensor_create_from_host_ptr_fn tensor_create_from_host_ptr = nullptr; |
| 126 | + ov_tensor_free_fn tensor_free = nullptr; |
| 127 | + ov_shape_create_fn shape_create = nullptr; |
| 128 | + ov_shape_free_fn shape_free = nullptr; |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace openvino |
| 132 | +} // namespace backends |
| 133 | +} // namespace executorch |
0 commit comments