yscv-tensor 0.1.7

SIMD-accelerated tensor library with f32/f16/bf16 support and 80+ operations
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
use super::aligned::AlignedVec;
use super::error::{DType, TensorError};
use super::shape::{compute_strides, shape_element_count};

// ── Inline shape/strides storage (no heap alloc for ≤6D tensors) ─────────

// WHY 6: covers all common tensor ranks (scalar(0)..conv weight(5)) without heap allocation.
const INLINE_CAP: usize = 6;

/// Stack-allocated small vector for tensor shape/strides.
/// Stores up to 6 dimensions inline; falls back to heap for higher ranks.
#[derive(Clone)]
pub(crate) enum DimsVec {
    Inline { buf: [usize; INLINE_CAP], len: u8 },
    Heap(Vec<usize>),
}

impl DimsVec {
    #[inline]
    fn new() -> Self {
        DimsVec::Inline {
            buf: [0; INLINE_CAP],
            len: 0,
        }
    }

    #[inline]
    fn as_slice(&self) -> &[usize] {
        match self {
            DimsVec::Inline { buf, len } => &buf[..*len as usize],
            DimsVec::Heap(v) => v,
        }
    }

    #[inline]
    fn to_vec(&self) -> Vec<usize> {
        self.as_slice().to_vec()
    }
}

impl std::ops::Deref for DimsVec {
    type Target = [usize];
    #[inline]
    fn deref(&self) -> &[usize] {
        self.as_slice()
    }
}

impl From<Vec<usize>> for DimsVec {
    #[inline]
    fn from(v: Vec<usize>) -> Self {
        if v.len() <= INLINE_CAP {
            let mut buf = [0usize; INLINE_CAP];
            buf[..v.len()].copy_from_slice(&v);
            DimsVec::Inline {
                buf,
                len: v.len() as u8,
            }
        } else {
            DimsVec::Heap(v)
        }
    }
}

impl From<&[usize]> for DimsVec {
    #[inline]
    fn from(s: &[usize]) -> Self {
        if s.len() <= INLINE_CAP {
            let mut buf = [0usize; INLINE_CAP];
            buf[..s.len()].copy_from_slice(s);
            DimsVec::Inline {
                buf,
                len: s.len() as u8,
            }
        } else {
            DimsVec::Heap(s.to_vec())
        }
    }
}

impl PartialEq for DimsVec {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl std::fmt::Debug for DimsVec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_slice().fmt(f)
    }
}

/// Logical device where a tensor resides.
///
/// This is currently a metadata tag only — no actual data transfer occurs.
/// GPU data movement is handled externally (e.g. via `GpuSession` in yscv-kernels).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Device {
    /// CPU (host) memory — the default.
    #[default]
    Cpu,
    /// GPU device with the given index.
    Gpu(usize),
}

/// Internal typed storage for tensor data.
#[derive(Debug, Clone)]
pub(crate) enum Storage {
    F32(AlignedVec<f32>),
    F16(Vec<u16>),
    BF16(Vec<u16>),
}

impl PartialEq for Storage {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Storage::F32(a), Storage::F32(b)) => a == b,
            (Storage::F16(a), Storage::F16(b)) => a == b,
            (Storage::BF16(a), Storage::BF16(b)) => a == b,
            _ => false,
        }
    }
}

impl Storage {
    fn len(&self) -> usize {
        match self {
            Storage::F32(v) => v.len(),
            Storage::F16(v) => v.len(),
            Storage::BF16(v) => v.len(),
        }
    }

    fn dtype(&self) -> DType {
        match self {
            Storage::F32(_) => DType::F32,
            Storage::F16(_) => DType::F16,
            Storage::BF16(_) => DType::BF16,
        }
    }
}

/// A compact, contiguous multi-dtype tensor representation.
///
/// Default dtype is `F32`. FP16 and BF16 dtypes are stored natively as `u16` bit patterns
/// and can be created via `from_f16` / `from_bf16` or converted via `to_dtype`.
#[derive(Debug, Clone, PartialEq)]
pub struct Tensor {
    shape: DimsVec,
    strides: DimsVec,
    storage: Storage,
    device: Device,
}

