Исходный код СУБД 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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
#include "adapter/pch.h"
 
#include "winml_adapter_model.h"
 
#include "winml_adapter_c_api.h"
#include "core/graph/onnx_protobuf.h"
#include "core/session/ort_apis.h"
#include "winml_adapter_apis.h"
#include "core/framework/error_code_helper.h"
#include "core/common/common.h"
#include "core/common/narrow.h"
 
#include <io.h>
#include <fcntl.h>
#include <limits>
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "core/framework/onnxruntime_typeinfo.h"
 
#include "onnx/defs/schema.h"
#include "core/framework/tensor_type_and_shape.h"
 
#include "onnx/onnx-ml.pb.h"
 
namespace winmla = Windows::AI::MachineLearning::Adapter;
 
static std::vector<const char*> GetInitializers(const ONNX_NAMESPACE::ModelProto& model_proto) {
  std::vector<const char*> initializers;
  auto& graph = model_proto.graph();
  auto& graph_initializers = graph.initializer();
  for (auto& initializer : graph_initializers) {
    initializers.push_back(initializer.name().c_str());
  }
  return initializers;
}
 
static std::vector<const ONNX_NAMESPACE::ValueInfoProto*> GetInputsWithoutInitializers(
  const ONNX_NAMESPACE::ModelProto& model_proto
) {
  auto initializers = GetInitializers(model_proto);
 
  std::vector<const ONNX_NAMESPACE::ValueInfoProto*> inputs_without_initializers;
  auto& graph = model_proto.graph();
  auto& inputs = graph.input();
  for (auto& input : inputs) {
    if (input.has_name() && input.has_type()) {
      auto found_it = std::find_if(std::begin(initializers), std::end(initializers), [&](auto& initializer) {
        return std::strcmp(initializer, input.name().c_str()) == 0;
      });
 
      auto is_initializer = found_it != std::end(initializers);
      if (!is_initializer) {
        inputs_without_initializers.push_back(&input);
      }
    }
  }
  return inputs_without_initializers;
}
 
static std::vector<const ONNX_NAMESPACE::ValueInfoProto*> GetOutputs(const ONNX_NAMESPACE::ModelProto& model_proto) {
  std::vector<const ONNX_NAMESPACE::ValueInfoProto*> outputs_with_name;
  auto& graph = model_proto.graph();
  auto& outputs = graph.output();
  for (auto& output : outputs) {
    if (output.has_name() && output.has_type()) {
      outputs_with_name.push_back(&output);
    }
  }
  return outputs_with_name;
}
 
class ModelInfo {
 public:
  ModelInfo(const ONNX_NAMESPACE::ModelProto* model_proto) { Initialize(model_proto); }
 
 public:
  // model metadata
  std::string author_;
  std::string name_;
  std::string domain_;
  std::string description_;
  int64_t version_;
  std::vector<std::pair<std::string, std::string>> model_metadata_;
  std::vector<const ONNX_NAMESPACE::ValueInfoProto*> input_features_;
  std::vector<const ONNX_NAMESPACE::ValueInfoProto*> output_features_;
  bool requires_float16_support_;
 
 private:
  void Initialize(const ONNX_NAMESPACE::ModelProto* model_proto) {
    for (auto& prop : model_proto->metadata_props()) {
      model_metadata_.push_back(std::make_pair(prop.key(), prop.value()));
    }
 
    input_features_ = GetInputsWithoutInitializers(*model_proto);
    output_features_ = ::GetOutputs(*model_proto);
 
    auto has_producer_name = model_proto->has_producer_name();
    author_ = has_producer_name ? model_proto->producer_name() : "";
 
    auto has_domain = model_proto->has_domain();
    domain_ = has_domain ? model_proto->domain() : "";
 
    auto has_graph = model_proto->has_graph();
    auto graph_has_name = model_proto->graph().has_name();
    auto is_name_available = has_graph && graph_has_name;
    name_ = is_name_available ? model_proto->graph().name() : "";
 
    auto has_description = model_proto->has_doc_string();
    description_ = has_description ? model_proto->doc_string() : "";
 
    auto has_version = model_proto->has_model_version();
    version_ = has_version ? model_proto->model_version() : 0;
  }
};
 
OrtModelImpl::OrtModelImpl(std::unique_ptr<ONNX_NAMESPACE::ModelProto> model_proto)
  : model_proto_(std::move(model_proto)),
    model_info_(std::make_unique<ModelInfo>(model_proto_.get())) {
}
 
