Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/session/utils.h"
 
#include <filesystem>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
 
#include "core/framework/error_code_helper.h"
#include "core/framework/execution_provider.h"
#include "core/framework/provider_options.h"
#include "core/common/narrow.h"
#include "core/platform/env.h"
#include "core/platform/telemetry.h"
#include "core/session/abi_session_options_impl.h"
#include "core/session/environment.h"
#include "core/session/inference_session.h"
#include "core/session/inference_session_utils.h"
#include "core/session/onnxruntime_c_api.h"
#include "core/session/onnxruntime_session_options_config_keys.h"
#include "core/session/ort_apis.h"
#include "core/session/ort_env.h"
#include "core/session/onnxruntime_ep_device_ep_metadata_keys.h"
#include "core/session/model_package/model_package_context.h"
#include "core/session/model_package/model_package_options.h"
 
#if !defined(ORT_MINIMAL_BUILD)
#include "core/graph/model_editor_api_types.h"
#include "core/session/plugin_ep/ep_factory_internal.h"
#include "core/session/plugin_ep/ep_plugin_provider_interfaces.h"
#include "core/session/plugin_ep/ep_library_plugin.h"
#include "core/session/plugin_ep/ep_library_provider_bridge.h"
#include "core/session/model_compilation_options.h"
#include "core/session/provider_policy_context.h"
#endif  // !defined(ORT_MINIMAL_BUILD)
 
using namespace onnxruntime;
#if !defined(ORT_MINIMAL_BUILD)
namespace {
// temporary implementation for testing. EP to 'select' is specified in config option
Status TestAutoSelectEPsImpl(const Environment& env, InferenceSession& sess, const std::string& ep_to_select) {
  const auto& execution_devices = env.GetOrtEpDevices();
 
  // Create OrtSessionOptions for the CreateEp call.
  // Once the InferenceSession is created, its SessionOptions is the source of truth and contains all the values from
  // the user provided OrtSessionOptions. We do a copy for simplicity. The OrtSessionOptions instance goes away
  // once we exit this function.
  auto& session_options = sess.GetMutableSessionOptions();
  OrtSessionOptions ort_so;
  ort_so.value = session_options;
  const auto& session_logger = sess.GetLogger();
  const OrtLogger& api_session_logger = *session_logger->ToExternal();
 
  for (const auto* ep_device : execution_devices) {
    if (ep_device->ep_name != ep_to_select) {
      continue;
    }
 
    // get internal factory if available.
    EpFactoryInternal* internal_factory = env.GetEpFactoryInternal(ep_device->ep_factory);
 
    // in the real implementation multiple devices can be assigned to an EP
    // in our current test-able setup it's 1:1
    std::vector<const OrtHardwareDevice*> devices{ep_device->device};
    std::vector<const OrtKeyValuePairs*> ep_metadata{&ep_device->ep_metadata};
 
    // add ep_options to SessionOptions with prefix.
    // preserve any user provided values.
    const std::string ep_options_prefix = OrtSessionOptions::GetProviderOptionPrefix(ep_device->ep_name.c_str());
    for (const auto& [key, value] : ep_device->ep_options.Entries()) {
      auto prefixed_key = ep_options_prefix + key;
      if (session_options.config_options.configurations.count(key) == 0) {
        // add the default value with prefix
        ORT_RETURN_IF_ERROR(session_options.config_options.AddConfigEntry(prefixed_key.c_str(), value.c_str()));
      }
    }
 
    std::unique_ptr<IExecutionProvider> ep;
 
    if (internal_factory) {
      // this is a factory we created and registered. internal or provider bridge EP.
      ORT_RETURN_IF_ERROR(ToStatusAndRelease(internal_factory->CreateIExecutionProvider(
          devices.data(), ep_metadata.data(), devices.size(), &ort_so, &api_session_logger, &ep)));
    } else {
      // in the real setup we need an IExecutionProvider wrapper implementation that uses the OrtEp internally,
      // and we would add that IExecutionProvider to the InferenceSession.
      ORT_NOT_IMPLEMENTED("IExecutionProvider that wraps OrtEp has not been implemented.");
 
      /*
      OrtEp* api_ep = nullptr;
      ORT_RETURN_IF_ERROR(ToStatusAndRelease(ep_device->ep_factory->CreateEp(
          ep_device->ep_factory, devices.data(), ep_metadata.data(), devices.size(),
          &ort_so, &api_session_logger, &api_ep)));
      */
    }
 
    ORT_RETURN_IF_ERROR(sess.RegisterExecutionProvider(std::move(ep)));
 
    // once we have the EP and one device that's enough for test purposes.
    break;
  }
 
  return Status::OK();
}
 
Status GetCustomOpDomainsFromEpDevice(const OrtEpDevice& ep_device, InlinedVector<OrtCustomOpDomain*>& domains_out) {
  InlinedVector<OrtCustomOpDomain*> domains{};
 
  // Get custom op domain provided by EP factory if any.
  // OrtEpFactory::GetNumCustomOpDomains and OrtEpFactory::GetCustomOpDomains were added in ORT 1.24.
  OrtEpFactory* ep_factory = ep_device.ep_factory;
  if (ep_factory &&
      ep_factory->ort_version_supported >= 24 &&
      ep_factory->GetNumCustomOpDomains != nullptr &&
      ep_factory->GetCustomOpDomains != nullptr) {
    size_t num_domains = 0;
    ORT_RETURN_IF_ERROR(ToStatusAndRelease(ep_factory->GetNumCustomOpDomains(ep_factory, &num_domains)));
 
    domains.resize(num_domains);
    ORT_RETURN_IF_ERROR(ToStatusAndRelease(ep_factory->GetCustomOpDomains(ep_factory, domains.data(),
                                                                          domains.size())));
  }
 
  domains_out = std::move(domains);
  return Status::OK();
}
 
bool DoesDomainWithNameExist(const std::string& domain_name, gsl::span<const OrtCustomOpDomain* const> domains) {
  for (auto ptr : domains) {
    if (domain_name == ptr->domain_) {
      return true;
    }
  }
  return false;
}
 
bool ShouldAddDomain(const OrtCustomOpDomain* domain_to_add,
                     gsl::span<const OrtCustomOpDomain* const> existing_domains) {
  if (!domain_to_add) {
    return false;
  }
 
  if (domain_to_add->custom_ops_.size() == 0) {
    LOGS_DEFAULT(WARNING) << "Skipping custom op domain '" << domain_to_add->domain_
                          << "': custom ops is empty.";
    return false;
  }
 
  if (DoesDomainWithNameExist(domain_to_add->domain_, existing_domains)) {
    LOGS_DEFAULT(WARNING) << "Skipping custom op domain '" << domain_to_add->domain_
                          << "': domain already exists in session options.";
    return false;
  }
 
  return true;
}
}  // namespace
#endif  // !defined(ORT_MINIMAL_BUILD)
 
