-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathllm.rs
More file actions
323 lines (257 loc) · 9.96 KB
/
llm.rs
File metadata and controls
323 lines (257 loc) · 9.96 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use std::cmp::Ordering;
use ndarray::{Array1, Array2, Axis};
use crate::{
EMBEDDING_DIM, Embeddings, HIDDEN_DIM, MAX_SEQ_LEN, Vocab, output_projection::OutputProjection,
transformer::TransformerBlock,
};
pub trait Layer {
fn layer_type(&self) -> &str;
fn forward(&mut self, input: &Array2<f32>) -> Array2<f32>;
fn backward(&mut self, grads: &Array2<f32>, lr: f32) -> Array2<f32>;
fn parameters(&self) -> usize;
}
#[allow(clippy::upper_case_acronyms)]
pub struct LLM {
pub vocab: Vocab,
pub network: Vec<Box<dyn Layer>>,
}
impl Default for LLM {
fn default() -> Self {
let transformer_block = TransformerBlock::new(EMBEDDING_DIM, HIDDEN_DIM);
let output_projection = OutputProjection::new(EMBEDDING_DIM, Vocab::default_words().len());
Self {
vocab: Vocab::default(),
network: vec![
Box::new(Embeddings::default()),
Box::new(transformer_block),
Box::new(output_projection),
],
}
}
}
impl LLM {
pub fn new(vocab: Vocab, network: Vec<Box<dyn Layer>>) -> Self {
Self { vocab, network }
}
}
impl LLM {
pub fn network_description(&self) -> String {
self.network
.iter()
.map(|layer| layer.layer_type())
.collect::<Vec<&str>>()
.join(", ")
}
pub fn total_parameters(&self) -> usize {
// Sum the parameters across all layers in the network
self.network
.iter()
.map(|layer| layer.parameters())
.sum::<usize>()
}
pub fn predict(&mut self, text: &str) -> String {
let output_tokens = self.forward(text);
// Handle empty output
if output_tokens.is_empty() {
return String::new();
}
// Convert token_ids to strings
let token_strs = output_tokens
.iter()
.map(|t| self.vocab.decode[t].clone())
.collect::<Vec<String>>();
token_strs.join(" ")
}
fn forward(&mut self, text: &str) -> Vec<usize> {
// Tokenize the input text
let mut tokenized = self.tokenize(text);
let mut output_tokens: Vec<usize> = Vec::new();
// Safety check: ensure we have at least one token
if tokenized.is_empty() {
return output_tokens;
}
let input_len = tokenized.len();
// Prevent overflow if input_len >= MAX_SEQ_LEN
if input_len >= MAX_SEQ_LEN {
return output_tokens;
}
for _ in 0..(MAX_SEQ_LEN - input_len) {
// let tokenized_clone = tokenized.clone();
// Check if we're approaching the maximum sequence length
if output_tokens.len() >= MAX_SEQ_LEN - 1 {
break;
}
let token_input = Array2::from_shape_vec(
(1, tokenized.len()),
tokenized.iter().map(|&x| x as f32).collect(),
)
.unwrap();
let mut input = token_input;
for layer in &mut self.network {
input = layer.forward(&input);
}
let logits = input;
// Safety check: ensure we have at least one token
if logits.shape()[0] == 0 {
break;
}
let last_logit = logits
.row(logits.shape()[0] - 1)
.to_owned()
.insert_axis(Axis(0));
// Softmax - convert activations of each token to a probability distribution over the
// vocabulary
let probs = Self::softmax(&last_logit); // 1 x vocab_size
// Greedy Decode - Choose the highest probability token for each position
let tokens = Self::greedy_decode(&probs);
let next_token = tokens[tokens.len() - 1];
output_tokens.push(next_token);
tokenized.push(next_token);
if next_token == self.vocab.encode("</s>").unwrap() {
break;
}
}
output_tokens
}
pub fn train(&mut self, data: Vec<&str>, epochs: usize, lr: f32) {
let tokenized_data = data
.iter()
.map(|input| self.tokenize(input))
.collect::<Vec<Vec<usize>>>();
for epoch in 0..epochs {
let mut total_loss = 0.0;
for training_row in &tokenized_data {
if training_row.len() < 2 {
continue;
}
// 1. Slice input and targets
let input_ids = &training_row[..training_row.len() - 1]; // Exclude the last token
let target_ids = &training_row[1..]; // This is a vector. Each element is the index in the vocab.
// Forward pass
let mut input: Array2<f32> = Array2::zeros((1, input_ids.len()));
input
.row_mut(0)
.assign(&input_ids.iter().map(|&x| x as f32).collect::<Array1<f32>>());
for layer in &mut self.network {
input = layer.forward(&input);
}
let logits = input;
let probs = Self::softmax(&logits);
total_loss += Self::cross_entropy_loss_step(&probs, target_ids);
// Backward pass
let mut grads_output = Self::compute_gradients_step(&probs, target_ids); // this is d_L/d_output_projection
// Apply gradient clipping BEFORE backpropagation
Self::clip_gradients(&mut grads_output, 5.0);
for layer in self.network.iter_mut().rev() {
grads_output = layer.backward(&grads_output, lr);
}
let tokens = Self::greedy_decode(&probs);
let next_token = tokens[tokens.len() - 1];
if next_token == self.vocab.encode("</s>").unwrap() {
continue;
}
}
println!(
"Epoch {}: Loss = {:.4}",
epoch,
total_loss / tokenized_data.len() as f32
);
}
}
pub fn tokenize(&self, text: &str) -> Vec<usize> {
// Split by whitespace first
let mut tokens = Vec::new();
for word in text.split_whitespace() {
// Special case for end token
if word == "</s>" {
if let Some(token_id) = self.vocab.encode(word) {
tokens.push(token_id);
}
continue;
}
let mut current_word = String::new();
for c in word.chars() {
if c.is_ascii_punctuation() {
// If we have a word before the punctuation, add it
if !current_word.is_empty() {
if let Some(token_id) = self.vocab.encode(¤t_word) {
tokens.push(token_id);
}
current_word.clear();
}
// Add the punctuation as its own token
if let Some(token_id) = self.vocab.encode(&c.to_string()) {
tokens.push(token_id);
}
} else {
current_word.push(c);
}
}
// Add any remaining word
if !current_word.is_empty()
&& let Some(token_id) = self.vocab.encode(¤t_word)
{
tokens.push(token_id);
}
}
tokens
}
fn softmax(logits: &Array2<f32>) -> Array2<f32> {
// logits is seq_len x vocab_size
let mut result = logits.clone();
// Apply softmax row-wise
for mut row in result.rows_mut() {
// Calculate exp for each element
let max_val = row.iter().copied().fold(f32::NEG_INFINITY, f32::max);
let exp_values: Vec<f32> = row.iter().map(|&x| (x - max_val).exp()).collect();
let sum_exp: f32 = exp_values.iter().sum();
// Normalize by sum
for (i, &exp_val) in exp_values.iter().enumerate() {
row[i] = exp_val / sum_exp;
}
}
result
}
fn greedy_decode(probs: &Array2<f32>) -> Vec<usize> {
probs
.map_axis(Axis(1), |row| {
row.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(Ordering::Equal))
.map(|(index, _)| index)
.unwrap()
})
.to_vec()
}
fn cross_entropy_loss_step(probs: &Array2<f32>, target: &[usize]) -> f32 {
let mut loss = 0.0;
for row_idx in 0..probs.shape()[0] {
let prob_target = probs[[row_idx, target[row_idx]]]; // Get probability of correct token
loss -= prob_target.max(1e-15).ln(); // Add numerical stability
}
loss / target.len() as f32
}
fn compute_gradients_step(probs: &Array2<f32>, target: &[usize]) -> Array2<f32> {
let mut grads = probs.clone(); // Start with softmax probabilities
if probs.shape()[0] != target.len() {
panic!("Probs and target must have the same number of rows");
}
let batch_size = target.len() as f32;
// Compute correct softmax + cross-entropy gradient: softmax - one_hot(target)
for row_idx in 0..grads.shape()[0] {
grads[[row_idx, target[row_idx]]] -= 1.0; // Convert to: p - y (where y is one-hot)
}
// Normalize by batch size for stable training
grads.mapv_inplace(|x| x / batch_size);
grads
}
fn clip_gradients(grads: &mut Array2<f32>, max_norm: f32) {
// Calculate L2 norm of gradients
let norm = grads.iter().map(|&x| x * x).sum::<f32>().sqrt();
// If norm exceeds max_norm, scale gradients down
if norm > max_norm {
let scale = max_norm / norm;
grads.mapv_inplace(|x| x * scale);
}
}
}