// factory methods for creating an ort model from a path
static OrtStatus* CreateModelProto(const char* path, std::unique_ptr<ONNX_NAMESPACE::ModelProto>& out) {
  int file_descriptor;
 
  auto path_str = std::string(path);
  auto wide_path = onnxruntime::ToWideString(path_str);
 
  _set_errno(0);  // clear errno
  _wsopen_s(
    &file_descriptor, wide_path.c_str(), O_RDONLY | _O_SEQUENTIAL | _O_BINARY, _SH_DENYWR, _S_IREAD | _S_IWRITE
  );
 
  errno_t err = 0;
  _get_errno(&err);
  if (err == ENOENT) {
    return OrtApis::CreateStatus(ORT_NO_SUCHFILE, "Model file not found!");
  }
 
  if (0 > file_descriptor) {
    return OrtApis::CreateStatus(ORT_NO_SUCHFILE, "Model file not found!");
  }
 
  google::protobuf::io::FileInputStream stream(file_descriptor);
  stream.SetCloseOnDelete(true);
 
  auto model_proto = std::unique_ptr<ONNX_NAMESPACE::ModelProto>(new ONNX_NAMESPACE::ModelProto());
 
  auto parse_succeeded = model_proto->ParseFromZeroCopyStream(&stream);
  if (!parse_succeeded) {
    return OrtApis::CreateStatus(ORT_INVALID_PROTOBUF, "Failed to parse model file!");
  }
 
  out = std::move(model_proto);
 
  return S_OK;
}
 
OrtStatus* OrtModelImpl::CreateEmptyModel(int64_t opset, ::OrtModel** model) {
  auto model_proto = std::unique_ptr<ONNX_NAMESPACE::ModelProto>(new ONNX_NAMESPACE::ModelProto());
  auto opsetimportproto = model_proto->add_opset_import();
  opsetimportproto->set_version(opset);
  model_proto->set_ir_version(ONNX_NAMESPACE::Version::IR_VERSION);
  return OrtModelImpl::CreateOrtModelFromProto(std::move(model_proto), model);
}
 
OrtStatus* OrtModelImpl::CreateOrtModelFromPath(const char* path, size_t len, OrtModel** model) {
  ORT_UNUSED_PARAMETER(len);
 
  std::unique_ptr<ONNX_NAMESPACE::ModelProto> model_proto;
 
  if (auto status = CreateModelProto(path, model_proto)) {
    return status;
  }
 
  return OrtModelImpl::CreateOrtModelFromProto(std::move(model_proto), model);
}
 
OrtStatus* OrtModelImpl::CreateOrtModelFromData(void* data, size_t len, ::OrtModel** model) {
  constexpr int32_t kProtobufMaxArraySize = std::numeric_limits<int32_t>::max();
  if (len > static_cast<size_t>(kProtobufMaxArraySize)) {
    const auto error_message = onnxruntime::MakeString(
      "Model data size (", len, " bytes) exceeds maximum supported size (INT32_MAX bytes, approximately 2GB)."
    );
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, error_message.c_str());
  }
 
  auto model_proto = std::unique_ptr<ONNX_NAMESPACE::ModelProto>(new ONNX_NAMESPACE::ModelProto());
 
  auto parse_succeeded = model_proto->ParseFromArray(data, onnxruntime::narrow<int32_t>(len));
  if (!parse_succeeded) {
    return OrtApis::CreateStatus(ORT_INVALID_PROTOBUF, "Failed to parse model stream!");
  }
 
  return OrtModelImpl::CreateOrtModelFromProto(std::move(model_proto), model);
}
 
OrtStatus* OrtModelImpl::CreateOrtModelFromProto(
  std::unique_ptr<ONNX_NAMESPACE::ModelProto>&& model_proto, OrtModel** model
) {
  *model = new (std::nothrow) OrtModelImpl(std::move(model_proto));
  if (*model == nullptr) {
    return OrtApis::CreateStatus(ORT_ENGINE_ERROR, "Engine failed to create a model!");
  }
 
  return nullptr;
}
 
const ModelInfo* OrtModelImpl::UseModelInfo() const {
  return model_info_.get();
}
 
ONNX_NAMESPACE::ModelProto* OrtModelImpl::UseModelProto() const {
  return model_proto_.get();
}
 
std::unique_ptr<ONNX_NAMESPACE::ModelProto> OrtModelImpl::DetachModelProto() {
  return std::move(model_proto_);
}
 
void OrtModelImpl::RefreshModelInfo() {
  auto new_info = std::make_unique<ModelInfo>(model_proto_.get());
  model_info_->author_ = std::move(new_info->author_);
  model_info_->description_ = std::move(new_info->description_);
  model_info_->domain_ = std::move(new_info->domain_);
  model_info_->input_features_ = std::move(new_info->input_features_);
  model_info_->model_metadata_ = std::move(new_info->model_metadata_);
  model_info_->name_ = std::move(new_info->name_);
  model_info_->output_features_ = std::move(new_info->output_features_);
  model_info_->requires_float16_support_ = std::move(new_info->requires_float16_support_);
  model_info_->version_ = std::move(new_info->version_);
}
 
