executorch-sys 0.13.0

Unsafe Rust bindings for ExecuTorch - On-device AI across mobile, embedded and edge for PyTorch
Documentation
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
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310

#include <cstddef>
#include <vector>

#include "executorch_rs/c_bridge.h"
#include "executorch_rs/layout.hpp"

#include "executorch/runtime/platform/platform.h"
#include "executorch/runtime/core/error.h"
#include "executorch/runtime/core/span.h"
#include "executorch/runtime/executor/program.h"
#include "executorch/runtime/executor/memory_manager.h"
#include "executorch/runtime/core/hierarchical_allocator.h"
#include "executorch/runtime/backend/options.h"
#include "executorch/runtime/backend/backend_options_map.h"
#include "executorch/runtime/core/exec_aten/exec_aten.h"
#include "executorch/runtime/core/exec_aten/util/tensor_util.h"
#include "executorch/runtime/core/exec_aten/util/dim_order_util.h"
#include "executorch/runtime/platform/assert.h"

#include "executorch/runtime/core/data_loader.h"
#include "executorch/extension/data_loader/buffer_data_loader.h"
#if defined(EXECUTORCH_RS_DATA_LOADER)
#include "executorch/extension/data_loader/file_data_loader.h"
#include "executorch/extension/data_loader/mmap_data_loader.h"
#endif

#if defined(EXECUTORCH_RS_FLAT_TENSOR)
#include "executorch/extension/flat_tensor/flat_tensor_data_map.h"
#endif

#if defined(EXECUTORCH_RS_ETDUMP)
#include "executorch/devtools/etdump/etdump_flatcc.h"
#endif

#if defined(EXECUTORCH_RS_STD)
#include "executorch/extension/memory_allocator/malloc_memory_allocator.h"
#endif

// Layout asserts
namespace
{
    using executorch_rs::is_equal_layout;

    static_assert(is_equal_layout<struct ET_ArrayRefF64, executorch::aten::ArrayRef<double>>());
    static_assert(is_equal_layout<struct ET_ArrayRefBool, executorch::aten::ArrayRef<bool>>());
    static_assert(is_equal_layout<struct ET_ArrayRefChar, executorch::aten::ArrayRef<char>>());
    static_assert(is_equal_layout<struct ET_BoxedEvalueListI64, executorch::runtime::BoxedEvalueList<int64_t>>());
    static_assert(is_equal_layout<struct ET_BoxedEvalueListTensor, executorch::runtime::BoxedEvalueList<executorch::aten::Tensor>>());
    static_assert(is_equal_layout<struct ET_BoxedEvalueListOptionalTensor, executorch::runtime::BoxedEvalueList<std::optional<executorch::aten::Tensor>>>());
    static_assert(is_equal_layout<struct ET_EValueStorage, executorch::runtime::EValue>());
    static_assert(is_equal_layout<struct ET_TensorStorage, executorch::aten::Tensor>());
    static_assert(is_equal_layout<struct ET_OptionalTensorStorage, executorch::aten::optional<executorch::aten::Tensor>>());

    static_assert(is_equal_layout<struct ET_TensorImpl, executorch::aten::TensorImpl>());
    static_assert(std::is_trivially_move_constructible_v<executorch::aten::TensorImpl>);

    static_assert(is_equal_layout<struct ET_FreeableBuffer, executorch::runtime::FreeableBuffer>());
    static_assert(is_equal_layout<struct ET_Program, executorch::runtime::Program>());
    // ET_Program is not trivially move constructible because it has a ET_FreeableBuffer field that
    // has a custom move constructor.
    // ET_FreeableBuffer has a custom move constructor and a destructor, but the move is trivial +cleaning
    // of the old object, which behave great with Rust move semantics as long as we only call the
    // destructor on the final object.
    //
    // static_assert(std::is_trivially_move_constructible_v<executorch::runtime::Program>);

    static_assert(is_equal_layout<struct ET_Device, executorch::runtime::etensor::Device>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::etensor::Device>);
    static_assert(is_equal_layout<enum ET_DeviceType, executorch::runtime::etensor::DeviceType>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::etensor::DeviceType>);
    static_assert(is_equal_layout<int8_t, executorch::runtime::etensor::DeviceIndex>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::etensor::DeviceIndex>);

    static_assert(is_equal_layout<struct ET_BackendOption, executorch::runtime::BackendOption>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::BackendOption>);
    static_assert(std::is_trivially_destructible_v<executorch::runtime::BackendOption>);
    static_assert(is_equal_layout<struct ET_LoadBackendOptionsMap, executorch::runtime::LoadBackendOptionsMap>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::LoadBackendOptionsMap>);
    static_assert(std::is_trivially_destructible_v<executorch::runtime::LoadBackendOptionsMap>);

    static_assert(is_equal_layout<struct ET_TensorInfo, executorch::runtime::TensorInfo>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::TensorInfo>);

    static_assert(is_equal_layout<struct ET_TensorLayout, executorch::runtime::TensorLayout>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::TensorLayout>);

    static_assert(is_equal_layout<struct ET_MethodMeta, executorch::runtime::MethodMeta>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::MethodMeta>);

    static_assert(is_equal_layout<struct ET_Method, executorch::runtime::Method>());
    // ET_Method has a move constructor that just clean the old object to avoid double free.
    // Its OK to move it in Rust because the old object is forgotten.
    //
    // static_assert(std::is_trivially_move_constructible_v<executorch::runtime::Method>);

#if defined(EXECUTORCH_RS_FLAT_TENSOR)
    static_assert(is_equal_layout<struct ET_FlatTensorDataMap, executorch::extension::FlatTensorDataMap>());
    // ET_FlatTensorDataMap is not trivially move constructible because it has a vtable with virtual
    // destructor inherited from DataLoader, but it has an empty implementation for it therefore
    // it is safe.
    //
    // static_assert(std::is_trivially_move_constructible_v<executorch::extension::FlatTensorDataMap>);
#endif

    static_assert(is_equal_layout<struct ET_BufferDataLoader, executorch::extension::BufferDataLoader>());
    // ET_BufferDataLoader is not trivially move constructible because it has a vtable with virtual
    // destructor inherited from DataLoader, but it has an empty implementation for it therefore
    // it is safe.
    //
    // static_assert(std::is_trivially_move_constructible_v<executorch::extension::BufferDataLoader>);

#if defined(EXECUTORCH_RS_DATA_LOADER)
    static_assert(is_equal_layout<struct ET_FileDataLoader, executorch::extension::FileDataLoader>());
    // ET_FileDataLoader has a custom move constructor and a destructor, but the move is trivial +cleaning
    // of the old object, which behave great with Rust move semantics as long as we only call the
    // destructor on the final object.
    //
    // static_assert(std::is_trivially_move_constructible_v<executorch::extension::FileDataLoader>);

    static_assert(is_equal_layout<struct ET_MmapDataLoader, executorch::extension::MmapDataLoader>());
    // ET_MmapDataLoader has a custom move constructor and a destructor, but the move is trivial +cleaning
    // of the old object, which behave great with Rust move semantics as long as we only call the
    // destructor on the final object.
    //
    // static_assert(std::is_trivially_move_constructible_v<executorch::extension::MmapDataLoader>);
#endif

    static_assert(is_equal_layout<struct ET_MemoryAllocator, executorch::runtime::MemoryAllocator>());
    // ET_MemoryAllocator is not trivially move constructible because it has a vtable with virtual
    // destructor, but when we have a concrete instance of it there is nothing virtual and no move
    // constructor, so it is safe to move it in Rust.
    //
    // static_assert(std::is_trivially_move_constructible_v<executorch::runtime::MemoryAllocator>);

    static_assert(is_equal_layout<struct ET_SpanDevice, executorch::runtime::Span<const executorch::runtime::etensor::Device>>());
    static_assert(is_equal_layout<struct ET_HierarchicalAllocator, executorch::runtime::HierarchicalAllocator>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::HierarchicalAllocator>);

    static_assert(is_equal_layout<struct ET_MemoryManager, executorch::runtime::MemoryManager>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::MemoryManager>);

#if defined(EXECUTORCH_RS_ETDUMP)
    static_assert(is_equal_layout<struct ET_DumpGen, executorch::etdump::ETDumpGen>());
// ET_MemoryAllocator is not trivially move constructible because it has a vtable with virtual
// destructor, but when we have a concrete instance of it there is nothing virtual and no move
// constructor, so it is safe to move it in Rust.
//
// static_assert(std::is_trivially_move_constructible_v<executorch::etdump::ETDumpGen>);
#endif

    static_assert(is_equal_layout<executorch_timestamp_t, et_timestamp_t>());
    static_assert(std::is_trivially_move_constructible_v<et_timestamp_t>);
    static_assert(is_equal_layout<struct executorch_tick_ratio, et_tick_ratio_t>());
    static_assert(std::is_trivially_move_constructible_v<et_tick_ratio_t>);
    static_assert(is_equal_layout<enum executorch_pal_log_level, et_pal_log_level_t>());
    static_assert(std::is_trivially_move_constructible_v<et_pal_log_level_t>);
    static_assert(is_equal_layout<struct ExecutorchPalImpl, executorch::runtime::PalImpl>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::PalImpl>);
}

constexpr size_t MAX_DIM = 16;

using executorch_rs::checked_reinterpret_cast;

#if defined(EXECUTORCH_RS_STD)
void executorch_VecChar_destructor(struct ET_VecChar *vec)
{
    delete[] vec->data;
}
void executorch_VecVecChar_destructor(struct ET_VecVecChar *vec)
{
    for (size_t i = 0; i < vec->len; i++)
    {
        executorch_VecChar_destructor(&vec->data[i]);
    }
    delete[] vec->data;
}
void executorch_VecEValue_destructor(struct ET_VecEValue *vec)
{
    // Its safe to call the destructor of elements in `vec->data[len..cap]` because we created them with `new T[len]`
    // aka default constructor
    auto data = reinterpret_cast<executorch::runtime::EValue *>(vec->data.ptr);
    delete[] data;
}
#endif

template <typename T>
static enum ET_Error extract_result(const executorch::runtime::Result<T> &&result, T *output)
{
    if (result.ok())
        *output = std::move(result.get());
    return static_cast<ET_Error>(result.error());
}

struct ET_MemoryAllocator executorch_MemoryAllocator_new(uint32_t size, uint8_t *base_address)
{
    struct ET_MemoryAllocator self;
    auto self_ = checked_reinterpret_cast<executorch::runtime::MemoryAllocator>(&self);
    new (self_) executorch::runtime::MemoryAllocator(size, base_address);
    return self;
}
void *executorch_MemoryAllocator_allocate(struct ET_MemoryAllocator *self, size_t size, size_t alignment)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MemoryAllocator>(self);
    return self_->allocate(size, alignment);
}
size_t executorch_MemoryAllocator_used_size(const struct ET_MemoryAllocator *self)
{
    auto self_ = checked_reinterpret_cast<const executorch::runtime::MemoryAllocator>(self);
    return self_->used_size();
}
size_t executorch_MemoryAllocator_free_size(const struct ET_MemoryAllocator *self)
{
    auto self_ = checked_reinterpret_cast<const executorch::runtime::MemoryAllocator>(self);
    return self_->free_size();
}
struct ET_HierarchicalAllocator executorch_HierarchicalAllocator_new(struct ET_SpanSpanU8 buffers)
{
    auto buffers_ = *checked_reinterpret_cast<executorch::runtime::Span<executorch::runtime::Span<uint8_t>>>(&buffers);
    ET_CHECK((void *)buffers_.begin() == (void *)buffers.data);
    ET_CHECK(buffers_.size() == buffers.len);
    struct ET_HierarchicalAllocator self;
    auto self_ = checked_reinterpret_cast<executorch::runtime::HierarchicalAllocator>(&self);
    new (self_) executorch::runtime::HierarchicalAllocator(buffers_);
    return self;
}
struct ET_HierarchicalAllocator executorch_HierarchicalAllocator_new_with_devices(struct ET_SpanSpanU8 buffers, struct ET_SpanDevice planned_buffer_devices)
{
    auto buffers_ = *checked_reinterpret_cast<executorch::runtime::Span<executorch::runtime::Span<uint8_t>>>(&buffers);
    ET_CHECK((void *)buffers_.begin() == (void *)buffers.data);
    ET_CHECK(buffers_.size() == buffers.len);
    auto devices_ = *checked_reinterpret_cast<executorch::runtime::Span<const executorch::runtime::etensor::Device>>(&planned_buffer_devices);
    ET_CHECK((const void *)devices_.begin() == (const void *)planned_buffer_devices.data);
    ET_CHECK(devices_.size() == planned_buffer_devices.len);
    struct ET_HierarchicalAllocator self;
    auto self_ = checked_reinterpret_cast<executorch::runtime::HierarchicalAllocator>(&self);
    new (self_) executorch::runtime::HierarchicalAllocator(buffers_, devices_);
    return self;
}
struct ET_SpanDevice executorch_HierarchicalAllocator_planned_buffer_devices(const struct ET_HierarchicalAllocator *self)
{
    auto self_ = checked_reinterpret_cast<const executorch::runtime::HierarchicalAllocator>(self);
    auto devices = self_->planned_buffer_devices();
    return ET_SpanDevice{
        .data = reinterpret_cast<const struct ET_Device *>(devices.data()),
        .len = devices.size(),
    };
}
void executorch_HierarchicalAllocator_destructor(struct ET_HierarchicalAllocator *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::HierarchicalAllocator>(self);
    self_->~HierarchicalAllocator();
}
struct ET_MemoryManager executorch_MemoryManager_new(
    struct ET_MemoryAllocator *method_allocator,
    struct ET_HierarchicalAllocator *planned_memory,
    struct ET_MemoryAllocator *temp_allocator)
{
    auto method_allocator_ = checked_reinterpret_cast<executorch::runtime::MemoryAllocator>(method_allocator);
    auto planned_memory_ = checked_reinterpret_cast<executorch::runtime::HierarchicalAllocator>(planned_memory);
    auto temp_allocator_ = checked_reinterpret_cast<executorch::runtime::MemoryAllocator>(temp_allocator);

