NVIDIA / NVIDIA/cuda-python

[PERF]: Handle generic sequences more efficiently

Open
#1,640 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

cuda.bindings P1 performance
Dominant language
Cython
Stars
3.4k
Forks
329
Avg merge
1d 23h
Merged PRs (30d)
116

Description

In driver, runtime and nvrtc, most of the sequence arguments are of a type like Optional[tuple[cuuint64_t] | list[cuuint64_t]].

They are then converted to C arrays using code like:

for idx in range(elementStridesLen):
            elementStridesStatic[idx] = <cydriver.cuuint32_t>(<cuuint32_t?> elementStrides[idx])._pvt_ptr[0]

elementStrides[idx] here uses the generic PySequence_GetItem, which first must check whether the sequence is a list or tuple before getting the item. This check is performed repeatedly for each item, even though the type of the sequence remains constant. The branch predictor may or may not be able to smooth this out.

Ideally, we would use the PySequence_Fast family of functions. Unfortunately, Cython's wrappers of low-level Python/C API functions involving borrowing are broken -- we would probably need to revert to raw C snippets to make that work.

Alternatively, we could do something like:

if type(seq) is list:
    for idx in range(len(seq)):
        # use cpython.PyList_GetItemInt...
elif type(seq) is tuple:
    for idx in range(len(seq)):
        # use cpython.PyTuple_GetItemInt...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the generic sequence-conversion loops in the driver, runtime, and nvrtc areas. Read the Cython wrappers around the sequence access and the documented PySequence_Fast APIs, then compare the possible access paths described in the issue. Done means the affected conversions use an efficient supported approach without changing their behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.