Skip to content

Commit

Permalink
[TVMC] Add configuration json files to the Python package (#11063)
Browse files Browse the repository at this point in the history
Add the `configs` directory to be part of the installed version of
TVM in the setuptools configuration, and introduce a new function
to load the `configs` directory from the right paths both when TVM
is locally installed for development, as well as, when it is installed
as a package.
  • Loading branch information
leandron authored Apr 20, 2022
1 parent 0b95780 commit 970f868
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
7 changes: 7 additions & 0 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def get_lib_path():
libs.append(candidate_path)
break

# Add tvmc configuration json files
for name in lib_path:
candidate_path = os.path.abspath(os.path.join(os.path.dirname(name), "..", "configs"))
if os.path.isdir(candidate_path):
libs.append(candidate_path)
break

else:
libs = None

Expand Down
42 changes: 39 additions & 3 deletions python/tvm/driver/tvmc/config_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,46 @@
"""
import os
import json

from tvm._ffi import libinfo
from tvm.driver.tvmc import TVMCException

CONFIGS_JSON_DIR = None


class ConfigsJsonNotFoundError(TVMCException):
"""Raised when the JSON configs dirtree cannot be found."""


def get_configs_json_dir() -> str:
"""Find the 'configs' directory, containing the JSON files used to configure tvmc
with persistent argument settings.
Returns
-------
str :
The path to the 'configs' directory
"""
global CONFIGS_JSON_DIR
if CONFIGS_JSON_DIR is None:
candidate_paths = []
candidate_paths.extend(libinfo.find_lib_path())
# When running from source, the configs directory will be located one directory above the
# native libraries, so covering that case.
candidate_paths.extend(
[os.path.abspath(os.path.join(lib_path, "..")) for lib_path in libinfo.find_lib_path()]
)
for path in candidate_paths:
configs_path = os.path.join(os.path.dirname(path), "configs")
if os.path.isdir(configs_path):
CONFIGS_JSON_DIR = configs_path
break

else:
raise ConfigsJsonNotFoundError()

return CONFIGS_JSON_DIR


def find_json_file(name, path):
"""search for json file given file name a path
Expand Down Expand Up @@ -69,9 +107,7 @@ def read_and_convert_json_into_dict(config_args):
if os.path.isfile(config_args.config):
json_config_file = config_args.config
else:
config_dir = os.path.abspath(
os.path.join(os.path.realpath(__file__), "..", "..", "..", "..", "..", "configs")
)
config_dir = get_configs_json_dir()
json_config_file = find_json_file(config_args.config, config_dir)
return json.load(open(json_config_file, "rb"))

Expand Down

0 comments on commit 970f868

Please sign in to comment.