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.
@@ -1,208 +1,289 @@
1
- require 'mkmf-rice'
2
-
3
- $CXXFLAGS += ' -std=c++17'
4
- $CXXFLAGS += ' -DC10_USING_CUSTOM_GENERATED_MACROS'
5
- $CXXFLAGS += ' -Wno-deprecated-declarations'
6
-
7
- # ==============================================================================
8
- # ExecuTorch Path Detection
9
- # ==============================================================================
10
- #
11
- # 1. --with-executorch-dir flag (bundle config or gem install)
12
- # 2. EXECUTORCH_DIR environment variable
13
- #
14
-
15
- include_dirs = []
16
- lib_dirs = []
17
-
18
- # Helper to add prefix paths
19
- def add_prefix_paths(prefix, include_dirs, lib_dirs)
20
- return false unless prefix && File.directory?(prefix)
21
-
22
- inc = File.join(prefix, 'include')
23
- lib = File.join(prefix, 'lib')
24
-
25
- if File.directory?(inc) && File.directory?(lib)
26
- include_dirs << inc
27
- lib_dirs << lib
28
- true
29
- else
30
- false
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "mkmf"
5
+ require "rbconfig"
6
+ require "shellwords"
7
+ require_relative "../../lib/executorch/sdk"
8
+
9
+ target_os = RbConfig::CONFIG["target_os"].to_s
10
+ is_darwin = target_os.match?(/darwin/i)
11
+ is_linux = target_os.match?(/linux/i)
12
+
13
+ if target_os.match?(/mswin|mingw|cygwin/i)
14
+ abort "executorch-ruby does not support Windows native builds yet."
15
+ end
16
+
17
+ # ----------------------------------------------------------------------------
18
+ # 1. Locate ExecuTorch C++ SDK
19
+ # ----------------------------------------------------------------------------
20
+
21
+ def resolve_dir(raw)
22
+ return nil if raw.nil? || raw.empty?
23
+
24
+ expanded = File.expand_path(raw)
25
+ return expanded if Executorch::SDK.valid_sdk?(expanded)
26
+
27
+ # Bundler runs extconf.rb inside ext/executorch/; also resolve relative paths
28
+ # against the original invocation PWD if set.
29
+ if !Pathname.new(raw).absolute? && ENV["PWD"]
30
+ from_pwd = File.expand_path(raw, ENV["PWD"])
31
+ return from_pwd if Executorch::SDK.valid_sdk?(from_pwd)
31
32
  end
33
+
34
+ nil
32
35
  end
33
36
 
34
- # Priority 1: --with-executorch-dir flag (standard Ruby gem pattern)
35
- # Usage: bundle config set --local build.executorch --with-executorch-dir=/path/to/executorch
36
- # or: gem install executorch -- --with-executorch-dir=/path/to/executorch
37
- executorch_dir = arg_config('--with-executorch-dir')
38
- if executorch_dir
39
- if add_prefix_paths(executorch_dir, include_dirs, lib_dirs)
40
- puts "Using --with-executorch-dir: #{executorch_dir}"
41
- else
42
- abort "Error: --with-executorch-dir path is invalid: #{executorch_dir}"
37
+ require "pathname"
38
+
39
+ sdk_dir = nil
40
+
41
+ if (arg = arg_config("--with-executorch-dir"))
42
+ sdk_dir = resolve_dir(arg)
43
+ unless sdk_dir
44
+ abort <<~ERR
45
+ Invalid --with-executorch-dir: #{arg}
46
+ Expected include/executorch/extension/module/module.h and lib/ inside that directory.
47
+ Tip: use an absolute path (Bundler runs extconf.rb inside ext/executorch/).
48
+ ERR
43
49
  end
50
+ puts "Using --with-executorch-dir: #{sdk_dir}"
44
51
  end
45
52
 
