Skip to main content

tvm_ffi/
dtype.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19//! Data types: [`DLDataTypeExt`] and the Rust element types of tensors.
20
21use crate::error::Result;
22use crate::type_traits::AnyCompatible;
23use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
24use tvm_ffi_sys::dlpack::{DLDataType, DLDataTypeCode};
25use tvm_ffi_sys::{TVMFFIAny, TVMFFIByteArray, TVMFFIDataTypeFromString, TVMFFIDataTypeToString};
26
27/// Name conversions of a `DLDataType`.
28pub trait DLDataTypeExt: Sized {
29    /// Return the name of the data type, such as `"int32"`, `"float64"` or
30    /// `"int8x4"` (bits and lanes), the name every language uses for it, or
31    /// `"dtype(code=.., bits=.., lanes=..)"` for one the runtime cannot name.
32    ///
33    /// ```
34    /// use tvm_ffi::{DLDataType, DLDataTypeExt};
35    /// let dtype = DLDataType::try_from_str("float16")?;
36    /// assert_eq!(dtype.to_string(), "float16");
37    /// # Ok::<(), tvm_ffi::Error>(())
38    /// ```
39    fn to_string(&self) -> std::string::String;
40
41    /// Parse a data type from its name, the inverse of
42    /// [`to_string`](Self::to_string).
43    ///
44    /// # Arguments
45    /// * `dtype_str` - The name, such as `"int32"`.
46    ///
47    /// # Returns
48    /// The data type.
49    ///
50    /// # Errors
51    /// If the name is not a data type.
52    fn try_from_str(dtype_str: &str) -> Result<Self>;
53}
54
55impl DLDataTypeExt for DLDataType {
56    fn to_string(&self) -> std::string::String {
57        let name = unsafe {
58            let mut ffi_any = TVMFFIAny::new();
59            crate::check_safe_call!(TVMFFIDataTypeToString(self, &mut ffi_any)).and_then(|()| {
60                crate::string::String::try_from(crate::any::Any::from_raw_ffi_any(ffi_any))
61            })
62        };
63        match name {
64            Ok(name) => name.as_str().to_owned(),
65            Err(_) => format!(
66                "dtype(code={}, bits={}, lanes={})",
67                self.code, self.bits, self.lanes
68            ),
69        }
70    }
71
72    fn try_from_str(dtype_str: &str) -> Result<Self> {
73        let mut dtype = DLDataType {
74            code: DLDataTypeCode::kDLOpaqueHandle as u8,
75            bits: 0,
76            lanes: 0,
77        };
78        unsafe {
79            let dtype_byte_array = TVMFFIByteArray::from_str(dtype_str);
80            crate::check_safe_call!(TVMFFIDataTypeFromString(&dtype_byte_array, &mut dtype))?;
81        }
82        Ok(dtype)
83    }
84}
85
86unsafe impl AnyCompatible for DLDataType {
87    fn type_str() -> String {
88        // The C++ name.
89        "DataType".to_string()
90    }
91
92    unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
93        data.type_index = TypeIndex::kTVMFFIDataType as i32;
94        data.small_str_len = 0;
95        data.data_union.v_uint64 = 0;
96        data.data_union.v_dtype = *src;
97    }
98
99    unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny) {
100        data.type_index = TypeIndex::kTVMFFIDataType as i32;
101        data.small_str_len = 0;
102        data.data_union.v_int64 = 0;
103        data.data_union.v_dtype = src;
104    }
105
106    unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
107        data.type_index == TypeIndex::kTVMFFIDataType as i32
108    }
109
110    unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
111        unsafe { data.data_union.v_dtype }
112    }
113
114    unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
115        unsafe { data.data_union.v_dtype }
116    }
117
118    unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
119        if data.type_index == TypeIndex::kTVMFFIDataType as i32 {
120            Ok(unsafe { data.data_union.v_dtype })
121        } else if let Ok(string) = unsafe { crate::string::String::try_cast_from_any_view(data) } {
122            // A data type also converts from its name, such as "float32".
123            DLDataType::try_from_str(string.as_str()).map_err(|_| ())
124        } else {
125            Err(())
126        }
127    }
128}
129
130/// A Rust element type of a tensor, such as `f32`, and its `DLDataType`.
131///
132/// # Safety
133/// Tensor data of `DL_DATA_TYPE` is read as `Self` without checks, so `Self`
134/// must have that data type's item size, be no more aligned than the item
135/// size, and accept every bit pattern of it.
136pub unsafe trait AsDLDataType: Copy {
137    /// The data type of `Self`.
138    const DL_DATA_TYPE: DLDataType;
139}
140
141macro_rules! impl_as_dl_data_type {
142    ($type: ty, $code: expr, $bits: expr) => {
143        unsafe impl AsDLDataType for $type {
144            const DL_DATA_TYPE: DLDataType = DLDataType {
145                code: $code as u8,
146                bits: $bits as u8,
147                lanes: 1,
148            };
149        }
150    };
151}
152
153impl_as_dl_data_type!(i8, DLDataTypeCode::kDLInt, 8);
154impl_as_dl_data_type!(i16, DLDataTypeCode::kDLInt, 16);
155impl_as_dl_data_type!(i32, DLDataTypeCode::kDLInt, 32);
156impl_as_dl_data_type!(i64, DLDataTypeCode::kDLInt, 64);
157impl_as_dl_data_type!(u8, DLDataTypeCode::kDLUInt, 8);
158impl_as_dl_data_type!(u16, DLDataTypeCode::kDLUInt, 16);
159impl_as_dl_data_type!(u32, DLDataTypeCode::kDLUInt, 32);
160impl_as_dl_data_type!(u64, DLDataTypeCode::kDLUInt, 64);
161impl_as_dl_data_type!(f32, DLDataTypeCode::kDLFloat, 32);
162impl_as_dl_data_type!(f64, DLDataTypeCode::kDLFloat, 64);