ORT_API_STATUS_IMPL(
  winmla::CreateModelFromPath, _In_ const char* model_path, _In_ size_t size, _Outptr_ OrtModel** out
) {
  API_IMPL_BEGIN
  if (auto status = OrtModelImpl::CreateOrtModelFromPath(model_path, size, out)) {
    return status;
  }
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::CreateModelFromData, _In_opt_ void* data, _In_ size_t size, _Outptr_ OrtModel** out) {
  API_IMPL_BEGIN
  if (auto status = OrtModelImpl::CreateOrtModelFromData(data, size, out)) {
    return status;
  }
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::CloneModel, _In_ const OrtModel* in, _Outptr_ OrtModel** out) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto_copy = std::make_unique<ONNX_NAMESPACE::ModelProto>(*model->UseModelProto());
  if (auto status = OrtModelImpl::CreateOrtModelFromProto(std::move(model_proto_copy), out)) {
    return status;
  }
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::SaveModel, _In_ const OrtModel* in, _In_ const wchar_t* const file_name, _In_ size_t len) {
  API_IMPL_BEGIN
  int fd;
  std::wstring file_path = file_name;
  Status status = onnxruntime::Env::Default().FileOpenWr(file_path, fd);
  if (fd < 0) {
    return OrtApis::CreateStatus(ORT_NO_SUCHFILE, "File not found!");
  }
 
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  google::protobuf::io::FileOutputStream output(fd);
  const bool success = model_proto->SerializeToZeroCopyStream(&output) && output.Flush();
  if (!success) {
    return OrtApis::CreateStatus(ORT_RUNTIME_EXCEPTION, "Failed to serialize model!");
  }
  output.Close();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetAuthor, _In_ const OrtModel* in, _Out_ const char** const author, _Out_ size_t* len
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *author = model->UseModelInfo()->author_.c_str();
  *len = model->UseModelInfo()->author_.size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::ModelGetName, _In_ const OrtModel* in, _Out_ const char** const name, _Out_ size_t* len) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *name = model->UseModelInfo()->name_.c_str();
  *len = model->UseModelInfo()->name_.size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::ModelSetName, _In_ const OrtModel* in, _In_ const char* const name) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  ONNX_NAMESPACE::GraphProto& graph = *model_proto->mutable_graph();
  graph.set_name(name);
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetDomain, _In_ const OrtModel* in, _Out_ const char** const domain, _Out_ size_t* len
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *domain = model->UseModelInfo()->domain_.c_str();
  *len = model->UseModelInfo()->domain_.size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetDescription, _In_ const OrtModel* in, _Out_ const char** const description, _Out_ size_t* len
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *description = model->UseModelInfo()->description_.c_str();
  *len = model->UseModelInfo()->description_.size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::ModelGetVersion, _In_ const OrtModel* in, _Out_ int64_t* version) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *version = model->UseModelInfo()->version_;
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::ModelGetMetadataCount, _In_ const OrtModel* in, _Out_ size_t* count) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *count = model->UseModelInfo()->model_metadata_.size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetMetadata,
  _In_ const OrtModel* in,
  _In_ size_t count,
  _Out_ const char** const key,
  _Out_ size_t* key_len,
  _Out_ const char** const value,
  _Out_ size_t* value_len
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *key = model->UseModelInfo()->model_metadata_[count].first.c_str();
  *key_len = model->UseModelInfo()->model_metadata_[count].first.size();
  *value = model->UseModelInfo()->model_metadata_[count].second.c_str();
  *value_len = model->UseModelInfo()->model_metadata_[count].second.size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::ModelGetInputCount, _In_ const OrtModel* in, _Out_ size_t* count) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *count = model->UseModelInfo()->input_features_.size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::ModelGetOutputCount, _In_ const OrtModel* in, _Out_ size_t* count) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *count = model->UseModelInfo()->output_features_.size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetInputName,
  _In_ const OrtModel* in,
  _In_ size_t index,
  _Out_ const char** input_name,
  _Out_ size_t* count
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *input_name = model->UseModelInfo()->input_features_[index]->name().c_str();
  *count = model->UseModelInfo()->input_features_[index]->name().size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetOutputName,
  _In_ const OrtModel* in,
  _In_ size_t index,
  _Out_ const char** output_name,
  _Out_ size_t* count
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *output_name = model->UseModelInfo()->output_features_[index]->name().c_str();
  *count = model->UseModelInfo()->output_features_[index]->name().size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetInputDescription,
  _In_ const OrtModel* in,
  _In_ size_t index,
  _Out_ const char** input_description,
  _Out_ size_t* count
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *input_description = model->UseModelInfo()->input_features_[index]->doc_string().c_str();
  *count = model->UseModelInfo()->input_features_[index]->doc_string().size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetOutputDescription,
  _In_ const OrtModel* in,
  _In_ size_t index,
  _Out_ const char** output_description,
  _Out_ size_t* count
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  *output_description = model->UseModelInfo()->output_features_[index]->doc_string().c_str();
  *count = model->UseModelInfo()->output_features_[index]->doc_string().size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetInputTypeInfo, _In_ const OrtModel* in, _In_ size_t index, _Outptr_ OrtTypeInfo** type_info
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto info = OrtTypeInfo::FromTypeProto(model->UseModelInfo()->input_features_[index]->type());
  *type_info = info.release();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetOutputTypeInfo, _In_ const OrtModel* in, _In_ size_t index, _Outptr_ OrtTypeInfo** type_info
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto info = OrtTypeInfo::FromTypeProto(model->UseModelInfo()->output_features_[index]->type());
  *type_info = info.release();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::ModelEnsureNoFloat16, _In_ const OrtModel* in) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_info = model->UseModelInfo();
  auto model_proto = model->UseModelProto();
  auto& graph = model_proto->graph();
 
  // The model will not contain fp16 operations if:
  // 1. The model has no fp16 inputs
  // 2. The model has no fp16 initializers
  // 3. The model does not create any fp16 intermediary tensors via the Cast (to float16) operator
  // 4. The model does not have any fp16 outputs
 
  // 1. Ensure that The model has no fp16 inputs
  for (auto input : model_info->input_features_) {
    auto& type = input->type();
    if (type.value_case() == ONNX_NAMESPACE::TypeProto::kTensorType) {
      auto& tensor_type = type.tensor_type();
      if (tensor_type.elem_type() == ONNX_NAMESPACE::TensorProto_DataType::TensorProto_DataType_BFLOAT16) {
        std::stringstream error_message;
        error_message << "The model contains a 16-bit input (" << input->name()
                      << "), but the current device does not support 16-bit float.";
        return OrtApis::CreateStatus(ORT_INVALID_GRAPH, error_message.str().c_str());
      }
    }
  }
 
  // 2. Ensure that the model has no fp16 initializers
  for (int i = 0; i < graph.node_size(); i++) {
    auto node = graph.node(i);
    if (node.op_type() == "Cast" && node.domain().empty()) {
      for (int attribIndex = 0; attribIndex < node.attribute_size(); attribIndex++) {
        auto attribute = node.attribute(attribIndex);
        if (attribute.name() == "to") {
          if (attribute.i() == ONNX_NAMESPACE::TensorProto::DataType::TensorProto_DataType_FLOAT16) {
            std::stringstream error_message;
            error_message << "The model contains a 16-bit input (" << node.name().c_str()
                          << "), but the current device does not support 16-bit float.";
            return OrtApis::CreateStatus(ORT_INVALID_GRAPH, error_message.str().c_str());
          }
        }
      }
    }
  }
 
  // 3. Ensure that the model does not create any fp16 intermediary
  //    tensors via the Cast (to float16) operator
  for (int i = 0; i < graph.initializer_size(); i++) {
    auto initializer = graph.initializer(i);
    if (initializer.data_type() == ONNX_NAMESPACE::TensorProto::DataType::TensorProto_DataType_FLOAT16) {
      std::stringstream error_message;
      error_message << "The model contains a 16-bit input (" << initializer.name().c_str()
                    << "), but the current device does not support 16-bit float.";
      return OrtApis::CreateStatus(ORT_INVALID_GRAPH, error_message.str().c_str());
    }
  }
 
  // 4. Ensure that the model does not have any fp16 outputs
  for (auto output : model_info->output_features_) {
    auto& type = output->type();
    if (type.value_case() == ONNX_NAMESPACE::TypeProto::kTensorType) {
      auto& tensor_type = type.tensor_type();
      if (tensor_type.elem_type() == ONNX_NAMESPACE::TensorProto_DataType::TensorProto_DataType_BFLOAT16) {
        std::stringstream error_message;
        error_message << "The model contains a 16-bit input (" << output->name()
                      << "), but the current device does not support 16-bit float.";
        return OrtApis::CreateStatus(ORT_INVALID_GRAPH, error_message.str().c_str());
      }
    }
  }
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::CreateModel, _In_ int64_t opset, _Outptr_ OrtModel** out) {
  API_IMPL_BEGIN
  return OrtModelImpl::CreateEmptyModel(opset, out);
  API_IMPL_END
}
 
