|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <filesystem> |
| 7 | +#include <fstream> |
| 8 | +#include <iterator> |
| 9 | +#include <limits> |
| 10 | +#include <memory> |
| 11 | +#include <string> |
| 12 | +#include <string_view> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#ifdef _WIN32 |
| 16 | +#include <windows.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +#include "plugin_ep_utils.h" |
| 20 | + |
| 21 | +// Sample-only EPContext data helpers. These are intentionally outside the ORT C and EP ABI. |
| 22 | +namespace ep_context_data_utils { |
| 23 | + |
| 24 | +#ifdef _WIN32 |
| 25 | +inline std::wstring Utf8ToWideString(std::string_view value) { |
| 26 | + if (value.empty() || value.size() > static_cast<size_t>(std::numeric_limits<int>::max())) { |
| 27 | + return {}; |
| 28 | + } |
| 29 | + |
| 30 | + const int wide_length = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(), |
| 31 | + static_cast<int>(value.size()), nullptr, 0); |
| 32 | + if (wide_length <= 0) { |
| 33 | + return {}; |
| 34 | + } |
| 35 | + |
| 36 | + std::wstring wide_value(static_cast<size_t>(wide_length), L'\0'); |
| 37 | + MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(), static_cast<int>(value.size()), |
| 38 | + wide_value.data(), wide_length); |
| 39 | + return wide_value; |
| 40 | +} |
| 41 | + |
| 42 | +inline std::string WideToUtf8String(std::wstring_view value) { |
| 43 | + if (value.empty() || value.size() > static_cast<size_t>(std::numeric_limits<int>::max())) { |
| 44 | + return {}; |
| 45 | + } |
| 46 | + |
| 47 | + const int utf8_length = WideCharToMultiByte(CP_UTF8, 0, value.data(), static_cast<int>(value.size()), |
| 48 | + nullptr, 0, nullptr, nullptr); |
| 49 | + if (utf8_length <= 0) { |
| 50 | + return {}; |
| 51 | + } |
| 52 | + |
| 53 | + std::string utf8_value(static_cast<size_t>(utf8_length), '\0'); |
| 54 | + WideCharToMultiByte(CP_UTF8, 0, value.data(), static_cast<int>(value.size()), utf8_value.data(), utf8_length, |
| 55 | + nullptr, nullptr); |
| 56 | + return utf8_value; |
| 57 | +} |
| 58 | +#endif |
| 59 | + |
| 60 | +inline std::filesystem::path Utf8Path(const char* path) { |
| 61 | +#ifdef _WIN32 |
| 62 | + return std::filesystem::path{Utf8ToWideString(path)}; |
| 63 | +#else |
| 64 | + return std::filesystem::path{path}; |
| 65 | +#endif |
| 66 | +} |
| 67 | + |
| 68 | +inline std::string PathToUtf8String(const std::filesystem::path& path) { |
| 69 | +#ifdef _WIN32 |
| 70 | + return WideToUtf8String(path.wstring()); |
| 71 | +#else |
| 72 | + return path.string(); |
| 73 | +#endif |
| 74 | +} |
| 75 | + |
| 76 | +inline OrtStatus* ResolveEpContextDataPath(const OrtApi& api, const char* file_name, const OrtGraph* graph, |
| 77 | + std::filesystem::path& data_path) { |
| 78 | + if (file_name == nullptr || file_name[0] == '\0') { |
| 79 | + return api.CreateStatus(ORT_INVALID_ARGUMENT, "EPContext data file name must not be empty"); |
| 80 | + } |
| 81 | + |
| 82 | + data_path = Utf8Path(file_name); |
| 83 | + if (data_path.empty()) { |
| 84 | + return api.CreateStatus(ORT_INVALID_ARGUMENT, "EPContext data file name is not a valid path"); |
| 85 | + } |
| 86 | + |
| 87 | + if (data_path.is_absolute() || graph == nullptr) { |
| 88 | + return nullptr; |
| 89 | + } |
| 90 | + |
| 91 | + const ORTCHAR_T* model_path = nullptr; |
| 92 | + RETURN_IF_ERROR(api.Graph_GetModelPath(graph, &model_path)); |
| 93 | + if (model_path == nullptr || model_path[0] == 0) { |
| 94 | + return nullptr; |
| 95 | + } |
| 96 | + |
| 97 | + data_path = std::filesystem::path{model_path}.parent_path() / data_path; |
| 98 | + return nullptr; |
| 99 | +} |
| 100 | + |
| 101 | +inline OrtStatus* ReadEpContextDataFromFile(const OrtApi& api, const char* file_name, const OrtGraph* graph, |
| 102 | + std::vector<char>& data) { |
| 103 | + data.clear(); |
| 104 | + |
| 105 | + std::filesystem::path data_path; |
| 106 | + RETURN_IF_ERROR(ResolveEpContextDataPath(api, file_name, graph, data_path)); |
| 107 | + |
| 108 | + std::ifstream input_stream(data_path, std::ios::binary); |
| 109 | + if (!input_stream) { |
| 110 | + const std::string message = "Failed to open EPContext data file for read: " + |
| 111 | + PathToUtf8String(data_path); |
| 112 | + return api.CreateStatus(ORT_FAIL, message.c_str()); |
| 113 | + } |
| 114 | + |
| 115 | + data.assign(std::istreambuf_iterator<char>{input_stream}, std::istreambuf_iterator<char>{}); |
| 116 | + if (input_stream.bad()) { |
| 117 | + const std::string message = "Failed to read EPContext data file: " + |
| 118 | + PathToUtf8String(data_path); |
| 119 | + return api.CreateStatus(ORT_FAIL, message.c_str()); |
| 120 | + } |
| 121 | + |
| 122 | + return nullptr; |
| 123 | +} |
| 124 | + |
| 125 | +inline OrtStatus* WriteEpContextDataToFile(const OrtApi& api, const char* file_name, const OrtGraph* graph, |
| 126 | + const void* buffer, size_t buffer_size) { |
| 127 | + if (buffer == nullptr && buffer_size != 0) { |
| 128 | + return api.CreateStatus(ORT_INVALID_ARGUMENT, "EPContext data buffer must not be null for non-empty data"); |
| 129 | + } |
| 130 | + |
| 131 | + std::filesystem::path data_path; |
| 132 | + RETURN_IF_ERROR(ResolveEpContextDataPath(api, file_name, graph, data_path)); |
| 133 | + |
| 134 | + std::ofstream output_stream(data_path, std::ios::binary); |
| 135 | + if (!output_stream) { |
| 136 | + const std::string message = "Failed to open EPContext data file for write: " + |
| 137 | + PathToUtf8String(data_path); |
| 138 | + return api.CreateStatus(ORT_FAIL, message.c_str()); |
| 139 | + } |
| 140 | + |
| 141 | + if (buffer_size != 0) { |
| 142 | + if (buffer_size > static_cast<size_t>(std::numeric_limits<std::streamsize>::max())) { |
| 143 | + return api.CreateStatus(ORT_INVALID_ARGUMENT, "EPContext data buffer is too large to write"); |
| 144 | + } |
| 145 | + |
| 146 | + output_stream.write(static_cast<const char*>(buffer), static_cast<std::streamsize>(buffer_size)); |
| 147 | + if (!output_stream) { |
| 148 | + const std::string message = "Failed to write EPContext data file: " + |
| 149 | + PathToUtf8String(data_path); |
| 150 | + return api.CreateStatus(ORT_FAIL, message.c_str()); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return nullptr; |
| 155 | +} |
| 156 | + |
| 157 | +inline OrtStatus* ReadEpContextDataWithFileFallback(const OrtApi& api, const OrtEpApi& ep_api, |
| 158 | + const OrtEpContextConfig* ep_context_config, |
| 159 | + const char* file_name, const OrtGraph* graph, |
| 160 | + std::vector<char>& data) { |
| 161 | + if (file_name == nullptr || file_name[0] == '\0') { |
| 162 | + return api.CreateStatus(ORT_INVALID_ARGUMENT, "EPContext data file name must not be empty"); |
| 163 | + } |
| 164 | + |
| 165 | + OrtReadEpContextDataFunc read_func = nullptr; |
| 166 | + void* read_state = nullptr; |
| 167 | + if (ep_context_config != nullptr) { |
| 168 | + RETURN_IF_ERROR(ep_api.EpContextConfig_GetEpContextDataReadFunc(ep_context_config, &read_func, &read_state)); |
| 169 | + } |
| 170 | + |
| 171 | + if (read_func == nullptr) { |
| 172 | + return ReadEpContextDataFromFile(api, file_name, graph, data); |
| 173 | + } |
| 174 | + |
| 175 | + Ort::AllocatorWithDefaultOptions allocator; |
| 176 | + void* ep_context_data = nullptr; |
| 177 | + size_t ep_context_data_size = 0; |
| 178 | + OrtStatus* status = read_func(read_state, file_name, allocator, &ep_context_data, &ep_context_data_size); |
| 179 | + auto buffer_deleter = [&allocator](void* buffer_to_free) { |
| 180 | + if (buffer_to_free != nullptr) { |
| 181 | + allocator.Free(buffer_to_free); |
| 182 | + } |
| 183 | + }; |
| 184 | + std::unique_ptr<void, decltype(buffer_deleter)> ep_context_data_guard(ep_context_data, buffer_deleter); |
| 185 | + |
| 186 | + if (status != nullptr) { |
| 187 | + return status; |
| 188 | + } |
| 189 | + |
| 190 | + if (ep_context_data_size != 0 && ep_context_data == nullptr) { |
| 191 | + return api.CreateStatus( |
| 192 | + ORT_FAIL, "OrtReadEpContextDataFunc returned a null buffer for non-empty EPContext data"); |
| 193 | + } |
| 194 | + |
| 195 | + data.clear(); |
| 196 | + if (ep_context_data != nullptr) { |
| 197 | + const char* ep_context_data_begin = static_cast<const char*>(ep_context_data); |
| 198 | + data.assign(ep_context_data_begin, ep_context_data_begin + ep_context_data_size); |
| 199 | + } |
| 200 | + |
| 201 | + return nullptr; |
| 202 | +} |
| 203 | + |
| 204 | +inline OrtStatus* WriteEpContextDataWithFileFallback(const OrtApi& api, const OrtEpApi& ep_api, |
| 205 | + const OrtEpContextConfig* ep_context_config, |
| 206 | + const char* file_name, const OrtGraph* graph, |
| 207 | + const void* buffer, size_t buffer_size) { |
| 208 | + if (file_name == nullptr || file_name[0] == '\0') { |
| 209 | + return api.CreateStatus(ORT_INVALID_ARGUMENT, "EPContext data file name must not be empty"); |
| 210 | + } |
| 211 | + |
| 212 | + if (buffer == nullptr && buffer_size != 0) { |
| 213 | + return api.CreateStatus(ORT_INVALID_ARGUMENT, "EPContext data buffer must not be null for non-empty data"); |
| 214 | + } |
| 215 | + |
| 216 | + OrtWriteEpContextDataFunc write_func = nullptr; |
| 217 | + void* write_state = nullptr; |
| 218 | + if (ep_context_config != nullptr) { |
| 219 | + RETURN_IF_ERROR(ep_api.EpContextConfig_GetEpContextDataWriteFunc(ep_context_config, &write_func, &write_state)); |
| 220 | + } |
| 221 | + |
| 222 | + if (write_func != nullptr) { |
| 223 | + return write_func(write_state, file_name, buffer, buffer_size); |
| 224 | + } |
| 225 | + |
| 226 | + return WriteEpContextDataToFile(api, file_name, graph, buffer, buffer_size); |
| 227 | +} |
| 228 | + |
| 229 | +} // namespace ep_context_data_utils |
0 commit comments