tvm-ffi 0.1.16

tvm-ffi rust support
Documentation
/*
 * 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.
 */
//! [`Shape`], the immutable dimensions of a tensor.

use crate::derive::{Object, ObjectRef};
use crate::object::ObjectRefAnyCompatible;
use crate::object::{Object, ObjectArc, ObjectCoreWithExtraItems, PlainObjectRef};
use crate::{AnyView, Array};
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use std::ops::Deref;
use tvm_ffi_sys::TVMFFIShapeCell;
use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;

//-----------------------------------------------------
// Shape
//-----------------------------------------------------
/// The object behind a [`Shape`].
#[repr(C)]
#[derive(Object)]
#[object(type_key = "ffi.Shape", unsafe type_index = TypeIndex::kTVMFFIShape)]
pub struct ShapeObj {
    object: Object,
    data: TVMFFIShapeCell,
}

/// An immutable sequence of `i64` dimensions, passed across languages as
/// `ffi.Shape`. A converting cast, such as a typed function argument, also
/// takes an `Array` or `List` of integers, as C++ does.
///
/// # Examples
///
/// ```
/// use tvm_ffi::Shape;
///
/// let shape = Shape::from([2_i64, 3]);
/// assert_eq!(shape.as_slice(), &[2, 3]);
/// assert_eq!(shape.len(), 2);
/// ```
#[repr(C)]
#[derive(ObjectRef, Clone)]
#[object_ref(skip_deref, skip_any_compatible)]
pub struct Shape {
    data: ObjectArc<ShapeObj>,
}

// SAFETY: every `ShapeObj` is a `Shape`; the fallback only builds new shapes.
unsafe impl ObjectRefAnyCompatible for Shape {
    #[cold]
    fn convert_fallback(value: AnyView<'_>) -> Option<Self> {
        let dims = Array::<i64>::try_from(value).ok()?;
        Some(Self::from(dims.iter().copied().collect::<Vec<_>>()))
    }
}

// SAFETY: as for `ObjectRefAnyCompatible`, every `ShapeObj` is a `Shape`.
unsafe impl PlainObjectRef for Shape {}

// SAFETY: a `Shape` is immutable.
unsafe impl Send for Shape {}
unsafe impl Sync for Shape {}

impl Default for Shape {
    /// The empty shape, like [`Shape::new`].
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl Shape {
    /// An empty shape.
    pub fn new() -> Self {
        let shape_obj = ShapeObj {
            object: Object::new(),
            data: TVMFFIShapeCell {
                data: std::ptr::null(),
                size: 0,
            },
        };
        Self {
            data: ObjectArc::new(shape_obj),
        }
    }

    /// The dimensions as a slice.
    pub fn as_slice(&self) -> &[i64] {
        unsafe {
            crate::util::slice_from_nullable_raw_parts(self.data.data.data, self.data.data.size)
        }
    }

    /// Fill `strides` with the row-major strides of `shape`, wrapping on
    /// overflow as C++ does: only a zero-element tensor gets that far.
    pub(crate) fn fill_strides_from_shape<T>(shape: T, strides: &mut [i64])
    where
        T: AsRef<[i64]>,
    {
        let shape = shape.as_ref();
        let mut stride = 1;
        for i in (0..shape.len()).rev() {
            strides[i] = stride;
            stride = stride.wrapping_mul(shape[i]);
        }
    }
}

unsafe impl ObjectCoreWithExtraItems for ShapeObj {
    type ExtraItem = i64;
    fn extra_items_count(this: &Self) -> usize {
        this.data.size
    }
}

impl<T> From<T> for Shape
where
    T: AsRef<[i64]>,
{
    fn from(value: T) -> Self {
        unsafe {
            let value_slice: &[i64] = value.as_ref();
            let mut obj_arc = ObjectArc::new_with_extra_items(ShapeObj {
                object: Object::new(),
                data: TVMFFIShapeCell {
                    data: std::ptr::null(),
                    size: value_slice.len(),
                },
            });
            // The fresh allocation is unique and has not been exposed.
            let object = ObjectArc::as_mut_ptr(&mut obj_arc);
            let items = ShapeObj::extra_items_ptr(object);
            std::ptr::copy_nonoverlapping(value_slice.as_ptr(), items, value_slice.len());
            (*object).data.data = items;
            Self { data: obj_arc }
        }
    }
}

impl Deref for Shape {
    type Target = [i64];
    #[inline]
    fn deref(&self) -> &[i64] {
        self.as_slice()
    }
}

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

impl Eq for Shape {}

impl PartialOrd for Shape {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Shape {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_slice().cmp(other.as_slice())
    }
}