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/04/12 21:04:36 UTC

[GitHub] [tvm] tkonolige commented on a change in pull request #7823: [TVMC] A simplified TVMC API for python scripting.

tkonolige commented on a change in pull request #7823:
URL: https://github.com/apache/tvm/pull/7823#discussion_r611952222



##########
File path: python/tvm/driver/tvmc/model.py
##########
@@ -0,0 +1,331 @@
+# 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.
+"""
+The TVMC Model class and helper functions.

Review comment:
       Some high level documentation would be useful here. Also example usage would be helpful. Maybe you can copy the example from the tests?

##########
File path: python/tvm/driver/tvmc/model.py
##########
@@ -0,0 +1,331 @@
+# 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.
+"""
+The TVMC Model class and helper functions.
+"""
+import os
+import json
+import tarfile
+from typing import Optional, Union, List, Dict, Callable
+import numpy as np
+
+import tvm
+from tvm import relay
+from tvm.micro import export_model_library_format
+from tvm.contrib import utils
+from tvm.relay.backend.graph_executor_factory import GraphExecutorFactoryModule
+
+from .common import TVMCException
+
+
+class TVMCModel(object):
+    """Initialize a TVMC model from a relay definition or a saved file.
+
+    Parameters
+    ----------
+    mod : tvm.IRModule, optional
+        The relay module corresponding to this model.
+    params : dict, optional
+        A parameter dictionary for the model.
+    model_path: str, optional
+        An alternative way to load a TVMCModel, the path to a previously
+        saved model.
+    name : str, optional
+        An optional name for the main library being compiled. If not specified,
+        'default' will be used.
+    """
+
+    def __init__(
+        self,
+        mod: Optional[tvm.IRModule] = None,
+        params: Optional[Dict[str, tvm.nd.NDArray]] = None,
+        model_path: Optional[str] = None,
+        name: Optional[str] = None,
+    ):
+        if (mod is None or params is None) and (model_path is None):
+            raise TVMCException(
+                "Either mod and params must be provided "
+                "or a path to a previously saved TVMCModel"
+            )
+        self.mod = mod
+        self.params = params if params else {}
+        self.name = "default" if name is None else name
+        self.dumps = None
+        self.tuning_records = None
+        self.package_path = None
+        self._tmp_dir = utils.tempdir()
+        if model_path is not None:
+            self.load(model_path)
+
+    def save(self, model_path: str):
+        """Save the TVMCModel to disk. Note that this saves the graph representation,
+        the parameters, and the tuning records if applicable. It will not save any
+        compiled artifacts.
+
+        Parameters
+        ----------
+        model_path : str
+            A path to save this TVMCModel to.
+        """
+        temp = self._tmp_dir
+        metadata = {"model_name": self.name}
+
+        # Save metadata
+        metadata_name = "metadata.json"
+        metadata_path = temp.relpath(metadata_name)
+        with open(metadata_path, "w") as metadata_file:
+            json.dump(metadata, metadata_file, indent=2, sort_keys=True)
+
+        # Save relay graph
+        relay_name = "model.json"
+        relay_path = temp.relpath(relay_name)
+        with open(relay_path, "w") as relay_file:
+            relay_file.write(tvm.ir.save_json(self.mod))
+
+        # Save params
+        params_name = "model.params"
+        params_path = temp.relpath(params_name)
+        with open(params_path, "wb") as params_file:
+            params_file.write(relay.save_param_dict(self.params))
+
+        # Save tuning logs if they are being kept as part of this model.
+        records_name = "tuning_records"
+        records_path = self.get_default_tuning_path()
+
+        # Create a tar file.
+        with tarfile.open(model_path, "w") as tar:
+            tar.add(metadata_path, metadata_name)
+            tar.add(relay_path, relay_name)
+            tar.add(params_path, params_name)
+            if self.tuning_records == records_path:
+                tar.add(records_path, records_name)
+
+    def load(self, model_path: str):
+        """Load a TVMCModel from disk.
+
+        Parameters
+        ----------
+        model_path : str
+            A path to load the TVMCModel from.
+        """
+        temp = self._tmp_dir
+        t = tarfile.open(model_path)
+        t.extractall(temp.relpath("."))
+
+        # Load metadata.
+        metadata_path = temp.relpath("metadata.json")
+        with open(metadata_path, "r") as metadata_file:
+            metadata = json.load(metadata_file)
+        self.name = metadata["model_name"]
+
+        # Load relay IR.
+        relay_path = temp.relpath("model.json")
+        with open(relay_path, "r") as relay_file:
+            self.mod = tvm.ir.load_json(relay_file.read())
+
+        # Load parameter dictionary.
+        params_path = temp.relpath("model.params")
+        with open(params_path, "rb") as params_file:
+            self.params = relay.load_param_dict(params_file.read())
+
+        records_path = self.get_default_tuning_path()
+        if os.path.exists(records_path):
+            self.tuning_records = records_path
+
+    def get_default_tuning_path(self):
+        """Returns a default path for tuning records.
+
+        Returns
+        -------
+        tuning_records : str
+            A path in the models temporary directory that tuning
+            records can be stored.
+        """
+        return self._tmp_dir.relpath("tuning_records")
+
+    def export_package(
+        self,
+        executor_factory: GraphExecutorFactoryModule,
+        package_path: Optional[str] = None,
+        cross: Optional[Union[str, Callable]] = None,
+    ):
+        """Save this TVMCModel to file.
+
+        Parameters
+        ----------
+        executor_factory : GraphExecutorFactoryModule
+            The compiled graph factory that will be used to generate the output package.
+        package_path : str, None
+            Where the model should be saved. Note that it will be packaged as a .tar file.
+            If not provided, the package will be saved to a generically named file in tmp.
+        cross : str or callable object, optional
+            Function that performs the actual compilation.
+        """
+        if package_path is None:
+            package_path = self._tmp_dir.relpath("package.tar")
+        export_model_library_format(executor_factory, package_path, cross=cross)
+        self.package_path = package_path
+
+    def summary(self):
+        """ Print the IR corressponding to this model."""
+        print(self.mod)
+
+
+class TVMCPackage(object):
+    """Load a saved TVMCPackage from disk.
+
+    Parameters
+    ----------
+    package_path : str
+        The path to the saved TVMCPackage that will be loaded.
+    """
+
+    def __init__(self, package_path: str):
+        self._tmp_dir = utils.tempdir()
+        self.import_package(package_path)
+
+    def import_package(self, package_path: str):
+        """Load a TVMCPackage from a previously exported TVMCModel.
+
+        Parameters
+        ----------
+        package_path : str
+            The path to the saved TVMCPackage.
+        """
+        temp = self._tmp_dir
+        t = tarfile.open(package_path)
+        t.extractall(temp.relpath("."))
+
+        metadata_path = "metadata.json"
+        with open(temp.relpath(metadata_path), "r") as metadata_file:
+            metadata = json.load(metadata_file)
+        self.name = metadata["model_name"]
+        self.target = metadata["target"]
+        self.runtimes = metadata["runtimes"]
+
+        parameter_path = os.path.join("parameters", f"{self.name}.params")
+        with open(temp.relpath(parameter_path), "rb") as param_file:
+            self.params = bytearray(param_file.read())
+
+        graph_path = os.path.join("runtime-config", "graph", "graph.json")
+        with open(temp.relpath(graph_path), "r") as graph_file:
+            self.graph = graph_file.read()
+
+        ir_path = "relay.txt"
+        with open(temp.relpath(ir_path), "r") as relay_file:
+            self.mod = relay_file.read()
+
+        self.lib_path = temp.relpath(f"{self.name}.so")
+
+    def summary(self):
+        print(self.mod)
+
+
+class TVMCResult(object):
+    """Create a convenience wrapper around the output of tvmc.run
+
+    Parameters
+    ----------
+    outputs : dict
+        Outputs dictionary mapping the name of the output to its numpy value.
+    times : list of str
+        The execution times measured by the time evaluator to produce outputs.
+    """
+
+    def __init__(self, outputs: Dict[str, np.ndarray], times: List[str]):
+        self.outputs = outputs
+        self.times = times
+
+    def format_times(self):
+        """Format the mean, max, min and std of the execution times.
+
+        This has the effect of producing a small table that looks like:
+
+            Execution time summary:

Review comment:
       Probably want to wrap this in a code block




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