executorch 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ #pragma once
2
+
3
+ #include <ruby.h>
4
+ #include <ruby/encoding.h>
5
+ #include <ruby/io/buffer.h>
6
+ #include <ruby/thread.h>
7
+
8
+ #include <cstddef>
9
+ #include <cstdint>
10
+ #include <memory>
11
+ #include <vector>
12
+
13
+ #include "core/dtype.hpp"
14
+ #include "core/module_core.hpp"
15
+ #include "core/status.hpp"
16
+ #include "core/tensor_core.hpp"
17
+
18
+ namespace etrb {
19
+
20
+ // Cached Ruby modules, classes, and symbols initialized once in Init_executorch.
21
+ struct RubyRefs {
22
+ VALUE mExecutorch = Qnil;
23
+ VALUE cModule = Qnil;
24
+ VALUE cMethodMeta = Qnil;
25
+ VALUE cTensorInfo = Qnil;
26
+ VALUE cTensor = Qnil;
27
+
28
+ // Error hierarchy (all < Executorch::Error < StandardError)
29
+ VALUE eError = Qnil;
30
+ VALUE eLoadError = Qnil;
31
+ VALUE eMethodNotFoundError = Qnil;
32
+ VALUE eOperatorMissingError = Qnil;
33
+ VALUE eDelegateError = Qnil;
34
+ VALUE eShapeError = Qnil;
35
+ VALUE eDTypeError = Qnil;
36
+ VALUE eExecutionError = Qnil;
37
+ VALUE eMemoryError = Qnil;
38
+ VALUE eClosedError = Qnil;
39
+
40
+ // Cached IDs
41
+ ID id_code = 0;
42
+ ID id_pal_log = 0;
43
+ ID id_float32 = 0;
44
+ ID id_float64 = 0;
45
+ ID id_float16 = 0;
46
+ ID id_bfloat16 = 0;
47
+ ID id_uint8 = 0;
48
+ ID id_int8 = 0;
49
+ ID id_int16 = 0;
50
+ ID id_uint16 = 0;
51
+ ID id_int32 = 0;
52
+ ID id_uint32 = 0;
53
+ ID id_int64 = 0;
54
+ ID id_uint64 = 0;
55
+ ID id_bool = 0;
56
+ ID id_unknown = 0;
57
+
58
+ // LoadMode symbols
59
+ ID id_mmap = 0;
60
+ ID id_file = 0;
61
+ ID id_mmap_mlock = 0;
62
+ ID id_mmap_mlock_ignore_errors = 0;
63
+ ID id_mmap_madvise = 0;
64
+
65
+ // Keyword IDs
66
+ ID id_kw_shape = 0;
67
+ ID id_kw_dtype = 0;
68
+ ID id_kw_load_mode = 0;
69
+ ID id_kw_data_files = 0;
70
+ ID id_kw_out = 0;
71
+ };
72
+
73
+ extern RubyRefs g_refs;
74
+
75
+ // TypedData structs
76
+ struct RbTensorWrap {
77
+ // Owned TensorPtr (null if borrowed_buf != Qnil or closed).
78
+ executorch::extension::TensorPtr tensor;
79
+ // When created via Tensor.from_buffer, holds the Ruby IO::Buffer VALUE
80
+ // (marked in dmark so GC keeps it alive; locked around off-GVL execute).
81
+ VALUE borrowed_buf = Qnil;
82
+ std::vector<executorch::aten::SizesType> borrowed_sizes;
83
+ executorch::aten::ScalarType borrowed_dtype =
84
+ executorch::aten::ScalarType::Undefined;
85
+ size_t borrowed_nbytes = 0;
86
+ size_t tracked_bytes = 0;
87
+ bool closed = false;
88
+ };
89
+
90
+ struct RbModuleWrap {
91
+ std::unique_ptr<ModuleCore> core;
92
+ size_t tracked_bytes = 0;
93
+ };
94
+
95
+ extern const rb_data_type_t kTensorDataType;
96
+ extern const rb_data_type_t kModuleDataType;
97
+
98
+ // Subsystem initializers
99
+ void init_errors(VALUE mExecutorch);
100
+ void init_tensor(VALUE mExecutorch);
101
+ void init_module(VALUE mExecutorch);
102
+
103
+ // Convert Status into a Ruby exception VALUE WITHOUT raising (so callers can
104
+ // finish destroying all C++ objects on the stack before rb_exc_raise).
105
+ VALUE status_to_ruby_exception(const Status& st);
106
+
107
+ // Helper: build Ruby exception from Status and raise it.
108
+ // SAFETY: Caller MUST ensure NO C++ objects with non-trivial destructors are
109
+ // live on the stack in the caller's frame (or any intervening C++ frames)!
110
+ [[noreturn]] void raise_status_no_live_cpp(VALUE exc);
111
+
112
+ // Tensor interop helpers used by rb_module.cpp
113
+ VALUE wrap_owned_tensor(executorch::extension::TensorPtr ptr);
114
+ bool is_rb_tensor(VALUE obj);
115
+ RbTensorWrap* get_rb_tensor(VALUE obj);
116
+
117
+ // Dtype Symbol <-> ScalarType helpers (using cached IDs, zero rb_intern on hot path)
118
+ VALUE scalar_type_to_sym(executorch::aten::ScalarType st);
119
+ bool sym_to_scalar_type(VALUE sym, executorch::aten::ScalarType* out);
120
+
121
+ } // namespace etrb