|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# pylint: disable=missing-docstring |
| 18 | +import argparse |
| 19 | +import json |
| 20 | +import os |
| 21 | + |
| 22 | +import numpy as np # type: ignore |
| 23 | +import tvm |
| 24 | +from tvm import auto_scheduler |
| 25 | +from tvm import meta_schedule as ms |
| 26 | +from tvm import relay |
| 27 | +from tvm.meta_schedule.testing.custom_builder_runner import run_module_via_rpc |
| 28 | +from tvm.meta_schedule.testing.relay_workload import get_network |
| 29 | + |
| 30 | + |
| 31 | +def _parse_args(): |
| 32 | + args = argparse.ArgumentParser() |
| 33 | + args.add_argument( |
| 34 | + "--workload", |
| 35 | + type=str, |
| 36 | + required=True, |
| 37 | + ) |
| 38 | + args.add_argument( |
| 39 | + "--input-shape", |
| 40 | + type=str, |
| 41 | + required=True, |
| 42 | + ) |
| 43 | + args.add_argument( |
| 44 | + "--target", |
| 45 | + type=str, |
| 46 | + required=True, |
| 47 | + ) |
| 48 | + args.add_argument( |
| 49 | + "--num-trials", |
| 50 | + type=int, |
| 51 | + required=True, |
| 52 | + ) |
| 53 | + args.add_argument( |
| 54 | + "--rpc-host", |
| 55 | + type=str, |
| 56 | + required=True, |
| 57 | + ) |
| 58 | + args.add_argument( |
| 59 | + "--rpc-port", |
| 60 | + type=int, |
| 61 | + required=True, |
| 62 | + ) |
| 63 | + args.add_argument( |
| 64 | + "--rpc-key", |
| 65 | + type=str, |
| 66 | + required=True, |
| 67 | + ) |
| 68 | + args.add_argument( |
| 69 | + "--rpc-workers", |
| 70 | + type=int, |
| 71 | + required=True, |
| 72 | + ) |
| 73 | + args.add_argument( |
| 74 | + "--log-dir", |
| 75 | + type=str, |
| 76 | + required=True, |
| 77 | + ) |
| 78 | + args.add_argument( |
| 79 | + "--cache-dir", |
| 80 | + type=str, |
| 81 | + default=None, |
| 82 | + ) |
| 83 | + parsed = args.parse_args() |
| 84 | + parsed.target = tvm.target.Target(parsed.target) |
| 85 | + parsed.input_shape = json.loads(parsed.input_shape) |
| 86 | + parsed.rpc_config = ms.runner.RPCConfig( |
| 87 | + tracker_host=parsed.rpc_host, |
| 88 | + tracker_port=parsed.rpc_port, |
| 89 | + tracker_key=parsed.rpc_key, |
| 90 | + session_timeout_sec=3600, |
| 91 | + ) |
| 92 | + return parsed |
| 93 | + |
| 94 | + |
| 95 | +ARGS = _parse_args() |
| 96 | + |
| 97 | + |
| 98 | +def main(): |
| 99 | + log_file = os.path.join(ARGS.log_dir, f"{ARGS.workload}.json") |
| 100 | + |
| 101 | + runner = auto_scheduler.RPCRunner( |
| 102 | + key=ARGS.rpc_key, |
| 103 | + host=ARGS.rpc_host, |
| 104 | + port=ARGS.rpc_port, |
| 105 | + n_parallel=ARGS.rpc_workers, |
| 106 | + number=3, |
| 107 | + repeat=1, |
| 108 | + min_repeat_ms=100, # TODO |
| 109 | + enable_cpu_cache_flush=False, # TODO |
| 110 | + ) |
| 111 | + |
| 112 | + if ARGS.target.kind.name == "llvm": |
| 113 | + hardware_params = auto_scheduler.HardwareParams( |
| 114 | + num_cores=int(ARGS.target.attrs["num-cores"]), |
| 115 | + target=ARGS.target, |
| 116 | + ) |
| 117 | + elif ARGS.target.kind.name == "cuda": |
| 118 | + hardware_params = auto_scheduler.HardwareParams( |
| 119 | + num_cores=-1, |
| 120 | + vector_unit_bytes=16, |
| 121 | + cache_line_bytes=64, |
| 122 | + max_shared_memory_per_block=int(ARGS.target.attrs["max_shared_memory_per_block"]), |
| 123 | + max_threads_per_block=int(ARGS.target.attrs["max_threads_per_block"]), |
| 124 | + # The value `max_local_memory_per_block` is not used in AutoScheduler, |
| 125 | + # but is required by the API. |
| 126 | + max_local_memory_per_block=12345678, |
| 127 | + max_vthread_extent=8, |
| 128 | + warp_size=32, |
| 129 | + ) |
| 130 | + else: |
| 131 | + raise NotImplementedError(f"Unsupported target {ARGS.target}") |
| 132 | + mod, params, (input_name, input_shape, input_dtype) = get_network( |
| 133 | + ARGS.workload, |
| 134 | + ARGS.input_shape, |
| 135 | + cache_dir=ARGS.cache_dir, |
| 136 | + ) |
| 137 | + print(f"Workload: {ARGS.workload}") |
| 138 | + print(f" input_name: {input_name}") |
| 139 | + print(f" input_shape: {input_shape}") |
| 140 | + print(f" input_dtype: {input_dtype}") |
| 141 | + tasks, task_weights = auto_scheduler.extract_tasks( |
| 142 | + mod["main"], |
| 143 | + params, |
| 144 | + target=ARGS.target, |
| 145 | + hardware_params=hardware_params, |
| 146 | + ) |
| 147 | + for idx, (task, task_weight) in enumerate(zip(tasks, task_weights)): |
| 148 | + print(f"==== Task {idx}: {task.desc} (weight {task_weight} key: {task.workload_key}) =====") |
| 149 | + print(task.compute_dag) |
| 150 | + |
| 151 | + tuner = auto_scheduler.TaskScheduler(tasks, task_weights) |
| 152 | + tuner.tune( |
| 153 | + auto_scheduler.TuningOptions( |
| 154 | + num_measure_trials=ARGS.num_trials, |
| 155 | + runner=runner, |
| 156 | + measure_callbacks=[ |
| 157 | + auto_scheduler.RecordToFile(log_file), |
| 158 | + ], |
| 159 | + ) |
| 160 | + ) |
| 161 | + |
| 162 | + with auto_scheduler.ApplyHistoryBest(log_file): |
| 163 | + with tvm.transform.PassContext( |
| 164 | + opt_level=3, |
| 165 | + config={"relay.backend.use_auto_scheduler": True}, |
| 166 | + ): |
| 167 | + lib = relay.build( |
| 168 | + mod, |
| 169 | + target=ARGS.target, |
| 170 | + params=params, |
| 171 | + ) |
| 172 | + |
| 173 | + if input_dtype.startswith("float"): |
| 174 | + input_data = np.random.uniform(size=input_shape).astype(input_dtype) |
| 175 | + else: |
| 176 | + input_data = np.random.randint(low=0, high=10000, size=input_shape, dtype=input_dtype) |
| 177 | + |
| 178 | + def f_timer(rt_mod, dev, input_data): |
| 179 | + # pylint: disable=import-outside-toplevel |
| 180 | + from tvm.contrib.graph_executor import GraphModule |
| 181 | + |
| 182 | + # pylint: enable=import-outside-toplevel |
| 183 | + |
| 184 | + mod = GraphModule(rt_mod["default"](dev)) |
| 185 | + mod.set_input(input_name, input_data) |
| 186 | + ftimer = mod.module.time_evaluator( |
| 187 | + "run", |
| 188 | + dev, |
| 189 | + min_repeat_ms=500, |
| 190 | + repeat=3, |
| 191 | + ) |
| 192 | + return list(np.array(ftimer().results)) |
| 193 | + |
| 194 | + results = run_module_via_rpc( |
| 195 | + rpc_config=ARGS.rpc_config, |
| 196 | + lib=lib, |
| 197 | + dev_type=ARGS.target.kind.name, |
| 198 | + args=[input_data], |
| 199 | + continuation=f_timer, |
| 200 | + ) |
| 201 | + |
| 202 | + print(results) |
| 203 | + |
| 204 | + |
| 205 | +if __name__ == "__main__": |
| 206 | + main() |
0 commit comments