Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Port to Cython 3.0#8457

Merged
kmaehashi merged 16 commits into
cupy:mainfrom
littlewu2508:cython-3.0
Feb 14, 2025
Merged

Port to Cython 3.0#8457
kmaehashi merged 16 commits into
cupy:mainfrom
littlewu2508:cython-3.0

Conversation

@littlewu2508

Copy link
Copy Markdown
Contributor

These should be the necessary changes for Cython-3:

  1. Resolve some compilation errors
  2. Enable a backward compatible mode of Cython: special binary operator set to True.

I applied these 2 patches on top of #8319, and built cupy-13.2.0 on ROCm 6. My cupy application run without errors.

I also observed one deprecation warning regarding Cython, which is currently not a blocker but may become one in the future:

The 'IF' statement is deprecated and will be removed in a future Cython version. Consider using runtime conditions or C macros instead. See cython/cython#4310

@leofang

leofang commented Aug 8, 2024

Copy link
Copy Markdown
Member

Thanks for your help!

I also observed one deprecation warning regarding Cython, which is currently not a blocker but may become one in the future:

The 'IF' statement is deprecated and will be removed in a future Cython version. Consider using runtime conditions or C macros instead. See cython/cython#4310

My suggestion is to use the compiler flag added in cython/cython#6243 to silence the build-time warning for the time being. IIRC (though I can't find the thread where this was discussed...) we wanted to investigate some lightweight codegen to address this, but I dunno if it'd be prioritized any time soon. The deprecation of IF/DEF was well aware and tracked in #4610.

@leofang

leofang commented Aug 9, 2024

Copy link
Copy Markdown
Member

My suggestion is to use the compiler flag added in cython/cython#6243

It seems the flag has not made it to the 3.0.x releases...

@littlewu2508

Copy link
Copy Markdown
Contributor Author

I meet an issue after upgrading to cython3: cupy.cuda.thrust.available is False, although cupy_thrust.cu and thrust.cpp are compiled without issue.

@littlewu2508

Copy link
Copy Markdown
Contributor Author

I meet an issue after upgrading to cython3: cupy.cuda.thrust.available is False, although cupy_thrust.cu and thrust.cpp are compiled without issue.

It is caused by ImportError: thrust.cpython-311-x86_64-linux-gnu.so: undefined symbol: cupy_free

@littlewu2508

littlewu2508 commented Oct 19, 2024

Copy link
Copy Markdown
Contributor Author

Turns out

cdef public void cupy_free(void *m, char* ptr) with gil:
is causing the problem. The cython 0.29 behavior is considered as a bug, as cython/cython#1839 documented. Should use cdef public externc to generate extern "C"; Cython didn't implement the cdef public externc, so I will try just use extern "C++" in cupy_thrust.cu

littlewu2508 added a commit to littlewu2508/cupy that referenced this pull request Oct 19, 2024
littlewu2508 added a commit to littlewu2508/cupy that referenced this pull request Oct 19, 2024
@EarlMilktea

Copy link
Copy Markdown
Contributor

MEMO: Encountering many performance warnings like this:

performance hint: cupy_backends/cuda/api/driver.pyx:122:32: Exception check after calling 'cuCtxGetCurrent' will always require the GIL to be acquired. Declare 'cuCtxGetCurrent' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions.

cpdef intptr_t ctxGetCurrent() except? 0:
    initialize()
    cdef Context ctx
    with nogil:
        status = cuCtxGetCurrent(&ctx) # <- HERE!
    check_status(status)
    return <intptr_t>ctx

@kmaehashi

kmaehashi commented Nov 5, 2024

Copy link
Copy Markdown
Member

Exception check after calling 'cuCtxGetCurrent' will always require the GIL to be acquired. Declare 'cuCtxGetCurrent' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions.

It seems invocations of function pointers (assigned to variables) are emitting this warning. I'm curious if anything has changed in Cython 3.0, or there was a slient overhead even in Cython 0.29. Can you check with this @EarlMilktea?

