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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#[macro_export]
macro_rules! tvm_call {
($e:expr) => {{
if unsafe { $e } != 0 {
Err($crate::get_last_error().into())
} else {
Ok(())
}
}};
}
#[macro_export]
macro_rules! check_call {
($e:expr) => {{
if unsafe { $e } != 0 {
panic!("{}", $crate::get_last_error());
}
}};
}
pub mod array;
pub mod device;
pub mod errors;
pub mod function;
pub mod graph_rt;
pub mod map;
pub mod module;
pub mod ndarray;
pub mod object;
pub mod string;
mod to_function;
pub use object::*;
pub use string::*;
use std::{
ffi::{CStr, CString},
str,
};
pub use crate::{
device::{Device, DeviceType},
errors::*,
function::Function,
module::Module,
ndarray::NDArray,
};
pub use function::{ArgValue, RetValue};
pub use tvm_sys::byte_array::ByteArray;
pub use tvm_sys::datatype::DataType;
use tvm_sys::ffi;
pub use tvm_macros::external;
pub fn get_last_error() -> &'static str {
unsafe {
match CStr::from_ptr(ffi::TVMGetLastError()).to_str() {
Ok(s) => s,
Err(_) => "Invalid UTF-8 message",
}
}
}
pub(crate) fn set_last_error<E: std::error::Error>(err: &E) {
let c_string = CString::new(err.to_string()).unwrap();
unsafe {
ffi::TVMAPISetLastError(c_string.as_ptr());
}
}
pub fn version() -> &'static str {
match str::from_utf8(ffi::TVM_VERSION) {
Ok(s) => s,
Err(_) => "Invalid UTF-8 string",
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{ByteArray, DataType, Device};
use std::{convert::TryInto, str::FromStr};
#[test]
fn print_version() {
println!("TVM version: {}", version());
}
#[test]
fn set_error() {
let err = errors::NDArrayError::EmptyArray;
set_last_error(&err);
assert_eq!(
get_last_error().trim(),
errors::NDArrayError::EmptyArray.to_string()
);
}
#[test]
fn ty() {
let t = DataType::from_str("int32").unwrap();
let tvm: DataType = RetValue::from(t).try_into().unwrap();
assert_eq!(tvm, t);
}
#[test]
fn device() {
let c = Device::from_str("cuda").unwrap();
let tvm: Device = RetValue::from(c).try_into().unwrap();
assert_eq!(tvm, c);
}
}