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] Add first stage of updating and rewriting Rust bindings. #5526

Merged
merged 6 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions rust/.rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true

3 changes: 2 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ members = [
"frontend",
"frontend/tests/basics",
"frontend/tests/callback",
"frontend/examples/resnet"
"frontend/examples/resnet",
"tvm-sys"
]
100 changes: 50 additions & 50 deletions rust/frontend/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
//! # Example
//!
//! ```
//! # use tvm_frontend::{TVMDeviceType, TVMContext};
//! let cpu = TVMDeviceType::from("cpu");
//! # use tvm_frontend::{DeviceType, TVMContext};
//! let cpu = DeviceType::from("cpu");
//! let ctx = TVMContext::new(cpu , 0);
//! let cpu0 = TVMContext::cpu(0);
//! assert_eq!(ctx, cpu0);
Expand Down Expand Up @@ -58,23 +58,23 @@ use crate::{function, TVMArgValue};
/// ## Example
///
/// ```
/// use tvm_frontend::TVMDeviceType;
/// let cpu = TVMDeviceType::from("cpu");
/// use tvm_frontend::DeviceType;
/// let cpu = DeviceType::from("cpu");
/// println!("device is: {}", cpu);
///```

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TVMDeviceType(pub i64);
pub struct DeviceType(pub i64);
jroesch marked this conversation as resolved.
Show resolved Hide resolved

