tvm_ffi/collections/
tensor.rs

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
/*
 * 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.
 */
use crate::any::{AnyView, TryFromTemp};
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 crate::type_traits::AnyCompatible;
use tvm_ffi_sys::dlpack::{DLDataType, DLDevice, DLDeviceType, DLTensor};
use tvm_ffi_sys::TVMFFIAny;
use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
use tvm_ffi_sys::{TVMFFIEnvTensorAlloc, TVMFFIObjectHandle};

//-----------------------------------------------------
// NDAllocator Trait
//-----------------------------------------------------
/// Trait for n-dimensional array allocators
pub unsafe trait NDAllocator: 'static {
    /// The minimum alignment of the data allocated by the allocator
    const MIN_ALIGN: usize;
    /// Allocate data for the given DLTensor
    ///
    /// # Arguments
    /// * `tensor` - The DLTensor to allocate data for
    ///
    /// This method should fill in the data pointer of the DLTensor.
    unsafe fn alloc_data(&mut self, prototype: &DLTensor) -> *mut core::ffi::c_void;

    /// Free data for the given DLTensor
    ///
    /// # Arguments
    /// * `tensor` - The DLTensor to free data for
    ///
    /// This method should free the data pointer of the DLTensor.
    unsafe fn free_data(&mut self, tensor: &DLTensor);
}

/// DLTensorExt trait
/// This trait provides methods to get the number of elements and the item size of a DLTensor
pub trait DLTensorExt {
    fn numel(&self) -> usize;
    fn item_size(&self) -> usize;
}

impl DLTensorExt for DLTensor {
    fn numel(&self) -> usize {
        unsafe {
            std::slice::from_raw_parts(self.shape, self.ndim as usize)
                .iter()
                .product::<i64>() as usize
        }
    }

    fn item_size(&self) -> usize {
        (self.dtype.bits as usize * self.dtype.lanes as usize + 7) / 8
    }
}

//-----------------------------------------------------
// Shape
//-----------------------------------------------------
// ShapeObj for heap-allocated shape
#[repr(C)]
#[derive(Object)]
#[type_key = "ffi.Tensor"]
#[type_index(TypeIndex::kTVMFFITensor)]
pub struct TensorObj {
    object: Object,
    dltensor: DLTensor,
}

/// ABI stable owned Shape for ffi
#[repr(C)]
#[derive(ObjectRef, Clone)]
pub struct Tensor {
    data: ObjectArc<TensorObj>,
}

impl Tensor {
    /// Get the data pointer of the Tensor
    ///
    /// # Returns
    /// * `*mut core::ffi::c_void` - The data pointer of the Tensor
    pub fn data_ptr(&self) -> *const core::ffi::c_void {
        self.data.dltensor.data
    }
    /// Get the data pointer of the Tensor
    ///
    /// # Returns
    /// * `*mut core::ffi::c_void` - The data pointer of the Tensor
    pub fn data_ptr_mut(&mut self) -> *mut core::ffi::c_void {
        self.data.dltensor.data
    }
    /// Check if the Tensor is contiguous
    ///
    /// # Returns
    /// * `bool` - True if the Tensor is contiguous, false otherwise
    pub fn is_contiguous(&self) -> bool {
        let strides = self.strides();
        let shape = self.shape();
        let mut expected_stride = 1;
        for i in (0..self.ndim()).rev() {
            if strides[i] != expected_stride {
                return false;
            }
            expected_stride *= shape[i];
        }
        true
    }

    pub fn data_as_slice<T: AsDLDataType>(&self) -> Result<&[T]> {
        let dtype = T::DL_DATA_TYPE;
        if self.dtype() != dtype {
            crate::bail!(
                crate::error::TYPE_ERROR,
                "Data type mismatch {} vs {}",
                self.dtype().to_string(),
                dtype.to_string()
            );
        }
        if self.device().device_type != DLDeviceType::kDLCPU {
            crate::bail!(crate::error::RUNTIME_ERROR, "Tensor is not on CPU");
        }
        crate::ensure!(
            self.is_contiguous(),
            crate::error::RUNTIME_ERROR,
            "Tensor is not contiguous"
        );

        unsafe {
            Ok(std::slice::from_raw_parts(
                self.data.dltensor.data as *const T,
                self.numel(),
            ))
        }
    }
    /// Returns the tensor data as a mutable slice.
    ///
    /// This method takes `&self` rather than `&mut self` by design: like
    /// `std::fs::File::write`, the *metadata* of a Tensor (shape, dtype,
    /// device) is governed by Rust's ownership rules, but writing to the
    /// underlying data buffer (CPU memory or a GPU pointer) is a side-effect
    /// outside Rust's aliasing model.  Most C/CUDA kernel APIs accept a
    /// non-mut Tensor and mutate its data content, so requiring `&mut self`
    /// here would force artificial mutability annotations throughout the
    /// deep-learning stack with no real safety benefit.
    ///
    /// # Safety contract (caller responsibility)
    /// If the `Tensor` has been cloned (via `ObjectArc`), the caller must
    /// ensure no other clone is concurrently reading the data.
    #[allow(clippy::wrong_self_convention)]
    pub fn data_as_slice_mut<T: AsDLDataType>(&self) -> Result<&mut [T]> {
        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 {
            crate::bail!(crate::error::RUNTIME_ERROR, "Tensor is not on CPU");
        }
        crate::ensure!(
            self.is_contiguous(),
            crate::error::RUNTIME_ERROR,
            "Tensor is not contiguous"
        );
        unsafe {
            Ok(std::slice::from_raw_parts_mut(
                self.data.dltensor.data as *mut T,
                self.numel(),
            ))
        }
    }

