|
| 1 | +use std::{ |
| 2 | + any::TypeId, |
| 3 | + mem, |
| 4 | + os::raw::{c_int, c_void}, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::ffi::{ |
| 8 | + DLContext, DLDataType, DLDataTypeCode_kDLFloat, DLDataTypeCode_kDLInt, DLDataTypeCode_kDLUInt, |
| 9 | + DLDeviceType_kDLCPU, DLTensor, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 13 | +pub struct DataType { |
| 14 | + pub code: usize, |
| 15 | + pub bits: usize, |
| 16 | + pub lanes: usize, |
| 17 | +} |
| 18 | + |
| 19 | +impl DataType { |
| 20 | + /// Returns the number of bytes occupied by an element of this `DataType`. |
| 21 | + pub fn itemsize(&self) -> usize { |
| 22 | + (self.bits * self.lanes) >> 3 |
| 23 | + } |
| 24 | + |
| 25 | + /// Returns whether this `DataType` represents primitive type `T`. |
| 26 | + pub fn is_type<T: 'static>(&self) -> bool { |
| 27 | + if self.lanes != 1 { |
| 28 | + return false; |
| 29 | + } |
| 30 | + let typ = TypeId::of::<T>(); |
| 31 | + (typ == TypeId::of::<i32>() && self.code == 0 && self.bits == 32) |
| 32 | + || (typ == TypeId::of::<i64>() && self.code == 0 && self.bits == 64) |
| 33 | + || (typ == TypeId::of::<u32>() && self.code == 1 && self.bits == 32) |
| 34 | + || (typ == TypeId::of::<u64>() && self.code == 1 && self.bits == 64) |
| 35 | + || (typ == TypeId::of::<f32>() && self.code == 2 && self.bits == 32) |
| 36 | + || (typ == TypeId::of::<f64>() && self.code == 2 && self.bits == 64) |
| 37 | + } |
| 38 | + |
| 39 | + pub fn code(&self) -> usize { |
| 40 | + self.code |
| 41 | + } |
| 42 | + |
| 43 | + pub fn bits(&self) -> usize { |
| 44 | + self.bits |
| 45 | + } |
| 46 | + |
| 47 | + pub fn lanes(&self) -> usize { |
| 48 | + self.lanes |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl<'a> From<&'a DataType> for DLDataType { |
| 53 | + fn from(dtype: &'a DataType) -> Self { |
| 54 | + Self { |
| 55 | + code: dtype.code as u8, |
| 56 | + bits: dtype.bits as u8, |
| 57 | + lanes: dtype.lanes as u16, |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl From<DLDataType> for DataType { |
| 63 | + fn from(dtype: DLDataType) -> Self { |
| 64 | + Self { |
| 65 | + code: dtype.code as usize, |
| 66 | + bits: dtype.bits as usize, |
| 67 | + lanes: dtype.lanes as usize, |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Debug, Clone, Copy, PartialEq)] |
| 73 | +pub struct TVMContext { |
| 74 | + pub device_type: usize, |
| 75 | + pub device_id: usize, |
| 76 | +} |
| 77 | + |
| 78 | +impl<'a> From<&'a TVMContext> for DLContext { |
| 79 | + fn from(ctx: &'a TVMContext) -> Self { |
| 80 | + Self { |
| 81 | + device_type: ctx.device_type as u32, |
| 82 | + device_id: ctx.device_id as i32, |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl Default for TVMContext { |
| 88 | + fn default() -> Self { |
| 89 | + Self { |
| 90 | + device_type: DLDeviceType_kDLCPU as usize, |
| 91 | + device_id: 0, |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// `From` conversions to `DLTensor` for `ndarray::Array`. |
| 97 | +/// Takes a reference to the `ndarray` since `DLTensor` is not owned. |
| 98 | +macro_rules! impl_dltensor_from_ndarray { |
| 99 | + ($type:ty, $typecode:expr) => { |
| 100 | + impl<'a, D: ndarray::Dimension> From<&'a mut ndarray::Array<$type, D>> for DLTensor { |
| 101 | + fn from(arr: &'a mut ndarray::Array<$type, D>) -> Self { |
| 102 | + DLTensor { |
| 103 | + data: arr.as_mut_ptr() as *mut c_void, |
| 104 | + ctx: DLContext { |
| 105 | + device_type: DLDeviceType_kDLCPU, |
| 106 | + device_id: 0, |
| 107 | + }, |
| 108 | + ndim: arr.ndim() as c_int, |
| 109 | + dtype: DLDataType { |
| 110 | + code: $typecode as u8, |
| 111 | + bits: 8 * mem::size_of::<$type>() as u8, |
| 112 | + lanes: 1, |
| 113 | + }, |
| 114 | + shape: arr.shape().as_ptr() as *const i64 as *mut i64, |
| 115 | + strides: arr.strides().as_ptr() as *const isize as *mut i64, |
| 116 | + byte_offset: 0, |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + }; |
| 121 | +} |
| 122 | + |
| 123 | +impl_dltensor_from_ndarray!(f32, DLDataTypeCode_kDLFloat); |
| 124 | +impl_dltensor_from_ndarray!(f64, DLDataTypeCode_kDLFloat); |
| 125 | +impl_dltensor_from_ndarray!(i32, DLDataTypeCode_kDLInt); |
| 126 | +impl_dltensor_from_ndarray!(i64, DLDataTypeCode_kDLInt); |
| 127 | +impl_dltensor_from_ndarray!(u32, DLDataTypeCode_kDLUInt); |
| 128 | +impl_dltensor_from_ndarray!(u64, DLDataTypeCode_kDLUInt); |
0 commit comments