jwfromm commented on a change in pull request #7823: URL: https://github.com/apache/tvm/pull/7823#discussion_r616972342
########## 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: I've made some changes that restrict arbitrary access to the models temporary directory. Now the only external facing functions that should access it are `default_package_path` and `default_tuning_records_path` which are can be used as defaults for the compiled records and tuning logs respectively. When these files exist, they will be saved and loaded with `save` and `load`. I think this should address your concerns while not presenting any user facing complexity. -- 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]
