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,760 @@
1
+ #include "rb_internal.hpp"
2
+
3
+ #include <atomic>
4
+ #include <new>
5
+
6
+ namespace etrb {
7
+
8
+ namespace {
9
+
10
+ void module_free(void* p) {
11
+ auto* wrap = static_cast<RbModuleWrap*>(p);
12
+ if (wrap->tracked_bytes > 0) {
13
+ rb_gc_adjust_memory_usage(-static_cast<ssize_t>(wrap->tracked_bytes));
14
+ wrap->tracked_bytes = 0;
15
+ }
16
+ delete wrap;
17
+ }
18
+
19
+ size_t module_memsize(const void* p) {
20
+ const auto* wrap = static_cast<const RbModuleWrap*>(p);
21
+ return sizeof(RbModuleWrap) + wrap->tracked_bytes;
22
+ }
23
+
24
+ VALUE module_alloc(VALUE klass) {
25
+ auto* wrap = new (std::nothrow) RbModuleWrap();
26
+ if (wrap == nullptr) {
27
+ rb_memerror();
28
+ }
29
+ return TypedData_Wrap_Struct(klass, &kModuleDataType, wrap);
30
+ }
31
+
32
+ RbModuleWrap* get_module_wrap(VALUE self) {
33
+ RbModuleWrap* wrap = nullptr;
34
+ TypedData_Get_Struct(self, RbModuleWrap, &kModuleDataType, wrap);
35
+ if (wrap == nullptr || !wrap->core || wrap->core->is_closed()) {
36
+ rb_raise(g_refs.eClosedError, "Executorch::Module is closed");
37
+ }
38
+ return wrap;
39
+ }
40
+
41
+ ModuleCore::LoadMode parse_load_mode(VALUE val) {
42
+ if (val == Qundef || val == Qnil) {
43
+ return ModuleCore::LoadMode::Mmap;
44
+ }
45
+ if (!RB_SYMBOL_P(val)) {
46
+ rb_raise(rb_eTypeError, "load_mode must be a Symbol");
47
+ }
48
+ ID id = SYM2ID(val);
49
+ if (id == g_refs.id_mmap) return ModuleCore::LoadMode::Mmap;
50
+ if (id == g_refs.id_file) return ModuleCore::LoadMode::File;
51
+ if (id == g_refs.id_mmap_mlock) return ModuleCore::LoadMode::MmapUseMlock;
52
+ if (id == g_refs.id_mmap_mlock_ignore_errors) {
53
+ return ModuleCore::LoadMode::MmapUseMlockIgnoreErrors;
54
+ }
55
+ if (id == g_refs.id_mmap_madvise) return ModuleCore::LoadMode::MmapUseMadvise;
56
+ rb_raise(
57
+ rb_eArgError,
58
+ "unknown load_mode :%s (expected :mmap, :file, :mmap_mlock, "
59
+ ":mmap_mlock_ignore_errors, or :mmap_madvise)",
60
+ rb_id2name(id));
61
+ }
62
+
63
+ std::string value_to_method_name(VALUE v) {
64
+ if (RB_SYMBOL_P(v)) {
65
+ const char* s = rb_id2name(SYM2ID(v));
66
+ return s ? std::string(s) : std::string();
67
+ }
68
+ VALUE str = StringValue(v);
69
+ return std::string(RSTRING_PTR(str), static_cast<size_t>(RSTRING_LEN(str)));
70
+ }
71
+
72
+ const char* tag_to_name(executorch::runtime::Tag tag) noexcept {
73
+ switch (tag) {
74
+ case executorch::runtime::Tag::None: return "none";
75
+ case executorch::runtime::Tag::Tensor: return "tensor";
76
+ case executorch::runtime::Tag::String: return "string";
77
+ case executorch::runtime::Tag::Double: return "double";
78
+ case executorch::runtime::Tag::Int: return "int";
79
+ case executorch::runtime::Tag::Bool: return "bool";
80
+ case executorch::runtime::Tag::ListBool: return "list_bool";
81
+ case executorch::runtime::Tag::ListDouble: return "list_double";
82
+ case executorch::runtime::Tag::ListTensor: return "list_tensor";
83
+ case executorch::runtime::Tag::ListScalar: return "list_scalar";
84
+ case executorch::runtime::Tag::ListOptionalTensor: return "list_optional_tensor";
85
+ case executorch::runtime::Tag::ListInt: return "list_int";
86
+ default: return "unknown";
87
+ }
88
+ }
89
+
90
+ VALUE build_value_meta(const ValueMetaSnapshot& vm) {
91
+ VALUE tag_sym = ID2SYM(rb_intern(tag_to_name(vm.tag)));
92
+ if (vm.tag != executorch::runtime::Tag::Tensor) {
93
+ return rb_funcall(
94
+ g_refs.cTensorInfo,
95
+ rb_intern("new"),
96
+ 6,
97
+ tag_sym,
98
+ Qnil,
99
+ Qnil,
100
+ Qnil,
101
+ Qnil,
102
+ Qfalse);
103
+ }
104
+ VALUE shape = rb_ary_new_capa(static_cast<long>(vm.tensor.sizes.size()));
105
+ for (auto s : vm.tensor.sizes) rb_ary_push(shape, LONG2NUM(s));
106
+ VALUE order = rb_ary_new_capa(static_cast<long>(vm.tensor.dim_order.size()));
107
+ for (auto o : vm.tensor.dim_order) rb_ary_push(order, INT2FIX(o));
108
+ VALUE dtype = scalar_type_to_sym(vm.tensor.dtype);
109
+ VALUE nbytes = ULL2NUM(vm.tensor.nbytes);
110
+ VALUE planned = vm.tensor.memory_planned ? Qtrue : Qfalse;
111
+ return rb_funcall(
112
+ g_refs.cTensorInfo,
113
+ rb_intern("new"),
114
+ 6,
115
+ tag_sym,
116
+ dtype,
117
+ shape,
118
+ order,
119
+ nbytes,
120
+ planned);
121
+ }
122
+
123
+ VALUE build_method_meta(const MethodMetaSnapshot& snap) {
124
+ VALUE name = rb_utf8_str_new(snap.name.data(), static_cast<long>(snap.name.size()));
125
+ VALUE inputs = rb_ary_new_capa(static_cast<long>(snap.inputs.size()));
126
+ for (const auto& vm : snap.inputs) rb_ary_push(inputs, build_value_meta(vm));
127
+ VALUE outputs = rb_ary_new_capa(static_cast<long>(snap.outputs.size()));
128
+ for (const auto& vm : snap.outputs) rb_ary_push(outputs, build_value_meta(vm));
129
+ VALUE backends = rb_ary_new_capa(static_cast<long>(snap.backends.size()));
130
+ for (const auto& b : snap.backends) {
131
+ rb_ary_push(backends, rb_utf8_str_new(b.data(), static_cast<long>(b.size())));
132
+ }
133
+ VALUE planned = LL2NUM(snap.planned_memory_bytes);
134
+ return rb_funcall(
135
+ g_refs.cMethodMeta,
136
+ rb_intern("new"),
137
+ 5,
138
+ name,
139
+ inputs,
140
+ outputs,
141
+ backends,
142
+ planned);
143
+ }
144
+
145
+ // Convert Ruby VALUE to CoreValue under the GVL. For borrowed IO::Buffer
146
+ // tensors, records the IO::Buffer VALUE in `locked_bufs` so we lock it around
147
+ // the off-GVL execute call and guarantee unlock via rb_ensure.
148
+ Status ruby_to_core_value(
149
+ VALUE v,
150
+ CoreValue* out,
151
+ std::vector<VALUE>* locked_bufs,
152
+ bool for_output_target) {
153
+ if (is_rb_tensor(v)) {
154
+ RbTensorWrap* wrap = nullptr;
155
+ TypedData_Get_Struct(v, RbTensorWrap, &kTensorDataType, wrap);
156
+ if (wrap == nullptr || wrap->closed ||
157
+ (!wrap->tensor && wrap->borrowed_buf == Qnil)) {
158
+ return Status::Make(ErrorKind::Closed, "Executorch::Tensor is closed");
159
+ }
160
+ if (wrap->borrowed_buf != Qnil) {
161
+ void* ptr = nullptr;
162
+ size_t len = 0;
163
+ if (for_output_target) {
164
+ rb_io_buffer_get_bytes_for_writing(wrap->borrowed_buf, &ptr, &len);
165
+ } else {
166
+ const void* cptr = nullptr;
167
+ rb_io_buffer_get_bytes_for_reading(wrap->borrowed_buf, &cptr, &len);
168
+ ptr = const_cast<void*>(cptr);
169
+ }
170
+ if (len != wrap->borrowed_nbytes) {
171
+ return Status::Make(
172
+ ErrorKind::Shape,
173
+ "IO::Buffer size changed: expected " +
174
+ std::to_string(wrap->borrowed_nbytes) + ", got " +
175
+ std::to_string(len));
176
+ }
177
+ auto tp_res = make_borrowed_tensor_ptr(
178
+ wrap->borrowed_sizes, wrap->borrowed_dtype, ptr);
179
+ if (!tp_res.ok()) {
180
+ return tp_res.status;
181
+ }
182
+ locked_bufs->push_back(wrap->borrowed_buf);
183
+ *out = CoreValue::Tensor(std::move(tp_res.value));
184
+ return Status::Ok();
185
+ }
186
+ *out = CoreValue::Tensor(wrap->tensor);
187
+ return Status::Ok();
188
+ }
189
+
190
+ if (for_output_target) {
191
+ return Status::Make(ErrorKind::Type, "out: target must be an Executorch::Tensor");
192
+ }
193
+
194
+ if (v == Qnil) {
195
+ *out = CoreValue::None();
196
+ return Status::Ok();
197
+ }
198
+ if (v == Qtrue) {
199
+ *out = CoreValue::Bool(true);
200
+ return Status::Ok();
201
+ }
202
+ if (v == Qfalse) {
203
+ *out = CoreValue::Bool(false);
204
+ return Status::Ok();
205
+ }
206
+ if (RB_INTEGER_TYPE_P(v)) {
207
+ *out = CoreValue::Int(NUM2LL(v));
208
+ return Status::Ok();
209
+ }
210
+ if (RB_FLOAT_TYPE_P(v)) {
211
+ *out = CoreValue::Double(RFLOAT_VALUE(v));
212
+ return Status::Ok();
213
+ }
214
+ if (RB_TYPE_P(v, T_STRING)) {
215
+ *out = CoreValue::String(
216
+ std::string(RSTRING_PTR(v), static_cast<size_t>(RSTRING_LEN(v))));
217
+ return Status::Ok();
218
+ }
219
+ return Status::Make(
220
+ ErrorKind::Type,
221
+ std::string("unsupported ExecuTorch input type: ") + rb_obj_classname(v));
222
+ }
223
+
224
+ VALUE core_value_to_ruby(CoreValue& cv) {
225
+ switch (cv.kind) {
226
+ case ValueKind::None:
227
+ return Qnil;
228
+ case ValueKind::Int:
229
+ return LL2NUM(cv.int_val);
230
+ case ValueKind::Double:
231
+ return DBL2NUM(cv.double_val);
232
+ case ValueKind::Bool:
233
+ return cv.bool_val ? Qtrue : Qfalse;
234
+ case ValueKind::String:
235
+ return rb_utf8_str_new(
236
+ cv.string_val.data(), static_cast<long>(cv.string_val.size()));
237
+ case ValueKind::Tensor:
238
+ return wrap_owned_tensor(std::move(cv.tensor_val));
239
+ case ValueKind::IntList: {
240
+ VALUE ary = rb_ary_new_capa(static_cast<long>(cv.int_list.size()));
241
+ for (auto x : cv.int_list) rb_ary_push(ary, LL2NUM(x));
242
+ return ary;
243
+ }
244
+ case ValueKind::DoubleList: {
245
+ VALUE ary = rb_ary_new_capa(static_cast<long>(cv.double_list.size()));
246
+ for (auto x : cv.double_list) rb_ary_push(ary, DBL2NUM(x));
247
+ return ary;
248
+ }
249
+ case ValueKind::BoolList: {
250
+ VALUE ary = rb_ary_new_capa(static_cast<long>(cv.bool_list.size()));
251
+ for (bool x : cv.bool_list) rb_ary_push(ary, x ? Qtrue : Qfalse);
252
+ return ary;
253
+ }
254
+ case ValueKind::TensorList: {
255
+ VALUE ary = rb_ary_new_capa(static_cast<long>(cv.tensor_list.size()));
256
+ for (auto& tp : cv.tensor_list) {
257
+ rb_ary_push(ary, wrap_owned_tensor(std::move(tp)));
258
+ }
259
+ return ary;
260
+ }
261
+ }
262
+ return Qnil;
263
+ }
264
+
265
+ // ---------------------------------------------------------------------------
266
+ // Ruby methods for Executorch::Module
267
+ // ---------------------------------------------------------------------------
268
+
269
+ // Module._open_file(path, load_mode:, data_files:)
270
+ VALUE rb_module_s_open_file(int argc, VALUE* argv, VALUE klass) {
271
+ VALUE path_val = Qnil;
272
+ VALUE opts = Qnil;
273
+ rb_scan_args(argc, argv, "1:", &path_val, &opts);
274
+
275
+ // Supports String and Pathname via rb_get_path!
276
+ VALUE path_str = rb_get_path(path_val);
277
+ VALUE kw_vals[2] = {Qundef, Qundef};
278
+ ID kw_ids[2] = {g_refs.id_kw_load_mode, g_refs.id_kw_data_files};
279
+ if (!NIL_P(opts)) {
280
+ rb_get_kwargs(opts, kw_ids, 0, 2, kw_vals);
281
+ }
282
+ const auto mode = parse_load_mode(kw_vals[0]);
283
+
284
+ VALUE err_exc = Qnil;
285
+ VALUE obj = Qnil;
286
+ int protect_state = 0;
287
+
288
+ {
289
+ std::string path(RSTRING_PTR(path_str), static_cast<size_t>(RSTRING_LEN(path_str)));
290
+ std::vector<std::string> data_files;
291
+
292
+ if (kw_vals[1] != Qundef && kw_vals[1] != Qnil) {
293
+ VALUE df_ary = kw_vals[1];
294
+ if (!RB_TYPE_P(df_ary, T_ARRAY)) {
295
+ err_exc = rb_exc_new_cstr(rb_eTypeError, "data_files must be an Array of paths");
296
+ } else {
297
+ struct DfArg {
298
+ VALUE ary;
299
+ std::vector<std::string>* out;
300
+ } dfa{df_ary, &data_files};
301
+ rb_protect(
302
+ [](VALUE a) -> VALUE {
303
+ auto* d = reinterpret_cast<DfArg*>(a);
304
+ long n = RARRAY_LEN(d->ary);
305
+ d->out->reserve(static_cast<size_t>(n));
306
+ for (long i = 0; i < n; ++i) {
307
+ VALUE ps = rb_get_path(rb_ary_entry(d->ary, i));
308
+ d->out->emplace_back(
309
+ RSTRING_PTR(ps), static_cast<size_t>(RSTRING_LEN(ps)));
310
+ }
311
+ return Qnil;
312
+ },
313
+ reinterpret_cast<VALUE>(&dfa),
314
+ &protect_state);
315
+ }
316
+ }
317
+
318
+ if (protect_state == 0 && err_exc == Qnil) {
319
+ struct OpenCtx {
320
+ const std::string* path;
321
+ const std::vector<std::string>* data_files;
322
+ ModuleCore::LoadMode mode;
323
+ StatusOr<std::unique_ptr<ModuleCore>> result;
324
+ } ctx{&path, &data_files, mode, {}};
325
+
326
+ rb_thread_call_without_gvl(
327
+ [](void* p) -> void* {
328
+ auto* c = static_cast<OpenCtx*>(p);
329
+ c->result = ModuleCore::open_file(*c->path, *c->data_files, c->mode);
330
+ return nullptr;
331
+ },
332
+ &ctx,
333
+ RUBY_UBF_IO,
334
+ nullptr);
335
+
336
+ if (!ctx.result.ok()) {
337
+ err_exc = status_to_ruby_exception(ctx.result.status);
338
+ } else {
339
+ obj = module_alloc(klass);
340
+ RbModuleWrap* wrap = nullptr;
341
+ TypedData_Get_Struct(obj, RbModuleWrap, &kModuleDataType, wrap);
342
+ wrap->core = std::move(ctx.result.value);
343
+ wrap->tracked_bytes = wrap->core->estimated_memory_bytes();
344
+ if (wrap->tracked_bytes > 0) {
345
+ rb_gc_adjust_memory_usage(static_cast<ssize_t>(wrap->tracked_bytes));
346
+ }
347
+ }
348
+ }
349
+ }
350
+
351
+ RB_GC_GUARD(path_str);
352
+ if (protect_state != 0) rb_jump_tag(protect_state);
353
+ if (err_exc != Qnil) raise_status_no_live_cpp(err_exc);
354
+ return obj;
355
+ }
356
+
357
+ // Module.from_bytes(str)
358
+ VALUE rb_module_s_from_bytes(VALUE klass, VALUE bytes_val) {
359
+ Check_Type(bytes_val, T_STRING);
360
+ VALUE err_exc = Qnil;
361
+ VALUE obj = Qnil;
362
+
363
+ {
364
+ const auto* raw = reinterpret_cast<const uint8_t*>(RSTRING_PTR(bytes_val));
365
+ const size_t len = static_cast<size_t>(RSTRING_LEN(bytes_val));
366
+ std::vector<uint8_t> copy;
367
+ try {
368
+ copy.assign(raw, raw + len);
369
+ } catch (const std::bad_alloc&) {
370
+ err_exc = rb_exc_new_cstr(g_refs.eMemoryError, "out of memory copying .pte bytes");
371
+ }
372
+
373
+ if (err_exc == Qnil) {
374
+ struct BytesCtx {
375
+ std::vector<uint8_t>* bytes;
376
+ StatusOr<std::unique_ptr<ModuleCore>> result;
377
+ } ctx{&copy, {}};
378
+
379
+ rb_thread_call_without_gvl(
380
+ [](void* p) -> void* {
381
+ auto* c = static_cast<BytesCtx*>(p);
382
+ c->result = ModuleCore::open_bytes(std::move(*c->bytes));
383
+ return nullptr;
384
+ },
385
+ &ctx,
386
+ RUBY_UBF_IO,
387
+ nullptr);
388
+
389
+ if (!ctx.result.ok()) {
390
+ err_exc = status_to_ruby_exception(ctx.result.status);
391
+ } else {
392
+ obj = module_alloc(klass);
393
+ RbModuleWrap* wrap = nullptr;
394
+ TypedData_Get_Struct(obj, RbModuleWrap, &kModuleDataType, wrap);
395
+ wrap->core = std::move(ctx.result.value);
396
+ wrap->tracked_bytes = wrap->core->estimated_memory_bytes();
397
+ if (wrap->tracked_bytes > 0) {
398
+ rb_gc_adjust_memory_usage(static_cast<ssize_t>(wrap->tracked_bytes));
399
+ }
400
+ }
401
+ }
402
+ }
403
+
404
+ RB_GC_GUARD(bytes_val);
405
+ if (err_exc != Qnil) raise_status_no_live_cpp(err_exc);
406
+ return obj;
407
+ }
408
+
409
+ VALUE rb_module_source(VALUE self) {
410
+ auto* wrap = get_module_wrap(self);
411
+ std::string s = wrap->core->source_label();
412
+ return rb_utf8_str_new(s.data(), static_cast<long>(s.size()));
413
+ }
414
+
415
+ VALUE rb_module_close(VALUE self) {
416
+ RbModuleWrap* wrap = nullptr;
417
+ TypedData_Get_Struct(self, RbModuleWrap, &kModuleDataType, wrap);
418
+ if (wrap != nullptr && wrap->core && !wrap->core->is_closed()) {
419
+ rb_thread_call_without_gvl(
420
+ [](void* p) -> void* {
421
+ static_cast<ModuleCore*>(p)->close();
422
+ return nullptr;
423
+ },
424
+ wrap->core.get(),
425
+ RUBY_UBF_IO,
426
+ nullptr);
427
+ if (wrap->tracked_bytes > 0) {
428
+ rb_gc_adjust_memory_usage(-static_cast<ssize_t>(wrap->tracked_bytes));
429
+ wrap->tracked_bytes = 0;
430
+ }
431
+ }
432
+ return Qnil;
433
+ }
434
+
435
+ VALUE rb_module_closed_p(VALUE self) {
436
+ RbModuleWrap* wrap = nullptr;
437
+ TypedData_Get_Struct(self, RbModuleWrap, &kModuleDataType, wrap);
438
+ return (wrap == nullptr || !wrap->core || wrap->core->is_closed()) ? Qtrue : Qfalse;
439
+ }
440
+
441
+ VALUE rb_module_method_names(VALUE self) {
442
+ auto* wrap = get_module_wrap(self);
443
+ VALUE err_exc = Qnil;
444
+ VALUE out = Qnil;
445
+ {
446
+ auto res = wrap->core->method_names();
447
+ if (!res.ok()) {
448
+ err_exc = status_to_ruby_exception(res.status);
449
+ } else {
450
+ out = rb_ary_new_capa(static_cast<long>(res.value.size()));
451
+ for (const auto& name : res.value) {
452
+ rb_ary_push(out, rb_utf8_str_new(name.data(), static_cast<long>(name.size())));
453
+ }
454
+ }
455
+ }
456
+ if (err_exc != Qnil) raise_status_no_live_cpp(err_exc);
457
+ return out;
458
+ }
459
+
460
+ VALUE rb_module_meta(int argc, VALUE* argv, VALUE self) {
461
+ auto* wrap = get_module_wrap(self);
462
+ VALUE name_val = Qnil;
463
+ rb_scan_args(argc, argv, "01", &name_val);
464
+ VALUE err_exc = Qnil;
465
+ VALUE out = Qnil;
466
+ {
467
+ std::string mname = NIL_P(name_val) ? std::string("forward")
468
+ : value_to_method_name(name_val);
469
+ auto res = wrap->core->method_meta(mname);
470
+ if (!res.ok()) {
471
+ err_exc = status_to_ruby_exception(res.status);
472
+ } else {
473
+ out = build_method_meta(res.value);
474
+ }
475
+ }
476
+ if (err_exc != Qnil) raise_status_no_live_cpp(err_exc);
477
+ return out;
478
+ }
479
+
480
+ VALUE rb_module_load_method(int argc, VALUE* argv, VALUE self) {
481
+ auto* wrap = get_module_wrap(self);
482
+ VALUE name_val = Qnil;
483
+ rb_scan_args(argc, argv, "01", &name_val);
484
+ VALUE err_exc = Qnil;
485
+ {
486
+ std::string mname = NIL_P(name_val) ? std::string("forward")
487
+ : value_to_method_name(name_val);
488
+ struct LoadCtx {
489
+ ModuleCore* core;
490
+ const std::string* name;
491
+ Status status;
492
+ } ctx{wrap->core.get(), &mname, Status::Ok()};
493
+
494
+ rb_thread_call_without_gvl(
495
+ [](void* p) -> void* {
496
+ auto* c = static_cast<LoadCtx*>(p);
497
+ c->status = c->core->load_method(*c->name);
498
+ return nullptr;
499
+ },
500
+ &ctx,
501
+ RUBY_UBF_IO,
502
+ nullptr);
503
+
504
+ if (!ctx.status.ok()) {
505
+ err_exc = status_to_ruby_exception(ctx.status);
506
+ }
507
+ }
508
+ RB_GC_GUARD(self);
509
+ if (err_exc != Qnil) raise_status_no_live_cpp(err_exc);
510
+ return self;
511
+ }
512
+
513
+ VALUE rb_module_unload_method(int argc, VALUE* argv, VALUE self) {
514
+ auto* wrap = get_module_wrap(self);
515
+ VALUE name_val = Qnil;
516
+ rb_scan_args(argc, argv, "01", &name_val);
517
+ VALUE err_exc = Qnil;
518
+ bool unloaded = false;
519
+ {
520
+ std::string mname = NIL_P(name_val) ? std::string("forward")
521
+ : value_to_method_name(name_val);
522
+ auto res = wrap->core->unload_method(mname);
523
+ if (!res.ok()) {
524
+ err_exc = status_to_ruby_exception(res.status);
525
+ } else {
526
+ unloaded = res.value;
527
+ }
528
+ }
529
+ if (err_exc != Qnil) raise_status_no_live_cpp(err_exc);
530
+ return unloaded ? Qtrue : Qfalse;
531
+ }
532
+
533
+ VALUE rb_module_method_loaded_p(int argc, VALUE* argv, VALUE self) {
534
+ auto* wrap = get_module_wrap(self);
535
+ VALUE name_val = Qnil;
536
+ rb_scan_args(argc, argv, "01", &name_val);
537
+ std::string mname = NIL_P(name_val) ? std::string("forward")
538
+ : value_to_method_name(name_val);
539
+ return wrap->core->is_method_loaded(mname) ? Qtrue : Qfalse;
540
+ }
541
+
542
+ // _execute(method_name, inputs_array, out_array_or_nil) -> Array of outputs
543
+ VALUE rb_module_execute_raw(VALUE self, VALUE name_val, VALUE inputs_ary, VALUE out_val) {
544
+ auto* wrap = get_module_wrap(self);
545
+ Check_Type(inputs_ary, T_ARRAY);
546
+
547
+ VALUE err_exc = Qnil;
548
+ VALUE result_ary = Qnil;
549
+ int protect_state = 0;
550
+
551
+ {
552
+ std::string mname = value_to_method_name(name_val);
553
+ std::vector<CoreValue> core_inputs;
554
+ std::vector<executorch::extension::TensorPtr> out_targets;
555
+ std::vector<VALUE> out_ruby_objs;
556
+ std::vector<VALUE> locked_bufs;
557
+
558
+ // 1. Convert inputs & out targets under the GVL.
559
+ struct PrepArg {
560
+ VALUE inputs_ary;
561
+ VALUE out_val;
562
+ std::vector<CoreValue>* core_inputs;
563
+ std::vector<executorch::extension::TensorPtr>* out_targets;
564
+ std::vector<VALUE>* out_ruby_objs;
565
+ std::vector<VALUE>* locked_bufs;
566
+ Status status;
567
+ } prep{inputs_ary, out_val, &core_inputs, &out_targets, &out_ruby_objs, &locked_bufs, Status::Ok()};
568
+
569
+ rb_protect(
570
+ [](VALUE a) -> VALUE {
571
+ auto* p = reinterpret_cast<PrepArg*>(a);
572
+ long n_in = RARRAY_LEN(p->inputs_ary);
573
+ p->core_inputs->reserve(static_cast<size_t>(n_in));
574
+ for (long i = 0; i < n_in; ++i) {
575
+ CoreValue cv;
576
+ auto st = ruby_to_core_value(
577
+ rb_ary_entry(p->inputs_ary, i), &cv, p->locked_bufs, false);
578
+ if (!st.ok()) {
579
+ p->status = std::move(st);
580
+ return Qnil;
581
+ }
582
+ p->core_inputs->push_back(std::move(cv));
583
+ }
584
+ if (!NIL_P(p->out_val)) {
585
+ if (RB_TYPE_P(p->out_val, T_ARRAY)) {
586
+ long n_out = RARRAY_LEN(p->out_val);
587
+ p->out_targets->reserve(static_cast<size_t>(n_out));
588
+ p->out_ruby_objs->reserve(static_cast<size_t>(n_out));
589
+ for (long i = 0; i < n_out; ++i) {
590
+ VALUE item = rb_ary_entry(p->out_val, i);
591
+ CoreValue cv;
592
+ auto st = ruby_to_core_value(item, &cv, p->locked_bufs, true);
593
+ if (!st.ok()) {
594
+ p->status = std::move(st);
595
+ return Qnil;
596
+ }
597
+ p->out_targets->push_back(std::move(cv.tensor_val));
598
+ p->out_ruby_objs->push_back(item);
599
+ }
600
+ } else {
601
+ CoreValue cv;
602
+ auto st = ruby_to_core_value(p->out_val, &cv, p->locked_bufs, true);
603
+ if (!st.ok()) {
604
+ p->status = std::move(st);
605
+ return Qnil;
606
+ }
607
+ p->out_targets->push_back(std::move(cv.tensor_val));
608
+ p->out_ruby_objs->push_back(p->out_val);
609
+ }
610
+ }
611
+ return Qnil;
612
+ },
613
+ reinterpret_cast<VALUE>(&prep),
614
+ &protect_state);
615
+
616
+ if (protect_state == 0 && !prep.status.ok()) {
617
+ err_exc = status_to_ruby_exception(prep.status);
618
+ }
619
+
620
+ // 2. Lock any borrowed IO::Buffers before releasing GVL, and unlock all of
621
+ // them unconditionally immediately after (even if an exception occurs).
622
+ if (protect_state == 0 && err_exc == Qnil) {
623
+ std::vector<VALUE> actually_locked;
624
+ actually_locked.reserve(locked_bufs.size());
625
+
626
+ struct ExecCtx {
627
+ ModuleCore* core;
628
+ const std::string* method_name;
629
+ const std::vector<CoreValue>* inputs;
630
+ const std::vector<executorch::extension::TensorPtr>* out_targets;
631
+ StatusOr<std::vector<CoreValue>> result;
632
+ } ectx{wrap->core.get(), &mname, &core_inputs, &out_targets, {}};
633
+
634
+ struct LockRunArg {
635
+ std::vector<VALUE>* to_lock;
636
+ std::vector<VALUE>* locked;
637
+ ExecCtx* ectx;
638
+ } lra{&locked_bufs, &actually_locked, &ectx};
639
+
640
+ rb_protect(
641
+ [](VALUE a) -> VALUE {
642
+ auto* l = reinterpret_cast<LockRunArg*>(a);
643
+ for (VALUE b : *l->to_lock) {
644
+ // Avoid double-locking the exact same IO::Buffer object twice.
645
+ bool seen = false;
646
+ for (VALUE already : *l->locked) {
647
+ if (already == b) { seen = true; break; }
648
+ }
649
+ if (!seen) {
650
+ rb_io_buffer_lock(b);
651
+ l->locked->push_back(b);
652
+ }
653
+ }
654
+ rb_thread_call_without_gvl(
655
+ [](void* p) -> void* {
656
+ auto* c = static_cast<ExecCtx*>(p);
657
+ c->result = c->core->execute(
658
+ *c->method_name, *c->inputs, *c->out_targets);
659
+ return nullptr;
660
+ },
661
+ l->ectx,
662
+ RUBY_UBF_IO,
663
+ nullptr);
664
+ return Qnil;
665
+ },
666
+ reinterpret_cast<VALUE>(&lra),
667
+ &protect_state);
668
+
669
+ // Always unlock every locked IO::Buffer under the GVL.
670
+ for (auto it = actually_locked.rbegin(); it != actually_locked.rend(); ++it) {
671
+ rb_io_buffer_try_unlock(*it);
672
+ }
673
+
674
+ if (protect_state == 0) {
675
+ if (!ectx.result.ok()) {
676
+ err_exc = status_to_ruby_exception(ectx.result.status);
677
+ } else if (!out_ruby_objs.empty()) {
678
+ // Return the exact caller-provided out: Ruby Tensor objects!
679
+ result_ary = rb_ary_new_capa(static_cast<long>(out_ruby_objs.size()));
680
+ for (VALUE ro : out_ruby_objs) {
681
+ rb_ary_push(result_ary, ro);
682
+ }
683
+ } else {
684
+ struct WrapOutArg {
685
+ std::vector<CoreValue>* vals;
686
+ } woa{&ectx.result.value};
687
+ result_ary = rb_protect(
688
+ [](VALUE a) -> VALUE {
689
+ auto* w = reinterpret_cast<WrapOutArg*>(a);
690
+ VALUE ary = rb_ary_new_capa(static_cast<long>(w->vals->size()));
691
+ for (auto& cv : *w->vals) {
692
+ rb_ary_push(ary, core_value_to_ruby(cv));
693
+ }
694
+ return ary;
695
+ },
696
+ reinterpret_cast<VALUE>(&woa),
697
+ &protect_state);
698
+ }
699
+ }
700
+ }
701
+ } // All C++ locals destroyed here before any rb_jump_tag / rb_exc_raise!
702
+
703
+ RB_GC_GUARD(self);
704
+ RB_GC_GUARD(inputs_ary);
705
+ RB_GC_GUARD(out_val);
706
+ if (protect_state != 0) rb_jump_tag(protect_state);
707
+ if (err_exc != Qnil) raise_status_no_live_cpp(err_exc);
708
+ return result_ary;
709
+ }
710
+
711
+ } // namespace
712
+
713
+ const rb_data_type_t kModuleDataType = {
714
+ "Executorch::Module",
715
+ {
716
+ nullptr,
717
+ module_free,
718
+ module_memsize,
719
+ nullptr,
720
+ {nullptr},
721
+ },
722
+ nullptr,
723
+ nullptr,
724
+ RUBY_TYPED_FREE_IMMEDIATELY,
725
+ };
726
+
727
+ void init_module(VALUE mExecutorch) {
728
+ g_refs.id_mmap = rb_intern("mmap");
729
+ g_refs.id_file = rb_intern("file");
730
+ g_refs.id_mmap_mlock = rb_intern("mmap_mlock");
731
+ g_refs.id_mmap_mlock_ignore_errors = rb_intern("mmap_mlock_ignore_errors");
732
+ g_refs.id_mmap_madvise = rb_intern("mmap_madvise");
733
+ g_refs.id_kw_load_mode = rb_intern("load_mode");
734
+ g_refs.id_kw_data_files = rb_intern("data_files");
735
+ g_refs.id_kw_out = rb_intern("out");
736
+
737
+ g_refs.cMethodMeta = rb_const_get(mExecutorch, rb_intern("MethodMeta"));
738
+ g_refs.cTensorInfo = rb_const_get(mExecutorch, rb_intern("TensorInfo"));
739
+ rb_global_variable(&g_refs.cMethodMeta);
740
+ rb_global_variable(&g_refs.cTensorInfo);
741
+
742
+ g_refs.cModule = rb_define_class_under(mExecutorch, "Module", rb_cObject);
743
+ rb_global_variable(&g_refs.cModule);
744
+ rb_undef_alloc_func(g_refs.cModule);
745
+
746
+ rb_define_singleton_method(g_refs.cModule, "_open_file", rb_module_s_open_file, -1);
747
+ rb_define_singleton_method(g_refs.cModule, "from_bytes", rb_module_s_from_bytes, 1);
748
+
749
+ rb_define_method(g_refs.cModule, "source", rb_module_source, 0);
750
+ rb_define_method(g_refs.cModule, "close", rb_module_close, 0);
751
+ rb_define_method(g_refs.cModule, "closed?", rb_module_closed_p, 0);
752
+ rb_define_method(g_refs.cModule, "method_names", rb_module_method_names, 0);
753
+ rb_define_method(g_refs.cModule, "meta", rb_module_meta, -1);
754
+ rb_define_method(g_refs.cModule, "load_method", rb_module_load_method, -1);
755
+ rb_define_method(g_refs.cModule, "unload_method", rb_module_unload_method, -1);
756
+ rb_define_method(g_refs.cModule, "method_loaded?", rb_module_method_loaded_p, -1);
757
+ rb_define_private_method(g_refs.cModule, "_execute", rb_module_execute_raw, 3);
758
+ }
759
+
760
+ } // namespace etrb