static ONNX_NAMESPACE::TensorProto_DataType ONNXTensorElementDataTypeToTensorProto_DataType(
  ONNXTensorElementDataType type
) {
  switch (type) {
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT:
      return ONNX_NAMESPACE::TensorProto_DataType_FLOAT;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8:
      return ONNX_NAMESPACE::TensorProto_DataType_UINT8;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8:
      return ONNX_NAMESPACE::TensorProto_DataType_INT8;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16:
      return ONNX_NAMESPACE::TensorProto_DataType_UINT16;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16:
      return ONNX_NAMESPACE::TensorProto_DataType_INT16;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:
      return ONNX_NAMESPACE::TensorProto_DataType_INT32;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64:
      return ONNX_NAMESPACE::TensorProto_DataType_INT64;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING:
      return ONNX_NAMESPACE::TensorProto_DataType_STRING;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL:
      return ONNX_NAMESPACE::TensorProto_DataType_BOOL;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16:
      return ONNX_NAMESPACE::TensorProto_DataType_FLOAT16;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE:
      return ONNX_NAMESPACE::TensorProto_DataType_DOUBLE;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32:
      return ONNX_NAMESPACE::TensorProto_DataType_UINT32;
    case ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64:
      return ONNX_NAMESPACE::TensorProto_DataType_UINT64;
    default:
      return ONNX_NAMESPACE::TensorProto_DataType_UNDEFINED;
  }
}
 
