rustorch-core 0.1.2

Core tensor library for RusTorch
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
use parking_lot::{RwLockReadGuard, RwLockWriteGuard};
use std::fmt;
use std::ops::{Add, Div, Mul, Sub};
use std::sync::{Arc, Mutex};
// use rand::Rng;
use rand_distr::{Distribution, Normal, Uniform};
// use rayon::prelude::*;
// use rayon::iter::{IntoParallelRefIterator, ParallelIterator, IndexedParallelIterator};
// use rayon::slice::ParallelSliceMut;
use crate::autograd::BackwardOp;
use crate::storage::Storage;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[derive(Clone, Debug)]
pub struct Tensor {
    pub(crate) inner: Arc<TensorImpl>,
}

impl PartialEq for Tensor {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

#[derive(Debug)]
pub(crate) struct TensorImpl {
    pub(crate) storage: Storage,
    pub(crate) shape: Vec<usize>,
    pub(crate) strides: Vec<usize>,
    pub(crate) grad: Mutex<Option<Tensor>>, // Gradient
    pub(crate) requires_grad: bool,
    pub(crate) op: Option<Arc<dyn BackwardOp>>, // Operation that created this tensor
    pub(crate) is_leaf: bool,
}

#[cfg(feature = "wgpu_backend")]
#[derive(Debug)]
struct ToCpuBackward {
    input: Tensor,
}

#[cfg(feature = "wgpu_backend")]
impl BackwardOp for ToCpuBackward {
    fn backward(&self, grad: &Tensor) {
        if self.input.requires_grad() {
            let grad_wgpu = grad.to_wgpu();
            self.input.accumulate_grad(&grad_wgpu);
            self.input.backward_step();
        }
    }
}

impl Tensor {
    pub fn new(data: &[f32], shape: &[usize]) -> Self {
        let size: usize = shape.iter().product();
        if data.len() != size {
            panic!(
                "Data size {} does not match shape {:?} (expected {})",
                data.len(),
                shape,
                size
            );
        }

        let strides = Self::compute_strides(shape);
        let storage = Storage::from_slice(data);

        Self {
            inner: Arc::new(TensorImpl {
                storage,
                shape: shape.to_vec(),
                strides,
                grad: Mutex::new(None),
                requires_grad: false,
                op: None,
                is_leaf: true,
            }),
        }
    }

    pub fn new_with_storage(storage: Storage, shape: &[usize]) -> Self {
        let strides = Self::compute_strides(shape);
        Self {
            inner: Arc::new(TensorImpl {
                storage,
                shape: shape.to_vec(),
                strides,
                grad: Mutex::new(None),
                requires_grad: false,
                op: None,
                is_leaf: true,
            }),
        }
    }

    pub fn zeros(shape: &[usize]) -> Self {
        let size: usize = shape.iter().product();
        Self::new(&vec![0.0; size], shape)
    }

    pub fn full(shape: &[usize], value: f32) -> Self {
        let size: usize = shape.iter().product();
        let data = vec![value; size];
        let storage = Storage::new(data);
        Self::new_with_storage(storage, shape)
    }

    pub fn ones(shape: &[usize]) -> Self {
        let size: usize = shape.iter().product();
        Self::new(&vec![1.0; size], shape)
    }

    pub fn storage(&self) -> &Storage {
        &self.inner.storage
    }

    #[cfg(feature = "wgpu_backend")]
    pub fn to_wgpu(&self) -> Self {
        if let Some(_) = self.storage().wgpu_buffer() {
            return self.clone();
        }

        let contig = if self.is_contiguous() {
            self.clone()
        } else {
            self.contiguous()
        };

        let data = contig.data();
        let ctx = crate::backend::wgpu::get_context().expect("WGPU context not initialized");

        use wgpu::util::DeviceExt;
        let buffer = ctx
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Tensor Buffer"),
                contents: bytemuck::cast_slice(&data),
                usage: wgpu::BufferUsages::STORAGE
                    | wgpu::BufferUsages::COPY_SRC
                    | wgpu::BufferUsages::COPY_DST,
            });

        let storage = Storage::new_wgpu(buffer, data.len(), 0);