46
- # Priority 2: Environment variable (for CI/scripting)
47
- if include_dirs.empty?
48
- executorch_prefix = ENV['EXECUTORCH_DIR']
49
- if add_prefix_paths(executorch_prefix, include_dirs, lib_dirs)
50
- puts "Using EXECUTORCH_DIR: #{executorch_prefix}"
53
+ if sdk_dir.nil? && ENV["EXECUTORCH_DIR"] && !ENV["EXECUTORCH_DIR"].empty?
54
+ sdk_dir = resolve_dir(ENV["EXECUTORCH_DIR"])
55
+ unless sdk_dir
56
+ abort "Invalid EXECUTORCH_DIR: #{ENV['EXECUTORCH_DIR']}"
51
57
  end
58
+ puts "Using EXECUTORCH_DIR: #{sdk_dir}"
52
59
  end
53
60
 
54
- include_dirs.compact!
55
- include_dirs.uniq!
56
- lib_dirs.compact!
57
- lib_dirs.uniq!
61
+ if sdk_dir.nil?
62
+ # Check cached official SDK first, then standard system prefixes.
63
+ candidates = [
64
+ Executorch::SDK.default_cache_dir,
65
+ "/opt/homebrew",
66
+ "/usr/local",
67
+ ]
68
+ sdk_dir = candidates.find { |c| Executorch::SDK.valid_sdk?(c) }
69
+ end
58
70
 
59
- if include_dirs.empty? || lib_dirs.empty?
60
- abort <<~ERROR
61
- ExecuTorch installation not found!
71
+ if sdk_dir.nil? && ENV["EXECUTORCH_NO_DOWNLOAD"] != "1"
72
+ sdk_dir = Executorch::SDK.ensure_sdk
73
+ end
62
74
 
63
- Configure the path using one of these methods:
75
+ unless Executorch::SDK.valid_sdk?(sdk_dir)
76
+ abort <<~ERR
77
+ ExecuTorch C++ SDK not found!
78
+ Provide --with-executorch-dir=/abs/path/to/executorch or EXECUTORCH_DIR=/abs/path,
79
+ or unset EXECUTORCH_NO_DOWNLOAD to fetch the verified ExecuTorch #{Executorch::SDK::VERSION} SDK.
80
+ ERR
81
+ end
82
+
83
+ include_dir = File.join(sdk_dir, "include")
84
+ lib_dir = File.join(sdk_dir, "lib")
85
+ c10_inc = File.join(include_dir, "executorch", "runtime", "core", "portable_type", "c10")
86
+
87
+ # Read SDK version from executorch.pc if present, else fall back to .executorch-version.
88
+ sdk_version = Executorch::SDK::VERSION
89
+ pc_file = File.join(lib_dir, "pkgconfig", "executorch.pc")
90
+ pc_cflags = []
91
+ if File.exist?(pc_file)
92
+ File.readlines(pc_file).each do |line|
93
+ if line =~ /^Version:\s*(\S+)/
94
+ sdk_version = Regexp.last_match(1)
95
+ elsif line =~ /^Cflags:\s*(.+)/
96
+ Regexp.last_match(1).split.each do |tok|
97
+ pc_cflags << tok if tok.start_with?("-D")
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ puts "ExecuTorch SDK: #{sdk_dir} (version #{sdk_version})"
64
104
 
65
- 1. Bundle config (recommended - set once per project):
66
- bundle config set --local build.executorch --with-executorch-dir=/path/to/executorch
105
+ # ----------------------------------------------------------------------------
106
+ # 2. Compiler & Symbol Visibility Flags
107
+ # ----------------------------------------------------------------------------
67
108
 
68
- 2. Environment variable (useful for CI):
69
- EXECUTORCH_DIR=/path/to/executorch bundle install
109
+ $CXXFLAGS = "#{CONFIG['CXXFLAGS']} -std=c++17 -fvisibility=hidden -fvisibility-inlines-hidden"
110
+ $CXXFLAGS += " -Wall -Wextra -Wno-unused-parameter -Wno-deprecated-declarations"
111
+ $CXXFLAGS += " -DEXECUTORCH_RUBY_SDK_VERSION=\\\"#{sdk_version}\\\""
70
112
 
