god-graph 0.6.0-alpha

A graph-based LLM white-box optimization toolbox: topology validation, Lie group orthogonalization, tensor ring compression
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Compute graph for tracking operations during forward pass

use std::collections::HashMap;
use crate::tensor::DenseTensor;
use crate::tensor::traits::{TensorBase, TensorOps};

/// Unique identifier for an operation node
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OpId(pub usize);

/// Unique identifier for a tensor
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TensorId(pub usize);

/// Reference to an operation
#[derive(Debug, Clone)]
pub struct OpRef {
    /// Operation ID
    pub id: OpId,
    /// Operation type
    pub op_type: OpType,
    /// Input tensor IDs
    pub inputs: Vec<TensorId>,
    /// Output tensor ID
    pub output: TensorId,
}

/// Operation type
#[derive(Debug, Clone)]
pub enum OpType {
    /// Element-wise addition
    Add,
    /// Element-wise subtraction
    Sub,
    /// Element-wise multiplication
    Mul,
    /// Element-wise division
    Div,
    /// Matrix multiplication
    MatMul,
    /// Transpose operation
    Transpose,
    /// Sum reduction
    Sum,
    /// Mean reduction
    Mean,
    /// ReLU activation
    ReLU,
    /// GELU activation
    GELU,
    /// Sigmoid activation
    Sigmoid,
    /// Tanh activation
    Tanh,
    /// SiLU/Swish activation
    SiLU,
    /// Softmax activation
    Softmax,
    /// Layer normalization
    LayerNorm,
    /// RMS normalization
    RMSNorm,
    /// Linear/fully connected layer
    Linear,
    /// Embedding lookup
    Embedding,
    /// Rotary position embedding
    RoPE,
    /// Scaled dot-product attention
    ScaledDotProduct,
}

/// Operation node in the compute graph
#[derive(Debug, Clone)]
pub struct OpNode {
    /// Operation ID
    pub id: OpId,
    /// Operation type
    pub op_type: OpType,
    /// Input tensor IDs
    pub inputs: Vec<TensorId>,
    /// Output tensor ID
    pub output: TensorId,
}

/// Data edge representing tensor dependencies
#[derive(Debug, Clone)]
pub struct DataEdge {
    /// Source operation ID
    pub from: OpId,
    /// Destination operation ID
    pub to: OpId,
    /// Tensor ID
    pub tensor_id: TensorId,
}

/// Checkpoint for gradient checkpointing
#[derive(Debug, Clone)]
pub struct Checkpoint {
    /// Tensor values
    pub tensors: HashMap<TensorId, DenseTensor>,
}

/// Compute graph for tracking operations
#[derive(Debug, Default, Clone)]
pub struct ComputeGraph {
    /// Operation nodes
    nodes: Vec<OpNode>,
    /// Data flow edges
    edges: Vec<DataEdge>,
    /// Gradient storage
    gradients: HashMap<TensorId, DenseTensor>,
    /// Tensor values (for forward pass)
    values: HashMap<TensorId, DenseTensor>,
    /// Checkpoint for memory optimization
    checkpoint: Option<Checkpoint>,
    /// Next operation ID
    next_op_id: usize,
    /// Next tensor ID
    next_tensor_id: usize,
    /// Whether to record operations (disable during eval mode)
    recording: bool,
}

