executorch-sys 0.9.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

#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/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 ArrayRefF64, executorch::aten::ArrayRef<double>>());
    static_assert(is_equal_layout<struct ArrayRefBool, executorch::aten::ArrayRef<bool>>());
    static_assert(is_equal_layout<struct ArrayRefChar, executorch::aten::ArrayRef<char>>());
    static_assert(is_equal_layout<struct BoxedEvalueListI64, executorch::runtime::BoxedEvalueList<int64_t>>());
    static_assert(is_equal_layout<struct BoxedEvalueListTensor, executorch::runtime::BoxedEvalueList<executorch::aten::Tensor>>());
    static_assert(is_equal_layout<struct BoxedEvalueListOptionalTensor, executorch::runtime::BoxedEvalueList<std::optional<executorch::aten::Tensor>>>());
    static_assert(is_equal_layout<struct EValueStorage, executorch::runtime::EValue>());
    static_assert(is_equal_layout<struct TensorStorage, executorch::aten::Tensor>());
    static_assert(is_equal_layout<struct OptionalTensorStorage, executorch::aten::optional<executorch::aten::Tensor>>());

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

    static_assert(is_equal_layout<struct FreeableBuffer, executorch::runtime::FreeableBuffer>());
    static_assert(is_equal_layout<struct Program, executorch::runtime::Program>());
    // Program is not trivially move constructible because it has a FreeableBuffer field that
    // has a custom move constructor.
    // 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 TensorInfo, executorch::runtime::TensorInfo>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::TensorInfo>);

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

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

    static_assert(is_equal_layout<struct Method, executorch::runtime::Method>());
    // 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 FlatTensorDataMap, executorch::extension::FlatTensorDataMap>());
    // 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 BufferDataLoader, executorch::extension::BufferDataLoader>());
    // 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 FileDataLoader, executorch::extension::FileDataLoader>());
    // 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 MmapDataLoader, executorch::extension::MmapDataLoader>());
    // 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 MemoryAllocator, executorch::runtime::MemoryAllocator>());
    // 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 HierarchicalAllocator, executorch::runtime::HierarchicalAllocator>());
    static_assert(std::is_trivially_move_constructible_v<executorch::runtime::HierarchicalAllocator>);

    static_assert(is_equal_layout<struct 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 ETDumpGen, executorch::etdump::ETDumpGen>());
// 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 VecChar *vec)
{
    delete[] vec->data;
}
void executorch_VecVecChar_destructor(struct 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 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 Error extract_result(const executorch::runtime::Result<T> &&result, T *output)
{
    if (result.ok())
        *output = std::move(result.get());
    return static_cast<Error>(result.error());
}

struct MemoryAllocator executorch_MemoryAllocator_new(uint32_t size, uint8_t *base_address)
{
    struct 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 MemoryAllocator *self, size_t size, size_t alignment)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MemoryAllocator>(self);
    return self_->allocate(size, alignment);
}
struct HierarchicalAllocator executorch_HierarchicalAllocator_new(struct 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 HierarchicalAllocator self;
    auto self_ = checked_reinterpret_cast<executorch::runtime::HierarchicalAllocator>(&self);
    new (self_) executorch::runtime::HierarchicalAllocator(buffers_);
    return self;
}
void executorch_HierarchicalAllocator_destructor(struct HierarchicalAllocator *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::HierarchicalAllocator>(self);
    self_->~HierarchicalAllocator();
}
struct MemoryManager executorch_MemoryManager_new(
    struct MemoryAllocator *method_allocator,
    struct HierarchicalAllocator *planned_memory,
    struct 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 MemoryManager self;
    auto self_ = checked_reinterpret_cast<executorch::runtime::MemoryManager>(&self);
    new (self_) executorch::runtime::MemoryManager(method_allocator_, planned_memory_, temp_allocator_);
    return self;
}

