trustformers-models 0.1.1

Model implementations for TrustformeRS
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
use crate::clip::config::{CLIPConfig, CLIPEncoderConfig, CLIPTextConfig, CLIPVisionConfig};
use scirs2_core::ndarray::{s, Array1, Array2, Array3, Array4}; // SciRS2 Integration Policy
use std::io::Read;
use trustformers_core::{
    device::Device,
    errors::{tensor_op_error, Result, TrustformersError},
    layers::{Embedding, FeedForward, LayerNorm, Linear, MultiHeadAttention},
    tensor::Tensor,
    traits::{Config, Layer, Model},
};

/// CLIP Vision Transformer patch embedding
pub struct CLIPVisionEmbeddings {
    patch_embedding: CLIPPatchEmbedding,
    class_embedding: Tensor,
    position_embedding: Embedding,
    num_patches: usize,
    num_positions: usize,
    device: Device,
}

impl CLIPVisionEmbeddings {
    pub fn new_with_device(config: &CLIPVisionConfig, device: Device) -> Result<Self> {
        let patch_embedding = CLIPPatchEmbedding::new_with_device(config, device)?;
        let num_patches = config.num_patches();
        let num_positions = config.seq_length();

        let class_embedding = Tensor::randn(&[config.hidden_size])?;
        let position_embedding = Embedding::new(num_positions, config.hidden_size, None)?;

        Ok(Self {
            patch_embedding,
            class_embedding,
            position_embedding,
            num_patches,
            num_positions,
            device,
        })
    }

    pub fn new(config: &CLIPVisionConfig) -> Result<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    pub fn device(&self) -> Device {
        self.device
    }

    /// Load weights for vision embeddings
    pub fn load_weights(
        &mut self,
        loader: &mut dyn crate::weight_loading::WeightLoader,
        prefix: &str,
    ) -> Result<()> {
        // Load patch embedding weights
        self.patch_embedding
            .load_weights(loader, &format!("{}.patch_embedding", prefix))?;

        // Load class embedding
        if let Ok(class_emb) = loader.load_tensor(&format!("{}.class_embedding", prefix)) {
            self.class_embedding = class_emb;
        }

        // Load position embeddings
        if let Ok(pos_weight) = loader.load_tensor(&format!("{}.position_embedding.weight", prefix))
        {
            self.position_embedding.set_weight(pos_weight)?;
        }

        Ok(())
    }
}

impl Layer for CLIPVisionEmbeddings {
    type Input = Array4<f32>; // (batch_size, height, width, channels)
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        let batch_size = input.shape()[0];

        // Get patch embeddings
        let patch_embeddings = self.patch_embedding.forward(input)?;

        // Prepare class token for each batch item
        let class_tokens = match &self.class_embedding {
            Tensor::F32(class_arr) => {
                let mut class_batch = Array2::zeros((batch_size, class_arr.len()));
                for i in 0..batch_size {
                    class_batch.row_mut(i).assign(class_arr);
                }
                Tensor::F32(class_batch.into_dyn())
            },
            _ => {
                return Err(tensor_op_error(
                    "tensor_operation",
                    "Unsupported class embedding tensor type",
                ))
            },
        };

        // Concatenate class token and patch embeddings
        let embeddings = match (&class_tokens, &patch_embeddings) {
            (Tensor::F32(class_arr), Tensor::F32(patch_arr)) => {
                let seq_len = 1 + self.num_patches;
                let hidden_size = class_arr.shape()[1];

                let mut combined = Array3::zeros((batch_size, seq_len, hidden_size));

                // Set class tokens at position 0
                for i in 0..batch_size {
                    combined.slice_mut(s![i, 0, ..]).assign(&class_arr.slice(s![i, ..]));
                }

                // Set patch embeddings at positions 1..seq_len
                for i in 0..batch_size {
                    for j in 0..self.num_patches {
                        combined.slice_mut(s![i, j + 1, ..]).assign(&patch_arr.slice(s![i, j, ..]));
                    }
                }

                Tensor::F32(combined.into_dyn())
            },
            _ => {
                return Err(tensor_op_error(
                    "tensor_operation",
                    "Unsupported tensor types for embedding concatenation",
                ))
            },
        };

        // Add positional embeddings
        let position_ids: Vec<u32> = (0..self.num_positions).map(|i| i as u32).collect();
        let position_embeddings = self.position_embedding.forward(position_ids)?;

        embeddings.add(&position_embeddings)
    }
}

/// CLIP patch embedding layer
pub struct CLIPPatchEmbedding {
    projection: Linear,
    patch_size: usize,
    hidden_size: usize,
    device: Device,
}

impl CLIPPatchEmbedding {
    pub fn new_with_device(config: &CLIPVisionConfig, device: Device) -> Result<Self> {
        let in_features = config.patch_size * config.patch_size * config.num_channels;
        let projection = Linear::new(in_features, config.hidden_size, true);

        Ok(Self {
            projection,
            patch_size: config.patch_size,
            hidden_size: config.hidden_size,
            device,
        })
    }

    pub fn new(config: &CLIPVisionConfig) -> Result<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    pub fn device(&self) -> Device {
        self.device
    }

    /// Load weights for patch embedding
    pub fn load_weights(
        &mut self,
        loader: &mut dyn crate::weight_loading::WeightLoader,
        prefix: &str,
    ) -> Result<()> {
        if let Ok(weight) = loader.load_tensor(&format!("{}.weight", prefix)) {
            self.projection.set_weight(weight)?;
        }
        if let Ok(bias) = loader.load_tensor(&format!("{}.bias", prefix)) {
            self.projection.set_bias(bias)?;
        }
        Ok(())
    }
}

impl Layer for CLIPPatchEmbedding {
    type Input = Array4<f32>; // (batch_size, height, width, channels)
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        let (batch_size, height, width, channels) = input.dim();
        let patch_size = self.patch_size;

        // Extract patches
        let patches_h = height / patch_size;
        let patches_w = width / patch_size;
        let num_patches = patches_h * patches_w;

        let mut patches =
            Array2::zeros((batch_size * num_patches, patch_size * patch_size * channels));

