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
use std::str::FromStr;
use crate::ffi::*;
use thiserror::Error;
macro_rules! impl_pod_tvm_value {
($field:ident, $field_ty:ty, $( $ty:ty ),+) => {
$(
impl From<$ty> for TVMValue {
fn from(val: $ty) -> Self {
TVMValue { $field: val as $field_ty }
}
}
impl From<TVMValue> for $ty {
fn from(val: TVMValue) -> Self {
unsafe { val.$field as $ty }
}
}
)+
};
($field:ident, $ty:ty) => {
impl_pod_tvm_value!($field, $ty, $ty);
}
}
impl_pod_tvm_value!(v_int64, i64, i8, u8, i16, u16, i32, u32, i64, u64, isize, usize);
impl_pod_tvm_value!(v_float64, f64, f32, f64);
impl_pod_tvm_value!(v_type, DLDataType);
impl_pod_tvm_value!(v_device, DLDevice);
#[derive(Debug, Error)]
#[error("unsupported device: {0}")]
pub struct UnsupportedDeviceError(String);
macro_rules! impl_tvm_device {
( $( $dev_type:ident : [ $( $dev_name:ident ),+ ] ),+ ) => {
impl FromStr for DLDevice {
type Err = UnsupportedDeviceError;
fn from_str(type_str: &str) -> Result<Self, Self::Err> {
Ok(Self {
device_type: match type_str {
$( $( stringify!($dev_name) )|+ => $dev_type ),+,
_ => return Err(UnsupportedDeviceError(type_str.to_string())),
},
device_id: 0,
})
}
}
impl DLDevice {
$(
$(
pub fn $dev_name(device_id: usize) -> Self {
Self {
device_type: $dev_type,
device_id: device_id as i32,
}
}
)+
)+
}
};
}
impl_tvm_device!(
DLDeviceType_kDLCPU: [cpu, llvm, stackvm],
DLDeviceType_kDLCUDA: [cuda, nvptx],
DLDeviceType_kDLOpenCL: [cl],
DLDeviceType_kDLMetal: [metal],
DLDeviceType_kDLVPI: [vpi],
DLDeviceType_kDLROCM: [rocm],
DLDeviceType_kDLExtDev: [ext_dev]
);