You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/09/23 13:52:27 UTC

[GitHub] [incubator-tvm] leandron opened a new pull request #6537: tvmc: introduce 'tune' subcommand (part 3/4)

leandron opened a new pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537


   Introduce a sub-command to drive auto-tuning via command line, using AutoTVM, to produce the tuning records that can be used at compile time, or to further improve other tuning sessions. It follow good practices present on the auto-tuning tutorials.
   
   In case you want to try it out (supposing you have your tvm built and working with this PR), you can have a look on the example below:
   ```
   $ wget https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet50-v2-7.onnx
   $ python -m tvm.driver.tvmc --verbose tune --trials 1 --output log1.txt --target="llvm" resnet50-v2-7.onnx
   (...messages...)
   WARNING:root:Attribute spatial is ignored in relay.sym.batch_norm
   [Task  1/23]  Current/Best:    0.00/   0.00 GFLOPS | Progress: (1/1) | 1.32 s Done.
   [Task  2/23]  Current/Best:    0.00/   0.00 GFLOPS | Progress: (1/1) | 1.23 s Done.
   [Task  3/23]  Current/Best:    0.00/   0.00 GFLOPS | Progress: (1/1) | 1.07 s Done.
   ...
   ```
   
   cc @comaniac @masahi @FrozenGene for reviews if possible
    


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r493875220



##########
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")

Review comment:
       My considerations at https://github.com/apache/incubator-tvm/pull/6537#discussion_r493871858. 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494906818



##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),
+    )
+
+    return tasks
+
+
+def tune_tasks(
+    tasks,
+    log_file,
+    measure_option,
+    tuner,
+    trials,
+    early_stopping=None,
+    tuning_records=None,
+):
+    """Tune a list of tasks and output the history to a log file.
+
+    Parameters
+    ----------
+    tasks : list
+        A list of autotvm.Tasks to tune.
+    log_file : str
+        A file to output the tuning history, in JSON.
+    measure_option : autotvm.measure_option
+        Options to build and run a tuning task.
+    tuner : str
+        Which tuner to use.
+    trials : int
+        The maximum number of tuning trials to perform.
+    early_stopping : int, optional
+        The minimum number of tuning trials to perform.
+        This will be equal to 'trials' if not specified.
+    tuning_records: str, optional
+        Path to the file produced by the tuning, to be used during
+        tuning.
+
+    """
+    if not tasks:
+        logging.warning("there were no tasks found to be tuned")
+        return
+
+    if not early_stopping:
+        early_stopping = trials
+
+    for i, tsk in enumerate(tasks):
+        prefix = "[Task %2d/%2d] " % (i + 1, len(tasks))
+
+        # Create a tuner
+        if tuner in ("xgb", "xgb-rank"):
+            tuner_obj = XGBTuner(tsk, loss_type="rank")
+        elif tuner == "xgb_knob":
+            tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="knob")
+        elif tuner == "ga":
+            tuner_obj = GATuner(tsk, pop_size=50)
+        elif tuner == "random":
+            tuner_obj = RandomTuner(tsk)
+        elif tuner == "gridsearch":
+            tuner_obj = GridSearchTuner(tsk)
+        else:
+            raise TVMCException("invalid tuner: %s " % tuner)
+
+        # If transfer learning is being used, load the existing results
+        if tuning_records and os.path.exists(tuning_records):
+            logging.warning("loading tuning records from %s", tuning_records)

Review comment:
       Yep. Done




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] FrozenGene commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494900132



##########
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")

Review comment:
       `--rpc-key` is acceptable for me.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494905913



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

Review comment:
       Makes sense. Removed.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] FrozenGene commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-698071078


   Thanks @leandron , great job. Besides this, I think we could also add some features I think it is important too:
   1. We could add `--num-threads` to control how many cores to tune. This is important for remote devices like ARM
   2. We could add `--cpu-mode` to control BIG core or LITTLE core to tune. Imagine remote devices like have A72 + A35. This feature maybe should require us to expose one interface in our AutoTVM to use API `runtime.config_threadpool`.
   
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494908785



