trustformers-core 0.2.1

Core traits and utilities 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
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
//! Tensor expression templates for lazy evaluation.
//!
//! This module provides a system for lazy evaluation of tensor operations,
//! allowing complex expressions to be built up and optimized before evaluation.
//! This can lead to significant performance improvements by:
//!
//! - Eliminating intermediate tensor allocations
//! - Enabling operation fusion
//! - Optimizing memory access patterns
//! - Allowing vectorization of multiple operations
//!
//! # Example
//!
//! ```no_run
//! use trustformers_core::tensor::{Tensor, TensorExpr};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let a = Tensor::randn(&[1000, 1000])?;
//! let b = Tensor::randn(&[1000, 1000])?;
//! let c = Tensor::randn(&[1000, 1000])?;
//!
//! // Without lazy evaluation (creates intermediate tensors):
//! let result1 = (a.add(&b)?.mul(&c)?.relu()?).sum(None, false)?;
//!
//! // With lazy evaluation (no intermediate tensors):
//! let expr = TensorExpr::from(&a)?
//!     .add(TensorExpr::from(&b)?)?
//!     .mul(TensorExpr::from(&c)?)?
//!     .relu()?
//!     .sum(None)?;
//! let result2 = expr.eval()?;
//! # Ok(())
//! # }
//! ```

use crate::errors::{Result, TrustformersError};
use crate::tensor::{DType, Tensor};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;

/// Operation types for expression templates
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum OpType {
    // Arithmetic operations
    Add,
    Sub,
    Mul,
    Div,
    // Matrix operations
    MatMul,
    Transpose,
    // Activation functions
    ReLU,
    Sigmoid,
    Tanh,
    GELU,
    Softmax(i32), // axis
    // Reduction operations
    Sum(Option<Vec<usize>>),  // axes
    Mean(Option<Vec<usize>>), // axes
    Max(Option<Vec<usize>>),  // axes
    Min(Option<Vec<usize>>),  // axes
    // Shape operations
    Reshape(Vec<usize>),
    Slice(Vec<(usize, usize)>), // (start, end) for each dimension
    Concat(usize),              // axis
    // Broadcasting operations
    Broadcast(Vec<usize>), // target shape
    // Element-wise operations
    Pow(f64), // scalar power
    Sqrt,
    Log,
    Exp,
    // Comparison operations
    Greater,
    Less,
    Equal,
    // Conditional operations
    Where, // requires 3 operands: condition, x, y
    /// A chain of *unary* elementwise operations collapsed into a single node.
    ///
    /// The ops are stored in application order: `FusedElementwise(vec![Exp,
    /// Sqrt])` computes `sqrt(exp(x))`. Evaluating it performs one traversal of
    /// the data instead of one full-size allocation and traversal per link, so
    /// an `n`-op chain over an `N`-element tensor drops from `n` intermediate
    /// buffers to zero.
    ///
    /// Produced by [`TensorExpr::optimize_fusion`]; never built directly by the
    /// public expression API.
    FusedElementwise(Vec<OpType>),
}

impl OpType {
    /// Whether this op maps one input element to one output element with no
    /// dependence on any other element (so a chain of them can be fused into a
    /// single pass).
    ///
    /// `Softmax` is deliberately excluded: it reduces over an axis. Binary ops
    /// are elementwise but take two operands, so they are not part of a *unary*
    /// fusion chain.
    fn is_unary_elementwise(&self) -> bool {
        matches!(
            self,
            OpType::ReLU
                | OpType::Sigmoid
                | OpType::Tanh
                | OpType::GELU
                | OpType::Pow(_)
                | OpType::Sqrt
                | OpType::Log
                | OpType::Exp
        )
    }

    /// Apply a single unary elementwise op to one `f32` value.
    ///
    /// The formulas mirror the corresponding `Tensor` methods exactly (notably
    /// the tanh GELU approximation), so fusing a chain cannot change results.
    fn apply_scalar_f32(&self, x: f32) -> Result<f32> {
        Ok(match self {
            OpType::ReLU => x.max(0.0),
            OpType::Sigmoid => 1.0 / (1.0 + (-x).exp()),
            OpType::Tanh => x.tanh(),
            OpType::GELU => 0.5 * x * (1.0 + (0.7978845608 * (x + 0.044715 * x.powi(3))).tanh()),
            OpType::Pow(power) => x.powf(*power as f32),
            OpType::Sqrt => x.sqrt(),
            OpType::Log => x.ln(),
            OpType::Exp => x.exp(),
            other => {
                return Err(TrustformersError::tensor_op_error(
                    &format!("{:?} is not a unary elementwise operation", other),
                    "OpType::apply_scalar_f32",
                ));
            },
        })
    }

    /// `f64` counterpart of [`OpType::apply_scalar_f32`].
    fn apply_scalar_f64(&self, x: f64) -> Result<f64> {
        Ok(match self {
            OpType::ReLU => x.max(0.0),
            OpType::Sigmoid => 1.0 / (1.0 + (-x).exp()),
            OpType::Tanh => x.tanh(),
            OpType::GELU => 0.5 * x * (1.0 + (0.7978845608 * (x + 0.044715 * x.powi(3))).tanh()),
            OpType::Pow(power) => x.powf(*power),
            OpType::Sqrt => x.sqrt(),
            OpType::Log => x.ln(),
            OpType::Exp => x.exp(),
            other => {
                return Err(TrustformersError::tensor_op_error(
                    &format!("{:?} is not a unary elementwise operation", other),
                    "OpType::apply_scalar_f64",
                ));
            },
        })
    }
}

/// Expression node in the computation graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExprNode {
    pub id: usize,
    pub op: OpType,
    pub operands: Vec<usize>, // IDs of operand nodes
    pub shape: Vec<usize>,
    pub dtype: DType,
    pub is_leaf: bool, // true for tensor constants
    #[serde(skip)]
    pub tensor_data: Option<Arc<Tensor>>, // only for leaf nodes
}

/// Tensor expression for lazy evaluation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TensorExpr {
    nodes: HashMap<usize, ExprNode>,
    root: usize,
    next_id: usize,
}

/// Expression builder for fluent API
#[allow(dead_code)] // Reserved for future expression building features
pub struct ExprBuilder<'a> {
    expr: &'a mut TensorExpr,
    current_node: usize,
}

