tvm-ffi 0.1.16

tvm-ffi rust support
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
//! [`Tensor`], an owned n-dimensional array, and [`TensorView`], a borrowed one.

use crate::any::AnyView;
use crate::collections::shape::Shape;
use crate::derive::{Object, ObjectRef};
use crate::dtype::AsDLDataType;
use crate::dtype::DLDataTypeExt;
use crate::error::Result;
use crate::object::{Object, ObjectArc, ObjectCore, ObjectCoreWithExtraItems};
use tvm_ffi_sys::TVMFFIAny;
use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
use tvm_ffi_sys::dlpack::{DLDataType, DLDevice, DLDeviceType, DLTensor};
use tvm_ffi_sys::{TVMFFIEnvTensorAlloc, TVMFFIObjectHandle};

//-----------------------------------------------------
// NDAllocator Trait
//-----------------------------------------------------
/// Allocates the data buffer of a tensor made by [`Tensor::from_nd_alloc`].
///
/// # Safety
/// `alloc_data` must return a pointer aligned to `MIN_ALIGN` and valid for the
/// prototype's data, `numel() * bits * lanes` bits rounded up to whole bytes
/// (sub-byte types are packed), and `free_data` must release
/// exactly what `alloc_data` returned. `free_data` may be called for a tensor
/// whose `data` is null, when `alloc_data` panicked, and must then do nothing.
///
/// `Send + Sync`: the tensor frees its data through the allocator when the
/// last reference drops, which may be on any thread.
pub unsafe trait NDAllocator: Send + Sync + 'static {
    /// The minimum alignment of the allocated data, in bytes.
    const MIN_ALIGN: usize;
    /// Allocate the data of a tensor.
    ///
    /// # Arguments
    /// * `prototype` - The tensor to allocate for; its data pointer is still null.
    ///
    /// # Returns
    /// The data pointer, which the tensor stores.
    ///
    /// # Safety
    /// `prototype` must have a valid shape and dtype.
    unsafe fn alloc_data(&mut self, prototype: &DLTensor) -> *mut core::ffi::c_void;

    /// Free the data of a tensor, when the tensor is dropped.
    ///
    /// # Arguments
    /// * `tensor` - The tensor whose `data` to free.
    ///
    /// # Safety
    /// `tensor.data` must be null or come from `alloc_data` on this allocator
    /// with the same shape and dtype, and must not be used afterwards.
    unsafe fn free_data(&mut self, tensor: &DLTensor);
}

/// Size queries on a `DLTensor`.
pub trait DLTensorExt {
    /// The number of elements: the product of the shape, 1 for a scalar.
    ///
    /// # Safety
    /// `shape` points to `ndim` extents, or `ndim` is 0.
    ///
    /// # Panics
    /// If an extent is negative or the product overflows `usize`.
    unsafe fn numel(&self) -> usize;
}

impl DLTensorExt for DLTensor {
    unsafe fn numel(&self) -> usize {
        // A scalar (`ndim == 0`) has one element and may have a null shape.
        let shape =
            unsafe { crate::util::slice_from_nullable_raw_parts(self.shape, self.ndim as usize) };
        checked_numel(shape).expect("tensor shape has a negative extent or overflows usize")
    }
}

/// The product of `shape`; `None` if an extent is negative or it overflows.
fn checked_numel(shape: &[i64]) -> Option<usize> {
    shape.iter().try_fold(1usize, |numel, &extent| {
        numel.checked_mul(usize::try_from(extent).ok()?)
    })
}

/// The bytes of `tensor`'s data, with sub-byte types packed as C++
/// `GetDataSize` does; it fits in `isize`, as an allocation must.
///
/// # Safety
/// As [`DLTensorExt::numel`].
unsafe fn data_size(tensor: &DLTensor) -> usize {
    let bits = tensor.dtype.bits as usize * tensor.dtype.lanes as usize;
    unsafe { tensor.numel() }
        .checked_mul(bits)
        .map(|total| total.div_ceil(8))
        .filter(|&size| size <= isize::MAX as usize)
        .expect("tensor byte size overflows isize")
}

//-----------------------------------------------------
// Tensor
//-----------------------------------------------------
/// The object behind a [`Tensor`]: an object header followed by a `DLTensor`.
#[repr(C)]
#[derive(Object)]
#[object(type_key = "ffi.Tensor", unsafe type_index = TypeIndex::kTVMFFITensor)]
pub struct TensorObj {
    object: Object,
    dltensor: DLTensor,
}

