oxirs-graphrag 0.3.1

GraphRAG: Hybrid Vector + Graph Retrieval-Augmented Generation for OxiRS
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
//! Message-passing GNN encoder for knowledge-graph entity embeddings.
//!
//! Implements a simple mean-aggregation multi-layer GNN with Xavier weight
//! initialisation, ReLU + L2-normalisation activations, and a margin-based
//! link-prediction training objective.  All random numbers are generated
//! via an internal Linear Congruential Generator so the crate does not depend
//! on the `rand` crate.

use std::collections::HashMap;

use crate::GraphRAGError;

use super::adjacency::AdjacencyGraph;

// ─────────────────────────────────────────────────────────────────────────────
// Linear Congruential Generator (internal; no rand dependency)
// ─────────────────────────────────────────────────────────────────────────────

/// Simple Linear Congruential Generator — Numerical Recipes parameters.
struct Lcg {
    state: u64,
}

impl Lcg {
    fn new(seed: u64) -> Self {
        Self {
            state: seed.wrapping_add(1),
        }
    }

    fn next_u64(&mut self) -> u64 {
        self.state = self
            .state
            .wrapping_mul(6_364_136_223_846_793_005)
            .wrapping_add(1_442_695_040_888_963_407);
        self.state
    }

    /// Uniform sample in [0, 1)
    fn next_f64(&mut self) -> f64 {
        (self.next_u64() >> 11) as f64 / (1u64 << 53) as f64
    }

    /// Uniform sample in [-scale, scale)
    fn next_f64_range(&mut self, scale: f64) -> f64 {
        (self.next_f64() * 2.0 - 1.0) * scale
    }

    /// Random usize in [0, n)
    fn next_usize(&mut self, n: usize) -> usize {
        if n == 0 {
            return 0;
        }
        (self.next_u64() as usize) % n
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Configuration
// ─────────────────────────────────────────────────────────────────────────────

/// Configuration for the message-passing GNN encoder.
#[derive(Debug, Clone)]
pub struct GnnEncoderConfig {
    /// Number of message-passing layers (default: 2)
    pub num_layers: usize,
    /// Dimensionality of hidden and output embeddings (default: 64)
    pub hidden_dim: usize,
    /// Training epochs for the link-prediction objective (default: 50)
    pub num_epochs: usize,
    /// SGD learning rate (default: 0.01)
    pub learning_rate: f64,
    /// Margin for the triplet margin loss (default: 1.0)
    pub margin: f64,
}

impl Default for GnnEncoderConfig {
    fn default() -> Self {
        Self {
            num_layers: 2,
            hidden_dim: 64,
            num_epochs: 50,
            learning_rate: 0.01,
            margin: 1.0,
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// GnnEncoder
// ─────────────────────────────────────────────────────────────────────────────

/// Multi-layer mean-aggregation GNN encoder.
///
/// After calling [`GnnEncoder::fit`] on a set of RDF triples the encoder can
/// produce a fixed-dimensional embedding for any entity seen during training
/// via [`GnnEncoder::embed_entity`].  Unknown entities return a zero vector.
pub struct GnnEncoder {
    /// Encoder configuration
    config: GnnEncoderConfig,
    /// Entity embeddings: `entity_embeddings[i]` is the embedding for node `i`
    entity_embeddings: Vec<Vec<f64>>,
    /// Weight matrices for each layer: `weight_matrices[l][i][j]`
    weight_matrices: Vec<Vec<Vec<f64>>>,
    /// Map from entity name → integer index
    entity_index: HashMap<String, usize>,
}

impl GnnEncoder {
    /// Create a new, untrained encoder.
    pub fn new(config: GnnEncoderConfig) -> Self {
        Self {
            config,
            entity_embeddings: Vec::new(),
            weight_matrices: Vec::new(),
            entity_index: HashMap::new(),
        }
    }

    /// Fit the encoder to the provided RDF triples.
    ///
    /// Builds the adjacency graph, initialises embeddings and weight matrices,
    /// then runs stochastic gradient descent with a margin-based
    /// link-prediction loss.
    pub fn fit(&mut self, triples: &[(String, String, String)]) -> Result<(), GraphRAGError> {
        if triples.is_empty() {
            return Err(GraphRAGError::EmbeddingError(
                "Cannot fit GnnEncoder on empty triple set".into(),
            ));
        }

        let graph = AdjacencyGraph::from_triples(triples);
        let n = graph.entity_count();
        let d = self.config.hidden_dim;

        // Store entity index map
        self.entity_index = graph.entity_to_idx.clone();

        let mut rng = Lcg::new(42);

        // Initialise entity embeddings with Xavier uniform
        self.entity_embeddings = Self::xavier_init(n, d, &mut rng);

        // Initialise one weight matrix per layer (d × d)
        self.weight_matrices = (0..self.config.num_layers)
            .map(|_| Self::xavier_init(d, d, &mut rng))
            .collect();

        // Training loop
        for _epoch in 0..self.config.num_epochs {
            // For each triple, run a positive + negative sample update
            for (s_str, _p_str, o_str) in triples {
                let Some(&s_idx) = self.entity_index.get(s_str.as_str()) else {
                    continue;
                };
                let Some(&o_idx) = self.entity_index.get(o_str.as_str()) else {
                    continue;
                };

                // Sample a random negative entity
                let neg_idx = loop {
                    let candidate = rng.next_usize(n);
                    if candidate != o_idx {
                        break candidate;
                    }
                };

                // Forward pass: compute embeddings for s, o, neg
                let emb_s = self.forward_entity(s_idx, &graph);
                let emb_o = self.forward_entity(o_idx, &graph);
                let emb_neg = self.forward_entity(neg_idx, &graph);

                let loss = Self::margin_loss(&emb_s, &emb_o, &emb_neg, self.config.margin);

                // Only update if loss is positive (violated margin)
                if loss > 0.0 {
                    self.sgd_update(s_idx, o_idx, neg_idx, &graph);
                }
            }
        }

        // Final forward pass to store steady-state embeddings
        for i in 0..n {
            self.entity_embeddings[i] = self.forward_entity(i, &graph);
        }

        Ok(())
    }

    /// Return the embedding vector for a named entity.
    /// Returns a zero vector of length `hidden_dim` if the entity is unknown.
    pub fn embed_entity(&self, entity: &str) -> Vec<f64> {
        match self.entity_index.get(entity) {
            Some(&idx) if idx < self.entity_embeddings.len() => self.entity_embeddings[idx].clone(),
            _ => vec![0.0; self.config.hidden_dim],
        }
    }

    // ─────────────────────────────────────────────────────────────────────────
    // Private helpers
    // ─────────────────────────────────────────────────────────────────────────

    /// Xavier/Glorot uniform initialisation for an (rows × cols) weight matrix.
    fn xavier_init(rows: usize, cols: usize, rng: &mut Lcg) -> Vec<Vec<f64>> {
        let scale = (6.0 / (rows + cols) as f64).sqrt();
        (0..rows)
            .map(|_| (0..cols).map(|_| rng.next_f64_range(scale)).collect())
            .collect()
    }

    /// Run a single forward pass for node `idx`, producing a `hidden_dim`-dimensional
    /// embedding by iterating over each message-passing layer.
    fn forward_entity(&self, idx: usize, graph: &AdjacencyGraph) -> Vec<f64> {
        let d = self.config.hidden_dim;
        let mut h = if idx < self.entity_embeddings.len() {
            self.entity_embeddings[idx].clone()
        } else {
            vec![0.0; d]
        };

        for layer in 0..self.config.num_layers {
            // Collect neighbour embeddings for mean aggregation
            let neighbors = graph.neighbors(idx);
            let neighbor_embs: Vec<&Vec<f64>> = neighbors
                .iter()
                .filter_map(|&nidx| self.entity_embeddings.get(nidx))
                .collect();

            let aggregated = if neighbor_embs.is_empty() {
                h.clone()
            } else {
                // Mean of self + neighbours
                let mut combined = neighbor_embs.clone();
                combined.push(&h);
                Self::mean_aggregate(&combined)
            };

            // Apply weight matrix for this layer: h_new = W * aggregated
            let w = &self.weight_matrices[layer];
            let mut new_h = vec![0.0; d];
            for (i, row) in w.iter().enumerate() {
                let dot: f64 = row.iter().zip(aggregated.iter()).map(|(a, b)| a * b).sum();
                new_h[i] = dot;
            }

            Self::relu_and_normalize(&mut new_h);
            h = new_h;
        }

        h
    }

    /// One SGD step pushing the positive pair (s, o) closer and the negative
    /// pair (s, neg) further apart in embedding space.
    fn sgd_update(&mut self, s_idx: usize, o_idx: usize, neg_idx: usize, graph: &AdjacencyGraph) {
        let lr = self.config.learning_rate;
        let d = self.config.hidden_dim;

        let emb_s = self.forward_entity(s_idx, graph);
        let emb_o = self.forward_entity(o_idx, graph);
        let emb_neg = self.forward_entity(neg_idx, graph);

        // Gradient for entity embeddings:
        //   push s closer to o: emb_s -= lr * (emb_s - emb_o)
        //   push s away from neg: emb_s += lr * (emb_s - emb_neg)
        for j in 0..d {
            if s_idx < self.entity_embeddings.len() {
                let grad_pos = emb_s[j] - emb_o[j];
                let grad_neg = emb_s[j] - emb_neg[j];
                self.entity_embeddings[s_idx][j] -= lr * (grad_pos - grad_neg);
            }
        }

        // Normalise updated embedding
        if s_idx < self.entity_embeddings.len() {
            let v = &mut self.entity_embeddings[s_idx];
            Self::relu_and_normalize(v);
        }
    }

    /// Compute the mean embedding of a non-empty slice of embedding vectors.
    pub fn mean_aggregate(embeddings: &[&Vec<f64>]) -> Vec<f64> {
        if embeddings.is_empty() {
            return Vec::new();
        }
        let d = embeddings[0].len();
        let mut mean = vec![0.0_f64; d];
        for emb in embeddings {
            for (j, &val) in emb.iter().enumerate() {
                if j < mean.len() {
                    mean[j] += val;
                }
            }
        }
        let n = embeddings.len() as f64;
        for v in &mut mean {
            *v /= n;
        }
        mean
    }

    /// Apply ReLU activation then L2-normalise the vector in-place.
    /// If the L2 norm is near zero the vector is left unchanged.
    pub fn relu_and_normalize(v: &mut [f64]) {
        // ReLU
        for x in v.iter_mut() {
            if *x < 0.0 {
                *x = 0.0;
            }
        }
        // L2 normalise
        let norm: f64 = v.iter().map(|x| x * x).sum::<f64>().sqrt();
        if norm > 1e-10 {
            for x in v.iter_mut() {
                *x /= norm;
            }
        }
    }

    /// Triplet margin loss: max(0, d(s, o) - d(s, neg) + margin)
    /// where d is squared Euclidean distance.
    pub fn margin_loss(pos_s: &[f64], pos_o: &[f64], neg_o: &[f64], margin: f64) -> f64 {
        let d_pos: f64 = pos_s
            .iter()
            .zip(pos_o.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum();
        let d_neg: f64 = pos_s
            .iter()
            .zip(neg_o.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum();
        (d_pos - d_neg + margin).max(0.0)
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    fn triples() -> Vec<(String, String, String)> {
        vec![
            ("Alice".into(), "knows".into(), "Bob".into()),
            ("Bob".into(), "worksAt".into(), "Acme".into()),
            ("Carol".into(), "worksAt".into(), "Acme".into()),
            ("Alice".into(), "friendOf".into(), "Carol".into()),
            ("Dave".into(), "knows".into(), "Alice".into()),
        ]
    }

    #[test]
    fn test_fit_completes() {
        let mut encoder = GnnEncoder::new(GnnEncoderConfig {
            num_layers: 2,
            hidden_dim: 16,
            num_epochs: 5,
            ..Default::default()
        });
        encoder.fit(&triples()).expect("fit should succeed");
    }

    #[test]
    fn test_embed_shape_correct() {
        let mut encoder = GnnEncoder::new(GnnEncoderConfig {
            num_layers: 2,
            hidden_dim: 32,
            num_epochs: 3,
            ..Default::default()
        });
        encoder.fit(&triples()).expect("fit should succeed");
        let emb = encoder.embed_entity("Alice");
        assert_eq!(emb.len(), 32, "Embedding dimension must match hidden_dim");
    }

    #[test]
    fn test_unseen_entity_returns_zero_vec() {
        let mut encoder = GnnEncoder::new(GnnEncoderConfig {
            num_layers: 1,
            hidden_dim: 8,
            num_epochs: 2,
            ..Default::default()
        });
        encoder.fit(&triples()).expect("fit should succeed");
        let emb = encoder.embed_entity("UnknownEntity_XYZ");
        assert_eq!(emb.len(), 8);
        assert!(
            emb.iter().all(|&x| x == 0.0),
            "Unknown entity must map to zero vector"
        );
    }

    #[test]
    fn test_loss_is_non_negative() {
        // The margin loss must always be ≥ 0
        let a = vec![1.0_f64, 0.0, 0.0];
        let b = vec![0.0_f64, 1.0, 0.0];
        let c = vec![0.0_f64, 0.0, 1.0];
        let loss = GnnEncoder::margin_loss(&a, &b, &c, 1.0);
        assert!(loss >= 0.0, "Margin loss must be non-negative");
    }

    #[test]
    fn test_embeddings_l2_normalized() {
        let mut encoder = GnnEncoder::new(GnnEncoderConfig {
            num_layers: 2,
            hidden_dim: 16,
            num_epochs: 5,
            ..Default::default()
        });
        encoder.fit(&triples()).expect("fit should succeed");

        for entity in &["Alice", "Bob", "Acme"] {
            let emb = encoder.embed_entity(entity);
            let norm: f64 = emb.iter().map(|x| x * x).sum::<f64>().sqrt();
            // After relu + l2-normalise, norm should be close to 1
            // (or zero if all activations are zero)
            assert!(
                (norm - 1.0).abs() < 1e-6 || norm < 1e-10,
                "Entity {} norm={} should be 1 or 0 (all-zero)",
                entity,
                norm
            );
        }
    }

    #[test]
    fn test_mean_aggregation_correct() {
        let a = vec![1.0_f64, 2.0, 3.0];
        let b = vec![3.0_f64, 4.0, 5.0];
        let result = GnnEncoder::mean_aggregate(&[&a, &b]);
        assert_eq!(result.len(), 3);
        assert!((result[0] - 2.0).abs() < 1e-10);
        assert!((result[1] - 3.0).abs() < 1e-10);
        assert!((result[2] - 4.0).abs() < 1e-10);
    }
}