    struct ET_MemoryManager self;
    auto self_ = checked_reinterpret_cast<executorch::runtime::MemoryManager>(&self);
    new (self_) executorch::runtime::MemoryManager(method_allocator_, planned_memory_, temp_allocator_);
    return self;
}
struct ET_SpanDevice executorch_MemoryManager_planned_buffer_devices(const struct ET_MemoryManager *self)
{
    auto self_ = checked_reinterpret_cast<const executorch::runtime::MemoryManager>(self);
    auto devices = self_->planned_buffer_devices();
    return ET_SpanDevice{
        .data = reinterpret_cast<const struct ET_Device *>(devices.data()),
        .len = devices.size(),
    };
}
bool executorch_MemoryManager_has_device_memory(const struct ET_MemoryManager *self)
{
    auto self_ = checked_reinterpret_cast<const executorch::runtime::MemoryManager>(self);
    return self_->has_device_memory();
}

// Loaders
static executorch::runtime::DataLoader *cast_data_loader_mut(struct ET_DataLoaderRefMut loader)
{
    return reinterpret_cast<executorch::runtime::DataLoader *>(loader.ptr);
}
static struct ET_DataLoaderRefMut cast_data_loader_mut(executorch::runtime::DataLoader *loader)
{
    return ET_DataLoaderRefMut{.ptr = loader};
}
struct ET_BufferDataLoader executorch_BufferDataLoader_new(const void *data, size_t size)
{
    struct ET_BufferDataLoader loader;
    auto loader_ = checked_reinterpret_cast<executorch::extension::BufferDataLoader>(&loader);
    new (loader_) executorch::extension::BufferDataLoader(data, size);
    return loader;
}
struct ET_DataLoaderRefMut executorch_BufferDataLoader_as_data_loader_mut(struct ET_BufferDataLoader *self)
{
    auto self_ = checked_reinterpret_cast<executorch::extension::BufferDataLoader>(self);
    auto loader = static_cast<executorch::runtime::DataLoader *>(self_);
    return cast_data_loader_mut(loader);
}
#if defined(EXECUTORCH_RS_DATA_LOADER)
enum ET_Error executorch_FileDataLoader_new(const char *file_path, size_t alignment, struct ET_FileDataLoader *out)
{
    auto out_ = checked_reinterpret_cast<executorch::extension::FileDataLoader>(out);
    // return extract_result(std::move(executorch::extension::FileDataLoader::from(file_path, alignment)), out);
    auto res = executorch::extension::FileDataLoader::from(file_path, alignment);
    if (!res.ok())
        return static_cast<ET_Error>(res.error());
    auto &loader = res.get();
    new (out_) executorch::extension::FileDataLoader(std::move(loader));
    return ET_Error::ET_Error_Ok;
}
void executorch_FileDataLoader_destructor(struct ET_FileDataLoader *self)
{
    auto self_ = checked_reinterpret_cast<executorch::extension::FileDataLoader>(self);
    self_->~FileDataLoader();
}
struct ET_DataLoaderRefMut executorch_FileDataLoader_as_data_loader_mut(struct ET_FileDataLoader *self)
{
    auto self_ = checked_reinterpret_cast<executorch::extension::FileDataLoader>(self);
    auto loader = static_cast<executorch::runtime::DataLoader *>(self_);
    return cast_data_loader_mut(loader);
}
enum ET_Error executorch_MmapDataLoader_new(const char *file_path, enum ET_MmapDataLoaderMlockConfig mlock_config, struct ET_MmapDataLoader *out)
{
    auto mlock_config_ = static_cast<executorch::extension::MmapDataLoader::MlockConfig>(mlock_config);
    auto out_ = checked_reinterpret_cast<executorch::extension::MmapDataLoader>(out);
    // return extract_result(executorch::extension::MmapDataLoader::from(file_path, mlock_config), out);
    auto res = executorch::extension::MmapDataLoader::from(file_path, mlock_config_);
    if (!res.ok())
        return static_cast<ET_Error>(res.error());
    auto &loader = res.get();
    new (out_) executorch::extension::MmapDataLoader(std::move(loader));
    return ET_Error::ET_Error_Ok;
}
void executorch_MmapDataLoader_destructor(struct ET_MmapDataLoader *self)
{
    auto self_ = checked_reinterpret_cast<executorch::extension::MmapDataLoader>(self);
    self_->~MmapDataLoader();
}
struct ET_DataLoaderRefMut executorch_MmapDataLoader_as_data_loader_mut(struct ET_MmapDataLoader *self)
{
    auto self_ = checked_reinterpret_cast<executorch::extension::MmapDataLoader>(self);
    auto loader = static_cast<executorch::runtime::DataLoader *>(self_);
    return cast_data_loader_mut(loader);
}
#endif

// NamedDataMap
enum ET_Error executorch_NamedDataMap_get_tensor_layout(
    struct ET_NamedDataMapRef self,
    struct ET_ArrayRefChar key,
    struct ET_TensorLayout *out)
{
    auto self_ = reinterpret_cast<const executorch::runtime::NamedDataMap *>(self.ptr);
    std::string_view key_(key.data, key.len);
    auto res = self_->get_tensor_layout(key_);
    auto out_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(out);
    if (!res.ok())
        return static_cast<ET_Error>(res.error());
    memcpy(out_, &res.get(), sizeof(executorch::runtime::TensorLayout));
    return ET_Error::ET_Error_Ok;
}
enum ET_Error executorch_NamedDataMap_get_num_keys(struct ET_NamedDataMapRef self, uint32_t *out)
{
    auto self_ = reinterpret_cast<const executorch::runtime::NamedDataMap *>(self.ptr);
    return extract_result(self_->get_num_keys(), out);
}
enum ET_Error executorch_NamedDataMap_get_key(
    struct ET_NamedDataMapRef self,
    uint32_t index,
    const char **out_data)
{
    auto self_ = reinterpret_cast<const executorch::runtime::NamedDataMap *>(self.ptr);
    auto res = self_->get_key(index);
    if (!res.ok())
        return static_cast<ET_Error>(res.error());
    *out_data = res.get();
    return ET_Error::ET_Error_Ok;
}

