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.
- checksums.yaml +4 -4
- data/.executorch-version +1 -0
- data/CHANGELOG.md +24 -38
- data/README.md +143 -129
- data/ext/executorch/core/core.cpp +1131 -0
- data/ext/executorch/core/dtype.hpp +157 -0
- data/ext/executorch/core/module_core.hpp +165 -0
- data/ext/executorch/core/status.hpp +100 -0
- data/ext/executorch/core/tensor_core.hpp +73 -0
- data/ext/executorch/executorch.map +6 -0
- data/ext/executorch/extconf.rb +254 -173
- data/ext/executorch/rb_errors.cpp +117 -0
- data/ext/executorch/rb_executorch.cpp +69 -0
- data/ext/executorch/rb_internal.hpp +121 -0
- data/ext/executorch/rb_module.cpp +760 -0
- data/ext/executorch/rb_tensor.cpp +1281 -0
- data/lib/executorch/method_meta.rb +70 -0
- data/lib/executorch/module.rb +56 -0
- data/lib/executorch/sdk.rb +251 -0
- data/lib/executorch/tensor.rb +85 -0
- data/lib/executorch/version.rb +3 -1
- data/lib/executorch.rb +8 -204
- metadata +26 -23
- data/ext/executorch/executorch.cpp +0 -582
- data/ext/executorch/utils.h +0 -140
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cstddef>
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <cstring>
|
|
6
|
+
#include <string_view>
|
|
7
|
+
|
|
8
|
+
#include <executorch/runtime/core/exec_aten/exec_aten.h>
|
|
9
|
+
#include <executorch/runtime/core/portable_type/bfloat16.h>
|
|
10
|
+
#include <executorch/runtime/core/portable_type/half.h>
|
|
11
|
+
|
|
12
|
+
namespace etrb {
|
|
13
|
+
|
|
14
|
+
// Single source of truth for all Ruby-constructible / readable ExecuTorch dtypes.
|
|
15
|
+
// Tuple: (ScalarType enumerator, canonical Ruby name, C++ storage type, byte size)
|
|
16
|
+
#define ETRB_FORALL_DTYPES(_) \
|
|
17
|
+
_(Float, "float32", float, 4) \
|
|
18
|
+
_(Double, "float64", double, 8) \
|
|
19
|
+
_(Half, "float16", executorch::runtime::etensor::Half, 2) \
|
|
20
|
+
_(BFloat16, "bfloat16", executorch::runtime::etensor::BFloat16, 2) \
|
|
21
|
+
_(Byte, "uint8", uint8_t, 1) \
|
|
22
|
+
_(Char, "int8", int8_t, 1) \
|
|
23
|
+
_(Short, "int16", int16_t, 2) \
|
|
24
|
+
_(Int, "int32", int32_t, 4) \
|
|
25
|
+
_(Long, "int64", int64_t, 8) \
|
|
26
|
+
_(UInt16, "uint16", uint16_t, 2) \
|
|
27
|
+
_(UInt32, "uint32", uint32_t, 4) \
|
|
28
|
+
_(UInt64, "uint64", uint64_t, 8) \
|
|
29
|
+
_(Bool, "bool", bool, 1)
|
|
30
|
+
|
|
31
|
+
inline constexpr const char* scalar_type_name(
|
|
32
|
+
executorch::aten::ScalarType st) noexcept {
|
|
33
|
+
switch (st) {
|
|
34
|
+
#define CASE_NAME(et_enum, name, ctype, sz) \
|
|
35
|
+
case executorch::aten::ScalarType::et_enum: \
|
|
36
|
+
return name;
|
|
37
|
+
ETRB_FORALL_DTYPES(CASE_NAME)
|
|
38
|
+
#undef CASE_NAME
|
|
39
|
+
case executorch::aten::ScalarType::QInt8:
|
|
40
|
+
return "qint8";
|
|
41
|
+
case executorch::aten::ScalarType::QUInt8:
|
|
42
|
+
return "quint8";
|
|
43
|
+
case executorch::aten::ScalarType::QInt32:
|
|
44
|
+
return "qint32";
|
|
45
|
+
case executorch::aten::ScalarType::ComplexHalf:
|
|
46
|
+
return "complex32";
|
|
47
|
+
case executorch::aten::ScalarType::ComplexFloat:
|
|
48
|
+
return "complex64";
|
|
49
|
+
case executorch::aten::ScalarType::ComplexDouble:
|
|
50
|
+
return "complex128";
|
|
51
|
+
case executorch::aten::ScalarType::Float8_e5m2:
|
|
52
|
+
return "float8_e5m2";
|
|
53
|
+
case executorch::aten::ScalarType::Float8_e4m3fn:
|
|
54
|
+
return "float8_e4m3fn";
|
|
55
|
+
default:
|
|
56
|
+
return "unknown";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
inline constexpr size_t scalar_type_size(
|
|
61
|
+
executorch::aten::ScalarType st) noexcept {
|
|
62
|
+
switch (st) {
|
|
63
|
+
#define CASE_SIZE(et_enum, name, ctype, sz) \
|
|
64
|
+
case executorch::aten::ScalarType::et_enum: \
|
|
65
|
+
return sz;
|
|
66
|
+
ETRB_FORALL_DTYPES(CASE_SIZE)
|
|
67
|
+
#undef CASE_SIZE
|
|
68
|
+
case executorch::aten::ScalarType::QInt8:
|
|
69
|
+
case executorch::aten::ScalarType::QUInt8:
|
|
70
|
+
case executorch::aten::ScalarType::Float8_e5m2:
|
|
71
|
+
case executorch::aten::ScalarType::Float8_e4m3fn:
|
|
72
|
+
return 1;
|
|
73
|
+
case executorch::aten::ScalarType::QInt32:
|
|
74
|
+
case executorch::aten::ScalarType::ComplexHalf:
|
|
75
|
+
return 4;
|
|
76
|
+
case executorch::aten::ScalarType::ComplexFloat:
|
|
77
|
+
return 8;
|
|
78
|
+
case executorch::aten::ScalarType::ComplexDouble:
|
|
79
|
+
return 16;
|
|
80
|
+
default:
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
inline constexpr bool is_supported_dtype(
|
|
86
|
+
executorch::aten::ScalarType st) noexcept {
|
|
87
|
+
switch (st) {
|
|
88
|
+
#define CASE_SUP(et_enum, name, ctype, sz) \
|
|
89
|
+
case executorch::aten::ScalarType::et_enum: \
|
|
90
|
+
return true;
|
|
91
|
+
ETRB_FORALL_DTYPES(CASE_SUP)
|
|
92
|
+
#undef CASE_SUP
|
|
93
|
+
default:
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Parse canonical dtype names AND backward-compatible short aliases.
|
|
99
|
+
inline bool parse_dtype_name(
|
|
100
|
+
std::string_view name,
|
|
101
|
+
executorch::aten::ScalarType* out) noexcept {
|
|
102
|
+
if (name == "float32" || name == "float") {
|
|
103
|
+
*out = executorch::aten::ScalarType::Float;
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
if (name == "float64" || name == "double") {
|
|
107
|
+
*out = executorch::aten::ScalarType::Double;
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
if (name == "float16" || name == "half") {
|
|
111
|
+
*out = executorch::aten::ScalarType::Half;
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
if (name == "bfloat16" || name == "bf16") {
|
|
115
|
+
*out = executorch::aten::ScalarType::BFloat16;
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
if (name == "uint8" || name == "byte") {
|
|
119
|
+
*out = executorch::aten::ScalarType::Byte;
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
if (name == "int8" || name == "char") {
|
|
123
|
+
*out = executorch::aten::ScalarType::Char;
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
if (name == "int16" || name == "short") {
|
|
127
|
+
*out = executorch::aten::ScalarType::Short;
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
if (name == "int32" || name == "int") {
|
|
131
|
+
*out = executorch::aten::ScalarType::Int;
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
if (name == "int64" || name == "long") {
|
|
135
|
+
*out = executorch::aten::ScalarType::Long;
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
if (name == "uint16") {
|
|
139
|
+
*out = executorch::aten::ScalarType::UInt16;
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
if (name == "uint32") {
|
|
143
|
+
*out = executorch::aten::ScalarType::UInt32;
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
if (name == "uint64") {
|
|
147
|
+
*out = executorch::aten::ScalarType::UInt64;
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
if (name == "bool") {
|
|
151
|
+
*out = executorch::aten::ScalarType::Bool;
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
} // namespace etrb
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cstddef>
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <mutex>
|
|
7
|
+
#include <shared_mutex>
|
|
8
|
+
#include <string>
|
|
9
|
+
#include <unordered_map>
|
|
10
|
+
#include <utility>
|
|
11
|
+
#include <vector>
|
|
12
|
+
|
|
13
|
+
#include <executorch/extension/module/module.h>
|
|
14
|
+
#include <executorch/extension/tensor/tensor_ptr.h>
|
|
15
|
+
|
|
16
|
+
#include "status.hpp"
|
|
17
|
+
|
|
18
|
+
namespace etrb {
|
|
19
|
+
|
|
20
|
+
enum class ValueKind : uint8_t {
|
|
21
|
+
None = 0,
|
|
22
|
+
Int,
|
|
23
|
+
Double,
|
|
24
|
+
Bool,
|
|
25
|
+
String,
|
|
26
|
+
Tensor,
|
|
27
|
+
IntList,
|
|
28
|
+
DoubleList,
|
|
29
|
+
BoolList,
|
|
30
|
+
TensorList,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Owned, Ruby-independent representation of an ExecuTorch EValue.
|
|
34
|
+
// Safe to pass across GVL boundaries without touching any Ruby VALUEs.
|
|
35
|
+
struct CoreValue {
|
|
36
|
+
ValueKind kind = ValueKind::None;
|
|
37
|
+
int64_t int_val = 0;
|
|
38
|
+
double double_val = 0.0;
|
|
39
|
+
bool bool_val = false;
|
|
40
|
+
std::string string_val;
|
|
41
|
+
executorch::extension::TensorPtr tensor_val;
|
|
42
|
+
std::vector<int64_t> int_list;
|
|
43
|
+
std::vector<double> double_list;
|
|
44
|
+
std::vector<bool> bool_list;
|
|
45
|
+
std::vector<executorch::extension::TensorPtr> tensor_list;
|
|
46
|
+
|
|
47
|
+
static CoreValue None() noexcept { return CoreValue{}; }
|
|
48
|
+
static CoreValue Int(int64_t v) noexcept {
|
|
49
|
+
CoreValue c;
|
|
50
|
+
c.kind = ValueKind::Int;
|
|
51
|
+
c.int_val = v;
|
|
52
|
+
return c;
|
|
53
|
+
}
|
|
54
|
+
static CoreValue Double(double v) noexcept {
|
|
55
|
+
CoreValue c;
|
|
56
|
+
c.kind = ValueKind::Double;
|
|
57
|
+
c.double_val = v;
|
|
58
|
+
return c;
|
|
59
|
+
}
|
|
60
|
+
static CoreValue Bool(bool v) noexcept {
|
|
61
|
+
CoreValue c;
|
|
62
|
+
c.kind = ValueKind::Bool;
|
|
63
|
+
c.bool_val = v;
|
|
64
|
+
return c;
|
|
65
|
+
}
|
|
66
|
+
static CoreValue String(std::string v) noexcept {
|
|
67
|
+
CoreValue c;
|
|
68
|
+
c.kind = ValueKind::String;
|
|
69
|
+
c.string_val = std::move(v);
|
|
70
|
+
return c;
|
|
71
|
+
}
|
|
72
|
+
static CoreValue Tensor(executorch::extension::TensorPtr ptr) noexcept {
|
|
73
|
+
CoreValue c;
|
|
74
|
+
c.kind = ValueKind::Tensor;
|
|
75
|
+
c.tensor_val = std::move(ptr);
|
|
76
|
+
return c;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
struct TensorMetaSnapshot {
|
|
81
|
+
std::string name;
|
|
82
|
+
executorch::aten::ScalarType dtype = executorch::aten::ScalarType::Undefined;
|
|
83
|
+
std::vector<int32_t> sizes;
|
|
84
|
+
std::vector<uint8_t> dim_order;
|
|
85
|
+
size_t nbytes = 0;
|
|
86
|
+
bool memory_planned = false;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
struct ValueMetaSnapshot {
|
|
90
|
+
executorch::runtime::Tag tag = executorch::runtime::Tag::None;
|
|
91
|
+
TensorMetaSnapshot tensor; // Valid when tag == Tag::Tensor
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
struct MethodMetaSnapshot {
|
|
95
|
+
std::string name;
|
|
96
|
+
std::vector<ValueMetaSnapshot> inputs;
|
|
97
|
+
std::vector<ValueMetaSnapshot> outputs;
|
|
98
|
+
std::vector<std::string> backends;
|
|
99
|
+
std::vector<int64_t> planned_buffer_sizes;
|
|
100
|
+
int64_t planned_memory_bytes = 0;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
class ModuleCore {
|
|
104
|
+
public:
|
|
105
|
+
using LoadMode = executorch::extension::Module::LoadMode;
|
|
106
|
+
|
|
107
|
+
~ModuleCore() noexcept = default;
|
|
108
|
+
|
|
109
|
+
ModuleCore(const ModuleCore&) = delete;
|
|
110
|
+
ModuleCore& operator=(const ModuleCore&) = delete;
|
|
111
|
+
|
|
112
|
+
static StatusOr<std::unique_ptr<ModuleCore>> open_file(
|
|
113
|
+
const std::string& path,
|
|
114
|
+
const std::vector<std::string>& data_files,
|
|
115
|
+
LoadMode load_mode) noexcept;
|
|
116
|
+
|
|
117
|
+
static StatusOr<std::unique_ptr<ModuleCore>> open_bytes(
|
|
118
|
+
std::vector<uint8_t> bytes) noexcept;
|
|
119
|
+
|
|
120
|
+
// Deterministic resource release. Waits for in-flight calls via unique lock.
|
|
121
|
+
void close() noexcept;
|
|
122
|
+
bool is_closed() const noexcept;
|
|
123
|
+
|
|
124
|
+
std::string source_label() const noexcept;
|
|
125
|
+
size_t estimated_memory_bytes() const noexcept;
|
|
126
|
+
|
|
127
|
+
StatusOr<std::vector<std::string>> method_names() noexcept;
|
|
128
|
+
StatusOr<MethodMetaSnapshot> method_meta(const std::string& name) noexcept;
|
|
129
|
+
Status load_method(const std::string& name) noexcept;
|
|
130
|
+
StatusOr<bool> unload_method(const std::string& name) noexcept;
|
|
131
|
+
bool is_method_loaded(const std::string& name) noexcept;
|
|
132
|
+
|
|
133
|
+
// Thread-safe execution (per-method mutex under shared lifecycle lock).
|
|
134
|
+
// If `out_targets` is non-empty, writes tensor outputs directly into the
|
|
135
|
+
// caller-provided pre-allocated output tensors (validating count, shape,
|
|
136
|
+
// dtype, and contiguity) instead of allocating new output tensors.
|
|
137
|
+
StatusOr<std::vector<CoreValue>> execute(
|
|
138
|
+
const std::string& method_name,
|
|
139
|
+
const std::vector<CoreValue>& inputs,
|
|
140
|
+
const std::vector<executorch::extension::TensorPtr>& out_targets) noexcept;
|
|
141
|
+
|
|
142
|
+
private:
|
|
143
|
+
ModuleCore() = default;
|
|
144
|
+
|
|
145
|
+
std::mutex& mutex_for_method(const std::string& name);
|
|
146
|
+
|
|
147
|
+
mutable std::shared_mutex lifecycle_mu_;
|
|
148
|
+
bool closed_ = false;
|
|
149
|
+
std::string source_label_;
|
|
150
|
+
size_t estimated_bytes_ = 0;
|
|
151
|
+
// Owned backing bytes when loaded via open_bytes (must outlive module_).
|
|
152
|
+
std::vector<uint8_t> owned_bytes_;
|
|
153
|
+
std::unique_ptr<executorch::extension::Module> module_;
|
|
154
|
+
|
|
155
|
+
std::mutex method_mutexes_mu_;
|
|
156
|
+
std::unordered_map<std::string, std::unique_ptr<std::mutex>> method_mutexes_;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// Runtime introspection & threadpool controls
|
|
160
|
+
std::vector<std::string> registered_backends() noexcept;
|
|
161
|
+
size_t registered_kernel_count() noexcept;
|
|
162
|
+
int64_t get_num_threads() noexcept;
|
|
163
|
+
Status set_num_threads(int64_t n) noexcept;
|
|
164
|
+
|
|
165
|
+
} // namespace etrb
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cstdint>
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <utility>
|
|
6
|
+
|
|
7
|
+
#include <executorch/runtime/core/error.h>
|
|
8
|
+
|
|
9
|
+
namespace etrb {
|
|
10
|
+
|
|
11
|
+
enum class ErrorKind : uint8_t {
|
|
12
|
+
Ok = 0,
|
|
13
|
+
Load,
|
|
14
|
+
MethodNotFound,
|
|
15
|
+
OperatorMissing,
|
|
16
|
+
Delegate,
|
|
17
|
+
Shape,
|
|
18
|
+
DType,
|
|
19
|
+
Execution,
|
|
20
|
+
Memory,
|
|
21
|
+
Closed,
|
|
22
|
+
Argument,
|
|
23
|
+
Type,
|
|
24
|
+
Range,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
struct Status {
|
|
28
|
+
ErrorKind kind = ErrorKind::Ok;
|
|
29
|
+
uint32_t et_code = 0;
|
|
30
|
+
std::string message;
|
|
31
|
+
std::string pal_log;
|
|
32
|
+
|
|
33
|
+
bool ok() const noexcept { return kind == ErrorKind::Ok; }
|
|
34
|
+
|
|
35
|
+
static Status Ok() noexcept { return Status{}; }
|
|
36
|
+
|
|
37
|
+
static Status Make(
|
|
38
|
+
ErrorKind k,
|
|
39
|
+
std::string msg,
|
|
40
|
+
uint32_t code = 0,
|
|
41
|
+
std::string log = {}) noexcept {
|
|
42
|
+
Status s;
|
|
43
|
+
s.kind = k;
|
|
44
|
+
s.et_code = code;
|
|
45
|
+
s.message = std::move(msg);
|
|
46
|
+
s.pal_log = std::move(log);
|
|
47
|
+
return s;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
template <typename T>
|
|
52
|
+
struct StatusOr {
|
|
53
|
+
Status status;
|
|
54
|
+
T value{};
|
|
55
|
+
|
|
56
|
+
bool ok() const noexcept { return status.ok(); }
|
|
57
|
+
|
|
58
|
+
static StatusOr<T> Ok(T v) noexcept {
|
|
59
|
+
StatusOr<T> out;
|
|
60
|
+
out.status = Status::Ok();
|
|
61
|
+
out.value = std::move(v);
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static StatusOr<T> Err(Status s) noexcept {
|
|
66
|
+
StatusOr<T> out;
|
|
67
|
+
out.status = std::move(s);
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// Initialize ExecuTorch runtime and register thread-local PAL log capture once.
|
|
73
|
+
void init_runtime_once() noexcept;
|
|
74
|
+
|
|
75
|
+
// Scoped thread-local PAL log collector. Captures Error/Fatal PAL messages
|
|
76
|
+
// emitted on the current thread during an ExecuTorch call so they can be
|
|
77
|
+
// attached to Ruby exceptions.
|
|
78
|
+
class PalLogScope {
|
|
79
|
+
public:
|
|
80
|
+
PalLogScope() noexcept;
|
|
81
|
+
~PalLogScope() noexcept;
|
|
82
|
+
|
|
83
|
+
PalLogScope(const PalLogScope&) = delete;
|
|
84
|
+
PalLogScope& operator=(const PalLogScope&) = delete;
|
|
85
|
+
|
|
86
|
+
std::string take() noexcept;
|
|
87
|
+
|
|
88
|
+
private:
|
|
89
|
+
std::string* prev_buf_ = nullptr;
|
|
90
|
+
std::string buf_;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// Map an ExecuTorch runtime::Error + context + captured PAL log to Status.
|
|
94
|
+
Status status_from_et_error(
|
|
95
|
+
executorch::runtime::Error err,
|
|
96
|
+
const char* context,
|
|
97
|
+
std::string pal_log = {},
|
|
98
|
+
ErrorKind default_kind = ErrorKind::Execution) noexcept;
|
|
99
|
+
|
|
100
|
+
} // namespace etrb
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cstddef>
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <vector>
|
|
7
|
+
|
|
8
|
+
#include <executorch/extension/tensor/tensor_ptr.h>
|
|
9
|
+
|
|
10
|
+
#include "dtype.hpp"
|
|
11
|
+
#include "status.hpp"
|
|
12
|
+
|
|
13
|
+
namespace etrb {
|
|
14
|
+
|
|
15
|
+
struct ShapeInfo {
|
|
16
|
+
std::vector<executorch::aten::SizesType> sizes;
|
|
17
|
+
size_t numel = 0;
|
|
18
|
+
size_t nbytes = 0;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Overflow-safe validation of shape dims (0 <= d <= INT32_MAX), numel, and nbytes.
|
|
22
|
+
StatusOr<ShapeInfo> validate_shape(
|
|
23
|
+
const std::vector<int64_t>& raw_dims,
|
|
24
|
+
executorch::aten::ScalarType dtype) noexcept;
|
|
25
|
+
|
|
26
|
+
// Allocate raw uninitialized byte storage (no redundant zero-fill before memcpy)
|
|
27
|
+
// aligned to at least 64 bytes for SIMD/XNNPACK friendliness.
|
|
28
|
+
struct AlignedBuffer {
|
|
29
|
+
void* ptr = nullptr;
|
|
30
|
+
size_t nbytes = 0;
|
|
31
|
+
|
|
32
|
+
AlignedBuffer() = default;
|
|
33
|
+
~AlignedBuffer() noexcept;
|
|
34
|
+
|
|
35
|
+
AlignedBuffer(const AlignedBuffer&) = delete;
|
|
36
|
+
AlignedBuffer& operator=(const AlignedBuffer&) = delete;
|
|
37
|
+
AlignedBuffer(AlignedBuffer&& other) noexcept;
|
|
38
|
+
AlignedBuffer& operator=(AlignedBuffer&& other) noexcept;
|
|
39
|
+
|
|
40
|
+
static StatusOr<AlignedBuffer> allocate(size_t nbytes) noexcept;
|
|
41
|
+
static StatusOr<AlignedBuffer> allocate_zeroed(size_t nbytes) noexcept;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// Check whether an ExecuTorch tensor is laid out in standard C-contiguous
|
|
45
|
+
// row-major order (matching logical shape without transposition/gaps).
|
|
46
|
+
bool is_standard_contiguous(const executorch::aten::Tensor& tensor) noexcept;
|
|
47
|
+
|
|
48
|
+
// Copy any ExecuTorch tensor (including non-contiguous / NHWC dim_order tensors)
|
|
49
|
+
// into a flat C-contiguous byte buffer of size `tensor.numel() * element_size`.
|
|
50
|
+
Status copy_to_contiguous_bytes(
|
|
51
|
+
const executorch::aten::Tensor& tensor,
|
|
52
|
+
void* dst,
|
|
53
|
+
size_t dst_nbytes) noexcept;
|
|
54
|
+
|
|
55
|
+
// Clone an ExecuTorch tensor into an owned, C-contiguous TensorPtr.
|
|
56
|
+
StatusOr<executorch::extension::TensorPtr> clone_contiguous_tensor_ptr(
|
|
57
|
+
const executorch::aten::Tensor& tensor) noexcept;
|
|
58
|
+
|
|
59
|
+
// Build an owned C-contiguous TensorPtr taking ownership of `buf`.
|
|
60
|
+
StatusOr<executorch::extension::TensorPtr> make_owned_tensor_ptr(
|
|
61
|
+
std::vector<executorch::aten::SizesType> sizes,
|
|
62
|
+
executorch::aten::ScalarType dtype,
|
|
63
|
+
AlignedBuffer buf) noexcept;
|
|
64
|
+
|
|
65
|
+
// Build an unowned view TensorPtr pointing into external memory (e.g. a pinned
|
|
66
|
+
// Ruby IO::Buffer). Caller guarantees `data` remains valid while the TensorPtr
|
|
67
|
+
// is used.
|
|
68
|
+
StatusOr<executorch::extension::TensorPtr> make_borrowed_tensor_ptr(
|
|
69
|
+
std::vector<executorch::aten::SizesType> sizes,
|
|
70
|
+
executorch::aten::ScalarType dtype,
|
|
71
|
+
void* data) noexcept;
|
|
72
|
+
|
|
73
|
+
} // namespace etrb
|