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
data/README.md
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
# Torobi(とろ火)
|
|
2
|
+
|
|
3
|
+
A Ruby training framework for Apple Silicon, built on MLX.
|
|
4
|
+
|
|
5
|
+
Torobi is for slow cooking: fine-tuning and distilling known model architectures on a local Mac, declaratively and safely.
|
|
6
|
+
|
|
7
|
+
Models and objectives are described once in a Graph DSL and become an immutable, serializable `GraphConfig`.
|
|
8
|
+
A Rust engine (via MLX) owns the execution, and the boundary between them is about a dozen calls wide.
|
|
9
|
+
|
|
10
|
+
## Requirements
|
|
11
|
+
|
|
12
|
+
- Apple Silicon Mac
|
|
13
|
+
- Ruby 3.3 or newer
|
|
14
|
+
- no Metal toolchain: a pre-built MLX is fetched and checked at build time
|
|
15
|
+
(`lib/torobi/mlx_prebuilt.rb`), and the engine binds its mlx-c directly
|
|
16
|
+
- a Rust toolchain, only for a source install (a precompiled gem needs none)
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
The precompiled gem for Apple Silicon installs with no Rust toolchain:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
gem install torobi
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
A source install (another platform, or a checkout) builds the extension and
|
|
27
|
+
needs Rust:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
gem "torobi", github: "takahashim/torobi"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## A first run
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
require "torobi"
|
|
37
|
+
|
|
38
|
+
# What to compute. Plain Ruby, run once, at build time.
|
|
39
|
+
model = Torobi.graph do |g|
|
|
40
|
+
x = g.input :x, [nil, 2]
|
|
41
|
+
y = g.input :y, [nil, 1]
|
|
42
|
+
g.output :loss, g.mse(g.linear(x, 1, name: "l"), y)
|
|
43
|
+
end
|
|
44
|
+
config = Torobi::GraphConfig.new(models: { m: model })
|
|
45
|
+
|
|
46
|
+
# What to start from. A file or a published checkpoint, or values here.
|
|
47
|
+
weights = { params: { "m.l.weight" => { shape: [1, 2], data: [0.0, 0.0] },
|
|
48
|
+
"m.l.bias" => { shape: [1], data: [0.0] } } }
|
|
49
|
+
|
|
50
|
+
batch = { x: Torobi::TensorData.nested([[1.0, 2.0], [2.0, 1.0]]),
|
|
51
|
+
y: Torobi::TensorData.nested([[3.0], [3.0]]) }
|
|
52
|
+
|
|
53
|
+
Torobi::Session.open(config, weights:, seed: 1,
|
|
54
|
+
optimizer: { kind: :adamw, lr: 0.05 }) do |s|
|
|
55
|
+
s.repeat(batch, steps: 100)
|
|
56
|
+
puts s.loss
|
|
57
|
+
puts s.fetch("m.l.weight").to_a.inspect
|
|
58
|
+
end
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Fine-tuning a published model
|
|
62
|
+
|
|
63
|
+
The parameter paths are the checkpoint's own, so a published model
|
|
64
|
+
imports with no renaming, and what the file does not hold is named
|
|
65
|
+
rather than guessed:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
config = Torobi::Models::ModernBERT.from_config_file("ruri-v3-130m/config.json")
|
|
69
|
+
student = Torobi::Models::ModernBERT.classifier(config, seq: 128, encoder_prefix: "")
|
|
70
|
+
|
|
71
|
+
Torobi::Session.open(
|
|
72
|
+
Torobi::GraphConfig.new(models: { student: }, objective:),
|
|
73
|
+
pretrained: { student: "ruri-v3-130m/model.safetensors" },
|
|
74
|
+
fresh: ["student.head.*", "student.classifier.*"],
|
|
75
|
+
optimizer: { kind: :adamw, lr: 2e-5 }, seed: 1, io: journal
|
|
76
|
+
) { |s| s.run(batches) }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`experiments/distill_reranker.rb` is that with the rest of an experiment
|
|
80
|
+
around it: a teacher's scores, a validation metric, and a record. It is
|
|
81
|
+
the run described in [docs/plan.md](docs/plan.md) section 15.28, where a
|
|
82
|
+
130M encoder reached nDCG@10 0.7680 against its 310M teacher's 0.7770.
|
|
83
|
+
|
|
84
|
+
## Training a sentence embedder
|
|
85
|
+
|
|
86
|
+
An embedder is the encoder, a mean over the tokens a row actually has,
|
|
87
|
+
and the normalization that makes a dot product a cosine. The contrastive
|
|
88
|
+
loss is not in the model: it reads across the batch, which makes it the
|
|
89
|
+
recipe's. `GradCache` is how the batch gets larger than the machine,
|
|
90
|
+
encoding the parts one at a time and still taking the loss over all of
|
|
91
|
+
them:
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
model = Torobi::Models::ModernBERT.embedder(config, seq: 128, pooling: :mean)
|
|
95
|
+
cache = Torobi::GradCache.new(session, loss: over_vectors,
|
|
96
|
+
tap: "student.embedding", into: :vectors, seed: :seed)
|
|
97
|
+
cache.step(parts) # one optimizer step over every part
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`test/contrastive_test.rb` is the whole of it on a tiny model, held to
|
|
101
|
+
the step the same batch would have taken in one piece.
|
|
102
|
+
|
|
103
|
+
## Asking a model what it produces
|
|
104
|
+
|
|
105
|
+
Training asks what the loss is. Everything else asks what the model
|
|
106
|
+
says, which is a different question and now a first-class one:
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
Torobi::Session.open(
|
|
110
|
+
Torobi::GraphConfig.new(models: { m: embedder }, train: []),
|
|
111
|
+
pretrained: { m: "runs/001/model.safetensors" }
|
|
112
|
+
) do |s|
|
|
113
|
+
s.output_names # => ["m.embedding"]
|
|
114
|
+
s.forward(batch)["m.embedding"] # => TensorData [rows, dim]
|
|
115
|
+
s.forward(batch, outputs: ["m.logits"]) # or just the one
|
|
116
|
+
end
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
It is the pass `evaluate` runs, stopping before the objective: no
|
|
120
|
+
gradients, no randomness, and nothing about the run moves. The batch
|
|
121
|
+
needs what the models read and no more, so a run trained against labels
|
|
122
|
+
does not need them to be asked what it thinks. `train: []` is a run
|
|
123
|
+
opened to be read, and needs no loss at all.
|
|
124
|
+
|
|
125
|
+
What this is not is serving. No HTTP, no tokenizer, no continuous
|
|
126
|
+
batching, no KV cache, no generation loop: those belong to whatever
|
|
127
|
+
serves the model (docs/plan.md section 14).
|
|
128
|
+
|
|
129
|
+
A model can be held in the precision its checkpoint is stored in, which
|
|
130
|
+
halves what it takes:
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
model = Torobi::Models::Llama.causal_lm(config, seq: 512, dtype: :bf16)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The loss is read as f32, so an objective over a bf16 model says where it
|
|
137
|
+
comes back (`g.cast(logits, :f32)`); a config whose loss is not f32 is
|
|
138
|
+
refused when it is built rather than on the first step.
|
|
139
|
+
|
|
140
|
+
## Fine-tuning without moving the model
|
|
141
|
+
|
|
142
|
+
LoRA trains a pair of small matrices beside each weight and leaves the
|
|
143
|
+
weight alone. What is adapted is the fine-tune's decision, so it is a
|
|
144
|
+
keyword rather than a different model description:
|
|
145
|
+
|
|
146
|
+
```ruby
|
|
147
|
+
adapter = Torobi::LoRA.new(rank: 8, alpha: 16, on: %w[q_proj v_proj])
|
|
148
|
+
model = Torobi::Models::Llama.causal_lm(config, seq: 512, adapter:)
|
|
149
|
+
config = Torobi::GraphConfig.new(models: { m: model }, objective:)
|
|
150
|
+
|
|
151
|
+
Torobi::Session.open(config, pretrained: { m: "Qwen2.5-0.5B/model.safetensors" },
|
|
152
|
+
fresh: adapter.fresh(config),
|
|
153
|
+
optimizer: { kind: :adamw, lr: 1e-4 }) { |s| s.run(batches) }
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Inside an adapter nothing else is trainable, so the base is the same
|
|
157
|
+
bytes at the end as at the start. On Qwen2.5-0.5B with rank 8 that
|
|
158
|
+
leaves 0.109% of the parameters being trained.
|
|
159
|
+
|
|
160
|
+
## Keeping the step bounded
|
|
161
|
+
|
|
162
|
+
A step can be capped by the L2 norm of its gradients. The cap is set on
|
|
163
|
+
the optimizer or turned while the run goes, is recorded with the run, and
|
|
164
|
+
`grad_norm` reports what the last step's norm was *before* clipping, so the
|
|
165
|
+
cap reads as a measurement rather than a switch:
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
Torobi::Session.open(config, weights:,
|
|
169
|
+
optimizer: { kind: :adamw, lr: 2e-5, clip: 1.0 }) do |s|
|
|
170
|
+
s.step!(batch)
|
|
171
|
+
s.grad_norm # => the norm this step had, before the cap
|
|
172
|
+
s.adjust(clip: 0.5)
|
|
173
|
+
s.adjust(clip: nil) # no cap again
|
|
174
|
+
end
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
`param_norm` is the other half of that reading: the L2 norm of every
|
|
178
|
+
weight, frozen ones included. It is a reduction over the whole model, so
|
|
179
|
+
it is computed when asked rather than carried with the step.
|
|
180
|
+
|
|
181
|
+
## Running a long one
|
|
182
|
+
|
|
183
|
+
A training run belongs in a process of its own with a cap on what it may
|
|
184
|
+
hold, because MLX draws from the same memory as the rest of the machine
|
|
185
|
+
and a run that asks for more than there is takes the machine with it:
|
|
186
|
+
|
|
187
|
+
```ruby
|
|
188
|
+
runner = Torobi::Runner.new(["ruby", "train.rb"], dir: "runs/001",
|
|
189
|
+
memory_limit: 8 * 1024**3).start
|
|
190
|
+
runner.progress # => {step: 1400, loss: 0.21, at: "..."}
|
|
191
|
+
runner.stop # asks it to stop at the next step boundary
|
|
192
|
+
runner.wait.finished? # => true
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Preprocessing
|
|
196
|
+
|
|
197
|
+
Torobi is handed token ids and does not tokenize. What turns a dataset
|
|
198
|
+
into them lives beside it, in whichever language you have:
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
ruby tools/retrieval_pairs.rb --tokenizer <model>/tokenizer.json \
|
|
202
|
+
--pairs 'data/*.parquet' --rows 100000 --seq 192 --out train.jsonl
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
That needs the `tokenizers` gem and nothing else: the parquet is read by
|
|
206
|
+
`parquet/`, a reader for the part of the format datasets are written in,
|
|
207
|
+
with no dependencies and no extension of its own. `tools/retrieval_pairs.py`
|
|
208
|
+
is the same thing through pyarrow, for the parquet the Ruby one refuses.
|
|
209
|
+
|
|
210
|
+
## Development
|
|
211
|
+
|
|
212
|
+
```
|
|
213
|
+
bundle exec rake # rubocop, the Ruby tests, the engine's Rust tests, engine:check
|
|
214
|
+
bundle exec rake smoke # build the gem, install it somewhere clean, take a step there
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
The Ruby tests build the extension, which links a prebuilt MLX fetched
|
|
218
|
+
once and checked against the digest in `lib/torobi/mlx_prebuilt.json`
|
|
219
|
+
(`rake mlx:pin` moves it). CI runs the same `rake` on a `macos-15`
|
|
220
|
+
runner, on the oldest Ruby the gemspec claims and on the newest.
|
|
221
|
+
|
|
222
|
+
`.rubocop.yml` says which rules are on and why the rest are not. What is
|
|
223
|
+
off is the part that would argue about design; what is on is the part a
|
|
224
|
+
reader cannot be relied on to catch.
|
|
225
|
+
|
|
226
|
+
## Status
|
|
227
|
+
|
|
228
|
+
`Models::Llama` is the Llama-shaped decoder, which is most of them: it
|
|
229
|
+
declares exactly what Qwen2.5-0.5B holds and exactly what
|
|
230
|
+
sarashina2.2-0.5b holds, its gradients agree with its forward, and both
|
|
231
|
+
of their numbers agree with transformers to about a millionth. It also
|
|
232
|
+
builds the scaled rotary embedding Llama 3.1, 3.2 and 3.3 carry, whose
|
|
233
|
+
frequencies are held to the reference arithmetic but not yet to a
|
|
234
|
+
published checkpoint's numbers. What it does not implement it refuses to
|
|
235
|
+
build (the other rope scalings, sliding window attention).
|
|
236
|
+
|
|
237
|
+
`Models::Gemma3` is the other shape: four norms a layer, head-wise
|
|
238
|
+
norms on q and k, `1 + w` scaling, a tanh GELU, and most layers seeing
|
|
239
|
+
only a window of the recent past. It declares exactly what gemma-3-270m
|
|
240
|
+
holds, and its numbers agree with transformers as closely as the family
|
|
241
|
+
above does.
|
|
242
|
+
|
|
243
|
+
The version is 0.1.0 and the API still moves.
|
|
244
|
+
|
|
245
|
+
## License
|
|
246
|
+
|
|
247
|
+
[MIT](./LICENSE).
|
|
248
|
+
|
|
249
|
+
The engine builds against MLX (MIT, ml-explore) through mlx-c (MIT,
|
|
250
|
+
ml-explore), which it binds itself; the binding follows mlx-rs (MIT or
|
|
251
|
+
Apache-2.0) as its reference.
|
|
252
|
+
|
|
253
|
+
The `arm64-darwin` platform gem links MLX's compiled code into
|
|
254
|
+
`torobi.bundle`, so it redistributes it: the notices for MLX, mlx-c,
|
|
255
|
+
gguflib and mlx-rs travel in [`licenses/`](./licenses). A source install
|
|
256
|
+
fetches the pre-built MLX, mlx-c included, from `takahashim/mlx-prebuilt`
|
|
257
|
+
and links it on your machine, so the package you download carries no MLX
|
|
258
|
+
bytes. `docs/vendoring.md` names all of them, with their licences and with
|
|
259
|
+
what a redistributor owes.
|
data/config/ops.yml
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# The single source of truth for the IR's operation vocabulary.
|
|
2
|
+
#
|
|
3
|
+
# Each entry declares: how many value inputs the op takes (an integer, or
|
|
4
|
+
# [min, max]), how many parameters it references, its attributes and their
|
|
5
|
+
# types, the shape rule that infers its output, and where the DSL surfaces
|
|
6
|
+
# it ("handle" ops become methods on node handles; everything else is either
|
|
7
|
+
# an operator, a builder on `g`, or internal to a lowering).
|
|
8
|
+
#
|
|
9
|
+
# Semantic ops (sdpa, rope, layer_norm, rms_norm) are kept whole: a backend
|
|
10
|
+
# may run a fused kernel or decompose them, but the IR does not lower them.
|
|
11
|
+
|
|
12
|
+
- name: parameter
|
|
13
|
+
inputs: 0
|
|
14
|
+
params: 1
|
|
15
|
+
attributes: {}
|
|
16
|
+
shape_rule: parameter
|
|
17
|
+
|
|
18
|
+
- name: add
|
|
19
|
+
inputs: 2
|
|
20
|
+
shape_rule: broadcast
|
|
21
|
+
- name: sub
|
|
22
|
+
inputs: 2
|
|
23
|
+
shape_rule: broadcast
|
|
24
|
+
- name: mul
|
|
25
|
+
inputs: 2
|
|
26
|
+
shape_rule: broadcast
|
|
27
|
+
- name: div
|
|
28
|
+
inputs: 2
|
|
29
|
+
shape_rule: broadcast
|
|
30
|
+
|
|
31
|
+
- name: add_scalar
|
|
32
|
+
inputs: 1
|
|
33
|
+
attributes: { value: number }
|
|
34
|
+
shape_rule: same_as_input
|
|
35
|
+
- name: sub_scalar
|
|
36
|
+
inputs: 1
|
|
37
|
+
attributes: { value: number }
|
|
38
|
+
shape_rule: same_as_input
|
|
39
|
+
- name: mul_scalar
|
|
40
|
+
inputs: 1
|
|
41
|
+
attributes: { value: number }
|
|
42
|
+
shape_rule: same_as_input
|
|
43
|
+
- name: div_scalar
|
|
44
|
+
inputs: 1
|
|
45
|
+
attributes: { value: number }
|
|
46
|
+
shape_rule: same_as_input
|
|
47
|
+
# The number divided by the value: what `2.0 / x` is. Its own op because
|
|
48
|
+
# the number cannot be made a graph value of x's shape from x without
|
|
49
|
+
# multiplying x by zero, which is NaN where x is infinite.
|
|
50
|
+
- name: rdiv_scalar
|
|
51
|
+
inputs: 1
|
|
52
|
+
attributes: { value: number }
|
|
53
|
+
shape_rule: same_as_input
|
|
54
|
+
|
|
55
|
+
- name: neg
|
|
56
|
+
inputs: 1
|
|
57
|
+
shape_rule: same_as_input
|
|
58
|
+
handle: true
|
|
59
|
+
- name: abs
|
|
60
|
+
inputs: 1
|
|
61
|
+
shape_rule: same_as_input
|
|
62
|
+
handle: true
|
|
63
|
+
- name: sqrt
|
|
64
|
+
inputs: 1
|
|
65
|
+
shape_rule: same_as_input
|
|
66
|
+
handle: true
|
|
67
|
+
- name: square
|
|
68
|
+
inputs: 1
|
|
69
|
+
shape_rule: same_as_input
|
|
70
|
+
handle: true
|
|
71
|
+
- name: exp
|
|
72
|
+
inputs: 1
|
|
73
|
+
shape_rule: same_as_input
|
|
74
|
+
handle: true
|
|
75
|
+
- name: log
|
|
76
|
+
inputs: 1
|
|
77
|
+
shape_rule: same_as_input
|
|
78
|
+
handle: true
|
|
79
|
+
# The exact form, 0.5x(1 + erf(x/sqrt2)), not the tanh approximation: the
|
|
80
|
+
# reference implementations of the models this has to match use erf, and a
|
|
81
|
+
# parity check against them is the point (docs/plan.md 9.2).
|
|
82
|
+
- name: gelu
|
|
83
|
+
inputs: 1
|
|
84
|
+
shape_rule: same_as_input
|
|
85
|
+
handle: true
|
|
86
|
+
# The tanh approximation, which is a different function from the one
|
|
87
|
+
# above and not a faster spelling of it. Gemma is trained with it
|
|
88
|
+
# (`hidden_activation: gelu_pytorch_tanh`), so a graph that used the
|
|
89
|
+
# exact form would be a model that runs and is not the published one.
|
|
90
|
+
- name: gelu_tanh
|
|
91
|
+
inputs: 1
|
|
92
|
+
shape_rule: same_as_input
|
|
93
|
+
handle: true
|
|
94
|
+
- name: relu
|
|
95
|
+
inputs: 1
|
|
96
|
+
shape_rule: same_as_input
|
|
97
|
+
handle: true
|
|
98
|
+
- name: sigmoid
|
|
99
|
+
inputs: 1
|
|
100
|
+
shape_rule: same_as_input
|
|
101
|
+
handle: true
|
|
102
|
+
- name: tanh
|
|
103
|
+
inputs: 1
|
|
104
|
+
shape_rule: same_as_input
|
|
105
|
+
handle: true
|
|
106
|
+
|
|
107
|
+
# The only op that consumes randomness. Its key comes from the session's
|
|
108
|
+
# RNG state, never from a global: that is what makes a resumed run take the
|
|
109
|
+
# same masks a continuous one would (docs/plan.md section 11.1).
|
|
110
|
+
- name: dropout
|
|
111
|
+
inputs: 1
|
|
112
|
+
attributes: { p: number }
|
|
113
|
+
shape_rule: same_as_input
|
|
114
|
+
handle: true
|
|
115
|
+
|
|
116
|
+
# Semantic op, kept whole: the backend decides how to realize it, and the
|
|
117
|
+
# IR says what it means. Gradients do not flow through it.
|
|
118
|
+
- name: stop_gradient
|
|
119
|
+
inputs: 1
|
|
120
|
+
shape_rule: same_as_input
|
|
121
|
+
handle: true
|
|
122
|
+
|
|
123
|
+
- name: softmax
|
|
124
|
+
inputs: 1
|
|
125
|
+
attributes: { axis: int }
|
|
126
|
+
shape_rule: same_as_input
|
|
127
|
+
handle: true
|
|
128
|
+
# `freqs` is what a scaled rope says instead: one angular frequency per
|
|
129
|
+
# pair of dimensions, so position p turns by `p * freqs[i]` rather than by
|
|
130
|
+
# `p / theta^(2i/width)`. Which frequencies those are is the model's
|
|
131
|
+
# business and not the engine's -- Llama 3 stretches the low ones and
|
|
132
|
+
# leaves the high ones alone (`Models::Llama`) -- so what crosses the
|
|
133
|
+
# boundary is the numbers rather than the name of a formula. Left out
|
|
134
|
+
# when the base alone says it, which is what keeps the digest of every
|
|
135
|
+
# graph built before this the same.
|
|
136
|
+
- name: rope
|
|
137
|
+
inputs: 1
|
|
138
|
+
attributes: { theta: number, freqs: number_list_or_null }
|
|
139
|
+
shape_rule: same_as_input
|
|
140
|
+
handle: true
|
|
141
|
+
|
|
142
|
+
- name: transpose
|
|
143
|
+
inputs: 1
|
|
144
|
+
attributes: { axes: int_list }
|
|
145
|
+
shape_rule: transpose
|
|
146
|
+
handle: true
|
|
147
|
+
# Multi-head attention is the reason this exists: [batch, seq, heads*head]
|
|
148
|
+
# has to become [batch, seq, heads, head] before the transpose that puts
|
|
149
|
+
# heads in front. One dimension may be -1, and it takes whatever is left,
|
|
150
|
+
# including a symbolic batch.
|
|
151
|
+
- name: reshape
|
|
152
|
+
inputs: 1
|
|
153
|
+
attributes: { shape: int_list }
|
|
154
|
+
shape_rule: reshape
|
|
155
|
+
handle: true
|
|
156
|
+
- name: slice
|
|
157
|
+
inputs: 1
|
|
158
|
+
attributes: { axis: int, start: int, length: int }
|
|
159
|
+
shape_rule: slice
|
|
160
|
+
handle: true
|
|
161
|
+
- name: mean
|
|
162
|
+
inputs: 1
|
|
163
|
+
attributes: { axes: int_list_or_null, keepdims: bool }
|
|
164
|
+
shape_rule: reduce
|
|
165
|
+
- name: sum
|
|
166
|
+
inputs: 1
|
|
167
|
+
attributes: { axes: int_list_or_null, keepdims: bool }
|
|
168
|
+
shape_rule: reduce
|
|
169
|
+
# The largest, which a stable log-softmax subtracts before it exponentiates:
|
|
170
|
+
# at the sizes a vocabulary has, exp of a raw logit overflows.
|
|
171
|
+
- name: max
|
|
172
|
+
inputs: 1
|
|
173
|
+
attributes: { axes: int_list_or_null, keepdims: bool }
|
|
174
|
+
shape_rule: reduce
|
|
175
|
+
|
|
176
|
+
# The one op that changes a dtype rather than a shape.
|
|
177
|
+
#
|
|
178
|
+
# What it is for: a model held in bf16 is half the memory of the same
|
|
179
|
+
# model in f32, and the boundary reads a loss as f32 (docs/plan.md section
|
|
180
|
+
# 15.51). The cast is where those two meet, and it is written down rather
|
|
181
|
+
# than inserted, because a precision change nobody asked for is how a
|
|
182
|
+
# training run quietly stops matching its reference.
|
|
183
|
+
- name: cast
|
|
184
|
+
inputs: 1
|
|
185
|
+
attributes: { dtype: dtype }
|
|
186
|
+
shape_rule: cast
|
|
187
|
+
|
|
188
|
+
- name: matmul
|
|
189
|
+
inputs: 2
|
|
190
|
+
shape_rule: matmul
|
|
191
|
+
# The matmul of a weight held quantized: x @ dequantize(weight)ᵀ, read
|
|
192
|
+
# packed (docs/qlora.md). Inputs are x, the packed u32 weight, the scales
|
|
193
|
+
# and, for the affine mode, the biases. The attributes say how the weight
|
|
194
|
+
# was quantized, since the arrays themselves do not; the weight is always
|
|
195
|
+
# [d_out, d_in] as a linear holds it, so the transpose is not an option.
|
|
196
|
+
- name: quantized_matmul
|
|
197
|
+
inputs: [3, 4]
|
|
198
|
+
attributes: { group_size: int, bits: int, mode: string }
|
|
199
|
+
shape_rule: quantized_matmul
|
|
200
|
+
|
|
201
|
+
- name: take
|
|
202
|
+
inputs: 2
|
|
203
|
+
shape_rule: take
|
|
204
|
+
|
|
205
|
+
- name: layer_norm
|
|
206
|
+
inputs: [2, 3]
|
|
207
|
+
attributes: { eps: number }
|
|
208
|
+
shape_rule: same_as_input
|
|
209
|
+
- name: rms_norm
|
|
210
|
+
inputs: 2
|
|
211
|
+
attributes: { eps: number }
|
|
212
|
+
shape_rule: same_as_input
|
|
213
|
+
# `causal` is the mask a decoder needs, and is an attribute rather than a
|
|
214
|
+
# batch field because it is a fact about the op and not about the data: a
|
|
215
|
+
# [1, 1, seq, seq] triangle handed over every step is megabytes of the same
|
|
216
|
+
# number, and the backend has the mode built in.
|
|
217
|
+
- name: sdpa
|
|
218
|
+
inputs: [3, 4]
|
|
219
|
+
attributes: { scale: number_or_null, causal: bool }
|
|
220
|
+
shape_rule: sdpa
|
|
221
|
+
|
|
222
|
+
# Semantic op, kept whole for the reason 6.1 gives: what is dangerous about
|
|
223
|
+
# a cross entropy is its numerics (`log(sum(exp))` over a vocabulary
|
|
224
|
+
# overflows unless the largest is taken out first) and its backward, which
|
|
225
|
+
# should not build a second [rows, vocabulary] to get there. It answers one
|
|
226
|
+
# number per position and reduces nothing: which positions count, and how
|
|
227
|
+
# they are weighed, is the objective's to say.
|
|
228
|
+
- name: cross_entropy
|
|
229
|
+
inputs: 2
|
|
230
|
+
shape_rule: cross_entropy
|