leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r496095293



##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -0,0 +1,301 @@
+# 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.
+"""
+Provides support to auto-tuning networks using AutoTVM.
+"""
+import os.path
+import logging
+import time
+
+from tvm import autotvm
+from tvm import relay
+from tvm.autotvm.tuner import GATuner
+from tvm.autotvm.tuner import GridSearchTuner
+from tvm.autotvm.tuner import RandomTuner
+from tvm.autotvm.tuner import XGBTuner
+
+from . import common, frontends
+from .common import TVMCException
+from .main import register_parser
+
+
+@register_parser
+def add_tune_parser(subparsers):
+    """ Include parser for 'tune' subcommand """
+
+    parser = subparsers.add_parser("tune", help="auto-tune a model")
+    parser.set_defaults(func=drive_tune)
+    parser.add_argument(
+        "--early-stopping",
+        type=int,
+        help="minimum number of trials before early stopping",
+    )
+    parser.add_argument("--hostname", help="hostname or IP address of the host 
machine")
+    parser.add_argument(
+        "--min-repeat-ms",
+        default=1000,
+        type=int,
+        help="minimum time to run each trial (in milliseconds)",
+    )
+    parser.add_argument(
+        "--model-format",
+        choices=frontends.get_frontend_names(),
+        help="specify input model format",
+    )
+    parser.add_argument(
+        "--number",
+        default=10,
+        type=int,
+        help="number of runs a single repeat is made of. "
+        "The final number of tuning executions is: "
+        "(1 + number * repeat)",
+    )
+    parser.add_argument(
+        "-o",
+        "--output",
+        required=True,
+        help="output file to store the tuning records for the tuning process",
+    )
+    parser.add_argument(
+        "--parallel",
+        default=4,
+        type=int,
+        help="the maximum number of parallel devices to use when tuning",
+    )
+    parser.add_argument("--port", default=9090, type=int, help="the port to 
connect to")
+    parser.add_argument(
+        "--repeat",
+        type=int,
+        default=1,
+        help="how many times to repeat each measurement",
+    )
+    parser.add_argument(
+        "--target",
+        help="compilation target as plain string, inline JSON or path to a 
JSON file",
+        required=True,
+    )
+    parser.add_argument("--timeout", default=10, help="time in seconds before 
timing out a config")
+    parser.add_argument("--tracker-key", help="the tracker key of the target 
device")
+    parser.add_argument(
+        "--trials",
+        type=int,
+        default=1000,
+        help="the maximum number of tuning trials to perform",
+    )
+    parser.add_argument(
+        "--tuner",
+        choices=["ga", "gridsearch", "random", "xgb", "xgb_knob", "xgb-rank"],
+        default="xgb",
+        help="type of tuner to use",
+    )
+    parser.add_argument(
+        "--tuning-records",
+        metavar="PATH",
+        help="path to an auto-tuning log file by AutoTVM.",
+    )
+    parser.add_argument(
+        "--desired-layout",
+        choices=["NCHW", "NHWC"],
+        default=None,
+        help="change the data layout of the whole graph",
+    )
+    # TODO (@leandron) This is a path to a physical file, but
+    #     can be improved in future to add integration with a modelzoo
+    #     or URL, for example.
+    parser.add_argument("FILE", help="path to the input model file")
+
+
+def drive_tune(args):
+    """Invoke auto-tuning with command line arguments
+
+    Parameters
+    ----------
+    args: argparse.Namespace
+        Arguments from command line parser.
+
+    Returns
+    --------
+    int
+        Zero if successfully completed
+
+    """
+
+    target = common.target_from_cli(args.target)
+    mod, params = frontends.load_model(args.FILE, args.model_format)
+
+    tasks = get_tuning_tasks(
+        mod=mod,
+        params=params,
+        target=target,
+        alter_layout=args.desired_layout,
+    )
+
+    if args.hostname:
+        logging.info("starting remote tuning:")
+        logging.info(" hostname: %s", args.hostname)
+        logging.info(" port    : %s", args.port)
+        if not args.tracker_key:
+            raise common.TVMCException(
+                "need to provide tracker key (--tracker-key) for remote tuning"
+            )
+
+        runner = autotvm.RPCRunner(
+            key=args.tracker_key,
+            host=args.hostname,
+            port=args.port,
+            number=args.number,
+            repeat=args.repeat,
+            n_parallel=args.parallel,
+            timeout=args.timeout,
+            min_repeat_ms=args.min_repeat_ms,
+        )
+    else:
+        logging.info("starting localhost tuning")
+        runner = autotvm.LocalRunner(
+            number=args.number,
+            repeat=args.repeat,
+            timeout=args.timeout,
+            min_repeat_ms=args.min_repeat_ms,
+        )
+
+    tuning_option = {
+        "tuner": args.tuner,
+        "trials": args.trials,
+        "early_stopping": args.early_stopping,
+        "measure_option": autotvm.measure_option(
+            builder=autotvm.LocalBuilder(build_func="default"), runner=runner
+        ),
+        "tuning_records": args.tuning_records,
+    }
+    logging.debug(" tuning options: %s", tuning_option)
+
+    tune_tasks(tasks, args.output, **tuning_option)
+    return 0
+
+
+def get_tuning_tasks(mod, params, target, target_host=None, alter_layout=None):
+    """Get the tuning tasks for a given relay module.
+
+    Parameters
+    ----------
+    mod : tvm.relay.Module
+        The relay module from which to extract tuning tasks.
+    params : dict
+        The params for the relay module.
+    target : tvm.target.Target
+        The compilation target.
+    target_host : str, optional
+        The compilation target for the host.
+    alter_layout : str, optional
+        The layout to convert the graph to. Note, the convert layout
+        pass doesn't currently guarantee the whole of the graph will
+        be converted to the chosen layout.
+
+    Returns
+    -------
+    tasks : list of autotvm.Tasks
+        list of tasks to be tuned
+
+    """
+    if not target_host:
+        target_host = target

Review comment:
       Understood. Done now.




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