        let inner = TensorImpl {
            storage,
            shape: contig.shape().to_vec(),
            strides: contig.strides().to_vec(),
            grad: Mutex::new(None),
            requires_grad: self.requires_grad(),
            op: None,
            is_leaf: self.inner.is_leaf,
        };

        Tensor {
            inner: Arc::new(inner),
        }
    }

    #[cfg(feature = "wgpu_backend")]
    pub fn to_cpu(&self) -> Self {
        if let Some(buffer) = self.storage().wgpu_buffer() {
            // Flush any pending commands to ensure buffer is ready
            crate::backend::wgpu::flush_queue();

            let ctx = crate::backend::wgpu::get_context().expect("WGPU context not initialized");

            let buf_size = buffer.size();

            let staging_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("Staging Buffer"),
                size: buf_size,
                usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            });

            let mut encoder = ctx
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                    label: Some("Download Encoder"),
                });
            encoder.copy_buffer_to_buffer(buffer, 0, &staging_buffer, 0, buf_size);
            ctx.queue.submit(Some(encoder.finish()));

            let buffer_slice = staging_buffer.slice(..);
            let (sender, receiver) = std::sync::mpsc::channel();
            buffer_slice.map_async(wgpu::MapMode::Read, move |v| sender.send(v).unwrap());

            ctx.device.poll(wgpu::Maintain::Wait);
            receiver.recv().unwrap().unwrap();

            let data = buffer_slice.get_mapped_range();
            let result: Vec<f32> = bytemuck::cast_slice(&data).to_vec();
            drop(data);
            staging_buffer.unmap();

            // Create CPU tensor with SAME shape and strides, but using the downloaded storage
            let storage = Storage::new(result);

            // We need to construct Tensor manually to preserve strides/offset
            let inner = TensorImpl {
                storage,
                shape: self.shape().to_vec(),
                strides: self.strides().to_vec(),
                grad: Mutex::new(None),
                requires_grad: self.requires_grad(),
                op: if self.requires_grad() {
                    Some(Arc::new(ToCpuBackward {
                        input: self.clone(),
                    }))
                } else {
                    None
                },
                is_leaf: self.inner.is_leaf,
            };
            return Self {
                inner: Arc::new(inner),
            };
        }
        // println!("DEBUG: to_cpu falling back to clone (no WGPU buffer)");
        self.clone()
    }

    pub fn shape(&self) -> &[usize] {
        &self.inner.shape
    }

    pub fn strides(&self) -> &[usize] {
        &self.inner.strides
    }

    pub fn set_requires_grad(self, requires_grad: bool) -> Self {
        let inner = &self.inner;
        let new_impl = TensorImpl {
            storage: inner.storage.clone(),
            shape: inner.shape.clone(),
            strides: inner.strides.clone(),
            grad: Mutex::new(None),
            requires_grad,
            op: inner.op.clone(),
            is_leaf: inner.is_leaf,
        };
        Self {
            inner: Arc::new(new_impl),
        }
    }

    pub fn set_requires_grad_mut(&mut self, requires_grad: bool) {
        if let Some(inner) = Arc::get_mut(&mut self.inner) {
            inner.requires_grad = requires_grad;
        } else {
            // Clone if shared
            *self = self.clone().set_requires_grad(requires_grad);
        }
    }

    pub fn requires_grad(&self) -> bool {
        self.inner.requires_grad
    }

    pub fn data(&self) -> RwLockReadGuard<'_, Vec<f32>> {
        self.inner.storage.data()
    }

    pub fn data_mut(&self) -> RwLockWriteGuard<'_, Vec<f32>> {
        self.inner.storage.data_mut()
    }

    pub fn grad(&self) -> Option<Tensor> {
        self.inner.grad.lock().unwrap().clone()
    }

    pub fn zero_grad(&self) {
        *self.inner.grad.lock().unwrap() = None;
    }

    pub fn accumulate_grad(&self, grad: &Tensor) {
        let mut g = self.inner.grad.lock().unwrap();
        if let Some(existing) = &*g {
            #[cfg(feature = "wgpu_backend")]
            {
                let existing_is_wgpu = existing.storage().wgpu_buffer().is_some();
                let grad_is_wgpu = grad.storage().wgpu_buffer().is_some();

                if existing_is_wgpu && grad_is_wgpu {
                    *g = Some(existing.add(grad));
                } else if existing_is_wgpu {
                    *g = Some(existing.add(&grad.to_wgpu()));
                } else if grad_is_wgpu {
                    *g = Some(existing.add(&grad.to_cpu()));
                } else {
                    *g = Some(existing.add(grad));
                }
            }
            #[cfg(not(feature = "wgpu_backend"))]
            {
                *g = Some(existing.add(grad));
            }
        } else {
            *g = Some(grad.clone());
        }
    }

    pub fn backward(&self) {
        // Gradient of scalar output is 1.0
        if self.shape().len() != 1 || self.shape()[0] != 1 {
            // Usually backward() is called on scalar loss.
            // If not scalar, PyTorch requires gradient argument.
            // RusTorch: implicitly assume 1.0 if scalar?
            // If tensor is not scalar, we should probably fill ones.
            // But for simplicity, let's assume scalar 1.0 or Tensor::ones.
        }

        let grad = Tensor::ones(self.shape());
        self.accumulate_grad(&grad);
        self.backward_step();
    }

    pub fn backward_step(&self) {
        if let Some(op) = &self.inner.op {
            if let Some(grad) = self.grad() {
                op.backward(&grad);
            }
        }
    }

    /// Returns a new Tensor, detached from the current graph.
    /// The result will never require gradient.
    pub fn detach(&self) -> Tensor {
        Tensor {
            inner: Arc::new(TensorImpl {
                storage: self.inner.storage.clone(),
                shape: self.inner.shape.clone(),
                strides: self.inner.strides.clone(),
                grad: Mutex::new(None),
                requires_grad: false,
                op: None,
                is_leaf: true,
            }),
        }
    }

    pub fn set_op(&mut self, op: Arc<dyn BackwardOp>) {
        if let Some(inner) = Arc::get_mut(&mut self.inner) {
            inner.op = Some(op);
        } else {
            // Panic or clone?
            // Usually set_op is called during construction where we have unique ownership.
            // If not, it means something is wrong.
            // But `permute` cloned `inner`...
            // In `permute`, I created a new Tensor with `inner: Arc::new(...)`.
            // So `self.inner` is unique there.
            panic!("Cannot set op on shared tensor storage wrapper");
        }
    }

    pub fn matmul(&self, rhs: &Tensor) -> Tensor {
        crate::ops::matmul(self, rhs)
    }

    pub fn t(&self) -> Tensor {
        crate::ops::view::transpose(self, 0, 1) // Default to 2D transpose
    }

    pub fn sub(&self, rhs: &Tensor) -> Tensor {
        crate::ops::sub(self, rhs)
    }

    pub fn add(&self, rhs: &Tensor) -> Tensor {
        crate::ops::add(self, rhs)
    }

    pub fn neg(&self) -> Tensor {
        crate::ops::neg(self)
    }

    pub fn relu(&self) -> Tensor {
        crate::ops::relu(self)
    }

    pub fn sigmoid(&self) -> Tensor {
        crate::ops::sigmoid(self)
    }

    pub fn tanh(&self) -> Tensor {
        crate::ops::tanh(self)
    }

    pub fn softmax(&self, dim: i64) -> Tensor {
        crate::ops::softmax(self, dim)
    }

    pub fn conv2d(
        &self,
        weight: &Tensor,
        stride: (usize, usize),
        padding: (usize, usize),
    ) -> Tensor {
        crate::ops::conv2d(self, weight, stride, padding)
    }

    pub fn max_pool2d(
        &self,
        kernel_size: (usize, usize),
        stride: (usize, usize),
        padding: (usize, usize),
    ) -> Tensor {
        crate::ops::max_pool2d(self, kernel_size, stride, padding)
    }

    #[allow(clippy::too_many_arguments)]
    pub fn batch_norm2d(
        &self,
        gamma: Option<&Tensor>,
        beta: Option<&Tensor>,
        running_mean: &Tensor,
        running_var: &Tensor,
        training: bool,
        momentum: f32,
        eps: f32,
    ) -> Tensor {
        crate::ops::batch_norm2d(
            self,
            gamma,
            beta,
            running_mean,
            running_var,
            training,
            momentum,
            eps,
        )
    }

    pub fn layer_norm(
        &self,
        normalized_shape: &[usize],
        weight: Option<&Tensor>,
        bias: Option<&Tensor>,
        eps: f32,
    ) -> Tensor {
        crate::ops::layer_norm(self, normalized_shape, weight, bias, eps)
    }

    pub fn permute(&self, dims: &[usize]) -> Tensor {
        crate::ops::view::permute(self, dims)
    }

    pub fn transpose(&self, dim0: usize, dim1: usize) -> Tensor {
        crate::ops::view::transpose(self, dim0, dim1)
    }

    pub fn contiguous(&self) -> Tensor {
        if self.is_contiguous() {
            return self.clone();
        }

        #[cfg(feature = "wgpu_backend")]
        if let Some(input_buf) = self.storage().wgpu_buffer() {
            use crate::backend::wgpu::contiguous_wgpu;
            let output_buf = contiguous_wgpu(input_buf, self.shape(), self.strides());
            let size: usize = self.shape().iter().product();
            let storage = Storage::new_wgpu(output_buf, size, 0);
            let mut tensor = Tensor::new_with_storage(storage, self.shape());
            tensor.set_requires_grad_mut(self.requires_grad());
            return tensor;
        }

        crate::ops::view::contiguous(self)
    }

    pub fn is_contiguous(&self) -> bool {
        let default_strides = Self::compute_strides(self.shape());
        if self.strides() != default_strides {
            return false;
        }
        let expected_size: usize = self.shape().iter().product();
        let actual_size = self.storage().len();
        expected_size == actual_size
    }

    pub fn normal_(&self, mean: f32, std: f32) {
        let mut guard = self.data_mut();
        let mut rng = rand::thread_rng();
        let normal = Normal::new(mean, std).unwrap();
        for x in guard.iter_mut() {
            *x = normal.sample(&mut rng);
        }
    }

    pub fn uniform_(&self, low: f32, high: f32) {
        let mut guard = self.data_mut();
        let mut rng = rand::thread_rng();
        let uniform = Uniform::new(low, high);
        for x in guard.iter_mut() {
            *x = uniform.sample(&mut rng);
        }
    }

    pub fn fill_(&self, value: f32) {
        let mut guard = self.data_mut();
        for x in guard.iter_mut() {
            *x = value;
        }
    }

    pub fn reshape(&self, new_shape: &[usize]) -> Tensor {
        let size: usize = self.shape().iter().product();
        let new_size: usize = new_shape.iter().product();
        if size != new_size {
            panic!(
                "Reshape: element count mismatch: {:?} vs {:?}",
                self.shape(),
                new_shape
            );
        }

        let inner = &self.inner;
        let strides = Self::compute_strides(new_shape);

        // Share storage, create new TensorImpl
        let mut tensor = Self {
            inner: Arc::new(TensorImpl {
                storage: inner.storage.clone(),
                shape: new_shape.to_vec(),
                strides,
                grad: Mutex::new(None),
                requires_grad: inner.requires_grad,
                op: None,
                is_leaf: false,
            }),
        };

        if inner.requires_grad {
            tensor.set_op(Arc::new(crate::ops::ReshapeBackward {
                input_shape: inner.shape.clone(),
                input: self.clone(),
            }));
        }

        tensor
    }

    pub fn mul(&self, rhs: &Tensor) -> Tensor {
        crate::ops::mul(self, rhs)
    }

    pub fn div(&self, rhs: &Tensor) -> Tensor {
        crate::ops::div(self, rhs)
    }

    #[cfg(feature = "wgpu_backend")]
    pub fn matmul_relu(&self, rhs: &Tensor) -> Tensor {
        crate::ops::matmul_fused(self, rhs, None, crate::backend::wgpu::Activation::ReLU)
    }

    #[cfg(not(feature = "wgpu_backend"))]
    pub fn matmul_relu(&self, rhs: &Tensor) -> Tensor {
        self.matmul(rhs).relu()
    }

    pub fn sgd_step(&self, grad: &Tensor, lr: f32) -> Tensor {
        crate::ops::sgd_step(self, grad, lr)
    }

    pub fn copy_(&self, src: &Tensor) {
        #[cfg(feature = "wgpu_backend")]
        if self.storage().device().is_wgpu() {
            if let Some(dest_buf) = self.storage().wgpu_buffer() {
                let ctx = crate::backend::wgpu::get_context().expect("WGPU context missing");

                // Case 1: src is WGPU -> GPU Copy
                if let Some(src_buf) = src.storage().wgpu_buffer() {
                    let mut encoder =
                        ctx.device
                            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                                label: Some("Copy Encoder"),
                            });
                    encoder.copy_buffer_to_buffer(src_buf, 0, dest_buf, 0, dest_buf.size());
                    ctx.queue.submit(Some(encoder.finish()));
                    return;
                }

                // Case 2: src is CPU -> Upload
                let src_cpu = if src.storage().device().is_wgpu() {
                    src.to_cpu()
                } else {
                    src.clone()
                };
                let src_guard = src_cpu.data();
                ctx.queue
                    .write_buffer(dest_buf, 0, bytemuck::cast_slice(&src_guard));
                return;
            }
        }

        // Case 3: self is CPU -> Memcpy
        let src_cpu = if src.storage().device().is_wgpu() {
            src.to_cpu()
        } else {
            src.clone()
        };
        let mut dest_guard = self.data_mut();
        let src_guard = src_cpu.data();
        if dest_guard.len() != src_guard.len() {
            // panic!("copy_: element count mismatch");
            // Allow broadcasting? No, strict copy_ usually.
        }
        let len = std::cmp::min(dest_guard.len(), src_guard.len());
        dest_guard[..len].copy_from_slice(&src_guard[..len]);
    }

    fn compute_strides(shape: &[usize]) -> Vec<usize> {
        let mut strides = vec![0; shape.len()];
        let mut stride = 1;
        for i in (0..shape.len()).rev() {
            strides[i] = stride;
            stride *= shape[i];
        }
        strides
    }

    // pub fn expand(&self, target_shape: &[usize]) -> Tensor {
    //    crate::broadcast::expand(self, target_shape)
    // }

    pub fn copy_from_slice(&self, src: &[f32]) {
        let mut guard = self.data_mut();
        let len = std::cmp::min(guard.len(), src.len());
        guard[..len].copy_from_slice(&src[..len]);
    }
}