        for b in 0..batch_size {
            for ph in 0..patches_h {
                for pw in 0..patches_w {
                    let patch_idx = b * num_patches + ph * patches_w + pw;
                    let mut patch_data = Vec::new();

                    for y in 0..patch_size {
                        for x in 0..patch_size {
                            for c in 0..channels {
                                let pixel_y = ph * patch_size + y;
                                let pixel_x = pw * patch_size + x;
                                patch_data.push(input[(b, pixel_y, pixel_x, c)]);
                            }
                        }
                    }

                    for (i, &val) in patch_data.iter().enumerate() {
                        patches[(patch_idx, i)] = val;
                    }
                }
            }
        }

        // Project patches to hidden dimension
        let projected = self.projection.forward(Tensor::F32(patches.into_dyn()))?;

        // Reshape to (batch_size, num_patches, hidden_size)
        match projected {
            Tensor::F32(arr) => {
                let reshaped =
                    arr.into_shape_with_order((batch_size, num_patches, self.hidden_size))?;
                Ok(Tensor::F32(reshaped.into_dyn()))
            },
            _ => Err(tensor_op_error(
                "tensor_operation",
                "Expected F32 tensor from projection",
            )),
        }
    }
}

/// CLIP text encoder layer
pub struct CLIPTextTransformer {
    pub(crate) embeddings: CLIPTextEmbeddings,
    pub(crate) encoder: CLIPEncoder<CLIPTextConfig>,
    pub(crate) final_layer_norm: LayerNorm,
    device: Device,
}

impl CLIPTextTransformer {
    pub fn new_with_device(config: &CLIPTextConfig, device: Device) -> Result<Self> {
        let embeddings = CLIPTextEmbeddings::new_with_device(config, device)?;
        let encoder = CLIPEncoder::new_with_device(config, device)?;
        let final_layer_norm = LayerNorm::new(vec![config.hidden_size], config.layer_norm_eps)?;

        Ok(Self {
            embeddings,
            encoder,
            final_layer_norm,
            device,
        })
    }

    pub fn new(config: &CLIPTextConfig) -> Result<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    pub fn device(&self) -> Device {
        self.device
    }

    /// Load weights for text transformer
    pub fn load_weights(
        &mut self,
        loader: &mut dyn crate::weight_loading::WeightLoader,
        prefix: &str,
    ) -> Result<()> {
        self.embeddings.load_weights(loader, &format!("{}.embeddings", prefix))?;
        self.encoder.load_weights(loader, &format!("{}.encoder", prefix))?;

        if let Ok(ln_weight) = loader.load_tensor(&format!("{}.final_layer_norm.weight", prefix)) {
            self.final_layer_norm.set_weight(ln_weight)?;
        }
        if let Ok(ln_bias) = loader.load_tensor(&format!("{}.final_layer_norm.bias", prefix)) {
            self.final_layer_norm.set_bias(ln_bias)?;
        }

        Ok(())
    }
}

impl Layer for CLIPTextTransformer {
    type Input = Vec<u32>; // Token IDs
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        let embeddings = self.embeddings.forward(input)?;
        let encoded = self.encoder.forward(embeddings)?;
        self.final_layer_norm.forward(encoded)
    }
}

/// CLIP vision encoder layer
pub struct CLIPVisionTransformer {
    pub(crate) embeddings: CLIPVisionEmbeddings,
    pub(crate) encoder: CLIPEncoder<CLIPVisionConfig>,
    pub(crate) layernorm: LayerNorm,
    device: Device,
}

impl CLIPVisionTransformer {
    pub fn new_with_device(config: &CLIPVisionConfig, device: Device) -> Result<Self> {
        let embeddings = CLIPVisionEmbeddings::new_with_device(config, device)?;
        let encoder = CLIPEncoder::new_with_device(config, device)?;
        let layernorm = LayerNorm::new(vec![config.hidden_size], config.layer_norm_eps)?;

        Ok(Self {
            embeddings,
            encoder,
            layernorm,
            device,
        })
    }

    pub fn new(config: &CLIPVisionConfig) -> Result<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    pub fn device(&self) -> Device {
        self.device
    }

    /// Load weights for vision transformer
    pub fn load_weights(
        &mut self,
        loader: &mut dyn crate::weight_loading::WeightLoader,
        prefix: &str,
    ) -> Result<()> {
        self.embeddings.load_weights(loader, &format!("{}.embeddings", prefix))?;
        self.encoder.load_weights(loader, &format!("{}.encoder", prefix))?;

        if let Ok(ln_weight) = loader.load_tensor(&format!("{}.layernorm.weight", prefix)) {
            self.layernorm.set_weight(ln_weight)?;
        }
        if let Ok(ln_bias) = loader.load_tensor(&format!("{}.layernorm.bias", prefix)) {
            self.layernorm.set_bias(ln_bias)?;
        }

        Ok(())
    }
}

impl Layer for CLIPVisionTransformer {
    type Input = Array4<f32>; // Images
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        let embeddings = self.embeddings.forward(input)?;
        let encoded = self.encoder.forward(embeddings)?;
        self.layernorm.forward(encoded)
    }
}

/// CLIP text embeddings
pub struct CLIPTextEmbeddings {
    token_embedding: Embedding,
    position_embedding: Embedding,
    device: Device,
}

impl CLIPTextEmbeddings {
    pub fn new_with_device(config: &CLIPTextConfig, device: Device) -> Result<Self> {
        let token_embedding = Embedding::new(config.vocab_size, config.hidden_size, None)?;
        let position_embedding =
            Embedding::new(config.max_position_embeddings, config.hidden_size, None)?;

        Ok(Self {
            token_embedding,
            position_embedding,
            device,
        })
    }

    pub fn new(config: &CLIPTextConfig) -> Result<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    pub fn device(&self) -> Device {
        self.device
    }

    /// Load weights for text embeddings
    pub fn load_weights(
        &mut self,
        loader: &mut dyn crate::weight_loading::WeightLoader,
        prefix: &str,
    ) -> Result<()> {
        // Load token embeddings
        if let Ok(token_weight) = loader.load_tensor(&format!("{}.token_embedding.weight", prefix))
        {
            self.token_embedding.set_weight(token_weight)?;
        }

        // Load position embeddings
        if let Ok(pos_weight) = loader.load_tensor(&format!("{}.position_embedding.weight", prefix))
        {
            self.position_embedding.set_weight(pos_weight)?;
        }

        Ok(())
    }
}

