Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust] Make TVM Rust bindings installable via Cargo. #7503

Merged
merged 3 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions rust/tvm-graph-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

[package]
name = "tvm-graph-rt"
version = "0.1.0"
version = "0.1.0-alpha"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come you bumped to -alpha but didn't increment any other number?

license = "Apache-2.0"
description = "A static graph executor for TVM."
repository = "https://github.com/apache/tvm"
Expand All @@ -38,8 +38,8 @@ nom = "5.0"
num_cpus = "1.10"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
tvm-sys = { version = "0.1", path = "../tvm-sys" }
tvm-macros = { version = "0.1", path = "../tvm-macros" }
tvm-sys = { version = "0.1.1-alpha", path = "../tvm-sys" }
tvm-macros = { version = "0.1.1-alpha", path = "../tvm-macros" }

[target.'cfg(not(any(target_arch = "wasm32", target_env = "sgx")))'.dependencies]
libloading = "0.5"
20 changes: 20 additions & 0 deletions rust/tvm-graph-rt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
<!--- or more contributor license agreements. See the NOTICE file -->
<!--- distributed with this work for additional information -->
<!--- regarding copyright ownership. The ASF licenses this file -->
<!--- to you under the Apache License, Version 2.0 (the -->
<!--- "License"); you may not use this file except in compliance -->
<!--- with the License. You may obtain a copy of the License at -->

<!--- http://www.apache.org/licenses/LICENSE-2.0 -->

<!--- Unless required by applicable law or agreed to in writing, -->
<!--- software distributed under the License is distributed on an -->
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
<!--- KIND, either express or implied. See the License for the -->
<!--- specific language governing permissions and limitations -->
<!--- under the License. -->

# tvm-graph-rt

An implementation of TVM's graph runtime in Rust. See `tvm` crate for more documentation.
2 changes: 1 addition & 1 deletion rust/tvm-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

[package]
name = "tvm-macros"
version = "0.1.1"
version = "0.1.1-alpha"
license = "Apache-2.0"
description = "Procedural macros of the TVM crate."
repository = "https://github.com/apache/tvm"
Expand Down
20 changes: 20 additions & 0 deletions rust/tvm-macros/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
<!--- or more contributor license agreements. See the NOTICE file -->
<!--- distributed with this work for additional information -->
<!--- regarding copyright ownership. The ASF licenses this file -->
<!--- to you under the Apache License, Version 2.0 (the -->
<!--- "License"); you may not use this file except in compliance -->
<!--- with the License. You may obtain a copy of the License at -->

<!--- http://www.apache.org/licenses/LICENSE-2.0 -->

<!--- Unless required by applicable law or agreed to in writing, -->
<!--- software distributed under the License is distributed on an -->
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
<!--- KIND, either express or implied. See the License for the -->
<!--- specific language governing permissions and limitations -->
<!--- under the License. -->

# tvm-macros

The procedural macro implementations for TVM crates, see `tvm` crate for more documentation.
10 changes: 5 additions & 5 deletions rust/tvm-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

[package]
name = "tvm-rt"
version = "0.1.0"
version = "0.1.0-alpha"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question

license = "Apache-2.0"
description = "Rust bindings for the TVM runtime API."
repository = "https://github.com/apache/tvm"
Expand All @@ -30,22 +30,22 @@ edition = "2018"

[features]
default = ["dynamic-linking"]
dynamic-linking = ["tvm-sys/bindings"]
static-linking = []
dynamic-linking = ["tvm-sys/dynamic-linking"]
static-linking = ["tvm-sys/static-linking"]
blas = ["ndarray/blas"]

[dependencies]
thiserror = "^1.0"
ndarray = "0.12"
num-traits = "0.2"
tvm-macros = { version = "0.1", path = "../tvm-macros" }
tvm-macros = { version = "0.1.1-alpha", path = "../tvm-macros" }
paste = "0.1"
mashup = "0.1"
once_cell = "^1.3.1"
memoffset = "0.5.6"

[dependencies.tvm-sys]
version = "0.1"
version = "0.1.1-alpha"
default-features = false
path = "../tvm-sys/"

Expand Down
4 changes: 2 additions & 2 deletions rust/tvm-rt/src/ndarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl NDArray {
check_call!(ffi::TVMArrayCopyFromBytes(
self.as_raw_dltensor(),
data.as_ptr() as *mut _,
data.len() * mem::size_of::<T>()
(data.len() * mem::size_of::<T>()) as _,
));
}

Expand All @@ -296,7 +296,7 @@ impl NDArray {
check_call!(ffi::TVMArrayCopyToBytes(
self.as_raw_dltensor(),
data.as_ptr() as *mut _,
self.size(),
self.size() as _,
));
}