71
- Need to build ExecuTorch first? See: https://pytorch.org/executorch/
72
- ERROR
113
+ if pc_cflags.any?
114
+ pc_cflags.each { |flag| $CXXFLAGS += " #{flag}" }
115
+ else
116
+ $CXXFLAGS += " -DC10_USING_CUSTOM_GENERATED_MACROS"
73
117
  end
74
118
 
75
- # Validate headers exist
76
- include_dir = include_dirs.first
77
- lib_dir = lib_dirs.first
119
+ if ENV["EXECUTORCH_SANITIZE"] == "1"
120
+ puts "Enabling AddressSanitizer + UndefinedBehaviorSanitizer"
121
+ $CXXFLAGS += " -fsanitize=address,undefined -fno-omit-frame-pointer -O1 -g"
122
+ $LDFLAGS += " -fsanitize=address,undefined"
123
+ end
78
124
 
79
- unless File.exist?(File.join(include_dir, 'executorch', 'extension', 'module', 'module.h'))
80
- abort <<~ERROR
81
- ExecuTorch module.h header not found at: #{include_dir}
82
- Make sure EXECUTORCH_BUILD_EXTENSION_MODULE=ON was set during build.
83
- ERROR
125
+ $INCFLAGS = "-I#{Shellwords.escape(include_dir)} #{$INCFLAGS}"
126
+ if File.directory?(c10_inc)
127
+ $INCFLAGS = "-I#{Shellwords.escape(c10_inc)} #{$INCFLAGS}"
84
128
  end
129
+ $INCFLAGS = "-I#{Shellwords.escape(File.join(__dir__, 'core'))} #{$INCFLAGS}"
85
130
 
86
- puts "Include directory: #{include_dir}"
87
- puts "Library directory: #{lib_dir}"
88
-
89
- # Configure include paths
90
- # Also add the portable c10 headers path for standalone builds
91
- portable_c10_dir = File.join(include_dir, 'executorch', 'runtime', 'core', 'portable_type', 'c10')
92
- $INCFLAGS = "-I#{include_dir} -I#{portable_c10_dir} #{$INCFLAGS}"
93
-
94
- # Configure library paths
95
- $LDFLAGS += " -L#{lib_dir}"
96
-
97
- # Add rpath for runtime library loading
98
- $LDFLAGS += if RUBY_PLATFORM =~ /darwin/
99
- " -Wl,-rpath,#{lib_dir}"
100
- else
101
- " -Wl,-rpath,#{lib_dir}"
102
- end
103
-
104
- # ==============================================================================
105
- # Library Linking Configuration
106
- # ==============================================================================
107
-
108
- # Default libraries required for basic operation
109
- DEFAULT_LIBS = %w[
110
- extension_module_static
111
- extension_data_loader
112
- extension_tensor
113
- extension_named_data_map
114
- extension_flat_tensor
115
- extension_threadpool
116
- executorch
117
- executorch_core
118
- ].freeze
119
-
120
- # Determine which libraries to link
121
- libs = if ENV['EXECUTORCH_LIBS']
122
- # User-specified library list (overrides defaults)
123
- user_libs = ENV['EXECUTORCH_LIBS'].split(',').map(&:strip).reject(&:empty?)
124
- puts "Using custom library list: #{user_libs.join(', ')}"
125
- user_libs
126
- else
127
- DEFAULT_LIBS.dup
128
- end
129
-
130
- # Link extension libraries first (order matters for static linking)
131
- libs.each do |lib|
132
- lib_file = File.join(lib_dir, "lib#{lib}.a")
133
- if File.exist?(lib_file)
134
- $LDFLAGS += " -l#{lib}"
135
- puts " Linking: #{lib}"
136
- else
137
- # Try without _static suffix
138
- alt_lib = lib.sub(/_static$/, '')
139
- alt_file = File.join(lib_dir, "lib#{alt_lib}.a")
140
- if File.exist?(alt_file)
141
- $LDFLAGS += " -l#{alt_lib}"
142
- puts " Linking: #{alt_lib} (alternative)"
143
- elsif lib.include?('extension_')
144
- # Extension libraries are optional
145
- puts " Skipping optional: #{lib} (not found)"
146
- else
147
- # Core libraries should exist
148
- abort "Required library not found: #{lib} (looked for #{lib_file})"
131
+ # ----------------------------------------------------------------------------
132
+ # 3. Linking: Shared SDK (preferred) vs Static Source Build
133
+ # ----------------------------------------------------------------------------
134
+
135
+ dlext = is_darwin ? "dylib" : "so"
136
+ shared_runtime = File.join(lib_dir, "libexecutorch.#{dlext}")
137
+
138
+ bundle_vendor = ENV["EXECUTORCH_BUNDLE_VENDOR"] == "1"
139
+ vendor_dir = File.expand_path("../../lib/executorch/vendor", __dir__)
140
+
141
+ $LDFLAGS += " -L#{Shellwords.escape(lib_dir)}"
142
+
143
+ if File.exist?(shared_runtime)
144
+ puts "Linking mode: shared ExecuTorch SDK (.#{dlext})"
145
+
146
+ if bundle_vendor
147
+ FileUtils.mkdir_p(vendor_dir)
148
+ key = Executorch::SDK.platform_key
149
+ files = Executorch::SDK::VENDOR_LIBS[key] || Dir.glob(File.join(lib_dir, "libexecutorch*.#{dlext}"))
150
+ .map { |p| File.basename(p) }
151
+ files.each do |fname|
152
+ src = File.join(lib_dir, fname)
153
+ next unless File.exist?(src)
154
+
155
+ FileUtils.cp(src, File.join(vendor_dir, fname))
156
+ puts " Vendored: #{fname}"
149
157
  end