/// Optimization hints for expression evaluation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationHints {
    /// Enable operation fusion
    pub enable_fusion: bool,
    /// Enable memory layout optimization
    pub optimize_memory_layout: bool,
    /// Enable vectorization
    pub enable_vectorization: bool,
    /// Maximum number of operations to fuse
    pub max_fusion_size: usize,
    /// Prefer in-place operations when possible
    pub prefer_inplace: bool,
}

/// Expression evaluation context
#[derive(Debug, Clone, Default)]
pub struct EvalContext {
    pub hints: OptimizationHints,
    pub device: Option<String>,
    pub memory_budget: Option<usize>, // bytes
}

impl fmt::Display for TensorExpr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.node_to_string(self.root))
    }
}

impl TensorExpr {
    /// Create a new expression from a tensor
    pub fn from(tensor: &Tensor) -> Result<Self> {
        let shape = tensor.shape();
        let dtype = tensor.dtype();

        let mut nodes = HashMap::new();
        let root_node = ExprNode {
            id: 0,
            op: OpType::Add, // dummy op for leaf nodes
            operands: vec![],
            shape,
            dtype,
            is_leaf: true,
            tensor_data: Some(Arc::new(tensor.clone())),
        };

        nodes.insert(0, root_node);

        Ok(TensorExpr {
            nodes,
            root: 0,
            next_id: 1,
        })
    }

    /// Create a constant expression
    pub fn constant(tensor: Tensor) -> Result<Self> {
        Self::from(&tensor)
    }

    /// Get the shape of the expression result
    pub fn shape(&self) -> Vec<usize> {
        self.nodes[&self.root].shape.clone()
    }

    /// Get the data type of the expression result
    pub fn dtype(&self) -> DType {
        self.nodes[&self.root].dtype
    }

    /// Add two expressions
    #[allow(clippy::should_implement_trait)] // Returns Result for error handling
    pub fn add(self, other: TensorExpr) -> Result<Self> {
        self.binary_op(other, OpType::Add)
    }

    /// Subtract two expressions
    #[allow(clippy::should_implement_trait)] // Returns Result for error handling
    pub fn sub(self, other: TensorExpr) -> Result<Self> {
        self.binary_op(other, OpType::Sub)
    }

    /// Multiply two expressions element-wise
    #[allow(clippy::should_implement_trait)] // Returns Result for error handling
    pub fn mul(self, other: TensorExpr) -> Result<Self> {
        self.binary_op(other, OpType::Mul)
    }

    /// Divide two expressions element-wise
    #[allow(clippy::should_implement_trait)] // Returns Result for error handling
    pub fn div(self, other: TensorExpr) -> Result<Self> {
        self.binary_op(other, OpType::Div)
    }

    /// Matrix multiplication
    pub fn matmul(mut self, other: TensorExpr) -> Result<Self> {
        // Collect shape information before borrowing
        let left_shape = self.nodes[&self.root].shape.clone();
        let right_shape = other.nodes[&other.root].shape.clone();

        if left_shape.len() < 2 || right_shape.len() < 2 {
            return Err(TrustformersError::tensor_op_error(
                "Matrix multiplication requires at least 2D tensors",
                "matmul_validate",
            ));
        }

        let left_cols = left_shape[left_shape.len() - 1];
        let right_rows = right_shape[right_shape.len() - 2];

        if left_cols != right_rows {
            return Err(TrustformersError::tensor_op_error(
                &format!(
                    "Incompatible shapes for matmul: {:?} x {:?}",
                    left_shape, right_shape
                ),
                "matmul_shape_check",
            ));
        }

        // Merge the other expression into this one
        let other_root = self.merge_expression(other)?;

        // Calculate result shape
        let mut result_shape = left_shape[..left_shape.len() - 1].to_vec();
        result_shape.push(right_shape[right_shape.len() - 1]);

        let new_node = ExprNode {
            id: self.next_id,
            op: OpType::MatMul,
            operands: vec![self.root, other_root],
            shape: result_shape,
            dtype: self.nodes[&self.root].dtype,
            is_leaf: false,
            tensor_data: None,
        };

        self.nodes.insert(self.next_id, new_node);
        self.root = self.next_id;
        self.next_id += 1;

        Ok(self)
    }

    /// Apply ReLU activation
    pub fn relu(self) -> Result<Self> {
        self.unary_op(OpType::ReLU)
    }

    /// Apply sigmoid activation
    pub fn sigmoid(self) -> Result<Self> {
        self.unary_op(OpType::Sigmoid)
    }

    /// Apply tanh activation
    pub fn tanh(self) -> Result<Self> {
        self.unary_op(OpType::Tanh)
    }

    /// Apply GELU activation
    pub fn gelu(self) -> Result<Self> {
        self.unary_op(OpType::GELU)
    }

    /// Apply softmax along the specified axis
    pub fn softmax(self, axis: i32) -> Result<Self> {
        self.unary_op(OpType::Softmax(axis))
    }

    /// Elementwise square root.
    ///
    /// `OpType::Sqrt` was already evaluated by `eval_recursive` but had no
    /// builder, so the operation was unreachable through the public API.
    pub fn sqrt(self) -> Result<Self> {
        self.unary_op(OpType::Sqrt)
    }

    /// Elementwise natural logarithm.
    pub fn log(self) -> Result<Self> {
        self.unary_op(OpType::Log)
    }

    /// Elementwise exponential.
    pub fn exp(self) -> Result<Self> {
        self.unary_op(OpType::Exp)
    }

    /// Elementwise power with a scalar exponent.
    pub fn pow(self, exponent: f64) -> Result<Self> {
        self.unary_op(OpType::Pow(exponent))
    }

    /// Sum along specified axes
    pub fn sum(mut self, axes: Option<Vec<usize>>) -> Result<Self> {
        let result_shape = if let Some(ref axes) = axes {
            let mut shape = self.nodes[&self.root].shape.clone();
            // Remove dimensions being summed (in reverse order to maintain indices)
            let mut sorted_axes = axes.clone();
            sorted_axes.sort_by(|a, b| b.cmp(a));
            for &axis in &sorted_axes {
                if axis >= shape.len() {
                    return Err(TrustformersError::tensor_op_error(
                        &format!(
                            "Axis {} out of bounds for tensor with {} dimensions",
                            axis,
                            shape.len()
                        ),
                        "reduce",
                    ));
                }
                shape.remove(axis);
            }
            shape
        } else {
            vec![] // scalar result
        };

        let new_node = ExprNode {
            id: self.next_id,
            op: OpType::Sum(axes),
            operands: vec![self.root],
            shape: result_shape,
            dtype: self.nodes[&self.root].dtype,
            is_leaf: false,
            tensor_data: None,
        };

        self.nodes.insert(self.next_id, new_node);
        self.root = self.next_id;
        self.next_id += 1;

        Ok(self)
    }