/// A reference-counted n-dimensional array, passed across languages as
/// `ffi.Tensor` and exchanged with other frameworks through DLPack.
///
/// # Examples
///
/// ```
/// use tvm_ffi::Tensor;
///
/// let tensor = Tensor::from_slice(&[1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3])?;
/// assert_eq!(tensor.shape(), &[2, 3]);
/// assert_eq!(tensor.numel(), 6);
/// assert_eq!(tensor.data_as_slice::<f32>()?[4], 5.0);
/// # Ok::<(), tvm_ffi::Error>(())
/// ```
#[repr(C)]
#[derive(ObjectRef, Clone)]
#[object_ref(skip_deref)]
pub struct Tensor {
    data: ObjectArc<TensorObj>,
}

impl Tensor {
    /// The data pointer, as in the `DLTensor`; `byte_offset` is not applied.
    ///
    /// A tensor is a shared handle, so `&mut self` would not make writes
    /// exclusive; getting the pointer is safe, and writing through it is the
    /// caller's `unsafe` responsibility, as for
    /// [`data_as_slice_mut`](Self::data_as_slice_mut).
    pub fn data_ptr(&self) -> *mut core::ffi::c_void {
        self.data.dltensor.data
    }

    /// Whether the elements are laid out in row-major order without gaps; see
    /// [`TensorView::is_contiguous`].
    pub fn is_contiguous(&self) -> bool {
        TensorView::from(self).is_contiguous()
    }

    /// The elements as a slice, starting `byte_offset` bytes into the data.
    ///
    /// # Errors
    /// A `TypeError` if `T` is not the tensor's dtype, and a `RuntimeError`
    /// if the tensor is not on the CPU, not contiguous, or its data is not
    /// aligned for `T`.
    pub fn data_as_slice<T: AsDLDataType>(&self) -> Result<&[T]> {
        let (data, len) = self.cpu_elements::<T>()?;
        unsafe { Ok(crate::util::slice_from_nullable_raw_parts(data, len)) }
    }
    /// The elements as a mutable slice, starting `byte_offset` bytes into the
    /// data.
    ///
    /// It takes `&self`: a tensor is a shared handle, and kernels write the
    /// data of tensors they receive by shared reference, so `&mut self` would
    /// not make the write exclusive.
    ///
    /// # Errors
    /// As [`data_as_slice`](Self::data_as_slice).
    ///
    /// # Safety
    ///
    /// While the returned slice is alive, nothing else may access the data:
    /// no other slice from this method or [`data_as_slice`](Self::data_as_slice)
    /// on this tensor or a clone of it, and no kernel or foreign caller writing
    /// the buffer. Rust cannot check this because the method takes `&self`, as
    /// `std::slice::from_raw_parts_mut` cannot check it for a raw pointer.
    #[allow(clippy::wrong_self_convention, clippy::mut_from_ref)]
    pub unsafe fn data_as_slice_mut<T: AsDLDataType>(&self) -> Result<&mut [T]> {
        let (data, len) = self.cpu_elements::<T>()?;
        unsafe { Ok(crate::util::slice_from_nullable_raw_parts_mut(data, len)) }
    }

    /// The first element and element count, checked for a slice of `T`.
    fn cpu_elements<T: AsDLDataType>(&self) -> Result<(*mut T, usize)> {
        let dtype = T::DL_DATA_TYPE;
        if self.dtype() != dtype {
            crate::bail!(
                crate::error::TYPE_ERROR,
                "Data type mismatch: expected {}, got {}",
                dtype.to_string(),
                self.dtype().to_string()
            );
        }
        if self.device().device_type != DLDeviceType::kDLCPU as i32 {
            crate::bail!(crate::error::RUNTIME_ERROR, "Tensor is not on CPU");
        }
        crate::ensure!(
            self.is_contiguous(),
            crate::error::RUNTIME_ERROR,
            "Tensor is not contiguous"
        );
        let len = self.numel();
        let t = &self.data.dltensor;
        let data = t
            .data
            .cast::<u8>()
            .wrapping_add(t.byte_offset as usize)
            .cast::<T>();
        // Imported DLPack data may sit at any address.
        crate::ensure!(
            len == 0 || data.is_aligned(),
            crate::error::RUNTIME_ERROR,
            "Tensor data is not aligned for {}",
            dtype.to_string()
        );
        Ok((data, len))
    }