#if defined(EXECUTORCH_RS_FLAT_TENSOR)
// ET_FlatTensorDataMap
enum ET_Error executorch_FlatTensorDataMap_load(struct ET_DataLoaderRefMut loader, struct ET_FlatTensorDataMap *out)
{
    auto loader_ = cast_data_loader_mut(loader);
    auto out_ = checked_reinterpret_cast<executorch::extension::FlatTensorDataMap>(out);
    // return extract_result(executorch::extension::FlatTensorDataMap::load(loader_), out_);
    auto res = executorch::extension::FlatTensorDataMap::load(loader_);
    if (!res.ok())
        return static_cast<ET_Error>(res.error());
    auto &program = res.get();
    new (out_) executorch::extension::FlatTensorDataMap(std::move(program));
    return ET_Error::ET_Error_Ok;
}
struct ET_NamedDataMapRefMut executorch_FlatTensorDataMap_as_named_data_map_mut(struct ET_FlatTensorDataMap *self)
{
    auto self_ = checked_reinterpret_cast<executorch::extension::FlatTensorDataMap>(self);
    auto named_data_map = static_cast<executorch::runtime::NamedDataMap *>(self_);
    return ET_NamedDataMapRefMut{.ptr = named_data_map};
}
#endif

// Tensor
static const executorch::aten::Tensor *cast_tensor(struct ET_TensorRef tensor)
{
    return reinterpret_cast<const executorch::aten::Tensor *>(tensor.ptr);
}
static struct ET_TensorRef cast_tensor(const executorch::aten::Tensor *tensor)
{
    return ET_TensorRef{.ptr = tensor};
}
static executorch::aten::Tensor *cast_tensor_mut(struct ET_TensorRefMut tensor)
{
    return reinterpret_cast<executorch::aten::Tensor *>(tensor.ptr);
}
// static TensorMut cast_tensor_mut(executorch::aten::Tensor *tensor)
// {
//     return reinterpret_cast<TensorMut>(tensor);
// }

bool executorch_is_valid_dim_order_and_strides(size_t dim, const ET_SizesType *sizes, const ET_DimOrderType *dim_order, const ET_StridesType *strides)
{
    ET_CHECK_MSG(dim <= MAX_DIM, "dim > 16");

    ET_StridesType computed_strides[MAX_DIM];
    auto error = executorch::runtime::dim_order_to_stride(
        sizes, dim_order, dim, &computed_strides[0]);
    if (error != executorch::runtime::Error::Ok)
        return false; // Invalid dim order

    for (size_t i = 0; i < dim; i++)
        if (computed_strides[i] != strides[i])
            return false;
    return true;
}
enum ET_Error executorch_stride_to_dim_order(const ET_StridesType *strides, size_t dims, ET_DimOrderType *dim_order)
{
    return static_cast<ET_Error>(executorch::runtime::stride_to_dim_order(strides, dims, dim_order));
}

void executorch_TensorImpl_new(
    struct ET_TensorImpl *self,
    enum ET_ScalarType type,
    size_t dim,
    ET_SizesType *sizes,
    void *data,
    ET_DimOrderType *dim_order,
    ET_StridesType *strides,
    enum ET_TensorShapeDynamism dynamism)
{
    auto self_ = checked_reinterpret_cast<executorch::aten::TensorImpl>(self);
    new (self_) executorch::aten::TensorImpl(
        static_cast<executorch::aten::ScalarType>(type),
        dim,
        static_cast<executorch::aten::SizesType *>(sizes),
        data,
        static_cast<executorch::aten::DimOrderType *>(dim_order),
        static_cast<executorch::aten::StridesType *>(strides),
        static_cast<executorch::aten::TensorShapeDynamism>(dynamism));
}
void executorch_Tensor_new(struct ET_TensorRefMut self, struct ET_TensorImpl *tensor_impl)
{
    auto self_ = cast_tensor_mut(self);
    auto tensor_impl_ = checked_reinterpret_cast<executorch::aten::TensorImpl>(tensor_impl);
    new (self_) executorch::aten::Tensor(tensor_impl_);
}
size_t executorch_Tensor_nbytes(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->nbytes();
}
size_t executorch_Tensor_size(struct ET_TensorRef self, size_t dim)
{
    auto self_ = cast_tensor(self);
    return self_->size(dim);
}
size_t executorch_Tensor_dim(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->dim();
}
size_t executorch_Tensor_numel(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->numel();
}
enum ET_ScalarType executorch_Tensor_scalar_type(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    auto ret = self_->scalar_type();
    return static_cast<ET_ScalarType>(ret);
}
struct ET_Device executorch_Tensor_device(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    auto d = self_->device();
    return ET_Device{static_cast<ET_DeviceType>(d.type()), static_cast<int8_t>(d.index())};
}
size_t executorch_Tensor_element_size(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->element_size();
}
struct ET_ArrayRefSizesType executorch_Tensor_sizes(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    auto sizes = self_->sizes();
    return ET_ArrayRefSizesType{
        .data = sizes.data(),
        .len = sizes.size(),
    };
}
struct ET_ArrayRefDimOrderType executorch_Tensor_dim_order(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    auto dim_order = self_->dim_order();
    return ET_ArrayRefDimOrderType{
        .data = dim_order.data(),
        .len = dim_order.size(),
    };
}
struct ET_ArrayRefStridesType executorch_Tensor_strides(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    auto strides = self_->strides();
    return ET_ArrayRefStridesType{
        .data = strides.data(),
        .len = strides.size(),
    };
}
const void *executorch_Tensor_const_data_ptr(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->const_data_ptr();
}
void *executorch_Tensor_mutable_data_ptr(struct ET_TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->mutable_data_ptr();
}

int64_t executorch_Tensor_coordinate_to_index(struct ET_TensorRef self, struct ET_ArrayRefUsizeType coordinate)
{
    auto self_ = cast_tensor(self);
    auto ndim = (size_t)self_->dim();
    if (coordinate.len != ndim)
    {
        return -1;
    }

    auto sizes = self_->sizes();
    auto strides = self_->strides();
    auto dim_order = self_->dim_order();
    ET_CHECK_MSG(sizes.size() == ndim, "Sizes must have the same number of dimensions as the tensor");
    ET_CHECK_MSG(strides.size() == ndim, "Strides must have the same number of dimensions as the tensor");
    // TODO: support dim order
    ET_CHECK_MSG(
        dim_order.data() == nullptr || executorch::runtime::is_contiguous_dim_order(dim_order.data(), ndim),
        "Only contiguous dim order is supported for now");

    for (size_t d = 0; d < ndim; d++)
    {
        if (coordinate.data[d] >= (size_t)sizes[d])
        {
            return -1;
        }
    }
    return executorch_Tensor_coordinate_to_index_unchecked(self, coordinate);
}
int64_t executorch_Tensor_coordinate_to_index_unchecked(struct ET_TensorRef self, struct ET_ArrayRefUsizeType coordinate)
{
    auto self_ = cast_tensor(self);
    auto ndim = (size_t)self_->dim();
    auto strides = self_->strides();
    size_t index = 0;
    for (size_t d = 0; d < ndim; d++)
    {
        index += coordinate.data[d] * strides[d];
    }
    return index;
}
void executorch_Tensor_destructor(struct ET_TensorRefMut self)
{
    auto self_ = cast_tensor_mut(self);
    self_->~Tensor();
}