@EarlMilktea

EarlMilktea commented Nov 6, 2024

Copy link
Copy Markdown
Contributor

Fortunately it seems that we can easily resolve most of the warnings by just replacing nogil by noexcept nogil in

  • cupy_backends/cuda/libs/_cnvrtc.pxi
  • cupy_backends/cuda/api/_driver_extern.pxi

(Check EarlMilktea@3c1a3bd for more details).

Of course we should check this replacement is valid, but I feel it's OK as these files just wrap the low-level CUDA APIs, which are also available in C codes...

@mergify

mergify Bot commented Dec 17, 2024

Copy link
Copy Markdown
Contributor

This pull request is now in conflicts. Could you fix it @littlewu2508? 🙏

littlewu2508 added a commit to littlewu2508/cupy that referenced this pull request Dec 17, 2024
cupy/_core/_kernel.pyx:1159:24: '__module__' redeclared
Fixed by removing "readonly object __module__"

cupy/_core/dlpack.pyx:152:25: Cannot assign type 'void (DLManagedTensor *) except * nogil' to 'void (*)(DLManagedTensor *) noexcept'. Exception values are incompatible. Suggest adding 'noexcept' to the type of 'deleter'.
cupy/_core/dlpack.pyx:154:57: Cannot assign type 'void (object) except *' to 'PyCapsule_Destructor' (alias of 'void (*)(object) noexcept'). Exception values are incompatible. Suggest adding 'noexcept' to the type of 'pycapsule_deleter'.
Fixed by adding noexcept to those functions

Closes: cupy#4610
Signed-off-by: Yiyang Wu <[email protected]>
Compatible with Cython-0.x

Bug: cupy#5893
Signed-off-by: Yiyang Wu <[email protected]>
@leofang

leofang commented Dec 29, 2024

Copy link
Copy Markdown
Member

