Skip to content

Commit

Permalink
Avoid runtime exception when file doesn't exist (#2441)
Browse files Browse the repository at this point in the history
* Avoid runtime exception when file doesn't exist

* Update the check based on feedback

* Revert the old fix
  • Loading branch information
sf-wind authored and merrymercy committed Jan 17, 2019
1 parent 2a871f3 commit d0f8366
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions python/tvm/autotvm/tophub.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def context(target, extra_files=None):
for name in possible_names:
name = _alias(name)
if name in all_packages:
check_backend(name)
if not check_backend(name):
continue

filename = "%s_%s.log" % (name, PACKAGE_VERSION[name])
best_context.load(os.path.join(AUTOTVM_TOPHUB_ROOT_PATH, filename))
Expand All @@ -98,23 +99,30 @@ def check_backend(backend):
----------
backend: str
The name of backend.
Returns
----------
success: bool
Whether the check is successful.
"""
backend = _alias(backend)
assert backend in PACKAGE_VERSION, 'Cannot find backend "%s" in TopHub' % backend

version = PACKAGE_VERSION[backend]
package_name = "%s_%s.log" % (backend, version)
if os.path.isfile(os.path.join(AUTOTVM_TOPHUB_ROOT_PATH, package_name)):
return
return True

if sys.version_info >= (3,):
import urllib.request as urllib2
else:
import urllib2
try:
download_package(package_name)
return True
except urllib2.URLError as e:
logging.warning("Failed to download tophub package for %s: %s", backend, e)
return False


def download_package(package_name):
Expand Down

0 comments on commit d0f8366

Please sign in to comment.