FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Statically-allocate small arrays of numbers by mdboom · Pull Request #1545 · NVIDIA/cuda-python · GitHub

Statically-allocate small arrays of numbers - #1545

Merged
mdboom merged 2 commits into
NVIDIA:mainfrom
mdboom:issue659-2
Feb 5, 2026
Merged

Statically-allocate small arrays of numbers#1545
mdboom merged 2 commits into
NVIDIA:mainfrom
mdboom:issue659-2

Conversation

mdboom commented Jan 30, 2026

Copy link
Copy Markdown
Contributor

See #659 for a discussion of this solution.

There weren't as many instances of this pattern as I had hoped, but nonetheless, for functions where it is used, we should see ~25% reduction in Python-to-C overhead.

Keeping this as a draft for testing while the generator changes get reviewed.

copy-pr-bot Bot commented Jan 30, 2026

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

mdboom commented Jan 30, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test

Copilot AI 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Pull request overview

This PR implements a performance optimization to reduce Python-to-C overhead in the CUDA bindings by statically allocating small arrays (≤5 elements) instead of using dynamic allocation. This addresses the performance gap reported in issue #659, where CUDA API calls through cuda-bindings were observed to be 3x slower than direct CUDA C++ API calls.

Changes:

  • Replaced dynamic memory allocation (calloc/free) with static stack allocation for array parameters with 5 or fewer elements in tensor map encoding functions
  • Removed explicit type validation checks that were previously performed before array processing
  • Simplified memory management by eliminating the need for explicit free() calls for small arrays

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +49981 to +49994
cdef cydriver.cuuint32_t* cyelementStrides
cdef size_t elementStridesLen
cdef cydriver.cuuint32_t[5] elementStridesStatic
elementStridesLen = 0 if elementStrides is None else len(elementStrides)
if elementStridesLen == 0:
cyelementStrides = NULL
elif elementStridesLen == 1:
cyelementStrides = <cydriver.cuuint32_t *>(<cuuint32_t?> elementStrides[0])._pvt_ptr
elif elementStridesLen <= 5:
for idx in range(elementStridesLen):
elementStridesStatic[idx] = <cydriver.cuuint32_t>(<cuuint32_t?> elementStrides[idx])._pvt_ptr[0]
cyelementStrides = elementStridesStatic
else:
raise ValueError("Argument 'elementStrides' too long, must be <= 5")

Copilot AI Jan 30, 2026

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The removal of type validation for the elementStrides parameter changes the error handling behavior. The old code explicitly checked that all elements were cuuint32_t instances and raised a clear TypeError. The new code relies on Cython's type casting, which may produce less clear error messages if invalid types are passed. Consider adding explicit type validation before the casting logic to maintain clear error messages for users.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good catch. I think that the Cython checking is sufficient -- it will raise an exception in the same cases as before, but is faster and non-duplicative.

This comment has been minimized.

mdboom marked this pull request as ready for review February 4, 2026 19:42

copy-pr-bot Bot commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for ready for review pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

mdboom enabled auto-merge (squash) February 4, 2026 19:42

mdboom commented Feb 4, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test

kkraus14 added cuda.bindings Everything related to the cuda.bindings module to-be-backported Trigger the bot to raise a backport PR upon merge labels Feb 5, 2026
mdboom merged commit a35cdd3 into NVIDIA:main Feb 5, 2026
171 of 173 checks passed

github-actions Bot commented Feb 5, 2026

Copy link
Copy Markdown

Successfully created backport PR for 12.9.x:

github-actions Bot commented Feb 5, 2026

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

kkraus14 pushed a commit that referenced this pull request Feb 5, 2026
(cherry picked from commit a35cdd3)

Co-authored-by: Michael Droettboom <mdboom@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.bindings Everything related to the cuda.bindings module to-be-backported Trigger the bot to raise a backport PR upon merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL