You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/03/11 21:36:23 UTC

[GitHub] [incubator-tvm] tqchen opened a new pull request #5045: [REFACTOR] Streamline Function Attr interface.

tqchen opened a new pull request #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045
 
 
   There has been quite a few recent changes that depends heavily on
   the function attr interface. This PR streamlines that interface by introducing
   two APIs that covers most of the usages.
   
   - GetAttr which gets a typed object for a given key
     - HasNonzeroAttr is a quick helper that calls GetAttr to quickly check an attribute
   - WithAttr that creates a new function object with the given attr
     - The API comes with copy on write optimization to avoid multiple copies
     - We deliberately pick the prefix With(instead of Set) to indicate this
       function does not mutate the original input.
   
   On the python side:
   - We allow read access via func.attrs (which is a DictAttr)
   - func.with_attrs to create a new instance with updated attrs.
   
   We also get rid of the small wrapper functions and make sure the API centered around
   the GetAttr and HasNonzeroAttr interface.
   
   This PR also changes the function construction to follow the new convention.
   
   cc @jroesch @zhiics @mbaret 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen commented on issue #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
tqchen commented on issue #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#issuecomment-597891230
 
 
   NOTE: I have not removed the UseDefaultCompiler member function, however, we should remove that in a followup PR by simply setting the default compiler to None and we could use `GetAttr<StringImm>(attr::kCompiler).defined()` to check whether the customization is defined.
   
   StringImm should be changed to String once #4628  is merged
   
   cc @comaniac @zhiics 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#discussion_r391342446
 
 

 ##########
 File path: include/tvm/ir/function.h
 ##########
 @@ -0,0 +1,119 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file tvm/ir/expr.h