    /// The extent of each dimension.
    pub fn shape(&self) -> &[i64] {
        let t = &self.data.dltensor;
        unsafe { crate::util::slice_from_nullable_raw_parts(t.shape, t.ndim as usize) }
    }

    /// The number of dimensions.
    pub fn ndim(&self) -> usize {
        self.data.dltensor.ndim as usize
    }

    /// The number of elements.
    ///
    /// # Panics
    /// If an extent is negative or the product overflows `usize`.
    pub fn numel(&self) -> usize {
        // SAFETY: a tensor's shape holds `ndim` extents.
        unsafe { self.data.dltensor.numel() }
    }

    /// The strides, in elements.
    ///
    /// # Panics
    /// If the tensor has null strides and at least one dimension, as
    /// [`TensorView::strides`] does.
    pub fn strides(&self) -> &[i64] {
        let t = &self.data.dltensor;
        if t.ndim == 0 {
            return &[];
        }
        assert!(
            !t.strides.is_null(),
            "Tensor::strides of a DLTensor with null strides"
        );
        unsafe { std::slice::from_raw_parts(t.strides, t.ndim as usize) }
    }

    /// The element type.
    pub fn dtype(&self) -> DLDataType {
        self.data.dltensor.dtype
    }

    /// The device that holds the data.
    pub fn device(&self) -> DLDevice {
        self.data.dltensor.device
    }
}

#[repr(C)]
struct TensorObjFromNDAlloc<TNDAlloc>
where
    TNDAlloc: NDAllocator,
{
    base_: TensorObj,
    alloc: TNDAlloc,
}

unsafe impl<TNDAlloc: NDAllocator> ObjectCore for TensorObjFromNDAlloc<TNDAlloc> {
    type Parent = TensorObj;
    const TYPE_KEY: &'static str = TensorObj::TYPE_KEY;
    const TYPE_DEPTH: i32 = TensorObj::TYPE_DEPTH;
    fn type_index() -> i32 {
        TensorObj::type_index()
    }
    unsafe fn object_header_mut(this: &mut Self) -> &mut tvm_ffi_sys::TVMFFIObject {
        unsafe { TensorObj::object_header_mut(&mut this.base_) }
    }
}

unsafe impl<TNDAlloc: NDAllocator> ObjectCoreWithExtraItems for TensorObjFromNDAlloc<TNDAlloc> {
    type ExtraItem = i64;
    fn extra_items_count(this: &Self) -> usize {
        (this.base_.dltensor.ndim * 2) as usize
    }
}

impl<TNDAlloc: NDAllocator> Drop for TensorObjFromNDAlloc<TNDAlloc> {
    fn drop(&mut self) {
        unsafe {
            self.alloc.free_data(&self.base_.dltensor);
        }
    }
}

