Skip to content

Various handle-related changes and improvements - #463

Merged
leofang merged 16 commits into
NVIDIA:mainfrom
leofang:handle
Feb 25, 2025
Merged

Various handle-related changes and improvements#463
leofang merged 16 commits into
NVIDIA:mainfrom
leofang:handle

Conversation

@leofang

@leofangleofang commented Feb 22, 2025

Copy link
Copy Markdown
Member

Close#370. Close#433.

  • Ensure Program/Linker have consistent .backend and .handle
  • Add get_cuda_native_handle() to fetch the underlying CUDA C object from the corresponding cuda.bindings Python-layer object
    • Recall that for all generated bindings there are 3 layers: Python, Cython, and internal. This refers to the Python layer. The Cython layer provides the C type information (i.e. think of it as a CUDA header)
    • Steps:
      1. Take the Python wrapper object from <some_cuda_core_object>.handle
      2. Pass it to get_cuda_native_handle(), and the underlying C object is returned (no refcount or lifetime management)
    • Since cuda.core is not yet cythonized, this is currently the best solution for Python/Cython/C++ interoperability. Once we lower to Cython we can also apply a similar treatment to all cuda.core objects.
    • For reviewers of this part: Please focus on these two files (the rest of Cython-related files are direct copy/paste from our existing cuda.bindings infrastructure):
      • tests/cython/test_get_cuda_native_handle.pyx
      • cuda/core/experimental/include/utility.hpp
  • some minor doc build fixes

@leofangleofang added enhancement Any code-related improvements P0 High priority - Must do! cuda.core Everything related to the cuda.core module labels Feb 22, 2025
@leofangleofang added this to the cuda.core beta 3 milestone Feb 22, 2025
@leofangleofang self-assigned this Feb 22, 2025
@copy-pr-bot

Copy link
Copy Markdown
Contributor

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.

@leofangleofang changed the title WIP: Ensure Program/Linker have consistent .backend and .handleVarious handle-related changes and improvementsFeb 24, 2025
@leofang

Copy link
Copy Markdown
MemberAuthor

/ok to test

@leofang
leofang requested review from rwgk and vzhurba01 and removed request for vzhurba01February 24, 2025 01:01
@leofang
leofang marked this pull request as ready for review February 24, 2025 01:03
Comment threadcuda_core/cuda/core/experimental/include/utility.hpp
@github-actions

This comment has been minimized.

@leofangleofang added the breaking Breaking changes are introduced label Feb 24, 2025
@leofang

Copy link
Copy Markdown
MemberAuthor

/ok to test

1 similar comment
@leofang

Copy link
Copy Markdown
MemberAuthor

/ok to test

@leofang

Copy link
Copy Markdown
MemberAuthor

/ok to test

@leofang

Copy link
Copy Markdown
MemberAuthor

/ok to test

@rwgkrwgk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I only have a few minor comments. I didn't see anything troubling, except the raise vs pass question maybe.

Comment threadcuda_core/cuda/core/experimental/include/utility.hpp Outdated
Comment thread.github/workflows/test-wheel-linux.yml Outdated
Comment threadcuda_core/cuda/core/experimental/_event.py Outdated
Comment threadcuda_core/cuda/core/experimental/_linker.py Outdated
Comment threadcuda_core/cuda/core/experimental/_program.py Outdated
Comment threadcuda_core/cuda/core/experimental/include/utility.hpp
@rwgk

rwgk commented Feb 24, 2025

Copy link
Copy Markdown
Contributor

I only have a few minor comments. I didn't see anything troubling, except the raise vs pass question maybe.

Oh wait, did the except ImportError: raise disappear in the meantime? Please disregard.

rwgk
rwgk previously approved these changes Feb 24, 2025
item = py_func(item)
setattr(sys.modules[__name__], name, item)
except ImportError:
raise

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here it is, I accidentally lost my original comment.

Did you mean to write pass here?

Also, it's safer to make the try-except scope as narrow as possible, e.g. (untested):

 try:
# For each callable in `mod` with name `test_*`,
# wrap the callable in a plain Python function
# and set the result as an attribute of this module.
mod = importlib.import_module(mod)
except ImportError:
pass
else:
for name in dir(mod):
item = getattr(mod, name)
if callable(item) and name.startswith("test_"):
item = py_func(item)
setattr(sys.modules[__name__], name, item)

If there is any tooling that might import this file for analysis: Moving the for loop into a function and using if __name__ == "__main__": could guard against surprises.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

This file is copied/pasted from cuda-bindings. I think Vlad (or @shwina? 🙂) was the original author:
https://shwina.github.io/cython-testing/

Did you mean to write pass here?

I suspect the answer was yes (based on Ashwin's blog post) but perhaps it caused some issues, so it was changed to raise? I can see that this is a cheap way to ensure the listed modules must exist and get tested. But then the whole try-except can be removed (or refactored). Perhaps @vzhurba01 can confirm the intention?

In any case, since this is a copy/paste let's change both files in a separate PR.

Comment thread.github/workflows/test-wheel-linux.yml Outdated
@leofang

Copy link
Copy Markdown
MemberAuthor

/ok to test

#!/bin/bash

SCRIPTPATH=$(dirname $(realpath "$0"))
CPLUS_INCLUDE_PATH=$SCRIPTPATH/../../cuda/core/experimental/include:$CUDA_HOME/include:$CPLUS_INCLUDE_PATH cythonize -3 -i $(dirname "$0")/test_*.pyx

@leofangleofangFeb 24, 2025

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Note: This approach left a bug uncaught (fixed in commit 37a6850): Ideally, we want to have a cmdline functionality like PyBind11 that returns the header path, something like python -m cuda.core --include-path. Here, we use the header from the local clone of this repo (hence using $SCRIPTPATH) instead of the installed cuda.core package, meaning we do not check if the header is actually packaged (and it was not before the fix). However, I think the said functionality needs to be done in a separate PR...

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

rwgk
rwgk previously approved these changes Feb 24, 2025
Comment threadcuda_core/cuda/core/experimental/_program.py
@leofang

Copy link
Copy Markdown
MemberAuthor

/ok to test

@rwgk

rwgk commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

/ok to test

@leofang
leofang merged commit 976246a into NVIDIA:mainFeb 25, 2025
@leofang
leofang deleted the handle branch February 25, 2025 13:11
@github-actions

Copy link
Copy Markdown
Doc Preview CI
Preview removed because the pull request was closed or merged.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breakingBreaking changes are introducedcuda.coreEverything related to the cuda.core moduleenhancementAny code-related improvementsP0High priority - Must do!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

revise the handling of backend and handle properties in Program Retain the underlying object types when retrieving the .handle attribute

3 participants

@leofang@rwgk@keenan-simpson