    pub fn shape(&self) -> &[i64] {
        unsafe { std::slice::from_raw_parts(self.data.dltensor.shape, self.ndim()) }
    }

    pub fn ndim(&self) -> usize {
        self.data.dltensor.ndim as usize
    }

    pub fn numel(&self) -> usize {
        self.data.dltensor.numel()
    }

    pub fn strides(&self) -> &[i64] {
        unsafe { std::slice::from_raw_parts(self.data.dltensor.strides, self.ndim()) }
    }

    pub fn dtype(&self) -> DLDataType {
        self.data.dltensor.dtype
    }

    pub fn device(&self) -> DLDevice {
        self.data.dltensor.device
    }
}

struct TensorObjFromNDAlloc<TNDAlloc>
where
    TNDAlloc: NDAllocator,
{
    base: TensorObj,
    alloc: TNDAlloc,
}

unsafe impl<TNDAlloc: NDAllocator> ObjectCore for TensorObjFromNDAlloc<TNDAlloc> {
    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 {
        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 Tensor from a NDAllocator
    ///
    /// # Arguments
    /// * `alloc` - The NDAllocator
    /// * `shape` - The shape of the Tensor
    /// * `dtype` - The data type of the Tensor
    /// * `device` - The device of the Tensor
    ///
    /// # Returns
    /// * `Tensor` - The created Tensor
    pub fn from_nd_alloc<TNDAlloc>(
        alloc: TNDAlloc,
        shape: &[i64],
        dtype: DLDataType,
        device: DLDevice,
    ) -> Self
    where
        TNDAlloc: NDAllocator,
    {
        let tensor_obj = TensorObjFromNDAlloc {
            base: TensorObj {
                object: Object::new(),
                dltensor: DLTensor {
                    data: std::ptr::null_mut(),
                    device: device,
                    ndim: shape.len() as i32,
                    dtype: dtype,
                    shape: std::ptr::null_mut(),
                    strides: std::ptr::null_mut(),
                    byte_offset: 0,
                },
            },
            alloc: 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 = &mut *ObjectArc::as_raw_mut(&mut obj_arc);
            object.base.dltensor.shape =
                TensorObjFromNDAlloc::extra_items(object).as_ptr() as *mut i64;
            object.base.dltensor.strides = object.base.dltensor.shape.add(shape.len());
            let extra_items = TensorObjFromNDAlloc::extra_items_mut(object);
            extra_items[..shape.len()].copy_from_slice(shape);
            Shape::fill_strides_from_shape(shape, &mut extra_items[shape.len()..]);
            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 Tensor with the environment allocator of this thread, as C++
    /// `Tensor::FromEnvAlloc(TVMFFIEnvTensorAlloc, ...)` does.
    ///
    /// 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 data type of the Tensor
    /// * `device` - The device of the Tensor
    ///
    /// # Returns
    /// * `Result<Tensor>` - The created Tensor, or the error the allocator
    ///   raised, which is a `RuntimeError` when no allocator is set
    pub fn from_env_alloc(shape: &[i64], dtype: DLDataType, device: DLDevice) -> Result<Self> {
        let mut prototype = DLTensor {
            data: std::ptr::null_mut(),
            device,
            ndim: shape.len() as i32,
            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 Tensor from a slice
    ///
    /// # Arguments
    /// * `slice` - The slice to create the Tensor from
    /// * `shape` - The shape of the Tensor
    ///
    /// # Returns
    /// * `Tensor` - The created Tensor
    pub fn from_slice<T: AsDLDataType>(slice: &[T], shape: &[i64]) -> Result<Self> {
        let dtype = T::DL_DATA_TYPE;
        let device = DLDevice::new(DLDeviceType::kDLCPU, 0);
        let tensor = Tensor::from_nd_alloc(CPUNDAlloc {}, shape, dtype, device);
        if tensor.numel() != slice.len() {
            crate::bail!(crate::error::VALUE_ERROR, "Slice length mismatch");
        }
        tensor.data_as_slice_mut::<T>()?.copy_from_slice(slice);
        Ok(tensor)
    }
}

/// Example CPU NDAllocator
/// This allocator allocates data on the CPU
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 numel = prototype.numel() as usize;
        let item_size = prototype.item_size();
        let size = numel * item_size as usize;
        let layout = std::alloc::Layout::from_size_align(size, Self::MIN_ALIGN).unwrap();
        let ptr = std::alloc::alloc(layout);
        ptr as *mut core::ffi::c_void
    }

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

/// A non-owning view of a tensor: a pointer to a `DLTensor`.
///
/// This mirrors `tvm::ffi::TensorView` in C++. A function argument of this
/// type accepts both a borrowed `DLTensor*` (type index `kTVMFFIDLTensorPtr`,
/// how C and 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. The caller must ensure that the
/// `DLTensor` it points to, and the memory that the `DLTensor` points to
/// (data, shape and strides), outlive every use of the view. For an argument
/// of an exported function, that is the duration of the call. For the same
/// reason, as in C++, a view cannot be moved into an owned [`Any`](crate::Any);
/// use [`Tensor`] instead.
#[derive(Clone, Copy, Debug)]
pub struct TensorView {
    tensor: *const DLTensor,
}

impl TensorView {
    /// 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 }
    }

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

    fn dltensor(&self) -> &DLTensor {
        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
    }

    pub fn device(&self) -> DLDevice {
        self.dltensor().device
    }

    pub fn dtype(&self) -> DLDataType {
        self.dltensor().dtype
    }

    pub fn ndim(&self) -> usize {
        self.dltensor().ndim as usize
    }

    pub fn shape(&self) -> &[i64] {
        let t = self.dltensor();
        if t.ndim == 0 {
            return &[];
        }
        unsafe { std::slice::from_raw_parts(t.shape, t.ndim as usize) }
    }

    pub fn numel(&self) -> usize {
        self.shape().iter().product::<i64>() as usize
    }

    /// The strides, in elements.
    ///
    /// # Panics
    /// If the `DLTensor` has null strides and at least one dimension, which
    /// DLPack forbids since v1.2, as C++ `TensorView::strides` checks.
    pub fn strides(&self) -> &[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, as
    /// C++ `tvm::ffi::IsContiguous` decides: 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 From<&Tensor> for TensorView {
    fn from(tensor: &Tensor) -> Self {
        Self {
            tensor: &tensor.data.dltensor as *const DLTensor,
        }
    }
}

// A borrowed `AnyView` converts as C++ `AnyView::cast<TensorView>` does: from
// a `DLTensor*` or from a tensor object. `try_as`, like C++ `as`, accepts only
// a `DLTensor*`. There is no conversion from an owned `Any`, which would leave
// the view pointing into a value the conversion drops.
impl<'a> TryFrom<AnyView<'a>> for TensorView {
    type Error = crate::error::Error;
    #[inline]
    fn try_from(value: AnyView<'a>) -> Result<Self> {
        TryFromTemp::<Self>::try_from(value).map(TryFromTemp::into_value)
    }
}

// As in C++, where `TypeTraits<DLTensor*>::MoveToAny` throws this, and
// `TypeTraits<TensorView>` does not support moves.
const NOT_OWNED: &str =
    "DLTensor* cannot be held in Any as it does not retain ownership, use Tensor instead";

unsafe impl AnyCompatible for TensorView {
    fn type_str() -> String {
        // make it consistent with c++ representation
        "DLTensor*".to_string()
    }

    unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
        data.type_index = TypeIndex::kTVMFFIDLTensorPtr as i32;
        data.small_str_len = 0;
        data.data_union.v_uint64 = 0;
        data.data_union.v_ptr = src.tensor as *mut core::ffi::c_void;
    }

    unsafe fn move_to_any(_src: Self, _data: &mut TVMFFIAny) {
        panic!("{}", NOT_OWNED);
    }

    unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
        data.type_index == TypeIndex::kTVMFFIDLTensorPtr as i32
    }

    unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
        Self {
            tensor: data.data_union.v_ptr as *const DLTensor,
        }
    }

    unsafe fn move_from_any_after_check(_data: &mut TVMFFIAny) -> Self {
        panic!("{}", NOT_OWNED);
    }

    unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> std::result::Result<Self, ()> {
        if data.type_index == TypeIndex::kTVMFFIDLTensorPtr as i32 {
            Ok(Self::copy_from_any_view_after_check(data))
        } else if data.type_index == TypeIndex::kTVMFFITensor as i32 {
            // A tensor object's DLTensor follows its object header.
            let obj = data.data_union.v_obj as *const TensorObj;
            Ok(Self {
                tensor: &(*obj).dltensor as *const DLTensor,
            })
        } else {
            Err(())
        }
    }
}