jwfromm commented on a change in pull request #7823: URL: https://github.com/apache/tvm/pull/7823#discussion_r615329078
########## File path: python/tvm/driver/tvmc/model.py ########## @@ -0,0 +1,357 @@ +# 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. +""" +This file contains the definition of a set of classes that wrap the outputs +of TVMC functions to create a simpler and more intuitive API. + +There is one class for each required stage of a TVM workflow. +The TVMCModel represents the result of importing a model into TVM, it +contains the precompiled graph definition and parameters that define +what the model does. + +Compiling a TVMCModel produces a TVMCPackage, which contains the generated +artifacts that allow the model to be run on the target hardware. + +Running a TVMCPackage produces a TVMCResult, which contains the outputs of +the model and the measured runtime. + +Examples +-------- +The following code shows a full lifecycle for a model using tvmc, first the +model is imported from an exterior framework, in this case onnx, then it +is tuned to find the best schedules on CPU, then compiled into a TVMCPackage, +and finally run. + +.. code-block:: python + tvmc_model = tvmc.load("my_model.onnx") + tvmc_model = tvmc.tune(tvmc_model, target="llvm") + tvmc_package = tvmc.compile(tvmc_model, "llvm") + result = tvmc.run(tvmc_package, device="cpu") + print(result) +""" +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 model 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: + .. code-block:: + Execution time summary: + mean (ms) max (ms) min (ms) std (ms) + 0.14310 0.16161 0.12933 0.01004 + + Returns + ------- + str + A formatted string containing the statistics. + """ + + # timestamps + mean_ts = np.mean(self.times) * 1000 + std_ts = np.std(self.times) * 1000 + max_ts = np.max(self.times) * 1000 + min_ts = np.min(self.times) * 1000 + + header = "Execution time summary:\n{0:^10} {1:^10} {2:^10} {3:^10}".format( + "mean (ms)", "max (ms)", "min (ms)", "std (ms)" + ) + stats = "{0:^10.2f} {1:^10.2f} {2:^10.2f} {3:^10.2f}".format( + mean_ts, max_ts, min_ts, std_ts + ) + + return "%s\n%s\n" % (header, stats) + + def get_top_results(self, max_results: int): Review comment: I disagree, getting the top n results from a neural net is extremely common and seems reasonable to be a method of a TVMCResult. I'd argue against creating more files / code unless there are other utility functions that we think are missing here. If there are quite a few, then I agree having `result_util.py` would be fine. -- 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: [email protected]