common::Status CopyStringToOutputArg(std::string_view str, const char* err_msg, char* out, size_t* size) {
  if (size == nullptr) {
    return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "`size` argument is NULL");
  }
 
  const size_t str_len = str.size();
  const size_t req_size = str_len + 1;
 
  if (out == nullptr) {  // User is querying the total output buffer size
    *size = req_size;
    return onnxruntime::common::Status::OK();
  }
 
  if (*size >= req_size) {  // User provided a buffer of sufficient size
    std::memcpy(out, str.data(), str_len);
    out[str_len] = '\0';
    *size = req_size;
    return onnxruntime::common::Status::OK();
  }
 
  // User has provided a buffer that is not large enough
  *size = req_size;
  return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, err_msg);
}
 
static Status CreateAndRegisterExecutionProviders(_In_ const OrtSessionOptions* options,
                                                  _In_ onnxruntime::InferenceSession& sess) {
  const logging::Logger* session_logger = sess.GetLogger();
  ORT_ENFORCE(session_logger != nullptr,
              "Session logger is invalid, but should have been initialized during session construction.");
 
  const bool has_provider_factories = options != nullptr && !options->provider_factories.empty();
 
  if (has_provider_factories) {
    std::vector<std::unique_ptr<IExecutionProvider>> provider_list;
    for (auto& factory : options->provider_factories) {
      auto provider = factory->CreateProvider(*options, *session_logger->ToExternal());
      provider_list.push_back(std::move(provider));
    }
    // register the providers
    for (auto& provider : provider_list) {
      if (provider) {
        ORT_RETURN_IF_ERROR(sess.RegisterExecutionProvider(std::move(provider)));
      }
    }
  }
#if !defined(ORT_MINIMAL_BUILD)
  else {
    // TEMPORARY for testing. Manually specify the EP to select.
    auto auto_select_ep_name = sess.GetSessionOptions().config_options.GetConfigEntry("test.ep_to_select");
    if (auto_select_ep_name) {
      ORT_RETURN_IF_ERROR(TestAutoSelectEPsImpl(sess.GetEnvironment(), sess, *auto_select_ep_name));
    }
 
    // if there are no providers registered, and there's an ep selection policy set, do auto ep selection.
    // note: the model has already been loaded so model metadata should be available to the policy delegate callback.
    if (options != nullptr && options->value.ep_selection_policy.enable) {
      ProviderPolicyContext context;
      ORT_RETURN_IF_ERROR(context.SelectEpsForSession(sess.GetEnvironment(), *options, sess));
    }
  }
#endif  // !defined(ORT_MINIMAL_BUILD)
 
  return Status::OK();
}
 
