Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task extraction & caching #27

Merged
merged 5 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion python/tvm/meta_schedule/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
from tvm._ffi import register_object
from tvm.ir import IRModule, transform
from tvm.meta_schedule.database.database import Database
from tvm.relay import Any, Function as RelayFunc, vm
from tvm.relay import Any
from tvm.relay import Function as RelayFunc
from tvm.relay import vm
from tvm.runtime import NDArray, Object
from tvm.target import Target
from tvm.tir import PrimFunc
Expand Down
80 changes: 62 additions & 18 deletions python/tvm/meta_schedule/testing/e2e.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import multiprocessing
import os
import pickle
from typing import Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple

import tvm
import tvm.relay.testing
from tvm import relay
from tvm.ir import IRModule
from tvm.meta_schedule.integration import ExtractedTask, extract_task_from_relay
from tvm.runtime import NDArray, load_param_dict, save_param_dict
from tvm.target import Target

SUPPORTED = [
# TorchVision
Expand Down Expand Up @@ -166,30 +168,72 @@ def _get_network(
return mod, params_bytearray, inputs


def _load_cache(cache_dir: Optional[str], filename: str) -> Optional[List[Any]]:
if cache_dir is None:
return None
path = os.path.join(os.path.expanduser(cache_dir), filename)
if not os.path.exists(path):
return None
print(f"Load from cache: {path}")
with open(path, "rb") as i_f:
return pickle.load(i_f)


def _save_cache(cache_dir: Optional[str], filename: str, objects: List[Any]) -> None:
if cache_dir is None:
return
path = os.path.join(os.path.expanduser(cache_dir), filename)
with open(path, "wb") as o_f:
pickle.dump(objects, o_f)


def get_network(
name: str,
input_shape: List[int],
*,
cache_dir: Optional[str] = None,
) -> Tuple[IRModule, Dict[str, NDArray], Tuple[str, List[int], str]]:
mod: IRModule
params_bytearray: bytearray
params: Dict[str, NDArray]
inputs: Tuple[str, List[int], str]
keyword = f'{name}-{",".join(str(i) for i in input_shape)}.json'
if cache_dir is not None:
path = os.path.join(cache_dir, keyword)
if os.path.exists(path):
print(f"Load cached network file: {path}")
with open(path, "rb") as i_f:
mod, params_bytearray, inputs = pickle.load(i_f)
params = load_param_dict(params_bytearray)
return mod, params, inputs
with multiprocessing.Pool(processes=1) as pool:
result = pool.map(_get_network, [(name, input_shape)])
params_bytearray: bytearray

filename = f'{name}-{",".join(str(i) for i in input_shape)}.json'
cached = _load_cache(cache_dir, filename)
if cached is None:
with multiprocessing.Pool(processes=1) as pool:
result = pool.map(_get_network, [(name, input_shape)])
((mod, params_bytearray, inputs),) = result
params = load_param_dict(params_bytearray)
if cache_dir is not None:
path = os.path.join(cache_dir, keyword)
with open(path, "wb") as o_f:
pickle.dump((mod, params_bytearray, inputs), o_f)
cached = [mod, params_bytearray, inputs]
_save_cache(cache_dir, filename, cached)
mod, params_bytearray, inputs = cached
params = load_param_dict(params_bytearray)
return mod, params, inputs


def extract(
filename: str,
mod: IRModule,
target: Target,
params: Optional[Dict[str, NDArray]] = None,
*,
cache_dir: Optional[str] = None,
opt_level: int = 3,
pass_config: Dict[str, Any] = {
"relay.backend.use_meta_schedule": True,
},
disabled_pass: List[str] = [],
) -> List[ExtractedTask]:
extracted_tasks = _load_cache(cache_dir, filename)
if extracted_tasks is None:
extracted_tasks = extract_task_from_relay(
mod=mod,
target=target,
params=params,
opt_level=opt_level,
pass_config=pass_config,
disabled_pass=disabled_pass,
)
extracted_tasks = list(extracted_tasks)
_save_cache(cache_dir, filename, extracted_tasks)
return extracted_tasks
Loading