impl Layer for CLIPTextEmbeddings {
    type Input = Vec<u32>; // Token IDs
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        let seq_len = input.len();
        let token_embeddings = self.token_embedding.forward(input)?;

        let position_ids: Vec<u32> = (0..seq_len).map(|i| i as u32).collect();
        let position_embeddings = self.position_embedding.forward(position_ids)?;

        token_embeddings.add(&position_embeddings)
    }
}

/// Generic CLIP encoder (works for both text and vision)
pub struct CLIPEncoder<C> {
    pub(crate) layers: Vec<CLIPEncoderLayer>,
    device: Device,
    _phantom: std::marker::PhantomData<C>,
}

impl<C> CLIPEncoder<C>
where
    C: CLIPEncoderConfig + Send + Sync,
{
    pub fn new_with_device(config: &C, device: Device) -> Result<Self> {
        let layer_config = CLIPEncoderLayerConfig {
            hidden_size: config.hidden_size(),
            num_attention_heads: config.num_attention_heads(),
            intermediate_size: config.intermediate_size(),
            hidden_act: config.hidden_act().to_string(),
            layer_norm_eps: config.layer_norm_eps(),
            attention_dropout: config.attention_dropout(),
            dropout: config.dropout(),
        };

        let num_layers = config.num_hidden_layers();
        let mut layers = Vec::with_capacity(num_layers);
        for _ in 0..num_layers {
            layers.push(CLIPEncoderLayer::new_with_device(&layer_config, device)?);
        }

        Ok(Self {
            layers,
            device,
            _phantom: std::marker::PhantomData,
        })
    }

    pub fn new(config: &C) -> Result<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    pub fn device(&self) -> Device {
        self.device
    }

    /// Load weights for all encoder layers
    pub fn load_weights(
        &mut self,
        loader: &mut dyn crate::weight_loading::WeightLoader,
        prefix: &str,
    ) -> Result<()> {
        for (i, layer) in self.layers.iter_mut().enumerate() {
            layer.load_weights(loader, &format!("{}.layers.{}", prefix, i))?;
        }
        Ok(())
    }
}

impl<C> Layer for CLIPEncoder<C>
where
    C: CLIPEncoderConfig + Send + Sync,
{
    type Input = Tensor;
    type Output = Tensor;

    fn forward(&self, mut input: Self::Input) -> Result<Self::Output> {
        for layer in &self.layers {
            input = layer.forward(input)?;
        }
        Ok(input)
    }
}

/// CLIP encoder layer configuration
#[derive(Debug, Clone)]
pub struct CLIPEncoderLayerConfig {
    pub hidden_size: usize,
    pub num_attention_heads: usize,
    pub intermediate_size: usize,
    pub hidden_act: String,
    pub layer_norm_eps: f32,
    pub attention_dropout: f32,
    pub dropout: f32,
}

/// CLIP encoder layer
pub struct CLIPEncoderLayer {
    self_attn: MultiHeadAttention,
    layer_norm1: LayerNorm,
    mlp: FeedForward,
    layer_norm2: LayerNorm,
    device: Device,
}

impl CLIPEncoderLayer {
    pub fn new_with_device(config: &CLIPEncoderLayerConfig, device: Device) -> Result<Self> {
        let self_attn = MultiHeadAttention::new(
            config.hidden_size,
            config.num_attention_heads,
            config.attention_dropout,
            true, // bias
        )?;
        let layer_norm1 = LayerNorm::new(vec![config.hidden_size], config.layer_norm_eps)?;
        let mlp = FeedForward::new(config.hidden_size, config.intermediate_size, config.dropout)?;
        let layer_norm2 = LayerNorm::new(vec![config.hidden_size], config.layer_norm_eps)?;

        Ok(Self {
            self_attn,
            layer_norm1,
            mlp,
            layer_norm2,
            device,
        })
    }

    pub fn new(config: &CLIPEncoderLayerConfig) -> Result<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    pub fn device(&self) -> Device {
        self.device
    }

    /// Load weights for encoder layer
    pub fn load_weights(
        &mut self,
        loader: &mut dyn crate::weight_loading::WeightLoader,
        prefix: &str,
    ) -> Result<()> {
        // Load attention weights
        if let Ok(q_weight) = loader.load_tensor(&format!("{}.self_attn.q_proj.weight", prefix)) {
            self.self_attn.set_query_weight(q_weight)?;
        }
        if let Ok(q_bias) = loader.load_tensor(&format!("{}.self_attn.q_proj.bias", prefix)) {
            self.self_attn.set_query_bias(q_bias)?;
        }
        if let Ok(k_weight) = loader.load_tensor(&format!("{}.self_attn.k_proj.weight", prefix)) {
            self.self_attn.set_key_weight(k_weight)?;
        }
        if let Ok(k_bias) = loader.load_tensor(&format!("{}.self_attn.k_proj.bias", prefix)) {
            self.self_attn.set_key_bias(k_bias)?;
        }
        if let Ok(v_weight) = loader.load_tensor(&format!("{}.self_attn.v_proj.weight", prefix)) {
            self.self_attn.set_value_weight(v_weight)?;
        }
        if let Ok(v_bias) = loader.load_tensor(&format!("{}.self_attn.v_proj.bias", prefix)) {
            self.self_attn.set_value_bias(v_bias)?;
        }
        if let Ok(o_weight) = loader.load_tensor(&format!("{}.self_attn.out_proj.weight", prefix)) {
            self.self_attn.set_out_proj_weight(o_weight)?;
        }
        if let Ok(o_bias) = loader.load_tensor(&format!("{}.self_attn.out_proj.bias", prefix)) {
            self.self_attn.set_out_proj_bias(o_bias)?;
        }

        // Load MLP weights
        if let Ok(fc1_weight) = loader.load_tensor(&format!("{}.mlp.fc1.weight", prefix)) {
            self.mlp.set_dense_weight(fc1_weight)?;
        }
        if let Ok(fc1_bias) = loader.load_tensor(&format!("{}.mlp.fc1.bias", prefix)) {
            self.mlp.set_dense_bias(fc1_bias)?;
        }
        if let Ok(fc2_weight) = loader.load_tensor(&format!("{}.mlp.fc2.weight", prefix)) {
            self.mlp.set_output_weight(fc2_weight)?;
        }
        if let Ok(fc2_bias) = loader.load_tensor(&format!("{}.mlp.fc2.bias", prefix)) {
            self.mlp.set_output_bias(fc2_bias)?;
        }

        // Load layer norms
        if let Ok(ln1_weight) = loader.load_tensor(&format!("{}.layer_norm1.weight", prefix)) {
            self.layer_norm1.set_weight(ln1_weight)?;
        }
        if let Ok(ln1_bias) = loader.load_tensor(&format!("{}.layer_norm1.bias", prefix)) {
            self.layer_norm1.set_bias(ln1_bias)?;
        }
        if let Ok(ln2_weight) = loader.load_tensor(&format!("{}.layer_norm2.weight", prefix)) {
            self.layer_norm2.set_weight(ln2_weight)?;
        }
        if let Ok(ln2_bias) = loader.load_tensor(&format!("{}.layer_norm2.bias", prefix)) {
            self.layer_norm2.set_bias(ln2_bias)?;
        }

        Ok(())
    }
}

