Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/session/model_package_api.h"
 
#include "core/common/common.h"
#include "core/framework/error_code_helper.h"
#include "core/session/inference_session.h"
#include "core/session/abi_session_options_impl.h"
#include "core/session/ort_apis.h"
#include "core/session/ort_env.h"
 
#if !defined(ORT_MINIMAL_BUILD)
#include "core/session/model_package/model_package_context.h"
#include "core/session/model_package/model_package_options.h"
#include "core/session/utils.h"
#endif
 
using namespace onnxruntime;
 
#define RETURN_NOT_IMPL_IN_MINIMAL_BUILD()          \
  return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, \
                               "Model package API is not supported in this build")
 
namespace OrtModelPackageAPI {
 
ORT_API(void, ReleaseModelPackageOptions,
        _Frees_ptr_opt_ OrtModelPackageOptions* options) {
#if !defined(ORT_MINIMAL_BUILD)
  delete reinterpret_cast<onnxruntime::ModelPackageOptions*>(options);
#else
  ORT_UNUSED_PARAMETER(options);
#endif
}
 
ORT_API_STATUS_IMPL(CreateModelPackageOptionsFromSessionOptions,
                    _In_ const OrtEnv* env,
                    _In_ const OrtSessionOptions* session_options,
                    _Outptr_ OrtModelPackageOptions** out) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (env == nullptr || session_options == nullptr || out == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "env, session_options and out must be non-null");
  }
 
  auto options = std::make_unique<onnxruntime::ModelPackageOptions>(env->GetEnvironment(), *session_options);
  *out = reinterpret_cast<OrtModelPackageOptions*>(options.release());
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(env);
  ORT_UNUSED_PARAMETER(session_options);
  ORT_UNUSED_PARAMETER(out);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API(void, ReleaseModelPackageContext,
        _Frees_ptr_opt_ OrtModelPackageContext* ctx) {
#if !defined(ORT_MINIMAL_BUILD)
  delete reinterpret_cast<onnxruntime::ModelPackageContext*>(ctx);
#else
  ORT_UNUSED_PARAMETER(ctx);
#endif
}
 