    /// Calculate mean along specified axes
    pub fn mean(mut self, axes: Option<Vec<usize>>) -> Result<Self> {
        let result_shape = if let Some(ref axes) = axes {
            let mut shape = self.nodes[&self.root].shape.clone();
            let mut sorted_axes = axes.clone();
            sorted_axes.sort_by(|a, b| b.cmp(a));
            for &axis in &sorted_axes {
                if axis >= shape.len() {
                    return Err(TrustformersError::tensor_op_error(
                        &format!(
                            "Axis {} out of bounds for tensor with {} dimensions",
                            axis,
                            shape.len()
                        ),
                        "reduce",
                    ));
                }
                shape.remove(axis);
            }
            shape
        } else {
            vec![] // scalar result
        };

        let new_node = ExprNode {
            id: self.next_id,
            op: OpType::Mean(axes),
            operands: vec![self.root],
            shape: result_shape,
            dtype: self.nodes[&self.root].dtype,
            is_leaf: false,
            tensor_data: None,
        };

        self.nodes.insert(self.next_id, new_node);
        self.root = self.next_id;
        self.next_id += 1;

        Ok(self)
    }

    /// Reshape the tensor
    pub fn reshape(mut self, shape: &[usize]) -> Result<Self> {
        // Validate that the total number of elements remains the same
        let current_shape = &self.nodes[&self.root].shape;
        let current_size: usize = current_shape.iter().product();
        let new_size: usize = shape.iter().product();

        if current_size != new_size {
            return Err(TrustformersError::tensor_op_error(
                &format!(
                    "Cannot reshape tensor with {} elements to shape with {} elements",
                    current_size, new_size
                ),
                "reshape",
            ));
        }

        let new_node = ExprNode {
            id: self.next_id,
            op: OpType::Reshape(shape.to_vec()),
            operands: vec![self.root],
            shape: shape.to_vec(),
            dtype: self.nodes[&self.root].dtype,
            is_leaf: false,
            tensor_data: None,
        };

        self.nodes.insert(self.next_id, new_node);
        self.root = self.next_id;
        self.next_id += 1;

        Ok(self)
    }

    /// Transpose the tensor
    pub fn transpose(mut self) -> Result<Self> {
        let current_shape = &self.nodes[&self.root].shape;
        if current_shape.len() < 2 {
            return Err(TrustformersError::tensor_op_error(
                "Transpose requires at least 2D tensor",
                "transpose",
            ));
        }

        let mut new_shape = current_shape.clone();
        let len = new_shape.len();
        new_shape.swap(len - 2, len - 1);

        let new_node = ExprNode {
            id: self.next_id,
            op: OpType::Transpose,
            operands: vec![self.root],
            shape: new_shape,
            dtype: self.nodes[&self.root].dtype,
            is_leaf: false,
            tensor_data: None,
        };

        self.nodes.insert(self.next_id, new_node);
        self.root = self.next_id;
        self.next_id += 1;

        Ok(self)
    }

    /// Evaluate the expression with default context
    pub fn eval(&self) -> Result<Tensor> {
        self.eval_with_context(&EvalContext::default())
    }

    /// Evaluate the expression with optimization context
    pub fn eval_with_context(&self, context: &EvalContext) -> Result<Tensor> {
        // First, optimize the expression if requested. `max_fusion_size` caps how
        // many links one fused node may absorb.
        let optimized_expr = if context.hints.enable_fusion {
            self.optimize_fusion_with_limit(context.hints.max_fusion_size)?
        } else {
            self.clone()
        };

        // Evaluate the optimized expression
        optimized_expr.eval_recursive(optimized_expr.root, context)
    }

    /// Check if two expressions can be fused
    pub fn can_fuse_with(&self, other: &TensorExpr) -> bool {
        // Simple heuristic: same shape and compatible operations
        self.shape() == other.shape() && self.is_elementwise() && other.is_elementwise()
    }

    /// Get the number of operations in the expression
    pub fn operation_count(&self) -> usize {
        self.nodes.len() - self.leaf_count()
    }

    /// Get the number of leaf nodes (tensors)
    pub fn leaf_count(&self) -> usize {
        self.nodes.values().filter(|n| n.is_leaf).count()
    }

    /// Export expression to DOT format for visualization
    pub fn to_dot(&self) -> String {
        let mut dot = String::from("digraph TensorExpr {\n");

        for node in self.nodes.values() {
            let label = if node.is_leaf {
                format!("Tensor\\n{:?}\\n{:?}", node.shape, node.dtype)
            } else {
                format!("{:?}\\n{:?}\\n{:?}", node.op, node.shape, node.dtype)
            };

            let color = if node.is_leaf { "lightblue" } else { "lightgreen" };
            dot.push_str(&format!(
                "  {} [label=\"{}\" fillcolor={} style=filled];\n",
                node.id, label, color
            ));

            for &operand in &node.operands {
                dot.push_str(&format!("  {} -> {};\n", operand, node.id));
            }
        }

        dot.push_str("}\n");
        dot
    }

    // Helper methods

    fn binary_op(mut self, other: TensorExpr, op: OpType) -> Result<Self> {
        // Check shape compatibility for broadcasting
        let left_shape = &self.nodes[&self.root].shape;
        let right_shape = &other.nodes[&other.root].shape;
        let result_shape = self.broadcast_shapes(left_shape, right_shape)?;

        // Merge the other expression into this one
        let other_root = self.merge_expression(other)?;

        let new_node = ExprNode {
            id: self.next_id,
            op,
            operands: vec![self.root, other_root],
            shape: result_shape,
            dtype: self.nodes[&self.root].dtype, // Assume same dtype for now
            is_leaf: false,
            tensor_data: None,
        };

        self.nodes.insert(self.next_id, new_node);
        self.root = self.next_id;
        self.next_id += 1;

        Ok(self)
    }