impl Layer for CLIPEncoderLayer {
    type Input = Tensor;
    type Output = Tensor;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        // Pre-norm architecture: norm -> attention -> residual
        let normalized1 = self.layer_norm1.forward(input.clone())?;
        let attn_output = self.self_attn.forward_self_attention(&normalized1, None, false)?;
        let residual1 = input.add(&attn_output)?;

        // Pre-norm architecture: norm -> mlp -> residual
        let normalized2 = self.layer_norm2.forward(residual1.clone())?;
        let mlp_output = self.mlp.forward(normalized2)?;
        let residual2 = residual1.add(&mlp_output)?;

        Ok(residual2)
    }
}

/// Main CLIP model
pub struct CLIPModel {
    config: CLIPConfig,
    pub(crate) text_model: CLIPTextTransformer,
    pub(crate) vision_model: CLIPVisionTransformer,
    text_projection: Linear,
    visual_projection: Linear,
    pub logit_scale: Tensor,
    device: Device,
}

impl CLIPModel {
    pub fn new_with_device(config: CLIPConfig, device: Device) -> Result<Self> {
        config.validate()?;

        let text_model = CLIPTextTransformer::new_with_device(&config.text_config, device)?;
        let vision_model = CLIPVisionTransformer::new_with_device(&config.vision_config, device)?;

        let text_projection =
            Linear::new(config.text_config.hidden_size, config.projection_dim, false);
        let visual_projection = Linear::new(
            config.vision_config.hidden_size,
            config.projection_dim,
            false,
        );

        let logit_scale =
            Tensor::F32(Array1::from_elem(1, config.logit_scale_init_value).into_dyn());

        Ok(Self {
            config,
            text_model,
            vision_model,
            text_projection,
            visual_projection,
            logit_scale,
            device,
        })
    }

    pub fn new(config: CLIPConfig) -> Result<Self> {
        Self::new_with_device(config, Device::CPU)
    }

    pub fn device(&self) -> Device {
        self.device
    }

    /// Get text features
    pub fn get_text_features(&self, input_ids: Vec<u32>) -> Result<Tensor> {
        let text_outputs = self.text_model.forward(input_ids)?;

        // Use the [CLS] token representation (first token)
        let cls_output = text_outputs.select_first_token()?;
        self.text_projection.forward(cls_output)
    }

    /// Get image features
    pub fn get_image_features(&self, pixel_values: Array4<f32>) -> Result<Tensor> {
        let vision_outputs = self.vision_model.forward(pixel_values)?;

        // Use the [CLS] token representation (first token)
        let cls_output = vision_outputs.select_first_token()?;
        self.visual_projection.forward(cls_output)
    }

    /// Forward pass that returns both text and image features
    pub fn forward(
        &self,
        input_ids: Option<Vec<u32>>,
        pixel_values: Option<Array4<f32>>,
    ) -> Result<CLIPOutput> {
        let mut text_embeds = None;
        let mut image_embeds = None;

        if let Some(input_ids) = input_ids {
            text_embeds = Some(self.get_text_features(input_ids)?);
        }

        if let Some(pixel_values) = pixel_values {
            image_embeds = Some(self.get_image_features(pixel_values)?);
        }

        Ok(CLIPOutput {
            text_embeds,
            image_embeds,
            logits_per_image: None,
            logits_per_text: None,
        })
    }

    /// Compute similarity scores between text and images
    pub fn compute_similarity(
        &self,
        input_ids: Vec<u32>,
        pixel_values: Array4<f32>,
    ) -> Result<(Tensor, Tensor)> {
        let text_features = self.get_text_features(input_ids)?;
        let image_features = self.get_image_features(pixel_values)?;

        // Normalize features (manual implementation)
        let text_norm = text_features.norm()?;
        let image_norm = image_features.norm()?;
        let text_features_norm = text_features.scale(1.0 / text_norm)?;
        let image_features_norm = image_features.scale(1.0 / image_norm)?;

        // Compute logits
        let logit_scale = match &self.logit_scale {
            Tensor::F32(scale_arr) => scale_arr[[0]].exp(),
            _ => {
                return Err(tensor_op_error(
                    "tensor_operation",
                    "Invalid logit scale tensor",
                ))
            },
        };

        let image_transposed = image_features_norm.transpose(0, 1)?;
        let logits_per_image = text_features_norm.matmul(&image_transposed)?.scale(logit_scale)?;
        let logits_per_text = logits_per_image.transpose(0, 1)?;

        Ok((logits_per_image, logits_per_text))
    }
}

/// CLIP model output
#[derive(Debug)]
pub struct CLIPOutput {
    pub text_embeds: Option<Tensor>,
    pub image_embeds: Option<Tensor>,
    pub logits_per_image: Option<Tensor>,
    pub logits_per_text: Option<Tensor>,
}