impl Default for TVMDeviceType {
impl Default for DeviceType {
/// default device is cpu.
fn default() -> Self {
TVMDeviceType(1)
DeviceType(1)
}
}

impl From<TVMDeviceType> for ffi::DLDeviceType {
fn from(device_type: TVMDeviceType) -> Self {
impl From<DeviceType> for ffi::DLDeviceType {
fn from(device_type: DeviceType) -> Self {
match device_type.0 {
1 => ffi::DLDeviceType_kDLCPU,
2 => ffi::DLDeviceType_kDLGPU,
Expand All @@ -90,63 +90,63 @@ impl From<TVMDeviceType> for ffi::DLDeviceType {
}
}

impl From<ffi::DLDeviceType> for TVMDeviceType {
impl From<ffi::DLDeviceType> for DeviceType {
fn from(device_type: ffi::DLDeviceType) -> Self {
match device_type {
ffi::DLDeviceType_kDLCPU => TVMDeviceType(1),
ffi::DLDeviceType_kDLGPU => TVMDeviceType(2),
ffi::DLDeviceType_kDLCPUPinned => TVMDeviceType(3),
ffi::DLDeviceType_kDLOpenCL => TVMDeviceType(4),
ffi::DLDeviceType_kDLVulkan => TVMDeviceType(7),
ffi::DLDeviceType_kDLMetal => TVMDeviceType(8),
ffi::DLDeviceType_kDLVPI => TVMDeviceType(9),
ffi::DLDeviceType_kDLROCM => TVMDeviceType(10),
ffi::DLDeviceType_kDLExtDev => TVMDeviceType(12),
ffi::DLDeviceType_kDLCPU => DeviceType(1),
ffi::DLDeviceType_kDLGPU => DeviceType(2),
ffi::DLDeviceType_kDLCPUPinned => DeviceType(3),
ffi::DLDeviceType_kDLOpenCL => DeviceType(4),
ffi::DLDeviceType_kDLVulkan => DeviceType(7),
ffi::DLDeviceType_kDLMetal => DeviceType(8),
ffi::DLDeviceType_kDLVPI => DeviceType(9),
ffi::DLDeviceType_kDLROCM => DeviceType(10),
ffi::DLDeviceType_kDLExtDev => DeviceType(12),
_ => panic!("device type not found!"),
}
}
}

impl Display for TVMDeviceType {
impl Display for DeviceType {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
TVMDeviceType(1) => "cpu",
TVMDeviceType(2) => "gpu",
TVMDeviceType(3) => "cpu_pinned",
TVMDeviceType(4) => "opencl",
TVMDeviceType(8) => "meta",
TVMDeviceType(9) => "vpi",
TVMDeviceType(10) => "rocm",
TVMDeviceType(_) => "rpc",
DeviceType(1) => "cpu",
DeviceType(2) => "gpu",
DeviceType(3) => "cpu_pinned",
DeviceType(4) => "opencl",
DeviceType(8) => "meta",
DeviceType(9) => "vpi",
DeviceType(10) => "rocm",
DeviceType(_) => "rpc",
}
)
}
}

impl<'a> From<&'a str> for TVMDeviceType {
impl<'a> From<&'a str> for DeviceType {
fn from(type_str: &'a str) -> Self {
match type_str {
"cpu" => TVMDeviceType(1),
"llvm" => TVMDeviceType(1),
"stackvm" => TVMDeviceType(1),
"gpu" => TVMDeviceType(2),
"cuda" => TVMDeviceType(2),
"nvptx" => TVMDeviceType(2),
"cl" => TVMDeviceType(4),
"opencl" => TVMDeviceType(4),
"metal" => TVMDeviceType(8),
"vpi" => TVMDeviceType(9),
"rocm" => TVMDeviceType(10),
"cpu" => DeviceType(1),
"llvm" => DeviceType(1),
"stackvm" => DeviceType(1),
"gpu" => DeviceType(2),
"cuda" => DeviceType(2),
"nvptx" => DeviceType(2),
"cl" => DeviceType(4),
"opencl" => DeviceType(4),
"metal" => DeviceType(8),
"vpi" => DeviceType(9),
"rocm" => DeviceType(10),
_ => panic!("{:?} not supported!", type_str),
}
}
}

impl<'a> From<&TVMDeviceType> for TVMArgValue<'a> {
fn from(dev: &TVMDeviceType) -> Self {
impl<'a> From<&DeviceType> for TVMArgValue<'a> {
fn from(dev: &DeviceType) -> Self {
Self::Int(dev.0)
}
}
Expand Down Expand Up @@ -174,14 +174,14 @@ impl<'a> From<&TVMDeviceType> for TVMArgValue<'a> {
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
pub struct TVMContext {
/// Supported device types
pub device_type: TVMDeviceType,
pub device_type: DeviceType,
/// Device id
pub device_id: i32,
}

impl TVMContext {
/// Creates context from device type and id.
pub fn new(device_type: TVMDeviceType, device_id: i32) -> Self {
pub fn new(device_type: DeviceType, device_id: i32) -> Self {
TVMContext {
device_type,
device_id,
Expand All @@ -194,7 +194,7 @@ macro_rules! impl_ctxs {
$(
impl TVMContext {
pub fn $ctx(device_id: i32) -> Self {
Self::new(TVMDeviceType($dldevt), device_id)
Self::new(DeviceType($dldevt), device_id)
}
}
)+
Expand All @@ -216,7 +216,7 @@ impl_ctxs!((cpu, 1);

impl<'a> From<&'a str> for TVMContext {
fn from(target: &str) -> Self {
TVMContext::new(TVMDeviceType::from(target), 0)
TVMContext::new(DeviceType::from(target), 0)
}
}

Expand Down Expand Up @@ -284,7 +284,7 @@ impl_device_attrs!((max_threads_per_block, 1);
impl From<ffi::DLContext> for TVMContext {
fn from(ctx: ffi::DLContext) -> Self {
TVMContext {
device_type: TVMDeviceType::from(ctx.device_type),
device_type: DeviceType::from(ctx.device_type),
device_id: ctx.device_id,
}
}
Expand Down Expand Up @@ -313,13 +313,13 @@ mod tests {
fn context() {
let ctx = TVMContext::cpu(0);
println!("ctx: {}", ctx);
let default_ctx = TVMContext::new(TVMDeviceType(1), 0);
let default_ctx = TVMContext::new(DeviceType(1), 0);
assert_eq!(ctx.clone(), default_ctx);
assert_ne!(ctx, TVMContext::gpu(0));

let str_ctx = TVMContext::new(TVMDeviceType::from("gpu"), 0);
let str_ctx = TVMContext::new(DeviceType::from("gpu"), 0);
assert_eq!(str_ctx.clone(), str_ctx);
assert_ne!(str_ctx, TVMContext::new(TVMDeviceType::from("cpu"), 0));
assert_ne!(str_ctx, TVMContext::new(DeviceType::from("cpu"), 0));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion rust/frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::{
use failure::Error;

pub use crate::{
context::{TVMContext, TVMDeviceType},
context::{TVMContext, DeviceType},
errors::*,
function::Function,
module::Module,
Expand Down
35 changes: 35 additions & 0 deletions rust/tvm-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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.

[package]
name = "tvm-sys"
version = "0.1.0"
authors = ["TVM Contributors"]
license = "Apache-2.0"
edition = "2018"

[features]
bindings = []

[dependencies]
thiserror = "^1.0"
anyhow = "^1.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we should bring in anyhow into a library crate (AFAIK it's form for libraries to pollute their API surface by exposing opinionated error types). But given that we only use it in one place, we can remove it later.

ndarray = "0.12"
enumn = "^0.1"

[build-dependencies]
bindgen = "0.51"
117 changes: 117 additions & 0 deletions rust/tvm-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.
*/

extern crate bindgen;

use std::path::PathBuf;

// extern crate cmake;

use std::env;
// use std::path::Path;
// use std::process::Command;
// use cmake::Config;

// fn main() {
// if !Path::new("tvm/.git").exists() {
jroesch marked this conversation as resolved.
Show resolved Hide resolved
// let _ = Command::new("git")
// .args(&["submodule", "update", "--recursive", "--init"])
// .status();
// }

// let dst = Config::new("tvm")
// .very_verbose(true)
// .build();

// // let dst = dst.join("build");

// let out_dir = env::var("OUT_DIR").unwrap();

// println!("{}", out_dir);
// // let _ = Command::new("mv")
// // .args(&[format!("{}/build/libtvm.dylib", dst.display()), out_dir])
// // .status();

// println!("cargo:rustc-link-search=native={}/lib", dst.display());
// // TODO(@jroesch): hack for dylib behavior
// for lib in &[/* "tvm", */ "tvm_runtime", /* "tvm_topi" */] {
// // let src = format!("{}/lib/lib{}.dylib", out_dir, lib);
// // let dst = format!("{}/../../../deps", out_dir);
// // let _ = Command::new("mv")
// // .args(&[src, dst])
// // .status();
// println!("cargo:rustc-link-lib=dylib={}", lib);
// }
// // "-Wl,-rpath,/scratch/library/"
// println!("cargo:rustc-env=TVM_HOME={}/build", dst.display());
// // panic!("");
// // cc::Build::new()
// // .cpp(true)
// // .flag("-std=c++11")
// // .flag("-Wno-ignored-qualifiers")
// // .flag("-Wno-unused-parameter")
// // .include("/Users/jroesch/Git/tvm/include")
// // .include("/Users/jroesch/Git/tvm/3rdparty/dmlc-core/include")
// // .include("/Users/jroesch/Git/tvm/3rdparty/dlpack/include")
// // .include("/Users/jroesch/Git/tvm/3rdparty/HalideIR/src")
// // .file("tvm_wrapper.cc")
// // .compile("tvm_ffi");
// // println!("cargo:rustc-link-lib=dylib=tvm");
// // println!("cargo:rustc-link-search=/Users/jroesch/Git/tvm/build");
// }

fn main() {
let tvm_home = option_env!("TVM_HOME").map(str::to_string).unwrap_or({
let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.canonicalize()
.unwrap();
crate_dir
.parent()
.unwrap()
.parent()
.unwrap()
.to_str()
.unwrap()
.to_string()
});

if cfg!(feature = "bindings") {
println!("cargo:rerun-if-env-changed=TVM_HOME");
// println!("cargo:rustc-link-lib=dylib=tvm_runtime");
// TODO: move to core
// println!("cargo:rustc-link-lib=dylib=tvm_runtime");
jroesch marked this conversation as resolved.
Show resolved Hide resolved
println!("cargo:rustc-link-lib=dylib=tvm");
println!("cargo:rustc-link-search={}/build", tvm_home);
}

// @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))
.blacklist_type("max_align_t")
.layout_tests(false)
.derive_partialeq(true)
.derive_eq(true)
.generate()
.expect("unable to generate bindings")
.write_to_file(PathBuf::from("src/c_runtime_api.rs"))
.expect("can not write the bindings!");
}
Loading