impl Tensor {
    /// Create a contiguous tensor whose data `alloc` allocates and frees.
    ///
    /// # Arguments
    /// * `alloc` - The allocator, kept by the tensor until it is dropped.
    /// * `shape` - The shape of the tensor.
    /// * `dtype` - The element type.
    /// * `device` - The device of the data.
    ///
    /// # Returns
    /// The new tensor, with row-major strides.
    ///
    /// # Panics
    /// If an extent of `shape` is negative or the byte size overflows `isize`.
    pub fn from_nd_alloc<TNDAlloc>(
        alloc: TNDAlloc,
        shape: &[i64],
        dtype: DLDataType,
        device: DLDevice,
    ) -> Self
    where
        TNDAlloc: NDAllocator,
    {
        let ndim = i32::try_from(shape.len()).expect("tensor ndim exceeds i32");
        let dltensor = DLTensor {
            data: std::ptr::null_mut(),
            device,
            ndim,
            dtype,
            shape: shape.as_ptr() as *mut i64,
            strides: std::ptr::null_mut(),
            byte_offset: 0,
        };
        // Allocators size the data from the shape, so check it before allocating.
        // SAFETY: `shape` holds `ndim` extents.
        unsafe { data_size(&dltensor) };
        let tensor_obj = TensorObjFromNDAlloc {
            base_: TensorObj {
                object: Object::new(),
                dltensor: DLTensor {
                    shape: std::ptr::null_mut(),
                    ..dltensor
                },
            },
            alloc,
        };
        unsafe {
            let mut obj_arc = ObjectArc::new_with_extra_items(tensor_obj);
            // The fresh allocation is unique and has not been exposed.
            let object = ObjectArc::as_mut_ptr(&mut obj_arc);
            let items = TensorObjFromNDAlloc::extra_items_ptr(object);
            std::ptr::copy_nonoverlapping(shape.as_ptr(), items, shape.len());
            let strides = items.add(shape.len());
            Shape::fill_strides_from_shape(
                shape,
                std::slice::from_raw_parts_mut(strides, shape.len()),
            );
            let object = &mut *object;
            object.base_.dltensor.shape = items;
            object.base_.dltensor.strides = strides;
            let dltensor_ptr = &object.base_.dltensor as *const DLTensor;
            object.base_.dltensor.data = object.alloc.alloc_data(&*dltensor_ptr);
            Self {
                data: ObjectArc::from_raw(ObjectArc::into_raw(obj_arc) as *mut TensorObj),
            }
        }
    }
    /// Create a contiguous tensor with this thread's environment allocator.
    ///
    /// Kernel libraries allocate intermediate tensors this way, so that they
    /// come from the caller's allocator, such as the one a framework sets
    /// with `TVMFFIEnvSetDLPackManagedTensorAllocator`.
    ///
    /// # Arguments
    /// * `shape` - The shape of the tensor.
    /// * `dtype` - The element type.
    /// * `device` - The device of the data.
    ///
    /// # Returns
    /// The new tensor.
    ///
    /// # Errors
    /// The error the allocator raises, or a `RuntimeError` when no allocator
    /// is set.
    pub fn from_env_alloc(shape: &[i64], dtype: DLDataType, device: DLDevice) -> Result<Self> {
        let ndim = i32::try_from(shape.len()).expect("tensor ndim exceeds i32");
        let mut prototype = DLTensor {
            data: std::ptr::null_mut(),
            device,
            ndim,
            dtype,
            shape: shape.as_ptr() as *mut i64,
            strides: std::ptr::null_mut(),
            byte_offset: 0,
        };
        let mut out: TVMFFIObjectHandle = std::ptr::null_mut();
        unsafe {
            if TVMFFIEnvTensorAlloc(&mut prototype, &mut out) != 0 {
                return Err(crate::error::Error::from_raised());
            }
            Ok(Self {
                data: ObjectArc::from_raw(out as *const TensorObj),
            })
        }
    }

    /// Create a CPU tensor holding a copy of `slice`.
    ///
    /// # Arguments
    /// * `slice` - The elements, in row-major order.
    /// * `shape` - The shape of the tensor.
    ///
    /// # Returns
    /// The new tensor, allocated with [`CPUNDAlloc`].
    ///
    /// # Errors
    /// A `ValueError` if `slice` does not have as many elements as `shape`,
    /// or `shape` has a negative extent.
    pub fn from_slice<T: AsDLDataType>(slice: &[T], shape: &[i64]) -> Result<Self> {
        if checked_numel(shape) != Some(slice.len()) {
            crate::bail!(crate::error::VALUE_ERROR, "Slice length mismatch");
        }
        let dtype = T::DL_DATA_TYPE;
        let device = DLDevice::new(DLDeviceType::kDLCPU, 0);
        let tensor = Tensor::from_nd_alloc(CPUNDAlloc, shape, dtype, device);
        // SAFETY: the tensor was just allocated, so nothing else can access it.
        unsafe { tensor.data_as_slice_mut::<T>()? }.copy_from_slice(slice);
        Ok(tensor)
    }
}

/// An [`NDAllocator`] for CPU memory from the Rust global allocator, aligned
/// to 64 bytes.
#[derive(Clone, Copy, Debug, Default)]
pub struct CPUNDAlloc;

unsafe impl NDAllocator for CPUNDAlloc {
    const MIN_ALIGN: usize = 64;

    unsafe fn alloc_data(&mut self, prototype: &DLTensor) -> *mut core::ffi::c_void {
        let size = unsafe { data_size(prototype) };
        // A zero-size allocation is undefined behavior; an empty tensor gets an
        // aligned dangling pointer, which `free_data` does not release.
        if size == 0 {
            return Self::MIN_ALIGN as *mut core::ffi::c_void;
        }
        let layout = std::alloc::Layout::from_size_align(size, Self::MIN_ALIGN).unwrap();
        let ptr = unsafe { std::alloc::alloc(layout) };
        if ptr.is_null() {
            std::alloc::handle_alloc_error(layout);
        }
        ptr as *mut core::ffi::c_void
    }

    unsafe fn free_data(&mut self, tensor: &DLTensor) {
        let size = unsafe { data_size(tensor) };
        if size == 0 {
            return;
        }
        let layout = std::alloc::Layout::from_size_align(size, Self::MIN_ALIGN).unwrap();
        unsafe { std::alloc::dealloc(tensor.data as *mut u8, layout) };
    }
}