    fn unary_op(mut self, op: OpType) -> Result<Self> {
        let new_node = ExprNode {
            id: self.next_id,
            op,
            operands: vec![self.root],
            shape: self.nodes[&self.root].shape.clone(),
            dtype: self.nodes[&self.root].dtype,
            is_leaf: false,
            tensor_data: None,
        };

        self.nodes.insert(self.next_id, new_node);
        self.root = self.next_id;
        self.next_id += 1;

        Ok(self)
    }

    fn merge_expression(&mut self, other: TensorExpr) -> Result<usize> {
        let id_offset = self.next_id;

        // Add all nodes from the other expression with updated IDs
        for (old_id, mut node) in other.nodes {
            let new_id = old_id + id_offset;
            node.id = new_id;

            // Update operand IDs
            for operand in &mut node.operands {
                *operand += id_offset;
            }

            self.nodes.insert(new_id, node);
        }

        self.next_id += other.next_id;
        Ok(other.root + id_offset)
    }

    fn broadcast_shapes(&self, left: &[usize], right: &[usize]) -> Result<Vec<usize>> {
        let max_len = left.len().max(right.len());
        let mut result = vec![1; max_len];

        for i in 0..max_len {
            let left_dim = if i < left.len() { left[left.len() - 1 - i] } else { 1 };
            let right_dim = if i < right.len() { right[right.len() - 1 - i] } else { 1 };

            if left_dim == right_dim {
                result[max_len - 1 - i] = left_dim;
            } else if left_dim == 1 {
                result[max_len - 1 - i] = right_dim;
            } else if right_dim == 1 {
                result[max_len - 1 - i] = left_dim;
            } else {
                return Err(TrustformersError::tensor_op_error(
                    &format!("Cannot broadcast shapes {:?} and {:?}", left, right),
                    "broadcast_shape_check",
                ));
            }
        }

        Ok(result)
    }

    fn is_elementwise(&self) -> bool {
        matches!(
            self.nodes[&self.root].op,
            OpType::Add
                | OpType::Sub
                | OpType::Mul
                | OpType::Div
                | OpType::ReLU
                | OpType::Sigmoid
                | OpType::Tanh
                | OpType::GELU
                | OpType::Pow(_)
                | OpType::Sqrt
                | OpType::Log
                | OpType::Exp
        )
    }

    /// Rewrite chains of unary elementwise operations into single
    /// [`OpType::FusedElementwise`] nodes.
    ///
    /// This is a real rewrite, not a report: the returned expression has fewer
    /// nodes, and evaluating it performs one traversal per fused chain instead
    /// of one full-size allocation and traversal per link. The previous version
    /// discovered chains and then discarded them (`fuse_operations` had an empty
    /// body), so `optimize_fusion` returned an unmodified clone.
    ///
    /// Only chains whose intermediate nodes have exactly one consumer are fused
    /// -- a shared intermediate is needed by another branch, so collapsing it
    /// would force it to be recomputed.
    pub fn optimize_fusion(&self) -> Result<TensorExpr> {
        self.optimize_fusion_with_limit(usize::MAX)
    }

    /// [`TensorExpr::optimize_fusion`] with an explicit cap on how many
    /// operations one fused node may absorb (`OptimizationHints::max_fusion_size`).
    fn optimize_fusion_with_limit(&self, max_chain: usize) -> Result<TensorExpr> {
        if max_chain < 2 {
            return Ok(self.clone());
        }

        let mut optimized = self.clone();
        for chain in self.find_fusion_chains(max_chain) {
            optimized.fuse_operations(&chain)?;
        }
        optimized.prune_unreachable_nodes();
        Ok(optimized)
    }

    /// How many nodes consume each node's output.
    fn consumer_counts(&self) -> HashMap<usize, usize> {
        let mut counts: HashMap<usize, usize> = HashMap::new();
        for node in self.nodes.values() {
            for &operand in &node.operands {
                *counts.entry(operand).or_insert(0) += 1;
            }
        }
        counts
    }

    /// Find maximal chains of unary elementwise nodes.
    ///
    /// A chain is returned outermost-first (`[outer, ..., inner]`); the node
    /// *below* the innermost link stays untouched and becomes the fused node's
    /// single operand.
    fn find_fusion_chains(&self, max_len: usize) -> Vec<Vec<usize>> {
        if max_len < 2 {
            return Vec::new();
        }

        let consumers = self.consumer_counts();

        // Visit consumers before their operands, so a chain always starts at the
        // outermost link that is still free. Without that order, capping a chain
        // (`max_len`) would strand the released interior nodes: they would still
        // look like the middle of a longer chain and be skipped forever.
        let node_ids = self.nodes_outermost_first();

        let mut claimed = std::collections::HashSet::new();
        let mut chains = Vec::new();

        for node_id in node_ids {
            if claimed.contains(&node_id) {
                continue;
            }
            if !self.is_fusable_link(node_id) {
                continue;
            }
            // Skip nodes that are still the interior of a chain rooted higher up.
            // Once that root has been processed the consumer is claimed, and this
            // node becomes a legal chain start on a later iteration.
            if self.has_unclaimed_fusable_sole_consumer(node_id, &consumers, &claimed) {
                continue;
            }

            let mut chain = vec![node_id];
            let mut current = node_id;
            while chain.len() < max_len {
                let Some(node) = self.nodes.get(&current) else {
                    break;
                };
                let Some(&operand) = node.operands.first() else {
                    break;
                };
                if !self.is_fusable_link(operand) {
                    break;
                }
                // Only absorb an intermediate that nothing else consumes.
                if consumers.get(&operand).copied().unwrap_or(0) != 1 {
                    break;
                }
                chain.push(operand);
                current = operand;
            }

            if chain.len() >= 2 {
                for &id in &chain {
                    claimed.insert(id);
                }
                chains.push(chain);
            }
        }

        chains
    }

    /// Whether `node_id` is a non-leaf, single-operand, unary elementwise node.
    fn is_fusable_link(&self, node_id: usize) -> bool {
        match self.nodes.get(&node_id) {
            Some(node) => {
                !node.is_leaf && node.operands.len() == 1 && node.op.is_unary_elementwise()
            },
            None => false,
        }
    }

    fn has_unclaimed_fusable_sole_consumer(
        &self,
        node_id: usize,
        consumers: &HashMap<usize, usize>,
        claimed: &std::collections::HashSet<usize>,
    ) -> bool {
        if consumers.get(&node_id).copied().unwrap_or(0) != 1 {
            return false;
        }
        self.nodes.values().any(|candidate| {
            candidate.operands.first() == Some(&node_id)
                && self.is_fusable_link(candidate.id)
                && !claimed.contains(&candidate.id)
        })
    }