impl ComputeGraph {
    /// Create a new compute graph
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            edges: Vec::new(),
            gradients: HashMap::new(),
            values: HashMap::new(),
            checkpoint: None,
            next_op_id: 0,
            next_tensor_id: 0,
            recording: true,
        }
    }

    /// Generate a new operation ID
    pub fn next_op_id(&mut self) -> OpId {
        let id = OpId(self.next_op_id);
        self.next_op_id += 1;
        id
    }

    /// Generate a new tensor ID
    pub fn next_tensor_id(&mut self) -> TensorId {
        let id = TensorId(self.next_tensor_id);
        self.next_tensor_id += 1;
        id
    }

    /// Record an operation in the compute graph
    pub fn record_op(&mut self, op_type: OpType, inputs: &[TensorId], output: TensorId) {
        if !self.recording {
            return;
        }

        let op_id = self.next_op_id();
        let node = OpNode {
            id: op_id,
            op_type: op_type.clone(),
            inputs: inputs.to_vec(),
            output,
        };
        self.nodes.push(node);

        // Create edges from input producers to this operation
        for &input_id in inputs {
            // Find the operation that produced this input
            if let Some(producer_op) = self.nodes.iter().rev().find(|n| {
                n.output == input_id
            }) {
                let edge = DataEdge {
                    from: producer_op.id,
                    to: op_id,
                    tensor_id: input_id,
                };
                self.edges.push(edge);
            }
        }
    }

    /// Store a tensor value
    pub fn store_value(&mut self, tensor_id: TensorId, value: DenseTensor) {
        self.values.insert(tensor_id, value);
    }

    /// Get a tensor value
    pub fn get_value(&self, tensor_id: TensorId) -> Option<&DenseTensor> {
        self.values.get(&tensor_id)
    }

    /// Get a mutable reference to a tensor value
    pub fn get_value_mut(&mut self, tensor_id: TensorId) -> Option<&mut DenseTensor> {
        self.values.get_mut(&tensor_id)
    }

    /// Store a gradient
    pub fn store_gradient(&mut self, tensor_id: TensorId, gradient: DenseTensor) {
        self.gradients.insert(tensor_id, gradient);
    }

    /// Get a gradient
    pub fn get_gradient(&self, tensor_id: TensorId) -> Option<&DenseTensor> {
        self.gradients.get(&tensor_id)
    }

    /// Perform backward pass to compute gradients
    ///
    /// # Arguments
    /// * `loss` - The tensor ID of the loss value
    ///
    /// # Returns
    /// A HashMap mapping tensor IDs to their computed gradients
    pub fn backward(&mut self, loss: TensorId) -> HashMap<TensorId, DenseTensor> {
        // Initialize gradient of loss as 1.0
        if let Some(loss_tensor) = self.values.get(&loss) {
            let shape = loss_tensor.shape().to_vec();
            let ones = DenseTensor::ones(shape);
            self.gradients.insert(loss, ones);
        }

        // Get topological order of operations (reverse)
        let topo_order = self.topological_sort();

        // Backpropagate in reverse topological order
        for op_id in topo_order.into_iter().rev() {
            // Clone node info to avoid borrow checker issues
            let (node_op_type, node_inputs, node_output) = if let Some(node) = self.nodes.iter().find(|n| n.id == op_id) {
                (node.op_type.clone(), node.inputs.clone(), node.output)
            } else {
                continue;
            };

            let grad_output = self.gradients.get(&node_output).cloned();

            if let Some(grad) = grad_output {
                // Compute gradients for inputs based on operation type
                let input_grads = self.compute_gradients(&node_op_type, &node_inputs, &grad);

                // Accumulate gradients for inputs
                for (i, &input_id) in node_inputs.iter().enumerate() {
                    if let Some(input_grad) = input_grads.get(&i) {
                        self.accumulate_gradient(input_id, input_grad.clone());
                    }
                }
            }
        }

        self.gradients.clone()
    }

    /// Compute gradients for a specific operation
    fn compute_gradients(
        &self,
        op_type: &OpType,
        inputs: &[TensorId],
        grad_output: &DenseTensor,
    ) -> HashMap<usize, DenseTensor> {
        let mut grads = HashMap::new();

        match op_type {
            OpType::Add => {
                // d(x+y)/dx = 1, d(x+y)/dy = 1
                for (i, _) in inputs.iter().enumerate() {
                    grads.insert(i, grad_output.clone());
                }
            }
            OpType::Sub => {
                // d(x-y)/dx = 1, d(x-y)/dy = -1
                for (i, _) in inputs.iter().enumerate() {
                    if i == 0 {
                        grads.insert(i, grad_output.clone());
                    } else {
                        grads.insert(i, grad_output.neg());
                    }
                }
            }
            OpType::Mul => {
                // Element-wise multiplication: d(x*y)/dx = y, d(x*y)/dy = x
                if inputs.len() >= 2 {
                    if let (Some(x), Some(y)) = (
                        self.values.get(&inputs[0]),
                        self.values.get(&inputs[1]),
                    ) {
                        grads.insert(0, grad_output.mul(y));
                        grads.insert(1, grad_output.mul(x));
                    }
                }
            }
            OpType::MatMul => {
                // Matrix multiplication: d(X@W)/dX = d_out @ W.T, d(X@W)/dW = X.T @ d_out
                if inputs.len() >= 2 {
                    if let (Some(x), Some(w)) = (
                        self.values.get(&inputs[0]),
                        self.values.get(&inputs[1]),
                    ) {
                        // Gradient w.r.t. input
                        let w_t = w.transpose(None);
                        let grad_x = grad_output.matmul(&w_t);
                        grads.insert(0, grad_x);

                        // Gradient w.r.t. weights
                        let x_t = x.transpose(None);
                        let grad_w = x_t.matmul(grad_output);
                        grads.insert(1, grad_w);
                    }
                }
            }
            OpType::ReLU => {
                // ReLU gradient: 1 if x > 0, else 0
                if let Some(x) = inputs.first().and_then(|id| self.values.get(id)) {
                    let mask = x.gt(0.0);
                    let grad = grad_output.mul(&mask);
                    grads.insert(0, grad);
                }
            }
            OpType::GELU => {
                // GELU gradient approximation
                if let Some(x) = inputs.first().and_then(|id| self.values.get(id)) {
                    let gelu_grad = x.gelu_derivative();
                    let grad = grad_output.mul(&gelu_grad);
                    grads.insert(0, grad);
                }
            }
            OpType::Softmax => {
                // Softmax gradient: s * (grad - sum(grad * s))
                if let Some(softmax_out) = inputs.first().and_then(|id| self.values.get(id)) {
                    let sum_grad_dot_s = grad_output.mul(softmax_out).sum(None);
                    let ones = DenseTensor::ones(softmax_out.shape().to_vec());
                    let ones_scaled = ones.scale(sum_grad_dot_s.data()[0]);
                    let diff = grad_output.sub(&ones_scaled);
                    let grad = softmax_out.mul(&diff);
                    grads.insert(0, grad);
                }
            }
            OpType::Transpose => {
                // Transpose gradient is just transpose of gradient
                if !inputs.is_empty() {
                    grads.insert(0, grad_output.transpose(None));
                }
            }
            OpType::LayerNorm | OpType::RMSNorm => {
                // Normalization gradients (simplified)
                if inputs.first().and_then(|id| self.values.get(id)).is_some() {
                    // Placeholder - actual implementation needs more careful handling
                    grads.insert(0, grad_output.clone());
                }
            }
            _ => {
                // For unimplemented operations, pass gradient through
                for (i, _) in inputs.iter().enumerate() {
                    grads.insert(i, grad_output.clone());
                }
            }
        }

        grads
    }

    /// Accumulate gradient for a tensor
    pub fn accumulate_gradient(&mut self, tensor_id: TensorId, gradient: DenseTensor) {
        self.gradients
            .entry(tensor_id)
            .and_modify(|existing| {
                *existing = existing.add(&gradient);
            })
            .or_insert(gradient);
    }

    /// Perform topological sort of operations
    pub fn topological_sort(&self) -> Vec<OpId> {
        let mut result = Vec::new();
        let mut visited = std::collections::HashSet::new();

        fn visit(
            node: &OpNode,
            nodes: &[OpNode],
            visited: &mut std::collections::HashSet<OpId>,
            result: &mut Vec<OpId>,
        ) {
            if visited.contains(&node.id) {
                return;
            }
            visited.insert(node.id);

            // Visit producers first
            for &input_id in &node.inputs {
                if let Some(producer) = nodes.iter().find(|n| n.output == input_id) {
                    visit(producer, nodes, visited, result);
                }
            }

            result.push(node.id);
        }

        for node in &self.nodes {
            visit(node, &self.nodes, &mut visited, &mut result);
        }

        result
    }

    /// Clear the compute graph (call after backward pass)
    pub fn clear(&mut self) {
        self.nodes.clear();
        self.edges.clear();
        self.gradients.clear();
        self.values.clear();
        self.checkpoint = None;
    }

    /// Enable/disable operation recording
    pub fn set_recording(&mut self, recording: bool) {
        self.recording = recording;
    }

    /// Check if recording is enabled
    pub fn is_recording(&self) -> bool {
        self.recording
    }

    /// Get number of recorded operations
    pub fn num_ops(&self) -> usize {
        self.nodes.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compute_graph_basic() {
        let mut graph = ComputeGraph::new();
        
        // Create some tensors
        let x_id = graph.next_tensor_id();
        let w_id = graph.next_tensor_id();
        
        let x = DenseTensor::new(vec![1.0, 2.0, 3.0], vec![1, 3]);
        let w = DenseTensor::new(vec![0.1, 0.2, 0.3], vec![3, 1]);
        
        graph.store_value(x_id, x);
        graph.store_value(w_id, w);
        
        // Record MatMul operation
        let out_id = graph.next_tensor_id();
        graph.record_op(OpType::MatMul, &[x_id, w_id], out_id);
        
        // Compute output
        if let (Some(x), Some(w)) = (graph.get_value(x_id), graph.get_value(w_id)) {
            let out = x.matmul(w);
            graph.store_value(out_id, out);
        }
        
        assert_eq!(graph.num_ops(), 1);
    }

    #[test]
    fn test_topological_sort() {
        let mut graph = ComputeGraph::new();
        
        // Create a simple chain: x -> MatMul -> ReLU -> output
        let x_id = graph.next_tensor_id();
        let w_id = graph.next_tensor_id();
        let matmul_out = graph.next_tensor_id();
        let relu_out = graph.next_tensor_id();
        
        graph.store_value(x_id, DenseTensor::new(vec![1.0, 2.0], vec![1, 2]));
        graph.store_value(w_id, DenseTensor::new(vec![0.1, 0.2], vec![2, 1]));
        
        graph.record_op(OpType::MatMul, &[x_id, w_id], matmul_out);
        graph.record_op(OpType::ReLU, &[matmul_out], relu_out);
        
        let order = graph.topological_sort();
        assert_eq!(order.len(), 2);
        // MatMul should come before ReLU
        assert!(order.iter().position(|&id| {
            graph.nodes.iter().any(|n| n.id == id && matches!(n.op_type, OpType::MatMul))
        }).unwrap() < order.iter().position(|&id| {
            graph.nodes.iter().any(|n| n.id == id && matches!(n.op_type, OpType::ReLU))
        }).unwrap());
    }
}