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

[RAY AIR] set the correct gpu id in TorchTrainer #26493

Merged
merged 15 commits into from
Jul 19, 2022
4 changes: 4 additions & 0 deletions python/ray/train/tests/test_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ def test_tune_fashion_mnist_gpu(ray_start_4_cpus_2_gpus):
torch_fashion_mnist(num_workers=2, use_gpu=True, num_samples=1)


def test_concurrent_tune_fashion_mnist_gpu(ray_start_4_cpus_2_gpus):
torch_fashion_mnist(num_workers=1, use_gpu=True, num_samples=2)


def test_tune_tensorflow_mnist_gpu(ray_start_4_cpus_2_gpus):
tune_tensorflow_mnist(num_workers=2, use_gpu=True, num_samples=1)

Expand Down
9 changes: 8 additions & 1 deletion python/ray/train/torch/train_loop_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,15 @@ def get_device(self) -> torch.device:
"""Gets the correct torch device to use for training."""
if torch.cuda.is_available():
gpu_ids = ray.get_gpu_ids()
richardliaw marked this conversation as resolved.
Show resolved Hide resolved

if len(gpu_ids) > 0:
device_id = gpu_ids[0]
gpu_id = gpu_ids[0]
cuda_visible_str = os.environ.get("CUDA_VISIBLE_DEVICES", None)
assert (
cuda_visible_str and cuda_visible_str != "NoDevFiles"
), "CUDA_VISIBLE_DEVICES is not set or empty."
cuda_visible_list = list(map(int, cuda_visible_str.split(",")))
device_id = cuda_visible_list.index(gpu_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this fail if cuda_visible_list is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @amogkam for the review, about to modify the code accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len(gpu_ids) > 0 will ensure this?

else:
# If called on the driver or outside of Ray Train, return the
# 0th device.
Expand Down