static void CreateTypeProto_Tensor(
  ONNX_NAMESPACE::TypeProto_Tensor* mutable_tensor_type,
  const char* const name,
  const int64_t* shape,
  size_t shape_len,
  ONNX_NAMESPACE::TensorProto_DataType data_type
) {
  mutable_tensor_type->set_elem_type(data_type);
 
  size_t dim_param = 0;
  for (size_t i = 0; i < shape_len; i++) {
    if (shape[i] == -1) {
      std::ostringstream str;
      str << name << dim_param++;
      mutable_tensor_type->mutable_shape()->add_dim()->set_dim_param(str.str().c_str(), 1);
    } else {
      mutable_tensor_type->mutable_shape()->add_dim()->set_dim_value(shape[i]);
    }
  }
 
  if (shape_len > 0) {
    mutable_tensor_type->mutable_shape()->mutable_dim(0)->set_denotation("DATA_BATCH");
  }
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelAddInput, _In_ OrtModel* in, _In_ const char* const input_name, _In_ OrtTypeInfo* info
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  ONNX_NAMESPACE::GraphProto& graph = *model_proto->mutable_graph();
  ONNX_NAMESPACE::ValueInfoProto& input = *graph.add_input();
  input.set_name(input_name);
 
  if (info->type == ONNXType::ONNX_TYPE_TENSOR) {
    auto num_dims = info->tensor_type_info->HasShape() ? info->tensor_type_info->GetShape()->GetDims().size() : 0;
    CreateTypeProto_Tensor(
      input.mutable_type()->mutable_tensor_type(),
      input_name,
      (num_dims == 0) ? nullptr : info->tensor_type_info->GetShape()->GetDims().data(),
      num_dims,
      ONNXTensorElementDataTypeToTensorProto_DataType(info->tensor_type_info->GetElementType())
    );
  }
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelAddConstantInput,
  _In_ OrtModel* in,
  _In_ const char* const input_name,
  _In_ OrtTypeInfo* info,
  _In_ OrtValue* value
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  ONNX_NAMESPACE::GraphProto& graph = *model_proto->mutable_graph();
  ONNX_NAMESPACE::TensorProto& input = *graph.add_initializer();
  input.set_name(input_name);
 
  auto num_dims = info->tensor_type_info->HasShape() ? info->tensor_type_info->GetShape()->GetDims().size() : 0;
  for (size_t i = 0; i < num_dims; i++) {
    input.add_dims(info->tensor_type_info->GetShape()->GetDims()[i]);
  }
 
  input.set_data_type(ONNXTensorElementDataTypeToTensorProto_DataType(info->tensor_type_info->GetElementType()));
  auto tensor = value->GetMutable<onnxruntime::Tensor>();
  onnxruntime::utils::SetRawDataInTensorProto(input, tensor->DataRaw(), tensor->SizeInBytes());
 
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelAddOutput, _In_ OrtModel* in, _In_ const char* const output_name, _In_ OrtTypeInfo* info
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  ONNX_NAMESPACE::GraphProto& graph = *model_proto->mutable_graph();
  ONNX_NAMESPACE::ValueInfoProto& output = *graph.add_output();
  output.set_name(output_name);
 
  auto num_dims = info->tensor_type_info->HasShape() ? info->tensor_type_info->GetShape()->GetDims().size() : 0;
  if (info->type == ONNXType::ONNX_TYPE_TENSOR) {
    CreateTypeProto_Tensor(
      output.mutable_type()->mutable_tensor_type(),
      output_name,
      (num_dims == 0) ? nullptr : info->tensor_type_info->GetShape()->GetDims().data(),
      num_dims,
      ONNXTensorElementDataTypeToTensorProto_DataType(info->tensor_type_info->GetElementType())
    );
  }
  return nullptr;
  API_IMPL_END
}
 
