Skip to content

Device properties example #474

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

Merged
merged 4 commits into from
Feb 27, 2025
Merged

Device properties example #474

merged 4 commits into from
Feb 27, 2025

Conversation

samaid
Copy link
Contributor

@samaid samaid commented Feb 26, 2025

This example aims to get familiar with cuda.core device and device properties concepts. The script iterates across all devices in the system and prints their device properties.
log.txt

Copy link
Contributor

copy-pr-bot bot commented Feb 26, 2025

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@samaid samaid closed this Feb 26, 2025
@samaid samaid reopened this Feb 26, 2025
@leofang leofang requested a review from rwgk February 26, 2025 22:49
@leofang leofang added enhancement Any code-related improvements P1 Medium priority - Should do cuda.core Everything related to the cuda.core module labels Feb 26, 2025
@leofang leofang added this to the cuda.core beta 3 milestone Feb 26, 2025
Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

What do you think about renaming to show_device_properties.py?

For quicker aha when people see the filename.

from cuda.core.experimental import Device, system


def _bool_str(value: bool) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

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

_yes_or_no (or just _yes_no works, too)

makes this self-explanatory (in the call sites).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, will change both filename and function to _yes_no

return "YES" if value else "NO"


def _bytes_to_mbytes(value):
Copy link
Collaborator

Choose a reason for hiding this comment

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

def _bytes_to_mbytes(value):
    return f"{value / (1024 ** 2):.2f}MB"

def _bytes_to_gbytes(value):
    return f"{value / (1024 ** 3):.2f}GB"

These were generated by ChatGPT: https://chatgpt.com/share/67bfde2c-7b20-8008-a099-302d0ed4dc60

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I am an old schooler. I used Windows calculator to calculate magic numbers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, it's more readable but my guts protest to use expensive power and division ops

Copy link
Collaborator

Choose a reason for hiding this comment

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

(Today I started experimenting with claude.ai)

https://claude.ai/share/5830e427-4b3b-44f5-8bef-c47d10ebe01f

I'm slightly modifying what it suggests:

BYTES_TO_GBYTES = 1 / (1024 ** 3)

def _bytes_to_gbytes(value):
    return f"{value * BYTES_TO_GBYTES:.2f}GB"

Making our examples readable seems valuable.

print(f"- Constant memory available: {properties.total_constant_memory}B")
print(f"- Support for unified address space with host: {_bool_str(properties.unified_addressing)}")
print(f"- Support for virtual memory management: {_bool_str(properties.virtual_memory_management_supported)}")

Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional, but I'd lean towards adding a print() here, so that the output doesn't run directly into the command line prompt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I double checked log.txt. The last few lines in the output will be:

...
- Support for unified address space with host: YES
- Support for virtual memory management: YES
*****************************************************

Two blank lines are printed before command line appears. See line 169

print(f"DEVICE {device.name} (id={device_id})")

device.set_current()
ctx = device.context
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems to be unused. (Hm, not sure why ruff doesn't catch this.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, not sure why ruff doesn't catch this.

Oh! Because it's currently a module-level variable (which could in theory be used from elsewhere). Another reason to move this code into a function. I think ruff will then flag this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have initiated a separate discussion on set_current(). I wanted to add some meaningful example for it but it appears that context management is not yet fully implemented. Or am I wrong? I am happy to extend example with context stuff

Copy link
Collaborator

Choose a reason for hiding this comment

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

I upvoted your suggestion (yesterday).

it appears that context management is not yet fully implemented.

I had to look this up myself: #189

What do you think about adding a comment (very rough, just to give the idea):

# Extend example to show device context information after #189 is resolved.
@rwgk rwgk mentioned this pull request Feb 27, 2025
@samaid
Copy link
Contributor Author

samaid commented Feb 27, 2025

@rwgk Your suggestions have been addressed

@rwgk
Copy link
Collaborator

rwgk commented Feb 27, 2025

/ok to test

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

Looks great to me!

We trigger CI testing via /ok to test comments. I think it works for anyone, but I just did that right now to get it going.

@leofang
Copy link
Member

leofang commented Feb 27, 2025

We trigger CI testing via /ok to test comments. I think it works for anyone,

It works for all GitHub IDs linked with a valid NVIDIA email address, so Sergey you should be able to trigger it yourself!

This comment has been minimized.

@rwgk
Copy link
Collaborator

rwgk commented Feb 27, 2025

(I tried to look at the log of the one failing test but it doesn't load. I'll try again later.)

@leofang
Copy link
Member

leofang commented Feb 27, 2025

It was a transient network issue, I reran the failed job and all is good now. Merging, thanks Sergey & Ralf!

@leofang leofang merged commit fc8be1c into NVIDIA:main Feb 27, 2025
74 checks passed
Copy link

Doc Preview CI
Preview removed because the pull request was closed or merged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cuda.core Everything related to the cuda.core module enhancement Any code-related improvements P1 Medium priority - Should do
3 participants