|
| 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 | + |
| 18 | +""" |
| 19 | +User interface for TVM Auto-scheduler. |
| 20 | +
|
| 21 | +The basic schedule search process for TVM Auto-scheduler is designed to be: |
| 22 | +`Program sampling` -> `Performance Tuning`. |
| 23 | +
|
| 24 | +In `Program sampling`, we use some predefined precise or heuristic rules to generate several |
| 25 | +initial schedules. Based on these initial starting points, we perform `Performance Tuning` which |
| 26 | +uses cost model based evolutionary search to select schedules with the best performance. |
| 27 | +
|
| 28 | +Candidate schedules are measured against the specific hardware target. |
| 29 | +""" |
| 30 | + |
| 31 | +import tvm._ffi |
| 32 | +from tvm.runtime import Object |
| 33 | +from .measure import LocalBuilder, LocalRunner |
| 34 | +from . import _ffi_api |
| 35 | + |
| 36 | + |
| 37 | +@tvm._ffi.register_object("auto_schedule.HardwareParams") |
| 38 | +class HardwareParams(Object): |
| 39 | + """ The parameters of target hardware used to guide the search policy |
| 40 | +
|
| 41 | + TODO(jcf94): This is considered to be merged with the new Target specification: |
| 42 | + https://discuss.tvm.ai/t/rfc-tvm-target-specification/6844 |
| 43 | +
|
| 44 | + Parameters |
| 45 | + ---------- |
| 46 | + num_cores : int |
| 47 | + The number of device cores. |
| 48 | + vector_unit_bytes : int |
| 49 | + The width of vector units in bytes. |
| 50 | + cache_line_bytes : int |
| 51 | + The size of cache line in bytes. |
| 52 | + """ |
| 53 | + def __init__(self, num_cores, vector_unit_bytes, cache_line_bytes): |
| 54 | + self.__init_handle_by_constructor__(_ffi_api.HardwareParams, num_cores, |
| 55 | + vector_unit_bytes, cache_line_bytes) |
| 56 | + |
| 57 | + |
| 58 | +@tvm._ffi.register_object("auto_schedule.SearchTask") |
| 59 | +class SearchTask(Object): |
| 60 | + """ The computation information and hardware parameters for a specific schedule search task. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + dag : ComputeDAG |
| 65 | + The ComputeDAG for the corresponding compute declaration. |
| 66 | + workload_key : str |
| 67 | + The workload key for the corresponding compute declaration. |
| 68 | + target : tvm.target.Target |
| 69 | + The target device of this search task. |
| 70 | + target_host : Optional[tvm.target.Target] |
| 71 | + The target host device of this search task. |
| 72 | + hardware_params : Optional[HardwareParams] |
| 73 | + Hardware parameters used in this search task. |
| 74 | + """ |
| 75 | + def __init__(self, dag, workload_key, target, target_host=None, |
| 76 | + hardware_params=None): |
| 77 | + self.__init_handle_by_constructor__(_ffi_api.SearchTask, dag, |
| 78 | + workload_key, target, target_host, |
| 79 | + hardware_params) |
| 80 | + |
| 81 | + |
| 82 | +@tvm._ffi.register_object("auto_schedule.SearchPolicy") |
| 83 | +class SearchPolicy(Object): |
| 84 | + """ The base class of search policies. """ |
| 85 | + |
| 86 | + |
| 87 | +@tvm._ffi.register_object("auto_schedule.EmptyPolicy") |
| 88 | +class EmptyPolicy(SearchPolicy): |
| 89 | + """ This is an example empty search policy which will always generate |
| 90 | + the init state of ComputeDAG. |
| 91 | + """ |
| 92 | + def __init__(self): |
| 93 | + self.__init_handle_by_constructor__(_ffi_api.EmptyPolicy) |
| 94 | + |
| 95 | + |
| 96 | +@tvm._ffi.register_object("auto_schedule.TuningOptions") |
| 97 | +class TuningOptions(Object): |
| 98 | + """ This controls the options of performance tuning. |
| 99 | +
|
| 100 | + Parameters |
| 101 | + ---------- |
| 102 | + num_measure_trials: int = 0 |
| 103 | + The number of measurement trials. |
| 104 | + The search policy measures `num_measure_trials` schedules in total and returns the best one |
| 105 | + among them. |
| 106 | + With `num_measure_trials` == 0, the policy will do the schedule search but won't involve |
| 107 | + measurement. This can be used to get a runnable schedule quickly without auto-tuning. |
| 108 | + early_stopping: Optional[int] |
| 109 | + Stop the tuning early if getting no improvement after n measurements. |
| 110 | + num_measures_per_round: int = 64 |
| 111 | + The number of schedules to be measured at each search round. |
| 112 | + The whole schedule search process will try a total number of `num_measure_trials` in several |
| 113 | + rounds. |
| 114 | + verbose: int = 1 |
| 115 | + Verbosity level. 0 for silent, 1 to output information during schedule search. |
| 116 | + builder: Union[ProgramBuilder, str] = 'local' |
| 117 | + ProgramBuilder which builds the program. |
| 118 | + runner: Union[ProgramRunner, str] = 'local' |
| 119 | + ProgramRunner which runs the program and measures time costs. |
| 120 | + measure_callbacks: Optional[List[MeasureCallback]] |
| 121 | + Callback functions called after each measurement. |
| 122 | + Candidates: |
| 123 | + - auto_schedule.RecordToFile |
| 124 | + pre_search_callbacks: Optional[List[SearchCallback]] |
| 125 | + Callback functions called before the search process. |
| 126 | + Candidates: |
| 127 | + - auto_schedule.PreloadMeasuredStates |
| 128 | + - auto_schedule.PreloadCustomSketchRule |
| 129 | + TODO(jcf94): Add these implementation in later PRs. |
| 130 | + """ |
| 131 | + def __init__(self, num_measure_trials=0, early_stopping=None, num_measures_per_round=64, |
| 132 | + verbose=1, builder='local', runner='local', measure_callbacks=None, |
| 133 | + pre_search_callbacks=None): |
| 134 | + if isinstance(builder, str): |
| 135 | + if builder == 'local': |
| 136 | + builder = LocalBuilder() |
| 137 | + else: |
| 138 | + raise ValueError("Invalid builder: " + builder) |
| 139 | + elif not isinstance(builder, tvm.auto_schedule.measure.ProgramBuilder): |
| 140 | + raise ValueError("Invalid builder: " + builder + |
| 141 | + " . TuningOptions expects a ProgramBuilder or string.") |
| 142 | + |
| 143 | + if isinstance(runner, str): |
| 144 | + if runner == 'local': |
| 145 | + runner = LocalRunner() |
| 146 | + else: |
| 147 | + raise ValueError("Invalid runner: " + runner) |
| 148 | + elif not isinstance(runner, tvm.auto_schedule.measure.ProgramRunner): |
| 149 | + raise ValueError("Invalid runner: " + runner + |
| 150 | + " . TuningOptions expects a ProgramRunner or string.") |
| 151 | + |
| 152 | + self.__init_handle_by_constructor__( |
| 153 | + _ffi_api.TuningOptions, num_measure_trials, early_stopping if early_stopping else -1, |
| 154 | + num_measures_per_round, verbose, builder, runner, measure_callbacks, |
| 155 | + pre_search_callbacks) |
| 156 | + |
| 157 | + |
| 158 | +def auto_schedule(task, search_policy='default', tuning_options=None): |
| 159 | + """ Do auto scheduling for a computation declaration. |
| 160 | +
|
| 161 | + The task parameter can be a `string` as workload_key, or directly |
| 162 | + passing a `SearchTask` as input. |
| 163 | +
|
| 164 | + Parameters |
| 165 | + ---------- |
| 166 | + task : SearchTask |
| 167 | + The SearchTask for the computation declaration. |
| 168 | + search_policy : Union[SearchPolicy, str] = 'default' |
| 169 | + The search policy to be used for schedule search. |
| 170 | + tuning_options : Optional[TuningOptions] |
| 171 | + Tuning and measurement options. |
| 172 | +
|
| 173 | + Returns |
| 174 | + ------- |
| 175 | + A `te.schedule` and the a list of `te.Tensor` to be used in `tvm.lower` or `tvm.build`. |
| 176 | + """ |
| 177 | + if not isinstance(task, SearchTask): |
| 178 | + raise ValueError("Invalid task: " + task + |
| 179 | + " . `auto_schedule.auto_schedule` expects a SearchTask.") |
| 180 | + |
| 181 | + if isinstance(search_policy, str): |
| 182 | + if search_policy == 'default': |
| 183 | + # TODO(jcf94): This is an example policy for minimum system, will be upgrated to |
| 184 | + # formal search policy later. |
| 185 | + search_policy = EmptyPolicy() |
| 186 | + else: |
| 187 | + raise ValueError("Invalid search policy: " + search_policy) |
| 188 | + elif not isinstance(search_policy, SearchPolicy): |
| 189 | + raise ValueError("Invalid search policy: " + search_policy + |
| 190 | + " . `auto_schedule.auto_schedule` expects a SearchPolicy or a string.") |
| 191 | + |
| 192 | + sch, tensors = _ffi_api.AutoSchedule(task, search_policy, |
| 193 | + tuning_options if tuning_options else TuningOptions()) |
| 194 | + return sch, tensors |
0 commit comments