impl Model for CLIPModel {
    type Config = CLIPConfig;
    type Input = (Option<Vec<u32>>, Option<Array4<f32>>); // (text_input, image_input)
    type Output = CLIPOutput;

    fn forward(&self, input: Self::Input) -> Result<Self::Output> {
        let (input_ids, pixel_values) = input;
        self.forward(input_ids, pixel_values)
    }

    fn load_pretrained(&mut self, _reader: &mut dyn Read) -> Result<()> {
        // Legacy interface - use load_from_path instead for new weight loading
        Err(
            trustformers_core::errors::TrustformersError::not_implemented(
                "Use load_from_path or load_from_huggingface for enhanced weight loading"
                    .to_string(),
            ),
        )
    }

    fn get_config(&self) -> &Self::Config {
        &self.config
    }

    fn num_parameters(&self) -> usize {
        // Text encoder parameters
        let text_vocab_size = self.config.text_config.vocab_size;
        let text_hidden_size = self.config.text_config.hidden_size;
        let text_num_layers = self.config.text_config.num_hidden_layers;
        let _text_num_heads = self.config.text_config.num_attention_heads;
        let text_intermediate_size = self.config.text_config.intermediate_size;
        let text_max_position = self.config.text_config.max_position_embeddings;

        // Text embeddings: token + position
        let text_embedding_params =
            text_vocab_size * text_hidden_size + text_max_position * text_hidden_size;

        // Text encoder layers: attention + FFN + layer norms
        let text_attention_params_per_layer =
            4 * (text_hidden_size * text_hidden_size + text_hidden_size); // Q,K,V,dense
        let text_ffn_params_per_layer = text_hidden_size * text_intermediate_size
            + text_intermediate_size
            + text_intermediate_size * text_hidden_size
            + text_hidden_size;
        let text_layer_norm_params = 4 * text_hidden_size; // 2 LayerNorms per layer

        let text_encoder_params = text_num_layers
            * (text_attention_params_per_layer
                + text_ffn_params_per_layer
                + text_layer_norm_params);

        // Vision encoder parameters
        let vision_hidden_size = self.config.vision_config.hidden_size;
        let vision_num_layers = self.config.vision_config.num_hidden_layers;
        let _vision_num_heads = self.config.vision_config.num_attention_heads;
        let vision_intermediate_size = self.config.vision_config.intermediate_size;
        let vision_patch_size = self.config.vision_config.patch_size;
        let vision_num_channels = self.config.vision_config.num_channels;

        // Vision patch embedding
        let vision_embedding_params =
            vision_patch_size * vision_patch_size * vision_num_channels * vision_hidden_size
                + vision_hidden_size;

        // Vision encoder layers
        let vision_attention_params_per_layer =
            4 * (vision_hidden_size * vision_hidden_size + vision_hidden_size);
        let vision_ffn_params_per_layer = vision_hidden_size * vision_intermediate_size
            + vision_intermediate_size
            + vision_intermediate_size * vision_hidden_size
            + vision_hidden_size;
        let vision_layer_norm_params = 4 * vision_hidden_size;

        let vision_encoder_params = vision_num_layers
            * (vision_attention_params_per_layer
                + vision_ffn_params_per_layer
                + vision_layer_norm_params);

        // Projection layers (text and vision to shared embedding space)
        let projection_dim = self.config.projection_dim;
        let text_projection_params = text_hidden_size * projection_dim;
        let vision_projection_params = vision_hidden_size * projection_dim;

        // Logit scale parameter
        let logit_scale_params = 1;

        text_embedding_params
            + text_encoder_params
            + vision_embedding_params
            + vision_encoder_params
            + text_projection_params
            + vision_projection_params
            + logit_scale_params
    }
}

impl CLIPModel {
    /// Load model weights from a directory containing HuggingFace format weights
    pub fn load_from_path(&mut self, model_path: impl AsRef<std::path::Path>) -> Result<()> {
        use crate::weight_loading::{auto_create_loader, WeightLoadingConfig};

        let config = WeightLoadingConfig {
            lazy_loading: true,
            memory_mapped: false,
            ..Default::default()
        };

        let mut loader = auto_create_loader(model_path, Some(config))?;

        // Load text model weights using the new weight loading infrastructure
        self.text_model.load_weights(loader.as_mut(), "text_model")?;

        // Load vision model weights using the new weight loading infrastructure
        self.vision_model.load_weights(loader.as_mut(), "vision_model")?;

        // Load text projection layer
        if let Ok(text_proj_weight) = loader.load_tensor("text_projection.weight") {
            self.text_projection.set_weight(text_proj_weight)?;
        }
        if let Ok(text_proj_bias) = loader.load_tensor("text_projection.bias") {
            self.text_projection.set_bias(text_proj_bias)?;
        }

        // Load visual projection layer
        if let Ok(visual_proj_weight) = loader.load_tensor("visual_projection.weight") {
            self.visual_projection.set_weight(visual_proj_weight)?;
        }
        if let Ok(visual_proj_bias) = loader.load_tensor("visual_projection.bias") {
            self.visual_projection.set_bias(visual_proj_bias)?;
        }

        // Load logit scale
        if let Ok(logit_scale_weight) = loader.load_tensor("logit_scale") {
            self.logit_scale = logit_scale_weight;
        }

        Ok(())
    }

    /// Load model weights from HuggingFace Hub or local cache
    pub fn load_from_huggingface(&mut self, model_name: &str) -> Result<()> {
        use std::path::PathBuf;

        // Try to find model in HuggingFace cache directory
        let cache_dir = std::env::var("HF_HOME")
            .or_else(|_| std::env::var("HUGGINGFACE_HUB_CACHE"))
            .unwrap_or_else(|_| {
                let home = std::env::var("HOME").unwrap_or_default();
                format!("{home}/.cache/huggingface/hub")
            });

        let model_base_path =
            PathBuf::from(cache_dir).join(format!("models--{}", model_name.replace("/", "--")));
        let model_path = model_base_path.join("snapshots");

        if let Ok(mut entries) = std::fs::read_dir(&model_path) {
            if let Some(Ok(entry)) = entries.next() {
                let snapshot_path = entry.path();
                return self.load_from_path(&snapshot_path);
            }
        }

        // Attempt to download the model from HuggingFace Hub
        self.download_from_huggingface_hub(model_name, &model_base_path)?;

        // Try to load again after download
        if let Ok(mut entries) = std::fs::read_dir(&model_path) {
            if let Some(Ok(entry)) = entries.next() {
                let snapshot_path = entry.path();
                return self.load_from_path(&snapshot_path);
            }
        }

        Err(trustformers_core::errors::TrustformersError::io_error(
            format!("Failed to find downloaded model files for {}", model_name),
        ))
    }

