executorch 1.0.0-aarch64-linux
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 +7 -0
- data/.executorch-version +1 -0
- data/CHANGELOG.md +33 -0
- data/LICENSE.txt +176 -0
- data/README.md +212 -0
- data/lib/executorch/3.2/executorch.so +0 -0
- data/lib/executorch/3.3/executorch.so +0 -0
- data/lib/executorch/3.4/executorch.so +0 -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/vendor/libexecutorch.so +0 -0
- data/lib/executorch/vendor/libexecutorch_backend_xnnpack.so +0 -0
- data/lib/executorch/vendor/libexecutorch_etdump.so +0 -0
- data/lib/executorch/vendor/libexecutorch_kernels_optimized.so +0 -0
- data/lib/executorch/vendor/libexecutorch_kernels_quantized.so +0 -0
- data/lib/executorch/vendor/libexecutorch_kernels_torchao.so +0 -0
- data/lib/executorch/vendor/libexecutorch_threadpool.so +0 -0
- data/lib/executorch/version.rb +5 -0
- data/lib/executorch.rb +16 -0
- metadata +66 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b28ddd6f4b5f73f76c0067281d3e456b951d852474d0066229cdbb46a8247a4e
|
|
4
|
+
data.tar.gz: c876a6cacfdd7d77f43fe59ca62e744145f24fbd02e4c04d9a5aa73085f5c36e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: '028d414c2b5e228068c3c5374512ca39aa4601058cdc4d749586310e022a821c1395e49d48c10b9f4e6e2c56dc77981b32aaa1830658b364dee57af6ae4d57dd'
|
|
7
|
+
data.tar.gz: 637a4785907f80ca8d97a5f0bba185693eea59d5dc6e71a11020e01720ac1b7efcf8ea42cbe45005c75b076ead53e394757a36f006a1b1f3ec78d379b6792343
|
data/.executorch-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1.6.0
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [1.0.0] - 2026-11-19
|
|
9
|
+
|
|
10
|
+
Complete from-scratch rewrite targeting ExecuTorch 1.6.0 and Ruby >= 3.2.
|
|
11
|
+
|
|
12
|
+
### Breaking Changes
|
|
13
|
+
|
|
14
|
+
- Replaced `Executorch::Model` with `Executorch::Module` (`Module.open`, `Module.from_bytes`, `#call`, `#forward`, `#execute`, `#meta`, `#load_method`, `#unload_method`, `#close`).
|
|
15
|
+
- Canonical tensor dtype symbols are now explicit-width (`:float32`, `:float64`, `:float16`, `:bfloat16`, `:uint8`, `:int8`, `:int16`, `:uint16`, `:int32`, `:uint32`, `:int64`, `:uint64`, `:bool`). Short aliases (`:float`, `:double`, `:int`, `:long`, etc.) remain accepted on input.
|
|
16
|
+
- Raised minimum Ruby version to `>= 3.2.0`.
|
|
17
|
+
- Removed `rice` dependency and leaked internal methods (`_original_to_a`, `Tensor.create`, `Tensor.from_array`, `NATIVE_VERSION`, `NativeError`).
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
|
|
21
|
+
- Two-layer `noexcept` C++17 core beneath a direct Ruby C-API seam — eliminates `rb_raise` `longjmp`-past-destructor UB by construction.
|
|
22
|
+
- Strict symbol visibility (`-fvisibility=hidden` + ELF version script / macOS `-exported_symbol,_Init_executorch`) preventing ODR collisions with `torch.rb`, `onnxruntime-ruby`, or `ruby-vips`.
|
|
23
|
+
- GVL release (`rb_thread_call_without_gvl`) during module load, method load, and execution, paired with per-method thread-safety mutexes and `RB_GC_GUARD`.
|
|
24
|
+
- Native memory pressure reporting (`dsize` + `rb_gc_adjust_memory_usage`) and deterministic `#close` / block-form `Module.open`.
|
|
25
|
+
- Support for all 13 numeric/boolean dtypes, 0-D scalar tensors (`Tensor#item`), non-contiguous / `NHWC` `dim_order` normalization, value equality (`Tensor#==`), `Tensor.zeros`/`ones`/`full`, `Tensor.from_bytes`/`#to_bytes`, zero-copy `IO::Buffer` (`Tensor.from_buffer`/`#to_buffer`), pre-allocated `out:` output reuse, and optional `Numo::NArray`/`Torch::Tensor` adapters.
|
|
26
|
+
- `Module.open` defaults to `:mmap` (supporting `Pathname`, all `LoadMode`s, and external `.ptd` `data_files`), plus `Module.from_bytes` (`BufferDataLoader`) and `MethodMeta` / `TensorInfo` introspection.
|
|
27
|
+
- Runtime introspection (`Executorch.backends`, `.kernel_count`, `.num_threads`, `.num_threads=`, `.executorch_version`), PAL initialization (`runtime_init`), and thread-local PAL error log capture attached to typed `Executorch::Error` subclasses (`LoadError`, `MethodNotFoundError`, `OperatorMissingError`, `DelegateError`, `ShapeError`, `DTypeError`, `ExecutionError`, `MemoryError`, `ClosedError`).
|
|
28
|
+
- Precompiled platform gems (`x86_64-linux`, `aarch64-linux`, `arm64-darwin`) bundling the official ExecuTorch 1.6.0 shared SDK (XNNPACK, optimized & quantized kernels, CoreML/MLX on Apple Silicon) plus a pure-stdlib SHA-256-verified SDK fetcher for source installs — no Python, CMake, or Buck2 required at install or inference time.
|
|
29
|
+
- Deterministic `.pte`/`.ptd` test fixture export pipeline (`test/fixtures/export_fixtures.py`) and comprehensive correctness, ASan/UBSan, GC stress, concurrency, and adversarial safety test suites (0 skipped tests).
|
|
30
|
+
|
|
31
|
+
## [0.1.0] - 2025-11-16
|
|
32
|
+
|
|
33
|
+
Initial prototype release.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# executorch-ruby
|
|
2
|
+
|
|
3
|
+
[](https://github.com/benngarcia/executorch-ruby/actions/workflows/ci.yml)
|
|
4
|
+
[](https://rubygems.org/gems/executorch)
|
|
5
|
+
[](LICENSE.txt)
|
|
6
|
+
|
|
7
|
+
Native Ruby bindings for Meta's [ExecuTorch](https://pytorch.org/executorch/) runtime (`1.6.0`), built directly on the Ruby C API.
|
|
8
|
+
|
|
9
|
+
Run exported PyTorch models (`.pte` / `.ptd`) in Ruby with precompiled platform gems, GVL-free execution, zero-copy `IO::Buffer` tensors, and hardware delegates (XNNPACK, CoreML, MLX).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Add to your `Gemfile`:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem "executorch", "~> 1.0"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or install directly:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
gem install executorch
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Requires **Ruby `>= 3.2`**.
|
|
26
|
+
|
|
27
|
+
### Supported platforms
|
|
28
|
+
|
|
29
|
+
| Platform | Precompiled Gem | Bundled Delegates & Kernels |
|
|
30
|
+
|---|---|---|
|
|
31
|
+
| `x86_64-linux` | Yes | XNNPACK, Optimized CPU, Quantized |
|
|
32
|
+
| `aarch64-linux` | Yes | XNNPACK (KleidiAI), Optimized CPU, Quantized, TorchAO |
|
|
33
|
+
| `arm64-darwin` | Yes | XNNPACK, CoreML, MLX, Optimized CPU, Quantized, TorchAO |
|
|
34
|
+
| Source (`ruby`) | Yes | Fetches the official ExecuTorch 1.6.0 C++ SDK (or uses `--with-executorch-dir`) |
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
Export a PyTorch model to `.pte` using ExecuTorch:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import torch
|
|
42
|
+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
|
|
43
|
+
from executorch.exir import to_edge_transform_and_lower
|
|
44
|
+
|
|
45
|
+
class AddMul(torch.nn.Module):
|
|
46
|
+
def forward(self, x):
|
|
47
|
+
return x * 2.0 + 1.0
|
|
48
|
+
|
|
49
|
+
example_input = (torch.tensor([[1.0, 2.0, 3.0]], dtype=torch.float32),)
|
|
50
|
+
exported = torch.export.export(AddMul().eval(), example_input)
|
|
51
|
+
et_program = to_edge_transform_and_lower(
|
|
52
|
+
exported, partitioner=[XnnpackPartitioner()]
|
|
53
|
+
).to_executorch()
|
|
54
|
+
|
|
55
|
+
with open("model.pte", "wb") as f:
|
|
56
|
+
et_program.write_to_file(f)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Load and run it in Ruby:
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
require "executorch"
|
|
63
|
+
|
|
64
|
+
Executorch::Module.open("model.pte") do |mod|
|
|
65
|
+
meta = mod.meta(:forward)
|
|
66
|
+
p meta.backends # => ["XnnpackBackend"]
|
|
67
|
+
p meta.inputs # => [#<Executorch::TensorInfo dtype=:float32 shape=[1, 3] nbytes=12>]
|
|
68
|
+
|
|
69
|
+
input = Executorch::Tensor.new([[1.0, 2.0, 3.0]])
|
|
70
|
+
output = mod.call(input)
|
|
71
|
+
p output.to_a # => [[3.0, 5.0, 7.0]]
|
|
72
|
+
end
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Usage
|
|
76
|
+
|
|
77
|
+
### Modules
|
|
78
|
+
|
|
79
|
+
`Executorch::Module.open` accepts a `String` or `Pathname`, defaults to `:mmap` loading (sharing read-only weight pages across forked Puma/Unicorn workers), and closes deterministically when given a block.
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
# Load modes: :mmap (default), :file, :mmap_mlock, :mmap_mlock_ignore_errors, :mmap_madvise
|
|
83
|
+
mod = Executorch::Module.open(Pathname.new("model.pte"), load_mode: :mmap)
|
|
84
|
+
mod.load_method(:forward) # optional pre-warm before serving requests
|
|
85
|
+
out = mod.call(input)
|
|
86
|
+
mod.close
|
|
87
|
+
|
|
88
|
+
# Multi-method programs
|
|
89
|
+
Executorch::Module.open("autoencoder.pte") do |m|
|
|
90
|
+
z = m.execute(:encode, x).first
|
|
91
|
+
reconstructed = m.execute(:decode, z).first
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# External .ptd weight files
|
|
95
|
+
Executorch::Module.open("model.pte", data_files: ["weights.ptd"]) do |m|
|
|
96
|
+
logits = m.call(tokens)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Load directly from an in-memory String (e.g. S3 or ActiveStorage)
|
|
100
|
+
mod = Executorch::Module.from_bytes(pte_bytes)
|
|
101
|
+
mod.close
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- `#call(*inputs, out: nil)` runs `"forward"` and returns the single output directly when there is one output, or an `Array` otherwise.
|
|
105
|
+
- `#forward(*inputs, out: nil)` and `#execute(name, *inputs, out: nil)` always return an `Array`.
|
|
106
|
+
- `#meta(name = :forward)` returns an `Executorch::MethodMeta` describing input/output shapes, dtypes, planned memory size, and lowered backends without loading the full method.
|
|
107
|
+
|
|
108
|
+
### Tensors
|
|
109
|
+
|
|
110
|
+
Supported dtypes: `:float32`, `:float64`, `:float16`, `:bfloat16`, `:uint8`, `:int8`, `:int16`, `:uint16`, `:int32`, `:uint32`, `:int64`, `:uint64`, and `:bool`. Short aliases (`:float`, `:double`, `:half`, `:byte`, `:char`, `:short`, `:int`, `:long`) are accepted at construction.
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
t = Executorch::Tensor.new([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
|
|
114
|
+
t.shape # => [2, 3]
|
|
115
|
+
t.dtype # => :float32
|
|
116
|
+
t.nbytes # => 24
|
|
117
|
+
t.to_a # => [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
|
|
118
|
+
|
|
119
|
+
# 0-D scalar tensors
|
|
120
|
+
scalar = Executorch::Tensor.new(42.5, dtype: :float32)
|
|
121
|
+
scalar.shape # => []
|
|
122
|
+
scalar.item # => 42.5
|
|
123
|
+
|
|
124
|
+
# Packed binary I/O & factories
|
|
125
|
+
raw = t.to_bytes
|
|
126
|
+
t2 = Executorch::Tensor.from_bytes(raw, shape: [2, 3], dtype: :float32)
|
|
127
|
+
t == t2 # => true
|
|
128
|
+
|
|
129
|
+
zeros = Executorch::Tensor.zeros([2, 3], dtype: :float32)
|
|
130
|
+
ones = Executorch::Tensor.ones([2, 3], dtype: :int64)
|
|
131
|
+
full = Executorch::Tensor.full([2, 3], 7, dtype: :uint8)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Optional adapters are provided for `Numo::NArray` (`Tensor.from_numo` / `Tensor#to_numo`) and `Torch::Tensor` (`Tensor.from_torch`).
|
|
135
|
+
|
|
136
|
+
### Zero-copy `IO::Buffer` and output reuse
|
|
137
|
+
|
|
138
|
+
Pass Ruby `IO::Buffer` instances directly into ExecuTorch without copying or allocating intermediate arrays. Pairing `Tensor.from_buffer` with `out:` writes inference outputs straight into a caller-owned buffer:
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
in_buf = IO::Buffer.for([1.0, 2.0, 3.0].pack("f*"))
|
|
142
|
+
out_buf = IO::Buffer.new(12)
|
|
143
|
+
|
|
144
|
+
input = Executorch::Tensor.from_buffer(in_buf, shape: [1, 3], dtype: :float32)
|
|
145
|
+
output = Executorch::Tensor.from_buffer(out_buf, shape: [1, 3], dtype: :float32)
|
|
146
|
+
|
|
147
|
+
Executorch::Module.open("model.pte") do |mod|
|
|
148
|
+
mod.call(input, out: output)
|
|
149
|
+
p out_buf.get_string.unpack("f*") # => [3.0, 5.0, 7.0]
|
|
150
|
+
end
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Concurrency, runtime introspection & errors
|
|
154
|
+
|
|
155
|
+
```ruby
|
|
156
|
+
Executorch.backends # => ["XnnpackBackend"]
|
|
157
|
+
Executorch.kernel_count # => 263
|
|
158
|
+
Executorch.num_threads # => 24
|
|
159
|
+
Executorch.num_threads = 2 # size the ExecuTorch threadpool for multi-worker servers
|
|
160
|
+
Executorch.executorch_version # => "1.6.0"
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
- **GVL release & thread safety:** Model loading and execution release the Ruby GVL (`rb_thread_call_without_gvl`). Calls on separate `Module` instances run in parallel across cores; concurrent calls to the same method on a shared `Module` serialize safely via a per-method mutex.
|
|
164
|
+
- **Typed errors:** Runtime failures inherit from `Executorch::Error < StandardError` (`LoadError`, `MethodNotFoundError`, `OperatorMissingError`, `DelegateError`, `ShapeError`, `DTypeError`, `ExecutionError`, `MemoryError`, `ClosedError`) and expose `#code` and `#pal_log`.
|
|
165
|
+
|
|
166
|
+
## Architecture
|
|
167
|
+
|
|
168
|
+
The native extension is written directly against the Ruby C API (no binding generator or runtime gem dependencies) in two layers:
|
|
169
|
+
|
|
170
|
+
- **`ext/executorch/core/`** — A `noexcept` C++17 core with zero Ruby headers. Handles ExecuTorch lifecycle, per-method locking, thread-local PAL log capture, overflow-checked shape math, and strided/`NHWC` layout normalization.
|
|
171
|
+
- **`ext/executorch/rb_*.cpp`** — A thin Ruby C-API layer managing `TypedData`, `dsize` + `rb_gc_adjust_memory_usage`, `GC.compact`-safe global roots, `rb_io_buffer_lock` around GVL-free execution, and single-pass C++ nested-array flattening. All C++ stack objects are destroyed before any Ruby exception is raised.
|
|
172
|
+
- **Symbol isolation:** Compiled with `-fvisibility=hidden` and a linker export map so only `Init_executorch` is exported, avoiding symbol collisions with `torch.rb`, `onnxruntime`, or `ruby-vips`.
|
|
173
|
+
|
|
174
|
+
## Custom ExecuTorch builds
|
|
175
|
+
|
|
176
|
+
To compile against a local ExecuTorch installation instead of the default SDK:
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
bundle config set --local build.executorch --with-executorch-dir=/absolute/path/to/executorch
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Or pass `EXECUTORCH_DIR=/absolute/path/to/executorch`. Set `EXECUTORCH_NO_DOWNLOAD=1` to disable automatic SDK fetching.
|
|
183
|
+
|
|
184
|
+
## Changelog
|
|
185
|
+
|
|
186
|
+
See [CHANGELOG.md](CHANGELOG.md) for release notes and migration details.
|
|
187
|
+
|
|
188
|
+
## Development
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
bundle install
|
|
192
|
+
bundle exec rake sdk:fetch # Fetches verified ExecuTorch 1.6.0 C++ SDK into ~/.cache/executorch-ruby/
|
|
193
|
+
bundle exec rake compile # Compiles native C++17 extension
|
|
194
|
+
bundle exec rake fixtures:export # Exports deterministic .pte/.ptd test models via uv
|
|
195
|
+
bundle exec rake test # Runs full test suite
|
|
196
|
+
ruby bench/run_bench.rb # Runs local latency benchmarks
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
To run the suite under AddressSanitizer + UndefinedBehaviorSanitizer on Linux:
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
bundle exec rake clean
|
|
203
|
+
EXECUTORCH_SANITIZE=1 bundle exec rake compile
|
|
204
|
+
LD_PRELOAD="$(g++ -print-file-name=libasan.so):$(g++ -print-file-name=libubsan.so)" \
|
|
205
|
+
ASAN_OPTIONS=detect_leaks=0:use_sigaltstack=0:halt_on_error=1 \
|
|
206
|
+
UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 \
|
|
207
|
+
bundle exec rake test
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
Apache License 2.0. See [LICENSE.txt](LICENSE.txt).
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Executorch
|
|
4
|
+
# Immutable metadata describing a single input or output slot of a method.
|
|
5
|
+
class TensorInfo
|
|
6
|
+
attr_reader :tag, :dtype, :shape, :dim_order, :nbytes
|
|
7
|
+
|
|
8
|
+
def initialize(tag, dtype, shape, dim_order, nbytes, memory_planned)
|
|
9
|
+
@tag = tag
|
|
10
|
+
@dtype = dtype
|
|
11
|
+
@shape = shape&.freeze
|
|
12
|
+
@dim_order = dim_order&.freeze
|
|
13
|
+
@nbytes = nbytes
|
|
14
|
+
@memory_planned = !!memory_planned
|
|
15
|
+
freeze
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def tensor?
|
|
19
|
+
@tag == :tensor
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def memory_planned?
|
|
23
|
+
@memory_planned
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def ==(other)
|
|
27
|
+
other.is_a?(TensorInfo) &&
|
|
28
|
+
@tag == other.tag &&
|
|
29
|
+
@dtype == other.dtype &&
|
|
30
|
+
@shape == other.shape &&
|
|
31
|
+
@dim_order == other.dim_order &&
|
|
32
|
+
@nbytes == other.nbytes &&
|
|
33
|
+
@memory_planned == other.memory_planned?
|
|
34
|
+
end
|
|
35
|
+
alias eql? ==
|
|
36
|
+
|
|
37
|
+
def inspect
|
|
38
|
+
if tensor?
|
|
39
|
+
"#<Executorch::TensorInfo dtype=#{@dtype.inspect} shape=#{@shape.inspect} nbytes=#{@nbytes}>"
|
|
40
|
+
else
|
|
41
|
+
"#<Executorch::TensorInfo tag=#{@tag.inspect}>"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
alias to_s inspect
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Immutable metadata snapshot for an ExecuTorch method, readable without
|
|
48
|
+
# paying the memory-planning cost of loading the full method.
|
|
49
|
+
class MethodMeta
|
|
50
|
+
attr_reader :name, :inputs, :outputs, :backends, :planned_memory_bytes
|
|
51
|
+
|
|
52
|
+
def initialize(name, inputs, outputs, backends, planned_memory_bytes)
|
|
53
|
+
@name = name.dup.freeze
|
|
54
|
+
@inputs = inputs.freeze
|
|
55
|
+
@outputs = outputs.freeze
|
|
56
|
+
@backends = backends.freeze
|
|
57
|
+
@planned_memory_bytes = planned_memory_bytes
|
|
58
|
+
freeze
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def uses_backend?(backend_name)
|
|
62
|
+
@backends.include?(backend_name.to_s)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def inspect
|
|
66
|
+
"#<Executorch::MethodMeta name=#{@name.inspect} inputs=#{@inputs.size} outputs=#{@outputs.size} backends=#{@backends.inspect} planned_memory_bytes=#{@planned_memory_bytes}>"
|
|
67
|
+
end
|
|
68
|
+
alias to_s inspect
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Executorch
|
|
4
|
+
class Module
|
|
5
|
+
class << self
|
|
6
|
+
# Open an ExecuTorch .pte program from disk.
|
|
7
|
+
# Defaults to :mmap for copy-on-write page sharing across forked workers.
|
|
8
|
+
# Supports an optional block for deterministic resource cleanup.
|
|
9
|
+
#
|
|
10
|
+
# @param path [String, Pathname] Path to .pte file
|
|
11
|
+
# @param load_mode [Symbol] :mmap (default), :file, :mmap_mlock,
|
|
12
|
+
# :mmap_mlock_ignore_errors, or :mmap_madvise
|
|
13
|
+
# @param data_files [Array<String, Pathname>, nil] Optional .ptd external weight files
|
|
14
|
+
def open(path, load_mode: :mmap, data_files: nil)
|
|
15
|
+
mod = _open_file(path, load_mode: load_mode, data_files: data_files)
|
|
16
|
+
return mod unless block_given?
|
|
17
|
+
|
|
18
|
+
begin
|
|
19
|
+
yield mod
|
|
20
|
+
ensure
|
|
21
|
+
mod.close
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
alias load open
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Execute a named method and always return an Array of outputs.
|
|
28
|
+
#
|
|
29
|
+
# @param method_name [String, Symbol] Method name (e.g. :forward, :encode)
|
|
30
|
+
# @param inputs [Array<Tensor, Integer, Float, Boolean, String, nil>]
|
|
31
|
+
# @param out [Tensor, Array<Tensor>, nil] Optional pre-allocated output tensor(s)
|
|
32
|
+
# @return [Array]
|
|
33
|
+
def execute(method_name, *inputs, out: nil)
|
|
34
|
+
_execute(method_name, inputs, out)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Execute the "forward" method and always return an Array of outputs.
|
|
38
|
+
def forward(*inputs, out: nil)
|
|
39
|
+
_execute("forward", inputs, out)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Ergonomic invocation of "forward": returns the single output directly when
|
|
43
|
+
# the method has 1 output, or an Array when it has 0 or >1 outputs.
|
|
44
|
+
def call(*inputs, out: nil)
|
|
45
|
+
outputs = _execute("forward", inputs, out)
|
|
46
|
+
outputs.size == 1 ? outputs.first : outputs
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def inspect
|
|
50
|
+
return "#<Executorch::Module (closed)>" if closed?
|
|
51
|
+
|
|
52
|
+
"#<Executorch::Module source=#{source.inspect} methods=#{method_names.inspect}>"
|
|
53
|
+
end
|
|
54
|
+
alias to_s inspect
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "net/http"
|
|
6
|
+
require "rbconfig"
|
|
7
|
+
require "tmpdir"
|
|
8
|
+
require "uri"
|
|
9
|
+
require "zlib"
|
|
10
|
+
|
|
11
|
+
module Executorch
|
|
12
|
+
# Fetches and extracts the official prebuilt ExecuTorch C++ SDK (headers,
|
|
13
|
+
# shared runtime/backend/kernel libraries, pkg-config, and CMake configs)
|
|
14
|
+
# from official PyPI wheels with strict SHA-256 verification and zero
|
|
15
|
+
# non-stdlib dependencies.
|
|
16
|
+
module SDK
|
|
17
|
+
VERSION = File.read(File.expand_path("../../.executorch-version", __dir__)).strip.freeze
|
|
18
|
+
|
|
19
|
+
# Official ExecuTorch 1.6.0 PyPI wheel manifest (only include/, lib/, share/cmake/ are extracted).
|
|
20
|
+
MANIFEST = {
|
|
21
|
+
"x86_64-linux" => {
|
|
22
|
+
url: "https://files.pythonhosted.org/packages/ef/24/f8fde978e1e5fd629d68c74a3f4acee30acb05f4f90c433cc1a8e29d8eed/executorch-1.6.0-cp312-cp312-manylinux_2_28_x86_64.whl",
|
|
23
|
+
sha256: "d05efe9afe00495112ecf304ca548fcd3239d39e96ac25381b528cdbad948b54",
|
|
24
|
+
},
|
|
25
|
+
"aarch64-linux" => {
|
|
26
|
+
url: "https://files.pythonhosted.org/packages/d5/7f/9028996ec1dead9c57ed2c6384037e3567c2f514e4ed0c54480d2d3eeb48/executorch-1.6.0-cp312-cp312-manylinux_2_28_aarch64.whl",
|
|
27
|
+
sha256: "ff595d236f4052a196a03a867ca0f53d2e3b003d00ab871433660360e194e78e",
|
|
28
|
+
},
|
|
29
|
+
"arm64-darwin" => {
|
|
30
|
+
url: "https://files.pythonhosted.org/packages/c6/47/1e2120ca1ee178e81471d57255ea0f87b6e04272e32ba20e5ed60ebdb46d/executorch-1.6.0-cp312-cp312-macosx_14_0_arm64.whl",
|
|
31
|
+
sha256: "02c220037b41a0a6074a94499a747400ef1a5075a3f2b4d83911871c7291a341",
|
|
32
|
+
},
|
|
33
|
+
}.freeze
|
|
34
|
+
|
|
35
|
+
# Core runtime + CPU/hardware delegate shared libs bundled in fat gems.
|
|
36
|
+
VENDOR_LIBS = {
|
|
37
|
+
"x86_64-linux" => %w[
|
|
38
|
+
libexecutorch.so
|
|
39
|
+
libexecutorch_threadpool.so
|
|
40
|
+
libexecutorch_kernels_optimized.so
|
|
41
|
+
libexecutorch_kernels_quantized.so
|
|
42
|
+
libexecutorch_backend_xnnpack.so
|
|
43
|
+
libexecutorch_etdump.so
|
|
44
|
+
],
|
|
45
|
+
"aarch64-linux" => %w[
|
|
46
|
+
libexecutorch.so
|
|
47
|
+
libexecutorch_threadpool.so
|
|
48
|
+
libexecutorch_kernels_optimized.so
|
|
49
|
+
libexecutorch_kernels_quantized.so
|
|
50
|
+
libexecutorch_kernels_torchao.so
|
|
51
|
+
libexecutorch_backend_xnnpack.so
|
|
52
|
+
libexecutorch_etdump.so
|
|
53
|
+
],
|
|
54
|
+
"arm64-darwin" => %w[
|
|
55
|
+
libexecutorch.dylib
|
|
56
|
+
libexecutorch_threadpool.dylib
|
|
57
|
+
libexecutorch_kernels_optimized.dylib
|
|
58
|
+
libexecutorch_kernels_quantized.dylib
|
|
59
|
+
libexecutorch_kernels_torchao.dylib
|
|
60
|
+
libexecutorch_backend_xnnpack.dylib
|
|
61
|
+
libexecutorch_backend_coreml.dylib
|
|
62
|
+
libexecutorch_backend_mlx.dylib
|
|
63
|
+
libexecutorch_etdump.dylib
|
|
64
|
+
mlx.metallib
|
|
65
|
+
],
|
|
66
|
+
}.freeze
|
|
67
|
+
|
|
68
|
+
module_function
|
|
69
|
+
|
|
70
|
+
def platform_key(os: RbConfig::CONFIG["target_os"], cpu: RbConfig::CONFIG["target_cpu"])
|
|
71
|
+
norm_cpu = case cpu
|
|
72
|
+
when /x86_64|amd64/i then "x86_64"
|
|
73
|
+
when /aarch64|arm64/i then os =~ /darwin/i ? "arm64" : "aarch64"
|
|
74
|
+
else cpu
|
|
75
|
+
end
|
|
76
|
+
norm_os = case os
|
|
77
|
+
when /darwin/i then "darwin"
|
|
78
|
+
when /linux/i then "linux"
|
|
79
|
+
else os
|
|
80
|
+
end
|
|
81
|
+
"#{norm_cpu}-#{norm_os}"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def default_cache_dir(key = platform_key)
|
|
85
|
+
base = ENV["XDG_CACHE_HOME"]
|
|
86
|
+
base = File.join(Dir.home, ".cache") if base.nil? || base.empty?
|
|
87
|
+
File.join(base, "executorch-ruby", "sdk-#{VERSION}-#{key}")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def valid_sdk?(dir)
|
|
91
|
+
return false unless dir && File.directory?(dir)
|
|
92
|
+
|
|
93
|
+
File.exist?(File.join(dir, "include", "executorch", "extension", "module", "module.h")) &&
|
|
94
|
+
File.directory?(File.join(dir, "lib"))
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def ensure_sdk(dest_dir: nil, key: platform_key, io: $stdout)
|
|
98
|
+
dest = dest_dir || default_cache_dir(key)
|
|
99
|
+
return dest if valid_sdk?(dest)
|
|
100
|
+
|
|
101
|
+
entry = MANIFEST[key]
|
|
102
|
+
raise "No prebuilt ExecuTorch #{VERSION} SDK manifest for platform '#{key}'. Set --with-executorch-dir or EXECUTORCH_DIR." unless entry
|
|
103
|
+
|
|
104
|
+
io&.puts "Fetching official ExecuTorch #{VERSION} C++ SDK for #{key}..."
|
|
105
|
+
Dir.mktmpdir("executorch-sdk") do |tmp|
|
|
106
|
+
whl_path = File.join(tmp, "executorch.whl")
|
|
107
|
+
download_with_redirects(entry[:url], whl_path)
|
|
108
|
+
actual_sha = Digest::SHA256.file(whl_path).hexdigest
|
|
109
|
+
unless actual_sha == entry[:sha256]
|
|
110
|
+
raise "SHA-256 mismatch for ExecuTorch SDK (#{key}): expected #{entry[:sha256]}, got #{actual_sha}"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
stage = File.join(tmp, "stage")
|
|
114
|
+
extract_sdk_from_wheel(whl_path, stage)
|
|
115
|
+
FileUtils.mkdir_p(File.dirname(dest))
|
|
116
|
+
FileUtils.rm_rf(dest)
|
|
117
|
+
FileUtils.mv(stage, dest)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
raise "Extracted SDK at #{dest} is incomplete" unless valid_sdk?(dest)
|
|
121
|
+
|
|
122
|
+
io&.puts "Installed ExecuTorch #{VERSION} C++ SDK to #{dest}"
|
|
123
|
+
dest
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def download_with_redirects(url, dest, limit = 5)
|
|
127
|
+
raise "Too many HTTP redirects downloading #{url}" if limit <= 0
|
|
128
|
+
|
|
129
|
+
uri = URI.parse(url)
|
|
130
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: 30, read_timeout: 120) do |http|
|
|
131
|
+
req = Net::HTTP::Get.new(uri)
|
|
132
|
+
http.request(req) do |res|
|
|
133
|
+
case res
|
|
134
|
+
when Net::HTTPSuccess
|
|
135
|
+
File.open(dest, "wb") { |f| res.read_body { |chunk| f.write(chunk) } }
|
|
136
|
+
when Net::HTTPRedirection
|
|
137
|
+
download_with_redirects(res["location"], dest, limit - 1)
|
|
138
|
+
else
|
|
139
|
+
raise "Failed downloading #{url}: HTTP #{res.code} #{res.message}"
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Minimal pure-Ruby ZIP reader (central directory) extracting ONLY
|
|
146
|
+
# executorch/{include,lib,share/cmake}/ from the wheel.
|
|
147
|
+
def extract_sdk_from_wheel(whl_path, dest_dir)
|
|
148
|
+
File.open(whl_path, "rb") do |f|
|
|
149
|
+
size = f.size
|
|
150
|
+
search_len = [size, 65_557].min
|
|
151
|
+
f.seek(size - search_len)
|
|
152
|
+
tail = f.read(search_len)
|
|
153
|
+
eocd_idx = tail.rindex("PK\x05\x06")
|
|
154
|
+
raise "Invalid wheel archive (missing EOCD)" unless eocd_idx
|
|
155
|
+
|
|
156
|
+
_sig, _disk, _cd_disk, _entries_disk, total_entries, cd_size, cd_offset =
|
|
157
|
+
tail.byteslice(eocd_idx, 22).unpack("a4vvvvVV")
|
|
158
|
+
|
|
159
|
+
# Handle ZIP64 if needed
|
|
160
|
+
if total_entries == 0xFFFF || cd_offset == 0xFFFFFFFF
|
|
161
|
+
loc_idx = tail.rindex("PK\x06\x07", eocd_idx)
|
|
162
|
+
raise "ZIP64 locator missing" unless loc_idx
|
|
163
|
+
|
|
164
|
+
zip64_offset = tail.byteslice(loc_idx + 8, 8).unpack1("Q<")
|
|
165
|
+
f.seek(zip64_offset)
|
|
166
|
+
z64 = f.read(56)
|
|
167
|
+
total_entries = z64.byteslice(32, 8).unpack1("Q<")
|
|
168
|
+
cd_size = z64.byteslice(40, 8).unpack1("Q<")
|
|
169
|
+
cd_offset = z64.byteslice(48, 8).unpack1("Q<")
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
f.seek(cd_offset)
|
|
173
|
+
cd = f.read(cd_size)
|
|
174
|
+
pos = 0
|
|
175
|
+
|
|
176
|
+
total_entries.times do
|
|
177
|
+
break if pos + 46 > cd.bytesize
|
|
178
|
+
|
|
179
|
+
sig = cd.byteslice(pos, 4)
|
|
180
|
+
raise "Invalid central directory header" unless sig == "PK\x01\x02"
|
|
181
|
+
|
|
182
|
+
method, _time, _date, _crc, comp_size, uncomp_size, name_len, extra_len, comment_len,
|
|
183
|
+
_disk_start, _int_attr, _ext_attr, local_offset =
|
|
184
|
+
cd.byteslice(pos + 10, 36).unpack("vvvVVVvvvvvVV")
|
|
185
|
+
|
|
186
|
+
name = cd.byteslice(pos + 46, name_len)
|
|
187
|
+
extra = cd.byteslice(pos + 46 + name_len, extra_len)
|
|
188
|
+
pos += 46 + name_len + extra_len + comment_len
|
|
189
|
+
|
|
190
|
+
next unless name.start_with?("executorch/include/", "executorch/lib/", "executorch/share/cmake/")
|
|
191
|
+
next if name.end_with?("/")
|
|
192
|
+
|
|
193
|
+
# Resolve ZIP64 extra fields if 32-bit fields overflowed
|
|
194
|
+
if comp_size == 0xFFFFFFFF || uncomp_size == 0xFFFFFFFF || local_offset == 0xFFFFFFFF
|
|
195
|
+
ep = 0
|
|
196
|
+
while ep + 4 <= extra.bytesize
|
|
197
|
+
eid, esz = extra.byteslice(ep, 4).unpack("vv")
|
|
198
|
+
if eid == 0x0001
|
|
199
|
+
ef = extra.byteslice(ep + 4, esz)
|
|
200
|
+
off = 0
|
|
201
|
+
if uncomp_size == 0xFFFFFFFF
|
|
202
|
+
uncomp_size = ef.byteslice(off, 8).unpack1("Q<")
|
|
203
|
+
off += 8
|
|
204
|
+
end
|
|
205
|
+
if comp_size == 0xFFFFFFFF
|
|
206
|
+
comp_size = ef.byteslice(off, 8).unpack1("Q<")
|
|
207
|
+
off += 8
|
|
208
|
+
end
|
|
209
|
+
if local_offset == 0xFFFFFFFF
|
|
210
|
+
local_offset = ef.byteslice(off, 8).unpack1("Q<")
|
|
211
|
+
end
|
|
212
|
+
break
|
|
213
|
+
end
|
|
214
|
+
ep += 4 + esz
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
rel = name.delete_prefix("executorch/")
|
|
219
|
+
# Prevent path traversal
|
|
220
|
+
raise "Unsafe archive path: #{rel}" if rel.include?("..") || rel.start_with?("/")
|
|
221
|
+
|
|
222
|
+
out_path = File.join(dest_dir, rel)
|
|
223
|
+
FileUtils.mkdir_p(File.dirname(out_path))
|
|
224
|
+
|
|
225
|
+
f.seek(local_offset)
|
|
226
|
+
lh = f.read(30)
|
|
227
|
+
raise "Invalid local file header for #{name}" unless lh&.start_with?("PK\x03\x04")
|
|
228
|
+
|
|
229
|
+
l_name_len, l_extra_len = lh.byteslice(26, 4).unpack("vv")
|
|
230
|
+
f.seek(local_offset + 30 + l_name_len + l_extra_len)
|
|
231
|
+
raw = f.read(comp_size)
|
|
232
|
+
|
|
233
|
+
data = case method
|
|
234
|
+
when 0 then raw
|
|
235
|
+
when 8
|
|
236
|
+
z = Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
|
237
|
+
begin
|
|
238
|
+
z.inflate(raw)
|
|
239
|
+
ensure
|
|
240
|
+
z.close
|
|
241
|
+
end
|
|
242
|
+
else
|
|
243
|
+
raise "Unsupported ZIP compression method #{method} for #{name}"
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
File.binwrite(out_path, data)
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Executorch
|
|
4
|
+
class Tensor
|
|
5
|
+
class << self
|
|
6
|
+
# Create a tensor filled with ones.
|
|
7
|
+
def ones(shape, dtype: :float32)
|
|
8
|
+
full(shape, 1, dtype: dtype)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Create a tensor filled with a constant scalar value.
|
|
12
|
+
def full(shape, value, dtype: :float32)
|
|
13
|
+
raise TypeError, "shape must be an Array" unless shape.is_a?(Array)
|
|
14
|
+
|
|
15
|
+
numel = shape.empty? ? 1 : shape.reduce(1, :*)
|
|
16
|
+
raise ShapeError, "shape dimensions must be non-negative" if shape.any? { |d| !d.is_a?(Integer) || d < 0 }
|
|
17
|
+
|
|
18
|
+
new(Array.new(numel, value), shape: shape, dtype: dtype)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Build a Tensor from a Numo::NArray (zero-copy byte path).
|
|
22
|
+
def from_numo(narray)
|
|
23
|
+
dtype = NUMO_TO_DTYPE[narray.class.name]
|
|
24
|
+
raise DTypeError, "Unsupported Numo::NArray type: #{narray.class.name}" unless dtype
|
|
25
|
+
|
|
26
|
+
from_bytes(narray.to_binary, shape: narray.shape, dtype: dtype)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Build a Tensor from a Torch::Tensor (torch.rb interop).
|
|
30
|
+
def from_torch(torch_tensor)
|
|
31
|
+
dtype = TORCH_TO_DTYPE[torch_tensor.dtype]
|
|
32
|
+
raise DTypeError, "Unsupported Torch::Tensor dtype: #{torch_tensor.dtype.inspect}" unless dtype
|
|
33
|
+
|
|
34
|
+
new(torch_tensor.contiguous.to_a, shape: torch_tensor.shape, dtype: dtype)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
NUMO_TO_DTYPE = {
|
|
39
|
+
"Numo::SFloat" => :float32,
|
|
40
|
+
"Numo::DFloat" => :float64,
|
|
41
|
+
"Numo::Int8" => :int8,
|
|
42
|
+
"Numo::Int16" => :int16,
|
|
43
|
+
"Numo::Int32" => :int32,
|
|
44
|
+
"Numo::Int64" => :int64,
|
|
45
|
+
"Numo::UInt8" => :uint8,
|
|
46
|
+
"Numo::UInt16" => :uint16,
|
|
47
|
+
"Numo::UInt32" => :uint32,
|
|
48
|
+
"Numo::UInt64" => :uint64,
|
|
49
|
+
}.freeze
|
|
50
|
+
|
|
51
|
+
TORCH_TO_DTYPE = {
|
|
52
|
+
float32: :float32,
|
|
53
|
+
float64: :float64,
|
|
54
|
+
float16: :float16,
|
|
55
|
+
bfloat16: :bfloat16,
|
|
56
|
+
int8: :int8,
|
|
57
|
+
int16: :int16,
|
|
58
|
+
int32: :int32,
|
|
59
|
+
int64: :int64,
|
|
60
|
+
uint8: :uint8,
|
|
61
|
+
bool: :bool,
|
|
62
|
+
}.freeze
|
|
63
|
+
|
|
64
|
+
def empty?
|
|
65
|
+
numel == 0
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def to_numo
|
|
69
|
+
raise LoadError, "Numo::NArray is not loaded (require 'numo/narray')" unless defined?(::Numo::NArray)
|
|
70
|
+
|
|
71
|
+
klass_name = NUMO_TO_DTYPE.key(dtype)
|
|
72
|
+
raise DTypeError, "No Numo::NArray equivalent for dtype #{dtype.inspect}" unless klass_name
|
|
73
|
+
|
|
74
|
+
klass = Object.const_get(klass_name)
|
|
75
|
+
klass.from_binary(to_bytes, shape)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def inspect
|
|
79
|
+
return "#<Executorch::Tensor (closed)>" if closed?
|
|
80
|
+
|
|
81
|
+
"#<Executorch::Tensor shape=#{shape.inspect} dtype=#{dtype.inspect} nbytes=#{nbytes}>"
|
|
82
|
+
end
|
|
83
|
+
alias to_s inspect
|
|
84
|
+
end
|
|
85
|
+
end
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
data/lib/executorch.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "executorch/version"
|
|
4
|
+
require_relative "executorch/method_meta"
|
|
5
|
+
|
|
6
|
+
# Load precompiled Ruby-version-specific native extension first (fat gems),
|
|
7
|
+
# falling back to standard single-version native extension path.
|
|
8
|
+
begin
|
|
9
|
+
ruby_abi = RUBY_VERSION[/\A\d+\.\d+/]
|
|
10
|
+
require_relative "executorch/#{ruby_abi}/executorch"
|
|
11
|
+
rescue LoadError
|
|
12
|
+
require_relative "executorch/executorch"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
require_relative "executorch/tensor"
|
|
16
|
+
require_relative "executorch/module"
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: executorch
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: aarch64-linux
|
|
6
|
+
authors:
|
|
7
|
+
- Benjamin Garcia
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Run PyTorch models exported with ExecuTorch (.pte / .ptd) natively in
|
|
13
|
+
Ruby with XNNPACK/CoreML acceleration, GVL-free execution, and zero-copy IO::Buffer
|
|
14
|
+
tensors.
|
|
15
|
+
email:
|
|
16
|
+
- hey@bengarcia.dev
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- ".executorch-version"
|
|
22
|
+
- CHANGELOG.md
|
|
23
|
+
- LICENSE.txt
|
|
24
|
+
- README.md
|
|
25
|
+
- lib/executorch.rb
|
|
26
|
+
- lib/executorch/3.2/executorch.so
|
|
27
|
+
- lib/executorch/3.3/executorch.so
|
|
28
|
+
- lib/executorch/3.4/executorch.so
|
|
29
|
+
- lib/executorch/method_meta.rb
|
|
30
|
+
- lib/executorch/module.rb
|
|
31
|
+
- lib/executorch/sdk.rb
|
|
32
|
+
- lib/executorch/tensor.rb
|
|
33
|
+
- lib/executorch/vendor/libexecutorch.so
|
|
34
|
+
- lib/executorch/vendor/libexecutorch_backend_xnnpack.so
|
|
35
|
+
- lib/executorch/vendor/libexecutorch_etdump.so
|
|
36
|
+
- lib/executorch/vendor/libexecutorch_kernels_optimized.so
|
|
37
|
+
- lib/executorch/vendor/libexecutorch_kernels_quantized.so
|
|
38
|
+
- lib/executorch/vendor/libexecutorch_kernels_torchao.so
|
|
39
|
+
- lib/executorch/vendor/libexecutorch_threadpool.so
|
|
40
|
+
- lib/executorch/version.rb
|
|
41
|
+
homepage: https://github.com/benngarcia/executorch-ruby
|
|
42
|
+
licenses:
|
|
43
|
+
- Apache-2.0
|
|
44
|
+
metadata:
|
|
45
|
+
source_code_uri: https://github.com/benngarcia/executorch-ruby
|
|
46
|
+
changelog_uri: https://github.com/benngarcia/executorch-ruby/blob/master/CHANGELOG.md
|
|
47
|
+
bug_tracker_uri: https://github.com/benngarcia/executorch-ruby/issues
|
|
48
|
+
rubygems_mfa_required: 'true'
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: 3.2.0
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
requirements: []
|
|
63
|
+
rubygems_version: 3.6.9
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: Native Ruby bindings for Meta's ExecuTorch runtime
|
|
66
|
+
test_files: []
|