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

BUG: disable import cuda error when cpu only in a shared python env #47

Merged
Merged
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
24 changes: 17 additions & 7 deletions python/xoscar/_utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,23 @@ cdef class TypeDispatcher:
cdef _reload_lazy_handlers(self):
for k, v in self._lazy_handlers.items():
mod_name, obj_name = k.rsplit('.', 1)
with warnings.catch_warnings():
# the lazy imported cudf will warn no device found,
# when we set visible device to -1 for CPU processes,
# ignore the warning to not distract users
warnings.simplefilter("ignore")
mod = importlib.import_module(mod_name, __name__)
self.register(getattr(mod, obj_name), v)
imported = False
try:
with warnings.catch_warnings():
# the lazy imported cudf will warn no device found,
# when we set visible device to -1 for CPU processes,
# ignore the warning to not distract users
warnings.simplefilter("ignore")
mod = importlib.import_module(mod_name, __name__)
imported = True
# The reason all exceptions are caught here instead of ImportError is that
# when, for example, a cpu machine tries to import cuda, the exception thrown is CudaAPIError.
except: # pragma: no cover
imported = False
warnings.warn(f'Import {mod_name} failed. Please check your current Python environment.')

if imported:
self.register(getattr(mod, obj_name), v)
self._lazy_handlers = dict()

cpdef get_handler(self, object type_):
Expand Down