// Internal function that creates an InferenceSession and loads a single model.
// Caller should provide either model_path, or model_data + model_data_length.
static OrtStatus* CreateSessionAndLoadSingleModelImpl(_In_ const OrtSessionOptions* options,
                                                      const onnxruntime::Environment& env,
                                                      _In_opt_z_ const ORTCHAR_T* model_path,
                                                      _In_opt_ const void* model_data,
                                                      size_t model_data_length,
                                                      std::unique_ptr<onnxruntime::InferenceSession>& sess) {
  // quick check here to decide load path. InferenceSession will provide error message for invalid values.
  // TODO: Could move to a helper
  const Env& os_env = Env::Default();  // OS environment (!= ORT environment)
  bool load_config_from_model =
      os_env.GetEnvironmentVar(inference_session_utils::kOrtLoadConfigFromModelEnvVar) == "1";
 
  // Check EPContext model generation options when the input model is loaded from memory (no input model path).
  if (options && model_path == nullptr) {
    epctx::ModelGenOptions ep_ctx_gen_options = options->value.GetEpContextGenerationOptions();
 
    if (ep_ctx_gen_options.enable) {
      auto* output_model_path = ep_ctx_gen_options.TryGetOutputModelPath();
 
      // If the user does not provide an output model location, ORT normally generates an output model file path based
      // on the input model's path (i.e., replace ".onnx" with "_ctx.onnx"). However, because there is no input model
      // path, we require the application to explicitly set the output model's location.
      //
      // Note: This is checked by the OrtCompileApi's CompileModel() function, but we check again here in case
      // the user used the older SessionOptions' configuration entries to generate a compiled model.
      if (!ep_ctx_gen_options.HasOutputModelLocation() ||               // No output model location (file, buffer, etc.)
          (output_model_path != nullptr && output_model_path->empty())  // Has an output file, but it is empty.
      ) {
        return OrtApis::CreateStatus(ORT_FAIL,
                                     "Inference session with a model loaded from bytes was configured with EPContext "
                                     "model generation enabled but without a valid location (e.g., file or buffer) "
                                     "for the output model. Please specify a valid ep.context_file_path via "
                                     "SessionOption configs or use the OrtCompileApi to compile a model to a "
                                     "file or buffer.");
      }
    }
  }
 
  int32_t model_data_length_int = 0;
  if (model_path == nullptr) {
    ORT_API_RETURN_IF(model_data == nullptr, ORT_INVALID_ARGUMENT, "Model data pointer is null.");
    ORT_API_RETURN_IF(model_data_length > static_cast<size_t>(std::numeric_limits<int32_t>::max()),
                      ORT_INVALID_ARGUMENT, "Model data size exceeds maximum supported size (2GB).");
    model_data_length_int = narrow<int32_t>(model_data_length);
  }
 
  if (load_config_from_model) {
#if !defined(ORT_MINIMAL_BUILD)
    if (model_path != nullptr) {
      sess = std::make_unique<onnxruntime::InferenceSession>(
          options == nullptr ? onnxruntime::SessionOptions() : options->value,
          env,
          model_path);
    } else {
      sess = std::make_unique<onnxruntime::InferenceSession>(
          options == nullptr ? onnxruntime::SessionOptions() : options->value,
          env,
          model_data, model_data_length_int);
    }
#else
    return OrtApis::CreateStatus(ORT_FAIL, "Loading config from ONNX models is not supported in this build.");
#endif
  } else {
    sess = std::make_unique<onnxruntime::InferenceSession>(
        options == nullptr ? onnxruntime::SessionOptions() : options->value,
        env);
  }
 
#if !defined(ORT_MINIMAL_BUILD) || defined(ORT_MINIMAL_BUILD_CUSTOM_OPS)
  // Add custom domains
  if (options && !options->custom_op_domains_.empty()) {
    ORT_API_RETURN_IF_STATUS_NOT_OK(sess->AddCustomOpDomains(options->custom_op_domains_));
  }
#endif
 
#if !defined(ORT_MINIMAL_BUILD)
  // Add custom domains for all OrtEpDevice instances to inference session.
  // The custom domains should be registered before model load for ORT to validate the custom ops.
  if (options != nullptr &&
      options->provider_factories.empty() &&
      options->value.ep_selection_policy.enable) {
    InlinedVector<OrtCustomOpDomain*> all_ep_custom_op_domains;
 
    for (const OrtEpDevice* ep_device : env.GetOrtEpDevices()) {
      InlinedVector<OrtCustomOpDomain*> domains;
      ORT_API_RETURN_IF_STATUS_NOT_OK(GetCustomOpDomainsFromEpDevice(*ep_device, domains));
 
      for (auto domain : domains) {
        // Multiple EP devices can expose the same custom-op domain. Keep one copy
        // in this session's collection before its custom registry is created.
        if (ShouldAddDomain(domain, options->custom_op_domains_) &&
            !DoesDomainWithNameExist(domain->domain_, all_ep_custom_op_domains)) {
          all_ep_custom_op_domains.push_back(domain);
        }
      }
    }
 
    if (!all_ep_custom_op_domains.empty()) {
      ORT_API_RETURN_IF_STATUS_NOT_OK(sess->AddCustomOpDomains(all_ep_custom_op_domains));
    }
  }
#endif
 
  // Finish load
  if (load_config_from_model) {
#if !defined(ORT_MINIMAL_BUILD)
    ORT_API_RETURN_IF_STATUS_NOT_OK(sess->Load());
#endif
  } else {
    if (model_path != nullptr) {
      ORT_API_RETURN_IF_STATUS_NOT_OK(sess->Load(model_path));
    } else {
      ORT_API_RETURN_IF_STATUS_NOT_OK(sess->Load(model_data, model_data_length_int));
    }
  }
 
  return nullptr;
}
 
// Internal function that creates an InferenceSession and loads the model.
// Caller should provide either a model file path, or model_data + model_data_length.
static OrtStatus* CreateSessionAndLoadModelImpl(_In_ const OrtSessionOptions* options,
                                                const onnxruntime::Environment& env,
                                                _In_opt_z_ const ORTCHAR_T* model_path,
                                                _In_opt_ const void* model_data,
                                                size_t model_data_length,
                                                std::unique_ptr<onnxruntime::InferenceSession>& sess) {
  if (model_path != nullptr) {
    std::error_code ec;
    if (std::filesystem::is_directory(model_path, ec) && !ec) {
      return OrtApis::CreateStatus(
          ORT_INVALID_ARGUMENT,
          "The model path is a directory. Loading a model package from a directory path is not supported. "
          "Use the model package API (CreateModelPackageContext, SelectComponent, CreateSession) instead.");
    }
  }
  return CreateSessionAndLoadSingleModelImpl(options, env, model_path, model_data, model_data_length, sess);
}
 