ORT_API_STATUS_IMPL(CreateModelPackageContext,
                    _In_ const ORTCHAR_T* package_root,
                    _Outptr_ OrtModelPackageContext** out) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
 
  if (package_root == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "package_root must be non-null");
  }
 
  if (out == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "out must be non-null");
  }
 
  auto ctx = std::make_unique<onnxruntime::ModelPackageContext>(std::filesystem::path{package_root});
 
  *out = reinterpret_cast<OrtModelPackageContext*>(ctx.release());
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(package_root);
  ORT_UNUSED_PARAMETER(out);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(ModelPackage_GetComponentCount,
                    _In_ const OrtModelPackageContext* ctx,
                    _Out_ size_t* out_count) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (ctx == nullptr || out_count == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ctx and out_count must be non-null");
  }
  *out_count = reinterpret_cast<const onnxruntime::ModelPackageContext*>(ctx)->GetComponentCount();
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(out_count);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(ModelPackage_GetComponentNames,
                    _In_ const OrtModelPackageContext* ctx,
                    _Outptr_result_buffer_maybenull_(*out_count) const char* const** out_names,
                    _Out_ size_t* out_count) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (ctx == nullptr || out_names == nullptr || out_count == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ctx, out_names, and out_count must be non-null");
  }
 
  gsl::span<const std::string> names;
  ORT_API_RETURN_IF_STATUS_NOT_OK(
      reinterpret_cast<const onnxruntime::ModelPackageContext*>(ctx)->GetComponentNames(names));
 
  const char* const* ptrs = nullptr;
  size_t count = 0;
  reinterpret_cast<const onnxruntime::ModelPackageContext*>(ctx)->GetComponentNamePtrs(ptrs, count);
  *out_names = ptrs;
  *out_count = count;
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(out_names);
  ORT_UNUSED_PARAMETER(out_count);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(ModelPackage_GetVariantCount,
                    _In_ const OrtModelPackageContext* ctx,
                    _In_ const char* component_name,
                    _Out_ size_t* out_count) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (ctx == nullptr || component_name == nullptr || out_count == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ctx, component_name, and out_count must be non-null");
  }
 
  size_t count = 0;
  ORT_API_RETURN_IF_STATUS_NOT_OK(
      reinterpret_cast<const onnxruntime::ModelPackageContext*>(ctx)->GetVariantCount(component_name, count));
  *out_count = count;
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(component_name);
  ORT_UNUSED_PARAMETER(out_count);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(ModelPackage_GetVariantNames,
                    _In_ const OrtModelPackageContext* ctx,
                    _In_ const char* component_name,
                    _Outptr_result_buffer_maybenull_(*out_count) const char* const** out_variant_names,
                    _Out_ size_t* out_count) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (ctx == nullptr || component_name == nullptr || out_variant_names == nullptr || out_count == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT,
                                 "ctx, component_name, out_variant_names, and out_count must be non-null");
  }
 
  gsl::span<const std::string> variant_names;
  ORT_API_RETURN_IF_STATUS_NOT_OK(
      reinterpret_cast<const onnxruntime::ModelPackageContext*>(ctx)->GetVariantNames(component_name, variant_names));
 
  const char* const* ptrs = nullptr;
  size_t count = 0;
  reinterpret_cast<const onnxruntime::ModelPackageContext*>(ctx)->GetVariantNamePtrs(component_name, ptrs, count);
  *out_variant_names = ptrs;
  *out_count = count;
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(component_name);
  ORT_UNUSED_PARAMETER(out_variant_names);
  ORT_UNUSED_PARAMETER(out_count);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(SelectComponent,
                    _In_ const OrtModelPackageContext* context,
                    _In_ const char* component_name,
                    _In_ const OrtModelPackageOptions* options,
                    _Outptr_ OrtModelPackageComponentContext** out) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (context == nullptr || component_name == nullptr || options == nullptr || out == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT,
                                 "context, component_name, options, and out must be non-null");
  }
 
  const auto* cxx_ctx = reinterpret_cast<const onnxruntime::ModelPackageContext*>(context);
  const auto* cxx_options = reinterpret_cast<const onnxruntime::ModelPackageOptions*>(options);
 
  const auto& package_info = cxx_ctx->GetModelPackageInfo();
  const ComponentInfo* component_info = nullptr;
  for (const auto& component : package_info.components) {
    if (component.component_name == component_name) {
      component_info = &component;
      break;
    }
  }
 
  if (component_info == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "Component model not found.");
  }
 
  auto cix = std::make_unique<onnxruntime::ModelPackageComponentContext>(
      component_name, *component_info, *cxx_options);
 
  ORT_API_RETURN_IF_STATUS_NOT_OK(cix->ResolveVariant());
 
  *out = reinterpret_cast<OrtModelPackageComponentContext*>(cix.release());
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(context);
  ORT_UNUSED_PARAMETER(component_name);
  ORT_UNUSED_PARAMETER(options);
  ORT_UNUSED_PARAMETER(out);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API(void, ReleaseModelPackageComponentContext,
        _Frees_ptr_opt_ OrtModelPackageComponentContext* cix) {
#if !defined(ORT_MINIMAL_BUILD)
  delete reinterpret_cast<onnxruntime::ModelPackageComponentContext*>(cix);
#else
  ORT_UNUSED_PARAMETER(cix);
#endif
}
 
