oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom models
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
488
489
490
491
492
493
494
//! Module for vision-language-graph integration

use super::*;
use anyhow::Result;
use scirs2_core::ndarray_ext::{Array1, Array2, Array3, Array4, Axis};
use scirs2_core::random::{Random, RngExt};
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct VisionEncoder {
    pub config: VisionEncoderConfig,
    /// CNN backbone parameters
    pub cnn_parameters: HashMap<String, Array4<f32>>,
    /// Vision transformer parameters
    pub vit_parameters: HashMap<String, Array2<f32>>,
    /// Projection layer
    pub projection: Array2<f32>,
}

impl VisionEncoder {
    pub fn new(config: VisionEncoderConfig) -> Self {
        let mut cnn_parameters = HashMap::new();
        let mut vit_parameters = HashMap::new();

        // Initialize CNN parameters
        for (i, &filter_size) in config.cnn_config.filter_sizes.iter().enumerate() {
            let layer_name = format!("conv_{i}");
            let weight_shape = (
                filter_size,
                if i == 0 {
                    config.channels
                } else {
                    config.cnn_config.filter_sizes[i - 1]
                },
                3,
                3,
            );
            let mut random = Random::default();
            cnn_parameters.insert(
                layer_name,
                Array4::from_shape_fn(weight_shape, |_| (random.random::<f32>() - 0.5) * 0.1),
            );
        }

        // Initialize ViT parameters
        let mut random = Random::default();
        vit_parameters.insert(
            "patch_embedding".to_string(),
            Array2::from_shape_fn(
                (
                    config.channels * config.patch_size.0 * config.patch_size.1,
                    config.vision_dim,
                ),
                |_| (random.random::<f32>() - 0.5) * 0.1,
            ),
        );

        // Projection to unified dimension
        let mut random = Random::default();
        let projection = Array2::from_shape_fn((config.vision_dim, config.vision_dim), |_| {
            (random.random::<f32>() - 0.5) * 0.1
        });

        Self {
            config,
            cnn_parameters,
            vit_parameters,
            projection,
        }
    }

    /// Encode image to visual embeddings
    pub fn encode_image(&self, image: &Array3<f32>) -> Result<Array1<f32>> {
        match self.config.architecture {
            VisionArchitecture::VisionTransformer => self.encode_with_vit(image),
            VisionArchitecture::ResNet => self.encode_with_cnn(image),
            _ => self.encode_with_vit(image), // Default to ViT
        }
    }

    /// Encode with Vision Transformer
    fn encode_with_vit(&self, image: &Array3<f32>) -> Result<Array1<f32>> {
        // Simulate patch extraction and embedding
        let (h, w, c) = image.dim();
        let (patch_h, patch_w) = self.config.patch_size;

        let num_patches_h = h / patch_h;
        let num_patches_w = w / patch_w;
        let num_patches = num_patches_h * num_patches_w;

        // Extract patches and flatten
        let mut patch_embeddings = Array2::zeros((num_patches, self.config.vision_dim));

        for i in 0..num_patches_h {
            for j in 0..num_patches_w {
                let patch_idx = i * num_patches_w + j;

                // Extract patch
                let patch = image.slice(scirs2_core::ndarray_ext::s![
                    i * patch_h..(i + 1) * patch_h,
                    j * patch_w..(j + 1) * patch_w,
                    ..
                ]);

                // Flatten patch
                let patch_owned = patch.to_owned();
                let flattened_patch = patch_owned
                    .into_shape_with_order(c * patch_h * patch_w)
                    .expect("reshape should succeed for valid patch dimensions");

                // Project to embedding space
                if let Some(patch_embedding_matrix) = self.vit_parameters.get("patch_embedding") {
                    let embedding = flattened_patch.dot(patch_embedding_matrix);
                    patch_embeddings.row_mut(patch_idx).assign(&embedding);
                }
            }
        }

        // Global average pooling over patches
        let global_embedding = patch_embeddings
            .mean_axis(Axis(0))
            .expect("mean_axis should succeed for non-empty array");

        Ok(global_embedding)
    }