    /// Node ids ordered so that every node precedes its operands (root first).
    ///
    /// Nodes unreachable from the root are appended in id order so the traversal
    /// is total and deterministic.
    fn nodes_outermost_first(&self) -> Vec<usize> {
        let mut order = Vec::with_capacity(self.nodes.len());
        let mut seen = std::collections::HashSet::new();
        let mut queue = std::collections::VecDeque::new();
        queue.push_back(self.root);

        while let Some(id) = queue.pop_front() {
            if !seen.insert(id) {
                continue;
            }
            order.push(id);
            if let Some(node) = self.nodes.get(&id) {
                for &operand in &node.operands {
                    queue.push_back(operand);
                }
            }
        }

        let mut orphans: Vec<usize> =
            self.nodes.keys().copied().filter(|id| !seen.contains(id)).collect();
        orphans.sort_unstable();
        order.extend(orphans);
        order
    }

    /// Replace `chain` (outermost first) with a single `FusedElementwise` node.
    ///
    /// The outermost node keeps its id, so every consumer -- including `root` --
    /// keeps pointing at it; only its `op` and `operands` change.
    fn fuse_operations(&mut self, chain: &[usize]) -> Result<()> {
        if chain.len() < 2 {
            return Ok(());
        }

        // Collect the ops innermost-first, which is application order.
        let mut ops = Vec::with_capacity(chain.len());
        for &node_id in chain.iter().rev() {
            let node = self.nodes.get(&node_id).ok_or_else(|| {
                TrustformersError::tensor_op_error(
                    &format!("fusion chain references unknown node {}", node_id),
                    "TensorExpr::fuse_operations",
                )
            })?;
            // Flatten a previously fused node so repeated passes stay linear.
            match &node.op {
                OpType::FusedElementwise(inner) => ops.extend(inner.iter().cloned()),
                other => ops.push(other.clone()),
            }
        }

        let innermost = *chain.last().unwrap_or(&chain[0]);
        let source = self
            .nodes
            .get(&innermost)
            .and_then(|node| node.operands.first().copied())
            .ok_or_else(|| {
                TrustformersError::tensor_op_error(
                    "innermost fusion link has no operand",
                    "TensorExpr::fuse_operations",
                )
            })?;

        let outermost = chain[0];
        let fused = self.nodes.get_mut(&outermost).ok_or_else(|| {
            TrustformersError::tensor_op_error(
                &format!("fusion chain references unknown node {}", outermost),
                "TensorExpr::fuse_operations",
            )
        })?;
        fused.op = OpType::FusedElementwise(ops);
        fused.operands = vec![source];

        // The interior nodes are now unreachable; `prune_unreachable_nodes`
        // removes them once every chain has been rewritten.
        Ok(())
    }

    /// Evaluate a fused chain of unary elementwise ops in a single traversal.
    ///
    /// One allocation for the output; every intermediate stays in a register.
    /// The per-op formulas are the same ones the unfused `Tensor` methods use,
    /// so results are bit-identical to evaluating the chain link by link
    /// (modulo the intermediate rounding that no longer happens through memory).
    fn eval_fused_elementwise(input: &Tensor, ops: &[OpType]) -> Result<Tensor> {
        if ops.is_empty() {
            return Ok(input.clone());
        }
        for op in ops {
            if !op.is_unary_elementwise() {
                return Err(TrustformersError::tensor_op_error(
                    &format!("{:?} cannot appear in a fused elementwise chain", op),
                    "eval_fused_elementwise",
                ));
            }
        }

        match input {
            Tensor::F32(a) => {
                let mut result = a.as_standard_layout().into_owned();
                for value in result.iter_mut() {
                    let mut acc = *value;
                    for op in ops {
                        acc = op.apply_scalar_f32(acc)?;
                    }
                    *value = acc;
                }
                Ok(Tensor::F32(result))
            },
            Tensor::F64(a) => {
                let mut result = a.as_standard_layout().into_owned();
                for value in result.iter_mut() {
                    let mut acc = *value;
                    for op in ops {
                        acc = op.apply_scalar_f64(acc)?;
                    }
                    *value = acc;
                }
                Ok(Tensor::F64(result))
            },
            // Integer / complex / device tensors have no fused kernel: fall back
            // to applying the chain one operation at a time, which is exactly
            // what an unfused expression would have done.
            other => {
                let mut current = other.clone();
                for op in ops {
                    current = match op {
                        OpType::ReLU => current.relu()?,
                        OpType::Sigmoid => current.sigmoid()?,
                        OpType::Tanh => current.tanh()?,
                        OpType::GELU => current.gelu()?,
                        OpType::Pow(power) => current.pow_scalar(*power)?,
                        OpType::Sqrt => current.sqrt()?,
                        OpType::Log => current.log()?,
                        OpType::Exp => current.exp()?,
                        unsupported => {
                            return Err(TrustformersError::tensor_op_error(
                                &format!(
                                    "{:?} cannot appear in a fused elementwise chain",
                                    unsupported
                                ),
                                "eval_fused_elementwise",
                            ));
                        },
                    };
                }
                Ok(current)
            },
        }
    }

    /// Drop nodes no longer reachable from the root.
    fn prune_unreachable_nodes(&mut self) {
        let mut reachable = std::collections::HashSet::new();
        let mut stack = vec![self.root];
        while let Some(id) = stack.pop() {
            if !reachable.insert(id) {
                continue;
            }
            if let Some(node) = self.nodes.get(&id) {
                stack.extend(node.operands.iter().copied());
            }
        }
        self.nodes.retain(|id, _| reachable.contains(id));
    }