Expand Down
11 changes: 7 additions & 4 deletions rust/tvm-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

[package]
name = "tvm-sys"
version = "0.1.0"
version = "0.1.1-alpha"
authors = ["TVM Contributors"]
license = "Apache-2.0"
edition = "2018"
description = "Low level bindings to TVM's cross language API."

[features]
default = []
bindings = []
default = ["dynamic-linking"]
static-linking = []
dynamic-linking = []

[dependencies]
thiserror = "^1.0"
Expand All @@ -33,5 +35,6 @@ ndarray = "0.12"
enumn = "^0.1"

[build-dependencies]
bindgen = { version="0.51", default-features=false }
bindgen = { version="0.57", default-features = false, features = ["runtime"] }
anyhow = "^1.0"
tvm-build = "0.1"
28 changes: 28 additions & 0 deletions rust/tvm-sys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
<!--- or more contributor license agreements. See the NOTICE file -->
<!--- distributed with this work for additional information -->
<!--- regarding copyright ownership. The ASF licenses this file -->
<!--- to you under the Apache License, Version 2.0 (the -->
<!--- "License"); you may not use this file except in compliance -->
<!--- with the License. You may obtain a copy of the License at -->

<!--- http://www.apache.org/licenses/LICENSE-2.0 -->

<!--- Unless required by applicable law or agreed to in writing, -->
<!--- software distributed under the License is distributed on an -->
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
<!--- KIND, either express or implied. See the License for the -->
<!--- specific language governing permissions and limitations -->
<!--- under the License. -->

# tvm-sys

The low level bindings to TVM's C APIs for interacting with the runtime,
the cross-language object system, and packed function API.

These will generate bindings to TVM, if you set `TVM_HOME` variable before
building it will instruct the bindings to use your source tree, if not the
crate will use `tvm-build` in order to build a sandboxed version of the library.

This feature is intended to simplify the installation for brand new TVM users
by trying to automate the build process as much as possible.
136 changes: 92 additions & 44 deletions rust/tvm-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,113 @@

extern crate bindgen;

use std::env;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use tvm_build::BuildConfig;

/// The necessary information for detecting a TVM installation.
struct TVMInstall {
source_path: PathBuf,
build_path: PathBuf,
}

/// Find the TVM install using the provided path.
fn find_using_tvm_path<P: AsRef<Path>>(tvm_path: P) -> Result<TVMInstall> {
Ok(TVMInstall {
source_path: tvm_path.as_ref().into(),
build_path: tvm_path.as_ref().into(),
})
}

#[allow(unused)]
fn if_unset<K: AsRef<std::ffi::OsStr>, V: AsRef<std::ffi::OsStr>>(k: K, v: V) -> Result<()> {
match std::env::var(k.as_ref()) {
Ok(other) if other != "" => {
println!(
"cargo:warning=Using existing environment variable setting {:?}={:?}",
k.as_ref(),
v.as_ref()
);
}
_ => std::env::set_var(k, v),
}

Ok(())
}

/// Find a TVM installation using TVM build by either first installing or detecting.
fn find_using_tvm_build() -> Result<TVMInstall> {
let mut build_config = BuildConfig::default();
build_config.repository = Some("https://github.com/apache/tvm".to_string());
build_config.branch = Some(option_env!("TVM_BRANCH").unwrap_or("main").into());
let build_result = tvm_build::build(build_config)?;
let source_path = build_result.revision.source_path();
let build_path = build_result.revision.build_path();
Ok(TVMInstall {
source_path,
build_path,
})
}