    /// Encode with CNN
    fn encode_with_cnn(&self, image: &Array3<f32>) -> Result<Array1<f32>> {
        // Simulate CNN forward pass
        let mut features = image.clone();

        // Apply multiple conv layers
        for i in 0..self.config.cnn_config.num_layers.min(2) {
            // Limit for simplicity
            // Simulate convolution + pooling
            let (h, w, c) = features.dim();
            let new_h = h / 2; // Simulate stride 2
            let new_w = w / 2;
            let new_c = self.config.cnn_config.filter_sizes[i];

            let mut new_features = Array3::zeros((new_h, new_w, new_c));

            // Simple downsampling simulation
            for new_i in 0..new_h {
                for new_j in 0..new_w {
                    for new_k in 0..new_c {
                        let old_i = new_i * 2;
                        let old_j = new_j * 2;

                        if old_i < h && old_j < w {
                            // Average over 2x2 region
                            let mut sum = 0.0;
                            let mut count = 0;
                            for di in 0..2 {
                                for dj in 0..2 {
                                    if old_i + di < h && old_j + dj < w {
                                        for k in 0..c.min(new_c) {
                                            sum += features[[old_i + di, old_j + dj, k]];
                                            count += 1;
                                        }
                                    }
                                }
                            }
                            new_features[[new_i, new_j, new_k]] = sum / count as f32;
                        }
                    }
                }
            }

            features = new_features;
        }

        // Global average pooling
        let features_len = features.len();
        let flattened = features
            .into_shape_with_order(features_len)
            .expect("reshape should succeed for valid features dimensions");
        let mut global_features = vec![0.0; self.config.vision_dim];

        for i in 0..global_features.len().min(flattened.len()) {
            global_features[i] = flattened[i];
        }

        Ok(Array1::from_vec(global_features))
    }
}

/// Language encoder
#[derive(Debug, Clone)]
pub struct LanguageEncoder {
    pub config: LanguageEncoderConfig,
    /// Token embeddings
    pub token_embeddings: Array2<f32>,
    /// Position embeddings
    pub position_embeddings: Array2<f32>,
    /// Transformer parameters
    pub transformer_parameters: HashMap<String, Array2<f32>>,
}

impl LanguageEncoder {
    pub fn new(config: LanguageEncoderConfig) -> Self {
        // Initialize embeddings
        let mut random = Random::default();
        let token_embeddings =
            Array2::from_shape_fn((config.vocab_size, config.language_dim), |_| {
                (random.random::<f32>() - 0.5) * 0.1
            });

        let mut random = Random::default();
        let position_embeddings =
            Array2::from_shape_fn((config.max_seq_length, config.language_dim), |_| {
                (random.random::<f32>() - 0.5) * 0.1
            });

        let mut transformer_parameters = HashMap::new();

        // Initialize transformer layers
        for layer in 0..config.transformer_config.num_layers {
            let mut random = Random::default();
            transformer_parameters.insert(
                format!("attention_weights_{layer}"),
                Array2::from_shape_fn((config.language_dim, config.language_dim), |_| {
                    (random.random::<f32>() - 0.5) * 0.1
                }),
            );

            let mut random = Random::default();
            transformer_parameters.insert(
                format!("feed_forward_{layer}"),
                Array2::from_shape_fn(
                    (
                        config.transformer_config.intermediate_dim,
                        config.language_dim,
                    ),
                    |_| (random.random::<f32>() - 0.5) * 0.1,
                ),
            );
        }

        Self {
            config,
            token_embeddings,
            position_embeddings,
            transformer_parameters,
        }
    }