    fn eval_recursive(&self, node_id: usize, _context: &EvalContext) -> Result<Tensor> {
        let node = &self.nodes[&node_id];

        if node.is_leaf {
            return node
                .tensor_data
                .as_ref()
                .ok_or_else(|| {
                    TrustformersError::tensor_op_error(
                        "Leaf node must have tensor data",
                        "eval_recursive",
                    )
                })
                .map(|t| t.as_ref().clone());
        }

        // Evaluate operands first
        let operand_results: Result<Vec<Tensor>> =
            node.operands.iter().map(|&id| self.eval_recursive(id, _context)).collect();
        let operands = operand_results?;

        // Apply the operation
        match &node.op {
            OpType::Add => operands[0].add(&operands[1]),
            OpType::Sub => operands[0].sub(&operands[1]),
            OpType::Mul => operands[0].mul(&operands[1]),
            OpType::Div => operands[0].div(&operands[1]),
            OpType::MatMul => operands[0].matmul(&operands[1]),
            OpType::Transpose => {
                let shape = operands[0].shape();
                let rank = shape.len();
                if rank < 2 {
                    return Err(crate::errors::TrustformersError::dimension_mismatch(
                        "at least 2 dimensions".to_string(),
                        format!("{} dimensions", rank),
                    ));
                }
                operands[0].transpose(rank - 2, rank - 1)
            },
            OpType::ReLU => operands[0].relu(),
            OpType::Sigmoid => operands[0].sigmoid(),
            OpType::Tanh => operands[0].tanh(),
            OpType::GELU => operands[0].gelu(),
            OpType::FusedElementwise(ops) => Self::eval_fused_elementwise(&operands[0], ops),
            OpType::Softmax(axis) => operands[0].softmax(*axis),
            OpType::Sum(axes) => {
                match axes {
                    Some(ref axes_vec) => operands[0].sum_axes(axes_vec),
                    None => {
                        // Sum all elements - use all axes
                        let shape = operands[0].shape();
                        let all_axes: Vec<usize> = (0..shape.len()).collect();
                        operands[0].sum_axes(&all_axes)
                    },
                }
            },
            OpType::Mean(axes) => match axes {
                Some(ref axes_vec) => operands[0].mean_axes(axes_vec),
                None => operands[0].mean(),
            },
            OpType::Reshape(shape) => operands[0].reshape(shape),
            OpType::Pow(power) => operands[0].pow_scalar(*power),
            OpType::Sqrt => operands[0].sqrt(),
            OpType::Log => operands[0].log(),
            OpType::Exp => operands[0].exp(),
            OpType::Max(axes) => match axes {
                Some(ref axes_vec) => operands[0].max_axes(axes_vec),
                None => operands[0].max_scalar(),
            },
            OpType::Min(axes) => match axes {
                Some(ref axes_vec) => operands[0].min_axes(axes_vec),
                None => operands[0].min_scalar(),
            },
            OpType::Slice(ranges) => {
                // Implement proper multi-dimensional slicing
                if ranges.is_empty() {
                    return Err(TrustformersError::tensor_op_error(
                        "No slice ranges provided",
                        "slice",
                    ));
                }
                operands[0].slice_multi(ranges)
            },
            OpType::Concat(axis) => {
                if operands.len() < 2 {
                    return Err(TrustformersError::tensor_op_error(
                        "Concat requires at least 2 operands",
                        "evaluate_node",
                    ));
                }

                // Pass slice of tensors directly for concatenation
                Tensor::concat(&operands, *axis)
            },
            OpType::Broadcast(target_shape) => operands[0].broadcast_to(target_shape),
            OpType::Greater => {
                if operands.len() != 2 {
                    return Err(TrustformersError::tensor_op_error(
                        "Greater operation requires exactly 2 operands",
                        "evaluate_node",
                    ));
                }
                operands[0].greater(&operands[1])
            },
            OpType::Less => {
                if operands.len() != 2 {
                    return Err(TrustformersError::tensor_op_error(
                        "Less operation requires exactly 2 operands",
                        "evaluate_node",
                    ));
                }
                operands[0].less(&operands[1])
            },
            OpType::Equal => {
                if operands.len() != 2 {
                    return Err(TrustformersError::tensor_op_error(
                        "Equal operation requires exactly 2 operands",
                        "evaluate_node",
                    ));
                }
                operands[0].equal(&operands[1])
            },
            OpType::Where => {
                if operands.len() != 3 {
                    return Err(TrustformersError::tensor_op_error(
                        "Where operation requires exactly 3 operands: condition, x, y",
                        "evaluate_node",
                    ));
                }
                // where(condition, x, y) - select x where condition is true, y otherwise
                operands[0].where_cond(&operands[1], &operands[2])
            },
        }
    }

    fn node_to_string(&self, node_id: usize) -> String {
        let node = &self.nodes[&node_id];

        if node.is_leaf {
            format!("Tensor{:?}", node.shape)
        } else {
            let operand_strs: Vec<String> =
                node.operands.iter().map(|&id| self.node_to_string(id)).collect();

            match &node.op {
                OpType::Add => format!("({} + {})", operand_strs[0], operand_strs[1]),
                OpType::Sub => format!("({} - {})", operand_strs[0], operand_strs[1]),
                OpType::Mul => format!("({} * {})", operand_strs[0], operand_strs[1]),
                OpType::Div => format!("({} / {})", operand_strs[0], operand_strs[1]),
                OpType::MatMul => format!("matmul({}, {})", operand_strs[0], operand_strs[1]),
                OpType::ReLU => format!("relu({})", operand_strs[0]),
                OpType::Sigmoid => format!("sigmoid({})", operand_strs[0]),
                OpType::Tanh => format!("tanh({})", operand_strs[0]),
                OpType::GELU => format!("gelu({})", operand_strs[0]),
                OpType::Softmax(axis) => format!("softmax({}, axis={})", operand_strs[0], axis),
                OpType::Sum(axes) => format!("sum({}, axes={:?})", operand_strs[0], axes),
                OpType::Mean(axes) => format!("mean({}, axes={:?})", operand_strs[0], axes),
                OpType::Reshape(shape) => format!("reshape({}, {:?})", operand_strs[0], shape),
                OpType::Transpose => format!("transpose({})", operand_strs[0]),
                OpType::Sqrt => format!("sqrt({})", operand_strs[0]),
                OpType::Log => format!("log({})", operand_strs[0]),
                OpType::Exp => format!("exp({})", operand_strs[0]),
                OpType::Pow(exponent) => format!("pow({}, {})", operand_strs[0], exponent),
                // Render a fused chain as the nested calls it replaced, so the
                // printed expression stays readable after `optimize_fusion`.
                OpType::FusedElementwise(ops) => {
                    let mut rendered =
                        operand_strs.first().cloned().unwrap_or_else(|| "<missing>".to_string());
                    for op in ops {
                        rendered = match op {
                            OpType::ReLU => format!("relu({})", rendered),
                            OpType::Sigmoid => format!("sigmoid({})", rendered),
                            OpType::Tanh => format!("tanh({})", rendered),
                            OpType::GELU => format!("gelu({})", rendered),
                            OpType::Sqrt => format!("sqrt({})", rendered),
                            OpType::Log => format!("log({})", rendered),
                            OpType::Exp => format!("exp({})", rendered),
                            OpType::Pow(exponent) => format!("pow({}, {})", rendered, exponent),
                            other => format!("{:?}({})", other, rendered),
                        };
                    }
                    format!("fused[{}]", rendered)
                },
                _ => format!("{:?}({})", node.op, operand_strs.join(", ")),
            }
        }
    }
}

