areusch commented on a change in pull request #7823:
URL: https://github.com/apache/tvm/pull/7823#discussion_r614167834
##########
File path: python/tvm/auto_scheduler/search_task.py
##########
@@ -89,6 +122,21 @@ def __init__(
warp_size,
)
+ def __repr__(self):
Review comment:
should this be __str__ or PrettyPrint(), and let `__repr__` follow
Python convention of being interpretable?
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -228,24 +240,137 @@ def drive_tune(args):
args: argparse.Namespace
Arguments from command line parser.
"""
- # extra arguments validation before importing the model, so that obvious
errors
- # are pointed in advance.
- if args.rpc_tracker:
- parsed_url = urlparse("//%s" % args.rpc_tracker)
+ tvmc_model = frontends.load_model(args.FILE, args.model_format,
shape_dict=args.input_shapes)
+ tvmc_model.tuning_records = args.tuning_records
+ # Specify hardware parameters, although they'll only be used if
autoscheduling.
+ hardware_params = auto_scheduler.HardwareParams(
+ args.num_cores,
+ args.vector_unit_bytes,
+ args.cache_line_bytes,
+ args.max_shared_memory_per_block,
+ args.max_local_memory_per_block,
+ args.max_threads_per_block,
+ args.max_vthread_extent,
+ args.warp_size,
+ args.target,
+ args.target_host,
+ )
+
+ tune_model(
+ tvmc_model,
+ args.target,
+ args.output,
+ args.enable_autoscheduler,
+ args.rpc_key,
+ args.rpc_tracker,
+ args.trials,
+ args.target_host,
+ args.tuner,
+ args.min_repeat_ms,
+ args.early_stopping,
+ args.desired_layout,
+ args.timeout,
+ args.number,
+ args.repeat,
+ args.parallel,
+ hardware_params,
+ args.include_simple_tasks,
+ args.log_estimated_latency,
+ )
+
+
+def tune_model(
+ tvmc_model: TVMCModel,
+ target: str,
+ tuning_records: Optional[str] = None,
+ enable_autoscheduler: bool = False,
+ rpc_key: Optional[str] = None,
+ rpc_tracker: Optional[str] = None,
+ trials: Optional[int] = None,
+ target_host: str = "llvm",
+ tuner: str = "xgb",
+ min_repeat_ms: Optional[int] = None,
+ early_stopping: Optional[int] = None,
+ desired_layout: Optional[str] = None,
+ timeout: int = 10,
+ number: int = 10,
+ repeat: int = 1,
+ parallel: int = 4,
+ hardware_params: Optional[HardwareParams] = None,
+ include_simple_tasks: bool = False,
+ log_estimated_latency: bool = False,
+):
+ """Use tuning to automatically optimize the functions in a model.
+
+ Parameters
+ ----------
+ tvmc_model : TVMCModel
+ The model to be optimized.
+ target : str
+ Compilation target as plain string, inline JSON or path to a JSON file.
+ tuning_records: str, optional
+ The path to a file that tuning results will be saved to. If not
specified,
+ a temporary file will be used.
Review comment:
temporary file doesn't really make sense--you don't want to save tuning
records to an anonymous file that may go away on reboot. should we choose a
better default?
##########
File path: python/tvm/driver/tvmc/compiler.py
##########
@@ -248,59 +254,14 @@ def compile_model(
source = str(mod) if source_type == "relay" else
lib.get_source(source_type)
dumps[source_type] = source
- # TODO we need to update this return to use the updated graph module APIs
- # as these getter functions will be deprecated in the next release
(@leandron)
- return graph_module.get_json(), graph_module.get_lib(),
graph_module.get_params(), dumps
-
-
-def save_module(module_path, graph, lib, params, cross=None):
- """
- Create a tarball containing the generated TVM graph,
- exported library and parameters
-
- Parameters
- ----------
- module_path : str
- path to the target tar.gz file to be created,
- including the file name
- graph : str
- A JSON-serialized TVM execution graph.
- lib : tvm.module.Module
- A TVM module containing the compiled functions.
- params : dict
- The parameters (weights) for the TVM module.
- cross : str or callable object, optional
- Function that performs the actual compilation
+ tvmc_model.dumps = dumps
Review comment:
how come we assign to an attribute here?
##########
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
Review comment:
would advocate for not pulling these off the tvmc_package--I think it
decreases readability by making you lookup a local var alias
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -142,50 +147,57 @@ def add_tune_parser(subparsers):
auto_scheduler_group.add_argument(
"--cache-line-bytes",
type=int,
- default=64,
- help="the size of cache line in bytes",
+ default=None,
+ help="the size of cache line in bytes. "
+ "If not specified, it will be extracted from the target.",
Review comment:
perhaps could clarify a bit more what "extracted from the target" means
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -228,24 +240,137 @@ def drive_tune(args):
args: argparse.Namespace
Arguments from command line parser.
"""
- # extra arguments validation before importing the model, so that obvious
errors
- # are pointed in advance.
- if args.rpc_tracker:
- parsed_url = urlparse("//%s" % args.rpc_tracker)
+ tvmc_model = frontends.load_model(args.FILE, args.model_format,
shape_dict=args.input_shapes)
+ tvmc_model.tuning_records = args.tuning_records
+ # Specify hardware parameters, although they'll only be used if
autoscheduling.
+ hardware_params = auto_scheduler.HardwareParams(
+ args.num_cores,
Review comment:
use kwargs since there are so many.
a follow-on question: since there are so many of these, is there a standard
file format you can write which could be parsed e.g.
auto_scheduler.HardwareParams.LoadFromString()
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -142,50 +147,57 @@ def add_tune_parser(subparsers):
auto_scheduler_group.add_argument(
"--cache-line-bytes",
type=int,
- default=64,
- help="the size of cache line in bytes",
+ default=None,
Review comment:
is `default=None,` needed?
##########
File path: python/tvm/auto_scheduler/search_task.py
##########
@@ -43,40 +43,73 @@
@tvm._ffi.register_object("auto_scheduler.HardwareParams")
class HardwareParams(Object):
- """The parameters of target hardware used to guide the search policy
+ """The parameters of target hardware used to guide the search policy. When
Review comment:
just a one-liner to start, followed by a broader description
https://numpydoc.readthedocs.io/en/latest/format.html#sections
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -228,24 +240,137 @@ def drive_tune(args):
args: argparse.Namespace
Arguments from command line parser.
"""
- # extra arguments validation before importing the model, so that obvious
errors
- # are pointed in advance.
- if args.rpc_tracker:
- parsed_url = urlparse("//%s" % args.rpc_tracker)
+ tvmc_model = frontends.load_model(args.FILE, args.model_format,
shape_dict=args.input_shapes)
+ tvmc_model.tuning_records = args.tuning_records
+ # Specify hardware parameters, although they'll only be used if
autoscheduling.
+ hardware_params = auto_scheduler.HardwareParams(
+ args.num_cores,
+ args.vector_unit_bytes,
+ args.cache_line_bytes,
+ args.max_shared_memory_per_block,
+ args.max_local_memory_per_block,
+ args.max_threads_per_block,
+ args.max_vthread_extent,
+ args.warp_size,
+ args.target,
+ args.target_host,
+ )
+
+ tune_model(
+ tvmc_model,
+ args.target,
+ args.output,
+ args.enable_autoscheduler,
+ args.rpc_key,
+ args.rpc_tracker,
+ args.trials,
+ args.target_host,
+ args.tuner,
+ args.min_repeat_ms,
+ args.early_stopping,
+ args.desired_layout,
+ args.timeout,
+ args.number,
+ args.repeat,
+ args.parallel,
+ hardware_params,
+ args.include_simple_tasks,
+ args.log_estimated_latency,
+ )
+
+
+def tune_model(
+ tvmc_model: TVMCModel,
+ target: str,
+ tuning_records: Optional[str] = None,
+ enable_autoscheduler: bool = False,
+ rpc_key: Optional[str] = None,
+ rpc_tracker: Optional[str] = None,
+ trials: Optional[int] = None,
+ target_host: str = "llvm",
+ tuner: str = "xgb",
+ min_repeat_ms: Optional[int] = None,
+ early_stopping: Optional[int] = None,
+ desired_layout: Optional[str] = None,
+ timeout: int = 10,
+ number: int = 10,
+ repeat: int = 1,
+ parallel: int = 4,
+ hardware_params: Optional[HardwareParams] = None,
+ include_simple_tasks: bool = False,
+ log_estimated_latency: bool = False,
+):
+ """Use tuning to automatically optimize the functions in a model.
+
+ Parameters
+ ----------
+ tvmc_model : TVMCModel
+ The model to be optimized.
+ target : str
+ Compilation target as plain string, inline JSON or path to a JSON file.
+ tuning_records: str, optional
+ The path to a file that tuning results will be saved to. If not
specified,
+ a temporary file will be used.
+ enable_autoscheduler : bool, optional
+ When true, use autoscheduling rather than autotvm. This should produce
+ faster kernels for compatible model-target pairs.
+ rpc_key : str, optional
+ The RPC tracker key of the target device. Required when rpc_tracker is
provided.
+ rpc_tracker : str, optional
+ The hostname and port (optional, defaults to 9090) of the RPC tracker,
+ e.g. 192.168.0.100:9999.
+ trials : int, optional
+ The number of schedules to try out. For autotvm, each task will have
this many
+ options explored. For autoscheduling, the total number of schedules
checked in
+ the entire model will be this many.
+ target_host : str, optional
+ The host compilation target, defaults to 'llvm'.
+ tuner : str, optional
+ The type of tuner to use when tuning with autotvm. Can be one of
+ "ga", "gridsearch", "random", "xgb", "xgb_knob", and "xgb-rank".
+ min_repeat_ms : int, optional
+ Minimum time to run each trial. Defaults to 0 on x86 and 1000 on other
targets.
+ early_stopping : int, optional
+ When specified, stop tuning after this number of trials if results
aren't improving.
+ desired_layout : str, optional
+ Can be one of "NCHW" or "NHWC". When specified, the graph will be
converted to this layout.
+ timeout : int, optional,
+ If a kernel trial lasts longer than this duration in seconds, it will
be
+ considered a failure.
+ number : int, optional
Review comment:
imo when bubbling this argument up to this level, we need more context.
"number" is very confusing particularly when we have "trials" here. I would
advocate to create contextual nested structs in the Config object, to help add
context without needing to prefix variables.
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -228,24 +240,137 @@ def drive_tune(args):
args: argparse.Namespace
Arguments from command line parser.
"""
- # extra arguments validation before importing the model, so that obvious
errors
- # are pointed in advance.
- if args.rpc_tracker:
- parsed_url = urlparse("//%s" % args.rpc_tracker)
+ tvmc_model = frontends.load_model(args.FILE, args.model_format,
shape_dict=args.input_shapes)
Review comment:
not that this was introduced in this change, but imo it's a bit weird to
have args.FILE followed by normal-case args.model_format. if we are changing
this function significantly, sort of prefer to avoid metavar
##########
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,
Review comment:
just specify "default" as the default kwarg, strings are immutable
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -228,24 +240,137 @@ def drive_tune(args):
args: argparse.Namespace
Arguments from command line parser.
"""
- # extra arguments validation before importing the model, so that obvious
errors
- # are pointed in advance.
- if args.rpc_tracker:
- parsed_url = urlparse("//%s" % args.rpc_tracker)
+ tvmc_model = frontends.load_model(args.FILE, args.model_format,
shape_dict=args.input_shapes)
+ tvmc_model.tuning_records = args.tuning_records
+ # Specify hardware parameters, although they'll only be used if
autoscheduling.
+ hardware_params = auto_scheduler.HardwareParams(
+ args.num_cores,
+ args.vector_unit_bytes,
+ args.cache_line_bytes,
+ args.max_shared_memory_per_block,
+ args.max_local_memory_per_block,
+ args.max_threads_per_block,
+ args.max_vthread_extent,
+ args.warp_size,
+ args.target,
+ args.target_host,
+ )
+
+ tune_model(
+ tvmc_model,
+ args.target,
+ args.output,
+ args.enable_autoscheduler,
+ args.rpc_key,
+ args.rpc_tracker,
+ args.trials,
+ args.target_host,
+ args.tuner,
+ args.min_repeat_ms,
+ args.early_stopping,
+ args.desired_layout,
+ args.timeout,
+ args.number,
+ args.repeat,
+ args.parallel,
+ hardware_params,
+ args.include_simple_tasks,
+ args.log_estimated_latency,
+ )
+
+
+def tune_model(
+ tvmc_model: TVMCModel,
+ target: str,
+ tuning_records: Optional[str] = None,
+ enable_autoscheduler: bool = False,
+ rpc_key: Optional[str] = None,
+ rpc_tracker: Optional[str] = None,
+ trials: Optional[int] = None,
+ target_host: str = "llvm",
+ tuner: str = "xgb",
+ min_repeat_ms: Optional[int] = None,
+ early_stopping: Optional[int] = None,
+ desired_layout: Optional[str] = None,
+ timeout: int = 10,
+ number: int = 10,
+ repeat: int = 1,
+ parallel: int = 4,
+ hardware_params: Optional[HardwareParams] = None,
+ include_simple_tasks: bool = False,
+ log_estimated_latency: bool = False,
+):
+ """Use tuning to automatically optimize the functions in a model.
+
+ Parameters
+ ----------
+ tvmc_model : TVMCModel
+ The model to be optimized.
+ target : str
+ Compilation target as plain string, inline JSON or path to a JSON file.
+ tuning_records: str, optional
+ The path to a file that tuning results will be saved to. If not
specified,
+ a temporary file will be used.
+ enable_autoscheduler : bool, optional
+ When true, use autoscheduling rather than autotvm. This should produce
+ faster kernels for compatible model-target pairs.
+ rpc_key : str, optional
+ The RPC tracker key of the target device. Required when rpc_tracker is
provided.
+ rpc_tracker : str, optional
+ The hostname and port (optional, defaults to 9090) of the RPC tracker,
+ e.g. 192.168.0.100:9999.
+ trials : int, optional
+ The number of schedules to try out. For autotvm, each task will have
this many
+ options explored. For autoscheduling, the total number of schedules
checked in
+ the entire model will be this many.
+ target_host : str, optional
+ The host compilation target, defaults to 'llvm'.
+ tuner : str, optional
+ The type of tuner to use when tuning with autotvm. Can be one of
+ "ga", "gridsearch", "random", "xgb", "xgb_knob", and "xgb-rank".
+ min_repeat_ms : int, optional
+ Minimum time to run each trial. Defaults to 0 on x86 and 1000 on other
targets.
+ early_stopping : int, optional
+ When specified, stop tuning after this number of trials if results
aren't improving.
+ desired_layout : str, optional
+ Can be one of "NCHW" or "NHWC". When specified, the graph will be
converted to this layout.
+ timeout : int, optional,
+ If a kernel trial lasts longer than this duration in seconds, it will
be
+ considered a failure.
+ number : int, optional
+ The number of runs a single repeat is made of.
+ repeat : int, optional
+ How many times each measurement should be repeated.
+ parallel : int, optional
+ The maximum number of parallel devices to use when tuning.
+ hardware_params : auto_scheduler.HardwareParams, optional
+ When using the autoscheduler, this object defines the configuration of
the target hardware.
+ include_simple_tasks : bool, optional
+ Whether to extract simple operations or only computationally intensive
ones when using
+ the autoscheduler.
+ log_estimated_latency : bool, optional
+ If using the autoscheduler, write the estimated latency at each step
of tuning to file.
+
+ """
+ if rpc_tracker:
+ parsed_url = urlparse("//%s" % rpc_tracker)
rpc_hostname = parsed_url.hostname
rpc_port = parsed_url.port or 9090
logger.info("RPC tracker hostname: %s", rpc_hostname)
logger.info("RPC tracker port: %s", rpc_port)
- if not args.rpc_key:
+ if not rpc_key:
raise common.TVMCException(
"need to provide an RPC tracker key (--rpc-key) for remote
tuning"
)
- target, extra_targets = common.target_from_cli(args.target)
- target_host = args.target_host
- target, target_host = Target.check_and_update_host_consist(target,
target_host)
- mod, params = frontends.load_model(args.FILE, args.model_format,
shape_dict=args.input_shapes)
+ target, extra_targets = common.target_from_cli(target)
+ if target_host is not None:
Review comment:
I thought target_host was slowly being deprecated. does it make sense to
bubble it up to the CLI?
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -228,24 +240,137 @@ def drive_tune(args):
args: argparse.Namespace
Arguments from command line parser.
"""
- # extra arguments validation before importing the model, so that obvious
errors
- # are pointed in advance.
- if args.rpc_tracker:
- parsed_url = urlparse("//%s" % args.rpc_tracker)
+ tvmc_model = frontends.load_model(args.FILE, args.model_format,
shape_dict=args.input_shapes)
+ tvmc_model.tuning_records = args.tuning_records
+ # Specify hardware parameters, although they'll only be used if
autoscheduling.
+ hardware_params = auto_scheduler.HardwareParams(
+ args.num_cores,
+ args.vector_unit_bytes,
+ args.cache_line_bytes,
+ args.max_shared_memory_per_block,
+ args.max_local_memory_per_block,
+ args.max_threads_per_block,
+ args.max_vthread_extent,
+ args.warp_size,
+ args.target,
+ args.target_host,
+ )
+
+ tune_model(
+ tvmc_model,
+ args.target,
+ args.output,
+ args.enable_autoscheduler,
+ args.rpc_key,
+ args.rpc_tracker,
+ args.trials,
+ args.target_host,
+ args.tuner,
+ args.min_repeat_ms,
+ args.early_stopping,
+ args.desired_layout,
+ args.timeout,
+ args.number,
+ args.repeat,
+ args.parallel,
+ hardware_params,
+ args.include_simple_tasks,
+ args.log_estimated_latency,
+ )
+
+
+def tune_model(
+ tvmc_model: TVMCModel,
+ target: str,
+ tuning_records: Optional[str] = None,
+ enable_autoscheduler: bool = False,
+ rpc_key: Optional[str] = None,
+ rpc_tracker: Optional[str] = None,
+ trials: Optional[int] = None,
+ target_host: str = "llvm",
+ tuner: str = "xgb",
+ min_repeat_ms: Optional[int] = None,
+ early_stopping: Optional[int] = None,
+ desired_layout: Optional[str] = None,
+ timeout: int = 10,
+ number: int = 10,
+ repeat: int = 1,
+ parallel: int = 4,
+ hardware_params: Optional[HardwareParams] = None,
+ include_simple_tasks: bool = False,
+ log_estimated_latency: bool = False,
+):
+ """Use tuning to automatically optimize the functions in a model.
+
+ Parameters
+ ----------
+ tvmc_model : TVMCModel
+ The model to be optimized.
+ target : str
+ Compilation target as plain string, inline JSON or path to a JSON file.
+ tuning_records: str, optional
+ The path to a file that tuning results will be saved to. If not
specified,
+ a temporary file will be used.
+ enable_autoscheduler : bool, optional
+ When true, use autoscheduling rather than autotvm. This should produce
+ faster kernels for compatible model-target pairs.
+ rpc_key : str, optional
+ The RPC tracker key of the target device. Required when rpc_tracker is
provided.
+ rpc_tracker : str, optional
+ The hostname and port (optional, defaults to 9090) of the RPC tracker,
+ e.g. 192.168.0.100:9999.
+ trials : int, optional
+ The number of schedules to try out. For autotvm, each task will have
this many
+ options explored. For autoscheduling, the total number of schedules
checked in
+ the entire model will be this many.
+ target_host : str, optional
+ The host compilation target, defaults to 'llvm'.
+ tuner : str, optional
+ The type of tuner to use when tuning with autotvm. Can be one of
+ "ga", "gridsearch", "random", "xgb", "xgb_knob", and "xgb-rank".
+ min_repeat_ms : int, optional
+ Minimum time to run each trial. Defaults to 0 on x86 and 1000 on other
targets.
+ early_stopping : int, optional
+ When specified, stop tuning after this number of trials if results
aren't improving.
+ desired_layout : str, optional
Review comment:
I think this is particularly confusing as a top-level option with no
context. "converting to NCHW" needs more context, so it be great to specify
whether this is passed to e.g. task extraction, importer, a pass, etc
##########
File path: python/tvm/driver/tvmc/compiler.py
##########
@@ -117,34 +116,37 @@ def drive_compile(args):
Zero if successfully completed
"""
- mod, params = frontends.load_model(args.FILE, args.model_format,
args.input_shapes)
+ tvmc_model = frontends.load_model(args.FILE, args.model_format,
args.input_shapes)
- graph, lib, params, dumps = compile_model(
- mod,
- params,
+ if args.tuning_records:
+ tvmc_model.tuning_records = args.tuning_records
+
+ dump_code = [x.strip() for x in args.dump_code.split(",")] if
args.dump_code else None
+
+ compile_model(
Review comment:
kwargs here
##########
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,
Review comment:
one-liner https://numpydoc.readthedocs.io/en/latest/format.html#sections
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -255,97 +380,100 @@ def drive_tune(args):
# min_repeat_ms should be:
# a. the value provided by the user, if any, or
# b. 0ms in case target is "cpu"; otherwise 1000ms
- if args.min_repeat_ms is not None:
- min_repeat_ms = args.min_repeat_ms
- else:
+ if min_repeat_ms is None:
min_repeat_ms = 0 if target.keys[0] == "cpu" else 1000
logger.debug("Default --min-repeat-ms for this target is %s",
min_repeat_ms)
- if args.rpc_tracker:
- runner_ctor = auto_scheduler.RPCRunner if args.enable_autoscheduler
else autotvm.RPCRunner
+ if rpc_tracker:
+ runner_ctor = auto_scheduler.RPCRunner if enable_autoscheduler else
autotvm.RPCRunner
runner = runner_ctor(
- key=args.rpc_key,
+ key=rpc_key,
host=rpc_hostname,
port=rpc_port,
- number=args.number,
- repeat=args.repeat,
- n_parallel=args.parallel,
- timeout=args.timeout,
+ number=number,
+ repeat=repeat,
+ n_parallel=parallel,
+ timeout=timeout,
min_repeat_ms=min_repeat_ms,
)
else:
logger.info("starting localhost tuning")
runner_ctor = (
- auto_scheduler.LocalRunner if args.enable_autoscheduler else
autotvm.LocalRunner
+ auto_scheduler.LocalRPCMeasureContext if enable_autoscheduler else
autotvm.LocalRunner
)
runner = runner_ctor(
- number=args.number,
- repeat=args.repeat,
- timeout=args.timeout,
+ number=number,
+ repeat=repeat,
+ timeout=timeout,
min_repeat_ms=min_repeat_ms,
)
- if args.enable_autoscheduler:
- # Specify hardware parameters
- hardware_params = auto_scheduler.HardwareParams(
- args.num_cores,
- args.vector_unit_bytes,
- args.cache_line_bytes,
- args.max_shared_memory_per_block,
- args.max_local_memory_per_block,
- args.max_threads_per_block,
- args.max_vthread_extent,
- args.warp_size,
- )
+ if enable_autoscheduler:
+
tasks, weights = autoscheduler_get_tuning_tasks(
mod=mod,
params=params,
target=target,
- alter_layout=args.desired_layout,
+ alter_layout=desired_layout,
hardware_params=hardware_params,
- include_simple_tasks=args.include_simple_tasks,
+ include_simple_tasks=include_simple_tasks,
)
+ # If not specified, choose a number of trials likely to produce good
results.
+ if trials is None:
+ trials = 10000
Review comment:
make 10000 a constant somewhere, and explain your choice
##########
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.
Review comment:
say more about the path, here or under "returns" section. it's the path
to a directory which is intended to hold the records
##########
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:
Review comment:
seems like we should do one or the other here
##########
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.
Review comment:
specify what kind of path--is it to a file, dir, etc? suggested file
extension? what kind of thing will be present at this path after calling this
function?
##########
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:
don't see why we should create the tempdir here
##########
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."""
Review comment:
nit: space.
should we provide a way to get a string summary, as well as a file= kwarg
which could be passed to print()?
##########
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.
Review comment:
state units, here or ideally as a suffix to the var. why are they str?
##########
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
Review comment:
I think this docstring is actually for `__init__`.
https://numpydoc.readthedocs.io/en/latest/format.html#documenting-classes
or, change "Parameters" to "Attributes" here and adjust the one-liner to
describe the class rather than the function.
##########
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):
+ """Return the top n results from the output tensor.
+
+ This function is primarily for image classification and will
+ not necessarily generalize.
+
+ Parameters
+ ----------
+ 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(self.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 get_output(self, name: str):
+ """A helper function to grab one of the outputs by name.
+
+ Parameters
+ ----------
+ name : str
+ The name of the output to return
Review comment:
document the return type
##########
File path: tests/python/driver/tvmc/test_autoscheduler.py
##########
@@ -26,29 +24,32 @@
def _get_tasks(model):
- mod, params = tvmc.frontends.load_model(model)
- tasks, weights = tvmc.autotuner.autoscheduler_get_tuning_tasks(mod,
params, "llvm")
+ tvmc_model = tvmc.frontends.load_model(model)
+ tasks, weights = tvmc.autotuner.autoscheduler_get_tuning_tasks(
+ tvmc_model.mod, tvmc_model.params, "llvm"
+ )
return (tasks, weights)
-def _autoscheduler_test_helper(
- model, tmpdir_name, tasks_weights=None, early_stopping=1,
tuning_records=None
-):
- tasks, weights = tasks_weights if tasks_weights else _get_tasks(model)
+def _autoscheduler_test_helper(model, tmpdir_name, early_stopping=1,
tuning_records=None):
+ tvmc_model = tvmc.frontends.load_model(model)
+ tvmc_model.tuning_records = tuning_records
Review comment:
another strange instance where we modify an attribute of TVMCModel
##########
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):
+ """Return the top n results from the output tensor.
+
+ This function is primarily for image classification and will
+ not necessarily generalize.
+
+ Parameters
+ ----------
+ 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(self.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 get_output(self, name: str):
+ """A helper function to grab one of the outputs by name.
+
+ Parameters
+ ----------
+ name : str
+ The name of the output to return
+ """
+ return self.outputs[name]
+
+ def save(self, output_path: str):
+ """Save the numpy outputs to disk.
+
+ Parameters
+ ----------
+ output_path : str
+ The path to save the numpy results to.
+ """
+ np.savez(output_path, **self.outputs)
+
+ def __repr__(self):
Review comment:
same question here: this should probably be `__str__`?
##########
File path: tests/python/driver/tvmc/conftest.py
##########
@@ -41,7 +41,7 @@ def download_and_untar(model_url, model_sub_path, temp_dir):
return os.path.join(temp_dir, model_sub_path)
-def get_sample_compiled_module(target_dir):
+def get_sample_compiled_module(target_dir, package_path):
Review comment:
path is misleading here, it's actually package_filename
##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -228,24 +240,137 @@ def drive_tune(args):
args: argparse.Namespace
Arguments from command line parser.
"""
- # extra arguments validation before importing the model, so that obvious
errors
- # are pointed in advance.
- if args.rpc_tracker:
- parsed_url = urlparse("//%s" % args.rpc_tracker)
+ tvmc_model = frontends.load_model(args.FILE, args.model_format,
shape_dict=args.input_shapes)
+ tvmc_model.tuning_records = args.tuning_records
+ # Specify hardware parameters, although they'll only be used if
autoscheduling.
+ hardware_params = auto_scheduler.HardwareParams(
+ args.num_cores,
+ args.vector_unit_bytes,
+ args.cache_line_bytes,
+ args.max_shared_memory_per_block,
+ args.max_local_memory_per_block,
+ args.max_threads_per_block,
+ args.max_vthread_extent,
+ args.warp_size,
+ args.target,
+ args.target_host,
+ )
+
+ tune_model(
Review comment:
same question for this API--there are way too many parameters IMO. at
minimum, use kwargs. more ideally, create a Config object which can also be
e.g. loaded from yaml or json.
##########
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:
is it possible to ever recover these inputs?
##########
File path: python/tvm/micro/model_library_format.py
##########
@@ -22,8 +22,9 @@
import os
import re
import tarfile
+import tvm
-from ..contrib import utils
+from ..contrib import utils, cc
Review comment:
i think you're reverting these changes, but in case not: I think cc is a
pretty surprising variable to have as a module-level thing. prefer to refer to
it as contrib.cc
##########
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:
this seems a little specific for the top-level TVMCResult class, and
really it should be possible to demonstrate that this class is well-organized
by making this a utils function that takes tvmc_result, max_results as
parameters. should we move to e.g. `result_util.py`?
##########
File path: python/tvm/driver/tvmc/runner.py
##########
@@ -112,8 +112,10 @@ def drive_run(args):
except IOError as ex:
raise TVMCException("Error loading inputs file: %s" % ex)
- outputs, times = run_module(
- args.FILE,
+ tvmc_package = TVMCPackage(package_path=args.FILE)
Review comment:
yeah that's fair. apologies for the miscommunication here.
one question though: what documentation will we require for the custom
packing format? as this is tvmc, the bar should be higher than just reading the
code, imo.
##########
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):
+ """Return the top n results from the output tensor.
+
+ This function is primarily for image classification and will
+ not necessarily generalize.
+
+ Parameters
+ ----------
+ 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(self.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 get_output(self, name: str):
+ """A helper function to grab one of the outputs by name.
+
+ Parameters
+ ----------
+ name : str
+ The name of the output to return
+ """
+ return self.outputs[name]
+
+ def save(self, output_path: str):
+ """Save the numpy outputs to disk.
Review comment:
state the format
--
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]