scirs2-neural 0.4.2

Neural network building blocks module for SciRS2 (scirs2-neural) - Minimal Version
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! CLIP (Contrastive Language-Image Pre-training) Architecture
//!
//! This module implements a CLIP-like architecture as described in
//! "Learning Transferable Visual Models From Natural Language Supervision"
//! (<https://arxiv.org/abs/2103.00020>)
//! CLIP is a multi-modal model that learns visual concepts from natural language supervision,
//! enabling zero-shot transfer to various visual classification tasks.

use crate::error::Result;
use crate::layers::{Dense, Layer, LayerNorm, Sequential};
// TODO: Re-enable once PatchEmbedding is implemented
// use crate::models::architectures::{ViTConfig, VisionTransformer};
use crate::models::architectures::ViTConfig;
use crate::transformer::TransformerEncoderLayer;
use crate::utils::positional_encoding::{PositionalEncoding, SinusoidalPositionalEncoding};
use scirs2_core::ndarray::{Array, Axis, IxDyn, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use scirs2_core::random::{rngs::SmallRng, SeedableRng};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
/// Type alias for CLIP output (image embeddings, text embeddings, logit scale)
type ClipOutput<F> = (Array<F, IxDyn>, Array<F, IxDyn>, Array<F, IxDyn>);
/// Configuration for the text encoder in CLIP
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CLIPTextConfig {
    /// Vocabulary size
    pub vocab_size: usize,
    /// Hidden size for embeddings and transformer
    pub hidden_size: usize,
    /// Intermediate size in feed-forward layers
    pub intermediate_size: usize,
    /// Number of transformer layers
    pub num_layers: usize,
    /// Number of attention heads
    pub num_heads: usize,
    /// Maximum sequence length
    pub max_position_embeddings: usize,
    /// Dropout rate
    pub dropout_rate: f64,
    /// Layer norm epsilon
    pub layer_norm_eps: f64,
}
/// Configuration for the CLIP model
pub struct CLIPConfig {
    /// Text encoder configuration
    pub text_config: CLIPTextConfig,
    /// Vision encoder configuration
    pub vision_config: ViTConfig,
    /// Projection dimension for both text and vision encoders
    pub projection_dim: usize,
    /// Whether to include the classifier
    pub include_head: bool,
    /// Number of classes for the classifier (if include_head is true)
    pub num_classes: usize,
}

impl Default for CLIPTextConfig {
    fn default() -> Self {
        Self {
            vocab_size: 49408,
            hidden_size: 512,
            intermediate_size: 2048,
            num_layers: 12,
            num_heads: 8,
            max_position_embeddings: 77,
            dropout_rate: 0.1,
            layer_norm_eps: 1e-5,
        }
    }
}

/// Text encoder for CLIP model
#[derive(Debug, Clone)]
pub struct CLIPTextEncoder<
    F: Float
        + Debug
        + ScalarOperand
        + Send
        + Sync
        + 'static
        + scirs2_core::simd_ops::SimdUnifiedOps
        + NumAssign,
> {
    /// Token embedding
    pub token_embedding: Sequential<F>,
    /// Position embedding
    pub position_embedding: SinusoidalPositionalEncoding<F>,
    /// Transformer encoder layers
    pub encoder_layers: Vec<TransformerEncoderLayer<F>>,
    /// Layer normalization
    pub layer_norm: LayerNorm<F>,
    /// Final projection layer
    pub projection: Dense<F>,
    /// Text configuration
    pub config: CLIPTextConfig,
}

impl<
        F: Float
            + Debug
            + ScalarOperand
            + Send
            + Sync
            + scirs2_core::simd_ops::SimdUnifiedOps
            + NumAssign,
    > CLIPTextEncoder<F>
{
    /// Create a new CLIPTextEncoder
    pub fn new(_config: CLIPTextConfig, projection_dim: usize) -> Result<Self> {
        // Token embedding
        let mut token_embedding = Sequential::new();
        let mut rng = SmallRng::from_seed([42; 32]);
        token_embedding.add(Dense::<F>::new(
            _config.vocab_size,
            _config.hidden_size,
            None,
            &mut rng,
        )?);
        // Position embedding
        let position_embedding = SinusoidalPositionalEncoding::<F>::new(
            _config.hidden_size,
            _config.max_position_embeddings,
        );
        // Transformer encoder layers
        let mut encoder_layers = Vec::with_capacity(_config.num_layers);
        for _i in 0.._config.num_layers {
            encoder_layers.push(TransformerEncoderLayer::<F>::new(
                _config.hidden_size,
                _config.num_heads,
                _config.intermediate_size,
                _config.dropout_rate,
                _config.layer_norm_eps,
                &mut rng,
            )?);
        }
        // Layer normalization
        let layer_norm =
            LayerNorm::<F>::new(_config.hidden_size, _config.layer_norm_eps, &mut rng)?;
        // Projection
        let projection = Dense::<F>::new(_config.hidden_size, projection_dim, None, &mut rng)?;
        Ok(Self {
            token_embedding,
            position_embedding,
            encoder_layers,
            layer_norm,
            projection,
            config: _config,
        })
    }
}
impl<
        F: Float
            + Debug
            + ScalarOperand
            + Send
            + Sync
            + scirs2_core::simd_ops::SimdUnifiedOps
            + 'static
            + NumAssign,
    > Layer<F> for CLIPTextEncoder<F>
{
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }

    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        // Apply token embedding
        let mut x = self.token_embedding.forward(input)?;
        // Apply position embedding
        x = self.position_embedding.forward(&x)?;
        // Apply transformer encoder layers
        for layer in &self.encoder_layers {
            x = layer.forward(&x)?;
        }
        // Apply layer normalization
        x = self.layer_norm.forward(&x)?;
        // Extract the [CLS] token embedding (assuming it's the first token)
        let batch_size = x.shape()[0];
        let hidden_size = x.shape()[2];
        let cls_token = x
            .slice_axis(Axis(1), scirs2_core::ndarray::Slice::from(0..1))
            .into_shape_with_order((batch_size, hidden_size))?;
        // Apply projection - convert to owned array to fix the reference type
        let cls_token_owned = cls_token.to_owned().into_dyn();
        let output = self.projection.forward(&cls_token_owned)?;
        Ok(output)
    }

    fn backward(
        &self,
        input: &Array<F, IxDyn>,
        grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        // CLIPTextEncoder backward: reverse the forward pass
        // Backward through projection
        let grad_after_proj = self.projection.backward(grad_output, grad_output)?;
        // Expand CLS token gradient back to full sequence
        // Note: This is simplified - in reality we need to handle the slicing properly
        let batch_size = input.shape()[0];
        let seq_len = input.shape()[1];
        let hidden_size = grad_after_proj.shape()[1];
        // Create gradient for full sequence (most gradients go to CLS token position)
        let mut grad_full_seq =
            Array::<F, IxDyn>::zeros(IxDyn(&[batch_size, seq_len, hidden_size]));
        // Put the gradient at the CLS token position (index 0)
        for i in 0..batch_size {
            for j in 0..hidden_size {
                grad_full_seq[[i, 0, j]] = grad_after_proj[[i, j]];
            }
        }
        let grad_full_seq = grad_full_seq.into_dyn();
        // Backward through layer normalization
        let mut grad = self.layer_norm.backward(&grad_full_seq, &grad_full_seq)?;
        // Backward through transformer encoder layers in reverse order
        for layer in self.encoder_layers.iter().rev() {
            grad = layer.backward(&grad, &grad)?;
        }
        // TODO: Backward through position embedding when backward method is implemented
        // grad = self.position_embedding.backward(&grad, &grad)?;
        // Backward through token embedding
        let grad_input = self.token_embedding.backward(input, &grad)?;
        Ok(grad_input)
    }

    fn update(&mut self, learning_rate: F) -> Result<()> {
        // Update all components
        // Update token embedding
        self.token_embedding.update(learning_rate)?;
        // Update position embedding
        self.position_embedding.update(learning_rate)?;
        // Update all encoder layers
        for layer in &mut self.encoder_layers {
            layer.update(learning_rate)?;
        }
        // Update layer normalization
        self.layer_norm.update(learning_rate)?;
        // Update projection layer
        self.projection.update(learning_rate)?;
        Ok(())
    }

    fn params(&self) -> Vec<Array<F, IxDyn>> {
        let mut params = Vec::new();
        params.extend(self.token_embedding.params());
        // position_embedding has no trainable parameters, so we skip it
        for layer in &self.encoder_layers {
            params.extend(layer.params());
        }
        params.extend(self.layer_norm.params());
        params.extend(self.projection.params());
        params
    }

    fn set_training(&mut self, training: bool) {
        self.token_embedding.set_training(training);
        self.position_embedding.set_training(training);
        for layer in &mut self.encoder_layers {
            layer.set_training(training);
        }
        self.layer_norm.set_training(training);
        self.projection.set_training(training);
    }

    fn is_training(&self) -> bool {
        self.token_embedding.is_training()
    }
}
// Vision encoder for CLIP model (uses Vision Transformer)
// TODO: Re-enable once PatchEmbedding is implemented
// pub struct CLIPVisionEncoder<
//     F: Float + Debug + ScalarOperand + Send + Sync + 'static + scirs2_core::simd_ops::SimdUnifiedOps,
// > {
//     /// Vision Transformer
//     pub vision_transformer: VisionTransformer<F>,
//     /// Projection layer
//     pub projection: Dense<F>,
// }
//
// impl<F: Float + Debug + ScalarOperand + Send + Sync + 'static + scirs2_core::simd_ops::SimdUnifiedOps + NumAssign>
//     CLIPVisionEncoder<F>
// {
//     /// Create a new CLIPVisionEncoder
//     pub fn new(_config: ViTConfig, projection_dim: usize) -> Result<Self> {
//         // Create ViT with a clone of the _config to avoid ownership issues
//         let vision_transformer = VisionTransformer::<F>::new(_config.clone())?;
//         // Projection layer
//         let mut rng_proj = SmallRng::from_seed([42; 32]);
//         let projection = Dense::<F>::new(_config.embed_dim, projection_dim, None, &mut rng_proj)?;
//         Ok(Self {
//             vision_transformer,
//             projection,
//         })
//     }
// }
//
// impl<
//         F: Float
//             + Debug
//             + ScalarOperand
//             + Send
//             + Sync
//             + scirs2_core::simd_ops::SimdUnifiedOps
//             + 'static
//             + NumAssign,
//     > Layer<F> for CLIPVisionEncoder<F>
// {
//     fn as_any(&self) -> &dyn std::any::Any {
//         self
//     }
//
//     fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
//         self
//     }
//
//     fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
//         // Apply vision transformer
//         let x = self.vision_transformer.forward(input)?;
//         // Apply projection
//         let output = self.projection.forward(&x)?;
//         Ok(output)
//     }
//
//     fn backward(
//         &self,
//         input: &Array<F, IxDyn>,
//         grad_output: &Array<F, IxDyn>,
//     ) -> Result<Array<F, IxDyn>> {
//         // CLIPVisionEncoder backward: reverse the forward pass
//         // Backward through projection
//         let grad_after_proj = self.projection.backward(grad_output, grad_output)?;
//         // Backward through vision transformer
//         let grad_input = self.vision_transformer.backward(input, &grad_after_proj)?;
//         Ok(grad_input)
//     }
//
//     fn update(&mut self, learning_rate: F) -> Result<()> {
//         // Update projection
//         self.projection.update(learning_rate)?;
//         // Update vision transformer
//         self.vision_transformer.update(learning_rate)?;
//         Ok(())
//     }
//
//     fn params(&self) -> Vec<Array<F, IxDyn>> {
//         let mut params = Vec::new();
//         params.extend(self.projection.params());
//         params.extend(self.vision_transformer.params());
//         params
//     }
//
//     fn set_training(&mut self, training: bool) {
//         self.projection.set_training(training);
//         self.vision_transformer.set_training(training);
//     }
//
//     fn is_training(&self) -> bool {
//         self.vision_transformer.is_training()
//     }
// }
// CLIP model implementation
// TODO: Re-enable once PatchEmbedding is implemented
// pub struct CLIP<
//     F: Float + Debug + ScalarOperand + Send + Sync + 'static + scirs2_core::simd_ops::SimdUnifiedOps,
// > {
//     /// Vision encoder
//     pub vision_encoder: CLIPVisionEncoder<F>,
//     /// Text encoder
//     pub text_encoder: CLIPTextEncoder<F>,
//     /// Optional classifier for zero-shot classification
//     pub classifier: Option<Dense<F>>,
//     /// Model configuration
//     pub _config: CLIPConfig,
//     /// Temperature parameter for contrastive loss
//     pub logit_scale: F,
// }