// OptionalTensor
static const executorch::aten::optional<executorch::aten::Tensor> *cast_optional_tensor(struct ET_OptionalTensorRef tensor)
{
    return reinterpret_cast<const executorch::aten::optional<executorch::aten::Tensor> *>(tensor.ptr);
}
// static executorch::aten::optional<executorch::aten::Tensor> *cast_optional_tensor_mut(struct ET_OptionalTensorRefMut tensor)
// {
//     return reinterpret_cast<executorch::aten::optional<executorch::aten::Tensor> *>(tensor.ptr);
// }
struct ET_TensorRef executorch_OptionalTensor_get(struct ET_OptionalTensorRef self)
{
    auto self_ = cast_optional_tensor(self);
    if (!self_->has_value())
        return ET_TensorRef{.ptr = nullptr};
    const executorch::aten::Tensor *tensor = &self_->value();
    return cast_tensor(tensor);
}

// ET_TensorLayout
// enum ET_Error executorch_TensorLayout_create(
//     struct ET_ArrayRefI32 sizes,
//     struct ET_ArrayRefU8 dim_order,
//     enum ET_ScalarType scalar_type,
//     struct ET_TensorLayout *out)
// {
//     auto sizes_ = *checked_reinterpret_cast<executorch::runtime::Span<const int32_t>>(&sizes);
//     auto dim_order_ = *checked_reinterpret_cast<executorch::runtime::Span<const uint8_t>>(&dim_order);
//     auto out_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(out);
//     auto scalar_type_ = static_cast<executorch::aten::ScalarType>(scalar_type);
//     auto res = executorch::runtime::TensorLayout::create(
//         sizes_,
//         dim_order_,
//         scalar_type_);
//     if (!res.ok())
//         return static_cast<ET_Error>(res.error());
//     auto &layout = res.get();
//     new (out_) executorch::runtime::TensorLayout(std::move(layout));
//     return ET_Error::ET_Error_Ok;
// }
struct ET_ArrayRefI32 executorch_TensorLayout_sizes(const struct ET_TensorLayout *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(self);
    auto sizes = self_->sizes();
    return ET_ArrayRefI32{
        .data = sizes.data(),
        .len = sizes.size(),
    };
}
struct ET_ArrayRefU8 executorch_TensorLayout_dim_order(const struct ET_TensorLayout *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(self);
    auto dim_order = self_->dim_order();
    return ET_ArrayRefU8{
        .data = dim_order.data(),
        .len = dim_order.size(),
    };
}
enum ET_ScalarType executorch_TensorLayout_scalar_type(const struct ET_TensorLayout *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(self);
    auto scalar_type = self_->scalar_type();
    return static_cast<ET_ScalarType>(scalar_type);
}
size_t executorch_TensorLayout_nbytes(const struct ET_TensorLayout *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(self);
    return self_->nbytes();
}

// EValue
static const executorch::runtime::EValue *cast_evalue(struct ET_EValueRef evalue)
{
    return reinterpret_cast<const executorch::runtime::EValue *>(evalue.ptr);
}
static struct ET_EValueRef cast_evalue(const executorch::runtime::EValue *evalue)
{
    return ET_EValueRef{.ptr = evalue};
}
static executorch::runtime::EValue *cast_evalue_mut(struct ET_EValueRefMut evalue)
{
    return reinterpret_cast<executorch::runtime::EValue *>(evalue.ptr);
}
// static EValueMut cast_evalue_mut(executorch::runtime::EValue *evalue)
// {
//     return evalue;
// }
void executorch_EValue_new_none(struct ET_EValueRefMut self)
{
    auto self_ = cast_evalue_mut(self);
    new (self_) executorch::runtime::EValue();
}
void executorch_EValue_new_from_i64(struct ET_EValueRefMut self, int64_t value)
{
    auto self_ = cast_evalue_mut(self);
    new (self_) executorch::runtime::EValue(value);
}
void executorch_EValue_new_from_i64_list(struct ET_EValueRefMut self, const struct ET_BoxedEvalueListI64 *value)
{
    auto self_ = cast_evalue_mut(self);
    auto value_ = checked_reinterpret_cast<executorch::runtime::BoxedEvalueList<int64_t>>(value);
    new (self_) executorch::runtime::EValue(const_cast<executorch::runtime::BoxedEvalueList<int64_t> *>(value_));
}
void executorch_EValue_new_from_f64(struct ET_EValueRefMut self, double value)
{
    auto self_ = cast_evalue_mut(self);
    new (self_) executorch::runtime::EValue(value);
}
void executorch_EValue_new_from_f64_list(struct ET_EValueRefMut self, const struct ET_ArrayRefF64 *value)
{
    auto self_ = cast_evalue_mut(self);
    auto value_ = checked_reinterpret_cast<executorch::aten::ArrayRef<double>>(value);
    new (self_) executorch::runtime::EValue(const_cast<executorch::aten::ArrayRef<double> *>(value_));
}
void executorch_EValue_new_from_bool(struct ET_EValueRefMut self, bool value)
{
    auto self_ = cast_evalue_mut(self);
    new (self_) executorch::runtime::EValue(value);
}
void executorch_EValue_new_from_bool_list(struct ET_EValueRefMut self, const struct ET_ArrayRefBool *value)
{
    auto self_ = cast_evalue_mut(self);
    auto value_ = checked_reinterpret_cast<executorch::aten::ArrayRef<bool>>(value);
    new (self_) executorch::runtime::EValue(const_cast<executorch::aten::ArrayRef<bool> *>(value_));
}
void executorch_EValue_new_from_string(struct ET_EValueRefMut self, const struct ET_ArrayRefChar *value)
{
    auto self_ = cast_evalue_mut(self);
    auto value_ = checked_reinterpret_cast<executorch::aten::ArrayRef<char>>(value);
    new (self_) executorch::runtime::EValue(const_cast<executorch::aten::ArrayRef<char> *>(value_));
}
void executorch_EValue_new_from_tensor(struct ET_EValueRefMut self, struct ET_TensorRef value)
{
    auto self_ = cast_evalue_mut(self);
    auto value_ = cast_tensor(value);
    new (self_) executorch::runtime::EValue(*value_);
}
void executorch_EValue_new_from_tensor_list(struct ET_EValueRefMut self, const struct ET_BoxedEvalueListTensor *value)
{
    auto self_ = cast_evalue_mut(self);
    auto value_ = checked_reinterpret_cast<executorch::runtime::BoxedEvalueList<executorch::aten::Tensor>>(value);
    new (self_) executorch::runtime::EValue(const_cast<executorch::runtime::BoxedEvalueList<executorch::aten::Tensor> *>(value_));
}
void executorch_EValue_new_from_optional_tensor_list(struct ET_EValueRefMut self, const struct ET_BoxedEvalueListOptionalTensor *value)
{
    auto self_ = cast_evalue_mut(self);
    auto value_ = checked_reinterpret_cast<executorch::runtime::BoxedEvalueList<std::optional<executorch::aten::Tensor>>>(value);
    new (self_) executorch::runtime::EValue(const_cast<executorch::runtime::BoxedEvalueList<std::optional<executorch::aten::Tensor>> *>(value_));
}
enum ET_Tag executorch_EValue_tag(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    return static_cast<ET_Tag>(self_->tag);
}
int64_t executorch_EValue_as_i64(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    return self_->toInt();
}
struct ET_ArrayRefI64 executorch_EValue_as_i64_list(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toIntList();
    return ET_ArrayRefI64{
        .data = list.data(),
        .len = list.size(),
    };
}
double executorch_EValue_as_f64(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    return self_->toDouble();
}
struct ET_ArrayRefF64 executorch_EValue_as_f64_list(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toDoubleList();
    return ET_ArrayRefF64{
        .data = list.data(),
        .len = list.size(),
    };
}
bool executorch_EValue_as_bool(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    return self_->toBool();
}
struct ET_ArrayRefBool executorch_EValue_as_bool_list(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toBoolList();
    return ET_ArrayRefBool{
        .data = list.data(),
        .len = list.size(),
    };
}
struct ET_ArrayRefChar executorch_EValue_as_string(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto str = self_->toString();
    return ET_ArrayRefChar{
        .data = str.data(),
        .len = str.size(),
    };
}
struct ET_TensorRef executorch_EValue_as_tensor(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    const executorch::aten::Tensor *tensor = &self_->toTensor();
    return cast_tensor(tensor);
}
struct ET_ArrayRefTensor executorch_EValue_as_tensor_list(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toTensorList();
    return ET_ArrayRefTensor{
        .data = cast_tensor(list.data()),
        .len = list.size(),
    };
}
struct ET_ArrayRefOptionalTensor executorch_EValue_as_optional_tensor_list(struct ET_EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toListOptionalTensor();
    return ET_ArrayRefOptionalTensor{
        .data = ET_OptionalTensorRef{.ptr = checked_reinterpret_cast<ET_OptionalTensorStorage>(list.data())},
        .len = list.size(),
    };
}
void executorch_EValue_copy(struct ET_EValueRef src, struct ET_EValueRefMut dst)
{
    auto src_ = cast_evalue(src);
    auto dst_ = cast_evalue_mut(dst);
    new (dst_) executorch::runtime::EValue(*src_);
}
void executorch_EValue_destructor(struct ET_EValueRefMut self)
{
    auto self_ = cast_evalue_mut(self);
    self_->~EValue();
}
void executorch_EValue_move(struct ET_EValueRefMut src, struct ET_EValueRefMut dst)
{
    auto src_ = cast_evalue_mut(src);
    auto dst_ = cast_evalue_mut(dst);
    new (dst_) executorch::runtime::EValue(std::move(*src_));
}

