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 2021/10/12 18:24:11 UTC

[GitHub] [tvm] Mousius commented on a change in pull request #9246: [Relay] Introduce Executor and Runtime representations with associated registries

Mousius commented on a change in pull request #9246:
URL: https://github.com/apache/tvm/pull/9246#discussion_r727388364



##########
File path: include/tvm/relay/executor.h
##########
@@ -0,0 +1,276 @@
+/*
+ * 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/executor.h
+ * \brief Object representation of Executor configuration and registry
+ */
+#ifndef TVM_RELAY_EXECUTOR_H_
+#define TVM_RELAY_EXECUTOR_H_
+
+#include <dmlc/registry.h>
+#include <tvm/ir/attrs.h>
+#include <tvm/ir/expr.h>
+#include <tvm/ir/type.h>
+#include <tvm/ir/type_relation.h>
+#include <tvm/node/attr_registry_map.h>
+#include <tvm/runtime/registry.h>
+
+#include <string>
+#include <unordered_map>
+#include <utility>
+#include <vector>
+
+namespace tvm {
+
+template <typename, typename>
+class AttrRegistry;
+
+namespace relay {
+
+/*!
+ * \brief Executor information.
+ *
+ * This data structure stores the meta-data
+ * about executors which can be used to pass around information.
+ *
+ * \sa Executor
+ */
+class ExecutorNode : public Object {
+ public:
+  /*! \brief name of the Executor */
+  String name;
+  /* \brief Additional attributes storing meta-data about the Executor. */
+  DictAttrs attrs;
+
+  /*!
+   * \brief Get an 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 Executor& executor) {
+   *    auto value = executor->GetAttr<Integer>("AttrKey", 0);
+   *  }
+   *
+   * \endcode
+   */
+  template <typename TObjectRef>
+  Optional<TObjectRef> GetAttr(
+      const std::string& attr_key,
+      Optional<TObjectRef> default_value = Optional<TObjectRef>(nullptr)) const {
+    return attrs.GetAttr(attr_key, default_value);
+  }
+  // variant that uses TObjectRef to enable implicit conversion to default value.
+  template <typename TObjectRef>
+  Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
+    return GetAttr<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
+  }
+
+  void VisitAttrs(AttrVisitor* v) {
+    v->Visit("name", &name);
+    v->Visit("attrs", &attrs);
+  }
+
+  bool SEqualReduce(const ExecutorNode* other, SEqualReducer equal) const {
+    return name == other->name && equal.DefEqual(attrs, other->attrs);
+  }
+
+  void SHashReduce(SHashReducer hash_reduce) const {
+    hash_reduce(name);
+    hash_reduce(attrs);
+  }
+
+  static constexpr const char* _type_key = "Executor";
+  TVM_DECLARE_FINAL_OBJECT_INFO(ExecutorNode, Object);
+};
+
+/*!
+ * \brief Managed reference class to ExecutorNode.
+ * \sa ExecutorNode
+ */
+class Executor : public ObjectRef {
+ public:
+  /*!
+   * \brief Create a new Executor object using the registry
+   * \throws Error if name is not registered
+   * \param name The name of the executor.
+   * \param attrs Attributes for the executor.
+   * \return the new Executor object.
+   */
+  TVM_DLL static Executor Create(String name, Map<String, ObjectRef> attrs);
+
+  /*!
+   * \brief List all registered Executors
+   * \return the list of Executors
+   */
+  TVM_DLL static Array<String> ListExecutors();
+
+  /*!
+   * \brief List all options for a specific Executor
+   * \param name The name of the Executor
+   * \return Map of option name to type
+   */
+  TVM_DLL static Map<String, String> ListExecutorOptions(const String& name);
+
+  /*! \brief specify container node */
+  TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Executor, ObjectRef, ExecutorNode);
+
+ private:
+  /*!
+   * \brief Private Constructor
+   * \param name The executor name
+   * \param attrs Attributes to apply to this Executor node
+   */
+  TVM_DLL Executor(String name, DictAttrs attrs) {
+    auto n = make_object<ExecutorNode>();
+    n->name = std::move(name);
+    n->attrs = std::move(attrs);
+    data_ = std::move(n);
+  }
+};
+
+/*!
+ * \brief Helper structure to register Executors
+ * \sa TVM_REGISTER_EXECUTOR
+ */
+class ExecutorRegEntry {

Review comment:
       I started down this path but realised that if we wanted to do this it'd make sense to do it in the context of:
   * `TargetKind` ([ValueTypeInfo](https://github.com/apache/tvm/blob/main/include/tvm/target/target_kind.h#L101-L106) / [RegEntry](https://github.com/apache/tvm/blob/main/include/tvm/target/target_kind.h#L175)) 
   * `PassConfigManager` ([ValueTypeInfo](https://github.com/apache/tvm/blob/3fbce70a8ad7e032de8c402fb27e0396435c8eca/src/ir/transform.cc#L164-L167))
   * `Op` ([OpRegEntry](https://github.com/apache/tvm/blob/main/include/tvm/ir/op.h#L215))
   * `InstructionKind` ([InstructionKindRegEntry](https://github.com/apache/tvm/blob/main/include/tvm/tir/schedule/instruction.h#L234))
   
   I started doing this and ended up with what was essentially a templated mess - so I'd rather take it later and in isolation :smile_cat: 




-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org