[incubator-tvm] branch master updated: Don't add cast for TF batch norm when type isn't changing (#5731)
This is an automated email from the ASF dual-hosted git repository. srk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-tvm.git The following commit(s) were added to refs/heads/master by this push: new 2e1ef8e Don't add cast for TF batch norm when type isn't changing (#5731) 2e1ef8e is described below commit 2e1ef8e4b7e39bcd0ce68192c38800e2364e0984 Author: Trevor Morris AuthorDate: Mon Jun 8 16:43:28 2020 -0700 Don't add cast for TF batch norm when type isn't changing (#5731) --- python/tvm/relay/frontend/tensorflow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 201c6ba..50987f9 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -1227,7 +1227,7 @@ def _fused_batch_norm(): attr['data_format'] = attr['data_format'].decode("utf-8") if attr['data_format'] == 'NCHW': axis = 1 -if 'U' in attr: +if 'U' in attr and attr['U'].name != attr['T'].name: need_cast = True inputs[0] = _op.cast(inputs[0], dtype=attr['U'].name) # Check if mean and variance are empty
[incubator-tvm] 02/04: Convert external macro to procmacro
This is an automated email from the ASF dual-hosted git repository. jroesch pushed a commit to branch rust-tvm in repository https://gitbox.apache.org/repos/asf/incubator-tvm.git commit 62b6d1bac749e53d40fd55b496feec486079 Author: Jared Roesch AuthorDate: Mon Jun 8 13:42:32 2020 -0700 Convert external macro to procmacro --- rust/macros/src/external.rs| 163 + rust/macros/src/lib.rs | 1 + rust/macros/src/object.rs | 58 +++ rust/macros/src/util.rs| 11 +++ rust/tvm-rt/Cargo.toml | 1 - rust/tvm-rt/src/context.rs | 5 +- rust/tvm-rt/src/errors.rs | 2 + rust/tvm-rt/src/function.rs| 11 +-- rust/tvm-rt/src/lib.rs | 2 + rust/tvm-rt/src/module.rs | 29 rust/tvm-rt/src/object/mod.rs | 14 ++-- rust/tvm-rt/src/string.rs | 5 +- rust/tvm-rt/src/to_boxed_fn.rs | 36 - 13 files changed, 255 insertions(+), 83 deletions(-) diff --git a/rust/macros/src/external.rs b/rust/macros/src/external.rs new file mode 100644 index 000..989cc6a --- /dev/null +++ b/rust/macros/src/external.rs @@ -0,0 +1,163 @@ +/* + * 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. + */ +use std::env; +use quote::quote; +use proc_macro2::Span; +use syn::parse::{Parse, ParseStream, Result}; + +use syn::{Ident, Meta, FnArg, Generics, TraitItemMethod, Lit, NestedMeta, Type, ReturnType, Pat}; + +struct External { +tvm_name: String, +ident: Ident, +generics: Generics, +inputs: Vec, +ret_type: ReturnType, +} + +impl Parse for External { +fn parse(input: ParseStream) -> Result { +let method: TraitItemMethod = input.parse()?; +assert_eq!(method.attrs.len(), 1); +let sig = method.sig; +let tvm_name = method.attrs[0].parse_meta()?; +let tvm_name = match tvm_name { +Meta::List(meta_list) => { +let name = meta_list.path.get_ident() +.expect("name"); +assert_eq!(name.to_string(), "name".to_string()); +match meta_list.nested.first() { +Some(NestedMeta::Lit(Lit::Str(lit))) => lit.value(), +_ => panic!(), +} +} +_ => panic!() +}; +assert_eq!(method.default, None); +assert!(method.semi_token != None); +let ident = sig.ident; +let generics = sig.generics; +let inputs = sig.inputs.iter().map(|param| param.clone()).collect(); +let ret_type = sig.output; + +Ok(External { +tvm_name, +ident, +generics, +inputs, +ret_type, +}) +} +} + +struct ExternalInput { +externs: Vec, +} + +impl Parse for ExternalInput { +fn parse(input: ParseStream) -> Result { +let mut externs: Vec = Vec::new(); + +loop { +if input.is_empty() { break; } +externs.push(input.parse()?); +} + +Ok(ExternalInput { externs }) +} +} + + pub fn macro_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +let ext_input = syn::parse_macro_input!(input as ExternalInput); + +let tvm_rt_crate = if env::var("CARGO_PKG_NAME").unwrap() == "tvm-rt" { +quote!( crate ) +} else { +quote!( tvm_rt ) +}; + +let err_type = quote! { #tvm_rt_crate::Error }; + +let mut items = Vec::new(); + +for external in _input.externs { +let name = +let global_name = format!("global_{}", external.ident); +let global_name = Ident::new(_name, Span::call_site()); +let ext_name = _name; + +let ty_params: Vec = +external.generics.params.iter().map(|ty_param| +match ty_param { +syn::GenericParam::Type(param) => param.clone(), +_ => panic!() +}).collect(); + +let args = + +let (args, tys): (Vec, Vec) = +args.iter().map(|arg| { +match arg { +FnArg::Typed(pat_type) => { +match &*pat_type.pat { +
[incubator-tvm] branch rust-tvm updated (a44a379 -> caa6904)
This is an automated email from the ASF dual-hosted git repository. jroesch pushed a change to branch rust-tvm in repository https://gitbox.apache.org/repos/asf/incubator-tvm.git. from a44a379 Refactor anyhow out of the rt layer new fc6fac2 Reworking errors and proc macros new 62b6d1ba Convert external macro to procmacro new 0c55c39 Finish removing anyhow and work with new external! macro new caa6904 All tests pass The 4 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "add" were already present in the repository and have only been added to this reference. Summary of changes: rust/macros/src/external.rs | 163 +++ rust/macros/src/lib.rs | 8 ++ rust/macros/src/object.rs| 58 ++--- rust/macros/src/util.rs | 11 +++ rust/tvm-rt/Cargo.toml | 4 +- rust/tvm-rt/src/context.rs | 17 ++-- rust/tvm-rt/src/errors.rs| 21 +++-- rust/tvm-rt/src/function.rs | 64 +- rust/tvm-rt/src/lib.rs | 8 +- rust/tvm-rt/src/module.rs| 31 --- rust/tvm-rt/src/ndarray.rs | 25 +++--- rust/tvm-rt/src/object/mod.rs| 14 +-- rust/tvm-rt/src/object/object_ptr.rs | 2 +- rust/tvm-rt/src/string.rs| 5 +- rust/tvm-rt/src/to_boxed_fn.rs | 21 ++--- rust/tvm-rt/src/to_function.rs | 30 +++ rust/tvm-rt/src/value.rs | 6 +- rust/tvm/src/ir/array.rs | 5 +- rust/tvm/src/lib.rs | 9 +- 19 files changed, 334 insertions(+), 168 deletions(-) create mode 100644 rust/macros/src/external.rs create mode 100644 rust/macros/src/util.rs
[incubator-tvm] 01/04: Reworking errors and proc macros
This is an automated email from the ASF dual-hosted git repository. jroesch pushed a commit to branch rust-tvm in repository https://gitbox.apache.org/repos/asf/incubator-tvm.git commit fc6fac254d02f91dae146c81c618cd17f8bf9d3c Author: Jared Roesch AuthorDate: Sat Jun 6 21:54:58 2020 -0700 Reworking errors and proc macros --- rust/macros/src/lib.rs | 7 + rust/tvm-rt/src/errors.rs| 5 +++- rust/tvm-rt/src/function.rs | 53 ++-- rust/tvm-rt/src/ndarray.rs | 23 ++-- rust/tvm-rt/src/object/mod.rs| 2 +- rust/tvm-rt/src/object/object_ptr.rs | 2 +- rust/tvm-rt/src/to_function.rs | 2 +- rust/tvm-rt/src/value.rs | 6 +--- rust/tvm/src/ir/array.rs | 5 ++-- rust/tvm/src/lib.rs | 9 ++ 10 files changed, 48 insertions(+), 66 deletions(-) diff --git a/rust/macros/src/lib.rs b/rust/macros/src/lib.rs index e9ddc25..d0ac1ca 100644 --- a/rust/macros/src/lib.rs +++ b/rust/macros/src/lib.rs @@ -18,6 +18,8 @@ */ use proc_macro::TokenStream; + +mod external; mod import_module; mod object; @@ -31,3 +33,8 @@ pub fn macro_impl(input: TokenStream) -> TokenStream { // let input = proc_macro2::TokenStream::from(input); TokenStream::from(object::macro_impl(input)) } + +#[proc_macro] +pub fn external(input: TokenStream) -> TokenStream { +external::macro_impl(input) +} diff --git a/rust/tvm-rt/src/errors.rs b/rust/tvm-rt/src/errors.rs index 41e873f..f081258 100644 --- a/rust/tvm-rt/src/errors.rs +++ b/rust/tvm-rt/src/errors.rs @@ -48,7 +48,10 @@ pub enum NDArrayError { #[error("a shape error occurred in the Rust ndarray library")] ShapeError(#[from] ndarray::ShapeError), #[error("Expected type `{expected}` but found `{actual}`")] -DataTypeMismatch { expected: DataType, actual: DataType } +DataTypeMismatch { +expected: DataType, +actual: DataType, +}, } #[derive(Debug, Error)] diff --git a/rust/tvm-rt/src/function.rs b/rust/tvm-rt/src/function.rs index 17f5f6e..b0122ff 100644 --- a/rust/tvm-rt/src/function.rs +++ b/rust/tvm-rt/src/function.rs @@ -25,6 +25,9 @@ //! //! See the tests and examples repository for more examples. +use anyhow::Result; +use lazy_static::lazy_static; +use std::convert::TryFrom; use std::{ collections::BTreeMap, ffi::{CStr, CString}, @@ -33,9 +36,6 @@ use std::{ ptr, slice, str, sync::Mutex, }; -use std::convert::{TryFrom}; -use anyhow::Result; -use lazy_static::lazy_static; pub use tvm_sys::{ffi, ArgValue, RetValue}; @@ -194,7 +194,10 @@ impl TryFrom for Function { fn try_from(ret_value: RetValue) -> Result { match ret_value { RetValue::FuncHandle(handle) => Ok(Function::new(handle)), -_ => Err(Error::downcast(format!("{:?}", ret_value), "FunctionHandle")) +_ => Err(Error::downcast( +format!("{:?}", ret_value), +"FunctionHandle", +)), } } } @@ -211,7 +214,10 @@ impl<'a> TryFrom> for Function { fn try_from(arg_value: ArgValue<'a>) -> Result { match arg_value { ArgValue::FuncHandle(handle) => Ok(Function::new(handle)), -_ => Err(Error::downcast(format!("{:?}", arg_value), "FunctionHandle")), +_ => Err(Error::downcast( +format!("{:?}", arg_value), +"FunctionHandle", +)), } } } @@ -222,7 +228,10 @@ impl<'a> TryFrom<<'a>> for Function { fn try_from(arg_value: <'a>) -> Result { match arg_value { ArgValue::FuncHandle(handle) => Ok(Function::new(*handle)), -_ => Err(Error::downcast(format!("{:?}", arg_value), "FunctionHandle")), +_ => Err(Error::downcast( +format!("{:?}", arg_value), +"FunctionHandle", +)), } } } @@ -286,38 +295,6 @@ where Ok(()) } -#[macro_export] -macro_rules! external_func_impl { -($name:ident , $($ty_param:tt)* , ( $($arg:ident : $ty:ty),* ), $ret_type:ty, $ext_name:literal) => { -::paste::item! { -#[allow(non_upper_case_globals)] -static []: ::once_cell::sync::Lazy<&'static $crate::Function> = -::once_cell::sync::Lazy::new(|| { -$crate::Function::get($ext_name) -.expect(concat!("unable to load external function", stringify!($ext_name), "from TVM registry.")) -}); -} - -pub fn $name<$($ty_param),*>($($arg : $ty),*) -> anyhow::Result<$ret_type> w,* { -let func_ref: &$crate::Function = ::paste::expr! { &*[] }; -let func_ref: Box anyhow::Result<$ret_type>> = func_ref.to_boxed_fn(); -let res: $ret_type = func_ref($($arg),*)?; -Ok(res) -} -} -} - - -#[macro_export] -macro_rules! external_func { -(fn $name:ident (
[incubator-tvm] 03/04: Finish removing anyhow and work with new external! macro
This is an automated email from the ASF dual-hosted git repository. jroesch pushed a commit to branch rust-tvm in repository https://gitbox.apache.org/repos/asf/incubator-tvm.git commit 0c55c39477979c75f2bf3e2e9974d90fde74fa26 Author: Jared Roesch AuthorDate: Mon Jun 8 13:56:28 2020 -0700 Finish removing anyhow and work with new external! macro --- rust/tvm-rt/src/context.rs | 12 rust/tvm-rt/src/errors.rs | 14 -- rust/tvm-rt/src/function.rs| 12 ++-- rust/tvm-rt/src/module.rs | 10 +- rust/tvm-rt/src/ndarray.rs | 2 +- rust/tvm-rt/src/to_boxed_fn.rs | 29 - rust/tvm-rt/src/to_function.rs | 30 +++--- 7 files changed, 59 insertions(+), 50 deletions(-) diff --git a/rust/tvm-rt/src/context.rs b/rust/tvm-rt/src/context.rs index 0c01d91..b1bdab5 100644 --- a/rust/tvm-rt/src/context.rs +++ b/rust/tvm-rt/src/context.rs @@ -1,13 +1,17 @@ -pub use tvm_sys::context::*; -use tvm_sys::ffi; use std::os::raw::c_void; use std::ptr; +use crate::errors::Error; + +use tvm_sys::ffi; + +pub use tvm_sys::context::*; + trait ContextExt { /// Checks whether the context exists or not. fn exist() -> bool; -fn sync() -> anyhow::Result<()>; +fn sync() -> Result<(), Error>; fn max_threads_per_block() -> isize; fn warp_size() -> isize; fn max_shared_memory_per_block() -> isize; @@ -44,7 +48,7 @@ impl ContextExt for Context { } /// Synchronize the context stream. -fn sync() -> anyhow::Result<()> { +fn sync() -> Result<(), Error> { check_call!(ffi::TVMSynchronize( self.device_type as i32, self.device_id as i32, diff --git a/rust/tvm-rt/src/errors.rs b/rust/tvm-rt/src/errors.rs index 414484d..197c875 100644 --- a/rust/tvm-rt/src/errors.rs +++ b/rust/tvm-rt/src/errors.rs @@ -21,12 +21,6 @@ use crate::DataType; use thiserror::Error; #[derive(Debug, Error)] -#[error("Handle `{name}` is null.")] -pub struct NullHandleError { -pub name: String, -} - -#[derive(Debug, Error)] #[error("Function was not set in `function::Builder`")] pub struct FunctionNotFoundError; @@ -62,6 +56,14 @@ pub enum Error { Null, #[error("failed to load module due to invalid path {0}")] ModuleLoadPath(String), +#[error("failed to convert String into CString due to embedded nul character")] +ToCString(#[from] std::ffi::NulError), +#[error("failed to convert CString into String")] +FromCString(#[from] std::ffi::IntoStringError), +#[error("Handle `{0}` is null.")] +NullHandle(String), +#[error("{0}")] +NDArray(#[from] NDArrayError), } impl Error { diff --git a/rust/tvm-rt/src/function.rs b/rust/tvm-rt/src/function.rs index 4b34bc1..cca918a 100644 --- a/rust/tvm-rt/src/function.rs +++ b/rust/tvm-rt/src/function.rs @@ -138,7 +138,7 @@ impl Function { } /// Calls the function that created from `Builder`. -pub fn invoke<'a>(, arg_buf: Vec>) -> Result { +pub fn invoke<'a>(, arg_buf: Vec>) -> Result { let num_args = arg_buf.len(); let (mut values, mut type_codes): (Vec, Vec) = arg_buf.iter().map(|arg| arg.to_tvm_value()).unzip(); @@ -192,7 +192,7 @@ impl From for RetValue { impl TryFrom for Function { type Error = Error; -fn try_from(ret_value: RetValue) -> Result { +fn try_from(ret_value: RetValue) -> Result { match ret_value { RetValue::FuncHandle(handle) => Ok(Function::new(handle)), _ => Err(Error::downcast( @@ -212,7 +212,7 @@ impl<'a> From for ArgValue<'a> { impl<'a> TryFrom> for Function { type Error = Error; -fn try_from(arg_value: ArgValue<'a>) -> Result { +fn try_from(arg_value: ArgValue<'a>) -> Result { match arg_value { ArgValue::FuncHandle(handle) => Ok(Function::new(handle)), _ => Err(Error::downcast( @@ -226,7 +226,7 @@ impl<'a> TryFrom> for Function { impl<'a> TryFrom<<'a>> for Function { type Error = Error; -fn try_from(arg_value: <'a>) -> Result { +fn try_from(arg_value: <'a>) -> Result { match arg_value { ArgValue::FuncHandle(handle) => Ok(Function::new(*handle)), _ => Err(Error::downcast( @@ -264,7 +264,7 @@ impl<'a> TryFrom<<'a>> for Function { /// let ret = boxed_fn(10, 20, 30).unwrap(); /// assert_eq!(ret, 60); /// ``` -pub fn register>(f: F, name: S) -> Result<(), Error> +pub fn register>(f: F, name: S) -> Result<()> where F: ToFunction, F: Typed, @@ -275,7 +275,7 @@ where /// Register a function with explicit control over whether to override an existing registration or not. /// /// See `register` for more details on how to use the registration API. -pub fn register_override>(f: F, name: S, override_: bool) -> Result<(), Error> +pub fn register_override>(f: F, name: S, override_: bool) -> Result<()> where F: ToFunction, F: Typed, diff --git
[incubator-tvm] 04/04: All tests pass
This is an automated email from the ASF dual-hosted git repository. jroesch pushed a commit to branch rust-tvm in repository https://gitbox.apache.org/repos/asf/incubator-tvm.git commit caa6904a4d070abf1f957a51c23ba7d553e364be Author: Jared Roesch AuthorDate: Mon Jun 8 14:01:09 2020 -0700 All tests pass --- rust/tvm-rt/Cargo.toml | 3 +++ rust/tvm-rt/src/function.rs | 6 +++--- rust/tvm-rt/src/lib.rs | 6 +++--- rust/tvm-rt/src/ndarray.rs | 6 +++--- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/rust/tvm-rt/Cargo.toml b/rust/tvm-rt/Cargo.toml index b234da1..00e225f 100644 --- a/rust/tvm-rt/Cargo.toml +++ b/rust/tvm-rt/Cargo.toml @@ -39,5 +39,8 @@ paste = "0.1" mashup = "0.1" once_cell = "^1.3.1" +[dev-dependencies] +anyhow = "^1.0" + [features] blas = ["ndarray/blas"] diff --git a/rust/tvm-rt/src/function.rs b/rust/tvm-rt/src/function.rs index cca918a..ef659b7 100644 --- a/rust/tvm-rt/src/function.rs +++ b/rust/tvm-rt/src/function.rs @@ -251,8 +251,7 @@ impl<'a> TryFrom<<'a>> for Function { /// /// ``` /// # use tvm_rt::{ArgValue, RetValue}; -/// # use tvm_rt::function::{Function, register}; -/// # use anyhow::{Result}; +/// # use tvm_rt::function::{Function, Result, register}; /// /// fn sum(x: i64, y: i64, z: i64) -> i64 { /// x + y + z @@ -317,6 +316,7 @@ mod tests { #[test] fn register_and_call_closure0() { use crate::function; +use function::Result; fn constfn() -> i64 { return 10; @@ -324,7 +324,7 @@ mod tests { function::register_override(constfn, "constfn".to_owned(), true).unwrap(); let func = Function::get("constfn").unwrap(); -let func = func.to_boxed_fn:: Result>(); +let func = func.to_boxed_fn:: Result>(); let ret = func().unwrap(); assert_eq!(ret, 10); } diff --git a/rust/tvm-rt/src/lib.rs b/rust/tvm-rt/src/lib.rs index 70a8efd..e21b5b7 100644 --- a/rust/tvm-rt/src/lib.rs +++ b/rust/tvm-rt/src/lib.rs @@ -115,8 +115,8 @@ mod tests { #[test] fn set_error() { -let err = errors::EmptyArrayError; -set_last_error(()); -assert_eq!(get_last_error().trim(), errors::EmptyArrayError.to_string()); +let err = errors::NDArrayError::EmptyArray; +set_last_error(); +assert_eq!(get_last_error().trim(), errors::NDArrayError::EmptyArray.to_string()); } } diff --git a/rust/tvm-rt/src/ndarray.rs b/rust/tvm-rt/src/ndarray.rs index 9a17502..6e1dcbf 100644 --- a/rust/tvm-rt/src/ndarray.rs +++ b/rust/tvm-rt/src/ndarray.rs @@ -190,7 +190,7 @@ impl NDArray { /// assert_eq!(ndarray.to_vec::().unwrap(), data); /// ``` pub fn to_vec() -> Result, NDArrayError> { -if self.shape().is_some() { +if !self.shape().is_some() { return Err(NDArrayError::EmptyArray); } let earr = NDArray::empty( @@ -308,7 +308,7 @@ macro_rules! impl_from_ndarray_rustndarray { type Error = NDArrayError; fn try_from(nd: ) -> Result, Self::Error> { -if nd.shape().is_some() { +if !nd.shape().is_some() { return Err(NDArrayError::MissingShape); } assert_eq!(nd.dtype(), DataType::from_str($type_name)?, "Type mismatch"); @@ -323,7 +323,7 @@ macro_rules! impl_from_ndarray_rustndarray { type Error = NDArrayError; fn try_from(nd: NDArray) -> Result, Self::Error> { -if nd.shape().is_some() { +if !nd.shape().is_some() { return Err(NDArrayError::MissingShape); }; assert_eq!(nd.dtype(), DataType::from_str($type_name)?, "Type mismatch");
[GitHub] [incubator-tvm] junrushao1994 commented on issue #5744: [Test][CPPTEST] File is not tested `./tests/cpp/relay_transform_sequential.cc`
junrushao1994 commented on issue #5744: URL: https://github.com/apache/incubator-tvm/issues/5744#issuecomment-640249909 Closed via #5745 This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [incubator-tvm] junrushao1994 commented on pull request #5746: [topi] block sparse dense on cuda
junrushao1994 commented on pull request #5746: URL: https://github.com/apache/incubator-tvm/pull/5746#issuecomment-640482803 @antinucleon @Laurawly if you guys are interested This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org