impl Tensor {
    /// Builds a scalar tensor from one value.
    pub fn scalar(value: f32) -> Self {
        Self {
            shape: DimsVec::new(),
            strides: DimsVec::new(),
            storage: Storage::F32(AlignedVec::filled(1, value)),
            device: Device::Cpu,
        }
    }

    /// Creates a tensor from pre-validated shape, strides, and data.
    /// No validation, no heap allocation (shape/strides stored inline for ≤6D).
    #[inline]
    pub fn from_raw_parts(shape: &[usize], strides: &[usize], data: AlignedVec<f32>) -> Self {
        debug_assert_eq!(
            shape.iter().copied().product::<usize>(),
            data.len(),
            "from_raw_parts: shape product != data.len()"
        );
        Self {
            shape: DimsVec::from(shape),
            strides: DimsVec::from(strides),
            storage: Storage::F32(data),
            device: Device::Cpu,
        }
    }

    /// Builds a tensor from `shape` and a pre-filled [`AlignedVec`].
    ///
    /// This avoids the extra copy that [`from_vec`](Self::from_vec) performs when
    /// converting a `Vec<f32>` into aligned storage.  Use this when the output
    /// buffer was already allocated as an `AlignedVec` (e.g. via
    /// [`AlignedVec::uninitialized`] filled by a BLAS/SIMD kernel).
    pub fn from_aligned(shape: Vec<usize>, data: AlignedVec<f32>) -> Result<Self, TensorError> {
        let expected = shape_element_count(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;
        if expected != data.len() {
            return Err(TensorError::SizeMismatch {
                shape,
                data_len: data.len(),
            });
        }

        let strides = compute_strides(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;

        Ok(Self {
            shape: DimsVec::from(shape),
            strides: DimsVec::from(strides),
            storage: Storage::F32(data),
            device: Device::Cpu,
        })
    }

    /// Builds a tensor from `shape` and raw contiguous `f32` data.
    pub fn from_vec(shape: Vec<usize>, data: Vec<f32>) -> Result<Self, TensorError> {
        let expected = shape_element_count(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;
        if expected != data.len() {
            return Err(TensorError::SizeMismatch {
                shape,
                data_len: data.len(),
            });
        }

        let strides = compute_strides(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;

        Ok(Self {
            shape: DimsVec::from(shape),
            strides: DimsVec::from(strides),
            storage: Storage::F32(AlignedVec::from_vec(data)),
            device: Device::Cpu,
        })
    }

    /// Builds a tensor from `shape` and raw FP16 bit patterns (`u16`).
    pub fn from_f16(shape: Vec<usize>, data: Vec<u16>) -> Result<Self, TensorError> {
        let expected = shape_element_count(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;
        if expected != data.len() {
            return Err(TensorError::SizeMismatch {
                shape,
                data_len: data.len(),
            });
        }
        let strides = compute_strides(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;
        Ok(Self {
            shape: DimsVec::from(shape),
            strides: DimsVec::from(strides),
            storage: Storage::F16(data),
            device: Device::Cpu,
        })
    }

    /// Builds a tensor from `shape` and raw BF16 bit patterns (`u16`).
    pub fn from_bf16(shape: Vec<usize>, data: Vec<u16>) -> Result<Self, TensorError> {
        let expected = shape_element_count(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;
        if expected != data.len() {
            return Err(TensorError::SizeMismatch {
                shape,
                data_len: data.len(),
            });
        }
        let strides = compute_strides(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;
        Ok(Self {
            shape: DimsVec::from(shape),
            strides: DimsVec::from(strides),
            storage: Storage::BF16(data),
            device: Device::Cpu,
        })
    }

    /// Builds a 1-D tensor from a slice. Equivalent to `from_vec(vec![data.len()], data.to_vec())`.
    pub fn from_slice(data: &[f32]) -> Self {
        let n = data.len();
        Self {
            shape: DimsVec::from(vec![n]),
            strides: DimsVec::from(vec![1usize]),
            storage: Storage::F32(AlignedVec::from_vec(data.to_vec())),
            device: Device::Cpu,
        }
    }

    /// Builds a value-initialized tensor for a given shape.
    ///
    /// Alias: [`full`](Self::full) is provided as a more familiar name.
    pub fn filled(shape: Vec<usize>, value: f32) -> Result<Self, TensorError> {
        let count = shape_element_count(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;
        let strides = compute_strides(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;

        Ok(Self {
            shape: DimsVec::from(shape),
            strides: DimsVec::from(strides),
            storage: Storage::F32(AlignedVec::filled(count, value)),
            device: Device::Cpu,
        })
    }

    /// Builds a zero-initialized tensor for a given shape.
    ///
    /// Uses `alloc_zeroed` under the hood so the OS can provide pre-zeroed pages
    /// without writing every byte — significantly faster for large tensors.
    pub fn zeros(shape: Vec<usize>) -> Result<Self, TensorError> {
        let count = shape_element_count(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;
        let strides = compute_strides(&shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: shape.clone(),
        })?;

        Ok(Self {
            shape: DimsVec::from(shape),
            strides: DimsVec::from(strides),
            storage: Storage::F32(AlignedVec::calloc(count)),
            device: Device::Cpu,
        })
    }

    /// Builds a one-initialized tensor for a given shape.
    pub fn ones(shape: Vec<usize>) -> Result<Self, TensorError> {
        Self::filled(shape, 1.0)
    }

    /// Builds a tensor filled with `value`. Alias for [`filled`](Self::filled).
    pub fn full(shape: Vec<usize>, value: f32) -> Result<Self, TensorError> {
        Self::filled(shape, value)
    }

    /// Returns the tensor shape.
    pub fn shape(&self) -> &[usize] {
        &self.shape
    }

    /// Returns the tensor strides.
    pub fn strides(&self) -> &[usize] {
        &self.strides
    }

    /// Returns tensor rank (number of axes).
    pub fn rank(&self) -> usize {
        self.shape.len()
    }

    /// Returns element count.
    pub fn len(&self) -> usize {
        self.storage.len()
    }

    /// Returns `true` if tensor contains zero elements.
    pub fn is_empty(&self) -> bool {
        self.storage.len() == 0
    }

    /// Returns the element data type.
    pub fn dtype(&self) -> DType {
        self.storage.dtype()
    }

    /// Returns the logical device this tensor is associated with.
    pub fn device(&self) -> Device {
        self.device
    }

    /// Returns a copy of this tensor tagged with the given device.
    ///
    /// This is currently a metadata-only operation — no actual data transfer
    /// occurs. GPU data movement is handled by `GpuSession` in yscv-kernels.
    pub fn to_device(&self, device: Device) -> Self {
        Self {
            shape: self.shape.clone(),
            strides: self.strides.clone(),
            storage: self.storage.clone(),
            device,
        }
    }

    /// Returns `true` if the tensor stores f32 data.
    pub fn is_f32(&self) -> bool {
        matches!(self.storage, Storage::F32(_))
    }

    /// Fallible version of `data()` — returns an error for non-F32 tensors.
    pub fn try_data(&self) -> Result<&[f32], TensorError> {
        match &self.storage {
            Storage::F32(v) => Ok(v),
            _ => Err(TensorError::DTypeMismatch {
                expected: DType::F32,
                got: self.dtype(),
            }),
        }
    }

    /// Fallible version of `data_mut()` — returns an error for non-F32 tensors.
    pub fn try_data_mut(&mut self) -> Result<&mut [f32], TensorError> {
        let dt = self.storage.dtype();
        match &mut self.storage {
            Storage::F32(v) => Ok(v),
            _ => Err(TensorError::DTypeMismatch {
                expected: DType::F32,
                got: dt,
            }),
        }
    }

    /// Returns an immutable view over contiguous f32 storage.
    ///
    /// # Panics
    /// Panics if the tensor dtype is not F32. Use `try_data()` for a fallible version.
    pub fn data(&self) -> &[f32] {
        self.try_data().expect("tensor is not F32")
    }

    /// Returns a mutable view over contiguous f32 storage.
    ///
    /// # Panics
    /// Panics if the tensor dtype is not F32. Use `try_data_mut()` for a fallible version.
    pub fn data_mut(&mut self) -> &mut [f32] {
        self.try_data_mut().expect("tensor is not F32")
    }

    /// Alias for `try_data()` for backward compatibility.
    pub fn try_data_f32(&self) -> Result<&[f32], TensorError> {
        self.try_data()
    }

    /// Returns raw FP16 bit-pattern data if dtype is F16.
    pub fn data_f16(&self) -> Result<&[u16], TensorError> {
        match &self.storage {
            Storage::F16(v) => Ok(v),
            _ => Err(TensorError::DTypeMismatch {
                expected: DType::F16,
                got: self.dtype(),
            }),
        }
    }

    /// Returns raw BF16 bit-pattern data if dtype is BF16.
    pub fn data_bf16(&self) -> Result<&[u16], TensorError> {
        match &self.storage {
            Storage::BF16(v) => Ok(v),
            _ => Err(TensorError::DTypeMismatch {
                expected: DType::BF16,
                got: self.dtype(),
            }),
        }
    }

    /// Converts this tensor to the specified dtype, returning a new tensor.
    /// Converting to the same dtype is a no-op clone.
    pub fn to_dtype(&self, target: DType) -> Self {
        if self.dtype() == target {
            return self.clone();
        }
        let f32_data = self.to_f32_vec();
        let storage = match target {
            DType::F32 => Storage::F32(AlignedVec::from_vec(f32_data)),
            DType::F16 => Storage::F16(f32_data.iter().map(|&v| f32_to_fp16_bits(v)).collect()),
            DType::BF16 => Storage::BF16(f32_data.iter().map(|&v| f32_to_bf16_bits(v)).collect()),
        };
        Self {
            shape: self.shape.clone(),
            strides: self.strides.clone(),
            storage,
            device: self.device,
        }
    }

    /// Returns f32 data regardless of internal dtype (converts if necessary).
    pub(crate) fn to_f32_vec(&self) -> Vec<f32> {
        match &self.storage {
            Storage::F32(v) => v.as_slice().to_vec(),
            Storage::F16(v) => v.iter().map(|&bits| fp16_bits_to_f32(bits)).collect(),
            Storage::BF16(v) => v.iter().map(|&bits| bf16_bits_to_f32(bits)).collect(),
        }
    }

    /// Reads one element by multi-dimensional index (always returns f32).
    pub fn get(&self, indices: &[usize]) -> Result<f32, TensorError> {
        let offset = self.offset_from_indices(indices)?;
        Ok(match &self.storage {
            Storage::F32(v) => v[offset],
            Storage::F16(v) => fp16_bits_to_f32(v[offset]),
            Storage::BF16(v) => bf16_bits_to_f32(v[offset]),
        })
    }

    /// Writes one element by multi-dimensional index (stores as native dtype).
    pub fn set(&mut self, indices: &[usize], value: f32) -> Result<(), TensorError> {
        let offset = self.offset_from_indices(indices)?;
        match &mut self.storage {
            Storage::F32(v) => v[offset] = value,
            Storage::F16(v) => v[offset] = f32_to_fp16_bits(value),
            Storage::BF16(v) => v[offset] = f32_to_bf16_bits(value),
        }
        Ok(())
    }

    /// Returns a reshaped tensor view with copied metadata and data.
    pub fn reshape(&self, new_shape: Vec<usize>) -> Result<Self, TensorError> {
        let new_count =
            shape_element_count(&new_shape).ok_or_else(|| TensorError::SizeOverflow {
                shape: new_shape.clone(),
            })?;
        if new_count != self.len() {
            return Err(TensorError::ReshapeSizeMismatch {
                from: self.shape.to_vec(),
                to: new_shape,
            });
        }

        let new_strides = compute_strides(&new_shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: new_shape.clone(),
        })?;

        Ok(Self {
            shape: DimsVec::from(new_shape),
            strides: DimsVec::from(new_strides),
            storage: self.storage.clone(),
            device: self.device,
        })
    }

    /// Consumes the tensor and returns a reshaped version without copying data.
    pub fn into_reshape(self, new_shape: Vec<usize>) -> Result<Self, TensorError> {
        let new_count =
            shape_element_count(&new_shape).ok_or_else(|| TensorError::SizeOverflow {
                shape: new_shape.clone(),
            })?;
        if new_count != self.len() {
            return Err(TensorError::ReshapeSizeMismatch {
                from: self.shape.to_vec(),
                to: new_shape,
            });
        }

        let new_strides = compute_strides(&new_shape).ok_or_else(|| TensorError::SizeOverflow {
            shape: new_shape.clone(),
        })?;

        Ok(Self {
            shape: DimsVec::from(new_shape),
            strides: DimsVec::from(new_strides),
            storage: self.storage,
            device: self.device,
        })
    }

    pub(crate) fn offset_from_indices(&self, indices: &[usize]) -> Result<usize, TensorError> {
        if indices.len() != self.rank() {
            return Err(TensorError::InvalidIndexRank {
                expected: self.rank(),
                got: indices.len(),
            });
        }

        let mut offset = 0usize;
        for (axis, (index, dim)) in indices.iter().zip(self.shape.iter()).enumerate() {
            if *index >= *dim {
                return Err(TensorError::IndexOutOfBounds {
                    axis,
                    index: *index,
                    dim: *dim,
                });
            }
            offset = offset
                .checked_add(index.checked_mul(self.strides[axis]).ok_or_else(|| {
                    TensorError::SizeOverflow {
                        shape: self.shape.to_vec(),
                    }
                })?)
                .ok_or_else(|| TensorError::SizeOverflow {
                    shape: self.shape.to_vec(),
                })?;
        }
        Ok(offset)
    }
}

// ── FP16/BF16 bit conversion primitives ────────────────────────────

fn f32_to_fp16_bits(val: f32) -> u16 {
    let bits = val.to_bits();
    let sign = ((bits >> 16) & 0x8000) as u16;
    let exponent = ((bits >> 23) & 0xFF) as i32;
    let mantissa = bits & 0x007FFFFF;

    if exponent == 0xFF {
        return sign | 0x7C00 | if mantissa != 0 { 0x0200 } else { 0 };
    }
    let unbiased = exponent - 127;
    if unbiased < -24 {
        return sign;
    }
    if unbiased < -14 {
        let shift = -1 - unbiased;
        let subnormal = ((mantissa | 0x00800000) >> (shift + 13)) as u16;
        return sign | subnormal;
    }
    if unbiased > 15 {
        return sign | 0x7C00;
    }
    let fp16_exp = ((unbiased + 15) as u16) << 10;
    let fp16_man = (mantissa >> 13) as u16;
    sign | fp16_exp | fp16_man
}

fn fp16_bits_to_f32(half: u16) -> f32 {
    let sign = ((half & 0x8000) as u32) << 16;
    let exponent = (half >> 10) & 0x1F;
    let mantissa = (half & 0x03FF) as u32;
    if exponent == 0 {
        if mantissa == 0 {
            return f32::from_bits(sign);
        }
        let mut e = 0i32;
        let mut m = mantissa;
        while m & 0x0400 == 0 {
            m <<= 1;
            e += 1;
        }
        let f32_exp = ((127 - 15 - e) as u32) << 23;
        let f32_man = (m & 0x03FF) << 13;
        return f32::from_bits(sign | f32_exp | f32_man);
    }
    if exponent == 31 {
        let f32_bits = sign | 0x7F800000 | if mantissa != 0 { 0x00400000 } else { 0 };
        return f32::from_bits(f32_bits);
    }
    let f32_exp = ((exponent as u32) + 112) << 23;
    let f32_man = mantissa << 13;
    f32::from_bits(sign | f32_exp | f32_man)
}

fn f32_to_bf16_bits(val: f32) -> u16 {
    let bits = val.to_bits();
    // Round to nearest even
    let rounding_bias = 0x7FFF + ((bits >> 16) & 1);
    ((bits.wrapping_add(rounding_bias)) >> 16) as u16
}

fn bf16_bits_to_f32(bits: u16) -> f32 {
    f32::from_bits((bits as u32) << 16)
}

// ── Display impl ────────────────────────────────────────────────────

impl std::fmt::Display for Tensor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let shape = self.shape();
        let dtype = self.dtype();
        let n = self.len();

        write!(f, "Tensor({shape:?}, {dtype:?}")?;

        // Show first few and last few values for a compact preview.
        const MAX_SHOW: usize = 6;
        if n == 0 {
            write!(f, ", []")?;
        } else {
            let vals = self.to_f32_vec();
            write!(f, ", [")?;
            if n <= MAX_SHOW {
                for (i, v) in vals.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{v}")?;
                }
            } else {
                let head = MAX_SHOW / 2;
                let tail = MAX_SHOW - head;
                for (i, v) in vals[..head].iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{v}")?;
                }
                write!(f, ", ...")?;
                for v in &vals[n - tail..] {
                    write!(f, ", {v}")?;
                }
            }
            write!(f, "]")?;
        }
        write!(f, ")")
    }
}

// ── Operator overloading (std::ops) ─────────────────────────────────

impl std::ops::Add for &Tensor {
    type Output = Tensor;
    /// Element-wise addition. Panics on shape mismatch.
    fn add(self, rhs: Self) -> Tensor {
        Tensor::add(self, rhs).expect("Tensor + Tensor: shape mismatch")
    }
}

impl std::ops::Add for Tensor {
    type Output = Tensor;
    fn add(self, rhs: Self) -> Tensor {
        Tensor::add(&self, &rhs).expect("Tensor + Tensor: shape mismatch")
    }
}

impl std::ops::Sub for &Tensor {
    type Output = Tensor;
    fn sub(self, rhs: Self) -> Tensor {
        Tensor::sub(self, rhs).expect("Tensor - Tensor: shape mismatch")
    }
}

impl std::ops::Sub for Tensor {
    type Output = Tensor;
    fn sub(self, rhs: Self) -> Tensor {
        Tensor::sub(&self, &rhs).expect("Tensor - Tensor: shape mismatch")
    }
}

impl std::ops::Mul for &Tensor {
    type Output = Tensor;
    /// Element-wise multiplication. Panics on shape mismatch.
    fn mul(self, rhs: Self) -> Tensor {
        Tensor::mul(self, rhs).expect("Tensor * Tensor: shape mismatch")
    }
}

impl std::ops::Mul for Tensor {
    type Output = Tensor;
    fn mul(self, rhs: Self) -> Tensor {
        Tensor::mul(&self, &rhs).expect("Tensor * Tensor: shape mismatch")
    }
}

impl std::ops::Mul<f32> for &Tensor {
    type Output = Tensor;
    /// Scalar multiplication.
    fn mul(self, rhs: f32) -> Tensor {
        Tensor::scale(self, rhs)
    }
}

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

impl std::ops::Div for &Tensor {
    type Output = Tensor;
    fn div(self, rhs: Self) -> Tensor {
        Tensor::div(self, rhs).expect("Tensor / Tensor: shape mismatch")
    }
}

impl std::ops::Div for Tensor {
    type Output = Tensor;
    fn div(self, rhs: Self) -> Tensor {
        Tensor::div(&self, &rhs).expect("Tensor / Tensor: shape mismatch")
    }
}

impl std::ops::Neg for &Tensor {
    type Output = Tensor;
    fn neg(self) -> Tensor {
        Tensor::neg(self)
    }
}

impl std::ops::Neg for Tensor {
    type Output = Tensor;
    fn neg(self) -> Tensor {
        Tensor::neg(&self)
    }
}