torobi 0.1.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 +7 -0
- data/CHANGELOG.md +71 -0
- data/Cargo.lock +539 -0
- data/Cargo.toml +6 -0
- data/LICENSE +21 -0
- data/README.md +259 -0
- data/config/ops.yml +230 -0
- data/docs/plan.md +3982 -0
- data/docs/vendoring.md +656 -0
- data/engine/Cargo.toml +35 -0
- data/engine/build.rs +253 -0
- data/engine/src/checkpoint.rs +513 -0
- data/engine/src/executor.rs +257 -0
- data/engine/src/export.rs +431 -0
- data/engine/src/fixtures.rs +518 -0
- data/engine/src/graph.rs +115 -0
- data/engine/src/init.rs +65 -0
- data/engine/src/interp.rs +985 -0
- data/engine/src/lib.rs +62 -0
- data/engine/src/main.rs +97 -0
- data/engine/src/memory.rs +109 -0
- data/engine/src/mlxc/array.rs +454 -0
- data/engine/src/mlxc/error.rs +205 -0
- data/engine/src/mlxc/fast.rs +46 -0
- data/engine/src/mlxc/handle.rs +155 -0
- data/engine/src/mlxc/io.rs +268 -0
- data/engine/src/mlxc/memory.rs +56 -0
- data/engine/src/mlxc/metal.rs +24 -0
- data/engine/src/mlxc/mod.rs +55 -0
- data/engine/src/mlxc/nn.rs +38 -0
- data/engine/src/mlxc/ops.rs +477 -0
- data/engine/src/mlxc/quantized.rs +405 -0
- data/engine/src/mlxc/random.rs +161 -0
- data/engine/src/mlxc/transforms.rs +439 -0
- data/engine/src/op.rs +745 -0
- data/engine/src/optimizer.rs +650 -0
- data/engine/src/plan.rs +1638 -0
- data/engine/src/runtime.rs +377 -0
- data/engine/src/session.rs +1523 -0
- data/engine/src/state.rs +1007 -0
- data/engine/src/tensor.rs +331 -0
- data/ext/torobi/Cargo.toml +21 -0
- data/ext/torobi/extconf.rb +42 -0
- data/ext/torobi/src/gvl.rs +182 -0
- data/ext/torobi/src/lib.rs +1037 -0
- data/lib/torobi/adapter_file.rb +184 -0
- data/lib/torobi/batch.rb +55 -0
- data/lib/torobi/checkpoint.rb +60 -0
- data/lib/torobi/dsl/builder.rb +414 -0
- data/lib/torobi/dsl/handle.rb +156 -0
- data/lib/torobi/dsl/layers.rb +82 -0
- data/lib/torobi/dsl.rb +35 -0
- data/lib/torobi/errors.rb +88 -0
- data/lib/torobi/export.rb +534 -0
- data/lib/torobi/freeze.rb +24 -0
- data/lib/torobi/freshness.rb +57 -0
- data/lib/torobi/grad_cache.rb +143 -0
- data/lib/torobi/graph_config.rb +301 -0
- data/lib/torobi/hooks.rb +439 -0
- data/lib/torobi/ir/dimensions.rb +26 -0
- data/lib/torobi/ir/dtype.rb +28 -0
- data/lib/torobi/ir/graph/check.rb +116 -0
- data/lib/torobi/ir/graph.rb +52 -0
- data/lib/torobi/ir/input_spec.rb +39 -0
- data/lib/torobi/ir/json.rb +45 -0
- data/lib/torobi/ir/node_spec.rb +52 -0
- data/lib/torobi/ir/parameter_spec.rb +67 -0
- data/lib/torobi/ir/ref.rb +52 -0
- data/lib/torobi/ir/source.rb +65 -0
- data/lib/torobi/ir.rb +23 -0
- data/lib/torobi/journal.rb +310 -0
- data/lib/torobi/lora.rb +190 -0
- data/lib/torobi/memory.rb +50 -0
- data/lib/torobi/metallib.rb +50 -0
- data/lib/torobi/mlx_prebuilt.json +24 -0
- data/lib/torobi/mlx_prebuilt.rb +378 -0
- data/lib/torobi/models/build.rb +37 -0
- data/lib/torobi/models/description.rb +59 -0
- data/lib/torobi/models/gemma3.rb +273 -0
- data/lib/torobi/models/llama.rb +381 -0
- data/lib/torobi/models/modern_bert.rb +513 -0
- data/lib/torobi/models/padding.rb +27 -0
- data/lib/torobi/models/windows.rb +53 -0
- data/lib/torobi/ops.rb +81 -0
- data/lib/torobi/preflight.rb +136 -0
- data/lib/torobi/provenance.rb +75 -0
- data/lib/torobi/quantization.rb +151 -0
- data/lib/torobi/replay.rb +229 -0
- data/lib/torobi/runner.rb +419 -0
- data/lib/torobi/session.rb +855 -0
- data/lib/torobi/shape.rb +438 -0
- data/lib/torobi/tensor_data.rb +178 -0
- data/lib/torobi/version.rb +5 -0
- data/lib/torobi/weights.rb +108 -0
- data/lib/torobi.rb +67 -0
- data/licenses/MLX.txt +21 -0
- data/licenses/README.md +16 -0
- data/licenses/gguflib.txt +21 -0
- data/licenses/mlx-c.txt +21 -0
- data/licenses/mlx-rs.txt +27 -0
- metadata +229 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "json"
|
|
6
|
+
require "uri"
|
|
7
|
+
|
|
8
|
+
# The MLX every cargo build here links, as a system install prefix.
|
|
9
|
+
#
|
|
10
|
+
# MLX's Metal kernels can only be compiled by Apple's Metal toolchain,
|
|
11
|
+
# which is not on every machine (docs/vendoring.md). So a build takes a
|
|
12
|
+
# pre-built MLX instead, with mlx-c already built into it: the engine binds
|
|
13
|
+
# mlx-c's headers and links its archive straight from the prefix
|
|
14
|
+
# (engine/build.rs, and docs/vendoring.md "mlx-c, bound here"). This
|
|
15
|
+
# fetches that MLX once, checks the bytes against a recorded digest, and
|
|
16
|
+
# says where the prefix landed, which is all a build is told.
|
|
17
|
+
#
|
|
18
|
+
# **The digest is the point.** A release asset can be replaced, and
|
|
19
|
+
# nothing about the URL would change; a recorded hash is what turns "what
|
|
20
|
+
# GitHub serves today" into "the bytes this project ran its tests on".
|
|
21
|
+
#
|
|
22
|
+
# It is deliberately small and free of Torobi: it runs before anything is
|
|
23
|
+
# built, so it may use only what ships with Ruby.
|
|
24
|
+
module MlxPrebuilt
|
|
25
|
+
# The pin, read beside this file rather than written into it.
|
|
26
|
+
#
|
|
27
|
+
# A digest is written by whoever built the archive, not by whoever wrote
|
|
28
|
+
# this code, and a value transcribed by hand is a value that can be
|
|
29
|
+
# transcribed wrongly. `rake mlx:pin` puts it there, having downloaded
|
|
30
|
+
# the bytes and hashed them; this only reads it.
|
|
31
|
+
PIN_PATH = File.join(__dir__, "mlx_prebuilt.json").freeze
|
|
32
|
+
|
|
33
|
+
# The shape of an MLX install prefix: the package `find_package(MLX)`
|
|
34
|
+
# reads, the headers mlx-c compiles against, and the kernels MLX loads
|
|
35
|
+
# at runtime. Everything is checked, because an archive missing any of
|
|
36
|
+
# them would fail later and less clearly.
|
|
37
|
+
CMAKE_CONFIG = "share/cmake/MLX/MLXConfig.cmake"
|
|
38
|
+
HEADERS = "include/mlx"
|
|
39
|
+
METALLIB = "lib/mlx.metallib"
|
|
40
|
+
LIBS = %w[lib/libmlx.a lib/libmlxc.a lib/libgguflib.a].freeze
|
|
41
|
+
|
|
42
|
+
# What MLX compiles its CUDA kernels against at run time. Everything
|
|
43
|
+
# above is in both archives, so this is also what tells a macOS one
|
|
44
|
+
# unpacked on Linux from an archive that belongs there.
|
|
45
|
+
JIT_HEADERS = "include/cccl"
|
|
46
|
+
|
|
47
|
+
# A note that this directory was extracted from an archive with the
|
|
48
|
+
# right digest. Cheaper than hashing 140 MB on every build, and enough:
|
|
49
|
+
# what it defends against is a different download, not a hostile
|
|
50
|
+
# filesystem.
|
|
51
|
+
STAMP = ".verified"
|
|
52
|
+
|
|
53
|
+
class Refused < StandardError; end
|
|
54
|
+
|
|
55
|
+
module_function
|
|
56
|
+
|
|
57
|
+
# Read every time rather than captured into constants: `rake mlx:pin`
|
|
58
|
+
# writes this file and then fetches through the same code an install
|
|
59
|
+
# uses, so a value read at load time would still be the pin it is
|
|
60
|
+
# replacing - which is how a pin could never move off an old release.
|
|
61
|
+
def pin = JSON.parse(File.read(PIN_PATH))
|
|
62
|
+
def repo = pin.fetch("repo")
|
|
63
|
+
def release = pin.fetch("release")
|
|
64
|
+
def url = "https://github.com/#{repo}/releases/download/#{release}/#{asset}"
|
|
65
|
+
|
|
66
|
+
# Which of a release's archives this machine wants. Coarse on purpose:
|
|
67
|
+
# nothing here can tell which CUDA a machine has, so moving from 12 to 13
|
|
68
|
+
# is an asset and digest under the same key rather than a new one.
|
|
69
|
+
def platform
|
|
70
|
+
case RUBY_PLATFORM
|
|
71
|
+
when /\Aarm64-darwin/ then "macos-arm64"
|
|
72
|
+
when /\Ax86_64-linux/ then "linux-x86_64"
|
|
73
|
+
else
|
|
74
|
+
raise Refused, "torobi has no pre-built MLX for #{RUBY_PLATFORM}. Build MLX " \
|
|
75
|
+
"yourself and set TOROBI_MLX_PREFIX to its install prefix."
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def metal? = platform.start_with?("macos")
|
|
80
|
+
|
|
81
|
+
# The pinned archive for this machine.
|
|
82
|
+
def entry
|
|
83
|
+
pin.fetch("platforms").fetch(platform) do
|
|
84
|
+
raise Refused, "no MLX archive is pinned for #{platform} in #{PIN_PATH}. " \
|
|
85
|
+
"Build one with takahashim/mlx-prebuilt and `rake mlx:pin`, " \
|
|
86
|
+
"or set TOROBI_MLX_PREFIX to a prefix you built yourself."
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def asset = entry.fetch("asset")
|
|
91
|
+
|
|
92
|
+
# SHA-256 of that asset. The whole point of this file: a release asset
|
|
93
|
+
# can be replaced without its URL changing, and this is what notices.
|
|
94
|
+
def digest = entry.fetch("digest")
|
|
95
|
+
|
|
96
|
+
# The generation the engine's binding was written against, keyed the way
|
|
97
|
+
# the manifest spells it. A different question from what the archive
|
|
98
|
+
# holds: the archive is what was built, this is which mlx-c the calls in
|
|
99
|
+
# engine/src/mlxc/ were chosen for and checked against. bindgen reads
|
|
100
|
+
# the archive's own headers, so a different mlx-c would still link; what
|
|
101
|
+
# it could do is compile and mean something else. Empty for a pin that
|
|
102
|
+
# predates the field.
|
|
103
|
+
#
|
|
104
|
+
# `mlx-c` is a commit, not the `v0.6.0` tag: mlx-c's tag pins MLX 0.31.1
|
|
105
|
+
# and seven later commits pin 0.32.2, and they change headers under
|
|
106
|
+
# `mlx/c/`. A manifest naming the tag is the wrong build even though it
|
|
107
|
+
# says 0.6.0.
|
|
108
|
+
def required = pin.fetch("requires", {})
|
|
109
|
+
|
|
110
|
+
# The MLX install prefix to hand mlx-c, fetching and checking it if it
|
|
111
|
+
# is not already there. `TOROBI_MLX_PREFIX` from the caller wins and is
|
|
112
|
+
# left alone: someone who has built MLX themselves has said so.
|
|
113
|
+
def ensure!(into: cache_dir, io: $stderr)
|
|
114
|
+
given = ENV.fetch("TOROBI_MLX_PREFIX", nil)
|
|
115
|
+
return given if given && !given.empty?
|
|
116
|
+
|
|
117
|
+
archive = nil
|
|
118
|
+
begin
|
|
119
|
+
unless ready?(into)
|
|
120
|
+
# The asset names the versions it holds, which is accurate even
|
|
121
|
+
# while `rake mlx:pin` moves the pin (the file's own `mlx` /
|
|
122
|
+
# `mlx_c` are still the outgoing ones until the manifest is read).
|
|
123
|
+
io.puts "torobi: fetching #{asset} from #{url}"
|
|
124
|
+
archive = download(io:)
|
|
125
|
+
verify(archive)
|
|
126
|
+
unpack(archive, into)
|
|
127
|
+
File.write(File.join(into, STAMP), digest)
|
|
128
|
+
io.puts "torobi: MLX ready in #{into}"
|
|
129
|
+
end
|
|
130
|
+
ensure
|
|
131
|
+
FileUtils.rm_f(archive) if archive
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
check_version(manifest(into:))
|
|
135
|
+
prefix(into)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Which directory of what was unpacked is the install prefix.
|
|
139
|
+
#
|
|
140
|
+
# Searched rather than named, because the archive's top directory is the
|
|
141
|
+
# archive's business; the only thing said here is what an MLX prefix is
|
|
142
|
+
# made of.
|
|
143
|
+
def prefix(dir)
|
|
144
|
+
found = Dir.glob(File.join(dir, "**", CMAKE_CONFIG))
|
|
145
|
+
.map { |path| File.dirname(path, 4) }
|
|
146
|
+
.find { |place| complete?(place) }
|
|
147
|
+
unless found
|
|
148
|
+
raise Refused, "#{dir} holds no MLX install prefix for #{platform} " \
|
|
149
|
+
"(wants #{(wanted_dirs + wanted_files).join(", ")})"
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
found
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def wanted_files
|
|
156
|
+
metal? ? [CMAKE_CONFIG, METALLIB, *LIBS] : [CMAKE_CONFIG, *LIBS]
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def wanted_dirs
|
|
160
|
+
metal? ? [HEADERS] : [HEADERS, JIT_HEADERS]
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def complete?(place)
|
|
164
|
+
wanted_dirs.all? { |rel| File.directory?(File.join(place, rel)) } &&
|
|
165
|
+
wanted_files.all? { |rel| File.file?(File.join(place, rel)) }
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Whether the archive is the generation the binding was written for.
|
|
169
|
+
#
|
|
170
|
+
# The digest says the bytes are the ones pinned, not that they are the
|
|
171
|
+
# right MLX under mlx-c's headers; only the manifest can say that, so a
|
|
172
|
+
# missing manifest is refused rather than waved through.
|
|
173
|
+
def check_version(said)
|
|
174
|
+
return if required.empty?
|
|
175
|
+
|
|
176
|
+
unless said
|
|
177
|
+
raise Refused, "#{asset} has no MANIFEST.txt, so which MLX it holds cannot " \
|
|
178
|
+
"be checked against the binding (#{required.inspect})"
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
required.each do |name, want|
|
|
182
|
+
got = said[name].to_s.delete_prefix("v")
|
|
183
|
+
# A commit may be recorded short or full; a version is exact.
|
|
184
|
+
next if got == want || got.start_with?(want)
|
|
185
|
+
|
|
186
|
+
raise Refused, "#{asset} holds #{name} #{got.empty? ? "(unstated)" : got}, " \
|
|
187
|
+
"but the binding wants #{want}. Build takahashim/mlx-prebuilt " \
|
|
188
|
+
"against that generation and `rake mlx:pin` it."
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# What the archive says it holds, or nil when it says nothing.
|
|
193
|
+
#
|
|
194
|
+
# `MANIFEST.txt` is a build's own account of itself: which mlx-c, which
|
|
195
|
+
# MLX, and on what. Read from here rather than from a caller, because
|
|
196
|
+
# where it sits is a fact about the archive and this is the file that
|
|
197
|
+
# knows about archives.
|
|
198
|
+
def manifest(into: cache_dir)
|
|
199
|
+
found = Dir.glob(File.join(into, "**", "MANIFEST.txt")).first
|
|
200
|
+
return nil unless found
|
|
201
|
+
|
|
202
|
+
File.read(found).lines.filter_map do |line|
|
|
203
|
+
key, value = line.split(/\s+/, 2)
|
|
204
|
+
[key, value.strip] if key && value
|
|
205
|
+
end.to_h
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Where mlx.metallib is, for whoever has to put it beside a binary.
|
|
209
|
+
def metallib(prefix)
|
|
210
|
+
File.join(prefix, METALLIB)
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# The kernels on their own, as a platform gem fetches them: the same
|
|
214
|
+
# `mlx.metallib`, tarred, so the first use pulls tens of megabytes rather
|
|
215
|
+
# than the whole archive and unpacks no SDK. macOS only; a Linux build
|
|
216
|
+
# compiles its CUDA kernels at run time and has no such file to fetch.
|
|
217
|
+
def metallib_entry
|
|
218
|
+
entry.fetch("metallib") do
|
|
219
|
+
raise Refused, "#{platform} has no standalone metallib asset pinned in " \
|
|
220
|
+
"#{PIN_PATH}, so a platform gem cannot fetch one. The full " \
|
|
221
|
+
"archive holds it; `rake mlx:pin` records what a release " \
|
|
222
|
+
"carries."
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def metallib_asset = metallib_entry.fetch("asset")
|
|
227
|
+
def metallib_digest = metallib_entry.fetch("digest")
|
|
228
|
+
def metallib_url = "https://github.com/#{repo}/releases/download/#{release}/#{metallib_asset}"
|
|
229
|
+
|
|
230
|
+
# Fetches `mlx.metallib` into `into` and returns where it landed. The
|
|
231
|
+
# asset is a tar.gz holding `mlx.metallib` at its root, so this is the
|
|
232
|
+
# same download, verify and untar the archive takes, for one file.
|
|
233
|
+
def fetch_metallib(into:, io: $stderr)
|
|
234
|
+
require "tmpdir"
|
|
235
|
+
Dir.mktmpdir("torobi-metallib") do |work|
|
|
236
|
+
archive = File.join(work, metallib_asset)
|
|
237
|
+
io.puts "torobi: fetching #{metallib_asset} from #{metallib_url}"
|
|
238
|
+
File.open(archive, "wb") { |file| stream(URI.parse(metallib_url), file, io) }
|
|
239
|
+
verify_digest(archive, metallib_digest, what: metallib_asset)
|
|
240
|
+
unless system("tar", "-xzf", archive, "-C", work)
|
|
241
|
+
raise Refused, "could not unpack #{metallib_asset}"
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
found = File.join(work, "mlx.metallib")
|
|
245
|
+
raise Refused, "#{metallib_asset} holds no mlx.metallib at its root" unless File.file?(found)
|
|
246
|
+
|
|
247
|
+
# Alongside, then moved into place: MLX must never see a half-written
|
|
248
|
+
# file, and a run that dies here should leave the old one alone.
|
|
249
|
+
target = File.join(into, "mlx.metallib")
|
|
250
|
+
FileUtils.mkdir_p(into)
|
|
251
|
+
FileUtils.cp(found, "#{target}.part")
|
|
252
|
+
FileUtils.mv("#{target}.part", target)
|
|
253
|
+
target
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Whether a previous run left a complete, checked copy here.
|
|
258
|
+
def ready?(dir)
|
|
259
|
+
return false unless File.read(File.join(dir, STAMP)).strip == digest
|
|
260
|
+
|
|
261
|
+
prefix(dir)
|
|
262
|
+
true
|
|
263
|
+
rescue SystemCallError, Refused
|
|
264
|
+
false
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# One copy per machine rather than one per build directory: the
|
|
268
|
+
# extension and the engine are two cargo builds of the same MLX, and
|
|
269
|
+
# this is 140 MB of it.
|
|
270
|
+
def cache_dir
|
|
271
|
+
home = ENV["XDG_CACHE_HOME"] || File.join(Dir.home, ".cache")
|
|
272
|
+
File.join(home, "torobi", "mlx-prebuilt", release)
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# Streamed to disk as it arrives, and told about as it goes: this is
|
|
276
|
+
# forty megabytes over whatever connection the machine has, in the
|
|
277
|
+
# middle of an install, and silence for ten minutes looks like a hang.
|
|
278
|
+
#
|
|
279
|
+
# `Net::HTTP` rather than open-uri, which buffers the whole body into a
|
|
280
|
+
# temporary file before handing it over: the same bytes written twice,
|
|
281
|
+
# and no way to say how far along it is.
|
|
282
|
+
def download(io:)
|
|
283
|
+
require "tmpdir"
|
|
284
|
+
path = File.join(Dir.mktmpdir("torobi-mlx"), asset)
|
|
285
|
+
File.open(path, "wb") { |file| stream(URI.parse(url), file, io) }
|
|
286
|
+
path
|
|
287
|
+
rescue Refused
|
|
288
|
+
raise
|
|
289
|
+
rescue StandardError => e
|
|
290
|
+
raise Refused, "could not fetch #{url} (#{e.class}: #{e.message}). " \
|
|
291
|
+
"Build MLX yourself and set TOROBI_MLX_PREFIX to its install " \
|
|
292
|
+
"prefix, or install the Metal toolchain " \
|
|
293
|
+
"(xcodebuild -downloadComponent MetalToolchain) and build MLX " \
|
|
294
|
+
"from source."
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# How many redirects to follow. A release asset is one hop to storage;
|
|
298
|
+
# more than a few means something else is going on.
|
|
299
|
+
HOPS = 5
|
|
300
|
+
|
|
301
|
+
def stream(uri, file, io, hops: HOPS)
|
|
302
|
+
# Required where it is used rather than at the top: a platform gem
|
|
303
|
+
# loads this module for `Torobi::Metallib`, which needs the network
|
|
304
|
+
# only on the first use, and net/http is not small.
|
|
305
|
+
require "net/http"
|
|
306
|
+
raise Refused, "#{url} redirects further than #{HOPS} hops" if hops.zero?
|
|
307
|
+
|
|
308
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
|
|
309
|
+
http.request(Net::HTTP::Get.new(uri)) do |response|
|
|
310
|
+
case response
|
|
311
|
+
when Net::HTTPRedirection
|
|
312
|
+
# Storage is a different host, and https only: a redirect that
|
|
313
|
+
# drops to http is refused rather than followed.
|
|
314
|
+
nxt = URI.parse(response["location"])
|
|
315
|
+
raise Refused, "#{url} redirects to #{nxt.scheme}" unless nxt.scheme == "https"
|
|
316
|
+
|
|
317
|
+
return stream(nxt, file, io, hops: hops - 1)
|
|
318
|
+
when Net::HTTPSuccess
|
|
319
|
+
report = progress(response["content-length"].to_i, io)
|
|
320
|
+
response.read_body do |chunk|
|
|
321
|
+
file.write(chunk)
|
|
322
|
+
report.call(file.size)
|
|
323
|
+
end
|
|
324
|
+
io.puts
|
|
325
|
+
else
|
|
326
|
+
raise Refused, "#{url} answered #{response.code} #{response.message}"
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# A line that overwrites itself, or nothing at all when the size is
|
|
333
|
+
# unknown or nobody is watching.
|
|
334
|
+
def progress(total, io)
|
|
335
|
+
return ->(_) {} if total.zero? || !io.tty?
|
|
336
|
+
|
|
337
|
+
last = -1
|
|
338
|
+
lambda do |so_far|
|
|
339
|
+
percent = so_far * 100 / total
|
|
340
|
+
next if percent == last
|
|
341
|
+
|
|
342
|
+
last = percent
|
|
343
|
+
io.print("\rtorobi: #{percent}% of #{total / 1024 / 1024} MB")
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
def verify(archive) = verify_digest(archive, digest, what: asset)
|
|
348
|
+
|
|
349
|
+
def verify_digest(path, expected, what:)
|
|
350
|
+
got = Digest::SHA256.file(path).hexdigest
|
|
351
|
+
return if got == expected
|
|
352
|
+
|
|
353
|
+
raise Refused, "#{what} is not the file this was built against.\n " \
|
|
354
|
+
"expected #{expected}\n " \
|
|
355
|
+
"received #{got}\n" \
|
|
356
|
+
"Nothing was installed. If the release was replaced on " \
|
|
357
|
+
"purpose, the new digest belongs in " \
|
|
358
|
+
"lib/torobi/mlx_prebuilt.json, next to the version of MLX " \
|
|
359
|
+
"it holds; `rake mlx:pin` puts it there."
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
# Into a directory of its own, and only once it is whole: an unpack
|
|
363
|
+
# interrupted half way should not look like a copy that is ready.
|
|
364
|
+
def unpack(archive, into)
|
|
365
|
+
staging = "#{into}.unpacking"
|
|
366
|
+
FileUtils.rm_rf(staging)
|
|
367
|
+
FileUtils.mkdir_p(staging)
|
|
368
|
+
unless system("tar", "-xzf", archive, "-C", staging)
|
|
369
|
+
raise Refused, "could not unpack #{archive}"
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
prefix(staging) # refuse a wrong archive before it is given a name
|
|
373
|
+
FileUtils.rm_rf(into)
|
|
374
|
+
FileUtils.mkdir_p(File.dirname(into))
|
|
375
|
+
FileUtils.mv(staging, into)
|
|
376
|
+
prefix(into)
|
|
377
|
+
end
|
|
378
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Torobi
|
|
4
|
+
module Models
|
|
5
|
+
# The context a model graph is built in, as one value.
|
|
6
|
+
#
|
|
7
|
+
# Four things travel from the top of a description down to the leaves
|
|
8
|
+
# and are read there: how long a sequence the graph is declared for
|
|
9
|
+
# (nil where it is declared for none), how many rows it is declared
|
|
10
|
+
# for, what precision the weights are held in, and which batch fields
|
|
11
|
+
# to read. None of them is a decision any layer makes. They are what
|
|
12
|
+
# the caller decided, carried.
|
|
13
|
+
#
|
|
14
|
+
# **As one value rather than four keywords, because that is what they
|
|
15
|
+
# are.** Threaded separately they cost every signature between the top
|
|
16
|
+
# and the leaf: adding `fields:`, so that one encoder could read two
|
|
17
|
+
# sets of rows (docs/plan.md 15.63), meant editing `body`, `encode`,
|
|
18
|
+
# `pool` and their callers to pass along something none of them looks
|
|
19
|
+
# at. The next one should cost the constructor that sets it and the
|
|
20
|
+
# leaf that reads it.
|
|
21
|
+
#
|
|
22
|
+
# A build says what the *graph* is; it does not say what a batch is.
|
|
23
|
+
# `ModernBERT.batch(config, rows, seq:)` keeps its own `seq`, and
|
|
24
|
+
# means something else by it: a graph's is the length it is declared
|
|
25
|
+
# for, and a batch's is the length its rows are padded to. Since
|
|
26
|
+
# 15.63 those are different numbers.
|
|
27
|
+
Build = Data.define(:seq, :rows, :dtype, :fields) do
|
|
28
|
+
def initialize(seq: nil, rows: nil, dtype: :f32, fields: "")
|
|
29
|
+
super(seq:, rows:, dtype: dtype.to_sym, fields: fields.to_s)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# A batch field's name under this build's prefix: "queries." and
|
|
33
|
+
# `:input_ids` is what the query side of `ModernBERT.towers` reads.
|
|
34
|
+
def field(name) = :"#{fields}#{name}"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Torobi
|
|
4
|
+
module Models
|
|
5
|
+
# What an architecture is described against: the builder it writes
|
|
6
|
+
# into, the configuration it is built from, and what it is being built
|
|
7
|
+
# for. One builder serves the whole graph and the config never
|
|
8
|
+
# changes, so they are held here and a signature carries what actually
|
|
9
|
+
# varies -- the value flowing through.
|
|
10
|
+
#
|
|
11
|
+
# def layer(x)
|
|
12
|
+
# x += attention(norm(x, name: "input_layernorm"))
|
|
13
|
+
# x + mlp(norm(x, name: "post_attention_layernorm"))
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# A subclass is one architecture. It reaches the graph through
|
|
17
|
+
# VOCABULARY below and the rest of what it needs through `@config` and
|
|
18
|
+
# `@build`; `Torobi.graph` still owns the builder and the frozen IR
|
|
19
|
+
# that comes out of it.
|
|
20
|
+
class Description
|
|
21
|
+
# What a description may say to the graph, and nothing else.
|
|
22
|
+
#
|
|
23
|
+
# Written out rather than inherited from `DSL::Builder` or forwarded
|
|
24
|
+
# wholesale, for the same reason the op manifest is a file: a
|
|
25
|
+
# description works in a vocabulary somebody chose, and one that can
|
|
26
|
+
# reach `emit` can put a node in the graph that no layer here means.
|
|
27
|
+
# Adding to this list is how the vocabulary grows, which is a
|
|
28
|
+
# deliberate act rather than a side effect of inheriting.
|
|
29
|
+
#
|
|
30
|
+
# These read without a receiver, so a call that builds the graph and
|
|
31
|
+
# a call that reads a number look different on the page:
|
|
32
|
+
# `linear(x, ...)` against `@config.hidden_size`.
|
|
33
|
+
VOCABULARY = %i[
|
|
34
|
+
input from_batch from_model output
|
|
35
|
+
scope sharing adapting quantized parameter
|
|
36
|
+
linear embedding layer_norm rms_norm geglu
|
|
37
|
+
matmul sdpa cross_entropy cast mean sum max
|
|
38
|
+
].freeze
|
|
39
|
+
|
|
40
|
+
VOCABULARY.each do |op|
|
|
41
|
+
define_method(op) do |*args, **kwargs, &block|
|
|
42
|
+
@g.public_send(op, *args, **kwargs, &block)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# `build` is what this graph is being built for (`Models::Build`).
|
|
47
|
+
# ModernBERT's towers make one description per side, because each
|
|
48
|
+
# side reads its own fields at its own length; they share the
|
|
49
|
+
# builder, which is what makes them one graph.
|
|
50
|
+
def initialize(g, config, build)
|
|
51
|
+
@g = g
|
|
52
|
+
@config = config
|
|
53
|
+
@build = build
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
attr_reader :config, :build
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|