+ * \brief Base expr nodes in TVM.
+ */
+#ifndef TVM_IR_FUNCTION_H_
+#define TVM_IR_FUNCTION_H_
+
+#include <tvm/ir/expr.h>
+#include <tvm/ir/attrs.h>
+#include <type_traits>
+#include <string>
+
+
+namespace tvm {
+
+/*!
+ * \brief Base node of all functions.
+ *
+ * We support several variants of functions throughout the stack.
+ * All of the functions shares the same type system(via checked_type)
+ * to support cross variant calls.
+ *
+ * \sa BaseFunc
+ */
+class BaseFuncNode : public RelayExprNode {
+ public:
+  /*! \brief Additional attributes storing the meta-data */
+  DictAttrs attrs;
+
+  /*!
+   * \brief Get a function attribute.
+   *
+   * \param attr_key The attribute key.
+   * \param default_value The default value if the key does not exist, defaults to nullptr.
+   *
+   * \return The result
+   *
+   * \tparam TOBjectRef the expected object type.
+   * \throw Error if the key exists but the value does not match TObjectRef
+   *
+   * \code
+   *
+   *  void GetAttrExample(const BaseFunc& f) {
+   *    Integer value = f->GetAttr<Integer>("AttrKey", 0);
+   *  }
+   *
+   * \endcode
+   */
+  template<typename TObjectRef>
+  TObjectRef GetAttr(const std::string& attr_key,
+                     TObjectRef default_value = NullValue<TObjectRef>()) const {
+    static_assert(std::is_base_of<ObjectRef, TObjectRef>::value,
+                  "Can only call GetAttr with ObjectRef types.");
+    if (!attrs.defined()) return default_value;
+    auto it = attrs->dict.find(attr_key);
+    if (it != attrs->dict.end()) {
+      return Downcast<TObjectRef>((*it).second);
+    } else {
+      return default_value;
+    }
+  }
+
+  /*!
+   * \brief Check whether the function has an non-zero integer attr.
+   *
+   * This function can be used to check whether an optional
+   * attribute mark(e.g. inline) exists.
+   *
+   * \param attr_key The key to the attribute.
+   * \return The check result.
+   *
+   * \code
+   *
+   *  void HasNonzeroAttrExample(const BaseFunc& f) {
+   *    if (f->HasNonzeroAttr(attr::Inline)) {
 
 Review comment:
   kInline

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] siju-samuel commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
siju-samuel commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#discussion_r391370555
 
 

 ##########
 File path: src/ir/function.cc
 ##########
 @@ -0,0 +1,33 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file src/tvm/ir/function.cc
 
 Review comment:
   src/ir/function.cc

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on issue #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
zhiics commented on issue #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#issuecomment-598254848
 
 
   Thanks @tqchen, @ZihengJiang, @siju-samuel 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] siju-samuel commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
siju-samuel commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#discussion_r391370627
 
 

 ##########
 File path: src/relay/ir/function.cc
 ##########
 @@ -0,0 +1,102 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file src/tvm/relay/ir/function.cc
 
 Review comment:
   same as above

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] siju-samuel commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
siju-samuel commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#discussion_r391370458
 
 

 ##########
 File path: include/tvm/ir/function.h
 ##########
 @@ -0,0 +1,119 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file tvm/ir/expr.h
+ * \brief Base expr nodes in TVM.
 
 Review comment:
   Update file & brief here

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#discussion_r391287229
 
 

 ##########
 File path: include/tvm/ir/attrs.h
 ##########
 @@ -327,6 +309,23 @@ class DictAttrsNode : public BaseAttrsNode {
   TVM_DECLARE_FINAL_OBJECT_INFO(DictAttrsNode, BaseAttrsNode);
 };
 
+/*!
+ * \brief Managed reference to DictAttrsNode
+ * \sa DictAttrsNode.
+ */
+class DictAttrs : public Attrs {
+ public:
+  /*!
+   * \brief Consruct a Attrs backed by DictAttrsNode.
 
 Review comment:
   s/Consruct/Construct

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] tqchen commented on issue #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
tqchen commented on issue #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#issuecomment-597965901
 
 
   thanks @zhiics please take another look

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#discussion_r391287476
 
 

 ##########
 File path: include/tvm/ir/function.h
 ##########
 @@ -0,0 +1,119 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file tvm/ir/expr.h
+ * \brief Base expr nodes in TVM.
+ */
+#ifndef TVM_IR_FUNCTION_H_
+#define TVM_IR_FUNCTION_H_
+
+#include <tvm/ir/expr.h>
+#include <tvm/ir/attrs.h>
+#include <type_traits>
+#include <string>
+
+
+namespace tvm {
+
+/*!
+ * \brief Base node of all functions.
+ *
+ * We support several variants of functions throughout the stack.
+ * All of the functions shares the same type system(via checked_type)
 
 Review comment:
   s/shares/share

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045#discussion_r391342663
 
 

 ##########
 File path: include/tvm/relay/function.h
 ##########
 @@ -0,0 +1,171 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file tvm/relay/function.h
+ * \brief Relay Function.
+ */
+#ifndef TVM_RELAY_FUNCTION_H_
+#define TVM_RELAY_FUNCTION_H_
+
+#include <tvm/ir/function.h>
+#include <tvm/relay/expr.h>
+#include <string>
+
+
+namespace tvm {
+namespace relay {
+
+/*!
+ * \brief Relay Function container
+ * \sa Function
+ */
+class FunctionNode : public BaseFuncNode {
+ public:
+  /*! \brief Function parameters */
+  tvm::Array<Var> params;
+  /*!
+   * \brief
+   * The expression which represents the computation of the function,
+   * the expression may reference the parameters, and the type of it
+   * or sub-expressions may reference the type variables.
+   */
+  Expr body;
+  /*! \brief User annotated return type of the function. */
+  Type ret_type;
+  /*!
+   * \brief Type parameters of the function.
+   *  Enables the function to vary its type based on these.
+   *  This corresponds to template paramaters in c++'s terminology.
+   *
+   * \note This can be usually empty for non-polymorphic functions.
+   */
+  tvm::Array<TypeVar> type_params;
+
+  void VisitAttrs(tvm::AttrVisitor* v) {
+    v->Visit("params", &params);
+    v->Visit("body", &body);
+    v->Visit("ret_type", &ret_type);
+    v->Visit("type_params", &type_params);
+    v->Visit("attrs", &attrs);
+    v->Visit("span", &span);
+    v->Visit("_checked_type_", &checked_type_);
+  }
+
+  /*!
+   * \brief Return the derived function annotation of this expression.
+   *
+   * \return The function type annotation.
+   * \note The function type annotation can contain IncompleteType.
+   */
+  TVM_DLL FuncType func_type_annotation() const;
+
+  /*!
+   * \brief Check whether the function should use the TVM default compiler to build, or
+   * use other compilers.
+   *
+   * \return Whether the function will be compiled using the default compiler
+   * (e.g. those are used in the TVM stack).
+   */
+  bool UseDefaultCompiler() const;
+
+  static constexpr const char* _type_key = "relay.Function";
+  TVM_DECLARE_FINAL_OBJECT_INFO(FunctionNode, BaseFuncNode);
+};
+
+
+/*!
+ * \brief Managed reference to FunctionNode.
+ * \sa FunctionNode
+ */
+class Function : public BaseFunc {
+ public:
+  /*!
+   * \brief Constructor
+   * \param params The parameters of the function.
+   * \param body The body of the function.
+   * \param ret_type The return type of the function.
+   * \param ty_params The type parameters.
+   * \param attrs Additional function attributes.
+   */
+  TVM_DLL Function(tvm::Array<Var> params,
+                   Expr body,
+                   Type ret_type,
+                   tvm::Array<TypeVar> ty_params,
+                   tvm::DictAttrs attrs = NullValue<DictAttrs>());
+
+  TVM_DEFINE_OBJECT_REF_METHODS(Function, BaseFunc, FunctionNode);
+  TVM_DEFINE_OBJECT_REF_COW_METHOD(FunctionNode);
+};
+
+/*!
+ * \brief Create a new function that copies func, but overrides
+ *        the attribute value key with the value.
+ *
+ * \param func The input function.
+ * \param attr_key The attribute key.
+ * \param attr_value The value attribute value.
+ *
+ * \returns The new function with updated attributes.
+ *
+ * \note This function performs copy on write optimization for func.
+ *       If we move an uniquely referenced func into WithAttr,
 
 Review comment:
   s/an/a

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-tvm] zhiics merged pull request #5045: [REFACTOR] Streamline Function Attr interface.

Posted by GitBox <gi...@apache.org>.
zhiics merged pull request #5045: [REFACTOR] Streamline Function Attr interface.
URL: https://github.com/apache/incubator-tvm/pull/5045
 
 
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services