static const onnx::OpSchema* GetSchema(const char* const op_type, int64_t opset, const char* const op_domain) {
  std::string domain = onnx::ONNX_DOMAIN;
  if (op_domain) {
    domain = op_domain;
  }
 
  auto registry = ONNX_NAMESPACE::OpSchemaRegistry::Instance();
  return registry->GetSchema(op_type, static_cast<int>(opset), domain);
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelAddOperator,
  _In_ OrtModel* in,
  _In_ const char* const op_type,
  _In_ const char* const op_name,
  _In_ int64_t opset,
  _In_ const char* const op_domain,
  _In_ const char* const* input_names,
  _In_ size_t num_inputs,
  _In_ const char* const* output_names,
  _In_ size_t num_outputs,
  _In_ const char* const* attribute_names,
  _In_ OrtValue** attribute_values,
  _In_ size_t num_attributes
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  ONNX_NAMESPACE::GraphProto& graph = *model_proto->mutable_graph();
  onnx::NodeProto& node = *graph.add_node();
  node.set_op_type(op_type);
  node.set_name(op_name);
  node.set_domain(op_domain);
 
  auto schema = GetSchema(op_type, opset, op_domain);
  auto all_attributes = schema->attributes();
 
  for (size_t i = 0; i < num_attributes; i++) {
    auto tensor = attribute_values[i]->GetMutable<onnxruntime::Tensor>();
 
    auto attr = node.add_attribute();
    attr->set_name(attribute_names[i]);
    auto& schema_attribute_definition = all_attributes.at(attribute_names[i]);
    attr->set_type(schema_attribute_definition.type);
 
    switch (schema_attribute_definition.type) {
      case onnx::AttributeProto_AttributeType_INT: {
        if (tensor->Shape().Size() != 1) {
          return OrtApis::CreateStatus(ORT_ENGINE_ERROR, "Expected a single int64 value!");
        }
        auto raw_data = tensor->DataRaw();
        attr->set_i(*reinterpret_cast<const int64_t*>(raw_data));
        break;
      }
      case onnx::AttributeProto_AttributeType_FLOAT: {
        if (tensor->Shape().Size() != 1) {
          return OrtApis::CreateStatus(ORT_ENGINE_ERROR, "Expected a single float value!");
        }
        auto raw_data = tensor->DataRaw();
        attr->set_f(*reinterpret_cast<const float*>(raw_data));
        break;
      }
      case onnx::AttributeProto_AttributeType_STRING: {
        if (tensor->Shape().Size() != 1) {
          return OrtApis::CreateStatus(ORT_ENGINE_ERROR, "Expected a single string value!");
        }
        auto raw_data = tensor->DataRaw();
        attr->set_s(*reinterpret_cast<const std::string*>(raw_data));
        break;
      }
      case onnx::AttributeProto_AttributeType_INTS: {
        auto raw_data = tensor->DataRaw();
        for (int j = 0; j < tensor->Shape().Size(); j++) {
          attr->add_ints(*(reinterpret_cast<const int64_t*>(raw_data) + j));
        }
        break;
      }
      case onnx::AttributeProto_AttributeType_FLOATS: {
        auto raw_data = tensor->DataRaw();
        for (int j = 0; j < tensor->Shape().Size(); j++) {
          attr->add_floats(*(reinterpret_cast<const float*>(raw_data) + j));
        }
        break;
      }
      case onnx::AttributeProto_AttributeType_TENSOR: {
        auto tensor_proto = attr->add_tensors();
        auto prim_type = tensor->DataType()->AsPrimitiveDataType();
        if (prim_type == nullptr) {
          return OrtApis::CreateStatus(ORT_ENGINE_ERROR, "Undefined tensor type!");
        }
        tensor_proto->set_data_type(prim_type->GetDataType());
        onnxruntime::utils::SetRawDataInTensorProto(*tensor_proto, tensor->DataRaw(), tensor->SizeInBytes());
        break;
      }
    }
  }
 
  for (size_t i = 0; i < num_inputs; i++) {
    auto name = input_names[i];
    if (name != nullptr) {
      node.add_input(name);
    } else {
      node.add_input();
    }
  }
 
  for (size_t i = 0; i < num_outputs; i++) {
    auto name = output_names[i];
    if (name != nullptr) {
      node.add_output(name);
    } else {
      node.add_output("unused");
    }
  }
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::ModelGetOpsetVersion, _In_ OrtModel* in, _In_ const char* const domain, _Out_ int32_t* version
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
 
  *version = -1;
  auto size = static_cast<int>(model_proto->opset_import_size());
  for (int i = 0; i < size; i++) {
    auto& current_opset = model_proto->opset_import(i);
    auto& current_domain = current_opset.domain();
    if (_strnicmp(domain, current_domain.c_str(), current_domain.size()) == 0) {
      *version = static_cast<int32_t>(current_opset.version());
      break;
    }
  }
 
  return nullptr;
  API_IMPL_END
}
 
ORT_API(void, winmla::ReleaseModel, OrtModel* ptr) {
  delete ptr;
}
 
