-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
onnxruntime_c_api.cc
2804 lines (2468 loc) · 110 KB
/
onnxruntime_c_api.cc
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "core/session/onnxruntime_c_api.h"
#include "core/session/allocator_adapters.h"
#include "core/session/inference_session_utils.h"
#include "core/session/IOBinding.h"
#include "core/framework/allocator.h"
#include "core/framework/error_code_helper.h"
#include "core/framework/execution_provider.h"
#include "core/framework/tensor_type_and_shape.h"
#include "core/framework/utils.h"
#include <cassert>
#include <cstring>
#include <functional>
#include <sstream>
#include "core/common/common.h"
#include "core/common/logging/logging.h"
#include "core/common/narrow.h"
#include "core/common/status.h"
#include "core/common/safeint.h"
#include "core/graph/constants.h"
#include "core/graph/graph.h"
#include "core/framework/allocator.h"
#include "core/framework/tensor.h"
#include "core/framework/ort_value.h"
#include "core/providers/get_execution_providers.h"
#include "core/session/environment.h"
#include "core/framework/callback.h"
#include "core/framework/tensorprotoutils.h"
#include "core/framework/onnxruntime_typeinfo.h"
#include "core/session/inference_session.h"
#include "core/session/ort_apis.h"
#include "core/session/ort_env.h"
#include "core/framework/data_types.h"
#include "abi_session_options_impl.h"
#include "core/framework/TensorSeq.h"
#include "core/platform/ort_mutex.h"
#include "core/common/string_helper.h"
#ifdef USE_CUDA
#include "core/providers/cuda/cuda_provider_factory.h"
#include "core/providers/cuda/cuda_execution_provider_info.h"
namespace onnxruntime {
ProviderInfo_CUDA* TryGetProviderInfo_CUDA();
}
#endif
#ifdef ENABLE_TRAINING_APIS
#include "orttraining/training_api/include/onnxruntime_training_c_api.h"
#include "orttraining/training_api/ort_training_apis.h"
#endif
#ifdef USE_CANN
#include "core/providers/cann/cann_provider_factory.h"
#include "core/providers/cann/cann_execution_provider_info.h"
namespace onnxruntime {
ProviderInfo_CANN* TryGetProviderInfo_CANN();
}
#endif
#ifdef USE_DNNL
#include "core/providers/dnnl/dnnl_provider_factory.h"
#include "core/providers/dnnl/dnnl_execution_provider_info.h"
namespace onnxruntime {
ProviderInfo_Dnnl* TryGetProviderInfo_Dnnl();
}
#endif
#ifdef USE_DML
#include "core/providers/dml/dml_provider_factory.h"
const OrtDmlApi* GetOrtDmlApi(_In_ uint32_t version) NO_EXCEPTION;
#endif
#ifdef ENABLE_EXTENSION_CUSTOM_OPS
#include "onnxruntime_extensions.h"
#endif
#if defined(_MSC_VER) && !defined(__clang__)
// The warning is: "Do not assign the result of an allocation or a function call with an owner<T> return value to a raw pointer, use owner<T> instead(i .11)."
// But this file is for C API. It can't use unique_ptr/shared_ptr in function signature.
#pragma warning(disable : 26400)
#endif
using namespace onnxruntime::logging;
using onnxruntime::DataTypeImpl;
using onnxruntime::Environment;
using onnxruntime::IAllocator;
using onnxruntime::InputDefList;
using onnxruntime::narrow;
using onnxruntime::OutputDefList;
using onnxruntime::Tensor;
using onnxruntime::ToOrtStatus;
using onnxruntime::common::Status;
using namespace onnxruntime;
#ifndef ORT_STATUS_PTR
#ifdef _WIN32
#define ORT_STATUS_PTR _Check_return_ _Ret_maybenull_ OrtStatusPtr
#else
#define ORT_STATUS_PTR OrtStatus*
#endif
#endif
#define TENSOR_READ_API_BEGIN \
API_IMPL_BEGIN \
auto v = reinterpret_cast<const ::OrtValue*>(value); \
auto& tensor = v->Get<onnxruntime::Tensor>();
#define TENSOR_READWRITE_API_BEGIN \
API_IMPL_BEGIN \
auto v = (value); \
auto tensor = v->GetMutable<onnxruntime::Tensor>();
ORT_API_STATUS_IMPL(OrtApis::CreateEnvWithCustomLogger, OrtLoggingFunction logging_function,
_In_opt_ void* logger_param, OrtLoggingLevel logging_level, _In_ const char* logid,
_Outptr_ OrtEnv** out) {
API_IMPL_BEGIN
OrtEnv::LoggingManagerConstructionInfo lm_info{logging_function, logger_param, logging_level, logid};
Status status;
*out = OrtEnv::GetInstance(lm_info, status);
return ToOrtStatus(status);
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::CreateEnv, OrtLoggingLevel logging_level,
_In_ const char* logid, _Outptr_ OrtEnv** out) {
API_IMPL_BEGIN
OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, logging_level, logid};
Status status;
*out = OrtEnv::GetInstance(lm_info, status);
return ToOrtStatus(status);
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::CreateEnvWithGlobalThreadPools, OrtLoggingLevel logging_level,
_In_ const char* logid, _In_ const struct OrtThreadingOptions* tp_options, _Outptr_ OrtEnv** out) {
API_IMPL_BEGIN
OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, logging_level, logid};
Status status;
*out = OrtEnv::GetInstance(lm_info, status, tp_options);
return ToOrtStatus(status);
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::CreateEnvWithCustomLoggerAndGlobalThreadPools, OrtLoggingFunction logging_function, _In_opt_ void* logger_param,
OrtLoggingLevel logging_level, _In_ const char* logid, _In_ const struct OrtThreadingOptions* tp_options,
_Outptr_ OrtEnv** out) {
API_IMPL_BEGIN
OrtEnv::LoggingManagerConstructionInfo lm_info{logging_function, logger_param, logging_level, logid};
Status status;
*out = OrtEnv::GetInstance(lm_info, status, tp_options);
return ToOrtStatus(status);
API_IMPL_END
}
// enable platform telemetry
ORT_API_STATUS_IMPL(OrtApis::EnableTelemetryEvents, _In_ const OrtEnv* ort_env) {
API_IMPL_BEGIN
ORT_UNUSED_PARAMETER(ort_env);
// note telemetry is controlled via the platform Env object, not the OrtEnv object instance
const Env& env = Env::Default();
env.GetTelemetryProvider().EnableTelemetryEvents();
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::DisableTelemetryEvents, _In_ const OrtEnv* ort_env) {
API_IMPL_BEGIN
ORT_UNUSED_PARAMETER(ort_env);
// note telemetry is controlled via the platform Env object, not the OrtEnv object instance
const Env& env = Env::Default();
env.GetTelemetryProvider().DisableTelemetryEvents();
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::UpdateEnvWithCustomLogLevel, _In_ OrtEnv* ort_env,
OrtLoggingLevel log_severity_level) {
API_IMPL_BEGIN
LoggingManager* default_logging_manager = ort_env->GetLoggingManager();
int severity_level = static_cast<int>(log_severity_level);
default_logging_manager->SetDefaultLoggerSeverity(static_cast<logging::Severity>(severity_level));
return nullptr;
API_IMPL_END
}
ORT_STATUS_PTR CreateTensorImpl(MLDataType ml_type, const int64_t* shape, size_t shape_len,
_Inout_ OrtAllocator* allocator, OrtValue& value) {
TensorShape tensor_shape(shape, shape_len);
AllocatorPtr alloc_ptr = std::make_shared<onnxruntime::IAllocatorImplWrappingOrtAllocator>(allocator);
Tensor::InitOrtValue(ml_type, tensor_shape, std::move(alloc_ptr), value);
return nullptr;
}
ORT_STATUS_PTR CreateTensorImplForSeq(MLDataType elem_type, const int64_t* shape, size_t shape_len, Tensor& out) {
OrtAllocator* allocator;
// TODO(pranav): what allocator should be used to create the tensor here?
// for the sake of simplicity of the API using the default one here
ORT_API_RETURN_IF_ERROR(OrtApis::GetAllocatorWithDefaultOptions(&allocator));
AllocatorPtr alloc_ptr = std::make_shared<onnxruntime::IAllocatorImplWrappingOrtAllocator>(allocator);
TensorShape tensor_shape(shape, shape_len);
out = Tensor(elem_type, tensor_shape, std::move(alloc_ptr));
return nullptr;
}
/**
*
* this function will create a copy of the allocator info
*/
ORT_STATUS_PTR CreateTensorImpl(MLDataType ml_type, const int64_t* shape, size_t shape_len, const OrtMemoryInfo* info,
void* p_data, size_t p_data_len, OrtValue& ort_value) {
TensorShape tensor_shape(shape, shape_len);
if (std::any_of(tensor_shape.GetDims().begin(), tensor_shape.GetDims().end(), [](int64_t v) { return v < 0; })) {
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "tried creating tensor with negative value in shape");
}
auto elem_count = narrow<size_t>(tensor_shape.Size());
size_t size_to_allocate;
if (!IAllocator::CalcMemSizeForArray(ml_type->Size(), elem_count, &size_to_allocate)) {
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "size overflow");
}
if (size_to_allocate > p_data_len) {
std::ostringstream oss;
oss << "not enough space: expected " << size_to_allocate << ", got " << p_data_len;
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, oss.str().c_str());
}
Tensor::InitOrtValue(ml_type, tensor_shape, p_data, *info, ort_value);
return nullptr;
}
ORT_API_STATUS_IMPL(OrtApis::CreateTensorWithDataAsOrtValue, _In_ const OrtMemoryInfo* info,
_Inout_ void* p_data, size_t p_data_len, _In_ const int64_t* shape, size_t shape_len,
ONNXTensorElementDataType type, _Outptr_ OrtValue** out) {
API_IMPL_BEGIN
auto ml_type = DataTypeImpl::TensorTypeFromONNXEnum(type)->GetElementType();
auto value = std::make_unique<OrtValue>();
ORT_API_RETURN_IF_ERROR(CreateTensorImpl(ml_type, shape, shape_len, info, p_data, p_data_len, *value));
*out = value.release();
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::CreateTensorAsOrtValue, _Inout_ OrtAllocator* allocator,
_In_ const int64_t* shape, size_t shape_len, ONNXTensorElementDataType type,
_Outptr_ OrtValue** out) {
API_IMPL_BEGIN
auto ml_type = DataTypeImpl::TensorTypeFromONNXEnum(type)->GetElementType();
auto value = std::make_unique<OrtValue>();
ORT_API_RETURN_IF_ERROR(CreateTensorImpl(ml_type, shape, shape_len, allocator, *value));
*out = value.release();
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::CreateSparseTensorAsOrtValue, _Inout_ OrtAllocator* allocator, _In_ const int64_t* dense_shape,
size_t dense_shape_len, ONNXTensorElementDataType type, _Outptr_ OrtValue** out) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
auto sparse_tensor_type = DataTypeImpl::SparseTensorTypeFromONNXEnum(type);
auto element_type = sparse_tensor_type->GetElementType();
assert(element_type->AsPrimitiveDataType() != nullptr);
TensorShape shape(dense_shape, dense_shape_len);
if (std::any_of(shape.GetDims().begin(), shape.GetDims().end(),
[](int64_t v) { return v < 0; })) {
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "tried creating tensor with negative value in shape");
}
auto alloc_ptr = std::make_shared<onnxruntime::IAllocatorImplWrappingOrtAllocator>(allocator);
auto value = std::make_unique<OrtValue>();
SparseTensor::InitOrtValue(element_type, shape, std::move(alloc_ptr), *value);
*out = value.release();
return nullptr;
#else
ORT_UNUSED_PARAMETER(allocator);
ORT_UNUSED_PARAMETER(dense_shape);
ORT_UNUSED_PARAMETER(dense_shape_len);
ORT_UNUSED_PARAMETER(type);
ORT_UNUSED_PARAMETER(out);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
namespace {
#if !defined(DISABLE_SPARSE_TENSORS)
std::unique_ptr<IDataTransfer> GetDataTransfer(const OrtDevice& src_device, const OrtDevice& dst_device) {
if (src_device.Type() == OrtDevice::CPU && dst_device.Type() == OrtDevice::CPU) {
return std::make_unique<CPUDataTransfer>();
}
#ifdef USE_CUDA
if (src_device.Type() == OrtDevice::GPU || dst_device.Type() == OrtDevice::GPU) {
if (auto* provider_info = TryGetProviderInfo_CUDA()) {
return provider_info->CreateGPUDataTransfer();
}
}
#endif
ORT_THROW("Not able to find appropriate IDataTransfer to copy sparse data");
}
SparseTensor& ValidateFillInputArgs(OrtValue* v, const TensorShape& values_shape, const OrtMemoryInfo* data_mem_info) {
auto& sparse_tensor = SparseTensor::GetSparseTensorFromOrtValue(*v);
if (sparse_tensor.IsDataTypeString()) {
if ((data_mem_info->device.Type() != OrtDevice::CPU) || sparse_tensor.Location().device.Type() != OrtDevice::CPU) {
ORT_THROW("Strings can only reside in CPU memory");
}
}
if (std::any_of(values_shape.GetDims().begin(), values_shape.GetDims().end(),
[](int64_t v) { return v < 0; })) {
ORT_THROW("tried Filling sparse tensor with negative value in values shape");
}
return sparse_tensor;
}
union PtrConvert {
explicit PtrConvert(const void* p_p) : p(p_p) {}
const void* p;
const char** strings;
};
#endif // !defined(DISABLE_SPARSE_TENSORS)
} // namespace
ORT_API_STATUS_IMPL(OrtApis::FillSparseTensorCoo, _Inout_ OrtValue* ort_value, _In_ const OrtMemoryInfo* data_mem_info,
_In_ const int64_t* values_shape, size_t values_shape_len, _In_ const void* values,
_In_ const int64_t* indices_data, size_t indices_num) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
TensorShape values_t_shape(values_shape, values_shape_len);
auto& sparse_tensor = ValidateFillInputArgs(ort_value, values_t_shape, data_mem_info);
auto values_size = narrow<size_t>(values_t_shape.Size());
auto indices_span = gsl::make_span(indices_data, indices_num);
if (sparse_tensor.IsDataTypeString()) {
PtrConvert conv(values);
ORT_THROW_IF_ERROR(sparse_tensor.MakeCooStrings(values_size, conv.strings, indices_span));
} else {
auto data_transfer = GetDataTransfer(data_mem_info->device, sparse_tensor.Location().device);
ORT_THROW_IF_ERROR(sparse_tensor.MakeCooData(*data_transfer, *data_mem_info, values_size,
values, indices_span));
}
return nullptr;
#else
ORT_UNUSED_PARAMETER(ort_value);
ORT_UNUSED_PARAMETER(data_mem_info);
ORT_UNUSED_PARAMETER(values_shape);
ORT_UNUSED_PARAMETER(values_shape_len);
ORT_UNUSED_PARAMETER(values);
ORT_UNUSED_PARAMETER(indices_data);
ORT_UNUSED_PARAMETER(indices_num);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::FillSparseTensorCsr, _Inout_ OrtValue* ort_value, _In_ const OrtMemoryInfo* data_mem_info,
_In_ const int64_t* values_shape, size_t values_shape_len, _In_ const void* values,
_In_ const int64_t* inner_indices_data, size_t inner_indices_num,
_In_ const int64_t* outer_indices_data, size_t outer_indices_num) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
TensorShape values_t_shape(values_shape, values_shape_len);
auto& sparse_tensor = ValidateFillInputArgs(ort_value, values_t_shape, data_mem_info);
auto values_size = narrow<size_t>(values_t_shape.Size());
auto inner_indices_span = gsl::make_span(inner_indices_data, inner_indices_num);
auto outer_indices_span = gsl::make_span(outer_indices_data, outer_indices_num);
if (sparse_tensor.IsDataTypeString()) {
PtrConvert conv(values);
ORT_THROW_IF_ERROR(sparse_tensor.MakeCsrStrings(values_size, conv.strings, inner_indices_span, outer_indices_span));
} else {
auto data_transfer = GetDataTransfer(data_mem_info->device, sparse_tensor.Location().device);
ORT_THROW_IF_ERROR(sparse_tensor.MakeCsrData(*data_transfer, *data_mem_info, values_size,
values, inner_indices_span, outer_indices_span));
}
return nullptr;
#else
ORT_UNUSED_PARAMETER(ort_value);
ORT_UNUSED_PARAMETER(data_mem_info);
ORT_UNUSED_PARAMETER(values_shape);
ORT_UNUSED_PARAMETER(values_shape_len);
ORT_UNUSED_PARAMETER(values);
ORT_UNUSED_PARAMETER(inner_indices_data);
ORT_UNUSED_PARAMETER(inner_indices_num);
ORT_UNUSED_PARAMETER(outer_indices_data);
ORT_UNUSED_PARAMETER(outer_indices_num);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::FillSparseTensorBlockSparse, _Inout_ OrtValue* ort_value, _In_ const OrtMemoryInfo* data_mem_info,
_In_ const int64_t* values_shape, size_t values_shape_len, _In_ const void* values,
_In_ const int64_t* indices_shape_data, size_t indices_shape_len,
_In_ const int32_t* indices_data) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
TensorShape values_t_shape(values_shape, values_shape_len);
auto& sparse_tensor = ValidateFillInputArgs(ort_value, values_t_shape, data_mem_info);
TensorShape indices_t_shape(indices_shape_data, indices_shape_len);
if (std::any_of(indices_t_shape.GetDims().begin(), indices_t_shape.GetDims().end(),
[](int64_t v) { return v < 0; })) {
ORT_THROW("tried Filling sparse tensor with negative value in block sparse indices shape");
}
if (sparse_tensor.IsDataTypeString()) {
PtrConvert conv(values);
ORT_THROW_IF_ERROR(sparse_tensor.MakeBlockSparseStrings(values_t_shape, conv.strings, indices_t_shape, indices_data));
} else {
auto data_transfer = GetDataTransfer(data_mem_info->device, sparse_tensor.Location().device);
ORT_THROW_IF_ERROR(sparse_tensor.MakeBlockSparseData(*data_transfer, *data_mem_info, values_t_shape,
values, indices_t_shape, indices_data));
}
return nullptr;
#else
ORT_UNUSED_PARAMETER(ort_value);
ORT_UNUSED_PARAMETER(data_mem_info);
ORT_UNUSED_PARAMETER(values_shape);
ORT_UNUSED_PARAMETER(values_shape_len);
ORT_UNUSED_PARAMETER(values);
ORT_UNUSED_PARAMETER(indices_shape_data);
ORT_UNUSED_PARAMETER(indices_shape_len);
ORT_UNUSED_PARAMETER(indices_data);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::CreateSparseTensorWithValuesAsOrtValue, _In_ const OrtMemoryInfo* info, _Inout_ void* p_data,
_In_ const int64_t* dense_shape, size_t dense_shape_len,
_In_ const int64_t* values_shape, size_t values_shape_len,
ONNXTensorElementDataType type, _Outptr_ OrtValue** out) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
auto sparse_tensor_type = DataTypeImpl::SparseTensorTypeFromONNXEnum(type);
auto element_type = sparse_tensor_type->GetElementType();
assert(element_type->AsPrimitiveDataType() != nullptr);
if (utils::IsDataTypeString(element_type)) {
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT,
"Can not use strings in pre-allocated memory."
" Use CreateSparseTensorAsOrtValue() to allocate memory inside and copy");
}
TensorShape tensor_dense_shape(dense_shape, dense_shape_len);
TensorShape tensor_values_shape(values_shape, values_shape_len);
if (std::any_of(tensor_values_shape.GetDims().begin(), tensor_values_shape.GetDims().end(),
[](int64_t v) { return v < 0; })) {
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "tried creating tensor with negative value in shape");
}
auto value = std::make_unique<OrtValue>();
SparseTensor::InitOrtValue(element_type, tensor_dense_shape, tensor_values_shape, p_data, *info, *value);
*out = value.release();
return nullptr;
#else
ORT_UNUSED_PARAMETER(info);
ORT_UNUSED_PARAMETER(p_data);
ORT_UNUSED_PARAMETER(dense_shape);
ORT_UNUSED_PARAMETER(dense_shape_len);
ORT_UNUSED_PARAMETER(values_shape);
ORT_UNUSED_PARAMETER(values_shape_len);
ORT_UNUSED_PARAMETER(type);
ORT_UNUSED_PARAMETER(out);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::UseCooIndices, _Inout_ OrtValue* ort_value, _Inout_ int64_t* indices_data, size_t indices_num) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
auto v = reinterpret_cast<::OrtValue*>(ort_value);
auto& sparse_tensor = SparseTensor::GetSparseTensorFromOrtValue(*v);
auto indices_span = (indices_num == 0 || indices_data == nullptr)
? gsl::span<int64_t>()
: gsl::make_span(indices_data, indices_num);
ORT_THROW_IF_ERROR(sparse_tensor.UseCooIndices(indices_span));
return nullptr;
#else
ORT_UNUSED_PARAMETER(ort_value);
ORT_UNUSED_PARAMETER(indices_data);
ORT_UNUSED_PARAMETER(indices_num);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::UseCsrIndices, _Inout_ OrtValue* ort_value,
_Inout_ int64_t* inner_data, size_t inner_num,
_Inout_ int64_t* outer_data, size_t outer_num) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
auto& sparse_tensor = SparseTensor::GetSparseTensorFromOrtValue(*ort_value);
auto inner_span = (inner_num == 0 || inner_data == nullptr)
? gsl::span<int64_t>()
: gsl::make_span(inner_data, inner_num);
auto outer_span = (outer_num == 0 || outer_data == nullptr)
? gsl::span<int64_t>()
: gsl::make_span(outer_data, outer_num);
ORT_THROW_IF_ERROR(sparse_tensor.UseCsrIndices(inner_span, outer_span));
return nullptr;
#else
ORT_UNUSED_PARAMETER(ort_value);
ORT_UNUSED_PARAMETER(inner_data);
ORT_UNUSED_PARAMETER(inner_num);
ORT_UNUSED_PARAMETER(outer_data);
ORT_UNUSED_PARAMETER(outer_num);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::UseBlockSparseIndices, _Inout_ OrtValue* ort_value, const int64_t* indices_shape,
size_t indices_shape_len, _Inout_ int32_t* indices_data) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
auto& sparse_tensor = SparseTensor::GetSparseTensorFromOrtValue(*ort_value);
TensorShape ind_shape(indices_shape, indices_shape_len);
ORT_THROW_IF_ERROR(sparse_tensor.UseBlockSparseIndices(ind_shape, indices_data));
return nullptr;
#else
ORT_UNUSED_PARAMETER(ort_value);
ORT_UNUSED_PARAMETER(indices_shape);
ORT_UNUSED_PARAMETER(indices_shape_len);
ORT_UNUSED_PARAMETER(indices_data);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::GetSparseTensorFormat, _In_ const OrtValue* ort_value, _Out_ enum OrtSparseFormat* out) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
auto v = reinterpret_cast<const ::OrtValue*>(ort_value);
if (!v->IsAllocated()) {
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "the ort_value must contain a constructed tensor");
}
const auto& sparse_tensor = v->Get<SparseTensor>();
*out = static_cast<OrtSparseFormat>(sparse_tensor.Format());
return nullptr;
#else
ORT_UNUSED_PARAMETER(ort_value);
ORT_UNUSED_PARAMETER(out);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::GetSparseTensorValues, _In_ const OrtValue* ort_value, _Outptr_ const void** out) {
API_IMPL_BEGIN
#if !defined(DISABLE_SPARSE_TENSORS)
const auto& sparse_tensor = SparseTensor::GetSparseTensorFromOrtValue(*ort_value);
if (sparse_tensor.IsDataTypeString()) {
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "Use GetStringTensor*() API to retrieve strings");
}
const auto& values = sparse_tensor.Values();
*out = values.DataRaw();
return nullptr;
#else
ORT_UNUSED_PARAMETER(ort_value);
ORT_UNUSED_PARAMETER(out);
return OrtApis::CreateStatus(ORT_FAIL, "SparseTensor is not supported in this build.");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::CreateCustomOpDomain, _In_ const char* domain, _Outptr_ OrtCustomOpDomain** out) {
API_IMPL_BEGIN
auto custom_op_domain = std::make_unique<OrtCustomOpDomain>();
custom_op_domain->domain_ = domain;
*out = custom_op_domain.release();
return nullptr;
API_IMPL_END
}
ORT_API(void, OrtApis::ReleaseCustomOpDomain, _Frees_ptr_opt_ OrtCustomOpDomain* ptr) {
delete ptr;
}
ORT_API_STATUS_IMPL(OrtApis::CustomOpDomain_Add, _Inout_ OrtCustomOpDomain* custom_op_domain, _In_ const OrtCustomOp* op) {
API_IMPL_BEGIN
custom_op_domain->custom_ops_.emplace_back(op);
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::AddCustomOpDomain, _Inout_ OrtSessionOptions* options,
_In_ OrtCustomOpDomain* custom_op_domain) {
API_IMPL_BEGIN
options->custom_op_domains_.emplace_back(custom_op_domain);
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::RegisterCustomOpsLibrary, _Inout_ OrtSessionOptions* options, _In_ const char* library_path, _Outptr_ void** library_handle) {
API_IMPL_BEGIN
auto path_str = ToPathString(library_path);
ORT_API_RETURN_IF_STATUS_NOT_OK(Env::Default().LoadDynamicLibrary(path_str, false, library_handle));
if (!*library_handle)
return OrtApis::CreateStatus(ORT_FAIL, "RegisterCustomOpsLibrary: Failed to load library");
RegisterCustomOpsFn RegisterCustomOps;
ORT_API_RETURN_IF_STATUS_NOT_OK(Env::Default().GetSymbolFromLibrary(*library_handle, "RegisterCustomOps",
(void**)&RegisterCustomOps));
if (!RegisterCustomOps)
return OrtApis::CreateStatus(ORT_FAIL, "RegisterCustomOpsLibrary: Entry point RegisterCustomOps not found in library");
return RegisterCustomOps(options, OrtGetApiBase());
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::RegisterCustomOpsLibrary_V2, _Inout_ OrtSessionOptions* options,
_In_ const ORTCHAR_T* library_name) {
API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD) || defined(ORT_MINIMAL_BUILD_CUSTOM_OPS)
ORT_API_RETURN_IF_STATUS_NOT_OK(options->RegisterCustomOpsLibrary(library_name));
return nullptr;
#else
ORT_UNUSED_PARAMETER(options);
ORT_UNUSED_PARAMETER(library_name);
return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Custom operator libraries are not supported in this build");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::RegisterCustomOpsUsingFunction, _Inout_ OrtSessionOptions* options,
_In_ const char* registration_func_name) {
API_IMPL_BEGIN
#if !defined(ORT_MINIMAL_BUILD) || defined(ORT_MINIMAL_BUILD_CUSTOM_OPS)
if (!registration_func_name) {
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT,
"RegisterCustomOpsUsingFunction: Registration function name must be specified.");
}
RegisterCustomOpsFn RegisterCustomOps;
ORT_API_RETURN_IF_STATUS_NOT_OK(Env::Default().GetSymbolFromLibrary(nullptr, registration_func_name,
(void**)&RegisterCustomOps));
if (!RegisterCustomOps) {
return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT,
"RegisterCustomOpsUsingFunction: Registration function was not found");
}
return RegisterCustomOps(options, OrtGetApiBase());
#else
ORT_UNUSED_PARAMETER(options);
ORT_UNUSED_PARAMETER(registration_func_name);
return OrtApis::CreateStatus(ORT_NOT_IMPLEMENTED, "Custom operator libraries are not supported in this build");
#endif
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::EnableOrtCustomOps, _Inout_ OrtSessionOptions* options) {
API_IMPL_BEGIN
if (options) {
#ifdef ENABLE_EXTENSION_CUSTOM_OPS
return RegisterCustomOps(options, OrtGetApiBase());
#else
return OrtApis::CreateStatus(ORT_FAIL, "EnableOrtCustomOps: Custom operators in onnxruntime-extensions are not enabled");
#endif
}
return nullptr;
API_IMPL_END
}
namespace {
// provider either model_path, or modal_data + model_data_length.
static ORT_STATUS_PTR 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) {
// 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";
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->GetEnvironment(),
model_path);
} else {
sess = std::make_unique<onnxruntime::InferenceSession>(
options == nullptr ? onnxruntime::SessionOptions() : options->value,
env->GetEnvironment(),
model_data, static_cast<int>(model_data_length));
}
#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->GetEnvironment());
}
#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
// 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, static_cast<int>(model_data_length)));
}
}
return nullptr;
}
static ORT_STATUS_PTR InitializeSession(_In_ const OrtSessionOptions* options,
_In_ std::unique_ptr<::onnxruntime::InferenceSession>& sess,
_Inout_opt_ OrtPrepackedWeightsContainer* prepacked_weights_container = nullptr) {
// we need to disable mem pattern if DML is one of the providers since DML doesn't have the concept of
// byte addressable memory
std::vector<std::unique_ptr<IExecutionProvider>> provider_list;
if (options) {
for (auto& factory : options->provider_factories) {
auto provider = factory->CreateProvider();
provider_list.push_back(std::move(provider));
}
}
// register the providers
for (auto& provider : provider_list) {
if (provider) {
ORT_API_RETURN_IF_STATUS_NOT_OK(sess->RegisterExecutionProvider(std::move(provider)));
}
}
if (prepacked_weights_container != nullptr) {
ORT_API_RETURN_IF_STATUS_NOT_OK(sess->AddPrePackedWeightsContainer(
reinterpret_cast<PrepackedWeightsContainer*>(prepacked_weights_container)));
}
ORT_API_RETURN_IF_STATUS_NOT_OK(sess->Initialize());
return nullptr;
}
} // namespace
ORT_API_STATUS_IMPL(OrtApis::CreateSession, _In_ const OrtEnv* env, _In_ const ORTCHAR_T* model_path,
_In_ const OrtSessionOptions* options, _Outptr_ OrtSession** out) {
API_IMPL_BEGIN
std::unique_ptr<onnxruntime::InferenceSession> sess;
OrtStatus* status = nullptr;
*out = nullptr;
ORT_TRY {
ORT_API_RETURN_IF_ERROR(CreateSessionAndLoadModel(options, env, model_path, nullptr, 0, sess));
ORT_API_RETURN_IF_ERROR(InitializeSession(options, sess));
*out = reinterpret_cast<OrtSession*>(sess.release());
}
ORT_CATCH(const std::exception& e) {
ORT_HANDLE_EXCEPTION([&]() {
status = OrtApis::CreateStatus(ORT_FAIL, e.what());
});
}
return status;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::CreateSessionFromArray, _In_ const OrtEnv* env, _In_ const void* model_data,
size_t model_data_length, _In_ const OrtSessionOptions* options, _Outptr_ OrtSession** out) {
API_IMPL_BEGIN
std::unique_ptr<onnxruntime::InferenceSession> sess;
OrtStatus* status = nullptr;
*out = nullptr;
ORT_TRY {
ORT_API_RETURN_IF_ERROR(CreateSessionAndLoadModel(options, env, nullptr, model_data, model_data_length, sess));
ORT_API_RETURN_IF_ERROR(InitializeSession(options, sess));
*out = reinterpret_cast<OrtSession*>(sess.release());
}
ORT_CATCH(const std::exception& e) {
ORT_HANDLE_EXCEPTION([&]() {
status = OrtApis::CreateStatus(ORT_FAIL, e.what());
});
}
return status;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::Run, _Inout_ OrtSession* sess, _In_opt_ const OrtRunOptions* run_options,
_In_reads_(input_len) const char* const* input_names,
_In_reads_(input_len) const OrtValue* const* input, size_t input_len,
_In_reads_(output_names_len) const char* const* output_names, size_t output_names_len,
_Inout_updates_all_(output_names_len) OrtValue** output) {
API_IMPL_BEGIN
auto session = reinterpret_cast<::onnxruntime::InferenceSession*>(sess);
gsl::span<const char* const> input_names_span(input_names, input_len);
gsl::span<const OrtValue* const> input_span(input, input_len);
gsl::span<const char* const> output_name_span(output_names, output_names_len);
gsl::span<OrtValue*> output_span(output, output_names_len);
Status status;
if (run_options) {
status = session->Run(*run_options,
input_names_span,
input_span,
output_name_span,
output_span);
} else {
const RunOptions default_run_options;
status = session->Run(default_run_options,
input_names_span,
input_span,
output_name_span,
output_span);
}
return ToOrtStatus(status);
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::RunAsync, _Inout_ OrtSession* sess, _In_opt_ const OrtRunOptions* run_options,
_In_reads_(input_len) const char* const* input_names,
_In_reads_(input_len) const OrtValue* const* input, size_t input_len,
_In_reads_(output_names_len) const char* const* output_names, size_t output_names_len,
_Inout_updates_all_(output_names_len) OrtValue** output,
_In_ RunAsyncCallbackFn run_async_callback, _In_opt_ void* user_data) {
API_IMPL_BEGIN
auto session = reinterpret_cast<::onnxruntime::InferenceSession*>(sess);
gsl::span<const char* const> input_names_span(input_names, input_len);
gsl::span<const OrtValue* const> input_span(input, input_len);
gsl::span<const char* const> output_name_span(output_names, output_names_len);
gsl::span<OrtValue*> output_span(output, output_names_len);
return ToOrtStatus(session->RunAsync(run_options,
input_names_span,
input_span,
output_name_span,
output_span,
run_async_callback,
user_data));
API_IMPL_END
}
struct OrtIoBinding {
std::unique_ptr<::onnxruntime::IOBinding> binding_;
explicit OrtIoBinding(std::unique_ptr<::onnxruntime::IOBinding>&& binding) : binding_(std::move(binding)) {}
OrtIoBinding(const OrtIoBinding&) = delete;
OrtIoBinding& operator=(const OrtIoBinding&) = delete;
};
ORT_API_STATUS_IMPL(OrtApis::RunWithBinding, _Inout_ OrtSession* sess, _In_ const OrtRunOptions* run_options,
_In_ const OrtIoBinding* binding_ptr) {
API_IMPL_BEGIN
auto session = reinterpret_cast<::onnxruntime::InferenceSession*>(sess);
Status status;
if (run_options == nullptr) {
OrtRunOptions default_run_options;
status = session->Run(default_run_options, *binding_ptr->binding_);
} else {
status = session->Run(*run_options, *binding_ptr->binding_);
}
if (!status.IsOK()) {
return ToOrtStatus(status);
}
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::CreateIoBinding, _Inout_ OrtSession* sess, _Outptr_ OrtIoBinding** out) {
API_IMPL_BEGIN
auto session = reinterpret_cast<::onnxruntime::InferenceSession*>(sess);
std::unique_ptr<::onnxruntime::IOBinding> binding;
auto status = session->NewIOBinding(&binding);
if (!status.IsOK()) {
return ToOrtStatus(status);
}
*out = std::make_unique<OrtIoBinding>(std::move(binding)).release();
return nullptr;
API_IMPL_END
}
ORT_API(void, OrtApis::ReleaseIoBinding, _Frees_ptr_opt_ OrtIoBinding* binding_ptr) {
delete binding_ptr;
}
ORT_API_STATUS_IMPL(OrtApis::BindInput, _Inout_ OrtIoBinding* binding_ptr, _In_ const char* name, _In_ const OrtValue* val_ptr) {
API_IMPL_BEGIN
auto st = binding_ptr->binding_->BindInput(name, *val_ptr);
if (!st.IsOK()) {
return ToOrtStatus(st);
}
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::BindOutput, _Inout_ OrtIoBinding* binding_ptr, _In_ const char* name, _In_ const OrtValue* val_ptr) {
API_IMPL_BEGIN
auto st = binding_ptr->binding_->BindOutput(name, *val_ptr);
if (!st.IsOK()) {
return ToOrtStatus(st);
}
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::BindOutputToDevice, _Inout_ OrtIoBinding* binding_ptr, _In_ const char* name, _In_ const OrtMemoryInfo* mem_info_ptr) {
API_IMPL_BEGIN
auto st = binding_ptr->binding_->BindOutput(name, mem_info_ptr->device);
if (!st.IsOK()) {
return ToOrtStatus(st);
}
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::GetBoundOutputNames, _In_ const OrtIoBinding* binding_ptr, _In_ OrtAllocator* allocator,
_Out_ char** buffer, _Outptr_result_maybenull_ size_t** lengths, _Out_ size_t* count) {
API_IMPL_BEGIN
const auto& output_names = binding_ptr->binding_->GetOutputNames();
if (output_names.empty()) {
*buffer = nullptr;
*lengths = nullptr;
*count = 0U;
return nullptr;
}
IAllocatorUniquePtr<size_t> lengths_alloc(reinterpret_cast<size_t*>(allocator->Alloc(allocator, output_names.size() * sizeof(size_t))),
[allocator](size_t* p) { if(p) allocator->Free(allocator, p); });
if (!lengths_alloc) {
return OrtApis::CreateStatus(ORT_FAIL, "lengths allocation failed");
}
size_t total_len = 0;
auto* len_ptr = lengths_alloc.get();
for (const auto& n : output_names) {
auto sz = n.size();
total_len += sz;
*len_ptr++ = sz;
}
IAllocatorUniquePtr<char> buffer_alloc(reinterpret_cast<char*>(allocator->Alloc(allocator, total_len * sizeof(char))),
[allocator](char* p) { if(p) allocator->Free(allocator, p); });
if (!buffer_alloc) {
return OrtApis::CreateStatus(ORT_FAIL, "string buffer allocation failed");
}
char* buf_ptr = buffer_alloc.get();
for (const auto& n : output_names) {
auto sz = n.size();
memcpy(buf_ptr, n.data(), sz);
buf_ptr += sz;
}
*buffer = buffer_alloc.release();
*lengths = lengths_alloc.release();
*count = output_names.size();
return nullptr;
API_IMPL_END
}
ORT_API_STATUS_IMPL(OrtApis::GetBoundOutputValues, _In_ const OrtIoBinding* binding_ptr, _In_ OrtAllocator* allocator,
_Outptr_result_maybenull_ OrtValue*** output, _Out_ size_t* output_count) {
API_IMPL_BEGIN
const auto& outputs = binding_ptr->binding_->GetOutputs();
if (outputs.empty()) {
*output = nullptr;
*output_count = 0U;
return nullptr;
}