// ET_Program
enum ET_ProgramHeaderStatus executorch_Program_check_header(const void *data, size_t size)
{
    auto status = executorch::runtime::Program::check_header(data, size);
    return static_cast<ET_ProgramHeaderStatus>(status);
}
enum ET_Error executorch_Program_load(struct ET_DataLoaderRefMut loader, enum ET_ProgramVerification verification, struct ET_Program *out)
{
    auto loader_ = cast_data_loader_mut(loader);
    auto verification_ = static_cast<executorch::runtime::Program::Verification>(verification);
    auto out_ = checked_reinterpret_cast<executorch::runtime::Program>(out);
    // return extract_result(executorch::runtime::Program::load(loader, verification), out);
    auto res = executorch::runtime::Program::load(loader_, verification_);
    if (!res.ok())
        return static_cast<ET_Error>(res.error());
    auto &program = res.get();
    new (out_) executorch::runtime::Program(std::move(program));
    return ET_Error::ET_Error_Ok;
}
enum ET_Error executorch_Program_load_method(
    const struct ET_Program *self,
    const char *method_name,
    struct ET_MemoryManager *memory_manager,
    struct ET_EventTracerRefMut event_tracer,
    struct ET_NamedDataMapRef named_data_map,
    const struct ET_LoadBackendOptionsMap *backend_options,
    struct ET_Method *out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Program>(self);
    auto memory_manager_ = checked_reinterpret_cast<executorch::runtime::MemoryManager>(memory_manager);
    auto event_tracer_ = reinterpret_cast<executorch::runtime::EventTracer *>(event_tracer.ptr);
    auto named_data_map_ = reinterpret_cast<const executorch::runtime::NamedDataMap *>(named_data_map.ptr);
    auto backend_options_ = checked_reinterpret_cast<const executorch::runtime::LoadBackendOptionsMap>(backend_options);
    auto out_ = checked_reinterpret_cast<executorch::runtime::Method>(out);

    auto res = self_->load_method(method_name, memory_manager_, event_tracer_, named_data_map_, backend_options_);
    if (!res.ok())
        return static_cast<ET_Error>(res.error());
    auto &method = res.get();
    new (out_) executorch::runtime::Method(std::move(method));
    return ET_Error::ET_Error_Ok;
}
enum ET_Error executorch_Program_get_method_name(const struct ET_Program *self, size_t method_index, const char **out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Program>(self);
    return extract_result(self_->get_method_name(method_index), out);
}
enum ET_Error executorch_Program_get_named_data_map(const struct ET_Program *self, struct ET_NamedDataMapRef *out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Program>(self);
    auto res = self_->get_named_data_map();
    if (!res.ok())
        return static_cast<ET_Error>(res.error());
    *out = ET_NamedDataMapRef{.ptr = res.get()};
    return ET_Error::ET_Error_Ok;
}
enum ET_Error executorch_Program_method_meta(const struct ET_Program *self, const char *method_name, struct ET_MethodMeta *method_meta_out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Program>(self);
    auto method_meta_out_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(method_meta_out);
    return extract_result(self_->method_meta(method_name), method_meta_out_);
}
size_t executorch_Program_num_methods(const struct ET_Program *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Program>(self);
    return self_->num_methods();
}
void executorch_Program_destructor(struct ET_Program *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Program>(self);
    self_->~Program();
}