#include "core/framework/onnxruntime_typeinfo.h"
#include "core/framework/tensor_type_and_shape.h"
 
ORT_API_STATUS_IMPL(
  winmla::CreateTensorTypeInfo,
  _In_ const int64_t* dim_values,
  size_t dim_count,
  ONNXTensorElementDataType type,
  _Out_ OrtTypeInfo** ort_type_info
) {
  API_IMPL_BEGIN
  auto tensor_shape = onnxruntime::TensorShape(dim_values, dim_count);
  auto type_and_shape = OrtTensorTypeAndShapeInfo::GetTensorShapeAndTypeHelper(type, &tensor_shape, nullptr);
  *ort_type_info = OrtTypeInfo::MakePtr(ONNX_TYPE_TENSOR, std::move(type_and_shape)).release();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::CreateSequenceTypeInfo, _Out_ OrtTypeInfo** type_info) {
  API_IMPL_BEGIN
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(winmla::CreateMapTypeInfo, _Out_ OrtTypeInfo** type_info) {
  API_IMPL_BEGIN
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::OperatorGetNumInputs,
  _In_ const char* const op_type,
  _In_ int64_t opset,
  _In_ const char* const op_domain,
  _Out_ size_t* num_inputs
) {
  API_IMPL_BEGIN
  auto schema = GetSchema(op_type, opset, op_domain);
  *num_inputs = schema->inputs().size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::OperatorGetInputName,
  _In_ const char* const op_type,
  _In_ int64_t opset,
  _In_ const char* const op_domain,
  _In_ size_t index,
  _Out_ const char** const name
) {
  API_IMPL_BEGIN
  auto schema = GetSchema(op_type, opset, op_domain);
  *name = schema->inputs().at(index).GetName().c_str();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::OperatorGetNumOutputs,
  _In_ const char* const op_type,
  _In_ int64_t opset,
  _In_ const char* const op_domain,
  _Out_ size_t* num_outputs
) {
  API_IMPL_BEGIN
  auto schema = GetSchema(op_type, opset, op_domain);
  *num_outputs = schema->outputs().size();
  return nullptr;
  API_IMPL_END
}
 
ORT_API_STATUS_IMPL(
  winmla::OperatorGetOutputName,
  _In_ const char* const op_type,
  _In_ int64_t opset,
  _In_ const char* const op_domain,
  _In_ size_t index,
  _Out_ const char** const name
) {
  API_IMPL_BEGIN
  auto schema = GetSchema(op_type, opset, op_domain);
  *name = schema->outputs().at(index).GetName().c_str();
  return nullptr;
  API_IMPL_END
}
#include "core/platform/threadpool.h"
#include "core/platform/env.h"
 
ORT_API_STATUS_IMPL(
  winmla::CreateThreadPool, _In_ ThreadPoolType type, _In_ OrtThreadPoolOptions* options, _Outptr_ OrtThreadPool** out
) {
  API_IMPL_BEGIN
  OrtThreadPoolParams params = {};
  params.thread_pool_size = options->thread_pool_size;
  params.auto_set_affinity = options->auto_set_affinity;
  params.allow_spinning = options->allow_spinning;
  params.dynamic_block_base_ = options->dynamic_block_base_;
  params.stack_size = options->stack_size;
  params.name = options->name;
  params.set_denormal_as_zero = options->set_denormal_as_zero;
 
  auto unique_tp = onnxruntime::concurrency::CreateThreadPool(
    &onnxruntime::Env::Default(), params, (onnxruntime::concurrency::ThreadPoolType)type
  );
  *out = reinterpret_cast<OrtThreadPool*>(unique_tp.release());
  return nullptr;
  API_IMPL_END
}
 
ORT_API(void, winmla::ReleaseThreadPool, OrtThreadPool* ptr) {
  delete reinterpret_cast<onnxruntime::concurrency::ThreadPool*>(ptr);
}
 
ORT_API_STATUS_IMPL(
  winmla::JoinModels,
  _In_ OrtModel* first_model_in,
  _In_ OrtModel* second_model_in,
  _In_ const char* const* output_names,
  _In_ const char* const* input_names,
  size_t num_linkages,
  bool promote_unlinked_outputs,
  _In_ const char* const join_node_prefix
) {
  API_IMPL_BEGIN
  OrtModelImpl* first_model = first_model_in->ToInternal();
  OrtModelImpl* second_model = second_model_in->ToInternal();
 
  std::string second_model_prefix = join_node_prefix;
  auto first_model_proto = first_model->UseModelProto();
  auto second_model_proto = second_model->DetachModelProto();
 
  // Remove old outputs
  if (promote_unlinked_outputs) {
    // Copy the output of the first model
    auto first_outputs = first_model_proto->graph().output();
 
    // Clear all outputs
    first_model_proto->mutable_graph()->mutable_output()->Clear();
 
    // Add back output
    for (int i = first_outputs.size() - 1; i >= 0; i--) {
      auto& output = first_outputs.at(i);
      auto output_name = output.name();
 
      auto found_it = std::find_if(output_names, output_names + num_linkages, [output_name](auto& name) {
        return std::strcmp(name, output_name.c_str()) == 0;
      });
      if (found_it == (output_names + num_linkages)) {
        // if output.name() is not found in the linkages, it is unlinked, and it should be promoted
        auto& promoted_output = *first_model_proto->mutable_graph()->add_output();
        promoted_output = std::move(output);
      }
    }
  } else {
    // remove all first model outputs
    first_model_proto->mutable_graph()->mutable_output()->Clear();
  }
 
  // add all model outputs from the second model
  for (int i = 0; i < second_model_proto->graph().output_size(); i++) {
    auto& other_output = *second_model_proto->mutable_graph()->mutable_output(i);
    *other_output.mutable_name() = second_model_prefix + other_output.name();
    auto& output = *first_model_proto->mutable_graph()->add_output();
    output = std::move(other_output);
  }
 
  // loop through second model inputs and promote the unlinked ones to the main model inputs
  for (int i = 0; i < second_model_proto->graph().input_size(); i++) {
    auto& other_input = *second_model_proto->mutable_graph()->mutable_input(i);
    auto old_name = other_input.name();
    *other_input.mutable_name() = second_model_prefix + old_name;
 
    auto found_it = std::find_if(input_names, input_names + num_linkages, [old_name](auto& name) {
      return std::strcmp(name, old_name.c_str()) == 0;
    });
    bool is_linked =
      found_it != (input_names + num_linkages);  // figure out if other_input.name() exists in the output_names mapped
    if (!is_linked) {
      auto& input = *first_model_proto->mutable_graph()->add_input();
      input = std::move(other_input);
    }
  }
 
  // add all initializers
  for (int i = 0; i < second_model_proto->graph().initializer_size(); i++) {
    auto& other_initializer = *second_model_proto->mutable_graph()->mutable_initializer(i);
    *other_initializer.mutable_name() = second_model_prefix + other_initializer.name();
    auto& initializer = *first_model_proto->mutable_graph()->add_initializer();
    initializer = std::move(other_initializer);
  }
 
  // add all nodes
  for (int i = 0; i < second_model_proto->graph().node_size(); i++) {
    auto& other_node = *second_model_proto->mutable_graph()->mutable_node(i);
    if (0 != strcmp(other_node.name().c_str(), "")) {
      *other_node.mutable_name() = second_model_prefix + other_node.name();
    }
    for (int j = 0; j < other_node.input_size(); j++) {
      *other_node.mutable_input(j) = second_model_prefix + other_node.input(j);
    }
    for (int j = 0; j < other_node.output_size(); j++) {
      *other_node.mutable_output(j) = second_model_prefix + other_node.output(j);
    }
    auto& node = *first_model_proto->mutable_graph()->add_node();
    node = std::move(other_node);
  }
 
  // WinML+RT API only supports opset 7 and above models.
  // In practice this number is always overwritten by the for loop below which will find the actual opset version.
  int64_t opset = 7;
  for (int i = 0; i < second_model_proto->opset_import_size(); i++) {
    auto mutable_opset_import = second_model_proto->mutable_opset_import(i);
    auto domain = mutable_opset_import->has_domain() ? mutable_opset_import->domain() : std::string("");
    auto version = mutable_opset_import->version();
 
    // does the domain exist in the first model?
    auto found_it = std::find_if(
      first_model_proto->mutable_opset_import()->begin(),
      first_model_proto->mutable_opset_import()->end(),
      [&domain](auto& mutable_opset_import) {
        auto first_model_domain = mutable_opset_import.has_domain() ? mutable_opset_import.domain() : std::string("");
        return 0 == strcmp(first_model_domain.c_str(), domain.c_str());
      }
    );
    if (found_it != first_model_proto->mutable_opset_import()->end()) {
      found_it->set_version(std::max(found_it->version(), version));
      if (0 == strcmp(domain.c_str(), "")) {
        opset = found_it->version();
      }
    }
  }
 
  // add identity ops to rename all of the first model outputs to secondmodel inputs with prefix for each linkage
  for (size_t i = 0; i < num_linkages; i++) {
    auto op_output_name = second_model_prefix + *(input_names + i);
    const char* const op_output_name_const_str = op_output_name.c_str();
    std::string name = "IdentityTo";
    name += second_model_prefix + *(input_names + i);
    ModelAddOperator(
      first_model,
      "Identity",
      name.c_str(),
      opset,
      "",
      (output_names + i),
      1,
      &op_output_name_const_str,
      1,
      nullptr,
      nullptr,
      0
    );
  }
  first_model->RefreshModelInfo();
 
  return nullptr;
  API_IMPL_END
}