150
158
  end
151
- end
152
159
 
153
- # Check for portable kernels (if available)
154
- # Use -force_load on macOS to include global constructors that register kernels
155
- portable_ops_lib = File.join(lib_dir, 'libportable_ops_lib.a')
156
- if File.exist?(portable_ops_lib)
157
- $LDFLAGS += if RUBY_PLATFORM =~ /darwin/
158
- # macOS: -force_load pulls in all symbols including global constructors
159
- " -Wl,-force_load,#{portable_ops_lib}"
160
- else
161
- # Linux: --whole-archive achieves the same effect
162
- ' -Wl,--whole-archive -lportable_ops_lib -Wl,--no-whole-archive'
163
- end
164
- puts ' Linking: portable_ops_lib (force_load)'
165
-
166
- # Portable kernels
167
- if File.exist?(File.join(lib_dir, 'libportable_kernels.a'))
168
- $LDFLAGS += ' -lportable_kernels'
169
- puts ' Linking: portable_kernels'
160
+ if is_darwin
161
+ if bundle_vendor
162
+ $LDFLAGS += " -Wl,-rpath,@loader_path/vendor -Wl,-rpath,@loader_path/../vendor"
163
+ else
164
+ $LDFLAGS += " -Wl,-rpath,#{Shellwords.escape(lib_dir)}"
165
+ end
166
+ elsif is_linux
167
+ if bundle_vendor
168
+ $LDFLAGS += " -Wl,--enable-new-dtags -Wl,-rpath,'$$ORIGIN/vendor' -Wl,-rpath,'$$ORIGIN/../vendor'"
169
+ else
170
+ $LDFLAGS += " -Wl,--enable-new-dtags -Wl,-rpath,#{Shellwords.escape(lib_dir)}"
171
+ end
170
172
  end
171
- end
172
173
 
