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

enable selecting gpu #1490

Merged
merged 10 commits into from
Jan 8, 2025
24 changes: 16 additions & 8 deletions desc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@
config = {"device": None, "avail_mem": None, "kind": None}


def set_device(kind="cpu"):
brokenhammer marked this conversation as resolved.
Show resolved Hide resolved
def set_device(kind="cpu", gpuid=None):
"""Sets the device to use for computation.

If kind==``'gpu'``, checks available GPUs and selects the one with the most
available memory.
If kind==``'gpu'`` and a gpuid is specified, uses the specified GPU. If
gpuid==``None`` or a wrong GPU id is given, checks available GPUs and selects the
one with the most available memory.
Respects environment variable CUDA_VISIBLE_DEVICES for selecting from multiple
available GPUs

Expand Down Expand Up @@ -127,14 +128,21 @@
set_device(kind="cpu")
return
devices = [dev for dev in devices if dev["index"] in gpu_ids]
for dev in devices:
mem = dev["mem_total"] - dev["mem_used"]
if mem > maxmem:
maxmem = mem
selected_gpu = dev

if gpuid is not None and (str(gpuid) in gpu_ids):
selected_gpu = [dev for dev in devices if dev["index"] == str(gpuid)][0]

Check warning on line 133 in desc/__init__.py

View check run for this annotation

Codecov / codecov/patch

desc/__init__.py#L132-L133

Added lines #L132 - L133 were not covered by tests
YigitElma marked this conversation as resolved.
Show resolved Hide resolved
else:
for dev in devices:
mem = dev["mem_total"] - dev["mem_used"]
dpanici marked this conversation as resolved.
Show resolved Hide resolved
if mem > maxmem:
maxmem = mem
selected_gpu = dev

Check warning on line 139 in desc/__init__.py

View check run for this annotation

Codecov / codecov/patch

desc/__init__.py#L135-L139

Added lines #L135 - L139 were not covered by tests
config["device"] = selected_gpu["type"] + " (id={})".format(
selected_gpu["index"]
)
if gpuid is not None and not (str(gpuid) in gpu_ids):
warnings.warn(colored("Specified gpuid {} not found, falling back to ".format(

Check warning on line 144 in desc/__init__.py

View check run for this annotation

Codecov / codecov/patch

desc/__init__.py#L143-L144

Added lines #L143 - L144 were not covered by tests
str(gpuid)) + config["device"], "yellow"))
config["avail_mem"] = (
selected_gpu["mem_total"] - selected_gpu["mem_used"]
) / 1024 # in GB
Expand Down
Loading