Skip to content

Commit 32d40b2

Browse files
committed
Fix support for linking to only libtvm_runtime
also ensures that the ResNet example uses the new support.
1 parent 4b9d43e commit 32d40b2

File tree

8 files changed

+52
-43
lines changed

8 files changed

+52
-43
lines changed

rust/tvm-rt/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ edition = "2018"
3232
default = ["dynamic-linking"]
3333
dynamic-linking = ["tvm-sys/dynamic-linking"]
3434
static-linking = ["tvm-sys/static-linking"]
35+
standalone = ["tvm-sys/runtime-only"]
3536
blas = ["ndarray/blas"]
3637

3738
[dependencies]

rust/tvm/src/runtime/graph_rt.rs renamed to rust/tvm-rt/src/graph_rt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
use std::convert::TryInto;
2121

22-
use crate::runtime::Function;
23-
use crate::{runtime::function::Result, runtime::ByteArray, Device, Module, NDArray};
22+
use crate::Function;
23+
use crate::{function::Result, ByteArray, Device, Module, NDArray};
2424

2525
/// An instance of the C++ graph executor.
2626
///

rust/tvm-rt/src/lib.rs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,40 @@
2626
//! The TVM object system enables cross-language interoperability including that of closures for all
2727
//! supported languages including C++, and Python.
2828
29+
// Macro to check the return call to TVM runtime shared library.
30+
31+
#[macro_export]
32+
macro_rules! tvm_call {
33+
($e:expr) => {{
34+
if unsafe { $e } != 0 {
35+
Err($crate::get_last_error().into())
36+
} else {
37+
Ok(())
38+
}
39+
}};
40+
}
41+
42+
#[macro_export]
43+
macro_rules! check_call {
44+
($e:expr) => {{
45+
if unsafe { $e } != 0 {
46+
panic!("{}", $crate::get_last_error());
47+
}
48+
}};
49+
}
50+
51+
// Define all sumodules.
52+
pub mod array;
53+
pub mod device;
54+
pub mod errors;
55+
pub mod function;
56+
pub mod graph_rt;
57+
pub mod map;
58+
pub mod module;
59+
pub mod ndarray;
2960
pub mod object;
3061
pub mod string;
62+
mod to_function;
3163

3264
pub use object::*;
3365
pub use string::*;
@@ -52,28 +84,6 @@ use tvm_sys::ffi;
5284

5385
pub use tvm_macros::external;
5486

55-
// Macro to check the return call to TVM runtime shared library.
56-
57-
#[macro_export]
58-
macro_rules! tvm_call {
59-
($e:expr) => {{
60-
if unsafe { $e } != 0 {
61-
Err($crate::get_last_error().into())
62-
} else {
63-
Ok(())
64-
}
65-
}};
66-
}
67-
68-
#[macro_export]
69-
macro_rules! check_call {
70-
($e:expr) => {{
71-
if unsafe { $e } != 0 {
72-
panic!("{}", $crate::get_last_error());
73-
}
74-
}};
75-
}
76-
7787
/// Gets the last error message.
7888
pub fn get_last_error() -> &'static str {
7989
unsafe {
@@ -91,15 +101,6 @@ pub(crate) fn set_last_error<E: std::error::Error>(err: &E) {
91101
}
92102
}
93103

94-
pub mod array;
95-
pub mod device;
96-
pub mod errors;
97-
pub mod function;
98-
pub mod map;
99-
pub mod module;
100-
pub mod ndarray;
101-
mod to_function;
102-
103104
/// Outputs the current TVM version.
104105
pub fn version() -> &'static str {
105106
match str::from_utf8(ffi::TVM_VERSION) {

rust/tvm-sys/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ description = "Low level bindings to TVM's cross language API."
2727
default = ["dynamic-linking"]
2828
static-linking = []
2929
dynamic-linking = []
30+
runtime-only = []
3031

3132
[dependencies]
3233
thiserror = "^1.0"

rust/tvm-sys/build.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,19 @@ fn main() -> Result<()> {
8484
println!("cargo:rerun-if-changed={}", build_path.display());
8585
println!("cargo:rerun-if-changed={}/include", source_path.display());
8686

87-
match &std::env::var("CARGO_CFG_TARGET_ARCH").unwrap()[..] {
87+
let library_name = if cfg!(feature = "runtime-only") {
88+
"tvm_runtime"
89+
} else {
90+
"tvm"
91+
};
92+
93+
match &std::env::var("CARGO_CFG_TARGET_ARCH")
94+
.expect("CARGO_CFG_TARGET_ARCH must be set by CARGO")[..]
95+
{
8896
"wasm32" => {}
8997
_ => {
9098
if cfg!(feature = "static-linking") {
91-
println!("cargo:rustc-link-lib=static=tvm");
99+
println!("cargo:rustc-link-lib=static={}", library_name);
92100
// TODO(@jroesch): move this to tvm-build as library_path?
93101
println!(
94102
"cargo:rustc-link-search=native={}/build",
@@ -97,14 +105,14 @@ fn main() -> Result<()> {
97105
}
98106

99107
if cfg!(feature = "dynamic-linking") {
100-
println!("cargo:rustc-link-lib=dylib=tvm");
108+
println!("cargo:rustc-link-lib=dylib={}", library_name);
101109
println!(
102110
"cargo:rustc-link-search=native={}/build",
103111
build_path.display()
104112
);
105113
}
106114
}
107-
}
115+
};
108116

109117
let runtime_api = source_path.join("include/tvm/runtime/c_runtime_api.h");
110118
let backend_api = source_path.join("include/tvm/runtime/c_backend_api.h");

rust/tvm/examples/resnet/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ edition = "2018"
2525

2626
[dependencies]
2727
ndarray = "0.12"
28-
tvm = { path = "../../" }
28+
tvm-rt = { path = "../../../tvm-rt", features = ["standalone"] }
2929
image = "0.20"
3030
csv = "1.1"
3131
anyhow = "^1.0"

rust/tvm/examples/resnet/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use ::ndarray::{Array, ArrayD, Axis};
2727
use image::{FilterType, GenericImageView};
2828

2929
use anyhow::Context as _;
30-
use tvm::runtime::graph_rt::GraphRt;
31-
use tvm::*;
30+
use tvm_rt::graph_rt::GraphRt;
31+
use tvm_rt::*;
3232

3333
fn main() -> anyhow::Result<()> {
3434
let dev = Device::cpu(0);
@@ -107,7 +107,7 @@ fn main() -> anyhow::Result<()> {
107107

108108
// create a hash map of (class id, class name)
109109
let file = File::open("synset.txt").context("failed to open synset")?;
110-
let synset: Vec<String> = BufReader::new(file)
110+
let synset: Vec<std::string::String> = BufReader::new(file)
111111
.lines()
112112
.into_iter()
113113
.map(|x| x.expect("readline failed"))

rust/tvm/src/runtime/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,3 @@
1818
*/
1919

2020
pub use tvm_rt::*;
21-
22-
pub mod graph_rt;

0 commit comments

Comments
 (0)