173
- # CPU info and pthreadpool (often required)
174
- %w[cpuinfo pthreadpool].each do |lib|
175
- if File.exist?(File.join(lib_dir, "lib#{lib}.a"))
176
- $LDFLAGS += " -l#{lib}"
177
- puts " Linking: #{lib}"
174
+ # Core runtime + threadpool
175
+ $LOCAL_LIBS += " -lexecutorch"
176
+ if File.exist?(File.join(lib_dir, "libexecutorch_threadpool.#{dlext}"))
177
+ $LOCAL_LIBS += " -lexecutorch_threadpool"
178
+ $CXXFLAGS += " -DET_USE_THREADPOOL" unless $CXXFLAGS.include?("-DET_USE_THREADPOOL")
178
179
  end
179
- end
180
180
 
181
- # Extra libraries from environment (consolidated from version B)
182
- if ENV['EXECUTORCH_EXTRA_LIBS']
183
- extra_libs = ENV['EXECUTORCH_EXTRA_LIBS'].split(',').map(&:strip).reject(&:empty?)
184
- extra_libs.each do |lib|
185
- if File.exist?(File.join(lib_dir, "lib#{lib}.a"))
186
- $LDFLAGS += " -l#{lib}"
187
- puts " Linking extra: #{lib}"
181
+ # Self-registering shared kernel & backend plugins:
182
+ # Must NOT be dropped by linker --as-needed / -dead_strip_dylibs!
183
+ plugin_libs = %w[
184
+ executorch_kernels_optimized
185
+ executorch_kernels_quantized
186
+ executorch_kernels_torchao
187
+ executorch_backend_xnnpack
188
+ executorch_backend_coreml
189
+ executorch_backend_mlx
190
+ ].select { |name| File.exist?(File.join(lib_dir, "lib#{name}.#{dlext}")) }
191
+
192
+ unless plugin_libs.empty?
193
+ puts " Plugins: #{plugin_libs.join(', ')}"
194
+ if is_linux
195
+ $LOCAL_LIBS += " -Wl,--no-as-needed"
196
+ plugin_libs.each { |lib| $LOCAL_LIBS += " -l#{lib}" }
197
+ $LOCAL_LIBS += " -Wl,--as-needed"
198
+ elsif is_darwin
199
+ plugin_libs.each { |lib| $LOCAL_LIBS += " -Wl,-needed-l#{lib}" }
200
+ end
201
+ end
202
+ else
203
+ # Fallback: static ExecuTorch source build (.a archives)
204
+ puts "Linking mode: static ExecuTorch archives (.a)"
205
+
206
+ def force_load_flag(path, is_darwin)
207
+ escaped = Shellwords.escape(path)
208
+ if is_darwin
209
+ " -Wl,-force_load,#{escaped}"
188
210
  else
189
- puts " Warning: extra library not found: #{lib}"
211
+ " -Wl,--whole-archive #{escaped} -Wl,--no-whole-archive"
190
212
  end
191
213
  end
192
- end
193
214
 
