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,1281 @@
1
+ #include "rb_internal.hpp"
2
+
3
+ #include <cmath>
4
+ #include <cstring>
5
+ #include <limits>
6
+ #include <new>
7
+
8
+ namespace etrb {
9
+
10
+ namespace {
11
+
12
+ void tensor_mark(void* p) {
13
+ auto* wrap = static_cast<RbTensorWrap*>(p);
14
+ if (wrap->borrowed_buf != Qnil) {
15
+ rb_gc_mark(wrap->borrowed_buf);
16
+ }
17
+ }
18
+
19
+ void tensor_free(void* p) {
20
+ auto* wrap = static_cast<RbTensorWrap*>(p);
21
+ if (wrap->tracked_bytes > 0) {
22
+ rb_gc_adjust_memory_usage(-static_cast<ssize_t>(wrap->tracked_bytes));
23
+ wrap->tracked_bytes = 0;
24
+ }
25
+ delete wrap;
26
+ }
27
+
28
+ size_t tensor_memsize(const void* p) {
29
+ const auto* wrap = static_cast<const RbTensorWrap*>(p);
30
+ return sizeof(RbTensorWrap) + wrap->tracked_bytes;
31
+ }
32
+
33
+ VALUE tensor_alloc(VALUE klass) {
34
+ auto* wrap = new (std::nothrow) RbTensorWrap();
35
+ if (wrap == nullptr) {
36
+ rb_memerror();
37
+ }
38
+ return TypedData_Wrap_Struct(klass, &kTensorDataType, wrap);
39
+ }
40
+
41
+ RbTensorWrap* check_open_tensor(VALUE self) {
42
+ RbTensorWrap* wrap = nullptr;
43
+ TypedData_Get_Struct(self, RbTensorWrap, &kTensorDataType, wrap);
44
+ if (wrap == nullptr || wrap->closed ||
45
+ (!wrap->tensor && wrap->borrowed_buf == Qnil)) {
46
+ rb_raise(g_refs.eClosedError, "Executorch::Tensor is closed");
47
+ }
48
+ return wrap;
49
+ }
50
+
51
+ // Validate & extract a borrowed IO::Buffer's bytes right now under the GVL.
52
+ void check_borrowed_read(
53
+ const RbTensorWrap* wrap,
54
+ const void** out_ptr,
55
+ size_t* out_len) {
56
+ const void* ptr = nullptr;
57
+ size_t len = 0;
58
+ rb_io_buffer_get_bytes_for_reading(wrap->borrowed_buf, &ptr, &len);
59
+ if (len != wrap->borrowed_nbytes) {
60
+ rb_raise(
61
+ g_refs.eShapeError,
62
+ "IO::Buffer size changed: expected %zu bytes, got %zu",
63
+ wrap->borrowed_nbytes,
64
+ len);
65
+ }
66
+ *out_ptr = ptr;
67
+ *out_len = len;
68
+ }
69
+
70
+ // Parse Ruby Array of dims into int64_t vector with reentrancy-safe length reads.
71
+ void parse_ruby_shape(VALUE shape_ary, std::vector<int64_t>* out) {
72
+ Check_Type(shape_ary, T_ARRAY);
73
+ long len = RARRAY_LEN(shape_ary);
74
+ out->reserve(static_cast<size_t>(len));
75
+ for (long i = 0; i < len; ++i) {
76
+ if (i >= RARRAY_LEN(shape_ary)) {
77
+ rb_raise(rb_eArgError, "shape array was mutated during conversion");
78
+ }
79
+ VALUE v = rb_ary_entry(shape_ary, i);
80
+ if (!RB_INTEGER_TYPE_P(v)) {
81
+ rb_raise(rb_eTypeError, "shape dimensions must be Integer");
82
+ }
83
+ out->push_back(NUM2LL(v));
84
+ }
85
+ }
86
+
87
+ // Safe scalar extraction from Ruby VALUE to target C++ dtype.
88
+ template <typename T>
89
+ T ruby_to_elem(VALUE v);
90
+
91
+ template <>
92
+ float ruby_to_elem<float>(VALUE v) {
93
+ if (RB_FLOAT_TYPE_P(v)) return static_cast<float>(RFLOAT_VALUE(v));
94
+ if (FIXNUM_P(v)) return static_cast<float>(FIX2LONG(v));
95
+ return static_cast<float>(NUM2DBL(v));
96
+ }
97
+
98
+ template <>
99
+ double ruby_to_elem<double>(VALUE v) {
100
+ if (RB_FLOAT_TYPE_P(v)) return RFLOAT_VALUE(v);
101
+ if (FIXNUM_P(v)) return static_cast<double>(FIX2LONG(v));
102
+ return NUM2DBL(v);
103
+ }
104
+
105
+ template <>
106
+ executorch::runtime::etensor::Half ruby_to_elem<executorch::runtime::etensor::Half>(VALUE v) {
107
+ return executorch::runtime::etensor::Half(ruby_to_elem<float>(v));
108
+ }
109
+
110
+ template <>
111
+ executorch::runtime::etensor::BFloat16 ruby_to_elem<executorch::runtime::etensor::BFloat16>(VALUE v) {
112
+ return executorch::runtime::etensor::BFloat16(ruby_to_elem<float>(v));
113
+ }
114
+
115
+ template <>
116
+ bool ruby_to_elem<bool>(VALUE v) {
117
+ if (v == Qtrue) return true;
118
+ if (v == Qfalse) return false;
119
+ if (FIXNUM_P(v)) return FIX2LONG(v) != 0;
120
+ rb_raise(rb_eTypeError, "expected true, false, or Integer for :bool tensor");
121
+ }
122
+
123
+ template <typename IntT>
124
+ IntT ruby_to_signed_int(VALUE v) {
125
+ long long val = 0;
126
+ if (FIXNUM_P(v)) {
127
+ val = static_cast<long long>(FIX2LONG(v));
128
+ } else {
129
+ if (RB_FLOAT_TYPE_P(v)) {
130
+ double d = RFLOAT_VALUE(v);
131
+ if (std::isnan(d) || std::isinf(d)) {
132
+ rb_raise(rb_eFloatDomainError, "non-finite float cannot convert to integer");
133
+ }
134
+ }
135
+ val = NUM2LL(v);
136
+ }
137
+ constexpr long long lo = static_cast<long long>(std::numeric_limits<IntT>::min());
138
+ constexpr long long hi = static_cast<long long>(std::numeric_limits<IntT>::max());
139
+ if (val < lo || val > hi) {
140
+ rb_raise(
141
+ rb_eRangeError,
142
+ "integer %lld out of range for %zu-bit signed dtype",
143
+ val,
144
+ sizeof(IntT) * 8);
145
+ }
146
+ return static_cast<IntT>(val);
147
+ }
148
+
149
+ template <typename UIntT>
150
+ UIntT ruby_to_unsigned_int(VALUE v) {
151
+ unsigned long long val = 0;
152
+ if (FIXNUM_P(v)) {
153
+ long lv = FIX2LONG(v);
154
+ if (lv < 0) {
155
+ rb_raise(rb_eRangeError, "negative value %ld out of range for unsigned dtype", lv);
156
+ }
157
+ val = static_cast<unsigned long long>(lv);
158
+ } else {
159
+ if (RB_FLOAT_TYPE_P(v)) {
160
+ double d = RFLOAT_VALUE(v);
161
+ if (std::isnan(d) || std::isinf(d)) {
162
+ rb_raise(rb_eFloatDomainError, "non-finite float cannot convert to unsigned integer");
163
+ }
164
+ if (d < 0.0) {
165
+ rb_raise(rb_eRangeError, "negative float out of range for unsigned dtype");
166
+ }
167
+ }
168
+ val = NUM2ULL(v);
169
+ }
170
+ constexpr unsigned long long hi =
171
+ static_cast<unsigned long long>(std::numeric_limits<UIntT>::max());
172
+ if (val > hi) {
173
+ rb_raise(
174
+ rb_eRangeError,
175
+ "integer %llu out of range for %zu-bit unsigned dtype",
176
+ val,
177
+ sizeof(UIntT) * 8);
178
+ }
179
+ return static_cast<UIntT>(val);
180
+ }
181
+
182
+ template <> int8_t ruby_to_elem<int8_t>(VALUE v) { return ruby_to_signed_int<int8_t>(v); }
183
+ template <> int16_t ruby_to_elem<int16_t>(VALUE v) { return ruby_to_signed_int<int16_t>(v); }
184
+ template <> int32_t ruby_to_elem<int32_t>(VALUE v) { return ruby_to_signed_int<int32_t>(v); }
185
+ template <> int64_t ruby_to_elem<int64_t>(VALUE v) { return ruby_to_signed_int<int64_t>(v); }
186
+ template <> uint8_t ruby_to_elem<uint8_t>(VALUE v) { return ruby_to_unsigned_int<uint8_t>(v); }
187
+ template <> uint16_t ruby_to_elem<uint16_t>(VALUE v) { return ruby_to_unsigned_int<uint16_t>(v); }
188
+ template <> uint32_t ruby_to_elem<uint32_t>(VALUE v) { return ruby_to_unsigned_int<uint32_t>(v); }
189
+ template <> uint64_t ruby_to_elem<uint64_t>(VALUE v) { return ruby_to_unsigned_int<uint64_t>(v); }
190
+
191
+ // Convert C++ element to Ruby VALUE.
192
+ template <typename T>
193
+ VALUE elem_to_ruby(T v);
194
+
195
+ template <> VALUE elem_to_ruby<float>(float v) { return DBL2NUM(static_cast<double>(v)); }
196
+ template <> VALUE elem_to_ruby<double>(double v) { return DBL2NUM(v); }
197
+ template <> VALUE elem_to_ruby<executorch::runtime::etensor::Half>(executorch::runtime::etensor::Half v) {
198
+ return DBL2NUM(static_cast<double>(static_cast<float>(v)));
199
+ }
200
+ template <> VALUE elem_to_ruby<executorch::runtime::etensor::BFloat16>(executorch::runtime::etensor::BFloat16 v) {
201
+ return DBL2NUM(static_cast<double>(static_cast<float>(v)));
202
+ }
203
+ template <> VALUE elem_to_ruby<bool>(bool v) { return v ? Qtrue : Qfalse; }
204
+ template <> VALUE elem_to_ruby<int8_t>(int8_t v) { return INT2FIX(v); }
205
+ template <> VALUE elem_to_ruby<int16_t>(int16_t v) { return INT2FIX(v); }
206
+ template <> VALUE elem_to_ruby<int32_t>(int32_t v) { return LONG2NUM(v); }
207
+ template <> VALUE elem_to_ruby<int64_t>(int64_t v) { return LL2NUM(v); }
208
+ template <> VALUE elem_to_ruby<uint8_t>(uint8_t v) { return INT2FIX(v); }
209
+ template <> VALUE elem_to_ruby<uint16_t>(uint16_t v) { return INT2FIX(v); }
210
+ template <> VALUE elem_to_ruby<uint32_t>(uint32_t v) { return ULONG2NUM(v); }
211
+ template <> VALUE elem_to_ruby<uint64_t>(uint64_t v) { return ULL2NUM(v); }
212
+
213
+ // Exact nested-array shape inference AND full structural validation (no silent
214
+ // data loss on [[], [1, 2]] or [[], "x"], no bypass on [[1], [[2]]]).
215
+ // Does NOT allocate any C++ objects that could leak if rb_raise runs because
216
+ // we only call rb_raise AFTER returning an error code from pure C++ traversal!
217
+ int infer_and_validate_nested_c(
218
+ VALUE node,
219
+ size_t depth,
220
+ std::vector<int64_t>& shape,
221
+ bool& shape_locked,
222
+ std::string& err_msg) {
223
+ if (!RB_TYPE_P(node, T_ARRAY)) {
224
+ if (!shape_locked) {
225
+ shape_locked = true;
226
+ } else if (depth != shape.size()) {
227
+ err_msg = "inconsistent nesting depth at level " + std::to_string(depth) +
228
+ ": expected Array";
229
+ return -1;
230
+ }
231
+ return 0;
232
+ }
233
+
234
+ const long n = RARRAY_LEN(node);
235
+ if (!shape_locked && depth == shape.size()) {
236
+ shape.push_back(static_cast<int64_t>(n));
237
+ } else if (depth >= shape.size()) {
238
+ err_msg = "unexpected nested Array at leaf depth " + std::to_string(depth);
239
+ return -1;
240
+ } else if (shape[depth] != static_cast<int64_t>(n)) {
241
+ err_msg = "jagged array at depth " + std::to_string(depth) +
242
+ ": expected size " + std::to_string(shape[depth]) + ", got " +
243
+ std::to_string(n);
244
+ return -1;
245
+ }
246
+
247
+ if (n == 0) {
248
+ // An empty sub-array at depth d means every sibling at depth d MUST also be
249
+ // an empty Array (size 0), and no deeper dimension exists below it.
250
+ if (!shape_locked) {
251
+ shape_locked = true;
252
+ } else if (depth + 1 != shape.size()) {
253
+ err_msg = "jagged empty array at depth " + std::to_string(depth);
254
+ return -1;
255
+ }
256
+ return 0;
257
+ }
258
+
259
+ for (long i = 0; i < n; ++i) {
260
+ VALUE child = rb_ary_entry(node, i);
261
+ if (infer_and_validate_nested_c(child, depth + 1, shape, shape_locked, err_msg) != 0) {
262
+ return -1;
263
+ }
264
+ }
265
+ return 0;
266
+ }
267
+
268
+ // Reentrancy-safe single-pass write from nested or flat Ruby Array into raw
269
+ // typed buffer. Re-checks RARRAY_LEN on EVERY iteration in case a custom #to_f
270
+ // or #to_int mutates the Ruby Array!
271
+ // Protected wrapper ensures C++ locals are never leaked across rb_raise.
272
+ template <typename T>
273
+ struct FillCtx {
274
+ VALUE root;
275
+ T* dst;
276
+ size_t cap;
277
+ size_t pos;
278
+ size_t target_depth;
279
+ };
280
+
281
+ template <typename T>
282
+ VALUE fill_nested_body(VALUE arg) {
283
+ auto* ctx = reinterpret_cast<FillCtx<T>*>(arg);
284
+
285
+ auto walk = [&](auto& self, VALUE node, size_t depth) -> void {
286
+ if (depth == ctx->target_depth) {
287
+ if (RB_TYPE_P(node, T_ARRAY)) {
288
+ rb_raise(rb_eArgError, "unexpected Array at leaf level during fill");
289
+ }
290
+ if (ctx->pos >= ctx->cap) {
291
+ rb_raise(rb_eArgError, "array grew during conversion");
292
+ }
293
+ T val = ruby_to_elem<T>(node);
294
+ ctx->dst[ctx->pos++] = val;
295
+ return;
296
+ }
297
+ if (!RB_TYPE_P(node, T_ARRAY)) {
298
+ rb_raise(rb_eArgError, "array mutated to non-Array during conversion");
299
+ }
300
+ long len = RARRAY_LEN(node);
301
+ for (long i = 0; i < len; ++i) {
302
+ // Re-check length on every step: prevents OOB read if a custom coercion
303
+ // shrank `node` via `node.clear` or `node.pop`.
304
+ if (i >= RARRAY_LEN(node)) {
305
+ rb_raise(rb_eArgError, "array was mutated during conversion");
306
+ }
307
+ VALUE elem = rb_ary_entry(node, i);
308
+ self(self, elem, depth + 1);
309
+ }
310
+ };
311
+
312
+ walk(walk, ctx->root, 0);
313
+ if (ctx->pos != ctx->cap) {
314
+ rb_raise(
315
+ rb_eArgError,
316
+ "data length (%zu) does not match shape element count (%zu)",
317
+ ctx->pos,
318
+ ctx->cap);
319
+ }
320
+ return Qnil;
321
+ }
322
+
323
+ template <typename T>
324
+ VALUE fill_flat_body(VALUE arg) {
325
+ auto* ctx = reinterpret_cast<FillCtx<T>*>(arg);
326
+ if (!RB_TYPE_P(ctx->root, T_ARRAY)) {
327
+ if (ctx->cap != 1) {
328
+ rb_raise(rb_eArgError, "scalar data requires shape [] or [1]");
329
+ }
330
+ ctx->dst[0] = ruby_to_elem<T>(ctx->root);
331
+ ctx->pos = 1;
332
+ return Qnil;
333
+ }
334
+ long initial_len = RARRAY_LEN(ctx->root);
335
+ if (static_cast<size_t>(initial_len) != ctx->cap) {
336
+ rb_raise(
337
+ g_refs.eShapeError,
338
+ "data length (%ld) does not match shape element count (%zu)",
339
+ initial_len,
340
+ ctx->cap);
341
+ }
342
+ for (long i = 0; i < initial_len; ++i) {
343
+ if (i >= RARRAY_LEN(ctx->root)) {
344
+ rb_raise(rb_eArgError, "array was mutated during conversion");
345
+ }
346
+ VALUE elem = rb_ary_entry(ctx->root, i);
347
+ ctx->dst[i] = ruby_to_elem<T>(elem);
348
+ }
349
+ ctx->pos = static_cast<size_t>(initial_len);
350
+ return Qnil;
351
+ }
352
+
353
+ // Dispatch typed fill inside rb_protect so AlignedBuffer is ALWAYS freed if
354
+ // Ruby raises TypeError/RangeError/FloatDomainError during element conversion!
355
+ int fill_buffer_protected(
356
+ VALUE data,
357
+ executorch::aten::ScalarType dtype,
358
+ void* dst,
359
+ size_t numel,
360
+ bool nested,
361
+ size_t target_depth) {
362
+ int state = 0;
363
+ switch (dtype) {
364
+ #define CASE_FILL(et_enum, name, ctype, sz) \
365
+ case executorch::aten::ScalarType::et_enum: { \
366
+ FillCtx<ctype> ctx{data, static_cast<ctype*>(dst), numel, 0, target_depth}; \
367
+ rb_protect( \
368
+ nested ? fill_nested_body<ctype> : fill_flat_body<ctype>, \
369
+ reinterpret_cast<VALUE>(&ctx), \
370
+ &state); \
371
+ break; \
372
+ }
373
+ ETRB_FORALL_DTYPES(CASE_FILL)
374
+ #undef CASE_FILL
375
+ default:
376
+ state = -1;
377
+ break;
378
+ }
379
+ return state;
380
+ }
381
+
382
+ // Recursively build nested Ruby Array directly from contiguous raw memory
383
+ // (zero intermediate flat array allocation, zero [result, offset] pairs!).
384
+ template <typename T>
385
+ VALUE build_nested_ruby(
386
+ const T* base,
387
+ const executorch::aten::SizesType* sizes,
388
+ size_t ndim,
389
+ size_t dim,
390
+ size_t& offset) {
391
+ const long count = static_cast<long>(sizes[dim]);
392
+ VALUE ary = rb_ary_new_capa(count);
393
+ if (dim + 1 == ndim) {
394
+ for (long i = 0; i < count; ++i) {
395
+ rb_ary_push(ary, elem_to_ruby<T>(base[offset++]));
396
+ }
397
+ } else {
398
+ for (long i = 0; i < count; ++i) {
399
+ rb_ary_push(ary, build_nested_ruby<T>(base, sizes, ndim, dim + 1, offset));
400
+ }
401
+ }
402
+ return ary;
403
+ }
404
+
405
+ VALUE build_ruby_from_contiguous(
406
+ const void* raw,
407
+ executorch::aten::ScalarType dtype,
408
+ const executorch::aten::SizesType* sizes,
409
+ size_t ndim,
410
+ size_t numel,
411
+ bool flat) {
412
+ if (ndim == 0) {
413
+ if (numel != 1) {
414
+ return Qnil;
415
+ }
416
+ switch (dtype) {
417
+ #define CASE_SCALAR(et_enum, name, ctype, sz) \
418
+ case executorch::aten::ScalarType::et_enum: \
419
+ return elem_to_ruby<ctype>(*static_cast<const ctype*>(raw));
420
+ ETRB_FORALL_DTYPES(CASE_SCALAR)
421
+ #undef CASE_SCALAR
422
+ default:
423
+ rb_raise(g_refs.eDTypeError, "unsupported dtype for scalar extraction");
424
+ }
425
+ }
426
+
427
+ if (flat || ndim == 1) {
428
+ VALUE ary = rb_ary_new_capa(static_cast<long>(numel));
429
+ switch (dtype) {
430
+ #define CASE_FLAT(et_enum, name, ctype, sz) \
431
+ case executorch::aten::ScalarType::et_enum: { \
432
+ const auto* p = static_cast<const ctype*>(raw); \
433
+ for (size_t i = 0; i < numel; ++i) { \
434
+ rb_ary_push(ary, elem_to_ruby<ctype>(p[i])); \
435
+ } \
436
+ return ary; \
437
+ }
438
+ ETRB_FORALL_DTYPES(CASE_FLAT)
439
+ #undef CASE_FLAT
440
+ default:
441
+ rb_raise(g_refs.eDTypeError, "unsupported dtype for to_a");
442
+ }
443
+ }
444
+
445
+ size_t offset = 0;
446
+ switch (dtype) {
447
+ #define CASE_NESTED(et_enum, name, ctype, sz) \
448
+ case executorch::aten::ScalarType::et_enum: \
449
+ return build_nested_ruby<ctype>( \
450
+ static_cast<const ctype*>(raw), sizes, ndim, 0, offset);
451
+ ETRB_FORALL_DTYPES(CASE_NESTED)
452
+ #undef CASE_NESTED
453
+ default:
454
+ rb_raise(g_refs.eDTypeError, "unsupported dtype for to_a");
455
+ }
456
+ }
457
+
458
+ // Helper: parse dtype option (default :float32).
459
+ executorch::aten::ScalarType parse_dtype_opt(VALUE v) {
460
+ if (v == Qundef || v == Qnil) {
461
+ return executorch::aten::ScalarType::Float;
462
+ }
463
+ executorch::aten::ScalarType st = executorch::aten::ScalarType::Undefined;
464
+ if (!sym_to_scalar_type(v, &st)) {
465
+ if (!RB_SYMBOL_P(v)) {
466
+ rb_raise(rb_eTypeError, "dtype must be a Symbol");
467
+ }
468
+ rb_raise(g_refs.eDTypeError, "unknown or unsupported dtype: :%s", rb_id2name(SYM2ID(v)));
469
+ }
470
+ return st;
471
+ }
472
+
473
+ // ---------------------------------------------------------------------------
474
+ // Ruby methods for Executorch::Tensor
475
+ // ---------------------------------------------------------------------------
476
+
477
+ // Tensor.new(data, shape: nil, dtype: :float32)
478
+ VALUE rb_tensor_s_new(int argc, VALUE* argv, VALUE klass) {
479
+ VALUE data = Qnil;
480
+ VALUE opts = Qnil;
481
+ rb_scan_args(argc, argv, "1:", &data, &opts);
482
+
483
+ VALUE kw_vals[2] = {Qundef, Qundef};
484
+ ID kw_ids[2] = {g_refs.id_kw_shape, g_refs.id_kw_dtype};
485
+ if (!NIL_P(opts)) {
486
+ rb_get_kwargs(opts, kw_ids, 0, 2, kw_vals);
487
+ }
488
+ VALUE shape_val = kw_vals[0];
489
+ VALUE dtype_val = kw_vals[1];
490
+
491
+ const auto dtype = parse_dtype_opt(dtype_val);
492
+ const bool explicit_shape = (shape_val != Qundef && shape_val != Qnil);
493
+
494
+ VALUE err_exc = Qnil;
495
+ VALUE result = Qnil;
496
+ int protect_state = 0;
497
+
498
+ {
499
+ std::vector<int64_t> raw_dims;
500
+ bool nested = false;
501
+ size_t target_depth = 0;
502
+
503
+ if (explicit_shape) {
504
+ if (!RB_TYPE_P(shape_val, T_ARRAY)) {
505
+ err_exc = rb_exc_new_cstr(rb_eTypeError, "shape must be an Array of Integers");
506
+ } else {
507
+ // Reentrancy-safe shape extraction inside rb_protect.
508
+ struct ShapeArg {
509
+ VALUE ary;
510
+ std::vector<int64_t>* dims;
511
+ } sa{shape_val, &raw_dims};
512
+ rb_protect(
513
+ [](VALUE a) -> VALUE {
514
+ auto* s = reinterpret_cast<ShapeArg*>(a);
515
+ parse_ruby_shape(s->ary, s->dims);
516
+ return Qnil;
517
+ },
518
+ reinterpret_cast<VALUE>(&sa),
519
+ &protect_state);
520
+ }
521
+ } else {
522
+ if (RB_TYPE_P(data, T_ARRAY)) {
523
+ bool locked = false;
524
+ std::string err_msg;
525
+ if (infer_and_validate_nested_c(data, 0, raw_dims, locked, err_msg) != 0) {
526
+ err_exc = rb_exc_new_str(
527
+ rb_eArgError,
528
+ rb_utf8_str_new(err_msg.data(), static_cast<long>(err_msg.size())));
529
+ } else {
530
+ nested = true;
531
+ target_depth = raw_dims.size();
532
+ }
533
+ } else if (RB_INTEGER_TYPE_P(data) || RB_FLOAT_TYPE_P(data) ||
534
+ data == Qtrue || data == Qfalse ||
535
+ rb_respond_to(data, rb_intern("to_f"))) {
536
+ // 0-D scalar tensor!
537
+ raw_dims.clear();
538
+ nested = false;
539
+ } else {
540
+ err_exc = rb_exc_new_cstr(
541
+ rb_eTypeError, "Tensor data must be an Array or scalar Numeric/Boolean");
542
+ }
543
+ }
544
+
545
+ if (protect_state == 0 && err_exc == Qnil) {
546
+ auto shape_res = validate_shape(raw_dims, dtype);
547
+ if (!shape_res.ok()) {
548
+ err_exc = status_to_ruby_exception(shape_res.status);
549
+ } else {
550
+ auto buf_res = AlignedBuffer::allocate(shape_res.value.nbytes);
551
+ if (!buf_res.ok()) {
552
+ err_exc = status_to_ruby_exception(buf_res.status);
553
+ } else {
554
+ // If explicit shape was passed with a nested array, validate & flatten cleanly!
555
+ if (explicit_shape && RB_TYPE_P(data, T_ARRAY) &&
556
+ RARRAY_LEN(data) > 0 && RB_TYPE_P(rb_ary_entry(data, 0), T_ARRAY)) {
557
+ std::vector<int64_t> inferred;
558
+ bool locked = false;
559
+ std::string err_msg;
560
+ if (infer_and_validate_nested_c(data, 0, inferred, locked, err_msg) != 0) {
561
+ err_exc = rb_exc_new_str(
562
+ rb_eArgError,
563
+ rb_utf8_str_new(err_msg.data(), static_cast<long>(err_msg.size())));
564
+ } else {
565
+ nested = true;
566
+ target_depth = inferred.size();
567
+ }
568
+ }
569
+
570
+ if (err_exc == Qnil) {
571
+ protect_state = fill_buffer_protected(
572
+ data,
573
+ dtype,
574
+ buf_res.value.ptr,
575
+ shape_res.value.numel,
576
+ nested,
577
+ target_depth);
578
+ }
579
+
580
+ if (protect_state == 0 && err_exc == Qnil) {
581
+ auto tp_res = make_owned_tensor_ptr(
582
+ std::move(shape_res.value.sizes), dtype, std::move(buf_res.value));
583
+ if (!tp_res.ok()) {
584
+ err_exc = status_to_ruby_exception(tp_res.status);
585
+ } else {
586
+ result = wrap_owned_tensor(std::move(tp_res.value));
587
+ (void)klass;
588
+ }
589
+ }
590
+ }
591
+ }
592
+ }
593
+ } // All C++ locals destroyed here!
594
+
595
+ if (protect_state != 0) {
596
+ rb_jump_tag(protect_state);
597
+ }
598
+ if (err_exc != Qnil) {
599
+ raise_status_no_live_cpp(err_exc);
600
+ }
601
+ return result;
602
+ }
603
+
604
+ // Tensor.from_bytes(str, shape:, dtype: :float32)
605
+ VALUE rb_tensor_s_from_bytes(int argc, VALUE* argv, VALUE klass) {
606
+ (void)klass;
607
+ VALUE bytes_val = Qnil;
608
+ VALUE opts = Qnil;
609
+ rb_scan_args(argc, argv, "1:", &bytes_val, &opts);
610
+ Check_Type(bytes_val, T_STRING);
611
+
612
+ VALUE kw_vals[2] = {Qundef, Qundef};
613
+ ID kw_ids[2] = {g_refs.id_kw_shape, g_refs.id_kw_dtype};
614
+ if (NIL_P(opts)) {
615
+ rb_raise(rb_eArgError, "missing keyword: :shape");
616
+ }
617
+ rb_get_kwargs(opts, kw_ids, 1, 1, kw_vals);
618
+
619
+ VALUE shape_val = kw_vals[0];
620
+ const auto dtype = parse_dtype_opt(kw_vals[1]);
621
+ Check_Type(shape_val, T_ARRAY);
622
+
623
+ VALUE err_exc = Qnil;
624
+ VALUE result = Qnil;
625
+ int protect_state = 0;
626
+
627
+ {
628
+ std::vector<int64_t> raw_dims;
629
+ struct ShapeArg {
630
+ VALUE ary;
631
+ std::vector<int64_t>* dims;
632
+ } sa{shape_val, &raw_dims};
633
+ rb_protect(
634
+ [](VALUE a) -> VALUE {
635
+ auto* s = reinterpret_cast<ShapeArg*>(a);
636
+ parse_ruby_shape(s->ary, s->dims);
637
+ return Qnil;
638
+ },
639
+ reinterpret_cast<VALUE>(&sa),
640
+ &protect_state);
641
+
642
+ if (protect_state == 0) {
643
+ auto shape_res = validate_shape(raw_dims, dtype);
644
+ if (!shape_res.ok()) {
645
+ err_exc = status_to_ruby_exception(shape_res.status);
646
+ } else {
647
+ const size_t actual = static_cast<size_t>(RSTRING_LEN(bytes_val));
648
+ const size_t expected = shape_res.value.nbytes;
649
+ if (actual != expected) {
650
+ std::string msg = "byte length mismatch: expected " +
651
+ std::to_string(expected) + " bytes, got " +
652
+ std::to_string(actual);
653
+ err_exc = rb_exc_new_str(
654
+ g_refs.eShapeError,
655
+ rb_utf8_str_new(msg.data(), static_cast<long>(msg.size())));
656
+ } else {
657
+ auto buf_res = AlignedBuffer::allocate(expected);
658
+ if (!buf_res.ok()) {
659
+ err_exc = status_to_ruby_exception(buf_res.status);
660
+ } else {
661
+ if (expected > 0) {
662
+ std::memcpy(buf_res.value.ptr, RSTRING_PTR(bytes_val), expected);
663
+ }
664
+ auto tp_res = make_owned_tensor_ptr(
665
+ std::move(shape_res.value.sizes), dtype, std::move(buf_res.value));
666
+ if (!tp_res.ok()) {
667
+ err_exc = status_to_ruby_exception(tp_res.status);
668
+ } else {
669
+ result = wrap_owned_tensor(std::move(tp_res.value));
670
+ }
671
+ }
672
+ }
673
+ }
674
+ }
675
+ } // All C++ locals destroyed here!
676
+
677
+ RB_GC_GUARD(bytes_val);
678
+ if (protect_state != 0) {
679
+ rb_jump_tag(protect_state);
680
+ }
681
+ if (err_exc != Qnil) {
682
+ raise_status_no_live_cpp(err_exc);
683
+ }
684
+ return result;
685
+ }
686
+
687
+ // Tensor.from_buffer(io_buffer, shape:, dtype: :float32) — zero-copy view!
688
+ VALUE rb_tensor_s_from_buffer(int argc, VALUE* argv, VALUE klass) {
689
+ VALUE buf_val = Qnil;
690
+ VALUE opts = Qnil;
691
+ rb_scan_args(argc, argv, "1:", &buf_val, &opts);
692
+
693
+ if (!rb_obj_is_kind_of(buf_val, rb_cIOBuffer)) {
694
+ rb_raise(rb_eTypeError, "expected an IO::Buffer");
695
+ }
696
+ if (NIL_P(opts)) {
697
+ rb_raise(rb_eArgError, "missing keyword: :shape");
698
+ }
699
+ VALUE kw_vals[2] = {Qundef, Qundef};
700
+ ID kw_ids[2] = {g_refs.id_kw_shape, g_refs.id_kw_dtype};
701
+ rb_get_kwargs(opts, kw_ids, 1, 1, kw_vals);
702
+
703
+ VALUE shape_val = kw_vals[0];
704
+ const auto dtype = parse_dtype_opt(kw_vals[1]);
705
+ Check_Type(shape_val, T_ARRAY);
706
+
707
+ const void* ptr = nullptr;
708
+ size_t actual = 0;
709
+ rb_io_buffer_get_bytes_for_reading(buf_val, &ptr, &actual);
710
+
711
+ VALUE err_exc = Qnil;
712
+ VALUE result = Qnil;
713
+ int protect_state = 0;
714
+
715
+ {
716
+ std::vector<int64_t> raw_dims;
717
+ struct ShapeArg {
718
+ VALUE ary;
719
+ std::vector<int64_t>* dims;
720
+ } sa{shape_val, &raw_dims};
721
+ rb_protect(
722
+ [](VALUE a) -> VALUE {
723
+ auto* s = reinterpret_cast<ShapeArg*>(a);
724
+ parse_ruby_shape(s->ary, s->dims);
725
+ return Qnil;
726
+ },
727
+ reinterpret_cast<VALUE>(&sa),
728
+ &protect_state);
729
+
730
+ if (protect_state == 0) {
731
+ auto shape_res = validate_shape(raw_dims, dtype);
732
+ if (!shape_res.ok()) {
733
+ err_exc = status_to_ruby_exception(shape_res.status);
734
+ } else if (actual != shape_res.value.nbytes) {
735
+ std::string msg = "IO::Buffer byte length mismatch: expected " +
736
+ std::to_string(shape_res.value.nbytes) +
737
+ " bytes, got " + std::to_string(actual);
738
+ err_exc = rb_exc_new_str(
739
+ g_refs.eShapeError,
740
+ rb_utf8_str_new(msg.data(), static_cast<long>(msg.size())));
741
+ } else {
742
+ result = tensor_alloc(klass);
743
+ RbTensorWrap* wrap = nullptr;
744
+ TypedData_Get_Struct(result, RbTensorWrap, &kTensorDataType, wrap);
745
+ wrap->borrowed_buf = buf_val;
746
+ wrap->borrowed_sizes = std::move(shape_res.value.sizes);
747
+ wrap->borrowed_dtype = dtype;
748
+ wrap->borrowed_nbytes = shape_res.value.nbytes;
749
+ }
750
+ }
751
+ }
752
+
753
+ if (protect_state != 0) {
754
+ rb_jump_tag(protect_state);
755
+ }
756
+ if (err_exc != Qnil) {
757
+ raise_status_no_live_cpp(err_exc);
758
+ }
759
+ return result;
760
+ }
761
+
762
+ // Tensor.zeros(shape, dtype: :float32)
763
+ VALUE rb_tensor_s_zeros(int argc, VALUE* argv, VALUE klass) {
764
+ (void)klass;
765
+ VALUE shape_val = Qnil;
766
+ VALUE opts = Qnil;
767
+ rb_scan_args(argc, argv, "1:", &shape_val, &opts);
768
+ Check_Type(shape_val, T_ARRAY);
769
+
770
+ VALUE kw_vals[1] = {Qundef};
771
+ ID kw_ids[1] = {g_refs.id_kw_dtype};
772
+ if (!NIL_P(opts)) {
773
+ rb_get_kwargs(opts, kw_ids, 0, 1, kw_vals);
774
+ }
775
+ const auto dtype = parse_dtype_opt(kw_vals[0]);
776
+
777
+ VALUE err_exc = Qnil;
778
+ VALUE result = Qnil;
779
+ int protect_state = 0;
780
+
781
+ {
782
+ std::vector<int64_t> raw_dims;
783
+ struct ShapeArg {
784
+ VALUE ary;
785
+ std::vector<int64_t>* dims;
786
+ } sa{shape_val, &raw_dims};
787
+ rb_protect(
788
+ [](VALUE a) -> VALUE {
789
+ auto* s = reinterpret_cast<ShapeArg*>(a);
790
+ parse_ruby_shape(s->ary, s->dims);
791
+ return Qnil;
792
+ },
793
+ reinterpret_cast<VALUE>(&sa),
794
+ &protect_state);
795
+
796
+ if (protect_state == 0) {
797
+ auto shape_res = validate_shape(raw_dims, dtype);
798
+ if (!shape_res.ok()) {
799
+ err_exc = status_to_ruby_exception(shape_res.status);
800
+ } else {
801
+ auto buf_res = AlignedBuffer::allocate_zeroed(shape_res.value.nbytes);
802
+ if (!buf_res.ok()) {
803
+ err_exc = status_to_ruby_exception(buf_res.status);
804
+ } else {
805
+ auto tp_res = make_owned_tensor_ptr(
806
+ std::move(shape_res.value.sizes), dtype, std::move(buf_res.value));
807
+ if (!tp_res.ok()) {
808
+ err_exc = status_to_ruby_exception(tp_res.status);
809
+ } else {
810
+ result = wrap_owned_tensor(std::move(tp_res.value));
811
+ }
812
+ }
813
+ }
814
+ }
815
+ }
816
+
817
+ if (protect_state != 0) {
818
+ rb_jump_tag(protect_state);
819
+ }
820
+ if (err_exc != Qnil) {
821
+ raise_status_no_live_cpp(err_exc);
822
+ }
823
+ return result;
824
+ }
825
+
826
+ VALUE rb_tensor_shape(VALUE self) {
827
+ auto* wrap = check_open_tensor(self);
828
+ if (wrap->borrowed_buf != Qnil) {
829
+ VALUE ary = rb_ary_new_capa(static_cast<long>(wrap->borrowed_sizes.size()));
830
+ for (auto d : wrap->borrowed_sizes) {
831
+ rb_ary_push(ary, LONG2NUM(d));
832
+ }
833
+ return ary;
834
+ }
835
+ const auto sizes = wrap->tensor->sizes();
836
+ VALUE ary = rb_ary_new_capa(static_cast<long>(sizes.size()));
837
+ for (auto d : sizes) {
838
+ rb_ary_push(ary, LONG2NUM(d));
839
+ }
840
+ return ary;
841
+ }
842
+
843
+ VALUE rb_tensor_strides(VALUE self) {
844
+ auto* wrap = check_open_tensor(self);
845
+ if (wrap->borrowed_buf != Qnil) {
846
+ const size_t ndim = wrap->borrowed_sizes.size();
847
+ VALUE ary = rb_ary_new_capa(static_cast<long>(ndim));
848
+ if (ndim == 0) return ary;
849
+ std::vector<int64_t> st(ndim, 1);
850
+ for (size_t i = ndim - 1; i > 0; --i) {
851
+ st[i - 1] = st[i] * static_cast<int64_t>(wrap->borrowed_sizes[i]);
852
+ }
853
+ for (auto s : st) {
854
+ rb_ary_push(ary, LL2NUM(s));
855
+ }
856
+ return ary;
857
+ }
858
+ const auto strides = wrap->tensor->strides();
859
+ VALUE ary = rb_ary_new_capa(static_cast<long>(strides.size()));
860
+ for (auto s : strides) {
861
+ rb_ary_push(ary, LONG2NUM(s));
862
+ }
863
+ return ary;
864
+ }
865
+
866
+ VALUE rb_tensor_dim_order(VALUE self) {
867
+ auto* wrap = check_open_tensor(self);
868
+ if (wrap->borrowed_buf != Qnil) {
869
+ const size_t ndim = wrap->borrowed_sizes.size();
870
+ VALUE ary = rb_ary_new_capa(static_cast<long>(ndim));
871
+ for (size_t i = 0; i < ndim; ++i) {
872
+ rb_ary_push(ary, ULONG2NUM(i));
873
+ }
874
+ return ary;
875
+ }
876
+ const auto order = wrap->tensor->dim_order();
877
+ VALUE ary = rb_ary_new_capa(static_cast<long>(order.size()));
878
+ for (auto o : order) {
879
+ rb_ary_push(ary, INT2FIX(o));
880
+ }
881
+ return ary;
882
+ }
883
+
884
+ VALUE rb_tensor_contiguous_p(VALUE self) {
885
+ auto* wrap = check_open_tensor(self);
886
+ if (wrap->borrowed_buf != Qnil) {
887
+ return Qtrue;
888
+ }
889
+ return is_standard_contiguous(*wrap->tensor) ? Qtrue : Qfalse;
890
+ }
891
+
892
+ VALUE rb_tensor_dim(VALUE self) {
893
+ auto* wrap = check_open_tensor(self);
894
+ if (wrap->borrowed_buf != Qnil) {
895
+ return LONG2NUM(static_cast<long>(wrap->borrowed_sizes.size()));
896
+ }
897
+ return LONG2NUM(wrap->tensor->dim());
898
+ }
899
+
900
+ VALUE rb_tensor_numel(VALUE self) {
901
+ auto* wrap = check_open_tensor(self);
902
+ if (wrap->borrowed_buf != Qnil) {
903
+ size_t item_sz = scalar_type_size(wrap->borrowed_dtype);
904
+ return ULL2NUM(item_sz == 0 ? 0 : wrap->borrowed_nbytes / item_sz);
905
+ }
906
+ return LL2NUM(wrap->tensor->numel());
907
+ }
908
+
909
+ VALUE rb_tensor_nbytes(VALUE self) {
910
+ auto* wrap = check_open_tensor(self);
911
+ if (wrap->borrowed_buf != Qnil) {
912
+ return ULL2NUM(wrap->borrowed_nbytes);
913
+ }
914
+ return ULL2NUM(wrap->tensor->nbytes());
915
+ }
916
+
917
+ VALUE rb_tensor_dtype(VALUE self) {
918
+ auto* wrap = check_open_tensor(self);
919
+ auto st = (wrap->borrowed_buf != Qnil) ? wrap->borrowed_dtype
920
+ : wrap->tensor->scalar_type();
921
+ return scalar_type_to_sym(st);
922
+ }
923
+
924
+ VALUE rb_tensor_to_bytes(VALUE self) {
925
+ auto* wrap = check_open_tensor(self);
926
+ if (wrap->borrowed_buf != Qnil) {
927
+ const void* ptr = nullptr;
928
+ size_t len = 0;
929
+ check_borrowed_read(wrap, &ptr, &len);
930
+ VALUE str = rb_str_buf_new(static_cast<long>(len));
931
+ if (len > 0) {
932
+ rb_str_cat(str, static_cast<const char*>(ptr), static_cast<long>(len));
933
+ }
934
+ RB_GC_GUARD(self);
935
+ return str;
936
+ }
937
+
938
+ const auto& t = *wrap->tensor;
939
+ const size_t nbytes = t.nbytes();
940
+ if (nbytes > static_cast<size_t>(std::numeric_limits<long>::max())) {
941
+ rb_raise(g_refs.eMemoryError, "tensor too large for Ruby String");
942
+ }
943
+ VALUE str = rb_str_new(nullptr, static_cast<long>(nbytes));
944
+ VALUE err_exc = Qnil;
945
+ {
946
+ auto st = copy_to_contiguous_bytes(t, RSTRING_PTR(str), nbytes);
947
+ if (!st.ok()) {
948
+ err_exc = status_to_ruby_exception(st);
949
+ }
950
+ }
951
+ RB_GC_GUARD(self);
952
+ if (err_exc != Qnil) {
953
+ raise_status_no_live_cpp(err_exc);
954
+ }
955
+ return str;
956
+ }
957
+
958
+ VALUE rb_tensor_to_buffer(VALUE self) {
959
+ auto* wrap = check_open_tensor(self);
960
+ size_t nbytes = 0;
961
+ if (wrap->borrowed_buf != Qnil) {
962
+ const void* src = nullptr;
963
+ check_borrowed_read(wrap, &src, &nbytes);
964
+ VALUE io_buf = rb_io_buffer_new(nullptr, nbytes, RB_IO_BUFFER_INTERNAL);
965
+ if (nbytes > 0) {
966
+ void* dst = nullptr;
967
+ size_t dst_len = 0;
968
+ rb_io_buffer_get_bytes_for_writing(io_buf, &dst, &dst_len);
969
+ std::memcpy(dst, src, nbytes);
970
+ }
971
+ RB_GC_GUARD(self);
972
+ return io_buf;
973
+ }
974
+
975
+ const auto& t = *wrap->tensor;
976
+ nbytes = t.nbytes();
977
+ VALUE io_buf = rb_io_buffer_new(nullptr, nbytes, RB_IO_BUFFER_INTERNAL);
978
+ VALUE err_exc = Qnil;
979
+ if (nbytes > 0) {
980
+ void* dst = nullptr;
981
+ size_t dst_len = 0;
982
+ rb_io_buffer_get_bytes_for_writing(io_buf, &dst, &dst_len);
983
+ auto st = copy_to_contiguous_bytes(t, dst, nbytes);
984
+ if (!st.ok()) {
985
+ err_exc = status_to_ruby_exception(st);
986
+ }
987
+ }
988
+ RB_GC_GUARD(self);
989
+ if (err_exc != Qnil) {
990
+ raise_status_no_live_cpp(err_exc);
991
+ }
992
+ return io_buf;
993
+ }
994
+
995
+ static VALUE tensor_to_a_impl(VALUE self, bool flat) {
996
+ auto* wrap = check_open_tensor(self);
997
+ if (wrap->borrowed_buf != Qnil) {
998
+ const void* ptr = nullptr;
999
+ size_t len = 0;
1000
+ check_borrowed_read(wrap, &ptr, &len);
1001
+ const size_t item_sz = scalar_type_size(wrap->borrowed_dtype);
1002
+ const size_t numel = item_sz == 0 ? 0 : len / item_sz;
1003
+ VALUE out = build_ruby_from_contiguous(
1004
+ ptr,
1005
+ wrap->borrowed_dtype,
1006
+ wrap->borrowed_sizes.data(),
1007
+ wrap->borrowed_sizes.size(),
1008
+ numel,
1009
+ flat);
1010
+ RB_GC_GUARD(self);
1011
+ return out;
1012
+ }
1013
+
1014
+ const auto& t = *wrap->tensor;
1015
+ const size_t numel = static_cast<size_t>(t.numel());
1016
+ const size_t ndim = static_cast<size_t>(t.dim());
1017
+
1018
+ if (is_standard_contiguous(t)) {
1019
+ VALUE out = build_ruby_from_contiguous(
1020
+ t.const_data_ptr(),
1021
+ t.scalar_type(),
1022
+ t.sizes().data(),
1023
+ ndim,
1024
+ numel,
1025
+ flat);
1026
+ RB_GC_GUARD(self);
1027
+ return out;
1028
+ }
1029
+
1030
+ // Non-contiguous: copy into temporary contiguous buffer first, then convert
1031
+ // inside rb_protect so buffer is always released if Ruby raises OOM.
1032
+ VALUE err_exc = Qnil;
1033
+ VALUE out = Qnil;
1034
+ int protect_state = 0;
1035
+ {
1036
+ auto buf_res = AlignedBuffer::allocate(t.nbytes());
1037
+ if (!buf_res.ok()) {
1038
+ err_exc = status_to_ruby_exception(buf_res.status);
1039
+ } else {
1040
+ auto cp = copy_to_contiguous_bytes(t, buf_res.value.ptr, t.nbytes());
1041
+ if (!cp.ok()) {
1042
+ err_exc = status_to_ruby_exception(cp);
1043
+ } else {
1044
+ struct BuildArg {
1045
+ const void* raw;
1046
+ executorch::aten::ScalarType dtype;
1047
+ const executorch::aten::SizesType* sizes;
1048
+ size_t ndim;
1049
+ size_t numel;
1050
+ bool flat;
1051
+ } ba{buf_res.value.ptr, t.scalar_type(), t.sizes().data(), ndim, numel, flat};
1052
+ out = rb_protect(
1053
+ [](VALUE a) -> VALUE {
1054
+ auto* b = reinterpret_cast<BuildArg*>(a);
1055
+ return build_ruby_from_contiguous(
1056
+ b->raw, b->dtype, b->sizes, b->ndim, b->numel, b->flat);
1057
+ },
1058
+ reinterpret_cast<VALUE>(&ba),
1059
+ &protect_state);
1060
+ }
1061
+ }
1062
+ }
1063
+ RB_GC_GUARD(self);
1064
+ if (protect_state != 0) rb_jump_tag(protect_state);
1065
+ if (err_exc != Qnil) raise_status_no_live_cpp(err_exc);
1066
+ return out;
1067
+ }
1068
+
1069
+ VALUE rb_tensor_to_a(VALUE self) { return tensor_to_a_impl(self, false); }
1070
+ VALUE rb_tensor_flat_to_a(VALUE self) { return tensor_to_a_impl(self, true); }
1071
+
1072
+ VALUE rb_tensor_item(VALUE self) {
1073
+ auto* wrap = check_open_tensor(self);
1074
+ const size_t numel = (wrap->borrowed_buf != Qnil)
1075
+ ? (wrap->borrowed_nbytes /
1076
+ std::max<size_t>(1, scalar_type_size(wrap->borrowed_dtype)))
1077
+ : static_cast<size_t>(wrap->tensor->numel());
1078
+ if (numel != 1) {
1079
+ rb_raise(
1080
+ g_refs.eShapeError,
1081
+ "item requires a tensor with exactly 1 element, got %zu",
1082
+ numel);
1083
+ }
1084
+ const void* ptr = nullptr;
1085
+ executorch::aten::ScalarType dtype = executorch::aten::ScalarType::Undefined;
1086
+ if (wrap->borrowed_buf != Qnil) {
1087
+ size_t len = 0;
1088
+ check_borrowed_read(wrap, &ptr, &len);
1089
+ dtype = wrap->borrowed_dtype;
1090
+ } else {
1091
+ ptr = wrap->tensor->const_data_ptr();
1092
+ dtype = wrap->tensor->scalar_type();
1093
+ }
1094
+ VALUE out = build_ruby_from_contiguous(ptr, dtype, nullptr, 0, 1, true);
1095
+ RB_GC_GUARD(self);
1096
+ return out;
1097
+ }
1098
+
1099
+ VALUE rb_tensor_close(VALUE self) {
1100
+ RbTensorWrap* wrap = nullptr;
1101
+ TypedData_Get_Struct(self, RbTensorWrap, &kTensorDataType, wrap);
1102
+ if (wrap != nullptr && !wrap->closed) {
1103
+ wrap->closed = true;
1104
+ wrap->tensor.reset();
1105
+ wrap->borrowed_buf = Qnil;
1106
+ wrap->borrowed_sizes.clear();
1107
+ if (wrap->tracked_bytes > 0) {
1108
+ rb_gc_adjust_memory_usage(-static_cast<ssize_t>(wrap->tracked_bytes));
1109
+ wrap->tracked_bytes = 0;
1110
+ }
1111
+ }
1112
+ return Qnil;
1113
+ }
1114
+
1115
+ VALUE rb_tensor_closed_p(VALUE self) {
1116
+ RbTensorWrap* wrap = nullptr;
1117
+ TypedData_Get_Struct(self, RbTensorWrap, &kTensorDataType, wrap);
1118
+ return (wrap == nullptr || wrap->closed) ? Qtrue : Qfalse;
1119
+ }
1120
+
1121
+ VALUE rb_tensor_eq(VALUE self, VALUE other) {
1122
+ if (!is_rb_tensor(other)) {
1123
+ return Qfalse;
1124
+ }
1125
+ auto* a = check_open_tensor(self);
1126
+ auto* b = check_open_tensor(other);
1127
+ if (a == b) return Qtrue;
1128
+
1129
+ const auto dtype_a = (a->borrowed_buf != Qnil) ? a->borrowed_dtype : a->tensor->scalar_type();
1130
+ const auto dtype_b = (b->borrowed_buf != Qnil) ? b->borrowed_dtype : b->tensor->scalar_type();
1131
+ if (dtype_a != dtype_b) return Qfalse;
1132
+
1133
+ const executorch::aten::SizesType* sa =
1134
+ (a->borrowed_buf != Qnil) ? a->borrowed_sizes.data() : a->tensor->sizes().data();
1135
+ const size_t da =
1136
+ (a->borrowed_buf != Qnil) ? a->borrowed_sizes.size() : static_cast<size_t>(a->tensor->dim());
1137
+ const executorch::aten::SizesType* sb =
1138
+ (b->borrowed_buf != Qnil) ? b->borrowed_sizes.data() : b->tensor->sizes().data();
1139
+ const size_t db =
1140
+ (b->borrowed_buf != Qnil) ? b->borrowed_sizes.size() : static_cast<size_t>(b->tensor->dim());
1141
+ if (da != db) return Qfalse;
1142
+ for (size_t i = 0; i < da; ++i) {
1143
+ if (sa[i] != sb[i]) return Qfalse;
1144
+ }
1145
+
1146
+ VALUE ba = rb_tensor_to_bytes(self);
1147
+ VALUE bb = rb_tensor_to_bytes(other);
1148
+ if (RSTRING_LEN(ba) != RSTRING_LEN(bb)) return Qfalse;
1149
+ const size_t n = static_cast<size_t>(RSTRING_LEN(ba));
1150
+ if (n == 0) return Qtrue;
1151
+ bool eq = (std::memcmp(RSTRING_PTR(ba), RSTRING_PTR(bb), n) == 0);
1152
+ RB_GC_GUARD(ba);
1153
+ RB_GC_GUARD(bb);
1154
+ return eq ? Qtrue : Qfalse;
1155
+ }
1156
+
1157
+ } // namespace
1158
+
1159
+ const rb_data_type_t kTensorDataType = {
1160
+ "Executorch::Tensor",
1161
+ {
1162
+ tensor_mark,
1163
+ tensor_free,
1164
+ tensor_memsize,
1165
+ nullptr,
1166
+ {nullptr},
1167
+ },
1168
+ nullptr,
1169
+ nullptr,
1170
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
1171
+ };
1172
+
1173
+ VALUE wrap_owned_tensor(executorch::extension::TensorPtr ptr) {
1174
+ VALUE obj = tensor_alloc(g_refs.cTensor);
1175
+ RbTensorWrap* wrap = nullptr;
1176
+ TypedData_Get_Struct(obj, RbTensorWrap, &kTensorDataType, wrap);
1177
+ const size_t nb = ptr ? ptr->nbytes() : 0;
1178
+ wrap->tensor = std::move(ptr);
1179
+ wrap->tracked_bytes = nb;
1180
+ if (nb > 0) {
1181
+ rb_gc_adjust_memory_usage(static_cast<ssize_t>(nb));
1182
+ }
1183
+ return obj;
1184
+ }
1185
+
1186
+ bool is_rb_tensor(VALUE obj) {
1187
+ return rb_typeddata_is_kind_of(obj, &kTensorDataType) != 0;
1188
+ }
1189
+
1190
+ RbTensorWrap* get_rb_tensor(VALUE obj) {
1191
+ return check_open_tensor(obj);
1192
+ }
1193
+
1194
+ VALUE scalar_type_to_sym(executorch::aten::ScalarType st) {
1195
+ switch (st) {
1196
+ case executorch::aten::ScalarType::Float: return ID2SYM(g_refs.id_float32);
1197
+ case executorch::aten::ScalarType::Double: return ID2SYM(g_refs.id_float64);
1198
+ case executorch::aten::ScalarType::Half: return ID2SYM(g_refs.id_float16);
1199
+ case executorch::aten::ScalarType::BFloat16: return ID2SYM(g_refs.id_bfloat16);
1200
+ case executorch::aten::ScalarType::Byte: return ID2SYM(g_refs.id_uint8);
1201
+ case executorch::aten::ScalarType::Char: return ID2SYM(g_refs.id_int8);
1202
+ case executorch::aten::ScalarType::Short: return ID2SYM(g_refs.id_int16);
1203
+ case executorch::aten::ScalarType::UInt16: return ID2SYM(g_refs.id_uint16);
1204
+ case executorch::aten::ScalarType::Int: return ID2SYM(g_refs.id_int32);
1205
+ case executorch::aten::ScalarType::UInt32: return ID2SYM(g_refs.id_uint32);
1206
+ case executorch::aten::ScalarType::Long: return ID2SYM(g_refs.id_int64);
1207
+ case executorch::aten::ScalarType::UInt64: return ID2SYM(g_refs.id_uint64);
1208
+ case executorch::aten::ScalarType::Bool: return ID2SYM(g_refs.id_bool);
1209
+ default: return ID2SYM(rb_intern(scalar_type_name(st)));
1210
+ }
1211
+ }
1212
+
1213
+ bool sym_to_scalar_type(VALUE sym, executorch::aten::ScalarType* out) {
1214
+ if (!RB_SYMBOL_P(sym)) {
1215
+ return false;
1216
+ }
1217
+ ID id = SYM2ID(sym);
1218
+ if (id == g_refs.id_float32) { *out = executorch::aten::ScalarType::Float; return true; }
1219
+ if (id == g_refs.id_float64) { *out = executorch::aten::ScalarType::Double; return true; }
1220
+ if (id == g_refs.id_int64) { *out = executorch::aten::ScalarType::Long; return true; }
1221
+ if (id == g_refs.id_int32) { *out = executorch::aten::ScalarType::Int; return true; }
1222
+ if (id == g_refs.id_uint8) { *out = executorch::aten::ScalarType::Byte; return true; }
1223
+ if (id == g_refs.id_bool) { *out = executorch::aten::ScalarType::Bool; return true; }
1224
+ if (id == g_refs.id_float16) { *out = executorch::aten::ScalarType::Half; return true; }
1225
+ if (id == g_refs.id_bfloat16) { *out = executorch::aten::ScalarType::BFloat16; return true; }
1226
+ if (id == g_refs.id_int8) { *out = executorch::aten::ScalarType::Char; return true; }
1227
+ if (id == g_refs.id_int16) { *out = executorch::aten::ScalarType::Short; return true; }
1228
+ if (id == g_refs.id_uint16) { *out = executorch::aten::ScalarType::UInt16; return true; }
1229
+ if (id == g_refs.id_uint32) { *out = executorch::aten::ScalarType::UInt32; return true; }
1230
+ if (id == g_refs.id_uint64) { *out = executorch::aten::ScalarType::UInt64; return true; }
1231
+ const char* name = rb_id2name(id);
1232
+ return name != nullptr && parse_dtype_name(name, out);
1233
+ }
1234
+
1235
+ void init_tensor(VALUE mExecutorch) {
1236
+ g_refs.id_float32 = rb_intern("float32");
1237
+ g_refs.id_float64 = rb_intern("float64");
1238
+ g_refs.id_float16 = rb_intern("float16");
1239
+ g_refs.id_bfloat16 = rb_intern("bfloat16");
1240
+ g_refs.id_uint8 = rb_intern("uint8");
1241
+ g_refs.id_int8 = rb_intern("int8");
1242
+ g_refs.id_int16 = rb_intern("int16");
1243
+ g_refs.id_uint16 = rb_intern("uint16");
1244
+ g_refs.id_int32 = rb_intern("int32");
1245
+ g_refs.id_uint32 = rb_intern("uint32");
1246
+ g_refs.id_int64 = rb_intern("int64");
1247
+ g_refs.id_uint64 = rb_intern("uint64");
1248
+ g_refs.id_bool = rb_intern("bool");
1249
+ g_refs.id_unknown = rb_intern("unknown");
1250
+ g_refs.id_kw_shape = rb_intern("shape");
1251
+ g_refs.id_kw_dtype = rb_intern("dtype");
1252
+
1253
+ g_refs.cTensor = rb_define_class_under(mExecutorch, "Tensor", rb_cObject);
1254
+ rb_global_variable(&g_refs.cTensor);
1255
+ rb_undef_alloc_func(g_refs.cTensor);
1256
+
1257
+ rb_define_singleton_method(g_refs.cTensor, "new", rb_tensor_s_new, -1);
1258
+ rb_define_singleton_method(g_refs.cTensor, "from_bytes", rb_tensor_s_from_bytes, -1);
1259
+ rb_define_singleton_method(g_refs.cTensor, "from_buffer", rb_tensor_s_from_buffer, -1);
1260
+ rb_define_singleton_method(g_refs.cTensor, "zeros", rb_tensor_s_zeros, -1);
1261
+
1262
+ rb_define_method(g_refs.cTensor, "shape", rb_tensor_shape, 0);
1263
+ rb_define_method(g_refs.cTensor, "strides", rb_tensor_strides, 0);
1264
+ rb_define_method(g_refs.cTensor, "dim_order", rb_tensor_dim_order, 0);
1265
+ rb_define_method(g_refs.cTensor, "contiguous?", rb_tensor_contiguous_p, 0);
1266
+ rb_define_method(g_refs.cTensor, "dim", rb_tensor_dim, 0);
1267
+ rb_define_method(g_refs.cTensor, "ndim", rb_tensor_dim, 0);
1268
+ rb_define_method(g_refs.cTensor, "numel", rb_tensor_numel, 0);
1269
+ rb_define_method(g_refs.cTensor, "nbytes", rb_tensor_nbytes, 0);
1270
+ rb_define_method(g_refs.cTensor, "dtype", rb_tensor_dtype, 0);
1271
+ rb_define_method(g_refs.cTensor, "to_a", rb_tensor_to_a, 0);
1272
+ rb_define_method(g_refs.cTensor, "flat_to_a", rb_tensor_flat_to_a, 0);
1273
+ rb_define_method(g_refs.cTensor, "to_bytes", rb_tensor_to_bytes, 0);
1274
+ rb_define_method(g_refs.cTensor, "to_buffer", rb_tensor_to_buffer, 0);
1275
+ rb_define_method(g_refs.cTensor, "item", rb_tensor_item, 0);
1276
+ rb_define_method(g_refs.cTensor, "close", rb_tensor_close, 0);
1277
+ rb_define_method(g_refs.cTensor, "closed?", rb_tensor_closed_p, 0);
1278
+ rb_define_method(g_refs.cTensor, "==", rb_tensor_eq, 1);
1279
+ }
1280
+
1281
+ } // namespace etrb