#if !defined(ORT_MINIMAL_BUILD)
// Overload of CreateSessionAndLoadModelImpl that takes an OrtModel* directly.
// This ensures load-path parity with file/buffer inputs by running the same checks
// (ORT_LOAD_CONFIG_FROM_MODEL, EP-context output validation, custom domain wiring).
static OrtStatus* CreateSessionAndLoadModelImpl(_In_ const OrtSessionOptions* options,
                                                const onnxruntime::Environment& env,
                                                _In_ const OrtModel* model,
                                                std::unique_ptr<onnxruntime::InferenceSession>& sess) {
  if (model == nullptr) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "OrtModel pointer is null");
  }
 
  // Check EPContext model generation options - OrtModel has no file path by default,
  // so we need explicit output location or embedded model path.
  if (options) {
    epctx::ModelGenOptions ep_ctx_gen_options = options->value.GetEpContextGenerationOptions();
 
    if (ep_ctx_gen_options.enable) {
      auto* output_model_path = ep_ctx_gen_options.TryGetOutputModelPath();
 
      // Check if OrtModel has a model_path set
      bool has_model_path = false;
      if (model->graph) {
        const ORTCHAR_T* model_path_cstr = model->graph->GetModelPath();
        has_model_path = model_path_cstr && model_path_cstr[0] != ORT_TSTR('\0');
      }
 
      // If there's no model path and no output location, fail early
      if (!has_model_path &&
          (!ep_ctx_gen_options.HasOutputModelLocation() ||
           (output_model_path != nullptr && output_model_path->empty()))) {
        return OrtApis::CreateStatus(ORT_FAIL,
                                     "OrtModel has no model_path set and no valid output location was specified "
                                     "for EPContext model generation. "
                                     "SetOutputModelPath/SetOutputModelBuffer, or set the model_path on the "
                                     "OrtGraph before adding it to OrtModel.");
      }
    }
  }
 
  sess = std::make_unique<onnxruntime::InferenceSession>(
      options == nullptr ? onnxruntime::SessionOptions() : options->value,
      env);
 
#if !defined(ORT_MINIMAL_BUILD) || defined(ORT_MINIMAL_BUILD_CUSTOM_OPS)
  // Add custom domains
  if (options && !options->custom_op_domains_.empty()) {
    ORT_API_RETURN_IF_STATUS_NOT_OK(sess->AddCustomOpDomains(options->custom_op_domains_));
  }
#endif
 
#if !defined(ORT_MINIMAL_BUILD)
  // Add custom domains for all OrtEpDevice instances to inference session.
  // The custom domains should be registered before model load for ORT to validate the custom ops.
  // This mirrors the same block in the file/buffer overload to maintain load-path parity.
  if (options != nullptr &&
      options->provider_factories.empty() &&
      options->value.ep_selection_policy.enable) {
    InlinedVector<OrtCustomOpDomain*> all_ep_custom_op_domains;
 
    for (const OrtEpDevice* ep_device : env.GetOrtEpDevices()) {
      InlinedVector<OrtCustomOpDomain*> domains;
      ORT_API_RETURN_IF_STATUS_NOT_OK(GetCustomOpDomainsFromEpDevice(*ep_device, domains));
 
      for (auto domain : domains) {
        // Multiple EP devices can expose the same custom-op domain. Keep one copy
        // in this session's collection before its custom registry is created.
        if (ShouldAddDomain(domain, options->custom_op_domains_) &&
            !DoesDomainWithNameExist(domain->domain_, all_ep_custom_op_domains)) {
          all_ep_custom_op_domains.push_back(domain);
        }
      }
    }
 
    if (!all_ep_custom_op_domains.empty()) {
      ORT_API_RETURN_IF_STATUS_NOT_OK(sess->AddCustomOpDomains(all_ep_custom_op_domains));
    }
  }
#endif  // !defined(ORT_MINIMAL_BUILD)
 
  // Load from OrtModel
  ORT_API_RETURN_IF_STATUS_NOT_OK(sess->Load(*model));
 
  return nullptr;
}
#endif  // !defined(ORT_MINIMAL_BUILD)
 
// Creates an InferenceSession and loads the model.
// Caller should provide either model_path, or modal_data + model_data_length.
OrtStatus* CreateSessionAndLoadModel(_In_ const OrtSessionOptions* options,
                                     _In_ const OrtEnv* env,
                                     _In_opt_z_ const ORTCHAR_T* model_path,
                                     _In_opt_ const void* model_data,
                                     size_t model_data_length,
                                     std::unique_ptr<onnxruntime::InferenceSession>& sess) {
  return CreateSessionAndLoadModelImpl(options, env->GetEnvironment(), model_path, model_data, model_data_length, sess);
}
 
#if !defined(ORT_MINIMAL_BUILD)
static const char* GetCompatibilityStatusString(OrtCompiledModelCompatibility status) {
  switch (status) {
    case OrtCompiledModelCompatibility_EP_SUPPORTED_OPTIMAL:
      return "SUPPORTED_OPTIMAL";
    case OrtCompiledModelCompatibility_EP_SUPPORTED_PREFER_RECOMPILATION:
      return "SUPPORTED_PREFER_RECOMPILATION";
    case OrtCompiledModelCompatibility_EP_UNSUPPORTED:
      return "UNSUPPORTED";
    case OrtCompiledModelCompatibility_EP_NOT_APPLICABLE:
      return "NOT_APPLICABLE";
    default:
      return "UNKNOWN";
  }
}
 