// TODO: Re-enable once PatchEmbedding is implemented
// impl<F: Float + Debug + ScalarOperand + Send + Sync + 'static + scirs2_core::simd_ops::SimdUnifiedOps + NumAssign>
//     CLIP<F>
// {
//     /// Create a new CLIP model
//     pub fn new(config: CLIPConfig) -> Result<Self> {
//         // Create vision encoder
//         let vision_encoder =
//             CLIPVisionEncoder::<F>::new(config.vision_config.clone(), config.projection_dim)?;
//         // Create text encoder
//         let text_encoder =
//             CLIPTextEncoder::<F>::new(config.text_config.clone(), config.projection_dim)?;
//         // Create classifier if needed
//         let classifier = if config.include_head {
//             let mut rng_cls = SmallRng::from_seed([42; 32]);
//             Some(Dense::<F>::new(
//                 config.projection_dim,
//                 config.num_classes,
//                 None,
//                 &mut rng_cls,
//             )?)
//         } else {
//             None
//         };
//         // Initialize logit scale (typically ln(1/0.07))
//         let logit_scale = F::from(2.6592).expect("Failed to convert constant to float");
//         Ok(Self {
//             vision_encoder,
//             text_encoder,
//             classifier,
//             _config: config,
//             logit_scale,
//         })
//     }
//     /// Forward pass for image-text contrastive learning
//     pub fn forward_contrastive(
//         &self,
//         image_input: &Array<F, IxDyn>,
//         text_input: &Array<F, IxDyn>,
//     ) -> Result<ClipOutput<F>> {
//         // Get image and text embeddings
//         let image_features = self.vision_encoder.forward(image_input)?;
//         let text_features = self.text_encoder.forward(text_input)?;
//         // Normalize embeddings
//         let image_features_norm = normalize_features(&image_features)?;
//         let text_features_norm = normalize_features(&text_features)?;
//         // Compute similarity matrix (batch_size x batch_size)
//         let logits_per_image =
//             compute_similarity(&image_features_norm, &text_features_norm, self.logit_scale)?;
//         // Transpose to get logits_pertext (currently unused but kept for API consistency)
//         let _logits_pertext = logits_per_image.t().into_dyn();
//         Ok((image_features, text_features, logits_per_image))
//     }
//
//     /// Forward pass for zero-shot image classification using a text encoder
//     pub fn forward_classification(
//         &self,
//         image_input: &Array<F, IxDyn>,
//         text_embeddings: &Array<F, IxDyn>,
//     ) -> Result<Array<F, IxDyn>> {
//         // Get image embeddings
//         let image_features = self.vision_encoder.forward(image_input)?;
//         // Normalize image embeddings
//         let image_features_norm = normalize_features(&image_features)?;
//         // Compute similarity with text embeddings
//         let logits = compute_similarity(&image_features_norm, text_embeddings, self.logit_scale)?;
//         Ok(logits)
//     }
//     /// Create a CLIP model with default settings
//     pub fn clip_base(num_classes: usize, include_head: bool) -> Result<Self> {
//         let vision_config = ViTConfig {
//             image_size: (224, 224),
//             patch_size: (16, 16),
//             in_channels: 3,
//             num_classes,
//             embed_dim: 768,
//             num_heads: 12,
//             mlp_dim: 3072,
//             num_layers: 12,
//             dropout_rate: 0.1,
//             attention_dropout_rate: 0.1,
//         };
//         let text_config = CLIPTextConfig::default();
//         let config = CLIPConfig {
//             text_config,
//             vision_config,
//             projection_dim: 512,
//             include_head,
//             num_classes,
//         };
//         Self::new(config)
//     }
//     /// Create a small CLIP model
//     pub fn clip_small(num_classes: usize, include_head: bool) -> Result<Self> {
//         let vision_config = ViTConfig {
//             image_size: (224, 224),
//             patch_size: (16, 16),
//             in_channels: 3,
//             num_classes,
//             embed_dim: 512,
//             num_heads: 6,
//             mlp_dim: 2048,
//             num_layers: 8,
//             dropout_rate: 0.1,
//             attention_dropout_rate: 0.1,
//         };
//         let text_config = CLIPTextConfig {
//             vocab_size: 49408,
//             hidden_size: 384,
//             intermediate_size: 1536,
//             num_layers: 8,
//             num_heads: 6,
//             max_position_embeddings: 77,
//             dropout_rate: 0.1,
//             layer_norm_eps: 1e-5,
//         };
//         let config = CLIPConfig {
//             text_config,
//             vision_config,
//             projection_dim: 256,
//             include_head,
//             num_classes,
//         };
//         Self::new(config)
//     }
// }
//
// impl<
//         F: Float
//             + Debug
//             + ScalarOperand
//             + Send
//             + Sync
//             + scirs2_core::simd_ops::SimdUnifiedOps
//             + 'static
//             + NumAssign,
//     > Layer<F> for CLIP<F>
// {
//     fn as_any(&self) -> &dyn std::any::Any {
//         self
//     }
//
//     fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
//         self
//     }
//
//     fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
//         // In a typical scenario, input would be an image
//         // For classification tasks, we would need pre-computed text embeddings
//         // Get image features
//         let image_features = self.vision_encoder.forward(input)?;
//         // If classifier is present, use it for direct classification
//         if let Some(ref classifier) = self.classifier {
//             return classifier.forward(&image_features);
//         }
//         Ok(image_features)
//     }
//
//     fn backward(
//         &self,
//         input: &Array<F, IxDyn>,
//         grad_output: &Array<F, IxDyn>,
//     ) -> Result<Array<F, IxDyn>> {
//         // CLIP backward: reverse the forward pass
//         let mut grad = grad_output.clone();
//         // Backward through classifier if present
//         if let Some(ref classifier) = self.classifier {
//             // For proper gradient computation, we need the intermediate features
//             // This is a simplified version
//             grad = classifier.backward(&grad, &grad)?;
//         }
//         // Backward through vision encoder
//         let grad_input = self.vision_encoder.backward(input, &grad)?;
//         Ok(grad_input)
//     }
//
//     fn update(&mut self, learning_rate: F) -> Result<()> {
//         // Update vision encoder
//         self.vision_encoder.update(learning_rate)?;
//         // Update text encoder
//         self.text_encoder.update(learning_rate)?;
//         // Update classifier if present
//         if let Some(ref mut classifier) = self.classifier {
//             classifier.update(learning_rate)?;
//         }
//         Ok(())
//     }
//
//     fn params(&self) -> Vec<Array<F, IxDyn>> {
//         let mut params = Vec::new();
//         params.extend(self.vision_encoder.params());
//         params.extend(self.text_encoder.params());
//         if let Some(ref classifier) = self.classifier {
//             params.extend(classifier.params());
//         }
//         params
//     }
//
//     fn set_training(&mut self, training: bool) {
//         self.vision_encoder.set_training(training);
//         self.text_encoder.set_training(training);
//         if let Some(ref mut classifier) = self.classifier {
//             classifier.set_training(training);
//         }
//     }
//
//     fn is_training(&self) -> bool {
//         self.vision_encoder.is_training()
//     }
// }
/// Normalize feature vectors (L2 normalization)
#[allow(dead_code)]
fn normalize_features<F: Float + Debug + ScalarOperand>(
    features: &Array<F, IxDyn>,
) -> Result<Array<F, IxDyn>> {
    let shape = features.shape();
    let batch_size = shape[0];
    let feature_dim = shape[1];
    // Reshape to 2D for easier computation
    let features_2d = features
        .clone()
        .into_shape_with_order((batch_size, feature_dim))?;
    // Compute L2 norm along the feature dimension
    let norm = features_2d.map_axis(Axis(1), |x| {
        let sum_squares = x.iter().fold(F::zero(), |acc, &val| acc + val * val);
        let norm = sum_squares.sqrt();
        // Avoid division by zero
        if norm > F::from(1e-12).expect("Failed to convert constant to float") {
            norm
        } else {
            F::one()
        }
    });
    // Expand norm to match feature dims for broadcasting
    let norm_expanded = norm.insert_axis(Axis(1));
    // Normalize features
    let normalized = features_2d.clone() / norm_expanded;
    // Reshape back to original shape
    Ok(normalized.into_shape_with_order(shape)?)
}

/// Compute similarity matrix between two sets of features
#[allow(dead_code)]
fn compute_similarity<F: Float + Debug + ScalarOperand>(
    features_a: &Array<F, IxDyn>,
    features_b: &Array<F, IxDyn>,
    temperature: F,
) -> Result<Array<F, IxDyn>> {
    // Get shapes
    let shape_a = features_a.shape();
    let shape_b = features_b.shape();
    let batch_a = shape_a[0];
    let batch_b = shape_b[0];
    // Reshape features to 2D matrices
    let features_a_2d = features_a
        .clone()
        .into_shape_with_order((batch_a, shape_a[1]))?;
    let features_b_2d = features_b
        .clone()
        .into_shape_with_order((batch_b, shape_b[1]))?;
    // Compute dot product (similarity matrix)
    let similarity = features_a_2d.dot(&features_b_2d.t());
    // Apply temperature scaling
    let scaled_similarity = similarity * temperature;
    Ok(scaled_similarity.into_dyn())
}