1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! A deep-learning library in Rust, from a two-layer XOR network up to a
//! 300M-parameter transformer language model.
//!
//! There are two model types, and they share the matrix, optimizer, and
//! serialization code underneath:
//!
//! - [`Network`] — a dense feed-forward network for tabular regression and
//! classification. Built with [`Network::builder`], trained with
//! [`Network::fit`].
//! - [`TransformerLm`] — a decoder-only language model with grouped-query
//! attention, rotary positions, RMSNorm, SwiGLU feed-forwards, and optional
//! sparse mixture-of-experts layers. Built with [`TransformerLm::builder`],
//! trained one batch at a time with [`TransformerLm::train_step`].
//!
//! # A language model in twenty lines
//!
//! ```no_run
//! use rusting_brain::{Optimizer, TransformerLm};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let mut model = TransformerLm::builder()
//! .vocab_size(32_000)
//! .d_model(512)
//! .n_layers(8)
//! .heads(8, 2, 64) // 8 query heads, 2 key/value heads (GQA)
//! .d_ff(1408)
//! .experts(8, 2) // 8 experts, 2 active per token
//! .moe_layers(2..8) // layers 0-1 stay dense
//! .max_seq_len(1024)
//! .optimizer(Optimizer::adam(3e-4))
//! .seed(42)
//! .build()?;
//!
//! println!("{}", model.parameter_counts()); // total vs. active
//!
//! let batch: Vec<Vec<u32>> = vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]];
//! let loss = model.train_step(&batch)?;
//! println!("loss {:.4}", loss.lm_loss);
//! # Ok(())
//! # }
//! ```
//!
//! # Generating text
//!
//! [`TransformerLm::forward_cached`] appends to a per-layer [`KvCache`], so
//! decoding the *n*-th token costs one row of attention instead of *n*:
//!
//! ```no_run
//! # use rusting_brain::TransformerLm;
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let model = TransformerLm::builder().build()?;
//! # let prompt = [1u32, 2, 3];
//! let mut caches = model.new_kv_caches();
//! let mut logits = model.forward_cached(&prompt, &mut caches)?; // prefill
//!
//! for _ in 0..50 {
//! let next = argmax(logits.row(logits.rows - 1));
//! logits = model.forward_cached(&[next], &mut caches)?; // decode
//! }
//! # fn argmax(row: &[f32]) -> u32 { 0 }
//! # Ok(())
//! # }
//! ```
//!
//! # Running on a GPU
//!
//! CUDA is an optional feature and is off by default; the crate builds and the
//! tests pass without a driver, a toolkit, or a device.
//!
//! ```bash
//! cargo add rusting_brain --features cuda
//! ```
//!
//! [`TransformerLm::to_cuda`] moves the parameters onto a device and every
//! later `train_step`, `forward_batch`, and `backward` runs there. The device
//! path is fail-closed: if the driver, cuBLAS, a kernel, an allocation, or a
//! numerical check fails, the call returns [`NetworkError`] rather than
//! silently falling back to the CPU.
//!
//! ```no_run
//! # use rusting_brain::TransformerLm;
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let mut model = TransformerLm::builder().build()?;
//! # #[cfg(feature = "cuda")] {
//! model.set_mixed_precision(true); // BF16 GEMMs, FP32 master weights
//! model.to_cuda(0, 9_000)?; // device 0, 9000 MiB budget
//! # }
//! # Ok(())
//! # }
//! ```
//!
//! Dense [`Network`]s use a separate device path,
//! [`TrainingBackend::Cuda`], or [`metal_training`] on Apple Silicon.
//!
//! # Where to read next
//!
//! - `tutorials/` — a sixteen-chapter course, from what a neural network is to
//! training a language model end to end.
//! - `docs/baseline.md` — measured throughput per architecture on an RTX 3060.
//! - `IMPORT_MODELS.md` — running ONNX models exported from TensorFlow or
//! PyTorch.
pub
pub
pub
pub use ;
pub use Activation;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Embedding;
pub use ;
pub use GpuContext;
pub use Loss;
pub use Matrix;
pub use ;
pub use ;
pub use ;
pub use RmsNorm;
pub use Optimizer;
pub use ;
pub use Rope;
pub use ;
pub use ;