static Status ValidateCompiledModelCompatibility(InferenceSession& sess) {
  // Get model metadata
  auto [status, model_metadata] = sess.GetModelMetadata();
  if (!status.IsOK() || !model_metadata) {
    // No metadata available, skip validation
    return Status::OK();
  }
 
  const auto& custom_metadata = model_metadata->custom_metadata_map;
  if (custom_metadata.empty()) {
    // No custom metadata available, skip validation
    return Status::OK();
  }
 
  // Check if user wants to fail on suboptimal models
  bool fail_on_suboptimal = sess.GetSessionOptions().config_options.GetConfigEntry(
                                kOrtSessionOptionsFailOnSuboptimalCompiledModel) == "1";
 
  const auto& registered_provider_types = sess.GetRegisteredProviderTypes();
 
  // Access the execution providers directly from the session.
  // This allows validation to run before Initialize() completes, avoiding expensive
  // graph transformations for incompatible models. EPs are fully registered at this point.
  const auto& execution_providers = sess.GetExecutionProviders();
 
  for (const auto& ep_type : registered_provider_types) {
    // Construct the full metadata key using the prefix + EP type
    const std::string metadata_key = std::string(kOrtModelMetadata_EpCompatibilityInfoPrefix) + ep_type;
 
    auto metadata_it = custom_metadata.find(metadata_key);
    if (metadata_it != custom_metadata.end()) {
      const std::string& compatibility_info = metadata_it->second;
 
      // Get the actual EP instance to call validation
      const IExecutionProvider* ep = execution_providers.Get(ep_type);
 
      if (ep != nullptr) {
        // Call the EP's validation method (virtual method with default implementation)
        OrtCompiledModelCompatibility compatibility_status;
        Status validation_result = ep->ValidateCompiledModelCompatibilityInfo(
            compatibility_info, compatibility_status);
 
        if (validation_result.IsOK()) {
          // Log the compatibility status
          const char* status_str = GetCompatibilityStatusString(compatibility_status);
          LOGS(*sess.GetLogger(), INFO)
              << "EP " << ep_type << " compiled model compatibility: " << status_str;
 
          // Enforce compatibility based on status
          switch (compatibility_status) {
            case OrtCompiledModelCompatibility_EP_NOT_APPLICABLE:
            case OrtCompiledModelCompatibility_EP_SUPPORTED_OPTIMAL:
              // Continue execution
              break;
 
            case OrtCompiledModelCompatibility_EP_UNSUPPORTED:
              // Always fail for unsupported models
              return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL,
                                     "Compiled model is not supported by execution provider: " + ep_type);
 
            case OrtCompiledModelCompatibility_EP_SUPPORTED_PREFER_RECOMPILATION:
              // Behavior depends on user setting
              if (fail_on_suboptimal) {
                return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL,
                                       "Compiled model is suboptimal for execution provider: " + ep_type +
                                           ". Recompilation recommended for better performance.");
              }
              // Otherwise continue with warning
              LOGS(*sess.GetLogger(), WARNING)
                  << "EP " << ep_type << " reports compiled model is supported but suboptimal. "
                  << "Consider recompiling for better performance.";
              break;
 
            default:
              // Handle any unknown status values
              LOGS(*sess.GetLogger(), WARNING)
                  << "EP " << ep_type << " returned unknown compatibility status: " << compatibility_status;
              break;
          }
        } else {
          // Validation failed - this should cause session initialization to fail
          return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL,
                                 "Failed to validate compiled model compatibility for EP " + ep_type +
                                     ": " + validation_result.ErrorMessage());
        }
      }
    } else {
      // No compatibility info found for this EP - normal for non-compiled models
      LOGS(*sess.GetLogger(), VERBOSE)
          << "No compiled model compatibility info found for EP " << ep_type;
    }
  }
 
  return Status::OK();
}
#endif  // !defined(ORT_MINIMAL_BUILD)
 
OrtStatus* InitializeSession(_In_ const OrtSessionOptions* options,
                             _In_ onnxruntime::InferenceSession& sess,
                             _Inout_opt_ OrtPrepackedWeightsContainer* prepacked_weights_container) {
  if (sess.GetRegisteredProviderTypes().empty()) {
    ORT_API_RETURN_IF_STATUS_NOT_OK(CreateAndRegisterExecutionProviders(options, sess));
  }
 
  if (prepacked_weights_container != nullptr) {
    ORT_API_RETURN_IF_STATUS_NOT_OK(sess.AddPrePackedWeightsContainer(
        reinterpret_cast<PrepackedWeightsContainer*>(prepacked_weights_container)));
  }
 
#if !defined(ORT_MINIMAL_BUILD)
  // Validate compiled model compatibility for all registered execution providers BEFORE Initialize().
  // This is an optimization to fail fast for incompatible models, avoiding expensive graph transformations,
  // partitioning, and kernel binding that occur during Initialize().
  // This is safe because:
  //   1. Model metadata (containing compatibility strings) is available after Load() completes.
  //   2. Compiling EPs are fully registered at this point.
  //   3. Non-compiling EPs (like CPU EP, which may be implicitly added during Initialize()) don't participate
  //      in compatibility validation - they return NOT_APPLICABLE by default.
  ORT_API_RETURN_IF_STATUS_NOT_OK(ValidateCompiledModelCompatibility(sess));
#endif  // !defined(ORT_MINIMAL_BUILD)
 
  ORT_API_RETURN_IF_STATUS_NOT_OK(sess.Initialize());
 
  return nullptr;
}
 