ORT_API_STATUS_IMPL(ModelPackageComponent_GetSelectedVariantFolderPath,
                    _In_ const OrtModelPackageComponentContext* ctx,
                    _Outptr_ const ORTCHAR_T** folder_path) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (ctx == nullptr || folder_path == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ctx and folder_path must be non-null");
  }
 
  const auto* cxx_ctx = reinterpret_cast<const onnxruntime::ModelPackageComponentContext*>(ctx);
 
  const std::filesystem::path* folder = nullptr;
  ORT_API_RETURN_IF_STATUS_NOT_OK(cxx_ctx->GetSelectedVariantFolderPath(folder));
  ORT_API_RETURN_IF(folder == nullptr, ORT_FAIL, "Selected variant folder path is null.");
 
  *folder_path = folder->c_str();
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(folder_path);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(CreateSession,
                    _In_ const OrtEnv* env,
                    _In_ OrtModelPackageComponentContext* ctx,
                    _In_opt_ const OrtSessionOptions* session_options,
                    _Outptr_ OrtSession** session) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (env == nullptr || ctx == nullptr || session == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT,
                                 "env, ctx, and session must be non-null");
  }
 
  auto& mp_ctx = *reinterpret_cast<onnxruntime::ModelPackageComponentContext*>(ctx);
 
  // 1) Get the selected variant model file path.
  //    ORT only supports a single ONNX file per variant folder.
  std::filesystem::path selected_file_path;
  ORT_API_RETURN_IF_STATUS_NOT_OK(mp_ctx.GetSelectedVariantFilePath(selected_file_path));
 
  // 2) Pick the OrtSessionOptions per precedence rules:
  //    - session_options == nullptr (default path): start from a clean OrtSessionOptions,
  //      and merge variant-specific session + provider options from the package metadata.
  //    - session_options != nullptr (advanced path): use caller-supplied as-is, no metadata merge.
  const OrtSessionOptions* effective_options = nullptr;
  std::optional<OrtSessionOptions> effective_options_storage;
 
  if (session_options == nullptr) {
    // Start from a clean session options. The only EP-related state comes from
    // the variant metadata (session options and provider options), not from the
    // original session options used to create the package options.
    effective_options_storage.emplace();
 
    // Merge variant file session options into config options.
    gsl::span<const std::string> session_option_keys;
    gsl::span<const std::string> session_option_values;
    ORT_API_RETURN_IF_STATUS_NOT_OK(
        mp_ctx.GetSelectedVariantFileSessionOptions(session_option_keys, session_option_values));
 
    ORT_API_RETURN_IF(session_option_keys.size() != session_option_values.size(),
                      ORT_FAIL, "Session option keys/values size mismatch.");
 
    for (size_t i = 0; i < session_option_keys.size(); ++i) {
      OrtStatus* st = OrtApis::AddSessionConfigEntry(&*effective_options_storage,
                                                     session_option_keys[i].c_str(),
                                                     session_option_values[i].c_str());
      if (st != nullptr) {
        return st;
      }
    }
 
    // Merge variant file provider options as flat key/value entries for the selected EP devices.
    gsl::span<const std::string> provider_option_keys;
    gsl::span<const std::string> provider_option_values;
    ORT_API_RETURN_IF_STATUS_NOT_OK(
        mp_ctx.GetSelectedVariantFileProviderOptions(provider_option_keys, provider_option_values));
 
    ORT_API_RETURN_IF(provider_option_keys.size() != provider_option_values.size(),
                      ORT_FAIL, "Provider option keys/values size mismatch.");
 
    if (!provider_option_keys.empty()) {
      // Use ep_devices from the captured EP info for applying provider options.
      // DevicesSelected() is only populated for the policy path, but ep_infos[0].ep_devices
      // is populated for both factory and policy paths.
      const auto& ep_infos = mp_ctx.EpInfos();
      if (!ep_infos.empty() && !ep_infos[0].ep_devices.empty()) {
        std::vector<const char*> provider_option_key_ptrs;
        std::vector<const char*> provider_option_value_ptrs;
        provider_option_key_ptrs.reserve(provider_option_keys.size());
        provider_option_value_ptrs.reserve(provider_option_values.size());
 
        for (size_t i = 0; i < provider_option_keys.size(); ++i) {
          provider_option_key_ptrs.push_back(provider_option_keys[i].c_str());
          provider_option_value_ptrs.push_back(provider_option_values[i].c_str());
        }
 
        ORT_API_RETURN_IF_STATUS_NOT_OK(onnxruntime::AddEpOptionsToSessionOptions(
            gsl::span<const OrtEpDevice* const>(ep_infos[0].ep_devices.data(), ep_infos[0].ep_devices.size()),
            gsl::span<const char* const>(provider_option_key_ptrs.data(), provider_option_key_ptrs.size()),
            gsl::span<const char* const>(provider_option_value_ptrs.data(), provider_option_value_ptrs.size()),
            effective_options_storage->value));
      }
    }
 
    effective_options = &*effective_options_storage;
  } else {
    // Advanced path: use the caller-supplied options. Still carry over the variant's path-valued
    // session options (e.g. the external initializers folder the model needs to load), but only
    // for keys the caller did not set, so an explicit user value wins.
    gsl::span<const std::string> session_option_keys;
    gsl::span<const std::string> session_option_values;
    ORT_API_RETURN_IF_STATUS_NOT_OK(
        mp_ctx.GetSelectedVariantFileSessionOptions(session_option_keys, session_option_values));
    ORT_API_RETURN_IF(session_option_keys.size() != session_option_values.size(),
                      ORT_FAIL, "Session option keys/values size mismatch.");
 
    effective_options_storage.emplace(*session_options);
    const auto& existing = effective_options_storage->value.config_options.GetConfigOptionsMap();
    for (size_t i = 0; i < session_option_keys.size(); ++i) {
      if (!onnxruntime::IsModelPackagePathSessionOption(session_option_keys[i]) ||
          existing.count(session_option_keys[i]) != 0) {
        continue;
      }
      OrtStatus* st = OrtApis::AddSessionConfigEntry(&*effective_options_storage,
                                                     session_option_keys[i].c_str(),
                                                     session_option_values[i].c_str());
      if (st != nullptr) {
        return st;
      }
    }
    effective_options = &*effective_options_storage;
  }
 
  // 3) Create session with the resolved file and effective session options.
  std::unique_ptr<onnxruntime::InferenceSession> sess;
  ORT_API_RETURN_IF_ERROR(onnxruntime::CreateSessionForModelPackage(
      effective_options,
      env->GetEnvironment(),
      selected_file_path,
      mp_ctx,
      sess));
 
  // 4) Initialize.
  ORT_API_RETURN_IF_ERROR(InitializeSession(effective_options, *sess));
 
  *session = reinterpret_cast<OrtSession*>(sess.release());
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(env);
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(session_options);
  ORT_UNUSED_PARAMETER(session);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