##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),
+    )
+
+    return tasks
+
+
+def tune_tasks(
+    tasks,
+    log_file,
+    measure_option,
+    tuner,
+    trials,
+    early_stopping=None,
+    tuning_records=None,
+):
+    """Tune a list of tasks and output the history to a log file.
+
+    Parameters
+    ----------
+    tasks : list
+        A list of autotvm.Tasks to tune.
+    log_file : str
+        A file to output the tuning history, in JSON.
+    measure_option : autotvm.measure_option
+        Options to build and run a tuning task.
+    tuner : str
+        Which tuner to use.
+    trials : int
+        The maximum number of tuning trials to perform.
+    early_stopping : int, optional
+        The minimum number of tuning trials to perform.
+        This will be equal to 'trials' if not specified.
+    tuning_records: str, optional
+        Path to the file produced by the tuning, to be used during
+        tuning.
+
+    """
+    if not tasks:
+        logging.warning("there were no tasks found to be tuned")
+        return
+
+    if not early_stopping:
+        early_stopping = trials
+
+    for i, tsk in enumerate(tasks):
+        prefix = "[Task %2d/%2d] " % (i + 1, len(tasks))
+
+        # Create a tuner
+        if tuner in ("xgb", "xgb-rank"):
+            tuner_obj = XGBTuner(tsk, loss_type="rank")
+        elif tuner == "xgb_knob":
+            tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="knob")
+        elif tuner == "ga":
+            tuner_obj = GATuner(tsk, pop_size=50)
+        elif tuner == "random":
+            tuner_obj = RandomTuner(tsk)
+        elif tuner == "gridsearch":
+            tuner_obj = GridSearchTuner(tsk)
+        else:
+            raise TVMCException("invalid tuner: %s " % tuner)
+
+        # If transfer learning is being used, load the existing results
+        if tuning_records and os.path.exists(tuning_records):
+            logging.warning("loading tuning records from %s", tuning_records)
+            start_time = time.time()
+            tuner_obj.load_history(autotvm.record.load_from_file(tuning_records))
+            logging.info("loaded history in %s", str(time.time() - start_time))

Review comment:
       Done




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494900498



##########
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")

Review comment:
       It refers to the compilation timeout. I've adjusted the help message to make it clear.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] FrozenGene commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-698888096


   > > We could add `--num-threads` to control how many cores to tune. This is important for remote devices like ARM
   > 
   > @FrozenGene that looks interesting. Just to clarify, is this not related to the parameter `n_parallel`, from `autotvm.RPCRunner`, already supported via `--parallel`?
   > 
   
   Yes, it is not related with `n_parallel`, which is to support we have multi devices to tune.
   
   > > We could add `--cpu-mode` to control BIG core or LITTLE core to tune. Imagine remote devices like have A72 + A35. This feature maybe should require us to expose one interface in our AutoTVM to use API `runtime.config_threadpool`.
   > 
   > Yes, that looks useful. Can we add it in a separate PR, considering it requires some infra to be exposed to AutoTVM?
   
   Fine to me.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac merged pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac merged pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494581431



##########
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")

Review comment:
       I aggregated the discussion of remote tuning here and resolve other places to better track the states. Per @FrozenGene 's suggestions of using `--tracker-key` and `--rpc-device-key`, I think `rpc-device-key` is fine, but `tracker-key` is not a key so I would still prefer `rpc-tracker`. I think we can expect them to know the meaning of "tracker" and "device key", because if users need to run RPC runner, they must know how to launch RPC tracker and RPC client in advance.
   
   In summary, here are the action items IMO:
   1. Use `--rpc-tracker=hostname[:port]` and `--rpc-device-key=key-name`.
   2. Have a checker to make sure both are specified or bot are not specified.
   3. Explain how to launch RPC tracker and RPC client in the TVMC tutorial.
   
   @leandron please share your thoughts on this.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-700701519


   This PR is passing CI again.
   
   @comaniac @FrozenGene do you mind having another look?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron edited a comment on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron edited a comment on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-698873119






----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494581431



##########
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")

Review comment:
       I aggregated the discussion of remote tuning here and resolve other places to better track the states. Per @FrozenGene 's suggestions of using `--tracker-key` and `--rpc-device-key`, I think `rpc-device-key` is fine, but `tracker-key` is not a key so I would still prefer `rpc-tracker`. I think we can expect them to know the meaning of "tracker" and "device key", because if users need to run RPC runner, they must know how to launch RPC tracker and RPC client in advance.
   
   In summary, here are the action items IMO:
   1. Use `--rpc-tracker=hostname[:port]` and `--rpc-device-key=key-name`.
   2. Have a checker to make sure both are specified or bot are not specified.
   3. Explain how to launch RPC tracker and RPC client in the TVMC tutorial.
   
   @leandron please share your thoughts on this.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] FrozenGene commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494900132



##########
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")

Review comment:
       `--rpc-key` is acceptable for me.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494903222



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

Review comment:
       I suppose here you mean the empty blank line (212). Please let me know if I misunderstood.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494906582



##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),

Review comment:
       Removed




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-698898950


   > > @FrozenGene that looks interesting. Just to clarify, is this not related to the parameter `n_parallel`, from `autotvm.RPCRunner`, already supported via `--parallel`?
   > 
   > Yes, it is not related with `n_parallel`, which is to support we have multi devices to tune.
   
   Understood. I'm happy to look into it. Can you expand a bit on how that would work, so that we can decide whether adding it here or on a future PR?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494926246



##########
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:
       looking into this




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r493883856



##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),

Review comment:
       Just don't specify this argument then it by default extracts all tasks.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-698873119


   > We could add `--num-threads` to control how many cores to tune. This is important for remote devices like ARM
   
   @FrozenGene that looks interesting. Just to clarify, is this not related to the parameter `n_parallel`, from `autotvm.RPCRunner`, already supported via `--parallel`?
   
   > We could add `--cpu-mode` to control BIG core or LITTLE core to tune. Imagine remote devices like have A72 + A35. This feature maybe should require us to expose one interface in our AutoTVM to use API `runtime.config_threadpool`.
   
   Yes, that looks useful. Can we add it in a separate PR?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r496009665



##########
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:
       Based on the existing tutorials that I had a look, it seems sensible just to completely remove `target_host` from here (and below, line 244), rather than expose it via some `--target-host TARGET_HOST`. What do you think?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r493803255



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

Review comment:
       The current suggested numbers would be 0 for x86 and 1000 for other targets. We might need to reflect the best practice.

##########
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")

Review comment:
       Would be better to put it along with the hostname.

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

Review comment:
       remove this line.

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

Review comment:
       Looks like 0 is the only possible return value, since other cases will directly raise exceptions. If so, it seems not necessary to have a return value.

##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),
+    )
+
+    return tasks
+
+
+def tune_tasks(
+    tasks,
+    log_file,
+    measure_option,
+    tuner,
+    trials,
+    early_stopping=None,
+    tuning_records=None,
+):
+    """Tune a list of tasks and output the history to a log file.
+
+    Parameters
+    ----------
+    tasks : list
+        A list of autotvm.Tasks to tune.
+    log_file : str
+        A file to output the tuning history, in JSON.
+    measure_option : autotvm.measure_option
+        Options to build and run a tuning task.
+    tuner : str
+        Which tuner to use.
+    trials : int
+        The maximum number of tuning trials to perform.
+    early_stopping : int, optional
+        The minimum number of tuning trials to perform.
+        This will be equal to 'trials' if not specified.
+    tuning_records: str, optional
+        Path to the file produced by the tuning, to be used during
+        tuning.
+
+    """
+    if not tasks:
+        logging.warning("there were no tasks found to be tuned")
+        return
+
+    if not early_stopping:
+        early_stopping = trials
+
+    for i, tsk in enumerate(tasks):
+        prefix = "[Task %2d/%2d] " % (i + 1, len(tasks))
+
+        # Create a tuner
+        if tuner in ("xgb", "xgb-rank"):
+            tuner_obj = XGBTuner(tsk, loss_type="rank")
+        elif tuner == "xgb_knob":
+            tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="knob")
+        elif tuner == "ga":
+            tuner_obj = GATuner(tsk, pop_size=50)
+        elif tuner == "random":
+            tuner_obj = RandomTuner(tsk)
+        elif tuner == "gridsearch":
+            tuner_obj = GridSearchTuner(tsk)
+        else:
+            raise TVMCException("invalid tuner: %s " % tuner)
+
+        # If transfer learning is being used, load the existing results
+        if tuning_records and os.path.exists(tuning_records):
+            logging.warning("loading tuning records from %s", tuning_records)
+            start_time = time.time()
+            tuner_obj.load_history(autotvm.record.load_from_file(tuning_records))
+            logging.info("loaded history in %s", str(time.time() - start_time))

Review comment:
       ```suggestion
               logging.info("loaded history in %.2f sec(s)", time.time() - start_time)
   ```

##########
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")

Review comment:
       Build timeout, measurement timeout, or both?

##########
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")

Review comment:
       Put this along with hostname and port. We can even consider creating an argument group for them.

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

Review comment:
       Explicitly say this is the error code.

##########
File path: python/tvm/driver/tvmc/common.py
##########
@@ -17,6 +17,11 @@
 """
 Common utility functions shared by TVMC modules.
 """
+import logging

Review comment:
       Use the standard logging, such as `logger = logging.getLogger("TVMC")`.

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

Review comment:
       We may need to figure out a better way to let user indicate whether to use RPC runner. Maybe we can use a single argument like
   ```
   --rpc-runner=0.0.0.0:9099-rasp4b
   ```
   @FrozenGene do you have an idea?

##########
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:
       Is this correct? What if `target` is `cuda`?

##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),

Review comment:
       Why only extracting conv2d tasks?

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

Review comment:
       remove this line.

##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),
+    )
+
+    return tasks
+
+
+def tune_tasks(
+    tasks,
+    log_file,
+    measure_option,
+    tuner,
+    trials,
+    early_stopping=None,
+    tuning_records=None,
+):
+    """Tune a list of tasks and output the history to a log file.
+
+    Parameters
+    ----------
+    tasks : list
+        A list of autotvm.Tasks to tune.
+    log_file : str
+        A file to output the tuning history, in JSON.
+    measure_option : autotvm.measure_option
+        Options to build and run a tuning task.
+    tuner : str
+        Which tuner to use.
+    trials : int
+        The maximum number of tuning trials to perform.
+    early_stopping : int, optional
+        The minimum number of tuning trials to perform.
+        This will be equal to 'trials' if not specified.
+    tuning_records: str, optional
+        Path to the file produced by the tuning, to be used during
+        tuning.
+
+    """
+    if not tasks:
+        logging.warning("there were no tasks found to be tuned")
+        return
+
+    if not early_stopping:
+        early_stopping = trials
+
+    for i, tsk in enumerate(tasks):
+        prefix = "[Task %2d/%2d] " % (i + 1, len(tasks))
+
+        # Create a tuner
+        if tuner in ("xgb", "xgb-rank"):
+            tuner_obj = XGBTuner(tsk, loss_type="rank")
+        elif tuner == "xgb_knob":
+            tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="knob")
+        elif tuner == "ga":
+            tuner_obj = GATuner(tsk, pop_size=50)
+        elif tuner == "random":
+            tuner_obj = RandomTuner(tsk)
+        elif tuner == "gridsearch":
+            tuner_obj = GridSearchTuner(tsk)
+        else:
+            raise TVMCException("invalid tuner: %s " % tuner)
+
+        # If transfer learning is being used, load the existing results
+        if tuning_records and os.path.exists(tuning_records):
+            logging.warning("loading tuning records from %s", tuning_records)

Review comment:
       `info` should be a proper logging level here.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r495446600



##########
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")

Review comment:
       I pushed a new commit that implements `--rpc-tracker` and `--rpc-key`, as discussed here.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-tvm] FrozenGene commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-698902831


   > > > @FrozenGene that looks interesting. Just to clarify, is this not related to the parameter `n_parallel`, from `autotvm.RPCRunner`, already supported via `--parallel`?
   > > 
   > > 
   > > Yes, it is not related with `n_parallel`, which is to support we have multi devices to tune.
   > 
   > Understood. I'm happy to look into it. Can you expand a bit on how that would work, so that we can decide whether adding it here or on a future PR?
   
   You need to do the similar thing like we control BIG / LITTLE core via `runtime.config_threadpool`, this api could let us control how many cores and which cpu mode we like. So I am fine leave this in the future pr together with control BIG / LITTLE cpu mode.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r495276024



##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -0,0 +1,294 @@
+# 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 platform
+import logging
+import time
+
+from tvm import autotvm
+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 logger, 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")
+
+    is_x86 = platform.machine() in ("i386", "AMD64", "x86_64")
+    parser.add_argument(
+        "--min-repeat-ms",
+        default=0 if is_x86 else 1000,

Review comment:
       It's not that complicate tho. We can simply have `0 if target.keys[0] == "cpu" else 1000`. Since we have enabled clflush and it can make the measurement much more accurate, we don't need to set this value to 1000. However, it is only for x86, so we just need to check whether the first target key is for x86 (i.e. `cpu`).




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-698873119






----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494908119



##########
File path: python/tvm/driver/tvmc/common.py
##########
@@ -17,6 +17,11 @@
 """
 Common utility functions shared by TVMC modules.
 """
+import logging

Review comment:
       Yep. I also added a note to migrate the compiler module to the common logger, in a separate PR.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron edited a comment on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron edited a comment on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-698873119






----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r493883417



##########
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")

Review comment:
       Fair enough. Then I'd suggest having a post-processing to check if `rpc-tracker` and `device-key` (need to think about the naming a bit more) are given or none of them. Similar to [this case](https://stackoverflow.com/questions/14350343/python-argparse-either-both-optional-arguments-or-else-neither-one).




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r493874196



##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),

Review comment:
       I thought about that too. Do we have any documentation that can point to other tasks we might be interested? Interestingly on tutorials we only extract convolutions.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r495267600



##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -0,0 +1,294 @@
+# 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 platform
+import logging
+import time
+
+from tvm import autotvm
+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 logger, 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")
+
+    is_x86 = platform.machine() in ("i386", "AMD64", "x86_64")
+    parser.add_argument(
+        "--min-repeat-ms",
+        default=0 if is_x86 else 1000,

Review comment:
       You're correct. I'm just wondering how can we have a mapping from "target string" to architecture, including the use cases for RPC tracking, in which supposedly the remote would need to provide the architecture somehow. Any ideas?
   
   Suggestion/brainstorming here: In case we don't have clever way to collect an architecture for the target (that we can shortly explain in the help), I'd suggest we set default as `0` for `LocalRunner` and `1000` for `RPCRunner`, and keep it customisable via command line.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494902856



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

Review comment:
       I suppose here you mean the empty blank line (134). Please let me know if I misunderstood.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] FrozenGene commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r493998400



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

Review comment:
       I think we could check if `--rpc-device-key` (`--tracker-key` is in this pr, suggest naming it into `--rpc-device-key` so that we know it explicitly) is `None` or not. If it is not `None`, we are running RPC. 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r495112191



##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -0,0 +1,294 @@
+# 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 platform
+import logging
+import time
+
+from tvm import autotvm
+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 logger, 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")
+
+    is_x86 = platform.machine() in ("i386", "AMD64", "x86_64")
+    parser.add_argument(
+        "--min-repeat-ms",
+        default=0 if is_x86 else 1000,

Review comment:
       The value should be determined by the target platform instead of the host, so it should be based on the `target`. You probably need to post-process this value.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] FrozenGene commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r493997962



##########
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")

Review comment:
       How about name `--tracker-key` into `--rpc-device-key`? 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-700837093


   Thanks @leandron @FrozenGene 


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494890474



##########
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")

Review comment:
       I think it is good to have a more meaningful name, replacing the generic `--hostname / --port / --tracker-key` with something with more context like `--rpc-****`.
   
   Just in the interest of making the parameters shorter and easier to remember, do you think `--rpc-key` would be acceptable, rather than `--rpc-device-key`? I find that two-word mnemonic are usually simpler to remember. I'm not sure it preserves the whole context you might want to give it.
   
   Example of a command line:
   ```
   tvmc tune --output log1.txt --target="llvm" --rpc-tracker=192.168.0.150:9191 --rpc-key=rasp4b resnet50-v2-7.onnx
   ```
   
   PS: I fully agree with the extra check (2) and the tutorial points (3) above.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494890474



##########
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")

Review comment:
       I think it is good to have a more meaningful name, replacing the generic `--hostname / --port / --tracker-key` with something with more context like `--rpc-****`.
   
   Just in the interest of making the parameters shorter and easier to remember, do you think `--rpc-key` would be acceptable, rather than `--rpc-device-key`? I find that two-word mnemonic are usually simpler to remember. I'm not sure it preserves the whole context you might want to give it.
   
   Example of a command line:
   ```
   tvmc tune --output log1.txt --target="llvm" --rpc-tracker=192.168.0.150:9191 --rpc-key=rasp4b resnet50-v2-7.onnx
   ```




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r493871858



##########
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")

Review comment:
       I agree that merging a host name with optional port in a single parameter, e.g. `hostname[:port]` would be beneficial. However, I think that also adding the key to the same network-related parameter, would be a bit more complicated and obscure, in terms of syntax, making it harder for an end user to grasp.
   
   Based on that, would you agree that for now we keep `hostname[:port]` in a single field, and the keep the key in a separate parameter?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r494890474



##########
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")

Review comment:
       I think it is good to have a more meaningful name, replacing the generic `--hostname / --port / --tracker-key` with something with more context like `--rpc-****`.
   
   Just in the interest of making the parameters shorter and easier to remember, do you think `--rpc-key` would be acceptable, rather than `--rpc-device-key`? I find that two-word mnemonic are usually simpler to remember. I'm not sure it preserves the whole context you might want to give it.
   
   Example of a command line:
   ```
   tvmc tune --output log1.txt --target="llvm" --rpc-tracker=192.168.0.150:9191 --rpc-key=rasp4b resnet50-v2-7.onnx
   ```

##########
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")

Review comment:
       I think it is good to have a more meaningful name, replacing the generic `--hostname / --port / --tracker-key` with something with more context like `--rpc-****`.
   
   Just in the interest of making the parameters shorter and easier to remember, do you think `--rpc-key` would be acceptable, rather than `--rpc-device-key`? I find that two-word mnemonic are usually simpler to remember. I'm not sure it preserves the whole context you might want to give it.
   
   Example of a command line:
   ```
   tvmc tune --output log1.txt --target="llvm" --rpc-tracker=192.168.0.150:9191 --rpc-key=rasp4b resnet50-v2-7.onnx
   ```
   
   PS: I fully agree with the extra check (2) and the tutorial points (3) above.

##########
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")

Review comment:
       It refers to the compilation timeout. I've adjusted the help message to make it clear.

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

Review comment:
       I suppose here you mean the empty blank line (134). Please let me know if I misunderstood.

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

Review comment:
       I suppose here you mean the empty blank line (212). Please let me know if I misunderstood.

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

Review comment:
       Makes sense. Removed.

##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),

Review comment:
       Removed

##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),
+    )
+
+    return tasks
+
+
+def tune_tasks(
+    tasks,
+    log_file,
+    measure_option,
+    tuner,
+    trials,
+    early_stopping=None,
+    tuning_records=None,
+):
+    """Tune a list of tasks and output the history to a log file.
+
+    Parameters
+    ----------
+    tasks : list
+        A list of autotvm.Tasks to tune.
+    log_file : str
+        A file to output the tuning history, in JSON.
+    measure_option : autotvm.measure_option
+        Options to build and run a tuning task.
+    tuner : str
+        Which tuner to use.
+    trials : int
+        The maximum number of tuning trials to perform.
+    early_stopping : int, optional
+        The minimum number of tuning trials to perform.
+        This will be equal to 'trials' if not specified.
+    tuning_records: str, optional
+        Path to the file produced by the tuning, to be used during
+        tuning.
+
+    """
+    if not tasks:
+        logging.warning("there were no tasks found to be tuned")
+        return
+
+    if not early_stopping:
+        early_stopping = trials
+
+    for i, tsk in enumerate(tasks):
+        prefix = "[Task %2d/%2d] " % (i + 1, len(tasks))
+
+        # Create a tuner
+        if tuner in ("xgb", "xgb-rank"):
+            tuner_obj = XGBTuner(tsk, loss_type="rank")
+        elif tuner == "xgb_knob":
+            tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="knob")
+        elif tuner == "ga":
+            tuner_obj = GATuner(tsk, pop_size=50)
+        elif tuner == "random":
+            tuner_obj = RandomTuner(tsk)
+        elif tuner == "gridsearch":
+            tuner_obj = GridSearchTuner(tsk)
+        else:
+            raise TVMCException("invalid tuner: %s " % tuner)
+
+        # If transfer learning is being used, load the existing results
+        if tuning_records and os.path.exists(tuning_records):
+            logging.warning("loading tuning records from %s", tuning_records)

Review comment:
       Yep. Done

##########
File path: python/tvm/driver/tvmc/common.py
##########
@@ -17,6 +17,11 @@
 """
 Common utility functions shared by TVMC modules.
 """
+import logging

Review comment:
       Yep. I also added a note to migrate the compiler module to the common logger, in a separate PR.

##########
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
+
+    if alter_layout:
+        mod = common.convert_graph_layout(mod, alter_layout)
+
+    tasks = autotvm.task.extract_from_program(
+        mod["main"],
+        target=target,
+        target_host=target_host,
+        params=params,
+        ops=(relay.op.get("nn.conv2d"),),
+    )
+
+    return tasks
+
+
+def tune_tasks(
+    tasks,
+    log_file,
+    measure_option,
+    tuner,
+    trials,
+    early_stopping=None,
+    tuning_records=None,
+):
+    """Tune a list of tasks and output the history to a log file.
+
+    Parameters
+    ----------
+    tasks : list
+        A list of autotvm.Tasks to tune.
+    log_file : str
+        A file to output the tuning history, in JSON.
+    measure_option : autotvm.measure_option
+        Options to build and run a tuning task.
+    tuner : str
+        Which tuner to use.
+    trials : int
+        The maximum number of tuning trials to perform.
+    early_stopping : int, optional
+        The minimum number of tuning trials to perform.
+        This will be equal to 'trials' if not specified.
+    tuning_records: str, optional
+        Path to the file produced by the tuning, to be used during
+        tuning.
+
+    """
+    if not tasks:
+        logging.warning("there were no tasks found to be tuned")
+        return
+
+    if not early_stopping:
+        early_stopping = trials
+
+    for i, tsk in enumerate(tasks):
+        prefix = "[Task %2d/%2d] " % (i + 1, len(tasks))
+
+        # Create a tuner
+        if tuner in ("xgb", "xgb-rank"):
+            tuner_obj = XGBTuner(tsk, loss_type="rank")
+        elif tuner == "xgb_knob":
+            tuner_obj = XGBTuner(tsk, loss_type="rank", feature_type="knob")
+        elif tuner == "ga":
+            tuner_obj = GATuner(tsk, pop_size=50)
+        elif tuner == "random":
+            tuner_obj = RandomTuner(tsk)
+        elif tuner == "gridsearch":
+            tuner_obj = GridSearchTuner(tsk)
+        else:
+            raise TVMCException("invalid tuner: %s " % tuner)
+
+        # If transfer learning is being used, load the existing results
+        if tuning_records and os.path.exists(tuning_records):
+            logging.warning("loading tuning records from %s", tuning_records)
+            start_time = time.time()
+            tuner_obj.load_history(autotvm.record.load_from_file(tuning_records))
+            logging.info("loaded history in %s", str(time.time() - start_time))

Review comment:
       Done

##########
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:
       looking into this




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] comaniac commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
comaniac commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r496090459



##########
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:
       Let's expose it via `--target-host` with `llvm` as its default value.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-700281819


   Re-triggering CI


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] FrozenGene commented on pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#issuecomment-698888096






----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-tvm] leandron commented on a change in pull request #6537: [tvmc] Introduce 'tune' subcommand (part 3/4)

Posted by GitBox <gi...@apache.org>.
leandron commented on a change in pull request #6537:
URL: https://github.com/apache/incubator-tvm/pull/6537#discussion_r495441041



##########
File path: python/tvm/driver/tvmc/autotuner.py
##########
@@ -0,0 +1,294 @@
+# 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 platform
+import logging
+import time
+
+from tvm import autotvm
+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 logger, 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")
+
+    is_x86 = platform.machine() in ("i386", "AMD64", "x86_64")
+    parser.add_argument(
+        "--min-repeat-ms",
+        default=0 if is_x86 else 1000,

Review comment:
       Fair enough. I'll update accordingly.




----------------------------------------------------------------
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:
users@infra.apache.org