/// A non-owning view of a tensor: a pointer to a `DLTensor`.
///
/// A function argument of this type accepts both a borrowed `DLTensor*` (type
/// index `kTVMFFIDLTensorPtr`, how C callers pass tensors) and a `Tensor`
/// object (type index `kTVMFFITensor`), and is passed on as a `DLTensor*`.
/// Prefer it over [`Tensor`] for arguments of exported functions that only
/// read a tensor's metadata and data.
///
/// The view does not keep anything alive: its lifetime `'a` ties it to what
/// it borrows. For the same reason, a view cannot be moved into an owned
/// [`Any`](crate::Any), which could outlive it; use [`Tensor`] instead.
/// Neither misuse compiles:
///
/// ```compile_fail,E0597
/// use tvm_ffi::{Tensor, TensorView};
/// let view = {
///     let tensor = Tensor::from_slice(&[1.0_f32], &[1]).unwrap();
///     TensorView::from(&tensor)
/// };
/// view.ndim();
/// ```
///
/// ```compile_fail,E0277
/// use tvm_ffi::{Any, Tensor, TensorView};
/// let tensor = Tensor::from_slice(&[1.0_f32], &[1]).unwrap();
/// let _ = Any::from(TensorView::from(&tensor));
/// ```
///
/// A view received as an argument of an exported function is valid only
/// during the call. The function may name any lifetime for it,
/// `TensorView<'static>` included, so the compiler does not stop it from
/// being kept past the call; doing so is undefined behavior.
#[derive(Clone, Copy, Debug)]
pub struct TensorView<'a> {
    tensor: *const DLTensor,
    _borrow: std::marker::PhantomData<&'a DLTensor>,
}

impl<'a> TensorView<'a> {
    /// Creates a view of a `DLTensor`.
    ///
    /// # Safety
    /// `tensor` must be non-null and valid for every use of the view.
    pub unsafe fn from_raw(tensor: *const DLTensor) -> Self {
        assert!(!tensor.is_null(), "TensorView of a null DLTensor");
        Self {
            tensor,
            _borrow: std::marker::PhantomData,
        }
    }

    /// The viewed `DLTensor`.
    pub fn as_raw(&self) -> *const DLTensor {
        self.tensor
    }

    fn dltensor(&self) -> &'a DLTensor {
        // SAFETY: the view borrows a live `DLTensor` for `'a`.
        unsafe { &*self.tensor }
    }

    /// The data pointer, as in the `DLTensor`; `byte_offset` is not applied.
    pub fn data_ptr(&self) -> *mut core::ffi::c_void {
        self.dltensor().data
    }

    /// The device that holds the data.
    pub fn device(&self) -> DLDevice {
        self.dltensor().device
    }

    /// The element type.
    pub fn dtype(&self) -> DLDataType {
        self.dltensor().dtype
    }

    /// The number of dimensions.
    pub fn ndim(&self) -> usize {
        self.dltensor().ndim as usize
    }

    /// The extent of each dimension.
    pub fn shape(&self) -> &'a [i64] {
        let t = self.dltensor();
        unsafe { crate::util::slice_from_nullable_raw_parts(t.shape, t.ndim as usize) }
    }

    /// The number of elements.
    ///
    /// # Panics
    /// If an extent is negative or the product overflows `usize`.
    pub fn numel(&self) -> usize {
        // SAFETY: a tensor's shape holds `ndim` extents.
        unsafe { self.dltensor().numel() }
    }

    /// The strides, in elements.
    ///
    /// # Panics
    /// If the `DLTensor` has null strides and at least one dimension, which
    /// DLPack forbids since v1.2.
    pub fn strides(&self) -> &'a [i64] {
        let t = self.dltensor();
        if t.ndim == 0 {
            return &[];
        }
        assert!(
            !t.strides.is_null(),
            "TensorView::strides of a DLTensor with null strides"
        );
        unsafe { std::slice::from_raw_parts(t.strides, t.ndim as usize) }
    }

    /// Whether the elements are laid out in row-major order without gaps: a
    /// `DLTensor` with null strides or no elements is contiguous, and a
    /// dimension of extent 1 may have any stride.
    pub fn is_contiguous(&self) -> bool {
        if self.dltensor().strides.is_null() {
            return true;
        }
        let shape = self.shape();
        if shape.contains(&0) {
            return true;
        }
        let mut expected_stride = 1;
        for (size, stride) in shape.iter().zip(self.strides()).rev() {
            if *size == 1 {
                continue;
            }
            if *stride != expected_stride {
                return false;
            }
            expected_stride *= size;
        }
        true
    }
}

