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

Update HAS_GPU variable to account for CUDA_VISIBLE_DEVICES #221

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
33 changes: 25 additions & 8 deletions merlin/core/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,37 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os

try:
from numba import cuda # pylint: disable=unused-import
except ImportError:
cuda = None

from dask.distributed.diagnostics import nvml

# Using the `dask.distributed.diagnostics.nvml.device_get_count`
# helper function from dask to check device counts with NVML
# since this handles some complexity of checking NVML state for us.

# Note: We can't use `numba.cuda.gpus`, since this has some side effects
# that are incompatible with Dask-CUDA. If CUDA runtime functions are
# called before Dask-CUDA can spawn worker processes
# then Dask-CUDA it will not work correctly (raises an exception)
def _get_gpu_count():
"""Get Number of GPU devices accounting for CUDA_VISIBLE_DEVICES environment variable"""
# Using the `dask.distributed.diagnostics.nvml.device_get_count`
# helper function from dask to check device counts with NVML
# since this handles some complexity of checking NVML state for us.

# Note: We can't use `numba.cuda.gpus`, since this has some side effects
# that are incompatible with Dask-CUDA. If CUDA runtime functions are
# called before Dask-CUDA can spawn worker processes
# then Dask-CUDA it will not work correctly (raises an exception)
nvml_device_count = nvml.device_get_count()
if nvml_device_count == 0:
return 0
try:
cuda_visible_devices = os.environ["CUDA_VISIBLE_DEVICES"]
if cuda_visible_devices:
return len(cuda_visible_devices.split(","))
else:
return 0
except KeyError:
return nvml_device_count


HAS_GPU = nvml.device_get_count() > 0
HAS_GPU = _get_gpu_count() > 0