namespace onnxruntime {
#if !defined(ORT_MINIMAL_BUILD)
Status CompileModel(const Environment& env, const ModelCompilationOptions& model_compile_options) {
  ORT_RETURN_IF_ERROR(model_compile_options.Check());
 
  const Telemetry& telemetry_provider = Env::Default().GetTelemetryProvider();
 
  OrtSessionOptions staged_session_options = model_compile_options.GetSessionOptions();
  auto& gen_options = staged_session_options.value.ep_context_gen_options;
  const auto* external_output =
      model_compile_options.GetSessionOptions().value.ep_context_gen_options.TryGetExternalInitializerBufferInfo();
  const auto* model_output =
      model_compile_options.GetSessionOptions().value.ep_context_gen_options.TryGetOutputModelBuffer();
  void* external_buffer = nullptr;
  size_t external_size = 0;
  void* model_buffer = nullptr;
  size_t model_size = 0;
  auto free_pending_buffers = gsl::finally([&]() {
    if (external_buffer) external_output->buffer_allocator->Free(external_buffer);
    if (model_buffer) model_output->buffer_allocator->Free(model_buffer);
  });
 
  // Saving the model and subsequent session validation can fail after initializers have been serialized.
  // Stage both allocations until the entire CompileModel call succeeds, including for EPContext models.
  if (external_output) {
    auto& staged_output = std::get<epctx::ExternalInitializerBufferInfo>(gen_options.initializers_location);
    staged_output.buffer_ptr = &external_buffer;
    staged_output.buffer_size_ptr = &external_size;
  }
  if (model_output) {
    auto& staged_output = std::get<epctx::BufferHolder>(gen_options.output_model_location);
    staged_output.buffer_ptr = &model_buffer;
    staged_output.buffer_size_ptr = &model_size;
  }
 
  std::unique_ptr<onnxruntime::InferenceSession> session;
  const OrtSessionOptions* session_options = &staged_session_options;
 
  Status status;
 
  if (model_compile_options.InputModelComesFromFile()) {
    const std::filesystem::path& input_model_path = model_compile_options.GetInputModelPath();
    status = ToStatusAndRelease(CreateSessionAndLoadModelImpl(session_options, env,
                                                              input_model_path.c_str(),
                                                              nullptr, 0, session));
  } else if (model_compile_options.InputModelComesFromOrtModel()) {
    // Use the OrtModel overload of CreateSessionAndLoadModelImpl to maintain load-path parity
    // with file/buffer inputs (same checks for ORT_LOAD_CONFIG_FROM_MODEL, EP-context output, etc.)
    const OrtModel* input_model = model_compile_options.GetInputModel();
    status = ToStatusAndRelease(CreateSessionAndLoadModelImpl(session_options, env,
                                                              input_model, session));
  } else {
    status = ToStatusAndRelease(CreateSessionAndLoadModelImpl(session_options, env, nullptr,
                                                              model_compile_options.GetInputModelData(),
                                                              model_compile_options.GetInputModelDataSize(),
                                                              session));
  }
 
  if (!status.IsOK()) {
    telemetry_provider.LogCompileModelComplete(
        0,  // No session ID available
        false,
        static_cast<uint32_t>(status.Code()),
        static_cast<uint32_t>(status.Category()),
        status.ErrorMessage());
    return status;
  }
 
  // Log start event now that we have the session ID and can get registered EP types
  telemetry_provider.LogCompileModelStart(
      session->GetCurrentSessionId(),
      model_compile_options.GetInputSourceForTelemetry(),
      model_compile_options.GetOutputTargetForTelemetry(),
      model_compile_options.GetFlagsForTelemetry(),
      model_compile_options.GetGraphOptimizationLevelForTelemetry(),
      model_compile_options.GetEmbedEpContextForTelemetry(),
      model_compile_options.HasExternalInitializersFileForTelemetry(),
      session->GetRegisteredProviderTypes());
 
  status = ToStatusAndRelease(InitializeSession(session_options, *session));
 
  telemetry_provider.LogCompileModelComplete(
      session->GetCurrentSessionId(),
      status.IsOK(),
      status.IsOK() ? 0 : static_cast<uint32_t>(status.Code()),
      status.IsOK() ? 0 : static_cast<uint32_t>(status.Category()),
      status.IsOK() ? "" : status.ErrorMessage());
 
  if (status.IsOK()) {
    if (external_output) {
      *external_output->buffer_ptr = std::exchange(external_buffer, nullptr);
      *external_output->buffer_size_ptr = external_size;
    }
    if (model_output) {
      *model_output->buffer_ptr = std::exchange(model_buffer, nullptr);
      *model_output->buffer_size_ptr = model_size;
    }
  }
 
  return status;
}
 
Status LoadPluginOrProviderBridge(const std::string& registration_name,
                                  const ORTCHAR_T* library_path,
                                  std::unique_ptr<EpLibrary>& ep_library,
                                  std::vector<EpFactoryInternal*>& internal_factories) {
  // If the `library_path` is absolute, use it as-is. Otherwise follow the precedent of ProviderLibrary::Load and make
  // it absolute by combining it with the OnnxRuntime location.
  std::filesystem::path resolved_library_path{library_path};
 
  if (!resolved_library_path.is_absolute()) {
    resolved_library_path = Env::Default().GetRuntimePath() / std::move(resolved_library_path);
  }
 
  // if it's a provider bridge library we need to create ProviderLibrary first to ensure the dependencies are loaded
  // like the onnxruntime_provider_shared library.
  auto provider_library = std::make_unique<ProviderLibrary>(resolved_library_path.native().c_str(),
                                                            true,
                                                            ProviderLibraryPathType::Absolute);
  bool is_provider_bridge = provider_library->Load() == Status::OK();  // library has GetProvider
  LOGS_DEFAULT(INFO) << "Loading EP library: " << resolved_library_path
                     << (is_provider_bridge ? " as a provider bridge" : " as a plugin");
 
  // create EpLibraryPlugin to ensure CreateEpFactories and ReleaseEpFactory are available
  auto ep_library_plugin = std::make_unique<EpLibraryPlugin>(registration_name, resolved_library_path);
  ORT_RETURN_IF_ERROR(ep_library_plugin->Load());
 
  if (is_provider_bridge) {
    // wrap the EpLibraryPlugin with EpLibraryProviderBridge to add to directly create an IExecutionProvider
    auto ep_library_provider_bridge = std::make_unique<EpLibraryProviderBridge>(std::move(provider_library),
                                                                                std::move(ep_library_plugin),
                                                                                resolved_library_path);
    ORT_RETURN_IF_ERROR(ep_library_provider_bridge->Load());
    internal_factories = ep_library_provider_bridge->GetInternalFactories();
    ep_library = std::move(ep_library_provider_bridge);
  } else {
    ep_library = std::move(ep_library_plugin);
  }
 
  return Status::OK();
}
 
Status CreateIExecutionProviderFactoryForEpDevices(const Environment& env,
                                                   gsl::span<const OrtEpDevice* const> ep_devices,
                                                   /*output*/ std::unique_ptr<IExecutionProviderFactory>& out) {
  if (ep_devices.empty()) {
    return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT,
                           "Must provide one or more OrtEpDevice instances.");
  }
 