impl Default for OptimizationHints {
    fn default() -> Self {
        Self {
            enable_fusion: true,
            optimize_memory_layout: true,
            enable_vectorization: true,
            max_fusion_size: 8,
            prefer_inplace: false,
        }
    }
}

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

    #[test]
    fn test_basic_expression_creation() -> Result<()> {
        let a = Tensor::ones(&[2, 3])?;
        let expr = TensorExpr::from(&a)?;

        assert_eq!(expr.shape(), vec![2, 3]);
        assert_eq!(expr.dtype(), DType::F32);
        assert_eq!(expr.operation_count(), 0);
        assert_eq!(expr.leaf_count(), 1);

        Ok(())
    }

    #[test]
    fn test_binary_operations() -> Result<()> {
        let a = Tensor::ones(&[2, 3])?;
        let b = Tensor::ones(&[2, 3])?;

        let expr_a = TensorExpr::from(&a)?;
        let expr_b = TensorExpr::from(&b)?;

        let result_expr = expr_a.add(expr_b)?;

        assert_eq!(result_expr.shape(), vec![2, 3]);
        assert_eq!(result_expr.operation_count(), 1);
        assert_eq!(result_expr.leaf_count(), 2);

        Ok(())
    }

    #[test]
    fn test_chained_operations() -> Result<()> {
        let a = Tensor::ones(&[2, 3])?;
        let b = Tensor::ones(&[2, 3])?;
        let c = Tensor::ones(&[2, 3])?;

        let expr = TensorExpr::from(&a)?
            .add(TensorExpr::from(&b)?)?
            .mul(TensorExpr::from(&c)?)?
            .relu()?;

        assert_eq!(expr.shape(), vec![2, 3]);
        assert_eq!(expr.operation_count(), 3); // add, mul, relu
        assert_eq!(expr.leaf_count(), 3);

        Ok(())
    }

    #[test]
    fn test_matrix_multiplication() -> Result<()> {
        let a = Tensor::ones(&[2, 3])?;
        let b = Tensor::ones(&[3, 4])?;

        let expr = TensorExpr::from(&a)?.matmul(TensorExpr::from(&b)?)?;

        assert_eq!(expr.shape(), vec![2, 4]);
        assert_eq!(expr.operation_count(), 1);

        Ok(())
    }

    #[test]
    fn test_reduction_operations() -> Result<()> {
        let a = Tensor::ones(&[2, 3, 4])?;

        let sum_all = TensorExpr::from(&a)?.sum(None)?;
        assert_eq!(sum_all.shape(), vec![] as Vec<usize>);

        let sum_axis = TensorExpr::from(&a)?.sum(Some(vec![1]))?;
        assert_eq!(sum_axis.shape(), vec![2, 4]);

        Ok(())
    }

    #[test]
    fn test_reshape_operation() -> Result<()> {
        let a = Tensor::ones(&[2, 3, 4])?;

        let reshaped = TensorExpr::from(&a)?.reshape(&[6, 4])?;
        assert_eq!(reshaped.shape(), vec![6, 4]);

        Ok(())
    }

    #[test]
    fn test_expression_evaluation() -> Result<()> {
        let a = Tensor::ones(&[2, 2])?;
        let b = Tensor::ones(&[2, 2])?;

        let expr = TensorExpr::from(&a)?.add(TensorExpr::from(&b)?)?;

        let result = expr.eval()?;
        assert_eq!(result.shape(), vec![2, 2]);

        // Result should be all 2.0s
        let _expected = Tensor::full_with_shape(&[2, 2], 2.0)?;
        // Note: Actual comparison would need tensor equality methods

        Ok(())
    }

    #[test]
    fn test_expression_to_string() -> Result<()> {
        let a = Tensor::ones(&[2, 2])?;
        let b = Tensor::ones(&[2, 2])?;

        let expr = TensorExpr::from(&a)?.add(TensorExpr::from(&b)?)?.relu()?;

        let expr_str = expr.to_string();
        assert!(expr_str.contains("+"));
        assert!(expr_str.contains("relu"));

        Ok(())
    }

    #[test]
    fn test_dot_export() -> Result<()> {
        let a = Tensor::ones(&[2, 2])?;
        let b = Tensor::ones(&[2, 2])?;

        let expr = TensorExpr::from(&a)?.add(TensorExpr::from(&b)?)?;

        let dot = expr.to_dot();
        assert!(dot.contains("digraph TensorExpr"));
        assert!(dot.contains("Add"));

        Ok(())
    }

    #[test]
    fn test_optimization_hints() {
        let hints = OptimizationHints::default();
        assert!(hints.enable_fusion);
        assert!(hints.optimize_memory_layout);
        assert!(hints.enable_vectorization);
        assert_eq!(hints.max_fusion_size, 8);
        assert!(!hints.prefer_inplace);
    }

    #[test]
    fn test_can_fuse_operations() -> Result<()> {
        let a = Tensor::ones(&[2, 2])?;
        let b = Tensor::ones(&[2, 2])?;

        let expr1 = TensorExpr::from(&a)?.relu()?;
        let expr2 = TensorExpr::from(&b)?.sigmoid()?;

        assert!(expr1.can_fuse_with(&expr2));

        Ok(())
    }

    // ------------------------------------------------------------------
    // Elementwise fusion
    // ------------------------------------------------------------------

    /// Regression test: `optimize_fusion` used to return an unmodified clone
    /// because `fuse_operations` had an empty body. The rewrite must actually
    /// shrink the graph.
    #[test]
    fn optimize_fusion_collapses_a_unary_chain() -> Result<()> {
        let a = Tensor::from_vec(vec![0.25f32, 1.0, 4.0, 9.0], &[4])?;
        // sqrt(exp(relu(x))) -- three fusable unary links over one leaf.
        let expr = TensorExpr::from(&a)?.relu()?.exp()?.sqrt()?;
        assert_eq!(expr.operation_count(), 3);

        let fused = expr.optimize_fusion()?;
        assert_eq!(
            fused.operation_count(),
            1,
            "three unary links must collapse into one fused node"
        );
        assert_eq!(fused.leaf_count(), 1);
        Ok(())
    }

    /// The fused node must carry the ops in application order.
    #[test]
    fn fused_node_records_the_chain_in_application_order() -> Result<()> {
        let a = Tensor::from_vec(vec![1.0f32, 2.0], &[2])?;
        let expr = TensorExpr::from(&a)?.relu()?.exp()?.sqrt()?;
        let fused = expr.optimize_fusion()?;

        let root_op = fused
            .nodes
            .get(&fused.root)
            .map(|node| node.op.clone())
            .expect("root node exists");
        match root_op {
            OpType::FusedElementwise(ops) => {
                assert_eq!(ops, vec![OpType::ReLU, OpType::Exp, OpType::Sqrt]);
            },
            other => panic!("expected a fused node, got {:?}", other),
        }
        Ok(())
    }

    /// Fusion must not change the numbers. Compared against a hand-computed
    /// reference: sqrt(exp(relu(x))) == exp(relu(x)/2).
    #[test]
    fn fused_evaluation_matches_the_unfused_result_and_a_hand_reference() -> Result<()> {
        let inputs = vec![-2.0f32, 0.0, 0.5, 2.0];
        let a = Tensor::from_vec(inputs.clone(), &[4])?;
        let expr = TensorExpr::from(&a)?.relu()?.exp()?.sqrt()?;

        let unfused_ctx = EvalContext {
            hints: OptimizationHints {
                enable_fusion: false,
                ..OptimizationHints::default()
            },
            ..EvalContext::default()
        };
        let unfused = expr.eval_with_context(&unfused_ctx)?.to_vec_f32()?;
        let fused = expr.eval()?.to_vec_f32()?;

        for (index, input) in inputs.iter().enumerate() {
            let expected = (input.max(0.0) / 2.0).exp();
            assert!(
                (fused[index] - expected).abs() < 1e-5,
                "fused[{index}] = {} but sqrt(exp(relu({input}))) = {expected}",
                fused[index]
            );
            assert!(
                (fused[index] - unfused[index]).abs() < 1e-5,
                "fusion changed the result at {index}: {} vs {}",
                fused[index],
                unfused[index]
            );
        }
        Ok(())
    }

    /// `max_fusion_size` must be honoured, and a limit below 2 disables fusion.
    #[test]
    fn max_fusion_size_caps_the_chain() -> Result<()> {
        let a = Tensor::from_vec(vec![1.0f32, 2.0, 3.0, 4.0], &[4])?;
        let expr = TensorExpr::from(&a)?.relu()?.exp()?.sqrt()?.tanh()?;
        assert_eq!(expr.operation_count(), 4);

        let capped = expr.optimize_fusion_with_limit(2)?;
        assert_eq!(
            capped.operation_count(),
            2,
            "a cap of 2 must leave two fused pairs"
        );

        let disabled = expr.optimize_fusion_with_limit(1)?;
        assert_eq!(
            disabled.operation_count(),
            4,
            "a cap below 2 disables fusion"
        );

        // The numbers survive either way.
        let reference = expr.eval()?.to_vec_f32()?;
        for value in capped.eval()?.to_vec_f32()?.iter().zip(reference.iter()) {
            assert!((value.0 - value.1).abs() < 1e-5);
        }
        Ok(())
    }

    /// A single unary op is not a chain and must be left alone.
    #[test]
    fn a_lone_unary_op_is_not_fused() -> Result<()> {
        let a = Tensor::from_vec(vec![-1.0f32, 1.0], &[2])?;
        let expr = TensorExpr::from(&a)?.relu()?;
        let fused = expr.optimize_fusion()?;
        assert_eq!(fused.operation_count(), 1);
        let root_op =
            fused.nodes.get(&fused.root).map(|node| node.op.clone()).expect("root exists");
        assert_eq!(root_op, OpType::ReLU);
        Ok(())
    }

    /// A binary op breaks the chain: `relu(x) * relu(x)` has no 2-link unary run
    /// rooted at the multiply, so nothing may be fused into it.
    #[test]
    fn a_binary_op_breaks_the_fusion_chain() -> Result<()> {
        let a = Tensor::from_vec(vec![1.0f32, -1.0], &[2])?;
        let b = Tensor::from_vec(vec![2.0f32, 3.0], &[2])?;
        let expr = TensorExpr::from(&a)?.relu()?.mul(TensorExpr::from(&b)?.exp()?)?;
        let before = expr.operation_count();
        let fused = expr.optimize_fusion()?;
        assert_eq!(
            fused.operation_count(),
            before,
            "no unary chain of length >= 2 exists here"
        );
        // And the result must still be right: relu([1,-1]) * exp([2,3]).
        let values = fused.eval()?.to_vec_f32()?;
        assert!((values[0] - 2.0f32.exp()).abs() < 1e-4);
        assert!(values[1].abs() < 1e-6);
        Ok(())
    }

    /// Fusion must also leave `f64` results untouched.
    #[test]
    fn fusion_preserves_f64_results() -> Result<()> {
        let a = Tensor::from_vec_with_dtype(vec![0.25f64, 1.0, 4.0], &[3], DType::F64)?;
        let expr = TensorExpr::from(&a)?.sqrt()?.log()?;

        let unfused_ctx = EvalContext {
            hints: OptimizationHints {
                enable_fusion: false,
                ..OptimizationHints::default()
            },
            ..EvalContext::default()
        };
        let unfused = expr.eval_with_context(&unfused_ctx)?;
        let fused = expr.eval()?;

        match (unfused, fused) {
            (Tensor::F64(u), Tensor::F64(f)) => {
                for (a, b) in u.iter().zip(f.iter()) {
                    assert!((a - b).abs() < 1e-12, "{a} vs {b}");
                }
                // ln(sqrt(4)) == ln(2)
                assert!((f[[2]] - std::f64::consts::LN_2).abs() < 1e-12);
            },
            _ => panic!("F64 expected"),
        }
        Ok(())
    }
}