FrozenGene commented on a change in pull request #5753:
URL: https://github.com/apache/incubator-tvm/pull/5753#discussion_r452009985



##########
File path: python/tvm/runtime/graph_runtime_factory.py
##########
@@ -0,0 +1,144 @@
+# 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.
+"""Graph runtime factory."""
+import numpy as np
+import warnings
+from tvm._ffi.base import string_types
+from tvm._ffi.registry import get_global_func
+from tvm._ffi.runtime_ctypes import TVMContext
+from tvm.contrib.graph_runtime import get_device_ctx
+from .packed_func import _set_class_module
+from tvm.rpc import base as rpc_base
+from .module import Module
+from . import ndarray
+
+
+def create(graph_runtime_kind, graph_json_str, libmod, params, 
module_name='default'):
+    """Create a runtime executor module given a graph and module.
+    Parameters
+    ----------
+    graph_runtime_kind: str
+        The kind of graph runtime. Like graphruntime, vm and so on.
+    graph_json_str : str or graph class
+        The graph to be deployed in json format output by nnvm graph.
+        The graph can only contain one operator(tvm_op) that
+        points to the name of PackedFunc in the libmod.
+    libmod : tvm.Module
+        The module of the corresponding function
+    Returns
+    -------
+    graph_module : GraphModule
+        Runtime graph module that can be used to execute the graph.
+    """
+    if not isinstance(graph_json_str, string_types):
+        try:
+            graph_json_str = graph_json_str._tvm_graph_json()
+        except AttributeError:
+            raise ValueError("Type %s is not supported" % type(graph_json_str))
+    fcreate = get_global_func("tvm.graph_runtime_factory.create")
+    args = []
+    for k, v in params.items():
+        args.append(k)
+        args.append(ndarray.array(v))
+    return GraphRuntimeFactoryModule(fcreate(graph_runtime_kind, 
graph_json_str, libmod, module_name, *args))
+
+
+class GraphRuntimeFactoryModule(Module):
+    """Graph runtime factory module.
+
+    This is a module of graph runtime factory
+
+    Parameters
+    ----------
+    module : Module
+        The interal tvm module that holds the actual graph functions.
+
+    Attributes
+    ----------
+    module : Module
+        The interal tvm module that holds the actual graph functions.
+    """
+
+    def __init__(self, module):
+        self.module = module
+        self._select_module = module["select_module"]
+        self._import_module = module["import_module"]
+        self.selected_module = None
+        self.graph_json = None
+        self.lib = None
+        self.params = {}
+        self.iter_cnt = 0
+        super(GraphRuntimeFactoryModule, self).__init__(self.module.handle)
+
+    def __del__(self):

Review comment:
       If we don't have this `__del__`, we will meet problem of `free(): 
corrupted unsorted chunks`. As `Module` class will call `__del__` to process 
the module handle. We also want to inherit `Module`, because we want to 
leverage its `__getitem__` / `__call__` and so on logic.




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


Reply via email to