    /// Encode text to language embeddings
    pub fn encode_text(&self, text: &str) -> Result<Array1<f32>> {
        // Simple tokenization (in real implementation would use proper tokenizer)
        let tokens = self.tokenize(text);

        // Get token embeddings
        let mut sequence_embeddings = Array2::zeros((tokens.len(), self.config.language_dim));

        for (i, &token_id) in tokens.iter().enumerate() {
            if token_id < self.token_embeddings.nrows() {
                let token_emb = self.token_embeddings.row(token_id);
                let pos_emb = self
                    .position_embeddings
                    .row(i.min(self.config.max_seq_length - 1));

                // Add token and position embeddings
                let combined = &token_emb + &pos_emb;
                sequence_embeddings.row_mut(i).assign(&combined);
            }
        }

        // Apply transformer layers (simplified)
        let mut hidden_states = sequence_embeddings;

        for layer in 0..self.config.transformer_config.num_layers.min(2) {
            // Limit for performance
            if let Some(attention_weights) = self
                .transformer_parameters
                .get(&format!("attention_weights_{layer}"))
            {
                // Apply self-attention (simplified)
                hidden_states = hidden_states.dot(attention_weights);

                // Apply layer norm (simplified)
                for mut row in hidden_states.rows_mut() {
                    let mean = row.mean().unwrap_or(0.0);
                    let var = row.var(0.0);
                    row.mapv_inplace(|x| (x - mean) / (var + 1e-8).sqrt());
                }
            }
        }

        // Pool to sentence-level representation (mean pooling)
        let sentence_embedding = hidden_states
            .mean_axis(Axis(0))
            .expect("mean_axis should succeed for non-empty array");

        Ok(sentence_embedding)
    }

    /// Simple tokenization
    fn tokenize(&self, text: &str) -> Vec<usize> {
        text.split_whitespace()
            .map(|word| {
                // Simple hash-based token ID
                let mut hash = 0usize;
                for byte in word.bytes() {
                    hash = hash.wrapping_mul(31).wrapping_add(byte as usize);
                }
                hash % self.config.vocab_size
            })
            .collect()
    }
}

/// Graph encoder
#[derive(Debug, Clone)]
pub struct GraphEncoder {
    pub config: GraphEncoderConfig,
    /// Node transformation parameters
    pub node_parameters: HashMap<String, Array2<f32>>,
    /// Edge transformation parameters  
    pub edge_parameters: HashMap<String, Array2<f32>>,
    /// Graph-level parameters
    pub graph_parameters: HashMap<String, Array2<f32>>,
}

impl GraphEncoder {
    pub fn new(config: GraphEncoderConfig) -> Self {
        let mut node_parameters = HashMap::new();
        let mut edge_parameters = HashMap::new();
        let mut graph_parameters = HashMap::new();

        // Initialize node transformation layers
        for layer in 0..config.num_layers {
            let mut random = Random::default();
            node_parameters.insert(
                format!("node_transform_{layer}"),
                Array2::from_shape_fn((config.node_dim, config.node_dim), |_| {
                    (random.random::<f32>() - 0.5) * 0.1
                }),
            );
        }

        // Initialize edge transformation layers
        for layer in 0..config.num_layers {
            let mut random = Random::default();
            edge_parameters.insert(
                format!("edge_transform_{layer}"),
                Array2::from_shape_fn((config.edge_dim, config.edge_dim), |_| {
                    (random.random::<f32>() - 0.5) * 0.1
                }),
            );
        }

        // Graph readout parameters (for attention mechanism)
        let mut random = Random::default();
        graph_parameters.insert(
            "readout".to_string(),
            Array2::from_shape_fn(
                (config.node_dim, 1), // Single attention score per node
                |_| (random.random::<f32>() - 0.5) * 0.1,
            ),
        );

        // Graph projection parameters (from node_dim to graph_dim)
        let mut random = Random::default();
        graph_parameters.insert(
            "graph_projection".to_string(),
            Array2::from_shape_fn((config.node_dim, config.graph_dim), |_| {
                (random.random::<f32>() - 0.5) * 0.1
            }),
        );

        Self {
            config,
            node_parameters,
            edge_parameters,
            graph_parameters,
        }
    }

