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/engine/src/init.rs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//! Building a parameter from what the graph says it should start as.
|
|
2
|
+
//!
|
|
3
|
+
//! A GraphConfig declares an initializer per parameter, and until now
|
|
4
|
+
//! nobody read it: every run was handed its parameters. Fine-tuning is
|
|
5
|
+
//! what changes that. A classification head put on top of a published
|
|
6
|
+
//! encoder exists in no checkpoint, so it has to come from its
|
|
7
|
+
//! declaration, while everything under it comes from the file.
|
|
8
|
+
//!
|
|
9
|
+
//! Drawn from the run's seed, not from a global. `docs/plan.md` section
|
|
10
|
+
//! 11.1 lists "parameter 初期化 seed" among the state a reproducible run
|
|
11
|
+
//! must manage explicitly, and this is where that begins: the same seed
|
|
12
|
+
//! and the same graph give the same starting parameters, on any machine.
|
|
13
|
+
|
|
14
|
+
use anyhow::{Context, Result};
|
|
15
|
+
use crate::mlxc::Array;
|
|
16
|
+
use serde_json::Value;
|
|
17
|
+
|
|
18
|
+
use crate::graph::ParameterSpec;
|
|
19
|
+
|
|
20
|
+
/// One parameter, as its declaration says it should start.
|
|
21
|
+
///
|
|
22
|
+
/// `key` is this parameter's own, split from the run's by the caller, so
|
|
23
|
+
/// that what one parameter draws does not depend on how many were built
|
|
24
|
+
/// before it.
|
|
25
|
+
pub fn build(spec: &ParameterSpec, key: &Array) -> Result<Array> {
|
|
26
|
+
let kind = spec
|
|
27
|
+
.initializer
|
|
28
|
+
.get("type")
|
|
29
|
+
.and_then(Value::as_str)
|
|
30
|
+
.with_context(|| format!("parameter {:?}: its initializer has no type", spec.path))?;
|
|
31
|
+
let shape = &spec.shape;
|
|
32
|
+
|
|
33
|
+
Ok(match kind {
|
|
34
|
+
"zeros" => Array::zeros::<f32>(shape)?,
|
|
35
|
+
"ones" => Array::ones::<f32>(shape)?,
|
|
36
|
+
"normal" => {
|
|
37
|
+
let std = number(spec, "std").unwrap_or(0.02);
|
|
38
|
+
crate::mlxc::random::normal::<f32>(shape, 0.0, std, key)?
|
|
39
|
+
}
|
|
40
|
+
// Kaiming uniform as PyTorch's `Linear` uses it: bound
|
|
41
|
+
// sqrt(6 / fan_in) with the default gain, where fan_in is the
|
|
42
|
+
// last dimension of a [out, in] weight.
|
|
43
|
+
"kaiming_uniform" => {
|
|
44
|
+
let fan_in = *shape.last().with_context(|| {
|
|
45
|
+
format!("parameter {:?}: kaiming_uniform wants a shape", spec.path)
|
|
46
|
+
})? as f32;
|
|
47
|
+
anyhow::ensure!(
|
|
48
|
+
fan_in > 0.0,
|
|
49
|
+
"parameter {:?}: kaiming_uniform wants a positive last dimension",
|
|
50
|
+
spec.path
|
|
51
|
+
);
|
|
52
|
+
let bound = (6.0f32 / fan_in).sqrt();
|
|
53
|
+
crate::mlxc::random::uniform::<f32>(-bound, bound, shape, key)?
|
|
54
|
+
}
|
|
55
|
+
other => anyhow::bail!(
|
|
56
|
+
"parameter {:?}: initializer {other:?} is not one this engine builds \
|
|
57
|
+
(zeros, ones, normal, kaiming_uniform)",
|
|
58
|
+
spec.path
|
|
59
|
+
),
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fn number(spec: &ParameterSpec, key: &str) -> Option<f32> {
|
|
64
|
+
spec.initializer.get(key).and_then(Value::as_f64).map(|v| v as f32)
|
|
65
|
+
}
|