// Loaders
static executorch::runtime::DataLoader *cast_data_loader_mut(struct DataLoaderRefMut loader)
{
    return reinterpret_cast<executorch::runtime::DataLoader *>(loader.ptr);
}
static struct DataLoaderRefMut cast_data_loader_mut(executorch::runtime::DataLoader *loader)
{
    return DataLoaderRefMut{.ptr = loader};
}
struct BufferDataLoader executorch_BufferDataLoader_new(const void *data, size_t size)
{
    struct BufferDataLoader loader;
    auto loader_ = checked_reinterpret_cast<executorch::extension::BufferDataLoader>(&loader);
    new (loader_) executorch::extension::BufferDataLoader(data, size);
    return loader;
}
struct DataLoaderRefMut executorch_BufferDataLoader_as_data_loader_mut(struct 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 Error executorch_FileDataLoader_new(const char *file_path, size_t alignment, struct 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<Error>(res.error());
    auto &loader = res.get();
    new (out_) executorch::extension::FileDataLoader(std::move(loader));
    return Error::Error_Ok;
}
void executorch_FileDataLoader_destructor(struct FileDataLoader *self)
{
    auto self_ = checked_reinterpret_cast<executorch::extension::FileDataLoader>(self);
    self_->~FileDataLoader();
}
struct DataLoaderRefMut executorch_FileDataLoader_as_data_loader_mut(struct 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 Error executorch_MmapDataLoader_new(const char *file_path, enum MmapDataLoaderMlockConfig mlock_config, struct 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<Error>(res.error());
    auto &loader = res.get();
    new (out_) executorch::extension::MmapDataLoader(std::move(loader));
    return Error::Error_Ok;
}
void executorch_MmapDataLoader_destructor(struct MmapDataLoader *self)
{
    auto self_ = checked_reinterpret_cast<executorch::extension::MmapDataLoader>(self);
    self_->~MmapDataLoader();
}
struct DataLoaderRefMut executorch_MmapDataLoader_as_data_loader_mut(struct 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 Error executorch_NamedDataMap_get_tensor_layout(
    struct NamedDataMapRef self,
    struct ArrayRefChar key,
    struct 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<Error>(res.error());
    memcpy(out_, &res.get(), sizeof(executorch::runtime::TensorLayout));
    return Error::Error_Ok;
}
enum Error executorch_NamedDataMap_get_num_keys(struct NamedDataMapRef self, uint32_t *out)
{
    auto self_ = reinterpret_cast<const executorch::runtime::NamedDataMap *>(self.ptr);
    return extract_result(self_->get_num_keys(), out);
}
enum Error executorch_NamedDataMap_get_key(
    struct 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<Error>(res.error());
    *out_data = res.get();
    return Error::Error_Ok;
}

#if defined(EXECUTORCH_RS_FLAT_TENSOR)
// FlatTensorDataMap
enum Error executorch_FlatTensorDataMap_load(struct DataLoaderRefMut loader, struct 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<Error>(res.error());
    auto &program = res.get();
    new (out_) executorch::extension::FlatTensorDataMap(std::move(program));
    return Error::Error_Ok;
}
struct NamedDataMapRefMut executorch_FlatTensorDataMap_as_named_data_map_mut(struct FlatTensorDataMap *self)
{
    auto self_ = checked_reinterpret_cast<executorch::extension::FlatTensorDataMap>(self);
    auto named_data_map = static_cast<executorch::runtime::NamedDataMap *>(self_);
    return NamedDataMapRefMut{.ptr = named_data_map};
}
#endif

// Tensor
static const executorch::aten::Tensor *cast_tensor(struct TensorRef tensor)
{
    return reinterpret_cast<const executorch::aten::Tensor *>(tensor.ptr);
}
static struct TensorRef cast_tensor(const executorch::aten::Tensor *tensor)
{
    return TensorRef{.ptr = tensor};
}
static executorch::aten::Tensor *cast_tensor_mut(struct 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 SizesType *sizes, const DimOrderType *dim_order, const StridesType *strides)
{
    ET_CHECK_MSG(dim <= MAX_DIM, "dim > 16");

    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 Error executorch_stride_to_dim_order(const StridesType *strides, size_t dims, DimOrderType *dim_order)
{
    return static_cast<Error>(executorch::runtime::stride_to_dim_order(strides, dims, dim_order));
}

void executorch_TensorImpl_new(
    struct TensorImpl *self,
    enum ScalarType type,
    size_t dim,
    SizesType *sizes,
    void *data,
    DimOrderType *dim_order,
    StridesType *strides,
    enum 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 TensorRefMut self, struct 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 TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->nbytes();
}
size_t executorch_Tensor_size(struct TensorRef self, size_t dim)
{
    auto self_ = cast_tensor(self);
    return self_->size(dim);
}
size_t executorch_Tensor_dim(struct TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->dim();
}
size_t executorch_Tensor_numel(struct TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->numel();
}
enum ScalarType executorch_Tensor_scalar_type(struct TensorRef self)
{
    auto self_ = cast_tensor(self);
    auto ret = self_->scalar_type();
    return static_cast<ScalarType>(ret);
}
size_t executorch_Tensor_element_size(struct TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->element_size();
}
struct ArrayRefSizesType executorch_Tensor_sizes(struct TensorRef self)
{
    auto self_ = cast_tensor(self);
    auto sizes = self_->sizes();
    return ArrayRefSizesType{
        .data = sizes.data(),
        .len = sizes.size(),
    };
}
struct ArrayRefDimOrderType executorch_Tensor_dim_order(struct TensorRef self)
{
    auto self_ = cast_tensor(self);
    auto dim_order = self_->dim_order();
    return ArrayRefDimOrderType{
        .data = dim_order.data(),
        .len = dim_order.size(),
    };
}
struct ArrayRefStridesType executorch_Tensor_strides(struct TensorRef self)
{
    auto self_ = cast_tensor(self);
    auto strides = self_->strides();
    return ArrayRefStridesType{
        .data = strides.data(),
        .len = strides.size(),
    };
}
const void *executorch_Tensor_const_data_ptr(struct TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->const_data_ptr();
}
void *executorch_Tensor_mutable_data_ptr(struct TensorRef self)
{
    auto self_ = cast_tensor(self);
    return self_->mutable_data_ptr();
}

int64_t executorch_Tensor_coordinate_to_index(struct TensorRef self, struct 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 TensorRef self, struct 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 TensorRefMut self)
{
    auto self_ = cast_tensor_mut(self);
    self_->~Tensor();
}

// OptionalTensor
static const executorch::aten::optional<executorch::aten::Tensor> *cast_optional_tensor(struct 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 OptionalTensorRefMut tensor)
// {
//     return reinterpret_cast<executorch::aten::optional<executorch::aten::Tensor> *>(tensor.ptr);
// }
struct TensorRef executorch_OptionalTensor_get(struct OptionalTensorRef self)
{
    auto self_ = cast_optional_tensor(self);
    if (!self_->has_value())
        return TensorRef{.ptr = nullptr};
    const executorch::aten::Tensor *tensor = &self_->value();
    return cast_tensor(tensor);
}

// TensorLayout
// enum Error executorch_TensorLayout_create(
//     struct ArrayRefI32 sizes,
//     struct ArrayRefU8 dim_order,
//     enum ScalarType scalar_type,
//     struct 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<Error>(res.error());
//     auto &layout = res.get();
//     new (out_) executorch::runtime::TensorLayout(std::move(layout));
//     return Error::Error_Ok;
// }
struct ArrayRefI32 executorch_TensorLayout_sizes(const struct TensorLayout *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(self);
    auto sizes = self_->sizes();
    return ArrayRefI32{
        .data = sizes.data(),
        .len = sizes.size(),
    };
}
struct ArrayRefU8 executorch_TensorLayout_dim_order(const struct TensorLayout *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(self);
    auto dim_order = self_->dim_order();
    return ArrayRefU8{
        .data = dim_order.data(),
        .len = dim_order.size(),
    };
}
enum ScalarType executorch_TensorLayout_scalar_type(const struct TensorLayout *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(self);
    auto scalar_type = self_->scalar_type();
    return static_cast<ScalarType>(scalar_type);
}
size_t executorch_TensorLayout_nbytes(const struct TensorLayout *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::TensorLayout>(self);
    return self_->nbytes();
}

// EValue
static const executorch::runtime::EValue *cast_evalue(struct EValueRef evalue)
{
    return reinterpret_cast<const executorch::runtime::EValue *>(evalue.ptr);
}
static struct EValueRef cast_evalue(const executorch::runtime::EValue *evalue)
{
    return EValueRef{.ptr = evalue};
}
static executorch::runtime::EValue *cast_evalue_mut(struct 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 EValueRefMut self)
{
    auto self_ = cast_evalue_mut(self);
    new (self_) executorch::runtime::EValue();
}
void executorch_EValue_new_from_i64(struct 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 EValueRefMut self, const struct 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 EValueRefMut self, double value)
{
    auto self_ = cast_evalue_mut(self);
    new (self_) executorch::runtime::EValue(value);
}
void executorch_EValue_new_from_f64_list(struct EValueRefMut self, const struct 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 EValueRefMut self, bool value)
{
    auto self_ = cast_evalue_mut(self);
    new (self_) executorch::runtime::EValue(value);
}
void executorch_EValue_new_from_bool_list(struct EValueRefMut self, const struct 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 EValueRefMut self, const struct 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 EValueRefMut self, struct 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 EValueRefMut self, const struct 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 EValueRefMut self, const struct 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 Tag executorch_EValue_tag(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    return static_cast<Tag>(self_->tag);
}
int64_t executorch_EValue_as_i64(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    return self_->toInt();
}
struct ArrayRefI64 executorch_EValue_as_i64_list(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toIntList();
    return ArrayRefI64{
        .data = list.data(),
        .len = list.size(),
    };
}
double executorch_EValue_as_f64(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    return self_->toDouble();
}
struct ArrayRefF64 executorch_EValue_as_f64_list(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toDoubleList();
    return ArrayRefF64{
        .data = list.data(),
        .len = list.size(),
    };
}
bool executorch_EValue_as_bool(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    return self_->toBool();
}
struct ArrayRefBool executorch_EValue_as_bool_list(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toBoolList();
    return ArrayRefBool{
        .data = list.data(),
        .len = list.size(),
    };
}
struct ArrayRefChar executorch_EValue_as_string(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto str = self_->toString();
    return ArrayRefChar{
        .data = str.data(),
        .len = str.size(),
    };
}
struct TensorRef executorch_EValue_as_tensor(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    const executorch::aten::Tensor *tensor = &self_->toTensor();
    return cast_tensor(tensor);
}
struct ArrayRefTensor executorch_EValue_as_tensor_list(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toTensorList();
    return ArrayRefTensor{
        .data = cast_tensor(list.data()),
        .len = list.size(),
    };
}
struct ArrayRefOptionalTensor executorch_EValue_as_optional_tensor_list(struct EValueRef self)
{
    auto self_ = cast_evalue(self);
    auto list = self_->toListOptionalTensor();
    return ArrayRefOptionalTensor{
        .data = OptionalTensorRef{.ptr = checked_reinterpret_cast<OptionalTensorStorage>(list.data())},
        .len = list.size(),
    };
}
void executorch_EValue_copy(struct EValueRef src, struct EValueRefMut dst)
{
    auto src_ = cast_evalue(src);
    auto dst_ = cast_evalue_mut(dst);
    new (dst_) executorch::runtime::EValue(*src_);
}
void executorch_EValue_destructor(struct EValueRefMut self)
{
    auto self_ = cast_evalue_mut(self);
    self_->~EValue();
}
void executorch_EValue_move(struct EValueRefMut src, struct EValueRefMut dst)
{
    auto src_ = cast_evalue_mut(src);
    auto dst_ = cast_evalue_mut(dst);
    new (dst_) executorch::runtime::EValue(std::move(*src_));
}

// Program
enum ProgramHeaderStatus executorch_Program_check_header(const void *data, size_t size)
{
    auto status = executorch::runtime::Program::check_header(data, size);
    return static_cast<ProgramHeaderStatus>(status);
}
enum Error executorch_Program_load(struct DataLoaderRefMut loader, enum ProgramVerification verification, struct 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<Error>(res.error());
    auto &program = res.get();
    new (out_) executorch::runtime::Program(std::move(program));
    return Error::Error_Ok;
}
enum Error executorch_Program_load_method(
    const struct Program *self,
    const char *method_name,
    struct MemoryManager *memory_manager,
    struct EventTracerRefMut event_tracer,
    struct NamedDataMapRef named_data_map,
    struct 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 out_ = checked_reinterpret_cast<executorch::runtime::Method>(out);

    auto res = self_->load_method(method_name, memory_manager_, event_tracer_, named_data_map_);
    if (!res.ok())
        return static_cast<Error>(res.error());
    auto &method = res.get();
    new (out_) executorch::runtime::Method(std::move(method));
    return Error::Error_Ok;
}
enum Error executorch_Program_get_method_name(const struct 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 Error executorch_Program_get_named_data_map(const struct Program *self, struct NamedDataMapRef *out)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Program>(self);
    auto res = self_->get_named_data_map();
    if (!res.ok())
        return static_cast<Error>(res.error());
    *out = NamedDataMapRef{.ptr = res.get()};
    return Error::Error_Ok;
}
enum Error executorch_Program_method_meta(const struct Program *self, const char *method_name, struct 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 Program *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Program>(self);
    return self_->num_methods();
}
void executorch_Program_destructor(struct Program *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Program>(self);
    self_->~Program();
}

// MethodMeta
size_t executorch_Method_inputs_size(const struct Method *self)
{
    auto *self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    return self_->inputs_size();
}
size_t executorch_Method_outputs_size(const struct Method *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    return self_->outputs_size();
}
enum Error executorch_Method_set_input(struct Method *self, struct 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<Error>(ret);
}
struct EValueRef executorch_Method_get_output(const struct 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 Error executorch_Method_get_attribute(struct Method *self, struct ArrayRefChar name, struct 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 Error executorch_Method_execute(struct Method *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    executorch::runtime::Error ret = self_->execute();
    return static_cast<Error>(ret);
}
void executorch_Method_destructor(struct Method *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::Method>(self);
    self_->~Method();
}
const char *executorch_MethodMeta_name(const struct 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 MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->num_inputs();
}
size_t executorch_MethodMeta_num_outputs(const struct 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 MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->num_memory_planned_buffers();
}
enum Error executorch_MethodMeta_input_tag(const struct MethodMeta *self, size_t index, enum 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 Error executorch_MethodMeta_output_tag(const struct MethodMeta *self, size_t index, enum 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 Error executorch_MethodMeta_input_tensor_meta(const struct MethodMeta *self, size_t index, struct 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 Error executorch_MethodMeta_output_tensor_meta(const struct MethodMeta *self, size_t index, struct 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 MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->num_attributes();
}
enum Error executorch_MethodMeta_attribute_tensor_meta(const struct MethodMeta *self, size_t index, struct 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 Error executorch_MethodMeta_memory_planned_buffer_size(const struct 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);
}
bool executorch_MethodMeta_uses_backend(const struct 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 MethodMeta *self)
{
    auto self_ = checked_reinterpret_cast<executorch::runtime::MethodMeta>(self);
    return self_->num_backends();
}
enum Error executorch_MethodMeta_get_backend_name(const struct 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);
}

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

#if defined(EXECUTORCH_RS_ETDUMP)
// ETDumpGen
struct ETDumpGen executorch_ETDumpGen_new(struct SpanU8 buffer)
{
    struct ETDumpGen self;
    auto self_ = checked_reinterpret_cast<executorch::etdump::ETDumpGen>(&self);
    new (self_) executorch::etdump::ETDumpGen({buffer.data, buffer.len});
    return self;
}
struct ArrayRefU8 executorch_ETDumpGen_get_etdump_data(struct ETDumpGen *self)
{
    auto self_ = checked_reinterpret_cast<executorch::etdump::ETDumpGen>(self);
    auto res = self_->get_etdump_data();
    return ArrayRefU8{.data = (uint8_t *)res.buf, .len = res.size};
}
struct EventTracerRefMut executorch_ETDumpGen_as_event_tracer_mut(struct ETDumpGen *self)
{
    auto self_ = checked_reinterpret_cast<executorch::etdump::ETDumpGen>(self);
    auto tracer = static_cast<executorch::runtime::EventTracer *>(self_);
    return 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);
}