Skip to main content

tvm_ffi_sys/
dlpack.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// DLPack C ABI declarations
20// NOTE: we manually write the C ABI as they are reasonably minimal
21// and we need to ensure clear control of the atomic access etc.
22#![allow(non_camel_case_types)]
23
24// DLPack related declarations
25#[repr(u32)]
26#[derive(Debug, Copy, Clone, PartialEq, Eq)]
27#[non_exhaustive]
28pub enum DLDeviceType {
29    kDLCPU = 1,
30    kDLCUDA = 2,
31    kDLCUDAHost = 3,
32    kDLOpenCL = 4,
33    kDLVulkan = 7,
34    kDLMetal = 8,
35    kDLVPI = 9,
36    kDLROCM = 10,
37    kDLROCMHost = 11,
38    kDLExtDev = 12,
39    kDLCUDAManaged = 13,
40    kDLOneAPI = 14,
41    kDLWebGPU = 15,
42    kDLHexagon = 16,
43    kDLMAIA = 17,
44    kDLTrn = 18,
45    kDLTPU = 19,
46    kDLTPUHost = 20,
47    kDLAscend = 21,
48}
49
50#[repr(C)]
51#[derive(Debug, Copy, Clone, PartialEq, Eq)]
52pub struct DLDevice {
53    /// A [`DLDeviceType`] code, an `i32` so the codes stay an open set.
54    pub device_type: i32,
55    pub device_id: i32,
56}
57
58/// DLPack data type code enum
59#[repr(u8)]
60#[derive(Debug, Copy, Clone, PartialEq, Eq)]
61#[non_exhaustive]
62pub enum DLDataTypeCode {
63    kDLInt = 0,
64    kDLUInt = 1,
65    kDLFloat = 2,
66    kDLBfloat = 4,
67    kDLComplex = 5,
68    kDLOpaqueHandle = 3,
69    kDLBool = 6,
70    kDLFloat8_e3m4 = 7,
71    kDLFloat8_e4m3 = 8,
72    kDLFloat8_e4m3b11fnuz = 9,
73    kDLFloat8_e4m3fn = 10,
74    kDLFloat8_e4m3fnuz = 11,
75    kDLFloat8_e5m2 = 12,
76    kDLFloat8_e5m2fnuz = 13,
77    kDLFloat8_e8m0fnu = 14,
78    kDLFloat6_e2m3fn = 15,
79    kDLFloat6_e3m2fn = 16,
80    kDLFloat4_e2m1fn = 17,
81    kDLBcomplex = 18,
82}
83
84/// DLPack data type struct
85#[repr(C)]
86#[derive(Debug, Copy, Clone, PartialEq, Eq)]
87pub struct DLDataType {
88    pub code: u8,
89    pub bits: u8,
90    pub lanes: u16,
91}
92
93/// DLPack tensor struct - plain C tensor object, does not manage memory
94#[repr(C)]
95#[derive(Debug, Clone, PartialEq, Eq)]
96pub struct DLTensor {
97    /// The data pointer points to the allocated data
98    pub data: *mut core::ffi::c_void,
99    /// The device of the tensor
100    pub device: DLDevice,
101    /// Number of dimensions
102    pub ndim: i32,
103    /// The data type of the pointer
104    pub dtype: DLDataType,
105    /// The shape of the tensor
106    pub shape: *mut i64,
107    /// Strides of the tensor (in number of elements, not bytes)
108    /// Can be NULL, indicating tensor is compact and row-majored
109    pub strides: *mut i64,
110    /// The offset in bytes to the beginning pointer to data
111    pub byte_offset: u64,
112}
113
114impl DLDevice {
115    pub fn new(device_type: DLDeviceType, device_id: i32) -> Self {
116        Self {
117            device_type: device_type as i32,
118            device_id,
119        }
120    }
121}
122
123impl DLDataType {
124    pub fn new(code: DLDataTypeCode, bits: u8, lanes: u16) -> Self {
125        Self {
126            code: code as u8,
127            bits,
128            lanes,
129        }
130    }
131}