    /// Download model from HuggingFace Hub
    fn download_from_huggingface_hub(
        &self,
        model_name: &str,
        model_base_path: &std::path::Path,
    ) -> Result<()> {
        use std::process::Command;

        println!(
            "Downloading model {} from HuggingFace Hub to {:?}",
            model_name, model_base_path
        );

        // Create the model directory and snapshots subdirectory
        let snapshots_path = model_base_path.join("snapshots").join("main");
        std::fs::create_dir_all(&snapshots_path).map_err(|e| {
            trustformers_core::errors::TrustformersError::io_error(format!(
                "Failed to create model directory: {}",
                e
            ))
        })?;

        // List of essential files for CLIP models
        let essential_files = vec![
            "config.json",
            "tokenizer.json",
            "tokenizer_config.json",
            "pytorch_model.bin", // Try .bin first
            "model.safetensors", // Fall back to safetensors
        ];

        let base_url = format!("https://huggingface.co/{}/resolve/main", model_name);

        // Try to download each essential file
        for file_name in &essential_files {
            let file_url = format!("{}/{}", base_url, file_name);
            let file_path = snapshots_path.join(file_name);

            println!("Attempting to download {}", file_url);

            // Convert path to string once for both commands
            let file_path_str = file_path.to_str().ok_or_else(|| {
                TrustformersError::invalid_config(format!("Invalid UTF-8 in path: {:?}", file_path))
            })?;

            // Try using curl first
            let curl_result = Command::new("curl")
                .args([
                    "-L", // Follow redirects
                    "-f", // Fail on HTTP errors
                    "-o",
                    file_path_str,
                    &file_url,
                ])
                .output();

            match curl_result {
                Ok(output) if output.status.success() => {
                    println!("Successfully downloaded {}", file_name);
                    continue;
                },
                Ok(output) => {
                    eprintln!(
                        "Failed to download {} with curl: {}",
                        file_name,
                        String::from_utf8_lossy(&output.stderr)
                    );
                },
                Err(e) => {
                    println!("curl not available: {}", e);
                },
            }

            // Try using wget as fallback
            let wget_result = Command::new("wget").args(["-O", file_path_str, &file_url]).output();

            match wget_result {
                Ok(output) if output.status.success() => {
                    println!("Successfully downloaded {} with wget", file_name);
                    continue;
                },
                Ok(output) => {
                    eprintln!(
                        "Failed to download {} with wget: {}",
                        file_name,
                        String::from_utf8_lossy(&output.stderr)
                    );
                },
                Err(e) => {
                    println!("wget not available: {}", e);
                },
            }

            // If essential files like config.json or pytorch_model.bin fail, return error
            if matches!(file_name, &"config.json" | &"pytorch_model.bin") {
                return Err(trustformers_core::errors::TrustformersError::io_error(format!(
                    "Failed to download essential file {} for model {}. Please ensure curl or wget is installed and you have internet access.",
                    file_name, model_name
                )));
            }
        }

        println!(
            "Successfully downloaded model {} from HuggingFace Hub",
            model_name
        );
        Ok(())
    }

