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



##########
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:
       when you run you have the option of providing numpy inputs. If you don't 
provide it, theyll be randomly generated and arent recoverable. Presumably if 
you're not giving it inputs you dont care about the outputs and are just doing 
performance benchmarking.




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


Reply via email to