  const auto& ep_name = ep_devices[0]->ep_name;
  OrtEpFactory* ep_factory = ep_devices[0]->ep_factory;
  bool all_match = std::all_of(ep_devices.begin() + 1, ep_devices.end(),
                               [&ep_name, &ep_factory](const OrtEpDevice* ep_device) {
                                 return (ep_device->ep_name == ep_name) && (ep_device->ep_factory == ep_factory);
                               });
  if (!all_match) {
    return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT,
                           "All OrtEpDevice values in ep_devices must have the same execution provider.");
  }
 
  EpFactoryInternal* internal_factory = env.GetEpFactoryInternal(ep_factory);
 
  if (internal_factory) {
    out = std::make_unique<InternalExecutionProviderFactory>(*internal_factory, ep_devices);
  } else {
    out = std::make_unique<PluginExecutionProviderFactory>(*ep_factory, ep_devices);
  }
 
  return Status::OK();
}
 
Status AddEpOptionsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devices,
                                    gsl::span<const char* const> ep_option_keys,
                                    gsl::span<const char* const> ep_option_vals,
                                    SessionOptions& session_options) {
  const size_t num_ep_options = ep_option_keys.size();
  if (ep_option_vals.size() != num_ep_options) {
    return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT,
                           "Must provide the same number of keys and values for EP options.");
  }
 
  for (const OrtEpDevice* ep_device : ep_devices) {
    // add the options to the session options with the EP prefix.
    // first add the default values with prefix followed by user specified values so those win
    const std::string prefix = OrtSessionOptions::GetProviderOptionPrefix(ep_device->ep_name.c_str());
    auto& config_options = session_options.config_options;
    for (const auto& [key, value] : ep_device->ep_options.Entries()) {
      ORT_RETURN_IF_ERROR(config_options.AddConfigEntry((prefix + key).c_str(), value.c_str()));
    }
 
    for (size_t j = 0; j < num_ep_options; ++j) {
      if (ep_option_keys[j] == nullptr) {
        continue;
      }
 
      ORT_RETURN_IF_ERROR(config_options.AddConfigEntry((prefix + ep_option_keys[j]).c_str(), ep_option_vals[j]));
    }
  }
 
  return Status::OK();
}
 
Status AddEpCustomDomainsToSessionOptions(gsl::span<const OrtEpDevice* const> ep_devices,
                                          OrtSessionOptions& ort_session_options) {
  for (const OrtEpDevice* ep_device : ep_devices) {
    // Add custom domains if EP factory has any.
    InlinedVector<OrtCustomOpDomain*> domains;
    ORT_RETURN_IF_ERROR(GetCustomOpDomainsFromEpDevice(*ep_device, domains));
 
    for (auto domain : domains) {
      if (ShouldAddDomain(domain, ort_session_options.custom_op_domains_)) {
        ort_session_options.custom_op_domains_.push_back(domain);
      }
    }
  }
 
  return Status::OK();
}
 