    /// Load model weights layer-by-layer (chunked) to reduce peak memory usage.
    ///
    /// Instead of loading every tensor at once, this method loads weights one
    /// encoder layer at a time and calls the user-supplied `progress` callback
    /// after each logical chunk has been loaded.  The callback receives:
    ///   - `chunk_index`: zero-based index of the chunk just loaded
    ///   - `total_chunks`: total number of chunks that will be loaded
    ///   - `description`: a human-readable label for the chunk
    ///
    /// Errors from individual tensor loads are collected and returned as a
    /// single combined error at the end, rather than panicking.
    pub fn load_weights_chunked<F>(
        &mut self,
        model_path: impl AsRef<std::path::Path>,
        mut progress: F,
    ) -> Result<()>
    where
        F: FnMut(usize, usize, &str),
    {
        use crate::weight_loading::{auto_create_loader, WeightLoadingConfig};

        let config = WeightLoadingConfig {
            lazy_loading: true,
            memory_mapped: false,
            ..Default::default()
        };

        let mut loader = auto_create_loader(model_path, Some(config))?;

        // Calculate total chunks:
        //   1  text embeddings
        // + N  text encoder layers
        //   1  text final layer norm
        //   1  vision embeddings
        // + M  vision encoder layers
        //   1  vision layer norm
        //   1  projections + logit_scale
        let text_layers = self.text_model.encoder.layers.len();
        let vision_layers = self.vision_model.encoder.layers.len();
        let total_chunks = 1 + text_layers + 1 + 1 + vision_layers + 1 + 1;

        let mut chunk: usize = 0;
        let mut errors: Vec<String> = Vec::new();

        // --- Text model ---
        // Chunk: text embeddings
        if let Err(e) = self
            .text_model
            .embeddings
            .load_weights(loader.as_mut(), "text_model.embeddings")
        {
            errors.push(format!("text_model.embeddings: {e}"));
        }
        progress(chunk, total_chunks, "text_model.embeddings");
        chunk += 1;

        // Chunks: text encoder layers (one per layer)
        for i in 0..text_layers {
            let prefix = format!("text_model.encoder.layers.{i}");
            if let Err(e) = self.text_model.encoder.layers[i].load_weights(loader.as_mut(), &prefix)
            {
                errors.push(format!("{prefix}: {e}"));
            }
            progress(chunk, total_chunks, &prefix);
            chunk += 1;
        }

        // Chunk: text final layer norm
        {
            let prefix = "text_model.final_layer_norm";
            if let Ok(w) = loader.load_tensor(&format!("{prefix}.weight")) {
                if let Err(e) = self.text_model.final_layer_norm.set_weight(w) {
                    errors.push(format!("{prefix}.weight: {e}"));
                }
            }
            if let Ok(b) = loader.load_tensor(&format!("{prefix}.bias")) {
                if let Err(e) = self.text_model.final_layer_norm.set_bias(b) {
                    errors.push(format!("{prefix}.bias: {e}"));
                }
            }
            progress(chunk, total_chunks, prefix);
            chunk += 1;
        }

        // --- Vision model ---
        // Chunk: vision embeddings
        if let Err(e) = self
            .vision_model
            .embeddings
            .load_weights(loader.as_mut(), "vision_model.embeddings")
        {
            errors.push(format!("vision_model.embeddings: {e}"));
        }
        progress(chunk, total_chunks, "vision_model.embeddings");
        chunk += 1;

        // Chunks: vision encoder layers (one per layer)
        for i in 0..vision_layers {
            let prefix = format!("vision_model.encoder.layers.{i}");
            if let Err(e) =
                self.vision_model.encoder.layers[i].load_weights(loader.as_mut(), &prefix)
            {
                errors.push(format!("{prefix}: {e}"));
            }
            progress(chunk, total_chunks, &prefix);
            chunk += 1;
        }

        // Chunk: vision layer norm
        {
            let prefix = "vision_model.layernorm";
            if let Ok(w) = loader.load_tensor(&format!("{prefix}.weight")) {
                if let Err(e) = self.vision_model.layernorm.set_weight(w) {
                    errors.push(format!("{prefix}.weight: {e}"));
                }
            }
            if let Ok(b) = loader.load_tensor(&format!("{prefix}.bias")) {
                if let Err(e) = self.vision_model.layernorm.set_bias(b) {
                    errors.push(format!("{prefix}.bias: {e}"));
                }
            }
            progress(chunk, total_chunks, prefix);
            chunk += 1;
        }

        // Chunk: projections + logit_scale
        {
            if let Ok(w) = loader.load_tensor("text_projection.weight") {
                if let Err(e) = self.text_projection.set_weight(w) {
                    errors.push(format!("text_projection.weight: {e}"));
                }
            }
            if let Ok(b) = loader.load_tensor("text_projection.bias") {
                if let Err(e) = self.text_projection.set_bias(b) {
                    errors.push(format!("text_projection.bias: {e}"));
                }
            }
            if let Ok(w) = loader.load_tensor("visual_projection.weight") {
                if let Err(e) = self.visual_projection.set_weight(w) {
                    errors.push(format!("visual_projection.weight: {e}"));
                }
            }
            if let Ok(b) = loader.load_tensor("visual_projection.bias") {
                if let Err(e) = self.visual_projection.set_bias(b) {
                    errors.push(format!("visual_projection.bias: {e}"));
                }
            }
            if let Ok(logit_scale_weight) = loader.load_tensor("logit_scale") {
                self.logit_scale = logit_scale_weight;
            }
            progress(chunk, total_chunks, "projections");
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(TrustformersError::io_error(format!(
                "Errors during chunked weight loading:\n{}",
                errors.join("\n")
            )))
        }
    }