// Implement arithmetic traits for &Tensor
impl Add for &Tensor {
    type Output = Tensor;
    fn add(self, rhs: Self) -> Tensor {
        self.add(rhs)
    }
}

impl Add<Tensor> for Tensor {
    type Output = Tensor;
    fn add(self, rhs: Tensor) -> Tensor {
        Tensor::add(&self, &rhs)
    }
}

impl Sub<Tensor> for Tensor {
    type Output = Tensor;
    fn sub(self, rhs: Tensor) -> Tensor {
        Tensor::sub(&self, &rhs)
    }
}

impl Mul<Tensor> for Tensor {
    type Output = Tensor;
    fn mul(self, rhs: Tensor) -> Tensor {
        Tensor::mul(&self, &rhs)
    }
}

impl Div<Tensor> for Tensor {
    type Output = Tensor;
    fn div(self, rhs: Tensor) -> Tensor {
        Tensor::div(&self, &rhs)
    }
}

impl Sub for &Tensor {
    type Output = Tensor;
    fn sub(self, rhs: Self) -> Tensor {
        self.sub(rhs)
    }
}

impl Mul for &Tensor {
    type Output = Tensor;
    fn mul(self, rhs: Self) -> Tensor {
        self.mul(rhs)
    }
}

impl Div for &Tensor {
    type Output = Tensor;
    fn div(self, rhs: Self) -> Tensor {
        self.div(rhs)
    }
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
struct TensorData {
    shape: Vec<usize>,
    data: Vec<f32>,
    requires_grad: bool,
}

#[cfg(feature = "serde")]
impl Serialize for Tensor {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let data = self.data().clone();
        let tensor_data = TensorData {
            shape: self.shape().to_vec(),
            data,
            requires_grad: self.requires_grad(),
        };
        tensor_data.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Tensor {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let tensor_data = TensorData::deserialize(deserializer)?;
        let tensor = Tensor::new(&tensor_data.data, &tensor_data.shape)
            .set_requires_grad(tensor_data.requires_grad);
        Ok(tensor)
    }
}

impl fmt::Display for Tensor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let data = self.data();
        let len = std::cmp::min(data.len(), 10);
        write!(
            f,
            "Tensor(shape={:?}, data={:?})",
            self.shape(),
            &data[..len]
        )
    }
}