194
- # ==============================================================================
195
- # Platform-Specific Configuration
196
- # ==============================================================================
215
+ mandatory = %w[extension_module_static extension_data_loader extension_tensor executorch executorch_core]
216
+ mandatory.each do |lib|
217
+ path = File.join(lib_dir, "lib#{lib}.a")
218
+ alt = File.join(lib_dir, "lib#{lib.sub(/_static$/, '')}.a")
219
+ unless File.exist?(path) || File.exist?(alt)
220
+ abort "Required static library missing: #{lib} (checked #{path})"
221
+ end
222
+ end
223
+
224
+ whole_libs = []
225
+ # Prefer optimized_native_cpu_ops_lib over portable_ops_lib (mutually exclusive)
226
+ %w[optimized_native_cpu_ops_lib portable_ops_lib].each do |ops|
227
+ p = File.join(lib_dir, "lib#{ops}.a")
228
+ if File.exist?(p)
229
+ whole_libs << p
230
+ break
231
+ end
232
+ end
233
+ # Additional non-conflicting self-registering archives
234
+ %w[quantized_ops_lib xnnpack_backend coreml_backend mps_backend].each do |extra|
235
+ p = File.join(lib_dir, "lib#{extra}.a")
236
+ whole_libs << p if File.exist?(p)
237
+ end
238
+
239
+ whole_libs.each do |p|
240
+ puts " Whole-archive: #{File.basename(p)}"
241
+ $LOCAL_LIBS += force_load_flag(p, is_darwin)
242
+ end
243
+
244
+ # Static dependency archives (ordered & grouped on Linux so GNU ld resolves cycles cleanly)
245
+ static_deps = %w[
246
+ extension_module_static extension_module
247
+ extension_data_loader extension_tensor extension_named_data_map
248
+ extension_flat_tensor extension_threadpool
249
+ optimized_kernels portable_kernels quantized_kernels cpublas eigen_blas
250
+ XNNPACK xnnpack-microkernels-prod microkernels-prod kleidiai
251
+ pthreadpool cpuinfo clog
252
+ executorch executorch_core
253
+ ].uniq.map { |l| File.join(lib_dir, "lib#{l}.a") }.select { |p| File.exist?(p) }
254
+
255
+ $LOCAL_LIBS += " -Wl,--start-group" if is_linux
256
+ static_deps.each { |p| $LOCAL_LIBS += " #{Shellwords.escape(p)}" }
257
+ $LOCAL_LIBS += " -Wl,--end-group" if is_linux
258
+
259
+ if is_darwin
260
+ $LOCAL_LIBS += " -framework Accelerate"
261
+ if File.exist?(File.join(lib_dir, "libcoreml_backend.a"))
262
+ $LOCAL_LIBS += " -framework CoreML -framework Foundation -lsqlite3"
263
+ end
264
+ end
265
+ end
197
266
 
198
- if (RUBY_PLATFORM =~ /darwin/) && (RUBY_PLATFORM =~ /arm64/)
199
- # Apple Silicon specific
200
- puts 'Building for Apple Silicon (arm64)'
267
+ # Export ONLY Init_executorch so zero C++ / ExecuTorch symbols leak globally!
268
+ if is_linux
269
+ map_file = File.expand_path("executorch.map", __dir__)
270
+ $DLDFLAGS += " -Wl,--version-script=#{Shellwords.escape(map_file)} -Wl,--exclude-libs,ALL"
271
+ $LOCAL_LIBS += " -lpthread -lm -ldl"
272
+ elsif is_darwin
273
+ $DLDFLAGS += " -Wl,-exported_symbol,_Init_executorch"
201
274
  end
202
275
 
203
- $LDFLAGS += ' -lpthread' if RUBY_PLATFORM =~ /linux/
276
+ # ----------------------------------------------------------------------------
277
+ # 4. Sources & Makefile
278
+ # ----------------------------------------------------------------------------
204
279
 
205
- # Create Makefile
206
- create_makefile('executorch/executorch')
280
+ $VPATH << "$(srcdir)/core"
281
+ $srcs = %w[
282
+ core.cpp
283
+ rb_errors.cpp
284
+ rb_tensor.cpp
285
+ rb_module.cpp
286
+ rb_executorch.cpp
287
+ ]
207
288
 