// ET_MethodMeta
size_t executorch_Method_inputs_size(const struct ET_Method *self)
{
    auto *self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    return self_->inputs_size();
}
size_t executorch_Method_outputs_size(const struct ET_Method *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    return self_->outputs_size();
}
enum ET_Error executorch_Method_set_input(struct ET_Method *self, struct ET_EValueRef input_evalue, size_t input_idx)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    auto input_evalue_ = cast_evalue(input_evalue);
    executorch::runtime::Error ret = self_->set_input(*input_evalue_, input_idx);
    return static_cast<ET_Error>(ret);
}
struct ET_EValueRef executorch_Method_get_output(const struct ET_Method *self, size_t i)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    const executorch::runtime::EValue *output = &self_->get_output(i);
    return cast_evalue(output);
}
enum ET_Error executorch_Method_get_attribute(struct ET_Method *self, struct ET_ArrayRefChar name, struct ET_TensorRefMut out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    auto out_ = cast_tensor_mut(out);
    std::string_view name_(name.data, name.len);
    return extract_result(self_->get_attribute(name_), out_);
}
enum ET_Error executorch_Method_execute(struct ET_Method *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    executorch::runtime::Error ret = self_->execute();
    return static_cast<ET_Error>(ret);
}
void executorch_Method_destructor(struct ET_Method *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    self_->~Method();
}
const char *executorch_MethodMeta_name(const struct ET_MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->name();
    self_->num_inputs();
}
size_t executorch_MethodMeta_num_inputs(const struct ET_MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->num_inputs();
}
size_t executorch_MethodMeta_num_outputs(const struct ET_MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->num_outputs();
}
size_t executorch_MethodMeta_num_memory_planned_buffers(const struct ET_MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->num_memory_planned_buffers();
}
enum ET_Error executorch_MethodMeta_input_tag(const struct ET_MethodMeta *self, size_t index, enum ET_Tag *tag_out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    auto tag_out_ = checked_reinterpret_cast<executorch::runtime::Tag>(tag_out);
    return extract_result(self_->input_tag(index), tag_out_);
}
enum ET_Error executorch_MethodMeta_output_tag(const struct ET_MethodMeta *self, size_t index, enum ET_Tag *tag_out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    auto tag_out_ = checked_reinterpret_cast<executorch::runtime::Tag>(tag_out);
    return extract_result(self_->output_tag(index), tag_out_);
}
enum ET_Error executorch_MethodMeta_input_tensor_meta(const struct ET_MethodMeta *self, size_t index, struct ET_TensorInfo *tensor_info_out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    auto tensor_info_out_ = checked_reinterpret_cast<executorch::runtime::TensorInfo>(tensor_info_out);
    return extract_result(self_->input_tensor_meta(index), tensor_info_out_);
}
enum ET_Error executorch_MethodMeta_output_tensor_meta(const struct ET_MethodMeta *self, size_t index, struct ET_TensorInfo *tensor_info_out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    auto tensor_info_out_ = checked_reinterpret_cast<executorch::runtime::TensorInfo>(tensor_info_out);
    return extract_result(self_->output_tensor_meta(index), tensor_info_out_);
}
size_t executorch_MethodMeta_num_attributes(const struct ET_MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->num_attributes();
}
enum ET_Error executorch_MethodMeta_attribute_tensor_meta(const struct ET_MethodMeta *self, size_t index, struct ET_TensorInfo *tensor_info_out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    auto tensor_info_out_ = checked_reinterpret_cast<executorch::runtime::TensorInfo>(tensor_info_out);
    return extract_result(self_->attribute_tensor_meta(index), tensor_info_out_);
}
enum ET_Error executorch_MethodMeta_memory_planned_buffer_size(const struct ET_MethodMeta *self, size_t index, int64_t *size_out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return extract_result(self_->memory_planned_buffer_size(index), size_out);
}
enum ET_Error executorch_MethodMeta_memory_planned_buffer_device(const struct ET_MethodMeta *self, size_t index, struct ET_Device *device_out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    auto result = self_->memory_planned_buffer_device(index);
    if (!result.ok())
        return static_cast<ET_Error>(result.error());
    auto d = result.get();
    *device_out = ET_Device{static_cast<ET_DeviceType>(d.type()), static_cast<int8_t>(d.index())};
    return ET_Error::ET_Error_Ok;
}

static const executorch::runtime::BackendOption *cast_backend_option(const struct ET_BackendOption *p)
{
    return checked_reinterpret_cast<const executorch::runtime::BackendOption>(p);
}
static enum ET_Error backend_option_new(struct ET_ArrayRefChar key, executorch::runtime::OptionValue value, struct ET_BackendOption *out)
{
    if (key.len >= executorch::runtime::kMaxOptionKeyLength)
        return ET_Error::ET_Error_InvalidArgument;
    auto *opt = new (out) executorch::runtime::BackendOption{};
    std::memcpy(opt->key, key.data, key.len);
    opt->key[key.len] = '\0';
    opt->value = std::move(value);
    return ET_Error::ET_Error_Ok;
}
enum ET_Error executorch_BackendOption_new_bool(struct ET_ArrayRefChar key, bool value, struct ET_BackendOption *out)
{
    return backend_option_new(key, executorch::runtime::OptionValue(value), out);
}
enum ET_Error executorch_BackendOption_new_int(struct ET_ArrayRefChar key, int value, struct ET_BackendOption *out)
{
    return backend_option_new(key, executorch::runtime::OptionValue(value), out);
}
enum ET_Error executorch_BackendOption_new_str(struct ET_ArrayRefChar key, struct ET_ArrayRefChar value, struct ET_BackendOption *out)
{
    if (value.len >= executorch::runtime::kMaxOptionValueLength)
        return ET_Error::ET_Error_InvalidArgument;
    std::array<char, executorch::runtime::kMaxOptionValueLength> value_arr{};
    std::memcpy(value_arr.data(), value.data, value.len);
    value_arr[value.len] = '\0';
    auto value_ = executorch::runtime::OptionValue(value_arr);

    return backend_option_new(key, value_, out);
}
const char *executorch_BackendOption_key(const struct ET_BackendOption *self)
{
    auto self_ = cast_backend_option(self);
    return self_->key;
}
bool executorch_BackendOption_is_bool(const struct ET_BackendOption *self)
{
    auto self_ = cast_backend_option(self);
    return std::holds_alternative<bool>(self_->value);
}
bool executorch_BackendOption_is_int(const struct ET_BackendOption *self)
{
    auto self_ = cast_backend_option(self);
    return std::holds_alternative<int>(self_->value);
}
bool executorch_BackendOption_is_str(const struct ET_BackendOption *self)
{
    auto self_ = cast_backend_option(self);
    return std::holds_alternative<std::array<char, executorch::runtime::kMaxOptionValueLength>>(self_->value);
}
enum ET_Error executorch_BackendOption_as_bool(const struct ET_BackendOption *self, bool *out)
{
    auto self_ = cast_backend_option(self);
    if (auto *v = std::get_if<bool>(&self_->value))
    {
        *out = *v;
        return ET_Error::ET_Error_Ok;
    }
    return ET_Error::ET_Error_InvalidArgument;
}
enum ET_Error executorch_BackendOption_as_int(const struct ET_BackendOption *self, int *out)
{
    auto self_ = cast_backend_option(self);
    if (auto *v = std::get_if<int>(&self_->value))
    {
        *out = *v;
        return ET_Error::ET_Error_Ok;
    }
    return ET_Error::ET_Error_InvalidArgument;
}
enum ET_Error executorch_BackendOption_as_str(const struct ET_BackendOption *self, const char **out)
{
    auto self_ = cast_backend_option(self);
    if (auto *v = std::get_if<std::array<char, executorch::runtime::kMaxOptionValueLength>>(&self_->value))
    {
        *out = v->data();
        return ET_Error::ET_Error_Ok;
    }
    return ET_Error::ET_Error_InvalidArgument;
}