// ---------- API table ------------------------------------------------------
 
ORT_API_STATUS_IMPL(ModelPackage_GetVariantEpName,
                    _In_ const OrtModelPackageContext* ctx,
                    _In_ const char* component_name,
                    _In_ const char* variant_name,
                    _Outptr_result_maybenull_ const char** out_ep) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (ctx == nullptr || component_name == nullptr || variant_name == nullptr || out_ep == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT,
                                 "ctx, component_name, variant_name, and out_ep must be non-null");
  }
 
  const onnxruntime::VariantEpCompatibilityInfo* info = nullptr;
  auto status = reinterpret_cast<const onnxruntime::ModelPackageContext*>(ctx)->GetVariantEpCompatibility(
      component_name, variant_name, info);
  if (!status.IsOK()) {
    return onnxruntime::ToOrtStatus(status);
  }
 
  *out_ep = (info != nullptr && info->ep.has_value()) ? info->ep->c_str() : nullptr;
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(component_name);
  ORT_UNUSED_PARAMETER(variant_name);
  ORT_UNUSED_PARAMETER(out_ep);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(ModelPackage_ResolveStringRef,
                    _In_ const OrtModelPackageContext* ctx,
                    _In_opt_ const char* base_dir,
                    _In_ const char* input,
                    _In_ int must_exist,
                    _Outptr_ const char** out_path) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (ctx == nullptr || input == nullptr || out_path == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ctx, input, and out_path must be non-null");
  }
 
  const char* resolved = nullptr;
  auto status = reinterpret_cast<const onnxruntime::ModelPackageContext*>(ctx)->ResolveStringRef(
      base_dir != nullptr ? std::string(base_dir) : std::string{}, std::string(input),
      must_exist != 0, resolved);
  if (!status.IsOK()) {
    return onnxruntime::ToOrtStatus(status);
  }
  *out_path = resolved;
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(base_dir);
  ORT_UNUSED_PARAMETER(input);
  ORT_UNUSED_PARAMETER(must_exist);
  ORT_UNUSED_PARAMETER(out_path);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(ModelPackage_GetSchemaVersion,
                    _In_ const OrtModelPackageContext* ctx,
                    _Out_ int64_t* out_version) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (ctx == nullptr || out_version == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ctx and out_version must be non-null");
  }
 
  const auto& package_info = reinterpret_cast<const onnxruntime::ModelPackageContext*>(ctx)->GetModelPackageInfo();
  *out_version = package_info.schema_version;
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(out_version);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(ModelPackageComponent_GetSelectedVariantName,
                    _In_ const OrtModelPackageComponentContext* ctx,
                    _Outptr_ const char** out_name) {
  API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD)
  if (ctx == nullptr || out_name == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "ctx and out_name must be non-null");
  }
 
  const auto* mp_ctx = reinterpret_cast<const onnxruntime::ModelPackageComponentContext*>(ctx);
  const std::string* name = nullptr;
  ORT_API_RETURN_IF_STATUS_NOT_OK(mp_ctx->GetSelectedVariantName(name));
  ORT_API_RETURN_IF(name == nullptr, ORT_FAIL, "Selected variant name is null.");
 
  *out_name = name->c_str();
  return nullptr;
#else
  ORT_UNUSED_PARAMETER(ctx);
  ORT_UNUSED_PARAMETER(out_name);
  RETURN_NOT_IMPL_IN_MINIMAL_BUILD();
#endif
  API_IMPL_END
}
 
static constexpr OrtModelPackageApi ort_model_package_api = {
    &CreateModelPackageOptionsFromSessionOptions,
    &ReleaseModelPackageOptions,
    &CreateModelPackageContext,
    &ReleaseModelPackageContext,
    &ModelPackage_GetSchemaVersion,
    &ModelPackage_GetComponentCount,
    &ModelPackage_GetComponentNames,
    &ModelPackage_GetVariantCount,
    &ModelPackage_GetVariantNames,
    &ModelPackage_GetVariantEpName,
    &ModelPackage_ResolveStringRef,
    &SelectComponent,
    &ReleaseModelPackageComponentContext,
    &ModelPackageComponent_GetSelectedVariantName,
    &ModelPackageComponent_GetSelectedVariantFolderPath,
    &CreateSession,
};
 
ORT_API(const OrtModelPackageApi*, GetModelPackageApi) {
  return &ort_model_package_api;
}
 
}  // namespace OrtModelPackageAPI