208
- puts "\nConfiguration complete. Run 'make' to build."
289
+ create_makefile("executorch/executorch")
@@ -0,0 +1,117 @@
1
+ #include "rb_internal.hpp"
2
+
3
+ namespace etrb {
4
+
5
+ static VALUE rb_error_code(VALUE self) {
6
+ return rb_ivar_get(self, g_refs.id_code);
7
+ }
8
+
9
+ static VALUE rb_error_pal_log(VALUE self) {
10
+ return rb_ivar_get(self, g_refs.id_pal_log);
11
+ }
12
+
13
+ void init_errors(VALUE mExecutorch) {
14
+ g_refs.id_code = rb_intern("@code");
15
+ g_refs.id_pal_log = rb_intern("@pal_log");
16
+
17
+ g_refs.eError =
18
+ rb_define_class_under(mExecutorch, "Error", rb_eStandardError);
19
+ rb_define_method(g_refs.eError, "code", rb_error_code, 0);
20
+ rb_define_method(g_refs.eError, "pal_log", rb_error_pal_log, 0);
21
+
22
+ g_refs.eLoadError =
23
+ rb_define_class_under(mExecutorch, "LoadError", g_refs.eError);
24
+ g_refs.eMethodNotFoundError =
25
+ rb_define_class_under(mExecutorch, "MethodNotFoundError", g_refs.eError);
26
+ g_refs.eOperatorMissingError =
27
+ rb_define_class_under(mExecutorch, "OperatorMissingError", g_refs.eError);
28
+ g_refs.eDelegateError =
29
+ rb_define_class_under(mExecutorch, "DelegateError", g_refs.eError);
30
+ g_refs.eShapeError =
31
+ rb_define_class_under(mExecutorch, "ShapeError", g_refs.eError);
32
+ g_refs.eDTypeError =
33
+ rb_define_class_under(mExecutorch, "DTypeError", g_refs.eError);
34
+ g_refs.eExecutionError =
35
+ rb_define_class_under(mExecutorch, "ExecutionError", g_refs.eError);
36
+ g_refs.eMemoryError =
37
+ rb_define_class_under(mExecutorch, "MemoryError", g_refs.eError);
38
+ g_refs.eClosedError =
39
+ rb_define_class_under(mExecutorch, "ClosedError", g_refs.eError);
40
+
41
+ rb_global_variable(&g_refs.eError);
42
+ rb_global_variable(&g_refs.eLoadError);
43
+ rb_global_variable(&g_refs.eMethodNotFoundError);
44
+ rb_global_variable(&g_refs.eOperatorMissingError);
45
+ rb_global_variable(&g_refs.eDelegateError);
46
+ rb_global_variable(&g_refs.eShapeError);
47
+ rb_global_variable(&g_refs.eDTypeError);
48
+ rb_global_variable(&g_refs.eExecutionError);
49
+ rb_global_variable(&g_refs.eMemoryError);
50
+ rb_global_variable(&g_refs.eClosedError);
51
+ }
52
+
53
+ VALUE status_to_ruby_exception(const Status& st) {
54
+ VALUE klass = g_refs.eExecutionError;
55
+ switch (st.kind) {
56
+ case ErrorKind::Load:
57
+ klass = g_refs.eLoadError;
58
+ break;
59
+ case ErrorKind::MethodNotFound:
60
+ klass = g_refs.eMethodNotFoundError;
61
+ break;
62
+ case ErrorKind::OperatorMissing:
63
+ klass = g_refs.eOperatorMissingError;
64
+ break;
65
+ case ErrorKind::Delegate:
66
+ klass = g_refs.eDelegateError;
67
+ break;
68
+ case ErrorKind::Shape:
69
+ klass = g_refs.eShapeError;
70
+ break;
71
+ case ErrorKind::DType:
72
+ klass = g_refs.eDTypeError;
73
+ break;
74
+ case ErrorKind::Memory:
75
+ klass = g_refs.eMemoryError;
76
+ break;
77
+ case ErrorKind::Closed:
78
+ klass = g_refs.eClosedError;
79
+ break;
80
+ case ErrorKind::Argument:
81
+ klass = rb_eArgError;
82
+ break;
83
+ case ErrorKind::Type:
84
+ klass = rb_eTypeError;
85
+ break;
86
+ case ErrorKind::Range:
87
+ klass = rb_eRangeError;
88
+ break;
89
+ case ErrorKind::Execution:
90
+ default:
91
+ klass = g_refs.eExecutionError;
92
+ break;
93
+ }
94
+
95
+ VALUE msg = rb_utf8_str_new(st.message.data(), static_cast<long>(st.message.size()));
96
+ VALUE exc = rb_exc_new_str(klass, msg);
97
+ if (rb_obj_is_kind_of(exc, g_refs.eError)) {
98
+ rb_ivar_set(
99
+ exc,
100
+ g_refs.id_code,
101
+ st.et_code == 0 ? Qnil : ULONG2NUM(static_cast<unsigned long>(st.et_code)));
102
+ rb_ivar_set(
103
+ exc,
104
+ g_refs.id_pal_log,
105
+ st.pal_log.empty()
106
+ ? Qnil
107
+ : rb_utf8_str_new(
108
+ st.pal_log.data(), static_cast<long>(st.pal_log.size())));
109
+ }
110
+ return exc;
111
+ }
112
+
113
+ [[noreturn]] void raise_status_no_live_cpp(VALUE exc) {
114
+ rb_exc_raise(exc);
115
+ }
116
+
117
+ } // namespace etrb
@@ -0,0 +1,69 @@
1
+ #include "rb_internal.hpp"
2
+
3
+ namespace etrb {
4
+
5
+ RubyRefs g_refs;
6
+
7
+ namespace {
8
+
9
+ VALUE rb_et_backends(VALUE) {
10
+ auto list = registered_backends();
11
+ VALUE ary = rb_ary_new_capa(static_cast<long>(list.size()));
12
+ for (const auto& name : list) {
13
+ rb_ary_push(ary, rb_utf8_str_new(name.data(), static_cast<long>(name.size())));
14
+ }
15
+ return ary;
16
+ }
17
+
18
+ VALUE rb_et_kernel_count(VALUE) {
19
+ return ULL2NUM(registered_kernel_count());
20
+ }
21
+
22
+ VALUE rb_et_num_threads(VALUE) {
23
+ return LL2NUM(get_num_threads());
24
+ }
25
+
26
+ VALUE rb_et_set_num_threads(VALUE, VALUE val) {
27
+ if (!RB_INTEGER_TYPE_P(val)) {
28
+ rb_raise(rb_eTypeError, "num_threads must be an Integer");
29
+ }
30
+ int64_t n = NUM2LL(val);
31
+ VALUE err_exc = Qnil;
32
+ {
33
+ auto st = set_num_threads(n);
34
+ if (!st.ok()) {
35
+ err_exc = status_to_ruby_exception(st);
36
+ }
37
+ }
38
+ if (err_exc != Qnil) raise_status_no_live_cpp(err_exc);
39
+ return val;
40
+ }
41
+
42
+ VALUE rb_et_executorch_version(VALUE) {
43
+ #ifdef EXECUTORCH_RUBY_SDK_VERSION
44
+ return rb_utf8_str_new_cstr(EXECUTORCH_RUBY_SDK_VERSION);
45
+ #else
46
+ return rb_utf8_str_new_cstr("unknown");
47
+ #endif
48
+ }
49
+
50
+ } // namespace
51
+ } // namespace etrb
52
+
53
+ extern "C" __attribute__((visibility("default"))) void Init_executorch(void) {
54
+ etrb::init_runtime_once();
55
+
56
+ VALUE mExecutorch = rb_define_module("Executorch");
57
+ etrb::g_refs.mExecutorch = mExecutorch;
58
+ rb_global_variable(&etrb::g_refs.mExecutorch);
59
+
60
+ etrb::init_errors(mExecutorch);
61
+ etrb::init_tensor(mExecutorch);
62
+ etrb::init_module(mExecutorch);
63
+
64
+ rb_define_singleton_method(mExecutorch, "backends", etrb::rb_et_backends, 0);
65
+ rb_define_singleton_method(mExecutorch, "kernel_count", etrb::rb_et_kernel_count, 0);
66
+ rb_define_singleton_method(mExecutorch, "num_threads", etrb::rb_et_num_threads, 0);
67
+ rb_define_singleton_method(mExecutorch, "num_threads=", etrb::rb_et_set_num_threads, 1);
68
+ rb_define_singleton_method(mExecutorch, "executorch_version", etrb::rb_et_executorch_version, 0);
69
+ }