forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MetaSchedule] Update scripts for subgraph tuning (apache#10501)
- Loading branch information
Showing
10 changed files
with
310 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
python/tvm/meta_schedule/testing/run_subgraph_auto_scheduler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# 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 limitatios | ||
# under the License. | ||
# pylint: disable=missing-docstring | ||
import argparse | ||
import os | ||
|
||
import tvm | ||
from tvm import auto_scheduler | ||
from tvm.meta_schedule.runner import RPCConfig | ||
from tvm.meta_schedule.testing.te_workload import CONFIGS | ||
|
||
|
||
def _parse_args(): | ||
args = argparse.ArgumentParser() | ||
args.add_argument( | ||
"--workload", | ||
type=str, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--target", | ||
type=str, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--num-trials", | ||
type=int, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--rpc-host", | ||
type=str, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--rpc-port", | ||
type=int, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--rpc-key", | ||
type=str, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--log-dir", | ||
type=str, | ||
required=True, | ||
) | ||
parsed = args.parse_args() | ||
parsed.target = tvm.target.Target(parsed.target) | ||
parsed.rpc_workers = RPCConfig( | ||
tracker_host=parsed.rpc_host, | ||
tracker_port=parsed.rpc_port, | ||
tracker_key=parsed.rpc_key, | ||
session_timeout_sec=30, | ||
).count_num_servers(allow_missing=True) | ||
return parsed | ||
|
||
|
||
ARGS = _parse_args() | ||
|
||
|
||
def main(): | ||
log_file = os.path.join(ARGS.log_dir, f"{ARGS.workload}.json") | ||
workload_func, params = CONFIGS[ARGS.workload] | ||
params = params[0] # type: ignore | ||
workload_func = auto_scheduler.register_workload(workload_func) | ||
|
||
if ARGS.target.kind.name == "llvm": | ||
hardware_params = auto_scheduler.HardwareParams( | ||
num_cores=int(ARGS.target.attrs["num-cores"]), | ||
target=ARGS.target, | ||
) | ||
elif ARGS.target.kind.name == "cuda": | ||
hardware_params = auto_scheduler.HardwareParams( | ||
num_cores=-1, | ||
vector_unit_bytes=16, | ||
cache_line_bytes=64, | ||
max_shared_memory_per_block=int(ARGS.target.attrs["max_shared_memory_per_block"]), | ||
max_threads_per_block=int(ARGS.target.attrs["max_threads_per_block"]), | ||
max_vthread_extent=8, | ||
warp_size=32, | ||
) | ||
else: | ||
raise NotImplementedError(f"Unsupported target {ARGS.target}") | ||
task = auto_scheduler.SearchTask( | ||
func=workload_func, | ||
args=params, | ||
target=ARGS.target, | ||
hardware_params=hardware_params, | ||
) | ||
runner = auto_scheduler.RPCRunner( | ||
key=ARGS.rpc_key, | ||
host=ARGS.rpc_host, | ||
port=ARGS.rpc_port, | ||
n_parallel=ARGS.rpc_workers, | ||
number=3, | ||
repeat=1, | ||
min_repeat_ms=100, | ||
enable_cpu_cache_flush=False, | ||
) | ||
|
||
# Inspect the computational graph | ||
print("Computational DAG:") | ||
print(task.compute_dag) | ||
tune_option = auto_scheduler.TuningOptions( | ||
num_measure_trials=ARGS.num_trials, | ||
measure_callbacks=[auto_scheduler.RecordToFile(log_file)], | ||
verbose=2, | ||
runner=runner, | ||
) | ||
print("Running AutoTuning:") | ||
task.tune(tune_option) | ||
print("History Best:") | ||
print(task.print_best(log_file)) | ||
sch, args = task.apply_best(log_file) | ||
print("Lowered TIR:") | ||
print(tvm.lower(sch, args, simple_mode=True)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
120 changes: 120 additions & 0 deletions
120
python/tvm/meta_schedule/testing/run_subgraph_meta_schedule.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# 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. | ||
# pylint: disable=missing-docstring | ||
import argparse | ||
import logging | ||
from os import cpu_count | ||
from typing import Optional | ||
|
||
import tvm | ||
from tvm import meta_schedule as ms | ||
from tvm import tir | ||
from tvm.meta_schedule.testing.te_workload import create_te_workload | ||
|
||
|
||
def _parse_args(): | ||
args = argparse.ArgumentParser() | ||
args.add_argument( | ||
"--workload", | ||
type=str, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--target", | ||
type=str, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--num-trials", | ||
type=int, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--work-dir", | ||
type=str, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--rpc-host", | ||
type=str, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--rpc-port", | ||
type=int, | ||
required=True, | ||
) | ||
args.add_argument( | ||
"--rpc-key", | ||
type=str, | ||
required=True, | ||
) | ||
parsed = args.parse_args() | ||
parsed.target = tvm.target.Target(parsed.target) | ||
if parsed.target.attrs.get("mtriple", None) == "aarch64-linux-gnu": | ||
parsed.alloc_repeat = 3 | ||
else: | ||
parsed.alloc_repeat = 1 | ||
parsed.rpc_config = ms.runner.RPCConfig( | ||
tracker_host=parsed.rpc_host, | ||
tracker_port=parsed.rpc_port, | ||
tracker_key=parsed.rpc_key, | ||
session_timeout_sec=30, | ||
) | ||
parsed.rpc_workers = parsed.rpc_config.count_num_servers(allow_missing=False) | ||
return parsed | ||
|
||
|
||
logging.basicConfig() | ||
logging.getLogger("tvm.meta_schedule").setLevel(logging.DEBUG) | ||
ARGS = _parse_args() | ||
|
||
|
||
def main(): | ||
runner = ms.runner.RPCRunner( | ||
rpc_config=ARGS.rpc_config, | ||
evaluator_config=ms.runner.EvaluatorConfig( | ||
number=3, | ||
repeat=1, | ||
min_repeat_ms=100, | ||
enable_cpu_cache_flush=False, | ||
), | ||
alloc_repeat=ARGS.alloc_repeat, | ||
max_workers=ARGS.rpc_workers, | ||
) | ||
sch: Optional[tir.Schedule] = ms.tune_tir( | ||
mod=create_te_workload(ARGS.workload, 0), | ||
target=ARGS.target, | ||
config=ms.EvolutionarySearchConfig( | ||
num_trials_per_iter=64, | ||
num_trials_total=ARGS.num_trials, | ||
init_min_unmeasured=50, | ||
), | ||
runner=runner, # type: ignore | ||
task_name=ARGS.workload, | ||
work_dir=ARGS.work_dir, | ||
num_threads=cpu_count(), | ||
) | ||
if sch is None: | ||
print("No valid schedule found!") | ||
else: | ||
print(sch.mod.script()) | ||
print(sch.trace) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.