Status PrintAvailableAndSelectedEpInfos(const Environment& env, std::vector<VariantSelectionEpInfo>& ep_infos) {
  const auto& execution_devices = env.GetOrtEpDevices();
 
  std::string available_eps_info = "Available EPs and devices:\n";
  if (execution_devices.empty()) {
    available_eps_info += "  (none)\n";
  } else {
    for (const auto* ep_device : execution_devices) {
      if (ep_device == nullptr) {
        continue;
      }
 
      available_eps_info += "  " + ep_device->ToString() + "\n";
    }
  }
 
  std::string selected_eps_info = "Selected EPs:\n";
  if (ep_infos.empty()) {
    selected_eps_info += "  (none)\n";
  } else {
    for (const auto& ep_info : ep_infos) {
      selected_eps_info += "  EP: " + ep_info.ep_name + "\n";
      const auto& selected_ep_devices = ep_info.ep_devices;
      for (const auto* selected_ep_device : selected_ep_devices) {
        if (selected_ep_device == nullptr) {
          continue;
        }
        selected_eps_info += "    " + selected_ep_device->ToString() + "\n";
      }
    }
  }
 
  LOGS_DEFAULT(INFO) << available_eps_info;
  LOGS_DEFAULT(INFO) << selected_eps_info;
  return Status::OK();
}
 
// Gets EP info needed for model package workflow to select suitable model.
//
// For simplicity, there are some constraints in this initial implementation:
// - Only one EP is supported, skip ORT CPU EP.
// - All devices should be supported by the same EP
//
Status GetVariantSelectionEpInfo(std::vector<std::unique_ptr<IExecutionProvider>>& provider_list,
                                 std::vector<VariantSelectionEpInfo>& ep_infos) {
  if (provider_list.empty()) {
    return Status::OK();
  }
 
  // Use the first provider in the list for variant selection.
  auto& provider = provider_list[0];
  if (!provider) {
    return Status::OK();
  }
 
  ep_infos.push_back(VariantSelectionEpInfo{});
  auto& ep_info = ep_infos.back();
 
  // Add ep name to ep_info
  ep_info.ep_name = provider->Type();
  ORT_ENFORCE(!ep_info.ep_name.empty(), "EP name should have been set at this point.");
 
  // CPU is built-in and needs no device/factory metadata for variant selection.
  if (ep_info.ep_name == onnxruntime::kCpuExecutionProvider) {
    return Status::OK();
  }
 
  // Add ep devices to ep_info
  auto& ep_devices = provider->GetEpDevices();
  ep_info.ep_devices = ep_devices;
 
  // Add ep factory to ep_info
  ep_info.ep_factory = ep_devices.empty() ? nullptr : ep_devices.front()->ep_factory;
 
  // Add hardware devices to ep_info
  ep_info.hardware_devices.reserve(ep_devices.size());
  for (const auto& ep_device : ep_devices) {
    if (ep_device->device != nullptr) {
      ep_info.hardware_devices.push_back(ep_device->device);
    }
  }
 
  // Add ep metadata to ep_info
  ep_info.ep_metadata.reserve(ep_devices.size());
  for (const auto& ep_device : ep_devices) {
    ep_info.ep_metadata.push_back(&ep_device->ep_metadata);
  }
 
  return Status::OK();
}
 
// Create session for model package workflow.
//
// Preconditions: caller has already
//   1. resolved EP selection  -> provider_list (owns the IExecutionProvider instances),
//   2. selected a model variant -> selected_model_path.
//
// This function:
//   a. creates and loads an InferenceSession for selected_model_path,
//   b. registers the providers from provider_list (moves them into the session),
//   c. optionally logs auto-EP-selection telemetry when from_policy is true.
OrtStatus* CreateSessionForModelPackage(_In_ const OrtSessionOptions* options,
                                        const onnxruntime::Environment& env,
                                        const std::filesystem::path& selected_model_path,
                                        onnxruntime::ModelPackageComponentContext& model_package_context,
                                        std::unique_ptr<onnxruntime::InferenceSession>& sess) {
  // The model is loaded from selected_model_path. Any path-valued options (e.g. the external
  // initializers folder via session.model_external_initializers_file_folder_path) were already
  // resolved and merged into `options` by the caller.
  std::unique_ptr<OrtSessionOptions> default_options;
  const OrtSessionOptions* options_to_use = options;
  if (options_to_use == nullptr) {
    // Caller did not pass options: synthesize a default OrtSessionOptions so the downstream
    // *options_to_use dereferences are safe.
    default_options = std::make_unique<OrtSessionOptions>();
    options_to_use = default_options.get();
  }
 
  ORT_API_RETURN_IF_ERROR(CreateSessionAndLoadSingleModelImpl(options_to_use, env,
                                                              selected_model_path.c_str(),
                                                              /*model_data*/ nullptr,
                                                              /*model_data_length*/ 0,
                                                              sess));
 
  // Providers were created earlier from the original options; rebuild now so
  // any merged variant-specific provider options take effect.
  ORT_API_RETURN_IF_STATUS_NOT_OK(model_package_context.RebuildProviderListForSession(env, *options_to_use));
 
  auto& provider_list = model_package_context.MutableProviderList();
 
  for (auto& provider : provider_list) {
    if (provider) {
      ORT_API_RETURN_IF_STATUS_NOT_OK(sess->RegisterExecutionProvider(std::move(provider)));
    }
  }
 
  if (model_package_context.IsFromPolicy()) {
    ProviderPolicyContext provider_policy_context;
    ORT_API_RETURN_IF_STATUS_NOT_OK(provider_policy_context.LogTelemetry(
        *sess, *options_to_use,
        model_package_context.ExecutionDevices(),
        model_package_context.DevicesSelected()));
  }
 
  return nullptr;
}
 
#endif  // !defined(ORT_MINIMAL_BUILD)
}  // namespace onnxruntime