    /// Load weights with lazy loading for large models
    pub fn load_with_lazy_loading(
        &mut self,
        model_path: impl AsRef<std::path::Path>,
    ) -> Result<()> {
        use crate::weight_loading::{auto_create_loader, WeightLoadingConfig};

        let config = WeightLoadingConfig {
            lazy_loading: true,
            memory_mapped: true,
            streaming: false,
            ..Default::default()
        };

        let _loader = auto_create_loader(&model_path, Some(config))?;

        // For lazy loading, we set up the loader but don't load weights immediately
        // Weights are loaded on-demand during forward passes
        // This is useful for very large models that don't fit in memory

        // For now, just perform regular loading
        self.load_from_path(model_path)
    }
}

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

    fn small_text_config() -> CLIPTextConfig {
        CLIPTextConfig {
            vocab_size: 100,
            hidden_size: 32,
            intermediate_size: 64,
            num_hidden_layers: 2,
            num_attention_heads: 4,
            max_position_embeddings: 32,
            hidden_act: "quick_gelu".to_string(),
            layer_norm_eps: 1e-5,
            dropout: 0.0,
            attention_dropout: 0.0,
            initializer_range: 0.02,
            initializer_factor: 1.0,
            pad_token_id: 0,
            bos_token_id: 1,
            eos_token_id: 2,
        }
    }

    fn small_vision_config() -> CLIPVisionConfig {
        CLIPVisionConfig {
            hidden_size: 32,
            intermediate_size: 64,
            num_hidden_layers: 2,
            num_attention_heads: 4,
            image_size: 32,
            patch_size: 8,
            num_channels: 3,
            hidden_act: "quick_gelu".to_string(),
            layer_norm_eps: 1e-5,
            dropout: 0.0,
            attention_dropout: 0.0,
            initializer_range: 0.02,
            initializer_factor: 1.0,
        }
    }

    fn small_clip_config() -> CLIPConfig {
        CLIPConfig {
            text_config: small_text_config(),
            vision_config: small_vision_config(),
            projection_dim: 32,
            logit_scale_init_value: 2.6592,
            initializer_range: 0.02,
            initializer_factor: 1.0,
        }
    }

    #[test]
    fn test_clip_text_config_creation() {
        let config = small_text_config();
        assert_eq!(config.vocab_size, 100);
        assert_eq!(config.hidden_size, 32);
        assert_eq!(config.num_hidden_layers, 2);
    }

    #[test]
    fn test_clip_vision_config_creation() {
        let config = small_vision_config();
        assert_eq!(config.image_size, 32);
        assert_eq!(config.patch_size, 8);
        assert_eq!(config.num_channels, 3);
    }

    #[test]
    fn test_clip_config_creation() {
        let config = small_clip_config();
        assert_eq!(config.projection_dim, 32);
        assert!((config.logit_scale_init_value - 2.6592).abs() < 0.001);
    }

    #[test]
    fn test_clip_vision_config_num_patches() {
        let config = small_vision_config();
        let num_patches = config.num_patches();
        // (32/8)^2 = 16
        assert_eq!(num_patches, 16);
    }

    #[test]
    fn test_clip_vision_config_seq_length() {
        let config = small_vision_config();
        let seq_len = config.seq_length();
        // num_patches + 1 (for CLS token)
        assert_eq!(seq_len, 17);
    }

    #[test]
    fn test_clip_vision_embeddings_creation() {
        let config = small_vision_config();
        let result = CLIPVisionEmbeddings::new(&config);
        assert!(result.is_ok());
        let emb = result.expect("embeddings creation should succeed");
        assert!(matches!(emb.device(), Device::CPU));
    }

    #[test]
    fn test_clip_vision_embeddings_with_device() {
        let config = small_vision_config();
        let result = CLIPVisionEmbeddings::new_with_device(&config, Device::CPU);
        assert!(result.is_ok());
    }

    #[test]
    fn test_clip_encoder_layer_config() {
        let config = CLIPEncoderLayerConfig {
            hidden_size: 32,
            intermediate_size: 64,
            num_attention_heads: 4,
            hidden_act: "quick_gelu".to_string(),
            layer_norm_eps: 1e-5,
            dropout: 0.0,
            attention_dropout: 0.0,
        };
        assert_eq!(config.hidden_size, 32);
        assert_eq!(config.num_attention_heads, 4);
    }

    #[test]
    fn test_clip_model_creation() {
        let config = small_clip_config();
        let result = CLIPModel::new(config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_clip_model_with_device() {
        let config = small_clip_config();
        let result = CLIPModel::new_with_device(config, Device::CPU);
        assert!(result.is_ok());
        let model = result.expect("model creation should succeed");
        assert!(matches!(model.device(), Device::CPU));
    }

    #[test]
    fn test_clip_model_num_parameters() {
        let config = small_clip_config();
        let model = CLIPModel::new(config).expect("model creation should succeed");
        assert!(model.num_parameters() > 0);
    }

    #[test]
    fn test_clip_model_text_config_access() {
        let config = small_clip_config();
        let model = CLIPModel::new(config.clone()).expect("model creation should succeed");
        let mc = model.get_config();
        assert_eq!(mc.text_config.vocab_size, config.text_config.vocab_size);
    }

    #[test]
    fn test_clip_text_config_values() {
        let config = small_text_config();
        assert_eq!(config.vocab_size, 100);
        assert_eq!(config.hidden_size, 32);
        assert_eq!(config.num_hidden_layers, 2);
        assert_eq!(config.num_attention_heads, 4);
    }

    #[test]
    fn test_clip_vision_config_values() {
        let config = small_vision_config();
        assert_eq!(config.image_size, 32);
        assert_eq!(config.patch_size, 8);
        assert_eq!(config.hidden_size, 32);
    }

    #[test]
    fn test_clip_config_values() {
        let config = small_clip_config();
        assert_eq!(config.projection_dim, 32);
        assert_eq!(config.text_config.vocab_size, 100);
    }

    #[test]
    fn test_clip_model_projection_dim() {
        let config = small_clip_config();
        assert_eq!(config.projection_dim, 32);
        let model = CLIPModel::new(config).expect("model creation should succeed");
        // Model should have been created with the correct projection dim
        assert!(model.num_parameters() > 0);
    }

    #[test]
    fn test_clip_vision_different_patch_sizes() {
        for patch_size in &[4, 8, 16] {
            let mut config = small_vision_config();
            config.patch_size = *patch_size;
            let num_patches = config.num_patches();
            let expected = (config.image_size / patch_size).pow(2);
            assert_eq!(num_patches, expected);
        }
    }

    #[test]
    fn test_clip_vision_different_image_sizes() {
        for image_size in &[16, 32, 64] {
            let mut config = small_vision_config();
            config.image_size = *image_size;
            let num_patches = config.num_patches();
            let expected = (image_size / config.patch_size).pow(2);
            assert_eq!(num_patches, expected);
        }
    }

    #[test]
    fn test_clip_encoder_layer_config_creation() {
        let config = CLIPEncoderLayerConfig {
            hidden_size: 64,
            intermediate_size: 256,
            num_attention_heads: 8,
            hidden_act: "gelu".to_string(),
            layer_norm_eps: 1e-6,
            dropout: 0.1,
            attention_dropout: 0.1,
        };
        assert_eq!(config.hidden_size, 64);
        assert_eq!(config.num_attention_heads, 8);
    }

    #[test]
    fn test_clip_model_config_projection_dim() {
        let config = small_clip_config();
        let model = CLIPModel::new(config).expect("model creation should succeed");
        let mc = model.get_config();
        assert_eq!(mc.projection_dim, 32);
    }

    #[test]
    fn test_clip_config_initializer_values() {
        let config = small_clip_config();
        assert!((config.initializer_range - 0.02).abs() < f32::EPSILON);
        assert!((config.initializer_factor - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_clip_text_config_special_tokens() {
        let config = small_text_config();
        assert_eq!(config.pad_token_id, 0);
        assert_eq!(config.bos_token_id, 1);
        assert_eq!(config.eos_token_id, 2);
    }

    #[test]
    fn test_clip_encoder_layer_config_clone() {
        let config = CLIPEncoderLayerConfig {
            hidden_size: 32,
            intermediate_size: 64,
            num_attention_heads: 4,
            hidden_act: "gelu".to_string(),
            layer_norm_eps: 1e-5,
            dropout: 0.0,
            attention_dropout: 0.0,
        };
        let cloned = config.clone();
        assert_eq!(cloned.hidden_size, 32);
        assert_eq!(cloned.hidden_act, "gelu");
    }

    #[test]
    fn test_clip_vision_config_channels() {
        let config = small_vision_config();
        assert_eq!(config.num_channels, 3);
        assert_eq!(config.hidden_act, "quick_gelu");
    }

    #[test]
    fn test_clip_model_with_different_projection_dim() {
        let mut config = small_clip_config();
        config.projection_dim = 16;
        let result = CLIPModel::new(config);
        assert!(result.is_ok());
        let model = result.expect("model creation should succeed");
        assert!(model.num_parameters() > 0);
    }
}