torsh-tensor 0.1.2

Tensor implementation for ToRSh with PyTorch-compatible API
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
//! Type conversion operations for tensors
//!
//! This module provides comprehensive type conversion operations:
//! - Numeric type conversions (f32, f64, i32, i64, etc.)
//! - Boolean conversions
//! - Complex number conversions
//! - Device transfers (CPU, CUDA, etc.)
//! - Type promotion for mixed operations

use crate::{FloatElement, Tensor, TensorElement};
use torsh_core::error::{Result, TorshError};
use torsh_core::device::DeviceType;

/// Type conversion operations
impl<T: TensorElement> Tensor<T> {
    /// Convert tensor to f32 type
    pub fn to_f32(&self) -> Result<Tensor<f32>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<f32>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .and_then(|f| if f.is_finite() && f >= f32::MIN as f64 && f <= f32::MAX as f64 {
                        Some(f as f32)
                    } else {
                        None
                    })
                    .ok_or_else(|| TorshError::InvalidArgument(
                        format!("Cannot convert value to f32: {}", f64::from_bits(<T as TensorElement>::to_f64(&x).unwrap_or(0.0) as u64))
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert tensor to f64 type
    pub fn to_f64(&self) -> Result<Tensor<f64>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<f64>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .ok_or_else(|| TorshError::InvalidArgument(
                        "Cannot convert value to f64".to_string()
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert tensor to i32 type
    pub fn to_i32(&self) -> Result<Tensor<i32>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<i32>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .and_then(|f| {
                        if f.is_finite() && f >= i32::MIN as f64 && f <= i32::MAX as f64 {
                            Some(f.round() as i32)
                        } else {
                            None
                        }
                    })
                    .ok_or_else(|| TorshError::InvalidArgument(
                        "Cannot convert value to i32: value out of range or not finite".to_string()
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert tensor to i64 type
    pub fn to_i64(&self) -> Result<Tensor<i64>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<i64>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .and_then(|f| {
                        if f.is_finite() && f >= i64::MIN as f64 && f <= i64::MAX as f64 {
                            Some(f.round() as i64)
                        } else {
                            None
                        }
                    })
                    .ok_or_else(|| TorshError::InvalidArgument(
                        "Cannot convert value to i64: value out of range or not finite".to_string()
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert tensor to bool type (non-zero values become true)
    pub fn to_bool(&self) -> Result<Tensor<bool>> {
        let data = self.data()?;
        let converted_data: Vec<bool> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .map(|f| f != 0.0)
                    .unwrap_or(false)
            })
            .collect();

        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert tensor to f16 (half-precision) type
    pub fn to_f16(&self) -> Result<Tensor<torsh_core::dtype::f16>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<torsh_core::dtype::f16>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .map(|f| torsh_core::dtype::f16::from_f64(f))
                    .ok_or_else(|| TorshError::InvalidArgument(
                        "Cannot convert value to f16".to_string()
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert tensor to bf16 (bfloat16) type
    pub fn to_bf16(&self) -> Result<Tensor<torsh_core::dtype::bf16>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<torsh_core::dtype::bf16>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .map(|f| torsh_core::dtype::bf16::from_f64(f))
                    .ok_or_else(|| TorshError::InvalidArgument(
                        "Cannot convert value to bf16".to_string()
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert tensor to u8 type
    pub fn to_u8(&self) -> Result<Tensor<u8>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<u8>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .and_then(|f| {
                        if f.is_finite() && f >= 0.0 && f <= u8::MAX as f64 {
                            Some(f.round() as u8)
                        } else {
                            None
                        }
                    })
                    .ok_or_else(|| TorshError::InvalidArgument(
                        "Cannot convert value to u8: value out of range or not finite".to_string()
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert tensor to i8 type
    pub fn to_i8(&self) -> Result<Tensor<i8>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<i8>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .and_then(|f| {
                        if f.is_finite() && f >= i8::MIN as f64 && f <= i8::MAX as f64 {
                            Some(f.round() as i8)
                        } else {
                            None
                        }
                    })
                    .ok_or_else(|| TorshError::InvalidArgument(
                        "Cannot convert value to i8: value out of range or not finite".to_string()
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert tensor to i16 type
    pub fn to_i16(&self) -> Result<Tensor<i16>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<i16>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .and_then(|f| {
                        if f.is_finite() && f >= i16::MIN as f64 && f <= i16::MAX as f64 {
                            Some(f.round() as i16)
                        } else {
                            None
                        }
                    })
                    .ok_or_else(|| TorshError::InvalidArgument(
                        "Cannot convert value to i16: value out of range or not finite".to_string()
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }


    /// Move tensor to CPU device
    pub fn to_cpu(&self) -> Result<Self> {
        self.to_device(DeviceType::Cpu)
    }

    /// Move tensor to CUDA device (if available)
    pub fn to_cuda(&self, device_id: usize) -> Result<Self> {
        self.to_device(DeviceType::Cuda(device_id))
    }

    /// Generic type conversion to any TensorElement type
    pub fn to_tensor<U: TensorElement>(&self) -> Result<Tensor<U>> {
        let data = self.data()?;
        let converted_data: std::result::Result<Vec<U>, _> = data
            .iter()
            .map(|&x| {
                <T as TensorElement>::to_f64(&x)
                    .and_then(|f| U::from_f64(f))
                    .ok_or_else(|| TorshError::InvalidArgument(
                        format!("Cannot convert value to target type")
                    ))
            })
            .collect();

        let converted_data = converted_data?;
        Tensor::from_data(
            converted_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }
}

/// Float-specific conversions
impl<T: FloatElement> Tensor<T> {
    /// Convert to complex tensor (imaginary part is zero)
    pub fn to_complex32(&self) -> Result<Tensor<torsh_core::dtype::Complex32>> {
        let data = self.data()?;
        let complex_data: Vec<torsh_core::dtype::Complex32> = data
            .iter()
            .map(|&x| {
                let real = <T as TensorElement>::to_f64(&x).unwrap_or(0.0) as f32;
                torsh_core::dtype::Complex32::new(real, 0.0)
            })
            .collect();

        Tensor::from_data(
            complex_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert to complex64 tensor (imaginary part is zero)
    pub fn to_complex64(&self) -> Result<Tensor<torsh_core::dtype::Complex64>> {
        let data = self.data()?;
        let complex_data: Vec<torsh_core::dtype::Complex64> = data
            .iter()
            .map(|&x| {
                let real = <T as TensorElement>::to_f64(&x).unwrap_or(0.0);
                torsh_core::dtype::Complex64::new(real, 0.0)
            })
            .collect();

        Tensor::from_data(
            complex_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }
}

/// Complex tensor conversions
impl Tensor<torsh_core::dtype::Complex32> {
    /// Extract real part as f32 tensor
    pub fn real_part(&self) -> Result<Tensor<f32>> {
        let data = self.data()?;
        let real_data: Vec<f32> = data.iter().map(|x| x.re).collect();

        Tensor::from_data(
            real_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Extract imaginary part as f32 tensor
    pub fn imag_part(&self) -> Result<Tensor<f32>> {
        let data = self.data()?;
        let imag_data: Vec<f32> = data.iter().map(|x| x.im).collect();

        Tensor::from_data(
            imag_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert to magnitude (absolute value) tensor
    pub fn magnitude(&self) -> Result<Tensor<f32>> {
        let data = self.data()?;
        let mag_data: Vec<f32> = data.iter().map(|x| x.norm()).collect();

        Tensor::from_data(
            mag_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert to phase (angle) tensor
    pub fn phase(&self) -> Result<Tensor<f32>> {
        let data = self.data()?;
        let phase_data: Vec<f32> = data.iter().map(|x| x.arg()).collect();

        Tensor::from_data(
            phase_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }
}

/// Complex64 tensor conversions
impl Tensor<torsh_core::dtype::Complex64> {
    /// Extract real part as f64 tensor
    pub fn real_part(&self) -> Result<Tensor<f64>> {
        let data = self.data()?;
        let real_data: Vec<f64> = data.iter().map(|x| x.re).collect();

        Tensor::from_data(
            real_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Extract imaginary part as f64 tensor
    pub fn imag_part(&self) -> Result<Tensor<f64>> {
        let data = self.data()?;
        let imag_data: Vec<f64> = data.iter().map(|x| x.im).collect();

        Tensor::from_data(
            imag_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert to magnitude (absolute value) tensor
    pub fn magnitude(&self) -> Result<Tensor<f64>> {
        let data = self.data()?;
        let mag_data: Vec<f64> = data.iter().map(|x| x.norm()).collect();

        Tensor::from_data(
            mag_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }

    /// Convert to phase (angle) tensor
    pub fn phase(&self) -> Result<Tensor<f64>> {
        let data = self.data()?;
        let phase_data: Vec<f64> = data.iter().map(|x| x.arg()).collect();

        Tensor::from_data(
            phase_data,
            self.shape().dims().to_vec(),
            self.device,
        )
    }
}

/// Boolean tensor conversions
impl Tensor<bool> {
}

/// Type promotion utilities
pub fn promote_types<T1: TensorElement, T2: TensorElement>(
    tensor1: &Tensor<T1>,
    tensor2: &Tensor<T2>,
) -> Result<(Tensor<f64>, Tensor<f64>)> {
    // For simplicity, promote both tensors to f64
    // In a more sophisticated implementation, we would use type hierarchy
    let promoted1 = tensor1.to_f64()?;
    let promoted2 = tensor2.to_f64()?;
    Ok((promoted1, promoted2))
}

/// Create a complex tensor from real and imaginary parts
pub fn complex_from_parts<T: FloatElement>(
    real: &Tensor<T>,
    imag: &Tensor<T>,
) -> Result<Tensor<torsh_core::dtype::Complex64>> {
    if real.shape() != imag.shape() {
        return Err(TorshError::ShapeMismatch {
            expected: real.shape().dims().to_vec(),
            got: imag.shape().dims().to_vec(),
        });
    }

    let real_data = real.data()?;
    let imag_data = imag.data()?;

    let complex_data: Vec<torsh_core::dtype::Complex64> = real_data
        .iter()
        .zip(imag_data.iter())
        .map(|(&r, &i)| {
            let real_f64 = <T as TensorElement>::to_f64(&r).unwrap_or(0.0);
            let imag_f64 = <T as TensorElement>::to_f64(&i).unwrap_or(0.0);
            torsh_core::dtype::Complex64::new(real_f64, imag_f64)
        })
        .collect();

    Tensor::from_data(
        complex_data,
        real.shape().dims().to_vec(),
        real.device,
    )
}

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

    #[test]
    fn test_to_f32() {
        let tensor = Tensor::from_data(vec![1i32, 2, 3, 4], vec![4], DeviceType::Cpu).expect("operation should succeed");
        let f32_tensor = tensor.to_f32().expect("f32 conversion should succeed");
        let data = f32_tensor.data().expect("data retrieval should succeed");
        assert_eq!(data.as_slice(), &[1.0f32, 2.0, 3.0, 4.0]);
    }

    #[test]
    fn test_to_i32() {
        let tensor = Tensor::from_data(vec![1.7f32, 2.3, -3.9, 4.0], vec![4], DeviceType::Cpu).expect("operation should succeed");
        let i32_tensor = tensor.to_i32().expect("i32 conversion should succeed");
        let data = i32_tensor.data().expect("data retrieval should succeed");
        assert_eq!(data.as_slice(), &[2i32, 2, -4, 4]); // Rounded values
    }

    #[test]
    fn test_to_bool() {
        let tensor = Tensor::from_data(vec![0.0f32, 1.0, -2.5, 0.0], vec![4], DeviceType::Cpu).expect("operation should succeed");
        let bool_tensor = tensor.to_bool().expect("bool conversion should succeed");
        let data = bool_tensor.data().expect("data retrieval should succeed");
        assert_eq!(data.as_slice(), &[false, true, true, false]);
    }

    #[test]
    fn test_bool_to_f32() {
        let tensor = Tensor::from_data(vec![true, false, true, false], vec![4], DeviceType::Cpu).expect("operation should succeed");
        let f32_tensor = tensor.to_f32().expect("f32 conversion should succeed");
        let data = f32_tensor.data().expect("data retrieval should succeed");
        assert_eq!(data.as_slice(), &[1.0f32, 0.0, 1.0, 0.0]);
    }

    #[test]
    fn test_to_device_cpu() {
        let tensor = Tensor::from_data(vec![1.0f32, 2.0, 3.0], vec![3], DeviceType::Cpu).expect("operation should succeed");
        let cpu_tensor = tensor.to_cpu().expect("cpu transfer should succeed");
        assert_eq!(cpu_tensor.device, DeviceType::Cpu);
    }

    #[test]
    fn test_to_complex32() {
        let tensor = Tensor::from_data(vec![1.0f32, 2.0, 3.0], vec![3], DeviceType::Cpu).expect("operation should succeed");
        let complex_tensor = tensor.to_complex32().expect("complex32 conversion should succeed");
        let data = complex_tensor.data().expect("data retrieval should succeed");

        assert_eq!(data[0].re, 1.0);
        assert_eq!(data[0].im, 0.0);
        assert_eq!(data[1].re, 2.0);
        assert_eq!(data[1].im, 0.0);
    }

    #[test]
    fn test_complex_real_imag_parts() {
        let real_data = vec![1.0f32, 2.0, 3.0];
        let imag_data = vec![4.0f32, 5.0, 6.0];

        let complex_data: Vec<torsh_core::dtype::Complex32> = real_data
            .iter()
            .zip(imag_data.iter())
            .map(|(&r, &i)| torsh_core::dtype::Complex32::new(r, i))
            .collect();

        let complex_tensor = Tensor::from_data(complex_data, vec![3], DeviceType::Cpu).expect("operation should succeed");

        let real_part = complex_tensor.real_part().expect("real_part extraction should succeed");
        let imag_part = complex_tensor.imag_part().expect("imag_part extraction should succeed");

        let real_data_result = real_part.data().expect("data retrieval should succeed");
        let imag_data_result = imag_part.data().expect("data retrieval should succeed");

        assert_eq!(real_data_result.as_slice(), &[1.0f32, 2.0, 3.0]);
        assert_eq!(imag_data_result.as_slice(), &[4.0f32, 5.0, 6.0]);
    }

    #[test]
    fn test_complex_magnitude() {
        let complex_data = vec![
            torsh_core::dtype::Complex32::new(3.0, 4.0), // magnitude = 5.0
            torsh_core::dtype::Complex32::new(0.0, 1.0), // magnitude = 1.0
        ];

        let complex_tensor = Tensor::from_data(complex_data, vec![2], DeviceType::Cpu).expect("operation should succeed");
        let magnitude = complex_tensor.magnitude().expect("magnitude computation should succeed");
        let mag_data = magnitude.data().expect("data retrieval should succeed");

        assert!((mag_data[0] - 5.0).abs() < 1e-6);
        assert!((mag_data[1] - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_complex_from_parts() {
        let real = Tensor::from_data(vec![1.0f32, 2.0], vec![2], DeviceType::Cpu).expect("operation should succeed");
        let imag = Tensor::from_data(vec![3.0f32, 4.0], vec![2], DeviceType::Cpu).expect("operation should succeed");

        let complex_tensor = complex_from_parts(&real, &imag).expect("operation should succeed");
        let data = complex_tensor.data().expect("data retrieval should succeed");

        assert_eq!(data[0].re, 1.0);
        assert_eq!(data[0].im, 3.0);
        assert_eq!(data[1].re, 2.0);
        assert_eq!(data[1].im, 4.0);
    }

    #[test]
    fn test_promote_types() {
        let tensor1 = Tensor::from_data(vec![1i32, 2], vec![2], DeviceType::Cpu).expect("operation should succeed");
        let tensor2 = Tensor::from_data(vec![3.5f32, 4.5], vec![2], DeviceType::Cpu).expect("operation should succeed");

        let (promoted1, promoted2) = promote_types(&tensor1, &tensor2).expect("operation should succeed");

        let data1 = promoted1.data().expect("data retrieval should succeed");
        let data2 = promoted2.data().expect("data retrieval should succeed");

        assert_eq!(data1.as_slice(), &[1.0f64, 2.0]);
        assert_eq!(data2.as_slice(), &[3.5f64, 4.5]);
    }

    #[test]
    fn test_conversion_error_handling() {
        // Test overflow handling
        let large_tensor = Tensor::from_data(
            vec![f64::MAX, f64::MIN],
            vec![2],
            DeviceType::Cpu
        ).expect("operation should succeed");

        // This should fail because f64::MAX cannot be represented as f32
        assert!(large_tensor.to_f32().is_err());
    }

    #[test]
    fn test_to_f16() {
        let tensor = Tensor::from_data(vec![1.0f32, 2.5, -3.75, 4.0], vec![4], DeviceType::Cpu).expect("operation should succeed");
        let f16_tensor = tensor.to_f16().expect("f16 conversion should succeed");
        let data = f16_tensor.data().expect("data retrieval should succeed");

        // Convert back to f32 for comparison
        assert!((data[0].to_f32() - 1.0).abs() < 1e-3);
        assert!((data[1].to_f32() - 2.5).abs() < 1e-3);
        assert!((data[2].to_f32() + 3.75).abs() < 1e-3);
        assert!((data[3].to_f32() - 4.0).abs() < 1e-3);
    }

    #[test]
    fn test_to_bf16() {
        let tensor = Tensor::from_data(vec![1.0f32, 2.5, -3.75, 4.0], vec![4], DeviceType::Cpu).expect("operation should succeed");
        let bf16_tensor = tensor.to_bf16().expect("bf16 conversion should succeed");
        let data = bf16_tensor.data().expect("data retrieval should succeed");

        // BF16 has less precision than F16, use larger epsilon
        assert!((data[0].to_f32() - 1.0).abs() < 1e-2);
        assert!((data[1].to_f32() - 2.5).abs() < 1e-2);
        assert!((data[2].to_f32() + 3.75).abs() < 1e-2);
        assert!((data[3].to_f32() - 4.0).abs() < 1e-2);
    }

    #[test]
    fn test_to_u8() {
        let tensor = Tensor::from_data(vec![0.0f32, 127.5, 255.0, 100.7], vec![4], DeviceType::Cpu).expect("operation should succeed");
        let u8_tensor = tensor.to_u8().expect("u8 conversion should succeed");
        let data = u8_tensor.data().expect("data retrieval should succeed");
        assert_eq!(data.as_slice(), &[0u8, 128, 255, 101]);
    }

    #[test]
    fn test_to_i8() {
        let tensor = Tensor::from_data(vec![-128.0f32, -50.5, 0.0, 127.0], vec![4], DeviceType::Cpu).expect("operation should succeed");
        let i8_tensor = tensor.to_i8().expect("i8 conversion should succeed");
        let data = i8_tensor.data().expect("data retrieval should succeed");
        assert_eq!(data.as_slice(), &[-128i8, -50, 0, 127]);
    }

    #[test]
    fn test_to_i16() {
        let tensor = Tensor::from_data(vec![-1000.0f32, -50.5, 0.0, 1000.7], vec![4], DeviceType::Cpu).expect("operation should succeed");
        let i16_tensor = tensor.to_i16().expect("i16 conversion should succeed");
        let data = i16_tensor.data().expect("data retrieval should succeed");
        assert_eq!(data.as_slice(), &[-1000i16, -50, 0, 1001]);
    }

    #[test]
    fn test_generic_to_tensor() {
        // Test generic conversion using to_tensor<U>
        let tensor = Tensor::from_data(vec![1.5f32, 2.5, 3.5], vec![3], DeviceType::Cpu).expect("operation should succeed");
        let f64_tensor: Tensor<f64> = tensor.to_tensor().expect("tensor type conversion should succeed");
        let data = f64_tensor.data().expect("data retrieval should succeed");
        assert!((data[0] - 1.5).abs() < 1e-6);
        assert!((data[1] - 2.5).abs() < 1e-6);
        assert!((data[2] - 3.5).abs() < 1e-6);
    }

    #[test]
    fn test_mixed_precision_workflow() {
        // Simulate mixed-precision training workflow
        let f32_tensor = Tensor::from_data(vec![1.0f32, 2.0, 3.0, 4.0], vec![4], DeviceType::Cpu).expect("operation should succeed");

        // Convert to f16 for forward pass
        let f16_tensor = f32_tensor.to_f16().expect("f16 conversion should succeed");

        // Convert back to f32 for loss computation
        let f32_result: Tensor<f32> = f16_tensor.to_tensor().expect("tensor type conversion should succeed");
        let data = f32_result.data().expect("data retrieval should succeed");

        // Values should be approximately equal (within f16 precision)
        assert!((data[0] - 1.0).abs() < 1e-3);
        assert!((data[1] - 2.0).abs() < 1e-3);
        assert!((data[2] - 3.0).abs() < 1e-3);
        assert!((data[3] - 4.0).abs() < 1e-3);
    }
}