Skip to content

Commit

Permalink
Fix rust rt link (apache#8631)
Browse files Browse the repository at this point in the history
* Fix support for linking to only libtvm_runtime

also ensures that the ResNet example uses the new support.

* Fix build.rs to rebuild if the Python script changes

Co-authored-by: Jared Roesch <roeschinc@gmail.com>
  • Loading branch information
2 people authored and mehrdadh committed Aug 11, 2021
1 parent 5390b82 commit 91737ca
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 44 deletions.
1 change: 1 addition & 0 deletions rust/tvm-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ edition = "2018"
default = ["dynamic-linking"]
dynamic-linking = ["tvm-sys/dynamic-linking"]
static-linking = ["tvm-sys/static-linking"]
standalone = ["tvm-sys/runtime-only"]
blas = ["ndarray/blas"]

[dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

use std::convert::TryInto;

use crate::runtime::Function;
use crate::{runtime::function::Result, runtime::ByteArray, Device, Module, NDArray};
use crate::Function;
use crate::{function::Result, ByteArray, Device, Module, NDArray};

/// An instance of the C++ graph executor.
///
Expand Down
63 changes: 32 additions & 31 deletions rust/tvm-rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,40 @@
//! The TVM object system enables cross-language interoperability including that of closures for all
//! supported languages including C++, and Python.

// Macro to check the return call to TVM runtime shared library.

#[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());
}
}};
}

// Define all sumodules.
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::*;
Expand All @@ -52,28 +84,6 @@ use tvm_sys::ffi;

pub use tvm_macros::external;

// Macro to check the return call to TVM runtime shared library.

#[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());
}
}};
}

/// Gets the last error message.
pub fn get_last_error() -> &'static str {
unsafe {
Expand All @@ -91,15 +101,6 @@ pub(crate) fn set_last_error<E: std::error::Error>(err: &E) {
}
}

pub mod array;
pub mod device;
pub mod errors;
pub mod function;
pub mod map;
pub mod module;
pub mod ndarray;
mod to_function;

/// Outputs the current TVM version.
pub fn version() -> &'static str {
match str::from_utf8(ffi::TVM_VERSION) {
Expand Down
1 change: 1 addition & 0 deletions rust/tvm-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ description = "Low level bindings to TVM's cross language API."
default = ["dynamic-linking"]
static-linking = []
dynamic-linking = []
runtime-only = []

[dependencies]
thiserror = "^1.0"
Expand Down
16 changes: 12 additions & 4 deletions rust/tvm-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,19 @@ fn main() -> Result<()> {
println!("cargo:rerun-if-changed={}", build_path.display());
println!("cargo:rerun-if-changed={}/include", source_path.display());

match &std::env::var("CARGO_CFG_TARGET_ARCH").unwrap()[..] {
let library_name = if cfg!(feature = "runtime-only") {
"tvm_runtime"
} else {
"tvm"
};

match &std::env::var("CARGO_CFG_TARGET_ARCH")
.expect("CARGO_CFG_TARGET_ARCH must be set by CARGO")[..]
{
"wasm32" => {}
_ => {
if cfg!(feature = "static-linking") {
println!("cargo:rustc-link-lib=static=tvm");
println!("cargo:rustc-link-lib=static={}", library_name);
// TODO(@jroesch): move this to tvm-build as library_path?
println!(
"cargo:rustc-link-search=native={}/build",
Expand All @@ -97,14 +105,14 @@ fn main() -> Result<()> {
}

if cfg!(feature = "dynamic-linking") {
println!("cargo:rustc-link-lib=dylib=tvm");
println!("cargo:rustc-link-lib=dylib={}", library_name);
println!(
"cargo:rustc-link-search=native={}/build",
build_path.display()
);
}
}
}
};

let runtime_api = source_path.join("include/tvm/runtime/c_runtime_api.h");
let backend_api = source_path.join("include/tvm/runtime/c_backend_api.h");
Expand Down
2 changes: 1 addition & 1 deletion rust/tvm/examples/resnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ edition = "2018"

[dependencies]
ndarray = "0.12"
tvm = { path = "../../" }
tvm-rt = { path = "../../../tvm-rt", features = ["standalone"] }
image = "0.20"
csv = "1.1"
anyhow = "^1.0"
Expand Down
10 changes: 9 additions & 1 deletion rust/tvm/examples/resnet/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@ use std::{io::Write, path::Path, process::Command};

fn main() -> Result<()> {
let out_dir = std::env::var("CARGO_MANIFEST_DIR")?;
let python_script = concat!(env!("CARGO_MANIFEST_DIR"), "/src/build_resnet.py");
let synset_txt = concat!(env!("CARGO_MANIFEST_DIR"), "/synset.txt");

println!("cargo:rerun-if-changed={}", python_script);
println!("cargo:rerun-if-changed={}", synset_txt);

let output = Command::new("python3")
.arg(concat!(env!("CARGO_MANIFEST_DIR"), "/src/build_resnet.py"))
.arg(python_script)
.arg(&format!("--build-dir={}", out_dir))
.output()
.with_context(|| anyhow::anyhow!("failed to run python3"))?;

if !output.status.success() {
std::io::stdout()
.write_all(&output.stderr)
.context("Failed to write error")?;
panic!("Failed to execute build script");
}

assert!(
Path::new(&format!("{}/deploy_lib.o", out_dir)).exists(),
"Could not prepare demo: {}",
Expand Down
3 changes: 3 additions & 0 deletions rust/tvm/examples/resnet/src/build_resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def download_img_labels():
f.write(synset[key])
f.write("\n")

print(synset_path)
print(synset_name)

return synset


Expand Down
6 changes: 3 additions & 3 deletions rust/tvm/examples/resnet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use ::ndarray::{Array, ArrayD, Axis};
use image::{FilterType, GenericImageView};

use anyhow::Context as _;
use tvm::runtime::graph_rt::GraphRt;
use tvm::*;
use tvm_rt::graph_rt::GraphRt;
use tvm_rt::*;

fn main() -> anyhow::Result<()> {
let dev = Device::cpu(0);
Expand Down Expand Up @@ -107,7 +107,7 @@ fn main() -> anyhow::Result<()> {

// create a hash map of (class id, class name)
let file = File::open("synset.txt").context("failed to open synset")?;
let synset: Vec<String> = BufReader::new(file)
let synset: Vec<std::string::String> = BufReader::new(file)
.lines()
.into_iter()
.map(|x| x.expect("readline failed"))
Expand Down
2 changes: 0 additions & 2 deletions rust/tvm/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@
*/

pub use tvm_rt::*;

pub mod graph_rt;

0 comments on commit 91737ca

Please sign in to comment.