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.
Files changed (101) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +71 -0
  3. data/Cargo.lock +539 -0
  4. data/Cargo.toml +6 -0
  5. data/LICENSE +21 -0
  6. data/README.md +259 -0
  7. data/config/ops.yml +230 -0
  8. data/docs/plan.md +3982 -0
  9. data/docs/vendoring.md +656 -0
  10. data/engine/Cargo.toml +35 -0
  11. data/engine/build.rs +253 -0
  12. data/engine/src/checkpoint.rs +513 -0
  13. data/engine/src/executor.rs +257 -0
  14. data/engine/src/export.rs +431 -0
  15. data/engine/src/fixtures.rs +518 -0
  16. data/engine/src/graph.rs +115 -0
  17. data/engine/src/init.rs +65 -0
  18. data/engine/src/interp.rs +985 -0
  19. data/engine/src/lib.rs +62 -0
  20. data/engine/src/main.rs +97 -0
  21. data/engine/src/memory.rs +109 -0
  22. data/engine/src/mlxc/array.rs +454 -0
  23. data/engine/src/mlxc/error.rs +205 -0
  24. data/engine/src/mlxc/fast.rs +46 -0
  25. data/engine/src/mlxc/handle.rs +155 -0
  26. data/engine/src/mlxc/io.rs +268 -0
  27. data/engine/src/mlxc/memory.rs +56 -0
  28. data/engine/src/mlxc/metal.rs +24 -0
  29. data/engine/src/mlxc/mod.rs +55 -0
  30. data/engine/src/mlxc/nn.rs +38 -0
  31. data/engine/src/mlxc/ops.rs +477 -0
  32. data/engine/src/mlxc/quantized.rs +405 -0
  33. data/engine/src/mlxc/random.rs +161 -0
  34. data/engine/src/mlxc/transforms.rs +439 -0
  35. data/engine/src/op.rs +745 -0
  36. data/engine/src/optimizer.rs +650 -0
  37. data/engine/src/plan.rs +1638 -0
  38. data/engine/src/runtime.rs +377 -0
  39. data/engine/src/session.rs +1523 -0
  40. data/engine/src/state.rs +1007 -0
  41. data/engine/src/tensor.rs +331 -0
  42. data/ext/torobi/Cargo.toml +21 -0
  43. data/ext/torobi/extconf.rb +42 -0
  44. data/ext/torobi/src/gvl.rs +182 -0
  45. data/ext/torobi/src/lib.rs +1037 -0
  46. data/lib/torobi/adapter_file.rb +184 -0
  47. data/lib/torobi/batch.rb +55 -0
  48. data/lib/torobi/checkpoint.rb +60 -0
  49. data/lib/torobi/dsl/builder.rb +414 -0
  50. data/lib/torobi/dsl/handle.rb +156 -0
  51. data/lib/torobi/dsl/layers.rb +82 -0
  52. data/lib/torobi/dsl.rb +35 -0
  53. data/lib/torobi/errors.rb +88 -0
  54. data/lib/torobi/export.rb +534 -0
  55. data/lib/torobi/freeze.rb +24 -0
  56. data/lib/torobi/freshness.rb +57 -0
  57. data/lib/torobi/grad_cache.rb +143 -0
  58. data/lib/torobi/graph_config.rb +301 -0
  59. data/lib/torobi/hooks.rb +439 -0
  60. data/lib/torobi/ir/dimensions.rb +26 -0
  61. data/lib/torobi/ir/dtype.rb +28 -0
  62. data/lib/torobi/ir/graph/check.rb +116 -0
  63. data/lib/torobi/ir/graph.rb +52 -0
  64. data/lib/torobi/ir/input_spec.rb +39 -0
  65. data/lib/torobi/ir/json.rb +45 -0
  66. data/lib/torobi/ir/node_spec.rb +52 -0
  67. data/lib/torobi/ir/parameter_spec.rb +67 -0
  68. data/lib/torobi/ir/ref.rb +52 -0
  69. data/lib/torobi/ir/source.rb +65 -0
  70. data/lib/torobi/ir.rb +23 -0
  71. data/lib/torobi/journal.rb +310 -0
  72. data/lib/torobi/lora.rb +190 -0
  73. data/lib/torobi/memory.rb +50 -0
  74. data/lib/torobi/metallib.rb +50 -0
  75. data/lib/torobi/mlx_prebuilt.json +24 -0
  76. data/lib/torobi/mlx_prebuilt.rb +378 -0
  77. data/lib/torobi/models/build.rb +37 -0
  78. data/lib/torobi/models/description.rb +59 -0
  79. data/lib/torobi/models/gemma3.rb +273 -0
  80. data/lib/torobi/models/llama.rb +381 -0
  81. data/lib/torobi/models/modern_bert.rb +513 -0
  82. data/lib/torobi/models/padding.rb +27 -0
  83. data/lib/torobi/models/windows.rb +53 -0
  84. data/lib/torobi/ops.rb +81 -0
  85. data/lib/torobi/preflight.rb +136 -0
  86. data/lib/torobi/provenance.rb +75 -0
  87. data/lib/torobi/quantization.rb +151 -0
  88. data/lib/torobi/replay.rb +229 -0
  89. data/lib/torobi/runner.rb +419 -0
  90. data/lib/torobi/session.rb +855 -0
  91. data/lib/torobi/shape.rb +438 -0
  92. data/lib/torobi/tensor_data.rb +178 -0
  93. data/lib/torobi/version.rb +5 -0
  94. data/lib/torobi/weights.rb +108 -0
  95. data/lib/torobi.rb +67 -0
  96. data/licenses/MLX.txt +21 -0
  97. data/licenses/README.md +16 -0
  98. data/licenses/gguflib.txt +21 -0
  99. data/licenses/mlx-c.txt +21 -0
  100. data/licenses/mlx-rs.txt +27 -0
  101. metadata +229 -0
@@ -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
+ }