impl<'a> From<&'a Tensor> for TensorView<'a> {
    fn from(tensor: &'a Tensor) -> Self {
        Self {
            tensor: &tensor.data.dltensor as *const DLTensor,
            _borrow: std::marker::PhantomData,
        }
    }
}

impl<'a> TensorView<'a> {
    /// The view of `value`'s cell, when it holds a `DLTensor*` or a tensor object.
    fn from_cell(value: &TVMFFIAny) -> Option<Self> {
        let tensor = if value.type_index == TypeIndex::kTVMFFIDLTensorPtr as i32 {
            unsafe { value.data_union.v_ptr as *const DLTensor }
        } else if value.type_index == TypeIndex::kTVMFFITensor as i32 {
            // A tensor object's DLTensor follows its object header.
            unsafe { &(*(value.data_union.v_obj as *const TensorObj)).dltensor as *const DLTensor }
        } else {
            return None;
        };
        Some(Self {
            tensor,
            _borrow: std::marker::PhantomData,
        })
    }

    /// The `TypeError` for a value that is not a tensor.
    fn mismatch(value: &TVMFFIAny, context: &str) -> crate::error::Error {
        let message = format!(
            "{context}Cannot convert from type `{}` to `DLTensor*`",
            crate::object::type_key_of_index(value.type_index)
        );
        crate::error::Error::new(crate::error::TYPE_ERROR, &message)
    }
}

// A view is not `AnyCompatible`: that trait builds values from a cell without
// a lifetime, so a view could outlive what it borrows, and a view cannot be
// held in an owned `Any` (C++ `TypeTraits<DLTensor*>::MoveToAny` throws). It
// instead converts from an `AnyView` with that view's lifetime, is a typed
// function argument, and passes as a `DLTensor*` argument.

// As C++ `AnyView::cast<TensorView>`: from a `DLTensor*` or a tensor object.
impl<'a> TryFrom<AnyView<'a>> for TensorView<'a> {
    type Error = crate::error::Error;
    #[inline]
    fn try_from(value: AnyView<'a>) -> Result<Self> {
        let cell = value.as_raw_ffi_any();
        Self::from_cell(cell).ok_or_else(|| Self::mismatch(cell, ""))
    }
}

// A typed function argument, valid only during the call; the lifetime is not
// checked against it (see the type docs).
impl crate::type_traits::TypeSchema for TensorView<'_> {
    fn type_schema() -> String {
        crate::type_traits::type_schema("DLTensor*", &[])
    }
}

impl crate::any::ArgTryFromAnyView for TensorView<'_> {
    fn try_from_any_view(value: &AnyView, arg_index: usize) -> Result<Self> {
        let cell = value.as_raw_ffi_any();
        Self::from_cell(cell)
            .ok_or_else(|| Self::mismatch(cell, &format!("Argument #{arg_index}: ")))
    }
}

// A view of the `DLTensor*`, for `call_packed` and other `AnyView` APIs.
impl<'a> From<&TensorView<'a>> for AnyView<'a> {
    fn from(view: &TensorView<'a>) -> Self {
        let mut cell = TVMFFIAny::new();
        cell.type_index = TypeIndex::kTVMFFIDLTensorPtr as i32;
        cell.data_union.v_ptr = view.tensor as *mut core::ffi::c_void;
        // SAFETY: the view keeps its `DLTensor` alive for `'a`.
        unsafe { AnyView::from_raw_ffi_any(cell) }
    }
}

// Passed to a function as a `DLTensor*`.
impl<'a> crate::function_internal::PackedCallArg for TensorView<'a> {
    type Holder = Self;
    #[inline(always)]
    fn into_packed_arg(self) -> Self {
        self
    }
    #[inline(always)]
    fn as_packed_arg(holder: &Self::Holder) -> AnyView<'_> {
        AnyView::from(holder)
    }
}

impl<'a, 'b> crate::function_internal::PackedCallArg for &'b TensorView<'a> {
    type Holder = Self;
    #[inline(always)]
    fn into_packed_arg(self) -> Self {
        self
    }
    #[inline(always)]
    fn as_packed_arg(holder: &Self::Holder) -> AnyView<'_> {
        AnyView::from(*holder)
    }
}