areusch commented on a change in pull request #7823: URL: https://github.com/apache/tvm/pull/7823#discussion_r616266979
########## 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() Review comment: imo we shouldn't even have it as a class member, the main reason we do is because `load()` doesn't precisely undo `save()`--it leaves some files on disk in `self._temp_dir`. I think it's weird to be able to have an instance of this in which `get_temp_path` works and another in which it doesn't, so I'd advocate for either immediately calling save() on first construction or loading everything into memory and pushing `self._temp_dir` into `save()` and `load()`. ########## File path: python/tvm/driver/tvmc/runner.py ########## @@ -337,135 +347,75 @@ def run_module( times : list of str execution times generated by the time evaluator """ - - with tempfile.TemporaryDirectory() as tmp_dir: - logger.debug("extracting module file %s", module_file) - t = tarfile.open(module_file) - t.extractall(tmp_dir) - graph = open(os.path.join(tmp_dir, "mod.json")).read() - params = bytearray(open(os.path.join(tmp_dir, "mod.params"), "rb").read()) - - if hostname: - # Remote RPC - if rpc_key: - logger.debug("running on remote RPC tracker with key %s", rpc_key) - session = request_remote(rpc_key, hostname, port, timeout=1000) - else: - logger.debug("running on remote RPC with no key") - session = rpc.connect(hostname, port) - else: - # Local - logger.debug("running a local session") - session = rpc.LocalSession() - - session.upload(os.path.join(tmp_dir, "mod.so")) - lib = session.load_module("mod.so") - - # TODO expand to other supported devices, as listed in tvm.rpc.client (@leandron) - logger.debug("device is %s", device) - if device == "gpu": - dev = session.gpu() - elif device == "cl": - dev = session.cl() + if not isinstance(tvmc_package, TVMCPackage): + raise TVMCException( + "This model doesn't seem to have been compiled yet. " + "Try calling tvmc.compile on the model before running it." + ) + + lib_path = tvmc_package.lib_path + graph = tvmc_package.graph + params = tvmc_package.params + + if hostname: + # Remote RPC + if rpc_key: + logger.debug("running on remote RPC tracker with key %s", rpc_key) + session = request_remote(rpc_key, hostname, port, timeout=1000) else: - assert device == "cpu" - dev = session.cpu() - - if profile: - logger.debug("creating runtime with profiling enabled") - module = debug_executor.create(graph, lib, dev, dump_root="./prof") - else: - logger.debug("creating runtime with profiling disabled") - module = runtime.create(graph, lib, dev) - - logger.debug("load params into the runtime module") - module.load_params(params) - - shape_dict, dtype_dict = get_input_info(graph, params) - inputs_dict = make_inputs_dict(shape_dict, dtype_dict, inputs, fill_mode) - - logger.debug("setting inputs to the module") - module.set_input(**inputs_dict) - - # Run must be called explicitly if profiling - if profile: - logger.debug("running the module with profiling enabled") - module.run() - - # create the module time evaluator (returns a function) - timer = module.module.time_evaluator("run", dev, 1, repeat=repeat) - # call the evaluator function to invoke the module and save execution times - prof_result = timer() - # collect a list of execution times from the profiling results - times = prof_result.results - - logger.debug("collecting the output tensors") - num_outputs = module.get_num_outputs() - outputs = {} - for i in range(num_outputs): - output_name = "output_{}".format(i) - outputs[output_name] = module.get_output(i).asnumpy() - - return outputs, times - - -def get_top_results(outputs, max_results): - """Return the top n results from the output tensor. - - This function is primarily for image classification and will - not necessarily generalise. - - Parameters - ---------- - outputs : dict - Outputs dictionary - {output_name: np.array}. - max_results : int - Number of results to return - - Returns - ------- - top_results : np.array - Results array of shape (2, n). - The first row is the indices and the second is the values. - - """ - output = np.copy(outputs["output_0"]) - sorted_labels = output.argsort()[0][-max_results:][::-1] - output.sort() - sorted_values = output[0][-max_results:][::-1] - top_results = np.array([sorted_labels, sorted_values]) - return top_results - - -def format_times(times): - """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: - mean (ms) max (ms) min (ms) std (ms) - 0.14310 0.16161 0.12933 0.01004 - - Parameters - ---------- - times : list - A list of execution times (in seconds). - - Returns - ------- - str - A formatted string containing the statistics. - """ - - # timestamps - mean_ts = np.mean(times) * 1000 - std_ts = np.std(times) * 1000 - max_ts = np.max(times) * 1000 - min_ts = np.min(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) + logger.debug("running on remote RPC with no key") + session = rpc.connect(hostname, port) + else: + # Local + logger.debug("running a local session") + session = rpc.LocalSession() + + session.upload(lib_path) + lib = session.load_module(f"{tvmc_package.name}.so") + + # TODO expand to other supported devices, as listed in tvm.rpc.client (@leandron) + logger.debug("device is %s", device) + if device == "gpu": + dev = session.gpu() + elif device == "cl": + dev = session.cl() + else: + assert device == "cpu" + dev = session.cpu() - return "%s\n%s\n" % (header, stats) + if profile: + logger.debug("creating runtime with profiling enabled") + module = debug_executor.create(graph, lib, dev, dump_root="./prof") + else: + logger.debug("creating runtime with profiling disabled") + module = runtime.create(graph, lib, dev) + + logger.debug("load params into the runtime module") + module.load_params(params) + + shape_dict, dtype_dict = get_input_info(graph, params) + inputs_dict = make_inputs_dict(shape_dict, dtype_dict, inputs, fill_mode) Review comment: ok, maybe worth including this as a comment 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: [email protected]