    /// Encode graph to graph embeddings
    pub fn encode_graph(
        &self,
        node_features: &Array2<f32>,
        edge_features: &Array2<f32>,
        adjacency_matrix: &Array2<f32>,
    ) -> Result<Array1<f32>> {
        let mut node_embeddings = node_features.clone();

        // Apply GNN layers
        for layer in 0..self.config.num_layers.min(2) {
            // Limit for performance
            node_embeddings =
                self.apply_gnn_layer(&node_embeddings, edge_features, adjacency_matrix, layer)?;
        }

        // Graph-level readout
        let graph_embedding = self.graph_readout(&node_embeddings)?;

        Ok(graph_embedding)
    }

    /// Apply a single GNN layer
    fn apply_gnn_layer(
        &self,
        node_embeddings: &Array2<f32>,
        _edge_features: &Array2<f32>,
        adjacency_matrix: &Array2<f32>,
        layer: usize,
    ) -> Result<Array2<f32>> {
        let transform_key = format!("node_transform_{layer}");

        if let Some(transform_matrix) = self.node_parameters.get(&transform_key) {
            // Message passing: aggregate neighbor features
            let aggregated = adjacency_matrix.dot(node_embeddings);

            // Apply transformation
            let transformed = aggregated.dot(transform_matrix);

            // Apply activation (ReLU)
            let activated = transformed.mapv(|x| x.max(0.0));

            Ok(activated)
        } else {
            Ok(node_embeddings.clone())
        }
    }

    /// Graph-level readout
    fn graph_readout(&self, node_embeddings: &Array2<f32>) -> Result<Array1<f32>> {
        let node_level_embedding = match self.config.readout {
            ReadoutFunction::GlobalMean => node_embeddings
                .mean_axis(Axis(0))
                .expect("mean_axis should succeed for non-empty array"),
            ReadoutFunction::GlobalMax => {
                node_embeddings.fold_axis(Axis(0), f32::NEG_INFINITY, |&a, &b| a.max(b))
            }
            ReadoutFunction::GlobalSum => node_embeddings.sum_axis(Axis(0)),
            ReadoutFunction::GlobalAttention => {
                if let Some(readout_matrix) = self.graph_parameters.get("readout") {
                    // Attention-based readout
                    let attention_scores = node_embeddings.dot(readout_matrix); // (num_nodes, 1)
                    let attention_scores_1d = attention_scores.column(0).to_owned(); // (num_nodes,)
                    let attention_weights = self.softmax_1d(&attention_scores_1d); // (num_nodes,)

                    // Weighted average of node embeddings
                    let mut weighted_sum = Array1::zeros(node_embeddings.ncols());
                    for (i, &weight) in attention_weights.iter().enumerate() {
                        let node_emb = node_embeddings.row(i);
                        weighted_sum = weighted_sum + weight * &node_emb;
                    }
                    weighted_sum
                } else {
                    node_embeddings
                        .mean_axis(Axis(0))
                        .expect("mean_axis should succeed for non-empty array")
                }
            }
            _ => node_embeddings
                .mean_axis(Axis(0))
                .expect("mean_axis should succeed for non-empty array"),
        };

        // Project from node_dim to graph_dim
        if let Some(projection_matrix) = self.graph_parameters.get("graph_projection") {
            Ok(projection_matrix.t().dot(&node_level_embedding))
        } else {
            Ok(node_level_embedding)
        }
    }

    /// Apply softmax to 2D array
    fn softmax_2d(&self, x: &Array2<f32>) -> Array2<f32> {
        let mut result = x.clone();
        for mut row in result.rows_mut() {
            let max_val = row.fold(f32::NEG_INFINITY, |a, &b| a.max(b));
            row.mapv_inplace(|v| (v - max_val).exp());
            let sum = row.sum();
            if sum > 0.0 {
                row /= sum;
            }
        }
        result
    }

    fn softmax_1d(&self, x: &Array1<f32>) -> Array1<f32> {
        let max_val = x.fold(f32::NEG_INFINITY, |a, &b| a.max(b));
        let mut result = x.mapv(|v| (v - max_val).exp());
        let sum = result.sum();
        if sum > 0.0 {
            result /= sum;
        }
        result
    }
}