FYI @kmaehashi we noticed (conda-forge/cupy-feedstock#295 (comment)) that this PR blocks the Python 3.13 support (#8651) even for the regular (with-GIL) builds, because Cython 0.29.x simply has no wheel or conda package released for 3.13.

@jakirkham jakirkham mentioned this pull request Jan 10, 2025
10 tasks
@kmaehashi kmaehashi added the to-be-backported Pull-requests to be backported to stable branch label Jan 10, 2025
`if module['name'] == 'numpy_allocator':` pass was never used
Azusachan added a commit to Azusachan/python-cupy-rocm that referenced this pull request Feb 11, 2025
Due to cupy/cupy#8457 can't be easily backported to an ancient commit, change cupy codebase to v13 tree.
@kmaehashi

Copy link
Copy Markdown
Member

/test mini

kmaehashi
kmaehashi previously approved these changes Feb 13, 2025
@kmaehashi

kmaehashi commented Feb 13, 2025

Copy link
Copy Markdown
Member

Here is a quote of the python ../cupy-performance/prof.py --plot ../cupy-performance/benchmarks/bench_ufunc_cupy.py result (code). There seems additional 5-10% CPU overhead 🤔

commit 8c41181 + Cython 0.29.37:

time_add             - case        100:    CPU:    18.558 us   +/-  0.672 (min:    18.086 / max:    22.357) us     GPU-0:    22.262 us   +/-  0.999 (min:    21.504 / max:    26.624) us
time_add             - case        200:    CPU:    19.059 us   +/-  0.809 (min:    18.237 / max:    22.413) us     GPU-0:    22.855 us   +/-  1.033 (min:    21.504 / max:    26.624) us
time_add             - case       5000:    CPU:    18.831 us   +/-  0.822 (min:    18.114 / max:    23.521) us     GPU-0:   619.763 us   +/-  2.899 (min:   615.424 / max:   628.736) us
time_add             - case      10000:    CPU:    19.864 us   +/-  0.698 (min:    18.973 / max:    23.287) us     GPU-0:  2396.750 us   +/-  5.202 (min:  2386.944 / max:  2408.448) us

commit 8c41181 + 6db06e4 (this PR) + Cython 3.0.12:

time_add             - case        100:    CPU:    20.164 us   +/-  0.266 (min:    19.746 / max:    21.094) us     GPU-0:    23.799 us   +/-  0.601 (min:    22.528 / max:    25.600) us
time_add             - case        200:    CPU:    21.090 us   +/-  2.221 (min:    19.798 / max:    31.464) us     GPU-0:    24.863 us   +/-  2.412 (min:    22.528 / max:    35.840) us
time_add             - case       5000:    CPU:    20.638 us   +/-  1.296 (min:    19.804 / max:    27.575) us     GPU-0:   623.083 us   +/-  3.222 (min:   614.400 / max:   629.760) us
time_add             - case      10000:    CPU:    21.175 us   +/-  0.769 (min:    20.165 / max:    25.300) us     GPU-0:  2391.365 us   +/-  5.118 (min:  2379.776 / max:  2404.352) us

@kmaehashi

Copy link
Copy Markdown
Member

I think we need to add two Cython compiler directives.

  • Several tests are failing because binding=True is now default, and we are assigning functions (e.g. cupy.cuda._malloc) as a class attribute of TestCase classes. Setting binding=False reverts the behavior to Cython 0.29.x. We can revisit this in future, but I'd keep the old behavior for now as this might break existing user's code, and this PR is planned to be backported to v13.
  • The performance issue seems to be caused because noexcept is no longer assumed by default. Setting legacy_implicit_noexcept=True reverts the behavior to Cython 0.29.x. Once we add noexcept annotation everywhere we can remove this option.

After adding these two options, I get the performance close to the current main branch:

time_add             - case        100:    CPU:    18.898 us   +/-  1.716 (min:    18.395 / max:    30.849) us     GPU-0:    22.093 us   +/-  1.906 (min:    21.504 / max:    34.816) us
time_add             - case        200:    CPU:    19.121 us   +/-  0.589 (min:    18.500 / max:    20.329) us     GPU-0:    22.461 us   +/-  1.034 (min:    21.472 / max:    26.688) us
time_add             - case       5000:    CPU:    18.955 us   +/-  0.888 (min:    18.297 / max:    24.032) us     GPU-0:   621.585 us   +/-  3.045 (min:   617.472 / max:   629.760) us
time_add             - case      10000:    CPU:    20.077 us   +/-  0.656 (min:    19.243 / max:    23.514) us     GPU-0:  2389.018 us   +/-  4.630 (min:  2379.776 / max:  2398.272) us

@mergify

This comment was marked as outdated.

@kmaehashi

Copy link
Copy Markdown
Member

/test mini

@kmaehashi kmaehashi merged commit 1f9c9d4 into cupy:main Feb 14, 2025
@chainer-ci

Copy link
Copy Markdown
Member

@kmaehashi Failed to backport automatically.


Not cleanly cherry-picked

@kmaehashi

kmaehashi commented Feb 14, 2025

Copy link
Copy Markdown
Member

Tests passed, merging! Thanks again, @littlewu2508 and @EarlMilktea!

@kmaehashi

Copy link
Copy Markdown
Member

I'll take care of backport after merging #8951

@jakirkham

Copy link
Copy Markdown
Member

Wow! This is fantastic 🤩

Thank you all for your hard work on Cython 3 compatibility 👏

kmaehashi added a commit to kmaehashi/cupy that referenced this pull request Feb 15, 2025
@kmaehashi kmaehashi added the no-compat Disrupts backward compatibility label Feb 15, 2025
kmaehashi added a commit to kmaehashi/cupy that referenced this pull request Feb 22, 2025
kmaehashi added a commit to kmaehashi/cupy that referenced this pull request Feb 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:enhancement Improvements to existing features no-compat Disrupts backward compatibility prio:high to-be-backported Pull-requests to be backported to stable branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants