|
| 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 | +"""Meta schedule tuning utilities for Hexagon.""" |
| 18 | +import os |
| 19 | +import tempfile |
| 20 | +from typing import Callable, List, Optional |
| 21 | +from tvm.contrib.popen_pool import PopenPoolExecutor |
| 22 | +from tvm.meta_schedule.utils import cpu_count, derived_object |
| 23 | +from tvm.meta_schedule.builder import LocalBuilder |
| 24 | +from tvm.meta_schedule.runner import ( |
| 25 | + EvaluatorConfig, |
| 26 | + RunnerInput, |
| 27 | + RunnerFuture, |
| 28 | + PyRunner, |
| 29 | +) |
| 30 | +from tvm.meta_schedule.runner.rpc_runner import ( |
| 31 | + default_alloc_argument, |
| 32 | + default_run_evaluator, |
| 33 | + RPCRunnerFuture, |
| 34 | +) |
| 35 | + |
| 36 | +from .build import HexagonLauncherRPC |
| 37 | +from .tools import export_module |
| 38 | + |
| 39 | + |
| 40 | +@derived_object |
| 41 | +class HexagonRPCRunner(PyRunner): |
| 42 | + """RPCRunner for Hexagon. See the documentation of RPCRunner for more details.""" |
| 43 | + |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + hexagon_launcher: HexagonLauncherRPC, |
| 47 | + evaluator_config: Optional[EvaluatorConfig] = None, |
| 48 | + cooldown_sec: float = 0.0, |
| 49 | + alloc_repeat: int = 1, |
| 50 | + max_workers: Optional[int] = None, |
| 51 | + initializer: Optional[Callable[[], None]] = None, |
| 52 | + ): |
| 53 | + """ |
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + hexagon_launcher : HexagonLauncherRPC |
| 57 | + The RPC launcher for Hexagon. It is needed for creating hexagon.Session |
| 58 | + object inside the worker function. |
| 59 | + evaluator_config: EvaluatorConfig |
| 60 | + The evaluator configuration. |
| 61 | + cooldown_sec: float |
| 62 | + The cooldown in seconds. |
| 63 | + alloc_repeat: int |
| 64 | + The number of times to random fill the allocation. |
| 65 | + max_workers: Optional[int] = None |
| 66 | + The maximum number of connections. Defaults to number of logical CPU cores. |
| 67 | + initializer: Optional[Callable[[], None]] |
| 68 | + The initializer function. |
| 69 | + """ |
| 70 | + |
| 71 | + super().__init__() |
| 72 | + self.hexagon_launcher = hexagon_launcher |
| 73 | + self.evaluator_config = EvaluatorConfig._normalized(evaluator_config) |
| 74 | + self.cooldown_sec = cooldown_sec |
| 75 | + self.alloc_repeat = alloc_repeat |
| 76 | + if max_workers is None: |
| 77 | + max_workers = cpu_count(logical=True) |
| 78 | + self.pool = PopenPoolExecutor( |
| 79 | + max_workers=max_workers, |
| 80 | + timeout=100, |
| 81 | + initializer=initializer, |
| 82 | + ) |
| 83 | + |
| 84 | + def run(self, runner_inputs: List[RunnerInput]) -> List[RunnerFuture]: |
| 85 | + results = [] |
| 86 | + for runner_input in runner_inputs: |
| 87 | + future = RPCRunnerFuture( |
| 88 | + future=self.pool.submit( |
| 89 | + _worker_func, |
| 90 | + self.hexagon_launcher, |
| 91 | + self.evaluator_config, |
| 92 | + self.alloc_repeat, |
| 93 | + str(runner_input.artifact_path), |
| 94 | + tuple(arg_info.as_json() for arg_info in runner_input.args_info), |
| 95 | + ), |
| 96 | + timeout_sec=100, |
| 97 | + ) |
| 98 | + results.append(future) |
| 99 | + return results |
| 100 | + |
| 101 | + |
| 102 | +def _worker_func(hexagon_launcher, evaluator_config, alloc_repeat, artifact_path, args_info): |
| 103 | + with hexagon_launcher.start_session() as session: |
| 104 | + device = session.device |
| 105 | + _, remote_path = os.path.split(artifact_path) |
| 106 | + uploaded = session.upload(artifact_path, remote_path) |
| 107 | + rt_mod = session.load_module(uploaded) |
| 108 | + repeated_args = default_alloc_argument( |
| 109 | + session, |
| 110 | + device, |
| 111 | + args_info, |
| 112 | + alloc_repeat, |
| 113 | + ) |
| 114 | + costs = default_run_evaluator( |
| 115 | + session, |
| 116 | + rt_mod, |
| 117 | + device, |
| 118 | + evaluator_config, |
| 119 | + repeated_args, |
| 120 | + ) |
| 121 | + return costs |
| 122 | + |
| 123 | + |
| 124 | +def get_hexagon_local_builder(): |
| 125 | + """Return Hexagon-compatible Builder for meta schedule.""" |
| 126 | + |
| 127 | + def export_func(mod): |
| 128 | + binary_path = export_module(mod, tempfile.mkdtemp()) |
| 129 | + return str(binary_path) |
| 130 | + |
| 131 | + return LocalBuilder(f_export=export_func) |
| 132 | + |
| 133 | + |
| 134 | +def get_hexagon_rpc_runner( |
| 135 | + hexagon_launcher: HexagonLauncherRPC, number=3, repeat=1, min_repeat_ms=100 |
| 136 | +): |
| 137 | + """Return Hexagon-compatible RPC Runner for meta schedule. |
| 138 | +
|
| 139 | + Parameters |
| 140 | + ---------- |
| 141 | + hexagon_launcher : HexagonLauncherRPC |
| 142 | + The RPC launcher for Hexagon. |
| 143 | + number: int |
| 144 | + The number of times to run this function for taking average. |
| 145 | + We call these runs as one `repeat` of measurement. |
| 146 | + repeat: int |
| 147 | + The number of times to repeat the measurement. |
| 148 | + In total, the function will be invoked (1 + number x repeat) times, |
| 149 | + where the first one is warm up and will be discarded. |
| 150 | + The returned result contains `repeat` costs, |
| 151 | + each of which is an average of `number` costs. |
| 152 | + min_repeat_ms: int |
| 153 | + Minimum repeat time in ms. if the execution latency is too short, |
| 154 | + increase the number of runs to the given time (in ms) to reduce the measurement error. |
| 155 | + """ |
| 156 | + evaluator_config = EvaluatorConfig( |
| 157 | + number=number, |
| 158 | + repeat=repeat, |
| 159 | + min_repeat_ms=min_repeat_ms, |
| 160 | + enable_cpu_cache_flush=False, |
| 161 | + ) |
| 162 | + |
| 163 | + return HexagonRPCRunner( |
| 164 | + hexagon_launcher, |
| 165 | + evaluator_config, |
| 166 | + ) |
0 commit comments