static executorch::runtime::LoadBackendOptionsMap *cast_options_map(struct ET_LoadBackendOptionsMap *p)
{
    return checked_reinterpret_cast<executorch::runtime::LoadBackendOptionsMap>(p);
}
static const executorch::runtime::LoadBackendOptionsMap *cast_options_map(const struct ET_LoadBackendOptionsMap *p)
{
    return checked_reinterpret_cast<const executorch::runtime::LoadBackendOptionsMap>(p);
}
void executorch_LoadBackendOptionsMap_new(struct ET_LoadBackendOptionsMap *out)
{
    new (out) executorch::runtime::LoadBackendOptionsMap{};
}
enum ET_Error executorch_LoadBackendOptionsMap_set_options(struct ET_LoadBackendOptionsMap *self, struct ET_ArrayRefChar backend_id, const struct ET_BackendOption *options, size_t n_options)
{
    auto self_ = cast_options_map(self);

    char backend_id_[64];
    if (backend_id.len >= sizeof(backend_id_))
        return ET_Error::ET_Error_InvalidArgument;
    std::memcpy(backend_id_, backend_id.data, backend_id.len);
    backend_id_[backend_id.len] = '\0';

    auto *options_ = const_cast<executorch::runtime::BackendOption *>(cast_backend_option(options));
    auto span = executorch::runtime::Span<executorch::runtime::BackendOption>(options_, n_options);
    return static_cast<ET_Error>(self_->set_options(backend_id_, span));
}
size_t executorch_LoadBackendOptionsMap_size(const struct ET_LoadBackendOptionsMap *self)
{
    auto self_ = cast_options_map(self);
    return self_->size();
}
enum ET_Error executorch_LoadBackendOptionsMap_entry_at(const struct ET_LoadBackendOptionsMap *self, size_t index, const char **backend_id_out, const struct ET_BackendOption **options_out, size_t *n_options_out)
{
    auto self_ = cast_options_map(self);
    if (index >= self_->size())
        return ET_Error::ET_Error_InvalidArgument;
    auto entry = self_->entry_at(index);
    *backend_id_out = entry.backend_id;
    *options_out = checked_reinterpret_cast<struct ET_BackendOption>(entry.options.data());
    *n_options_out = entry.options.size();
    return ET_Error::ET_Error_Ok;
}
bool executorch_MethodMeta_uses_backend(const struct ET_MethodMeta *self, const char *backend_name)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->uses_backend(backend_name);
}
size_t executorch_MethodMeta_num_backends(const struct ET_MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->num_backends();
}
enum ET_Error executorch_MethodMeta_get_backend_name(const struct ET_MethodMeta *self, size_t index, const char **backend_name_out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return extract_result(self_->get_backend_name(index), backend_name_out);
}

// ET_TensorInfo
struct ET_ArrayRefI32 executorch_TensorInfo_sizes(const struct ET_TensorInfo *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorInfo>(self);
    auto sizes = self_->sizes();
    return ET_ArrayRefI32{
        .data = sizes.data(),
        .len = sizes.size(),
    };
}
struct ET_ArrayRefU8 executorch_TensorInfo_dim_order(const struct ET_TensorInfo *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorInfo>(self);
    auto dim_order = self_->dim_order();
    return ET_ArrayRefU8{
        .data = dim_order.data(),
        .len = dim_order.size(),
    };
}
enum ET_ScalarType executorch_TensorInfo_scalar_type(const struct ET_TensorInfo *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorInfo>(self);
    auto ret = self_->scalar_type();
    return static_cast<ET_ScalarType>(ret);
}
bool executorch_TensorInfo_is_memory_planned(const struct ET_TensorInfo *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorInfo>(self);
    return self_->is_memory_planned();
}
size_t executorch_TensorInfo_nbytes(const struct ET_TensorInfo *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorInfo>(self);
    return self_->nbytes();
}
struct ET_ArrayRefChar executorch_TensorInfo_name(const struct ET_TensorInfo *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorInfo>(self);
    auto name = self_->name();
    return ET_ArrayRefChar{
        .data = name.data(),
        .len = name.size(),
    };
}

#if defined(EXECUTORCH_RS_ETDUMP)
// ET_DumpGen
struct ET_DumpGen executorch_ETDumpGen_new(struct ET_SpanU8 buffer)
{
    struct ET_DumpGen self;
    auto self_ = checked_reinterpret_cast<executorch::etdump::ETDumpGen>(&self);
    new (self_) executorch::etdump::ETDumpGen({buffer.data, buffer.len});
    return self;
}
struct ET_ArrayRefU8 executorch_ETDumpGen_get_etdump_data(struct ET_DumpGen *self)
{
    auto self_ = checked_reinterpret_cast<executorch::etdump::ETDumpGen>(self);
    auto res = self_->get_etdump_data();
    return ET_ArrayRefU8{.data = (uint8_t *)res.buf, .len = res.size};
}
struct ET_EventTracerRefMut executorch_ETDumpGen_as_event_tracer_mut(struct ET_DumpGen *self)
{
    auto self_ = checked_reinterpret_cast<executorch::etdump::ETDumpGen>(self);
    auto tracer = static_cast<executorch::runtime::EventTracer *>(self_);
    return ET_EventTracerRefMut{.ptr = tracer};
}
#endif

// Platform structs and functions

bool executorch_register_pal(ExecutorchPalImpl impl)
{
    executorch::runtime::PalImpl pal_impl = executorch::runtime::PalImpl::create(
        impl.init,
        impl.abort,
        impl.current_ticks,
        reinterpret_cast<pal_ticks_to_ns_multiplier_method>(impl.ticks_to_ns_multiplier),
        reinterpret_cast<pal_emit_log_message_method>(impl.emit_log_message),
        impl.allocate,
        impl.free,
        impl.source_filename);
    return executorch::runtime::register_pal(pal_impl);
}

const ExecutorchPalImpl *executorch_get_pal_impl()
{
    auto impl = executorch::runtime::get_pal_impl();
    return impl ? checked_reinterpret_cast<ExecutorchPalImpl>(impl) : nullptr;
}

void executorch_pal_init()
{
    executorch::runtime::pal_init();
}

void executorch_pal_abort()
{
    executorch::runtime::pal_abort();
}

executorch_timestamp_t executorch_pal_current_ticks()
{
    return executorch::runtime::pal_current_ticks();
}

struct executorch_tick_ratio executorch_pal_ticks_to_ns_multiplier()
{
    auto ratio = executorch::runtime::pal_ticks_to_ns_multiplier();
    return executorch_tick_ratio{
        .numerator = ratio.numerator,
        .denominator = ratio.denominator,
    };
}

void executorch_pal_emit_log_message(
    executorch_timestamp_t timestamp,
    enum executorch_pal_log_level level,
    const char *filename,
    const char *function,
    size_t line,
    const char *message,
    size_t length)
{
    executorch::runtime::pal_emit_log_message(
        timestamp,
        static_cast<et_pal_log_level_t>(level),
        filename,
        function,
        line,
        message,
        length);
}

void *executorch_pal_allocate(size_t size)
{
    return executorch::runtime::pal_allocate(size);
}

void executorch_pal_free(void *ptr)
{
    executorch::runtime::pal_free(ptr);
}