Skip to content

Move callback wrappers to Python layer - #544

Merged
leofang merged 2 commits into
NVIDIA:mainfrom
vzhurba01:531-move-callback-wrappers
Apr 4, 2025
Merged

Move callback wrappers to Python layer#544
leofang merged 2 commits into
NVIDIA:mainfrom
vzhurba01:531-move-callback-wrappers

Conversation

@vzhurba01

Copy link
Copy Markdown
Contributor

close#531

@vzhurba01vzhurba01 added enhancement Any code-related improvements P0 High priority - Must do! cuda.bindings Everything related to the cuda.bindings module labels Apr 1, 2025
@vzhurba01vzhurba01 self-assigned this Apr 1, 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.

@vzhurba01

Copy link
Copy Markdown
ContributorAuthor

/ok to test

@github-actions

This comment has been minimized.

Comment threadcuda_bindings/cuda/bindings/cyruntime.pyx.in
@vzhurba01
vzhurba01 requested a review from leofangApril 2, 2025 19:46

@leofangleofang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have reviewed 3 out of 4 files (haven't had time to cover runtime.pyx.in yet).

Q: I assume so far we have not supported passing a pure Python function as callback to any of the bindings directly, without using ctypes or other means as a trampoline?

cdef cuAsyncCallbackData *cbData = NULL
cbData = <cuAsyncCallbackData *>malloc(sizeof(cbData[0]))
if cbData == NULL:
return CUresult.CUDA_ERROR_OUT_OF_MEMORY

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Q: Should this be a 1-tuple?

Suggested change
return CUresult.CUDA_ERROR_OUT_OF_MEMORY
return (CUresult.CUDA_ERROR_OUT_OF_MEMORY,)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Good catch. Fixed.

@kkraus14kkraus14Apr 4, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It would be good to have type annotations or something else as a way to prevent issues like this from sneaking in.

@leofangleofangApr 4, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems currently Cython cannot raise a compile-time error for either case, only a run-time error (which is not desired I think).

cpdef tuple f():
return42defg() -> tuple:
return 42

Output:

>>>importtest_tuple>>>test_tuple.f()
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"test_tuple.pyx", line1, intest_tuple.fcpdeftuplef():
File"test_tuple.pyx", line3, intest_tuple.freturn42TypeError: Expectedtuple, gotint>>>test_tuple.g()
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"test_tuple.pyx", line7, intest_tuple.greturn42TypeError: Expectedtuple, gotint

Comment threadcuda_bindings/cuda/bindings/driver.pyx.in Outdated
Comment threadcuda_bindings/cuda/bindings/driver.pyx.in Outdated
@vzhurba01

Copy link
Copy Markdown
ContributorAuthor

Q: I assume so far we have not supported passing a pure Python function as callback to any of the bindings directly, without using ctypes or other means as a trampoline?

Correct.

@vzhurba01

Copy link
Copy Markdown
ContributorAuthor

/ok to test

@vzhurba01vzhurba01 mentioned this pull request Apr 4, 2025

@leofangleofang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@leofang
leofang merged commit 6d07877 into NVIDIA:mainApr 4, 2025
@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

cuda.bindingsEverything related to the cuda.bindings moduleenhancementAny code-related improvementsP0High priority - Must do!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move callback wrappers to the Python layer

3 participants

@vzhurba01@kkraus14@leofang