fn main() -> Result<()> {
let tvm_home = option_env!("TVM_HOME")
.map::<Result<String>, _>(|s: &str| Ok(str::to_string(s)))
.unwrap_or_else(|| {
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.canonicalize()
.with_context(|| {
format!(
"failed to cannonicalize() CARGO_MANIFEST_DIR={}",
env!("CARGO_MANIFEST_DIR")
)
})?;

Ok(crate_dir
.parent()
.with_context(|| {
format!(
"failed to find parent of CARGO_MANIFEST_DIR={}",
env!("CARGO_MANIFEST_DIR")
)
})?
.parent()
.with_context(|| {
format!(
"failed to find the parent of the parent of CARGO MANIFEST_DIR={}",
env!("CARGO_MANIFEST_DIR")
)
})?
.to_str()
.context("failed to convert to strings")?
.to_string())
})?;

if cfg!(feature = "bindings") {
println!("cargo:rerun-if-env-changed=TVM_HOME");
let TVMInstall {
source_path,
build_path,
} = match option_env!("TVM_HOME") {
Some(tvm_path) if tvm_path != "" => find_using_tvm_path(tvm_path),
_ => find_using_tvm_build(),
}?;

// If the TVM_HOME environment variable changed, the LLVM_CONFIG_PATH environment variable
// changed, the build directory or headers have changed we need to rebuild the Rust bindings.
println!("cargo:rerun-if-env-changed=TVM_HOME");
println!("cargo:rerun-if-env-changed=LLVM_CONFIG_PATH");
println!("cargo:rerun-if-changed={}", build_path.display());
println!("cargo:rerun-if-changed={}/include", source_path.display());

if cfg!(feature = "static-linking") {
println!("cargo:rustc-link-lib=static=tvm");
// TODO(@jroesch): move this to tvm-build as library_path?
println!(
"cargo:rustc-link-search=native={}/build",
build_path.display()
);
}

if cfg!(feature = "dynamic-linking") {
println!("cargo:rustc-link-lib=dylib=tvm");
println!("cargo:rustc-link-search=native={}/build", tvm_home);
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");
let source_path = source_path.display().to_string();
let dlpack_include = format!("-I{}/3rdparty/dlpack/include/", source_path);
let tvm_include = format!("-I{}/include/", source_path);

let out_file = PathBuf::from(std::env::var("OUT_DIR")?).join("c_runtime_api.rs");

// @see rust-bindgen#550 for `blacklist_type`
bindgen::Builder::default()
.header(format!("{}/include/tvm/runtime/c_runtime_api.h", tvm_home))
.header(format!("{}/include/tvm/runtime/c_backend_api.h", tvm_home))
.clang_arg(format!("-I{}/3rdparty/dlpack/include/", tvm_home))
.clang_arg(format!("-I{}/include/", tvm_home))
.header(runtime_api.display().to_string())
.header(backend_api.display().to_string())
.clang_arg(dlpack_include)
.clang_arg(tvm_include)
.blacklist_type("max_align_t")
.layout_tests(false)
.derive_partialeq(true)
.derive_eq(true)
.derive_default(true)
.generate()
.map_err(|()| anyhow::anyhow!("failed to generate bindings"))?
.write_to_file(PathBuf::from("src/c_runtime_api.rs"))
.context("failed to write bindings")?;
.map_err(|()| {
anyhow::anyhow!("bindgen failed to generate the Rust bindings for the C API")
})?
.write_to_file(out_file)
.context("failed to write the generated Rust binding to disk")?;

Ok(())
}
6 changes: 3 additions & 3 deletions rust/tvm-sys/src/byte_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ pub struct ByteArray {
impl ByteArray {
/// Gets the underlying byte-array
pub fn data(&self) -> &'static [u8] {
unsafe { std::slice::from_raw_parts(self.array.data as *const u8, self.array.size) }
unsafe { std::slice::from_raw_parts(self.array.data as *const u8, self.array.size as _) }
}

/// Gets the length of the underlying byte-array
pub fn len(&self) -> usize {
self.array.size
self.array.size as _
}

/// Converts the underlying byte-array to `Vec<u8>`
Expand All @@ -66,7 +66,7 @@ impl<T: AsRef<[u8]>> From<T> for ByteArray {
ByteArray {
array: TVMByteArray {
data: arg.as_ptr() as *const c_char,
size: arg.len(),
size: arg.len() as _,
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/tvm-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod ffi {

use std::os::raw::{c_char, c_int, c_void};

include!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/c_runtime_api.rs"));
include!(concat!(env!("OUT_DIR"), "/c_runtime_api.rs"));

pub type BackendPackedCFunc = extern "C" fn(
args: *const TVMValue,
Expand Down
6 changes: 3 additions & 3 deletions rust/tvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

[package]
name = "tvm"
version = "0.1.0"
version = "0.1.1-alpha"
license = "Apache-2.0"
description = "Rust frontend support for TVM"
repository = "https://github.com/apache/tvm"
Expand All @@ -36,7 +36,7 @@ blas = ["ndarray/blas"]
python = ["pyo3"]

[dependencies.tvm-rt]
version = "0.1"
version = "0.1.0-alpha"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question

default-features = false
path = "../tvm-rt/"

Expand All @@ -46,7 +46,7 @@ anyhow = "^1.0"
lazy_static = "1.1"
ndarray = "0.12"
num-traits = "0.2"
tvm-macros = { version = "*", path = "../tvm-macros/" }
tvm-macros = { version = "0.1.1-alpha", path = "../tvm-macros/" }
paste = "0.1"
mashup = "0.1"
once_cell = "^1.3.1"
Expand Down
Loading